Java Code Examples for htsjdk.samtools.SAMRecordSetBuilder#setReadLength()

The following examples show how to use htsjdk.samtools.SAMRecordSetBuilder#setReadLength() . 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: QuerySortedReadPairIteratorUtilTest.java    From picard with MIT License 6 votes vote down vote up
@Test
public void testBasicPairedRead() {
    SAMRecordSetBuilder builder = new SAMRecordSetBuilder(false, SAMFileHeader.SortOrder.queryname);
    builder.setReadLength(READ_LENGTH);
    builder.addPair("mapped_paired", 1, 1, 31);
    PeekableIterator<SAMRecord> iterator = new PeekableIterator<SAMRecord>(builder.iterator());

    QuerySortedReadPairIteratorUtil.ReadPair pair = QuerySortedReadPairIteratorUtil.getNextReadPair(iterator);
    Assert.assertNotNull(pair);
    Assert.assertNotNull(pair.read1);
    Assert.assertNotNull(pair.read2);
    Assert.assertEquals("mapped_paired", pair.read1.getReadName());
    Assert.assertEquals("mapped_paired", pair.read2.getReadName());

    pair = QuerySortedReadPairIteratorUtil.getNextReadPair(iterator);
    Assert.assertNull(pair);
}
 
Example 2
Source File: QuerySortedReadPairIteratorUtilTest.java    From picard with MIT License 6 votes vote down vote up
@Test
public void testBasicUnmappedReadPair() {
    SAMRecordSetBuilder builder = new SAMRecordSetBuilder(false, SAMFileHeader.SortOrder.queryname);
    builder.setReadLength(READ_LENGTH);
    builder.addUnmappedPair("unmapped_paired");
    PeekableIterator<SAMRecord> iterator = new PeekableIterator<SAMRecord>(builder.iterator());

    QuerySortedReadPairIteratorUtil.ReadPair pair = QuerySortedReadPairIteratorUtil.getNextReadPair(iterator);
    Assert.assertNotNull(pair);
    Assert.assertNotNull(pair.read1);
    Assert.assertNotNull(pair.read2);
    Assert.assertEquals("unmapped_paired", pair.read1.getReadName());
    Assert.assertEquals("unmapped_paired", pair.read2.getReadName());

    pair = QuerySortedReadPairIteratorUtil.getNextReadPair(iterator);
    Assert.assertNull(pair);
}
 
Example 3
Source File: QuerySortedReadPairIteratorUtilTest.java    From picard with MIT License 6 votes vote down vote up
@Test
public void testBasicHalfmappedReadPair() {
    SAMRecordSetBuilder builder = new SAMRecordSetBuilder(false, SAMFileHeader.SortOrder.queryname);
    builder.setReadLength(READ_LENGTH);
    builder.addPair("halfmapped_paired", 1, 1, 31, false, true, "20M", "20M", true, false, 20);
    PeekableIterator<SAMRecord> iterator = new PeekableIterator<SAMRecord>(builder.iterator());

    QuerySortedReadPairIteratorUtil.ReadPair pair = QuerySortedReadPairIteratorUtil.getNextReadPair(iterator);
    Assert.assertNotNull(pair);
    Assert.assertNotNull(pair.read1);
    Assert.assertNotNull(pair.read2);
    Assert.assertEquals("halfmapped_paired", pair.read1.getReadName());
    Assert.assertEquals("halfmapped_paired", pair.read2.getReadName());

    pair = QuerySortedReadPairIteratorUtil.getNextReadPair(iterator);
    Assert.assertNull(pair);
}
 
