Java Code Examples for htsjdk.variant.vcf.VCFHeader#getInfoHeaderLine()

The following examples show how to use htsjdk.variant.vcf.VCFHeader#getInfoHeaderLine() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReferenceConfidenceVariantContextMerger.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Comparable<?> parseNumericInfoAttributeValue(final VCFHeader vcfHeader, final String key, final String stringValue) {
    final VCFInfoHeaderLine infoLine = vcfHeader.getInfoHeaderLine(key);
    if (infoLine == null) {
        oneShotHeaderLineLogger.warn(String.format("At least one attribute was found (%s) for which there is no corresponding header line", key));
        if (stringValue.contains(".")) {
            return Double.parseDouble(stringValue);
        } else {
            return Integer.parseInt(stringValue);
        }
    }
    switch (infoLine.getType()) {
        case Integer:
            return Integer.parseInt(stringValue);
        case Float:
            return Double.parseDouble(stringValue);
        default:
            throw new NumberFormatException(
                    String.format(
                            "The VCF header specifies type %s type for INFO attribute key %s, but a numeric value is required",
                            infoLine.getType().name(),
                            key)
            );
    }
}
 
Example 2
Source File: FilterFuncotations.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onTraversalStart() {
    final VCFHeader vcfHeader = getHeaderForVariants();

    final VCFInfoHeaderLine funcotationHeaderLine = vcfHeader.getInfoHeaderLine(VcfOutputRenderer.FUNCOTATOR_VCF_FIELD_NAME);
    if (funcotationHeaderLine != null) {
        funcotationKeys = FuncotatorUtils.extractFuncotatorKeysFromHeaderDescription(funcotationHeaderLine.getDescription());
        outputVcfWriter = createVCFWriter(outputFile);
        vcfHeader.addMetaDataLine(new VCFFilterHeaderLine(FilterFuncotationsConstants.NOT_CLINSIG_FILTER,
                FilterFuncotationsConstants.NOT_CLINSIG_FILTER_DESCRIPTION));
        vcfHeader.addMetaDataLine(new VCFInfoHeaderLine(FilterFuncotationsConstants.CLINSIG_INFO_KEY, 1,
                VCFHeaderLineType.String, FilterFuncotationsConstants.CLINSIG_INFO_KEY_DESCRIPTION));
        outputVcfWriter.writeHeader(vcfHeader);
    } else {
        throw new UserException.BadInput("Could not extract Funcotation keys from " +
                VcfOutputRenderer.FUNCOTATOR_VCF_FIELD_NAME + " field in input VCF header.");
    }

    registerFilters();
}
 
Example 3
Source File: FuncotatorIntegrationTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void assertEqualVariantFiles(final File outputFile, final String eColiExpectedOut) {
    // Get the actual data:
    final Pair<VCFHeader, List<VariantContext>> actualVcfInfo               = VariantContextTestUtils.readEntireVCFIntoMemory(outputFile.getAbsolutePath());
    final List<VariantContext>                  actualVariantContexts       = actualVcfInfo.getRight();
    final VCFHeader                             actualVcfHeader             = actualVcfInfo.getLeft();
    final VCFInfoHeaderLine                     actualFuncotationHeaderLine = actualVcfHeader.getInfoHeaderLine(VcfOutputRenderer.FUNCOTATOR_VCF_FIELD_NAME);

    // Get the expected data:
    final Pair<VCFHeader, List<VariantContext>> expectedVcfInfo               = VariantContextTestUtils.readEntireVCFIntoMemory(new File(eColiExpectedOut).getAbsolutePath());
    final List<VariantContext>                  expectedVariantContexts       = expectedVcfInfo.getRight();
    final VCFHeader                             expectedVcfHeader             = expectedVcfInfo.getLeft();
    final VCFInfoHeaderLine                     expectedFuncotationHeaderLine = expectedVcfHeader.getInfoHeaderLine(VcfOutputRenderer.FUNCOTATOR_VCF_FIELD_NAME);

    // Check that they're equal:
    Assert.assertEquals(actualFuncotationHeaderLine, expectedFuncotationHeaderLine);
    VariantContextTestUtils.assertEqualVariants(actualVariantContexts, expectedVariantContexts);
}
 
Example 4
Source File: VcfToVariant.java    From genomewarp with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static Map<String, ListValue> getInfo(VariantContext vc, VCFHeader header) {
  Map<String, ListValue> toReturn = new HashMap<>();

  for (Map.Entry<String, Object> entry : vc.getAttributes().entrySet()) {
    String currKey = entry.getKey();
    VCFInfoHeaderLine metaData = header.getInfoHeaderLine(currKey);

    // All info fields must have a corresponding header field.
    if (metaData == null) {
      logger.log(Level.WARNING, String.format("Could not find matching VCF header field, "
          + "skipping info field %s", currKey));
      continue;
    }

    Object currObject = entry.getValue();
    ListValue.Builder listValueBuilder = ListValue.newBuilder();

    VCFHeaderLineType type = metaData.getType();
    if (!(currObject instanceof List)) {
      toReturn.put(currKey,
          listValueBuilder.addValues(createTypedValue(type, currObject)).build());
      continue;
    }

    List<Object> currObjectList = (List<Object>) currObject;
    for (Object currObj : currObjectList) {
      listValueBuilder.addValues(createTypedValue(type, currObj));
    }
    toReturn.put(currKey, listValueBuilder.build());
  }

  return toReturn;
}
 
