@Override public VariantContext convert(CharSequence buff) { return this.codec.decode(buff.toString()); }
@Override public VariantContext decode() { try { final String line; return ((line = inputReader.readLine()) != null) ? this.vcfDecoder.decode(line) : null; } catch (final IOException ioe) { throw new RuntimeException("Could not decode/read a VCF record for a sorting collection: " + ioe.getMessage(), ioe); } }
@Override public VariantContext decode() { try { final String line; return ((line = inputReader.readLine()) != null) ? this.vcfDecoder.decode(line) : null; } catch (final IOException ioe) { throw new RuntimeIOException("Could not decode/read a VCF record for a sorting collection: " + ioe.getMessage(), ioe); } }
@Override public VariantContext decode() { try { final String line; return ((line = inputReader.readLine()) != null) ? this.vcfDecoder.decode(line) : null; } catch (final IOException ioe) { throw new RuntimeIOException("Could not decode/read a VCF record for a sorting collection: " + ioe.getMessage(), ioe); } }
@Override protected VariantContext advance() { return this.lineIterator.hasNext() ? this.codec.decode(this.lineIterator.next()) : null; }
@Override protected VariantContext advance() { return this.lineIterator.hasNext() ? this.codec.decode(this.lineIterator.next()) : null; }
protected List<Variant> processLine(String line) { // long curr = System.currentTimeMillis(); VariantContext htsVar = this.vcfCodec.decode(line); // this.timeHts.addAndGet(System.currentTimeMillis() - curr); // curr = System.currentTimeMillis(); Variant variant = this.converter.convert(htsVar); // this.timeAvro.addAndGet(System.currentTimeMillis() - curr); // curr = System.currentTimeMillis(); List<Variant> normVar = this.normalizer.apply(Collections.singletonList(variant)); // this.timeNorm.addAndGet(System.currentTimeMillis() - curr); // curr = System.currentTimeMillis(); this.variantStatsTask.apply(normVar); // this.timeStats.addAndGet(System.currentTimeMillis() - curr); return normVar; }
/** * Read all of the VCF records from source into memory, returning the header and the VariantContexts * * SHOULD ONLY BE USED FOR UNIT/INTEGRATION TESTING PURPOSES! * * @param source the file to read, must be in VCF4 format * @return * @throws java.io.IOException */ public static Pair<VCFHeader, List<VariantContext>> readVCF(final File source) throws IOException { // read in the features final List<VariantContext> vcs = new ArrayList<VariantContext>(); final VCFCodec codec = new VCFCodec(); PositionalBufferedStream pbs = new PositionalBufferedStream(new FileInputStream(source)); final LineIterator vcfSource = codec.makeSourceFromStream(pbs); try { final VCFHeader vcfHeader = (VCFHeader) codec.readActualHeader(vcfSource); while (vcfSource.hasNext()) { final VariantContext vc = codec.decode(vcfSource); if ( vc != null ) vcs.add(vc); } return new Pair<VCFHeader, List<VariantContext>>(vcfHeader, vcs); } finally { codec.close(vcfSource); } }
v = codec.decode(line); } catch (TribbleException e) { if (stringency == ValidationStringency.STRICT) {
@Override public boolean nextKeyValue() throws IOException { while (true) { String line; while (true) { if (!lineRecordReader.nextKeyValue()) { return false; } line = lineRecordReader.getCurrentValue().toString(); if (!line.startsWith("#")) { break; } } final VariantContext v = codec.decode(line); if (!overlaps(v)) { continue; } Integer chromIdx = contigDict.get(v.getContig()); if (chromIdx == null) chromIdx = (int) MurmurHash3.murmurhash3(v.getContig(), 0); key.set((long) chromIdx << 32 | (long) (v.getStart() - 1)); vc.set(v, header); return true; } }
/** Set vcfHeader and vcfCodec to null if reading non-vcf line. * */ private void addLineToIndex(String line, TabixIndexCreator indexCreator, long filePosition, TabixFormat fmt, VCFHeader vcfHeader, VCFCodec vcfCodec) throws InvalidRecordException { if(fmt.equals(TabixFormat.BED)){ BedLineCodec bedCodec= new BedLineCodec(); BedLine bed = bedCodec.decode(line); indexCreator.addFeature(bed, filePosition); } else if(fmt.equals(TabixFormat.GFF)){ GtfLine gtf= new GtfLine(line.split("\t")); indexCreator.addFeature(gtf, filePosition); } else if(fmt.equals(TabixFormat.VCF)) { VariantContext vcf = vcfCodec.decode(line); indexCreator.addFeature(vcf, filePosition); } else { System.err.println("Unexpected TabixFormat: " + fmt.sequenceColumn + " " + fmt.startPositionColumn); throw new InvalidRecordException(); } }
@Test public void testVCFHeaderSampleRenamingSingleSampleVCF() throws Exception { final VCFCodec codec = new VCFCodec(); codec.setRemappedSampleName("FOOSAMPLE"); final AsciiLineReaderIterator vcfIterator = new AsciiLineReaderIterator(AsciiLineReader.from(new FileInputStream(variantTestDataRoot + "HiSeq.10000.vcf"))); final VCFHeader header = (VCFHeader) codec.readHeader(vcfIterator).getHeaderValue(); Assert.assertEquals(header.getNGenotypeSamples(), 1, "Wrong number of samples in remapped header"); Assert.assertEquals(header.getGenotypeSamples().get(0), "FOOSAMPLE", "Sample name in remapped header has incorrect value"); int recordCount = 0; while (vcfIterator.hasNext() && recordCount < 10) { recordCount++; final VariantContext vcfRecord = codec.decode(vcfIterator.next()); Assert.assertEquals(vcfRecord.getSampleNames().size(), 1, "Wrong number of samples in vcf record after remapping"); Assert.assertEquals(vcfRecord.getSampleNames().iterator().next(), "FOOSAMPLE", "Wrong sample in vcf record after remapping"); } }
/** * Returns a list of VariantContext records from a VCF file * * @param vcfFile VCF file * * @throws IOException if the file does not exist or can not be opened * * @return list of VariantContext records */ private static List<VariantContext> getVariantContexts(final File vcfFile) throws IOException { final VCFCodec codec = new VCFCodec(); final FileInputStream s = new FileInputStream(vcfFile); final LineIterator lineIteratorVCF = codec.makeSourceFromStream(new PositionalBufferedStream(s)); codec.readHeader(lineIteratorVCF); final List<VariantContext> VCs = new ArrayList<>(); while ( lineIteratorVCF.hasNext() ) { final String line = lineIteratorVCF.next(); Assert.assertFalse(line == null); VCs.add(codec.decode(line)); } return VCs; }
@Test public void HCTestDanglingTailMergingForDeletions() throws IOException { final String base = String.format("-T HaplotypeCaller --disableDithering --pcr_indel_model NONE -R %s -I %s", REF, NA12878_BAM) + " --no_cmdline_in_header -o %s -L 20:10130740-10130800 --allowNonUniqueKmersInRef"; final WalkerTestSpec spec = new WalkerTestSpec(base, 1, Arrays.asList("")); final File outputVCF = executeTest("HCTestDanglingTailMergingForDeletions", spec).getFirst().get(0); // confirm that the call is the correct one final VCFCodec codec = new VCFCodec(); final FileInputStream s = new FileInputStream(outputVCF); final LineIterator lineIterator = codec.makeSourceFromStream(new PositionalBufferedStream(s)); codec.readHeader(lineIterator); final String line = lineIterator.next(); Assert.assertFalse(line == null); final VariantContext vc = codec.decode(line); Assert.assertTrue(vc.isBiallelic()); Assert.assertTrue(vc.getReference().basesMatch("ATGTATG")); Assert.assertTrue(vc.getAlternateAllele(0).basesMatch("A")); }
/** * Create an IntervalFeature from a String. Typically this is a line read from file. * vcfHeader can be null if trackformat is not VCF. */ public IntervalFeature(String line, TrackFormat format, VCFCodec vcfCodec) throws InvalidGenomicCoordsException{ if(format.equals(TrackFormat.BED) || format.equals(TrackFormat.BEDGRAPH) || format.equals(TrackFormat.BIGBED)){ this.intervalFeatureFromBedLine(line); this.trackFormat= TrackFormat.BED; } else if(format.equals(TrackFormat.GFF) || format.equals(TrackFormat.GTF)){ this.intervalFeatureFromGtfLine(line); this.trackFormat= TrackFormat.GFF; } else if(format.equals(TrackFormat.VCF)) { this.variantContext= vcfCodec.decode(line); this.setRaw(line); this.trackFormat= TrackFormat.VCF; this.chrom= this.variantContext.getContig(); this.from= this.setFromForVCF(); this.to= this.setToForVCF(); this.name= this.variantContext.getID(); } else { System.err.println("Format " + format + " not supported"); throw new RuntimeException(); } }
while (lineIteratorVCF.hasNext()) { final String line = lineIteratorVCF.next(); final VariantContext vc = codec.decode(line);
/** * Returns a list of attribute values from a VCF file * * @param vcfFile VCF file * @param attributeName attribute name * * @throws IOException if the file does not exist or can not be opened * * @return list of attribute values */ private List<String> getAttributeValues(final File vcfFile, final String attributeName) throws IOException { final VCFCodec codec = new VCFCodec(); final FileInputStream s = new FileInputStream(vcfFile); final LineIterator lineIteratorVCF = codec.makeSourceFromStream(new PositionalBufferedStream(s)); codec.readHeader(lineIteratorVCF); final List<String> attributeValues = new ArrayList<String>(); while (lineIteratorVCF.hasNext()) { final String line = lineIteratorVCF.next(); Assert.assertFalse(line == null); final VariantContext vc = codec.decode(line); for (final Genotype g : vc.getGenotypes()) { if (g.hasExtendedAttribute(attributeName)) { attributeValues.add((String) g.getExtendedAttribute(attributeName)); } } } return attributeValues; }
int counter = 0; while (iterator.hasNext()) { VariantContext context = codec.decode(iterator.next()); counter++;
/** test, using the writer and reader, that we can output and input a VCF body without problems */ @Test public void testWriteAndReadAsyncVCFHeaderless() throws IOException { final File fakeVCFFile = VariantBaseTest.createTempFile("testWriteAndReadAsyncVCFHeaderless.", IOUtil.VCF_FILE_EXTENSION); fakeVCFFile.deleteOnExit(); Tribble.indexFile(fakeVCFFile).deleteOnExit(); final Set<VCFHeaderLine> metaData = new HashSet<>(); final Set<String> additionalColumns = new HashSet<>(); final SAMSequenceDictionary sequenceDict = createArtificialSequenceDictionary(); final VCFHeader header = createFakeHeader(metaData, additionalColumns, sequenceDict); try (final VariantContextWriter writer = new VariantContextWriterBuilder() .setOutputFile(fakeVCFFile).setReferenceDictionary(sequenceDict) .setOptions(EnumSet.of(Options.ALLOW_MISSING_FIELDS_IN_HEADER, Options.INDEX_ON_THE_FLY, Options.USE_ASYNC_IO)) .build()) { writer.setHeader(header); writer.add(createVC(header)); writer.add(createVC(header)); } final VCFCodec codec = new VCFCodec(); codec.setVCFHeader(header, VCFHeaderVersion.VCF4_2); try (final FileInputStream fis = new FileInputStream(fakeVCFFile)) { final AsciiLineReaderIterator iterator = new AsciiLineReaderIterator(new AsciiLineReader(fis)); int counter = 0; while (iterator.hasNext()) { VariantContext context = codec.decode(iterator.next()); counter++; } Assert.assertEquals(counter, 2); } }