Egaz/Command/blastn.pm

filter: Statementstmt 50.7% 35 / 69 Branchbran 16.6% 3 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.9% 10 / 11 Podpod 100% 6 / 6 totaltotal 51.9% 54 / 104 CC 13 SCAR 36.0
CC
13
Cov
44%
execute
90.0
validate_args
5.0
BEGIN
1.0
CRAP 36.7
« Egaz/Command/blastlink.pm ↑ Summary Egaz/Command/prepseq.pm »
1 package App::Egaz::Command::blastn;
2 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 ▶use App::Egaz -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use App::Egaz::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶sub abstract {
Subroutine abstract: called CC=1 SCAR=0.0
Pod documented
10 2 return 'blastn wrapper between two fasta files';
11 }
12  
13 4 ▶sub opt_spec {
Subroutine opt_spec: called CC=1 SCAR=0.0
Pod documented
14 return (
15 4 [ "outfile|o=s", "Output filename. [stdout] for screen", { default => "stdout" }, ],
16 [ "evalue=f", "expectation value (E) threshold", { default => 0.01 }, ],
17 [ "wordsize=i", "length of best perfect match", { default => 40 }, ],
18 [ "outfmt=s", "out format",
19 { default => "7 qseqid sseqid qstart qend sstart send qlen slen nident" },
20 ],
21 [ "tmp=s", "user defined tempdir", ],
22 [ "parallel|p=i", "number of threads", { default => 2 }, ],
23 [ "verbose|v", "verbose mode", ],
24 { show_defaults => 1, }
25 );
26 }
27  
28 4 ▶sub usage_desc {
Subroutine usage_desc: called CC=1 SCAR=0.0
Pod documented
29 4 return "egaz blastn [options] <infile> <genome.fa>";
30 }
31  
32 1 ▶sub description {
Subroutine description: called CC=1 SCAR=0.0
Pod documented
33 1 my $desc;
34 1 $desc .= ucfirst(abstract) . ".\n";
35 1 $desc .= <<'MARKDOWN';
36  
37 * <infile> should be a blocked fasta file (*.fas)
38 * <genome.fa> is a multi-sequence fasta file contains genome sequences
39 * `makeblastdb` and `blastn` should be in $PATH
40  
41 MARKDOWN
42  
43 1 return $desc;
44 }
45  
46 3 ▶sub validate_args {
Subroutine validate_args: called CC=5 SCAR=16.1
Pod documented
47 3 my ( $self, $opt, $args ) = @_;
48  
49 3 ▶ if ( @{$args} != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$args;} != 2) 2 1 3
50 2 my $message = "This command need two input files.\n\tIt found";
51 2 $message .= sprintf " [%s]", $_ for @{$args};
52 2 $message .= ".\n";
53 2 $self->usage_error($message);
54 }
55 1 for ( @{$args} ) {
56 1 ▶ if ( !Path::Tiny::path($_)->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Path::Tiny::path($_)->is_file) 1 0 1
57 1 $self->usage_error("The input file [$_] doesn't exist.");
58 }
59 }
60  
61 }
62  
63 0 ▶sub execute {
Subroutine execute: not called CC=9 SCAR=45.0
Pod documented
64 0 my ( $self, $opt, $args ) = @_;
65  
66 # absolute pathes as we will chdir to tempdir later
67 0 my @infiles;
68 0 for my $infile ( @{$args} ) {
69 0 push @infiles, Path::Tiny::path($infile)->absolute->stringify;
70 }
71  
72 0 ▶ if ( lc $opt->{outfile} ne "stdout" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $opt->{"outfile"} ne "stdout") 0 0 0
73 0 $opt->{outfile} = Path::Tiny::path( $opt->{outfile} )->absolute->stringify;
74 }
75  
76 # record cwd, we'll return there
77 0 my $cwd = Path::Tiny->cwd;
78 0 my $tempdir;
79 0 ▶ if ( $opt->{tmp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'tmp'}) { } 0 0 0
80 $tempdir = Path::Tiny->tempdir(
81 TEMPLATE => "blastn_XXXXXXXX",
82 DIR => $opt->{tmp},
83 0 );
84 }
85 else {
86 0 $tempdir = Path::Tiny->tempdir("blastn_XXXXXXXX");
87 }
88 0 chdir $tempdir;
89  
90 0 my $basename = $tempdir->basename();
91 0 $basename =~ s/\W+/_/g;
92  
93 { # makeblastdb
94 0 my $cmd = "makeblastdb -dbtype nucl -in $infiles[1] -out $basename -logfile $basename.log";
95 0 App::Egaz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } );
96 0 ▶ if ( !$tempdir->child("$basename.nsq")->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tempdir->child("$basename.nsq")->is_file) 0 0 0
97 0 Carp::croak "Failed: makeblastdb\n";
98 }
99 }
100  
101 { # blastn
102 0 my $cmd
103 = sprintf "blastn -task megablast"
104 . " -max_target_seqs 20 -culling_limit 20" # reduce size of reports
105 . " -dust no -soft_masking false" # disable dust and soft masking
106 . " -evalue $opt->{evalue} -word_size $opt->{wordsize}"
107 . " -outfmt '$opt->{outfmt}'"
108 . " -num_threads $opt->{parallel} -db $basename -query $infiles[0]"
109 . " -out $basename.blast";
110  
111 0 my $blastn_usage = `blastn -h`;
112 0 ▶ if ( $blastn_usage =~ /\-max_hsps int/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($blastn_usage =~ /\-max_hsps int/) { } 0 0 0
elsif ($blastn_usage =~ /\-max_hsps_per_subject int/) { } 0 0 0
113 0 $cmd .= " -max_hsps 10"; # Nucleotide-Nucleotide BLAST 2.6.0+
114 }
115 elsif ( $blastn_usage =~ /\-max_hsps_per_subject int/ ) {
116 0 $cmd .= " -max_hsps_per_subject 10"; # Nucleotide-Nucleotide BLAST 2.2.28+
117 }
118  
119 0 App::Egaz::Common::exec_cmd( $cmd, { verbose => $opt->{verbose}, } );
120 0 ▶ if ( !$tempdir->child("$basename.blast")->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tempdir->child("$basename.blast")->is_file) 0 0 0
121 0 Carp::croak "Failed: blastn\n";
122 }
123 }
124  
125 { # outputs
126 0 ▶ if ( lc $opt->{outfile} ne "stdout" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $opt->{'outfile'} ne 'stdout') { } 0 0 0
127 0 Path::Tiny::path("$basename.blast")->copy( $opt->{outfile} );
128 }
129 else {
130 0 print Path::Tiny::path("$basename.blast")->slurp;
131 }
132 }
133  
134 0 chdir $cwd;
135 }
136  
137 19 1;
« Egaz/Command/blastlink.pm ↑ Summary Egaz/Command/prepseq.pm »