Skip to content

Commit

Permalink
refactor: nonNull and isNull replaced by operators
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisVerkhoturov committed Sep 20, 2017
1 parent 11a9c1f commit 96bc12a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 33 deletions.
48 changes: 20 additions & 28 deletions src/main/java/picard/sam/HitsForInsert.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
import java.util.List;
import java.util.Objects;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

/**
* Holds all the hits (alignments) for a read or read pair. For single-end reads, all the alignments are
* stored in firstOfPairOrFragment list. For paired-end, alignments are stored in firstOfPairOrFragment list and
Expand Down Expand Up @@ -207,27 +204,22 @@ record -> record.getIntegerAttribute(SAMTag.HI.name()),
// 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.
for (int i = 0; i < Math.min(firstOfPairOrFragment.size(), secondOfPair.size()); ++i) {
final Integer hiTagValueOfFirst = firstOfPairOrFragment.get(i).getIntegerAttribute(SAMTag.HI.name());
final Integer hiTagValueOfSecond = secondOfPair.get(i).getIntegerAttribute(SAMTag.HI.name());

if (nonNull(hiTagValueOfFirst) && nonNull(hiTagValueOfSecond)) {
if (hiTagValueOfFirst < hiTagValueOfSecond) {
secondOfPair.add(i, null);
final Integer leftHi = firstOfPairOrFragment.get(i).getIntegerAttribute(SAMTag.HI.name());
final Integer rightHi = secondOfPair.get(i).getIntegerAttribute(SAMTag.HI.name());
if (leftHi != null) {
if (rightHi != null) {
if (leftHi < rightHi) secondOfPair.add(i, null);
else if (rightHi < leftHi) firstOfPairOrFragment.add(i, null);
// else the are correlated
}
else if (hiTagValueOfFirst > hiTagValueOfSecond) {
firstOfPairOrFragment.add(i, null);
}
// else the are correlated
} else if (rightHi != null) {
firstOfPairOrFragment.add(i, null);
} else {
// Both alignments do not have HI, so push down the ones on the right.
// Right is arbitrarily picked to push down.
secondOfPair.add(i, null);
}

if (isNull(hiTagValueOfFirst) && nonNull(hiTagValueOfSecond)) firstOfPairOrFragment.add(i, null);

// Both alignments do not have HI, so push down the ones on the right.
// Right is arbitrarily picked to push down.
if (isNull(hiTagValueOfFirst) && isNull(hiTagValueOfSecond)) secondOfPair.add(i, null);
}

renumberHitIndex();
}

/**
Expand All @@ -237,12 +229,12 @@ private void renumberHitIndex() {
for (int i = 0; i < numHits(); ++i) {
final SAMRecord first = getFirstOfPair(i);
final SAMRecord second = getSecondOfPair(i);
if (nonNull(first) && nonNull(second)) {
if (first != null && second != null) {
first.setAttribute(SAMTag.HI.name(), i);
second.setAttribute(SAMTag.HI.name(), i);
}
if (nonNull(first) && isNull(second)) first.setAttribute(SAMTag.HI.name(), null);
if (isNull(first) && nonNull(second)) second.setAttribute(SAMTag.HI.name(), null);
if (first != null && second == null) first.setAttribute(SAMTag.HI.name(), null);
if (first == null && second != null) second.setAttribute(SAMTag.HI.name(), null);
}
}

Expand Down Expand Up @@ -270,7 +262,7 @@ void coordinateByMate() {
}

for (int i = 0; i < secondOfPair.size(); i++) {
if (nonNull(secondOfPair.get(i))) {
if (secondOfPair.get(i) != null) {
newFirstOfPairOrFragment.add(i, null);
newSecondOfPair.add(i, secondOfPair.get(i));
}
Expand All @@ -288,7 +280,7 @@ void coordinateByMate() {
* @return true if records belong to the same pairwise alignment
*/
private static boolean arePair(final SAMRecord first, final SAMRecord second) {
return nonNull(first) && nonNull(second)
return first != null && second != null
&& first.getMateAlignmentStart() == second.getAlignmentStart()
&& first.getAlignmentStart() == second.getMateAlignmentStart()
&& Objects.equals(first.getReferenceName(), second.getMateReferenceName())
Expand All @@ -312,8 +304,8 @@ private NumPrimaryAlignmentState tallyPrimaryAlignments(final List<SAMRecord> re
}

public NumPrimaryAlignmentState tallyPrimaryAlignments(final boolean firstEnd) {
if (firstEnd) return tallyPrimaryAlignments(firstOfPairOrFragment);
else return tallyPrimaryAlignments(secondOfPair);
final List<SAMRecord> records = firstEnd ? firstOfPairOrFragment : secondOfPair;
return tallyPrimaryAlignments(records);
}

List<SAMRecord> getSupplementalFirstOfPairOrFragment() {
Expand Down
7 changes: 2 additions & 5 deletions src/test/java/picard/sam/MergeBamAlignmentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@
import java.lang.reflect.Field;
import java.util.*;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

/**
* Test for the MergeBamAlignment class
*
Expand Down Expand Up @@ -1985,7 +1982,7 @@ public void testCoordinateByMateMethodWorksProperly () throws IOException {
final SAMRecord firstOfPair = hitsForInsert.getFirstOfPair(i);
final SAMRecord secondOfPair = hitsForInsert.getSecondOfPair(i);

if (nonNull(secondOfPair) && nonNull(firstOfPair)){
if (secondOfPair != null && firstOfPair != null){

leftMateAlignmentStart = firstOfPair.getMateAlignmentStart();
rightMateAlignmentStart = secondOfPair.getMateAlignmentStart();
Expand All @@ -1997,7 +1994,7 @@ public void testCoordinateByMateMethodWorksProperly () throws IOException {
Assert.assertEquals(rightMateAlignmentStart, leftAlignmentStart);


} else if (isNull(secondOfPair)){
} else if (secondOfPair == null){
leftMateAlignmentStart = firstOfPair.getMateAlignmentStart();
Assert.assertEquals(leftMateAlignmentStart, 0);

Expand Down

0 comments on commit 96bc12a

Please sign in to comment.