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

Source Code for Module Bio.Motif.Parsers.AlignAce

  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  """Parsing AlignACE and CompareACE files: AlignAceParser,CompareAceParser 
  6  """ 
  7  #changed string.atof to float, for compatibility with python 2.6 and 3k, BW 
  8   
  9  from Bio import File 
 10  from Bio.ParserSupport import * 
 11  from Bio.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
78
79 -class AlignAceScanner:
80 """Scannner for AlignACE output 81 82 Methods: 83 feed Feed data into the scanner. 84 85 The scanner generates (and calls the consumer) the following types of events: 86 87 noevent - blank line 88 89 version - AlignACE version number 90 command_line - AlignACE command line string 91 parameters - the begining of the parameters 92 parameter - the line containing a parameter 93 sequences - the begining of the sequences list 94 sequence - line containing the name of the input sequence (and a respective number) 95 motif - the begining of the motif (contains the number) 96 motif_hit - one hit for a motif 97 motif_mask - mask of the motif (space - gap, asterisk - significant position) 98 motif_score - MAP score of the motif - approx. N * log R, where R == (num. of actual occur.) / (num. of occur. expected by random.) 99 100 """
101 - def feed(self, handle, consumer):
102 """S.feed(handle, consumer) 103 104 Feed in a AlignACE report for scanning. handle is a file-like 105 object that contains the AlignACE report. consumer is a Consumer 106 object that will receive events as the report is scanned. 107 """ 108 consumer.version(handle.readline()) 109 consumer.command_line(handle.readline()) 110 for line in handle: 111 if line.strip() == "": 112 consumer.noevent(line) 113 elif line[:4]=="Para": 114 consumer.parameters(line) 115 elif line[0]=="#": 116 consumer.sequence(line) 117 elif "=" in line: 118 consumer.parameter(line) 119 elif line[:5]=="Input": 120 consumer.sequences(line) 121 elif line[:5]=="Motif": 122 consumer.motif(line) 123 elif line[:3]=="MAP": 124 consumer.motif_score(line) 125 elif len(line.split("\t"))==4: 126 consumer.motif_hit(line) 127 elif "*" in line: 128 consumer.motif_mask(line) 129 else: 130 raise ValueError(line)
131
132 -class CompareAceScanner:
133 """Scannner for CompareACE output 134 135 Methods: 136 feed Feed data into the scanner. 137 138 The scanner generates (and calls the consumer) the following types of events: 139 140 motif_score - CompareACE score of motifs 141 142 ###### TO DO #############3 143 extend the scanner to include other, more complex outputs. 144 """
145 - def feed(self, handle, consumer):
146 """S.feed(handle, consumer) 147 148 Feed in a CompareACE report for scanning. handle is a file-like 149 object that contains the CompareACE report. consumer is a Consumer 150 object that will receive events as the report is scanned. 151 """ 152 consumer.motif_score(handle.readline())
153 154
155 -class CompareAceConsumer:
156 """ 157 The general purpose consumer for the CompareAceScanner. 158 159 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. 160 """
161 - def __init__(self):
162 pass
163 - def motif_score(self,line):
164 self.data = float(line.split()[-1])
165
166 -class CompareAceParser(AbstractParser):
167 """Parses CompareAce output to usable form 168 169 ### so far only in a very limited way 170 """
171 - def __init__(self):
172 """__init__(self)""" 173 self._scanner = CompareAceScanner() 174 self._consumer = CompareAceConsumer()
175
176 - def parse(self, handle):
177 """parse(self, handle)""" 178 self._scanner.feed(handle, self._consumer) 179 return self._consumer.data
180