Search in sources :

Example 1 with SAMReadGroupRecord

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

the class SampleCollectionUnitTest method testSampleIndexByReadWithReadGroupWithoutSample.

@Test(dataProvider = "samFileHeaderData", dependsOnMethods = "testCreation")
public void testSampleIndexByReadWithReadGroupWithoutSample(final SAMFileHeader header) {
    final String readGroup = header.getReadGroups().stream().filter(rg -> rg.getSample() == null).map(SAMReadGroupRecord::getId).findFirst().orElse(null);
    if (readGroup == null)
        return;
    final SampleCollection sampleCollection = new SampleCollection(header);
    final GATKRead read = ArtificialReadUtils.createRandomRead(header, 100);
    if (sampleCollection.sampleCount() > 0) {
        read.setReadGroup(readGroup);
        Assert.assertEquals(sampleCollection.sampleIndexByRead(read), -1);
    }
}
Also used : java.util(java.util) SAMReadGroupRecord(htsjdk.samtools.SAMReadGroupRecord) ArtificialReadUtils(org.broadinstitute.hellbender.utils.read.ArtificialReadUtils) Assert(org.testng.Assert) DataProvider(org.testng.annotations.DataProvider) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) BeforeClass(org.testng.annotations.BeforeClass) Test(org.testng.annotations.Test) GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) SAMFileHeader(htsjdk.samtools.SAMFileHeader) Collectors(java.util.stream.Collectors) GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) SAMReadGroupRecord(htsjdk.samtools.SAMReadGroupRecord) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 2 with SAMReadGroupRecord

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

the class SampleCollectionUnitTest method testReadGroupIndexByReadWithReadGroupWithoutSample.

@Test(dataProvider = "samFileHeaderData", dependsOnMethods = "testCreation")
public void testReadGroupIndexByReadWithReadGroupWithoutSample(final SAMFileHeader header) {
    final String readGroup = header.getReadGroups().stream().filter(rg -> rg.getSample() == null).map(SAMReadGroupRecord::getId).findFirst().orElse(null);
    if (readGroup == null)
        return;
    final SampleCollection sampleCollection = new SampleCollection(header);
    final GATKRead read = ArtificialReadUtils.createRandomRead(header, 100);
    read.setReadGroup(readGroup);
    Assert.assertEquals(sampleCollection.readGroupIndexByRead(read), sampleCollection.readGroupIndexById(readGroup));
}
Also used : java.util(java.util) SAMReadGroupRecord(htsjdk.samtools.SAMReadGroupRecord) ArtificialReadUtils(org.broadinstitute.hellbender.utils.read.ArtificialReadUtils) Assert(org.testng.Assert) DataProvider(org.testng.annotations.DataProvider) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) BeforeClass(org.testng.annotations.BeforeClass) Test(org.testng.annotations.Test) GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) SAMFileHeader(htsjdk.samtools.SAMFileHeader) Collectors(java.util.stream.Collectors) GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) SAMReadGroupRecord(htsjdk.samtools.SAMReadGroupRecord) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 3 with SAMReadGroupRecord

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

the class SampleCollectionUnitTest method testSampleIds.

@Test(dataProvider = "samFileHeaderData")
public void testSampleIds(final SAMFileHeader header) {
    final SampleCollection sampleCollection = new SampleCollection(header);
    final Set<String> sampleIds = header.getReadGroups().stream().map(SAMReadGroupRecord::getSample).filter(Objects::nonNull).collect(Collectors.toSet());
    for (final SAMReadGroupRecord rg : header.getReadGroups()) {
        final String sampleId = rg.getSample();
        if (sampleId == null) {
            continue;
        }
        sampleIds.add(rg.getSample());
    }
    final List<String> observedSampleIds = sampleCollection.sampleIds();
    Assert.assertEquals(observedSampleIds.size(), sampleIds.size(), Arrays.toString(observedSampleIds.toArray()));
    for (final String sampleId : observedSampleIds) {
        Assert.assertTrue(sampleIds.contains(sampleId), "Missing expected sample id: " + sampleId);
    }
    final List<String> sortedSampleIds = new ArrayList<>(sampleIds);
    Collections.sort(sortedSampleIds);
    Assert.assertEquals(observedSampleIds, sortedSampleIds, Arrays.toString(observedSampleIds.toArray()) + " " + Arrays.toString(sortedSampleIds.toArray()));
}
Also used : SAMReadGroupRecord(htsjdk.samtools.SAMReadGroupRecord) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 4 with SAMReadGroupRecord

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

