htsjdk.samtools.util.CloseableIterator Java Examples

The following examples show how to use htsjdk.samtools.util.CloseableIterator. 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: SingleCellRnaSeqMetricsCollector.java    From Drop-seq with MIT License 7 votes vote down vote up
RnaSeqMetricsCollector getRNASeqMetricsCollector(final String cellBarcodeTag, final List<String> cellBarcodes, final File inBAM,
  		final RnaSeqMetricsCollector.StrandSpecificity strand, final double rRNAFragmentPCT, final int readMQ,
  		final File annotationsFile, final File rRNAIntervalsFile) {

  	CollectorFactory factory = new CollectorFactory(inBAM, strand, rRNAFragmentPCT, annotationsFile, rRNAIntervalsFile);
RnaSeqMetricsCollector collector=  factory.getCollector(cellBarcodes);
List<SAMReadGroupRecord> rg = factory.getReadGroups(cellBarcodes);

      // iterate by cell barcodes.  Skip all the reads without cell barcodes.
CloseableIterator<SAMRecord> iter = getReadsInTagOrder (inBAM, cellBarcodeTag, rg, cellBarcodes, readMQ);

      ProgressLogger p = new ProgressLogger(log, 1000000, "Accumulating metrics");
while (iter.hasNext()) {
	SAMRecord r = iter.next();
	String cellBarcode = r.getStringAttribute(cellBarcodeTag);
	r.setAttribute("RG", cellBarcode);
          p.record(r);
   	collector.acceptRecord(r, null);
}

collector.finish();
return (collector);
  }
 
Example #2
Source File: CollapseBarcodesInPlace.java    From Drop-seq with MIT License 6 votes vote down vote up
public void processOnlyPrimary () {
       final SamHeaderAndIterator inputs = openInputs();
	CloseableIterator<SAMRecord> inputSam = inputs.iterator;
	SAMFileHeader header = inputs.header;
	header.addComment("Edit distance collapsed tag " +  this.PRIMARY_BARCODE + " to new tag " + this.OUT_BARCODE+ " with edit distance "+ this.EDIT_DISTANCE);
       SAMFileWriter writer= new SAMFileWriterFactory().makeSAMOrBAMWriter(header, true, this.OUTPUT);

	// gather up the barcodes that exist in the BAM
       final SamHeaderAndIterator inputs2 = openInputs();
	ObjectCounter<String> barcodes = new BamTagHistogram().getBamTagCounts(inputs2.iterator, this.PRIMARY_BARCODE,this.MINIMUM_MAPPING_QUALITY, this.FILTER_PCR_DUPLICATES);
       CloserUtil.close(inputs2.iterator);

	// filter barcodes by #reds in each barcode.
	barcodes=filterBarcodesByNumReads(barcodes, this.MIN_NUM_READS_NONCORE);

	// collapse them
	Map<String, String> childParentBarcodes=collapseBarcodes(this.MIN_NUM_READS_CORE, this.NUM_CORE_BARCODES, barcodes, this.FIND_INDELS, this.EDIT_DISTANCE);
	// iterate through the reads and retag with the proper reads.
	// log.info("STUFF");
	retagReads(inputSam, writer, childParentBarcodes, this.PRIMARY_BARCODE, this.OUT_BARCODE);
	// collapsed.size();

	CloserUtil.close(inputSam);
	writer.close();
}
 
Example #3
Source File: SortVcfsTest.java    From picard with MIT License 6 votes vote down vote up
/**
 * Checks the ordering and total number of variant context entries in the specified output VCF file.
 * Does NOT check explicitly that the VC genomic positions match exactly those from the inputs. We assume this behavior from other tests.
 *
 * @param output VCF file representing the output of SortVCF
 * @param expectedVariantContextCount the total number of variant context entries from all input files that were merged/sorted
 */
