Trap SAMFormatException and rethrow as MalformatedBAM exception

-- Trap errors in header and rethrow
-- Wrap underlying iterator in MalformatedBAMErrorReformattingIterator
This commit is contained in:
Mark DePristo 2012-02-27 13:34:44 -05:00
parent f2ef8d1d23
commit 9712fed7a5
2 changed files with 48 additions and 0 deletions

View File

@ -570,6 +570,7 @@ public class SAMDataSource {
inputStream.submitAccessPlan(new SAMReaderPosition(id,inputStream,(GATKBAMFileSpan)shard.getFileSpans().get(id)));
}
iterator = readers.getReader(id).iterator(shard.getFileSpans().get(id));
iterator = new MalformedBAMErrorReformatingIterator(id.samFile, iterator);
if(shard.getGenomeLocs().size() > 0)
iterator = new IntervalOverlapFilteringIterator(iterator,shard.getGenomeLocs());
mergingIterator.addIterator(readers.getReader(id),iterator);
@ -873,6 +874,8 @@ public class SAMDataSource {
throw new UserException.CouldNotReadInputFile(readerID.samFile, e);
else
throw e;
} catch ( SAMFormatException e ) {
throw new UserException.MalformedBAM(readerID.samFile, e.getMessage());
}
reader.setSAMRecordFactory(factory);
reader.enableFileSource(true);

View File

@ -0,0 +1,45 @@
package org.broadinstitute.sting.gatk.iterators;
import net.sf.samtools.SAMFormatException;
import net.sf.samtools.SAMRecord;
import net.sf.samtools.util.CloseableIterator;
import org.broadinstitute.sting.utils.exceptions.UserException;
import java.io.File;
import java.util.Iterator;
/**
* Traps BAM formatting errors in underlying iterator and rethrows meaningful GATK UserExceptions
*/
public class MalformedBAMErrorReformatingIterator implements CloseableIterator<SAMRecord> {
File source;
CloseableIterator<SAMRecord> it;
public MalformedBAMErrorReformatingIterator(final File source, final CloseableIterator<SAMRecord> it) {
this.it = it;
this.source = source;
}
public boolean hasNext() {
try {
return this.it.hasNext();
} catch ( SAMFormatException e ) {
throw new UserException.MalformedBAM(source, e.getMessage());
}
}
public SAMRecord next() {
try {
return it.next();
} catch ( SAMFormatException e ) {
throw new UserException.MalformedBAM(source, e.getMessage());
}
}
public void remove() {
throw new UnsupportedOperationException("Can not remove records from a SAM file via an iterator!");
}
public void close() { it.close(); }
public Iterator<SAMRecord> iterator() { return this; }
}