lib/Bio/Tradis/AddTagsToSeq.pm

filter: Statementstmt 32.9% 26 / 79 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 71.4% 5 / 7 Podpod 0.0% 0 / 1 totaltotal 29.8% 31 / 104 CC 8 SCAR 29.9
CC
8
Cov
28%
add_tags_to_seq
37.2
_number_of_lines_in_bam_file
6.0
__ANON__
2.0
CRAP 19.9
« blib/lib/Bio/Tradis/CommandLine/TradisAnalysis.pm ↑ Summary blib/lib/Bio/Tradis/CommandLine/FilterFastqTags.pm »
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 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Bio::Seq;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Bio::Tradis::Parser::Bam;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 has 'bamfile' => ( is => 'rw', isa => 'Str', required => 1 );
11 has 'outfile' => (
12 is => 'rw',
13 isa => 'Str',
14 required => 0,
15 default => sub {
16 0 ▶ my ($self) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
17 0 my $o = $self->bamfile;
18 0 $o =~ s/\.bam/\.tr\.bam/;
19 0 return $o;
20 }
21 1 );
22  
23 sub add_tags_to_seq {
24 1 ▶ my ($self) = @_;
Subroutine add_tags_to_seq: called CC=7 SCAR=36.2
Pod undocumented
25  
26 #set up BAM parser
27 1 my $filename = $self->bamfile;
28 1 my $outfile = $self->outfile;
29  
30 #open temp file in SAM format and output headers from current BAM to it
31 1 print STDERR "Reading BAM header\n";
32 1 system("samtools view -H $filename > tmp.sam");
33 1 open( TMPFILE, '>>tmp.sam' );
34  
35 #open BAM file
36 1 print STDERR "Reading BAM file\n";
37 1 my $pars = Bio::Tradis::Parser::Bam->new( file => $filename );
38 1 my $read_info = $pars->read_info;
39  
40 1 ▶ while ( $pars->next_read ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pars->next_read) 0 0 0
41 0 my $read_info = $pars->read_info;
42 0 my $line = ${$read_info}{READ};
43  
44 # get tags, seq, qual and cigar str
45 0 my $trtag = ${$read_info}{tr};
46 0 my $tqtag = ${$read_info}{tq};
47  
48 0 my $seq_tagged = ${$read_info}{SEQ};
49 0 my $qual_tagged = ${$read_info}{QUAL};
50 0 my $cigar_update = ${$read_info}{CIGAR};
51  
52 #Check if seq is mapped & rev complement. If so, reformat.
53 0 my $mapped = $pars->is_mapped;
54 0 my $rev = $pars->is_reverse;
55 0 ▶ if ( $mapped && $rev ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mapped and $rev) { } 0 0 0
Condition0%0/3
$mapped and $rev
!ll&&!rl&&r
000
MC/DC0%0/2
$mapped and $rev
$mapped $rev
Truth table0%0/3
A and B
A= $mappedB= $rev
ABresult
0X0
100
111
56  
57 # The transposon is not reverse complimented but the genomic read is
58  
59 # reverse the genomic quality scores.
60 0 $qual_tagged = reverse($qual_tagged);
61  
62 # Add the transposon quality score on the beginning
63 0 $qual_tagged = $tqtag . $qual_tagged;
64  
65 # Reverse the whole quality string.
66 0 $qual_tagged = reverse($qual_tagged);
67  
68 # Reverse the genomic sequence
69 0 my $genomic_seq_obj =
70 Bio::Seq->new( -seq => $seq_tagged, -alphabet => 'dna' );
71 0 my $reversed_genomic_seq_obj = $genomic_seq_obj->revcom;
72  
73 # Add on the tag sequence
74 0 $seq_tagged = $trtag . $reversed_genomic_seq_obj->seq;
75  
76 # Reverse the tag+genomic sequence to get it back into the correct orentation.
77 0 my $genomic_and_tag_seq_obj =
78 Bio::Seq->new( -seq => $seq_tagged, -alphabet => 'dna' );
79 0 $seq_tagged = $genomic_and_tag_seq_obj->revcom->seq;
80  
81 }
82 else {
83 0 ▶ print STDERR "$line\n" if(!defined($tqtag));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tqtag 0 0 0
84 0 $seq_tagged = $trtag . $seq_tagged;
85 0 $qual_tagged = $tqtag . $qual_tagged;
86 }
87  
88 0 ▶ if ($mapped) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mapped) { } 0 0 0
89 0 my $cigar = length($seq_tagged);
90 0 $cigar_update = $cigar . 'M';
91 }
92 else {
93 0 $cigar_update = '*';
94 }
95  
96 # replace updated fields and print to TMPFILE
97 0 my @cols = split( " ", $line );
98 0 $cols[5] = $cigar_update;
99 0 $cols[9] = $seq_tagged;
100 0 $cols[10] = $qual_tagged;
101  
102 0 print TMPFILE join( "\t", @cols ) . "\n";
103 }
104 0 $pars->close_file_handle;
105 0 close TMPFILE;
106  
107 #convert tmp.sam to bam
108 0 print STDERR "Convert SAM to BAM\n";
109 0 system("samtools view -S -b -o $outfile tmp.sam");
110  
111 0 ▶ if ( $self->_number_of_lines_in_bam_file($outfile) !=
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_number_of_lines_in_bam_file($outfile) != $self->_number_of_lines_in_bam_file($filename)) 0 0 0
112 $self->_number_of_lines_in_bam_file($filename) )
113 {
114 0 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 0 unlink("tmp.sam");
120 0 return 1;
121 }
122  
123 sub _number_of_lines_in_bam_file {
124 0 ▶ my ( $self, $filename ) = @_;
Subroutine _number_of_lines_in_bam_file: not called CC=2 SCAR=17.9
125 0 ▶ open( my $fh, '-|', "samtools view $filename | wc -l" )
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", "samtools view $filename | wc -l" or die q[Couldn't open file :] . $filename 0 0 0
126 or die "Couldn't open file :" . $filename;
127 0 my $number_of_lines_in_file = <$fh>;
128 0 $number_of_lines_in_file =~ s!\W!!gi;
129 0 return $number_of_lines_in_file;
130 }
131  
132 1 __PACKAGE__->meta->make_immutable;
133 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
134 1 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
« blib/lib/Bio/Tradis/CommandLine/TradisAnalysis.pm ↑ Summary blib/lib/Bio/Tradis/CommandLine/FilterFastqTags.pm »