| 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 File::Temp qw(tempfile); |
| 19 |
use FindBin '$Bin'; |
| 20 |
|
| 21 |
use constant HAS_LEAKTRACE => eval{ require Test::LeakTrace }; |
| 22 |
use Test::More HAS_LEAKTRACE ? (tests => 6) : (skip_all => 'require Test::LeakTrace'); |
| 23 |
use Test::LeakTrace; |
| 24 |
|
| 25 |
use lib "$Bin/../lib", "$Bin/../blib/lib", "$Bin/../blib/arch"; |
| 26 |
|
| 27 |
use_ok('Bio::DB::HTS'); |
| 28 |
use_ok('Bio::DB::HTS::AlignWrapper'); |
| 29 |
use_ok('Bio::DB::HTS::VCF'); |
| 30 |
|
| 31 |
my $cramfile = "$Bin/data/yeast.sorted.cram"; |
| 32 |
my $fastafile = "$Bin/data/yeast.fasta"; |
| 33 |
|
| 34 |
# low level tests (defined in lib/Bio/DB/HTS.pm) |
| 35 |
no_leaks_ok { |
| 36 |
my $hts_file = Bio::DB::HTSfile->open($cramfile); |
| 37 |
my $header = $hts_file->header_read(); |
| 38 |
my $targets = $header->n_targets; |
| 39 |
|
| 40 |
my $target_names = $header->target_name; |
| 41 |
my $target_lens = $header->target_len; |
| 42 |
my $text = $header->text; |
| 43 |
|
| 44 |
my $c = "\@CO\tThis is a comment\n"; |
| 45 |
$header->text($c); |
| 46 |
|
| 47 |
my $region = 'X:51-1000' ; |
| 48 |
my $fai = Bio::DB::HTS::Fai->open("$Bin/data/yeast.fasta"); |
| 49 |
my $seq = $fai->fetch($region); |
| 50 |
|
| 51 |
my $count; |
| 52 |
while ( my $b = $hts_file->read1($header) ) |
| 53 |
{ |
| 54 |
$count++; |
| 55 |
} |
| 56 |
|
| 57 |
my @result = $header->parse_region($region); |
| 58 |
@result = $header->parse_region('seq_invalid:51-1000'); |
| 59 |
|
| 60 |
Bio::DB::HTSfile->index_build($cramfile); |
| 61 |
my $index = Bio::DB::HTSfile->index_load($hts_file); |
| 62 |
|
| 63 |
my @a; |
| 64 |
my $print_region = sub { |
| 65 |
my ( $alignment, $data ) = @_; |
| 66 |
push @a, $alignment; |
| 67 |
return; |
| 68 |
}; |
| 69 |
|
| 70 |
$index->fetch( $hts_file, $header->parse_region('X'), |
| 71 |
$print_region, "foobar" ); |
| 72 |
|
| 73 |
my %matches; |
| 74 |
my $fetch_back = sub { |
| 75 |
my ( $tid, $pos, $p ) = @_; |
| 76 |
my $p1 = $pos + 1; |
| 77 |
my $r = $fai->fetch( $header->target_name->[$tid] . ":$p1-$p1" ); |
| 78 |
for my $pileup (@$p) { |
| 79 |
my $b = $pileup->b; |
| 80 |
my $qpos = $pileup->qpos; |
| 81 |
my $base = |
| 82 |
$pileup->indel == 0 ? substr( $b->qseq, $qpos, 1 ) : |
| 83 |
$pileup->indel > 0 ? '*' : |
| 84 |
'-'; |
| 85 |
$matches{matched}++ if $r eq $base; |
| 86 |
$matches{total}++; |
| 87 |
} |
| 88 |
}; |
| 89 |
|
| 90 |
$index->pileup( $hts_file, $header->parse_region($region), $fetch_back ); |
| 91 |
} 'Low level'; |
| 92 |
|
| 93 |
# high level tests (defined in lib/Bio/DB/HTS.pm) |
| 94 |
|
| 95 |
# |
| 96 |
# Note that the high level API does not reset the CRAM file pointer to the start |
| 97 |
# of the file as the method to do so is (at time or writing) not easily accessible. |
| 98 |
# Therefore some of these tests create a new HTS object. |
| 99 |
# |
| 100 |
|
| 101 |
no_leaks_ok { |
| 102 |
for my $use_fasta (0,1) { |
| 103 |
my $hts = Bio::DB::HTS->new( |
| 104 |
-fasta => $fastafile, |
| 105 |
-bam => $cramfile, |
| 106 |
-expand_flags => 1, |
| 107 |
-autoindex => 1, |
| 108 |
-force_refseq => $use_fasta, ); |
| 109 |
my $seg = $hts->segment('I'); |
| 110 |
my $seq = $seg->seq; |
| 111 |
|
| 112 |
my @alignments = |
| 113 |
$hts->get_features_by_location( -seq_id => 'I', |
| 114 |
-start => 500, |
| 115 |
-end => 20000 ); |
| 116 |
|
| 117 |
my @keys = $alignments[0]->get_all_tags; |
| 118 |
|
| 119 |
my %att = $alignments[0]->attributes; |
| 120 |
|
| 121 |
my $query = $alignments[0]->query; |
| 122 |
my $target = $alignments[0]->target; |
| 123 |
|
| 124 |
my @pads = $alignments[0]->padded_alignment; |
| 125 |
|
| 126 |
# There is an issue to be resolved still with fetching features by name for CRAM files |
| 127 |
# so opening a new object each time to ensure the tests take place. |
| 128 |
$hts = Bio::DB::HTS->new( |
| 129 |
-fasta => $fastafile, |
| 130 |
-bam => $cramfile, |
| 131 |
-expand_flags => 1, |
| 132 |
-autoindex => 1, |
| 133 |
-force_refseq => $use_fasta, ); |
| 134 |
|
| 135 |
my @f = $hts->features( -name => 'SRR507778.24982' ); |
| 136 |
|
| 137 |
$hts = Bio::DB::HTS->new( |
| 138 |
-fasta => $fastafile, |
| 139 |
-bam => $cramfile, |
| 140 |
-expand_flags => 1, |
| 141 |
-autoindex => 1, |
| 142 |
-force_refseq => $use_fasta, ); |
| 143 |
@f = $hts->features( |
| 144 |
-filter => sub { |
| 145 |
my $a = shift; |
| 146 |
return 1 if $a->display_name eq 'SRR507778.24982'; |
| 147 |
} ); |
| 148 |
|
| 149 |
$hts = Bio::DB::HTS->new( |
| 150 |
-fasta => $fastafile, |
| 151 |
-bam => $cramfile, |
| 152 |
-expand_flags => 1, |
| 153 |
-autoindex => 1, |
| 154 |
-force_refseq => $use_fasta, ); |
| 155 |
@f = $hts->features( |
| 156 |
-seq_id => 'XIII', |
| 157 |
-filter => sub { |
| 158 |
my $a = shift; |
| 159 |
return 1 if $a->display_name =~ /^SRR507778/; |
| 160 |
} ); |
| 161 |
|
| 162 |
$hts = Bio::DB::HTS->new( |
| 163 |
-fasta => $fastafile, |
| 164 |
-bam => $cramfile, |
| 165 |
-expand_flags => 1, |
| 166 |
-autoindex => 1, |
| 167 |
-force_refseq => $use_fasta, ); |
| 168 |
@f = $hts->features( |
| 169 |
-filter => sub { |
| 170 |
my $a = shift; |
| 171 |
return 1 if $a->display_name =~ /^SRR507778/; |
| 172 |
} ); |
| 173 |
|
| 174 |
$hts = Bio::DB::HTS->new( |
| 175 |
-fasta => $fastafile, |
| 176 |
-bam => $cramfile, |
| 177 |
-expand_flags => 1, |
| 178 |
-autoindex => 1, |
| 179 |
-force_refseq => $use_fasta, ); |
| 180 |
@f = $hts->features( -name => 'SRR507778.24982', |
| 181 |
-tags => { FIRST_MATE => 1 } ); |
| 182 |
|
| 183 |
# try iteration |
| 184 |
my $i = |
| 185 |
$hts->get_seq_stream( -seq_id => 'XIII', -start => 200, -end => 10000 ); |
| 186 |
my $count = 0; |
| 187 |
while ( $i->next_seq ) { |
| 188 |
$count++; |
| 189 |
} |
| 190 |
|
| 191 |
# try tam fh retrieval |
| 192 |
my $fh = $hts->get_seq_fh( -seq_id => 'XIII', -start => 200, -end => 10000, ); |
| 193 |
$count = 0; |
| 194 |
$count++ while <$fh>; |
| 195 |
$fh->close; |
| 196 |
|
| 197 |
$hts = Bio::DB::HTS->new( |
| 198 |
-fasta => $fastafile, |
| 199 |
-bam => $cramfile, |
| 200 |
-expand_flags => 1, |
| 201 |
-autoindex => 1, |
| 202 |
-force_refseq => $use_fasta, ); |
| 203 |
$i = $hts->get_seq_stream(); # all features! |
| 204 |
$count = 0; |
| 205 |
while ( $i->next_seq ) { |
| 206 |
$count++; |
| 207 |
} |
| 208 |
|
| 209 |
$hts = Bio::DB::HTS->new( |
| 210 |
-fasta => $fastafile, |
| 211 |
-bam => $cramfile, |
| 212 |
-expand_flags => 1, |
| 213 |
-autoindex => 1, |
| 214 |
-force_refseq => $use_fasta, ); |
| 215 |
$i = $hts->get_seq_stream( -max_features => 200, -seq_id => 'XIII' ); |
| 216 |
$count = 0; |
| 217 |
while ( $i->next_seq ) { |
| 218 |
$count++; |
| 219 |
} |
| 220 |
|
| 221 |
# try the read_pair aggregation |
| 222 |
my @pairs = $hts->features( -type => 'read_pair', -seq_id => 'XIII' ); |
| 223 |
|
| 224 |
# test high level API version of pileup |
| 225 |
$hts = Bio::DB::HTS->new( |
| 226 |
-fasta => $fastafile, |
| 227 |
-bam => $cramfile, |
| 228 |
-expand_flags => 1, |
| 229 |
-autoindex => 1, |
| 230 |
-force_refseq => $use_fasta, ); |
| 231 |
my @coverage = $hts->features( -type => 'coverage', -seq_id => 'XIII' ); |
| 232 |
my ($c) = $coverage[0]->get_tag_values('coverage'); |
| 233 |
|
| 234 |
my %matches; |
| 235 |
my $fetch_back = sub { |
| 236 |
my ( $seqid, $pos, $p ) = @_; |
| 237 |
my $r = $hts->segment( $seqid, $pos, $pos )->dna; |
| 238 |
for my $pileup (@$p) { |
| 239 |
my $a = $pileup->alignment; |
| 240 |
my $qpos = $pileup->qpos; |
| 241 |
my $dna = $a->query->dna; |
| 242 |
my $base = |
| 243 |
$pileup->indel == 0 ? substr( $dna, $qpos, 1 ) : |
| 244 |
$pileup->indel > 0 ? '*' : |
| 245 |
'-'; |
| 246 |
$matches{matched}++ if $r eq $base; |
| 247 |
$matches{total}++; |
| 248 |
} |
| 249 |
}; |
| 250 |
$hts->pileup( 'XIII:1-1000', $fetch_back ); |
| 251 |
|
| 252 |
# test filtered coverage (no filtering) |
| 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 |
@coverage = $hts->features( -type => 'coverage', -seq_id => 'XIII', |
| 260 |
-filter => sub { return 1; } ); |
| 261 |
($c) = $coverage[0]->get_tag_values('coverage'); |
| 262 |
|
| 263 |
# test filtered coverage (really filtering) |
| 264 |
$hts = Bio::DB::HTS->new( |
| 265 |
-fasta => $fastafile, |
| 266 |
-bam => $cramfile, |
| 267 |
-expand_flags => 1, |
| 268 |
-autoindex => 1, |
| 269 |
-force_refseq => $use_fasta, ); |
| 270 |
@coverage = $hts->features( -type => 'coverage', -seq_id => 'XIII', |
| 271 |
-filter => sub { my $a = shift; |
| 272 |
return defined $a->start && $a->start < 100000; } ); |
| 273 |
($c) = $coverage[0]->get_tag_values('coverage'); |
| 274 |
|
| 275 |
} # end for my $use_fasta ( 0, ...) |
| 276 |
} 'High level'; |
| 277 |
|
| 278 |
# get_info/format have been modified to allow not to specify ID, |
| 279 |
# lots of allow/dealloc going underneath, test SVs are not leaking |
| 280 |
no_leaks_ok { |
| 281 |
my $v = Bio::DB::HTS::VCF->new( filename => $Bin . "/data/test.bcf" ); |
| 282 |
my $h = $v->header(); |
| 283 |
my $row = $v->next(); |
| 284 |
|
| 285 |
my $info_result = $row->get_info($h); |
| 286 |
$info_result = $row->get_info($h, 'DUMMY'); |
| 287 |
$info_result = $row->get_info($h, 'NS'); |
| 288 |
|
| 289 |
my $fmt_result = $row->get_format($h, 'DUMMY'); |
| 290 |
$fmt_result = $row->get_format($h, 'HQ'); |
| 291 |
$fmt_result = $row->get_format($h); |
| 292 |
|
| 293 |
# Query tests |
| 294 |
$v = Bio::DB::HTS::VCF->new( filename => $Bin . "/data/test.vcf.gz" ); |
| 295 |
my $iter = $v->query("20:1000000-1231000"); |
| 296 |
my $i = 0; |
| 297 |
$i++ while $row = $iter->next; |
| 298 |
|
| 299 |
} 'VCF/BCF reading/querying'; |
| 300 |
|
| 301 |
done_testing(); |