annotate fetch_fasta_from_NCBI.py @ 4:c667d0ee39f5 draft

planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
author artbio
date Wed, 29 Nov 2017 17:38:52 -0500
parents 8be88084f89c
children 706fe8139955
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
1 #!/usr/bin/env python
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
2 # -*- coding: utf-8 -*-
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
3 """
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
4 From a taxonomy ID retrieves all the nucleotide sequences
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
5 It returns a multiFASTA nuc/prot file
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
6
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
7 Entrez Database UID common name E-utility Database Name
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
8 Nucleotide GI number nuccore
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
9 Protein GI number protein
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
10
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
11 Retrieve strategy:
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
12
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
13 esearch to get total number of UIDs (count)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
14 esearch to get UIDs in batches
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
15 loop untile end of UIDs list:
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
16 epost to put a batch of UIDs in the history server
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
17 efetch to retrieve info from previous post
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
18
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
19 retmax of efetch is 1/10 of declared value from NCBI
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
20
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
21 queries are 1 sec delayed, to satisfy NCBI guidelines
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
22 (more than what they request)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
23 """
3
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
24 import argparse
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
25 import httplib
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
26 import logging
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
27 import re
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
28 import sys
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
29 import time
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
30 import urllib
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
31 import urllib2
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
32
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
33
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
34 class QueryException(Exception):
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
35 pass
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
36
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
37
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
38 class Eutils:
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
39
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
40 def __init__(self, options, logger):
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
41 """
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
42 Initialize retrieval parameters
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
43 """
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
44 self.logger = logger
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
45 self.base = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/"
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
46 self.query_string = options.query_string
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
47 self.dbname = options.dbname
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
48 if options.outname:
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
49 self.outname = options.outname
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
50 else:
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
51 self.outname = 'NCBI_download' + '.' + self.dbname + '.fasta'
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
52 self.ids = []
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
53 self.retmax_esearch = 100000
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
54 self.retmax_efetch = 500
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
55 self.count = 0
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
56 self.webenv = ""
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
57 self.query_key = ""
4
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
58 if options.get_uids:
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
59 self.get_uids = True
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
60 else:
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
61 self.get_uids = False
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
62 if options.iuds_file:
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
63 with open(options.iuds_file, 'r') as f:
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
64 self.ids.extend(f.readline().split(' '))
3
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
65
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
66 def dry_run(self):
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
67 self.get_count_value()
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
68
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
69 def retrieve(self):
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
70 """
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
71 Retrieve the fasta sequences corresponding to the query
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
72 """
4
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
73 if len(self.ids) == 0:
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
74 self.get_count_value()
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
75 else:
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
76 self.count = len(self.ids)
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
77 # If no UIDs are found exit script
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
78 if self.count > 0:
4
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
79 if len(self.ids) == 0:
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
80 self.get_uids_list()
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
81 if not self.get_uids:
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
82 try:
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
83 self.get_sequences()
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
84 except QueryException as e:
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
85 self.logger.error("Exiting script.")
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
86 raise e
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
87 else:
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
88 with open(self.outname, 'w') as f:
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
89 f.write('\t'.join(self.ids)+'\n')
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
90 else:
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
91 self.logger.error("No UIDs were found. Exiting script.")
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
92 raise Exception("")
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
93
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
94 def get_count_value(self):
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
95 """
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
96 just to retrieve Count (number of UIDs)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
97 Total number of UIDs from the retrieved set to be shown in the XML
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
98 output (default=20). By default, ESearch only includes the first 20
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
99 UIDs retrieved in the XML output. If usehistory is set to 'y',
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
100 the remainder of the retrieved set will be stored on the History server
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
101
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
102 http://www.ncbi.nlm.nih.gov/books/NBK25499/#chapter4.EFetch
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
103 """
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
104 self.logger.info("retrieving data from %s" % self.base)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
105 self.logger.info("for Query: %s and database: %s" %
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
106 (self.query_string, self.dbname))
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
107 querylog = self.esearch(self.dbname, self.query_string, '', '',
3
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
108 "count")
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
109 self.logger.debug("Query response:")
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
110 for line in querylog:
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
111 self.logger.debug(line.rstrip())
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
112 if '</Count>' in line:
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
113 self.count = int(line[line.find('<Count>')+len('<Count>'):
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
114 line.find('</Count>')])
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
115 self.logger.info("Found %d UIDs" % self.count)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
116
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
117 def get_uids_list(self):
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
118 """
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
119 Increasing retmax allows more of the retrieved UIDs to be included in
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
120 the XML output, up to a maximum of 100,000 records.
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
121 from http://www.ncbi.nlm.nih.gov/books/NBK25499/#chapter4.ESearch
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
122 """
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
123 retmax = self.retmax_esearch
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
124 if (self.count > retmax):
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
125 num_batches = (self.count / retmax) + 1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
126 else:
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
127 num_batches = 1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
128 self.logger.info("Batch size for esearch action: %d UIDs" % retmax)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
129 self.logger.info("Number of batches for esearch action: %d " %
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
130 num_batches)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
131 for n in range(num_batches):
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
132 querylog = self.esearch(self.dbname, self.query_string, n*retmax,
3
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
133 retmax, '')
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
134 for line in querylog:
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
135 if '<Id>' in line and '</Id>' in line:
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
136 uid = (line[line.find('<Id>')+len('<Id>'):
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
137 line.find('</Id>')])
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
138 self.ids.append(uid)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
139 self.logger.info("Retrieved %d UIDs" % len(self.ids))
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
140
3
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
141 def esearch(self, db, term, retstart, retmax, rettype):
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
142 url = self.base + "esearch.fcgi"
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
143 self.logger.debug("url: %s" % url)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
144 values = {'db': db,
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
145 'term': term,
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
146 'rettype': rettype,
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
147 'retstart': retstart,
3
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
148 'retmax': retmax}
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
149 data = urllib.urlencode(values)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
150 self.logger.debug("data: %s" % str(data))
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
151 req = urllib2.Request(url, data)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
152 response = urllib2.urlopen(req)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
153 querylog = response.readlines()
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
154 response.close()
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
155 time.sleep(1)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
156 return querylog
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
157
4
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
158 def sanitiser(self, db, fastaseq):
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
159 if(db not in "nuccore protein"):
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
160 return fastaseq
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
161 regex = re.compile(r"[ACDEFGHIKLMNPQRSTVWYBZ]{49,}")
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
162 sane_seqlist = []
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
163 seqlist = fastaseq.split("\n\n")
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
164 for seq in seqlist[:-1]:
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
165 fastalines = seq.split("\n")
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
166 if len(fastalines) < 2:
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
167 self.logger.info("Empty sequence for %s" %
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
168 ("|".join(fastalines[0].split("|")[:4])))
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
169 self.logger.info("%s download is skipped" %
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
170 ("|".join(fastalines[0].split("|")[:4])))
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
171 continue
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
172 if db == "nuccore":
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
173 badnuc = 0
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
174 for nucleotide in fastalines[1]:
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
175 if nucleotide not in "ATGC":
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
176 badnuc += 1
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
177 if float(badnuc)/len(fastalines[1]) > 0.4:
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
178 self.logger.info("%s ambiguous nucleotides in %s\
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
179 or download interrupted at this offset\
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
180 | %s" % (float(badnuc)/len(fastalines[1]),
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
181 "|".join(fastalines[0].split("|")
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
182 [:4]),
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
183 fastalines[1]))
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
184 self.logger.info("%s download is skipped" %
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
185 (fastalines[0].split("|")[:4]))
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
186 continue
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
187 """ remove spaces and trim the header to 100 chars """
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
188 fastalines[0] = fastalines[0].replace(" ", "_")[:100]
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
189 cleanseq = "\n".join(fastalines)
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
190 sane_seqlist.append(cleanseq)
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
191 elif db == "protein":
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
192 fastalines[0] = fastalines[0][0:100]
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
193 fastalines[0] = fastalines[0].replace(" ", "_")
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
194 fastalines[0] = fastalines[0].replace("[", "_")
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
195 fastalines[0] = fastalines[0].replace("]", "_")
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
196 fastalines[0] = fastalines[0].replace("=", "_")
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
197 """ because blast makedb doesn't like it """
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
198 fastalines[0] = fastalines[0].rstrip("_")
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
199 fastalines[0] = re.sub(regex, "_", fastalines[0])
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
200 cleanseq = "\n".join(fastalines)
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
201 sane_seqlist.append(cleanseq)
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
202 self.logger.info("clean sequences appended: %d" % (len(sane_seqlist)))
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
203 return "\n".join(sane_seqlist)
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
204
4
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
205 def efetch(self, db, uid_list):
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
206 url = self.base + "efetch.fcgi"
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
207 self.logger.debug("url_efetch: %s" % url)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
208 values = {'db': db,
4
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
209 'id': uid_list,
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
210 'rettype': "fasta",
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
211 'retmode': "text"}
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
212 data = urllib.urlencode(values)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
213 req = urllib2.Request(url, data)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
214 self.logger.debug("data: %s" % str(data))
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
215 serverTransaction = False
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
216 counter = 0
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
217 response_code = 0
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
218 while not serverTransaction:
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
219 counter += 1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
220 self.logger.info("Server Transaction Trial: %s" % (counter))
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
221 try:
3
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
222 self.logger.debug("Going to open")
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
223 response = urllib2.urlopen(req)
3
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
224 self.logger.debug("Going to get code")
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
225 response_code = response.getcode()
3
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
226 self.logger.debug("Going to read, de code was : %s",
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
227 str(response_code))
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
228 fasta = response.read()
3
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
229 self.logger.debug("Did all that")
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
230 response.close()
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
231 if((response_code != 200) or
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
232 ("Resource temporarily unavailable" in fasta) or
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
233 ("Error" in fasta) or (not fasta.startswith(">"))):
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
234 serverTransaction = False
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
235 if (response_code != 200):
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
236 self.logger.info("urlopen error: Response code is not\
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
237 200")
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
238 elif ("Resource temporarily unavailable" in fasta):
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
239 self.logger.info("Ressource temporarily unavailable")
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
240 elif ("Error" in fasta):
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
241 self.logger.info("Error in fasta")
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
242 else:
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
243 self.logger.info("Fasta doesn't start with '>'")
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
244 else:
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
245 serverTransaction = True
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
246 except urllib2.HTTPError as e:
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
247 serverTransaction = False
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
248 self.logger.info("urlopen error:%s, %s" % (e.code, e.read()))
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
249 except urllib2.URLError as e:
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
250 serverTransaction = False
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
251 self.logger.info("urlopen error: Failed to reach a server")
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
252 self.logger.info("Reason :%s" % (e.reason))
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
253 except httplib.IncompleteRead as e:
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
254 serverTransaction = False
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
255 self.logger.info("IncompleteRead error: %s" % (e.partial))
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
256 if (counter > 500):
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
257 serverTransaction = True
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
258 if (counter > 500):
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
259 raise QueryException({"message":
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
260 "500 Server Transaction Trials attempted for\
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
261 this batch. Aborting."})
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
262 fasta = self.sanitiser(self.dbname, fasta)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
263 time.sleep(0.1)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
264 return fasta
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
265
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
266 def get_sequences(self):
4
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
267 batch_size = 200
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
268 count = self.count
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
269 uids_list = self.ids
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
270 self.logger.info("Batch size for efetch action: %d" % batch_size)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
271 self.logger.info("Number of batches for efetch action: %d" %
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
272 ((count / batch_size) + 1))
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
273 with open(self.outname, 'w') as out:
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
274 for start in range(0, count, batch_size):
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
275 end = min(count, start+batch_size)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
276 batch = uids_list[start:end]
4
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
277 self.logger.info("retrieving batch %d" %
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
278 ((start / batch_size) + 1))
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
279 try:
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
280 mfasta = self.efetch(self.dbname, ','.join(batch))
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
281 out.write(mfasta + '\n')
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
282 except QueryException as e:
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
283 self.logger.error("%s" % e.message)
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
284 raise e
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
285 urllib.urlcleanup()
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
286
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
287
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
288 LOG_FORMAT = '%(asctime)s|%(levelname)-8s|%(message)s'
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
289 LOG_DATEFMT = '%Y-%m-%d %H:%M:%S'
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
290 LOG_LEVELS = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
291
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
292
3
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
293 def command_parse():
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
294 parser = argparse.ArgumentParser(description='Retrieve data from NCBI')
4
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
295 parser.add_argument('-i', dest='query_string', help='NCBI Query String')
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
296 parser.add_argument('--UID_list', dest='iuds_file',
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
297 help='file containing a list of iuds to be fetched')
3
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
298 parser.add_argument('-o', dest='outname', help='output file name')
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
299 parser.add_argument('-d', dest='dbname', help='database type')
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
300 parser.add_argument('--count', '-c', dest='count_ids',
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
301 action='store_true', default=False,
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
302 help='dry run ouputing only the number of sequences\
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
303 found')
4
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
304 parser.add_argument('--get_uids', '-u', dest='get_uids', default=False,
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
305 action='store_true', help='prints to the output a list\
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
306 of UIDs')
3
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
307 parser.add_argument('-l', '--logfile', help='log file (default=stderr)')
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
308 parser.add_argument('--loglevel', choices=LOG_LEVELS, default='INFO',
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
309 help='logging level (default: INFO)')
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
310 args = parser.parse_args()
4
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
311 if args.query_string is not None and args.iuds_file is not None:
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
312 parser.error('Please choose either fetching the -i query or the -u\
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
313 list.')
3
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
314 return args
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
315
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
316
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
317 def __main__():
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
318 """ main function """
3
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
319 args = command_parse()
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
320 log_level = getattr(logging, args.loglevel)
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
321 kwargs = {'format': LOG_FORMAT,
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
322 'datefmt': LOG_DATEFMT,
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
323 'level': log_level}
3
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
324 if args.logfile:
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
325 kwargs['filename'] = args.logfile
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
326 logging.basicConfig(**kwargs)
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
327 logger = logging.getLogger('data_from_NCBI')
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
328
3
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
329 E = Eutils(args, logger)
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
330 if args.count_ids:
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
331 try:
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
332 E.dry_run()
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
333 except Exception:
4
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
334 sys.exit(-1)
3
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
335 else:
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
336 try:
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
337 E.retrieve()
8be88084f89c planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ab45487db8cc69750f92a40d763d51ffac940e25
artbio
parents: 2
diff changeset
338 except Exception:
4
c667d0ee39f5 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit ca3070e85c370b914ffa0562afe12b363e05aea4
artbio
parents: 3
diff changeset
339 sys.exit(-1)
1
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
340
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
341
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
342 if __name__ == "__main__":
7e41bbb94159 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/fetch_fasta_from_ncbi commit 6008aafac37eec1916d6b72c05d9cfcb002b8095
artbio
parents:
diff changeset
343 __main__()