Example 4
Source File: QuerySortedReadPairIteratorUtilTest.java    From picard with MIT License 6 votes vote down vote up
@Test
public void testFragmentNoReadPair() {
    SAMRecordSetBuilder builder = new SAMRecordSetBuilder(false, SAMFileHeader.SortOrder.queryname);
    builder.setReadLength(READ_LENGTH);
    builder.addFrag("mapped_frag_a", 1, 1, false);
    builder.addFrag("mapped_frag_b", 1, 1, false);
    PeekableIterator<SAMRecord> iterator = new PeekableIterator<SAMRecord>(builder.iterator());

    QuerySortedReadPairIteratorUtil.ReadPair pair = QuerySortedReadPairIteratorUtil.getNextReadPair(iterator);
    Assert.assertNotNull(pair);
    Assert.assertNotNull(pair.read1);
    Assert.assertNull(pair.read2);
    Assert.assertEquals("mapped_frag_a", pair.read1.getReadName());

    pair = QuerySortedReadPairIteratorUtil.getNextReadPair(iterator);
    Assert.assertNotNull(pair);
    Assert.assertNotNull(pair.read1);
    Assert.assertNull(pair.read2);
    Assert.assertEquals("mapped_frag_b", pair.read1.getReadName());

    pair = QuerySortedReadPairIteratorUtil.getNextReadPair(iterator);
    Assert.assertNull(pair);
}
 
Example 5
Source File: AbstractAlignmentMergerTest.java    From picard with MIT License 4 votes vote down vote up
@Test(dataProvider = "overlapReadData")
public void testOverlappedReadClipping(final int readLength, final int start1, final int start2, final String cigar1, final String cigar2,
                                       final boolean strand1, final boolean strand2,
                                       final int r1ExpectedAlignmentStart, final int r2ExpectedAlignmentStart,
                                       final String expectedR1Cigar, final String expectedR2Cigar, final boolean hardClipOverlappingReads,
                                       final String read1Bases, final String read2Bases, final String expectedR1Bases, final String expectedR2Bases,
                                       final String expectedR1ClippedBases, final String expectedR2ClippedBases, final String read1Qualities,
                                       final String read2Qualities, final String expectedR1Qualities, final String expectedR2Qualities,
                                       final String expectedR1ClippedQualities, final String expectedR2ClippedQualities) {

    final SAMRecordSetBuilder set = new SAMRecordSetBuilder();
    set.setReadLength(readLength);
    final List<SAMRecord> recs = set.addPair("q1", 0, start1, start2, false, false, cigar1, cigar2, strand1, strand2, 30);
    final SAMRecord r1 = recs.get(0);
    final SAMRecord r2 = recs.get(1);

    r1.setReadBases(StringUtil.stringToBytes(read1Bases));
    r2.setReadBases(StringUtil.stringToBytes(read2Bases));

    r1.setBaseQualities(SAMUtils.fastqToPhred(read1Qualities));
    r2.setBaseQualities(SAMUtils.fastqToPhred(read2Qualities));

    AbstractAlignmentMerger.clipForOverlappingReads(r1, r2, hardClipOverlappingReads);
    Assert.assertEquals(r1.getAlignmentStart(), r1ExpectedAlignmentStart);
    Assert.assertEquals(r1.getCigarString(), expectedR1Cigar);
    Assert.assertEquals(r2.getAlignmentStart(), r2ExpectedAlignmentStart);
    Assert.assertEquals(r2.getCigarString(), expectedR2Cigar);
    Assert.assertEquals(r1.getReadString(), expectedR1Bases);
    Assert.assertEquals(r2.getReadString(), expectedR2Bases);
    Assert.assertEquals(SAMUtils.phredToFastq(r1.getBaseQualities()), expectedR1Qualities);
    Assert.assertEquals(SAMUtils.phredToFastq(r2.getBaseQualities()), expectedR2Qualities);

    Assert.assertEquals(r1.getAttribute(AbstractAlignmentMerger.HARD_CLIPPED_BASES_TAG), expectedR1ClippedBases);
    Assert.assertEquals(r2.getAttribute(AbstractAlignmentMerger.HARD_CLIPPED_BASES_TAG), expectedR2ClippedBases);

    Assert.assertEquals(r1.getAttribute(AbstractAlignmentMerger.HARD_CLIPPED_BASE_QUALITIES_TAG), expectedR1ClippedQualities);
    Assert.assertEquals(r2.getAttribute(AbstractAlignmentMerger.HARD_CLIPPED_BASE_QUALITIES_TAG), expectedR2ClippedQualities);


    // Swap first and second read to ensure logic is correct for both F1R2 and F2R1
    final SAMRecordSetBuilder setSwapped = new SAMRecordSetBuilder();
    setSwapped.setReadLength(readLength);
    final List<SAMRecord> recsSwapped = set.addPair("q1", 0, start2, start1, false, false, cigar2, cigar1, strand2, strand1, 30);
    final SAMRecord r1Swapped = recsSwapped.get(0);
    final SAMRecord r2Swapped = recsSwapped.get(1);

    r1Swapped.setReadBases(StringUtil.stringToBytes(read2Bases));
    r2Swapped.setReadBases(StringUtil.stringToBytes(read1Bases));

    r1Swapped.setBaseQualities(SAMUtils.fastqToPhred(read2Qualities));
    r2Swapped.setBaseQualities(SAMUtils.fastqToPhred(read1Qualities));

    AbstractAlignmentMerger.clipForOverlappingReads(r1Swapped, r2Swapped, hardClipOverlappingReads);
    Assert.assertEquals(r1Swapped.getAlignmentStart(), r2ExpectedAlignmentStart);
    Assert.assertEquals(r1Swapped.getCigarString(), expectedR2Cigar);
    Assert.assertEquals(r2Swapped.getAlignmentStart(), r1ExpectedAlignmentStart);
    Assert.assertEquals(r2Swapped.getCigarString(), expectedR1Cigar);
    Assert.assertEquals(r1Swapped.getReadString(), expectedR2Bases);
    Assert.assertEquals(r2Swapped.getReadString(), expectedR1Bases);
    Assert.assertEquals(SAMUtils.phredToFastq(r1Swapped.getBaseQualities()), expectedR2Qualities);
    Assert.assertEquals(SAMUtils.phredToFastq(r2Swapped.getBaseQualities()), expectedR1Qualities);

    Assert.assertEquals(r1Swapped.getAttribute(AbstractAlignmentMerger.HARD_CLIPPED_BASES_TAG), expectedR2ClippedBases);
    Assert.assertEquals(r2Swapped.getAttribute(AbstractAlignmentMerger.HARD_CLIPPED_BASES_TAG), expectedR1ClippedBases);

    Assert.assertEquals(r1Swapped.getAttribute(AbstractAlignmentMerger.HARD_CLIPPED_BASE_QUALITIES_TAG), expectedR2ClippedQualities);
    Assert.assertEquals(r2Swapped.getAttribute(AbstractAlignmentMerger.HARD_CLIPPED_BASE_QUALITIES_TAG), expectedR1ClippedQualities);

}
 
