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