#!/usr/bin/env python3
# Downloads the latest gene data from genenames.org
# Unsupported, might break at any moment
import requests
import logging

logging.basicConfig(level=logging.getLevelName('DEBUG'), format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S')

logging.info("Downloading gene data")
URL = 'https://www.genenames.org/cgi-bin/download/custom?col=gd_hgnc_id&col=gd_app_sym&col=gd_app_name&col=gd_pub_refseq_ids&col=md_eg_id&status=Approved&hgnc_dbtag=on&order_by=gd_app_sym_sort&format=text&submit=submit'
downloaded_file = requests.get(URL)
open('genes.tsv', 'wb').write(downloaded_file.content)
logging.info("Finished downloading gene data")

# Remove the first line of the file (header)
# TODO: Not exactly memory efficient
with open('genes.tsv', 'r') as infile:
    data = infile.read().splitlines(True)
with open('genes.tsv', 'w') as outfile:
    outfile.writelines(data[1:])

logging.info("Successfully downloaded and parsed all data")
