diff --git a/java/src/org/broadinstitute/sting/utils/AlignmentUtils.java b/java/src/org/broadinstitute/sting/utils/AlignmentUtils.java index a9c3f289d..aeff9a789 100644 --- a/java/src/org/broadinstitute/sting/utils/AlignmentUtils.java +++ b/java/src/org/broadinstitute/sting/utils/AlignmentUtils.java @@ -268,8 +268,16 @@ public class AlignmentUtils { */ public static boolean isReadUnmapped(final SAMRecord r) { if ( r.getReadUnmappedFlag() ) return true; - if ( r.getReferenceIndex() == SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX || - r.getAlignmentStart() == SAMRecord.NO_ALIGNMENT_START ) return true; - return false; + + // our life would be so much easier if all sam files followed the specs. In reality, + // sam files (including those generated by maq or bwa) miss headers alltogether. When + // reading such a SAM file, reference name is set, but since there is no sequence dictionary, + // null is always returned for referenceIndex. Let's be paranoid here, and make sure that + // we do not call the read "unmapped" when it has only reference name set with ref. index missing + // or vice versa. + if ( ( r.getReferenceIndex() != null && r.getReferenceIndex() != SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX + || r.getReferenceName() != null && r.getReferenceName() != SAMRecord.NO_ALIGNMENT_REFERENCE_NAME ) + && r.getAlignmentStart() != SAMRecord.NO_ALIGNMENT_START ) return false ; + return true; } }