diff --git a/基因的双序列对比算法(needleman-wunsch)实现.cpp b/基因的双序列对比算法(needleman-wunsch)实现.cpp new file mode 100644 index 0000000..cd1145c --- /dev/null +++ b/基因的双序列对比算法(needleman-wunsch)实现.cpp @@ -0,0 +1,282 @@ +#include +#include +#include +#include +#include + +using namespace std; + +#define INDEL_CHAR '-' +int MATCH = 1, DIS_MATCH = -1, INDEL = -3; +char choice; + +class ResUnit //һ˫бȶԺĽ +{ + public: + string str1; //ԭʼ1 + string str2; //ԭʼ2 + string res1; //1 + string res2; //2 + int score; //ܵ÷֣ӳеƳ̶ + int tag; //ֹ +}; + +class SingleSeq //һбϺ +{ + public: + string str; //һеԭʼ + string res; //һбϺ +}; + +struct BacktrackingUnit { + int goUp; //Ƿϻ + int goLeftUp; //Ƿϻ + int goLeft; //Ƿ + int score; //÷־(i, j)Ԫķֵ +}; + +typedef struct BacktrackingUnit *unitLine; + +struct SequenceUnit { + string *str1; //ƥ1 + string *str2; //ƥ2 + int score; +}; + +/* +Ƚ·֮˭ +f(i-1,j-1),f(i-1,j)+indel,f(i,j-1)+indel +*/ + +int max3(int a, int b, int c) +{ + return max(max(a, b), c); +} + +/* +Ƚַʲômatchdismatchindel +*/ + +int myCompare(char a, char b) +{ + if (a == b) + return MATCH; + else if (a == ' ' || b == ' ') + return INDEL; + return DIS_MATCH; +} + +ResUnit traceback(unitLine **item, const int i, const int j, string str1, + + string str2, string res1, string res2, int n, ResUnit resUnit) + + +{ + unitLine temp = item[i][j]; + if (resUnit.tag != 1) { + if (!(i || j)) { // Ԫ(0, 0)ʼַÿַȶԵ + resUnit.str1 = str1; + resUnit.str2 = str2; + resUnit.res1 = res1; + resUnit.res2 = res2; + resUnit.tag = 1; + return resUnit; + } + if (temp->goUp) { // ϻһ + res1 = str1[i - 1] + res1; + res2 = INDEL_CHAR + res2; + resUnit = traceback(item, i - 1, j, str1, str2, res1, res2, n + 1, resUnit); + } + if (temp->goLeftUp) { // ϻһ + res1 = str1[i - 1] + res1; + res2 = str2[j - 1] + res2; + resUnit = traceback(item, i - 1, j - 1, str1, str2, res1, res2, n + 1, resUnit); + } + if (temp->goLeft) { // һ + res1 = INDEL_CHAR + res1; + res2 = str2[j - 1] + res2; + resUnit = traceback(item, i, j - 1, str1, str2, res1, res2, n + 1, resUnit); + } + return resUnit; + } + return resUnit; +} + +ResUnit NeedlemanWunch(string str1, string str2) +{ + //ַstr1,str2 + const int m = str1.length(); + const int n = str2.length(); + int m1, m2, m3, mm; + unitLine **unit; + + // ʼ + if ((unit = (unitLine **)malloc(sizeof(unitLine *) * (m + 1))) == NULL) { + fputs("Error: Out of space!\n", stderr); + exit(1); + } + + for (int i = 0; i <= m; i++) { + if ((unit[i] = (unitLine *)malloc(sizeof(unitLine) * (n + 1))) == NULL) { + fputs("Error: Out of space!\n", stderr); + exit(1); + } + for (int j = 0; j <= n; j++) { + if ((unit[i][j] = (unitLine)malloc(sizeof(struct BacktrackingUnit))) == NULL) { + fputs("Error: Out of space!\n", stderr); + exit(1); + } + unit[i][j]->goUp = 0; + unit[i][j]->goLeftUp = 0; + unit[i][j]->goLeft = 0; + } + } + unit[0][0]->score = 0; + + for (int i = 1; i <= m; i++) { + unit[i][0]->score = INDEL * i; + unit[i][0]->goUp = 1; + } + + for (int j = 1; j <= n; j++) { + unit[0][j]->score = INDEL * j; + unit[0][j]->goLeft = 1; + } + + // ̬滮㷨÷־ÿԪķֵ + for (int i = 1; i <= m; i++) { + for (int j = 1; j <= n; j++) { + m1 = unit[i - 1][j]->score + INDEL; + m2 = unit[i - 1][j - 1]->score + myCompare(str1[i - 1], str2[j - 1]); + m3 = unit[i][j - 1]->score + INDEL; + mm = max3(m1, m2, m3); + unit[i][j]->score = mm; + //ж·Դ + if (m1 == mm) + unit[i][j]->goUp = 1; + if (m2 == mm) + unit[i][j]->goLeftUp = 1; + if (m3 == mm) + unit[i][j]->goLeft = 1; + } + } + + //ʼ + ResUnit res; + res.tag = 0; + res = traceback(unit, m, n, str1, str2, "", "", 0, res); + res.score = unit[m][n]->score; + + //ͷڴ + for (int i = 0; i <= m; i++) { + for (int j = 0; j <= n; j++) { + free(unit[i][j]); + } + free(unit[i]); + } + free(unit); + //ֵ + return res; +} + +bool legal_string(string s) +{ + for (unsigned i = 0; i < s.size(); i++) { + if (s[i] != 'A' && s[i] != 'T' && s[i] != 'C' && s[i] != 'G') + return false; + } + return true; +} + +void read(string &a, string &b) +{ + char c; + if (!choice) { + while (true) { + cout << "ѡ\n1.ʾ\n2.Ϊı\n"; + cin >> choice; + if (choice != '1' && choice != '2') { + cout << "#ѡϷѡ\n"; + Sleep(1000); + } else + break; + } + } + + while (true) { + cout << "ҪȶԵĻУ\nһ:"; + cin >> a; + if (legal_string(a)) + break; + else { + cout << "#ϷĻУ\n"; + Sleep(1000); + } + } + + while (true) { + cout << "ж:"; + cin >> b; + if (legal_string(b)) + break; + else { + cout << "#ϷĻУ\n"; + Sleep(1000); + } + } + + while (true) { + cout << "ѡȶԹ:\n1.ȱȣĬϣ\n2.\n3.û\n"; + cin >> c; + if (c != '1' && c != '2' && c != '3') { + cout << "#ѡϷѡ\n"; + Sleep(1000); + } else + break; + } + + if (c == '2') { + DIS_MATCH = -3; + INDEL = -1; + + } else if (c == '3') { + DIS_MATCH = INDEL = -1; + } +} + +int main() +{ + while (true) { + string a, b; + read(a, b); + ResUnit s = NeedlemanWunch(a, b); + if (choice == '2') { + ofstream ofile{"ANS.txt", ios::out}; + ofile << s.res1 << endl; + ofile << s.res2 << endl; + ofile.close(); + } else { + cout << s.res1 << endl; + cout << s.res2 << endl; + } + Sleep(2000); + cout << "ѡ\n1.ȶ\n2.ֹͣȶ\n"; + char c; + while (true) { + cin >> c; + if (c != '1' && c != '2') { + cout << "#ѡϷѡ\n"; + Sleep(1000); + } else + break; + } + if (c == '2') + break; + } + return 0; +} + +/* +"GGATCGA" +"GAATTCAGTTA" +*/ \ No newline at end of file