Search in sources :

Example 46 with SAMRecord

use of htsjdk.samtools.SAMRecord in project gatk by broadinstitute.

the class EarliestFragmentPrimaryAlignmentSelectionStrategy method pickPrimaryAlignment.

@Override
public void pickPrimaryAlignment(final HitsForInsert hitsForInsert) {
    if (hitsForInsert.numHits() == 0)
        throw new IllegalArgumentException("No alignments to pick from");
    // Gather the earliest alignment(s) with best MAPQ
    final List<Integer> earliestAlignments = new ArrayList<>();
    int earliestMappedBase = Integer.MAX_VALUE;
    int bestMapQ = -1;
    for (int i = 0; i < hitsForInsert.numHits(); ++i) {
        final SAMRecord rec = hitsForInsert.getFragment(i);
        if (rec.getReadUnmappedFlag())
            continue;
        final int thisFirstMappedBase = getIndexOfFirstAlignedBase(rec);
        final int thisMapQ = rec.getMappingQuality();
        if (thisFirstMappedBase < earliestMappedBase || (thisFirstMappedBase == earliestMappedBase && thisMapQ > bestMapQ)) {
            earliestAlignments.clear();
            earliestAlignments.add(i);
            earliestMappedBase = thisFirstMappedBase;
            bestMapQ = thisMapQ;
        } else if (thisFirstMappedBase == earliestMappedBase && thisMapQ == bestMapQ) {
            earliestAlignments.add(i);
        }
    // else it is not the earliest or the best so skip it.
    }
    if (earliestAlignments.size() == 1) {
        // If only one best, pick it.
        hitsForInsert.setPrimaryAlignment(earliestAlignments.get(0));
    } else {
        // Arbitrarily select one of the best
        hitsForInsert.setPrimaryAlignment(earliestAlignments.get(random.nextInt(earliestAlignments.size())));
    }
}
Also used : SAMRecord(htsjdk.samtools.SAMRecord) ArrayList(java.util.ArrayList)

Example 47 with SAMRecord

use of htsjdk.samtools.SAMRecord in project gatk by broadinstitute.

the class HaplotypeBAMWriterUnitTest method getReadCounts.

private int getReadCounts(final String resultFileName) throws IOException {
    final File path = new File(resultFileName);
    IOUtil.assertFileIsReadable(path);
    int count = 0;
    try (final SamReader in = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT).open(path)) {
        for (@SuppressWarnings("unused") final SAMRecord rec : in) {
            count++;
        }
    }
    return count;
}
Also used : SamReader(htsjdk.samtools.SamReader) SAMRecord(htsjdk.samtools.SAMRecord) File(java.io.File)

Example 48 with SAMRecord

use of htsjdk.samtools.SAMRecord in project gatk by broadinstitute.

the class CollectQualityYieldMetrics method doWork.

/**
     * Main method for the program.  Checks that all input files are present and
     * readable and that the output file can be written to.  Then iterates through
     * all the records accumulating metrics.  Finally writes metrics file
     */
@Override
protected Object doWork() {
    final ProgressLogger progress = new ProgressLogger(logger);
    File output = new File(qualityYieldMetricsArgs.output);
    // Some quick parameter checking
    IOUtil.assertFileIsReadable(INPUT);
    IOUtil.assertFileIsWritable(output);
    logger.info("Reading input file and calculating metrics.");
    final SamReader sam = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT);
    final MetricsFile<QualityYieldMetrics, Integer> metricsFile = getMetricsFile();
    final QualityYieldMetrics metrics = new QualityYieldMetrics();
    metrics.setUseOriginalQualities(qualityYieldMetricsArgs.useOriginalQualities);
    for (final SAMRecord rec : sam) {
        metrics.addRead(new SAMRecordToGATKReadAdapter(rec));
        progress.record(rec);
    }
    metrics.finish();
    metricsFile.addMetric(metrics);
    metricsFile.write(output);
    return null;
}
Also used : SamReader(htsjdk.samtools.SamReader) QualityYieldMetrics(org.broadinstitute.hellbender.metrics.QualityYieldMetrics) SAMRecordToGATKReadAdapter(org.broadinstitute.hellbender.utils.read.SAMRecordToGATKReadAdapter) SAMRecord(htsjdk.samtools.SAMRecord) ProgressLogger(org.broadinstitute.hellbender.utils.runtime.ProgressLogger) MetricsFile(htsjdk.samtools.metrics.MetricsFile) File(java.io.File)

