Debian Codegraph @

t/05vcf.t

raw ◀ ▶
1 Debian patch touch this file
1 # Copyright [2015-2024] EMBL-European Bioinformatics Institute
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 use strict;
16 use warnings;
17 use Test::More tests => 177, 'die';
18
19 use FindBin qw( $Bin );
20 use Data::Dumper;
21
22 BEGIN {
23 use lib "$Bin/../lib", "$Bin/../blib/lib", "$Bin/../blib/arch";
24 use_ok 'Bio::DB::HTS::VCF';
25 use_ok 'Bio::DB::HTS::VCF::Row';
26 use_ok 'Bio::DB::HTS::VCF::RowPtr';
27 use_ok 'Bio::DB::HTS::VCF::Header';
28 use_ok 'Bio::DB::HTS::VCF::HeaderPtr';
29 }
30
31 {
32 # Test sweep functions
33 my $sweep = Bio::DB::HTS::VCF::Sweep->new(filename => $Bin . "/data/test.vcf.gz");
34 my $h = $sweep->header ;
35
36 my $row = $sweep->next_row();
37 is $row->chromosome($h), "19", "Chromosome value read" ;
38 #This should one day be fixed to 2 - but the HTSlib API always returns 0 at the moment
39 #is $row->num_filters(), "2", "Number of filters read" ;
40 #Once that is corrected, also add a test for the filters themselves
41
42 $row = $sweep->previous_row();
43 is $row->chromosome($h), "X", "Chromosome value read" ;
44 is $row->position(), 10, "Position value read" ;
45 is $row->quality(), 10, "Quality value read" ;
46
47 $row = $sweep->previous_row();
48 is $row->reference(), "T", "Quality value read" ;
49
50 $row = $sweep->previous_row();
51 is $row->id(), "microsat1", "ID value read" ;
52 is $row->num_alleles(), 2, "Number of alleles value read" ;
53 my $a_team = $row->get_alleles() ;
54 isa_ok($a_team, 'ARRAY');
55 is_deeply $a_team, ['GA', 'GAC'], 'alleles are correct';
56
57 $sweep->close() ;
58 }
59
60 {
61 # Test standard functions on a VCF file
62 ok my $v = Bio::DB::HTS::VCF->new( filename => $Bin . "/data/test.vcf.gz" ), "VCF file open";
63 is $v->num_variants(), 9, 'correct number of variants identified in file';
64
65 my $h = $v->header();
66
67 my $header_str = <<"HEADER";
68 ##fileformat=VCFv4.0
69 ##FILTER=<ID=PASS,Description="All filters passed">
70 ##fileDate=20090805
71 ##source=myImputationProgramV3.1
72 ##reference=1000GenomesPilot-NCBI36
73 ##phasing=partial
74 ##INFO=<ID=NS,Number=1,Type=Integer,Description="Number of Samples With Data">
75 ##INFO=<ID=AN,Number=1,Type=Integer,Description="Total number of alleles in called genotypes">
76 ##INFO=<ID=AC,Number=.,Type=Integer,Description="Allele count in genotypes, for each ALT allele, in the same order as listed">
77 ##INFO=<ID=DP,Number=1,Type=Integer,Description="Total Depth">
78 ##INFO=<ID=AF,Number=.,Type=Float,Description="Allele Frequency">
79 ##INFO=<ID=AA,Number=1,Type=String,Description="Ancestral Allele">
80 ##INFO=<ID=TT,Number=0,Type=String,Description="Test">
81 ##INFO=<ID=DB,Number=0,Type=Flag,Description="dbSNP membership, build 129">
82 ##INFO=<ID=H2,Number=0,Type=Flag,Description="HapMap2 membership">
83 ##FILTER=<ID=q10,Description="Quality below 10">
84 ##FILTER=<ID=s50,Description="Less than 50% of samples have data">
85 ##FILTER=<ID=MQ45,Description="MQ45 info">
86 ##FILTER=<ID=DP50,Description="DP50 info">
87 ##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
88 ##FORMAT=<ID=GQ,Number=1,Type=Integer,Description="Genotype Quality">
89 ##FORMAT=<ID=DP,Number=1,Type=Integer,Description="Read Depth">
90 ##FORMAT=<ID=HQ,Number=2,Type=Integer,Description="Haplotype Quality">
91 ##ALT=<ID=DEL:ME:ALU,Description="Deletion of ALU element">
92 ##ALT=<ID=CNV,Description="Copy number variable region">
93 ##contig=<ID=19>
94 ##contig=<ID=20>
95 ##contig=<ID=X>
96 #CHROM POS ID REF ALT QUAL FILTER INFO FORMAT NA00001 NA00002 NA00003
97 HEADER
98
99 is($h->fmt_text, $header_str, "Header formatted string");
100 is $h->version(), "VCFv4.0", "VCF Header version matches" ;
101 is $h->num_samples(), 3, "Number of samples" ;
102 is_deeply $h->get_sample_names(), ['NA00001','NA00002','NA00003'], "sample names correct" ;
103 is $h->num_seqnames(), 3, "Number of seqnames" ;
104 ok my $seqnames = $h->get_seqnames() ;
105 is_deeply $seqnames, ['19','20','X'], "sequence names correct" ;
106
107 ok my $row = $v->next(), "Next row";
108 is $row->chromosome($h), "19", "Chromosome value read" ;
109 is $row->position(), "111", "Position value read" ;
110 is $row->id(), "testid", "ID value read" ;
111 is $row->num_filters(), 2, "Num Filters OK" ;
112 is $row->has_filter($h,"DP50"), 1, "Actual Filter present" ;
113 is $row->has_filter($h,"."), 0, "PASS filter absent" ;
114 is $row->get_variant_type(1),1, "Variant type matches" ;
115
116 #info related tests
117 my $info_result ;
118 $info_result = $row->get_info($h);
119 is_deeply($info_result, { AF => [0.5], DB => [1], DP => [14], H2 => [1], NS => [3], TT => ['TESTSTRING'] }, 'info read correctly');
120 $info_result = $row->get_info($h,"DB") ;
121 isa_ok($info_result, 'ARRAY');
122 is_deeply $info_result, [1], 'info flag read correctly';
123 is $row->get_info_type($h,"DB"), "Flag", "info flag type correct" ;
124
125 $info_result = $row->get_info($h,"AF") ;
126 isa_ok($info_result, 'ARRAY');
127 is_deeply $info_result, [0.5], 'info float read correctly';
128 is $row->get_info_type($h,"AF"), "Float", "info float type correct" ;
129
130 $info_result = $row->get_info($h,"TT") ;
131 isa_ok($info_result, 'ARRAY');
132 is_deeply $info_result, ["TESTSTRING"], 'info string read correctly';
133 is $row->get_info_type($h,"TT"), "String", "info String type correct" ;
134
135 $info_result = $row->get_info($h,"NS") ;
136 isa_ok($info_result, 'ARRAY');
137 is_deeply $info_result, [3], 'info ints read correctly';
138 is $row->get_info_type($h,"NS"), "Integer", "info int type correct" ;
139
140 # format related tests
•141 is_deeply($row->get_format($h, "GT"), [3, 3, 3, 3, 2, 4], 'format int read');
142 is_deeply($row->get_format($h, "HQ"), [10, 10, 10, 10, 3, 3], 'format int read');
143 is($row->get_format($h, "INVALID"), "ID_NOT_FOUND", 'format id not present');
144 is_deeply($row->get_format($h), {
•145 GT => [3, 3, 3, 3, 2, 4],
146 HQ => [10, 10, 10, 10, 3, 3]
147 }, "format read");
148
149 ok $row = $v->next(), "Next row";
150 is $row->chromosome($h), "19", "Chromosome value read" ;
151 is $row->position(), "112", "Position value read" ;
152 is $row->quality(), "10", "Quality value read" ;
153 is $row->reference(), "A", "Reference value read" ;
154 is $row->num_alleles(), 1, "Num Alleles" ;
155 is $row->is_snp(), 1, "Is SNP" ;
156 my $a_team = $row->get_alleles() ;
157 isa_ok($a_team, 'ARRAY');
158 is_deeply $a_team, ['G'], 'alleles are correct';
159 is $row->num_filters(), 1, "Num Filters OK" ;
160 is $row->has_filter($h,"PASS"), 1, "PASS Filter present" ;
161 is $row->has_filter($h,"DP50"), 0, "Actual Filter absent" ;
162 is $row->has_filter($h,"sdkjsdf"), -1, "Made up filter not existing" ;
163 $info_result = $row->get_info($h);
164 is_deeply($info_result, {}, 'info read correctly');
165
166 # format related tests
•167 is_deeply($row->get_format($h, "GT"), [3, 3, 3, 3, 2, 4], 'format int read');
168 is_deeply($row->get_format($h, "HQ"), [10, 10, 10, 10, 3, 3], 'format int read');
169 is($row->get_format($h, "INVALID"), "ID_NOT_FOUND", 'format id not present');
170 is_deeply($row->get_format($h), {
•171 GT => [3, 3, 3, 3, 2, 4],
172 HQ => [10, 10, 10, 10, 3, 3]
173 }, "format read");
174
175 # Format and genotype tests
176 ok $row = $v->next(), "Next row";
177 is $row->chromosome($h), "20", "Chromosome value read" ;
178 is $row->get_format_type($h,"DP"), "Integer", "int format type correct";
•179 is_deeply($row->get_format($h, "GT"), [3,3,5,3,4,4], "format int read");
180 is_deeply($row->get_format($h, "GQ"), [48,48,43], 'format int read');
181 is_deeply($row->get_format($h, "DP"), [1,8,5], 'format int read');
182 note "!!! Not sure this is correct !!!";
183 is_deeply($row->get_format($h, "HQ"), [51,51,51,51,'-2147483648','-2147483648'], 'format int read');
184 is_deeply($row->get_format($h), {
•185 GT => [3,3,5,3,4,4],
186 GQ => [48,48,43],
187 DP => [1,8,5],
188 HQ => [51,51,51,51,'-2147483648','-2147483648']
189 }, "format read");
190
191 my $fmt_result = $row->get_genotypes($h) ;
192 isa_ok($fmt_result, 'ARRAY');
193 #TODO resolve how these translate to the strings in htslib
•194 is_deeply $fmt_result, [3,3,5,3,4,4], 'genotypes read correctly' ;
195 is $row->get_format($h,"IDONTEXIST"), 'ID_NOT_FOUND', 'format id not found';
196 is $row->get_info($h,"IDONTEXIST"), 'ID_NOT_FOUND', 'info id not found';
197 $info_result = $row->get_info($h);
198 is_deeply($info_result, { NS => [3],
199 DP => [14],
200 AF => [0.5],
201 DB => [1],
202 H2 => [1]
203 }, 'info read correctly');
204
205 # Query tests
206 ok my $iter = $v->query("20:1000000-1231000"), "can query a region";
207 ok $row = $iter->next, "can get a value from the iterator";
208 # 20 1110696 rs6040355 A G,T 67 PASS NS=2;DP=10;AF=0.333,0.667;AA=T;DB GT:GQ:DP:HQ 1|2:21:6:23,27 2|1:2:0:18,2 2/2:35:4:.,.
209 is($row->chromosome($h), 20, 'chr');
210 is($row->position, 1110696, 'position');
211 is($row->id, 'rs6040355', 'id');
212 is($row->reference, 'A', 'reference');
213 is_deeply($row->get_info($h, 'NS'), [2], 'info');
214 is_deeply($row->get_info($h, 'DP'), [10], 'info');
215 is_deeply($row->get_info($h, 'AA'), ['T'], 'info');
216 is_deeply($row->get_info($h, 'DB'), [1], 'info');
217 ok $row = $iter->next, "can get a value from the iterator";
218 # 20 1230237 . T . 47 PASS NS=3;DP=13;AA=T GT:GQ:DP:HQ 0|0:54:.:56,60 0|0:48:4:51,51 0/0:61:2:.,.
219 is($row->chromosome($h), 20, 'chr');
220 is($row->position, 1230237, 'position');
221 is($row->id, '.', 'id');
222 is($row->reference, 'T', 'reference');
223 is_deeply($row->get_info($h), {
224 NS => [3],
225 DP => [13],
226 AA => ['T']}, 'info');
227 ok !$iter->next, "no more results";
228
229 $v->close();
230 }
231
232
233 {
234 # Test standard functions on a BCF file
235 ok my $v = Bio::DB::HTS::VCF->new( filename => $Bin . "/data/test.bcf" ), "BCF file open";
236 is $v->num_variants(), 9, 'correct number of variants identified in file';
237
238 my $h = $v->header();
239 my $header_str = <<"HEADER";
240 ##fileformat=VCFv4.0
241 ##FILTER=<ID=PASS,Description="All filters passed">
242 ##fileDate=20090805
243 ##source=myImputationProgramV3.1
244 ##reference=1000GenomesPilot-NCBI36
245 ##phasing=partial
246 ##INFO=<ID=NS,Number=1,Type=Integer,Description="Number of Samples With Data">
247 ##INFO=<ID=AN,Number=1,Type=Integer,Description="Total number of alleles in called genotypes">
248 ##INFO=<ID=AC,Number=.,Type=Integer,Description="Allele count in genotypes, for each ALT allele, in the same order as listed">
249 ##INFO=<ID=DP,Number=1,Type=Integer,Description="Total Depth">
250 ##INFO=<ID=AF,Number=.,Type=Float,Description="Allele Frequency">
251 ##INFO=<ID=AA,Number=1,Type=String,Description="Ancestral Allele">
252 ##INFO=<ID=TT,Number=0,Type=String,Description="Test">
253 ##INFO=<ID=DB,Number=0,Type=Flag,Description="dbSNP membership, build 129">
254 ##INFO=<ID=H2,Number=0,Type=Flag,Description="HapMap2 membership">
255 ##FILTER=<ID=q10,Description="Quality below 10">
256 ##FILTER=<ID=s50,Description="Less than 50% of samples have data">
257 ##FILTER=<ID=MQ45,Description="MQ45 info">
258 ##FILTER=<ID=DP50,Description="DP50 info">
259 ##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
260 ##FORMAT=<ID=GQ,Number=1,Type=Integer,Description="Genotype Quality">
261 ##FORMAT=<ID=DP,Number=1,Type=Integer,Description="Read Depth">
262 ##FORMAT=<ID=HQ,Number=2,Type=Integer,Description="Haplotype Quality">
263 ##ALT=<ID=DEL:ME:ALU,Description="Deletion of ALU element">
264 ##ALT=<ID=CNV,Description="Copy number variable region">
265 ##contig=<ID=19>
266 ##contig=<ID=20>
267 ##contig=<ID=X>
268 ##bcftools_viewVersion=1.3.1+htslib-1.3.1
269 ##bcftools_viewCommand=view -o test.bcf -O b test.vcf.gz
270 #CHROM POS ID REF ALT QUAL FILTER INFO FORMAT NA00001 NA00002 NA00003
271 HEADER
272
273 is($h->fmt_text, $header_str, "Header formatted string");
274 is $h->version(), "VCFv4.0", "VCF Header version matches" ;
275 is $h->num_samples(), 3, "Number of samples" ;
276 is_deeply $h->get_sample_names(), ['NA00001','NA00002','NA00003'], "sample names correct" ;
277 is $h->num_seqnames(), 3, "Number of seqnames" ;
278 is_deeply $h->get_seqnames(), ['19','20','X'], "sequence names correct" ;
279
280 ok my $row = $v->next(), "Next row";
281 is $row->chromosome($h), "19", "Chromosome value read" ;
282 is $row->position(), "111", "Position value read" ;
283 is $row->id(), "testid", "ID value read" ;
284 is $row->num_filters(), 2, "Num Filters OK" ;
285 is $row->has_filter($h,"DP50"), 1, "Actual Filter present" ;
286 is $row->has_filter($h,"."), 0, "PASS filter absent" ;
287 is $row->get_variant_type(1),1, "Variant type matches" ;
288
289 # info related tests
290 my $info_result ;
291 $info_result = $row->get_info($h);
292 is_deeply($info_result, { AF => [0.5], DB => [1], DP => [14], H2 => [1], NS => [3], TT => ['TESTSTRING'] }, 'info read correctly');
293 $info_result = $row->get_info($h,"DB") ;
294 isa_ok($info_result, 'ARRAY');
295 is_deeply $info_result, [1], 'info flag read correctly';
296 is $row->get_info_type($h,"DB"), "Flag", "info flag type correct" ;
297
298 $info_result = $row->get_info($h,"AF") ;
299 isa_ok($info_result, 'ARRAY');
300 is_deeply $info_result, [0.5], 'info float read correctly';
301 is $row->get_info_type($h,"AF"), "Float", "info float type correct" ;
302
303 $info_result = $row->get_info($h,"TT") ;
304 isa_ok($info_result, 'ARRAY');
305 is_deeply $info_result, ["TESTSTRING"], 'info string read correctly';
306 is $row->get_info_type($h,"TT"), "String", "info String type correct" ;
307 $info_result = $row->get_info($h,"NS") ;
308 isa_ok($info_result, 'ARRAY');
309 is_deeply $info_result, [3], 'info ints read correctly';
310 is $row->get_info_type($h,"NS"), "Integer", "info int type correct" ;
311
312 # format related tests
•313 is_deeply($row->get_format($h, "GT"), [3, 3, 3, 3, 2, 4], 'format int read');
314 is_deeply($row->get_format($h, "HQ"), [10, 10, 10, 10, 3, 3], 'format int read');
315 is($row->get_format($h, "INVALID"), "ID_NOT_FOUND", 'format ID not present');
316 is_deeply($row->get_format($h), {
•317 GT => [3, 3, 3, 3, 2, 4],
318 HQ => [10, 10, 10, 10, 3, 3]
319 }, "format read");
320
321 ok $row = $v->next(), "Next row";
322 is $row->chromosome($h), "19", "Chromosome value read" ;
323 is $row->position(), "112", "Position value read" ;
324 is $row->quality(), "10", "Quality value read" ;
325 is $row->reference(), "A", "Reference value read" ;
326 is $row->num_alleles(), 1, "Num Alleles" ;
327 is $row->is_snp(), 1, "Is SNP" ;
328 my $a_team = $row->get_alleles() ;
329 isa_ok($a_team, 'ARRAY');
330 is_deeply $a_team, ['G'], 'alleles are correct';
331 is $row->num_filters(), 1, "Num Filters OK" ;
332 is $row->has_filter($h,"PASS"), 1, "PASS Filter present" ;
333 is $row->has_filter($h,"DP50"), 0, "Actual Filter absent" ;
334 is $row->has_filter($h,"sdkjsdf"), -1, "Made up filter not existing" ;
335 $info_result = $row->get_info($h);
336 is_deeply($info_result, {}, 'info read correctly');
337
338 # format related tests
•339 is_deeply($row->get_format($h, "GT"), [3, 3, 3, 3, 2, 4], 'format int read');
340 is_deeply($row->get_format($h, "HQ"), [10, 10, 10, 10, 3, 3], 'format int read');
341 is($row->get_format($h, "INVALID"), "ID_NOT_FOUND", 'format ID not present');
342 is_deeply($row->get_format($h), {
•343 GT => [3, 3, 3, 3, 2, 4],
344 HQ => [10, 10, 10, 10, 3, 3]
345 }, "format read");
346
347 #Format and genotype tests
348 ok $row = $v->next(), "Next row";
349 is $row->chromosome($h), "20", "Chromosome value read" ;
350 is $row->get_format_type($h,"DP"), "Integer", "int format type correct" ;
•351 is_deeply($row->get_format($h, "GT"), [3,3,5,3,4,4], 'format int read');
352 is_deeply($row->get_format($h, "GQ"), [48,48,43], 'format int read');
353 is_deeply($row->get_format($h, "DP"), [1,8,5], 'format int read');
354 note "!!! Not sure this is correct !!!";
355 is_deeply($row->get_format($h, "HQ"), [51,51,51,51,'-2147483648','-2147483648'], 'format int read');
356 is_deeply($row->get_format($h), {
•357 GT => [3,3,5,3,4,4],
358 GQ => [48,48,43],
359 DP => [1,8,5],
360 HQ => [51,51,51,51,'-2147483648','-2147483648']
361 }, "format read");
362
363 my $fmt_result = $row->get_genotypes($h) ;
364 isa_ok($fmt_result, 'ARRAY');
365 # TODO resolve how these translate to the strings in htslib
•366 is_deeply $fmt_result, [3,3,5,3,4,4], 'genotypes read correctly' ;
367 is $row->get_format($h,"IDONTEXIST"), 'ID_NOT_FOUND', 'format id not found';
368 is $row->get_info($h,"IDONTEXIST"), 'ID_NOT_FOUND', 'info id not found';
369 $info_result = $row->get_info($h);
370 is_deeply($info_result, { NS => [3],
371 DP => [14],
372 AF => [0.5],
373 DB => [1],
374 H2 => [1]
375 }, 'info read correctly');
376
377 # Query tests
378 ok my $iter = $v->query("20:1000000-1231000"), "can query a region";
379 ok $row = $iter->next, "can get a value from the iterator";
380 # 20 1110696 rs6040355 A G,T 67 PASS NS=2;DP=10;AF=0.333,0.667;AA=T;DB GT:GQ:DP:HQ 1|2:21:6:23,27 2|1:2:0:18,2 2/2:35:4:.,.
381 is($row->chromosome($h), 20, 'chr');
382 is($row->position, 1110696, 'position');
383 is($row->id, 'rs6040355', 'id');
384 is($row->reference, 'A', 'reference');
385 is_deeply($row->get_info($h, 'NS'), [2], 'info');
386 is_deeply($row->get_info($h, 'DP'), [10], 'info');
387 is_deeply($row->get_info($h, 'AA'), ['T'], 'info');
388 is_deeply($row->get_info($h, 'DB'), [1], 'info');
389 ok $row = $iter->next, "can get a value from the iterator";
390 # 20 1230237 . T . 47 PASS NS=3;DP=13;AA=T GT:GQ:DP:HQ 0|0:54:.:56,60 0|0:48:4:51,51 0/0:61:2:.,.
391 is($row->chromosome($h), 20, 'chr');
392 is($row->position, 1230237, 'position');
393 is($row->id, '.', 'id');
394 is($row->reference, 'T', 'reference');
395 is_deeply($row->get_info($h), {
396 NS => [3],
397 DP => [13],
398 AA => ['T']}, 'info');
399 ok !$iter->next, "no more results";
400
401 $v->close();
402 }