1
2
3 """This module is DEPRECATED.
4
5 Andrew Dalke is no longer maintaining Martel or Bio.Mindy, and these modules
6 and associate ones like Bio.StdHandler are now deprecated. They are no longer
7 used in any of the current Biopython parsers, and are likely to be removed
8 in a future release.
9 """
10
11 import warnings
12 warnings.warn("Martel and those parts of Biopython depending on it" \
13 +" directly (such as Bio.Mindy and Bio.StdHandler) are now" \
14 +" deprecated, and will be removed in a future release of"\
15 +" Biopython. If you want to continue to use this code,"\
16 +" please get in contact with the Biopython developers via"\
17 +" the mailing lists to avoid its permanent removal from"\
18 +" Biopython.", \
19 DeprecationWarning)
20
21 from xml.sax import handler
22 from Martel import Parser, Dispatch
23 from Bio import Std, Decode
24
25
26
27
28
30 assert not hasattr(klass, "start_" +tag), "existing method exists"
31 assert not hasattr(klass, "end_" +tag), "existing method exists"
32 s = """if 1:
33 def start(self, tag, attrs):
34 self.save_characters()
35 def end(self, tag):
36 self.%s = int(self.get_characters())
37 """ % attrname
38 d = {}
39 exec s in d
40 setattr(klass, "start_" + tag, d["start"])
41 setattr(klass, "end_" + tag, d["end"])
42
43 -def add_text_handler(klass, tag, attrname):
44 assert not hasattr(klass, "start_" +tag), "existing method exists"
45 assert not hasattr(klass, "end_" +tag), "existing method exists"
46 s = """if 1:
47 def start(self, tag, attrs):
48 self.save_characters()
49 def end(self, tag):
50 self.%s = self.get_characters()
51 """ % attrname
52 d = {}
53 exec s in d
54 setattr(klass, "start_" + tag, d["start"])
55 setattr(klass, "end_" + tag, d["end"])
56
57 -def add_text_dict_handler(klass, tag, attrname, key):
58 assert not hasattr(klass, "start_" +tag), "existing method exists"
59 assert not hasattr(klass, "end_" +tag), "existing method exists"
60 s = """if 1:
61 def start(self, tag, attrs):
62 self.save_characters()
63 def end(self, tag):
64 self.%s["%s"] = self.get_characters()
65 """ % (attrname, key)
66 d = {}
67 exec s in d
68 setattr(klass, "start_" + tag, d["start"])
69 setattr(klass, "end_" + tag, d["end"])
70
71 -def add_text_decode_handler(klass, tag, attrname):
72 assert not hasattr(klass, "start_" +tag), "existing method exists"
73 assert not hasattr(klass, "end_" +tag), "existing method exists"
74 s = """if 1:
75 def start(self, tag, attrs):
76 self.save_characters()
77 self._decode_%s = attrs.get("bioformat:decode", None)
78 def end(self, tag):
79 if self._decode_%s is not None:
80 s = Decode.make_decoder(self._decode_%s)(s)
81 self.%s = self.get_characters()
82 """ % (tag, tag, tag, attrname)
83 d = {"Decode": Decode}
84 exec s in d
85 setattr(klass, "start_" + tag, d["start"])
86 setattr(klass, "end_" + tag, d["end"])
87
88 -def add_first_text_handler(klass, tag, attrname):
89 assert not hasattr(klass, "start_" +tag), "existing method exists"
90 assert not hasattr(klass, "end_" +tag), "existing method exists"
91 s = """if 1:
92 def start(self, tag, attrs):
93 if self.%s is None:
94 self.save_characters()
95 def end(self, tag):
96 if self.%s is None:
97 self.%s = self.get_characters()
98 """ % (attrname, attrname, attrname)
99 d = {}
100 exec s in d
101 setattr(klass, "start_" + tag, d["start"])
102 setattr(klass, "end_" + tag, d["end"])
103
104 -def add_text_block_handler(klass, tag, joinattr, defaultjoin, attrname):
105 assert not hasattr(klass, "start_" + tag), "existing method exists"
106 assert not hasattr(klass, "end_" + tag), "existing method exists"
107 assert not hasattr(klass, "start_"+tag+"_block"), "existing method exists"
108 assert not hasattr(klass, "end_" +tag+"_block"), "existing method exists"
109 s = """if 1:
110 def start_block(self, tag, attrs):
111 self._%(tag)s_join_func = Decode.make_decoder(attrs.get(%(joinattr)r, %(defaultjoin)r))
112 self._%(tag)s_lines = []
113 def end_block(self, tag):
114 self.%(attrname)s = self._%(tag)s_join_func(self._%(tag)s_lines)
115 def start(self, tag, attrs):
116 self.save_characters()
117 def end(self, tag):
118 self._%(tag)s_lines.append(self.get_characters())
119 """ % locals()
120 d = {"Decode": Decode}
121 exec s in d
122 setattr(klass, "start_" + tag, d["start"])
123 setattr(klass, "end_" + tag, d["end"])
124 setattr(klass, "start_" + tag + "_block", d["start_block"])
125 setattr(klass, "end_" + tag + "_block", d["end_block"])
126
128 assert not hasattr(klass, "start_" +tag), "existing method exists"
129 assert not hasattr(klass, "end_" +tag), "existing method exists"
130 s = """if 1:
131 def start(self, tag, attrs):
132 self._%(tag)s_name = attrs["name"]
133 self._%(tag)s_decode = attrs.get("bioformat:decode", None)
134 self.save_characters()
135 def end(self, tag):
136 s = self.get_characters()
137 if self._%(tag)s_decode is not None:
138 s = Decode.make_decoder(self._%(tag)s_decode)(s)
139 self.%(attrname)s[self._%(tag)s_name] = s
140 """ % locals()
141 d = {"Decode": Decode}
142 exec s in d
143 setattr(klass, "start_" + tag, d["start"])
144 setattr(klass, "end_" + tag, d["end"])
145
146
147
148
150 """Used to read records and produce output"""
151 - def __init__(self, record_builder, writer, record_tag = "record"):
152 handler.ContentHandler.__init__(self)
153 self.record_builder = record_builder
154 self.writer = writer
155 self.record_tag = record_tag
156
160
169
178
181
183 """Used to read records and produce output through a Dispatcher"""
184 - def __init__(self, record_builder, writer, record_tag = "record"):
185 setattr(self, "end_" + record_tag, self.write_record)
186 Dispatch.Dispatcher.__init__(self,
187 remap = {record_tag: "bioformat:"}
188 )
189 self.acquire(record_builder)
190 self.record_builder = record_builder
191 self.writer = writer
192 self.record_tag = record_tag
194 self.writer.write(self.record_builder.document)
195
196
197
200 self.recognized = 1
201 self.exc = None
202
210
211 error = fatalError
212
216
217
218
227
228
243 self.callback(self.join_fctn(self.descriptions))
244
245
246
247
248
249
250
251
252
253
254
255
256
257
280
281
282
283
284
285 _fast_dbxref_sp_general_data = None
287 global _fast_dbxref_sp_general_data
288 if _fast_dbxref_sp_general_data is None:
289 from Bio.expressions.swissprot import sprot38
290 _fast_dbxref_sp_general_data = _fixup_sp_pattern(
291 sprot38.real_DR_general)
292
293 pat, dbstyle, primary_type, secondary_type = _fast_dbxref_sp_general_data
294
295 m = pat.match(s)
296 assert m is not None, "Ill-formated sp-general dxbref: %r" % s
297 return (
298 (dbstyle, m.group("dbname"), primary_type,
299 m.group("primary_dbid"), 0),
300 (dbstyle, m.group("dbname"), secondary_type,
301 m.group("secondary_dbid"), 0)
302 )
303
304
305
306
307 _fast_dbxref_sp_prosite_data = None
309 global _fast_dbxref_sp_prosite_data
310
311 if _fast_dbxref_sp_prosite_data is None:
312 from Bio.expressions.swissprot import sprot38
313 _fast_dbxref_sp_prosite_data = _fixup_sp_pattern(
314 sprot38.real_DR_prosite)
315
316 pat, dbstyle, primary_type, secondary_type = _fast_dbxref_sp_prosite_data
317 m = pat.match(s)
318 assert m is not None, "Ill-formated sp-prosite dxbref: %r" % s
319 return (
320 (dbstyle, m.group("dbname"), primary_type,
321 m.group("primary_dbid"), 0),
322 (dbstyle, m.group("dbname"), secondary_type,
323 m.group("secondary_dbid"), 0)
324 )
325
326
327
328 _fast_dbxref_sp_embl_data = None
330 global _fast_dbxref_sp_embl_data
331
332 if _fast_dbxref_sp_embl_data is None:
333 from Bio.expressions.swissprot import sprot38
334 _fast_dbxref_sp_embl_data = _fixup_sp_pattern(
335 sprot38.real_DR_embl)
336
337 pat, dbstyle, primary_type, secondary_type = _fast_dbxref_sp_embl_data
338 m = pat.match(s)
339 assert m is not None, "Ill-formated sp-embl dxbref: %r" % s
340 return (
341 (dbstyle, m.group("dbname"), primary_type,
342 m.group("primary_dbid"), 0),
343 (dbstyle, m.group("dbname"), secondary_type,
344 m.group("secondary_dbid"), 0)
345 )
346
347 _fast_dbxref_parser_table = {
348 "sp-general": _fast_dbxref_sp_general,
349 "sp-prosite": _fast_dbxref_sp_prosite,
350 "sp-embl": _fast_dbxref_sp_embl,
351 }
352
359 self.negate = 0
360 self.dbname = None
361 self.dbids = []
362 self.info = []
363
365 assert self.dbname is None, "cannot set the dbname twice"
366 self.dbname_style = attrs.get("style", "unknown")
367 self.save_characters()
370
372 d = attrs.get("dbname", None)
373 if d is None:
374 assert self.dbname is not None, "must set the dbname"
375 self.info.append( (self.dbname_style, self.dbname,
376 attrs.get("type", "primary")) )
377 else:
378 self.info.append( ("bioformat", d,
379 attrs.get("type", "primary")) )
380 self.save_characters()
381
384
387
389 cb = self.slow_callback
390 if cb is None:
391 return
392 negate = self.negate
393 for ( (dbname_style, dbname, idtype), dbid) in zip(self.info,
394 self.dbids):
395 self.slow_callback(dbname_style, dbname, idtype, dbid, negate)
396
403 for info in self._fast_parser(self.get_characters()):
404 self.callback(*info)
405 self.slow_callback = self.callback
406
407
409 global_alphabet = None
410 - def start_(self, tag, attrs):
412
414 self.local_alphabet = attrs.get("alphabet", None)
415 self.gapchar = attrs.get("gapchar", None)
416 self.stopchar = attrs.get("stopchar", None)
417 j = attrs.get("join", None)
418 if j is not None:
419 self.join_func = Decode.make_typechecked_decoder(j, list, str)
420 else:
421 self.join_func = None
422 self.sequences = []
423
432
435
440
442 - def __init__(self, name, description, location, qualifiers):
448 return "Feature %r %r %s num_qualifiers = %d" % \
449 (self.name, self.description, self.location,
450 len(self.qualifiers))
451
452
454 - def __init__(self, callback, settings = {}):
457
459 self.location_style = attrs.get("location-style",
460 self.settings["location-style"])
461 j = attrs.get("join-feature", None)
462 if j is None:
463 self.text_join_func = "".join
464 else:
465 self.text_join_func = Decode.make_typechecked_decoder(j, list, str)
466
467 self.location_start = None
468 self.location_end = None
469 self.text_lines = []
470
472 if self.location_start or self.location_end:
473 if self.text_lines:
474 raise TypeError("Cannot have both location text and start/end")
475 self.callback(self.location_style,
476 (self.location_start, self.location_end))
477 else:
478 self.callback(self.location_style,
479 (self.text_join_func(self.text_lines), None))
480
485
486 add_text_handler(Handle_feature_location, "feature_location_start",
487 "location_start")
488 add_text_handler(Handle_feature_location, "feature_location_end",
489 "location_end")
490
491
492
494 - def __init__(self, callback, settings):
497
506
509
514
515 add_text_handler(Handle_feature_qualifier, "feature_qualifier_name", "name")
516
517
518
528
546
550
556
561
568
571
573 self.location = (style, location_info)
574
575 add_text_handler(Handle_features, "feature_name", "name")
576
577
578
579
582 self.query_name = None
583 self.subject_name = None
584
585 self.query_seq = ""
586 self.homology_seq = ""
587 self.subject_seq = ""
588
589 self.query_start_loc = None
590 self.query_end_loc = None
591
592 self.subject_start_loc = None
593 self.subject_end_loc = None
594
597
599 self.sub_leader = None
600
604 s = self.get_characters()
605 self.query_seq += s
606 self.sub_query_seq_len = len(s)
607
611 query_leader = self.leader_size
612 query_seq_len = self.sub_query_seq_len
613 line = self.get_characters()
614 s = line[query_leader:query_leader+query_seq_len]
615 assert len(s) == query_seq_len, (len(s), query_seq_len, line)
616 self.homology_seq += s
617
622
627
628 add_first_text_handler(Handle_hsp_seqalign, "hsp_seqalign_query_name",
629 "query_name")
630
631 add_first_text_handler(Handle_hsp_seqalign, "hsp_seqalign_subject_name",
632 "subject_name")
633
634 add_first_text_handler(Handle_hsp_seqalign, "hsp_seqalign_query_start",
635 "query_start_loc")
636 add_text_handler(Handle_hsp_seqalign, "hsp_seqalign_query_end",
637 "query_end_loc")
638
639 add_first_text_handler(Handle_hsp_seqalign, "hsp_seqalign_subject_start",
640 "subject_start_loc")
641 add_text_handler(Handle_hsp_seqalign, "hsp_seqalign_subject_end",
642 "subject_end_loc")
643
644
645
646
647
648
653
655 self.hsp_values = {}
656 self.strands = {}
657 self.frames = {}
658
660 self.callback(self.hsp_values,
661 self.hsp_info,
662 self.strands, self.frames,
663 )
664
666 self.strands[attrs["which"]] = attrs["strand"]
667
671
673 self.frames[self.getting_frame] = self.get_characters()
674 self.getting_frame = None
675
677 self.hsp_info = hsp_info
678
680 self.value_convert = attrs.get("bioformat:decode", None)
681 self.value_name = attrs["name"]
682 self.save_characters()
683
685 s = self.get_characters()
686 if self.value_name is not None:
687 if self.value_name == "float":
688 s = float(s)
689 else:
690 s = Decode.make_decoder(self.value_convert)(s)
691 self.hsp_values[self.value_name] = s
692
693
694
695
726
727 add_text_handler(Handle_search_table, "search_table_description",
728 "description")
729
730
731
733 - def start_(self, tag, attrs):
736
741
742 add_text_block_handler(Handle_search_header, "query_description",
743 "join-query", "join|fixspaces", "query_description")
744
745 add_text_dict_handler(Handle_search_header, "application_name",
746 "dict", "appname")
747 add_text_dict_handler(Handle_search_header, "application_version",
748 "dict", "appversion")
749 add_text_dict_handler(Handle_search_header, "database_name",
750 "dict", "dbname")
751 add_text_dict_handler(Handle_search_header, "database_num_sequences",
752 "dict", "db_num_sequences")
753 add_text_dict_handler(Handle_search_header, "database_num_letters",
754 "dict", "db_num_letters")
755 add_text_dict_handler(Handle_search_header, "query_size",
756 "dict", "query_size")
757
758
759
760
762 - def start_(self, tag, attrs):
765
766 - def end_(self, tag):
767 self.callback(self.parameters, self.statistics)
768
769 add_value_handler(Handle_search_info, "search_parameter", "parameters")
770 add_value_handler(Handle_search_info, "search_statistic", "statistics")
771