blib/lib/Bio/Tradis/RunTradis.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Bio/Tradis/Analysis/InsertSite.pm ↑ Summary
1 package Bio::Tradis::RunTradis;
2  
3 # ABSTRACT: Perform all steps required for a tradis analysis
4  
5  
6 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Bio::Tradis::FilterTags;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Bio::Tradis::RemoveTags;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Bio::Tradis::Map;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Bio::Tradis::TradisPlot;
Subroutine BEGIN: called CC=1 SCAR=2.6
12  
13 has 'fastqfile' => ( is => 'rw', isa => 'Str', required => 1 );
14 has '_unzipped_fastq' =>
15 ( is => 'rw', isa => 'Str', lazy => 1, builder => '_build__unzipped_fastq' );
16 has 'tag' => ( is => 'ro', isa => 'Str', required => 1 );
17 has 'tagdirection' =>
18 ( is => 'ro', isa => 'Str', required => 1, default => '3' );
19 has 'mismatch' => ( is => 'rw', isa => 'Int', required => 1, default => 0 );
20 has 'mapping_score' =>
21 ( is => 'ro', isa => 'Int', required => 1, default => 30 );
22 has 'reference' => ( is => 'rw', isa => 'Str', required => 1 );
23 has 'outfile' => (
24 is => 'rw',
25 isa => 'Str',
26 required => 0,
27 default => sub {
28 my ($self) = @_;
29 my @dirs = split( '/', $self->fastqfile );
30 my $o = pop(@dirs);
31 $o =~ s/fastq/out/;
32 return $o;
33 }
34 );
35 has '_destination' => (
36 is => 'rw',
37 isa => 'Str',
38 required => 0,
39 lazy => 1,
40 builder => '_build__destination'
41 );
42 has '_stats_handle' => ( is => 'ro', isa => 'FileHandle', required => 1 );
43 has '_sequence_info' => (
44 is => 'rw',
45 isa => 'HashRef',
46 required => 0,
47 lazy => 1,
48 builder => '_build__sequence_info'
49 );
50 has '_current_directory' => (
51 is => 'rw',
52 isa => 'Str',
53 required => 0,
54 lazy => 1,
55 builder => '_build__current_directory'
56 );
57  
58 sub _is_gz {
59 my ($self) = @_;
60 my $fq = $self->fastqfile;
61  
62 if ( $fq =~ /\.gz/ ) {
63 return 1;
64 }
65 else {
66 return 0;
67 }
68 }
69  
70 sub _build__unzipped_fastq {
71 my ($self) = @_;
72 my $fq = $self->fastqfile;
73 my $destination_directory = $self->_destination;
74  
75 if ( $self->_is_gz ) {
76 $fq =~ /([^\/]+)$/;
77 my $newfq = $1;
78 $newfq =~ s/\.gz//;
79 if ( !-e "$destination_directory/$newfq" ) {
80 `gunzip -c $fq > $destination_directory/$newfq`;
81 }
82 return "$destination_directory/$newfq";
83 }
84 else {
85 return $fq;
86 }
87 }
88  
89 sub _build__stats_handle {
90 my ($self) = @_;
91 my $outfile = $self->outfile;
92  
93 open( my $stats, ">", "$outfile.stats" );
94 return $stats;
95 }
96  
97 sub _build__sequence_info {
98 my ($self) = @_;
99 my $destination_directory = $self->_destination;
100 open( GREP,
101 "grep \@SQ $destination_directory/mapped.sam | awk '{print \$2, \$3}' |"
102 );
103 my %sns = ();
104 while ( my $sn = <GREP> ) {
105 chomp($sn);
106 $sn =~ /SN:(\S+)\s+LN:(\d+)/;
107 $sns{$1} = $2;
108 }
109 return \%sns;
110 }
111  
112 sub _build__destination {
113 my $tmp_dir = File::Temp->newdir( CLEANUP => 0 );
114 return $tmp_dir->dirname;
115 }
116  
117 sub _build__current_directory {
118 my ($self) = @_;
119 my $fq = $self->fastqfile;
120  
121 my @dirs = split( '/', $fq );
122 pop(@dirs);
123 return join( '/', @dirs );
124 }
125  
126 sub run_tradis {
127 my ($self) = @_;
128 my $destination_directory = $self->_destination;
129 my $fq = $self->fastqfile;
130  
131 print STDERR "::::::::::::::::::\n$fq\n::::::::::::::::::\n\n";
132  
133 # Step 1: Filter tags that match user input tag
134 print STDERR "..........Step 1: Filter tags that match user input tag\n";
135 $self->_filter;
136  
137 # Step 2: Remove the tag from the sequence and quality strings
138 print STDERR
139 "..........Step 2: Remove the tag from the sequence and quality strings\n";
140 $self->_remove;
141  
142 # Step 3: Map file to reference
143 print STDERR "..........Step 3: Map file to reference\n";
144 $self->_map;
145  
146 # Step 4: Convert output from SAM to BAM, sort and index
147 print STDERR
148 "..........Step 3.5: Convert output from SAM to BAM and sort\n";
149 $self->_sam2bam;
150 $self->_sort_bam;
151  
152 # Step 5: Generate plot
153 print STDERR "..........Step 4: Generate plot\n";
154 $self->_make_plot;
155  
156 # Step 6: Generate statistics
157 print STDERR "..........Step 5: Generate statistics\n";
158 $self->_stats;
159  
160 # Step 7: Move files to current directory
161 print STDERR "..........Step 6: Move files to current directory\n";
162 my $outfile = $self->outfile;
163 system("mv $destination_directory/$outfile* \.");
164 system("mv $destination_directory/mapped.sort.bam \./$outfile.mapped.bam");
165 system(
166 "mv $destination_directory/mapped.sort.bam.bai \./$outfile.mapped.bam.bai"
167 );
168  
169 # Clean up
170 print STDERR "..........Clean up\n";
171  
172 unlink("$destination_directory/filter.fastq");
173 unlink("$destination_directory/tags_removed.fastq");
174 unlink("$destination_directory/mapped.sam");
175 unlink("$destination_directory/ref.index.sma");
176 unlink("$destination_directory/ref.index.smi");
177 unlink("$destination_directory/mapped.bam");
178 unlink("$destination_directory/tmp.plot");
179 unlink($self->_unzipped_fastq) if ($self->_is_gz);
180  
181 File::Temp::cleanup();
182  
183 return 1;
184 }
185  
186 sub _filter {
187 my ($self) = @_;
188 my $destination_directory = $self->_destination;
189 my $fqfile = $self->_unzipped_fastq;
190 my $tag = $self->tag;
191 my $mm = $self->mismatch;
192  
193 my $filter = Bio::Tradis::FilterTags->new(
194 fastqfile => $fqfile,
195 tag => $tag,
196 mismatch => $mm,
197 outfile => "$destination_directory/filter.fastq"
198 )->filter_tags;
199 }
200  
201 sub _remove {
202 my ($self) = @_;
203 my $destination_directory = $self->_destination;
204 my $tag = $self->tag;
205 my $mm = $self->mismatch;
206  
207 my $rm_tags = Bio::Tradis::RemoveTags->new(
208 fastqfile => "$destination_directory/filter.fastq",
209 tag => $tag,
210 mismatch => $mm,
211 outfile => "$destination_directory/tags_removed.fastq"
212 )->remove_tags;
213 }
214  
215 sub _map {
216 my ($self) = @_;
217 my $destination_directory = $self->_destination;
218  
219 my $ref = $self->reference;
220  
221 my $mapping = Bio::Tradis::Map->new(
222 fastqfile => "$destination_directory/tags_removed.fastq",
223 reference => "$ref",
224 refname => "$destination_directory/ref.index",
225 outfile => "$destination_directory/mapped.sam"
226 );
227 $mapping->index_ref;
228 $mapping->do_mapping;
229 }
230  
231 sub _sam2bam {
232 my ($self) = @_;
233 my $destination_directory = $self->_destination;
234  
235 system(
236 "samtools view -b -o $destination_directory/mapped.bam -S $destination_directory/mapped.sam"
237 );
238 return 1;
239 }
240  
241 sub _sort_bam {
242 my ($self) = @_;
243 my $destination_directory = $self->_destination;
244  
245 system(
246 "samtools sort $destination_directory/mapped.bam $destination_directory/mapped.sort"
247 );
248 system("samtools index $destination_directory/mapped.sort.bam");
249 return 1;
250 }
251  
252 sub _make_plot {
253 my ($self) = @_;
254 my $destination_directory = $self->_destination;
255 my $ref = $self->reference;
256 my $outfile = $self->outfile;
257 my $tr_d = $self->tagdirection;
258
259 my $plot = Bio::Tradis::TradisPlot->new(
260 mappedfile => "$destination_directory/mapped.sort.bam",
261 mapping_score => $self->mapping_score,
262 outfile => "$destination_directory/$outfile"
263 )->plot;
264  
265 # if tag direction is 5, reverse plot columns
266 if ( $self->tagdirection eq '5' ) {
267 $self->_reverse_plots;
268 }
269 return 1;
270 }
271  
272 sub _reverse_plots {
273 my ($self) = @_;
274 my $destination_directory = $self->_destination;
275 my $outfile = $self->outfile;
276 my @seqnames = keys %{ $self->_sequence_info };
277  
278 foreach my $sn (@seqnames) {
279 my $plotname = $self->_plotname($sn);
280 system("gunzip -c $plotname > $destination_directory/tmp.plot");
281 system(
282 "awk '{ t = \$1; \$1 = \$2; \$2 = t; print; }' $destination_directory/tmp.plot > rv_plot"
283 );
284 system("gzip -c rv_plot > $plotname");
285 }
286 unlink("$destination_directory/tmp.plot");
287 unlink("rv_plot");
288 }
289  
290 sub _stats {
291 my ($self) = @_;
292 my $outfile = $self->outfile;
293 my $destination_directory = $self->_destination;
294 my $fq = $self->_unzipped_fastq;
295 my $seq_info = $self->_sequence_info;
296  
297 #write header to stats file
298 $self->_write_stats_header;
299  
300 # Add file name and number of reads in it
301 my @fql = split( "/", $fq );
302 my $stats = "$fql[-1],";
303 my $total_reads = `wc $fq | awk '{print \$1/4}'`;
304 chomp($total_reads);
305 $stats .= "$total_reads,";
306  
307 # Matching reads
308 my $matching =
309 `wc $destination_directory/filter.fastq | awk '{print \$1/4}'`;
310 chomp($matching);
311 $stats .= "$matching,";
312 $stats .= ( $matching / $total_reads ) * 100 . ",";
313  
314 # Mapped reads
315 my $mapped = $self->_number_of_mapped_reads;
316 $stats .= "$mapped,";
317 $stats .= ( $mapped / $matching ) * 100 . ",";
318  
319 # Unique insertion sites
320 my ( $total_uis, $total_seq_len );
321 foreach my $si ( keys %{$seq_info} ) {
322 my $plotname = $self->_plotname($si);
323 system(
324 "gunzip -c $destination_directory/$plotname > $destination_directory/tmp.plot"
325 );
326 my $uis =
327 `grep -v "^0" $destination_directory/tmp.plot | sort | uniq | wc | awk '{ print \$1 }'`;
328 chomp($uis);
329 $total_uis += $uis;
330 $stats .= "$uis,";
331 my $seqlen = ${$seq_info}{$si};
332 $total_seq_len += $seqlen;
333 my $uis_per_seqlen = $uis / $seqlen;
334 chomp($uis_per_seqlen);
335 $stats .= "$uis_per_seqlen,";
336 }
337 $stats .= "$total_uis,";
338 my $t_uis_p_l = $total_uis / $total_seq_len;
339 $stats .= "$t_uis_p_l\n";
340 print { $self->_stats_handle } $stats;
341 }
342  
343 sub _write_stats_header {
344 my ($self) = @_;
345 my @seqnames = keys %{ $self->_sequence_info };
346 my @fields = (
347 "File",
348 "Total Reads",
349 "Reads Matched",
350 "\% Matched",
351 "Reads Mapped",
352 "\% Mapped"
353 );
354 print { $self->_stats_handle } join( ",", @fields ) . ",";
355 foreach my $sn (@seqnames) {
356 print { $self->_stats_handle } "Unique Insertion Sites : $sn,";
357 print { $self->_stats_handle } "UIS/Seq Len : $sn,";
358 }
359 print { $self->_stats_handle } "Total Unique Insertion Sites,";
360 print { $self->_stats_handle } "Total UIS/Total Seq Len\n";
361 }
362  
363 sub _plotname {
364 my ( $self, $seq_name ) = @_;
365 my $outfile = $self->outfile;
366  
367 my $plotfile_name = "$outfile.$seq_name.insert_site_plot.gz";
368 return $plotfile_name;
369 }
370  
371 sub _number_of_mapped_reads {
372 my ($self) = @_;
373 my $destination_directory = $self->_destination;
374  
375 my $pars =
376 Bio::Tradis::Parser::Bam->new(
377 file => "$destination_directory/mapped.bam" );
378 my $c = 0;
379 while ( $pars->next_read ) {
380 if ( $pars->is_mapped ) {
381 $c++;
382 }
383 }
384 return $c;
385 }
386  
387 __PACKAGE__->meta->make_immutable;
388 no Moose;
389 1;
390  
391 __END__
392  
393 =pod
394  
395 =head1 NAME
396  
397 Bio::Tradis::RunTradis - Perform all steps required for a tradis analysis
398  
399 =head1 VERSION
400  
401 version 1.132190
402  
403 =head1 SYNOPSIS
404  
405 Takes a fastq file with tags already attached, filters the tags matching user input,
406 removes the tags, maps to a reference (.fa) and generates insertion site plots for use in
407 Artemis (or other genome browsers), mapped BAM files for each lane and a statistical summary of the analysis.
408  
409 use Bio::Tradis::RunTradis;
410
411 my $pipeline = Bio::Tradis::RunTradis->new(
412 fastqfile => 'abc',
413 reference => 'abc',
414 tag => 'abc',
415 tagdirection => '5'|'3'
416 );
417 $pipeline->run_tradis();
418  
419 =head1 PARAMETERS
420  
421 =head2 Required
422  
423 =over
424 =item * C<fastqfile> - file containing a list of fastqs (gzipped or raw) to run the
425 complete analysis on. This includes all (including
426 intermediary format conversion and sorting) steps starting from
427 filtering.
428 =item * C<tag> - TraDIS tag to filter and then remove
429 =item * C<reference> - path to/name of reference genome in fasta format (.fa)
430 =back
431  
432 =head2 Optional
433  
434 =over
435 =item * C<mismatch> - number of mismatches to allow when filtering/removing the tag. Default = 0
436 =item * C<tagdirection> - direction of the tag, 5' or 3'. Default = 3
437 =item * C<mapping_score> - cutoff value for mapping score when creating insertion site plots. Default = 30
438 =back
439  
440 =head1 METHODS
441  
442 C<run_tradis> - run complete analysis with given parameters
443  
444 =head1 AUTHOR
445  
446 Carla Cummins <cc21@sanger.ac.uk>
447  
448 =head1 COPYRIGHT AND LICENSE
449  
450 This software is Copyright (c) 2013 by Wellcome Trust Sanger Institute.
451  
452 This is free software, licensed under:
453  
454 The GNU General Public License, Version 3, June 2007
455  
456 =cut
« blib/lib/Bio/Tradis/Analysis/InsertSite.pm ↑ Summary