Bio/Matrix/PSM/IO/masta.pm

filter: Statementstmt 97.9% 141 / 144 Branchbran 81.4% 44 / 54 Conditioncond 55.0% 11 / 20 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 86.6% 214 / 247 CC 45 SCAR 25.4
CC
45
Cov
90%
next_psm
23.2
write_psm
12.0
_make_matrix
9.0
CRAP 12.7
« Bio/Search/Result/BlastPullResult.pm ↑ Summary Bio/Seq/Meta/Array.pm »
1 #---------------------------------------------------------
2  
3 =head1 NAME
4  
5 Bio::Matrix::PSM::IO::masta - motif fasta format parser
6  
7 =head1 SYNOPSIS
8  
9 MASTA is a position frequency matrix format similar to
10 fasta. It contains one ID row just like fasta and then the actual
11 data, which is tab delimited:
12  
13 0.1 0.62 .017 0.11
14 0.22 0.13 0.54 0.11
15  
16 Or A,C,G and T could be horizontally positioned (positioning is
17 automatically detected). Please note masta will parse only DNA at the
18 moment.
19  
20 It will also convert a set of aligned sequences:
21 ACATGCAT
22 ACAGGGAT
23 ACAGGCAT
24 ACCGGCAT
25  
26 to a PFM (SiteMatrix object). When writing if you supply SEQ it will
27 write 10 random instances, which represent correctly the frequency and
28 can be used as an input for weblogo creation purposes.
29  
30 See Bio::Matrix::PSM::IO for detailed documentation on how to use masta parser
31  
32 =head1 DESCRIPTION
33  
34 Parser for meme.
35  
36 =head1 FEEDBACK
37  
38 =head2 Mailing Lists
39  
40 User feedback is an integral part of the evolution of this
41 and other Bioperl modules. Send your comments and suggestions preferably
42 to one of the Bioperl mailing lists.
43 Your participation is much appreciated.
44  
45 bioperl-l@bioperl.org - General discussion
46 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
47  
48 =head2 Support
49  
50 Please direct usage questions or support issues to the mailing list:
51  
52 I<bioperl-l@bioperl.org>
53  
54 rather than to the module maintainer directly. Many experienced and
55 reponsive experts will be able look at the problem and quickly
56 address it. Please include a thorough description of the problem
57 with code and data examples if at all possible.
58  
59 =head2 Reporting Bugs
60  
61 Report bugs to the Bioperl bug tracking system to help us keep track
62 the bugs and their resolution. Bug reports can be submitted via the
63 web:
64  
65 https://github.com/bioperl/bioperl-live/issues
66  
67 =head1 AUTHOR - Stefan Kirov
68  
69 Email skirov@utk.edu
70  
71 =head1 APPENDIX
72  
73 =cut
74  
75  
76 # Let the code begin...
77 package Bio::Matrix::PSM::IO::masta;
78 2 $Bio::Matrix::PSM::IO::masta::VERSION = '1.7.8';
79 2 ▶use Bio::Matrix::PSM::SiteMatrix;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 2 ▶use vars qw(@HEADER);
Subroutine BEGIN: called CC=1 SCAR=0.0
81 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 2 ▶use base qw(Bio::Matrix::PSM::IO Bio::Root::Root);
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85  
86  
87 =head2 new
88  
89 Title : new
90 Usage : my $psmIO = Bio::Matrix::PSM::IO->new(-format=> 'masta',
91 -file => $file,
92 -mtype => 'PWM');
93 Function: Associates a file with the appropriate parser
94 Throws :
95 Example :
96 Args : hash
97 Returns : "Bio::Matrix::PSM::$format"->new(@args);
98  
99 =cut
100  
101 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
102 3 my($class, @args)=@_;
103 3 my $self = $class->SUPER::new(@args);
104 3 my ($file)=$self->_rearrange(['FILE'], @args);
105 3 my ($query,$tr1)=split(/\./,$file,2);
106 3 $self->{file} = $file;
107 3 $self->{_end} = 0;
108 3 ▶ $self->{mtype} = uc($self->_rearrange(['MTYPE'], @args) || "PFM");
Condition50%1/2
$self->_rearrange(['MTYPE'], @args) || 'PFM'
l!l
03
MC/DC0%0/1
$self->_rearrange(['MTYPE'], @args) || 'PFM'
$self->_rearrange(['MTYPE'], @args)
Truth table50%1/2
A
A= $self->_rearrange(['MTYPE'], @args)
Aresult
11
00
109 3 ▶ $self->_initialize_io(@args) || $self->warn("Did you intend to use STDIN?"); #Read only for now
Branch: 1 branch
BranchTrueFalseTotal
$self->_initialize_io(@args) or $self->warn("Did you intend to use STDIN?") 3 0 3
110 3 return $self;
111 }
112  
113 =head2 write_psm
114  
115 Title : write_psm
116 Usage :
117 Function: writes a pfm/pwm/raw sequence in a simple masta format
118 Throws :
119 Example :
120 Args : SiteMatrix object, type (optional string: PWM, SEQ or PFM)
121 Returns :
122  
123 =cut
124  
125 3 ▶sub write_psm {
Subroutine write_psm: called CC=12 SCAR=24.9
Pod documented
126 3 my ($self,$matrix,$type)=@_;
127 3 ▶ $self->{mtype} = uc($type) if ($type);
Branch: 1 branch
BranchTrueFalseTotal
if $type 3 0 3
128 3 my $idline=">". $matrix->id . "\n";
129 3 $self->_print($idline);
130 3 ▶ unless ($self->{mtype} eq 'SEQ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'mtype'} eq 'SEQ') { } 1 2 3
131 2 ▶ while (my %h=$matrix->next_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(%h) = $matrix->next_pos) 50 2 52
132 50 ▶ my $row=$self->{mtype} eq 'PWM' ? join("\t",$h{lA},$h{lC},$h{lG},$h{lT},"\n"):join("\t",$h{pA},$h{pC},$h{pG},$h{pT},"\n");
Branch: 1 branch
BranchTrueFalseTotal
$self->{'mtype'} eq 'PWM' ? : 25 25 50
133 50 $self->_print ($row);
134 }
135 } else {
136 1 my @seq;
137 1 ▶ while (my %h=$matrix->next_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(%h) = $matrix->next_pos) 25 1 26
138 25 my ($a,$c,$g,$t)=_freq_to_count(\%h);
139 25 ▶ $self->throw("Could not convert from frequency to count\n") if (($a+$c+$g+$t) !=10);
Branch: 1 branch
BranchTrueFalseTotal
if $a + $c + $g + $t != 10 0 25 25
140 25 for my $i (0..$a-1) {$seq[$i].='A';}
141 25 my $m=$a+$c;
142 25 for my $i ($a..$m-1) {$seq[$i].='C';}
143 25 my $n=$a+$c+$g;
144 25 for my $i ($m..$n-1) {$seq[$i].='G';}
145 25 for my $i ($n..9) {$seq[$i].='T';}
146 }
147 1 foreach my $s (@seq) {
148 10 $s.="\n";
149 10 $self->_print ($s);
150 }
151 }
152 }
153  
154 =head2 next_matrix
155  
156 Title : next_matrix
157 Usage : my $matrix = $psmio->next_matrix;
158 Function: Alias of next_psm function
159  
160 =cut
161  
162 7 ▶sub next_matrix {
Subroutine next_matrix: called CC=1 SCAR=0.0
Pod documented
163 7 shift->next_psm(@_);
164 }
165  
166 =head2 next_psm
167  
168 Title : next_psm
169 Usage : my $matrix=$psmio->next_psm;
170 Function: returns the next matrix in the stream
171 Throws : If there is you mix different types, for example weights and
172 frequencies occur in the same entry You can mix weights, but these
173 should be designated by different ID lines
174 Example :
175 Args :
176 Returns : Bio::Matrix::PSM::SiteMatrix
177  
178 =cut
179  
180 7 ▶sub next_psm {
Subroutine next_psm: called CC=19 SCAR=31.4
Pod documented
181 7 my $self=shift;
182 7 ▶ return if ($self->{_end});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_end"} 1 6 7
183 6 my $line=$self->_readline;
184 6 ▶ $self->throw("No ID line- wrong format\n") unless ($line=~/^>/);
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^>/ 0 6 6
185 6 my ($id,$desc)=split(/[\t\s]+/,$line,2);
186 6 $id=~s/>//;
187 6 my ($mtype,$format,@mdata,$len);
188 6 $self->{_mtype} = 0;
189 6 ▶ while ($line=$self->_readline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line = $self->_readline) 123 2 125
190 123 ▶ next if $line =~ /^\s+$/;# There should not be empty lines, but just in case...
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s+$/ 1 122 123
191 122 chomp $line;
192 122 ▶ if ($line =~ /^>/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^>/) 4 118 122
193 4 $self->_pushback($line);
194 4 last;
195 }
196  
197 118 ▶ if ($line !~ /[^ACGTacgt]/g) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $line =~ /[^ACGTacgt]/g) { } 18 100 118
198 # This is a set of aligned sequences
199 $self->throw("Mixing between types is not allowed or a parsing error occurred\n")
200 18 ▶ if (($self->{_mtype} != 3) && ($mtype)) ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_mtype"} != 3 and $mtype 0 18 18
Condition66%2/3
$self->{"_mtype"} != 3 and $mtype
!ll&&!rl&&r
1620
MC/DC0%0/2
$self->{"_mtype"} != 3 and $mtype
$self->{"_mtype"} != 3 $mtype
Truth table66%2/3
A and B
A= $self->{"_mtype"} != 3B= $mtype
ABresult
0X0
100
111
201 18 ▶ $self->throw("Bad sequence- different length: $line\n")
Branch: 1 branch
BranchTrueFalseTotal
if $len and $len != length $line 0 18 18
Condition66%2/3
$len and $len != length $line
!ll&&!rl&&r
2160
MC/DC0%0/2
$len and $len != length $line
$len $len != length $line
Truth table66%2/3
A and B
A= $lenB= $len != length $line
ABresult
0X0
100
111
202 if (($len) && ($len!=length($line)));
203 18 ▶ $len=length($line) unless ($len);
Branch: 1 branch
BranchTrueFalseTotal
unless $len 2 16 18
204 18 push @mdata,$line;
205 18 $self->{_mtype}=3;
206 } else {
207 # do not strip 'e's since they are part of number notation for small/big numbers
208 100 $line=~s/[a-df-zA-DF-Z]//g; #Well we may wanna do a hash and auto check for letter order if there is a really boring talk...
209 100 $line=~s/^[\s\t]+//;
210 100 $line=~s/[\s\t]+/\t/g;
211 100 my @data=split(/[\s\t]+/,$line);
212 100 ▶ if ($#data==3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#data == 3) { } 100 0 100
213 100 ▶ $self->throw("Mixing between types is not allowed or a parsing error occurred\n") if (($mtype)&&($self->{_mtype} !=1)) ;
Branch: 1 branch
BranchTrueFalseTotal
if $mtype and $self->{"_mtype"} != 1 0 100 100
Condition66%2/3
$mtype and $self->{"_mtype"} != 1
!ll&&!rl&&r
4960
MC/DC0%0/2
$mtype and $self->{"_mtype"} != 1
$mtype $self->{"_mtype"} != 1
Truth table66%2/3
A and B
A= $mtypeB= $self->{"_mtype"} != 1
ABresult
0X0
100
111
214 100 $self->{_mtype}=1;
215 100 $mtype=1;
216 }
217 else {
218 0 ▶ $self->throw("Mixing between types is not allowedor a parsing error occurred\n") if (($mtype)&&($self->{_mtype} !=2)) ;
Branch: 1 branch
BranchTrueFalseTotal
if $mtype and $self->{"_mtype"} != 2 0 0 0
Condition0%0/3
$mtype and $self->{"_mtype"} != 2
!ll&&!rl&&r
000
MC/DC0%0/2
$mtype and $self->{"_mtype"} != 2
$mtype $self->{"_mtype"} != 2
Truth table0%0/3
A and B
A= $mtypeB= $self->{"_mtype"} != 2
ABresult
0X0
100
111
219 0 $self->{_mtype}=2;
220 0 $mtype=1;
221 }
222 100 push @mdata,\@data;
223 }
224 }
225 6 ▶ $self->{_end} = 1 if (!defined $line || $line !~ /^>/);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and $line =~ /^>/ 2 4 6
Condition66%2/3
defined $line and $line =~ /^>/
!ll&&!rl&&r
204
MC/DC50%1/2
defined $line and $line =~ /^>/
defined $line $line =~ /^>/
Truth table66%2/3
A and B
A= defined $lineB= $line =~ /^>/
ABresult
0X0
100
111
226 6 return _make_matrix(\@mdata,$self->{_mtype},$id,$desc);
227 }
228  
229 6 ▶sub _make_matrix {
Subroutine _make_matrix: called CC=9 SCAR=22.0
230 6 my ($mdata,$type,$id,$desc)=@_;
231 6 ▶ if ($type==1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 1) 4 2 6
232 4 my @rearr=_rearrange_matrix($mdata);
233 4 $mdata=\@rearr;
234 }
235 #Auto recognition for what type is this entry (PFM, PWM or simple count)
236 #A bit dangerous, I hate too much auto stuff, but I want to be able to mix different
237 #types in a single file
238 6 my $mformat='count';
239 6 my ($a,$c,$g,$t);
240 6 ▶ if ($type == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 3) { } 2 4 6
241 2 ($a,$c,$g,$t)= &_count_positions($mdata);
242 } else {
243 4 ($a,$c,$g,$t)=@{$mdata};
244 4 my $k=$a->[0]+$c->[0]+$g->[0]+$t->[0];
245 4 my $l= ($a->[0]+$c->[0]+$g->[0]+$t->[0]) -
246 (abs($a->[0])+abs($c->[0])+abs($g->[0])+abs($t->[0]));
247 4 ▶ $mformat='freq' if (($k==1) && ($l==0));
Branch: 1 branch
BranchTrueFalseTotal
if $k == 1 and $l == 0 2 2 4
Condition66%2/3
$k == 1 and $l == 0
!ll&&!rl&&r
202
MC/DC50%1/2
$k == 1 and $l == 0
$k == 1 $l == 0
Truth table66%2/3
A and B
A= $k == 1B= $l == 0
ABresult
0X0
100
111
248 4 ▶ $mformat='pwm' if ($l!=0);
Branch: 1 branch
BranchTrueFalseTotal
if $l != 0 2 2 4
249 }
250 6 my (@fa,@fc,@fg,@ft,%mparam);
251  
252 6 ▶ if ($mformat eq 'pwm') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mformat eq "pwm") 2 4 6
253 2 foreach my $i (0..$#{$a}) {
254 50 my $ca=exp $a->[$i];
255 50 my $cc=exp $c->[$i];
256 50 my $cg=exp $g->[$i];
257 50 my $ct=exp $t->[$i];
258 50 my $all=$ca+$cc+$cg+$ct;
259 50 push @fa,($ca/$all)*100;
260 50 push @fc,($cc/$all)*100;
261 50 push @fg,($cg/$all)*100;
262 50 push @ft,($ct/$all)*100;
263 }
264 }
265 6 $desc.=", source is $mformat";
266 6 ▶ if ($mformat eq 'pwm') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mformat eq 'pwm') { } 2 4 6
267 2 $desc=~s/^pwm//;
268 2 %mparam=(-pA=>\@fa,-pC=>\@fc,-pG=>\@fg,-pT=>\@ft,-id=>$id,-desc=>$desc,
269 -lA=>$a,-lC=>$c,-lG=>$g,-lT=>$t);
270 }
271 else {
272 4 %mparam=(-pA=>$a,-pC=>$c,-pG=>$g,-pT=>$t,-id=>$id,-desc=>$desc);
273 }
274 6 return new Bio::Matrix::PSM::SiteMatrix(%mparam);
275 }
276  
277 4 ▶sub _rearrange_matrix {
Subroutine _rearrange_matrix: called CC=2 SCAR=6.9
278 4 my $mdata=shift;
279 4 my (@a,@c,@g,@t);
280 4 foreach my $entry (@{$mdata}) {
281 100 my ($a,$c,$g,$t)=@$entry;
282 100 push @a,$a;
283 100 push @c,$c;
284 100 push @g,$g;
285 100 push @t,$t;
286 }
287 4 return \@a,\@c,\@g,\@t;
288 }
289  
290  
291 2 ▶sub _count_positions {
Subroutine _count_positions: called CC=5 SCAR=16.1
292 2 my $seq=shift;
293 2 my %pos;
294 2 my $l=length($seq->[0])-1;
295 2 ▶ for( my $i = 0; $i <= $l; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $l) 50 2 52
296 50 for ( qw(A C G T) ) {
297 200 $pos{$_}->[$i] = 0;
298 }
299 }
300 2 foreach my $sequence (@{$seq}) {
301 18 my @let= split(//,$sequence);
302 18 for my $i (0..$#let) {
303 450 $pos{uc($let[$i])}->[$i]++;
304 }
305 }
306 2 return $pos{A},$pos{C},$pos{G},$pos{T};
307 }
308  
309  
310 25 ▶sub _freq_to_count {
Subroutine _freq_to_count: called CC=1 SCAR=0.0
311 25 my $h=shift;
312 25 my $a=int(10*$h->{pA}+0.5);
313 25 my $c=int(10*$h->{pC}+0.5);
314 25 my $g=int(10*$h->{pG}+0.5);
315 25 my $t=int(10*$h->{pT}+0.5);
316 25 return ($a,$c,$g,$t);
317 }
318  
319 2 1;
« Bio/Search/Result/BlastPullResult.pm ↑ Summary Bio/Seq/Meta/Array.pm »