Skip to content

Commit a53419b

Browse files
committed
More ploidy fixes to #2122
1 parent 8e0867e commit a53419b

File tree

8 files changed

+76
-13
lines changed

8 files changed

+76
-13
lines changed

ploidy.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2014-2016 Genome Research Ltd.
2+
Copyright (C) 2014-2025 Genome Research Ltd.
33
44
Author: Petr Danecek <[email protected]>
55
@@ -58,7 +58,7 @@ int ploidy_parse(const char *line, char **chr_beg, char **chr_end, uint32_t *beg
5858
ploidy_t *ploidy = (ploidy_t*) usr;
5959
void *sex2id = ploidy->sex2id;
6060

61-
// Check for special case of default ploidy "* * * <sex> <ploidy>"
61+
// Check for special case of default ploidy "* * * SEX PLOIDY"
6262
int default_ploidy_def = 0;
6363

6464
char *ss = (char*) line;
@@ -112,7 +112,7 @@ int ploidy_parse(const char *line, char **chr_beg, char **chr_end, uint32_t *beg
112112
// Special case, chr="*" stands for a default value
113113
if ( default_ploidy_def )
114114
{
115-
ploidy->sex2dflt[ploidy->nsex-1] = sp->ploidy;
115+
ploidy->sex2dflt[sp->sex] = sp->ploidy;
116116
return -1;
117117
}
118118

@@ -266,3 +266,23 @@ int ploidy_min(ploidy_t *ploidy)
266266
return ploidy->dflt < ploidy->min ? ploidy->dflt : ploidy->min;
267267
}
268268

269+
char *ploidy_format(ploidy_t *ploidy)
270+
{
271+
kstring_t str = {0,0,0};
272+
273+
regitr_t *itr = regitr_init(ploidy->idx);
274+
while ( regitr_loop(itr) )
275+
{
276+
int id = regitr_payload(itr,sex_ploidy_t).sex;
277+
int pld = regitr_payload(itr,sex_ploidy_t).ploidy;
278+
ksprintf(&str,"%s\t%d\t%d\t%s\t%d\n", itr->seq, itr->beg+1, itr->end+1, ploidy->id2sex[id],pld);
279+
}
280+
regitr_destroy(itr);
281+
282+
int i;
283+
for (i=0; i<ploidy->nsex; i++)
284+
ksprintf(&str,"*\t*\t*\t%s\t%d\n", ploidy->id2sex[i],ploidy->sex2dflt[i]);
285+
286+
return str.s;
287+
}
288+

ploidy.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/*
2-
Copyright (C) 2014-2015 Genome Research Ltd.
1+
/*
2+
Copyright (C) 2014-2025 Genome Research Ltd.
33
44
Author: Petr Danecek <[email protected]>
55
@@ -9,10 +9,10 @@
99
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1010
copies of the Software, and to permit persons to whom the Software is
1111
furnished to do so, subject to the following conditions:
12-
12+
1313
The above copyright notice and this permission notice shall be included in
1414
all copies or substantial portions of the Software.
15-
15+
1616
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1717
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1818
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -81,8 +81,8 @@ void ploidy_destroy(ploidy_t *ploidy);
8181
* @param seq: chromosome name
8282
* @param pos: 0-based position
8383
* @param sex2ploidy: if not NULL, array will be filled with mapping from sex id to ploidy
84-
* @param min: if not NULL, minimum encountered encountered will be set
85-
* @param max: if not NULL, maximum encountered encountered will be set
84+
* @param min: if not NULL, minimum encountered ploidy will be set
85+
* @param max: if not NULL, maximum encountered ploidy will be set
8686
*
8787
* Returns 1 if the position is listed in the regions or 0 otherwise.
8888
*/
@@ -125,5 +125,8 @@ regidx_t *ploidy_regions(ploidy_t *ploidy);
125125
int ploidy_max(ploidy_t *ploidy);
126126
int ploidy_min(ploidy_t *ploidy);
127127

128+
/** Create a parseable ploidy file for debugging. The string must be free()-ed by the caller */
129+
char *ploidy_format(ploidy_t *ploidy);
130+
128131
#endif
129132

test/call-ploidy.1.1.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
##fileformat=VCFv4.2
2+
##FILTER=<ID=PASS,Description="All filters passed">
3+
##reference=file://ref.fa
4+
##contig=<ID=chr1>
5+
##contig=<ID=chr2>
6+
##contig=<ID=chr3>
7+
##FORMAT=<ID=PL,Number=G,Type=Integer,Description="List of Phred-scaled genotype likelihoods">
8+
##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
9+
##INFO=<ID=AC,Number=A,Type=Integer,Description="Allele count in genotypes for each ALT allele, in the same order as listed">
10+
##INFO=<ID=AN,Number=1,Type=Integer,Description="Total number of alleles in called genotypes">
11+
##INFO=<ID=DP4,Number=4,Type=Integer,Description="Number of high-quality ref-forward , ref-reverse, alt-forward and alt-reverse bases">
12+
##INFO=<ID=MQ,Number=1,Type=Integer,Description="Average mapping quality">
13+
#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT smpl1 smpl2
14+
chr1 621 . A . 24.4632 . AN=2;DP4=11,0,1,0;MQ=41 GT . 0/0
15+
chr2 621 . A . 27.9838 . AN=2;DP4=11,0,1,0;MQ=41 GT 0 0
16+
chr3 621 . A . 24.4632 . AN=2;DP4=11,0,1,0;MQ=41 GT 0/0 .

test/call-ploidy.1.ped

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fam1 smpl1 0 0 1
2+
Fam1 smpl2 0 0 2

test/call-ploidy.1.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
chr1 1 16750 1 0
2+
chr2 1 16750 1 1
3+
* * * 1 2
4+
chr1 1 16750 2 2
5+
chr2 1 16750 2 1
6+
* * * 2 0

test/call-ploidy.1.vcf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
##fileformat=VCFv4.2
2+
##reference=file://ref.fa
3+
##contig=<ID=chr1>
4+
##contig=<ID=chr2>
5+
##contig=<ID=chr3>
6+
##INFO=<ID=I16,Number=16,Type=Float,Description="Auxiliary tag used for calling, see description of bcf_callret1_t in bam2bcf.h">
7+
##INFO=<ID=QS,Number=R,Type=Float,Description="Auxiliary tag used for calling">
8+
##FORMAT=<ID=PL,Number=G,Type=Integer,Description="List of Phred-scaled genotype likelihoods">
9+
#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT smpl1 smpl2
10+
chr1 621 . A C,<*> 0 . I16=11,0,1,0,407,15059,37,1369,440,17600,60,3600,252,6032,25,625;QS=8,1,0 PL 0,3,37,3,37,37 0,3,37,3,37,37
11+
chr2 621 . A C,<*> 0 . I16=11,0,1,0,407,15059,37,1369,440,17600,60,3600,252,6032,25,625;QS=8,1,0 PL 0,3,37,3,37,37 0,3,37,3,37,37
12+
chr3 621 . A C,<*> 0 . I16=11,0,1,0,407,15059,37,1369,440,17600,60,3600,252,6032,25,625;QS=8,1,0 PL 0,3,37,3,37,37 0,3,37,3,37,37

test/test.pl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@
430430
run_test(\&test_vcf_head2,$opts,in=>'mpileup.2',out=>'head.1.out',args=>'-s0');
431431
run_test(\&test_vcf_head2,$opts,in=>'mpileup.2',out=>'head.2.out',args=>'-s1');
432432
run_test(\&test_vcf_head2,$opts,in=>'mpileup.2',out=>'head.3.out',args=>'-s2 -h2');
433+
run_test(\&test_vcf_call,$opts,in=>'call-ploidy.1',out=>'call-ploidy.1.1.out',args=>'-m --ploidy-file {PATH}/call-ploidy.1.txt -S {PATH}/call-ploidy.1.ped');
433434
run_test(\&test_vcf_call,$opts,in=>'mpileup',out=>'mpileup.1.out',args=>'-mv');
434435
run_test(\&test_vcf_call,$opts,in=>'mpileup',out=>'mpileup.2.out',args=>'-mg0');
435436
run_test(\&test_vcf_call,$opts,in=>'mpileup',out=>'mpileup.3.out',args=>'-mv -S {PATH}/mpileup.3.samples');

vcfcall.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ static char **parse_ped_samples(args_t *args, call_t *call, char **vals, int nva
239239
else if ( !strcmp(sex,"2") && ploidy_sex2id(args->ploidy,fsex)>=0 ) sex = fsex;
240240
else error("[E::%s] The sex \"%s\" has not been declared in --ploidy/--ploidy-file\n",__func__,sex);
241241
}
242-
243242
lines = add_sample(name2idx, lines, &nlines, &mlines, col_ends[0]+1, sex, &j);
244243
if ( strcmp(col_ends[1]+1,"0") && strcmp(col_ends[2]+1,"0") ) // father and mother
245244
{
@@ -325,10 +324,14 @@ static void set_samples(args_t *args, const char *fn, int is_file)
325324
while ( *se && !isspace(*se) ) se++;
326325
if ( se==ss ) { *xptr = x; error("Could not parse: \"%s\"\n", lines[i]); }
327326

328-
if ( ss[1]==0 && (ss[0]=='0' || ss[0]=='1' || ss[0]=='2') )
329-
args->sample2sex[nsmpl] = -1*(ss[0]-'0');
327+
char *sex = ss;
328+
if ( ploidy_sex2id(args->ploidy,sex)<0 )
329+
{
330+
if ( sex[1]==0 && (sex[0]=='0' || sex[0]=='1' || sex[0]=='2') ) args->sample2sex[nsmpl] = -1*(sex[0]-'0');
331+
else error("[E::%s] The sex \"%s\" has not been declared in --ploidy/--ploidy-file\n",__func__,sex);
332+
}
330333
else
331-
args->sample2sex[nsmpl] = ploidy_add_sex(args->ploidy, ss);
334+
args->sample2sex[nsmpl] = ploidy_add_sex(args->ploidy,sex);
332335

333336
if ( ismpl!=nsmpl ) map_needed = 1;
334337
args->samples_map[nsmpl] = ismpl;

0 commit comments

Comments
 (0)