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