Implementation of BySampleSAMFileWriter
ReduceReads now works with the n-way-out capability, splitting by sample. DEV-27 #resolve #time 3m
This commit is contained in:
parent
1f23a82aa4
commit
95a4ba57bf
|
|
@ -25,6 +25,9 @@
|
||||||
|
|
||||||
package org.broadinstitute.sting.gatk.walkers.compression.reducereads;
|
package org.broadinstitute.sting.gatk.walkers.compression.reducereads;
|
||||||
|
|
||||||
|
import net.sf.samtools.SAMFileHeader;
|
||||||
|
import net.sf.samtools.SAMFileWriter;
|
||||||
|
import net.sf.samtools.SAMProgramRecord;
|
||||||
import net.sf.samtools.util.SequenceUtil;
|
import net.sf.samtools.util.SequenceUtil;
|
||||||
import org.broadinstitute.sting.commandline.Argument;
|
import org.broadinstitute.sting.commandline.Argument;
|
||||||
import org.broadinstitute.sting.commandline.Hidden;
|
import org.broadinstitute.sting.commandline.Hidden;
|
||||||
|
|
@ -45,6 +48,7 @@ import org.broadinstitute.sting.utils.Utils;
|
||||||
import org.broadinstitute.sting.utils.clipping.ReadClipper;
|
import org.broadinstitute.sting.utils.clipping.ReadClipper;
|
||||||
import org.broadinstitute.sting.utils.exceptions.ReviewedStingException;
|
import org.broadinstitute.sting.utils.exceptions.ReviewedStingException;
|
||||||
import org.broadinstitute.sting.utils.help.DocumentedGATKFeature;
|
import org.broadinstitute.sting.utils.help.DocumentedGATKFeature;
|
||||||
|
import org.broadinstitute.sting.utils.sam.BySampleSAMFileWriter;
|
||||||
import org.broadinstitute.sting.utils.sam.GATKSAMRecord;
|
import org.broadinstitute.sting.utils.sam.GATKSAMRecord;
|
||||||
import org.broadinstitute.sting.utils.sam.ReadUtils;
|
import org.broadinstitute.sting.utils.sam.ReadUtils;
|
||||||
|
|
||||||
|
|
@ -86,7 +90,8 @@ import java.util.*;
|
||||||
public class ReduceReads extends ReadWalker<LinkedList<GATKSAMRecord>, ReduceReadsStash> {
|
public class ReduceReads extends ReadWalker<LinkedList<GATKSAMRecord>, ReduceReadsStash> {
|
||||||
|
|
||||||
@Output
|
@Output
|
||||||
private StingSAMFileWriter out;
|
private StingSAMFileWriter out = null;
|
||||||
|
private SAMFileWriter writerToUse = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The number of bases to keep around mismatches (potential variation)
|
* The number of bases to keep around mismatches (potential variation)
|
||||||
|
|
@ -196,6 +201,10 @@ public class ReduceReads extends ReadWalker<LinkedList<GATKSAMRecord>, ReduceRea
|
||||||
@Argument(fullName = "contigs", shortName = "ctg", doc = "", required = false)
|
@Argument(fullName = "contigs", shortName = "ctg", doc = "", required = false)
|
||||||
private int nContigs = 2;
|
private int nContigs = 2;
|
||||||
|
|
||||||
|
@Hidden
|
||||||
|
@Argument(fullName = "nwayout", shortName = "nw", doc = "", required = false)
|
||||||
|
private boolean nwayout = false;
|
||||||
|
|
||||||
@Hidden
|
@Hidden
|
||||||
@Argument(fullName = "", shortName = "dl", doc = "", required = false)
|
@Argument(fullName = "", shortName = "dl", doc = "", required = false)
|
||||||
private int debugLevel = 0;
|
private int debugLevel = 0;
|
||||||
|
|
@ -227,6 +236,7 @@ public class ReduceReads extends ReadWalker<LinkedList<GATKSAMRecord>, ReduceRea
|
||||||
SortedSet<GenomeLoc> intervalList;
|
SortedSet<GenomeLoc> intervalList;
|
||||||
|
|
||||||
private static final String PROGRAM_RECORD_NAME = "GATK ReduceReads"; // The name that will go in the @PG tag
|
private static final String PROGRAM_RECORD_NAME = "GATK ReduceReads"; // The name that will go in the @PG tag
|
||||||
|
private static final String PROGRAM_FILENAME_EXTENSION = ".reduced.bam";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Basic generic initialization of the readNameHash and the intervalList. Output initialization
|
* Basic generic initialization of the readNameHash and the intervalList. Output initialization
|
||||||
|
|
@ -242,10 +252,24 @@ public class ReduceReads extends ReadWalker<LinkedList<GATKSAMRecord>, ReduceRea
|
||||||
if (toolkit.getIntervals() != null)
|
if (toolkit.getIntervals() != null)
|
||||||
intervalList.addAll(toolkit.getIntervals());
|
intervalList.addAll(toolkit.getIntervals());
|
||||||
|
|
||||||
if (!NO_PG_TAG)
|
|
||||||
Utils.setupWriter(out, toolkit, false, true, this, PROGRAM_RECORD_NAME);
|
// todo -- rework the whole NO_PG_TAG thing
|
||||||
else
|
final boolean preSorted = true;
|
||||||
|
final boolean indexOnTheFly = true;
|
||||||
|
final boolean generateMD5 = true;
|
||||||
|
final boolean keep_records = true;
|
||||||
|
final SAMFileHeader.SortOrder sortOrder = SAMFileHeader.SortOrder.coordinate;
|
||||||
|
if (nwayout) {
|
||||||
|
SAMProgramRecord programRecord = NO_PG_TAG ? null : Utils.createProgramRecord(toolkit, this, PROGRAM_RECORD_NAME);
|
||||||
|
writerToUse = new BySampleSAMFileWriter(toolkit, PROGRAM_FILENAME_EXTENSION, sortOrder, preSorted, indexOnTheFly, NO_PG_TAG, programRecord, true);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
writerToUse = out;
|
||||||
out.setPresorted(false);
|
out.setPresorted(false);
|
||||||
|
if (!NO_PG_TAG) {
|
||||||
|
Utils.setupWriter(out, toolkit, !preSorted, keep_records, this, PROGRAM_RECORD_NAME);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -386,6 +410,9 @@ public class ReduceReads extends ReadWalker<LinkedList<GATKSAMRecord>, ReduceRea
|
||||||
// output any remaining reads in the compressor
|
// output any remaining reads in the compressor
|
||||||
for (GATKSAMRecord read : stash.close())
|
for (GATKSAMRecord read : stash.close())
|
||||||
outputRead(read);
|
outputRead(read);
|
||||||
|
|
||||||
|
if (nwayout)
|
||||||
|
writerToUse.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -554,7 +581,7 @@ public class ReduceReads extends ReadWalker<LinkedList<GATKSAMRecord>, ReduceRea
|
||||||
if (!DONT_COMPRESS_READ_NAMES)
|
if (!DONT_COMPRESS_READ_NAMES)
|
||||||
compressReadName(read);
|
compressReadName(read);
|
||||||
|
|
||||||
out.addAlignment(read);
|
writerToUse.addAlignment(read);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,70 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2010 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.utils.sam;
|
||||||
|
|
||||||
|
import net.sf.samtools.SAMFileHeader;
|
||||||
|
import net.sf.samtools.SAMProgramRecord;
|
||||||
|
import net.sf.samtools.SAMReadGroupRecord;
|
||||||
|
import net.sf.samtools.SAMRecord;
|
||||||
|
import org.broadinstitute.sting.gatk.GenomeAnalysisEngine;
|
||||||
|
import org.broadinstitute.sting.gatk.datasources.reads.SAMReaderID;
|
||||||
|
import org.broadinstitute.sting.utils.exceptions.ReviewedStingException;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by IntelliJ IDEA.
|
||||||
|
* User: carneiro
|
||||||
|
* Date: Nov 13
|
||||||
|
*/
|
||||||
|
public class BySampleSAMFileWriter extends NWaySAMFileWriter{
|
||||||
|
|
||||||
|
private final Map<String, SAMReaderID> sampleToWriterMap;
|
||||||
|
|
||||||
|
public BySampleSAMFileWriter(GenomeAnalysisEngine toolkit, String ext, SAMFileHeader.SortOrder order, boolean presorted, boolean indexOnTheFly, boolean generateMD5, SAMProgramRecord pRecord, boolean keep_records) {
|
||||||
|
super(toolkit, ext, order, presorted, indexOnTheFly, generateMD5, pRecord, keep_records);
|
||||||
|
|
||||||
|
sampleToWriterMap = new HashMap<String, SAMReaderID>(toolkit.getSAMFileHeader().getReadGroups().size() * 2);
|
||||||
|
|
||||||
|
for (SAMReaderID readerID : toolkit.getReadsDataSource().getReaderIDs()) {
|
||||||
|
for (SAMReadGroupRecord rg : toolkit.getReadsDataSource().getHeader(readerID).getReadGroups()) {
|
||||||
|
String sample = rg.getSample();
|
||||||
|
if (sampleToWriterMap.containsKey(sample) && sampleToWriterMap.get(sample) != readerID) {
|
||||||
|
throw new ReviewedStingException("The same sample appears in multiple files, this input cannot be multiplexed using the BySampleSAMFileWriter, try NWaySAMFileWriter instead.");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sampleToWriterMap.put(sample, readerID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addAlignment(SAMRecord samRecord) {
|
||||||
|
super.addAlignment(samRecord, sampleToWriterMap.get(samRecord.getReadGroup().getSample()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,186 +1,188 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2010 The Broad Institute
|
* Copyright (c) 2010 The Broad Institute
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person
|
* Permission is hereby granted, free of charge, to any person
|
||||||
* obtaining a copy of this software and associated documentation
|
* obtaining a copy of this software and associated documentation
|
||||||
* files (the "Software"), to deal in the Software without
|
* files (the "Software"), to deal in the Software without
|
||||||
* restriction, including without limitation the rights to use,
|
* restriction, including without limitation the rights to use,
|
||||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the
|
* copies of the Software, and to permit persons to whom the
|
||||||
* Software is furnished to do so, subject to the following
|
* Software is furnished to do so, subject to the following
|
||||||
* conditions:
|
* conditions:
|
||||||
*
|
*
|
||||||
* The above copyright notice and this permission notice shall be
|
* The above copyright notice and this permission notice shall be
|
||||||
* included in all copies or substantial portions of the Software.
|
* included in all copies or substantial portions of the Software.
|
||||||
*
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
* OTHER DEALINGS IN THE SOFTWARE.
|
* OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.broadinstitute.sting.utils.sam;
|
package org.broadinstitute.sting.utils.sam;
|
||||||
|
|
||||||
import net.sf.samtools.*;
|
import net.sf.samtools.*;
|
||||||
import org.broadinstitute.sting.gatk.GenomeAnalysisEngine;
|
import org.broadinstitute.sting.gatk.GenomeAnalysisEngine;
|
||||||
import org.broadinstitute.sting.gatk.datasources.reads.SAMReaderID;
|
import org.broadinstitute.sting.gatk.datasources.reads.SAMReaderID;
|
||||||
import org.broadinstitute.sting.gatk.io.StingSAMFileWriter;
|
import org.broadinstitute.sting.utils.exceptions.StingException;
|
||||||
import org.broadinstitute.sting.utils.exceptions.StingException;
|
import org.broadinstitute.sting.utils.exceptions.UserException;
|
||||||
import org.broadinstitute.sting.utils.exceptions.UserException;
|
|
||||||
import org.broadinstitute.sting.utils.text.TextFormattingUtils;
|
import java.io.File;
|
||||||
|
import java.util.*;
|
||||||
import java.io.File;
|
|
||||||
import java.util.*;
|
/**
|
||||||
|
* Created by IntelliJ IDEA.
|
||||||
/**
|
* User: asivache
|
||||||
* Created by IntelliJ IDEA.
|
* Date: May 31, 2011
|
||||||
* User: asivache
|
* Time: 3:52:49 PM
|
||||||
* Date: May 31, 2011
|
* To change this template use File | Settings | File Templates.
|
||||||
* Time: 3:52:49 PM
|
*/
|
||||||
* To change this template use File | Settings | File Templates.
|
public class NWaySAMFileWriter implements SAMFileWriter {
|
||||||
*/
|
|
||||||
public class NWaySAMFileWriter implements SAMFileWriter {
|
private Map<SAMReaderID,SAMFileWriter> writerMap = null;
|
||||||
|
private boolean presorted ;
|
||||||
private Map<SAMReaderID,SAMFileWriter> writerMap = null;
|
GenomeAnalysisEngine toolkit;
|
||||||
private boolean presorted ;
|
boolean KEEP_ALL_PG_RECORDS = false;
|
||||||
GenomeAnalysisEngine toolkit;
|
|
||||||
boolean KEEP_ALL_PG_RECORDS = false;
|
public NWaySAMFileWriter(GenomeAnalysisEngine toolkit, Map<String,String> in2out, SAMFileHeader.SortOrder order,
|
||||||
|
boolean presorted, boolean indexOnTheFly, boolean generateMD5, SAMProgramRecord pRecord, boolean keep_records) {
|
||||||
public NWaySAMFileWriter(GenomeAnalysisEngine toolkit, Map<String,String> in2out, SAMFileHeader.SortOrder order,
|
this.presorted = presorted;
|
||||||
boolean presorted, boolean indexOnTheFly, boolean generateMD5, SAMProgramRecord pRecord, boolean keep_records) {
|
this.toolkit = toolkit;
|
||||||
this.presorted = presorted;
|
this.KEEP_ALL_PG_RECORDS = keep_records;
|
||||||
this.toolkit = toolkit;
|
writerMap = new HashMap<SAMReaderID,SAMFileWriter>();
|
||||||
this.KEEP_ALL_PG_RECORDS = keep_records;
|
setupByReader(toolkit,in2out,order, presorted, indexOnTheFly, generateMD5, pRecord);
|
||||||
writerMap = new HashMap<SAMReaderID,SAMFileWriter>();
|
}
|
||||||
setupByReader(toolkit,in2out,order, presorted, indexOnTheFly, generateMD5, pRecord);
|
|
||||||
}
|
public NWaySAMFileWriter(GenomeAnalysisEngine toolkit, String ext, SAMFileHeader.SortOrder order,
|
||||||
|
boolean presorted, boolean indexOnTheFly , boolean generateMD5, SAMProgramRecord pRecord, boolean keep_records) {
|
||||||
public NWaySAMFileWriter(GenomeAnalysisEngine toolkit, String ext, SAMFileHeader.SortOrder order,
|
this.presorted = presorted;
|
||||||
boolean presorted, boolean indexOnTheFly , boolean generateMD5, SAMProgramRecord pRecord, boolean keep_records) {
|
this.toolkit = toolkit;
|
||||||
this.presorted = presorted;
|
this.KEEP_ALL_PG_RECORDS = keep_records;
|
||||||
this.toolkit = toolkit;
|
writerMap = new HashMap<SAMReaderID,SAMFileWriter>();
|
||||||
this.KEEP_ALL_PG_RECORDS = keep_records;
|
setupByReader(toolkit,ext,order, presorted, indexOnTheFly, generateMD5, pRecord);
|
||||||
writerMap = new HashMap<SAMReaderID,SAMFileWriter>();
|
}
|
||||||
setupByReader(toolkit,ext,order, presorted, indexOnTheFly, generateMD5, pRecord);
|
|
||||||
}
|
public NWaySAMFileWriter(GenomeAnalysisEngine toolkit, Map<String,String> in2out, SAMFileHeader.SortOrder order,
|
||||||
|
boolean presorted, boolean indexOnTheFly, boolean generateMD5) {
|
||||||
public NWaySAMFileWriter(GenomeAnalysisEngine toolkit, Map<String,String> in2out, SAMFileHeader.SortOrder order,
|
this(toolkit, in2out, order, presorted, indexOnTheFly, generateMD5, null,false);
|
||||||
boolean presorted, boolean indexOnTheFly, boolean generateMD5) {
|
}
|
||||||
this(toolkit, in2out, order, presorted, indexOnTheFly, generateMD5, null,false);
|
|
||||||
}
|
public NWaySAMFileWriter(GenomeAnalysisEngine toolkit, String ext, SAMFileHeader.SortOrder order,
|
||||||
|
boolean presorted, boolean indexOnTheFly , boolean generateMD5) {
|
||||||
public NWaySAMFileWriter(GenomeAnalysisEngine toolkit, String ext, SAMFileHeader.SortOrder order,
|
this(toolkit, ext, order, presorted, indexOnTheFly, generateMD5, null,false);
|
||||||
boolean presorted, boolean indexOnTheFly , boolean generateMD5) {
|
}
|
||||||
this(toolkit, ext, order, presorted, indexOnTheFly, generateMD5, null,false);
|
|
||||||
}
|
/**
|
||||||
|
* Instantiates multiple underlying SAM writes, one per input SAM reader registered with GATK engine (those will be retrieved
|
||||||
/**
|
* from <code>toolkit</code>). The <code>in2out</code> map must contain an entry for each input filename and map it
|
||||||
* Instantiates multiple underlying SAM writes, one per input SAM reader registered with GATK engine (those will be retrieved
|
* onto a unique output file name.
|
||||||
* from <code>toolkit</code>). The <code>in2out</code> map must contain an entry for each input filename and map it
|
* @param toolkit
|
||||||
* onto a unique output file name.
|
* @param in2out
|
||||||
* @param toolkit
|
*/
|
||||||
* @param in2out
|
public void setupByReader(GenomeAnalysisEngine toolkit, Map<String,String> in2out, SAMFileHeader.SortOrder order,
|
||||||
*/
|
boolean presorted, boolean indexOnTheFly, boolean generateMD5, SAMProgramRecord pRecord) {
|
||||||
public void setupByReader(GenomeAnalysisEngine toolkit, Map<String,String> in2out, SAMFileHeader.SortOrder order,
|
if ( in2out==null ) throw new StingException("input-output bam filename map for n-way-out writing is NULL");
|
||||||
boolean presorted, boolean indexOnTheFly, boolean generateMD5, SAMProgramRecord pRecord) {
|
for ( SAMReaderID rid : toolkit.getReadsDataSource().getReaderIDs() ) {
|
||||||
if ( in2out==null ) throw new StingException("input-output bam filename map for n-way-out writing is NULL");
|
|
||||||
for ( SAMReaderID rid : toolkit.getReadsDataSource().getReaderIDs() ) {
|
String fName = toolkit.getReadsDataSource().getSAMFile(rid).getName();
|
||||||
|
|
||||||
String fName = toolkit.getReadsDataSource().getSAMFile(rid).getName();
|
String outName;
|
||||||
|
if ( ! in2out.containsKey(fName) )
|
||||||
String outName;
|
throw new UserException.BadInput("Input-output bam filename map does not contain an entry for the input file "+fName);
|
||||||
if ( ! in2out.containsKey(fName) )
|
outName = in2out.get(fName);
|
||||||
throw new UserException.BadInput("Input-output bam filename map does not contain an entry for the input file "+fName);
|
|
||||||
outName = in2out.get(fName);
|
if ( writerMap.containsKey( rid ) )
|
||||||
|
throw new StingException("nWayOut mode: Reader id for input sam file "+fName+" is already registered; "+
|
||||||
if ( writerMap.containsKey( rid ) )
|
"map file likely contains multiple entries for this input file");
|
||||||
throw new StingException("nWayOut mode: Reader id for input sam file "+fName+" is already registered; "+
|
|
||||||
"map file likely contains multiple entries for this input file");
|
addWriter(rid,outName, order, presorted, indexOnTheFly, generateMD5, pRecord);
|
||||||
|
}
|
||||||
addWriter(rid,outName, order, presorted, indexOnTheFly, generateMD5, pRecord);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
/**
|
||||||
|
* Instantiates multiple underlying SAM writes, one per input SAM reader registered with GATK engine (those will be retrieved
|
||||||
/**
|
* from <code>toolkit</code>). The output file names will be generated automatically by stripping ".sam" or ".bam" off the
|
||||||
* Instantiates multiple underlying SAM writes, one per input SAM reader registered with GATK engine (those will be retrieved
|
* input file name and adding ext instead (e.g. ".cleaned.bam").
|
||||||
* from <code>toolkit</code>). The output file names will be generated automatically by stripping ".sam" or ".bam" off the
|
* onto a unique output file name.
|
||||||
* input file name and adding ext instead (e.g. ".cleaned.bam").
|
* @param toolkit
|
||||||
* onto a unique output file name.
|
* @param ext
|
||||||
* @param toolkit
|
*/
|
||||||
* @param ext
|
public void setupByReader(GenomeAnalysisEngine toolkit, String ext, SAMFileHeader.SortOrder order,
|
||||||
*/
|
boolean presorted, boolean indexOnTheFly, boolean generateMD5, SAMProgramRecord pRecord) {
|
||||||
public void setupByReader(GenomeAnalysisEngine toolkit, String ext, SAMFileHeader.SortOrder order,
|
for ( SAMReaderID rid : toolkit.getReadsDataSource().getReaderIDs() ) {
|
||||||
boolean presorted, boolean indexOnTheFly, boolean generateMD5, SAMProgramRecord pRecord) {
|
|
||||||
for ( SAMReaderID rid : toolkit.getReadsDataSource().getReaderIDs() ) {
|
String fName = toolkit.getReadsDataSource().getSAMFile(rid).getName();
|
||||||
|
|
||||||
String fName = toolkit.getReadsDataSource().getSAMFile(rid).getName();
|
String outName;
|
||||||
|
int pos ;
|
||||||
String outName;
|
if ( fName.toUpperCase().endsWith(".BAM") ) pos = fName.toUpperCase().lastIndexOf(".BAM");
|
||||||
int pos ;
|
else {
|
||||||
if ( fName.toUpperCase().endsWith(".BAM") ) pos = fName.toUpperCase().lastIndexOf(".BAM");
|
if ( fName.toUpperCase().endsWith(".SAM") ) pos = fName.toUpperCase().lastIndexOf(".SAM");
|
||||||
else {
|
else throw new UserException.BadInput("Input file name "+fName+" does not end with .sam or .bam");
|
||||||
if ( fName.toUpperCase().endsWith(".SAM") ) pos = fName.toUpperCase().lastIndexOf(".SAM");
|
}
|
||||||
else throw new UserException.BadInput("Input file name "+fName+" does not end with .sam or .bam");
|
String prefix = fName.substring(0,pos);
|
||||||
}
|
outName = prefix+ext;
|
||||||
String prefix = fName.substring(0,pos);
|
|
||||||
outName = prefix+ext;
|
if ( writerMap.containsKey( rid ) )
|
||||||
|
throw new StingException("nWayOut mode: Reader id for input sam file "+fName+" is already registered");
|
||||||
if ( writerMap.containsKey( rid ) )
|
addWriter(rid,outName, order, presorted, indexOnTheFly, generateMD5, pRecord);
|
||||||
throw new StingException("nWayOut mode: Reader id for input sam file "+fName+" is already registered");
|
}
|
||||||
addWriter(rid,outName, order, presorted, indexOnTheFly, generateMD5, pRecord);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
private void addWriter(SAMReaderID id , String outName, SAMFileHeader.SortOrder order, boolean presorted,
|
||||||
|
boolean indexOnTheFly, boolean generateMD5, SAMProgramRecord programRecord) {
|
||||||
private void addWriter(SAMReaderID id , String outName, SAMFileHeader.SortOrder order, boolean presorted,
|
File f = new File(outName);
|
||||||
boolean indexOnTheFly, boolean generateMD5, SAMProgramRecord programRecord) {
|
SAMFileHeader header = toolkit.getSAMFileHeader(id).clone();
|
||||||
File f = new File(outName);
|
header.setSortOrder(order);
|
||||||
SAMFileHeader header = toolkit.getSAMFileHeader(id).clone();
|
|
||||||
header.setSortOrder(order);
|
if ( programRecord != null ) {
|
||||||
|
// --->> add program record
|
||||||
if ( programRecord != null ) {
|
List<SAMProgramRecord> oldRecords = header.getProgramRecords();
|
||||||
// --->> add program record
|
List<SAMProgramRecord> newRecords = new ArrayList<SAMProgramRecord>(oldRecords.size()+1);
|
||||||
List<SAMProgramRecord> oldRecords = header.getProgramRecords();
|
for ( SAMProgramRecord record : oldRecords ) {
|
||||||
List<SAMProgramRecord> newRecords = new ArrayList<SAMProgramRecord>(oldRecords.size()+1);
|
if ( !record.getId().startsWith(programRecord.getId()) || KEEP_ALL_PG_RECORDS )
|
||||||
for ( SAMProgramRecord record : oldRecords ) {
|
newRecords.add(record);
|
||||||
if ( !record.getId().startsWith(programRecord.getId()) || KEEP_ALL_PG_RECORDS )
|
}
|
||||||
newRecords.add(record);
|
newRecords.add(programRecord);
|
||||||
}
|
header.setProgramRecords(newRecords);
|
||||||
newRecords.add(programRecord);
|
// <-- add program record ends here
|
||||||
header.setProgramRecords(newRecords);
|
}
|
||||||
// <-- add program record ends here
|
SAMFileWriterFactory factory = new SAMFileWriterFactory();
|
||||||
}
|
factory.setCreateIndex(indexOnTheFly);
|
||||||
SAMFileWriterFactory factory = new SAMFileWriterFactory();
|
factory.setCreateMd5File(generateMD5);
|
||||||
factory.setCreateIndex(indexOnTheFly);
|
SAMFileWriter sw = factory.makeSAMOrBAMWriter(header, presorted, f);
|
||||||
factory.setCreateMd5File(generateMD5);
|
writerMap.put(id,sw);
|
||||||
SAMFileWriter sw = factory.makeSAMOrBAMWriter(header, presorted, f);
|
}
|
||||||
writerMap.put(id,sw);
|
|
||||||
}
|
public Collection<SAMFileWriter> getWriters() {
|
||||||
|
return writerMap.values();
|
||||||
public Collection<SAMFileWriter> getWriters() {
|
}
|
||||||
return writerMap.values();
|
|
||||||
}
|
public void addAlignment(SAMRecord samRecord) {
|
||||||
|
final SAMReaderID id = toolkit.getReaderIDForRead(samRecord);
|
||||||
public void addAlignment(SAMRecord samRecord) {
|
String rg = samRecord.getStringAttribute("RG");
|
||||||
final SAMReaderID id = toolkit.getReaderIDForRead(samRecord);
|
if ( rg != null ) {
|
||||||
String rg = samRecord.getStringAttribute("RG");
|
String rg_orig = toolkit.getReadsDataSource().getOriginalReadGroupId(rg);
|
||||||
if ( rg != null ) {
|
samRecord.setAttribute("RG",rg_orig);
|
||||||
String rg_orig = toolkit.getReadsDataSource().getOriginalReadGroupId(rg);
|
}
|
||||||
samRecord.setAttribute("RG",rg_orig);
|
addAlignment(samRecord, id);
|
||||||
}
|
}
|
||||||
writerMap.get(id).addAlignment(samRecord);
|
|
||||||
}
|
public void addAlignment(SAMRecord samRecord, SAMReaderID readerID) {
|
||||||
|
writerMap.get(readerID).addAlignment(samRecord);
|
||||||
public SAMFileHeader getFileHeader() {
|
}
|
||||||
return toolkit.getSAMFileHeader();
|
|
||||||
}
|
public SAMFileHeader getFileHeader() {
|
||||||
|
return toolkit.getSAMFileHeader();
|
||||||
public void close() {
|
}
|
||||||
for ( SAMFileWriter w : writerMap.values() ) w.close();
|
|
||||||
}
|
public void close() {
|
||||||
}
|
for ( SAMFileWriter w : writerMap.values() ) w.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue