2009-12-11 03:21:16 +08:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2009 The Broad Institute
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
|
* obtaining a copy of this software and associated documentation
|
|
|
|
|
* files (the "Software"), to deal in the Software without
|
|
|
|
|
* restriction, including without limitation the rights to use,
|
|
|
|
|
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following
|
|
|
|
|
* conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
|
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package org.broadinstitute.sting.gatk.contexts;
|
|
|
|
|
|
|
|
|
|
import net.sf.samtools.SAMRecord;
|
|
|
|
|
import net.sf.samtools.SAMReadGroupRecord;
|
|
|
|
|
import org.broadinstitute.sting.utils.GenomeLoc;
|
|
|
|
|
import org.broadinstitute.sting.utils.StingException;
|
|
|
|
|
import org.broadinstitute.sting.utils.pileup.ReadBackedPileup;
|
|
|
|
|
import org.broadinstitute.sting.utils.pileup.PileupElement;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Useful class for storing different AlignmentContexts
|
|
|
|
|
* User: ebanks
|
2010-02-27 02:18:38 +08:00
|
|
|
* Modified: chartl (split by read group)
|
2009-12-11 03:21:16 +08:00
|
|
|
*/
|
|
|
|
|
public class StratifiedAlignmentContext {
|
|
|
|
|
|
2009-12-11 03:50:06 +08:00
|
|
|
// Definitions:
|
|
|
|
|
// COMPLETE = full alignment context
|
2010-01-08 13:40:42 +08:00
|
|
|
// FORWARD = reads on forward strand
|
2009-12-17 01:28:09 +08:00
|
|
|
// REVERSE = reads on forward strand
|
2009-12-11 03:50:06 +08:00
|
|
|
//
|
2009-12-17 01:28:09 +08:00
|
|
|
public enum StratifiedContextType { COMPLETE, FORWARD, REVERSE }
|
2009-12-11 03:21:16 +08:00
|
|
|
|
|
|
|
|
private GenomeLoc loc;
|
2009-12-11 03:50:06 +08:00
|
|
|
private AlignmentContext[] contexts = new AlignmentContext[StratifiedContextType.values().length];
|
|
|
|
|
private ArrayList<SAMRecord>[] reads = new ArrayList[StratifiedContextType.values().length];
|
|
|
|
|
private ArrayList<Integer>[] offsets = new ArrayList[StratifiedContextType.values().length];
|
2009-12-11 03:21:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
public StratifiedAlignmentContext(GenomeLoc loc) {
|
|
|
|
|
this.loc = loc;
|
2009-12-11 03:50:06 +08:00
|
|
|
for ( int i = 0; i < StratifiedContextType.values().length; i++) {
|
|
|
|
|
reads[i] = new ArrayList<SAMRecord>();
|
|
|
|
|
offsets[i] = new ArrayList<Integer>();
|
2009-12-11 03:21:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-11 03:50:06 +08:00
|
|
|
public AlignmentContext getContext(StratifiedContextType context) {
|
|
|
|
|
int index = context.ordinal();
|
|
|
|
|
if ( contexts[index] == null )
|
|
|
|
|
contexts[index] = new AlignmentContext(loc, new ReadBackedPileup(loc, reads[index], offsets[index]));
|
|
|
|
|
return contexts[index];
|
2009-12-11 03:21:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void add(SAMRecord read, int offset) {
|
2009-12-17 01:28:09 +08:00
|
|
|
if ( read.getReadNegativeStrandFlag() ) {
|
|
|
|
|
reads[StratifiedContextType.REVERSE.ordinal()].add(read);
|
|
|
|
|
offsets[StratifiedContextType.REVERSE.ordinal()].add(offset);
|
|
|
|
|
} else {
|
|
|
|
|
reads[StratifiedContextType.FORWARD.ordinal()].add(read);
|
|
|
|
|
offsets[StratifiedContextType.FORWARD.ordinal()].add(offset);
|
2009-12-11 03:21:16 +08:00
|
|
|
}
|
2009-12-11 03:50:06 +08:00
|
|
|
reads[StratifiedContextType.COMPLETE.ordinal()].add(read);
|
|
|
|
|
offsets[StratifiedContextType.COMPLETE.ordinal()].add(offset);
|
2009-12-11 03:21:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Splits the given AlignmentContext into a StratifiedAlignmentContext per sample.
|
2010-01-08 13:40:42 +08:00
|
|
|
*
|
|
|
|
|
* @param pileup the original pileup
|
|
|
|
|
*
|
|
|
|
|
* @return a Map of sample name to StratifiedAlignmentContext
|
|
|
|
|
*
|
|
|
|
|
**/
|
|
|
|
|
public static Map<String, StratifiedAlignmentContext> splitContextBySample(ReadBackedPileup pileup) {
|
|
|
|
|
return splitContextBySample(pileup, null, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Splits the given AlignmentContext into a StratifiedAlignmentContext per sample.
|
2009-12-11 03:21:16 +08:00
|
|
|
*
|
2009-12-17 01:28:09 +08:00
|
|
|
* @param pileup the original pileup
|
2009-12-11 03:21:16 +08:00
|
|
|
* @param assumedSingleSample if not null, any read without a readgroup will be given this sample name
|
|
|
|
|
* @param collapseToThisSample if not null, all reads will be assigned this read group regardless of their actual read group
|
|
|
|
|
*
|
|
|
|
|
* @return a Map of sample name to StratifiedAlignmentContext
|
|
|
|
|
*
|
|
|
|
|
**/
|
2009-12-17 01:28:09 +08:00
|
|
|
public static Map<String, StratifiedAlignmentContext> splitContextBySample(ReadBackedPileup pileup, String assumedSingleSample, String collapseToThisSample) {
|
2009-12-11 03:21:16 +08:00
|
|
|
|
|
|
|
|
HashMap<String, StratifiedAlignmentContext> contexts = new HashMap<String, StratifiedAlignmentContext>();
|
|
|
|
|
|
|
|
|
|
for (PileupElement p : pileup ) {
|
|
|
|
|
|
|
|
|
|
// get the read
|
|
|
|
|
SAMRecord read = p.getRead();
|
|
|
|
|
|
|
|
|
|
// find the sample
|
|
|
|
|
String sample;
|
|
|
|
|
if ( collapseToThisSample != null ) {
|
|
|
|
|
sample = collapseToThisSample;
|
|
|
|
|
} else {
|
|
|
|
|
SAMReadGroupRecord readGroup = read.getReadGroup();
|
|
|
|
|
if ( readGroup == null ) {
|
|
|
|
|
if ( assumedSingleSample == null )
|
|
|
|
|
throw new StingException("Missing read group for read " + read.getReadName());
|
|
|
|
|
sample = assumedSingleSample;
|
|
|
|
|
} else {
|
|
|
|
|
sample = readGroup.getSample();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create a new context object if this is the first time we're seeing a read for this sample
|
|
|
|
|
StratifiedAlignmentContext myContext = contexts.get(sample);
|
|
|
|
|
if ( myContext == null ) {
|
2009-12-17 01:28:09 +08:00
|
|
|
myContext = new StratifiedAlignmentContext(pileup.getLocation());
|
2009-12-11 03:21:16 +08:00
|
|
|
contexts.put(sample, myContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// add the read to this sample's context
|
|
|
|
|
// note that bad bases are added to the context (for DoC calculations later)
|
|
|
|
|
myContext.add(read, p.getOffset());
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-27 02:18:38 +08:00
|
|
|
return contexts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Splits the given AlignmentContext into a StratifiedAlignmentContext per read group.
|
|
|
|
|
*
|
|
|
|
|
* @param pileup the original pileup
|
|
|
|
|
* @return a Map of sample name to StratifiedAlignmentContext
|
|
|
|
|
* @todo - support for collapsing or assuming read groups if they are missing
|
|
|
|
|
*
|
|
|
|
|
**/
|
|
|
|
|
public static Map<String,StratifiedAlignmentContext> splitContextByReadGroup(ReadBackedPileup pileup) {
|
|
|
|
|
HashMap<String,StratifiedAlignmentContext> contexts = new HashMap<String,StratifiedAlignmentContext>();
|
|
|
|
|
|
|
|
|
|
for ( PileupElement p : pileup ) {
|
|
|
|
|
SAMRecord read = p.getRead();
|
|
|
|
|
|
|
|
|
|
SAMReadGroupRecord readGroup = read.getReadGroup();
|
|
|
|
|
if ( readGroup == null ) {
|
|
|
|
|
throw new StingException("Missing read group for read " + read.getReadName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String group = readGroup.getReadGroupId();
|
|
|
|
|
|
|
|
|
|
StratifiedAlignmentContext myContext = contexts.get(group);
|
|
|
|
|
|
|
|
|
|
if ( myContext == null ) {
|
|
|
|
|
myContext = new StratifiedAlignmentContext(pileup.getLocation());
|
|
|
|
|
contexts.put(group,myContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
myContext.add(read,p.getOffset());
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-11 03:21:16 +08:00
|
|
|
return contexts;
|
|
|
|
|
}
|
|
|
|
|
}
|