package test;
import kmer.ProteinKmerBitFeatureVector;
import junit.framework.TestCase;
public class ProteinKmerBitFeatureVectorTest extends TestCase {
/** Test for {@link ProteinKmerBitFeatureVectorTest#getIndexForSequence(String)}. */
public void testGetIndexForSequence() {
ProteinKmerBitFeatureVector vector = new ProteinKmerBitFeatureVector(2, 20);
assertEquals(vector.getIndexForSequence("AR"), 1);
assertEquals(vector.getIndexForSequence("VV"), 399);
vector = new ProteinKmerBitFeatureVector(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 ProteinKmerBitFeatureVector(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 ProteinKmerBitFeatureVectorTest#ProteinKmerFeatureVector(byte[])}. */
public void testConstructorCopy() {
byte[] testBytes = new byte[(int) Math.ceil(Math.pow(20, 3) / ProteinKmerBitFeatureVector.BITS_IN_BYTE)];
initializeBytes(testBytes);
testBytes[0] = (byte) 0x1;
ProteinKmerBitFeatureVector vector = new ProteinKmerBitFeatureVector(testBytes, 3, 20);
assertTrue(equalBytes(vector.getFeatureVector(), testBytes));
testBytes[0] = (byte) 0x0;
assertTrue(!equalBytes(vector.getFeatureVector(), testBytes));
}
/** Test for {@link ProteinKmerBitFeatureVectorTest#ProteinKmerFeatureVector(byte[])}. */
public void testPrintKmers() {
ProteinKmerBitFeatureVector vector = new ProteinKmerBitFeatureVector(3, 20);
vector.parseKmersFromSequence("AVAA");
System.out.println(vector.printKmers());
assertTrue(true);
}
/** Test for {@link ProteinKmerBitFeatureVectorTest#parseKmersFromSequence(String)}. */
public void testSetBitsCorrectly() {
ProteinKmerBitFeatureVector vector = new ProteinKmerBitFeatureVector(2, 20);
String sequence = "AA";
byte[] testBytes = new byte[(int) Math.ceil(Math.pow(vector.getAlphabetSize(),
vector.getKmerLength()) / ProteinKmerBitFeatureVector.BITS_IN_BYTE)];
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 ProteinKmerBitFeatureVector(2, 20);
vector.parseKmersFromSequence(sequence);
bytes = vector.getFeatureVector();
testBytes[0] = (byte) 0x2;
System.out.println(vector.printByteVector());
System.out.println(printByteVector(testBytes));
assertTrue(equalBytes(bytes, testBytes));
sequence = "AV";
vector = new ProteinKmerBitFeatureVector(2, 20);
vector.parseKmersFromSequence(sequence);
bytes = vector.getFeatureVector();
testBytes[0] = (byte) 0x0;
testBytes[2] = (byte) 0x8;
System.out.println(vector.printByteVector());
System.out.println(printByteVector(testBytes));
assertTrue(equalBytes(bytes, testBytes));
sequence = "VV";
vector = new ProteinKmerBitFeatureVector(2, 20);
vector.parseKmersFromSequence(sequence);
bytes = vector.getFeatureVector();
testBytes[0] = (byte) 0x0;
testBytes[2] = (byte) 0x0;
testBytes[49] = (byte) 0x80;
System.out.println(vector.printByteVector());
System.out.println(printByteVector(testBytes));
assertTrue(equalBytes(bytes, testBytes));
// 32, b = -65
Byte aByte = (byte) 32;
Byte bByte = (byte) -65;
assertEquals(aByte.intValue(), 32);
assertEquals(bByte.intValue(), -65);
//assertEquals(aByte.byteValue() & bByte.byteValue(), 0);
}
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();
}
}