Skip to content

Commit

Permalink
VCF codec should handle multiple missing GL fields (#1372)
Browse files Browse the repository at this point in the history
* VCF codec should handle multiple missing GL fields
* throw error for partial missing values for GL field
  • Loading branch information
mitochon authored and Yossi Farjoun committed Jun 17, 2019
1 parent 58199e6 commit b804a47
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public static double getGQLog10FromLikelihoods(int iOfChoosenGenotype, double[]
}

private final static double[] parsePLsIntoLikelihoods(String likelihoodsAsString_PLs) {
if ( !likelihoodsAsString_PLs.equals(VCFConstants.MISSING_VALUE_v4) ) {
if ( likelihoodsAsString_PLs != null && !likelihoodsAsString_PLs.equals(VCFConstants.MISSING_VALUE_v4) ) {
String[] strings = likelihoodsAsString_PLs.split(",");
double[] likelihoodsAsVector = new double[strings.length];
try {
Expand All @@ -266,10 +266,21 @@ private final static double[] parseDeprecatedGLString(String GLString) {
if ( !GLString.equals(VCFConstants.MISSING_VALUE_v4) ) {
String[] strings = GLString.split(",");
double[] likelihoodsAsVector = new double[strings.length];
int missing = 0;
for ( int i = 0; i < strings.length; i++ ) {
likelihoodsAsVector[i] = Double.parseDouble(strings[i]);
if (strings[i].equals(VCFConstants.MISSING_VALUE_v4)) {
missing++;
} else {
likelihoodsAsVector[i] = Double.parseDouble(strings[i]);
}
}
if (missing == 0) {
return likelihoodsAsVector;
} else if (likelihoodsAsVector.length == missing) {
return null; // array of missing values
} else {
throw new TribbleException("partial missing values for GL field");
}
return likelihoodsAsVector;
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ public void testFromString2() {
Assert.assertEquals(gl.getAsString(), vPLString);
}

@Test
public void testFromStringMultipleMissing() {
String missingWithPloidy2AltAllele1 = ".,.,.";
GenotypeLikelihoods gl = GenotypeLikelihoods.fromGLField(missingWithPloidy2AltAllele1);
Assert.assertNull(gl.getAsPLs());
}

@Test (expectedExceptions = TribbleException.class)
public void testFromStringOneMissing() {
String oneMissingFromPloidy2AltAllele1 = "-3.0,.,-1.2";
GenotypeLikelihoods.fromGLField(oneMissingFromPloidy2AltAllele1);
}

@Test (expectedExceptions = TribbleException.class)
public void testErrorBadFormat() {
GenotypeLikelihoods gl = GenotypeLikelihoods.fromPLField("adf,b,c");
Expand Down

0 comments on commit b804a47

Please sign in to comment.