Skip to content

Commit

Permalink
Merge pull request #374 from xlxs4/israndomdna-refactor
Browse files Browse the repository at this point in the history
refactor: Use stream.allMatch in isRandomDNA
  • Loading branch information
VerisimilitudeX authored Apr 8, 2023
2 parents 19f3f77 + 15cc12b commit f8656df
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 44 deletions.
44 changes: 20 additions & 24 deletions src/main/java/DNAnalyzer/core/Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.PrintStream;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

import DNAnalyzer.core.DNAAnalysis.BasePairIndex;
import DNAnalyzer.data.aminoAcid.AminoAcid;
Expand Down Expand Up @@ -129,30 +130,25 @@ public static void printNucleotideCount(final String dna, PrintStream out) {
*/
public static boolean isRandomDNA(final String dna) {
long[] nucleotideCount = countBasePairs(dna);

// This sorts the array to get min and max value
Arrays.sort(nucleotideCount);

// Only calculate 2 Percentages, as only the highest difference (max - min) is
// relevant
final int maxPercent = nucleotidePercentage(nucleotideCount[3], dna);
final int minPercent = nucleotidePercentage(nucleotideCount[0], dna);
// If the percentage of each nucleotide is between 2% of one another, then it is
// random

return isDifferenceLessOrEqualTo2(maxPercent, minPercent);
}

/**
* Checks if the differnce between two numbers is less or equal to 2
*
* @param maxPercent one number to calculate the difference
* @param minPercent the other number to calculate the difference
* @return Whether the difference is less or equal to 2
* @category Properties
*/
public static boolean isDifferenceLessOrEqualTo2(int maxPercent, int minPercent) {
return Math.abs(maxPercent - minPercent) <= 2;
int[] percentages = Arrays.stream(nucleotideCount)
.mapToInt(c -> nucleotidePercentage(c, dna))
.toArray();

int a = percentages[BasePairIndex.ADENINE];
int t = percentages[BasePairIndex.THYMINE];
int g = percentages[BasePairIndex.GUANINE];
int c = percentages[BasePairIndex.CYTOSINE];

IntStream diffs = IntStream.of(
Math.abs(a - t),
Math.abs(a - g),
Math.abs(a - c),
Math.abs(t - g),
Math.abs(t - c),
Math.abs(g - c)
);

return diffs.allMatch(diff -> diff <= 2);
}

/**
Expand Down
20 changes: 0 additions & 20 deletions src/test/java/DNAnalyzer/core/PropertiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,4 @@ void shouldIsRandomDNAReturnTrue() {
boolean actual = Properties.isRandomDNA(dnaString);
assertTrue(actual);
}

// @Test
// void shouldIsRandomDNAReturnFalse() {
// String testEmptyString = "";
// boolean actual = Properties.isRandomDNA(testEmptyString);
// assertFalse(actual);
// }

@Test
void shouldIsDifferenceLessOrEqualTo2ReturnTrue() {
boolean actual = Properties.isDifferenceLessOrEqualTo2(10, 9);
assertTrue(actual);
}

@Test
void shouldIsDifferenceLessOrEqualTo2ReturnFalse() {
boolean actual = Properties.isDifferenceLessOrEqualTo2(10, 1);
assertFalse(actual);
}

}

0 comments on commit f8656df

Please sign in to comment.