annotate toolOptBioDes.py @ 28:501f39987c9b draft

planemo upload commit 708fd9f837af7425a8986c336aab18cec7ff5b05-dirty
author pablocarb
date Fri, 02 Aug 2019 05:27:32 -0400
parents 8f42eba7e2eb
children 0ad6fda90f41
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
1 #!/usr/bin/env python3
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
2 # -*- coding: utf-8 -*-
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
3 """
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
4 Created on Mar 19
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
5
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
6 @author: Pablo Carbonell
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
7 @description: Query OptBioDes: optimal synbio design.
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
8
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
9 """
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
10 import requests
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
11 import argparse
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
12 import csv
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
13 import os
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
14 import json
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
15
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
16 def arguments():
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
17 parser = argparse.ArgumentParser(description='toolOptBioDes: Optimal SynBio Design. Pablo Carbonell, SYNBIOCHEM, 2019')
4
6dbb43bdf2f4 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents: 3
diff changeset
18 parser.add_argument('size',
6dbb43bdf2f4 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents: 3
diff changeset
19 help='Library size.')
2
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
20 parser.add_argument('outfile',
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
21 help='Output csv design file.')
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
22 parser.add_argument('diagfile',
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
23 help='Output csv diagnostics file.')
24
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
24 parser.add_argument('-infile', default=None,
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
25 help='Input csv file (DoE specifications sheet).')
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
26 parser.add_argument('-partsfile', default=None,
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
27 help='Input csv file (Regulartory parts).')
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
28 parser.add_argument('-genesfile', default=None,
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
29 help='Input csv file (Genetic parts).')
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
30 parser.add_argument('-sbolOut', default=None,
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
31 help='Output design in SBOL format.')
10
6a1fbacdfaf5 planemo upload commit b97a4df6e4e3a6f3d14f18f3f30c47eb5657a362
pablocarb
parents: 9
diff changeset
32 parser.add_argument('-server', default='http://optbiodes.synbiochem.co.uk/REST',
2
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
33 help='OptBioDes server.')
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
34 return parser
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
35
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
36 def testApp(url):
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
37 r = requests.get( url )
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
38 res = json.loads( r.content.decode('utf-8') )
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
39 print( res )
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
40
4
6dbb43bdf2f4 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents: 3
diff changeset
41 def sheetUpload(doefile, size, outfile, diagfile, url):
2
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
42 files = { 'file': open(doefile, 'rb' ) }
8
6572347b3e1a planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents: 6
diff changeset
43 values = {'size': int(size), 'format': 'csv'}
2
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
44 r = requests.post( os.path.join(url, 'Query' ), files=files, data=values )
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
45 res = json.loads( r.content.decode('utf-8') )
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
46 M = res['data']['M']
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
47 with open(outfile, 'w') as h:
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
48 cw = csv.writer(h)
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
49 cw.writerow( res['data']['names'] )
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
50 for row in M:
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
51 cw.writerow( row )
9
f9aef3df1fc8 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents: 8
diff changeset
52 with open(diagfile, 'w') as h:
8
6572347b3e1a planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents: 6
diff changeset
53 cw = csv.writer(h)
6572347b3e1a planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents: 6
diff changeset
54 cw.writerow( ['Size', 'Efficiency'] )
6572347b3e1a planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents: 6
diff changeset
55 cw.writerow( [res['data']['libsize'], res['data']['J']] )
2
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
56 print( 'Size:', res['data']['libsize'], 'Efficiency:', res['data']['J'] )
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
57
24
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
58 def partsUploadSbol( partsfile, genesfile, size, outfile, diagfile, sbolOut, server ):
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
59 files = {
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
60 'parts': open(partsfile, 'rb' ),
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
61 'genes': open(genesfile, 'rb')
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
62 }
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
63 values = {'size': size}
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
64 r = requests.post( os.path.join(url, 'Parts' ), files=files, data=values )
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
65 res = json.loads( r.content.decode('utf-8') )
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
66 M = res['data']['M']
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
67 with open(outfile, 'w') as h:
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
68 cw = csv.writer(h)
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
69 cw.writerow( res['data']['names'] )
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
70 for row in M:
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
71 cw.writerow( row )
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
72 with open(diagfile, 'w') as h:
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
73 cw = csv.writer(h)
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
74 cw.writerow( ['Size', 'Efficiency'] )
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
75 cw.writerow( [res['data']['libsize'], res['data']['J']] )
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
76 open(sbolOut, 'w').write(res['data']['sbol'])
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
77 print( 'Size:', res['data']['libsize'], 'Efficiency:', res['data']['J'] )
2
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
78
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
79
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
80 if __name__ == "__main__":
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
81 parser = arguments()
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
82 arg = parser.parse_args()
856bd14e6e96 planemo upload commit 6a252d04f4b2f79606ab6679b6a91f957e33da7b-dirty
pablocarb
parents:
diff changeset
83 assert os.path.exists(arg.infile)
24
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
84 if arg.sbolOut is None:
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
85 sheetUpload( arg.infile, arg.size, arg.outfile, arg.diagfile, arg.server )
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
86 else:
8f42eba7e2eb planemo upload commit 2414e76bee8adbe48fee12b14b13a743a03b9748-dirty
pablocarb
parents: 10
diff changeset
87 partsUploadSbol( arg.partsfile, arg.genesfile, arg.size, arg.outfile, arg.diagfile, arg.sbolOut, arg.server )