1
"""Code to interact with Primer-related programs from EMBOSS (DEPRECATED).
3
Bio.Emboss.Primer has been deprecated, please use Bio.Emboss.Primer3 or
4
Bio.Emboss.PrimerSearch instead.
6
To parse primersearch output into a PrimerSearch.OutputRecord, use
7
from Bio.Emboss import PrimerSearch
8
handle = open('myprimersearchoutputfile.txt')
9
record = PrimerSearch.read(handle)
11
To parse primer3 output into a Primer3.Record, use
12
from Bio.Emboss import Primer3
13
handle = open('myprimer3outputfile.txt')
14
record = Primer3.read(handle)
19
Bio.Emboss.Primer has been deprecated.
20
Please use Bio.Emboss.Primer3 or Bio.Emboss.PrimerSearch instead.
22
To parse primersearch output into a PrimerSearch.OutputRecord, use
23
from Bio.Emboss import PrimerSearch
24
handle = open('myprimersearchoutputfile.txt')
25
record = PrimerSearch.read(handle)
27
To parse primer3 output into a Primer3.Record, use
28
from Bio.Emboss import Primer3
29
handle = open('myprimer3outputfile.txt')
30
record = Primer3.read(handle)
31
""", DeprecationWarning)
35
from xml.sax import handler
41
from Bio.ParserSupport import AbstractConsumer
42
from Bio.ParserSupport import EventGenerator
44
import primersearch_format
49
class PrimerSearchInputRecord:
50
"""Represent the input file into the primersearch program.
52
This makes it easy to add primer information and write it out to the
53
simple primer file format.
60
for name, primer1, primer2 in self.primer_info:
61
output += "%s %s %s\n" % (name, primer1, primer2)
64
def add_primer_set(self, primer_name, first_primer_seq,
66
"""Add primer information to the record.
68
self.primer_info.append((primer_name, first_primer_seq,
71
class PrimerSearchParser:
72
"""Parse primersearch output into a PrimerSearchOutputRecord.
74
def __init__(self, debug_level = 0):
75
self._scanner = _PrimerSearchScanner(debug_level)
77
def parse(self, handle):
78
self._consumer = _PrimerSearchRecordConsumer()
79
self._scanner.feed(handle, self._consumer)
80
return self._consumer.data
82
class PrimerSearchOutputRecord:
83
"""Represent the information from a primersearch job.
85
amplifiers is a dictionary where the keys are the primer names and
86
the values are a list of PrimerSearchAmplifier objects.
91
class PrimerSearchAmplifier:
92
"""Represent a single amplification from a primer.
98
class _PrimerSearchRecordConsumer(AbstractConsumer):
99
"""Get output from primersearch into a PrimerSearchOutputRecord
102
self.data = PrimerSearchOutputRecord()
103
self._cur_primer = None
104
self._cur_amplifier = None
106
def _add_last_amplifier(self):
107
# add on the last amplifier
108
if self._cur_primer is not None and self._cur_amplifier is not None:
109
self.data.amplifiers[self._cur_primer].append(self._cur_amplifier)
111
def primer_name(self, name):
112
self._add_last_amplifier()
113
self.data.amplifiers[name] = []
114
self._cur_primer = name
116
def amplifier(self, amplifier_num):
117
self._add_last_amplifier()
118
self._cur_amplifier = PrimerSearchAmplifier()
120
def amplifier_sequence(self, sequence_info):
121
self._cur_amplifier.hit_info = sequence_info
123
def amplifier_length(self, amplifier_info):
124
self._cur_amplifier.length = int(amplifier_info)
126
def end_record(self):
127
self._add_last_amplifier()
129
class _PrimerSearchScanner:
130
"""Scan output from the primersearch program.
132
def __init__(self, debug = 0):
133
self.interest_tags = ["primer_name", "amplifier",
134
"amplifier_sequence", "amplifier_length",
137
expression = Martel.select_names(primersearch_format.record,
139
self._parser = expression.make_parser(debug_level = debug)
141
def feed(self, handle, consumer):
142
self._parser.setContentHandler(EventGenerator(consumer,
145
self._parser.setErrorHandler(handler.ErrorHandler())
146
self._parser.parseFile(handle)
148
consumer.end_record()
153
"""Represent information from a primer3 run finding primers.
157
primers A list of primers that are generated (usually 5)
163
class Primer3Primers:
164
"""A primer set designed by Primer3.
182
self.forward_seq = ""
183
self.forward_start = 0
184
self.forward_length = 0
185
self.forward_tm = 0.0
186
self.forward_gc = 0.0
187
self.reverse_seq = ""
188
self.reverse_start = 0
189
self.reverse_length = 0
190
self.reverse_tm = 0.0
191
self.reverse_gc = 0.0
194
"""Parse primer3 output into a Primer3Record.
196
def __init__(self, debug_level = 0):
197
self._scanner = _Primer3Scanner(debug_level)
199
def parse(self, handle):
200
self._consumer = _Primer3RecordConsumer()
201
self._scanner.feed(handle, self._consumer)
202
return self._consumer.data
204
class _Primer3RecordConsumer(AbstractConsumer):
205
"""Get output from prime3 into a Primer3Record
208
self.data = Primer3Record()
209
self._cur_primer = None
211
def _add_last_primer(self):
212
# add on the last amplifier
213
if self._cur_primer is not None:
214
self.data.primers.append(self._cur_primer)
216
def comments(self, comment):
217
self.data.comments = comment
219
def start_primer(self, junk):
220
self._add_last_primer()
221
self._cur_primer = Primer3Primers()
223
def single_primer_line(self, junk):
224
self.start_primer(junk)
226
def product_size(self, size):
227
self._cur_primer.size = int(size)
229
def forward_start(self, start):
230
self._cur_primer.forward_start = int(start)
232
def forward_length(self, length):
233
self._cur_primer.forward_length = int(length)
235
def forward_tm(self, tm):
236
self._cur_primer.forward_tm = float(tm)
238
def forward_gc(self, gc):
239
self._cur_primer.forward_gc = float(gc)
241
def forward_seq(self, seq):
242
self._cur_primer.forward_seq = seq
244
def reverse_start(self, start):
245
self._cur_primer.reverse_start = int(start)
247
def reverse_length(self, length):
248
self._cur_primer.reverse_length = int(length)
250
def reverse_tm(self, tm):
251
self._cur_primer.reverse_tm = float(tm)
253
def reverse_gc(self, gc):
254
self._cur_primer.reverse_gc = float(gc)
256
def reverse_seq(self, seq):
257
self._cur_primer.reverse_seq = seq
259
def internal_start(self, start):
260
self._cur_primer.internal_start = int(start)
262
def internal_length(self, length):
263
self._cur_primer.internal_length = int(length)
265
def internal_tm(self, tm):
266
self._cur_primer.internal_tm = float(tm)
268
def internal_gc(self, gc):
269
self._cur_primer.internal_gc = float(gc)
271
def internal_seq(self, seq):
272
self._cur_primer.internal_seq = seq
274
def end_record(self):
275
self._add_last_primer()
277
class _Primer3Scanner:
278
"""Scan output from the primer3 program.
280
def __init__(self, debug = 0):
281
self.interest_tags = ["comments", "single_primer_line",
282
"start_primer", "product_size",
283
"forward_start", "forward_length",
284
"forward_tm", "forward_gc", "forward_seq",
285
"reverse_start", "reverse_length",
286
"reverse_tm", "reverse_gc", "reverse_seq",
287
"internal_start", "internal_length",
288
"internal_tm", "internal_gc", "internal_seq",
291
expression = Martel.select_names(primer3_format.record,
293
self._parser = expression.make_parser(debug_level = debug)
295
def feed(self, handle, consumer):
296
self._parser.setContentHandler(EventGenerator(consumer,
299
self._parser.setErrorHandler(handler.ErrorHandler())
300
self._parser.parseFile(handle)
302
consumer.end_record()
304
def _strip_and_combine(line_list):
305
"""Combine multiple lines of content separated by spaces.
307
# first strip out extra whitespace
308
stripped_line_list = map(string.strip, line_list)
309
# now combine everything with spaces
310
return ' '.join(stripped_line_list)