ToPS
TargetModel.cpp
00001 /*
00002  *       TargetModel.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 "TargetModel.hpp"
00026 //#include "TargetModelCreator.hpp"
00027 #include "TrainDiscreteIIDModel.hpp"
00028 
00029 #include <iostream>
00030 #include <cmath>
00031 #include <sstream>
00032 #include <vector>
00033 #include <iterator>
00034 
00035 namespace tops {
00036 
00037   std::string TargetModel::str() const
00038   {
00039  {
00040       std::stringstream out;
00041       out << "model_name=\"TargetModel\""<< std::endl;
00042       out << ProbabilisticModel::alphabet()->str() << std::endl;
00043       return out.str();
00044     }
00045 
00046   }
00047 
00049   double TargetModel::evaluate(const Sequence & s, unsigned int begin, unsigned int end) const {
00050     if (end >= s.size())
00051       return -HUGE;
00052     if(begin < 0)
00053       return -HUGE;
00054     Sequence subseq(end - begin + 1);
00055     int k = 0;
00056     for(int i = begin ; i <= (int)end; i++){
00057       subseq[k] = s[i];
00058       k++;
00059     }
00060     SequenceEntryList samples;
00061     SequenceEntryPtr entry = SequenceEntryPtr(new SequenceEntry(alphabet()));
00062     entry->setSequence(subseq);
00063     samples.push_back(entry);
00064     TrainDiscreteIIDModelPtr trainingAlgorithm = TrainDiscreteIIDModelPtr(new TrainDiscreteIIDModel());
00065     ProbabilisticModelPtr m = trainingAlgorithm->train(samples, alphabet());
00066     return m->evaluate(s, begin, end);
00067   }
00068 
00069   void TargetModel::initialize (const ProbabilisticModelParameters & p )
00070   {
00071     ProbabilisticModelParameterValuePtr alphabet = p.getOptionalParameterValue("alphabet");
00072     if (alphabet != NULL)
00073       {
00074         AlphabetPtr alpha = AlphabetPtr(new Alphabet());
00075         alpha->initializeFromVector(alphabet->getStringVector());
00076         setAlphabet(alpha);
00077       }
00078   }
00079   ProbabilisticModelParameters TargetModel::parameters() const
00080   {
00081     ProbabilisticModelParameters p;
00082     p.add("model_name", StringParameterValuePtr(new StringParameterValue("TargetModel")));
00083     p.add("alphabet", alphabet()->getParameterValue());
00084     return p;
00085   }
00086 
00087 }