ToPS
TrainHMMBaumWelch.cpp
00001 /*
00002  *       TrainHMMBaumWelch.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 "ProbabilisticModel.hpp"
00026 #include "ProbabilisticModelCreator.hpp"
00027 #include "ConfigurationReader.hpp"
00028 #include "TrainHMMBaumWelch.hpp"
00029 #include "util.hpp"
00030 #include "ProbabilisticModelCreatorClient.hpp"
00031 namespace tops {
00032 
00033   ProbabilisticModelPtr TrainHMMBaumWelch::create( ProbabilisticModelParameters & parameters) const
00034   {
00035     ProbabilisticModelParameterValuePtr initmodelpar = parameters.getMandatoryParameterValue("initial_model");
00036     ProbabilisticModelParameterValuePtr trainpar = parameters.getMandatoryParameterValue("training_set");
00037     ProbabilisticModelParameterValuePtr thrpar = parameters.getOptionalParameterValue("threshold");
00038     ProbabilisticModelParameterValuePtr maxiterpar = parameters.getOptionalParameterValue("maxiter");
00039     double threshold = 1e-5;
00040     if(thrpar != NULL)
00041       threshold = thrpar->getDouble();
00042     int maxiter = 500;
00043     if(maxiterpar != NULL)
00044       maxiter = maxiterpar->getInt();
00045 
00046     ProbabilisticModelCreatorClient creator;
00047     std::string name = initmodelpar->getString();
00048     ProbabilisticModelPtr m = creator.create(name);
00049     SequenceEntryList sample_set;
00050     AlphabetPtr alphabet = m->alphabet();
00051     readSequencesFromFile(sample_set, alphabet, trainpar->getString());
00052     SequenceList seqs;
00053     for(int i = 0; i < (int)sample_set.size(); i++)
00054       seqs.push_back(sample_set[i]->getSequence());
00055     m->trainBaumWelch(seqs, maxiter, threshold);
00056     return m;
00057   }
00058 };
00059 
00060 
00061