| 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 |
|
| 19 |
use ExtUtils::MakeMaker; |
| 20 |
use File::Temp qw(tempfile); |
| 21 |
use FindBin '$Bin'; |
| 22 |
|
| 23 |
use Test::More; |
| 24 |
|
| 25 |
use lib "$Bin/../lib", "$Bin/../blib/lib", "$Bin/../blib/arch"; |
| 26 |
use Bio::DB::HTS; |
| 27 |
|
| 28 |
my $samfile = "$Bin/data/test.sam"; |
| 29 |
|
| 30 |
# low-level tests |
| 31 |
my $hts_file = Bio::DB::HTSfile->open($samfile); |
| 32 |
ok($hts_file); |
| 33 |
|
| 34 |
# |
| 35 |
# https://github.com/Ensembl/Bio-DB-HTS/issues/54 |
| 36 |
# reading header again should seek at start of file for sam as well |
| 37 |
# and should not exit with samtools error: |
| 38 |
# [E::sam_parse1] missing SAM header |
| 39 |
# [W::sam_read1] parse error at line 4 |
| 40 |
# |
| 41 |
# WARNING |
| 42 |
# |
| 43 |
# Couldn't get it to working for htslib < 1.5 (segfault) |
| 44 |
# Skip the tests in these cases |
| 45 |
# |
| 46 |
SKIP: { |
| 47 |
skip "Cannot test header reading for htslib < 1.5", 10 |
| 48 |
if Bio::DB::HTS->htslib_version() lt "1.5"; |
| 49 |
|
| 50 |
my $header = $hts_file->header_read(); |
| 51 |
my $targets = $header->n_targets; |
| 52 |
is( $targets, 1, "Number of reference sequences" ); |
| 53 |
|
| 54 |
$header = $hts_file->header_read(); |
| 55 |
$targets = $header->n_targets; |
| 56 |
is( $targets, 1, "num reference sequences" ); |
| 57 |
|
| 58 |
my $target_names = $header->target_name; |
| 59 |
ok($target_names); |
| 60 |
is( scalar @$target_names, 1, "num reference sequence names" ); |
| 61 |
is( $target_names->[0], 'ref', "reference sequence name" ); |
| 62 |
|
| 63 |
my $target_lens = $header->target_len; |
| 64 |
ok($target_lens); |
| 65 |
is( scalar @$target_lens, 1 ); |
| 66 |
# ok( $target_lens->[0], 100 ); |
| 67 |
|
| 68 |
my $text = $header->text; |
| 69 |
ok( length $text > 0 ); |
| 70 |
|
| 71 |
my $c = "\@CO\tThis is a comment\n"; |
| 72 |
$header->text($c); |
| 73 |
is( $header->text, $c ); |
| 74 |
|
| 75 |
my $count; |
| 76 |
while ( my $b = $hts_file->read1($header) ) { |
| 77 |
$count++; |
| 78 |
} |
| 79 |
is( $count, 2, "num reads mapped" ); |
| 80 |
} |
| 81 |
|
| 82 |
# TODO |
| 83 |
# parse region |
| 84 |
|
| 85 |
# high-level tests |
| 86 |
my $hts = Bio::DB::HTS->new( -bam => $samfile ); |
| 87 |
ok( $hts ); |
| 88 |
is( $hts->n_targets, 1 ); |
| 89 |
|
| 90 |
# |
| 91 |
# https://github.com/Ensembl/Bio-DB-HTS/issues/54 again |
| 92 |
# should read header correctly and not exit with error |
| 93 |
# |
| 94 |
SKIP: { |
| 95 |
skip "Cannot test iterating over alignments for htslib < 1.5", 4 |
| 96 |
if Bio::DB::HTS->htslib_version() lt "1.5"; |
| 97 |
|
| 98 |
my $alignment_iterator = $hts->features(-iterator => 1); |
| 99 |
ok($alignment_iterator->next_seq->qname eq "r001"); |
| 100 |
ok($alignment_iterator->next_seq->qname eq "r002"); |
| 101 |
ok(!$alignment_iterator->next_seq); |
| 102 |
} |
| 103 |
|
| 104 |
is( $hts->length('ref'), 100 ); |
| 105 |
is( join('', $hts->seq_ids), 'ref' ); |
| 106 |
|
| 107 |
my $seg = $hts->segment('ref'); |
| 108 |
ok($seg); |
| 109 |
is( $seg->length, 100 ); |
| 110 |
my $seq = $seg->seq; |
| 111 |
isa_ok( $seq, 'Bio::PrimarySeq' ); |
| 112 |
is( length $seq->seq, 100 ); |
| 113 |
|
| 114 |
# TODO |
| 115 |
# get_features_by_location |
| 116 |
# seq_id, qscore |
| 117 |
# get_tag_values |
| 118 |
# attributes |
| 119 |
# query |
| 120 |
# target |
| 121 |
# features |
| 122 |
|
| 123 |
done_testing(); |