lib/Bio/Tradis/AddTagsToSeq.pm

filter: Statementstmt 46.5% 47 / 101 Branchbran 16.6% 3 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 83.3% 10 / 12 Podpod 0.0% 0 / 1 totaltotal 43.8% 60 / 137 CC 11 SCAR 30.7
CC
11
Cov
41%
add_tags_to_seq
49.6
_number_of_lines_in_bam_file
6.0
_output_switch
2.1
CRAP 21.5
« lib/Bio/Tradis/CommandLine/TradisAnalysis.pm ↑ Summary blib/lib/Bio/Tradis/CommandLine/AddTags.pm »
1 package Bio::Tradis::AddTagsToSeq;
2 1 $Bio::Tradis::AddTagsToSeq::VERSION = '1.4.5';
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 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 has 'verbose' => ( is => 'rw', isa => 'Bool', default => 0 );
14 1 has 'samtools_exec' => ( is => 'rw', isa => 'Str', default => 'samtools' );
15 1 has 'bamfile' => ( is => 'rw', isa => 'Str', required => 1 );
16 has 'outfile' => (
17 is => 'rw',
18 isa => 'Str',
19 required => 0,
20 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 0 my ($self) = @_;
22 0 my $o = $self->bamfile;
23 0 $o =~ s/\.bam/\.tr\.bam/;
24 0 $o =~ s/\.cram/\.tr\.cram/;
25 0 return $o;
26 }
27 1 );
28  
29 1 has '_file_extension' => ( is => 'rw', isa => 'Str', lazy => 1, builder => '_build__file_extension' );
30 1 ▶has 'extension_to_output_switch' => ( is => 'rw', isa => 'HashRef', default => sub{ {cram => '-C', bam => '-b'} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
31  
32 sub _build__file_extension
33 1 ▶{
Subroutine _build__file_extension: called CC=1 SCAR=0.0
34 1 my ($self) = @_;
35 1 my($filename, $dirs, $suffix) = fileparse($self->bamfile,qr/[^.]*/);
36 1 return lc($suffix);
37 }
38  
39 1 ▶sub add_tags_to_seq {
Subroutine add_tags_to_seq: called CC=9 SCAR=39.0
Pod undocumented
40 1 my ($self) = @_;
41  
42 #set up BAM parser
43 1 my $filename = $self->bamfile;
44 1 my $outfile = $self->outfile;
45  
46 #open temp file in SAM format and output headers from current BAM to it
47 1 my @now = localtime();
48 1 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 1 my $tmp_sam = "tmp.$timeStamp.sam";
52 1 ▶ print STDERR "Reading ".uc($self->_file_extension)." header\n" if($self->verbose);
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 1 1
53 1 system($self->samtools_exec." view -H $filename > $tmp_sam");
54 1 open( TMPFILE, ">>$tmp_sam" );
55  
56 #open BAM file
57 1 ▶ print STDERR "Reading ".uc($self->_file_extension)." file\n" if($self->verbose);
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 1 1
58 1 my $pars = Bio::Tradis::Parser::Bam->new( file => $filename, samtools_exec => $self->samtools_exec );
59 1 my $read_info = $pars->read_info;
60  
61 1 ▶ while ( $pars->next_read ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pars->next_read) 0 0 0
62 0 my $read_info = $pars->read_info;
63 0 my $line = ${$read_info}{READ};
64  
65 # get tags, seq, qual and cigar str
66 0 my $trtag = ${$read_info}{tr};
67 0 my $tqtag = ${$read_info}{tq};
68  
69 0 my $seq_tagged = ${$read_info}{SEQ};
70 0 my $qual_tagged = ${$read_info}{QUAL};
71 0 my $cigar_update = ${$read_info}{CIGAR};
72  
73 #Check if seq is mapped & rev complement. If so, reformat.
74 0 my $mapped = $pars->is_mapped;
75 0 my $rev = $pars->is_reverse;
76 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
77  
78 # The transposon is not reverse complimented but the genomic read is
79  
80 # reverse the genomic quality scores.
81 0 $qual_tagged = reverse($qual_tagged);
82  
83 # Add the transposon quality score on the beginning
84 0 $qual_tagged = $tqtag . $qual_tagged;
85  
86 # Reverse the whole quality string.
87 0 $qual_tagged = reverse($qual_tagged);
88  
89 # Reverse the genomic sequence
90 0 my $genomic_seq_obj =
91 Bio::Seq->new( -seq => $seq_tagged, -alphabet => 'dna' );
92 0 my $reversed_genomic_seq_obj = $genomic_seq_obj->revcom;
93  
94 # Add on the tag sequence
95 0 $seq_tagged = $trtag . $reversed_genomic_seq_obj->seq;
96  
97 # Reverse the tag+genomic sequence to get it back into the correct orentation.
98 0 my $genomic_and_tag_seq_obj =
99 Bio::Seq->new( -seq => $seq_tagged, -alphabet => 'dna' );
100 0 $seq_tagged = $genomic_and_tag_seq_obj->revcom->seq;
101  
102 }
103 else {
104 #print STDERR "$line\n" if(!defined($tqtag));
105 0 $seq_tagged = $trtag . $seq_tagged;
106 0 $qual_tagged = $tqtag . $qual_tagged;
107 }
108  
109 0 ▶ if ($mapped) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mapped) { } 0 0 0
110 0 my $cigar = length($seq_tagged);
111 0 $cigar_update = $cigar . 'M';
112 }
113 else {
114 0 $cigar_update = '*';
115 }
116  
117 # replace updated fields and print to TMPFILE
118 0 my @cols = split( " ", $line );
119 0 $cols[5] = $cigar_update;
120 0 $cols[9] = $seq_tagged;
121 0 $cols[10] = $qual_tagged;
122  
123 0 print TMPFILE join( "\t", @cols ) . "\n";
124 }
125 0 $pars->close_file_handle;
126 0 close TMPFILE;
127  
128 #convert tmp.sam to bam
129 0 ▶ print STDERR "Convert SAM to ".uc($self->_file_extension)."\n" if($self->verbose);
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
130
131
132 0 system($self->samtools_exec." view -h -S ".$self->_output_switch." -o $outfile $tmp_sam");
133  
134 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
135 $self->_number_of_lines_in_bam_file($filename) )
136 {
137 0 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 0 unlink("$tmp_sam");
143 0 return 1;
144 }
145  
146 sub _output_switch
147 1 ▶{
Subroutine _output_switch: called CC=2 SCAR=7.6
148 1 my ( $self ) = @_;
149 1 ▶ if(defined($self->extension_to_output_switch->{$self->_file_extension}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->extension_to_output_switch->{$self->_file_extension}) { } 1 0 1
150 {
151 1 return $self->extension_to_output_switch->{$self->_file_extension};
152 }
153 else
154 {
155 0 return '';
156 }
157 }
158  
159 0 ▶sub _number_of_lines_in_bam_file {
Subroutine _number_of_lines_in_bam_file: not called CC=2 SCAR=17.9
160 0 my ( $self, $filename ) = @_;
161 0 ▶ open( my $fh, '-|', $self->samtools_exec." view $filename | wc -l" )
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", $self->samtools_exec . " view $filename | wc -l" or die q[Couldn't open file :] . $filename 0 0 0
162 or die "Couldn't open file :" . $filename;
163 0 my $number_of_lines_in_file = <$fh>;
164 0 $number_of_lines_in_file =~ s!\W!!gi;
165 0 return $number_of_lines_in_file;
166 }
167  
168 1 __PACKAGE__->meta->make_immutable;
169 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
170 1 1;
171  
172 __END__
« lib/Bio/Tradis/CommandLine/TradisAnalysis.pm ↑ Summary blib/lib/Bio/Tradis/CommandLine/AddTags.pm »