Example 6
Source File: CollectInsertSizeMetricsTest.java    From picard with MIT License 4 votes vote down vote up
@Test
public void testWidthOfMetrics() throws IOException {
    final File testSamFile = File.createTempFile("CollectInsertSizeMetrics", ".bam");
    testSamFile.deleteOnExit();
    final File testSamFileIndex = new File(testSamFile.getParentFile(),testSamFile.getName().replaceAll("bam$","bai"));
    testSamFileIndex.deleteOnExit();


    new File(testSamFile.getAbsolutePath().replace(".bam$",".bai")).deleteOnExit();

    final SAMRecordSetBuilder setBuilder = new SAMRecordSetBuilder(true, SAMFileHeader.SortOrder.coordinate, true);
    setBuilder.setReadLength(10);

    final int insertBy = 3; // the # of bases to increase the insert by in the records below.
    int queryIndex = 0;

    // Create records such that we have 10 records in the 10th through the 90th percentiles, and 5 for the 95th and 99th percentiles.
    // WIDTH_OF_10_PERCENT through WIDTH_OF_90_PERCENT (90 pairs total))
    for (int j = 0; j < 9; j++) {
        for (int i = 0; i < 5; i++, queryIndex++) {
            setBuilder.addPair("query:" + queryIndex, 0, 1, 50 + j*insertBy, false, false, "10M", "10M", false, true, 60);
        }
        for (int i = 0; i < 5; i++, queryIndex++) {
            setBuilder.addPair("query:" + queryIndex, 0, 1, 50 - j*insertBy, false, false, "10M", "10M", false, true, 60);
        }
    }
    // WIDTH_OF_95_PERCENT through WIDTH_OF_99_PERCENT (10 pairs total)
    for (int j = 9; j < 11; j++) {
        for (int i = 0; i < 3; i++, queryIndex++) {
            setBuilder.addPair("query:" + queryIndex, 0, 1, 50 + j*insertBy, false, false, "10M", "10M", false, true, 60);
        }
        for (int i = 0; i < 2; i++, queryIndex++) {
            setBuilder.addPair("query:" + queryIndex, 0, 1, 50 - j*insertBy, false, false, "10M", "10M", false, true, 60);
        }
    }

    // Add one to make the an odd # of pairs for the median
    setBuilder.addPair("query:" + queryIndex, 0, 1, 50, false, false, "10M", "10M", false, true, 60);

    final SAMFileWriter writer = new SAMFileWriterFactory().setCreateIndex(true).makeBAMWriter(setBuilder.getHeader(), false, testSamFile);
    setBuilder.forEach(writer::addAlignment);
    writer.close();


    final File outfile = File.createTempFile("test", ".insert_size_metrics");
    final File pdf     = File.createTempFile("test", ".pdf");
    outfile.deleteOnExit();
    pdf.deleteOnExit();
    final String[] args = new String[] {
            "INPUT="  + testSamFile.getAbsolutePath(),
            "OUTPUT=" + outfile.getAbsolutePath(),
            "Histogram_FILE=" + pdf.getAbsolutePath()
    };
    Assert.assertEquals(runPicardCommandLine(args), 0);

    final MetricsFile<InsertSizeMetrics, Comparable<?>> output = new MetricsFile<InsertSizeMetrics, Comparable<?>>();
    output.read(new FileReader(outfile));
    
    final List<InsertSizeMetrics> metrics = output.getMetrics();
    
    Assert.assertEquals(metrics.size(), 1);
    
    final InsertSizeMetrics metric = metrics.get(0);

    Assert.assertEquals(metric.PAIR_ORIENTATION.name(), "FR");
    Assert.assertEquals((int) metric.MEDIAN_INSERT_SIZE, 59);
    Assert.assertEquals((int) metric.MODE_INSERT_SIZE, 59);
    Assert.assertEquals(metric.MIN_INSERT_SIZE, 29);
    Assert.assertEquals(metric.MAX_INSERT_SIZE, 89);
    Assert.assertEquals(metric.READ_PAIRS, 101);
    Assert.assertEquals(metric.WIDTH_OF_10_PERCENT, 1);
    Assert.assertEquals(metric.WIDTH_OF_20_PERCENT, 1 + insertBy*2);
    Assert.assertEquals(metric.WIDTH_OF_30_PERCENT, 1 + insertBy*4);
    Assert.assertEquals(metric.WIDTH_OF_40_PERCENT, 1 + insertBy*6);
    Assert.assertEquals(metric.WIDTH_OF_50_PERCENT, 1 + insertBy*8);
    Assert.assertEquals(metric.WIDTH_OF_60_PERCENT, 1 + insertBy*10);
    Assert.assertEquals(metric.WIDTH_OF_70_PERCENT, 1 + insertBy*12);
    Assert.assertEquals(metric.WIDTH_OF_80_PERCENT, 1 + insertBy*14);
    Assert.assertEquals(metric.WIDTH_OF_90_PERCENT, 1 + insertBy*16);
    Assert.assertEquals(metric.WIDTH_OF_95_PERCENT, 1 + insertBy*18);
    Assert.assertEquals(metric.WIDTH_OF_99_PERCENT, 1 + insertBy*20);
}