다음은 표현을 처리하기위한 빠르고 쉬운 코드입니다.
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 등으로 이동하십시오.
http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm을 읽으십시오. –