File Coverage

blib/lib/Bio/Gonzales/Project/Functions.pm
Criterion Covered Total %
statement 49 73 67.1
branch 6 20 30.0
condition 4 17 23.5
subroutine 12 25 48.0
pod 3 12 25.0
total 74 147 50.3


line stmt bran cond sub pod time code
1             package Bio::Gonzales::Project::Functions;
2              
3 12     12   1394448 use warnings;
  12         156  
  12         444  
4 12     12   60 use strict;
  12         24  
  12         336  
5 12     12   48 use Carp;
  12         24  
  12         720  
6              
7 12     12   408 use 5.010;
  12         60  
8              
9 12     12   6768 use File::Spec::Functions qw/catfile/;
  12         11316  
  12         744  
10 12     12   5400 use Bio::Gonzales::Project;
  12         1620  
  12         756  
11 12     12   120 use Carp;
  12         24  
  12         756  
12 12     12   96 use Bio::Gonzales::Util::Cerial;
  12         48  
  12         1044  
13 12     12   16200 use Parallel::ForkManager;
  12         215772  
  12         540  
14              
15 12     12   108 use base 'Exporter';
  12         24  
  12         12420  
16             our ( @EXPORT, @EXPORT_OK, %EXPORT_TAGS );
17             our $VERSION = '0.083'; # VERSION
18              
19             @EXPORT
20             = qw(catfile nfi analysis_version path_to analysis_path gonzlog gonzconf iof gonzc gonzl gonz_iterate gonzsys analysis_name);
21             %EXPORT_TAGS = ();
22             @EXPORT_OK = qw();
23              
24             sub _bgp {
25 0     0   0 state $bgp = Bio::Gonzales::Project->new();
26             }
27              
28 0     0 0 0 sub analysis_version { _bgp->analysis_version(@_) }
29 0     0 1 0 sub path_to { _bgp->path_to(@_) }
30 0     0 1 0 sub nfi { _bgp->nfi(@_) }
31 0     0 1 0 sub iof { _bgp->conf(@_) }
32 0     0 0 0 sub gonzconf { _bgp->conf(@_) }
33 0     0 0 0 sub gonzc { _bgp->conf(@_) }
34 0     0 0 0 sub analysis_path { _bgp->analysis_path(@_) }
35 0     0 0 0 sub analysis_name { _bgp->analysis_name(@_) }
36              
37 0 0 0 0 0 0 sub gonzlog { confess "deprecated call syntax, use gonzlog->info" if ( @_ > 0 && $_[0] ); _bgp->log() }
  0         0  
38 0 0 0 0 0 0 sub gonzl { confess "deprecated call syntax, use gonzl->info" if ( @_ > 0 && $_[0] ); _bgp->log() }
  0         0  
39              
40             sub gonzsys {
41 0     0 0 0 _bgp->log->info( "(exec) > " . join( " ", @_ ) . " <" );
42 0 0       0 system(@_) == 0 or confess "system failed: $?";
43             }
44              
45             sub gonz_iterate {
46 12     12 0 2124 my ( $src, $code, $conf ) = @_;
47 12   50     132 $conf->{processes} //= 4;
48 12         36 my $data;
49 12         36 my $ref_type = ref($src);
50 12 50 33     108 if ( !$ref_type || ( $ref_type ne 'ARRAY' && $ref_type ne 'HASH' ) ) {
      33        
51 0         0 $data = jslurp($src);
52             } else {
53 12         36 $data = $src;
54             }
55              
56 12 50       48 if ( $conf->{test} ) {
57 0     0   0 $code = sub { say jfreeze( \@_ ); return };
  0         0  
  0         0  
58             }
59              
60 12         108 my $pm = Parallel::ForkManager->new( $conf->{processes} );
61              
62 12         56832 my @result_all;
63             $pm->run_on_finish(
64             sub {
65 51     51   13050632 my ( $pid, $exit_code, $ident, $exit_signal, $core_dump, $res ) = @_;
66              
67 51 50 33     1151 if ( defined($res) && @$res > 0 ) {
68 51         568 push @result_all, $res;
69             }
70             }
71 12         144 );
72              
73 12 50       192 if ( ref($data) eq 'ARRAY' ) {
    0          
74 12         72 for ( my $i = 0; $i < @$data; $i++ ) {
75 77 100       107782 $pm->start and next; # do the fork
76 11         79370 my $res = $code->( $i, $data->[$i] );
77 11         642 $pm->finish( 0, $res ); # do the exit in the child process
78             }
79 1         1691 $pm->wait_all_children;
80             } elsif ( ref($data) eq 'HASH' ) {
81 0         0 for my $k ( keys %$data ) {
82 0 0       0 $pm->start and next; # do the fork
83 0         0 my $res = $code->( $k, $data->{$k} );
84 0         0 $pm->finish( 0, $res ); # do the exit in the child process
85             }
86 0         0 $pm->wait_all_children;
87              
88             }
89 1         53 return \@result_all;
90             }
91             1;
92              
93             __END__
94              
95             =head1 NAME
96              
97             Bio::Gonzales::AV - analysis project utils
98              
99             =head1 SYNOPSIS
100              
101             use Bio::Gonzales::AV qw(catfile nfi $ANALYSIS_VERSION iof path_to analysis_path msg error debug);
102              
103             =head1 SUBROUTINES
104              
105             =over 4
106              
107             =item B<< msg(@stuff) >>
108              
109             say C<@stuff> to C<STDERR>.
110              
111             =item B<< path_to($filename) >>
112              
113             Locate the root of the project and prepend it to the C<$filename>.
114              
115             =item B<< iof() >>
116              
117             get access to the IO files config file. Use like
118              
119             my $protein_files = iof()->{protein_files}
120              
121             =item B<< nfi($filename) >>
122              
123             Prepend the current analysis version diretory to the filename.
124              
125              
126             =item B<< catfile($path, $file) >>
127              
128             make them whole again...
129              
130             =back
131              
132             =head1 SEE ALSO
133              
134             =head1 AUTHOR
135              
136             jw bargsten, C<< <joachim.bargsten at wur.nl> >>
137              
138             =cut