Mercurial > repos > bgruening > uniprot_rest_interface
annotate uniprot.py @ 10:95fb5712344f draft
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
| author | bgruening | 
|---|---|
| date | Tue, 06 Aug 2024 14:49:45 +0000 | 
| parents | 468c71dac78a | 
| children | 60f7e2a6b9c7 | 
| rev | line source | 
|---|---|
| 1 
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
 bgruening parents: 
0diff
changeset | 1 import argparse | 
| 9 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 2 import json | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 3 import re | 
| 1 
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
 bgruening parents: 
0diff
changeset | 4 import sys | 
| 9 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 5 import time | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 6 import zlib | 
| 10 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 7 from time import sleep | 
| 9 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 8 from urllib.parse import ( | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 9 parse_qs, | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 10 urlencode, | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 11 urlparse, | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 12 ) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 13 from xml.etree import ElementTree | 
| 0 
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 14 | 
| 
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 15 import requests | 
| 9 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 16 from requests.adapters import ( | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 17 HTTPAdapter, | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 18 Retry, | 
| 7 
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 19 ) | 
| 0 
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 20 | 
| 1 
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
 bgruening parents: 
0diff
changeset | 21 | 
| 10 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 22 BATCH_SIZE = 50000 # Limit at UniProt is 100k | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 23 POLLING_INTERVAL = 5 | 
| 9 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 24 API_URL = "https://rest.uniprot.org" | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 25 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 26 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 27 retries = Retry(total=5, backoff_factor=0.25, status_forcelist=[500, 502, 503, 504]) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 28 session = requests.Session() | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 29 session.mount("https://", HTTPAdapter(max_retries=retries)) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 30 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 31 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 32 def check_response(response): | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 33 try: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 34 response.raise_for_status() | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 35 except requests.HTTPError: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 36 raise | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 37 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 38 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 39 def submit_id_mapping(from_db, to_db, ids): | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 40 print(f"{from_db} {to_db}") | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 41 request = requests.post( | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 42 f"{API_URL}/idmapping/run", | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 43 data={"from": from_db, "to": to_db, "ids": ",".join(ids)}, | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 44 ) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 45 check_response(request) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 46 return request.json()["jobId"] | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 47 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 48 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 49 def get_next_link(headers): | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 50 re_next_link = re.compile(r'<(.+)>; rel="next"') | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 51 if "Link" in headers: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 52 match = re_next_link.match(headers["Link"]) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 53 if match: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 54 return match.group(1) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 55 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 56 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 57 def check_id_mapping_results_ready(job_id): | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 58 while True: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 59 request = session.get(f"{API_URL}/idmapping/status/{job_id}") | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 60 check_response(request) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 61 j = request.json() | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 62 if "jobStatus" in j: | 
| 10 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 63 if j["jobStatus"] in ["NEW", "RUNNING"]: | 
| 9 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 64 print(f"Retrying in {POLLING_INTERVAL}s") | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 65 time.sleep(POLLING_INTERVAL) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 66 else: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 67 raise Exception(j["jobStatus"]) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 68 else: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 69 return bool(j["results"] or j["failedIds"]) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 70 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 71 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 72 def get_batch(batch_response, file_format, compressed): | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 73 batch_url = get_next_link(batch_response.headers) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 74 while batch_url: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 75 batch_response = session.get(batch_url) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 76 batch_response.raise_for_status() | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 77 yield decode_results(batch_response, file_format, compressed) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 78 batch_url = get_next_link(batch_response.headers) | 
| 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 | 
| 9 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 80 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 81 def combine_batches(all_results, batch_results, file_format): | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 82 if file_format == "json": | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 83 for key in ("results", "failedIds"): | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 84 if key in batch_results and batch_results[key]: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 85 all_results[key] += batch_results[key] | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 86 elif file_format == "tsv": | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 87 return all_results + batch_results[1:] | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 88 else: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 89 return all_results + batch_results | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 90 return all_results | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 91 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 92 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 93 def get_id_mapping_results_link(job_id): | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 94 url = f"{API_URL}/idmapping/details/{job_id}" | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 95 request = session.get(url) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 96 check_response(request) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 97 return request.json()["redirectURL"] | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 98 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 99 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 100 def decode_results(response, file_format, compressed): | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 101 if compressed: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 102 decompressed = zlib.decompress(response.content, 16 + zlib.MAX_WBITS) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 103 if file_format == "json": | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 104 j = json.loads(decompressed.decode("utf-8")) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 105 return j | 
| 10 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 106 elif file_format in ["tsv", "gff"]: | 
| 9 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 107 return [line for line in decompressed.decode("utf-8").split("\n") if line] | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 108 elif file_format == "xlsx": | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 109 return [decompressed] | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 110 elif file_format == "xml": | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 111 return [decompressed.decode("utf-8")] | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 112 else: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 113 return decompressed.decode("utf-8") | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 114 elif file_format == "json": | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 115 return response.json() | 
| 10 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 116 elif file_format in ["tsv", "gff"]: | 
| 9 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 117 return [line for line in response.text.split("\n") if line] | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 118 elif file_format == "xlsx": | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 119 return [response.content] | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 120 elif file_format == "xml": | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 121 return [response.text] | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 122 return response.text | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 123 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 124 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 125 def get_xml_namespace(element): | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 126 m = re.match(r"\{(.*)\}", element.tag) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 127 return m.groups()[0] if m else "" | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 128 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 129 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 130 def merge_xml_results(xml_results): | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 131 merged_root = ElementTree.fromstring(xml_results[0]) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 132 for result in xml_results[1:]: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 133 root = ElementTree.fromstring(result) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 134 for child in root.findall("{http://uniprot.org/uniprot}entry"): | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 135 merged_root.insert(-1, child) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 136 ElementTree.register_namespace("", get_xml_namespace(merged_root[0])) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 137 return ElementTree.tostring(merged_root, encoding="utf-8", xml_declaration=True) | 
| 0 
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 138 | 
| 
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 139 | 
| 9 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 140 def print_progress_batches(batch_index, size, total): | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 141 n_fetched = min((batch_index + 1) * size, total) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 142 print(f"Fetched: {n_fetched} / {total}") | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 143 | 
| 0 
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 144 | 
| 10 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 145 def get_id_mapping_results_search(url, first): | 
| 9 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 146 parsed = urlparse(url) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 147 query = parse_qs(parsed.query) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 148 file_format = query["format"][0] if "format" in query else "json" | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 149 if "size" in query: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 150 size = int(query["size"][0]) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 151 else: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 152 size = 500 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 153 query["size"] = size | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 154 compressed = ( | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 155 query["compressed"][0].lower() == "true" if "compressed" in query else False | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 156 ) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 157 parsed = parsed._replace(query=urlencode(query, doseq=True)) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 158 url = parsed.geturl() | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 159 request = session.get(url) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 160 check_response(request) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 161 results = decode_results(request, file_format, compressed) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 162 total = int(request.headers["x-total-results"]) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 163 print_progress_batches(0, size, total) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 164 for i, batch in enumerate(get_batch(request, file_format, compressed), 1): | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 165 results = combine_batches(results, batch, file_format) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 166 print_progress_batches(i, size, total) | 
| 10 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 167 if len(results) > 1 and file_format == "tsv" and not first: | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 168 results = results[1:] | 
| 9 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 169 if file_format == "xml": | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 170 return merge_xml_results(results) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 171 return results | 
| 0 
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 172 | 
| 
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 173 | 
| 9 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 174 # print(results) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 175 # {'results': [{'from': 'P05067', 'to': 'CHEMBL2487'}], 'failedIds': ['P12345']} | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 176 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 177 if __name__ == "__main__": | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 178 parser = argparse.ArgumentParser(description="retrieve uniprot mapping") | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 179 subparsers = parser.add_subparsers(dest="tool") | 
| 0 
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 180 | 
| 9 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 181 mapping = subparsers.add_parser("map") | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 182 mapping.add_argument("f", help="from") | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 183 mapping.add_argument("t", help="to") | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 184 mapping.add_argument( | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 185 "inp", | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 186 nargs="?", | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 187 type=argparse.FileType("r"), | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 188 default=sys.stdin, | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 189 help="input file (default: stdin)", | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 190 ) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 191 mapping.add_argument( | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 192 "out", | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 193 nargs="?", | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 194 type=argparse.FileType("w"), | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 195 default=sys.stdout, | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 196 help="output file (default: stdout)", | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 197 ) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 198 mapping.add_argument("--format", default="tab", help="output format") | 
| 0 
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 199 | 
| 9 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 200 retrieve = subparsers.add_parser("retrieve") | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 201 retrieve.add_argument( | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 202 "inp", | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 203 metavar="in", | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 204 nargs="?", | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 205 type=argparse.FileType("r"), | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 206 default=sys.stdin, | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 207 help="input file (default: stdin)", | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 208 ) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 209 retrieve.add_argument( | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 210 "out", | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 211 nargs="?", | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 212 type=argparse.FileType("w"), | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 213 default=sys.stdout, | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 214 help="output file (default: stdout)", | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 215 ) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 216 retrieve.add_argument("-f", "--format", help="specify output format", default="txt") | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 217 mapping = subparsers.add_parser("menu") | 
| 0 
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 218 | 
| 
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 219 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: 
5diff
changeset | 220 | 
| 9 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 221 # code for auto generating the from - to conditional | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 222 if args.tool == "menu": | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 223 from lxml import etree | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 224 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 225 request = session.get("https://rest.uniprot.org/configure/idmapping/fields") | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 226 check_response(request) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 227 fields = request.json() | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 228 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 229 tos = dict() | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 230 from_cond = etree.Element("conditional", name="from_cond") | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 231 from_select = etree.SubElement( | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 232 from_cond, "param", name="from", type="select", label="Source database:" | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 233 ) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 234 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 235 rules = dict() | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 236 for rule in fields["rules"]: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 237 rules[rule["ruleId"]] = rule["tos"] | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 238 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 239 for group in fields["groups"]: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 240 group_name = group["groupName"] | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 241 group_name = group_name.replace("databases", "DBs") | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 242 for item in group["items"]: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 243 if item["to"]: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 244 tos[item["name"]] = f"{group_name} - {item['displayName']}" | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 245 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 246 for group in fields["groups"]: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 247 group_name = group["groupName"] | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 248 group_name = group_name.replace("databases", "DBs") | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 249 for item in group["items"]: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 250 if not item["from"]: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 251 continue | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 252 option = etree.SubElement(from_select, "option", value=item["name"]) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 253 option.text = f"{group_name} - {item['displayName']}" | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 254 when = etree.SubElement(from_cond, "when", value=item["name"]) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 255 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 256 to_select = etree.SubElement( | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 257 when, "param", name="to", type="select", label="Target database:" | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 258 ) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 259 ruleId = item["ruleId"] | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 260 for to in rules[ruleId]: | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 261 option = etree.SubElement(to_select, "option", value=to) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 262 option.text = tos[to] | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 263 etree.indent(from_cond, space=" ") | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 264 print(etree.tostring(from_cond, pretty_print=True, encoding="unicode")) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 265 sys.exit(0) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 266 | 
| 7 
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 267 # 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: 
5diff
changeset | 268 # (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: 
5diff
changeset | 269 query = set() | 
| 
bfdc6a7ffd3a
"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit ddbed5f0b0879b4a001d2da6a521b0c9a39c1e7b"
 bgruening parents: 
