lib/MS/Search/DB.pm

filter: Statementstmt 57.9% 73 / 126 Branchbran 23.3% 7 / 30 Conditioncond 23.5% 4 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 73.6% 14 / 19 Podpod 100% 8 / 8 totaltotal 50.7% 106 / 209 CC 28 SCAR 31.7
CC
28
Cov
49%
add_from_url
90.0
add_from_source
30.0
deduplicate
12.0
CRAP 23.8
« lib/MS/Spectrum.pm ↑ Summary script/unimod2storable.pl »
1 package MS::Search::DB;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use BioX::Seq::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use BioX::Seq::Fetch;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Net::FTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use List::Util qw/shuffle/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 use Module::Pluggable
14 3 ▶ require => 1, sub_name => 'sources', search_path => ['MS::Search::DB::Source'];
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
17  
18 2 my ($class, $fn) = @_;
19  
20 2 my $self = bless {} => $class;
21  
22 2 ▶ $self->add_from_file($fn) if (defined $fn);
Branch: 1 branch
BranchTrueFalseTotal
if defined $fn 1 1 2
23  
24 2 return $self;
25  
26 }
27  
28 1 ▶sub add_decoys {
Subroutine add_decoys: called CC=6 SCAR=18.7
Pod documented
29  
30 1 my ($self, %args) = @_;
31  
32 1 $self->{decoys} = [];
33 1 ▶ my $type = $args{type} // 'reverse';
Condition50%1/2
$args{"type"} // "reverse"
l!l
01
MC/DC0%0/1
$args{"type"} // "reverse"
$args{"type"}
Truth table50%1/2
A
A= $args{"type"}
Aresult
11
00
34 1 ▶ my $prefix = $args{prefix} // 'DECOY_';
Condition50%1/2
$args{"prefix"} // "DECOY_"
l!l
10
MC/DC0%0/1
$args{"prefix"} // "DECOY_"
$args{"prefix"}
Truth table50%1/2
A
A= $args{"prefix"}
Aresult
11
00
35 1 my $added = 0;
36 1 for my $seq (@{ $self->{seqs} }) {
37  
38 1 ▶ my $new = $type eq 'reverse' ? reverse $seq
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'reverse' ? : 1 0 1
$type eq 'shuffle' ? : 0 0 0
39 : $type eq 'shuffle' ? join( '', shuffle( split '', $seq ) )
40 : die "Unknown decoy type: $type\n";
41 1 my $decoy = BioX::Seq->new(
42 $new,
43 $prefix . $seq->id,
44 $seq->desc,
45 undef
46 );
47  
48 1 push @{ $self->{decoys} }, $decoy;
49 1 ++$added;
50 }
51  
52 1 return $added;
53  
54 }
55  
56 0 ▶sub add_from_source {
Subroutine add_from_source: not called CC=5 SCAR=34.0
Pod documented
57  
58 0 my ($self, %args) = @_;
59  
60 0 ▶ my $suffix = $args{id_suffix} // '';
Condition0%0/2
$args{"id_suffix"} // ""
l!l
00
MC/DC0%0/1
$args{"id_suffix"} // ""
$args{"id_suffix"}
Truth table0%0/2
A
A= $args{"id_suffix"}
Aresult
11
00
61  
62 0 my $added = 0;
63 0 for my $src ($self->sources) {
64 0 ▶ next if ($src ne "MS::Search::DB::Source::$args{source}");
Branch: 1 branch
BranchTrueFalseTotal
if $src ne qq[MS::Search::DB::Source::$args{"source"}] 0 0 0
65 0 delete $args{source};
66 0 my $f = $src->new(%args);
67 0 my ($fh, $pid) = $f->_fetch_fh;
68 0 my $p = BioX::Seq::Stream->new($fh);
69 0 ▶ while (my $seq = $p->next_seq) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $seq = $p->next_seq) 0 0 0
70 0 $seq->id = $seq->id . $suffix;
71 0 push @{ $self->{seqs} }, $seq;
72 0 ++$added;
73 }
74 0 close $fh;
75 0 waitpid($pid, 0);
76 0 last;
77 }
78  
79 0 return $added;
80  
81 }
82  
83 2 ▶sub add_from_file {
Subroutine add_from_file: called CC=4 SCAR=13.9
Pod documented
84  
85 2 my ($self, $fn, %args) = @_;
86  
87 2 ▶ die "File not found\n" if (! -e $fn);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $fn 0 2 2
88  
89 2 ▶ my $suffix = $args{id_suffix} // '';
Condition50%1/2
$args{"id_suffix"} // ""
l!l
02
MC/DC0%0/1
$args{"id_suffix"} // ""
$args{"id_suffix"}
Truth table50%1/2
A
A= $args{"id_suffix"}
Aresult
11
00
90  
91 2 my $added = 0;
92 2 my $p = BioX::Seq::Stream->new($fn);
93 2 ▶ while (my $seq = $p->next_seq) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $seq = $p->next_seq) 2 2 4
94 2 $seq->id = $seq->id . $suffix;
95 2 push @{ $self->{seqs} }, $seq;
96 2 ++$added;
97 }
98  
99 2 return $added;
100  
101 }
102  
103  
104 0 ▶sub add_from_url {
Subroutine add_from_url: not called CC=9 SCAR=45.0
Pod documented
105  
106 0 my ($self, $url, %args) = @_;
107  
108 0 ▶ my $suffix = $args{id_suffix} // '';
Condition0%0/2
$args{"id_suffix"} // ""
l!l
00
MC/DC0%0/1
$args{"id_suffix"} // ""
$args{"id_suffix"}
Truth table0%0/2
A
A= $args{"id_suffix"}
Aresult
11
00
109  
110 0 my $tmp = File::Temp->new(UNLINK => 1);
111  
112 0 my $added = 0;
113  
114 0 my $u = URI->new($url);
115 0 ▶ if ($u->scheme eq 'ftp') {
Branch: 2 branches
BranchTrueFalseTotal
if ($u->scheme eq 'ftp') { } 0 0 0
elsif ($u->scheme eq 'http' or $u->scheme eq 'https') { } 0 0 0
116 0 my $ftp = Net::FTP->new($u->host, Passive => 1);
117 0 ▶ $ftp->login or die "Failed login: $@\n";
Branch: 1 branch
BranchTrueFalseTotal
$ftp->login or die "Failed login: $@\n" 0 0 0
118 0 $ftp->binary();
119 0 ▶ $ftp->get($u->path => $tmp)
Branch: 1 branch
BranchTrueFalseTotal
$ftp->get($u->path, $tmp) or die 'Download failed:' . $ftp->message . "\n" 0 0 0
120 or die "Download failed:" . $ftp->message . "\n";
121 0 $ftp->quit();
122 }
123 ▶ elsif ($u->scheme eq 'http'|| $u->scheme eq 'https') {
Condition0%0/3
$u->scheme eq "http" or $u->scheme eq "https"
l!l&&r!l&&!r
000
MC/DC0%0/2
$u->scheme eq "http" or $u->scheme eq "https"
$u->scheme eq "http" $u->scheme eq "https"
Truth table0%0/3
A or B
A= $u->scheme eq "http"B= $u->scheme eq "https"
ABresult
1X1
011
000
124 my $resp = HTTP::Tiny->new->get($u, { data_callback
125 0 ▶ => sub { print {$tmp} $_[0] } } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
126 0 ▶ die "Download failed\n" if (! $resp->{success});
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->{"success"} 0 0 0
127 }
128 else {
129 0 die "Only FTP and HTTP downloads are currently supported\n";
130 }
131  
132 0 close $tmp;
133  
134 0 my $p = BioX::Seq::Stream->new("$tmp");
135 0 ▶ while (my $seq = $p->next_seq) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $seq = $p->next_seq) 0 0 0
136 0 $seq->id = $seq->id . $suffix;
137 0 push @{ $self->{seqs} }, $seq;
138 0 ++$added;
139 }
140  
141 0 return $added;
142  
143 }
144  
145 1 ▶sub write {
Subroutine write: called CC=4 SCAR=13.9
Pod documented
146  
147 1 my ($self, %args) = @_;
148  
149 1 ▶ my $fh = $args{fh} // \*STDOUT;
Condition50%1/2
$args{"fh"} // \*STDOUT
l!l
10
MC/DC0%0/1
$args{"fh"} // \*STDOUT
$args{"fh"}
Truth table50%1/2
A
A= $args{"fh"}
Aresult
11
00
150  
151 1 my @pool;
152 1 push @pool, map {[$_,'seqs']} 0..$#{ $self->{seqs} };
153 1 push @pool, map {[$_,'decoys']} 0..$#{ $self->{decoys} };
154  
155 1 ▶ @pool = shuffle @pool if ($args{randomize});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"randomize"} 0 1 1
156  
157 1 for (@pool) {
158 2 print {$fh} $self->{$_->[1]}->[$_->[0]]->as_fasta;
159 }
160  
161 1 return 1;
162  
163 }
164  
165 0 ▶sub add_crap {
Subroutine add_crap: not called CC=2 SCAR=17.9
Pod documented
166  
167 0 my ($self, $url) = @_;
168  
169 0 ▶ $url //= 'ftp://ftp.thegpm.org/fasta/cRAP/crap.fasta';
Condition0%0/2
$url //= "ftp://ftp.thegpm.org/fasta/cRAP/crap.fasta"
l!l
00
MC/DC0%0/1
$url //= "ftp://ftp.thegpm.org/fasta/cRAP/crap.fasta"
$url
Truth table0%0/2
A
A= $url
Aresult
11
00
170  
171 0 $self->add_from_url($url);
172  
173 }
174  
175 0 ▶sub deduplicate {
Subroutine deduplicate: not called CC=3 SCAR=24.8
Pod documented
176  
177 0 my ($self) = @_;
178  
179 0 my @dedup;
180 my %seen;
181  
182 0 for my $seq (@{ $self->{seqs} }) {
183 0 ▶ next if ($seen{uc $seq});
Branch: 1 branch
BranchTrueFalseTotal
if $seen{uc $seq} 0 0 0
184 0 push @dedup, $seq;
185 0 $seen{uc $seq} = 1;
186 }
187  
188 0 $self->{seqs} = \@dedup;
189  
190 }
191  
192 3 1;
193  
194 __END__
« lib/MS/Spectrum.pm ↑ Summary script/unimod2storable.pl »