1
2
3
4
5 """Parsing AlignACE and CompareACE files: AlignAceParser,CompareAceParser
6 """
7
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
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 """
23 self.motifs=[]
24 self.current_motif=None
25 self.param_dict = None
26
29
31 par_name = line.split("=")[0].strip()
32 par_value = line.split("=")[1].strip()
33 self.param_dict[par_name]=par_value
34
37
39 seq_name = line.split("\t")[1]
40 self.seq_dict.append(seq_name)
41
46
50
52 self.current_motif.score = float(line.split()[-1])
53
56
59
62
65
67 """Parses AlignAce data into a sequence of Motifs.
68 """
73
75 """parse(self, handle)"""
76 self._scanner.feed(handle, self._consumer)
77 return self._consumer
78
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
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
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 """
165
167 """Parses CompareAce output to usable form
168
169 ### so far only in a very limited way
170 """
175
176 - def parse(self, handle):
177 """parse(self, handle)"""
178 self._scanner.feed(handle, self._consumer)
179 return self._consumer.data
180