Initial Commit
This commit is contained in:
0
java/nucleotide-count/src/main/java/.keep
Normal file
0
java/nucleotide-count/src/main/java/.keep
Normal file
50
java/nucleotide-count/src/main/java/DNA.java
Normal file
50
java/nucleotide-count/src/main/java/DNA.java
Normal file
@@ -0,0 +1,50 @@
|
||||
import java.util.*;
|
||||
import java.lang.String;
|
||||
|
||||
|
||||
/**
|
||||
* DNA holds a string of nucleotides and allows
|
||||
* @param nucleos The nucleotide string
|
||||
* anything that isn't ACG or T is ignored
|
||||
* by all functions in the class.
|
||||
*/
|
||||
public class DNA {
|
||||
private String nucleotides = "";
|
||||
public DNA(String nucleos) {
|
||||
this.nucleotides = nucleos;
|
||||
}
|
||||
|
||||
/**
|
||||
* nucleotideCounts returns a Map of how many there are of each
|
||||
* nucleotide in the string.
|
||||
* @return The Map from nucleotide -> count
|
||||
*/
|
||||
public Map<Character, Integer> nucleotideCounts() {
|
||||
Map<Character, Integer> ret = new HashMap<Character, Integer>();
|
||||
ret.put('A', this.count('A'));
|
||||
ret.put('C', this.count('C'));
|
||||
ret.put('G', this.count('G'));
|
||||
ret.put('T', this.count('T'));
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts how many instances of a specific nucleotide exist
|
||||
* in the string.
|
||||
* @param which The nucleotide that we're counting
|
||||
* @return How many of that nucleotide there were.
|
||||
*/
|
||||
public int count(char which) {
|
||||
if(which != 'A' && which != 'C' && which != 'G' && which != 'T') {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
char[] nucChar = this.nucleotides.toCharArray();
|
||||
int ret = 0;
|
||||
for(int i = 0; i < nucChar.length; i++) {
|
||||
if(nucChar[i] == which) {
|
||||
ret++;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
0
java/nucleotide-count/src/test/java/.keep
Normal file
0
java/nucleotide-count/src/test/java/.keep
Normal file
83
java/nucleotide-count/src/test/java/NucleotideTest.java
Normal file
83
java/nucleotide-count/src/test/java/NucleotideTest.java
Normal file
@@ -0,0 +1,83 @@
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
import static org.fest.assertions.api.Assertions.entry;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class NucleotideTest {
|
||||
@Test
|
||||
public void testEmptyDnaStringHasNoAdenosine() {
|
||||
DNA dna = new DNA("");
|
||||
assertThat(dna.count('A')).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyDnaStringHasNoNucleotides() {
|
||||
DNA dna = new DNA("");
|
||||
assertThat(dna.nucleotideCounts()).hasSize(4).contains(
|
||||
entry('A', 0),
|
||||
entry('C', 0),
|
||||
entry('G', 0),
|
||||
entry('T', 0)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRepetitiveCytidineGetsCounted() {
|
||||
DNA dna = new DNA("CCCCC");
|
||||
assertThat(dna.count('C')).isEqualTo(5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRepetitiveSequenceWithOnlyGuanosine() {
|
||||
DNA dna = new DNA("GGGGGGGG");
|
||||
assertThat(dna.nucleotideCounts()).hasSize(4).contains(
|
||||
entry('A', 0),
|
||||
entry('C', 0),
|
||||
entry('G', 8),
|
||||
entry('T', 0)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCountsOnlyThymidine() {
|
||||
DNA dna = new DNA("GGGGGTAACCCGG");
|
||||
assertThat(dna.count('T')).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCountsANucleotideOnlyOnce() {
|
||||
DNA dna = new DNA("CGATTGGG");
|
||||
dna.count('T');
|
||||
assertThat(dna.count('T')).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDnaCountsDoNotChangeAfterCountingAdenosine() {
|
||||
DNA dna = new DNA("GATTACA");
|
||||
dna.count('A');
|
||||
assertThat(dna.nucleotideCounts()).hasSize(4).contains(
|
||||
entry('A', 3),
|
||||
entry('C', 1),
|
||||
entry('G', 1),
|
||||
entry('T', 2)
|
||||
);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testValidatesNucleotides() {
|
||||
DNA dna = new DNA("GACT");
|
||||
dna.count('X');
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCountsAllNucleotides() {
|
||||
String s = "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC";
|
||||
DNA dna = new DNA(s);
|
||||
assertThat(dna.nucleotideCounts()).hasSize(4).contains(
|
||||
entry('A', 20),
|
||||
entry('C', 12),
|
||||
entry('G', 17),
|
||||
entry('T', 21)
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user