ToPS
FixedSequenceAtPositionCreator.cpp
00001 /*
00002  *       FixedSequenceAtPositionCreator.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 "FixedSequenceAtPositionCreator.hpp"
00026 #include "SequenceFactory.hpp"
00027 #include "SequenceFactory.hpp"
00028 #include "ProbabilisticModelCreatorClient.hpp"
00029 #include "FixedSequenceAtPosition.hpp"
00030 
00031 namespace tops {
00032   ProbabilisticModelPtr FixedSequenceAtPositionCreator::create( ProbabilisticModelParameters & parameters) const {
00033     ProbabilisticModelParameterValuePtr positionpar = parameters.getMandatoryParameterValue("position");
00034     ProbabilisticModelParameterValuePtr sequencepar = parameters.getMandatoryParameterValue("sequence");
00035     ProbabilisticModelParameterValuePtr probabilitypar = parameters.getMandatoryParameterValue("probability");
00036     ProbabilisticModelParameterValuePtr modelpar = parameters.getMandatoryParameterValue("model");
00037     boost::regex sep(" ");
00038     std::vector<std::string> seqstr;
00039     split_regex(sequencepar->getString(), seqstr,  sep);
00040 
00041 
00042     int position = positionpar->getInt();
00043     std::vector<double> probs;
00044     probs.push_back(probabilitypar->getDouble());
00045     probs.push_back(1.0 - probabilitypar->getDouble());
00046     ProbabilisticModelCreatorClient creator;
00047     ConfigurationReader reader;
00048     std::string modelstr = modelpar->getString();
00049 
00050     ProbabilisticModelPtr m ;
00051     if((modelstr.size() > 0) && (modelstr[0] == '[') ){
00052       modelstr = modelstr.substr(1, modelstr.size() -2 );
00053       reader.load(modelstr);
00054       ProbabilisticModelParametersPtr par = reader.parameters();
00055       m = creator.create(*par);
00056     } else
00057       {
00058         m = creator.create(modelstr) ;
00059         if(m == NULL) {
00060           std::cerr << "Can not load model file " << modelstr<< "!" << std::endl;
00061           exit(-1);
00062         }
00063       }
00064     FixedSequenceAtPositionPtr decorator = FixedSequenceAtPositionPtr(new FixedSequenceAtPosition(m));
00065 
00066     DiscreteIIDModelPtr distr = DiscreteIIDModelPtr(new DiscreteIIDModel(probs));
00067     AlphabetPtr alpha = m->alphabet();
00068     SequenceFactory factory(alpha);
00069     Sequence sequence = factory.createSequence(seqstr);
00070     decorator->initialize(position, sequence, distr);
00071     decorator->setAlphabet(m->alphabet());
00072     decorator->subModelName(modelstr);
00073     return decorator;
00074   }
00075 
00076 }