#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <math.h>
#define MAX 128
#define SIZE 5
int charGroup[128];
int** similarity;
char* letters;
int gap;
int insDel;
struct Index {
int indexA;
int indexB;
int length;
};
bool Max(std::string a, std::string b){
return a.length() > b.length();
}
void Swap(std::string& a, std::string& b){
std::string buf = a;
a = b;
b = buf;
}
void SpaceErase(std::string &str) {
for(int i = 0; i < str.length(); i++) {
if(str[i] == ' ') {
str.erase(i,1);
i--;
}
}
}
void DefAlphabet(std::istream& in){
std::string buf;
//Считываем штраф за геп, инсерцию-делецию
std::getline(in, buf, ';');
gap = atoi(buf.c_str());
std::getline(in, buf);
std::getline(in, buf, ';');
insDel = atoi(buf.c_str());
std::getline(in, buf); //Проопускаем лишние строки
std::getline(in, buf);
std::getline(in, buf);
//Распределаяем буквы алфавита по "факторгруппам",
//Далее по номеру группы будет происходить обращение к таблице
//для выяснения штрафа
std::string alph;
std::getline(in, alph);
letters = new char[alph.length()/2];
int lettersNumber = 0;
for (int i = 0; i < alph.length(); i++) //
if (alph[i] != ' '){
letters[lettersNumber] = alph[i];
charGroup[alph[i]] = lettersNumber;
lettersNumber++;
}
//Выделяем память для матрицы с очками
// в зависимости от длины алфавита
similarity = new int*[alph.length()];
for (int i = 0; i < alph.length(); i++){
similarity[i] = new int[alph.length()];
}
std::getline(in, buf);
std::getline(in, buf);
//Считываем матрицу
for(int i = 0; i < lettersNumber; i++){
for(int j = 0; j < lettersNumber; j++){
if (j != lettersNumber - 1)
std::getline(in, buf, ' ');
else
std::getline(in, buf);
similarity[i][j] = atoi(buf.c_str());
std::cout << similarity[i][j] << " ";
}
std::cout << std::endl;
}
}
void GetLines(std::istream& fs, std::string& a, std::string& b){
std::string seq1 = "", seq2 = "";
std::string name1, name2;
int seq_num = 0;
for (std::string tek_line; std::getline(fs, tek_line); ) {
if (tek_line[0] == '>') {
(seq_num) ? name2 = tek_line : name1 = tek_line;
seq_num++;
continue;
}
SpaceErase(tek_line);
(seq_num > 1) ? seq2 += tek_line : seq1 += tek_line;
}
a = seq1;
b = seq2;
}
//Ищем наибольшую общую подстроку
Index GetLargestCommonSubstring(const std::string& a,
const std::string& b) {
const int a_size = a.size();
const int b_size = b.size();
typedef std::vector<int> solution;
const int solution_size = b_size + 1;
std::vector<int> x(solution_size, 0), y(solution_size);
std::vector<int> * previous = &x;
std::vector<int> * current = &y;
int max_length = 0;
int result_index = 0;
int result_index_b = 0;
for(int i = a_size - 1; i >= 0; i--) {
for(int j = b_size - 1; j >= 0; j--) {
int & current_match = (*current)[j];
if(a[i] != b[j]) {
current_match = 0;
}
else {
const int length = 1 + (*previous)[j + 1];
if (length > max_length) {
max_length = length;
result_index = i;
result_index_b = j;
}
current_match = length;
}
}
swap(previous, current);
}
Index result;
result.indexA = result_index;
result.indexB = result_index_b;
result.length = max_length;
return result;
}
int GetScore(std::string a, std::string b){
int result = 0;
for (int i = 0; i < a.length(); i++){
if (a[i] == '-' || b[i] == '-'){
result += gap;
} else {
result += similarity[charGroup[a[i]]][charGroup[b[i]]];
}
}
return result;
}
//Выравниваем просто по наибольшей общей подстроке :(((
void Align(std::string a, std::string b){
std::string newA, newB;
Index result = GetLargestCommonSubstring(a, b);
std::cout << result.indexA << " " << result.indexB << std::endl;
if (result.indexA >= result.indexB){
for (int i = 0; i < result.indexA - result.indexB; i++){
newB.append("-");
}
newB.append(b);
newA.append(a);
for (int i = a.length(); i < a.length() + result.indexA - result.indexB; i++){
newA.append("-");
}
} else {
for (int i = 0; i < result.indexB - result.indexA; i++){
std::cout << result.indexB - result.indexA << std::endl;
newA.append("-");
}
newA.append(a);
newB.append(b);
std::cout << newB.length() << std::endl;
for (int i = b.length(); i < b.length() + result.indexB - result.indexA + abs(b.length() - a.length()); i++){
newB.append("-");
}
}
std::cout << newA
<< std::endl
<< newB
<< std::endl
<< "New Score "
<< GetScore(newA, newB)
<< std::endl;
}
int main(int argc, char** argv){
if (argc < 3){
std::cout << "type input files" << std::endl;
return 0;
}
//Через аргументы коммандной строки передаются фходные файлы
//Сначала файл с алфавитом, затем файл сo строками
std::ifstream finAlph(argv[1]);
std::ifstream fin(argv[2]);
std::string a, b, buf;
DefAlphabet(finAlph);
GetLines(fin, a, b);
/*std::getline(fin, buf);
std::getline(fin, a);
std::getline(fin, buf);
std::getline(fin, buf);
std::getline(fin, b);*/
//Swap(a, b);
Align(a, b);
std::cout << "Old Score " << GetScore(a,b) << std::endl;
return 0;
}