Search in sources :

Example 36 with Locatable

use of htsjdk.samtools.util.Locatable in project gatk by broadinstitute.

the class AlignmentContext method splitContextBySampleName.

/**
     * Splits the given AlignmentContext into a StratifiedAlignmentContext per sample, but referenced by sample name instead
     * of sample object.
     *
     * @param assumedSingleSample If non-null, assume this is the only sample in our pileup and return immediately.
     *                            If null, get the list of samples from the provided header and do the work of splitting by sample.
     * @return a Map of sample name to StratifiedAlignmentContext; samples without coverage are not included
     **/
public Map<String, AlignmentContext> splitContextBySampleName(final String assumedSingleSample, final SAMFileHeader header) {
    if (assumedSingleSample != null) {
        return Collections.singletonMap(assumedSingleSample, this);
    }
    final Locatable loc = this.getLocation();
    // this will throw an user error if there are samples without RG/sampleName
    final Map<String, ReadPileup> pileups = this.getBasePileup().splitBySample(header, assumedSingleSample);
    final Map<String, AlignmentContext> contexts = new LinkedHashMap<>(pileups.size());
    for (final Map.Entry<String, ReadPileup> entry : pileups.entrySet()) {
        // Don't add empty pileups to the split context.
        if (entry.getValue().isEmpty()) {
            continue;
        }
        contexts.put(entry.getKey(), new AlignmentContext(loc, entry.getValue()));
    }
    return contexts;
}
Also used : ReadPileup(org.broadinstitute.hellbender.utils.pileup.ReadPileup) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Locatable(htsjdk.samtools.util.Locatable) LinkedHashMap(java.util.LinkedHashMap)

Example 37 with Locatable

use of htsjdk.samtools.util.Locatable in project gatk by broadinstitute.

the class AlignmentContextUnitTest method testNoSample.

@Test(expectedExceptions = UserException.ReadMissingReadGroup.class)
public void testNoSample() throws Exception {
    final SAMReadGroupRecord readGroupOne = new SAMReadGroupRecord("rg1");
    final SAMFileHeader header = ArtificialReadUtils.createArtificialSamHeader(1, 1, 1000);
    header.addReadGroup(readGroupOne);
    final Locatable loc = new SimpleInterval("chr1", 1, 1);
    final GATKRead read1 = ArtificialReadUtils.createArtificialRead(header, "read1", 0, 1, 10);
    read1.setReadGroup(readGroupOne.getId());
    final ReadPileup pileup = new ReadPileup(loc, Arrays.asList(read1), 1);
    final AlignmentContext ac = new AlignmentContext(loc, pileup);
    ac.splitContextBySampleName(header);
}
Also used : GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) ReadPileup(org.broadinstitute.hellbender.utils.pileup.ReadPileup) SAMReadGroupRecord(htsjdk.samtools.SAMReadGroupRecord) SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval) SAMFileHeader(htsjdk.samtools.SAMFileHeader) Locatable(htsjdk.samtools.util.Locatable) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 38 with Locatable

use of htsjdk.samtools.util.Locatable in project gatk by broadinstitute.

the class AlignmentContextUnitTest method test1Sample2Readgroups.

