Mercurial > repos > drosofff > fetch_fasta_from_ncbi
annotate retrieve_fasta_from_NCBI.py @ 3:a9d8f69d59fb draft
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit b6de14061c479f0418cd89e26d6f5ac26e565a07
author | drosofff |
---|---|
date | Wed, 09 Nov 2016 11:27:31 -0500 |
parents | 79cb7620843d |
children | 64f45c5e94a0 |
rev | line source |
---|---|
0
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
1 #!/usr/bin/env python |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
3 """ |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
4 From a taxonomy ID retrieves all the nucleotide sequences |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
5 It returns a multiFASTA nuc/prot file |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
6 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
7 Entrez Database UID common name E-utility Database Name |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
8 Nucleotide GI number nuccore |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
9 Protein GI number protein |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
10 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
11 Retrieve strategy: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
12 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
13 esearch to get total number of UIDs (count) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
14 esearch to get UIDs in batches |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
15 loop untile end of UIDs list: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
16 epost to put a batch of UIDs in the history server |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
17 efetch to retrieve info from previous post |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
18 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
19 retmax of efetch is 1/10 of declared value from NCBI |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
20 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
21 queries are 1 sec delayed, to satisfy NCBI guidelines (more than what they request) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
22 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
23 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
24 python get_fasta_from_taxon.py -i 1638 -o test.out -d protein |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
25 python get_fasta_from_taxon.py -i 327045 -o test.out -d nuccore # 556468 UIDs |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
26 """ |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
27 import sys |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
28 import logging |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
29 import optparse |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
30 import time |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
31 import urllib |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
32 import urllib2 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
33 import httplib |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
34 import re |
3
a9d8f69d59fb
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit b6de14061c479f0418cd89e26d6f5ac26e565a07
drosofff
parents:
1
diff
changeset
|
35 |
a9d8f69d59fb
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit b6de14061c479f0418cd89e26d6f5ac26e565a07
drosofff
parents:
1
diff
changeset
|
36 |
0
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
37 class Eutils: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
38 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
39 def __init__(self, options, logger): |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
40 self.logger = logger |
3
a9d8f69d59fb
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit b6de14061c479f0418cd89e26d6f5ac26e565a07
drosofff
parents:
1
diff
changeset
|
41 self.base = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/" |
0
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
42 self.query_string = options.query_string |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
43 self.dbname = options.dbname |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
44 if options.outname: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
45 self.outname = options.outname |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
46 else: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
47 self.outname = 'NCBI_download' + '.' + self.dbname + '.fasta' |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
48 self.ids = [] |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
49 self.retmax_esearch = 100000 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
50 self.retmax_efetch = 1000 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
51 self.count = 0 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
52 self.webenv = "" |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
53 self.query_key = "" |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
54 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
55 def retrieve(self): |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
56 """ """ |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
57 self.get_count_value() |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
58 self.get_uids_list() |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
59 self.get_sequences() |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
60 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
61 def get_count_value(self): |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
62 """ |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
63 just to retrieve Count (number of UIDs) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
64 Total number of UIDs from the retrieved set to be shown in the XML |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
65 output (default=20). By default, ESearch only includes the first 20 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
66 UIDs retrieved in the XML output. If usehistory is set to 'y', |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
67 the remainder of the retrieved set will be stored on the History server; |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
68 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
69 http://www.ncbi.nlm.nih.gov/books/NBK25499/#chapter4.EFetch |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
70 """ |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
71 self.logger.info("retrieving data from %s" % self.base) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
72 self.logger.info("for Query: %s and database: %s" % |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
73 (self.query_string, self.dbname)) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
74 querylog = self.esearch(self.dbname, self.query_string, '', '', "count") |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
75 self.logger.debug("Query response:") |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
76 for line in querylog: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
77 self.logger.debug(line.rstrip()) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
78 if '</Count>' in line: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
79 self.count = int(line[line.find('<Count>')+len('<Count>') : line.find('</Count>')]) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
80 self.logger.info("Founded %d UIDs" % self.count) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
81 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
82 def get_uids_list(self): |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
83 """ |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
84 Increasing retmax allows more of the retrieved UIDs to be included in the XML output, |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
85 up to a maximum of 100,000 records. |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
86 from http://www.ncbi.nlm.nih.gov/books/NBK25499/#chapter4.ESearch |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
87 """ |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
88 retmax = self.retmax_esearch |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
89 if (self.count > retmax): |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
90 num_batches = (self.count / retmax) + 1 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
91 else: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
92 num_batches = 1 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
93 self.logger.info("Batch size for esearch action: %d UIDs" % retmax) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
94 self.logger.info("Number of batches for esearch action: %d " % num_batches) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
95 for n in range(num_batches): |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
96 querylog = self.esearch(self.dbname, self.query_string, n*retmax, retmax, '') |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
97 for line in querylog: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
98 if '<Id>' in line and '</Id>' in line: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
99 uid = (line[line.find('<Id>')+len('<Id>') : line.find('</Id>')]) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
100 self.ids.append(uid) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
101 self.logger.info("Retrieved %d UIDs" % len(self.ids)) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
102 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
103 def esearch(self, db, term, retstart, retmax, rettype): |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
104 url = self.base + "esearch.fcgi" |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
105 self.logger.debug("url: %s" % url) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
106 values = {'db': db, |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
107 'term': term, |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
108 'rettype': rettype, |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
109 'retstart': retstart, |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
110 'retmax': retmax} |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
111 data = urllib.urlencode(values) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
112 self.logger.debug("data: %s" % str(data)) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
113 req = urllib2.Request(url, data) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
114 response = urllib2.urlopen(req) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
115 querylog = response.readlines() |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
116 time.sleep(1) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
117 return querylog |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
118 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
119 def epost(self, db, ids): |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
120 url = self.base + "epost.fcgi" |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
121 self.logger.debug("url_epost: %s" % url) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
122 values = {'db': db, |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
123 'id': ids} |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
124 data = urllib.urlencode(values) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
125 req = urllib2.Request(url, data) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
126 #self.logger.debug("data: %s" % str(data)) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
127 req = urllib2.Request(url, data) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
128 serverResponse = False |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
129 while not serverResponse: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
130 try: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
131 response = urllib2.urlopen(req) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
132 serverResponse = True |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
133 except: # catch *all* exceptions |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
134 e = sys.exc_info()[0] |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
135 self.logger.info( "Catched Error: %s" % e ) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
136 self.logger.info( "Retrying in 10 sec") |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
137 time.sleep(10) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
138 querylog = response.readlines() |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
139 self.logger.debug("query response:") |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
140 for line in querylog: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
141 self.logger.debug(line.rstrip()) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
142 if '</QueryKey>' in line: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
143 self.query_key = str(line[line.find('<QueryKey>')+len('<QueryKey>'):line.find('</QueryKey>')]) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
144 if '</WebEnv>' in line: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
145 self.webenv = str(line[line.find('<WebEnv>')+len('<WebEnv>'):line.find('</WebEnv>')]) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
146 self.logger.debug("*** epost action ***") |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
147 self.logger.debug("query_key: %s" % self.query_key) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
148 self.logger.debug("webenv: %s" % self.webenv) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
149 time.sleep(1) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
150 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
151 def efetch(self, db, query_key, webenv): |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
152 url = self.base + "efetch.fcgi" |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
153 self.logger.debug("url_efetch: %s" % url) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
154 values = {'db': db, |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
155 'query_key': query_key, |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
156 'webenv': webenv, |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
157 'rettype': "fasta", |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
158 'retmode': "text"} |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
159 data = urllib.urlencode(values) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
160 req = urllib2.Request(url, data) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
161 self.logger.debug("data: %s" % str(data)) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
162 req = urllib2.Request(url, data) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
163 serverTransaction = False |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
164 counter = 0 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
165 while not serverTransaction: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
166 counter += 1 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
167 self.logger.info("Server Transaction Trial: %s" % ( counter ) ) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
168 try: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
169 response = urllib2.urlopen(req) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
170 fasta = response.read() |
1
79cb7620843d
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit e0a1114b735bf1af257456174f64e5ef8d205754-dirty
drosofff
parents:
0
diff
changeset
|
171 if ("Resource temporarily unavailable" in fasta) or (not fasta.startswith(">") ): |
0
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
172 serverTransaction = False |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
173 else: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
174 serverTransaction = True |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
175 except urllib2.HTTPError as e: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
176 serverTransaction = False |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
177 self.logger.info("urlopen error:%s, %s" % (e.code, e.read() ) ) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
178 except httplib.IncompleteRead as e: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
179 serverTransaction = False |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
180 self.logger.info("IncompleteRead error: %s" % ( e.partial ) ) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
181 fasta = self.sanitiser(self.dbname, fasta) # |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
182 time.sleep(1) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
183 return fasta |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
184 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
185 def sanitiser(self, db, fastaseq): |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
186 if db not in "nuccore protein" : return fastaseq |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
187 regex = re.compile(r"[ACDEFGHIKLMNPQRSTVWYBZ]{49,}") |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
188 sane_seqlist = [] |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
189 seqlist = fastaseq.split("\n\n") |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
190 for seq in seqlist[:-1]: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
191 fastalines = seq.split("\n") |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
192 if len(fastalines) < 2: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
193 self.logger.info("Empty sequence for %s" % ("|".join(fastalines[0].split("|")[:4]) ) ) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
194 self.logger.info("%s download is skipped" % ("|".join(fastalines[0].split("|")[:4]) ) ) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
195 continue |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
196 if db == "nuccore": |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
197 badnuc = 0 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
198 for nucleotide in fastalines[1]: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
199 if nucleotide not in "ATGC": |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
200 badnuc += 1 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
201 if float(badnuc)/len(fastalines[1]) > 0.4: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
202 self.logger.info("%s ambiguous nucleotides in %s or download interrupted at this offset | %s" % ( float(badnuc)/len(fastalines[1]), "|".join(fastalines[0].split("|")[:4]), fastalines[1]) ) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
203 self.logger.info("%s download is skipped" % (fastalines[0].split("|")[:4]) ) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
204 continue |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
205 fastalines[0] = fastalines[0].replace(" ","_")[:100] # remove spaces and trim the header to 100 chars |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
206 cleanseq = "\n".join(fastalines) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
207 sane_seqlist.append(cleanseq) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
208 elif db == "protein": |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
209 fastalines[0] = fastalines[0][0:100] |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
210 fastalines[0] = fastalines[0].replace(" ", "_") |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
211 fastalines[0] = fastalines[0].replace("[", "_") |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
212 fastalines[0] = fastalines[0].replace("]", "_") |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
213 fastalines[0] = fastalines[0].replace("=", "_") |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
214 fastalines[0] = fastalines[0].rstrip("_") # because blast makedb doesn't like it |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
215 fastalines[0] = re.sub(regex, "_", fastalines[0]) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
216 cleanseq = "\n".join(fastalines) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
217 sane_seqlist.append(cleanseq) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
218 self.logger.info("clean sequences appended: %d" % (len(sane_seqlist) ) ) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
219 return "\n".join(sane_seqlist) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
220 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
221 def get_sequences(self): |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
222 """ |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
223 Total number of records from the input set to be retrieved, up to a maximum |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
224 of 10,000. Optionally, for a large set the value of retstart can be iterated |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
225 while holding retmax constant, thereby downloading the entire set in batches |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
226 of size retmax. |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
227 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
228 http://www.ncbi.nlm.nih.gov/books/NBK25499/#chapter4.EFetch |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
229 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
230 """ |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
231 batch_size = self.retmax_efetch |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
232 count = self.count |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
233 uids_list = self.ids |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
234 self.logger.info("Batch size for efetch action: %d" % batch_size) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
235 self.logger.info("Number of batches for efetch action: %d" % ((count / batch_size) + 1)) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
236 with open(self.outname, 'w') as out: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
237 for start in range(0, count, batch_size): |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
238 end = min(count, start+batch_size) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
239 batch = uids_list[start:end] |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
240 self.epost(self.dbname, ",".join(batch)) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
241 mfasta = '' |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
242 while not mfasta: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
243 self.logger.info("retrieving batch %d" % ((start / batch_size) + 1)) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
244 mfasta = self.efetch(self.dbname, self.query_key, self.webenv) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
245 out.write(mfasta + '\n') |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
246 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
247 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
248 LOG_FORMAT = '%(asctime)s|%(levelname)-8s|%(message)s' |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
249 LOG_DATEFMT = '%Y-%m-%d %H:%M:%S' |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
250 LOG_LEVELS = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'] |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
251 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
252 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
253 def __main__(): |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
254 """ main function """ |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
255 parser = optparse.OptionParser(description='Retrieve data from NCBI') |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
256 parser.add_option('-i', dest='query_string', help='NCBI Query String') |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
257 parser.add_option('-o', dest='outname', help='output file name') |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
258 parser.add_option('-l', '--logfile', help='log file (default=stderr)') |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
259 parser.add_option('--loglevel', choices=LOG_LEVELS, default='INFO', help='logging level (default: INFO)') |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
260 parser.add_option('-d', dest='dbname', help='database type') |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
261 (options, args) = parser.parse_args() |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
262 if len(args) > 0: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
263 parser.error('Wrong number of arguments') |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
264 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
265 log_level = getattr(logging, options.loglevel) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
266 kwargs = {'format': LOG_FORMAT, |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
267 'datefmt': LOG_DATEFMT, |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
268 'level': log_level} |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
269 if options.logfile: |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
270 kwargs['filename'] = options.logfile |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
271 logging.basicConfig(**kwargs) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
272 logger = logging.getLogger('data_from_NCBI') |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
273 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
274 E = Eutils(options, logger) |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
275 E.retrieve() |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
276 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
277 |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
278 if __name__ == "__main__": |
0bdc5a73c8d1
planemo upload for repository https://bitbucket.org/drosofff/gedtools/
drosofff
parents:
diff
changeset
|
279 __main__() |