annotate uniprot.py @ 9:f31d8d59ffb6 draft default tip

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