the class FragmentUtilsUnitTest method makeOverlappingRead.

private GATKRead makeOverlappingRead(final String leftFlank, final int leftQual, final String overlapBases, final byte[] overlapQuals, final String rightFlank, final int rightQual, final int alignmentStart) {
    final SAMFileHeader header = ArtificialReadUtils.createArtificialSamHeader();
    header.addReadGroup(new SAMReadGroupRecord("RG1"));
    final String bases = leftFlank + overlapBases + rightFlank;
    final int readLength = bases.length();
    final GATKRead read = ArtificialReadUtils.createArtificialRead(header, "myRead", 0, alignmentStart, readLength);
    final byte[] leftQuals = Utils.dupBytes((byte) leftQual, leftFlank.length());
    final byte[] rightQuals = Utils.dupBytes((byte) rightQual, rightFlank.length());
    final byte[] quals = Utils.concat(leftQuals, overlapQuals, rightQuals);
    read.setCigar(readLength + "M");
    read.setBases(bases.getBytes());
    read.setBaseQualities(quals);
    read.setReadGroup("RG1");
    read.setMappingQuality(60);
    return read;
}
Also used : GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) SAMReadGroupRecord(htsjdk.samtools.SAMReadGroupRecord) SAMFileHeader(htsjdk.samtools.SAMFileHeader)

Example 5 with SAMReadGroupRecord

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

the class CollectRnaSeqMetricsTest method testMultiLevel.

