lib/Bio/Tradis/CommandLine/TradisAnalysis.pm

filter: Statementstmt 73.4% 97 / 132 Branchbran 40.3% 21 / 52 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 70.5% 12 / 17 Podpod 0.0% 0 / 4 totaltotal 60.0% 134 / 223 CC 33 SCAR 34.4
CC
33
Cov
62%
BUILD
59.5
_tidy_stats
20.0
run
14.2
CRAP 31.3
« blib/lib/Bio/Tradis/Parser/Cigar.pm ↑ Summary blib/lib/Bio/Tradis/AddTagsToSeq.pm »
1 package Bio::Tradis::CommandLine::TradisAnalysis;
2 1 $Bio::Tradis::CommandLine::TradisAnalysis::VERSION = '1.4.1';
3 # ABSTRACT: Perform full tradis analysis
4  
5  
6 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Getopt::Long qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Cwd qw(abs_path cwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Bio::Tradis::RunTradis;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 has 'args' => ( is => 'ro', isa => 'ArrayRef', required => 1 );
13 1 has 'script_name' => ( is => 'ro', isa => 'Str', required => 1 );
14 1 has 'fastqfile' => ( is => 'rw', isa => 'Str', required => 0 );
15 1 has 'tag' => ( is => 'rw', isa => 'Str', required => 0 );
16 1 has 'mismatch' => ( is => 'rw', isa => 'Int', required => 0, default => 0 );
17 1 has 'tagdirection' =>
18 ( is => 'rw', isa => 'Str', required => 0, default => '3' );
19 1 has 'reference' => ( is => 'rw', isa => 'Str', required => 0 );
20 1 has 'help' => ( is => 'rw', isa => 'Bool', required => 0 );
21 1 has 'mapping_score' =>
22 ( is => 'rw', isa => 'Int', required => 0, default => 30 );
23 1 has 'smalt_k' => ( is => 'rw', isa => 'Maybe[Int]', required => 0 );
24 1 has 'smalt_s' => ( is => 'rw', isa => 'Maybe[Int]', required => 0 );
25 1 has 'smalt_y' => ( is => 'rw', isa => 'Maybe[Num]', required => 0, default => 0.96 );
26 1 has 'smalt_r' => ( is => 'rw', isa => 'Maybe[Int]', required => 0, default => -1 );
27 1 has 'smalt_n' => ( is => 'rw', isa => 'Maybe[Int]', required => 0, default => 1 );
28 1 has 'essentiality' => ( is => 'rw', isa => 'Bool', required => 0, default => 0);
29  
30 1 has 'verbose' => ( is => 'rw', isa => 'Bool', default => 0 );
31 1 has 'samtools_exec' => ( is => 'rw', isa => 'Str', default => 'samtools' );
32  
33 1 has '_output_directory' => (
34 is => 'rw',
35 isa => 'Str',
36 required => 0,
37 lazy => 1,
38 builder => '_build__output_directory'
39 );
40  
41 1 has '_stats_handle' => (
42 is => 'ro',
43 isa => 'FileHandle',
44 required => 0,
45 lazy => 1,
46 builder => '_build__stats_handle'
47 );
48  
49 sub BUILD {
50 1 ▶ my ($self) = @_;
Subroutine BUILD: called CC=22 SCAR=40.9
Pod undocumented
51  
52 my (
53 1 $fastqfile, $tag, $td, $mismatch, $ref,$smalt_n, $essentiality,
54 $map_score, $smalt_k, $smalt_s, $smalt_y, $smalt_r, $help, $verbose,$samtools_exec
55 );
56  
57 1 GetOptionsFromArray(
58 $self->args,
59 'f|fastqfile=s' => \$fastqfile,
60 't|tag=s' => \$tag,
61 'td|tagdirection=i' => \$td,
62 'mm|mismatch=i' => \$mismatch,
63 'r|reference=s' => \$ref,
64 'm|mapping_score=i' => \$map_score,
65 'sk|smalt_k=i' => \$smalt_k,
66 'ss|smalt_s=i' => \$smalt_s,
67 'sy|smalt_y=f' => \$smalt_y,
68 'n|smalt_n=i' => \$smalt_n,
69 'sr|smalt_r=i' => \$smalt_r,
70 'v|verbose' => \$verbose,
71 'samtools_exec=s' => \$samtools_exec,
72 'e|essentiality' => \$essentiality,
73 'h|help' => \$help
74 );
75  
76 #Essentiality analysis requires different defaults.
77 1 ▶ if(!defined($smalt_r) && !defined($map_score) && defined($essentiality))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $smalt_r || defined $map_score and defined $essentiality) 0 1 1
Condition33%2/6
not defined $smalt_r || defined $map_score and defined $essentiality
!ll&&!rl&&r
010
defined $smalt_r || defined $map_score
l!l&&r!l&&!r
001
MC/DC0%0/3
not defined $smalt_r || defined $map_score and defined $essentiality
defined $smalt_r defined $map_score defined $essentiality
Truth table25%1/4
not(A || B) and C
A= defined $smalt_rB= defined $map_scoreC= defined $essentiality
ABCresult
1XX0
01X0
0000
0011
78 {
79 0 ▶ $self->essentiality($essentiality) if ( defined($essentiality));
Branch: 1 branch
BranchTrueFalseTotal
if defined $essentiality 0 0 0
80 0 $smalt_r = 0;
81 0 $map_score = 0;
82 }
83  
84 1 ▶ $self->fastqfile( abs_path($fastqfile) ) if ( defined($fastqfile) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $fastqfile 1 0 1
85 1 ▶ $self->tag( uc($tag) ) if ( defined($tag) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $tag 1 0 1
86 1 ▶ $self->tagdirection($td) if ( defined($td) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $td 0 1 1
87 1 ▶ $self->mismatch($mismatch) if ( defined($mismatch) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $mismatch 0 1 1
88 1 ▶ $self->reference( abs_path($ref) ) if ( defined($ref) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $ref 1 0 1
89 1 ▶ $self->mapping_score($map_score) if ( defined($map_score) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $map_score 0 1 1
90 1 ▶ $self->smalt_k($smalt_k) if ( defined($smalt_k) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $smalt_k 0 1 1
91 1 ▶ $self->smalt_s($smalt_s) if ( defined($smalt_s) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $smalt_s 0 1 1
92 1 ▶ $self->smalt_y($smalt_y) if ( defined($smalt_y) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $smalt_y 0 1 1
93 1 ▶ $self->smalt_r($smalt_r) if ( defined($smalt_r) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $smalt_r 0 1 1
94 1 ▶ $self->smalt_n($smalt_n) if ( defined($smalt_n) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $smalt_n 0 1 1
95 1 ▶ $self->help($help) if ( defined($help) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $help 0 1 1
96 1 ▶ $self->verbose($verbose) if ( defined($verbose));
Branch: 1 branch
BranchTrueFalseTotal
if defined $verbose 0 1 1
97 1 ▶ $self->samtools_exec($samtools_exec) if ( defined($samtools_exec) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $samtools_exec 0 1 1
98
99 # print usage text if required parameters are not present
100 1 ▶ ( $fastqfile && $tag && $ref ) or die $self->usage_text;
Branch: 1 branch
BranchTrueFalseTotal
$fastqfile and $tag and $ref or die $self->usage_text 0 1 1
Condition33%2/6
$fastqfile and $tag and $ref
!ll&&!rl&&r
001
$fastqfile and $tag
!ll&&!rl&&r
001
MC/DC0%0/3
$fastqfile and $tag and $ref
$fastqfile $tag $ref
Truth table25%1/4
A and B and C
A= $fastqfileB= $tagC= $ref
ABCresult
0XX0
10X0
1100
1111
101 }
102  
103 sub run {
104 1 ▶ my ($self) = @_;
Subroutine run: called CC=9 SCAR=26.5
Pod undocumented
105  
106 1 ▶ if ( defined( $self->help ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->help) 0 1 1
107  
108 #if ( scalar( @{ $self->args } ) == 0 ) {
109 0 $self->usage_text;
110 }
111  
112 #parse list of files and run pipeline for each one if they all exist
113 1 my $fq = $self->fastqfile;
114 1 ▶ open( FILES, "<", $fq ) or die "Cannot find $fq";
Branch: 1 branch
BranchTrueFalseTotal
open FILES, "<", $fq or die "Cannot find $fq" 0 1 1
115 1 my @filelist = <FILES>;
116 1 my $file_dir = $self->get_file_dir;
117  
118 #check files exist before running
119 1 my $line_no = 0;
120 1 my $full_path;
121 1 foreach my $f1 (@filelist) {
122 2 chomp($f1);
123 2 $line_no++;
124 2 ▶ if ( $f1 =~ /^\// ) { $full_path = $f1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($f1 =~ m[^/]) { } 0 2 2
125 2 else { $full_path = "$file_dir/$f1"; }
126 2 ▶ unless ( -e $full_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $full_path) 0 2 2
127 0 die "File $full_path does not exist ($fq, line $line_no)\n";
128 }
129 }
130  
131 #if all files exist, continue with analysis
132 1 my $at_least_one_good_fastq = 0;
133 1 foreach my $f2 (@filelist) {
134 1 chomp($f2);
135 1 ▶ if ( $f2 =~ /^\// ) { $full_path = $f2; }
Branch: 1 branch
BranchTrueFalseTotal
if ($f2 =~ m[^/]) { } 0 1 1
136 1 else { $full_path = "$file_dir/$f2"; }
137 1 my $analysis = Bio::Tradis::RunTradis->new(
138 fastqfile => $full_path,
139 tag => $self->tag,
140 tagdirection => $self->tagdirection,
141 mismatch => $self->mismatch,
142 reference => $self->reference,
143 mapping_score => $self->mapping_score,
144 output_directory => $self->_output_directory,
145 _stats_handle => $self->_stats_handle,
146 smalt_k => $self->smalt_k,
147 smalt_s => $self->smalt_s,
148 smalt_y => $self->smalt_y,
149 smalt_r => $self->smalt_r,
150 smalt_n => $self->smalt_n,
151 verbose => $self->verbose,
152 samtools_exec => $self->samtools_exec
153 );
154 try {
155 1 ▶ $analysis->run_tradis;
Subroutine __ANON__: called CC=1 SCAR=1.2
156 0 $at_least_one_good_fastq = 1;
157 }
158 catch {
159 0 ▶ my $tag = $self->tag;
Subroutine __ANON__: not called CC=1 SCAR=6.9
160 0 warn "There was a problem filtering '$full_path' by '$tag'; it looks like the tag was not found in any read\n";
161 1 };
162 }
163 0 ▶ if ( ! $at_least_one_good_fastq ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($at_least_one_good_fastq) 0 0 0
164 0 Bio::Tradis::Exception::TagFilterError->throw( error => "None of the input files contained the specified tag. Please check that your inputs are valid fastq files and that at least one read in one of them starts with the specified tag\n" );
165 }
166  
167 0 $self->_tidy_stats;
168 0 close(FILES);
169  
170 #$self->_combine_plots;
171 }
172  
173 sub _build__output_directory {
174 0 ▶ return cwd();
Subroutine _build__output_directory: not called CC=1 SCAR=6.9
175 }
176  
177 sub _stats_filename {
178 1 ▶ my ($self) = @_;
Subroutine _stats_filename: called CC=1 SCAR=0.0
179 1 my $dir = $self->get_file_dir;
180 1 my $output_directory = $self->_output_directory;
181 1 my $stats_filename = $self->fastqfile;
182 1 $stats_filename =~ s/$dir\///;
183 1 $stats_filename =~ s/[^\.]+$/stats/;
184 1 return "$output_directory/$stats_filename";
185 }
186  
187 sub _build__stats_handle {
188 1 ▶ my ($self) = @_;
Subroutine _build__stats_handle: called CC=1 SCAR=0.0
189 1 my $stats_filename = $self->_stats_filename();
190 1 open( my $stats, ">", $stats_filename );
191 1 return $stats;
192 }
193  
194 sub _tidy_stats {
195 0 ▶ my ($self) = @_;
Subroutine _tidy_stats: not called CC=4 SCAR=30.0
196 0 my $output_directory = $self->_output_directory;
197 0 my $stats_filename = $self->_stats_filename();
198 0 open( STATS, '<', $stats_filename );
199 0 open( TMP, '>', "$output_directory/tmp.stats" );
200  
201 0 my $header = 0;
202 0 ▶ while ( my $line = <STATS> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline STATS)) 0 0 0
203 0 ▶ if ( $line =~ /^File/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^File/) { } 0 0 0
204 0 ▶ if ( $header == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header == 0) 0 0 0
205 0 print TMP "$line";
206 0 $header = 1;
207 }
208 }
209 else {
210 0 print TMP "$line";
211 }
212 }
213 0 close(TMP);
214 0 close(STATS);
215 0 system("mv $output_directory/tmp.stats $stats_filename");
216 }
217  
218 sub _combine_plots {
219 0 ▶ my ($self) = @_;
Subroutine _combine_plots: not called CC=1 SCAR=6.9
220 0 my $filelist = $self->fastqfile;
221  
222 0 return 1;
223 }
224  
225 sub get_file_dir {
226 2 ▶ my ($self) = @_;
Subroutine get_file_dir: called CC=1 SCAR=0.0
Pod undocumented
227 2 my $fq = $self->fastqfile;
228  
229 2 my @dirs = split( '/', $fq );
230 2 pop(@dirs);
231 2 return join( '/', @dirs );
232 }
233  
234 sub usage_text {
235 0 ▶ print <<USAGE;
Subroutine usage_text: not called CC=1 SCAR=6.9
Pod undocumented
236 Run a TraDIS analysis. This involves:
237 1: filtering the data with tags matching that passed via -t option
238 2: removing the tags from the sequences
239 3: mapping
240 4: creating an insertion site plot
241 5: creating a stats summary
242  
243 Usage: bacteria_tradis [options]
244  
245 Options:
246 -f : text file listing fastq files with tradis tags attached
247 -t : tag to search for
248 -r : reference genome in fasta format (.fa)
249 -td : tag direction - 3 or 5 (optional. default = 3)
250 -mm : number of mismatches allowed when matching tag (optional. default = 0)
251 -m : mapping quality cutoff score (optional. default = 30)
252 --smalt_k : custom k-mer value for SMALT mapping (optional)
253 --smalt_s : custom step size for SMALT mapping (optional)
254 --smalt_y : custom y parameter for SMALT (optional. default = 0.96)
255 --smalt_r : custom r parameter for SMALT (optional. default = -1)
256 -n : number of threads to use for SMALT and samtools sort (optional. default = 1)
257 -e : set defaults for essentiality experiment (smalt_r = 0, -m = 0)
258 -v : verbose debugging output
259 USAGE
260 0 exit;
261 }
262  
263 1 __PACKAGE__->meta->make_immutable;
264 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
265 1 1;
266  
267 __END__
268  
269 =pod
270  
271 =encoding UTF-8
272  
273 =head1 NAME
274  
275 Bio::Tradis::CommandLine::TradisAnalysis - Perform full tradis analysis
276  
277 =head1 VERSION
278  
279 version 1.4.1
280  
281 =head1 SYNOPSIS
282  
283 Takes a fastq, reference and a tag and generates insertion
284 site plots for use in Artemis
285  
286 =head1 AUTHOR
287  
288 Carla Cummins <path-help@sanger.ac.uk>
289  
290 =head1 COPYRIGHT AND LICENSE
291  
292 This software is Copyright (c) 2013 by Wellcome Trust Sanger Institute.
293  
294 This is free software, licensed under:
295  
296 The GNU General Public License, Version 3, June 2007
297  
298 =cut
« blib/lib/Bio/Tradis/Parser/Cigar.pm ↑ Summary blib/lib/Bio/Tradis/AddTagsToSeq.pm »