comparison run_p3.py @ 5:b321e0517be3 draft

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