Example 5
Source File: FuncotatorIntegrationTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test(enabled = enableFullScaleValidationTest,
      groups = {"funcotatorValidation"},
      dataProvider = "provideForLargeDataValidationTest")
public void largeDataValidationTest(final String inputVcfName,
                                    final String referencePath,
                                    final String referenceVersion,
                                    final String dataSourcesPath,
                                    final boolean isDsEnvironmentPath) throws IOException {

    // Get our main test folder path from our environment:
    final String testFolderInputPath = getFuncotatorLargeDataValidationTestInputPath();

    long startTime = 0, endTime = 0;
    final long overallStartTime = System.nanoTime();

    final String outFileBaseName = inputVcfName + ".funcotator";

    final String dataSourcesPathString;
    if (isDsEnvironmentPath) {
        dataSourcesPathString = getFuncotatorLargeDataValidationTestInputPath() + dataSourcesPath;
    }
    else {
        dataSourcesPathString = dataSourcesPath;
    }

    for (final FuncotatorArgumentDefinitions.OutputFormatType outFormat : FuncotatorArgumentDefinitions.OutputFormatType.values()) {

        startTime = System.nanoTime();

        final File outputFile;
        if (outFormat == FuncotatorArgumentDefinitions.OutputFormatType.VCF) {
            outputFile = getOutputFile(outFileBaseName, outFormat.toString().toLowerCase());
        } else {
            outputFile = getOutputFile(outFileBaseName + ".maf", "tsv");
        }

        final ArgumentsBuilder arguments = createBaselineArgumentsForFuncotator(
                testFolderInputPath + inputVcfName,
                outputFile,
                referencePath,
                dataSourcesPathString,
                referenceVersion,
                outFormat,
                true);

        // Add our manual annotations to the arguments:
        addManualAnnotationsToArguments(arguments);

        // Run the tool with our args:
        runCommandLine(arguments);

        endTime = System.nanoTime();

        logger.warn("  Elapsed Time (" + outFormat.toString() + "): " + (endTime - startTime) / 1e9 + "s");

        // Perform a simple validation if the file was a VCF:
        if ( outFormat == FuncotatorArgumentDefinitions.OutputFormatType.VCF) {

            try ( final FeatureDataSource<VariantContext> vcfReader = new FeatureDataSource<>(outputFile.getAbsolutePath()) ) {
                Assert.assertTrue(vcfReader.getHeader() instanceof VCFHeader, "Header is not a VCFHeader!");
                final VCFHeader vcfHeader = (VCFHeader)vcfReader.getHeader();

                final VCFInfoHeaderLine funcotationHeaderLine = vcfHeader.getInfoHeaderLine(VcfOutputRenderer.FUNCOTATOR_VCF_FIELD_NAME);
                final String[] funcotationFieldNames = FuncotatorUtils.extractFuncotatorKeysFromHeaderDescription(funcotationHeaderLine.getDescription());

                validateFuncotationsOnVcf(vcfReader, funcotationFieldNames);

            }
        }
    }

    logger.warn("Total Elapsed Time: " + (endTime - overallStartTime) / 1e9 + "s");
}
 
