<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import urllib2 
import optparse
import sys
import os

#################################################

def save_entry(g, entry):
    """

    """
    line="\t".join(entry[:6])
    if entry[6]==None:
        entry6=""
    else:
        entry6="\t".join(entry[6])
    line= line + "\t" + entry6 + "\n"
    g.write(line) 
    return(init_entry(entry))

#################################################
def init_entry(entry):
    entry = ["","","","","","",[]] 
    return(entry)


#################################################
def add_info(field, x, entry):
    """
    field = twp letters
    x = line[2:].strip()
    entry = [ID,primary_AC, OS, OG, OX, OH, [OC_1,OC_2,...]] 
    """    
    if field=="ID":
        try:
            if entry[0] != "":
                pass # safe from &gt;1  ID lines
            else:
                entry[0]=x.split()[0]
        except:
            print("Error:\tWrong format of line %s %s in input file! Skipped" %  (field, x)) 
        return(entry)

    if field=="AC":
        try:
            if entry[1] != "":
                pass # Not primary AC!
            else:
                entry[1]=x.split(";")[0] 
        except:
            print("Error:\tWrong format of line %s %s in input file! Skipped" %  (field, x)) 
        return(entry)
        
    if field=="OS":
        try:
            entry[2] = entry[2] + " " + x
        except:
            print("Error:\tWrong format of line %s %s in input file! Skipped" %  (field, x)) 
        return(entry)
        
    if field=="OG":
        try:
            entry[3] = entry[3] + " " + x
        except:
            print("Error:\tWrong format of line %s %s in input file! Skipped" %  (field, x)) 
        return(entry)
        
    if field=="OX":
        try:
            entry[4] = entry[4] + " " + x
        except:
            print("Error:\tWrong format of line %s %s in input file! Skipped" %  (field, x)) 
        return(entry)
        
    if field=="OH":
        try:
            entry[5] = entry[5] + " " + x
        except:
            print("Error:\tWrong format of line %s %s in input file! Skipped" %  (field, x)) 
        return(entry)
        
    if field=="OC":
        try:
            for y in x.split(";"):
                if y != "":
                    entry[6].append(y)
        except:
            print("Error:\tWrong format of line %s %s in input file! Skipped" %  (field, x)) 
        return(entry)
       
    return(entry)


#################################################

if len(sys.argv)==1:
    print("Create taxonomy in Excel format from file in uniprot format")
    print("python uniprot_to_taxonomy.py -h for command line parameters!")
    exit()

parser = optparse.OptionParser()
parser.add_option("-i", "--input", help="Input file in Uniprot format", dest="in_file")
parser.add_option("-o", "--output", help="Output Excel table", dest="out_file", default="result.xls")

options, args = parser.parse_args()
vars().update(vars(options))

if in_file==None:
  print("Input file name missed...")
  print("python uniprot_to_taxonomy.py -h for command line parameters!")
  exit()

try:
    f=open(in_file)
except:
    print("Error\t File %s was not found" %  in_file)
    exit()

title = "\t".join(["ID","primary_AC", "OS", "OG", "OX", "OH", "Taxonomy"]) + "\n"
print("Wait...")

g = open(out_file,'w')
g.write(title)

entry=[]
entry=init_entry(entry)


for x in f:
    if len(x.strip())&lt;2:
        continue
    field = x[:2]
    if field=="//":
        entry=save_entry(g,entry)
        continue
    else:
        add_info(field,x[2:].strip(), entry)

f.close()
g.close()
print "...Done"
</pre></body></html>