@@ -287,18 +287,15 @@ else if ( clazz.equals(int[].class) ) {
287287 *
288288 * @param vc target variant context.
289289 * @param numAltAllelesToKeep number of alt alleles to keep.
290+ * @param ensureReturnContainsAlt make sure the alleles returned include an alternate, even if it's not in the most likely genotype
290291 * @return the list of alleles to keep, including the reference and {@link Allele#NON_REF_ALLELE} if present
291292 *
292293 */
293294 public static List <Allele > calculateMostLikelyAlleles (final VariantContext vc , final int defaultPloidy ,
294- final int numAltAllelesToKeep ) {
295+ final int numAltAllelesToKeep , boolean ensureReturnContainsAlt ) {
295296 Utils .nonNull (vc , "vc is null" );
296297 Utils .validateArg (defaultPloidy > 0 , () -> "default ploidy must be > 0 but defaultPloidy=" + defaultPloidy );
297298 Utils .validateArg (numAltAllelesToKeep > 0 , () -> "numAltAllelesToKeep must be > 0, but numAltAllelesToKeep=" + numAltAllelesToKeep );
298- //allow PLs or GPs (as for GATK-DRAGEN), but we need some kind of genotype data
299- Utils .validateArg (vc .getGenotypes ().stream ().anyMatch (g -> g .hasPL () || g .hasExtendedAttribute (VCFConstants .GENOTYPE_POSTERIORS_KEY )), () -> "Most likely alleles cannot be calculated without likelihoods" );
300- //NOTE: this is used in the reblocking case when we have a hom-ref GT and real ALTs
301- final boolean allHomRefData = vc .getGenotypes ().stream ().allMatch (g -> g .hasPL () && g .getPL ()[0 ] == 0 ); //PL=[0,0,0] is okay, we just don't want confident variants
302299
303300 final boolean hasSymbolicNonRef = vc .hasAllele (Allele .NON_REF_ALLELE );
304301 final int numberOfAllelesThatArentProperAlts = hasSymbolicNonRef ? 2 : 1 ;
@@ -308,11 +305,7 @@ public static List<Allele> calculateMostLikelyAlleles(final VariantContext vc, f
308305 return vc .getAlleles ();
309306 }
310307
311- final double [] likelihoodSums = calculateLikelihoodSums (vc , defaultPloidy , allHomRefData );
312- if (MathUtils .sum (likelihoodSums ) == 0.0 && !allHomRefData ) {
313- throw new IllegalStateException ("No likelihood sum exceeded zero -- method was called for variant data " +
314- "with no variant information." );
315- }
308+ final double [] likelihoodSums = calculateLikelihoodSums (vc , defaultPloidy , ensureReturnContainsAlt );
316309 return filterToMaxNumberOfAltAllelesBasedOnScores (numAltAllelesToKeep , vc .getAlleles (), likelihoodSums );
317310 }
318311
@@ -342,17 +335,20 @@ public static List<Allele> filterToMaxNumberOfAltAllelesBasedOnScores(int numAlt
342335 *
343336 * Since GLs are log likelihoods, this quantity is thus
344337 * SUM_{samples whose likeliest genotype contains this alt allele} log(likelihood alt / likelihood hom ref)
338+ * @param vc
339+ * @param defaultPloidy
340+ * @param countAllelesWithoutHomRef true if we know the input is hom-ref, but we still want to know the most likely ALT
345341 */
346342 @ VisibleForTesting
347- static double [] calculateLikelihoodSums (final VariantContext vc , final int defaultPloidy , final boolean allHomRefData ) {
343+ static double [] calculateLikelihoodSums (final VariantContext vc , final int defaultPloidy , final boolean countAllelesWithoutHomRef ) {
348344 final double [] likelihoodSums = new double [vc .getNAlleles ()];
349345 for ( final Genotype genotype : vc .getGenotypes ().iterateInSampleNameOrder () ) {
350346 final GenotypeLikelihoods gls = genotype .getLikelihoods ();
351347 if (gls == null ) {
352348 continue ;
353349 }
354350 final double [] glsVector = gls .getAsVector ();
355- final int indexOfMostLikelyVariantGenotype = MathUtils .maxElementIndex (glsVector , allHomRefData ? 1 : 0 , glsVector .length );
351+ final int indexOfMostLikelyVariantGenotype = MathUtils .maxElementIndex (glsVector , countAllelesWithoutHomRef ? 1 : 0 , glsVector .length );
356352 final double GLDiffBetweenRefAndBestVariantGenotype = Math .abs (glsVector [indexOfMostLikelyVariantGenotype ] - glsVector [PL_INDEX_OF_HOM_REF ]);
357353 final int ploidy = genotype .getPloidy () > 0 ? genotype .getPloidy () : defaultPloidy ;
358354
0 commit comments