lib/FASTX/ScriptHelper.pm

filter: Statementstmt 75.4% 154 / 204 Branchbran 39.2% 33 / 84 Conditioncond 37.5% 12 / 32 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 92.8% 26 / 28 Podpod 100% 11 / 11 totaltotal 62.9% 236 / 375 CC 58 SCAR 26.2
CC
58
Cov
62%
verbose
44.5
fu_printfastq
30.0
download
20.0
CRAP 13.8
« lib/FASTX/Reader.pm ↑ Summary lib/FASTX/Seq.pm »
1 package FASTX::ScriptHelper;
2 #ABSTRACT: Shared routines for binaries using FASTX::Reader and FASTX::PE.
3  
4 2 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use File::Fetch;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp qw(confess cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use FASTX::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Term::ANSIColor qw(color);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Capture::Tiny qw(capture);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Time::HiRes qw( time );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Scalar::Util qw( blessed refaddr reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 $FASTX::ScriptHelper::VERSION = '0.1.2';
18  
19 2 our @ISA = qw(Exporter);
20 2 our @EXPORT = qw(rc fu_printfasta fu_printfastq verbose);
21 2 our @EXPORT_OK = qw($fu_linesize $fu_verbose); # symbols to export on request
22  
23  
24 sub new {
25  
26 # Instantiate object
27 3 ▶ my ($class, $args) = @_;
Subroutine new: called CC=9 SCAR=23.5
Pod documented
28  
29 3 my %accepted_parameters = (
30 'verbose' => 1,
31 'debug' => 1,
32 'logfile' => 1,
33 'linesize'=> 1,
34 );
35  
36 3 my $valid_attributes = join(', ', keys %accepted_parameters);
37  
38 3 for my $parameter (keys %{ $args} ) {
39 confess("Attribute <$parameter> is not expected. Valid attributes are: $valid_attributes\n")
40 8 ▶ if (! $accepted_parameters{$parameter} );
Branch: 1 branch
BranchTrueFalseTotal
unless $accepted_parameters{$parameter} 1 7 8
41 }
42  
43  
44 my $self = {
45 logfile => $args->{logfile} // undef,
46 debug => $args->{debug} // 0,
47 verbose => $args->{verbose} // 0,
48 2 ▶ linesize => $args->{linesize} // 0,
Condition50%4/8
$args->{"logfile"} // undef
l!l
20
$args->{"debug"} // 0
l!l
02
$args->{"verbose"} // 0
l!l
20
$args->{"linesize"} // 0
l!l
20
MC/DC0%0/1
$args->{"logfile"} // undef
$args->{"logfile"}
MC/DC0%0/1
$args->{"debug"} // 0
$args->{"debug"}
MC/DC0%0/1
$args->{"verbose"} // 0
$args->{"verbose"}
MC/DC0%0/1
$args->{"linesize"} // 0
$args->{"linesize"}
Truth table50%1/2
A
A= $args->{"logfile"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"debug"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"verbose"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"linesize"}
Aresult
11
00
49 };
50 2 my $object = bless $self, $class;
51  
52 # Regular log file
53 2 ▶ if (defined $self->{logfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'logfile'}) { } 2 0 2
54 2 verbose($self, "Ready to log in $object->{logfile}");
55 2 ▶ open my $logfh, '>', "$object->{logfile}" || confess("ERROR: Unable to write log file to $object->{logfile}\n");
Condition33%1/3
"$object->{'logfile'}" || confess("ERROR: Unable to write log file to $object->{'logfile'}\n")
l!l&&r!l&&!r
200
MC/DC0%0/2
"$object->{'logfile'}" || confess("ERROR: Unable to write log file to $object->{'logfile'}\n")
"$object->{'logfile'}" confess("ERROR: Unable to write log file to $object->{'logfile'}\n")
Truth table33%1/3
A || B
A= "$object->{'logfile'}"B= confess("ERROR: Unable to write log file to $object->{'logfile'}\n")
ABresult
1X1
011
000
56 2 $object->{logfh} = $logfh;
57 2 $object->{do_log} = 1;
58 } else {
59 # Set {logfh} to Stderr, but do not set {do_log}
60 0 $object->{logfh} = *STDERR;
61 }
62  
63 2 return $object;
64 }
65  
66  
67  
68 sub fu_printfasta {
69  
70 2 ▶ my $self = undef;
Subroutine fu_printfasta: called CC=5 SCAR=17.4
Pod documented
71 2 ▶ if ( ref($_[0]) eq 'FASTX::ScriptHelper' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "FASTX::ScriptHelper") 2 0 2
72 2 $self = shift @_;
73 }
74  
75 2 my ($name, $comment, $seq) = @_;
76 2 ▶ confess("No sequence provided for $name") unless defined $seq;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seq 0 2 2
77 2 my $print_comment = '';
78 2 ▶ if (defined $comment) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comment) 0 2 2
79 0 $print_comment = ' ' . $comment;
80 }
81  
82 2 say '>', $name, $print_comment;
83 2 ▶ if ($self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) { } 2 0 2
84 2 print split_string($self,$seq);
85 } else {
86 0 print split_string($seq);
87 }
88  
89 }
90  
91  
92 sub fu_printfastq {
93 0 ▶ my $self = undef;
Subroutine fu_printfastq: not called CC=5 SCAR=34.0
Pod documented
94 0 ▶ if ( ref($_[0]) eq 'FASTX::ScriptHelper' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "FASTX::ScriptHelper") 0 0 0
95 0 $self = shift @_;
96 }
97 0 my ($name, $comment, $seq, $qual) = @_;
98 0 my $print_comment = '';
99 0 ▶ if (defined $comment) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comment) 0 0 0
100 0 $print_comment = ' ' . $comment;
101 }
102 0 ▶ $qual = 'I' x length($seq) unless (defined $qual);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $qual 0 0 0
103 0 say '@', $name, $print_comment;
104 0 ▶ if ($self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) { } 0 0 0
105 0 print split_string($self,$seq) , "+\n", split_string($self,$qual);
106 } else {
107 0 print split_string($seq) , "+\n", split_string($qual);
108 }
109  
110 }
111  
112  
113 sub rc {
114 1 ▶ my $self = undef;
Subroutine rc: called CC=3 SCAR=11.2
Pod documented
115 1 ▶ if ( ref($_[0]) eq 'FASTX::ScriptHelper' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "FASTX::ScriptHelper") 1 0 1
116 1 $self = shift @_;
117 }
118 1 my $sequence = reverse($_[0]);
119 1 ▶ if (is_seq($sequence)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_seq($sequence)) 1 0 1
120 1 $sequence =~tr/ACGTacgt/TGCAtgca/;
121 1 return $sequence;
122 }
123 }
124  
125  
126 sub is_seq {
127 1 ▶ my $self = undef;
Subroutine is_seq: called CC=3 SCAR=12.3
Pod documented
128 1 ▶ if ( ref($_[0]) eq 'FASTX::ScriptHelper' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "FASTX::ScriptHelper") 0 1 1
129 0 $self = shift @_;
130 }
131 1 my $string = shift @_;
132 1 ▶ if ($string =~/[^ACGTRYSWKMBDHVN]/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /[^ACGTRYSWKMBDHVN]/iu) { } 0 1 1
133 0 return 0;
134 } else {
135 1 return 1;
136 }
137 }
138  
139  
140 sub split_string {
141 2 ▶ my $self = undef;
Subroutine split_string: called CC=7 SCAR=20.4
Pod documented
142 2 ▶ if ( ref($_[0]) eq 'FASTX::ScriptHelper' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "FASTX::ScriptHelper") 2 0 2
143 2 $self = shift @_;
144 }
145 2 my $input_string = shift @_;
146 2 ▶ confess("No string provided") unless $input_string;
Branch: 1 branch
BranchTrueFalseTotal
unless $input_string 0 2 2
147 2 my $formatted = '';
148 2 ▶ my $line_width = $self->{linesize} // $main::opt_line_size // 0; # change here
Condition40%2/5
($self->{"linesize"} // $main::opt_line_size) // 0
l!l
20
$self->{"linesize"} // $main::opt_line_size
l!l&&r!l&&!r
200
MC/DC0%0/2
($self->{"linesize"} // $main::opt_line_size) // 0
$self->{"linesize"} $main::opt_line_size
Truth table33%1/3
A // B
A= $self->{"linesize"}B= $main::opt_line_size
ABresult
1X1
011
000
149  
150 2 ▶ return $input_string. "\n" unless ($line_width);
Branch: 1 branch
BranchTrueFalseTotal
unless $line_width 0 2 2
151 2 ▶ for (my $i = 0; $i < length($input_string); $i += $line_width) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $input_string) 2 2 4
152 2 my $frag = substr($input_string, $i, $line_width);
153 2 $formatted .= $frag."\n";
154 }
155 2 return $formatted;
156 }
157  
158  
159 sub verbose {
160 2 ▶ my $self = undef;
Subroutine verbose: called CC=13 SCAR=38.0
Pod documented
161 2 ▶ if ( ref($_[0]) eq 'FASTX::ScriptHelper' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "FASTX::ScriptHelper") 2 0 2
162 2 $self = shift @_;
163 }
164 2 my ($message, $reference, $reference_name, @remainder) = @_;
165 2 ▶ if ($remainder[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($remainder[0]) { } 0 2 2
elsif (defined $reference and builtin::reftype($reference) eq undef) { } 0 4 4
166 0 $message .= $reference . $reference_name . join('', @remainder);
167 0 $reference = undef;
168 0 $reference_name = undef;
169 ▶ } elsif (defined $reference and reftype $reference eq undef) {
Condition33%1/3
defined $reference and builtin::reftype($reference) eq undef
!ll&&!rl&&r
200
MC/DC0%0/2
defined $reference and builtin::reftype($reference) eq undef
defined $reference builtin::reftype($reference) eq undef
Truth table33%1/3
A and B
A= defined $referenceB= builtin::reftype($reference) eq undef
ABresult
0X0
100
111
170 # Mistakenly passed list instead of string
171 0 $message .= $reference;
172 0 ▶ if (defined $reference_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reference_name) 0 0 0
173 0 $message .= $reference_name;
174 0 $reference_name = undef;
175 }
176 0 $reference = undef;
177  
178 }
179 2 ▶ my $variable_name = $reference_name // 'data';
Condition50%1/2
$reference_name // "data"
l!l
02
MC/DC0%0/1
$reference_name // "data"
$reference_name
Truth table50%1/2
A
A= $reference_name
Aresult
11
00
180 2 my $timestamp = _getTimeStamp();
181 2 ▶ if ( (defined $self and $self->{verbose} ) or (defined $main::opt_verbose and $main::opt_verbose) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self and $self->{'verbose'} or defined $main::opt_verbose and $main::opt_verbose) { } 2 0 2
Condition22%2/9
defined $self and $self->{"verbose"} or defined $main::opt_verbose and $main::opt_verbose
l!l&&r!l&&!r
200
defined $self and $self->{"verbose"}
!ll&&!rl&&r
002
defined $main::opt_verbose and $main::opt_verbose
!ll&&!rl&&r
000
MC/DC0%0/4
defined $self and $self->{"verbose"} or defined $main::opt_verbose and $main::opt_verbose
defined $self $self->{"verbose"} defined $main::opt_verbose $main::opt_verbose
Truth table14%1/7
A and B or C and D
A= defined $selfB= $self->{"verbose"}C= defined $main::opt_verboseD= $main::opt_verbose
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
182 # Print
183 2 ▶ if (defined $self->{do_log}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"do_log"}) 0 2 2
184 0 $self->writelog($message, $reference, $reference_name);
185 }
186 2 say STDERR color('cyan'),"[$timestamp]", color('reset'), " $message";
187 2 ▶ say STDERR color('magenta'), Data::Dumper->Dump([$reference], [$variable_name])
Branch: 1 branch
BranchTrueFalseTotal
if defined $reference 0 2 2
188 if (defined $reference);
189 } else {
190 # No --verbose, don't print
191 0 return -1;
192 }
193  
194 }
195  
196  
197  
198 sub writelog {
199 3 ▶ my $self = undef;
Subroutine writelog: called CC=4 SCAR=14.5
Pod documented
200 3 ▶ if ( ref($_[0]) eq 'FASTX::ScriptHelper' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "FASTX::ScriptHelper") 3 0 3
201 3 $self = shift @_;
202 }
203  
204 3 my ($message, $reference, $reference_name) = @_;
205 3 ▶ my $variable_name = $reference_name // 'data';
Condition50%1/2
$reference_name // "data"
l!l
03
MC/DC0%0/1
$reference_name // "data"
$reference_name
Truth table50%1/2
A
A= $reference_name
Aresult
11
00
206 3 my $timestamp = _getTimeStamp();
207 3 say {$self->{logfh}} "[$timestamp] $message";
208 3 ▶ say {$self->{logfh}} Data::Dumper->Dump([$reference], [$variable_name]) if (defined $reference);
Branch: 1 branch
BranchTrueFalseTotal
if defined $reference 0 3 3
209  
210  
211 }
212  
213  
214  
215  
216 sub download {
217 0 ▶ my $begin_time = time();
Subroutine download: not called CC=4 SCAR=30.0
Pod documented
218 0 my $self = undef;
219 0 ▶ if ( ref($_[0]) eq 'FASTX::ScriptHelper' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "FASTX::ScriptHelper") 0 0 0
220 0 $self = shift @_;
221 }
222  
223 0 my ($url, $destination) = @_;
224 0 ▶ if (defined $self->{do_log}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"do_log"}) 0 0 0
225 0 $self->writelog( qq(Downloading "$url") );
226 }
227  
228  
229 0 my $downloader = File::Fetch->new(uri => $url);
230 0 ▶ my $file_path = $downloader->fetch( to => $destination ) or confess($downloader->error);
Branch: 1 branch
BranchTrueFalseTotal
my $file_path = $downloader->fetch("to", $destination) or confess($downloader->error) 0 0 0
231 0 my $end_time = time();
232 0 say Dumper $downloader;
233 0 my $duration = sprintf("%.2fs", $end_time - $begin_time);
234 0 return $file_path;
235 }
236  
237 sub run {
238 1 ▶ my $begin_time = time();
Subroutine run: called CC=7 SCAR=21.8
Pod documented
239 1 my $time_stamp = _getTimeStamp();
240 1 my $self = undef;
241 1 ▶ if ( ref($_[0]) eq 'FASTX::ScriptHelper' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "FASTX::ScriptHelper") 1 0 1
242 1 $self = shift @_;
243 }
244 1 my %valid_attributes = (
245 candie => 1,
246 logall => 1,
247 );
248  
249  
250 1 my ($command, $options) = @_;
251 1 _validate_attributes(\%valid_attributes, $options, 'run');
252 1 ▶ if (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self) 1 0 1
253 1 $self->writelog("Shell> $command");
254 }
255  
256  
257 1 my $cmd = _runCmd($command);
258 1 ▶ if ($cmd->{exit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd->{"exit"}) 0 1 1
259 0 $cmd->{failed} = 1;
260 0 ▶ if (! $options->{candie}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"candie"}) 0 0 0
261 0 confess("Execution of an external command failed:\n$command");
262 }
263 }
264 1 my $end_time = time();
265 1 $cmd->{time} = $time_stamp;
266 1 $cmd->{duration} = sprintf("%.2fs", $end_time - $begin_time);
267 1 ▶ if (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self) 1 0 1
268 1 ▶ if ($options->{logall}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"logall"}) 0 1 1
269 0 $self->writelog(" +> Output: $cmd->{stdout}");
270 0 $self->writelog(" +> Messages: $cmd->{stderr}");
271 }
272 1 $self->writelog(" +> Elapsed time: $cmd->{duration}; Exit status: $cmd->{exit};");
273  
274 }
275  
276 1 return ($cmd);
277  
278  
279 }
280  
281  
282 sub cpu_count {
283 1 ▶ if ( $^O =~ m/linux/i ) {
Subroutine cpu_count: called CC=5 SCAR=24.5
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if ($^O =~ /linux/iu) { } 1 0 1
elsif ($^O =~ /darwin/iu) { } 0 0 0
284 1 my($num) = qx(grep -c ^processor /proc/cpuinfo);
285 1 ▶ return $1 if $num =~ m/^(\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $num =~ /^(\d+)/u 1 0 1
286 }
287 elsif ( $^O =~ m/darwin/i ) {
288 0 my($num) = qx(system_profiler SPHardwareDataType | grep Cores);
289 0 ▶ return $1 if $num =~ /.*Cores: (\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $num =~ /.*Cores: (\d+)/u 0 0 0
290 }
291 0 return 1;
292 }
293  
294 sub _getTimeStamp {
295  
296 6 ▶ my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
Subroutine _getTimeStamp: called CC=1 SCAR=0.0
297 6 my $timestamp = sprintf ( "%04d-%02d-%02d %02d:%02d:%02d",
298 $year+1900,$mon+1,$mday,$hour,$min,$sec);
299 6 return $timestamp;
300 }
301  
302  
303 sub _validate_attributes {
304 1 ▶ my ($hash_ref, $options, $title) = @_;
Subroutine _validate_attributes: called CC=3 SCAR=11.0
305  
306 1 for my $attr (sort keys %{ $options } ) {
307 1 ▶ confess "Invalid attribute '$attr' used calling routine '$title'\n" if (not defined ${ $hash_ref}{ $attr });
Branch: 1 branch
BranchTrueFalseTotal
unless defined do { $hash_ref }->{$attr} 0 1 1
308 }
309 1 return;
310 }
311 sub _runCmd {
312 1 ▶ if ( ref($_[0]) eq 'FASTX::ScriptHelper' ) {
Subroutine _runCmd: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "FASTX::ScriptHelper") 0 1 1
313 0 shift @_;
314 }
315 1 my @cmd = @_;
316 1 my $output;
317 1 $output->{cmd} = join(' ', @cmd);
318  
319 my ($stdout, $stderr, $exit) = capture {
320 1 ▶ system( @cmd );
Subroutine __ANON__: called CC=1 SCAR=0.0
321 1 };
322 1 chomp($stderr);
323 1 chomp($stdout);
324 1 $output->{stdout} = $stdout;
325 1 $output->{stderr} = $stderr;
326 1 $output->{exit} = $exit;
327  
328 1 return $output;
329 }
330  
331  
332  
333 2 1;
334  
335 __END__
« lib/FASTX/Reader.pm ↑ Summary lib/FASTX/Seq.pm »