Package Bio :: Package Emboss :: Module Primer3
[hide private]
[frames] | no frames]

Source Code for Module Bio.Emboss.Primer3

  1  # Copyright 2008 Michiel de Hoon. 
  2  # Revisions copyright 2009 Leighton Pritchard. 
  3  # Revisions copyright 2010 Peter Cock. 
  4  # All rights reserved. 
  5  # This code is part of the Biopython distribution and governed by its 
  6  # license.  Please see the LICENSE file that should have been included 
  7  # as part of this package. 
  8  """Code to parse output from the EMBOSS eprimer3 program. 
  9   
 10  As elsewhere in Biopython there are two input functions, read and parse, 
 11  for single record output and multi-record output. For primer3, a single 
 12  record object is created for each target sequence and may contain 
 13  multiple primers. 
 14   
 15  i.e. If you ran eprimer3 with a single target sequence, use the read 
 16  function. If you ran eprimer3 with multiple targets, use the parse 
 17  function to iterate over the retsults. 
 18  """ 
 19   
 20  # --- primer3 
 21   
22 -class Record:
23 """Represent information from a primer3 run finding primers. 24 25 Members: 26 27 primers - list of Primer objects describing primer pairs for 28 this target sequence. 29 comments - the comment line(s) for the record 30 """
31 - def __init__(self):
32 self.comments = "" 33 self.primers = []
34
35 -class Primers:
36 """A primer set designed by Primer3. 37 38 Members: 39 40 size 41 forward_seq 42 forward_start 43 forward_length 44 forward_tm 45 forward_gc 46 reverse_seq 47 reverse_start 48 reverse_length 49 reverse_tm 50 reverse_gc 51 """
52 - def __init__(self):
53 self.size = 0 54 self.forward_seq = "" 55 self.forward_start = 0 56 self.forward_length = 0 57 self.forward_tm = 0.0 58 self.forward_gc = 0.0 59 self.reverse_seq = "" 60 self.reverse_start = 0 61 self.reverse_length = 0 62 self.reverse_tm = 0.0 63 self.reverse_gc = 0.0
64 65
66 -def parse(handle):
67 """Iterate over primer3 output as Bio.Emboss.Primer3.Record objects. 68 """ 69 # Skip blank lines at head of file 70 while True: 71 line = handle.readline() 72 if line.strip(): 73 break # Starting a record 74 75 # Read each record 76 record = None 77 primer = None 78 while True: 79 if line.startswith('# EPRIMER3') or line.startswith('# PRIMER3'): 80 # Record data 81 if record is not None: 82 yield record 83 record = Record() 84 record.comments += line 85 primer = None 86 elif line.startswith('#'): 87 if line.strip() != '# Start Len Tm GC% Sequence': 88 record.comments += line 89 elif not line.strip(): 90 pass 91 elif line[5:19]=="PRODUCT SIZE: ": 92 primer = Primers() 93 primer.size = int(line[19:]) 94 record.primers.append(primer) 95 elif line[5:19]=="FORWARD PRIMER": 96 words = line.split() 97 if not primer or primer.size==0: 98 primer = Primers() 99 record.primers.append(primer) 100 primer.forward_start = int(words[2]) 101 primer.forward_length = int(words[3]) 102 primer.forward_tm = float(words[4]) 103 primer.forward_gc = float(words[5]) 104 primer.forward_seq = words[6] 105 elif line[5:19]=="REVERSE PRIMER": 106 words = line.split() 107 if not primer or primer.size==0: 108 primer = Primers() 109 record.primers.append(primer) 110 primer.reverse_start = int(words[2]) 111 primer.reverse_length = int(words[3]) 112 primer.reverse_tm = float(words[4]) 113 primer.reverse_gc = float(words[5]) 114 primer.reverse_seq = words[6] 115 elif line[5:19]=="INTERNAL OLIGO": 116 words = line.split() 117 if not primer or primer.size==0: 118 primer = Primers() 119 record.primers.append(primer) 120 primer.internal_start = int(words[2]) 121 primer.internal_length = int(words[3]) 122 primer.internal_tm = float(words[4]) 123 primer.internal_gc = float(words[5]) 124 primer.internal_seq = words[6] 125 try: 126 line = handle.next() 127 except StopIteration: 128 break 129 if record: 130 yield record
131 132
133 -def read(handle):
134 """Parse primer3 output into a Bio.Emboss.Primer3.Record object. 135 136 This is for when there is one and only one target sequence. If 137 designing primers for multiple sequences, use the parse function. 138 """ 139 iterator = parse(handle) 140 try: 141 first = iterator.next() 142 except StopIteration: 143 raise ValueError("No records found in handle") 144 try: 145 second = iterator.next() 146 except StopIteration: 147 second = None 148 if second is not None: 149 raise ValueError("More than one record found in handle") 150 return first
151