From 9712fed7a53267d5916e03ccd3f27d849fbbcb25 Mon Sep 17 00:00:00 2001 From: Mark DePristo Date: Mon, 27 Feb 2012 13:34:44 -0500 Subject: [PATCH] Trap SAMFormatException and rethrow as MalformatedBAM exception -- Trap errors in header and rethrow -- Wrap underlying iterator in MalformatedBAMErrorReformattingIterator --- .../gatk/datasources/reads/SAMDataSource.java | 3 ++ .../MalformedBAMErrorReformatingIterator.java | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 public/java/src/org/broadinstitute/sting/gatk/iterators/MalformedBAMErrorReformatingIterator.java diff --git a/public/java/src/org/broadinstitute/sting/gatk/datasources/reads/SAMDataSource.java b/public/java/src/org/broadinstitute/sting/gatk/datasources/reads/SAMDataSource.java index bba5c21e2..b215763b5 100755 --- a/public/java/src/org/broadinstitute/sting/gatk/datasources/reads/SAMDataSource.java +++ b/public/java/src/org/broadinstitute/sting/gatk/datasources/reads/SAMDataSource.java @@ -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); diff --git a/public/java/src/org/broadinstitute/sting/gatk/iterators/MalformedBAMErrorReformatingIterator.java b/public/java/src/org/broadinstitute/sting/gatk/iterators/MalformedBAMErrorReformatingIterator.java new file mode 100644 index 000000000..f5dee4961 --- /dev/null +++ b/public/java/src/org/broadinstitute/sting/gatk/iterators/MalformedBAMErrorReformatingIterator.java @@ -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 { + File source; + CloseableIterator it; + + public MalformedBAMErrorReformatingIterator(final File source, final CloseableIterator 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 iterator() { return this; } +}