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

The following examples show how to use htsjdk.variant.vcf.VCFHeader#getSampleNamesInOrder() . 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: FastaAlternateReferenceMaker.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onTraversalStart() {
    super.onTraversalStart();

    if (snpmaskPriority && snpmask == null){
        throw new CommandLineException("Cannot specify --" + SNP_MASK_PRIORITY_LONG_NAME + " without " + " --" + SNP_MASK_LONG_NAME);
    }

    if ( iupacSample != null ) {
        final VCFHeader variantHeader = (VCFHeader) getHeaderForFeatures(variants);
        final ArrayList<String> samples = variantHeader.getSampleNamesInOrder();
        if( samples == null || !samples.contains(iupacSample)) {
            throw new UserException.BadInput("the IUPAC sample specified is not present in the provided VCF file");
        }
    }
}
 
Example 2
Source File: LazyBCFGenotypesContext.java    From Hadoop-BAM with MIT License 5 votes vote down vote up
@Override public void setHeader(VCFHeader header) {
	genoFieldDecoders = new BCF2GenotypeFieldDecoders(header);
	fieldDict = BCF2Utils.makeDictionary(header);

	builders = new GenotypeBuilder[header.getNGenotypeSamples()];
	final List<String> genotypeSamples = header.getGenotypeSamples();
	for (int i = 0; i < builders.length; ++i)
		builders[i] = new GenotypeBuilder(genotypeSamples.get(i));

	sampleNamesInOrder = header.getSampleNamesInOrder();
	sampleNameToOffset = header.getSampleNameToOffset();
}
 
Example 3
Source File: CNVInputReader.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void validateCNVcallDataSource(final SAMFileHeader headerForReads,
                                              final String sampleId,
                                              final FeatureDataSource<VariantContext> dataSource) {
    final VCFHeader cnvCallHeader = (VCFHeader) dataSource.getHeader();
    final ArrayList<String> sampleNamesInOrder = cnvCallHeader.getSampleNamesInOrder();
    Utils.validate(sampleNamesInOrder.size() == 1, "CNV call VCF should be single sample");
    Utils.validate(sampleNamesInOrder.contains(sampleId), ("CNV call VCF does not contain calls for sample " + sampleId));
    Utils.validate(cnvCallHeader.getSequenceDictionary() != null,
            "CNV calls file does not have a valid sequence dictionary");
    Utils.validate(cnvCallHeader.getSequenceDictionary().isSameDictionary(headerForReads.getSequenceDictionary()),
            "CNV calls file does not have the same sequence dictionary as the read evidence");
}
 
Example 4
Source File: SamplePairExtractor.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * This method should not be needed in germline use cases, but should produce an empty list in such cases.
 *
 * Pairing is done as follows:
 *
 * - Using the M2 convention:  "tumor_sample" in the header and "normal_sample" if applicable
 * - If there is only one sample, the pair will be the sample name, "".
 * - The samples have names such as "TUMOR" and "NORMAL".  See {@link SamplePairExtractor#TUMOR_SAMPLE_NAME_LIST} and
 *     {@link SamplePairExtractor#NORMAL_SAMPLE_NAME_LIST}
 *
 * If none of these are fulfilled, the returned list will be empty.
 *
 * @param vcfHeader Never {@code null}.  Use blank constructor in {@link VCFHeader} rather than null.
 * @return Tumor-normal sample names.  In tumor-only cases, the normal sample will have an empty string.
 *  Never {@code null}
 */
public static List<TumorNormalPair> extractPossibleTumorNormalPairs(final VCFHeader vcfHeader) {
    Utils.nonNull(vcfHeader);
    // First attempt the M2 nomenclature
    if (vcfHeader.getMetaDataLine(Mutect2Engine.TUMOR_SAMPLE_KEY_IN_VCF_HEADER) != null) {
        final VCFHeaderLine normalMetaDataLine = vcfHeader.getMetaDataLine(Mutect2Engine.NORMAL_SAMPLE_KEY_IN_VCF_HEADER);
        return Collections.singletonList(new TumorNormalPair(vcfHeader.getMetaDataLine(Mutect2Engine.TUMOR_SAMPLE_KEY_IN_VCF_HEADER).getValue(),
                (normalMetaDataLine == null ?
                        NO_NORMAL : normalMetaDataLine.getValue())));
    }

    // Then try sample names (e.g. "TUMOR", "NORMAL")
    final List<String> sampleNames = vcfHeader.getSampleNamesInOrder();
    if (sampleNames.size() == 1) {
        return Collections.singletonList(new TumorNormalPair(sampleNames.get(0), NO_NORMAL));
    }
    if (sampleNames.size() > 0) {
        final List<String> tumorSamples = sampleNames.stream()
                .filter(s -> TUMOR_SAMPLE_NAME_LIST.contains(s)).collect(Collectors.toList());
        final List<String> normalSamples = sampleNames.stream()
                .filter(s -> NORMAL_SAMPLE_NAME_LIST.contains(s)).collect(Collectors.toList());

        return tumorSamples.stream().flatMap(t ->
                normalSamples.stream().map(n -> new TumorNormalPair(t,n))).collect(Collectors.toList());
    }

    return Collections.emptyList();
}
 
Example 5
Source File: GenomeWarpSerial.java    From genomewarp with Apache License 2.0 4 votes vote down vote up
private static VCFHeader warpHeader(VCFHeader in, Map<String, Long> namesAndLength)
    throws IllegalArgumentException {
  Set<VCFHeaderLine> newLines = new HashSet<>();
  boolean hasSource = false;
  VCFHeaderVersion version = DEFAULT_VCF_VERSION;

  for (VCFHeaderLine line : in.getMetaDataInInputOrder()) {
    if (line.getKey().equals("reference")) {
      newLines.add(new VCFHeaderLine(line.getKey(), ARGS.refTargetFASTA));
    } else if (line.getKey().equals("source")) {
      newLines.add(new VCFHeaderLine(line.getKey(), line.getValue() + "_and_"
          + GENOME_WARP_VERSION));
      hasSource = true;
    } else if (line.getKey().equals("fileformat")) {
      version = VCFHeaderVersion.toHeaderVersion(line.getValue());
      if (version == null) {
        throw new IllegalArgumentException("malformed version: " + line.getValue());
      }
    } else if (line.getKey().equals(VCFConstants.CONTIG_HEADER_KEY)) {
      continue;
    } else {
      newLines.add(line);
    }
  }

  if (!hasSource) {
    newLines.add(new VCFHeaderLine("source", GENOME_WARP_VERSION));
  }

  // Add contigs
  int i = 0;
  for (Map.Entry<String, Long> entry : namesAndLength.entrySet()) {
    String currName = entry.getKey();
    long chrSize = entry.getValue();

    newLines.add(new VCFContigHeaderLine(VCFHeaderLine.toStringEncoding(
        createContigEntry(currName, chrSize, ARGS.targetAssembly, ARGS.species)),
        version, VCFConstants.CONTIG_HEADER_KEY, i++));
  }

  return new VCFHeader(newLines, in.getSampleNamesInOrder());
}