MASA-Core
Alignment.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 <vector>
00023 
00024 #include "Alignment.hpp"
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include <stdlib.h>
00028 #include <algorithm>
00029 
00030 #include "AlignmentBinaryFile.hpp"
00031 
00032 //#define USE_CAIRO
00033 
00034 Alignment::Alignment(AlignmentParams* params) : params(params) {
00035         alignmentFinalized = false;
00036 }
00037 
00038 Alignment::Alignment(const Alignment& orig) {
00039         alignmentFinalized = false;     
00040 }
00041 
00042 Alignment::~Alignment() {
00043         gap_sequences.clear();
00044 }
00045 
00046 void Alignment::addGapInSeq0(int i) {
00047     addGap(0, i);
00048 }
00049 
00050 void Alignment::addGapInSeq1(int j) {
00051     addGap(1, j);
00052 }
00053 
00054 vector<gap_sequence_t>* Alignment::getGapSequences() {
00055         if (gap_sequences.size() > 0) {
00056                 return &gap_sequences;
00057         }
00058         
00059         int i0=this->start[0];
00060         int j0=this->start[1];
00061         int i1=this->end[0];
00062         int j1=this->end[1];
00063 
00064         int dir_i = (i1 > i0) ? +1 : -1;
00065         int dir_j = (j1 > j0) ? +1 : -1;
00066         
00067         int c0 = (dir_i > 0) ? 0 : gaps[0].size()-1;
00068         int c1 = (dir_j > 0) ? 0 : gaps[1].size()-1;
00069         
00070         int next_gap_i = (c0<0 || c0>=gaps[0].size()) ? -1 : gaps[0][c0].pos;
00071         int next_gap_j = (c1<0 || c1>=gaps[1].size()) ? -1 : gaps[1][c0].pos;//FIXME c0??? não deveria ser c1??
00072         
00073         gap_sequences.resize(gaps[0].size() + gaps[1].size());
00074         int k=0;
00075         int i=i0;
00076         int j=j0;
00077         while (next_gap_i != -1 || next_gap_j != -1) {
00078                 // Calcula a distancia do ponto atual até o proxy gap do tipo 0 e do tipo 1
00079                 int diff_i = next_gap_i-i;
00080                 int diff_j = next_gap_j-j;
00081 
00082                 /*printf("(%d %d) (%d %d) = (%d %d) (%d:%d %d:%d)\n", 
00083                            i, j, next_gap_i, next_gap_j, diff_i, diff_j, c0, gaps[0][c0].len, c1, gaps[1][c1].len);*/
00084                 gap_sequence_t gap_sequence;
00085                 
00086                 // Processa o gap mais proximo
00087                 if (abs(diff_j) < abs(diff_i)) {
00088                         i += diff_j;
00089                         j = next_gap_j;
00090                         gap_sequences[k].i0 = i;
00091                         gap_sequences[k].j0 = j;
00092                         i += gaps[1][c1+=dir_j].len;
00093                 } else {
00094                         i = next_gap_i;
00095                         j += diff_i;
00096                         gap_sequences[k].i0 = i;
00097                         gap_sequences[k].j0 = j;
00098                         j += gaps[0][c0+=dir_i].len;
00099                 }
00100                 gap_sequences[k].i1 = i;
00101                 gap_sequences[k].j1 = j;
00102                 k++;
00103 
00104                 next_gap_i = (c0<0 || c0>=gaps[0].size()) ? -1 : gaps[0][c0].pos;
00105                 next_gap_j = (c1<0 || c1>=gaps[1].size()) ? -1 : gaps[1][c0].pos;
00106         }
00107         printf("%d/%d: (%d %d) (%d %d)\n", k, gaps[0].size() + gaps[1].size(), i, j, end[0], end[1]);
00108         printf("count: %d\n", gap_sequences.size());    
00109         
00110         return &gap_sequences;
00111 }
00112 
00113 bool myfunction (gap_t a,gap_t b) { return (a.pos<b.pos); }
00114 
00115 void Alignment::finalize(/*int i0, int j0, int i1, int j1*/) {
00116     /*this->start[0] = i0;
00117     this->start[1] = j0;
00118     this->end[0] = i1;
00119     this->end[1] = j1;*/
00120         this->alignmentFinalized = true;
00121 
00122         for (int i=0; i<params->getSequencesCount(); i++) {
00123                 sort(gaps[i].begin(), gaps[i].end(), myfunction);
00124         }
00125 }
00126 
00127 
00128 bool Alignment::isFinalized() {
00129         return alignmentFinalized;
00130 }
00131 
00132 /*void Alignment::printBinary(string filename) {
00133         AlignmentBinaryFile::write(filename, this);
00134 }
00135 
00136 void Alignment::loadBinary(string filename) {
00137         AlignmentBinaryFile::read(filename, this);
00138 }*/
00139 
00140 AlignmentParams* Alignment::getAlignmentParams() const {
00141         return params;
00142 }
00143 
00144 int Alignment::getRawScore() const {
00145         return rawScore;
00146 }
00147 
00148 void Alignment::setRawScore(int rawScore) {
00149         this->rawScore = rawScore;
00150 }
00151 
00152 void Alignment::setStart(int seq, int pos) {
00153         start[seq] = pos;
00154 }
00155 
00156 void Alignment::setEnd(int seq, int pos) {
00157         end[seq] = pos;
00158 }
00159 
00160 int Alignment::getStart(int seq) {
00161         return start[seq];
00162 }
00163 
00164 int Alignment::getEnd(int seq) {
00165         return end[seq];
00166 }
00167 
00168 string Alignment::getPruningFile() const {
00169         return pruningFile;
00170 }
00171 
00172 void Alignment::setPruningFile(string pruningFile) {
00173         this->pruningFile = pruningFile;
00174 }
00175 
00176 int Alignment::getGapExtensions() const {
00177         return gapExtensions;
00178 }
00179 
00180 void Alignment::setGapExtensions(int gapExtensions) {
00181         this->gapExtensions = gapExtensions;
00182 }
00183 
00184 int Alignment::getGapOpen() const {
00185         return gapOpen;
00186 }
00187 
00188 void Alignment::setGapOpen(int gapOpen) {
00189         this->gapOpen = gapOpen;
00190 }
00191 
00192 int Alignment::getMatches() const {
00193         return matches;
00194 }
00195 
00196 void Alignment::setMatches(int matches) {
00197         this->matches = matches;
00198 }
00199 
00200 int Alignment::getMismatches() const {
00201         return mismatches;
00202 }
00203 
00204 void Alignment::setMismatches(int mismatches) {
00205         this->mismatches = mismatches;
00206 }
00207 
00208 void Alignment::addGap(int seq, int i) {
00209     if (gaps[seq].size() != 0 && gaps[seq].back().pos == i) {
00210         gaps[seq].back().len++;
00211         //printf("-seq: %d  i: %d  (%d)   [%d]\n", seq, i, gaps[seq].back().len, gaps[seq].size());
00212     } else {
00213         gap_t new_gap(i, 1);
00214         new_gap.pos = i;
00215         new_gap.len = 1;
00216         gaps[seq].push_back(new_gap);
00217         //printf("+seq: %d  i: %d  (%d)   [%d]\n", seq, i, gaps[seq].back().len, gaps[seq].size());
00218     }
00219 
00220     /*int* c = &gaps[seq][0];
00221     int* s = &gaps[seq][(*c)*2+1];
00222     int* e = &gaps[seq][(*c)*2+2];
00223 
00224     //printf("seq: %d  i: %d  [%d/%d]\n", seq, i, *s, *e);
00225     if (*c>0 && i == *s) {
00226         (*e)++;
00227     } else {
00228         (*c)++;
00229         gaps[seq][(*c)*2+1] = i;
00230         gaps[seq][(*c)*2+2] = 1;
00231     } */
00232 }