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