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 parsing 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   
16 -class AlignAceConsumer:
17 """ 18 The general purpose consumer for the AlignAceScanner. 19 20 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. 21 """
22 - def __init__(self):
23 self.motifs=[] 24 self.current_motif=None 25 self.param_dict = None
26
27 - def parameters(self,line):
28 self.param_dict={}
29
30 - def parameter(self,line):
31 par_name = line.split("=")[0].strip() 32 par_value = line.split("=")[1].strip() 33 self.param_dict[par_name]=par_value
34
35 - def sequences(self,line):
36 self.seq_dict=[]
37
38 - def sequence(self,line):
39 seq_name = line.split("\t")[1] 40 self.seq_dict.append(seq_name)
41
42 - def motif(self,line):
43 self.current_motif = Motif() 44 self.motifs.append(self.current_motif) 45 self.current_motif.alphabet=IUPAC.unambiguous_dna
46
47 - def motif_hit(self,line):
48 seq = Seq(line.split("\t")[0],IUPAC.unambiguous_dna) 49 self.current_motif.add_instance(seq)
50
51 - def motif_score(self,line):
52 self.current_motif.score = float(line.split()[-1])
53
54 - def motif_mask(self,line):
55 self.current_motif.set_mask(line.strip("\n\c"))
56
57 - def noevent(self,line):
58 pass
59
60 - def version(self,line):
61 self.ver = line
62
63 - def command_line(self,line):
64 self.cmd_line = line
65
66 -class AlignAceParser(AbstractParser):
67 """Parses AlignAce data into a sequence of Motifs. 68 """
69 - def __init__(self):
70 """__init__(self)""" 71 self._scanner = AlignAceScanner() 72 self._consumer = AlignAceConsumer()
73
74 - def parse(self, handle):
75 """parse(self, handle)""" 76 self._scanner.feed(handle, self._consumer) 77 return self._consumer.motifs
78 79
80 -class CompareAceConsumer:
81 """ 82 The general purpose consumer for the CompareAceScanner. 83 84 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. 85 """
86 - def __init__(self):
87 pass
88 - def motif_score(self,line):
89 self.data = float(line.split()[-1])
90
91 -class CompareAceParser(AbstractParser):
92 """Parses CompareAce output to usable form 93 94 ### so far only in a very limited way 95 """
96 - def __init__(self):
97 """__init__(self)""" 98 self._scanner = CompareAceScanner() 99 self._consumer = CompareAceConsumer()
100
101 - def parse(self, handle):
102 """parse(self, handle)""" 103 self._scanner.feed(handle, self._consumer) 104 return self._consumer.data
105