Mercurial > repos > pablocarb > synbiodesign
annotate toolSelenzyme.py @ 30:0ad6fda90f41 draft default tip
planemo upload commit f74fd52b330cc7460d98121b9bb78c08ca915d1e
author | pablocarb |
---|---|
date | Thu, 29 Aug 2019 06:59:58 -0400 |
parents | 635b76a9bd7a |
children |
rev | line source |
---|---|
0
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
3 """ |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
4 Created on Mar 19 |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
5 |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
6 @author: Pablo Carbonell |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
7 @description: Query Selenzme: Enzyme sequence selection. |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
8 |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
9 """ |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
10 import requests |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
11 import argparse |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
12 import csv |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
13 import os |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
14 import json |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
15 |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
16 def arguments(): |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
17 parser = argparse.ArgumentParser(description='toolSelenzyme: Enzyme sequence selection. Pablo Carbonell, SYNBIOCHEM, 2019') |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
18 parser.add_argument('infile', |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
19 help='Input csv file.') |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
20 parser.add_argument('outfile', |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
21 help='Input csv file.') |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
22 parser.add_argument('-server', default='http://selenzyme.synbiochem.co.uk/REST', |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
23 help='Selenzyme server.') |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
24 return parser |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
25 |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
26 # Output columns, to be improved |
23
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
27 columns = ['smarts', 'Seq. ID', 'Score', 'Organism Source', 'Description'] |
0
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
28 |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
29 if __name__ == "__main__": |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
30 parser = arguments() |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
31 arg = parser.parse_args() |
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
32 assert os.path.exists(arg.infile) |
23
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
33 header = True |
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
34 with open(arg.infile) as handler, open(arg.outfile, 'w' ) as writer: |
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
35 cw = csv.writer( writer ) |
0
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
36 cv = csv.DictReader(handler) |
23
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
37 for row in cv: |
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
38 url = arg.server |
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
39 assert 'smarts' in row |
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
40 smarts = row['smarts'] |
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
41 r = requests.post( os.path.join(url, 'Query') , json={'smarts': smarts} ) |
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
42 res = json.loads( r.content.decode('utf-8') ) |
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
43 try: |
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
44 assert res['data'] is not None |
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
45 val = json.loads( res['data'] ) |
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
46 assert 'Seq. ID' in val and len(val['Seq. ID'])>0 |
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
47 if header: |
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
48 cw.writerow( columns ) |
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
49 header = False |
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
50 for ix in sorted(val['Seq. ID'], key=lambda z: int(z)): |
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
51 cw.writerow( [smarts]+[val[j][ix] for j in columns[1:]] ) |
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
52 except: |
635b76a9bd7a
planemo upload commit 6f56159bc3cd5339f88517f6822240410bc9e4dd-dirty
pablocarb
parents:
0
diff
changeset
|
53 continue |
0
ac027c9ace4d
planemo upload commit 9e24fae395aeaca30c5bcdef80a21b7decb04042-dirty
pablocarb
parents:
diff
changeset
|
54 |