private void validateSortingResults(final File output, final int expectedVariantContextCount) {
    final VCFFileReader outputReader = new VCFFileReader(output, false);
    final VariantContextComparator outputComparator = outputReader.getFileHeader().getVCFRecordComparator();
    VariantContext last = null;
    int variantContextCount = 0;
    final CloseableIterator<VariantContext> iterator = outputReader.iterator();
    while (iterator.hasNext()) {
        final VariantContext outputContext = iterator.next();
        if (last != null) Assert.assertTrue(outputComparator.compare(last, outputContext) <= 0);
        last = outputContext;
        variantContextCount++;
    }
    iterator.close();
    Assert.assertEquals(variantContextCount, expectedVariantContextCount);
}
 
Example #4
Source File: MergingIteratorTest.java    From picard with MIT License 6 votes vote down vote up
@Test(expectedExceptions = IllegalStateException.class)
public void testOutOfOrderIterators() {
	final Queue<Integer> queueOne = new LinkedList<Integer>();
	queueOne.add(1);
	queueOne.add(3);

	final Queue<Integer> queueTwo = new LinkedList<Integer>();
	queueTwo.add(4);
	queueTwo.add(2);

	final Collection<CloseableIterator<Integer>> iterators = new ArrayList<CloseableIterator<Integer>>(3);
	Collections.addAll(
			iterators,
			new QueueBackedIterator<Integer>(queueOne),
			new QueueBackedIterator<Integer>(queueTwo));

	final MergingIterator<Integer> mergingIterator = new MergingIterator<Integer>(
			INTEGER_COMPARATOR,
			iterators);

	Assert.assertEquals(mergingIterator.next().intValue(), 1);
	Assert.assertEquals(mergingIterator.next().intValue(), 3);
	Assert.assertEquals(mergingIterator.next().intValue(), 4);
	mergingIterator.next(); // fails, because the next element would be "2"
}
 
Example #5
Source File: ThreadsafeTest.java    From picard with MIT License 6 votes vote down vote up
@Test
public void ensureSameVariantsReadAsSimpleVcfFileIterator() {
    final VariantIteratorProducer.Threadsafe iteratorFactory =
            new VariantIteratorProducer.Threadsafe(
                    VcfFileSegmentGenerator.byWholeContigSubdividingWithWidth(TEN_MILLION),
                    Arrays.asList(VCF_WITH_MULTI_ALLELIC_VARIANT_AT_POSITION_10MILLION)
            );
    final Set<String> observedVcs = new HashSet<String>();
    final Set<String> actual = new HashSet<String>();
    final VCFFileReader actualVcs = new VCFFileReader(VCF_WITH_MULTI_ALLELIC_VARIANT_AT_POSITION_10MILLION);
    for (final VariantContext actualVc : actualVcs) {
        actual.add(actualVc.toString());
    }

    for (final CloseableIterator<VariantContext> i : iteratorFactory.iterators()) {
        while (i.hasNext()) {
            observedVcs.add(i.next().toString());
        }
    }
    
    Assert.assertEquals(actual, observedVcs);
}
 
Example #6
Source File: MultiHitAlignedReadIterator.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 *
 * @param querynameOrderIterator
 * @param primaryAlignmentSelectionStrategy Algorithm for selecting primary alignment when it is not clear from
 *                                          the input what should be primary.
 */
MultiHitAlignedReadIterator(final CloseableIterator<SAMRecord> querynameOrderIterator,
                            final PrimaryAlignmentSelectionStrategy primaryAlignmentSelectionStrategy) {
    this.primaryAlignmentSelectionStrategy = primaryAlignmentSelectionStrategy;
    peekIterator = new PeekableIterator<>(new FilteringSamIterator(querynameOrderIterator,
            new SamRecordFilter() {
                // Filter unmapped reads.
                @Override
                public boolean filterOut(final SAMRecord record) {
                    return record.getReadUnmappedFlag() || SAMUtils.cigarMapsNoBasesToRef(record.getCigar());
                }
                @Override
                public boolean filterOut(final SAMRecord first, final SAMRecord second) {
                    return ((first.getReadUnmappedFlag() || SAMUtils.cigarMapsNoBasesToRef(first.getCigar()))
                            && (second.getReadUnmappedFlag() || SAMUtils.cigarMapsNoBasesToRef(second.getCigar())));
                }
            }));


    advance();
}
 
