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

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