diff --git a/java/src/org/broadinstitute/sting/gatk/io/storage/SAMFileWriterStorage.java b/java/src/org/broadinstitute/sting/gatk/io/storage/SAMFileWriterStorage.java index face5116e..65ba31b5c 100644 --- a/java/src/org/broadinstitute/sting/gatk/io/storage/SAMFileWriterStorage.java +++ b/java/src/org/broadinstitute/sting/gatk/io/storage/SAMFileWriterStorage.java @@ -90,7 +90,11 @@ public class SAMFileWriterStorage implements SAMFileWriter, Storage { throw new StingException("nWayOut mode: Reader id for input sam file "+fName+" is already registered"); File f = new File(outName); - SAMFileWriter sw = new SAMFileWriterFactory().makeSAMOrBAMWriter(setupHeader(getToolkit().getSAMFileHeader(rid)), - false,f); + SAMFileWriter sw = new SAMFileWriterFactory().makeSAMOrBAMWriter(setupHeader(getToolkit().getSAMFileHeader(rid)), false, f); nwayWriters.put(rid,sw); } @@ -379,28 +379,10 @@ public class IndelRealigner extends ReadWalker { } private void emit(final SAMRecord read) { - if ( writer != null ) - writer.addAlignment(read); - if ( N_WAY_OUT != null ) { - SAMReaderID rid = getToolkit().getReaderIDForRead(read); - SAMFileWriter w = nwayWriters.get(rid); - // reset read's read group from merged to original if read group id collision has happened in merging: - if ( getToolkit().getReadsDataSource().hasReadGroupCollisions() ) { - read.setAttribute("RG", - getToolkit().getReadsDataSource().getOriginalReadGroupId((String)read.getAttribute("RG"))); - } - w.addAlignment(read); - } - } - - private void emit(final List reads) { - if ( writer != null ) { - for ( SAMRecord read : reads ) + try { + if ( writer != null ) writer.addAlignment(read); - } - if ( N_WAY_OUT != null ) { - for ( SAMRecord read : reads ) { - // in initialize() we ensured that every reader has exactly one tag, so the following line is safe: + else if ( N_WAY_OUT != null ) { SAMReaderID rid = getToolkit().getReaderIDForRead(read); SAMFileWriter w = nwayWriters.get(rid); // reset read's read group from merged to original if read group id collision has happened in merging: @@ -409,11 +391,17 @@ public class IndelRealigner extends ReadWalker { getToolkit().getReadsDataSource().getOriginalReadGroupId((String)read.getAttribute("RG"))); } w.addAlignment(read); - } + } catch (RuntimeIOException e) { + throw new UserException.ErrorWritingBamFile(e.getMessage()); } } + private void emit(final List reads) { + for ( SAMRecord read : reads ) + emit(read); + } + public Integer map(ReferenceContext ref, SAMRecord read, ReadMetaDataTracker metaDataTracker) { if ( currentInterval == null ) { emit(read); @@ -544,7 +532,12 @@ public class IndelRealigner extends ReadWalker { } if ( N_WAY_OUT != null ) { - for ( SAMFileWriter w : nwayWriters.values() ) w.close(); + try { + for ( SAMFileWriter w : nwayWriters.values() ) + w.close(); + } catch (RuntimeIOException e) { + throw new UserException.ErrorWritingBamFile(e.getMessage()); + } } if ( CHECKEARLY ) { logger.info("SW alignments runs: "+SWalignmentRuns); diff --git a/java/src/org/broadinstitute/sting/utils/exceptions/UserException.java b/java/src/org/broadinstitute/sting/utils/exceptions/UserException.java index ac67bd94b..d1c7dab4f 100755 --- a/java/src/org/broadinstitute/sting/utils/exceptions/UserException.java +++ b/java/src/org/broadinstitute/sting/utils/exceptions/UserException.java @@ -77,6 +77,12 @@ public class UserException extends ReviewedStingException { } } + public static class ErrorWritingBamFile extends UserException { + public ErrorWritingBamFile(String message) { + super(String.format("An error occurred when trying to write the BAM file. Usually this happens when there is not enough space in the directory to which the data is being written (generally the temp directory) or when your system's open file handle limit is too small. To tell Java to use a bigger/better file system use -Djava.io.tmpdir=X on the command line. The exact error was %s", message)); + } + } + public static class CouldNotReadInputFile extends UserException { public CouldNotReadInputFile(String message, Exception e) { super(String.format("Couldn't read file because %s caused by %s", message, e.getMessage()));