MASA-Core
AlignerUtils.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 "AlignerUtils.hpp"
00023 
00024 #include <stdlib.h>
00025 #include <unistd.h>
00026 
00027 #define MAX2(A,B) (((A)>(B))?(A):(B))
00028 
00029 #define DEBUG (0)
00030 
00031 /**
00032  * Splits the columns in n equal blocks.
00033  *
00034  * @param pos           A vector with (blocks+1) elements.
00035  * @param j0,j1         Range of the columns
00036  * @param blocks        Number of blocks to split.
00037  */
00038 void AlignerUtils::splitBlocksEvenly(int* pos, int j0, int j1, int blocks) {
00039         int xLen = j1 - j0;
00040         int xMod = xLen % blocks;
00041         for (int i = 0; i < blocks; i++) {
00042                 pos[i] = j0 + ((xLen / blocks) * i + (i < xMod ? i : xMod));
00043         }
00044         pos[blocks] = j1;
00045 }
00046 
00047 
00048 
00049 
00050 match_result_t AlignerUtils::matchColumn(const cell_t* buffer, const cell_t* base, int len, int goalScore, int gap_open_penalty) {
00051         //printf ( "BUSOUT[%d..%d](%d) goal: %d\n", i, i+len, len, goal );
00052         //cell_t* h_busOut = &col[i];
00053         //char* my_seq0 = getSeqVertical();
00054         //char* my_seq1 = getSeqHorizontal();
00055         bool end = false;
00056         match_result_t match_result;
00057         match_result.found = false;
00058 
00059         for ( int k = 0; k < len && !end; k++ ) {
00060                 int sum_match = base[k].h + buffer[k].h;
00061                 int sum_gap = base[k].e + buffer[k].e + gap_open_penalty;
00062 
00063                 const char* result;
00064                 if  (sum_match == goalScore) {
00065                         result = "*Match";
00066                         match_result.found = true;
00067                         match_result.k = k;
00068                         match_result.score = base[k].h;
00069                         match_result.type = MATCH_ALIGNED;
00070                         end = true;
00071                 } else if (sum_gap == goalScore) {
00072                         result = "*Gap";
00073                         match_result.found = true;
00074                         match_result.k = k;
00075                         match_result.score = base[k].e;// + gap_open_penalty;
00076                         match_result.type = MATCH_GAPPED;
00077                         end = true;
00078                 } else if (sum_match>goalScore || sum_gap>goalScore) {
00079                         result = "*Error";
00080                         match_result.type = sum_match > goalScore ? MATCH_ERROR_1 : MATCH_ERROR_2;
00081                         end = true;
00082                 } else {
00083                         result = "";
00084                 }
00085 
00086                 if (DEBUG) {
00087                         //char chunk0[6] = {};
00088                         //char chunk1[6] = {};
00089                         //strncpy(chunk0, &my_seq0[MAX2(my_i-5, 0)], my_i < 5 ? my_i : 5);
00090                         //strncpy(chunk1, &my_seq1[MAX2(my_j-5, 0)], my_j < 5 ? my_j : 5);
00091                         //if (debug) printf ( "i:%8d[%4d] SW: %4d/%4d  BUS_H: (%4d/%4d)  SUM: %4d/%4d%s\n",
00092                         //printf ( "k:%4d %5s[%.1s]%.5s %5s[%.1s]%.5s SW: %4d/%4d  BUS_H: (%4d/%4d)  SUM: %4d/%4d%s\n",
00093                         //chunk0, &my_seq0[my_i], &my_seq0[my_i + 1],
00094                         //chunk1, &my_seq1[my_j], &my_seq1[my_j + 1],
00095                         printf ( "k:%4d SW: %4d/%4d  BUS_H: (%4d/%4d)  SUM: %4d/%4d%s GOAL: %d\n", k,
00096                                         buffer[k].h, buffer[k].e,
00097                                         base[k].h, base[k].e,
00098                                         sum_match, sum_gap, result, goalScore);
00099                 }
00100         }
00101         if (end && match_result.type < 0) {
00102                 fflush(stdout);
00103                 fprintf ( stderr, "ERROR: Backtrace lost (%d)! Can't continue.\n", match_result.type);
00104                 _exit ( 1 );
00105         }
00106         return match_result;
00107 }