File Coverage

lib/Bio/Roary/External/CheckTools.pm
Criterion Covered Total %
statement 47 61 77.0
branch 12 24 50.0
condition 6 12 50.0
subroutine 9 9 100.0
pod 0 4 0.0
total 74 110 67.2


line stmt bran cond sub pod time code
1             package Bio::Roary::External::CheckTools;
2             $Bio::Roary::External::CheckTools::VERSION = '3.10.1';
3             # ABSTRACT: Check external executables are available and are the correct version
4              
5              
6 2     2   102970 use Moose;
  2         382082  
  2         13  
7 2     2   12089 use File::Spec;
  2         5  
  2         966  
8 2     2   664 use Log::Log4perl qw(:easy);
  2         31217  
  2         17  
9             has 'logger' => ( is => 'ro', lazy => 1, builder => '_build_logger' );
10              
11             sub _build_logger {
12 1     1   2 my ($self) = @_;
13 1         8 Log::Log4perl->easy_init($DEBUG);
14 1         2653 my $logger = get_logger();
15 1         87 return $logger;
16             }
17              
18             my $BIDEC = '(\d+\.\d+)'; # pattern of NN.NN for versions that can be compared
19              
20             my %tools = (
21             'parallel' => {
22             GETVER => "parallel --version | grep '^GNU parallel 2'",
23             REGEXP => qr/GNU parallel (\d+)/,
24             MINVER => "20130422",
25             NEEDED => 1,
26             },
27             'blastp' => {
28             GETVER => "blastp -version",
29             REGEXP => qr/blastp:\s+(\d+\.\d+\.\d+)/,
30             NEEDED => 1,
31             },
32             'makeblastdb' => {
33             GETVER => "makeblastdb -version",
34             REGEXP => qr/makeblastdb:\s+(\d+\.\d+\.\d+)/,
35             NEEDED => 1,
36             },
37             'mcl' => {
38             GETVER => "mcl --version | head -n 1",
39             REGEXP => qr/(\d+\-\d+)/,
40             NEEDED => 1,
41             },
42             'bedtools' => {
43             GETVER => "bedtools --version",
44             REGEXP => qr/bedtools v($BIDEC)/,
45             MINVER => "2.1",
46             NEEDED => 1,
47             },
48             'mafft' => {
49             GETVER => "mafft --version < /dev/null 2>&1",
50             REGEXP => qr/(\d+\.\d+) /,
51             NEEDED => 1,
52             },
53             'kraken' => {
54             GETVER => "kraken --version | head -n 1",
55             REGEXP => qr/Kraken version kraken-(\d+\.\d+\.\d+.*)/,
56             NEEDED => 0,
57             },
58             'kraken-report' => {
59             GETVER => "kraken-report --version | head -n 1",
60             REGEXP => qr/Kraken version kraken-(\d+\.\d+\.\d+.*)/,
61             NEEDED => 0,
62             },
63             'Rscript' => {
64             GETVER => "Rscript --version 2>&1 | head -n 1",
65             REGEXP => qr/R scripting front-end version ($BIDEC)/,
66             MINVER => "3",
67             NEEDED => 0,
68             },
69              
70             # prank version also performs an update check so cant use it
71             'prank' => { NEEDED => 0 },
72              
73             # now just the standard unix tools we need
74             'grep' => { NEEDED => 1 },
75             'sed' => { NEEDED => 1 },
76             'awk' => { NEEDED => 1 },
77            
78             );
79              
80             my %cdhit_tools = (
81             'cdhit' => {
82             GETVER => "cdhit -h | grep 'CD-HIT version'",
83             REGEXP => qr/version ($BIDEC) /,
84             MINVER => "4.6",
85             },
86             'cd-hit' => {
87             GETVER => "cd-hit -h | grep 'CD-HIT version'",
88             REGEXP => qr/version ($BIDEC) /,
89             MINVER => "4.6",
90             }
91             );
92              
93             my %fasttree_tools = (
94             'fasttree' => {
95             GETVER => "fasttree 2>&1 | head -n 1",
96             REGEXP => qr/Usage for FastTree version ($BIDEC)/,
97             },
98             'FastTree' => {
99             GETVER => "FastTree 2>&1 | head -n 1",
100             REGEXP => qr/Usage for FastTree version ($BIDEC)/,
101             }
102             );
103              
104             sub which_tool_exec {
105 2     2 0 4 my ( $self, $alt_tools ) = @_;
106 2         3 for my $toolname ( sort keys %{$alt_tools} ) {
  2         8  
107 4         7 my $fp = $self->find_exe($toolname);
108 4 50       9 return $toolname if $fp;
109             }
110 2         54 $self->logger->error( "Required tool missing. Can't find one of " . join( '/', keys %{$alt_tools} ) . " in your \$PATH." );
  2         10  
111 2         516 return undef;
112             }
113              
114             sub check_tool {
115 23     23 0 17682 my ( $self, $toolname ) = @_;
116 23         43 my $t = $tools{$toolname};
117 23         37 my $fp = $self->find_exe($toolname);
118 23 100 100     378 $self->logger->error("ERROR: Can't find required '$toolname' in your \$PATH") if !$fp and $t->{NEEDED};
119 23 100 100     3377 $self->logger->error("Optional tool '$toolname' not found in your \$PATH") if !$fp and !$t->{NEEDED};
120              
121 23 100       1263 if ($fp) {
122 6         22 $t->{HAVE} = $fp;
123 6         157 $self->logger->warn("Looking for '$toolname' - found $fp");
124 6 50       1534 if ( $t->{GETVER} ) {
125 0         0 my ($s) = qx($t->{GETVER});
126 0 0       0 if ( defined $s ) {
127 0         0 $s =~ $t->{REGEXP};
128 0 0       0 $t->{VERSION} = $1 if defined $1;
129 0         0 $self->logger->warn("Determined $toolname version is $t->{VERSION}");
130 0 0 0     0 if ( defined $t->{MINVER} and $t->{VERSION} < $t->{MINVER} ) {
131 0         0 $self->logger->error("Roary needs $toolname $t->{MINVER} or higher. Please upgrade and try again.");
132             }
133 0 0 0     0 if ( defined $t->{MAXVER} and $t->{VERSION} > $t->{MAXVER} ) {
134 0         0 $self->logger->error(
135             "Roary needs a version of $toolname between $t->{MINVER} and $t->{MAXVER}. Please downgrade and try again.");
136             }
137             }
138             else {
139 0         0 $self->logger->error( "Could not determine version of $toolname - please install version ", $t->{MINVER}, " or higher" )
140             ; # FIXME: or less <= MAXVER if given
141             }
142             }
143             }
144             }
145              
146             sub check_all_tools {
147 1     1 0 1035 my ($self) = @_;
148 1         6 $ENV{"GREP_OPTIONS"} = ''; # --colour => version grep fails (Issue #117)
149 1         10 for my $toolname ( sort keys %tools ) {
150 13         26 $self->check_tool($toolname);
151             }
152            
153 1         5 my $cdhit = $self->which_tool_exec( \%cdhit_tools );
154 1 50       4 if ($cdhit) {
155 0         0 $tools{$cdhit} = $cdhit_tools{$cdhit};
156 0         0 $self->check_tool($cdhit);
157             }
158              
159 1         4 my $fasttree = $self->which_tool_exec( \%fasttree_tools );
160 1 50       4 if ($fasttree) {
161 0         0 $tools{$fasttree} = $fasttree_tools{$fasttree};
162 0         0 $self->check_tool($fasttree);
163             }
164              
165 1         5 return $self;
166             }
167              
168             sub find_exe {
169 27     27 0 32 my ( $self, $bin ) = @_;
170 27         229 for my $dir ( File::Spec->path ) {
171 233         1059 my $exe = File::Spec->catfile( $dir, $bin );
172 233 100       1138 return $exe if -x $exe;
173             }
174 21         52 return;
175             }
176              
177 2     2   3768 no Moose;
  2         6  
  2         16  
178             __PACKAGE__->meta->make_immutable;
179              
180             1;
181              
182             __END__
183              
184             =pod
185              
186             =encoding UTF-8
187              
188             =head1 NAME
189              
190             Bio::Roary::External::CheckTools - Check external executables are available and are the correct version
191              
192             =head1 VERSION
193              
194             version 3.10.1
195              
196             =head1 SYNOPSIS
197             Functionality borrowed from PROKKA by Torsten Seemann.
198             Check external executables are available and are the correct version
199              
200             use Bio::Roary::External::CheckTools;
201            
202             my $obj = Bio::Roary::External::CheckTools->new();
203             $obj->check_all_tools;
204              
205             =head1 AUTHOR
206              
207             Andrew J. Page <ap13@sanger.ac.uk>
208              
209             =head1 COPYRIGHT AND LICENSE
210              
211             This software is Copyright (c) 2013 by Wellcome Trust Sanger Institute.
212              
213             This is free software, licensed under:
214              
215             The GNU General Public License, Version 3, June 2007
216              
217             =cut