@Test
public void testMultiLevel() throws Exception {
    final String sequence = "chr1";
    final String ignoredSequence = "chrM";
    // Create some alignments that hit the ribosomal sequence, various parts of the gene, and intergenic.
    final SAMRecordSetBuilder builder = new SAMRecordSetBuilder(true, SAMFileHeader.SortOrder.coordinate, false);
    // Set seed so that strandedness is consistent among runs.
    builder.setRandomSeed(0);
    final int sequenceIndex = builder.getHeader().getSequenceIndex(sequence);
    final SAMReadGroupRecord rg1 = new SAMReadGroupRecord("2");
    rg1.setSample("Sample");
    rg1.setLibrary("foo");
    builder.setReadGroup(rg1);
    builder.addPair("pair1", sequenceIndex, 45, 475);
    builder.addPair("pair2", sequenceIndex, 90, 225);
    builder.addFrag("frag1", sequenceIndex, 150, true);
    builder.addFrag("frag2", sequenceIndex, 450, true);
    final SAMReadGroupRecord rg2 = new SAMReadGroupRecord("3");
    rg2.setSample("Sample");
    rg2.setLibrary("bar");
    builder.setReadGroup(rg2);
    builder.addPair("pair3", sequenceIndex, 120, 600);
    builder.addFrag("frag3", sequenceIndex, 225, false);
    builder.addPair("rrnaPair", sequenceIndex, 400, 500);
    builder.addFrag("ignoredFrag", builder.getHeader().getSequenceIndex(ignoredSequence), 1, false);
    final File samFile = BaseTest.createTempFile("tmp.collectRnaSeqMetrics.", ".sam");
    try (final SAMFileWriter samWriter = new SAMFileWriterFactory().makeSAMWriter(builder.getHeader(), false, samFile)) {
        for (final SAMRecord rec : builder.getRecords()) samWriter.addAlignment(rec);
    }
    // Create an interval list with one ribosomal interval.
    final Interval rRnaInterval = new Interval(sequence, 300, 520, true, "rRNA");
    final IntervalList rRnaIntervalList = new IntervalList(builder.getHeader());
    rRnaIntervalList.add(rRnaInterval);
    final File rRnaIntervalsFile = BaseTest.createTempFile("tmp.rRna.", ".interval_list");
    rRnaIntervalList.write(rRnaIntervalsFile);
    // Generate the metrics.
    final File metricsFile = BaseTest.createTempFile("tmp.", ".rna_metrics");
    final String[] args = new String[] { "--input", samFile.getAbsolutePath(), "--output", metricsFile.getAbsolutePath(), "--REF_FLAT", getRefFlatFile(sequence).getAbsolutePath(), "--RIBOSOMAL_INTERVALS", rRnaIntervalsFile.getAbsolutePath(), "--STRAND_SPECIFICITY", "SECOND_READ_TRANSCRIPTION_STRAND", "--IGNORE_SEQUENCE", ignoredSequence, "--LEVEL", "SAMPLE", "--LEVEL", "LIBRARY" };
    runCommandLine(args);
    final MetricsFile<RnaSeqMetrics, Comparable<?>> output = new MetricsFile<>();
    output.read(new FileReader(metricsFile));
    for (final RnaSeqMetrics metrics : output.getMetrics()) {
        if (metrics.LIBRARY == null) {
            Assert.assertEquals(metrics.PF_ALIGNED_BASES, 396);
            Assert.assertEquals(metrics.PF_BASES, 432);
            Assert.assertEquals(metrics.RIBOSOMAL_BASES.longValue(), 108L);
            Assert.assertEquals(metrics.CODING_BASES, 136);
            Assert.assertEquals(metrics.UTR_BASES, 51);
            Assert.assertEquals(metrics.INTRONIC_BASES, 50);
            Assert.assertEquals(metrics.INTERGENIC_BASES, 51);
            Assert.assertEquals(metrics.CORRECT_STRAND_READS, 3);
            Assert.assertEquals(metrics.INCORRECT_STRAND_READS, 4);
            Assert.assertEquals(metrics.IGNORED_READS, 1);
        } else if (metrics.LIBRARY.equals("foo")) {
            Assert.assertEquals(metrics.PF_ALIGNED_BASES, 216);
            Assert.assertEquals(metrics.PF_BASES, 216);
            Assert.assertEquals(metrics.RIBOSOMAL_BASES.longValue(), 36L);
            Assert.assertEquals(metrics.CODING_BASES, 89);
            Assert.assertEquals(metrics.UTR_BASES, 51);
            Assert.assertEquals(metrics.INTRONIC_BASES, 25);
            Assert.assertEquals(metrics.INTERGENIC_BASES, 15);
            Assert.assertEquals(metrics.CORRECT_STRAND_READS, 3);
            Assert.assertEquals(metrics.INCORRECT_STRAND_READS, 2);
            Assert.assertEquals(metrics.IGNORED_READS, 0);
        } else if (metrics.LIBRARY.equals("bar")) {
            Assert.assertEquals(metrics.PF_ALIGNED_BASES, 180);
            Assert.assertEquals(metrics.PF_BASES, 216);
            Assert.assertEquals(metrics.RIBOSOMAL_BASES.longValue(), 72L);
            Assert.assertEquals(metrics.CODING_BASES, 47);
            Assert.assertEquals(metrics.UTR_BASES, 0);
            Assert.assertEquals(metrics.INTRONIC_BASES, 25);
            Assert.assertEquals(metrics.INTERGENIC_BASES, 36);
            Assert.assertEquals(metrics.CORRECT_STRAND_READS, 0);
            Assert.assertEquals(metrics.INCORRECT_STRAND_READS, 2);
            Assert.assertEquals(metrics.IGNORED_READS, 1);
        }
    }
}
Also used : MetricsFile(htsjdk.samtools.metrics.MetricsFile) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMReadGroupRecord(htsjdk.samtools.SAMReadGroupRecord) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) SAMRecordSetBuilder(htsjdk.samtools.SAMRecordSetBuilder) IntervalList(htsjdk.samtools.util.IntervalList) SAMRecord(htsjdk.samtools.SAMRecord) FileReader(java.io.FileReader) MetricsFile(htsjdk.samtools.metrics.MetricsFile) File(java.io.File) Interval(htsjdk.samtools.util.Interval) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest)

Aggregations

SAMReadGroupRecord (htsjdk.samtools.SAMReadGroupRecord)81 SAMFileHeader (htsjdk.samtools.SAMFileHeader)48 SAMRecord (htsjdk.samtools.SAMRecord)33 Test (org.testng.annotations.Test)31 SamReader (htsjdk.samtools.SamReader)29 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)26 File (java.io.File)23 ArrayList (java.util.ArrayList)22 SAMRecordIterator (htsjdk.samtools.SAMRecordIterator)20 GATKRead (org.broadinstitute.hellbender.utils.read.GATKRead)20 HashMap (java.util.HashMap)18 CigarElement (htsjdk.samtools.CigarElement)17 Cigar (htsjdk.samtools.Cigar)16 HashSet (java.util.HashSet)16 SAMFileWriter (htsjdk.samtools.SAMFileWriter)15 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)15 CigarOperator (htsjdk.samtools.CigarOperator)14 IOException (java.io.IOException)14 SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)13 List (java.util.List)12