ToPS
ReverseComplementDNA.cpp
00001 /*
00002  *       ReverseComplementDNA.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 "ProbabilisticModelCreatorClient.hpp"
00026 #include "ConfigurationReader.hpp"
00027 #include "ReverseComplementDNA.hpp"
00028 #include "Alphabet.hpp"
00029 #include "Symbol.hpp"
00030 namespace tops {
00031 
00032   void ReverseComplementDNA::revcomp(Sequence & revCompSeq, const Sequence & s, int begin,  int end) const{
00033     AlphabetPtr a = subModel()->alphabet();
00034     if(end >= (int)s.size())
00035       end = s.size()-1;
00036     if(begin >= (int)s.size())
00037       begin = s.size()-1;
00038 
00039     for(int i = end; i >= begin; i--) {
00040       SymbolPtr symbol1 = a->getSymbol(s[i]);
00041       SymbolPtr revSymbol = revAlphabet->getSymbol(symbol1->name());
00042       revCompSeq.push_back(revSymbol->id());
00043     }
00044   }
00045 
00046   double ReverseComplementDNA::evaluate(const Sequence & s, unsigned int begin, unsigned int end) const{
00047     Sequence revCompSeq;
00048     revcomp(revCompSeq, s, (int)begin, (int)end);
00049     double result = subModel()->evaluate(revCompSeq, begin, end);
00050     return result;
00051   }
00052 
00053 
00054   Sequence & ReverseComplementDNA::choose(Sequence & h, int size) const {
00055     Sequence revCompSeq;
00056     ProbabilisticModelDecorator::choose(revCompSeq, size);
00057     revcomp(h, revCompSeq, 0, revCompSeq.size()-1);
00058     return h;
00059   }
00060 
00061 
00062   Sequence & ReverseComplementDNA::choose(Sequence &h, int initial_phase, int size) const{
00063     Sequence revCompSeq;
00064     ProbabilisticModelDecorator::choose(revCompSeq, initial_phase, size);
00065     revcomp(h, revCompSeq, 0, size-1);
00066     return h;
00067   }
00068 
00069 
00070   double ReverseComplementDNA::prefix_sum_array_compute(int begin , int end){
00071     int b = _seqLength - end - 1;
00072     int e = _seqLength - begin - 1;
00073     double r = ProbabilisticModelDecorator::prefix_sum_array_compute(b, e);
00074     return r;
00075   }
00076   double ReverseComplementDNA::prefix_sum_array_compute(int begin , int end, int phase){
00077     int b = _seqLength - end - 1;
00078     int e = _seqLength - begin - 1;
00079     double r = ProbabilisticModelDecorator::prefix_sum_array_compute(b, e, phase);
00080     return r;
00081   }
00082 
00083   bool ReverseComplementDNA::initialize_prefix_sum_array(const Sequence & s, int phase){
00084     Sequence revCompSeq;
00085     revcomp(revCompSeq, s, 0, s.size()-1);
00086     _seqLength = s.size();
00087     return ProbabilisticModelDecorator::initialize_prefix_sum_array(revCompSeq, phase);
00088   }
00089 
00090   bool ReverseComplementDNA::initialize_prefix_sum_array(const Sequence & s) {
00091     Sequence revCompSeq;
00092     revcomp(revCompSeq, s, 0, s.size()-1);
00093     _seqLength = s.size();
00094     return ProbabilisticModelDecorator::initialize_prefix_sum_array(revCompSeq);
00095   }
00096   std::string ReverseComplementDNA::model_name () const {
00097     return "ReverseComplementDNA";
00098   }
00099 
00100   std::string ReverseComplementDNA::str() const{
00101 
00102     std::stringstream out;
00103     out << "model_name =\"" << model_name() << "\"" << std::endl;
00104     std::string modelname = ProbabilisticModelDecorator::subModelName();
00105     if(modelname.length() > 0)
00106       out << "model = " << modelname << std::endl ;
00107     else
00108       out << "model = [" << subModel()->str() << "]" << std::endl ;
00109     return out.str();
00110   }
00111 
00112 
00113   ProbabilisticModelParameters ReverseComplementDNA::parameters() const
00114   {
00115     ProbabilisticModelParameters p ;
00116     p.add("model_name", StringParameterValuePtr(new StringParameterValue(model_name())));
00117     std::string modelname = ProbabilisticModelDecorator::subModelName();
00118     if(modelname.length() > 0)
00119       p.add("model", StringParameterValuePtr( new StringParameterValue(modelname)));
00120     else
00121       p.add("model", ProbabilisticModelParameterListValuePtr (new ProbabilisticModelParameterListValue(subModel()->parameters())));
00122     return p;
00123   }
00124 
00125   void ReverseComplementDNA::initialize(const ProbabilisticModelParameters & p)
00126   {
00127     ProbabilisticModelParameterValuePtr modelpar = p.getMandatoryParameterValue("model");
00128     ProbabilisticModelCreatorClient creator;
00129     ConfigurationReader reader;
00130     std::string modelstr = modelpar->getString();
00131     ProbabilisticModelPtr m ;
00132     if((modelstr.size() > 0) && (modelstr[0] == '[') ){
00133       modelstr = modelstr.substr(1, modelstr.size() -2 );
00134       reader.load(modelstr);
00135       ProbabilisticModelParametersPtr par = reader.parameters();
00136       m = creator.create(*par);
00137     } else {
00138       m = creator.create(modelstr) ;
00139       if(m == NULL) {
00140         std::cerr << "Can not load model file " << modelstr<< "!" << std::endl;
00141         exit(-1);
00142       }
00143     }
00144     setSubModel(m);
00145     setAlphabet(m->alphabet());
00146   }
00147 
00148 }