ToPS
PhasedRunLengthDistribution.cpp
00001 /*
00002  *       PhasedRunLengthDistribution.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 "ConfigurationReader.hpp"
00026 #include "ProbabilisticModelParameter.hpp"
00027 #include "PhasedRunLengthDistribution.hpp"
00028 #include "ProbabilisticModelCreatorClient.hpp"
00029 namespace tops {
00030     int PhasedRunLengthDistribution::size() const {
00031         return subModel()->size();
00032     }
00033 
00034   ProbabilisticModelParameters PhasedRunLengthDistribution::parameters() const
00035   {
00036     ProbabilisticModelParameters p;
00037     p.add("model_name", StringParameterValuePtr(new StringParameterValue(model_name())));
00038     p.add("input_phase", IntParameterValuePtr(new IntParameterValue(_iphase)));
00039     p.add("output_phase", IntParameterValuePtr(new IntParameterValue(_ophase)));
00040     p.add("number_of_phases", IntParameterValuePtr(new IntParameterValue(_nphases)));
00041     p.add("delta", DoubleParameterValuePtr(new DoubleParameterValue(_delta)));
00042     std::string modelname = ProbabilisticModelDecorator::subModelName();
00043     if(modelname.length() > 0)
00044       p.add("model", StringParameterValuePtr(new StringParameterValue(modelname)));
00045     else
00046       p.add("model", StringParameterValuePtr(new StringParameterValue(subModel()->str())));
00047 
00048     return p;
00049   }
00050 
00051   void PhasedRunLengthDistribution::initialize(const ProbabilisticModelParameters & p)
00052   {
00053     ProbabilisticModelParameterValuePtr iphasepar = p.getMandatoryParameterValue("input_phase");
00054     ProbabilisticModelParameterValuePtr ophasepar = p.getMandatoryParameterValue("output_phase");
00055     ProbabilisticModelParameterValuePtr nphasepar = p.getMandatoryParameterValue("number_of_phases");
00056     ProbabilisticModelParameterValuePtr  deltapar = p.getMandatoryParameterValue("delta");
00057     ProbabilisticModelParameterValuePtr  modelpar = p.getMandatoryParameterValue("model");
00058 
00059     int iphase = iphasepar -> getInt();
00060     int ophase = ophasepar -> getInt();
00061     int nphase = nphasepar -> getInt();
00062     int delta = deltapar -> getInt();
00063     std::string modelstr = modelpar->getString();
00064 
00065     ProbabilisticModelCreatorClient creator;
00066     ConfigurationReader reader;
00067     ProbabilisticModelPtr m ;
00068     if((modelstr.size() > 0) && (modelstr[0] == '[') ){
00069       modelstr = modelstr.substr(1, modelstr.size() -2 );
00070       reader.load(modelstr);
00071       ProbabilisticModelParametersPtr par = reader.parameters();
00072       m = creator.create(*par);
00073     } else  {
00074         m = creator.create(modelstr) ;
00075         if(m == NULL) {
00076           std::cerr << "Can not load model file " << modelstr<< "!" << std::endl;
00077           exit(-1);
00078         }
00079       }
00080     AlphabetPtr alpha = m->alphabet();
00081     initialize(delta, iphase, ophase, nphase);
00082     setAlphabet(m->alphabet());
00083     setSubModel(m);
00084   }
00085 
00086 
00087   void PhasedRunLengthDistribution::initialize(int delta, int input_phase, int output_phase, int nphase)
00088   {
00089     _delta = delta;
00090     _iphase = input_phase;
00091     _ophase = output_phase;
00092     _nphases = nphase;
00093     double sum = -HUGE;
00094     for(int i = 0; i < subModel()->size(); i++) {
00095       int d = i + _delta;
00096       if(mod((d + _iphase-1), _nphases) == _ophase)
00097         sum = log_sum(sum, subModel()->log_probability_of(i));
00098     }
00099     _normfactor = sum;
00100   }
00101   double PhasedRunLengthDistribution::choose() const
00102   {
00103     int L = (int)(ProbabilisticModelDecorator::choose());
00104     while(mod((L + _iphase-1), _nphases ) != _ophase) {L++;}
00105 
00106     int d = L - _delta;
00107     if (d < 1) {
00108       L = _delta+1;
00109       while(mod((L + _iphase-1), _nphases ) != _ophase) {L++;}
00110       d = L - _delta;
00111     }
00112 
00113     return d;
00114   }
00115 
00116   double  PhasedRunLengthDistribution::log_probability_of(int s) const
00117   {
00118     int d = s + _delta;
00119     if(mod((d + _iphase-1), _nphases) != _ophase)
00120       return -HUGE;
00121     double result = subModel()->log_probability_of(d);
00122     return result-_normfactor;
00123   }
00124 
00125   Sequence & PhasedRunLengthDistribution::choose(Sequence & h, int size) const {
00126     for(int i = 0; i < size; i++)
00127       h.push_back(choose());
00128     return h;
00129   }
00130 
00131 std::string PhasedRunLengthDistribution::str() const{
00132   std::stringstream out;
00133   out << "model_name =\"" << model_name() << "\"" << std::endl;
00134   out << "delta = " << _delta << std::endl;
00135   out << "input_phase = " << _iphase << std::endl;
00136   out << "output_phase = " << _ophase << std::endl;
00137   out << "number_of_phases = " << _nphases << std::endl;
00138   std::string modelname = ProbabilisticModelDecorator::subModelName();
00139   if(modelname.length() > 0)
00140     out << "model = " << modelname << std::endl ;
00141   else
00142     out << "model = [" << subModel()->str() << "]" << std::endl ;
00143   return out.str();
00144 }
00145 std::string PhasedRunLengthDistribution::model_name () const{
00146   return "PhasedRunLengthDistribution";
00147 }
00148   double PhasedRunLengthDistribution::evaluate(const Sequence & s, unsigned int begin, unsigned int end) const{
00149     double result = 0.0;
00150     for(int i = begin; (i < (int)s.size()) && (i <= (int)end); i++)
00151       result += log_probability_of(s[i]);
00152     return result;
00153   }
00154 
00155 }
00156