Mercurial > repos > bgruening > uniprot_rest_interface
annotate uniprot.py @ 7:bfdc6a7ffd3a draft
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
author | bgruening |
---|---|
date | Thu, 22 Apr 2021 17:32:16 +0000 |
parents | f7ebd1b4783b |
children | af5eccf83605 |
rev | line source |
---|---|
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
1 #!/usr/bin/env python |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
2 """ |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
3 uniprot python interface |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
4 to access the uniprot database |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
5 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
6 Based on work from Jan Rudolph: https://github.com/jdrudolph/uniprot |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
7 available services: |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
8 map |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
9 retrieve |
7
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
10 |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
11 rewitten using inspiration form: https://findwork.dev/blog/advanced-usage-python-requests-timeouts-retries-hooks/ |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
12 """ |
1
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
13 import argparse |
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
14 import sys |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
15 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
16 import requests |
7
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
17 from requests.adapters import HTTPAdapter |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
18 from requests.packages.urllib3.util.retry import Retry |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
19 |
7
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
20 |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
21 DEFAULT_TIMEOUT = 5 # seconds |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
22 URL = 'https://www.uniprot.org/' |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
23 |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
24 retry_strategy = Retry( |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
25 total=5, |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
26 backoff_factor=2, |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
27 status_forcelist=[429, 500, 502, 503, 504], |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
28 allowed_methods=["HEAD", "GET", "OPTIONS", "POST"] |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
29 ) |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
30 |
1
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
31 |
7
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
32 class TimeoutHTTPAdapter(HTTPAdapter): |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
33 def __init__(self, *args, **kwargs): |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
34 self.timeout = DEFAULT_TIMEOUT |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
35 if "timeout" in kwargs: |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
36 self.timeout = kwargs["timeout"] |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
37 del kwargs["timeout"] |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
38 super().__init__(*args, **kwargs) |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
39 |
7
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
40 def send(self, request, **kwargs): |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
41 timeout = kwargs.get("timeout") |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
42 if timeout is None: |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
43 kwargs["timeout"] = self.timeout |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
44 return super().send(request, **kwargs) |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
45 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
46 |
7
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
47 def _map(query, f, t, format='tab', chunk_size=100): |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
48 """ _map is not meant for use with the python interface, use `map` instead |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
49 """ |
5
f7ebd1b4783b
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit f4fd11072886b30675afb8f8bb459eba9c4671bb
bgruening
parents:
4
diff
changeset
|
50 tool = 'uploadlists/' |
7
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
51 data = {'format': format, 'from': f, 'to': t} |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
52 |
7
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
53 req = [] |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
54 for i in range(0, len(query), chunk_size): |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
55 q = query[i:i + chunk_size] |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
56 req.append(dict([("url", URL + tool), |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
57 ('data', data), |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
58 ("files", {'file': ' '.join(q)})])) |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
59 return req |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
60 response = requests.post(URL + tool, data=data) |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
61 response.raise_for_status() |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
62 page = response.text |
7
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
63 if "The service is temporarily unavailable" in page: |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
64 exit("The UNIPROT service is temporarily unavailable. Please try again later.") |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
65 return page |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
66 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
67 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
68 if __name__ == '__main__': |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
69 parser = argparse.ArgumentParser(description='retrieve uniprot mapping') |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
70 subparsers = parser.add_subparsers(dest='tool') |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
71 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
72 mapping = subparsers.add_parser('map') |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
73 mapping.add_argument('f', help='from') |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
74 mapping.add_argument('t', help='to') |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
75 mapping.add_argument('inp', nargs='?', type=argparse.FileType('r'), |
1
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
76 default=sys.stdin, help='input file (default: stdin)') |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
77 mapping.add_argument('out', nargs='?', type=argparse.FileType('w'), |
1
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
78 default=sys.stdout, help='output file (default: stdout)') |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
79 mapping.add_argument('--format', default='tab', help='output format') |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
80 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
81 retrieve = subparsers.add_parser('retrieve') |
1
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
82 retrieve.add_argument('inp', metavar='in', nargs='?', type=argparse.FileType('r'), |
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
83 default=sys.stdin, help='input file (default: stdin)') |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
84 retrieve.add_argument('out', nargs='?', type=argparse.FileType('w'), |
1
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
85 default=sys.stdout, help='output file (default: stdout)') |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
86 retrieve.add_argument('-f', '--format', help='specify output format', default='txt') |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
87 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
88 args = parser.parse_args() |
7
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
89 |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
90 # get the IDs from the file as sorted list |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
91 # (sorted is convenient for testing) |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
92 query = set() |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
93 for line in args.inp: |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
94 query.add(line.strip()) |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
95 query = sorted(query) |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
96 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
97 if args.tool == 'map': |
7
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
98 pload = _map(query, args.f, args.t, chunk_size=100) |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
99 elif args.tool == 'retrieve': |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
100 pload = _map(query, 'ACC+ID', 'ACC', args.format, chunk_size=100) |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
101 |
7
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
102 adapter = TimeoutHTTPAdapter(max_retries=retry_strategy) |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
103 http = requests.Session() |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
104 http.mount("https://", adapter) |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
105 for i, p in enumerate(pload): |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
106 response = http.post(**p) |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
107 args.out.write(response.text) |
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
bgruening
parents:
5
diff
changeset
|
108 http.close() |