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   
23 -class Record(object):
24 """Represent information from a primer3 run finding primers. 25 26 Members: 27 28 primers - list of Primer objects describing primer pairs for 29 this target sequence. 30 comments - the comment line(s) for the record 31 """
32 - def __init__(self):
33 self.comments = "" 34 self.primers = []
35 36
37 -class Primers(object):
38 """A primer set designed by Primer3. 39 40 Members: 41 42 size - length of product, note you can use len(primer) as an 43 alternative to primer.size 44 45 forward_seq 46 forward_start 47 forward_length 48 forward_tm 49 forward_gc 50 51 reverse_seq 52 reverse_start 53 reverse_length 54 reverse_tm 55 reverse_gc 56 57 internal_seq 58 internal_start 59 internal_length 60 internal_tm 61 internal_gc 62 """
63 - def __init__(self):
64 self.size = 0 65 self.forward_seq = "" 66 self.forward_start = 0 67 self.forward_length = 0 68 self.forward_tm = 0.0 69 self.forward_gc = 0.0 70 self.reverse_seq = "" 71 self.reverse_start = 0 72 self.reverse_length = 0 73 self.reverse_tm = 0.0 74 self.reverse_gc = 0.0 75 self.internal_seq = "" 76 self.internal_start = 0 77 self.internal_length = 0 78 self.internal_tm = 0.0 79 self.internal_gc = 0.0
80
81 - def __len__(self):
82 """Length of the primer product (i.e. product size).""" 83 return self.size
84 85
86 -def parse(handle):
87 """Iterate over primer3 output as Bio.Emboss.Primer3.Record objects. 88 """ 89 # Skip blank lines at head of file 90 while True: 91 line = handle.readline() 92 if line.strip(): 93 break # Starting a record 94 95 # Read each record 96 record = None 97 primer = None 98 while True: 99 if line.startswith('# EPRIMER3') or line.startswith('# PRIMER3'): 100 # Record data 101 if record is not None: 102 yield record 103 record = Record() 104 record.comments += line 105 primer = None 106 elif line.startswith('#'): 107 if line.strip() != '# Start Len Tm GC% Sequence': 108 record.comments += line 109 elif not line.strip(): 110 pass 111 elif line[5:19]=="PRODUCT SIZE: ": 112 primer = Primers() 113 primer.size = int(line[19:]) 114 record.primers.append(primer) 115 elif line[5:19]=="FORWARD PRIMER": 116 words = line.split() 117 if not primer or primer.size==0: 118 primer = Primers() 119 record.primers.append(primer) 120 primer.forward_start = int(words[2]) 121 primer.forward_length = int(words[3]) 122 primer.forward_tm = float(words[4]) 123 primer.forward_gc = float(words[5]) 124 primer.forward_seq = words[6] 125 elif line[5:19]=="REVERSE PRIMER": 126 words = line.split() 127 if not primer or primer.size==0: 128 primer = Primers() 129 record.primers.append(primer) 130 primer.reverse_start = int(words[2]) 131 primer.reverse_length = int(words[3]) 132 primer.reverse_tm = float(words[4]) 133 primer.reverse_gc = float(words[5]) 134 primer.reverse_seq = words[6] 135 elif line[5:19]=="INTERNAL OLIGO": 136 words = line.split() 137 if not primer or primer.size==0: 138 primer = Primers() 139 record.primers.append(primer) 140 primer.internal_start = int(words[2]) 141 primer.internal_length = int(words[3]) 142 primer.internal_tm = float(words[4]) 143 primer.internal_gc = float(words[5]) 144 try: 145 primer.internal_seq = words[6] 146 except IndexError: # eprimer3 reports oligo without sequence 147 primer.internal_seq = '' 148 try: 149 line = next(handle) 150 except StopIteration: 151 break 152 if record: 153 yield record
154 155
156 -def read(handle):
157 """Parse primer3 output into a Bio.Emboss.Primer3.Record object. 158 159 This is for when there is one and only one target sequence. If 160 designing primers for multiple sequences, use the parse function. 161 """ 162 iterator = parse(handle) 163 try: 164 first = next(iterator) 165 except StopIteration: 166 raise ValueError("No records found in handle") 167 try: 168 second = next(iterator) 169 except StopIteration: 170 second = None 171 if second is not None: 172 raise ValueError("More than one record found in handle") 173 return first
174