Package Bio :: Package AlignAce :: Module Parser
[hide private]
[frames] | no frames]

Source Code for Module Bio.AlignAce.Parser

  1  # Copyright 2003 by Bartek Wilczynski.  All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5  """ 
  6  Classes for pparsing AlignAce and CompareACE files 
  7  """ 
  8   
  9  from Bio.ParserSupport import * 
 10  from Scanner import AlignAceScanner,CompareAceScanner 
 11  from Motif import Motif 
 12  from Bio.Alphabet import IUPAC 
 13  from Bio.Seq import Seq 
 14   
15 -class AlignAceConsumer:
16 """ 17 The general purpose consumer for the AlignAceScanner. 18 19 Should be passed as the consumer to the feed method of the AlignAceScanner. After 'consuming' the file, it has the list of motifs in the motifs property. 20 """
21 - def __init__(self):
22 self.motifs=[] 23 self.current_motif=None 24 self.param_dict = None
25
26 - def parameters(self,line):
27 self.param_dict={}
28
29 - def parameter(self,line):
30 par_name = line.split("=")[0].strip() 31 par_value = line.split("=")[1].strip() 32 self.param_dict[par_name]=par_value
33
34 - def sequences(self,line):
35 self.seq_dict=[]
36
37 - def sequence(self,line):
38 seq_name = line.split("\t")[1] 39 self.seq_dict.append(seq_name)
40
41 - def motif(self,line):
42 self.current_motif = Motif() 43 self.motifs.append(self.current_motif) 44 self.current_motif.alphabet=IUPAC.unambiguous_dna
45
46 - def motif_hit(self,line):
47 seq = Seq(line.split("\t")[0],IUPAC.unambiguous_dna) 48 self.current_motif.add_instance(seq)
49
50 - def motif_score(self,line):
51 self.current_motif.score = string.atof(line.split()[-1])
52
53 - def motif_mask(self,line):
54 self.current_motif.set_mask(line.strip("\n\c"))
55
56 - def noevent(self,line):
57 pass
58
59 - def version(self,line):
60 self.ver = line
61
62 - def command_line(self,line):
63 self.cmd_line = line
64
65 -class AlignAceParser(AbstractParser):
66 """Parses AlignAce data into a sequence of Motifs. 67 """
68 - def __init__(self):
69 """__init__(self)""" 70 self._scanner = AlignAceScanner() 71 self._consumer = AlignAceConsumer()
72
73 - def parse(self, handle):
74 """parse(self, handle)""" 75 self._scanner.feed(handle, self._consumer) 76 return self._consumer.motifs
77 78
79 -class CompareAceConsumer:
80 """ 81 The general purpose consumer for the CompareAceScanner. 82 83 Should be passed as the consumer to the feed method of the CompareAceScanner. After 'consuming' the file, it has the list of motifs in the motifs property. 84 """
85 - def __init__(self):
86 pass
87 - def motif_score(self,line):
88 self.data = string.atof(line.split()[-1])
89
90 -class CompareAceParser(AbstractParser):
91 """Parses CompareAce output to usable form 92 93 ### so far only in a very limited way 94 """
95 - def __init__(self):
96 """__init__(self)""" 97 self._scanner = CompareAceScanner() 98 self._consumer = CompareAceConsumer()
99
100 - def parse(self, handle):
101 """parse(self, handle)""" 102 self._scanner.feed(handle, self._consumer) 103 return self._consumer.data
104