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