@Test
public void test1Sample2Readgroups() throws Exception {
    final SAMReadGroupRecord readGroupOne = new SAMReadGroupRecord("rg1");
    readGroupOne.setSample("sample1");
    final SAMReadGroupRecord readGroupTwo = new SAMReadGroupRecord("rg2");
    readGroupTwo.setSample("sample1");
    final SAMFileHeader header = ArtificialReadUtils.createArtificialSamHeader(1, 1, 1000);
    header.addReadGroup(readGroupOne);
    header.addReadGroup(readGroupTwo);
    final Locatable loc = new SimpleInterval("chr1", 1, 1);
    final GATKRead read1 = ArtificialReadUtils.createArtificialRead(header, "read1", 0, 1, 10);
    read1.setReadGroup(readGroupOne.getId());
    final GATKRead read2 = ArtificialReadUtils.createArtificialRead(header, "read2", 0, 1, 10);
    read2.setReadGroup(readGroupTwo.getId());
    final GATKRead read3 = ArtificialReadUtils.createArtificialRead(header, "read3", 0, 1, 10);
    read3.setReadGroup(readGroupOne.getId());
    final GATKRead read4 = ArtificialReadUtils.createArtificialRead(header, "read4", 0, 1, 10);
    read4.setReadGroup(readGroupTwo.getId());
    final GATKRead read5 = ArtificialReadUtils.createArtificialRead(header, "read5", 0, 1, 10);
    read5.setReadGroup(readGroupTwo.getId());
    final GATKRead read6 = ArtificialReadUtils.createArtificialRead(header, "read6", 0, 1, 10);
    read6.setReadGroup(readGroupOne.getId());
    final GATKRead read7 = ArtificialReadUtils.createArtificialRead(header, "read7", 0, 1, 10);
    read7.setReadGroup(readGroupOne.getId());
    final ReadPileup pileup = new ReadPileup(loc, Arrays.asList(read1, read2, read3, read4, read5, read6, read7), 1);
    final AlignmentContext ac = new AlignmentContext(loc, pileup);
    Assert.assertSame(ac.getBasePileup(), pileup);
    Assert.assertEquals(ac.getContig(), loc.getContig());
    Assert.assertEquals(ac.getEnd(), loc.getEnd());
    Assert.assertEquals(ac.getLocation(), loc);
    Assert.assertEquals(ac.getPosition(), loc.getStart());
    Assert.assertEquals(ac.getStart(), loc.getStart());
    Assert.assertEquals(ac.hasPileupBeenDownsampled(), false);
    Assert.assertEquals(ac.size(), pileup.size());
    Assert.assertSame(ac.stratify(AlignmentContext.ReadOrientation.COMPLETE), ac, "Complete");
    final AlignmentContext acFWD = ac.stratify(AlignmentContext.ReadOrientation.FORWARD);
    Assert.assertEquals(acFWD.getLocation(), loc, "Forward Loc");
    Assert.assertEquals((Iterable<?>) acFWD.getBasePileup(), (Iterable<?>) pileup, "Forward Pileup");
    final AlignmentContext acREV = ac.stratify(AlignmentContext.ReadOrientation.REVERSE);
    AlignmentContext emptyAC = new AlignmentContext(loc, new ReadPileup(loc));
    Assert.assertEquals(acREV.getLocation(), loc, "Reverse Loc");
    Assert.assertEquals((Iterable<?>) acREV.getBasePileup(), (Iterable<?>) emptyAC.getBasePileup(), "Reverse pileup");
    Assert.assertNotNull(ac.toString());
    final Map<String, AlignmentContext> bySample = ac.splitContextBySampleName(header);
    Assert.assertEquals(bySample.size(), 1);
    Assert.assertEquals(bySample.keySet(), ReadUtils.getSamplesFromHeader(header));
    final AlignmentContext firstAC = bySample.values().iterator().next();
    Assert.assertEquals(firstAC.getLocation(), ac.getLocation());
    Assert.assertEquals(firstAC.getBasePileup(), ac.getBasePileup());
    final Map<String, AlignmentContext> bySampleAssume1 = ac.splitContextBySampleName("sample1", header);
    Assert.assertEquals(bySampleAssume1.size(), 1);
    Assert.assertEquals(bySampleAssume1.keySet(), ReadUtils.getSamplesFromHeader(header));
    final AlignmentContext firstACAssume1 = bySampleAssume1.values().iterator().next();
    Assert.assertEquals(firstACAssume1.getLocation(), ac.getLocation());
    Assert.assertEquals(firstACAssume1.getBasePileup(), ac.getBasePileup());
    final Map<String, AlignmentContext> stringAlignmentContextMap = AlignmentContext.splitContextBySampleName(pileup, header);
    Assert.assertEquals(stringAlignmentContextMap.keySet(), Collections.singleton("sample1"));
    Assert.assertEquals(stringAlignmentContextMap.get("sample1").getLocation(), loc);
    Assert.assertEquals(stringAlignmentContextMap.get("sample1").getBasePileup(), pileup);
}
Also used : GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) ReadPileup(org.broadinstitute.hellbender.utils.pileup.ReadPileup) SAMReadGroupRecord(htsjdk.samtools.SAMReadGroupRecord) SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval) SAMFileHeader(htsjdk.samtools.SAMFileHeader) Locatable(htsjdk.samtools.util.Locatable) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Aggregations

Locatable (htsjdk.samtools.util.Locatable)38 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)24 Test (org.testng.annotations.Test)22 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)20 ReadPileup (org.broadinstitute.hellbender.utils.pileup.ReadPileup)11 GATKRead (org.broadinstitute.hellbender.utils.read.GATKRead)10 SAMFileHeader (htsjdk.samtools.SAMFileHeader)6 ArrayList (java.util.ArrayList)5 UserException (org.broadinstitute.hellbender.exceptions.UserException)5 DataProvider (org.testng.annotations.DataProvider)4 SAMSequenceRecord (htsjdk.samtools.SAMSequenceRecord)3 com.google.common.collect (com.google.common.collect)2 SAMReadGroupRecord (htsjdk.samtools.SAMReadGroupRecord)2 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)2 OverlapDetector (htsjdk.samtools.util.OverlapDetector)2 IOException (java.io.IOException)2 Serializable (java.io.Serializable)2 java.util (java.util)2 LinkedHashMap (java.util.LinkedHashMap)2 Collectors (java.util.stream.Collectors)2