Skip to content

Commit

Permalink
tests ok
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafdal committed May 6, 2022
1 parent 8e7c746 commit f919ae6
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 47 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ project(main VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 11)

# Imprimir informacion para debuggear
set(CTEST_OUTPUT_ON_FAILURE ON)

# From "Working with CMake" documentation:
Expand Down
26 changes: 12 additions & 14 deletions Needlemachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void printMat(Cell** mat, size_t rows, size_t cols)
}
}

void initMat(Cell** mat, size_t& rows, size_t& cols, const string &seq1, const string &seq2)
void initMat(Cell**& mat, size_t& rows, size_t& cols, const string &seq1, const string &seq2)
{
rows = seq1.length()+1; // cant filas
cols = seq2.length()+1; // cant columnas
Expand Down Expand Up @@ -86,7 +86,7 @@ void fillMat(Cell** mat, size_t rows, size_t cols, const string &seq1, const str


//TODO poner este prototipo con menos de 100 caracteres
long alignMat(Cell** mat, size_t rows, size_t cols, const string &seq1, const string &seq2, array<string,3>& output)
int32_t alignMat(Cell** mat, size_t rows, size_t cols, const string &seq1, const string &seq2, array<string,3>& output)
{
long alignScore = 0;
array<char,3> terna;
Expand Down Expand Up @@ -124,12 +124,15 @@ long alignMat(Cell** mat, size_t rows, size_t cols, const string &seq1, const st
}
}

//reverse(output.begin(), output.end());
// Voltear las cadenas
reverse(output[0].begin(), output[0].end());
reverse(output[1].begin(), output[1].end());
reverse(output[2].begin(), output[2].end());

return alignScore;
}

long getGlobalAlignment(const string &seq1, const string &seq2, array<string,3> &alignment)
int32_t getGlobalAlignment(const string &seq1, const string &seq2, array<string,3> &alignment)
{
Cell** mat = NULL;
size_t rows = 0;
Expand All @@ -144,17 +147,12 @@ long getGlobalAlignment(const string &seq1, const string &seq2, array<string,3>
// (3) Alinear
alignMat(mat, rows, cols, seq1, seq2, alignment);

cout << "len: " << alignment.size() << endl;
// cout << "len: " << alignment.size() << endl;
// cout << alignment[0].data() << endl;
// cout << alignment[1].data() << endl;
// cout << alignment[2].data() << endl;

reverse(alignment[0].begin(), alignment[0].end());
reverse(alignment[1].begin(), alignment[1].end());
reverse(alignment[2].begin(), alignment[2].end());

cout << alignment[0].data() << endl;
cout << alignment[1].data() << endl;
cout << alignment[2].data() << endl;

long bestAlignment = mat[rows - 1][cols - 1].score;
int32_t bestAlignment = mat[rows - 1][cols - 1].score;

// Liberar memoria
for(size_t i=0; i < rows; i++)
Expand Down
6 changes: 3 additions & 3 deletions Needlemachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ typedef union
};
} Cell;

void initMat(Cell **mat, size_t &rows, size_t &cols, const std::string &seq1,
void initMat(Cell**& mat, size_t &rows, size_t &cols, const std::string &seq1,
const std::string &seq2);
void fillMat(Cell **mat, size_t rows, size_t cols, const std::string &seq1,
const std::string &seq2);
long alignMat(Cell **mat, size_t rows, size_t cols, const std::string &seq1,
int32_t alignMat(Cell **mat, size_t rows, size_t cols, const std::string &seq1,
const std::string &seq2, std::array<std::string, 3> &output);

long getGlobalAlignment(const std::string &seq1, const std::string &seq2,
int32_t getGlobalAlignment(const std::string &seq1, const std::string &seq2,
std::array<std::string, 3> &alignment);

#endif
137 changes: 107 additions & 30 deletions main_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,136 @@ using namespace std;

#include "Needlemachine.h"
#include <string>
#include <array>

using namespace std;

bool testMatInit(string& s1, string& s2, size_t cols, size_t rows, int32_t** expectedScore)
bool testMatInit(Cell **&mat, size_t &cols, size_t &rows, string &s1, string &s2,
size_t expectedCols, size_t expectedRows, int32_t (*expectedScoreMat)[8])
{
Cell** mat = NULL;

if((s1.size() + 1) != rows || (s2.size() + 1) != cols)
bool output = true;

initMat(mat, rows, cols, s1, s2);

if (rows != expectedRows || cols != expectedCols)
{
cout << "Mat size init error!\n";
return false;
output = false;
}
size_t cols=0, rows=0;
initMat(mat, rows, cols, s1, s2);

for(size_t i=0; i < rows; i++)
for (size_t i = 0; i < rows; i++)
{
if(mat[i][0].score != expectedScore[i][0])
if (mat[i][0].score != expectedScoreMat[i][0])
{
cout << "score mismatch at element [" << i << "][0]\n";
return false;
cout << "initial score mismatch at element [" << i << "][0]\n";
cout << "score: " << mat[i][0].score << endl;
output = false;
}
}

for(size_t i=0; i < cols; i++)
for (size_t i = 0; i < cols; i++)
{
if(mat[0][i].score != expectedScore[0][i|])
if (mat[0][i].score != expectedScoreMat[0][i])
{
cout << "score mismatch at element [0][" << i << "]\n";
return false;
cout << "initial score mismatch at element [0][" << i << "]\n";
cout << "score: " << mat[0][i].score << endl;
output = false;
}
}

return output;
}

int main(int nargs, char** argv)
bool testFillMat(Cell **mat, size_t cols, size_t rows, string &s1, string &s2,
int32_t (*expectedScoreMat)[8])
{
size_t cols = 8;
size_t rows = 8;
int32_t matFinalScore[8][8] = {
{ 0, -1, -2, -3, -4, -5, -6, -7},
{-1, -1, 0, -1, -2, -3, -4, -5},
{-2, -2, -1, 1, 0, -1, -2, -3},
{-3, -3, -2, 0, 2, 1, 0, 1},
{-4, -4, -3, -1, 1, 1, 0, -1},
{-5, -5, -4, -2, 0, 2, 1, 0},
{-6, -4, -5, -3, -1, 1, 3, 2},
{-7, -5, -5, -4, -2, 0, 2, 2},
};
fillMat(mat, rows, cols, s1, s2);

for (size_t i = 0; i < rows; i++)
{
for (size_t j = 0; j < cols; j++)
{
if (mat[i][j].score != expectedScoreMat[i][j])
{
cout << "fill score mismatch at element [" << i << "][" << j << "]\n";
cout << mat[i][j].score << " != " << expectedScoreMat[i][j] << endl;
return false;
}
}
}
return true;
}

bool testAlignment(Cell **mat, size_t cols, size_t rows, string &s1, string &s2,
int32_t expectedScore, array<string, 3>& expectedAlignment)
{
array<string, 3> alignment;
int32_t score = alignMat(mat, rows, cols, s1, s2, alignment);

for (uint8_t i = 0; i < 3; i++)
{
for (size_t j = 0; j < alignment.at(0).size(); j++)
{
char c1 = expectedAlignment.at(i).at(j);
char c2 = alignment.at(i).at(j);
if(c1 != c2)
{
cout << "alignment mismatch at char [" << j << "], string: " << i << endl;
cout << c1 << " != " << c2 << endl;
return false;
}
}
}
return true;
}

int main(int nargs, char **argv)
{
Cell **mat = NULL;
size_t cols = 0, rows = 0;

// Test data
string seq1 = "GATTACA";
string seq2 = "CGATACG";

// Expected output
size_t expectedCols = 8;
size_t expectedRows = 8;
int32_t expectedScoreMat[8][8] = {
{0, -1, -2, -3, -4, -5, -6, -7},
{-1, -1, 0, -1, -2, -3, -4, -5},
{-2, -2, -1, 1, 0, -1, -2, -3},
{-3, -3, -2, 0, 2, 1, 0, -1},
{-4, -4, -3, -1, 1, 1, 0, -1},
{-5, -5, -4, -2, 0, 2, 1, 0},
{-6, -4, -5, -3, -1, 1, 3, 2},
{-7, -5, -5, -4, -2, 0, 2, 2},
};

int32_t expectedScore = 2;
array<string, 3> expectedAlignment = {
"-GATTACA",
" || |||*",
"CGA-TACG",
};

// Test 1
if (!testMatInit(mat, cols, rows, seq1, seq2, expectedCols, expectedRows, expectedScoreMat))
return -1;
cout << "Mat Init success!!\n";

// Test 2
if (!testFillMat(mat, cols, rows, seq1, seq2, expectedScoreMat))
return -1;
cout << "Fill Mat test success!!\n";

// Test 3
if(!testAlignment(mat, cols, rows, seq1, seq2, expectedScore, expectedAlignment))
return -1;
cout << "Alignment test success\n";

// Liberar memoria
for (size_t i = 0; i < rows; i++)
delete[] mat[i];
delete[] mat;

return 0;
}

0 comments on commit f919ae6

Please sign in to comment.