これは、表現を処理するためのすばやく簡単なコードです。
public static enum Nucleotide {
A,B,C,D;
}
public static int setbit(int val, int pos, boolean on) {
if (on) {
// set bit
return val | (1 << (8-pos-1));
}
else {
// unset bit
return val & ~(1 << (8-pos-1));
}
}
public static int set2bits(int val, int pos, int bits) {
// set/unset the first bit
val = setbit(val, pos, (bits & 2) > 0);
// set/unset the second bit
val = setbit(val, pos+1, (bits & 1) > 0);
return val;
}
public static int setNucleotide(int sequence, int pos, Nucleotide tide) {
// set both bits based on the ordinal position in the enum
return set2bits(sequence, pos*2, tide.ordinal());
}
public static void setNucleotide(int [] sequence, int pos, Nucleotide tide) {
// figure out which element in the array to work with
int intpos = pos/4;
// figure out which of the 4 bit pairs to work with.
int bitpos = pos%4;
sequence[intpos] = setNucleotide(sequence[intpos], bitpos, tide);
}
public static Nucleotide getNucleotide(int [] sequence, int pos) {
int intpos = pos/4;
int bitpos = pos%4;
int val = sequence[intpos];
// get the bits for the requested on, and shift them
// down into the least significant bits so we can
// convert batch to the enum.
int shift = (8-(bitpos+1)*2);
int tide = (val & (3 << shift)) >> shift;
return Nucleotide.values()[tide];
}
public static void main(String args[]) {
int sequence[] = new int[4];
setNucleotide(sequence, 4, Nucleotide.C);
System.out.println(getNucleotide(sequence, 4));
}
明らかに、多くのビットシフトが行われていますが、コメントの数が少ないことは、何が起こっているかについて意味があるはずです。
もちろん、この表現の欠点は、4つのグループで作業していることです。たとえば10ヌクレオチドが必要な場合は、シーケンスの最後の2ヌクレオチドがそうではないことがわかるように、カウントとともに別の変数をどこかに保持する必要があります。使える。
あいまいマッチングは、他に何もない場合でもブルートフォースで実行できます。Nヌクレオチドのシーケンスを取り込んでから、0から始めて、ヌクレオチド0:N-1と照合し、一致するヌクレオチドの数を確認します。次に、1:N、2:N+1などから移動します。