ToPS
FixedSequenceAtPosition.cpp
00001 /*
00002  *       FixedSequenceAtPosition.cpp
00003  *
00004  *       Copyright 2011 Andre Yoshiaki Kashiwabara <akashiwabara@usp.br>
00005  *                      Ígor Bonádio <ibonadio@ime.usp.br>
00006  *                      Vitor Onuchic <vitoronuchic@gmail.com>
00007  *                      Alan Mitchell Durham <aland@usp.br>
00008  *
00009  *       This program is free software; you can redistribute it and/or modify
00010  *       it under the terms of the GNU  General Public License as published by
00011  *       the Free Software Foundation; either version 3 of the License, or
00012  *       (at your option) any later version.
00013  *
00014  *       This program is distributed in the hope that it will be useful,
00015  *       but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *       GNU General Public License for more details.
00018  *
00019  *       You should have received a copy of the GNU General Public License
00020  *       along with this program; if not, write to the Free Software
00021  *       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00022  *       MA 02110-1301, USA.
00023  */
00024 
00025 #include "FixedSequenceAtPosition.hpp"
00026 #include "Symbol.hpp"
00027 namespace tops {
00028 
00029   void FixedSequenceAtPosition::initialize(int position, Sequence sequence, DiscreteIIDModelPtr distr){
00030     _position = position;
00031     _sequence =sequence;
00032     _probabilities = distr;
00033   }
00034   double FixedSequenceAtPosition::evaluate(const Sequence & s, unsigned int begin, unsigned int end) const{
00035     double result = ProbabilisticModelDecorator::evaluate(s, begin, end);
00036     int j;
00037     for(j = 0; (j < (int)_sequence.size()) && ((_position  + j) < (int)s.size()); j++)
00038       if(_sequence[j] != s[_position + j] )
00039         break;
00040     if(j != (int)_sequence.size())
00041       result += _probabilities->log_probability_of(1);
00042     else
00043       result += _probabilities ->log_probability_of(0);
00044     return result;
00045   }
00046   void FixedSequenceAtPosition::addSequence(Sequence &h) const {
00047     if ((int)_probabilities->choose()  == 1)
00048       return;
00049     for(int i = _position; ((i-_position) < (int)_sequence.size()) && (i < (int)h.size()); i++)
00050       h[i] = _sequence[i-_position];
00051   }
00052 
00053   void FixedSequenceAtPosition::initializeMatchedPositions(const Sequence &s)  {
00054     _matchSeq.resize(s.size());
00055     for(int i = 0; i < (int)s.size(); i++)
00056       {
00057         int j;
00058         for(j = 0; (j < (int)_sequence.size()) && ((_position  + j) < (int)s.size()); j++)
00059           if(_sequence[j] != s[_position + j] )
00060             break;
00061         if(j == (int)_sequence.size())
00062           _matchSeq[i] = true;
00063         else
00064           _matchSeq[i] = false;
00065       }
00066   }
00067 
00068   Sequence & FixedSequenceAtPosition::choose(Sequence & h, int size) const {
00069     ProbabilisticModelDecorator::choose(h, size);
00070     addSequence(h);
00071     return h;
00072   }
00073 
00074 
00075   Sequence & FixedSequenceAtPosition::choose(Sequence &h, int initial_phase, int size) const{
00076     ProbabilisticModelDecorator::choose(h, initial_phase, size);
00077     addSequence(h);
00078     return h;
00079   }
00080 
00081   Sequence & FixedSequenceAtPosition::choose(Sequence & h, Sequence & path,  int size) const{
00082     ProbabilisticModelDecorator::choose(h, path, size);
00083     addSequence(h);
00084     return h;
00085   }
00086 
00087   Sequence & FixedSequenceAtPosition::choose(Sequence & h, Sequence & path,  int i, int size) const{
00088     ProbabilisticModelDecorator::choose(h, path, i, size);
00089     addSequence(h);
00090     return h;
00091   }
00092 
00093   Sequence & FixedSequenceAtPosition::chooseWithHistory(Sequence & h, int i,  int size) const{
00094     ProbabilisticModelDecorator::chooseWithHistory(h, i, size);
00095     addSequence(h);
00096     return h;
00097   }
00098 
00099   Sequence & FixedSequenceAtPosition::chooseWithHistory(Sequence & h, int i,  int phase, int size) const{
00100     ProbabilisticModelDecorator::chooseWithHistory(h, i, phase, size);
00101     addSequence(h);
00102     return h;
00103   }
00104   double FixedSequenceAtPosition::prefix_sum_array_compute(int begin , int end){
00105     double r = ProbabilisticModelDecorator::prefix_sum_array_compute(begin, end);
00106     if(_matchSeq[begin])
00107       r += _probabilities->log_probability_of(1);
00108     else
00109       r+= _probabilities->log_probability_of(0);
00110     return r;
00111   }
00112   double FixedSequenceAtPosition::prefix_sum_array_compute(int begin , int end, int phase){
00113     double r = ProbabilisticModelDecorator::prefix_sum_array_compute(begin, end, phase);
00114     if(_matchSeq[begin])
00115       r += _probabilities->log_probability_of(1);
00116     else
00117       r+= _probabilities->log_probability_of(0);
00118     return r;
00119   }
00120 
00121   bool FixedSequenceAtPosition::initialize_prefix_sum_array(const Sequence & s, int phase){
00122     if(ProbabilisticModelDecorator::initialize_prefix_sum_array(s, phase))
00123       {
00124         return true;
00125       }
00126     initializeMatchedPositions(s);
00127     return true;
00128   }
00129 
00130   bool FixedSequenceAtPosition::initialize_prefix_sum_array(const Sequence & s) {
00131     if(ProbabilisticModelDecorator::initialize_prefix_sum_array(s)){
00132       return true;
00133     }
00134 
00135     initializeMatchedPositions(s);
00136     return true;
00137   }
00138   std::string FixedSequenceAtPosition::model_name () const {
00139     return "FixedSequenceAtPosition";
00140   }
00141 
00142   std::string FixedSequenceAtPosition::str() const{
00143     std::stringstream out;
00144     out << "model_name =\"" << model_name() << "\"" << std::endl;
00145     out << "position = " << _position << std::endl;
00146     out << "sequences = ";
00147     if(_sequence.size() >= 1) {
00148       out << "\"" ;
00149       out << subModel()->alphabet()->getSymbol(_sequence[0])->name();
00150       for( int i = 1; i < (int)_sequence.size(); i++){
00151         out << " " ;
00152         out << subModel()->alphabet()->getSymbol(_sequence[i])->name();
00153       }
00154       out << "\"" << std::endl;
00155     }
00156     out << "probability = ";
00157     out << exp( _probabilities->log_probability_of(1));
00158     out << std::endl;
00159 
00160     std::string modelname = ProbabilisticModelDecorator::subModelName();
00161     if(modelname.length() > 0)
00162       out << "model = " << modelname << std::endl ;
00163     else
00164       out << "model = [" << subModel()->str() << "]" << std::endl ;
00165     return out.str();
00166   }
00167 }