blib/lib/Bio/Gonzales/Feat/IO/SWISS.pm

filter: Statementstmt 93.3% 112 / 120 Branchbran 76.6% 72 / 94 Conditioncond 35.2% 6 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 2 totaltotal 79.0% 204 / 258 CC 57 SCAR 40.1
CC
57
Cov
82%
Parse_entry
78.7
next_feat
6.1
_merge_descriptions
5.0
CRAP 55.0
« blib/lib/Bio/Gonzales/Seq/IO.pm ↑ Summary blib/lib/Bio/Gonzales/Seq/Filter.pm »
1 package Bio::Gonzales::Feat::IO::SWISS;
2  
3 # https://github.com/biopython/biopython/blob/master/Bio/SwissProt/__init__.py
4 # http://web.expasy.org/docs/userman.html#GN_line
5 # https://metacpan.org/pod/Bio::SeqIO::swiss
6 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use List::MoreUtils qw/zip/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Bio::Gonzales::Feat;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Bio::Gonzales::Seq::Util qw/crc64/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Bio::Gonzales::MiniFeat;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 extends 'Bio::Gonzales::Feat::IO::Base';
22  
23 #my @lineObjects = ('IDs', 'ACs', 'DTs', 'DEs', 'GNs',
24 #'OSs', 'OGs', 'OCs', 'OXs', 'OHs',
25 #'Refs', 'CCs', 'DRs', 'PE', 'KWs', 'FTs', 'Stars', 'SQs');
26  
27 1 our $VERSION = '0.058'; # VERSION
28 1 has check_crc64 => ( is => 'rw' );
29  
30 sub next_feat {
31 26 ▶ my ($self) = @_;
Subroutine next_feat: called CC=6 SCAR=18.1
Pod undocumented
32 26 my $fhi = $self->_fhi;
33  
34 26 my $l;
35 my @entry;
36 26 ▶ while ( defined( $l = $fhi->() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($l = &$fhi())) 1060 1 1061
37 1060 ▶ next if ( !$l || $l =~ /^\s*$/ );
Branch: 1 branch
BranchTrueFalseTotal
if not $l or $l =~ /^\s*$/ 0 1060 1060
Condition33%1/3
not $l or $l =~ /^\s*$/
l!l&&r!l&&!r
001060
MC/DC0%0/2
not $l or $l =~ /^\s*$/
not $l $l =~ /^\s*$/
Truth table33%1/3
not(A) or B
A= not $lB= $l =~ /^\s*$/
ABresult
1X1
011
000
38 1060 push @entry, $l;
39 1060 ▶ last if $l =~ m{^//};
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ m[^//] 25 1035 1060
40 }
41 26 ▶ if ( @entry > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@entry > 0) { } 25 1 26
42 25 return Parse_entry( \@entry );
43 } else {
44 1 return;
45 }
46 }
47  
48 sub Parse_entry {
49 25 ▶ my $data = shift;
Subroutine Parse_entry: called CC=48 SCAR=43.7
Pod undocumented
50  
51 25 my $lines;
52 25 ▶ if ( ref $data eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 25 0 25
elsif (ref $data eq 'SCALAR') { } 0 0 0
53 25 $lines = $data;
54 } elsif ( ref $data eq 'SCALAR' ) {
55 0 $lines = [ split /\n/, $$data ];
56 } else {
57 0 $lines = [ split /\n/, $data ];
58 }
59  
60 25 my $ide = shift @$lines;
61  
62 25 ▶ confess "could not parse ID field: $ide"
Branch: 1 branch
BranchTrueFalseTotal
unless $ide =~ /^ ID \s+ # (\S+) \s+ # $1 entryname ([^\s;]+); \s+ # $2 DataClass (?:PRT;)? \s+ # Molecule Type (optional) [0-9]+[ ]AA \. # Sequencelength (capture?) $ /ox 0 25 25
63 unless (
64 $ide =~ m{^
65 ID \s+ #
66 (\S+) \s+ # $1 entryname
67 ([^\s;]+); \s+ # $2 DataClass
68 (?:PRT;)? \s+ # Molecule Type (optional)
69 [0-9]+[ ]AA \. # Sequencelength (capture?)
70 $
71 }ox
72 );
73  
74 25 my ( $name, $seq_div ) = ( $1, $2 );
75 25 ▶ my $src
Branch: 2 branches
BranchTrueFalseTotal
$seq_div eq 'Reviewed' || $seq_div eq 'STANDARD' ? : 0 25 25
$seq_div eq 'Unreviewed' || $seq_div eq 'PRELIMINARY' ? : 25 0 25
Condition33%2/6
$seq_div eq "Reviewed" or $seq_div eq "STANDARD"
l!l&&r!l&&!r
0025
$seq_div eq "Unreviewed" or $seq_div eq "PRELIMINARY"
l!l&&r!l&&!r
2500
MC/DC0%0/2
$seq_div eq "Reviewed" or $seq_div eq "STANDARD"
$seq_div eq "Reviewed" $seq_div eq "STANDARD"
MC/DC0%0/2
$seq_div eq "Unreviewed" or $seq_div eq "PRELIMINARY"
$seq_div eq "Unreviewed" $seq_div eq "PRELIMINARY"
Truth table33%1/3
A or B
A= $seq_div eq "Reviewed"B= $seq_div eq "STANDARD"
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= $seq_div eq "Unreviewed"B= $seq_div eq "PRELIMINARY"
ABresult
1X1
011
000
76 = ( $seq_div eq 'Reviewed' || $seq_div eq 'STANDARD' ) ? 'sp'
77 : ( $seq_div eq 'Unreviewed' || $seq_div eq 'PRELIMINARY' ) ? 'tr'
78 : $seq_div;
79 25 my $mfeat = Bio::Gonzales::MiniFeat->new(
80 type => 'polypeptide',
81 source => $src,
82 attributes => { 'Name' => [$name] }
83 );
84 25 my $sequence;
85 25 my %description = (main => []);
86 25 my $description_level = 'main';
87  
88 25 my @seq;
89 25 for my $e (@$lines) {
90  
91 1035 my $key = substr $e, 0, 2;
92  
93 # FIXME also parse these parts of the data entry
94 #last if ( $key eq 'FT' );
95 #last if ( $key eq 'SQ' );
96  
97 1035 ▶ last if ( $key eq '//' );
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "//" 25 1010 1035
98  
99 1010 my $val = substr $e, 5;
100  
101 1010 ▶ die "too short: $e" unless ( $key && $val );
Branch: 1 branch
BranchTrueFalseTotal
unless $key and $val 0 1010 1010
Condition33%1/3
$key and $val
!ll&&!rl&&r
001010
MC/DC0%0/2
$key and $val
$key $val
Truth table33%1/3
A and B
A= $keyB= $val
ABresult
0X0
100
111
102 1010 $val =~ s/[.;]\s*$//;
103  
104 1010 ▶ if ( $key eq '**' ) {
Branch: 26 branches
BranchTrueFalseTotal
if ($key eq '**') { } 0 1010 1010
elsif ($key eq 'AC') { } 25 985 1010
elsif ($key eq 'DT') { } 75 910 985
elsif ($key eq 'DE') { } 49 861 910
elsif ($key eq 'GN') { } 24 837 861
elsif ($key eq 'OS') { } 25 812 837
elsif ($key eq 'OG') { } 0 812 812
elsif ($key eq 'OC') { } 27 785 812
elsif ($key eq 'OX') { } 25 760 785
elsif ($key eq 'OH') { } 0 760 760
elsif ($key eq 'RN') { } 30 730 760
elsif ($key eq 'RP') { } 30 700 730
elsif ($key eq 'RC') { } 2 698 700
elsif ($key eq 'RX') { } 5 693 698
elsif ($key eq 'RL') { } 30 663 693
elsif ($key eq 'RA') { } 52 611 663
elsif ($key eq 'RG') { } 0 611 611
elsif ($key eq 'RT') { } 53 558 611
elsif ($key eq 'CC') { } 103 455 558
elsif ($key eq 'DR') { } 261 194 455
elsif ($key eq 'PE') { } 25 169 194
elsif ($key eq 'KW') { } 6 163 169
elsif ($key eq 'FT') { } 46 117 163
elsif ($key eq 'SQ') { } 25 92 117
elsif ($key eq ' ') { } 92 0 92
elsif ($key eq '//') { } 0 0 0
105 #See Bug 2353, some files from the EBI have extra lines
106 #starting "**" (two asterisks/stars). They appear
107 #to be unofficial automated annotations. e.g.
108 #**
109 #** ################# INTERNAL SECTION ##################
110 #**HA SAM; Annotated by PicoHamap 1.88; MF_01138.1; 09-NOV-2003.
111 0 next;
112 } elsif ( $key eq 'AC' ) {
113 25 $mfeat->add_attr( 'accession_number' => [ split /;\s+/, $val ] );
114 } elsif ( $key eq 'DT' ) {
115 } elsif ( $key eq 'DE' ) {
116 49 ▶ if($val =~ /^(\w+):\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^(\w+):\s*$/) 0 49 49
117 0 $description_level = lc($1);
118 0 next;
119 }
120 49 ▶ if($val =~ s/^Flags:\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ s/^Flags:\s*//) 23 26 49
121 23 push @{$description{main}}, [ 'flags', undef, split( /;\s*/, $val )];
122 23 next;
123 }
124 26 ▶ die $val unless($val =~ /^(?:(\w+):)?\s*(\w+)=\s*(.*)$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^(?:(\w+):)?\s*(\w+)=\s*(.*)$/ 0 26 26
125 26 ▶ my $cat = $1 // $description{$description_level}[-1][0];
Condition33%1/3
$1 // $description{$description_level}[-1][0]
l!l&&r!l&&!r
2600
MC/DC0%0/2
$1 // $description{$description_level}[-1][0]
$1 $description{$description_level}[-1][0]
Truth table33%1/3
A // B
A= $1B= $description{$description_level}[-1][0]
ABresult
1X1
011
000
126 26 push @{$description{$description_level}}, [ lc($cat), lc($2), $3 ];
127 } elsif ( $key eq 'GN' ) {
128 24 ▶ next if ( $val eq 'and' );
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "and" 0 24 24
129 24 for my $a ( split /;\s+/, $val ) {
130 24 my ( $ak, $av ) = split /=/, $a, 2;
131 24 ▶ $mfeat->add_attr( "gene_" . lc($ak) => [ $av ? split( /\s*,\s*/, $av ) : '' ] );
Branch: 1 branch
BranchTrueFalseTotal
$av ? : 24 0 24
132 }
133 } elsif ( $key eq 'OS' ) {
134 } elsif ( $key eq 'OG' ) {
135 } elsif ( $key eq 'OC' ) {
136 } elsif ( $key eq 'OX' ) {
137 25 ▶ if ( $val =~ /NCBI_TaxID=(\w+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /NCBI_TaxID=(\w+)/) { } 25 0 25
138 25 $mfeat->add_attr( 'ncbi_taxid' => $1 );
139 } else {
140 0 confess "$val doesn't look like NCBI_TaxID";
141 }
142 } elsif ( $key eq 'OH' ) {
143 } elsif ( $key eq 'RN' ) {
144  
145 } elsif ( $key eq 'RP' ) {
146 # rn required
147 } elsif ( $key eq 'RC' ) {
148 # rn required
149 } elsif ( $key eq 'RX' ) {
150 # rn required
151 } elsif ( $key eq 'RL' ) {
152 # rn required
153 # In UniProt release 1.12 of 6/21/04, there is a new RG
154 # (Reference Group) line, which references a group instead of
155 # an author. Each block must have at least 1 RA or RG line.
156 } elsif ( $key eq 'RA' ) {
157 # rn required
158 } elsif ( $key eq 'RG' ) {
159 # rn required
160 } elsif ( $key eq "RT" ) {
161 } elsif ( $key eq 'CC' ) {
162 } elsif ( $key eq 'DR' ) {
163 261 my ( $database, $primaryid, $optional, @comment ) = split /;\s+/, $val;
164 261 my $comment = join " ", @comment;
165  
166 # drop leading and training spaces and trailing .
167 261 $comment =~ s/\.\s*$//;
168  
169 261 $mfeat->add_attr(
170 'dbxref' => {
171 db => $database,
172 id => $primaryid,
173 optional_id => $optional,
174 comment => $comment
175 }
176 );
177 } elsif ( $key eq 'PE' ) {
178 } elsif ( $key eq 'KW' ) {
179 #cols = value.rstrip(";.").split('; ')
180 #record.keywords.extend(cols)
181 } elsif ( $key eq 'FT' ) {
182 #_read_ft($feat, line)
183 } elsif ( $key eq 'SQ' ) {
184 #SQ SEQUENCE XXXX AA; XXXXX MW; XXXXXXXXXXXXXXXX CRC64;
185 25 $val =~ s/^SEQUENCE\s+//;
186 25 my ( $length, $weight, $crc64 ) = split /;\s+/, $val;
187 25 $length =~ s/\s+AA$//;
188 25 $weight =~ s/\s+MW$//;
189 25 $crc64 =~ s/\s+CRC64$//;
190 25 $mfeat->add_attr(
191 'seq' => { length => int($length), molecular_weight => $weight + 0.0, crc64 => $crc64 } );
192 } elsif ( $key eq ' ' ) {
193 92 $val =~ y/A-Za-z//cd;
194 92 $sequence .= $val;
195 } elsif ( $key eq '//' ) {
196 # Join multiline data into one string
197 #record.description = " ".join(record.description)
198 #record.organism = " ".join(record.organism)
199 #record.organelle = record.organelle.rstrip()
200 #for reference in record.references:
201 #reference.authors = " ".join(reference.authors).rstrip(";")
202 #reference.title = " ".join(reference.title).rstrip(";")
203 #if reference.title.startswith('"') and reference.title.endswith('"'):
204 #reference.title = reference.title[1:-1] # remove quotes
205 #reference.location = " ".join(reference.location)
206 #record.sequence = "".join(_sequence_lines)
207 #return record
208 0 last;
209 } else {
210 0 die sprintf( "Unknown keyword '%s' found", $key );
211 }
212 }
213  
214 25 $mfeat->add_attr(description => _merge_descriptions(\%description));
215 25 $mfeat->add_attr( ID => $mfeat->attr_first('accession_number') );
216  
217 die "no sequence object found in " . $mfeat->id . Dumper($mfeat)
218 25 ▶ unless ( $mfeat->attr->{seq} );
Branch: 1 branch
BranchTrueFalseTotal
unless $mfeat->attr->{"seq"} 0 25 25
219 die "CRC64 does not match for " . $mfeat->id
220 25 ▶ unless ( crc64($sequence) eq $mfeat->attr->{seq}[0]{crc64} );
Branch: 1 branch
BranchTrueFalseTotal
unless crc64($sequence) eq $mfeat->attr->{"seq"}[0]{"crc64"} 0 25 25
221 25 $mfeat->attr->{seq}[0]{data} = $sequence;
222  
223 25 return $mfeat;
224 }
225  
226 sub _merge_descriptions {
227 25 ▶ my $desc = shift;
Subroutine _merge_descriptions: called CC=5 SCAR=16.1
228 25 my %desc_new;
229 25 ▶ while(my ($lvl, $data) = each %$desc) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$desc) 25 25 50
230 25 for my $d (@$data) {
231 49 my $cat = shift @$d;
232 49 my $scat = shift @$d;
233  
234 49 ▶ $cat .= "_" . $scat if($scat);
Branch: 1 branch
BranchTrueFalseTotal
if $scat 26 23 49
235  
236 49 ▶ $desc_new{$lvl}{$cat} //= [];
Condition50%1/2
$desc_new{$lvl}{$cat} //= []
l!l
049
MC/DC0%0/1
$desc_new{$lvl}{$cat} //= []
$desc_new{$lvl}{$cat}
Truth table50%1/2
A
A= $desc_new{$lvl}{$cat}
Aresult
11
00
237  
238 49 push @{$desc_new{$lvl}{$cat}}, @$d;
239 }
240  
241 }
242 25 return { %{delete $desc_new{main}}, %desc_new };
243  
244 }
245  
246 1 __PACKAGE__->meta->make_immutable();
« blib/lib/Bio/Gonzales/Seq/IO.pm ↑ Summary blib/lib/Bio/Gonzales/Seq/Filter.pm »