package test;
import junit.framework.TestCase;
import kmer.ProteinKmerCountFeatureVector;
public class ProteinKmerCountFeatureVectorTest extends TestCase {
/** Test for {@link ProteinKmerCountFeatureVectorTest#getIndexForSequence(String)}. */
public void testGetIndexForSequence() {
ProteinKmerCountFeatureVector vector = new ProteinKmerCountFeatureVector(2, 20);
assertEquals(vector.getIndexForSequence("AR"), 1);
assertEquals(vector.getIndexForSequence("VV"), 399);
vector = new ProteinKmerCountFeatureVector(3, 20);
assertEquals(vector.getIndexForSequence("AAA"), 0);
assertEquals(vector.getIndexForSequence("AAR"), 1);
assertEquals(vector.getIndexForSequence("AAD"), 3);
assertEquals(vector.getIndexForSequence("RAA"), 400);
assertEquals(vector.getIndexForSequence("RAR"), 401);
assertEquals(vector.getIndexForSequence("VVV"), 7999);
vector = new ProteinKmerCountFeatureVector(4, 20);
assertEquals(vector.getIndexForSequence("AAAA"), 0);
assertEquals(vector.getIndexForSequence("AAAA"), 0);
assertEquals(vector.getIndexForSequence("EQHI"), 42569);
assertEquals(vector.getIndexForSequence("VVVV"), 159999);
}
/** Test for {@link ProteinKmerCountFeatureVectorTest#ProteinKmerFeatureVector(byte[])}. */
public void testConstructorCopy() {
byte[] testBytes = new byte[(int)Math.pow(20, 3)];
initializeBytes(testBytes);
testBytes[0] = (byte) 0x1;
ProteinKmerCountFeatureVector vector = new ProteinKmerCountFeatureVector(testBytes, 3, 20);
assertTrue(equalBytes(vector.getFeatureVector(), testBytes));
testBytes[0] = (byte) 0x0;
assertTrue(!equalBytes(vector.getFeatureVector(), testBytes));
}
/** Test for {@link ProteinKmerCountFeatureVectorTest#ProteinKmerFeatureVector(byte[])}. */
public void testPrintKmers() {
ProteinKmerCountFeatureVector vector = new ProteinKmerCountFeatureVector(3, 20);
vector.parseKmersFromSequence("AVAA");
System.out.println(vector.printKmers());
assertTrue(true);
}
/** Test for {@link ProteinKmerCountFeatureVectorTest#parseKmersFromSequence(String)}. */
public void testSetBitsCorrectly() {
ProteinKmerCountFeatureVector vector = new ProteinKmerCountFeatureVector(2, 20);
String sequence = "AA";
byte[] testBytes = new byte[(int) Math.pow(vector.getAlphabetSize(),
vector.getKmerLength())];
initializeBytes(testBytes);
testBytes[0] = (byte) 0x1;
vector.parseKmersFromSequence(sequence);
byte[] bytes = vector.getFeatureVector();
System.out.println(vector.printByteVector());
System.out.println(printByteVector(testBytes));
assertTrue(equalBytes(bytes, testBytes));
sequence = "AR";
vector = new ProteinKmerCountFeatureVector(2, 20);
vector.parseKmersFromSequence(sequence);
bytes = vector.getFeatureVector();
testBytes[0] = (byte) 0x0;
testBytes[1] = (byte) 0x1;
System.out.println(vector.printByteVector());
System.out.println(printByteVector(testBytes));
assertTrue(equalBytes(bytes, testBytes));
sequence = "AV";
vector = new ProteinKmerCountFeatureVector(2, 20);
vector.parseKmersFromSequence(sequence);
bytes = vector.getFeatureVector();
testBytes[1] = (byte) 0x0;
testBytes[19] = (byte) 0x1;
System.out.println(vector.printByteVector());
System.out.println(printByteVector(testBytes));
assertTrue(equalBytes(bytes, testBytes));
sequence = "VV";
vector = new ProteinKmerCountFeatureVector(2, 20);
vector.parseKmersFromSequence(sequence);
bytes = vector.getFeatureVector();
testBytes[19] = (byte) 0x0;
testBytes[399] = (byte) 0x1;
System.out.println(vector.printByteVector());
System.out.println(printByteVector(testBytes));
assertTrue(equalBytes(bytes, testBytes));
}
/** Test for {@link ProteinKmerCountFeatureVectorTest#getIndexForSequence(String)}. */
public void testincrementCountAtPosition() {
ProteinKmerCountFeatureVector vector = new ProteinKmerCountFeatureVector(2, 20);
String sequence = "AAAAAAAA";
vector.parseKmersFromSequence(sequence);
assertEquals(vector.getFeatureVector()[0], 7);
vector = new ProteinKmerCountFeatureVector(3, 20);
sequence = "ARARARARAR";
vector.parseKmersFromSequence(sequence);
assertEquals(vector.getFeatureVector()[vector.getIndexForSequence("ARA")],
vector.getFeatureVector()[vector.getIndexForSequence("RAR")]);
}
private void initializeBytes(byte[] bytes) {
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) 0x0;
}
}
private boolean equalBytes(byte[] byte1, byte[] byte2) {
if (byte1.length != byte2.length)
return false;
for (int i = 0; i < byte1.length; i++) {
if (byte1[i] != byte2[i])
return false;
}
return true;
}
public String printByteVector(byte[] featureVector) {
StringBuilder builder = new StringBuilder("");
for (int i = 0; i < featureVector.length; i++) {
builder.append((int) featureVector[i] + " ");
}
return builder.toString();
}
}