Entrez模块
Entrez提供了链向在NCBI服务器的esearch和efetch工具的连接
列出Entrez模块的方法和属性
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'sunchengquan'
__mail__ = '[email protected]'
from Bio import Entrez
s = dir(Entrez)
print(s)
运行结果:
['_HTTPError', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_as_bytes', '_binary_to_string_handle', '_construct_cgi', '_construct_params', '_encode_options', '_open', '_update_ecitmatch_variables', '_urlencode', '_urlopen', 'ecitmatch', 'efetch', 'egquery', 'einfo', 'elink', 'email', 'epost', 'esearch', 'espell', 'esummary', 'parse', 'print_function', 'read', 'time', 'tool', 'warnings']
Entrez数据库提供了什么
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'sunchengquan'
__mail__ = '[email protected]'
#从Entrez数据库得到信息,使用Entrez.einfo()函数
from Bio import Entrez
#email属性把邮箱地址告诉NCBI(可选)
Entrez.email = '[email protected]'
handle = Entrez.einfo()
info = Entrez.read(handle)
print(info)
运行结果:
{'DbList': ['pubmed', 'protein', 'nuccore', 'ipg', 'nucleotide', 'nucgss', 'nucest', 'structure', 'sparcle', 'genome', 'annotinfo', 'assembly', 'bioproject', 'biosample', 'blastdbinfo', 'books', 'cdd', 'clinvar', 'clone', 'gap', 'gapplus', 'grasp', 'dbvar', 'gene', 'gds', 'geoprofiles', 'homologene', 'medgen', 'mesh', 'ncbisearch', 'nlmcatalog', 'omim', 'orgtrack', 'pmc', 'popset', 'probe', 'proteinclusters', 'pcassay', 'biosystems', 'pccompound', 'pcsubstance', 'pubmedhealth', 'seqannot', 'snp', 'sra', 'taxonomy', 'biocollections', 'unigene', 'gencoll', 'gtr']}
#给定一个数据库名字作为Entrez.einfo()的参数
#则可以得到关于这个数据库的信息
from Bio import Entrez
Entrez.email = '[email protected]'
handle = Entrez.einfo(db ='pubmed')
info = Entrez.read(handle)
#print(info)
#print(info['DbInfo'])
print(info.keys())
print(info['DbInfo']['Description'])
运行结果:
dict_keys(['DbInfo'])
PubMed bibliographic record
多于一个术语搜索Pubmed,用AND/OR组合关键词
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'sunchengquan'
__mail__ = '[email protected]'
from Bio import Entrez
Entrez.email = '[email protected]'
handle = Entrez.esearch(db='pubmed',term='PyCogent AND RNA')
record = Entrez.read(handle)
print(record['IdList'])
运行结果:
['18230758']
#验证在NCBI网站pumed用关键词‘PyCogent AND RNA’检索
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'sunchengquan'
__mail__ = '[email protected]'
from Bio import Entrez
Entrez.email = '[email protected]'
handle = Entrez.esearch(db='pubmed',term='PyCogent OR RNA')
record = Entrez.read(handle)
print(record['Count'])
运行结果:
961515
#可选参数retmax最大返回数目,可以用来设置查询文本的最大检索数目
from Bio import Entrez
Entrez.email = '[email protected]'
handle = Entrez.esearch(db='pubmed',term='PyCogent or RAN',retmax=3)
record = Entrez.read(handle)
print(record['IdList'])
运行结果:
['29192776', '29191911', '29191431']
用GenBank格式检索和解析核酸数据库条目
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'sunchengquan'
__mail__ = '[email protected]'
from Bio import Entrez
# search sequences by a combination of keywords
Entrez.email = '[email protected]'
handle = Entrez.esearch(db='nucleotide',term='Homo sapiens AND mRNA AND Mapk')
records = Entrez.read(handle)
print(records['Count'])
top3_records = records['IdList'][0:3]
print(top3_records)
# retriveve the sequences by their GI number
#多个ID必须用一个逗号隔开的GI号的字符串
gi_list = ','.join(top3_records)
print(gi_list)
#文件格式retmode必须设置成xml
handle = Entrez.efetch(db='nucleotide',id=gi_list,rettype='gb',retmode='xml')
records = Entrez.read(handle)
print(len(records))
print(records[0].keys())
print(records[0]['GBSeq_organism'])
运行结果:
2855
['385298702', '373938425', '239046737']
385298702,373938425,239046737
3
dict_keys(['GBSeq_locus', 'GBSeq_length', 'GBSeq_strandedness', 'GBSeq_moltype', 'GBSeq_topology', 'GBSeq_division', 'GBSeq_update-date', 'GBSeq_create-date', 'GBSeq_definition', 'GBSeq_primary-accession', 'GBSeq_accession-version', 'GBSeq_other-seqids', 'GBSeq_keywords', 'GBSeq_source', 'GBSeq_organism', 'GBSeq_taxonomy', 'GBSeq_references', 'GBSeq_comment', 'GBSeq_primary', 'GBSeq_feature-table', 'GBSeq_sequence'])
Homo sapiens
用关键词搜索NCBI蛋白质数据库条目
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'sunchengquan'
__mail__ = '[email protected]'
from Bio import Entrez
# search sequences by a combination of keywords
Entrez.email = '[email protected]'
handle = Entrez.esearch(db='protein',term='Human AND cancer AND p21')
records = Entrez.read(handle)
print(records['Count'])
id_list = records['IdList'][0:3]
#retrieve sequences
id_list = ','.join(id_list)
print(id_list)
handle = Entrez.efetch(db='protein',id=id_list,rettype='fasta',retmode='xml')
records = Entrez.read(handle)
print(records[0].keys())
print(records[0]['TSeq_defline'])
运行结果:
1320
161377472,161377470,161377468
dict_keys(['TSeq_seqtype', 'TSeq_gi', 'TSeq_accver', 'TSeq_taxid', 'TSeq_orgname', 'TSeq_defline', 'TSeq_length', 'TSeq_sequence'])
cyclin-A1 isoform c [Homo sapiens]