diff run_p3.py @ 5:b321e0517be3 draft

Uploaded
author ben-warren
date Thu, 22 May 2014 20:30:19 -0400
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/run_p3.py	Thu May 22 20:30:19 2014 -0400
@@ -0,0 +1,49 @@
+#!/usr/bin/pyrthon
+##run primer3 by passing Python dictionary
+
+#un_P3.py
+
+import subprocess as sp
+import copy
+
+
+if "check_output" not in dir(sp): # duck punch it in!
+    def f(*popenargs, **kwargs):
+        if 'stdout' in kwargs:
+            raise ValueError('stdout argument not allowed, it will be overridden.')
+        process = sp.Popen(stdout=sp.PIPE, *popenargs, **kwargs)
+        output, unused_err = process.communicate()
+        retcode = process.poll()
+        if retcode:
+            cmd = kwargs.get("args")
+            if cmd is None:
+                cmd = popenargs[0]
+            raise sp.CalledProcessError(retcode,cmd)
+        return output
+    sp.check_output = f
+
+
+
+
+##call P3 with dict of args, returns dict, no exception handling 
+def run_P3(target_dict):
+    p3_str=''
+    for key in target_dict:
+        p3_str+=key
+        p3_str+='='
+        p3_str+=str(target_dict[key])
+        p3_str+='\n'
+    p3_str+='='
+    input_str='echo -e \"' + p3_str + '\" | primer3_core '
+###exception handling to be added here
+    output = sp.check_output(input_str,shell=True)
+    output_fields=output.split('\n')
+    ##put output into a dict, omitting trailing =
+    P3_dict=dict([X.split('=') for X in output_fields][:len(output_fields)-2])
+    ##return iterable list
+    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)]
+    return(primer_list)
+
+
+
+