Mercurial > repos > bgruening > uniprot_rest_interface
annotate uniprot.py @ 7:b1cc2c5bde0e 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:31:48 +0000 | 
| parents | 6e9fabe16b0c | 
| children | d2ad6e2c55d1 | 
| rev | line source | 
|---|---|
| 0 
ddcc8591ddfb
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 | 
| 
ddcc8591ddfb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 2 """ | 
| 
ddcc8591ddfb
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 | 
| 
ddcc8591ddfb
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 | 
| 
ddcc8591ddfb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 5 | 
| 
ddcc8591ddfb
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 | 
| 
ddcc8591ddfb
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: | 
| 
ddcc8591ddfb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 8 map | 
| 
ddcc8591ddfb
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 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 10 | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 11 rewitten using inspiration form: https://findwork.dev/blog/advanced-usage-python-requests-timeouts-retries-hooks/ | 
| 0 
ddcc8591ddfb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 12 """ | 
| 1 
357dc1f016f5
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
 bgruening parents: 
0diff
changeset | 13 import argparse | 
| 
357dc1f016f5
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
 bgruening parents: 
0diff
changeset | 14 import sys | 
| 0 
ddcc8591ddfb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 15 | 
| 
ddcc8591ddfb
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 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 17 from requests.adapters import HTTPAdapter | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 18 from requests.packages.urllib3.util.retry import Retry | 
| 0 
ddcc8591ddfb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 19 | 
| 7 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 20 | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 21 DEFAULT_TIMEOUT = 5 # seconds | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 22 URL = 'https://www.uniprot.org/' | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 23 | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 24 retry_strategy = Retry( | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 25 total=5, | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 26 backoff_factor=2, | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 27 status_forcelist=[429, 500, 502, 503, 504], | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 28 allowed_methods=["HEAD", "GET", "OPTIONS", "POST"] | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 29 ) | 
| 0 
ddcc8591ddfb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 30 | 
| 1 
357dc1f016f5
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
 bgruening parents: 
0diff
changeset | 31 | 
| 7 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 32 class TimeoutHTTPAdapter(HTTPAdapter): | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 33 def __init__(self, *args, **kwargs): | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 34 self.timeout = DEFAULT_TIMEOUT | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 35 if "timeout" in kwargs: | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 36 self.timeout = kwargs["timeout"] | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 37 del kwargs["timeout"] | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 38 super().__init__(*args, **kwargs) | 
| 0 
ddcc8591ddfb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 39 | 
| 7 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 40 def send(self, request, **kwargs): | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 41 timeout = kwargs.get("timeout") | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 42 if timeout is None: | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 43 kwargs["timeout"] = self.timeout | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 44 return super().send(request, **kwargs) | 
| 0 
ddcc8591ddfb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 45 | 
| 
ddcc8591ddfb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 46 | 
| 7 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 47 def _map(query, f, t, format='tab', chunk_size=100): | 
| 0 
ddcc8591ddfb
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 | 
| 
ddcc8591ddfb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 49 """ | 
| 5 
6e9fabe16b0c
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit f4fd11072886b30675afb8f8bb459eba9c4671bb
 bgruening parents: 
4diff
changeset | 50 tool = 'uploadlists/' | 
| 7 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 51 data = {'format': format, 'from': f, 'to': t} | 
| 0 
ddcc8591ddfb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 52 | 
| 7 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 53 req = [] | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 54 for i in range(0, len(query), chunk_size): | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 55 q = query[i:i + chunk_size] | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 56 req.append(dict([("url", URL + tool), | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 57 ('data', data), | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 58 ("files", {'file': ' '.join(q)})])) | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 59 return req | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 60 response = requests.post(URL + tool, data=data) | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 61 response.raise_for_status() | 
| 0 
ddcc8591ddfb
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 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 63 if "The service is temporarily unavailable" in page: | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 64 exit("The UNIPROT service is temporarily unavailable. Please try again later.") | 
| 0 
ddcc8591ddfb
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 | 
| 
ddcc8591ddfb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 66 | 
| 
ddcc8591ddfb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 67 | 
| 
ddcc8591ddfb
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__': | 
| 
ddcc8591ddfb
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') | 
| 
ddcc8591ddfb
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') | 
| 
ddcc8591ddfb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 71 | 
| 
ddcc8591ddfb
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') | 
| 
ddcc8591ddfb
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') | 
| 
ddcc8591ddfb
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') | 
| 
ddcc8591ddfb
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 
357dc1f016f5
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
 bgruening parents: 
0diff
changeset | 76 default=sys.stdin, help='input file (default: stdin)') | 
| 0 
ddcc8591ddfb
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 
357dc1f016f5
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
 bgruening parents: 
0diff
changeset | 78 default=sys.stdout, help='output file (default: stdout)') | 
| 0 
ddcc8591ddfb
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') | 
| 
ddcc8591ddfb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 80 | 
| 
ddcc8591ddfb
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 
357dc1f016f5
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
 bgruening parents: 
0diff
changeset | 82 retrieve.add_argument('inp', metavar='in', nargs='?', type=argparse.FileType('r'), | 
| 
357dc1f016f5
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
 bgruening parents: 
0diff
changeset | 83 default=sys.stdin, help='input file (default: stdin)') | 
| 0 
ddcc8591ddfb
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 
357dc1f016f5
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
 bgruening parents: 
0diff
changeset | 85 default=sys.stdout, help='output file (default: stdout)') | 
| 0 
ddcc8591ddfb
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') | 
| 
ddcc8591ddfb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 87 | 
| 
ddcc8591ddfb
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 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 89 | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 90 # get the IDs from the file as sorted list | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 91 # (sorted is convenient for testing) | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 92 query = set() | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 93 for line in args.inp: | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 94 query.add(line.strip()) | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 95 query = sorted(query) | 
| 0 
ddcc8591ddfb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 96 | 
| 
ddcc8591ddfb
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 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 98 pload = _map(query, args.f, args.t, chunk_size=100) | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 99 elif args.tool == 'retrieve': | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 100 pload = _map(query, 'ACC+ID', 'ACC', args.format, chunk_size=100) | 
| 0 
ddcc8591ddfb
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 101 | 
| 7 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 102 adapter = TimeoutHTTPAdapter(max_retries=retry_strategy) | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 103 http = requests.Session() | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 104 http.mount("https://", adapter) | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 105 for i, p in enumerate(pload): | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 106 response = http.post(**p) | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 107 args.out.write(response.text) | 
| 
b1cc2c5bde0e
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 108 http.close() | 
