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