Abi.pm

filter: Statementstmt 96.6% 174 / 180 Branchbran 84.2% 58 / 70 Conditioncond 66.6% 8 / 15 MC/DCmcdc 50.0% 3 / 10 Subroutinesub 100% 20 / 20 Podpod 100% 5 / 5 totaltotal 91.0% 268 / 300 CC 43 SCAR 22.1
CC
43
Cov
92%
new
15.4
merge
12.2
get_fastq
9.4
CRAP 9.1
« sw.pm ↑ Summary
1 package FASTX::Abi;
2 12 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Bio::Trace::ABIF;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use FASTX::sw 'align';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 $FASTX::Abi::VERSION = '1.0.0';
10  
11 12 use constant DEFAULT_MATRIX => { 'wildcard_match' => 0,
12 'match' => 1,
13 'mismatch' => -1,
14 'gap' => -2,
15 'gap_extend' => 0,
16 'wildcard' => 'N',
17 12 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
18
19 12 ▶use constant SCORE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 12 ▶use constant EVENT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 12 ▶use constant EXTEND => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 12 ▶use constant GAP_SRC => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 12 ▶use constant GAP_TGT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25  
26  
27 #ABSTRACT: Read Sanger trace file (chromatograms) in FASTQ format. For traces called with I<hetero> option, the ambiguities will be split into two sequences to allow usage from NGS tools that usually do not understand IUPAC ambiguities.
28  
29 12 our @valid_new_attributes = (
30 'filename', # *REQUIRED* input trace filepath
31 'trim_ends', # bool (default: 1) trim low quality ends
32 'wnd', # int (default: 16) sliding window for quality trim
33 'min_qual', # int (default: 22) threshold for low quality calls
34 'bad_bases', # int (default: 2) maximum number of low quality bases per window
35 'keep_abi', # bool (default: 0) import the Bio::Trace::ABIF object in FASTX::Abi (otherwise deleted after import)
36 );
37 12 our @valid_obj_attributes = (
38 'diff', # number of ambiguous bases
39 'diff_array', # array of ambiguous bases position
40 'sequence_name', # sequence name from filename
41 'instrument', # Instrument
42 'avg_peak_spacing', # Avg Peak Spacing in chromas
43 'version', # version chromatograms
44 'chromas', # Bio::Trace::ABIF object
45  
46 'hetero', # ambiguity
47 'seq1', # Sequence 1 (non ambiguous, allele1)
48 'seq2', # Sequence 2 (non ambiguous, allele2)
49 'sequence', # Sequence, trimmed
50 'quality', # Quality, trimmed
51 'raw_sequence', # Raw sequence
52 'raw_quality', # Raw quality
53 'iso_seq', # Sequence are equal
54 'discard', # Low quality sequence
55 );
56  
57 12 our %iupac = (
58 'R' => 'AG',
59 'Y' => 'CT',
60 'M' => 'CA',
61 'K' => 'TG',
62 'W' => 'TA',
63 'S' => 'CG'
64 );
65  
66  
67 sub new {
68 # Instantiate object
69 45 ▶ my ($class, $args) = @_;
Subroutine new: called CC=15 SCAR=27.3
Pod documented
70  
71 my $self = {
72 filename => $args->{filename}, # Chromatogram file name
73 trim_ends => $args->{trim_ends}, # Trim low quality ends (bool)
74 min_qual => $args->{min_qual}, # Minimum quality
75 wnd => $args->{wnd}, # Window for end trimming
76 bad_bases => $args->{bad_bases}, # Number of low qual bases per $window_width
77 keep_abi => $args->{keep_abi}, # Do not destroy $self->{chromas} after use
78 45 };
79  
80 #check valid inputs:
81 45 for my $input (sort keys %{ $args } ) {
82 67 ▶ if ( ! grep( /^$input$/, @valid_new_attributes ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^$input$/u, @FASTX::Abi::valid_new_attributes) 1 66 67
83 1 confess("Method new() does not accept \"$input\" attribute. Valid attributes are:\n", join(', ', @valid_new_attributes));
84 }
85 }
86  
87 # CHECK INPUT FILE
88 # -----------------------------------
89 44 ▶ if (not defined $self->{filename}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"filename"}) 0 44 44
90 0 confess("ABI file must be provided when creating new object");
91 }
92  
93 44 ▶ if (not -e $self->{filename}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $self->{"filename"}) 1 43 44
94 1 confess("ABI file not found: ", $self->{filename});
95 }
96 43 my $abif;
97 my $try = eval
98 43 {
99 43 $abif = Bio::Trace::ABIF->new();
100 43 ▶ $abif->open_abif($self->{filename}) or confess "Error in file: ", $self->{filename};
Branch: 1 branch
BranchTrueFalseTotal
$abif->open_abif($self->{"filename"}) or confess("Error in file: ", $self->{"filename"}) 0 43 43
101 43 1;
102 };
103  
104 43 ▶ if (not $try) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($try) 0 43 43
105 0 confess("Bio::Trace::ABIF was unable to read: ", $self->{filename});
106 }
107 43 my $object = bless $self, $class;
108 43 $object->{chromas} = $abif;
109  
110 43 my @ext = ('.abi','.ab1','.ABI','.abI','.AB1','.ab');
111 43 my ($seqname) = basename($self->{filename}, @ext);
112 43 $object->{sequence_name} = $abif->sample_name();
113  
114 # DEFAULTS
115 # -----------------------------------
116 43 ▶ $object->{trim_ends} = 1 unless defined $object->{trim_ends};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object->{"trim_ends"} 33 10 43
117 43 ▶ $object->{wnd} = 10 unless defined $object->{wnd};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object->{"wnd"} 41 2 43
118 43 ▶ $object->{min_qual} = 20 unless defined $object->{min_qual};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object->{"min_qual"} 37 6 43
119 43 ▶ $object->{bad_bases} = 4 unless defined $object->{bad_bases};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object->{"bad_bases"} 41 2 43
120 43 ▶ $object->{keep_abi} = 0 unless defined $object->{keep_abi};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object->{"keep_abi"} 41 2 43
121 43 $object->{discard} = 0;
122  
123 # GET SEQUENCE FROM AB1 FILE
124 # -----------------------------------
125 43 my $seq = _get_sequence($self);
126 43 ▶ if ($self->{keep_abi} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"keep_abi"} == 0) 42 1 43
127 42 $self->{chromas} = undef;
128 }
129  
130 #check valid attributes:
131 43 for my $input (sort keys %{ $self} ) {
132 # [this is a developer's safety net]
133 # uncoverable condition false
134 946 ▶ if ( ! grep( /^$input$/, @valid_new_attributes, @valid_obj_attributes ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^$input$/u, @FASTX::Abi::valid_new_attributes, @FASTX::Abi::valid_obj_attributes) 0 946 946
135 0 confess("Method new() does not accept \"$input\" attribute. Valid attributes are:\n", join(', ', @valid_new_attributes, @valid_obj_attributes));
136 }
137 }
138  
139  
140 43 return $object;
141 }
142  
143  
144 sub get_fastq {
145 16 ▶ my ($self, $name, $quality_value) = @_;
Subroutine get_fastq: called CC=9 SCAR=22.5
Pod documented
146  
147 16 ▶ if (not defined $name) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $name) { } 0 16 16
elsif ($name =~ /\s+/u) { } 0 16 16
148 0 $name = $self->{sequence_name};
149 } elsif ($name=~/\s+/) {
150 0 $name =~s/\s+/_/g;
151 }
152  
153 16 my $quality = $self->{quality};
154 16 ▶ if (defined $quality_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $quality_value) 12 4 16
155 12 ▶ if ($quality_value =~/^\d+$/ and $quality_value >= 10) {
Branch: 2 branches
BranchTrueFalseTotal
if ($quality_value =~ /^\d+$/u and $quality_value >= 10) { } 4 8 12
elsif (length $quality_value == 1) { } 4 4 8
Condition66%2/3
$quality_value =~ /^\d+$/u and $quality_value >= 10
!ll&&!rl&&r
804
MC/DC50%1/2
$quality_value =~ /^\d+$/u and $quality_value >= 10
$quality_value =~ /^\d+$/u $quality_value >= 10
Truth table66%2/3
A and B
A= $quality_value =~ /^\d+$/uB= $quality_value >= 10
ABresult
0X0
100
111
156 4 ▶ my $q = chr(($quality_value <= 93 ? $quality_value : 93) + 33);
Branch: 1 branch
BranchTrueFalseTotal
$quality_value <= 93 ? : 4 0 4
157 4 $quality = $q x length($quality);
158 } elsif (length($quality_value) == 1) {
159 4 $quality = $quality_value x length($quality);
160 } else {
161 4 confess("Supplied quality is neither a valid integer or a single char: <$quality_value>\n");
162 }
163 }
164  
165 12 my $output = '';
166 12 ▶ if ( $self->{iso_seq} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'iso_seq'}) { } 3 9 12
167 $output .= '@' . $name . "\n" .
168 3 $self->{seq1} . "\n+\n" .
169 $quality . "\n";
170 } else {
171 $output .= '@' . $name . "_1\n" .
172 9 $self->{seq1} . "\n+\n" .
173 $quality . "\n";
174 $output .= '@' . $name . "_2\n" .
175 9 $self->{seq2} . "\n+\n" .
176 $quality . "\n";
177 }
178 12 return $output;
179 }
180  
181  
182 sub get_trace_info {
183 1 ▶ my $self = shift;
Subroutine get_trace_info: called CC=1 SCAR=0.0
Pod documented
184 1 my $data;
185 1 $data->{instrument} = $self->{instrument};
186 1 $data->{version} = $self->{version};
187 1 $data->{avg_peak_spacing} = $self->{avg_peak_spacing};
188  
189 1 return $data;
190 }
191  
192  
193 sub rc {
194 1 ▶ my $self = shift;
Subroutine rc: called CC=1 SCAR=0.0
Pod documented
195 1 $self->{seq1} = reverse $self->{seq1};
196 1 $self->{seq2} = reverse $self->{seq2};
197 1 $self->{seq1} =~ tr/ACGTacgt/TGCAtgca/;
198 1 $self->{seq2} =~ tr/ACGTacgt/TGCAtgca/;
199 1 return $self;
200 }
201  
202  
203 sub merge {
204 1 ▶ my ($self, $other) = @_;
Subroutine merge: called CC=12 SCAR=25.1
Pod documented
205 1 $other->rc();
206  
207 1 my $aligner = FASTX::sw->new($self->{seq1}, $other->{seq1});
208 1 my ($top, $bars, $bottom) = $aligner->pads;
209 1 my $consensus = "";
210 1 my $pos1 = 0;
211 1 my $pos2 = 0;
212 1 ▶ for (my $pos = 0; $pos < length($top); $pos++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $top) 807 1 808
213 807 my $base1 = substr($top, $pos, 1);
214 807 my $base2 = substr($bottom, $pos, 1);
215  
216 807 ▶ my $pos1++ if ($base1 ne '-');
Branch: 1 branch
BranchTrueFalseTotal
if $base1 ne "-" 612 195 807
217 807 ▶ my $pos2++ if ($base2 ne '-');
Branch: 1 branch
BranchTrueFalseTotal
if $base2 ne "-" 677 130 807
218  
219 807 ▶ if ($base1 eq $base2) {
Branch: 4 branches
BranchTrueFalseTotal
if ($base1 eq $base2) { } 481 326 807
elsif ($base1 eq '-' and $base2 ne '-') { } 195 131 326
elsif ($base1 ne '-' and $base2 eq '-') { } 130 1 131
elsif ($base1 ne '-' and $base2 ne '-') { } 1 0 1
220 481 $consensus .= $base1;
221 ▶ } elsif ($base1 eq '-' and $base2 ne '-') {
Condition66%2/3
$base1 eq "-" and $base2 ne "-"
!ll&&!rl&&r
1310195
MC/DC50%1/2
$base1 eq "-" and $base2 ne "-"
$base1 eq "-" $base2 ne "-"
Truth table66%2/3
A and B
A= $base1 eq "-"B= $base2 ne "-"
ABresult
0X0
100
111
222 195 $consensus .= $base2;
223 ▶ } elsif ($base1 ne '-' and $base2 eq '-') {
Condition66%2/3
$base1 ne "-" and $base2 eq "-"
!ll&&!rl&&r
01130
MC/DC50%1/2
$base1 ne "-" and $base2 eq "-"
$base1 ne "-" $base2 eq "-"
Truth table66%2/3
A and B
A= $base1 ne "-"B= $base2 eq "-"
ABresult
0X0
100
111
224 130 $consensus .= $base1;
225 ▶ } elsif ($base1 ne '-' and $base2 ne '-') {
Condition33%1/3
$base1 ne "-" and $base2 ne "-"
!ll&&!rl&&r
001
MC/DC0%0/2
$base1 ne "-" and $base2 ne "-"
$base1 ne "-" $base2 ne "-"
Truth table33%1/3
A and B
A= $base1 ne "-"B= $base2 ne "-"
ABresult
0X0
100
111
226 1 my $qual1 = substr($self->{quality}, $pos1, 1);
227 1 my $qual2 = substr($other->{quality}, $pos2, 1);
228 1 ▶ $consensus .= _ascii_qual($qual1) >= _ascii_qual($qual2) ? lc($base1) : lc($base2);
Branch: 1 branch
BranchTrueFalseTotal
_ascii_qual($qual1) >= _ascii_qual($qual2) ? : 0 1 1
229 }
230 }
231 # evaluate longest stretch of "|" in $m
232 # my $longest = 0;
233 # my $longest_start = 0;
234 # my $longest_end = 0;
235 # my $longest_str = '';
236 # my $i = 0;
237 # while ($i < length($m)) {
238 # my $str = substr($m, $i, 1);
239 # if ($str eq '|') {
240 # my $start = $i;
241 # my $end = $i;
242 # while ($str eq '|') {
243 # $end++;
244 # $str = substr($m, $end, 1);
245 # }
246 # if ($end - $start > $longest) {
247 # $longest = $end - $start;
248 # $longest_start = $start;
249 # $longest_end = $end;
250 # $longest_str = $str;
251 # }
252 # }
253 # $i++;
254 # }
255
256 1 $self->{merge} = $aligner;
257 1 $self->{top} = $top;
258 1 $self->{bottom} = $bottom;
259 1 $self->{consensus} = $consensus;
260 1 $self->{bars} = $bars;
261 1 return $consensus;
262  
263 }
264  
265  
266 sub _get_sequence {
267 43 ▶ my $self = shift;
Subroutine _get_sequence: called CC=9 SCAR=22.0
268 43 my $abif = $self->{chromas};
269  
270 43 $self->{raw_sequence} = $abif->sequence();
271  
272 # Get quality values
273 43 my @qv = $abif->quality_values();
274 # Encode quality in FASTQ chars
275 43 ▶ my @fqv = map {chr(int(($_<=93? $_ : 93)*4/6) + 33)} @qv;
Branch: 1 branch
BranchTrueFalseTotal
$_ <= 93 ? : 32692 0 32692
276  
277 # FASTQ
278 43 my $q = join('', @fqv);
279  
280  
281 43 $self->{raw_quality} = $q;
282  
283 43 $self->{sequence} = $self->{raw_sequence};
284 43 $self->{quality} = $self->{raw_quality};
285  
286 # Trim
287 43 ▶ if ($self->{trim_ends}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"trim_ends"}) 39 4 43
288 #The Sequencing Analysis program determines the clear range of the sequence by trimming bases from the 5' to 3'
289 #ends until fewer than 4 bases out of 20 have a quality value less than 20.
290 #You can change these parameters by explicitly passing arguments to this method
291 #(the default values are $window_width = 20, $bad_bases_threshold = 4, $quality_threshold = 20).
292 # Note that Sequencing Analysis counts the bases starting from one, so you have to add one to the return values to get consistent results.
293  
294 my ($begin_pos, $end_pos) = $abif->clear_range(
295 $self->{wnd},
296 $self->{bad_bases},
297 $self->{min_qual},
298  
299 39 );
300  
301 # This can be tested with low quality chromatograms
302 # *TODO* to ask for some bad trace
303  
304 # uncoverable branch false
305 # uncoverable condition left
306 # uncoverable condition right
307  
308 39 ▶ if ($begin_pos>0 and $end_pos>0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($begin_pos > 0 and $end_pos > 0) { } 39 0 39
Condition100%3/3
$begin_pos > 0 and $end_pos > 0
!ll&&!rl&&r
0039
MC/DC0%0/2
$begin_pos > 0 and $end_pos > 0
-$begin_pos > 0 -$end_pos > 0
Truth table33%1/3
A and B
A= $begin_pos > 0B= $end_pos > 0
ABresult
0X0
100
111
309 39 my $hi_qual_length = $end_pos-$begin_pos+1;
310 39 $self->{sequence} = substr($self->{sequence}, $begin_pos, $hi_qual_length);
311 39 $self->{quality} = substr($self->{quality} , $begin_pos, $hi_qual_length);
312 } else {
313 0 $self->{discard} = 1;
314 }
315 }
316  
317 # Check hetero bases
318 43 ▶ if ($self->{sequence}!~/[ACGT][RYMKWS]+[ACGT]/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'sequence'} =~ /[ACGT][RYMKWS]+[ACGT]/iu) { } 14 29 43
319 14 $self->{hetero} = 0;
320 } else {
321 29 $self->{hetero} = 1;
322 }
323  
324 # Check
325 43 $self->{diff_array} = ();
326 43 $self->{diff} = 0;
327 43 my $seq1 = '';
328 43 my $seq2 = '';
329 43 ▶ for (my $i = 0; $i<length($self->{sequence}); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $self->{"sequence"}) 27280 43 27323
330 27280 my $q0 = substr($self->{quality}, $i, 1);
331 27280 my $s0 = substr($self->{sequence}, $i,1);
332  
333 # Ambiguity detected:
334 27280 ▶ if ($iupac{$s0}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FASTX::Abi::iupac{$s0}) { } 74 27206 27280
335 74 my ($base1, $base2) = split //, $iupac{$s0};
336 74 $seq1.=$base1;
337 74 $seq2.=$base2;
338 74 $self->{diff}++;
339 74 push(@{ $self->{diff_array} }, $i);
340 } else {
341 27206 $seq1.=$s0;
342 27206 $seq2.=$s0;
343  
344 }
345 }
346 43 $self->{seq1} = $seq1;
347 43 $self->{seq2} = $seq2;
348  
349 43 ▶ if ($seq1 eq $seq2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq1 eq $seq2) { } 14 29 43
350 14 $self->{iso_seq} = 1
351 } else {
352 29 $self->{iso_seq} = 0;
353 }
354  
355  
356 43 $self->{instrument} = $self->{chromas}->official_instrument_name();
357 43 $self->{version} = $self->{chromas}->abif_version();
358 43 $self->{avg_peak_spacing} = $self->{chromas}->avg_peak_spacing();
359  
360 }
361  
362 sub _ascii_qual {
363 2 ▶ my ($qual, $offset) = @_;
Subroutine _ascii_qual: called CC=2 SCAR=7.1
364 2 ▶ $offset = 33 unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 2 0 2
365 2 return ord($qual) - $offset;
366 }
367 12 1;
368  
369 __END__
« sw.pm ↑ Summary