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;
|
2010-09-10 07:21:17 +08:00
|
|
|
import org.broadinstitute.sting.utils.GATKException;
|
2009-12-11 03:21:16 +08:00
|
|
|
import org.broadinstitute.sting.utils.GenomeLoc;
|
|
|
|
|
import org.broadinstitute.sting.utils.StingException;
|
2010-09-10 07:21:17 +08:00
|
|
|
import org.broadinstitute.sting.utils.exceptions.UserError;
|
2010-04-21 01:38:09 +08:00
|
|
|
import org.broadinstitute.sting.utils.pileup.*;
|
2009-12-11 03:21:16 +08:00
|
|
|
|
2010-04-21 01:38:09 +08:00
|
|
|
import java.util.*;
|
2009-12-11 03:21:16 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
*/
|
2010-07-01 10:46:05 +08:00
|
|
|
public class StratifiedAlignmentContext<RBP extends ReadBackedPileup> {
|
2009-12-11 03:21:16 +08:00
|
|
|
|
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;
|
2010-07-01 10:46:05 +08:00
|
|
|
private RBP basePileup = null;
|
2010-03-29 05:45:22 +08:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// accessors
|
|
|
|
|
//
|
|
|
|
|
public GenomeLoc getLocation() { return loc; }
|
2009-12-11 03:21:16 +08:00
|
|
|
|
|
|
|
|
public StratifiedAlignmentContext(GenomeLoc loc) {
|
2010-07-01 10:46:05 +08:00
|
|
|
this(loc,null);
|
2010-04-21 01:38:09 +08:00
|
|
|
}
|
|
|
|
|
|
2010-07-01 10:46:05 +08:00
|
|
|
public StratifiedAlignmentContext(GenomeLoc loc, RBP pileup) {
|
2009-12-11 03:21:16 +08:00
|
|
|
this.loc = loc;
|
2010-07-01 10:46:05 +08:00
|
|
|
this.basePileup = pileup;
|
2009-12-11 03:21:16 +08:00
|
|
|
}
|
|
|
|
|
|
2010-03-29 05:45:22 +08:00
|
|
|
public AlignmentContext getContext(StratifiedContextType type) {
|
2010-07-01 10:46:05 +08:00
|
|
|
switch(type) {
|
|
|
|
|
case COMPLETE:
|
|
|
|
|
return new AlignmentContext(loc,basePileup);
|
|
|
|
|
case FORWARD:
|
|
|
|
|
return new AlignmentContext(loc,basePileup.getPositiveStrandPileup());
|
|
|
|
|
case REVERSE:
|
|
|
|
|
return new AlignmentContext(loc,basePileup.getNegativeStrandPileup());
|
|
|
|
|
default:
|
2010-09-10 07:21:17 +08:00
|
|
|
throw new GATKException("Unable to get alignment context for type = " + type);
|
2010-04-21 01:38:09 +08:00
|
|
|
}
|
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
|
|
|
|
|
*
|
|
|
|
|
**/
|
2010-07-01 10:46:05 +08:00
|
|
|
public static <RBP extends ReadBackedPileup,PE extends PileupElement> Map<String, StratifiedAlignmentContext> splitContextBySample(RBP pileup) {
|
|
|
|
|
return splitContextBySample(pileup, null);
|
2010-01-08 13:40:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*
|
|
|
|
|
* @return a Map of sample name to StratifiedAlignmentContext
|
|
|
|
|
*
|
|
|
|
|
**/
|
2010-07-01 10:46:05 +08:00
|
|
|
public static <RBP extends ReadBackedPileup> Map<String, StratifiedAlignmentContext> splitContextBySample(RBP pileup, String assumedSingleSample) {
|
2009-12-11 03:21:16 +08:00
|
|
|
|
2010-03-10 12:30:12 +08:00
|
|
|
GenomeLoc loc = pileup.getLocation();
|
|
|
|
|
HashMap<String, StratifiedAlignmentContext> contexts = new HashMap<String, StratifiedAlignmentContext>();
|
|
|
|
|
|
2010-07-01 10:46:05 +08:00
|
|
|
for(String sampleName: pileup.getSamples()) {
|
|
|
|
|
RBP pileupBySample = (RBP)pileup.getPileupForSample(sampleName);
|
2010-03-10 12:30:12 +08:00
|
|
|
|
2010-07-07 09:07:46 +08:00
|
|
|
// Don't add empty pileups to the split context.
|
|
|
|
|
if(pileupBySample.size() == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
2010-07-01 10:46:05 +08:00
|
|
|
if(sampleName != null)
|
|
|
|
|
contexts.put(sampleName,new StratifiedAlignmentContext<RBP>(loc,pileupBySample));
|
|
|
|
|
else {
|
2010-09-10 07:21:17 +08:00
|
|
|
if(assumedSingleSample == null) {
|
|
|
|
|
throw new UserError.MalformedBam(pileupBySample.iterator().next().getRead(), "Missing read group for read");
|
|
|
|
|
}
|
2010-07-01 10:46:05 +08:00
|
|
|
contexts.put(assumedSingleSample,new StratifiedAlignmentContext<RBP>(loc,pileupBySample));
|
2009-12-11 03:21:16 +08:00
|
|
|
}
|
2010-03-10 12:30:12 +08:00
|
|
|
}
|
2009-12-11 03:21:16 +08:00
|
|
|
|
2010-07-01 10:46:05 +08:00
|
|
|
return contexts;
|
2010-02-27 02:18:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Splits the given AlignmentContext into a StratifiedAlignmentContext per read group.
|
|
|
|
|
*
|
|
|
|
|
* @param pileup the original pileup
|
|
|
|
|
* @return a Map of sample name to StratifiedAlignmentContext
|
2010-03-10 12:30:12 +08:00
|
|
|
* TODO - support for collapsing or assuming read groups if they are missing
|
2010-02-27 02:18:38 +08:00
|
|
|
*
|
|
|
|
|
**/
|
2010-07-01 10:46:05 +08:00
|
|
|
public static <RBP extends ReadBackedPileup> Map<String,StratifiedAlignmentContext<RBP>> splitContextByReadGroup(RBP pileup) {
|
|
|
|
|
HashMap<String,StratifiedAlignmentContext<RBP>> contexts = new HashMap<String,StratifiedAlignmentContext<RBP>>();
|
|
|
|
|
for(String readGroupId: pileup.getReadGroups())
|
|
|
|
|
contexts.put(readGroupId,new StratifiedAlignmentContext<RBP>(pileup.getLocation(),(RBP)pileup.getPileupForReadGroup(readGroupId)));
|
|
|
|
|
return contexts;
|
|
|
|
|
}
|
2010-02-27 02:18:38 +08:00
|
|
|
|
2010-07-01 10:46:05 +08:00
|
|
|
public static AlignmentContext joinContexts(Collection<StratifiedAlignmentContext> contexts) {
|
|
|
|
|
|
|
|
|
|
// validation
|
|
|
|
|
GenomeLoc loc = contexts.iterator().next().getLocation();
|
|
|
|
|
boolean isExtended = contexts.iterator().next().basePileup instanceof ReadBackedExtendedEventPileup;
|
|
|
|
|
for(StratifiedAlignmentContext context: contexts) {
|
|
|
|
|
if(!loc.equals(context.getLocation()))
|
2010-09-10 07:21:17 +08:00
|
|
|
throw new GATKException("Illegal attempt to join contexts from different genomic locations");
|
2010-07-01 10:46:05 +08:00
|
|
|
if(isExtended != (context.basePileup instanceof ReadBackedExtendedEventPileup))
|
2010-09-10 07:21:17 +08:00
|
|
|
throw new GATKException("Illegal attempt to join simple and extended contexts");
|
2010-07-01 10:46:05 +08:00
|
|
|
}
|
2010-02-27 02:18:38 +08:00
|
|
|
|
2010-07-01 10:46:05 +08:00
|
|
|
AlignmentContext jointContext;
|
|
|
|
|
if(isExtended) {
|
|
|
|
|
List<ExtendedEventPileupElement> pe = new ArrayList<ExtendedEventPileupElement>();
|
|
|
|
|
for(StratifiedAlignmentContext context: contexts) {
|
|
|
|
|
for(PileupElement pileupElement: context.basePileup)
|
|
|
|
|
pe.add((ExtendedEventPileupElement)pileupElement);
|
2010-02-27 02:18:38 +08:00
|
|
|
}
|
2010-07-01 10:46:05 +08:00
|
|
|
jointContext = new AlignmentContext(loc, new ReadBackedExtendedEventPileupImpl(loc,pe));
|
2010-02-27 02:18:38 +08:00
|
|
|
}
|
2010-07-01 10:46:05 +08:00
|
|
|
else {
|
|
|
|
|
List<PileupElement> pe = new ArrayList<PileupElement>();
|
|
|
|
|
for(StratifiedAlignmentContext context: contexts) {
|
|
|
|
|
for(PileupElement pileupElement: context.basePileup)
|
|
|
|
|
pe.add(pileupElement);
|
|
|
|
|
}
|
|
|
|
|
jointContext = new AlignmentContext(loc, new ReadBackedPileupImpl(loc,pe));
|
2010-04-21 01:38:09 +08:00
|
|
|
}
|
|
|
|
|
|
2010-07-01 10:46:05 +08:00
|
|
|
return jointContext;
|
2010-03-29 05:45:22 +08:00
|
|
|
}
|
2009-12-11 03:21:16 +08:00
|
|
|
}
|