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   
  6  """Parsing AlignACE and CompareACE files: AlignAceParser,CompareAceParser 
  7  """ 
  8   
  9  from Bio.Motif import Motif 
 10  from Bio.Alphabet import IUPAC 
 11  from Bio.Seq import Seq 
 12   
 13   
14 -class Record:
15 - def __init__(self):
16 self.motifs=[] 17 self.current_motif=None 18 self.param_dict = None
19 20
21 -def read(handle):
22 """read(handle)""" 23 record = Record() 24 record.ver = handle.next() 25 record.cmd_line = handle.next() 26 for line in handle: 27 if line.strip() == "": 28 pass 29 elif line[:4]=="Para": 30 record.param_dict={} 31 elif line[0]=="#": 32 seq_name = line.split("\t")[1] 33 record.seq_dict.append(seq_name) 34 elif "=" in line: 35 par_name = line.split("=")[0].strip() 36 par_value = line.split("=")[1].strip() 37 record.param_dict[par_name]=par_value 38 elif line[:5]=="Input": 39 record.seq_dict=[] 40 elif line[:5]=="Motif": 41 record.current_motif = Motif() 42 record.motifs.append(record.current_motif) 43 record.current_motif.alphabet=IUPAC.unambiguous_dna 44 elif line[:3]=="MAP": 45 record.current_motif.score = float(line.split()[-1]) 46 elif len(line.split("\t"))==4: 47 seq = Seq(line.split("\t")[0],IUPAC.unambiguous_dna) 48 record.current_motif.add_instance(seq) 49 elif "*" in line: 50 record.current_motif.set_mask(line.strip("\n\c")) 51 else: 52 raise ValueError(line) 53 return record
54 55 56 # Everything below is obsolete. 57 58 from Bio.ParserSupport import * 59 60
61 -class AlignAceConsumer:
62 """ 63 The general purpose consumer for the AlignAceScanner. 64 65 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. 66 """
67 - def __init__(self):
68 self.motifs=[] 69 self.current_motif=None 70 self.param_dict = None
71
72 - def parameters(self,line):
73 self.param_dict={}
74
75 - def parameter(self,line):
76 par_name = line.split("=")[0].strip() 77 par_value = line.split("=")[1].strip() 78 self.param_dict[par_name]=par_value
79
80 - def sequences(self,line):
81 self.seq_dict=[]
82
83 - def sequence(self,line):
84 seq_name = line.split("\t")[1] 85 self.seq_dict.append(seq_name)
86
87 - def motif(self,line):
88 self.current_motif = Motif() 89 self.motifs.append(self.current_motif) 90 self.current_motif.alphabet=IUPAC.unambiguous_dna
91
92 - def motif_hit(self,line):
93 seq = Seq(line.split("\t")[0],IUPAC.unambiguous_dna) 94 self.current_motif.add_instance(seq)
95
96 - def motif_score(self,line):
97 self.current_motif.score = float(line.split()[-1])
98
99 - def motif_mask(self,line):
100 self.current_motif.set_mask(line.strip("\n\c"))
101
102 - def noevent(self,line):
103 pass
104
105 - def version(self,line):
106 self.ver = line
107
108 - def command_line(self,line):
109 self.cmd_line = line
110
111 -class AlignAceParser(AbstractParser):
112 """Parses AlignAce data into a sequence of Motifs. 113 """
114 - def __init__(self):
115 """__init__(self)""" 116 self._scanner = AlignAceScanner() 117 self._consumer = AlignAceConsumer()
118
119 - def parse(self, handle):
120 """parse(self, handle)""" 121 self._scanner.feed(handle, self._consumer) 122 return self._consumer
123
124 -class AlignAceScanner:
125 """Scannner for AlignACE output 126 127 Methods: 128 feed Feed data into the scanner. 129 130 The scanner generates (and calls the consumer) the following types of events: 131 132 noevent - blank line 133 134 version - AlignACE version number 135 command_line - AlignACE command line string 136 parameters - the begining of the parameters 137 parameter - the line containing a parameter 138 sequences - the begining of the sequences list 139 sequence - line containing the name of the input sequence (and a respective number) 140 motif - the begining of the motif (contains the number) 141 motif_hit - one hit for a motif 142 motif_mask - mask of the motif (space - gap, asterisk - significant position) 143 motif_score - MAP score of the motif - approx. N * log R, where R == (num. of actual occur.) / (num. of occur. expected by random.) 144 145 """
146 - def feed(self, handle, consumer):
147 """S.feed(handle, consumer) 148 149 Feed in a AlignACE report for scanning. handle is a file-like 150 object that contains the AlignACE report. consumer is a Consumer 151 object that will receive events as the report is scanned. 152 """ 153 consumer.version(handle.readline()) 154 consumer.command_line(handle.readline()) 155 for line in handle: 156 if line.strip() == "": 157 consumer.noevent(line) 158 elif line[:4]=="Para": 159 consumer.parameters(line) 160 elif line[0]=="#": 161 consumer.sequence(line) 162 elif "=" in line: 163 consumer.parameter(line) 164 elif line[:5]=="Input": 165 consumer.sequences(line) 166 elif line[:5]=="Motif": 167 consumer.motif(line) 168 elif line[:3]=="MAP": 169 consumer.motif_score(line) 170 elif len(line.split("\t"))==4: 171 consumer.motif_hit(line) 172 elif "*" in line: 173 consumer.motif_mask(line) 174 else: 175 raise ValueError(line)
176
177 -class CompareAceScanner:
178 """Scannner for CompareACE output 179 180 Methods: 181 feed Feed data into the scanner. 182 183 The scanner generates (and calls the consumer) the following types of events: 184 185 motif_score - CompareACE score of motifs 186 187 ###### TO DO #############3 188 extend the scanner to include other, more complex outputs. 189 """
190 - def feed(self, handle, consumer):
191 """S.feed(handle, consumer) 192 193 Feed in a CompareACE report for scanning. handle is a file-like 194 object that contains the CompareACE report. consumer is a Consumer 195 object that will receive events as the report is scanned. 196 """ 197 consumer.motif_score(handle.readline())
198 199
200 -class CompareAceConsumer:
201 """ 202 The general purpose consumer for the CompareAceScanner. 203 204 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. 205 """
206 - def __init__(self):
207 pass
208 - def motif_score(self,line):
209 self.data = float(line.split()[-1])
210
211 -class CompareAceParser(AbstractParser):
212 """Parses CompareAce output to usable form 213 214 ### so far only in a very limited way 215 """
216 - def __init__(self):
217 """__init__(self)""" 218 import warnings 219 warnings.warn("CompareAceParser and ComparAceConsumer are" \ 220 +" deprecated, and will be removed in a future release of"\ 221 +" Biopython. If you want to continue to use this code,"\ 222 +" please get in contact with the Biopython developers via"\ 223 +" the mailing lists to avoid its permanent removal from"\ 224 +" Biopython. See also the Python built in set datatype.", \ 225 DeprecationWarning) 226 self._scanner = CompareAceScanner() 227 self._consumer = CompareAceConsumer()
228
229 - def parse(self, handle):
230 """parse(self, handle)""" 231 self._scanner.feed(handle, self._consumer) 232 return self._consumer.data
233