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