Skip to content

Commit 082160d

Browse files
committed
Remove CNV:TR-related INFO tags when empty
If all the alleles referenced by CNV:TR related INFO tags are removed, the INFO tags will no longer have any values and so also need to be removed.
1 parent 8b6689c commit 082160d

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

test/test-vcf-api.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,7 @@ void test_bcf_remove_allele_set(void)
982982
"5\t112000\t.\tC\t<CNV:TR>,<CNV:TR>\t.\tPASS\tRN=2,1;RUS=CAG,TTG,CA;RUL=3,3,2;RB=12,6,6;RUC=4,2,3;RUB=3,3,3,3,3,3,2,2,2;SVLEN=18,6",
983983
"5\t113000\t.\tT\tC,A\t.\tPASS\tAC=90,1;AD=6,5,6;AF=0.009,0.0001;VL_A_STR_INFO=alt_c,alt_a;VL_R_STR_INFO=ref,alt_c,alt_a\tGT:LAA:LAD:LEC:LPL:VL_LA_STR_FMT:VL_LG_STR_FMT:VL_LR_STR_FMT\t0/0:.:3:.:0:.:gt_00:ref\t0/1:1,2:3,2,0:44,27:114,0,15,35,73,113:alt_c,alt_a:gt_00,gt_01,gt_11,gt_02,gt_12,gt_22:ref,alt_c,alt_a\t1/1:1:0,3:46:110,15,0:alt_c:gt_00,gt_01,gt_11:ref,alt_c",
984984
"5\t114000\t.\tT\tC,A\t.\tPASS\tAC=90,1;AD=6,5,6;AF=0.009,0.0001;VL_A_STR_INFO=alt_c,alt_a;VL_R_STR_INFO=ref,alt_c,alt_a\tGT:LAA:LAD:LEC:LPL:VL_LA_STR_FMT:VL_LG_STR_FMT:VL_LR_STR_FMT\t0/0:.:3:.:0:.:gt_00:ref\t0/1:1,2:3,2,0:44,27:114,0,15,35,73,113:alt_c,alt_a:gt_00,gt_01,gt_11,gt_02,gt_12,gt_22:ref,alt_c,alt_a\t1/1:1:0,3:46:110,15,0:alt_c:gt_00,gt_01,gt_11:ref,alt_c",
985+
"5\t115000\t.\tC\t<CNV:TR>,<CNV:TR>\t.\tPASS\tRN=2,1;RUS=CAG,TTG,CA;RUL=3,3,2;RB=12,6,6;RUC=4,2,3;RUB=3,3,3,3,3,3,2,2,2;SVLEN=18,6",
985986
};
986987
const char * expected[] = {
987988
// 2nd allele removed
@@ -995,6 +996,7 @@ void test_bcf_remove_allele_set(void)
995996
"5\t113000\t.\tT\tA\t.\tPASS\tAC=1;AD=6,6;AF=0.0001;VL_A_STR_INFO=alt_a;VL_R_STR_INFO=ref,alt_a\tGT:LAA:LAD:LEC:LPL:VL_LA_STR_FMT:VL_LG_STR_FMT:VL_LR_STR_FMT\t0/0:.:3:.:0:.:gt_00:ref\t0/.:1:3,0:27:114,35,113:alt_a:gt_00,gt_02,gt_22:ref,alt_a\t./.:.:0:.:110:.:gt_00:ref",
996997
// Both alleles removed
997998
"5\t114000\t.\tT\t.\t.\tPASS\tAD=6;VL_R_STR_INFO=ref\tGT:LAA:LAD:LEC:LPL:VL_LA_STR_FMT:VL_LG_STR_FMT:VL_LR_STR_FMT\t0/0:.:3:.:0:.:gt_00:ref\t0/.:.:3:.:114:.:gt_00:ref\t./.:.:0:.:110:.:gt_00:ref",
999+
"5\t115000\t.\tC\t.\t.\tPASS\t.",
9981000
};
9991001

10001002
kstring_t kstr = KS_INITIALIZE;
@@ -1021,7 +1023,7 @@ void test_bcf_remove_allele_set(void)
10211023
kbs_clear(rm_set);
10221024
if (rec->pos == 113000 - 1) {
10231025
kbs_insert(rm_set, 1);
1024-
} else if (rec->pos == 114000 - 1) {
1026+
} else if (rec->pos >= 114000 - 1) {
10251027
kbs_insert(rm_set, 1);
10261028
kbs_insert(rm_set, 2);
10271029
} else {

vcfutils.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,18 @@ static int fixup_info_length_code(bcf_info_t *info)
404404
return 0;
405405
}
406406

407+
static int mark_for_removal(bcf_info_t *info)
408+
{
409+
if (info->vptr_free)
410+
{
411+
free(info->vptr - info->vptr_off);
412+
info->vptr_free = 0;
413+
}
414+
info->vptr = NULL;
415+
info->vptr_off = info->vptr_len = 0;
416+
return 0;
417+
}
418+
407419
// Remove integer CNV:TR-related tag data
408420
// Returns 0 on success
409421
// 1 if the tag had an unexpected type or number of elements
@@ -468,6 +480,10 @@ static int trim_int_cnv_tr_int_tags(bcf_info_t *info,
468480
new_pos += byte_len;
469481
new_total += n_items;
470482
}
483+
484+
if (new_total == 0)
485+
return mark_for_removal(info);
486+
471487
info->vptr_len = new_pos;
472488
info->len = new_total;
473489
if (info->len == 1)
@@ -525,6 +541,9 @@ static int trim_int_cnv_tr_str_tags(bcf_info_t *info,
525541
orig_pos += end - start;
526542
new_pos += end - start;
527543
}
544+
if (new_pos == 0)
545+
return mark_for_removal(info);
546+
528547
if (new_pos < orig_pos)
529548
{
530549
info->vptr[new_pos] = '\0';
@@ -620,10 +639,10 @@ static int fixup_cnv_tr_info_tags(const bcf_hdr_t *header, bcf1_t *line,
620639
return res;
621640
}
622641

623-
// Check if storage had to be reallocated. This can only happen if
624-
// the length code stored before info->vptr was too small, which
625-
// should hopefully never be the case.
626-
if (info->vptr - info->vptr_off != orig_ptr)
642+
// Check if the info tag was removed, or storage had to be reallocated.
643+
// The latter can only happen if the length code stored before info->vptr
644+
// was too small, which should hopefully never be the case.
645+
if (!info->vptr || info->vptr - info->vptr_off != orig_ptr)
627646
line->d.shared_dirty |= BCF1_DIRTY_INF;
628647
}
629648
// Now do RUC, if present

0 commit comments

Comments
 (0)