WGS2NCBI/Seq.pm

filter: Statementstmt 97.9% 96 / 98 Branchbran 72.2% 26 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 12 totaltotal 85.0% 137 / 161 CC 23 SCAR 11.0
CC
23
Cov
91%
read_fasta
5.0
seq
4.0
get_non_missing_index
4.0
CRAP 3.0
« WGS2NCBI/Logger.pm ↑ Summary WGS2NCBI/StrandedFeature.pm »
1 package Bio::WGS2NCBI::Seq;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Bio::WGS2NCBI::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # this sequence class implements the needed bits of the Bio::Seq interface while holding
7 # the raw sequence data as a scalar ref
8  
9 5 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
10 5 my $class = shift;
11 5 my %args = @_;
12 5 my $self = bless {}, $class;
13 5 for ( qw(id seq desc) ) {
14 15 ▶ $self->$_( $args{"-$_"} ) if $args{"-$_"};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"-$_"} 14 1 15
15 }
16 5 return $self;
17 }
18  
19 14 ▶sub seq {
Subroutine seq: called CC=4 SCAR=13.9
Pod undocumented
20 14 my $self = shift;
21 14 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 8 14
22 6 my $seq = shift;
23 6 ▶ if ( ref $seq ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $seq) { } 4 2 6
24 4 $self->{'_seq'} = $seq;
25 }
26 else {
27 2 $self->{'_seq'} = \$seq;
28 }
29 }
30 14 ▶ if ( ref $self->{'_seq'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'_seq'}) { } 14 0 14
31 14 return ${ $self->{'_seq'} };
32 }
33 else {
34 0 return $self->{'_seq'};
35 }
36 }
37  
38 3 ▶sub length {
Subroutine length: called CC=2 SCAR=7.1
Pod undocumented
39 3 my $self = shift;
40 3 ▶ return $self->{'_seq'} ? length ${ $self->{'_seq'} } : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_seq'} ? : 3 0 3
41 }
42  
43 2 ▶sub get_non_missing_index {
Subroutine get_non_missing_index: called CC=4 SCAR=13.9
Pod undocumented
44 2 my ( $seq, $reverse ) = @_;
45
46 # make a 1-based index array
47 2 my @i = 1 .. $seq->length;
48
49 # turn into decrementing if we read from the end
50 2 ▶ @i = reverse @i if $reverse;
Branch: 1 branch
BranchTrueFalseTotal
if $reverse 1 1 2
51
52 # iterate over indices, return 0-based index of first non-missing residue
53 2 for my $i ( @i ) {
54 2 ▶ return $i - 1 if uc( $seq->subseq($i,$i) ) ne 'N';
Branch: 1 branch
BranchTrueFalseTotal
if uc $seq->subseq($i, $i) ne "N" 2 0 2
55 }
56 }
57  
58 11 ▶sub id {
Subroutine id: called CC=2 SCAR=6.9
Pod undocumented
59 11 my $self = shift;
60 11 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5 6 11
61 5 $self->{'_id'} = shift;
62 }
63 11 return $self->{'_id'};
64 }
65  
66 9 ▶sub desc {
Subroutine desc: called CC=2 SCAR=6.9
Pod undocumented
67 9 my $self = shift;
68 9 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 5 9
69 4 $self->{'_desc'} = shift;
70 }
71 9 return $self->{'_desc'};
72 }
73  
74 2 ▶sub subseq {
Subroutine subseq: called CC=2 SCAR=7.0
Pod undocumented
75 2 my ( $self, $start, $stop ) = @_;
76 2 my $subseq;
77 2 ▶ if ( $self->{'_seq'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_seq"}) 2 0 2
78 2 $subseq = substr ${ $self->{'_seq'} }, $start - 1, $stop - $start + 1;
79 }
80 2 return $subseq;
81 }
82  
83 1 ▶sub trunc {
Subroutine trunc: called CC=2 SCAR=7.0
Pod undocumented
84 1 my ( $self, $start, $stop ) = @_;
85 1 my $substring;
86 1 ▶ if ( $self->{'_seq'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_seq"}) 1 0 1
87 1 $substring = substr ${ $self->{'_seq'} }, $start - 1, $stop - $start + 1;
88 }
89 1 return ref($self)->new(
90 '-id' => $self->id,
91 '-desc' => $self->desc,
92 '-seq' => \$substring,
93 );
94 }
95  
96 1 ▶sub revcom {
Subroutine revcom: called CC=2 SCAR=7.0
Pod undocumented
97 1 my $self = shift;
98 1 my $revcom;
99 1 ▶ if ( $self->{'_seq'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_seq"}) 1 0 1
100 1 $revcom = reverse ${ $self->{'_seq'} };
101 1 $revcom =~ tr/ACGTacgt/TGCAtgca/;
102 }
103 1 return ref($self)->new(
104 '-id' => $self->id,
105 '-desc' => $self->desc,
106 '-seq' => \$revcom,
107 );
108 }
109  
110 1 ▶sub mask {
Subroutine mask: called CC=3 SCAR=11.0
Pod undocumented
111 1 my ( $seq, @masks ) = @_;
112 1 my $raw = $seq->seq;
113 1 my $id = $seq->id;
114 1 for my $m ( @masks ) {
115 1 ▶ if ( $m =~ /(\d+)\.\.(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m =~ /(\d+)\.\.(\d+)/) 1 0 1
116 1 my ( $start, $stop ) = ( $1, $2 );
117 1 my $index = $start - 1;
118 1 my $length = $stop - $index;
119 1 substr $raw, $index, $length, ( 'N' x $length );
120 1 INFO "Masked ${id}:${start}-${stop}";
121 }
122 }
123 1 $seq->seq( \$raw );
124 }
125  
126 1 ▶sub write_fasta {
Subroutine write_fasta: called CC=3 SCAR=11.1
Pod undocumented
127 1 my ( $seq, $fh ) = @_;
128
129 # print basic header
130 1 print $fh '>', $seq->id;
131
132 # add extra info
133 1 ▶ if ( $seq->desc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq->desc) { } 1 0 1
134 1 print $fh ' ', $seq->desc, "\n";
135 }
136
137 # XXX in previous runs there always was additional info (read from an INI file)
138 # that would be added to the FASTA definitions line. These would have been
139 # key/value pairs about the sample, e.g. its sex or developmental stage. After
140 # that additional info we would have then added a line break (at line 134). We
141 # have now encountered a case where there is no additional info, which meant
142 # that the line break would not be added, resulting in garbled FASTA. The
143 # conditional statement below this addresses that specific case.
144 else {
145 0 print $fh "\n";
146 }
147
148 # fold lines at 80 characters
149 1 my $retval;
150 1 for my $line ( unpack "(a80)*", $seq->seq ) {
151 1 $retval = print $fh $line, "\n";
152 }
153 1 DEBUG "wrote sequence ".$seq->id;
154 1 return $retval;
155 }
156  
157 1 ▶sub read_fasta {
Subroutine read_fasta: called CC=5 SCAR=16.1
Pod undocumented
158 1 my ( $class, $fh, $pos ) = @_;
159 1 ▶ $pos = 0 if not defined $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 1 0 1
160
161 # advance to the current position
162 1 seek $fh, $pos, 0;
163 1 DEBUG "reading starts at $pos";
164
165 # these will store what's on the def line and the subsequent seq data
166 1 my ( $chr, $seq, $desc );
167 1 ▶ LINE: while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 2 1 3
168 2 chomp;
169
170 # we have a definition line, break if we've already processed a defline
171 2 ▶ if ( /^>(.+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^>(.+)$/) { } 1 1 2
172 1 ▶ last LINE if $chr;
Branch: 1 branch
BranchTrueFalseTotal
if $chr 0 1 1
173 1 my $defline = $1;
174 1 ( $chr, $desc ) = split /\s+/, $defline, 2;
175 1 DEBUG "going to concatenate sequence data for $chr";
176 }
177 else {
178 1 $seq .= $_;
179
180 # we store the current position here so that it is set to
181 # just before the defline when we encounter it
182 1 $pos = tell $fh;
183 }
184 }
185 1 DEBUG "reading ended at $pos";
186 1 return $pos, $class->new(
187 '-id' => $chr,
188 '-desc' => $desc,
189 '-seq' => \$seq,
190 );
191 }
192  
193 4 1;
« WGS2NCBI/Logger.pm ↑ Summary WGS2NCBI/StrandedFeature.pm »