From 484763e23945ec052ca3d385f9c7dcaa6b17825c Mon Sep 17 00:00:00 2001 From: pt7b5kpz9 <1722542098@qq.com> Date: Mon, 17 Jun 2024 19:25:58 +0800 Subject: [PATCH] ADD file via upload --- 第二学期大作业 序列比对.cpp | 334 +++++++++++++++++++++++++ 1 file changed, 334 insertions(+) create mode 100644 第二学期大作业 序列比对.cpp diff --git a/第二学期大作业 序列比对.cpp b/第二学期大作业 序列比对.cpp new file mode 100644 index 0000000..6fb81bf --- /dev/null +++ b/第二学期大作业 序列比对.cpp @@ -0,0 +1,334 @@ +#include +#include +#include +#include +#include + +using namespace std; + +#define INDEL_CHAR '-' +int MATCH = 1, DIS_MATCH = -1, INDEL = -3; +char choice1, choice2; + +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; +}; + +int max3(int a, int b, int c) +{ + return max(max(a, b), c); +} + +int Compare(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) +{ + 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) + exit(1); + for (int i = 0; i <= m; i++) + { + if ((unit[i] = (unitLine *)malloc(sizeof(unitLine) * (n + 1))) == NULL) + exit(1); + for (int j = 0; j <= n; j++) + { + if ((unit[i][j] = (unitLine)malloc(sizeof(struct BacktrackingUnit))) == NULL) + 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 + Compare(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) +{ + if (!choice1 && !choice2) + { + while (true) + { + cout << "ѡ\n1.̶\n2.ı\n"; + cin >> choice1; + if (choice1 != '1' && choice1 != '2') + { + cout << "#ѡϷѡ\n"; + Sleep(1000); + } + else + break; + } + while (true) + { + cout << "ѡ\n1.ʾ\n2.Ϊı\n"; + cin >> choice2; + if (choice2 != '1' && choice2 != '2') + { + cout << "#ѡϷѡ\n"; + Sleep(1000); + } + else + break; + } + } + + if (choice1 == '1') + { + 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); + } + } + } + else + { + while (true) + { + char x; + cout << "ı 1\n"; + while (true) + { + cin >> x; + if (x == '1') + break; + } + ifstream ifile("TEST.txt"); + if (!ifile) + { + cerr << "ûҵļ뽫 TEST.txt С\n"; + continue; + } + string a, b; + ifile >> a >> b; + ifile.close(); + if (!legal_string(a) || !legal_string(b)) + { + cout << "#ϷĻУ\n"; + Sleep(1000); + } + else + break; + } + } + char c; + 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 (choice2 == '1') + { + cout << s.res1 << endl; + cout << s.res2 << endl; + } + else + { + ofstream ofile{"ANS.txt", ios::out}; + ofile << s.res1 << endl; + ofile << s.res2 << endl; + ofile.close(); + } + 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