@Test(expectedExceptions = {SAMException.class}, dataProvider = "TestFailReference") public void testFailGet(final String fileName, final int index1, final int index2) throws SAMException { final Path refPath = Paths.get(fileName); final ReferenceSequenceFileWalker refWalker = new ReferenceSequenceFileWalker(refPath); try { refWalker.get(index1); refWalker.get(index2); } finally { CloserUtil.close(refWalker); } }
/** * Constructor that takes a {@link ReferenceSequenceFile} and a {@link SamLocusIterator}. * The inputs must have equal {@link htsjdk.samtools.SAMSequenceDictionary SAMSequenceDictionary}s and an {@link IllegalArgumentException} * will be thrown otherwise. * * @param referenceFile * @param locusIterator * * @throws IllegalArgumentException if arguments have non-equal {@link htsjdk.samtools.SAMSequenceDictionary SAMSequenceDictionary}s */ public SamLocusAndReferenceIterator(final ReferenceSequenceFileWalker referenceFile, final SamLocusIterator locusIterator) throws IllegalArgumentException { if(!SequenceUtil.areSequenceDictionariesEqual( locusIterator.getHeader().getSequenceDictionary(), referenceFile.getSequenceDictionary())) { throw new IllegalArgumentException("reference and locus iterator have difference dictionaries." + locusIterator.getHeader().getSequenceDictionary().toString() + referenceFile.getSequenceDictionary().toString()); } this.referenceSequenceFileWalker = referenceFile; this.locusIterator = locusIterator; }
} else { IOUtil.assertFileIsReadable(referenceSequence); walker = new ReferenceSequenceFileWalker(referenceSequence); walker.getSequenceDictionary()); ref = null; } else { ref = walker.get(rec.getReferenceIndex());
protected void resetRefSeqFileWalker() { this.refSeq = new ReferenceSequenceFileWalker(referenceFasta); }
/** Calculates and sets UQ tag from the record and the reference * * @param record the record to be fixed * @param refSeqWalker a ReferenceSequenceWalker that will be used to traverse the reference * @param isBisulfiteSequence a flag indicating whether the sequence came from bisulfite-sequencing. * * No return value, modifies the provided record. */ public static void fixUq(final SAMRecord record, final ReferenceSequenceFileWalker refSeqWalker, final boolean isBisulfiteSequence) { if (record.getBaseQualities() != SAMRecord.NULL_QUALS) { final byte[] referenceBases = refSeqWalker.get(record.getReferenceIndex()).getBases(); record.setAttribute(SAMTag.UQ.name(), SequenceUtil.sumQualitiesOfMismatches(record, referenceBases, 0, isBisulfiteSequence)); } }
@DataProvider public Object[][] testOverlappingErrorCalculatorWithManyReadsData() throws IOException { final File temp = File.createTempFile("Overlapping", ".bam"); temp.deleteOnExit(); try ( final ReferenceSequenceFileWalker referenceSequenceFileWalker = new ReferenceSequenceFileWalker(new File("testdata/picard/sam/BamErrorMetrics/chrM.reference.fasta"))) { final SAMRecordSetBuilder builder = new SAMRecordSetBuilder(); builder.getHeader().setSequenceDictionary(referenceSequenceFileWalker.getSequenceDictionary()); for (int i = 0; i < 4000; i++) { builder.addPair("Read" + i, 0, 1, 1, false, false, "36M", "36M", true, false, 20); } try (final SAMFileWriter writer = new SAMFileWriterFactory() .setCompressionLevel(2) .makeBAMWriter(builder.getHeader(), false, temp)) { builder.forEach(writer::addAlignment); } } return new Object[][]{{temp}}; }
IOUtil.assertFileIsReadable(REFERENCE_SEQUENCE); final ReferenceSequenceFileWalker refWalker = new ReferenceSequenceFileWalker(REFERENCE_SEQUENCE); final SamReader in = SamReaderFactory.makeDefault().open(INPUT); refWalker.getSequenceDictionary()); final byte[] bases = refWalker.get(info.getSequenceIndex()).getBases(); if (pos <= CONTEXT_SIZE|| pos > bases.length - CONTEXT_SIZE) continue;
protected void resetRefSeqFileWalker() { this.refSeq = new ReferenceSequenceFileWalker(referenceFasta); }
/** * Ensure that the requested sequence is loaded. Throws an exception if out-of-order * request is made, or if there is a mismatch between the requested name and the name * found in the ReferenceSequenceFile */ public ReferenceSequence get(final int sequenceIndex, final String sequenceName, final int length) { // Has the side-effect of setting referenceSequence member get(sequenceIndex); if (!referenceSequence.getName().equals(sequenceName)) { // Sanity check the sequence names against the sequence dictionary while scanning through. throw new SAMException("Sequence name mismatch at sequence index (" + referenceSequence.getContigIndex() + ", " + referenceSequence.getName() + ") != " + sequenceName); } if (referenceSequence.getBases().length != length) { throw new SAMException("Sequence length mismatch for (" + sequenceIndex + ", " + sequenceName + "). expected " + length + " but found " + referenceSequence.getBases().length); } return referenceSequence; }
final ReferenceSequenceFileWalker referenceSequenceFileWalker = new ReferenceSequenceFileWalker(REFERENCE_SEQUENCE); final PeekableIterator<VariantContext> vcfIterator = new PeekableIterator<>( VCF == null ? Collections.emptyIterator() : new VCFFileReader(VCF, true).iterator()) ) { final SAMSequenceDictionary sequenceDictionary = referenceSequenceFileWalker.getSequenceDictionary(); if (sam.getFileHeader().getSortOrder() != SAMFileHeader.SortOrder.coordinate) { throw new PicardException("Input BAM must be sorted by coordinate");
IOUtil.assertFileIsReadable(REFERENCE_SEQUENCE); final ReferenceSequenceFileWalker refWalker = new ReferenceSequenceFileWalker(REFERENCE_SEQUENCE); final SamReader in = SamReaderFactory.makeDefault().open(INPUT); refWalker.getSequenceDictionary()); final byte[] bases = refWalker.get(info.getSequenceIndex()).getBases(); if (pos <= CONTEXT_SIZE|| pos > bases.length - CONTEXT_SIZE) continue;
@Test(dataProvider = "TestReference") public void testGetFile(final String fileName, final int index1, final int index2) throws SAMException { final File refFile = new File(fileName); final ReferenceSequenceFileWalker refWalker = new ReferenceSequenceFileWalker(refFile); ReferenceSequence sequence = refWalker.get(index1); Assert.assertEquals(sequence.getContigIndex(), index1); sequence = refWalker.get(index2); Assert.assertEquals(sequence.getContigIndex(), index2); CloserUtil.close(refWalker); }
static ReferenceSequenceFileWalker getReferenceSequenceFileWalker(String referenceString){ ReferenceSequenceFile referenceSequenceFile = createReferenceSequenceFile(referenceString); return new ReferenceSequenceFileWalker(referenceSequenceFile); }
/** * Ensure that the requested sequence is loaded. Throws an exception if out-of-order * request is made, or if there is a mismatch between the requested name and the name * found in the ReferenceSequenceFile */ public ReferenceSequence get(final int sequenceIndex, final String sequenceName, final int length) { // Has the side-effect of setting referenceSequence member get(sequenceIndex); if (!referenceSequence.getName().equals(sequenceName)) { // Sanity check the sequence names against the sequence dictionary while scanning through. throw new SAMException("Sequence name mismatch at sequence index (" + referenceSequence.getContigIndex() + ", " + referenceSequence.getName() + ") != " + sequenceName); } if (referenceSequence.getBases().length != length) { throw new SAMException("Sequence length mismatch for (" + sequenceIndex + ", " + sequenceName + "). expected " + length + " but found " + referenceSequence.getBases().length); } return referenceSequence; }
final ReferenceSequenceFileWalker referenceSequenceFileWalker = new ReferenceSequenceFileWalker(REFERENCE_SEQUENCE); final PeekableIterator<VariantContext> vcfIterator = new PeekableIterator<>( VCF == null ? Collections.emptyIterator() : new VCFFileReader(VCF, true).iterator()) ) { final SAMSequenceDictionary sequenceDictionary = referenceSequenceFileWalker.getSequenceDictionary(); if (sam.getFileHeader().getSortOrder() != SAMFileHeader.SortOrder.coordinate) { throw new PicardException("Input BAM must be sorted by coordinate");
/** * Constructor that takes a {@link ReferenceSequenceFile} and a {@link SamLocusIterator}. * The inputs must have equal {@link htsjdk.samtools.SAMSequenceDictionary SAMSequenceDictionary}s and an {@link IllegalArgumentException} * will be thrown otherwise. * * @param referenceFile * @param locusIterator * * @throws IllegalArgumentException if arguments have non-equal {@link htsjdk.samtools.SAMSequenceDictionary SAMSequenceDictionary}s */ public SamLocusAndReferenceIterator(final ReferenceSequenceFileWalker referenceFile, final SamLocusIterator locusIterator) throws IllegalArgumentException { if(!SequenceUtil.areSequenceDictionariesEqual( locusIterator.getHeader().getSequenceDictionary(), referenceFile.getSequenceDictionary())) { throw new IllegalArgumentException("reference and locus iterator have difference dictionaries." + locusIterator.getHeader().getSequenceDictionary().toString() + referenceFile.getSequenceDictionary().toString()); } this.referenceSequenceFileWalker = referenceFile; this.locusIterator = locusIterator; }
} else { IOUtil.assertFileIsReadable(referenceSequence); walker = new ReferenceSequenceFileWalker(referenceSequence); walker.getSequenceDictionary()); ref = null; } else { ref = walker.get(rec.getReferenceIndex());
@Test(dataProvider = "TestReference") public void testGet(final String fileName, final int index1, final int index2) throws SAMException { final Path refPath = Paths.get(fileName); final ReferenceSequenceFileWalker refWalker = new ReferenceSequenceFileWalker(refPath); ReferenceSequence sequence = refWalker.get(index1); Assert.assertEquals(sequence.getContigIndex(), index1); sequence = refWalker.get(index2); Assert.assertEquals(sequence.getContigIndex(), index2); CloserUtil.close(refWalker); }
public enum UnmappingReadStrategy { // Leave on record, and copy to tag COPY_TO_TAG(false, true), // Leave on record, but do not create additional tag DO_NOT_CHANGE(false, false), // Add tag with information, and remove from standard fields in record MOVE_TO_TAG(true, true); private final boolean resetMappingInformation, populatePATag; UnmappingReadStrategy(final boolean resetMappingInformation, final boolean populatePATag) { this.resetMappingInformation = resetMappingInformation; this.populatePATag = populatePATag; } public boolean isResetMappingInformation() { return resetMappingInformation; } public boolean isPopulatePaTag() { return populatePATag; } }
/** * Ensure that the requested sequence is loaded. Throws an exception if out-of-order * request is made, or if there is a mismatch between the requested name and the name * found in the ReferenceSequenceFile */ public ReferenceSequence get(final int sequenceIndex, final String sequenceName, final int length) { // Has the side-effect of setting referenceSequence member get(sequenceIndex); if (!referenceSequence.getName().equals(sequenceName)) { // Sanity check the sequence names against the sequence dictionary while scanning through. throw new SAMException("Sequence name mismatch at sequence index (" + referenceSequence.getContigIndex() + ", " + referenceSequence.getName() + ") != " + sequenceName); } if (referenceSequence.getBases().length != length) { throw new SAMException("Sequence length mismatch for (" + sequenceIndex + ", " + sequenceName + "). expected " + length + " but found " + referenceSequence.getBases().length); } return referenceSequence; }