Example #7
Source File: PerTileParserTest.java    From picard with MIT License 6 votes vote down vote up
@Override
protected CloseableIterator<DummyDt> makeTileIterator(final File file) {
    return new CloseableIterator<DummyDt>() {
        private final Iterator<Integer> values = FILE_TO_VALUE.get(file.getName()).iterator();

        @Override
        public void close() {

        }

        @Override
        public boolean hasNext() {
            return values.hasNext();
        }

        @Override
        public DummyDt next() {
            return new DummyDt(values.next());
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}
 
Example #8
Source File: BamSlicerApplication.java    From hmftools with GNU General Public License v3.0 6 votes vote down vote up
private static void sliceFromVCF(@NotNull CommandLine cmd) throws IOException {
    String inputPath = cmd.getOptionValue(INPUT);
    String vcfPath = cmd.getOptionValue(VCF);
    int proximity = Integer.parseInt(cmd.getOptionValue(PROXIMITY, "500"));

    SamReaderFactory readerFactory = createFromCommandLine(cmd);
    SamReader reader = readerFactory.open(new File(inputPath));

    QueryInterval[] intervals = getIntervalsFromVCF(vcfPath, reader.getFileHeader(), proximity);
    CloseableIterator<SAMRecord> iterator = reader.queryOverlapping(intervals);
    SAMFileWriter writer = new SAMFileWriterFactory().setCreateIndex(true)
            .makeBAMWriter(reader.getFileHeader(), true, new File(cmd.getOptionValue(OUTPUT)));

    writeToSlice(writer, iterator);

    writer.close();
    reader.close();
}
 
Example #9
Source File: CollectSamErrorMetrics.java    From picard with MIT License 6 votes vote down vote up
/**
 * If there's a variant at the locus return true, otherwise false.
 * <p>
 * HAS SIDE EFFECTS!!! Queries the vcfFileReader
 *
 * @param vcfFileReader      a {@link VCFFileReader} to query for the given locus
 * @param locusInfo          a {@link SamLocusIterator.LocusInfo} at which to examine the variants
 * @return true if there's a variant over the locus, false otherwise.
 */
private static boolean checkLocus(final VCFFileReader vcfFileReader, final SamLocusIterator.LocusInfo locusInfo) {
    boolean overlaps = false;

    if (locusInfo != null) {
        try (final CloseableIterator<VariantContext> vcfIterator = vcfFileReader.query(locusInfo)) {

            while (vcfIterator.hasNext()) {
                if (vcfIterator.next().isFiltered()) {
                    continue;
                }
                overlaps = true;
                break;
            }
        }
    }
    return overlaps;
}
 
Example #10
Source File: AbstractVcfMergingClpTester.java    From picard with MIT License 6 votes vote down vote up
/**
 * Make sure that the order of the output file is identical to the order
 * of the input files by iterating through the output, making sure that,
 * if the context is an indel (snp), the next genomic position in the indel
 * (snp) queue is the same. Also make sure that the context is in the order
 * specified by the input files.
 */
private void validateSnpAndIndelResults(final File output, final Queue<String> indelContigPositions, final Queue<String> snpContigPositions) {
    final VCFFileReader outputReader = new VCFFileReader(output, false);
    final VariantContextComparator outputComparator = outputReader.getFileHeader().getVCFRecordComparator();
    VariantContext last = null;
    final CloseableIterator<VariantContext> iterator = outputReader.iterator();
    while (iterator.hasNext()) {
        final VariantContext outputContext = iterator.next();
        if (outputContext.isIndel()) Assert.assertEquals(getContigPosition(outputContext), indelContigPositions.poll());
        if (outputContext.isSNP()) Assert.assertEquals(getContigPosition(outputContext), snpContigPositions.poll());
        if (last != null) Assert.assertTrue(outputComparator.compare(last, outputContext) <= 0);
        last = outputContext;
    }
    iterator.close();

    // We should have polled everything off the indel (snp) queues
    Assert.assertEquals(indelContigPositions.size(), 0);
    Assert.assertEquals(snpContigPositions.size(), 0);
}
 
Example #11
Source File: MultiVariantDataSource.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Close any existing iterator, create a new iterator and update the local cached iterator reference.
 * @param iteratorFromSource function to retrieve individual iterator, to be applied to each data source
 * @return
 */
private Iterator<VariantContext> getMergedIteratorFromDataSources(
        final Function<FeatureDataSource<VariantContext>, Iterator<VariantContext>> iteratorFromSource) {

    // Tribble documentation states that having multiple iterators open simultaneously over the same FeatureReader
    // results in undefined behavior
    closeOpenIterationIfNecessary();

    if (featureDataSources.size() > 1) {
        final List<CloseableIterator<VariantContext>> iterators = new ArrayList<>(featureDataSources.size());
        featureDataSources.forEach(ds -> iterators.add(getCloseableIteratorWrapper(iteratorFromSource.apply((ds)))));

        final VariantContextComparator varComparator = new VariantContextComparator(getSequenceDictionary());
        currentIterator = new MergingIterator<>(varComparator, iterators);
    } else {
        currentIterator = getCloseableIteratorWrapper(iteratorFromSource.apply(featureDataSources.get(0)));
    }
    return currentIterator;
}
 
Example #12
Source File: MultiHitAlignedReadIterator.java    From picard with MIT License 6 votes vote down vote up
/**
 *
 * @param querynameOrderIterator
 * @param primaryAlignmentSelectionStrategy Algorithm for selecting primary alignment when it is not clear from
 *                                          the input what should be primary.
 */
MultiHitAlignedReadIterator(final CloseableIterator<SAMRecord> querynameOrderIterator,
                            final PrimaryAlignmentSelectionStrategy primaryAlignmentSelectionStrategy) {
    this.primaryAlignmentSelectionStrategy = primaryAlignmentSelectionStrategy;
    peekIterator = new PeekableIterator<SAMRecord>(new FilteringSamIterator(querynameOrderIterator,
            new SamRecordFilter() {
                // Filter unmapped reads.
                public boolean filterOut(final SAMRecord record) {
                    return record.getReadUnmappedFlag() || SAMUtils.cigarMapsNoBasesToRef(record.getCigar());
                }
                public boolean filterOut(final SAMRecord first, final SAMRecord second) {
                    return ((first.getReadUnmappedFlag() || SAMUtils.cigarMapsNoBasesToRef(first.getCigar()))
                            && (second.getReadUnmappedFlag() || SAMUtils.cigarMapsNoBasesToRef(second.getCigar())));
                }
            }));


    advance();
}
 
Example #13
Source File: CollapseTagWithContext.java    From Drop-seq with MIT License 6 votes vote down vote up
private PeekableGroupingIterator<SAMRecord> orderReadsByTagsPeekable (final SamReader reader, final String collapseTag, final List<String> contextTag, final int mapQuality, String outTag, ObjectSink<SAMRecord> uninformativeReadsSink) {
	// SORT on the context tags.
	StringTagComparator [] comparators = contextTag.stream().map(x -> new StringTagComparator(x)).toArray(StringTagComparator[]::new);
	final MultiComparator<SAMRecord> multiComparator = new MultiComparator<>(comparators);
	
	// set up filters.
       MapQualityPredicate mapQualityPredicate = CollapseTagWithContext.getMapQualityPredicate(mapQuality);
       RequiredTagPredicate requiredTagPredicate = CollapseTagWithContext.getRequiredTagPredicate(collapseTag, contextTag);
       
       // log progress on read iteration
       ProgressLogger progressLogger = new ProgressLogger(log);
       ProgressLoggingIterator progressLoggingIter = new ProgressLoggingIterator(reader.iterator(), progressLogger);
       
       // apply a default result tag to all reads - this is useful for reads that are not in the analysis and automatically sunk to the writer.
       DefaultTaggingIterator iter = new DefaultTaggingIterator(progressLoggingIter.iterator(), collapseTag, outTag);
       
       // reads that don't pass the filter are sunk, reads that pass are sorted and grouped.
	InformativeReadFilter filter = new InformativeReadFilter(iter, uninformativeReadsSink, mapQualityPredicate, requiredTagPredicate);				
			
	// sort and group the relevant data
	CloseableIterator<SAMRecord> sortedIter = SamRecordSortingIteratorFactory.create(
               reader.getFileHeader(), filter.iterator(), multiComparator, null);
	PeekableGroupingIterator<SAMRecord> groupedIterator = new PeekableGroupingIterator<>(sortedIter, multiComparator);		
	return groupedIterator;
	
}
 
Example #14
Source File: CustomBAMIterators.java    From Drop-seq with MIT License 6 votes vote down vote up
/**
 * If the file is sorter in query name order, return an iterator over
 * the file.  Otherwise, sort records in queryname order and return an iterator over those records.
 * @return An iterator over the records in the file, in queryname order.
 */

public static CloseableIterator<SAMRecord> getQuerynameSortedRecords(SamReader reader) {
	if (reader.getFileHeader().getSortOrder().equals(SortOrder.queryname)) {
		return reader.iterator();
	}
	log.info("Input SAM/BAM not in queryname order, sorting...");
	SAMSequenceDictionary dict= reader.getFileHeader().getSequenceDictionary();
	List<SAMProgramRecord> programs =reader.getFileHeader().getProgramRecords();
	
	final SAMFileHeader writerHeader = new SAMFileHeader();
       writerHeader.setSortOrder(SAMFileHeader.SortOrder.queryname);
       writerHeader.setSequenceDictionary(dict);
       for (SAMProgramRecord spr : programs) {
       	writerHeader.addProgramRecord(spr);
       }
       log.info("Reading in records for query name sorting");
       final ProgressLogger progressLogger = new ProgressLogger(log, 1000000, "Sorting reads in query name order");
       final CloseableIterator<SAMRecord> result =
               SamRecordSortingIteratorFactory.create(writerHeader, reader.iterator(), new SAMRecordQueryNameComparator(), progressLogger);
       log.info("Sorting finished.");
       return result; 
}
 
Example #15
Source File: CustomBAMIterators.java    From Drop-seq with MIT License 6 votes vote down vote up
public static CloseableIterator<SAMRecord> getReadsInTagOrder (SamReader reader, String primaryTag) {
	// SamReader reader = SamReaderFactory.makeDefault().open(bamFile);
	SAMSequenceDictionary dict= reader.getFileHeader().getSequenceDictionary();
	List<SAMProgramRecord> programs =reader.getFileHeader().getProgramRecords();
	
	final SAMFileHeader writerHeader = new SAMFileHeader();
       writerHeader.setSortOrder(SAMFileHeader.SortOrder.queryname);
       writerHeader.setSequenceDictionary(dict);
       for (SAMProgramRecord spr : programs) {
       	writerHeader.addProgramRecord(spr);
       }
       final ProgressLogger progressLogger = new ProgressLogger(log, 1000000);
       log.info("Reading in records for TAG name sorting");
       final CloseableIterator<SAMRecord> result =
               SamRecordSortingIteratorFactory.create(writerHeader, reader.iterator(), new StringTagComparator(primaryTag), progressLogger);

	log.info("Sorting finished.");
	return (result);
}
 
Example #16
Source File: SamReaderQueryingIterator.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private CloseableIterator<SAMRecord> loadNextIterator() {
    // The SamReader API requires us to close out the previous iterator over our reader before opening a new one.
    if ( currentIterator != null ) {
        currentIterator.close();
    }

    if ( hasQueryIntervals() && ! intervalQueryPerformed ) {
        intervalQueryPerformed = true;
        return reader.queryOverlapping(queryIntervals);
    }
    else if ( queryUnmapped && ! unmappedQueryPerformed ) {
        unmappedQueryPerformed = true;
        return reader.queryUnmapped();
    }

    return null;
}
 
Example #17
Source File: MultiVariantDataSource.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Wrap the sourceIterator in a CloseableIterator to make it usable as a MergingIterator source.
 */
private CloseableIterator<VariantContext> getCloseableIteratorWrapper(final Iterator<VariantContext> sourceIterator) {
    Utils.nonNull(sourceIterator);

    return new CloseableIterator<VariantContext>() {
        Iterator<VariantContext> delegateIterator = sourceIterator;
        @Override
        public void close() { delegateIterator = null; }

        @Override
        public boolean hasNext() {
            return delegateIterator != null && delegateIterator.hasNext();
        }

        @Override
        public VariantContext next() {
            if (!hasNext()) {
                throw new NoSuchElementException("hasNext should be called before next");
            }
            return delegateIterator.next();
        }
    };
}
 
Example #18
Source File: SortingIteratorFactory.java    From Drop-seq with MIT License 6 votes vote down vote up
/**
 *
 * @param componentType Required because of Java generic syntax limitations.
 * @param underlyingIterator All records are pulled from this iterator, which is then closed if closeable.
 * @param comparator Defines sort order.
 * @param codec For spilling to temp files
 * @param maxRecordsInRam
 * @param progressLogger Pass null if not interested in logging.
 * @return An iterator in the order defined by comparator, that will produce all the records from underlyingIterator.
 */
public static <T> CloseableIterator<T> create(final Class<T> componentType,
                                              final Iterator<T> underlyingIterator,
                                              final Comparator<T> comparator,
                                              final SortingCollection.Codec<T> codec,
                                              final int maxRecordsInRam,
                                              final ProgressCallback progressLogger) {

    SortingCollection<T> sortingCollection =
            SortingCollection.newInstance(componentType, codec, comparator, maxRecordsInRam);

    while (underlyingIterator.hasNext()) {
        final T rec = underlyingIterator.next();
        if (progressLogger != null)
progressLogger.logProgress(rec);
        sortingCollection.add(rec);
    }
    CloseableIterator<T> ret = sortingCollection.iterator();
    CloserUtil.close(underlyingIterator);
    return ret;
}
 
Example #19
Source File: VariantAccumulatorExecutor.java    From picard with MIT License 6 votes vote down vote up
@Override
public void run() {
    try {
        Optional<CloseableIterator<VariantContext>> readerMaybe;
        while ((readerMaybe = vcIterators.next()).isPresent()) {
            final CloseableIterator<VariantContext> reader = readerMaybe.get();
            while (reader.hasNext()) processor.accumulate(reader.next());
            reader.close();

            if (!childrenErrors.isEmpty()) {
                LOG.error(Thread.currentThread() + " aborting: observed error in another child thread.");
                break;
            }
        }
    } catch (final Throwable e) {
        childrenErrors.add(e);
        LOG.error(e, "Unexpected exception encountered in child thread.");
    } finally {
        LOG.debug(String.format("Thread %s is finishing.", Thread.currentThread()));
    }
}
 
Example #20
Source File: MachineOrientationTest.java    From rtg-tools with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private SAMRecord readOneSamRecord(String samstr) throws IOException {
  final File input = FileUtils.createTempDir("testcheck", "sv_in");
  try {
    FileUtils.stringToFile(samstr, new File(input, OUT_SAM));

    final String inn = input.getPath();

    final SAMRecord rec;
    final File samfile = new File(inn + FS + OUT_SAM);
    try (SamReader reader = SamUtils.makeSamReader(FileUtils.createInputStream(samfile, false))) {
      final CloseableIterator<SAMRecord> iterator = reader.iterator();
      if (iterator.hasNext()) {
        rec = iterator.next();
      } else {
        rec = null;
      }

    }
    return rec;
  } finally {
    FileHelper.deleteAll(input);
  }
}
 
Example #21
Source File: CpxVariantReInterpreterSparkIntegrationTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static void vcfEquivalenceTest(final String generatedVCFPath, final String expectedVCFPath,
                                       final List<String> attributesToIgnore, final boolean onHDFS) throws Exception {

    List<VariantContext> expectedVcs;
    try (final VCFFileReader fileReader = new VCFFileReader(new File(expectedVCFPath), false) ) {
        try (final CloseableIterator<VariantContext> iterator = fileReader.iterator()) {
            expectedVcs = Utils.stream(iterator).collect(Collectors.toList());
        }
    }

    final List<VariantContext> actualVcs = StructuralVariationDiscoveryPipelineSparkIntegrationTest
            .extractActualVCs(generatedVCFPath, onHDFS);

    GATKBaseTest.assertCondition(actualVcs, expectedVcs,
            (a, e) -> VariantContextTestUtils.assertVariantContextsAreEqual(a, e, attributesToIgnore, Collections.emptyList()));
}
 
Example #22
Source File: UpdateVcfSequenceDictionary.java    From picard with MIT License 5 votes vote down vote up
@Override
protected int doWork() {
    IOUtil.assertFileIsReadable(INPUT);
    IOUtil.assertFileIsReadable(SEQUENCE_DICTIONARY);
    IOUtil.assertFileIsWritable(OUTPUT);

    final SAMSequenceDictionary samSequenceDictionary = SAMSequenceDictionaryExtractor.extractDictionary(SEQUENCE_DICTIONARY.toPath());

    final VCFFileReader fileReader = new VCFFileReader(INPUT, false);
    final VCFHeader fileHeader = fileReader.getFileHeader();

    final VariantContextWriterBuilder builder = new VariantContextWriterBuilder()
            .setReferenceDictionary(samSequenceDictionary)
            .clearOptions();
    if (CREATE_INDEX)
        builder.setOption(Options.INDEX_ON_THE_FLY);

    final VariantContextWriter vcfWriter = builder.setOutputFile(OUTPUT).build();
    fileHeader.setSequenceDictionary(samSequenceDictionary);
    vcfWriter.writeHeader(fileHeader);

    final ProgressLogger progress = new ProgressLogger(log, 10000);
    final CloseableIterator<VariantContext> iterator = fileReader.iterator();
    while (iterator.hasNext()) {
        final VariantContext context = iterator.next();
        vcfWriter.add(context);
        progress.record(context.getContig(), context.getStart());
    }

    CloserUtil.close(iterator);
    CloserUtil.close(fileReader);
    vcfWriter.close();

    return 0;
}
 
Example #23
Source File: AsyncIterator.java    From picard with MIT License 5 votes vote down vote up
public AsyncIterator(final CloseableIterator<T> underlyingIterator,
                        final int queueSize,
                        final String threadNamePrefix) {
    this.underlyingIterator = underlyingIterator;
    this.queue = new ArrayBlockingQueue<T>(queueSize);
    this.readerRunnable = new ReaderRunnable();
    this.reader = new Thread(readerRunnable, threadNamePrefix + threadsCreated++);
    this.reader.setDaemon(true);
    this.reader.start();
    getNext();
}
 
Example #24
Source File: VariantIteratorProducer.java    From picard with MIT License 5 votes vote down vote up
/**
 * Converts a {@link VcfFileSegment} into a {@link VariantContext} iterator.  Applies filtering via {@link #intervalsOfInterestDetector}
 * if it is defined.
 */
private CloseableIterator<VariantContext> iteratorForSegment(final VcfFileSegment segment) {
    final CloseableIterator<VariantContext> query =
            localVcfFileReaders.get() // Get the collection of VCF file readers local to this thread
                    .get(segment.vcf()) // Get or generate the reader for this segment's VCF file
                    .query(segment.contig(), segment.start(), segment.stop()); // Query the segment

    // Then wrap the iterator in a on-the-fly interval-list based filter, if requested.
    final Collection<Predicate<VariantContext>> filters = new ArrayList<>();
    if (intervalsOfInterestDetector != null) {
        filters.add(new OverlapsPredicate());
    }
    filters.add(new NonUniqueVariantPredicate(segment));
    return new PredicateFilterDecoratingClosableIterator<>(query, filters);
}
 
Example #25
Source File: PredicateFilterDecoratingClosableIterator.java    From picard with MIT License 5 votes vote down vote up
public PredicateFilterDecoratingClosableIterator(final CloseableIterator<T> underlyingIterator, final Collection<Predicate<T>> predicates) {
    Preconditions.checkArgument(!predicates.isEmpty(), "predicates must not be empty");
    Iterator<T> nestedPredicateIterator = underlyingIterator;
    for (final Predicate<T> predicate : predicates) {
       nestedPredicateIterator = Iterators.filter(nestedPredicateIterator, predicate);   
    }
    filteredIterator = nestedPredicateIterator;
    
    this.underlyingIterator = underlyingIterator;
}
 
Example #26
Source File: SamRestrictingIterator.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * @param iterator source of records
 * @param regions the restriction regions
 */
public SamRestrictingIterator(CloseableIterator<SAMRecord> iterator, ReferenceRanges<String> regions) {
  if (regions == null || iterator == null) {
    throw new NullPointerException();
  }
  //System.out.println("SamRestrictingIterator to ranges: " + regions);
  mRegions = regions;
  mIterator = iterator;
  for (int templateId : regions.sequenceIds()) {
    mEndTemplate = templateId;
  }
  populateNext();
}
 
Example #27
Source File: FilterParser.java    From picard with MIT License 5 votes vote down vote up
/** Wrap a filterFile reader in a closeable iterator and return it*/
@Override
protected CloseableIterator<PfData> makeTileIterator(final File iterator) {
    return new CloseableIterator<PfData>() {
        private FilterFileReader reader = new FilterFileReader(iterator);

        public void close() {
            reader = null;
        }

        public boolean hasNext() {
            return reader.hasNext();
        }

        public PfData next() {
            final boolean nextValue = reader.next();
            return new PfData() {
                public boolean isPf() {
                    return nextValue;
                }
            };
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}
 
Example #28
Source File: AbstractVcfMergingClpTester.java    From picard with MIT License 5 votes vote down vote up
static Queue<String> loadContigPositions(final File inputFile) {
	final VCFFileReader reader = new VCFFileReader(inputFile, false);
	final Queue<String> contigPositions = new LinkedList<String>();
	final CloseableIterator<VariantContext> iterator = reader.iterator();
	while (iterator.hasNext()) contigPositions.add(getContigPosition(iterator.next()));
	iterator.close();
	reader.close();
	return contigPositions;
}
 
Example #29
Source File: SamMultiRestrictingIterator.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void closeCurrent() {
  if (mCurrentIt != null) {
    final CloseableIterator<SAMRecord> currentIt = mCurrentIt;
    mCurrentIt = null;
    currentIt.close();
  }
}
 
Example #30
Source File: ReadsPathDataSource.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Shut down this data source permanently, closing all iterations and readers.
 */
@Override
public void close() {
    closePreviousIterationsIfNecessary();

    try {
        for ( Map.Entry<SamReader, CloseableIterator<SAMRecord>> readerEntry : readers.entrySet() ) {
            readerEntry.getKey().close();
        }
    }
    catch ( IOException e ) {
        throw new GATKException("Error closing SAMReader");
    }
}