@@ -82,6 +82,7 @@ args_t *args = NULL;
82
82
#define GT_CUSTOM (1<<10)
83
83
#define GT_X_VAF (1<<11)
84
84
#define GT_RAND (1<<12)
85
+ #define GT_INVPHASE (1<<13)
85
86
86
87
#define MINOR_ALLELE -1
87
88
#define MAJOR_ALLELE -2
@@ -113,6 +114,7 @@ const char *usage(void)
113
114
" X .. allele with bigger read depth as determined from FMT/AD\n"
114
115
" p .. phase genotype (0/1 becomes 0|1)\n"
115
116
" u .. unphase genotype and sort by allele (1|0 becomes 0/1)\n"
117
+ " i .. invert the genotype phase (0|1 becomes 1|0)\n"
116
118
"Usage: bcftools +setGT [General Options] -- [Plugin Options]\n"
117
119
"Options:\n"
118
120
" run \"bcftools plugin\" for a list of common options\n"
@@ -235,6 +237,7 @@ int init(int argc, char **argv, bcf_hdr_t *in, bcf_hdr_t *out)
235
237
if ( strchr (optarg ,'X' ) ) args -> new_mask |= GT_X_VAF ;
236
238
if ( strchr (optarg ,'p' ) ) args -> new_mask |= GT_PHASED ;
237
239
if ( strchr (optarg ,'u' ) ) args -> new_mask |= GT_UNPHASED ;
240
+ if ( strchr (optarg ,'i' ) ) args -> new_mask |= GT_INVPHASE ;
238
241
if ( !strncmp (optarg ,"c:" ,2 ) ) { args -> new_mask |= GT_CUSTOM ; args -> custom .gt_str = optarg ; }
239
242
if ( args -> new_mask == 0 ) error ("Unknown parameter to --new-gt: %s\n" , optarg );
240
243
break ;
@@ -364,6 +367,21 @@ static inline int unphase_gt(int32_t *ptr, int ngts)
364
367
return changed ;
365
368
}
366
369
370
+ // invert the phase for a single sample, ngts is the ploidy
371
+ static inline int invert_phase_gt (int32_t * ptr , int ngts )
372
+ {
373
+ int gt ;
374
+ if ( ngts != 2 ) return 0 ; // don't invert the phase for sample ploidy != 2
375
+ if ( ptr [0 ]== bcf_int32_vector_end || ptr [1 ]== bcf_int32_vector_end ) return 0 ;
376
+ gt = bcf_gt_allele (ptr [1 ]);
377
+ if (bcf_gt_is_phased (ptr [1 ]))
378
+ ptr [1 ] = bcf_gt_phased (bcf_gt_allele (ptr [0 ]));
379
+ else
380
+ ptr [1 ] = bcf_gt_unphased (bcf_gt_allele (ptr [0 ]));
381
+ ptr [0 ] = bcf_gt_unphased (gt ); // first gt doesn't carry the phased flag per BCF specifications
382
+ return 2 ;
383
+ }
384
+
367
385
// sets GT for a single sample, ngts is the ploidy, allele
368
386
static inline int set_gt (int32_t * ptr , int ngts , int allele )
369
387
{
@@ -543,6 +561,8 @@ bcf1_t *process(bcf1_t *rec)
543
561
}
544
562
else if ( args -> new_mask & GT_X_VAF )
545
563
changed += set_gt (ptr , ngts , args -> xarr [i ]);
564
+ else if ( args -> new_mask & GT_INVPHASE )
565
+ changed += invert_phase_gt (ptr , ngts );
546
566
else
547
567
changed += set_gt (ptr , ngts , args -> new_gt );
548
568
}
@@ -584,6 +604,8 @@ bcf1_t *process(bcf1_t *rec)
584
604
}
585
605
else if ( args -> new_mask & GT_X_VAF )
586
606
changed += set_gt (args -> gts + i * ngts , ngts , args -> xarr [i ]);
607
+ else if ( args -> new_mask & GT_INVPHASE )
608
+ changed += invert_phase_gt (args -> gts , ngts );
587
609
else
588
610
changed += set_gt (args -> gts + i * ngts , ngts , args -> new_gt );
589
611
}
@@ -620,6 +642,8 @@ bcf1_t *process(bcf1_t *rec)
620
642
}
621
643
else if ( args -> new_mask & GT_X_VAF )
622
644
changed += set_gt (ptr , ngts , args -> xarr [i ]);
645
+ else if ( args -> new_mask & GT_INVPHASE )
646
+ changed += invert_phase_gt (ptr , ngts );
623
647
else
624
648
changed += set_gt (ptr , ngts , args -> new_gt );
625
649
}
0 commit comments