Debian Codegraph @

t/03cram.t

raw
1
2 #-*-Perl-*-
3 # Copyright [2015-2024] EMBL-European Bioinformatics Institute
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 use strict;
18 use warnings;
19 use File::Temp qw(tempfile);
20 use FindBin '$Bin';
21 use constant TEST_COUNT => 139;
22
23 use lib "$Bin/../lib", "$Bin/../blib/lib", "$Bin/../blib/arch";
24
25 BEGIN {
26 # to handle systems with no installed Test module
27 # we include the t dir (where a copy of Test.pm is located)
28 # as a fallback
29 eval { require Test; };
30 if ($@) {
31 use lib 't';
32 }
33 use Test;
34 plan test => TEST_COUNT;
35 }
36
37 use Bio::DB::HTS;
38 use Bio::DB::HTS::AlignWrapper;
39
40 my $cramfile = "$Bin/data/yeast.sorted.cram";
41 my $fastafile = "$Bin/data/yeast.fasta";
42
43 # low level tests (defined in lib/Bio/DB/HTS.pm)
44 {
45 my $hts_file = Bio::DB::HTSfile->open($cramfile);
46 ok($hts_file);
47
48 my $header = $hts_file->header_read();
49 my $targets = $header->n_targets;
50 ok( $targets, 17, "Targets Found" );
51
52 my $target_names = $header->target_name;
53 ok( $target_names );
54 ok( scalar @$target_names );
55 ok( $target_names->[10], 'X' );
56
57 my $target_lens = $header->target_len;
58 ok( $target_lens ) ;
59 ok( scalar @$target_lens, 17 );
60 ok( $target_lens->[0], 230218 );
61
62 my $text = $header->text;
63 ok( length $text > 0 );
64
65 my $c = "\@CO\tThis is a comment\n";
66 $header->text($c);
67 ok( $header->text, $c );
68
69 my $region = 'X:51-1000' ;
70 my $fai = Bio::DB::HTS::Fai->open("$Bin/data/yeast.fasta");
71 my $seq = $fai->fetch($region);
72 ok( length $seq, 950 );
73
74 my $count;
75 while ( my $b = $hts_file->read1($header) )
76 {
77 $count++;
78 }
79 ok( $count, 50000 );
80
81 my @result = $header->parse_region($region);
82 ok( $result[0], 10 );
83 ok( $result[1], 50 );
84 @result = $header->parse_region('seq_invalid:51-1000');
85 ok( scalar @result, 0 );
86
87 Bio::DB::HTSfile->index_build($cramfile);
88 my $index = Bio::DB::HTSfile->index_load($hts_file);
89 ok($index);
90
91 my @a;
92 my $print_region = sub {
93 my ( $alignment, $data ) = @_;
94 push @a, $alignment;
95 return;
96 };
97
98 $index->fetch( $hts_file, $header->parse_region('X'),
99 $print_region, "foobar" );
100 ok( scalar @a > 1 );
101
102 my %matches;
103 my $fetch_back = sub {
104 my ( $tid, $pos, $p ) = @_;
105 my $p1 = $pos + 1;
106 my $r = $fai->fetch( $header->target_name->[$tid] . ":$p1-$p1" );
107 for my $pileup (@$p) {
108 my $b = $pileup->b;
109 my $qpos = $pileup->qpos;
110 my $base =
111 $pileup->indel == 0 ? substr( $b->qseq, $qpos, 1 ) :
112 $pileup->indel > 0 ? '*' :
113 '-';
114 $matches{matched}++ if $r eq $base;
115 $matches{total}++;
116 }
117 };
118
119 $index->pileup( $hts_file, $header->parse_region($region), $fetch_back );
120 ok( $matches{matched}, 37 );
121 ok( $matches{total}, 96 );
122
123 }
124
125 # high level tests (defined in lib/Bio/DB/HTS.pm)
126
127 #
128 # Note that the high level API does not reset the CRAM file pointer to the start
129 # of the file as the method to do so is (at time or writing) not easily accessible.
130 # Therefore some of these tests create a new HTS object.
131 #
132 my $dummy = eval
133 {
134 Bio::DB::HTS->new( -fasta => "invalid_path.txt",
135 -bam => "invalid_path.txt" );
136 };
137 ok( $dummy, undef );
138 ok( $@ =~ /does not exist/ );
139
140
141 for my $use_fasta (0,1)
142 {
143 my $hts = Bio::DB::HTS->new(
144 -fasta => $fastafile,
145 -bam => $cramfile,
146 -expand_flags => 1,
147 -autoindex => 1,
148 -force_refseq => $use_fasta, );
149 ok($hts);
150 ok( $hts->n_targets, 17 );
151
152 ok( $hts->length('X'), 745751 );
153
154 my $seg = $hts->segment('I');
155 ok($seg);
156 ok( $seg->length, 230218 );
157 my $seq = $seg->seq;
158 ok( $seq->isa('Bio::PrimarySeq') );
159 ok( length $seq->seq, 230218 );
160
161 my @alignments =
162 $hts->get_features_by_location( -seq_id => 'I',
163 -start => 500,
164 -end => 20000 );
165 ok( scalar @alignments, 71 );
166 ok( $alignments[0]->seq_id, 'I' );
167
168 ok( scalar @{ $alignments[0]->qscore }, length $alignments[0]->dna );
169
170 my @keys = $alignments[0]->get_all_tags;
171 ok( scalar @keys, 17 );
172 ok( $alignments[0]->get_tag_values('AS'), 36 );
173
174 my %att = $alignments[0]->attributes;
175 ok( scalar( keys %att ), 17 );
176 ok( $alignments[0]->cigar_str, '36M' );
177
178 my $query = $alignments[0]->query;
179 ok($query);
180 ok( $query->start, 1 );
181 ok( $query->end, 36 );
182 ok( $query->length, 36 );
183 ok( $query->dna, $alignments[0]->dna );
184 ok( $alignments[0]->strand, -1 );
185 ok( $query->strand, -1 );
186
187 my $target = $alignments[0]->target;
188 ok($target);
189 ok( $target->start, 36 );
190 ok( $target->end, 1 );
191 ok( $target->length, 36 );
192 ok( $target->dna, reversec( $alignments[0]->dna ) );
193
194 ok( $alignments[0]->get_tag_values('FLAGS'), $alignments[0]->flag_str );
195
196 my @pads = $alignments[0]->padded_alignment;
197 ok( scalar @pads, 3 );
198 ok( $pads[0], $pads[2] );
199 ok( $pads[1] =~ tr/|/|/, length( $pads[0] ) );
200
201 # There is an issue to be resolved still with fetching features by name for CRAM files
202 # so opening a new object each time to ensure the tests take place.
203 $hts = Bio::DB::HTS->new(
204 -fasta => $fastafile,
205 -bam => $cramfile,
206 -expand_flags => 1,
207 -autoindex => 1,
208 -force_refseq => $use_fasta, );
209
210 my @f = $hts->features( -name => 'SRR507778.24982' );
211 ok( scalar @f, 2 );
212
213 $hts = Bio::DB::HTS->new(
214 -fasta => $fastafile,
215 -bam => $cramfile,
216 -expand_flags => 1,
217 -autoindex => 1,
218 -force_refseq => $use_fasta, );
219 @f = $hts->features(
220 -filter => sub {
221 my $a = shift;
222 return 1 if $a->display_name eq 'SRR507778.24982';
223 } );
224 ok( scalar @f, 2 );
225
226 $hts = Bio::DB::HTS->new(
227 -fasta => $fastafile,
228 -bam => $cramfile,
229 -expand_flags => 1,
230 -autoindex => 1,
231 -force_refseq => $use_fasta, );
232 @f = $hts->features(
233 -seq_id => 'XIII',
234 -filter => sub {
235 my $a = shift;
236 return 1 if $a->display_name =~ /^SRR507778/;
237 } );
238 ok( scalar @f, 3296 );
239
240 $hts = Bio::DB::HTS->new(
241 -fasta => $fastafile,
242 -bam => $cramfile,
243 -expand_flags => 1,
244 -autoindex => 1,
245 -force_refseq => $use_fasta, );
246 @f = $hts->features(
247 -filter => sub {
248 my $a = shift;
249 return 1 if $a->display_name =~ /^SRR507778/;
250 } );
251 ok( scalar @f, 50000 );
252
253 $hts = Bio::DB::HTS->new(
254 -fasta => $fastafile,
255 -bam => $cramfile,
256 -expand_flags => 1,
257 -autoindex => 1,
258 -force_refseq => $use_fasta, );
259 @f = $hts->features( -name => 'SRR507778.24982',
260 -tags => { FIRST_MATE => 1 } );
261 ok( scalar @f, 1 );
262
263 # try iteration
264 my $i =
265 $hts->get_seq_stream( -seq_id => 'XIII', -start => 200, -end => 10000 );
266 ok($i);
267 my $count = 0;
268 while ( $i->next_seq ) { $count++ }
269 ok( $count, 65 );
270
271 # try tam fh retrieval
272 my $fh = $hts->get_seq_fh( -seq_id => 'XIII', -start => 200, -end => 10000, );
273 $count = 0;
274 $count++ while <$fh>;
275 ok( $count, 65 );
276 $fh->close;
277
278 $hts = Bio::DB::HTS->new(
279 -fasta => $fastafile,
280 -bam => $cramfile,
281 -expand_flags => 1,
282 -autoindex => 1,
283 -force_refseq => $use_fasta, );
284 $i = $hts->get_seq_stream(); # all features!
285 ok($i);
286 $count = 0;
287 while ( $i->next_seq ) { $count++ }
288 ok( $count, 50000 );
289
290 $hts = Bio::DB::HTS->new(
291 -fasta => $fastafile,
292 -bam => $cramfile,
293 -expand_flags => 1,
294 -autoindex => 1,
295 -force_refseq => $use_fasta, );
296 $i = $hts->get_seq_stream( -max_features => 200, -seq_id => 'XIII' );
297 ok($i);
298 $count = 0;
299 while ( $i->next_seq ) { $count++ }
300 ok( $count, 200 );
301 ok( $hts->last_feature_count, 3296 );
302
303 # try the read_pair aggregation
304 my @pairs = $hts->features( -type => 'read_pair', -seq_id => 'XIII' );
305 ok( scalar @pairs, 1682 );
306
307 # test high level API version of pileup
308 $hts = Bio::DB::HTS->new(
309 -fasta => $fastafile,
310 -bam => $cramfile,
311 -expand_flags => 1,
312 -autoindex => 1,
313 -force_refseq => $use_fasta, );
314 my @coverage = $hts->features( -type => 'coverage', -seq_id => 'XIII' );
315 my ($c) = $coverage[0]->get_tag_values('coverage');
316 ok( $coverage[0]->type, "coverage:924431" ) ;
317 my %matches;
318 my $fetch_back = sub {
319 my ( $seqid, $pos, $p ) = @_;
320 my $r = $hts->segment( $seqid, $pos, $pos )->dna;
321 for my $pileup (@$p) {
322 my $a = $pileup->alignment;
323 my $qpos = $pileup->qpos;
324 my $dna = $a->query->dna;
325 my $base =
326 $pileup->indel == 0 ? substr( $dna, $qpos, 1 ) :
327 $pileup->indel > 0 ? '*' :
328 '-';
329 $matches{matched}++ if $r eq $base;
330 $matches{total}++;
331 }
332 };
333 $hts->pileup( 'XIII:1-1000', $fetch_back );
334 ok( $matches{matched}, 115 );
335 ok( $matches{total}, 211 );
336
337 # test filtered coverage (no filtering)
338 $hts = Bio::DB::HTS->new(
339 -fasta => $fastafile,
340 -bam => $cramfile,
341 -expand_flags => 1,
342 -autoindex => 1,
343 -force_refseq => $use_fasta, );
344 @coverage = $hts->features( -type => 'coverage', -seq_id => 'XIII',
345 -filter => sub { return 1; } );
346 ($c) = $coverage[0]->get_tag_values('coverage');
347 ok( $c );
348 ok( $c->[30], 3 );
349 ok( $c->[33148], 3 );
350 ok( $c->[924333], 5 );
351 ok( $c->[924351], 4 );
352 ok( $coverage[0]->type, "coverage:924431" );
353
354 # test filtered coverage (really filtering)
355 $hts = Bio::DB::HTS->new(
356 -fasta => $fastafile,
357 -bam => $cramfile,
358 -expand_flags => 1,
359 -autoindex => 1,
360 -force_refseq => $use_fasta, );
361 @coverage = $hts->features( -type => 'coverage', -seq_id => 'XIII',
362 -filter => sub { my $a = shift;
363 return defined $a->start && $a->start < 100000; } );
364 ($c) = $coverage[0]->get_tag_values('coverage');
365 ok( $c );
366 ok( $c->[30], 3 );
367 ok( $c->[33148], 3 );
368 ok( $c->[924333], 0 );
369 ok( $c->[924351], 0 );
370 ok( $coverage[0]->type, "coverage:924431" );
371
372 } ## end for my $use_fasta ( 0, ...)
373
374 exit 0;
375
376 sub reversec {
377 my $dna = shift;
378 $dna =~ tr/gatcGATC/ctagCTAG/;
379 return scalar reverse $dna;
380 }
381
382 __END__