From xml about sequence information of Genbank You can retrieve taxon information with the following script
import xml.etree.ElementTree as ET
tree = ET.parse("./gene_file.xml")
root = tree.getroot()
for child in root.findall('GBSeq'):
accession = child.find('GBSeq_accession-version').text
taxon = child.find('GBSeq_taxonomy').text
for child in child.findall('GBSeq_feature-table'):
for child in child.findall('GBFeature'):
for child in child.findall('GBFeature_quals'):
for child in child.findall('GBQualifier'):
if child.find('GBQualifier_value') is not None:
taxon_id = child.find('GBQualifier_value').text
if('taxon:' in taxon_id):
taxon_id_out = taxon_id
else:
taxon_id_out = ""
out +=(accession+"\t"+taxon_id_out+ "\t"+ taxon +"\n")
with open("out10.taxon.txt", mode='w') as f:
f.write(out)
Parsing from flat file is troublesome + exceptions are placed, so I tried to read and extract from xml.
Recommended Posts