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

Source Code for Module Bio.Emboss.Primer

  1  """Code to interact with various Primer-related programs from EMBOSS. 
  2  """ 
  3  # standard library 
  4  import string 
  5  from xml.sax import handler 
  6   
  7  # Martel 
  8  import Martel 
  9  from Martel import RecordReader 
 10   
 11  # biopython stuff 
 12  from Bio.ParserSupport import AbstractConsumer 
 13  from Bio.ParserSupport import EventGenerator 
 14   
 15  import primersearch_format 
 16  import primer3_format 
 17   
 18  # --- primersearch 
 19   
20 -class PrimerSearchInputRecord:
21 """Represent the input file into the primersearch program. 22 23 This makes it easy to add primer information and write it out to the 24 simple primer file format. 25 """
26 - def __init__(self):
27 self.primer_info = []
28
29 - def __str__(self):
30 output = "" 31 for name, primer1, primer2 in self.primer_info: 32 output += "%s %s %s\n" % (name, primer1, primer2) 33 return output
34
35 - def add_primer_set(self, primer_name, first_primer_seq, 36 second_primer_seq):
37 """Add primer information to the record. 38 """ 39 self.primer_info.append((primer_name, first_primer_seq, 40 second_primer_seq))
41
42 -class PrimerSearchParser:
43 """Parse primersearch output into a PrimerSearchOutputRecord. 44 """
45 - def __init__(self, debug_level = 0):
46 self._scanner = _PrimerSearchScanner(debug_level)
47
48 - def parse(self, handle):
49 self._consumer = _PrimerSearchRecordConsumer() 50 self._scanner.feed(handle, self._consumer) 51 return self._consumer.data
52
53 -class PrimerSearchOutputRecord:
54 """Represent the information from a primersearch job. 55 56 amplifiers is a dictionary where the keys are the primer names and 57 the values are a list of PrimerSearchAmplifier objects. 58 """
59 - def __init__(self):
60 self.amplifiers = {}
61
62 -class PrimerSearchAmplifier:
63 """Represent a single amplification from a primer. 64 """
65 - def __init__(self):
66 self.hit_info = "" 67 self.length = 0
68
69 -class _PrimerSearchRecordConsumer(AbstractConsumer):
70 """Get output from primersearch into a PrimerSearchOutputRecord 71 """
72 - def __init__(self):
73 self.data = PrimerSearchOutputRecord() 74 self._cur_primer = None 75 self._cur_amplifier = None
76
77 - def _add_last_amplifier(self):
78 # add on the last amplifier 79 if self._cur_primer is not None and self._cur_amplifier is not None: 80 self.data.amplifiers[self._cur_primer].append(self._cur_amplifier)
81
82 - def primer_name(self, name):
83 self._add_last_amplifier() 84 self.data.amplifiers[name] = [] 85 self._cur_primer = name
86
87 - def amplifier(self, amplifier_num):
88 self._add_last_amplifier() 89 self._cur_amplifier = PrimerSearchAmplifier()
90
91 - def amplifier_sequence(self, sequence_info):
92 self._cur_amplifier.hit_info = sequence_info
93
94 - def amplifier_length(self, amplifier_info):
95 self._cur_amplifier.length = int(amplifier_info)
96
97 - def end_record(self):
99
100 -class _PrimerSearchScanner:
101 """Scan output from the primersearch program. 102 """
103 - def __init__(self, debug = 0):
104 self.interest_tags = ["primer_name", "amplifier", 105 "amplifier_sequence", "amplifier_length", 106 "end_record"] 107 108 expression = Martel.select_names(primersearch_format.record, 109 self.interest_tags) 110 self._parser = expression.make_parser(debug_level = debug)
111
112 - def feed(self, handle, consumer):
113 self._parser.setContentHandler(EventGenerator(consumer, 114 self.interest_tags, 115 _strip_and_combine)) 116 self._parser.setErrorHandler(handler.ErrorHandler()) 117 self._parser.parseFile(handle) 118 119 consumer.end_record()
120 121 # --- primer3 122
123 -class Primer3Record:
124 """Represent information from a primer3 run finding primers. 125 126 Members: 127 128 primers A list of primers that are generated (usually 5) 129 """
130 - def __init__(self):
131 self.comments = "" 132 self.primers = []
133
134 -class Primer3Primers:
135 """A primer set designed by Primer3. 136 137 Members: 138 139 size 140 forward_seq 141 forward_start 142 forward_length 143 forward_tm 144 forward_gc 145 reverse_seq 146 reverse_start 147 reverse_length 148 reverse_tm 149 reverse_gc 150 """
151 - def __init__(self):
152 self.size = 0 153 self.forward_seq = "" 154 self.forward_start = 0 155 self.forward_length = 0 156 self.forward_tm = 0.0 157 self.forward_gc = 0.0 158 self.reverse_seq = "" 159 self.reverse_start = 0 160 self.reverse_length = 0 161 self.reverse_tm = 0.0 162 self.reverse_gc = 0.0
163
164 -class Primer3Parser:
165 """Parse primer3 output into a Primer3Record. 166 """
167 - def __init__(self, debug_level = 0):
168 self._scanner = _Primer3Scanner(debug_level)
169
170 - def parse(self, handle):
171 self._consumer = _Primer3RecordConsumer() 172 self._scanner.feed(handle, self._consumer) 173 return self._consumer.data
174
175 -class _Primer3RecordConsumer(AbstractConsumer):
176 """Get output from prime3 into a Primer3Record 177 """
178 - def __init__(self):
179 self.data = Primer3Record() 180 self._cur_primer = None
181
182 - def _add_last_primer(self):
183 # add on the last amplifier 184 if self._cur_primer is not None: 185 self.data.primers.append(self._cur_primer)
186
187 - def comments(self, comment):
188 self.data.comments = comment
189
190 - def start_primer(self, junk):
191 self._add_last_primer() 192 self._cur_primer = Primer3Primers()
193
194 - def single_primer_line(self, junk):
195 self.start_primer(junk)
196
197 - def product_size(self, size):
198 self._cur_primer.size = int(size)
199
200 - def forward_start(self, start):
201 self._cur_primer.forward_start = int(start)
202
203 - def forward_length(self, length):
204 self._cur_primer.forward_length = int(length)
205
206 - def forward_tm(self, tm):
207 self._cur_primer.forward_tm = float(tm)
208
209 - def forward_gc(self, gc):
210 self._cur_primer.forward_gc = float(gc)
211
212 - def forward_seq(self, seq):
213 self._cur_primer.forward_seq = seq
214
215 - def reverse_start(self, start):
216 self._cur_primer.reverse_start = int(start)
217
218 - def reverse_length(self, length):
219 self._cur_primer.reverse_length = int(length)
220
221 - def reverse_tm(self, tm):
222 self._cur_primer.reverse_tm = float(tm)
223
224 - def reverse_gc(self, gc):
225 self._cur_primer.reverse_gc = float(gc)
226
227 - def reverse_seq(self, seq):
228 self._cur_primer.reverse_seq = seq
229
230 - def internal_start(self, start):
231 self._cur_primer.internal_start = int(start)
232
233 - def internal_length(self, length):
234 self._cur_primer.internal_length = int(length)
235
236 - def internal_tm(self, tm):
237 self._cur_primer.internal_tm = float(tm)
238
239 - def internal_gc(self, gc):
240 self._cur_primer.internal_gc = float(gc)
241
242 - def internal_seq(self, seq):
243 self._cur_primer.internal_seq = seq
244
245 - def end_record(self):
246 self._add_last_primer()
247
248 -class _Primer3Scanner:
249 """Scan output from the primer3 program. 250 """
251 - def __init__(self, debug = 0):
252 self.interest_tags = ["comments", "single_primer_line", 253 "start_primer", "product_size", 254 "forward_start", "forward_length", 255 "forward_tm", "forward_gc", "forward_seq", 256 "reverse_start", "reverse_length", 257 "reverse_tm", "reverse_gc", "reverse_seq", 258 "internal_start", "internal_length", 259 "internal_tm", "internal_gc", "internal_seq", 260 "end_record"] 261 262 expression = Martel.select_names(primer3_format.record, 263 self.interest_tags) 264 self._parser = expression.make_parser(debug_level = debug)
265
266 - def feed(self, handle, consumer):
267 self._parser.setContentHandler(EventGenerator(consumer, 268 self.interest_tags, 269 _strip_and_combine)) 270 self._parser.setErrorHandler(handler.ErrorHandler()) 271 self._parser.parseFile(handle) 272 273 consumer.end_record()
274
275 -def _strip_and_combine(line_list):
276 """Combine multiple lines of content separated by spaces. 277 """ 278 # first strip out extra whitespace 279 stripped_line_list = map(string.strip, line_list) 280 # now combine everything with spaces 281 return string.join(stripped_line_list, ' ')
282