Commit 8e4ba82e authored by Sander Bollen's avatar Sander Bollen
Browse files

use subprocess in design

parent e35d806b
...@@ -12,6 +12,7 @@ from pyfaidx import Fasta ...@@ -12,6 +12,7 @@ from pyfaidx import Fasta
from pysam import AlignmentFile from pysam import AlignmentFile
from .models import Primer, Region from .models import Primer, Region
from .primer3 import Primer3, parse_primer3_output
from .utils import NoPrimersException, calc_gc, NEW_VCF, generate_fastq_from_primers from .utils import NoPrimersException, calc_gc, NEW_VCF, generate_fastq_from_primers
PRIMER3_SCRIPT = os.path.join(os.path.join(os.path.dirname(__file__) ,"static"), 'getprimers.sh') PRIMER3_SCRIPT = os.path.join(os.path.join(os.path.dirname(__file__) ,"static"), 'getprimers.sh')
...@@ -43,25 +44,10 @@ def run_primer3(sequence, region, padding=True, ...@@ -43,25 +44,10 @@ def run_primer3(sequence, region, padding=True,
target_len = len(sequence) target_len = len(sequence)
target = ",".join(map(str, [target_start, target_len])) target = ",".join(map(str, [target_start, target_len]))
opt_size = str(sum(map(int, product_size.split("-"))) / 2)
args = [primer3_script, product_size, target, sequence, target, opt_size, prim3_exe] p3 = Primer3(prim3_exe, sequence, target, target)
retval = check_call(args) p3_out = p3.run()
if retval != 0: primers = parse_primer3_output(p3_out)
raise ValueError("Primer3 crashed")
# now read example.for and example.rev
with open("example.for", "rb") as forward, open("example.rev", "rb") as reverse:
forwards = _sanitize_p3(forward)
reverses = _sanitize_p3(reverse)
forwards, reverses = _get_shortest(forwards, reverses, n_primers)
if len(forwards) == 0 or len(reverses) == 0:
raise NoPrimersException("No acceptable primers could be found. Try increasing the padding")
primers = [Primer.from_p3(x, y, sequence, region.chr, region.start) for x, y in zip(forwards, reverses)]
return primers return primers
......
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
from subprocess import check_call from subprocess import check_call
import os
import re import re
...@@ -67,7 +68,8 @@ class Primer3(object): ...@@ -67,7 +68,8 @@ class Primer3(object):
"PRIMER_EXPLAIN_FLAG=1\n" \ "PRIMER_EXPLAIN_FLAG=1\n" \
"PRIMER_MIN_TM={it}\n" \ "PRIMER_MIN_TM={it}\n" \
"PRIMER_MAX_TM={at}\n" \ "PRIMER_MAX_TM={at}\n" \
"PRIMER_NUM_RETURN=200".format( "PRIMER_NUM_RETURN=200\n" \
"=".format(
seq=self.template, seq=self.template,
tar=self.target, tar=self.target,
exc=self.excluded_region, exc=self.excluded_region,
...@@ -84,13 +86,14 @@ class Primer3(object): ...@@ -84,13 +86,14 @@ class Primer3(object):
handle.write(cfg_str) handle.write(cfg_str)
def run(self): def run(self):
cfg = NamedTemporaryFile() cfg = NamedTemporaryFile(delete=False)
out = NamedTemporaryFile() out = NamedTemporaryFile()
self.create_config(cfg) self.create_config(cfg)
cfg.close()
args = [self.primer3_exe, "-output", out.name, cfg.name] args = [self.primer3_exe, "-output", out.name, cfg.name]
_ = check_call(args=args) _ = check_call(args)
retval = [] retval = []
with open(out.name) as handle: with open(out.name) as handle:
...@@ -98,7 +101,7 @@ class Primer3(object): ...@@ -98,7 +101,7 @@ class Primer3(object):
retval.append(l.strip()) retval.append(l.strip())
out.close() out.close()
cfg.close() os.remove(cfg.name)
return retval return retval
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment