MASA-Core
AlignerTester.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2010-2015   Edans Sandes
00004  *
00005  * This file is part of MASA-Core.
00006  * 
00007  * MASA-Core is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  * 
00012  * MASA-Core is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with MASA-Core.  If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  ******************************************************************************/
00021 
00022 #include "AlignerTester.hpp"
00023 
00024 #include "../../common/Common.hpp"
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <sys/stat.h>
00028 
00029 static score_t best_score;
00030 static FILE* lastRow;
00031 static FILE* lastColumn;
00032 
00033 static void processBestScoreFunction(score_t score, int bx, int by) {
00034         if (best_score.score < score.score) {
00035                 best_score = score;
00036                 fprintf(stderr, " (%d,%d,%d)\n", score.i, score.j, score.score);
00037         }
00038 }
00039 
00040 static void processLastRowFunction(int i, int j, int len, cell_t* data) {
00041         if (lastRow != NULL) {
00042                 fwrite(data, sizeof(cell_t), len, lastRow);
00043         }
00044 }
00045 
00046 static void processLastColumnFunction(int i, int j, int len, cell_t* data) {
00047         if (lastColumn != NULL) {
00048                 fwrite(data, sizeof(cell_t), len, lastColumn);
00049         }
00050 }
00051 
00052 
00053 AlignerTester::AlignerTester(IAligner* aligner) {
00054         this->aligner = aligner;
00055 }
00056 
00057 AlignerTester::~AlignerTester()
00058 {
00059         // TODO Auto-generated destructor stub
00060 }
00061 
00062 void AlignerTester::compare_files(const char* file0, const char* file1) {
00063         FILE* f0 = fopen(file0, "rb");
00064         if (f0 == NULL) {
00065                 fprintf(stderr, "File %s not found\n", file0);
00066                 exit(1);
00067         }
00068         FILE* f1 = fopen(file1, "rb");
00069         if (f1 == NULL) {
00070                 fprintf(stderr, "File %s not found\n", file1);
00071                 exit(1);
00072         }
00073         char c0;
00074         char c1;
00075         int bytes = 0;
00076         int equal_bytes = 0;
00077         while (fread(&c0, 1, 1, f0) == 1 && fread(&c1, 1, 1, f1) == 1) {
00078                 bytes++;
00079                 if (c0 == c1)
00080                         equal_bytes++;
00081         }
00082         if (ftell(f0) != ftell(f1)) {
00083                 fprintf(stderr, "CHECK ERROR: File size Differs\n");
00084         }
00085         if (bytes == equal_bytes) {
00086                 fprintf(stderr, "CHECK OK   : Identifical Files (%d bytes)\n", bytes);
00087         } else {
00088                 fprintf(stderr, "CHECK ERROR: Different Files (%d bytes differs)\n",
00089                                 bytes - equal_bytes);
00090         }
00091         fclose(f0);
00092         fclose(f1);
00093 }
00094 
00095 bool AlignerTester::test(char* test_filename) {
00096         FILE* file = fopen(test_filename, "rt");
00097         if (file == NULL) {
00098                 fprintf(stderr, "File %s not found\n", test_filename);
00099                 exit(1);
00100         }
00101 
00102         string output_dir = "./test_output/";
00103         mkdir(output_dir.c_str(), 0774);
00104         string lastRowName = output_dir + "last_row.bin";
00105         string lastColumnName = output_dir + "last_column.bin";
00106 
00107 
00108         char line[1024];
00109         char cmd[3][1024];
00110         int i0 = 0;
00111         int i1 = 0;
00112         int j0 = 0;
00113         int j1 = 0;
00114     Sequence* seq[2];
00115 
00116         while (fgets(line, sizeof(line), file) != NULL) {
00117                 fprintf(stderr, "%s", line);
00118                 cmd[0][0] = '\0';
00119                 cmd[1][0] = '\0';
00120                 cmd[2][0] = '\0';
00121                 sscanf(line, "%s %s %s", cmd[0], cmd[1], cmd[2]);
00122                 if (strcmp(cmd[0], "set") == 0) {
00123                         if (strcmp(cmd[1], "partition") == 0) {
00124                                 sscanf(line, "%*s %*s %d%d%d%d", &i0, &i1, &j0, &j1);
00125                                 printf("set partition %d %d %d %d\n", i0, i1, j0, j1);
00126                         } else if (strcmp(cmd[1], "sequence") == 0) {
00127                                 int id;
00128                                 char fasta_file[1024];
00129                                 sscanf(line, "%*s %*s %d %s", &id, fasta_file);
00130 
00131                         SequenceInfo* sequenceInfo = new SequenceInfo();
00132                         sequenceInfo->setFilename(fasta_file);
00133 
00134                         SequenceModifiers* modifiers = new SequenceModifiers();
00135                             modifiers->setClearN(false);
00136                             modifiers->setReverse(false);
00137                             modifiers->setComplement(false);
00138                             modifiers->setTrimStart(0);
00139                             modifiers->setTrimEnd(0);
00140 
00141                             seq[id] = new Sequence(sequenceInfo, modifiers);
00142                         }
00143                 } else if (strcmp(cmd[0], "check") == 0) {
00144                         if (strcmp(cmd[1], "score") == 0) {
00145                                 score_t check_score;
00146                                 sscanf(line, "%*s %*s %d%d%d", &check_score.i, &check_score.j, &check_score.score);
00147 
00148                                 if (check_score.score == best_score.score) {
00149                                         fprintf(stderr, "CHECK OK   : Score: %d\n", best_score.score);
00150                                 } else {
00151                                         fprintf(stderr, "CHECK ERROR: Score: %d != %d\n", check_score.score, best_score.score);
00152                                 }
00153                                 if (check_score.i == best_score.i && check_score.j == best_score.j) {
00154                                         fprintf(stderr, "CHECK OK   : Pos (%d, %d)\n", best_score.i, best_score.j);
00155                                 } else {
00156                                         fprintf(stderr, "CHECK ERROR: Pos (%d, %d) != (%d, %d)\n", check_score.i, check_score.j, best_score.i, best_score.j);
00157                                 }
00158                         } else if (strcmp(cmd[1], "column") == 0) {
00159                                 int i;
00160                                 char file[1024];
00161                                 sscanf(line, "%*s %*s %d %s", &i, file);
00162                                 compare_files(file, lastColumnName.c_str());
00163                         } else if (strcmp(cmd[1], "row") == 0) {
00164                                 int i;
00165                                 char file[1024];
00166                                 sscanf(line, "%*s %*s %d %s", &i, file);
00167                                 compare_files(file, lastRowName.c_str());
00168                         }
00169                 } else if (strcmp(cmd[0], "test") == 0) {
00170                         lastRow = fopen(lastRowName.c_str(), "wb");
00171                         if (lastRow == NULL) {
00172                                 fprintf(stderr, "Could not create file %s not found\n", lastRowName.c_str());
00173                                 exit(1);
00174                         }
00175 
00176                         lastColumn = fopen(lastColumnName.c_str(), "wb");
00177                         if (lastColumn == NULL) {
00178                                 fprintf(stderr, "Could not create file %s not found\n", lastColumnName.c_str());
00179                                 exit(1);
00180                         }
00181 
00182                         Sequence* seq_vertical = new Sequence(seq[0]);
00183                         Sequence* seq_horizontal = new Sequence(seq[1]);
00184 
00185                         const score_params_t* score_params = aligner->getScoreParameters();
00186                         AlignerManager* sw = new AlignerManager(aligner);
00187 
00188                         sw->setFirstRowSource(false);
00189                         //sw->setFirstRowSource(file);
00190                         sw->setBlockPruning(false);
00191                         //sw->setProcessLastCellFunction(processLastCellFunction);
00192 //                      sw->setProcessScoreFunction(processBestScoreFunction);
00193 //                      sw->setProcessLastRowFunction(processLastRowFunction);
00194 //                      sw->setProcessLastColumnFunction(processLastColumnFunction);
00195                         sw->setFirstColumnSource(false);
00196                         sw->setRecurrenceType(SMITH_WATERMAN);
00197                         sw->setSequences(seq_vertical, seq_horizontal);
00198                         //sw->setProcessSpecialRowFunction(processSpecialRowFunction);
00199                         aligner->initialize();
00200 
00201                         Partition partition(i0, j0, i1, j1);
00202                         best_score.score = -1;
00203                         sw->alignPartition(partition, START_TYPE_MATCH);
00204                         /*aligner->prepareIterations();
00205                         while (aligner->hasMoreIterations()) {
00206                                 fprintf(stderr, "NextIteration\n");
00207                                 aligner->processNextIteration();
00208                         }*/
00209                         fprintf(stderr, "Done\n");
00210                         fclose(lastRow);
00211                         fclose(lastColumn);
00212 
00213                 }
00214         }
00215 
00216 
00217         return true;
00218 }
00219 
00220 
00221 
00222