【发布时间】:2013-05-06 10:27:02
【问题描述】:
我正在尝试更改以前的脚本,该脚本利用 biopython 获取有关物种门的信息。编写此脚本是为了一次检索一个物种的信息。我想修改脚本,以便一次可以为 100 个生物体执行此操作。 这是初始代码
import sys
from Bio import Entrez
def get_tax_id(species):
"""to get data from ncbi taxomomy, we need to have the taxid. we can
get that by passing the species name to esearch, which will return
the tax id"""
species = species.replace(" ", "+").strip()
search = Entrez.esearch(term = species, db = "taxonomy", retmode = "xml")
record = Entrez.read(search)
return record['IdList'][0]
def get_tax_data(taxid):
"""once we have the taxid, we can fetch the record"""
search = Entrez.efetch(id = taxid, db = "taxonomy", retmode = "xml")
return Entrez.read(search)
Entrez.email = ""
if not Entrez.email:
print "you must add your email address"
sys.exit(2)
taxid = get_tax_id("Erodium carvifolium")
data = get_tax_data(taxid)
lineage = {d['Rank']:d['ScientificName'] for d in
data[0]['LineageEx'] if d['Rank'] in ['family', 'order']}
我已设法修改脚本,使其接受包含我正在使用的一种生物体的本地文件。但我需要将其扩展到 100 个生物体。
所以想法是从我的生物体文件中生成一个列表,并以某种方式分别将从列表中生成的每个项目输入taxid = get_tax_id("Erodium carvifolium") 行,并用我的生物体名称替换“Erodium carvifolium”。但我不知道该怎么做。
这是代码的示例版本以及我的一些调整
import sys
from Bio import Entrez
def get_tax_id(species):
"""to get data from ncbi taxomomy, we need to have the taxid. we can
get that by passing the species name to esearch, which will return
the tax id"""
species = species.replace(' ', "+").strip()
search = Entrez.esearch(term = species, db = "taxonomy", retmode = "xml")
record = Entrez.read(search)
return record['IdList'][0]
def get_tax_data(taxid):
"""once we have the taxid, we can fetch the record"""
search = Entrez.efetch(id = taxid, db = "taxonomy", retmode = "xml")
return Entrez.read(search)
Entrez.email = ""
if not Entrez.email:
print "you must add your email address"
sys.exit(2)
list = ['Helicobacter pylori 26695', 'Thermotoga maritima MSB8', 'Deinococcus radiodurans R1', 'Treponema pallidum subsp. pallidum str. Nichols', 'Aquifex aeolicus VF5', 'Archaeoglobus fulgidus DSM 4304']
i = iter(list)
item = i.next()
for item in list:
???
taxid = get_tax_id(?)
data = get_tax_data(taxid)
lineage = {d['Rank']:d['ScientificName'] for d in
data[0]['LineageEx'] if d['Rank'] in ['phylum']}
print lineage, taxid
问号是指我被难住的地方,下一步该做什么。我看不到如何连接我的循环来替换?在 get_tax_id(?) 中。还是我需要以某种方式附加列表中的每个项目,以便每次修改它们以包含get_tax_id(Helicobacter pylori 26695),然后找到某种方法将它们放在包含taxid =
【问题讨论】:
-
你应该问biostars:biostars.org
-
感谢您的建议
标签: list loops iteration bioinformatics biopython