Commit 54901447 by Celine Mercier

Fixed a bug in obitaxonomy sometimes triggering an error when adding a

new taxon from a file. Also now checks if taxon already in taxonomy.
parent adbeac96
......@@ -246,37 +246,48 @@ if __name__ == '__main__':
restricting_ancestor = 1
t = seq[options.key_name]
# try :
# taxid = options.taxonomy.findTaxonByName(t)[0]
# except KeyError :
if (resAncInHeader and options.res_anc in seq) :
taxid = addTaxonFromFile(t,'species',restricting_ancestor,options)
elif options.path != '' :
previous = options.taxonomy.findTaxonByTaxid(restricting_ancestor)
if seq[options.path] != '' :
ancestors = [a for a in seq[options.path].split(', ')]
if ancestors[-1] != t :
ancestors.append(t)
else : # useful when data is from UNITE databases but could disappear
if len(t.split(' ')) >= 2 and not numberInStr(t) :
genus, trash = t.split(" ",1)
ancestors = [genus, t]
else :
ancestors = [t]
for a in ancestors :
try:
possible_previous = options.taxonomy.findTaxonByName(a)
keyError = True
for p in possible_previous :
if options.taxonomy.isAncestor(restricting_ancestor, p[0]) :
previous = p
keyError = False
if keyError :
raise KeyError()
key_error = False
taxid = None
# check if the taxon isn't already in the taxonomy with the right ancestor
try :
possible_taxids = options.taxonomy.findTaxonByName(t)
for p in possible_taxids :
if options.taxonomy.isAncestor(restricting_ancestor, p[0]) :
taxid = p[0]
except KeyError :
except KeyError :
key_error = True
if key_error or taxid is None :
if (resAncInHeader and options.res_anc in seq) :
taxid = addTaxonFromFile(t,'species',restricting_ancestor,options)
elif options.path != '' :
previous = options.taxonomy.findTaxonByTaxid(restricting_ancestor)
if seq[options.path] != '' :
ancestors = [a for a in seq[options.path].split(', ')]
if ancestors[-1] != t :
ancestors.append(t)
else : # useful when data is from UNITE databases but could disappear
if len(t.split(' ')) >= 2 and not numberInStr(t) :
genus, trash = t.split(" ",1)
ancestors = [genus, t]
else :
ancestors = [t]
for a in ancestors :
try:
possible_previous = options.taxonomy.findTaxonByName(a)
keyError = True
for p in possible_previous :
if options.taxonomy.isAncestor(restricting_ancestor, p[0]) :
previous = p
keyError = False
if keyError :
raise KeyError()
except KeyError :
if (len(ancestors) > 1 and a == ancestors[-2] and len(ancestors[-1].split(' ')) >= 2 and ((not numberInStr(a)) or 'sp' in a.split(' '))) : #a voirrrrr, trop restrictif ?
rank = 'genus'
elif a == ancestors[-1] :
......@@ -285,24 +296,33 @@ if __name__ == '__main__':
rank = 'no rank'
taxid = addTaxonFromFile(a,rank,previous[0],options)
previous = (taxid, options.taxonomy.findRankByName(rank))
else :
if (len(t.split(' ')) >= 2 and (not numberInStr(t) or 'sp' in t.split(' ') or t[0].isupper()) \
and t.split(' ')[0] not in useless_words) :
genus,species = t.split(" ",1)
try :
options.taxonomy.findTaxonByName(genus)
except KeyError :
taxid = addTaxonFromFile(genus,'genus',restricting_ancestor,options)
parent = options.taxonomy.findTaxonByName(genus)
taxid = addTaxonFromFile(t, 'species', parent[0], options)
else :
taxid = addTaxonFromFile(t, 'species', restricting_ancestor, options)
localdata=True
if (len(t.split(' ')) >= 2 and (not numberInStr(t) or 'sp' in t.split(' ') or t[0].isupper()) \
and t.split(' ')[0] not in useless_words) :
genus,species = t.split(" ",1)
try :
possible_genuses = options.taxonomy.findTaxonByName(genus)
genus_taxid = None
for g in possible_genuses :
if options.taxonomy.isAncestor(restricting_ancestor, g[0]) :
genus_taxid = g[0]
except KeyError :
genus_taxid = addTaxonFromFile(genus,'genus',restricting_ancestor,options)
if genus_taxid is None : # Genuses matching the name were found but they weren't under the restricting ancestor
parent = restricting_ancestor
else :
parent = genus_taxid
taxid = addTaxonFromFile(t, 'species', parent, options)
else :
taxid = addTaxonFromFile(t, 'species', restricting_ancestor, options)
localdata=True
# seq['taxid'] = taxid
# print formatFasta(seq)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment