blib/lib/Bio/Tradis/CommandLine/TradisAnalysis.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 132 CC 12 SCAR 34.1
CC
12
Cov
0%
BUILD
72.0
run
12.0
_tidy_stats
12.0
CRAP 30.2
1 package Bio::Tradis::CommandLine::TradisAnalysis;
2  
3 # ABSTRACT: Perform full tradis analysis
4  
5  
6 use Moose;
7 use Getopt::Long qw(GetOptionsFromArray);
8 use Cwd 'abs_path';
9 use Bio::Tradis::RunTradis;
10  
11 has 'args' => ( is => 'ro', isa => 'ArrayRef', required => 1 );
12 has 'script_name' => ( is => 'ro', isa => 'Str', required => 1 );
13 has 'fastqfile' => ( is => 'rw', isa => 'Str', required => 0 );
14 has 'tag' => ( is => 'rw', isa => 'Str', required => 0 );
15 has 'mismatch' => ( is => 'rw', isa => 'Int', required => 0, default => 0 );
16 has 'tagdirection' =>
17 ( is => 'rw', isa => 'Str', required => 0, default => '3' );
18 has 'reference' => ( is => 'rw', isa => 'Str', required => 0 );
19 has 'help' => ( is => 'rw', isa => 'Bool', required => 0 );
20 has 'mapping_score' =>
21 ( is => 'ro', isa => 'Int', required => 0, default => 30 );
22 has '_stats_handle' => (
23 is => 'ro',
24 isa => 'FileHandle',
25 required => 0,
26 lazy => 1,
27 builder => '_build__stats_handle'
28 );
29  
30 sub BUILD {
31 my ($self) = @_;
32  
33 my ( $fastqfile, $tag, $td, $mismatch, $ref, $map_score, $help );
34  
35 GetOptionsFromArray(
36 $self->args,
37 'f|fastqfile=s' => \$fastqfile,
38 't|tag=s' => \$tag,
39 'td|tagdirection=i' => \$td,
40 'mm|mismatch=i' => \$mismatch,
41 'r|reference=s' => \$ref,
42 'm|mapping_score=i' => \$map_score,
43 'h|help' => \$help
44 );
45  
46 $self->fastqfile( abs_path($fastqfile) ) if ( defined($fastqfile) );
47 $self->tag( uc($tag) ) if ( defined($tag) );
48 $self->tagdirection($td) if ( defined($td) );
49 $self->mismatch($mismatch) if ( defined($mismatch) );
50 $self->reference( abs_path($ref) ) if ( defined($ref) );
51 $self->mapping_score($map_score) if ( defined($map_score) );
52 $self->help($help) if ( defined($help) );
53  
54 # print usage text if required parameters are not present
55 ($fastqfile && $tag && $ref) or die $self->usage_text;
56 }
57  
58 sub run {
59 my ($self) = @_;
60  
61 if ( defined( $self->help ) ) {
62  
63 #if ( scalar( @{ $self->args } ) == 0 ) {
64 $self->usage_text;
65 }
66  
67 #parse list of files and run pipeline for each one
68 open( FILES, "<", $self->fastqfile );
69 my @filelist = <FILES>;
70 my $file_dir = $self->get_file_dir;
71 foreach my $f (@filelist) {
72 chomp($f);
73 if( substr($f, 0, 1) ne "/"){
74 $f = "$file_dir/$f";
75 }
76 my $analysis = Bio::Tradis::RunTradis->new(
77 fastqfile => $f,
78 tag => $self->tag,
79 tagdirection => $self->tagdirection,
80 mismatch => $self->mismatch,
81 reference => $self->reference,
82 mapping_score => $self->mapping_score,
83 _stats_handle => $self->_stats_handle
84 );
85 $analysis->run_tradis;
86 }
87 $self->_tidy_stats;
88 close(FILES);
89 }
90  
91 sub _build__stats_handle {
92 my ($self) = @_;
93 my $filelist = $self->fastqfile;
94 my $dir = $self->get_file_dir;
95 $filelist =~ s/$dir\///;
96 $filelist =~ s/[^\.]+$/stats/;
97 open( my $stats, ">", "$filelist" );
98 return $stats;
99 }
100  
101 sub _tidy_stats {
102 my ($self) = @_;
103 my $filelist = $self->fastqfile;
104 my $dir = $self->get_file_dir;
105 $filelist =~ s/$dir\///;
106 $filelist =~ s/[^\.]+$/stats/;
107 open(STATS, '<', $filelist);
108 open(TMP, '>', 'tmp.stats');
109  
110 my $header = 0;
111 while(my $line = <STATS>){
112 if($line =~ /^File/){
113 if($header == 0){
114 print TMP "$line";
115 $header = 1;
116 }
117 }
118 else{
119 print TMP "$line";
120 }
121 }
122 close(TMP);
123 close(STATS);
124 system("mv tmp.stats $filelist");
125 }
126  
127 sub get_file_dir {
128 my ($self) = @_;
129 my $fq = $self->fastqfile;
130  
131 my @dirs = split( '/', $fq );
132 pop(@dirs);
133 return join( '/', @dirs );
134 }
135  
136 sub usage_text {
137 print <<USAGE;
138 Run a TraDIS analysis. This involves:
139 1: filtering the data with tags matching that passed via -t option
140 2: removing the tags from the sequences
141 3: mapping
142 4: creating an insertion site plot
143 5: creating a stats summary
144  
145 Usage: run_tradis [options]
146  
147 Options:
148 -f : list of fastq files with tradis tags attached
149 -t : tag to search for
150 -td : tag direction - 3 or 5 (optional. default = 3)
151 -mm : number of mismatches allowed when matching tag (optional. default = 0)
152 -r : reference genome in fasta format (.fa)
153 -m : mapping quality cutoff score (optional. default = 30)
154  
155 USAGE
156 exit;
157 }
158  
159 __PACKAGE__->meta->make_immutable;
160 no Moose;
161 1;
162  
163 __END__
164  
165 =pod
166  
167 =head1 NAME
168  
169 Bio::Tradis::CommandLine::TradisAnalysis - Perform full tradis analysis
170  
171 =head1 VERSION
172  
173 version 1.132190
174  
175 =head1 SYNOPSIS
176  
177 Takes a fastq, reference and a tag and generates insertion
178 site plots for use in Artemis
179  
180 =head1 AUTHOR
181  
182 Carla Cummins <cc21@sanger.ac.uk>
183  
184 =head1 COPYRIGHT AND LICENSE
185  
186 This software is Copyright (c) 2013 by Wellcome Trust Sanger Institute.
187  
188 This is free software, licensed under:
189  
190 The GNU General Public License, Version 3, June 2007
191  
192 =cut