#!/usr/bin/env python 

import os
import sys

__description__ = "Parses the taxonomy id file from UniProt for all the species."
__author__ = "Malvika Sharan <malvika.sharan@uni-wuerzburg.de>"
__email__ = "malvika.sharan@uni-wuerzburg.de"

def select_taxids(taxonomy_file, species):
    '''Selects taxonomy ids for the query species'''
    if not str(species) == 'None':
        parse_uniprot_tax_file(species, taxonomy_file)
    else:
        parse_uniprot_tax_file('N=', taxonomy_file)
    
def parse_uniprot_tax_file(species, taxonomy_file):
    '''Parses the taxonomy id file from UniProt for all the species'''
    with open('bin/selected_taxonomy_ids.txt', 'w') as out_fh:
        out_fh.write("Official (scientific) name\tTaxonomy ID\n")
        with open(taxonomy_file, 'r') as in_fh:
            for entry in in_fh:
                if 'N=' in entry:
                    if ',' in species:
                        for each_species in species.split(','):
                            if each_species.lower() in entry.lower():
                                tax_id = entry.split(':')[0].split(' ')[-1]
                                species_name = entry.strip().split('=')[1]
                                out_fh.write("%s\t%s\n" % (species_name, tax_id))
                    else:
                        if species.lower() in entry.lower():
                            tax_id = entry.split(':')[0].split(' ')[-1]
                            species_name = entry.strip().split('=')[1]
                            out_fh.write("%s\t%s\n" % (species_name, tax_id))
  
if __name__ == '__main__':
    taxonomy_file = sys.argv[1]
    species = sys.argv[2]
    setup_analysis_folders(taxonomy_file, species)

@@ -250,16 +250,59 @@
 _taxids(
+species, reference_taxonomy_file, selected_
 taxonomy
@@ -306,25 +306,16 @@
 omy_file
-, species
 ):%0A    '
@@ -429,32 +429,66 @@
 x_file(species, 
+reference_taxonomy_file, selected_
 taxonomy_file)%0A 
@@ -533,16 +533,50 @@
 e('N=', 
+reference_taxonomy_file, selected_
 taxonomy
@@ -623,16 +623,50 @@
 pecies, 
+reference_taxonomy_file, selected_
 taxonomy
@@ -762,13 +762,8 @@
 pen(
-'bin/
 sele
@@ -780,16 +780,12 @@
 omy_
-ids.txt'
+file
 , 'w
@@ -882,16 +882,26 @@
 th open(
+reference_
 taxonomy
@@ -1736,16 +1736,52 @@
 _':%0A    
+species = sys.argv%5B1%5D%0A    reference_
 taxonomy
@@ -1801,22 +1801,37 @@
 rgv%5B
-1
+2
 %5D%0A    s
-pecies
+elected_taxonomy_file
  = s
@@ -1830,33 +1830,33 @@
 file = sys.argv%5B
-2
+3
 %5D%0A    setup_anal
@@ -1868,16 +1868,59 @@
 folders(
+species, reference_taxonomy_file, selected_
 taxonomy
@@ -1928,15 +1928,6 @@
 file
-, species
 )%0A

Update uniprot_taxid.py