Mercurial > repos > bgruening > uniprot_rest_interface
annotate uniprot.py @ 3:df4c547b2533 draft
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 074fed99c6df6bad17341bbfa57645f222907ad4
author | bgruening |
---|---|
date | Mon, 10 Apr 2017 13:24:24 -0400 |
parents | cd2a41c65447 |
children | 144e9558d3fd |
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 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
16 url = 'http://www.uniprot.org/' |
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""" |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
22 tool = 'batch/' |
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 |
1
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
27 data = {'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
|
28 |
1
cd2a41c65447
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit fb201f32a92466c2bd8086e91bbf777401f1b489
bgruening
parents:
0
diff
changeset
|
29 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
|
30 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
|
31 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
|
32 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
33 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
34 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
|
35 """ _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
|
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 tool = 'mapping/' |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
38 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
39 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
|
40 '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
|
41 '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
|
42 '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
|
43 '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
|
44 } |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
45 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
|
46 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
|
47 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
|
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 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
50 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
|
51 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
|
52 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
|
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 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
70 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
|
71 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
|
72 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
73 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
|
74 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
|
75 |
48522382b6a4
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/uniprot_rest_interface commit 2b8ad1bbfe098129ae32cd8311a755dff58ae97b-dirty
bgruening
parents:
diff
changeset
|
76 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
|
77 args.out.write(_retrieve(query, format=args.format)) |