Example 49 with SAMRecord

use of htsjdk.samtools.SAMRecord in project gatk by broadinstitute.

the class CreateHadoopBamSplittingIndex method createBaiAndSplittingIndex.

private static void createBaiAndSplittingIndex(final File inputBam, final File index, final int granularity, final ValidationStringency readValidationStringency) {
    assertIsBam(inputBam);
    try (SamReader reader = SamReaderFactory.makeDefault().validationStringency(readValidationStringency).setOption(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS, true).open(inputBam);
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(index))) {
        final SAMFileHeader header = reader.getFileHeader();
        assertBamIsCoordinateSorted(header);
        final SplittingBAMIndexer indexer = new SplittingBAMIndexer(out, granularity);
        final BAMIndexer bamIndexer = new BAMIndexer(IOUtils.replaceExtension(index, BAMIndex.BAMIndexSuffix), header);
        for (final SAMRecord read : reader) {
            indexer.processAlignment(read);
            bamIndexer.processAlignment(read);
        }
        indexer.finish(inputBam.length());
        bamIndexer.finish();
    } catch (final IOException e) {
        throw new UserException("Couldn't create splitting index", e);
    }
}
Also used : SamReader(htsjdk.samtools.SamReader) FileOutputStream(java.io.FileOutputStream) BAMIndexer(htsjdk.samtools.BAMIndexer) SplittingBAMIndexer(org.seqdoop.hadoop_bam.SplittingBAMIndexer) SAMRecord(htsjdk.samtools.SAMRecord) IOException(java.io.IOException) UserException(org.broadinstitute.hellbender.exceptions.UserException) SAMFileHeader(htsjdk.samtools.SAMFileHeader) BufferedOutputStream(java.io.BufferedOutputStream) SplittingBAMIndexer(org.seqdoop.hadoop_bam.SplittingBAMIndexer)

Example 50 with SAMRecord

use of htsjdk.samtools.SAMRecord in project gatk by broadinstitute.

the class CRAMSupportIntegrationTest method checkReadNames.

private void checkReadNames(final File outputFile, final File reference, final List<String> expectedReadNames) throws IOException {
    List<String> actualReadNames = new ArrayList<>();
    try (final SamReader reader = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT).referenceSequence(reference).open(outputFile)) {
        for (SAMRecord read : reader) {
            actualReadNames.add(read.getReadName());
        }
    }
    Assert.assertEquals(actualReadNames, expectedReadNames, "Read names in output do not match expected read names");
}
Also used : SamReader(htsjdk.samtools.SamReader) SAMRecord(htsjdk.samtools.SAMRecord) ArrayList(java.util.ArrayList)

Aggregations

SAMRecord (htsjdk.samtools.SAMRecord)53 SamReader (htsjdk.samtools.SamReader)31 File (java.io.File)20 Test (org.testng.annotations.Test)15 ProgressLogger (org.broadinstitute.hellbender.utils.runtime.ProgressLogger)12 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)11 UserException (org.broadinstitute.hellbender.exceptions.UserException)10 SAMFileHeader (htsjdk.samtools.SAMFileHeader)9 SAMFileWriter (htsjdk.samtools.SAMFileWriter)9 MetricsFile (htsjdk.samtools.metrics.MetricsFile)9 ArrayList (java.util.ArrayList)8 CommandLineProgramTest (org.broadinstitute.hellbender.CommandLineProgramTest)6 SAMReadGroupRecord (htsjdk.samtools.SAMReadGroupRecord)4 SamReaderFactory (htsjdk.samtools.SamReaderFactory)4 GATKRead (org.broadinstitute.hellbender.utils.read.GATKRead)4 SAMRecordToGATKReadAdapter (org.broadinstitute.hellbender.utils.read.SAMRecordToGATKReadAdapter)4 BAMRecordCodec (htsjdk.samtools.BAMRecordCodec)3 SAMFileWriterFactory (htsjdk.samtools.SAMFileWriterFactory)3 SAMRecordQueryNameComparator (htsjdk.samtools.SAMRecordQueryNameComparator)3 Histogram (htsjdk.samtools.util.Histogram)3