~ubuntu-branches/ubuntu/trusty/python-biopython/trusty-proposed

« back to all changes in this revision

Viewing changes to Bio/Emboss/Primer.py

  • Committer: Bazaar Package Importer
  • Author(s): Philipp Benner
  • Date: 2009-08-24 09:29:27 UTC
  • mfrom: (1.3.3 upstream) (4.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090824092927-2wwh36vwnfgn98kl
Tags: 1.51final-2
Merged patch from ubuntu to make the package ready for
python2.6 (Closes: #543236).

Show diffs side-by-side

added added

removed removed

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