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())));
}
}
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;
}
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;
}
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);
}
}
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");
}
Aggregations