diff --git a/src/main/java/picard/sam/HitsForInsert.java b/src/main/java/picard/sam/HitsForInsert.java index 443c705f6b..535193a09d 100644 --- a/src/main/java/picard/sam/HitsForInsert.java +++ b/src/main/java/picard/sam/HitsForInsert.java @@ -27,7 +27,6 @@ import htsjdk.samtools.SAMTag; import java.util.ArrayList; -import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Objects; @@ -49,28 +48,26 @@ */ class HitsForInsert { - private static final HitIndexComparator comparator = new HitIndexComparator(); - public enum NumPrimaryAlignmentState { NONE, ONE, MORE_THAN_ONE } // These are package-visible to make life easier for the PrimaryAlignmentSelectionStrategies. - final List firstOfPairOrFragment = new ArrayList(); - final List secondOfPair = new ArrayList(); + final List firstOfPairOrFragment = new ArrayList<>(); + final List secondOfPair = new ArrayList<>(); - private final List supplementalFirstOfPairOrFragment = new ArrayList(); - private final List supplementalSecondOfPair = new ArrayList(); + private final List supplementalFirstOfPairOrFragment = new ArrayList<>(); + private final List supplementalSecondOfPair = new ArrayList<>(); /** - * @throws if numHits() == 0 + * @throws IllegalStateException if numHits() == 0 */ public String getReadName() { return getRepresentativeRead().getReadName(); } /** - * @throws if numHits() == 0 + * @throws IllegalStateException if numHits() == 0 */ public boolean isPaired() { return getRepresentativeRead().getReadPairedFlag(); @@ -196,8 +193,12 @@ public void setPrimaryAlignment(final int primaryAlignmentIndex) { */ public void coordinateByHitIndex() { // Sort by HI value, with reads with no HI going at the end. - Collections.sort(firstOfPairOrFragment, comparator); - Collections.sort(secondOfPair, comparator); + final Comparator comparator = Comparator.comparing( + record -> record.getIntegerAttribute(SAMTag.HI.name()), + Comparator.nullsLast(Comparator.naturalOrder()) + ); + firstOfPairOrFragment.sort(comparator); + secondOfPair.sort(comparator); // Insert nulls as necessary in the two lists so that correlated alignments have the same index // and uncorrelated alignments have null in the other list at the corresponding index. @@ -224,14 +225,12 @@ public void coordinateByHitIndex() { * Renumber any correlated alignments, and remove hit index if no correlated read. */ private void renumberHitIndex() { - int hi = 0; for (int i = 0; i < numHits(); ++i) { final SAMRecord first = getFirstOfPair(i); final SAMRecord second = getSecondOfPair(i); if (first != null && second != null) { first.setAttribute(SAMTag.HI.name(), i); second.setAttribute(SAMTag.HI.name(), i); - ++hi; } else if (first != null) { first.setAttribute(SAMTag.HI.name(), null); } else { @@ -303,35 +302,18 @@ private static boolean arePair(final SAMRecord first, final SAMRecord second) { */ private NumPrimaryAlignmentState tallyPrimaryAlignments(final List records) { boolean seenPrimary = false; - for (int i = 0; i < records.size(); ++i) { - if (records.get(i) != null && !records.get(i).isSecondaryOrSupplementary()) { + for (SAMRecord record : records) { + if (record != null && !record.isSecondaryOrSupplementary()) { if (seenPrimary) return NumPrimaryAlignmentState.MORE_THAN_ONE; else seenPrimary = true; } } - if (seenPrimary) return NumPrimaryAlignmentState.ONE; - else return NumPrimaryAlignmentState.NONE; + return seenPrimary ? NumPrimaryAlignmentState.ONE : NumPrimaryAlignmentState.NONE; } public NumPrimaryAlignmentState tallyPrimaryAlignments(final boolean firstEnd) { - if (firstEnd) return tallyPrimaryAlignments(firstOfPairOrFragment); - else return tallyPrimaryAlignments(secondOfPair); - } - - // null HI tag sorts after any non-null. - private static class HitIndexComparator implements Comparator { - public int compare(final SAMRecord rec1, final SAMRecord rec2) { - final Integer hi1 = rec1.getIntegerAttribute(SAMTag.HI.name()); - final Integer hi2 = rec2.getIntegerAttribute(SAMTag.HI.name()); - if (hi1 == null) { - if (hi2 == null) return 0; - else return 1; - } else if (hi2 == null) { - return -1; - } else { - return hi1.compareTo(hi2); - } - } + final List records = firstEnd ? firstOfPairOrFragment : secondOfPair; + return tallyPrimaryAlignments(records); } List getSupplementalFirstOfPairOrFragment() {