Mercurial > repos > bgruening > uniprot_rest_interface
annotate uniprot.py @ 5:f7ebd1b4783b draft
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit f4fd11072886b30675afb8f8bb459eba9c4671bb
author | bgruening |
---|---|
date | Sat, 15 Sep 2018 10:23:35 -0400 |
parents | 144e9558d3fd |
children | bfdc6a7ffd3a |
rev | line source |
---|---|
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
1 #!/usr/bin/env python |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
2 """ |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
3 uniprot python interface |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
4 to access the uniprot database |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
5 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
6 Based on work from Jan Rudolph: https://github.com/jdrudolph/uniprot |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
7 available services: |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
8 map |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
9 retrieve |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
10 """ |
1
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
11 import argparse |
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
12 import sys |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
13 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
14 import requests |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
15 |
4
144e9558d3fd
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 64c4336509437b9772df82a8b89046c9044b1ff5
bgruening
parents:
1
diff
changeset
|
16 url = 'https://www.uniprot.org/' |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
17 |
1
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
18 |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
19 def _retrieve(query, format='txt'): |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
20 """_retrieve is not meant for use with the python interface, use `retrieve` |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
21 instead""" |
5
f7ebd1b4783b
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit f4fd11072886b30675afb8f8bb459eba9c4671bb
bgruening
parents:
4
diff
changeset
|
22 tool = 'uploadlists/' |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
23 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
24 query = list(set(query.split('\n'))) |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
25 queries = [query[i:i+100] for i in range(0, len(query), 100)] |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
26 |
5
f7ebd1b4783b
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit f4fd11072886b30675afb8f8bb459eba9c4671bb
bgruening
parents:
4
diff
changeset
|
27 data = { |
f7ebd1b4783b
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit f4fd11072886b30675afb8f8bb459eba9c4671bb
bgruening
parents:
4
diff
changeset
|
28 'format': format, |
f7ebd1b4783b
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit f4fd11072886b30675afb8f8bb459eba9c4671bb
bgruening
parents:
4
diff
changeset
|
29 'from': 'ACC+ID', |
f7ebd1b4783b
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit f4fd11072886b30675afb8f8bb459eba9c4671bb
bgruening
parents:
4
diff
changeset
|
30 'to': 'ACC' |
f7ebd1b4783b
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit f4fd11072886b30675afb8f8bb459eba9c4671bb
bgruening
parents:
4
diff
changeset
|
31 } |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
32 |
1
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
33 responses = [requests.post(url + tool, data=data, files={'file': ' '.join(_)}) for _ in queries] |
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
34 page = ''.join(response.text for response in responses) |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
35 return page |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
36 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
37 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
38 def _map(query, f, t, format='tab'): |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
39 """ _map is not meant for use with the python interface, use `map` instead |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
40 """ |
5
f7ebd1b4783b
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit f4fd11072886b30675afb8f8bb459eba9c4671bb
bgruening
parents:
4
diff
changeset
|
41 tool = 'uploadlists/' |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
42 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
43 data = { |
1
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
44 'from': f, |
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
45 'to': t, |
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
46 'format': 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
|
47 'query': query |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
48 } |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
49 response = requests.post(url + tool, data=data) |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
50 page = response.text |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
51 return page |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
52 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
53 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
54 if __name__ == '__main__': |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
55 parser = argparse.ArgumentParser(description='retrieve uniprot mapping') |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
56 subparsers = parser.add_subparsers(dest='tool') |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
57 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
58 mapping = subparsers.add_parser('map') |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
59 mapping.add_argument('f', help='from') |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
60 mapping.add_argument('t', help='to') |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
61 mapping.add_argument('inp', nargs='?', type=argparse.FileType('r'), |
1
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
62 default=sys.stdin, help='input file (default: stdin)') |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
63 mapping.add_argument('out', nargs='?', type=argparse.FileType('w'), |
1
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
64 default=sys.stdout, help='output file (default: stdout)') |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
65 mapping.add_argument('--format', default='tab', help='output format') |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
66 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
67 retrieve = subparsers.add_parser('retrieve') |
1
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
68 retrieve.add_argument('inp', metavar='in', nargs='?', type=argparse.FileType('r'), |
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
69 default=sys.stdin, help='input file (default: stdin)') |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
70 retrieve.add_argument('out', nargs='?', type=argparse.FileType('w'), |
1
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
71 default=sys.stdout, help='output file (default: stdout)') |
0
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
72 retrieve.add_argument('-f', '--format', help='specify output format', default='txt') |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
73 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
74 args = parser.parse_args() |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
75 query = args.inp.read() |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
76 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
77 if args.tool == 'map': |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
78 args.out.write(_map(query, args.f, args.t, args.format)) |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
79 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
80 elif args.tool == 'retrieve': |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
81 args.out.write(_retrieve(query, format=args.format)) |