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

The following examples show how to use htsjdk.variant.vcf.VCFHeader#getNGenotypeSamples() . 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: MakeVcfSampleNameMap.java    From picard with MIT License 5 votes vote down vote up
@Override
protected int doWork() {
    IOUtil.assertFileIsWritable(OUTPUT);

    // For building the output-map without sample-name collisions.
    final Map<String, String> pathToNameMap = new HashMap<>(INPUT.size());
    // For detecting sample-name collisions, so we can warn about them.
    final Map<String, String> nameToPathMap = new HashMap<>(INPUT.size());

    for (final String variantPathString : INPUT) {
        final Path variantPath = getVariantPath(variantPathString);
        final VCFHeader header = getHeaderFromPath(variantPath);

        final int numberOfSamples = header.getNGenotypeSamples();
        if (numberOfSamples != 1) {
            throw new PicardException("Input: " + variantPathString + " was expected to contain a single sample" +
                    " but actually contained " + numberOfSamples + " samples.");
        }

        final String sampleName = header.getGenotypeSamples().get(0);
        pathToNameMap.put(variantPathString, sampleName);
        final String previousPath = nameToPathMap.put(sampleName, variantPathString);
        if (previousPath != null) {
            log.warn("Duplicate sample: " + sampleName + ". Sample was found in both " +
                    previousPath + " and in " + variantPathString);
        }
    }

    final List<String> mapLines = new ArrayList<>(INPUT.size());
    pathToNameMap.forEach((path, name) -> mapLines.add(path + "\t" + name));

    try {
        Files.write(OUTPUT.toPath(), mapLines);
    } catch (IOException e) {
        throw new PicardException("Error while writing output", e);
    }

    return 0;
}
 
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: GenomicsDBImport.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void assertGVCFHasOnlyOneSample(final String variantPath, final VCFHeader header) {
    // A GVCF file must contain only one sample, throw an exception otherwise
    final int numberOfSamples = header.getNGenotypeSamples();
    if (numberOfSamples != 1) {
        throw new UserException("Input GVCF: " + variantPath + " was expected to contain a single sample but actually contained " + numberOfSamples + " samples.");
    }
}
 
Example 4
Source File: VcfToAdpc.java    From picard with MIT License 4 votes vote down vote up
@Override
protected int doWork() {
    final List<File> inputs = IOUtil.unrollFiles(VCF, IOUtil.VCF_EXTENSIONS);
    IOUtil.assertFilesAreReadable(inputs);
    IOUtil.assertFileIsWritable(SAMPLES_FILE);
    IOUtil.assertFileIsWritable(NUM_MARKERS_FILE);
    IOUtil.assertFileIsWritable(OUTPUT);
    final List<String> sampleNames = new ArrayList<>();

    Integer numberOfLoci = null;
    try (IlluminaAdpcFileWriter adpcFileWriter = new IlluminaAdpcFileWriter(OUTPUT)) {
        for (final File inputVcf : inputs) {
            VCFFileReader vcfFileReader = new VCFFileReader(inputVcf, false);
            final VCFHeader header = vcfFileReader.getFileHeader();
            for (int sampleNumber = 0; sampleNumber < header.getNGenotypeSamples(); sampleNumber++) {
                final String sampleName = header.getGenotypeSamples().get(sampleNumber);
                sampleNames.add(sampleName);
                log.info("Processing sample: " + sampleName + " from VCF: " + inputVcf.getAbsolutePath());

                CloseableIterator<VariantContext> variants = vcfFileReader.iterator();
                int lociCount = 0;
                while (variants.hasNext()) {
                    final VariantContext context = variants.next();
                    final float gcScore = getFloatAttribute(context, InfiniumVcfFields.GC_SCORE);

                    final Genotype genotype = context.getGenotype(sampleNumber);
                    final IlluminaGenotype illuminaGenotype = getIlluminaGenotype(genotype, context);

                    final int rawXIntensity = getUnsignedShortAttributeAsInt(genotype, InfiniumVcfFields.X);
                    final int rawYIntensity = getUnsignedShortAttributeAsInt(genotype, InfiniumVcfFields.Y);

                    final Float normalizedXIntensity = getFloatAttribute(genotype, InfiniumVcfFields.NORMX);
                    final Float normalizedYIntensity = getFloatAttribute(genotype, InfiniumVcfFields.NORMY);

                    final IlluminaAdpcFileWriter.Record record = new IlluminaAdpcFileWriter.Record(rawXIntensity, rawYIntensity, normalizedXIntensity, normalizedYIntensity, gcScore, illuminaGenotype);
                    adpcFileWriter.write(record);
                    lociCount++;
                }
                if (lociCount == 0) {
                    throw new PicardException("Found no records in VCF' " + inputVcf.getAbsolutePath() + "'");
                }
                if (numberOfLoci == null) {
                    numberOfLoci = lociCount;
                } else {
                    if (lociCount != numberOfLoci) {
                        throw new PicardException("VCFs have differing number of loci");
                    }
                }
            }
        }
        writeTextToFile(SAMPLES_FILE, StringUtils.join(sampleNames, "\n"));
        writeTextToFile(NUM_MARKERS_FILE, "" + numberOfLoci);
    } catch (Exception e) {
        log.error(e);
        return 1;
    }

    return 0;
}