2 개의 문자열이 주어지면 적어도 4 자의 첫 번째 일치 항목을 찾고 싶습니다.Java : 문자열 간의 일치 검색
이것은 현재 수행해야하는 코드입니다. 제대로 작동하지만 더 나은 방법이있을 수 있다고 생각합니다. 내가하는 일에 비명을 지르는 비효율적이거나 나쁜 관행이 있습니까? Apache Commons와 같은 일반적인 라이브러리가 있습니까?
Gene
클래스에 대해 걱정할 필요가 없습니다. 문제의 문자열 만 포함됩니다. 또한 GeneMatch()
은 일치하는 항목이없는 반면, 인수가있는 GeneMatch
생성자는 일치하는 항목이 있음을 나타냅니다.
Constants.MIN_MATCH
== 4,이 경우.
public static GeneMatch findMatch(Gene g0, Gene g1) {
String g0DNA = g0.getDNA();
String g1DNA = g1.getDNA();
if (g0DNA.equals("") || g1DNA.equals("")) { //there won't be a match if one is empty
return new GeneMatch();
}
int g0Left = -1;
int g0Right = -1;
int g1Left = -1;
int g1Right = -1;
String window;
for (int inx = 0; inx <= g0DNA.length() - Constants.MIN_MATCH; inx++) {
window = g0DNA.substring(inx, inx + Constants.MIN_MATCH);
if (g1DNA.indexOf(window) != -1) {
g0Left = inx;
g0Right = inx + Constants.MIN_MATCH;
g1Left = g1DNA.indexOf(window);
g1Right = g1Left + Constants.MIN_MATCH;
/* grow the match to the right
* while the two right indices are less than the lengths of their respective strings, and the
* characters at the indices match, increment each index
*/
while (g0Right < g0DNA.length() && g1Right < g1DNA.length() && g0DNA.charAt(g0Right) == g1DNA.charAt(g1Right)) {
g0Right++;
g1Right++;
}
break; //we've already found a match, no need to continue sliding the window
}
}
//now that the indices are found, convert to Genes
if (g0Left == -1 || g0Right == -1 || g1Left == -1 || g1Right == -1) { //no match found
return new GeneMatch();
}
Gene gL0 = new Gene(g0DNA.substring(0, g0Left));
Gene gL1 = new Gene(g1DNA.substring(0, g1Left));
Gene g0match = new Gene(g0DNA.substring(g0Left, g0Right));
Gene g1match = new Gene(g1DNA.substring(g1Left, g1Right));
Gene gR0 = new Gene(g0DNA.substring(g0Right));
Gene gR1 = new Gene(g1DNA.substring(g1Right));
//sanity check
assert g0DNA.equals(gL0.getDNA() + g0match.getDNA() + gR0.getDNA()) : "g0 didn't add up";
assert g1DNA.equals(gL1.getDNA() + g1match.getDNA() + gR1.getDNA()) : "g1 didn't add up";
return new GeneMatch(gL0, gR0, g0match, g1match, gL1, gR1);
}
나는 이것을 어떻게 사용할 것인지 두려워합니다. 두 시퀀스를 정렬하기 위해 설계된 즉시 사용 가능한 소프트웨어를 사용하고 싶지 않습니까? 자세한 답변을 원하시면 http://en.wikipedia.org/wiki/Sequence_alignment_software – Tim