/*
 * The MIT License
 *
 * Copyright (c) 2009 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 net.sf.samtools;

/**
 * Comparator for "queryname" ordering of SAMRecords.
 */
public class SAMRecordQueryNameComparator implements SAMRecordComparator {

    public int compare(final SAMRecord samRecord1, final SAMRecord samRecord2) {
        final int cmp = fileOrderCompare(samRecord1, samRecord2);
        if (cmp != 0) {
            return cmp;
        }
        if (samRecord1.getReadPairedFlag()) {
            if (samRecord1.getFirstOfPairFlag() && samRecord2.getSecondOfPairFlag()) {
                return -1;
            }
            else if (samRecord2.getFirstOfPairFlag() && samRecord1.getSecondOfPairFlag()) {
                return 1;
            }
        }

        if (samRecord1.getReadNegativeStrandFlag() == samRecord2.getReadNegativeStrandFlag()) {
            return 0;
        }
        return (samRecord1.getReadNegativeStrandFlag()? 1: -1);
    }

    /**
     * Less stringent compare method than the regular compare.  If the two records
     * are equal enough that their ordering in a sorted SAM file would be arbitrary,
     * this method returns 0.
     *
     * @return negative if samRecord1 < samRecord2,  0 if equal, else positive
     */
    public int fileOrderCompare(final SAMRecord samRecord1, final SAMRecord samRecord2) {
        return samRecord1.getReadName().compareTo(samRecord2.getReadName());
    }
}

@@ -1508,24 +1508,25 @@
       %7D%0A
+%0A
         
 if (samR
@@ -1509,36 +1509,57 @@
      %7D%0A%0A        
-if (
+final boolean r1Paired = 
 samRecord1.getRe
@@ -1568,27 +1568,25 @@
 PairedFlag()
-) %7B
+;
 %0A           
@@ -1586,50 +1586,32 @@
     
-    if (samRecord1.getFirstOfPairFlag() &&
+final boolean r2Paired =
  sam
@@ -1621,34 +1621,67 @@
 ord2.get
-SecondOf
+Read
 Pair
+ed
 Flag()
+;%0A%0A        if (r1Paired %7C%7C r2Paired
 ) %7B%0A    
@@ -1688,27 +1688,38 @@
         
-   
+if (!r1Paired)
  return 
 -1;%0A    
@@ -1710,17 +1710,16 @@
  return 
--
 1;%0A     
@@ -1717,33 +1717,62 @@
  1;%0A            
-%7D
+else if (!r2Paired) return -1;
 %0A            els
@@ -1782,25 +1782,25 @@
 f (samRecord
-2
+1
 .getFirstOfP
@@ -1808,16 +1808,17 @@
 irFlag()
+ 
  && samR
@@ -1818,25 +1818,25 @@
 && samRecord
-1
+2
 .getSecondOf
@@ -1839,33 +1839,42 @@
 ndOfPairFlag()) 
-%7B
+return -1;
 %0A               
@@ -1874,35 +1874,95 @@
     
-    return 1;%0A            %7D
+else if (samRecord1.getSecondOfPairFlag() && samRecord2.getFirstOfPairFlag()) return 1;
 %0A   
@@ -2062,30 +2062,16 @@
 dFlag())
- %7B%0A           
  return 
@@ -2085,17 +2085,12 @@
     
-%7D%0A       
+else
  ret

Fix for bug where samRecord2.getSecondOfPairFlag() was being called without checking that samRecord2 is paired.