1

我想使用 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 文件相关的错误,但其他一切都可以解决。(NCBI 的新 RefSeq 版本与 Bio.Entrez.Parser 兼容?

我在 CentOS Python 3.4.3 :: Anaconda 2.3.0 (64-bit) :: Biopython 1.66 上运行这个脚本

4

1 回答 1

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.htmlhttp: //blastedbio.blogspot.co.uk/2012/04/missing-feature-locations-in-genbank.html(自此修复)。

我猜您收到的套接字错误与您的工作互联网访问控制有关。

于 2015-12-17T16:51:05.930 回答