blib/lib/Bio/Tradis/AddTagsToSeq.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 135 CC 8 SCAR 32.1
CC
8
Cov
0%
add_tags_to_seq
56.0
_output_switch
6.0
_build__file_extension
2.0
CRAP 24.7
1 package Bio::Tradis::AddTagsToSeq;
2 $Bio::Tradis::AddTagsToSeq::VERSION = '1.4.1';
3 # ABSTRACT: Takes a BAM file and creates a new BAM with tr and tq tags added to the sequence and quality strings.
4  
5  
6 use Moose;
7 use Bio::Seq;
8 use Bio::Tradis::Parser::Bam;
9 use File::Basename;
10  
11 no warnings qw(uninitialized);
12  
13 has 'verbose' => ( is => 'rw', isa => 'Bool', default => 0 );
14 has 'samtools_exec' => ( is => 'rw', isa => 'Str', default => 'samtools' );
15 has 'bamfile' => ( is => 'rw', isa => 'Str', required => 1 );
16 has 'outfile' => (
17 is => 'rw',
18 isa => 'Str',
19 required => 0,
20 default => sub {
21 my ($self) = @_;
22 my $o = $self->bamfile;
23 $o =~ s/\.bam/\.tr\.bam/;
24 $o =~ s/\.cram/\.tr\.cram/;
25 return $o;
26 }
27 );
28  
29 has '_file_extension' => ( is => 'rw', isa => 'Str', lazy => 1, builder => '_build__file_extension' );
30 has 'extension_to_output_switch' => ( is => 'rw', isa => 'HashRef', default => sub{ {cram => '-C', bam => '-b'} } );
31  
32 sub _build__file_extension
33 {
34 my ($self) = @_;
35 my($filename, $dirs, $suffix) = fileparse($self->bamfile,qr/[^.]*/);
36 return lc($suffix);
37 }
38  
39 sub add_tags_to_seq {
40 my ($self) = @_;
41  
42 #set up BAM parser
43 my $filename = $self->bamfile;
44 my $outfile = $self->outfile;
45  
46 #open temp file in SAM format and output headers from current BAM to it
47 my @now = localtime();
48 my $timeStamp = sprintf("%04d%02d%02d%02d%02d%02d",
49 $now[5]+1900, $now[4]+1, $now[3],
50 $now[2], $now[1], $now[0]);
51 my $tmp_sam = "tmp.$timeStamp.sam";
52 print STDERR "Reading ".uc($self->_file_extension)." header\n" if($self->verbose);
53 system($self->samtools_exec." view -H $filename > $tmp_sam");
54 open( TMPFILE, ">>$tmp_sam" );
55  
56 #open BAM file
57 print STDERR "Reading ".uc($self->_file_extension)." file\n" if($self->verbose);
58 my $pars = Bio::Tradis::Parser::Bam->new( file => $filename, samtools_exec => $self->samtools_exec );
59 my $read_info = $pars->read_info;
60  
61 while ( $pars->next_read ) {
62 my $read_info = $pars->read_info;
63 my $line = ${$read_info}{READ};
64  
65 # get tags, seq, qual and cigar str
66 my $trtag = ${$read_info}{tr};
67 my $tqtag = ${$read_info}{tq};
68  
69 my $seq_tagged = ${$read_info}{SEQ};
70 my $qual_tagged = ${$read_info}{QUAL};
71 my $cigar_update = ${$read_info}{CIGAR};
72  
73 #Check if seq is mapped & rev complement. If so, reformat.
74 my $mapped = $pars->is_mapped;
75 my $rev = $pars->is_reverse;
76 if ( $mapped && $rev ) {
77  
78 # The transposon is not reverse complimented but the genomic read is
79  
80 # reverse the genomic quality scores.
81 $qual_tagged = reverse($qual_tagged);
82  
83 # Add the transposon quality score on the beginning
84 $qual_tagged = $tqtag . $qual_tagged;
85  
86 # Reverse the whole quality string.
87 $qual_tagged = reverse($qual_tagged);
88  
89 # Reverse the genomic sequence
90 my $genomic_seq_obj =
91 Bio::Seq->new( -seq => $seq_tagged, -alphabet => 'dna' );
92 my $reversed_genomic_seq_obj = $genomic_seq_obj->revcom;
93  
94 # Add on the tag sequence
95 $seq_tagged = $trtag . $reversed_genomic_seq_obj->seq;
96  
97 # Reverse the tag+genomic sequence to get it back into the correct orentation.
98 my $genomic_and_tag_seq_obj =
99 Bio::Seq->new( -seq => $seq_tagged, -alphabet => 'dna' );
100 $seq_tagged = $genomic_and_tag_seq_obj->revcom->seq;
101  
102 }
103 else {
104 #print STDERR "$line\n" if(!defined($tqtag));
105 $seq_tagged = $trtag . $seq_tagged;
106 $qual_tagged = $tqtag . $qual_tagged;
107 }
108  
109 if ($mapped) {
110 my $cigar = length($seq_tagged);
111 $cigar_update = $cigar . 'M';
112 }
113 else {
114 $cigar_update = '*';
115 }
116  
117 # replace updated fields and print to TMPFILE
118 my @cols = split( " ", $line );
119 $cols[5] = $cigar_update;
120 $cols[9] = $seq_tagged;
121 $cols[10] = $qual_tagged;
122  
123 print TMPFILE join( "\t", @cols ) . "\n";
124 }
125 $pars->close_file_handle;
126 close TMPFILE;
127  
128 #convert tmp.sam to bam
129 print STDERR "Convert SAM to ".uc($self->_file_extension)."\n" if($self->verbose);
130
131
132 system($self->samtools_exec." view -h -S ".$self->_output_switch." -o $outfile $tmp_sam");
133  
134 if ( $self->_number_of_lines_in_bam_file($outfile) !=
135 $self->_number_of_lines_in_bam_file($filename) )
136 {
137 die
138 "The number of lines in the input and output files don't match, so something's gone wrong\n";
139 }
140  
141 #remove tmp file
142 unlink("$tmp_sam");
143 return 1;
144 }
145  
146 sub _output_switch
147 {
148 my ( $self ) = @_;
149 if(defined($self->extension_to_output_switch->{$self->_file_extension}))
150 {
151 return $self->extension_to_output_switch->{$self->_file_extension};
152 }
153 else
154 {
155 return '';
156 }
157 }
158  
159 sub _number_of_lines_in_bam_file {
160 my ( $self, $filename ) = @_;
161 open( my $fh, '-|', $self->samtools_exec." view $filename | wc -l" )
162 or die "Couldn't open file :" . $filename;
163 my $number_of_lines_in_file = <$fh>;
164 $number_of_lines_in_file =~ s!\W!!gi;
165 return $number_of_lines_in_file;
166 }
167  
168 __PACKAGE__->meta->make_immutable;
169 no Moose;
170 1;
171  
172 __END__
173  
174 =pod
175  
176 =encoding UTF-8
177  
178 =head1 NAME
179  
180 Bio::Tradis::AddTagsToSeq - Takes a BAM file and creates a new BAM with tr and tq tags added to the sequence and quality strings.
181  
182 =head1 VERSION
183  
184 version 1.4.1
185  
186 =head1 SYNOPSIS
187  
188 Bio::Tradis::AddTagsToSeq parses BAM files, adds given tags to the start of the sequence and creates temporary SAM file,
189 which is then converted to BAM
190  
191 use Bio::Tradis::AddTagsToSeq;
192
193 my $pipeline = Bio::Tradis::AddTagsToSeq->new(bamfile => 'abc');
194 $pipeline->add_tags_to_seq();
195  
196 =head1 NAME
197  
198 Bio::Tradis::AddTagsToSeq
199  
200 =head1 PARAMETERS
201  
202 =head2 Required
203  
204 C<bamfile> - path to/name of file containing reads and tags
205  
206 =head2 Optional
207  
208 C<outfile> - name to assign to output BAM. Defaults to C<file.tr.bam> for an input file named C<file.bam>
209  
210 =head1 METHODS
211  
212 C<add_tags_to_seq> - add TraDIS tags to reads. For unmapped reads, the tag
213 is added to the start of the read sequence and quality
214 strings. For reads where the flag indicates that it is
215 mapped and reverse complemented, the reverse complemented
216 tags are added to the end of the read strings.
217 This is because many conversion tools (e.g. picard) takes
218 the read orientation into account and will re-reverse the
219 mapped/rev comp reads during conversion, leaving all tags
220 in the correct orientation at the start of the sequences
221 in the resulting FastQ file.
222  
223 =head1 AUTHOR
224  
225 Carla Cummins <path-help@sanger.ac.uk>
226  
227 =head1 COPYRIGHT AND LICENSE
228  
229 This software is Copyright (c) 2013 by Wellcome Trust Sanger Institute.
230  
231 This is free software, licensed under:
232  
233 The GNU General Public License, Version 3, June 2007
234  
235 =cut