#!/usr/bin/env python3
# Downloads the latest pathway data from reactome
# Based on Nanostring's script they put in their requirements
import requests
import logging
import os

logging.basicConfig(level=logging.getLevelName('DEBUG'), format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S')

logging.info("Downloading pathway list")
URL = 'https://reactome.org/download/current/ReactomePathways.txt'
downloaded_file = requests.get(URL)
open('pathways.tsv', 'wb').write(downloaded_file.content)
logging.info("Finished downloading pathway list")

logging.info("Downloading pathway relationship list")
URL = 'https://reactome.org/download/current/ReactomePathwaysRelation.txt'
downloaded_file = requests.get(URL)
open('pathway_relationships.tsv', 'wb').write(downloaded_file.content)
logging.info("Finished downloading pathway relationship list")

logging.info("Downloading pathway-gene map")
URL = 'https://reactome.org/download/current/NCBI2Reactome_All_Levels.txt'
downloaded_file = requests.get(URL)
open('pathway_levels.tsv', 'wb').write(downloaded_file.content)
logging.info("Finished downloading pathway-gene map")

# Obtain geneIds from pathway_levels
gene_sets = {}
i = 0
with open('pathway_levels.tsv') as infile:
    for line in infile:
        split = line.strip().split('\t')
        gene = split[0]
        gene_set_name = split[1]
        gene_set_description = split[3]
        if gene_set_name in gene_sets:
            if gene_sets[gene_set_name][0] != gene_set_description:
                logging.debug('Got a gene set %s with a changed description - original: %s, new: %s', gene_set_name, gene_sets[gene_set_name][0], gene_set_description)
            gene_sets[gene_set_name][0] = gene_set_description
            gene_sets[gene_set_name][1].append(gene)
        else:
            gene_sets[gene_set_name] = [gene_set_description, [gene]]

with open('pathway_genes.tsv', 'w') as outfile:
    for gene_set_name in gene_sets:
        gene_set_description = gene_sets[gene_set_name][0].strip()
        genes = gene_sets[gene_set_name][1]
        sets = "\t".join(list(set(genes)))
        line = f'{gene_set_name}\t"{gene_set_description}"\t{sets}\n'
        outfile.write(line)
os.remove('pathway_levels.tsv')

logging.info("Successfully downloaded and parsed all data")
