blib/lib/Bio/Gonzales/Seq/Validate/fasta.pm

filter: Statementstmt 89.3% 92 / 103 Branchbran 71.7% 33 / 46 Conditioncond 77.7% 14 / 18 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 1 totaltotal 81.6% 156 / 191 CC 33 SCAR 32.2
CC
33
Cov
83%
validate
38.4
_find_unknown_character
4.0
_shorten_seq
2.0
CRAP 25.1
« blib/lib/Bio/Gonzales/Matrix/Util.pm ↑ Summary blib/lib/Bio/Gonzales/Util/Development/File.pm »
1 #Copyright (c) 2010 Joachim Bargsten <code at bargsten dot org>. All rights reserved.
2  
3 package Bio::Gonzales::Seq::Validate::fasta;
4  
5 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.058'; # VERSION
12  
13 #no use yet
14 1 our %alphabets = (
15 'dna' => [qw(A C G T R Y M K S W H B V D X N)],
16 'rna' => [qw(A C G U R Y M K S W H B V D X N)],
17 'protein' => [
18 qw(A R N D C Q E G H I L K M F U
19 P S T W X Y V B Z J O *)
20 ],
21 );
22  
23 #no use yet
24 1 our %alphabets_strict = (
25 'dna' => [qw( A C G T )],
26 'rna' => [qw( A C G U )],
27 'protein' => [
28 qw(A R N D C Q E G H I L K M F U
29 P S T W Y V O)
30 ],
31 );
32  
33 1 our @protein_alphabet = qw/A B C D E F G H I K L M N P Q R S T V W X Y Z * \-/;
34  
35 1 our @nuc_alphabet = qw/A C G T U M R W S Y K V H D B X N \-/;
36  
37 1 has max_seq_len_out => ( is => 'ro', default => 21 );
38 1 has fh => ( is => 'rw', required => 1 );
39 has prot_regex => (
40 is => 'ro',
41 default => sub {
42 1 ▶ my $r = join "", (@protein_alphabet);
Subroutine __ANON__: called CC=1 SCAR=0.0
43 1 return "[$r]+";
44 }
45 1 );
46 has nuc_regex => (
47 is => 'ro',
48 default => sub {
49 1 ▶ my $r = join "", (@nuc_alphabet);
Subroutine __ANON__: called CC=1 SCAR=0.0
50 1 return "[$r]+";
51 }
52 1 );
53 has seq_regex => (
54 is => 'ro',
55 default => sub {
56 1 ▶ my $r = join "", ( @protein_alphabet, @nuc_alphabet );
Subroutine __ANON__: called CC=1 SCAR=0.0
57 1 return "[$r]+";
58 }
59 1 );
60  
61 1 ▶has _error_cache => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
62  
63 sub validate {
64 1 ▶ my ($self) = @_;
Subroutine validate: called CC=28 SCAR=36.5
Pod undocumented
65  
66 #clear cache
67 1 $self->_error_cache( {} );
68  
69 1 my $fh = $self->fh;
70 1 my $pre = $self->prot_regex;
71 1 my $nre = $self->nuc_regex;
72 1 my $sre = $self->seq_regex;
73  
74 1 my $header;
75 my $seq_type;
76 1 my $is_dos;
77 1 my $probably_seq_in_header;
78 1 my %seen;
79  
80 1 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 371 1 372
81 371 ▶ if ( $. == 1 && /\r\n/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($. == 1 and /\r\n/) 1 370 371
Condition66%2/3
$. == 1 and /\r\n/
!ll&&!rl&&r
37001
MC/DC50%1/2
$. == 1 and /\r\n/
$. == 1 /\r\n/
Truth table66%2/3
A and B
A= $. == 1B= /\r\n/
ABresult
0X0
100
111
82 1 $is_dos = 1;
83 1 $self->_add_error( 0, "File seems to be in DOS format" );
84 }
85  
86 s/\r\n/\n/
87 371 ▶ if ($is_dos);
Branch: 1 branch
BranchTrueFalseTotal
if $is_dos 371 0 371
88  
89 371 chomp;
90  
91 371 ▶ if (/^>/) {
Branch: 4 branches
BranchTrueFalseTotal
if (/^>/) { } 182 189 371
elsif (/[^>]+>/) { } 4 185 189
elsif (/^\s*$/) { } 0 185 185
elsif ($header or $seq_type) { } 185 0 185
92 182 ▶ if ( $header && $probably_seq_in_header ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($header and $probably_seq_in_header) { } 4 178 182
elsif ($header) { } 1 177 178
Condition100%3/3
$header and $probably_seq_in_header
!ll&&!rl&&r
17714
MC/DC100%2/2
$header and $probably_seq_in_header
$header $probably_seq_in_header
Truth table100%3/3
A and B
A= $headerB= $probably_seq_in_header
ABresult
0X0
100
111
93 4 $self->_add_error(
94 ( $. - 1 ),
95 "Wrong header format, seems to be sequence in the header: >>"
96 . $self->_shorten_seq($probably_seq_in_header) . "<<"
97 );
98 4 undef $probably_seq_in_header;
99 } elsif ($header) {
100 1 $self->_add_error( $. - 1, "No sequence after header." );
101 }
102 #header
103 182 for my $re ( $pre, $nre ) {
104 364 ▶ $probably_seq_in_header = $1
Branch: 1 branch
BranchTrueFalseTotal
if /\s+($re)\s*$/ || /\s+($re)\s*$/i and length $1 > 3 9 355 364
Condition83%5/6
/\s+($re)\s*$/ || /\s+($re)\s*$/i and length $1 > 3
!ll&&!rl&&r
35509
/\s+($re)\s*$/ or /\s+($re)\s*$/i
l!l&&r!l&&!r
45355
MC/DC66%2/3
/\s+($re)\s*$/ || /\s+($re)\s*$/i and length $1 > 3
/\s+($re)\s*$/ /\s+($re)\s*$/i length $1 > 3
Truth table60%3/5
A or B and C
A= /\s+($re)\s*$/B= /\s+($re)\s*$/iC= length $1 > 3
ABCresult
00X0
1X00
0100
1X11
0111
105 if ( ( (/\s+($re)\s*$/) || (/\s+($re)\s*$/i) ) && length($1) > 3 );
106 }
107 182 $header = 1;
108  
109 182 ▶ if (/^>([^\s]+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^>([^\s]+)/) { } 182 0 182
elsif (/^>\s*$/) { } 0 0 0
110 182 ▶ $self->_add_error( $., "ID is very long: " . $self->_shorten_seq($1) )
Branch: 1 branch
BranchTrueFalseTotal
if length $1 > 50 0 182 182
111 if ( length($1) > 50 );
112 $self->_add_error( $., "ID is ambiguous." )
113 182 ▶ if ( exists $seen{$1} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen{$1} 1 181 182
114 182 $seen{$1} = 1;
115 } elsif (/^>\s*$/) {
116 0 $self->_add_error( $., "Empty header" );
117 } else {
118 0 $self->_add_error( $., "No ID in header." );
119 }
120 182 undef $seq_type;
121 } elsif (/[^>]+>/) {
122 4 $self->_add_error( $., "Wrong header format, '>' not in the beginning." );
123  
124 4 ▶ $self->_add_error( $. - 1, "No sequence after header." )
Branch: 1 branch
BranchTrueFalseTotal
if $header 1 3 4
125 if ($header);
126 4 $header = 1;
127 4 undef $seq_type;
128 } elsif (/^\s*$/) {
129 0 $self->_add_error( $., "Empty line." );
130 ▶ } elsif ( $header || $seq_type ) {
Condition66%2/3
$header or $seq_type
l!l&&r!l&&!r
18050
MC/DC0%0/2
$header or $seq_type
$header $seq_type
Truth table66%2/3
A or B
A= $headerB= $seq_type
ABresult
1X1
011
000
131 #now sequence should be there
132 185 ▶ if ( $seq_type && !/^$seq_type$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq_type and not /^$seq_type$/i) { } 0 185 185
Condition66%2/3
$seq_type and not /^$seq_type$/i
!ll&&!rl&&r
18050
MC/DC0%0/2
$seq_type and not /^$seq_type$/i
$seq_type not /^$seq_type$/i
Truth table66%2/3
A and not(B)
A= $seq_typeB= not /^$seq_type$/i
ABresult
0X0
100
111
133 0 my @unknown = $self->_find_unknown_character( $_, $sre );
134 0 ▶ if ( @unknown > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unknown > 1) { } 0 0 0
135 0 $self->_add_error( $., "Found unknown characters: >>" . join( "", @unknown ) . "<<" );
136 } else {
137 0 $self->_add_error( $., "Sequence type switch (from nucleotide to protein or vice versa)" );
138 }
139 } else {
140 185 ▶ if (/^$pre$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^$pre$/i) { } 180 5 185
elsif (/^$nre$/i) { } 0 5 5
141 180 $seq_type = $pre;
142 } elsif (/^$nre$/i) {
143 0 $seq_type = $nre;
144 } else {
145 5 my @unknown = $self->_find_unknown_character( $_, $sre );
146 5 ▶ if ( @unknown > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unknown > 0) { } 5 0 5
147 5 $self->_add_error( $., "Found unknown characters: >>" . join( "", @unknown ) . "<<" );
148 } else {
149 0 $self->_add_error( $., "Found mixed protein and DNA/RNA sequence." );
150 }
151 5 $seq_type = $sre;
152 }
153 }
154 185 undef $header;
155 } else {
156 0 $self->_add_error( $., " ERROR IN THE SCRIPT" );
157 }
158 }
159 1 return $self->_error_cache;
160 }
161  
162 sub _add_error {
163 17 ▶ my ( $self, $line, $msg ) = @_;
Subroutine _add_error: called CC=2 SCAR=6.9
164  
165 17 ▶ $self->_error_cache->{$line} = [] unless defined $self->_error_cache->{$line};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_error_cache->{$line} 16 1 17
166 17 push @{ $self->_error_cache->{$line} }, $msg;
167 }
168  
169 sub _find_unknown_character {
170 5 ▶ my ( $self, $seq, @re ) = @_;
Subroutine _find_unknown_character: called CC=4 SCAR=13.9
171  
172 5 my @unknown;
173 5 my @seq = split //, $seq;
174 5 for my $re (@re) {
175 5 for my $c (@seq) {
176 176 ▶ push @unknown, $c
Branch: 1 branch
BranchTrueFalseTotal
unless $c =~ /$re/i 131 45 176
177 unless ( $c =~ /$re/i );
178 }
179 }
180 5 return @unknown;
181 }
182  
183 sub _shorten_seq {
184 4 ▶ my ( $self, $seq ) = @_;
Subroutine _shorten_seq: called CC=2 SCAR=7.1
185  
186 4 my $len = $self->max_seq_len_out;
187  
188 4 my $short_seq;
189 4 ▶ if ( length($seq) >= $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $seq >= $len) { } 4 0 4
190 4 $short_seq = substr $seq, 0, 9;
191 4 $short_seq .= " ... ";
192 4 $short_seq .= substr $seq, -9;
193  
194 4 return $short_seq;
195 } else {
196 0 return $seq;
197 }
198 }
199  
200 1 1;
« blib/lib/Bio/Gonzales/Matrix/Util.pm ↑ Summary blib/lib/Bio/Gonzales/Util/Development/File.pm »