我想使用Biopython从NCBI下载genbank文件和一个入藏号列表(请注意我用电子邮件地址作为参数调用脚本,例如,python scriptName.py emailAddress)
import os
import os.path
import sys
from Bio import Entrez
import datetime
import time
###############################################################################
# Call Entrez to download files
# If downloading more than 100 files...
# Run this script only between 9pm-5am Monday - Friday EST
# Send E-utilities requests to http://eutils.ncbi.nlm.nih.gov
# Make no more than 3 requests every 1 second.
# Use URL parameter email & tool for distributed software
# NCBI's Disclaimer and Copyright notice must be evident to users of your service.
#
# Use this script at your own risk.
# Neither the script author nor author's employers are responsible for consequences arising from improper usage
###############################################################################
start_day = datetime.date.today().weekday() # 0 is Monday, 6 is Sunday
start_time = datetime.datetime.now().time()
accession = ['NC_002695.1', 'NC_002128.1', 'NC_002127.1']
arguments = sys.argv
Entrez.email = arguments[1]
if ((start_day < 5 and start_time > datetime.time(hour=21)) or (start_day < 5 and start_time < datetime.time(hour=5)) or start_day > 5 or len(accession) <= 100 ):
for a in accession:
print(a)
if ( (datetime.date.today().weekday() < 5 and datetime.datetime.now().time() > datetime.time(hour=21)) or (datetime.date.today().weekday() < 5 and datetime.datetime.now().time() < datetime.time(hour=5)) or (datetime.date.today().weekday() == start_day + 1 and datetime.datetime.now().time() < datetime.time(hour=5)) or (datetime.date.today().weekday() > 5) or len(accession) <= 100 ):
while True:
print('Downloading ' + a)
#try:
handle=Entrez.efetch(db='nucleotide', id=a, rettype='gb', retmode='text')
FILENAME = 'GenbankFiles_E_coli/' + a + '.gb'
local_file=open(FILENAME,'w')
local_file.write(handle.read())
handle.close()
local_file.close()
当我运行脚本时,它会在我收到错误之前(仅)部分下载第一个文件:
NC_002128.1
Downloading NC_002128.1
Downloading NC_002128.1
Traceback (most recent call last):
File "/usr/local/anaconda3/lib/python3.4/urllib/request.py", line 1182, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "/usr/local/anaconda3/lib/python3.4/http/client.py", line 1088, in request
self._send_request(method, url, body, headers)
File "/usr/local/anaconda3/lib/python3.4/http/client.py", line 1126, in _send_request
self.endheaders(body)
File "/usr/local/anaconda3/lib/python3.4/http/client.py", line 1084, in endheaders
self._send_output(message_body)
File "/usr/local/anaconda3/lib/python3.4/http/client.py", line 922, in _send_output
self.send(msg)
File "/usr/local/anaconda3/lib/python3.4/http/client.py", line 857, in send
self.connect()
File "/usr/local/anaconda3/lib/python3.4/http/client.py", line 834, in connect
self.timeout, self.source_address)
File "/usr/local/anaconda3/lib/python3.4/socket.py", line 494, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/local/anaconda3/lib/python3.4/socket.py", line 533, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "download_files.py", line 92, in <module>
handle=Entrez.efetch(db='nucleotide', id=a, rettype='gb', retmode='text')
File "/usr/local/anaconda3/lib/python3.4/site-packages/Bio/Entrez/__init__.py", line 155, in efetch
return _open(cgi, variables, post)
File "/usr/local/anaconda3/lib/python3.4/site-packages/Bio/Entrez/__init__.py", line 516, in _open
handle = _urlopen(cgi)
File "/usr/local/anaconda3/lib/python3.4/urllib/request.py", line 161, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/anaconda3/lib/python3.4/urllib/request.py", line 463, in open
response = self._open(req, data)
File "/usr/local/anaconda3/lib/python3.4/urllib/request.py", line 481, in _open
'_open', req)
File "/usr/local/anaconda3/lib/python3.4/urllib/request.py", line 441, in _call_chain
result = func(*args)
File "/usr/local/anaconda3/lib/python3.4/urllib/request.py", line 1210, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "/usr/local/anaconda3/lib/python3.4/urllib/request.py", line 1184, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno -2] Name or service not known>
我还没有确定我的代码中是否有错误,如果我选择的模块存在问题(即使Biopython应该处理这些调用),如果我的连接出现问题(我的工作块和没有警告的油门),或者是否是别的东西。
我尝试使用和不使用urllib * / http *模块运行它无济于事(我得到了同样的错误)。但是,部分文件很有趣。下载到最终序列的所有内容(最后都有一个contig条目)。以下是下载文件的最后几行:
/translation="MVPPSAVLCYHNEISRQIPVNMKNIRTEFIPRFNLTLCFPRYWM
TWTGIGIICVFAMVPPALRDPLLGKLGMLVGRLGKSARQRALINLSLCFPEYSDKEKE
NIVDAMFATASMAVVLMAELALSGPDKISHRIRWNGLEIVEKMAQNNEKVIFLVPHAW
GVDIPAMLMAASGRKMAAMFHNQRNPVVDYVWNSVRRRFGGKLHARNDGIASFVRSVR
QGYWGYYLPDQDHGPEFSEFADFFATYKATLPVIGRLSRISGARIIPLFPVYDGKTHH
LTIHVSPPLAIRQKSDAHIARQINEVVENFVRPHPEQYTWILKLLKTRKEGEEDPY"
CONTIG join(AB011549.2:1..92721) ###this line is not in the original file
//
可以将其与原始genbank文件进行比较:http://www.ncbi.nlm.nih.gov/nuccore/10955266/?report=genbank
我可以确认这不是与DTD文件相关的错误,但其他所有内容都可以用于争夺。 (The new RefSeq release from NCBI is compatible with Bio.Entrez.Parser?)
我在CentOS上运行此脚本 Python 3.4.3 :: Anaconda 2.3.0(64位):: Biopython 1.66
答案 0 :(得分:0)
NCBI Entrez fetch API可以区分返回类型rettype="gb"
和rettype="gbwithparts"
,第一个可以通过提供引用其他记录的CONTIG
行来缩短,而后者会扩展它们以便为您提供完整的序列(在网站上查找“GenBank(full)”)。
当NCBI方面出现问题时,您有时可以看到这一点,例如: http://blastedbio.blogspot.co.uk/2012/03/missing-external-exons-in-genbank-with.html和http://blastedbio.blogspot.co.uk/2012/04/missing-feature-locations-in-genbank.html(已修复)。
我猜你得到的套接字错误与你的工作互联网访问控制有关。