5diff
changeset | 270 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: 
5diff
changeset | 271 query.add(line.strip()) | 
| 10 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 272 query = list(query) | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 273 results = [] | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 274 first = True # if False the header is removed | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 275 while len(query) > 0: | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 276 batch = query[:BATCH_SIZE] | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 277 query = query[BATCH_SIZE:] | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 278 print(f"processing {len(batch)} left {len(query)}") | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 279 if args.tool == "map": | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 280 job_id = submit_id_mapping(from_db=args.f, to_db=args.t, ids=batch) | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 281 elif args.tool == "retrieve": | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 282 job_id = submit_id_mapping(from_db="UniProtKB_AC-ID", to_db="UniProtKB", ids=batch) | 
| 0 
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
 bgruening parents: diff
changeset | 283 | 
| 10 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 284 if check_id_mapping_results_ready(job_id): | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 285 link = get_id_mapping_results_link(job_id) | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 286 link = f"{link}?format={args.format}" | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 287 print(link) | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 288 results.extend(get_id_mapping_results_search(link, first)) | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 289 first = False | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 290 print(f"got {len(results)} results so far") | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 291 if len(query): | 
| 
95fb5712344f
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 1c020106d4d7f957c9f1ec0d9885bbb2d56e70e7
 bgruening parents: 
9diff
changeset | 292 sleep(5) | 
| 9 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 293 | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 294 if not isinstance(results, str): | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 295 results = "\n".join(results) | 
| 
468c71dac78a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit da476148d1c609f5c26e880a3e593f0fa71ff2f6
 bgruening parents: 
8diff
changeset | 296 args.out.write(f"{results}\n") | 
