| 1 |
#-*-Perl-*- |
| 2 |
# Copyright [2015-2024] EMBL-European Bioinformatics Institute |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
# you may not use this file except in compliance with the License. |
| 6 |
# You may obtain a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
# See the License for the specific language governing permissions and |
| 14 |
# limitations under the License. |
| 15 |
|
| 16 |
use strict; |
| 17 |
use warnings; |
| 18 |
use ExtUtils::MakeMaker; |
| 19 |
use File::Temp qw(tempfile); |
| 20 |
use FindBin '$Bin'; |
| 21 |
use constant TEST_COUNT => 22; |
| 22 |
use Data::Dumper; |
| 23 |
|
| 24 |
use lib "$Bin/../lib", "$Bin/../blib/lib", "$Bin/../blib/arch"; |
| 25 |
|
| 26 |
BEGIN { |
| 27 |
# to handle systems with no installed Test module |
| 28 |
# we include the t dir (where a copy of Test.pm is located) |
| 29 |
# as a fallback |
| 30 |
eval { require Test; }; |
| 31 |
if ($@) { |
| 32 |
use lib 't'; |
| 33 |
} |
| 34 |
use Test; |
| 35 |
plan test => TEST_COUNT; |
| 36 |
} |
| 37 |
|
| 38 |
use Bio::DB::HTS; |
| 39 |
|
| 40 |
{ |
| 41 |
my $infile = "$Bin/data/ex1.bam"; |
| 42 |
my $hts_file = Bio::DB::HTSfile->open($infile); |
| 43 |
ok($hts_file); |
| 44 |
|
| 45 |
my $header = $hts_file->header_read(); |
| 46 |
|
| 47 |
my $bamfile = "$Bin/data/write_test_08.bam"; |
| 48 |
my $hts_file2 = Bio::DB::HTSfile->open( $bamfile, 'wb' ); |
| 49 |
ok($hts_file2); |
| 50 |
|
| 51 |
$hts_file2->header_write($header); |
| 52 |
my $count; |
| 53 |
while ( my $b = $hts_file->read1($header) ) { |
| 54 |
$hts_file2->write1($header, $b); |
| 55 |
$count++; |
| 56 |
} |
| 57 |
$hts_file2 = undef; |
| 58 |
ok( $count, 3307 ); |
| 59 |
|
| 60 |
$hts_file = Bio::DB::HTSfile->open( $bamfile ); |
| 61 |
ok($hts_file); |
| 62 |
|
| 63 |
$header = $hts_file->header_read(); |
| 64 |
my $targets = $header->n_targets; |
| 65 |
ok( $targets, 2 ); |
| 66 |
|
| 67 |
my $target_names = $header->target_name; |
| 68 |
ok($target_names); |
| 69 |
ok( scalar @$target_names, 2 ); |
| 70 |
ok( $target_names->[0], 'seq1' ); |
| 71 |
|
| 72 |
my $target_lens = $header->target_len; |
| 73 |
ok($target_lens); |
| 74 |
ok( scalar @$target_lens, 2 ); |
| 75 |
ok( $target_lens->[0], 1575 ); |
| 76 |
|
| 77 |
my $text = $header->text; |
| 78 |
ok( length $text > 0 ); |
| 79 |
|
| 80 |
$count = 0; |
| 81 |
while ( my $b = $hts_file->read1($header) ) { |
| 82 |
$count++; |
| 83 |
} |
| 84 |
ok( $count, 3307 ); |
| 85 |
|
| 86 |
my @result = $header->parse_region('seq2:51-1000'); |
| 87 |
ok( $result[0], 1 ); |
| 88 |
ok( $result[1], 50 ); |
| 89 |
@result = $header->parse_region('seq_invalid:51-1000'); |
| 90 |
ok( scalar @result, 0 ); |
| 91 |
|
| 92 |
Bio::DB::HTSfile->index_build($bamfile); |
| 93 |
my $index = Bio::DB::HTSfile->index_load($hts_file); |
| 94 |
ok($index); |
| 95 |
|
| 96 |
my @a; |
| 97 |
my $print_region = sub { |
| 98 |
my ( $alignment, $data ) = @_; |
| 99 |
push @a, $alignment; |
| 100 |
return; |
| 101 |
}; |
| 102 |
|
| 103 |
$index->fetch( $hts_file, $header->parse_region('seq2'), |
| 104 |
$print_region, "foobar" ); |
| 105 |
ok( scalar @a > 1 ); |
| 106 |
|
| 107 |
my $fai = Bio::DB::HTS::Fai->open("$Bin/data/ex1.fa"); |
| 108 |
|
| 109 |
my %matches; |
| 110 |
my $fetch_back = sub { |
| 111 |
my ( $tid, $pos, $p ) = @_; |
| 112 |
my $p1 = $pos + 1; |
| 113 |
my $r = $fai->fetch( $header->target_name->[$tid] . ":$p1-$p1" ); |
| 114 |
for my $pileup (@$p) { |
| 115 |
my $b = $pileup->b; |
| 116 |
my $qpos = $pileup->qpos; |
| 117 |
my $base = |
| 118 |
$pileup->indel == 0 ? substr( $b->qseq, $qpos, 1 ) : |
| 119 |
$pileup->indel > 0 ? '*' : |
| 120 |
'-'; |
| 121 |
$matches{matched}++ if $r eq $base; |
| 122 |
$matches{total}++; |
| 123 |
} |
| 124 |
}; |
| 125 |
|
| 126 |
$index->pileup( $hts_file, $header->parse_region('seq2:1-100'), $fetch_back ); |
| 127 |
ok( $matches{matched}/$matches{total} > 0.99 ); |
| 128 |
|
| 129 |
# try to get coverage |
| 130 |
my $coverage = |
| 131 |
$index->coverage( $hts_file, $header->parse_region('seq2'), 100, 9000 ); |
| 132 |
ok( scalar @$coverage, 100 ); |
| 133 |
my @c = sort { $a <=> $b } @$coverage; |
| 134 |
ok( $c[0] >= 0 ); |
| 135 |
ok( $c[-1] < 1000 ); |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
exit 0; |
| 140 |
|
| 141 |
sub reversec { |
| 142 |
my $dna = shift; |
| 143 |
$dna =~ tr/gatcGATC/ctagCTAG/; |
| 144 |
return scalar reverse $dna; |
| 145 |
} |
| 146 |
|
| 147 |
__END__ |