annotate run_p3.py @ 6:f201e8c6e004 draft default tip

Uploaded
author ben-warren
date Mon, 07 Jul 2014 19:28:17 -0400
parents b321e0517be3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
1 #!/usr/bin/pyrthon
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
2 ##run primer3 by passing Python dictionary
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
3
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
4 #un_P3.py
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
5
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
6 import subprocess as sp
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
7 import copy
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
8
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
9
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
10 if "check_output" not in dir(sp): # duck punch it in!
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
11 def f(*popenargs, **kwargs):
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
12 if 'stdout' in kwargs:
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
13 raise ValueError('stdout argument not allowed, it will be overridden.')
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
14 process = sp.Popen(stdout=sp.PIPE, *popenargs, **kwargs)
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
15 output, unused_err = process.communicate()
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
16 retcode = process.poll()
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
17 if retcode:
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
18 cmd = kwargs.get("args")
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
19 if cmd is None:
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
20 cmd = popenargs[0]
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
21 raise sp.CalledProcessError(retcode,cmd)
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
22 return output
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
23 sp.check_output = f
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
24
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
25
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
26
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
27
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
28 ##call P3 with dict of args, returns dict, no exception handling
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
29 def run_P3(target_dict):
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
30 p3_str=''
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
31 for key in target_dict:
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
32 p3_str+=key
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
33 p3_str+='='
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
34 p3_str+=str(target_dict[key])
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
35 p3_str+='\n'
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
36 p3_str+='='
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
37 input_str='echo -e \"' + p3_str + '\" | primer3_core '
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
38 ###exception handling to be added here
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
39 output = sp.check_output(input_str,shell=True)
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
40 output_fields=output.split('\n')
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
41 ##put output into a dict, omitting trailing =
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
42 P3_dict=dict([X.split('=') for X in output_fields][:len(output_fields)-2])
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
43 ##return iterable list
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
44 primer_list=[dict(PRIMER_RIGHT_SEQUENCE=P3_dict.get('PRIMER_RIGHT_'+ str(X) + '_SEQUENCE'),PRIMER_LEFT=P3_dict.get('PRIMER_LEFT_'+ str(X) ),PRIMER_RIGHT=P3_dict.get('PRIMER_RIGHT_'+ str(X) ),PRIMER_LEFT_SEQUENCE=P3_dict.get('PRIMER_LEFT_'+ str(X) + '_SEQUENCE')) for X in range(0,int(P3_dict.get('PRIMER_RIGHT_NUM_RETURNED'))-1)]
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
45 return(primer_list)
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
46
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
47
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
48
b321e0517be3 Uploaded
ben-warren
parents:
diff changeset
49