Bio/Matrix/PSM/IO/meme.pm

filter: Statementstmt 88.0% 133 / 151 Branchbran 69.2% 36 / 52 Conditioncond 88.8% 8 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 92.3% 12 / 13 Podpod 100% 3 / 3 totaltotal 83.7% 196 / 234 CC 30 SCAR 21.1
CC
30
Cov
83%
next_psm
16.9
new
7.0
_parseInstance
5.2
CRAP 8.3
« Bio/Ontology/RelationshipType.pm ↑ Summary Bio/Tools/CodonTable.pm »
1 #---------------------------------------------------------
2  
3 =head1 NAME
4  
5 Bio::Matrix::PSM::IO::meme - PSM meme parser implementation
6  
7 =head1 SYNOPSIS
8  
9 See Bio::Matrix::PSM::IO for detailed documentation on how to use PSM parsers
10  
11 =head1 DESCRIPTION
12  
13 Parser for meme.
14  
15 =head1 FEEDBACK
16  
17 =head2 Mailing Lists
18  
19 User feedback is an integral part of the evolution of this
20 and other Bioperl modules. Send your comments and suggestions preferably
21 to one of the Bioperl mailing lists.
22 Your participation is much appreciated.
23  
24 bioperl-l@bioperl.org - General discussion
25 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
26  
27 =head2 Support
28  
29 Please direct usage questions or support issues to the mailing list:
30  
31 I<bioperl-l@bioperl.org>
32  
33 rather than to the module maintainer directly. Many experienced and
34 reponsive experts will be able look at the problem and quickly
35 address it. Please include a thorough description of the problem
36 with code and data examples if at all possible.
37  
38 =head2 Reporting Bugs
39  
40 Report bugs to the Bioperl bug tracking system to help us keep track
41 the bugs and their resolution. Bug reports can be submitted via the
42 web:
43  
44 https://github.com/bioperl/bioperl-live/issues
45  
46 =head1 AUTHOR - Stefan Kirov
47  
48 Email skirov@utk.edu
49  
50 =head1 APPENDIX
51  
52 =cut
53  
54  
55 # Let the code begin...
56 package Bio::Matrix::PSM::IO::meme;
57 2 $Bio::Matrix::PSM::IO::meme::VERSION = '1.7.8';
58 2 ▶use Bio::Matrix::PSM::InstanceSite;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 ▶use Bio::Matrix::PSM::SiteMatrix;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 2 ▶use Bio::Matrix::PSM::Psm;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 2 ▶use vars qw(@HEADER);
Subroutine BEGIN: called CC=1 SCAR=0.0
62 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 2 ▶use base qw(Bio::Matrix::PSM::PsmHeader Bio::Matrix::PSM::IO);
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 2 @Bio::Matrix::PSM::IO::meme::HEADER = qw(e_val sites IC width);
67  
68 =head2 new
69  
70 Title : new
71 Usage : my $psmIO = Bio::Matrix::PSM::IO->new(-format=>'meme',
72 -file=>$file);
73 Function: Associates a file with the appropriate parser
74 Throws : Throws if the file passed is in HTML format or
75 if the MEME header cannot be found.
76 Example :
77 Args : hash
78 Returns : "Bio::Matrix::PSM::$format"->new(@args);
79  
80 =cut
81  
82 1 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
83 1 my($class, @args)=@_;
84 1 my $self = $class->SUPER::new(@args);
85 1 my ($file)=$self->_rearrange(['FILE'], @args);
86 1 my ($query,$tr1)=split(/\./,$file,2);
87 1 $self->{file} = $file;
88 1 $self->{query}= $query;
89 1 $self->{end} = 0;
90 1 $self->{_strand}=0; #This we'll need to see if revcom option is used
91 1 ▶ $self->_initialize_io(@args) || warn "Did you intend to use STDIN?"; #Read only for now
Branch: 1 branch
BranchTrueFalseTotal
$self->_initialize_io(@args) or warn "Did you intend to use STDIN?" 1 0 1
92 #Skip header
93 1 my $line;
94 1 ▶ while (my $line=$self->_readline) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->_readline) 32 0 32
95 32 ▶ $self->throw('Cannot parse HTML, please use text output\n') if ($line=~/<HEAD>/); #Should start parsing HTML output, not a bug deal
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /<HEAD>/ 0 32 32
96 32 chomp($line);
97 32 ▶ if ($line=~"^ALPHABET") {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^ALPHABET/) 1 31 32
98 1 $self=_parse_coordinates($self);
99 1 last;
100 }
101 31 ▶ push @{$self->{unstructured}},$line unless (($line=~/\*{10,}/) || ($line eq ''));
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\*{10,}/ or $line eq "" 16 15 31
Condition100%3/3
$line =~ /\*{10,}/ or $line eq ""
l!l&&r!l&&!r
8716
MC/DC100%2/2
$line =~ /\*{10,}/ or $line eq ""
$line =~ /\*{10,}/ $line eq ""
Truth table100%3/3
A or B
A= $line =~ /\*{10,}/B= $line eq ""
ABresult
1X1
011
000
102 }
103 1 $self->_initialize;
104 1 return $self;
105 }
106  
107 =head2 _parse_coordinates
108  
109 Title : _parse_coordinates
110 Usage :
111 Function:
112 Throws :
113 Example : Internal stuff
114 Returns :
115 Args :
116  
117 =cut
118  
119 1 ▶sub _parse_coordinates {
Subroutine _parse_coordinates: called CC=3 SCAR=11.0
120 1 my $self=shift;
121 1 $self->_readline;
122 1 $self->_readline;
123 1 my $line=$self->_readline;
124 1 ▶ while ($line !~ /^\*{10,}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\*{10,}/) 2 1 3
125 2 chomp $line;
126 2 $line =~ s/\s+/,/g;
127 2 my ($id1,$w1,$l1,$id2,$w2,$l2)=split(/,/,$line);
128 2 push @{$self->{hid}},$id1;
129 2 $self->{weight}->{$id1}=$w1;
130 2 $self->{length}->{$id1}=$l1;
131 2 ▶ if ($id2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id2) 2 0 2
132 2 push @{$self->{hid}},$id2;
133 2 $self->{weight}->{$id2}=$w2;
134 2 $self->{length}->{$id2}=$l2;
135 }
136 2 $line=$self->_readline;
137 }
138 1 return $self;
139 }
140  
141 =head2 header
142  
143 Title : header
144 Usage : my %header=$psmIO->header;
145 Function: Returns the header for the MEME file
146 Throws :
147 Example : Fetching all the sequences included in the MEME analysis,
148 being parsed
149 my %header=$psmIO->header;
150 foreach my $seqid (@{$header{instances}}) {
151 my $seq=$db->get_Seq_by_acc($id);
152 #Do something with the sequence
153 }
154 where $db might be Bio::DB:GenBank object, see
155 Returns : Hash with three keys: instances, weights and lengths, which
156 should be self-explenatory. Each value is an array
157 reference. Each array element corresponds to the same
158 element in the other two arrays. So $header{instances}->[$i]
159 will refer to the same sequence in the motif file as
160 $header{weights}->[$i] and $header{lengths}->[$i]
161 Args : none
162 Notes : OBSOLETE!
163  
164 =cut
165  
166 0 ▶sub header {
Subroutine header: not called CC=1 SCAR=6.9
Pod documented
167 0 my $self=shift;
168 0 my @instances=@{$self->{_inst_name}};
169 0 my @weights=@{$self->{_inst_weight}};
170 0 my @lengths=@{$self->{_inst_coord}};
171 0 return (instances=>\@instances,weights=>\@weights,lengths=>\@lengths);
172 }
173  
174 =head2 next_psm
175  
176 Title : next_psm
177 Usage : my $psm=$psmIO->next_psm();
178 Function: Reads the next PSM from the input file, associated with this object
179 Throws : Throws if the format is inconsistent with the rules for MEME 3.0.4:
180 no SUMMARY Section present or some keywords are missing/altered.
181 Example :
182 Returns : Bio::Matrix::PSM::Psm object
183 Args : none
184  
185 =cut
186  
187 1 ▶sub next_psm {
Subroutine next_psm: called CC=14 SCAR=28.3
Pod documented
188 #Parses the next prediction and returns a psm objects
189 1 my $self=shift;
190 1 ▶ return if ($self->{end});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"end"} 0 1 1
191 1 my ($endm,$line,$instances,$tr,$width,$motif_id,$sites,$e_val,$id,$ic,$lA,$lC,$lG,$lT);
192 1 ▶ while (defined( $line = $self->_readline) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = $self->_readline)) 91 0 91
193 #Check if revcom is enabled, not very original check....
194 91 ▶ $self->{_strand}=1 if (($line=~/^Sequence name/) && ($line=~/Strand/));
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^Sequence name/ and $line =~ /Strand/ 0 91 91
Condition66%2/3
$line =~ /^Sequence name/ and $line =~ /Strand/
!ll&&!rl&&r
9010
MC/DC0%0/2
$line =~ /^Sequence name/ and $line =~ /Strand/
$line =~ /^Sequence name/ $line =~ /Strand/
Truth table66%2/3
A and B
A= $line =~ /^Sequence name/B= $line =~ /Strand/
ABresult
0X0
100
111
195 91 ▶ if ($line=~ m/\sSite\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\sSite\s/) 1 90 91
196 1 $instances= $self->_parseInstance;
197 }
198 #Here starts the next motif
199 91 ▶ if ( ($line=~/width/) && ($line=~/sites/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /width/ and $line =~ /sites/) 1 90 91
Condition100%3/3
$line =~ /width/ and $line =~ /sites/
!ll&&!rl&&r
8821
MC/DC100%2/2
$line =~ /width/ and $line =~ /sites/
$line =~ /width/ $line =~ /sites/
Truth table100%3/3
A and B
A= $line =~ /width/B= $line =~ /sites/
ABresult
0X0
100
111
200 1 chomp($line);
201 1 $line=~s/[\t\s=]+/,/g;
202 1 $line=~s/\t/,/g;
203 #Parsing the general information for this prediction
204 1 ($tr,$motif_id,$tr,$width,$tr,$sites,
205 $tr,$tr,$tr,$e_val)=split(/,/,$line);
206 1 $self->{id}=$self->{query} . $motif_id;
207 }
208 91 ▶ if ($line =~ /content/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /content/i) 1 90 91
209 1 $line=$self->_readline;
210 1 chomp($line);
211 1 $line=~s/[\)\(]//g;
212 1 ($ic)=split(/\s/,$line);
213 }
214 #Last info-prob matrix data
215 91 ▶ if ($line=~/position-specific\s+scoring matrix/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /position-specific\s+scoring matrix/) 1 90 91
216 1 ($lA,$lC,$lG,$lT)=_parse_logs($self);
217 }
218 91 ▶ if ($line=~/^letter-probability\smatrix/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^letter-probability\smatrix/) 1 90 91
219 1 my %matrix_dat=$self->_parseMatrix($motif_id);
220 1 my $psm= Bio::Matrix::PSM::Psm->new(%matrix_dat,
221 -instances=>$instances,
222 -e_val=>$e_val,
223 -IC=>$ic,
224 -width=>$width,
225 -sites=>$sites,
226 -lA=>$lA,
227 -lC=>$lC,
228 -lG=>$lG,
229 -lT=>$lT,
230 );
231 1 return $psm;
232 }
233 90 ▶ if ($line=~"SUMMARY OF MOTIFS") {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /SUMMARY OF MOTIFS/) 0 90 90
234 0 $self->{end}=1;
235 0 return;
236 }
237 90 ▶ $endm=1 if ($line=~/^Time\s/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^Time\s/ 0 90 90
238 }
239 0 ▶ if ($endm) { #End of file found, end of current motif too, but not all predictions were made as requested (No summary)
Branch: 1 branch
BranchTrueFalseTotal
if ($endm) 0 0 0
240 0 $self->{end}=1;
241 0 warn "This MEME analysis was terminated prematurely, you may have less motifs than you requested\n";
242 0 return;
243 }
244 0 $self->throw("Wrong format\n"); # Multiple keywords not found, probably wrong format
245 }
246  
247 =head2 _parseMatrix
248  
249 Title : _parseMatrix
250 Usage :
251 Function: Parses the next site matrix information in the meme file
252 Throws :
253 Example : Internal stuff
254 Returns : hash as for constructing a SiteMatrix object (see SiteMatrixI)
255 Args : string
256  
257 =cut
258  
259 1 ▶sub _parseMatrix {
Subroutine _parseMatrix: called CC=3 SCAR=11.0
260 1 my ($self,$id)=@_;
261 1 my (@pA,@pC,@pG,@pT);
262 1 my $i=0;
263 1 my $line = $self->_readline;
264 #Most important part- the probability matrix
265 1 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /\-{10,}/ or do { do { chomp $line; last if $line eq ""; $line =~ s/^\s+//; $line =~ s/\s+/,/g; ($pA[$i], $pC[$i], $pG[$i], $pT[$i]) = split(/,/, $line, 5); ++$i; $line = $self->_readline } } 1 24 25
266 25 chomp $line;
267 25 ▶ last if ($line eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 0 25 25
268 25 $line=~s/^\s+//;
269 25 $line=~s/\s+/,/g;
270 25 ($pA[$i],$pC[$i],$pG[$i],$pT[$i])=split(/,/,$line);
271 25 $i++;
272 25 $line=$self->_readline;
273 } until $line =~ /\-{10,}/;
274 1 return (-pA=>\@pA,-pC=>\@pC,-pG=>\@pG,-pT=>\@pT,-id=>$id);
275 }
276  
277 =head2 _parse_logs
278  
279 Title : _parse_logs
280 Usage :
281 Function: Parses the next site matrix log values in the meme file
282 Throws :
283 Example : Internal stuff
284 Returns : array of array refs
285 Args : string
286  
287 =cut
288  
289 1 ▶sub _parse_logs {
Subroutine _parse_logs: called CC=3 SCAR=11.0
290 1 my $self=shift;
291 1 my (@lA,@lC,@lG,@lT);
292 1 my $i=0;
293 1 $self->_readline; $self->_readline;
294 1 my $line = $self->_readline;
295 #Most important part- the probability matrix
296 1 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /\-{10,}/ or do { do { chomp $line; last if $line eq ""; $line =~ s/^\s+//; $line =~ s/\s+/,/g; ($lA[$i], $lC[$i], $lG[$i], $lT[$i]) = split(/,/, $line, 5); ++$i; $line = $self->_readline } } 1 24 25
297 25 chomp $line;
298 25 ▶ last if ($line eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 0 25 25
299 25 $line=~s/^\s+//;
300 25 $line=~s/\s+/,/g;
301 25 ($lA[$i],$lC[$i],$lG[$i],$lT[$i])=split(/,/,$line);
302 25 $i++;
303 25 $line=$self->_readline;
304 } until $line =~ /\-{10,}/;
305
306 1 return (\@lA,\@lC,\@lG,\@lT);
307 }
308  
309 =head2 _parseInstance
310  
311 Title : _parseInstance
312 Usage :
313 Function: Parses the next sites instances from the meme file
314 Throws :
315 Example : Internal stuff
316 Returns : Bio::Matrix::PSM::InstanceSite object
317 Args : none
318  
319 =cut
320  
321 1 ▶sub _parseInstance {
Subroutine _parseInstance: called CC=5 SCAR=16.5
322 1 my $self = shift;
323 1 my $i=0;
324 1 $self->_readline;
325 1 my ($line,@instance);
326 1 ▶ while (defined($line=$self->_readline) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = $self->_readline)) 5 0 5
327 5 ▶ last if ($line =~ /\-{5}/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\-{5}/ 1 4 5
328 4 chomp($line);
329 4 my @comp=split(/\s+/,$line);
330 4 my ($id,$start,$score,$strand,$s1,$s2,$s3);
331 4 ▶ if ( $self->{_strand}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_strand'}) { } 0 4 4
332 0 ($id,$strand,$start,$score,$s1,$s2,$s3)=@comp;
333 } else {
334 4 ($id,$start,$score,$s1,$s2,$s3)=@comp;
335 4 $strand=1;
336 }
337 4 my $seq= $s1.$s2.$s3;
338 4 ▶ if ($seq =~ /[^ACGTacgtNnXx\-\.]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq =~ /[^ACGTacgtNnXx\-\.]/) 0 4 4
339 0 my $col=$#comp;
340 0 $self->throw("I have not been able to parse the correct instance sequence: $seq, $col columns\n");
341 }
342 4 my $sid = $self->{id} . '@' . $id;
343 $instance[$i] = Bio::Matrix::PSM::InstanceSite->new
344 (-mid => $self->{id},
345 4 -start => $start,
346 -score => $score,
347 -seq => $seq,
348 -strand => $strand,
349 -accession_number => $id,
350 -primary_id => $sid,
351 -desc => 'Bioperl MEME parser object' );
352 4 $i++;
353 }
354 1 $self->{instances} = \@instance;
355 1 return \@instance;
356 }
357  
358
359
360  
361 2 1;
« Bio/Ontology/RelationshipType.pm ↑ Summary Bio/Tools/CodonTable.pm »