Example 6
Source File: FuncotatorIntegrationTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testVcfDatasourceAccountsForAltAlleles() {
    final FuncotatorArgumentDefinitions.OutputFormatType vcfOutputFormatType = FuncotatorArgumentDefinitions.OutputFormatType.VCF;
    final File vcfOutputFile = getOutputFile(vcfOutputFormatType);

    final ArgumentsBuilder arguments = createBaselineArgumentsForFuncotator(
            PIK3CA_VCF_HG19_ALTS,
            vcfOutputFile,
            b37Chr3Ref,
            DS_PIK3CA_DIR,
            FuncotatorTestConstants.REFERENCE_VERSION_HG19,
            vcfOutputFormatType,
            false);

    // We need this argument since we are testing on a subset of b37
    arguments.add(FuncotatorArgumentDefinitions.FORCE_B37_TO_HG19_REFERENCE_CONTIG_CONVERSION, true);

    runCommandLine(arguments);

    final Pair<VCFHeader, List<VariantContext>> vcfInfo = VariantContextTestUtils.readEntireVCFIntoMemory(vcfOutputFile.getAbsolutePath());
    final List<VariantContext> variantContexts = vcfInfo.getRight();
    Assert.assertTrue(variantContexts.size() > 0);
    final VCFHeader vcfHeader = vcfInfo.getLeft();
    final VCFInfoHeaderLine funcotationHeaderLine = vcfHeader.getInfoHeaderLine(VcfOutputRenderer.FUNCOTATOR_VCF_FIELD_NAME);
    final String[] funcotationKeys = FuncotatorUtils.extractFuncotatorKeysFromHeaderDescription(funcotationHeaderLine.getDescription());

    // The first variant context should have clinvar annotations, since it hit on the alt allele.  None of the rest.
    // This test assumes that each test variant context has only one alt allele.
    // The rest should not have any clinvar hits.
    for (int i = 0; i < variantContexts.size(); i++) {
        final String gtString = (i == 0) ? "MedGen:C0027672,SNOMED_CT:699346009" : "";
        final Map<Allele, FuncotationMap> alleleToFuncotationMap = FuncotatorUtils.createAlleleToFuncotationMapFromFuncotationVcfAttribute(funcotationKeys,
                variantContexts.get(i), "Gencode_19_annotationTranscript", "TEST");
        Assert.assertEquals(alleleToFuncotationMap.entrySet().size(), 1, "Found more than 1 alternate allele!");

        final FuncotationMap funcotationMap = alleleToFuncotationMap.values().iterator().next();
        Assert.assertEquals(funcotationMap.getTranscriptList().size(), 1, "Found more than 1 funcotation!");
        Assert.assertTrue(funcotationMap.getTranscriptList().stream().noneMatch(k -> k.equals(FuncotationMap.NO_TRANSCRIPT_AVAILABLE_KEY)), "Found a funcotation with an unknown transcript name: " + FuncotationMap.NO_TRANSCRIPT_AVAILABLE_KEY);
        Assert.assertTrue(funcotationMap.getTranscriptList().stream().noneMatch(StringUtils::isEmpty), "Found a funcotation with an empty transcript!");
        final List<Funcotation> funcotations = funcotationMap.get(funcotationMap.getTranscriptList().get(0));
        Assert.assertEquals(funcotations.size(), 1, "Found more than one funcotation in the funcotation map!");
        final Funcotation funcotation = funcotations.get(0);

        Assert.assertEquals(funcotation.getField("dummy_ClinVar_VCF_CLNDISDB"), FuncotatorUtils.sanitizeFuncotationFieldForVcf(gtString), "Field (dummy_ClinVar_VCF_CLNDISDB) was unsanititzed: " + funcotation.getField("dummy_ClinVar_VCF_CLNDISDB"));
    }
}
 
Example 7
Source File: FuncotatorIntegrationTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testCanAnnotateSpanningDeletions() {
    final FuncotatorArgumentDefinitions.OutputFormatType outputFormatType = FuncotatorArgumentDefinitions.OutputFormatType.VCF;
    final File outputFile = getOutputFile(outputFormatType);

    final ArgumentsBuilder arguments = createBaselineArgumentsForFuncotator(
            SPANNING_DEL_VCF,
            outputFile,
            hg38Chr3Ref,
            DS_PIK3CA_DIR,
            FuncotatorTestConstants.REFERENCE_VERSION_HG38,
            outputFormatType,
            false);

    runCommandLine(arguments);

    final Pair<VCFHeader, List<VariantContext>> vcfInfo = VariantContextTestUtils.readEntireVCFIntoMemory(outputFile.getAbsolutePath());
    final List<VariantContext> variantContexts = vcfInfo.getRight();
    Assert.assertTrue(variantContexts.size() > 0);
    final VCFHeader vcfHeader = vcfInfo.getLeft();
    final VCFInfoHeaderLine funcotationHeaderLine = vcfHeader.getInfoHeaderLine(VcfOutputRenderer.FUNCOTATOR_VCF_FIELD_NAME);
    final String[] funcotationKeys = FuncotatorUtils.extractFuncotatorKeysFromHeaderDescription(funcotationHeaderLine.getDescription());

    final int NUM_VARIANTS = 10;
    Assert.assertEquals(variantContexts.size(), NUM_VARIANTS);
    Assert.assertEquals(variantContexts.stream().filter(v -> v.hasAllele(Allele.SPAN_DEL)).count(), NUM_VARIANTS);

    for (final VariantContext vc : variantContexts) {
        final Map<Allele, FuncotationMap> alleleToFuncotationMap = FuncotatorUtils.createAlleleToFuncotationMapFromFuncotationVcfAttribute(funcotationKeys,
                vc, "Gencode_28_annotationTranscript", "TEST");

        // Make sure that all spanning deletions are funcotated with could not determine and that the others are something else.
        for (final Allele allele : alleleToFuncotationMap.keySet()) {
            final List<String> txIds = alleleToFuncotationMap.get(allele).getTranscriptList();
            for (final String txId : txIds) {
                if (allele.equals(Allele.SPAN_DEL)) {
                    Assert.assertEquals(alleleToFuncotationMap.get(allele).get(txId).get(0).getField("Gencode_28_variantClassification"), GencodeFuncotation.VariantClassification.COULD_NOT_DETERMINE.toString());
                    Assert.assertEquals(alleleToFuncotationMap.get(allele).get(txId).get(0).getField("Gencode_28_variantType"), GencodeFuncotation.VariantType.NA.toString());
                } else {
                    Assert.assertNotEquals(alleleToFuncotationMap.get(allele).get(txId).get(0).getField("Gencode_28_variantClassification"), GencodeFuncotation.VariantClassification.COULD_NOT_DETERMINE.toString());
                }
            }
        }
    }
}