Generated on Mon Jun 23 17:17:53 2008 for ell-2.3.0 by doxygen 1.5.1

src/ell/LT_MinimaSet.cc

Go to the documentation of this file.
00001 
00002 #include <fstream>
00003 #include <sstream>
00004 #include <cmath>
00005 #include <algorithm>
00006 
00007 #include <biu/assertbiu.hh>
00008 
00009 #include "ell/LT_MinimaSet.hh"
00010 
00011 
00012 namespace ell {
00013 
00014 const std::string LT_MinimaSet::LT_ID = "MS";
00015 
00016 LT_MinimaSet::LT_MinimaSet()
00017 : mfeIndex(LandscapeTopology::INVALID_INDEX), sorted(true)
00018 { }
00019 
00020 void 
00021 LT_MinimaSet::clear() {
00022     for (size_t i = 0; i < vMinima.size(); ++i)
00023         delete vMinima[i];
00024     vMinima.clear();
00025     mfeIndex = LandscapeTopology::INVALID_INDEX;
00026     sorted = true;
00027 }
00028 
00029 LT_MinimaSet::~LT_MinimaSet() {
00030     clear();
00031 }
00032 
00033 bool
00034 LT_MinimaSet::addSaddle(const size_t i, const size_t j, const State* const s) {
00035     // no saddle handling and therefore no change
00036     return false;
00037 }
00038 
00039 bool
00040 LT_MinimaSet::addSaddle(const State* const si, const State* const sj, const State* const s) {
00041     // no saddle handling and therefore no change
00042     return false;
00043 }
00044 
00045 const size_t 
00046 LT_MinimaSet::addMin(const State* const s) {
00047     assertbiu(s != NULL, "given minimum State to add is NULL");
00048     assertbiu(getMinIndex(s) == LandscapeTopology::INVALID_INDEX, "given minimum is known and already part of the landscape");
00049     
00050     // add a copy of s to the set
00051     vMinima.push_back(s->clone());
00052     
00053      // update mfeIndex if necessary
00054     if (vMinima.size() == 1) {
00055         mfeIndex = 0;
00056     } else {
00057         if (LandscapeTopology::isSmaller_E_S( s, vMinima[mfeIndex])) {
00058               // new energy is smaller 
00059               // OR new energy is equal but string representation is smaller
00060             mfeIndex = vMinima.size()-1;
00061               // is not sorted anymore because smallest is at the end now
00062             sorted = false;
00063         } else if (sorted) {
00064               // compare last with newly added if the new one is not smaller
00065             sorted = LandscapeTopology::isSmaller_E_S( vMinima[vMinima.size()-2], s );
00066         }
00067     }
00068 
00069     return vMinima.size()-1;
00070 }
00071 
00072 
00073 const State* const
00074 LT_MinimaSet::getMin(size_t i) const {
00075     assertbiu(i < vMinima.size(), "minimum index i out of bound!");
00076     
00077     return vMinima[i];
00078 }
00079 
00080 
00081 const size_t 
00082 LT_MinimaSet::getMinIndex(const State* const s) const{
00083     assertbiu(s != NULL, "given minimum State to get index of is NULL");
00084 
00085     for (size_t i = 0; i < vMinima.size(); ++i)
00086         if(*vMinima[i] == *s) 
00087             return i;
00088     
00089     return LandscapeTopology::INVALID_INDEX;
00090 }
00091 
00092 const size_t 
00093 LT_MinimaSet::getMinCount() const {
00094 
00095     return vMinima.size();
00096 }
00097 
00098 const State* const 
00099 LT_MinimaSet::getMFEState() const {
00100     assertbiu(mfeIndex != ell::LandscapeTopology::INVALID_INDEX, "no minimum present and therefore no mfe state too");
00101     return vMinima[mfeIndex];   
00102 }
00103 
00104 const std::vector<const State* > & 
00105 LT_MinimaSet::getAllMin() const {
00106     return vMinima;
00107 }
00108 
00109 
00110 // calculates the RMSD between this and a given LT_MinimaSet(difference
00111 double 
00112 LT_MinimaSet::getRMSD(const LandscapeTopology* const pLT ) const {
00113     assertbiu(pLT!=NULL, "given LandscapeTopology is NULL!");
00114     
00115     const LT_MinimaSet* const ms = dynamic_cast<const LT_MinimaSet* const>(pLT);
00116     assertbiu(ms!=NULL, "given LandscapeTopology no LT_MinimaSet!");
00117         
00118     double rmsd = 0;
00119     
00120     
00121     std::vector<const State*> vM1 = vMinima;
00122     std::vector<const State*> vM2 = ms->vMinima;
00123     
00124     std::sort(vM1.begin(), vM1.end(), LandscapeTopology::isSmaller_E_S);
00125     std::sort(vM2.begin(), vM2.end(), LandscapeTopology::isSmaller_E_S);
00126     
00127     size_t i = 0;
00128     for (; i < vM1.size() && i < vM2.size(); ++i)
00129         rmsd += pow( vM1[i]->getEnergy() - vM2[i]->getEnergy(), 2);
00130 
00131     for (; i < vM1.size() || i < vM2.size(); ++i)
00132         if(i < vM1.size())
00133             rmsd += pow( vM1[i]->getEnergy() - vM2[0]->getEnergy(), 2);
00134         else // (i < vM2.size())
00135             rmsd += pow( vM1[0]->getEnergy() - vM2[i]->getEnergy(), 2);
00136     
00137     rmsd /= static_cast<double>(std::max(vM1.size(),vM2.size()));
00138     rmsd = sqrt(rmsd);
00139     
00140     return rmsd;
00141 }
00142 
00143 void 
00144 LT_MinimaSet::sort() {
00145       // sort only if necessary
00146     if (sorted)
00147         return;
00148       // sort minima
00149     std::sort(vMinima.begin(),vMinima.end(),LandscapeTopology::isSmaller_E_S);
00150       // update mfe index
00151     mfeIndex = 0;
00152       // set sorted
00153     sorted = true;
00154 }
00155 
00156 bool
00157 LT_MinimaSet::isSorted() const {
00158     return sorted;
00159 }
00160 
00161 std::pair<int,std::string>
00162 LT_MinimaSet::read( std::istream & in, 
00163                     const State * const templateState, 
00164                     const std::string & StateDescription )
00165 {
00166     typedef std::pair<int,std::string> RETURNTYPE;
00167     State* s;
00168     enum STATUS {NOTHING, MINIMUM};
00169     STATUS status;
00170     
00171     std::string sequence, structure, line, substring;
00172     std::stringstream sstr;
00173     
00174     size_t type= INT_MAX, dim = INT_MAX;
00175     size_t index_i, index_j;
00176     size_t lineNumber = 0;
00177     
00178     std::string::size_type i, k, l;
00179     
00180     
00182     std::getline(in, line);
00183     
00184     i = line.find("ELL-LT-TYPE=");
00185     if(i != std::string::npos) { 
00186         line=line.substr(i+12);
00187         
00188         if (line.compare(LT_MinimaSet::LT_ID) != 0) {
00189             return RETURNTYPE(-1,"ELL-LT-TYPE not supported");
00190         }
00191     } else  {
00192         return RETURNTYPE(-1,"ELL-LT-TYPE missing in first line of file");
00193     }
00194     
00195     std::getline(in, line);
00196     
00197     i = line.find("STATETYPE=");
00198     if(i != std::string::npos) { 
00199         line=line.substr(i+10);
00200         
00201         if(line.compare(StateDescription) != 0) {
00202             RETURNTYPE(-2,"STATETYPE differs given type");
00203         } 
00204     } else  {
00205         RETURNTYPE(-2,"STATETYPE missing in second line of file");
00206     }
00207 
00208     std::getline(in, line);
00209     i = line.find("DIMENSION=");
00210     if(i != std::string::npos) {
00211         line=line.substr(i+10);         
00212         
00213         sstr.clear();
00214         sstr.str(line);
00215         sstr >> dim;
00216     } else  {
00217         RETURNTYPE(-3,"DIMENSION missing in third line of file");
00218     }
00219     
00220     
00221     lineNumber = 3;
00223     while( std::getline(in, line) ) {
00224         ++lineNumber;
00225         s = NULL;
00226         
00227 //      cout << "line "<<lineNumber<<" is "<<line <<endl;
00228         index_i = index_j = INT_MAX;
00229         i = k = l = INT_MAX;
00230         
00231         status = NOTHING;
00232         
00234         i = line.find("GRAPHVIZ");
00235         if(i != std::string::npos) break;
00236         
00237         i = line.find("MINIMUM");
00238         if(i != std::string::npos) {
00239             if(type== INT_MAX || dim==INT_MAX) {
00240                 RETURNTYPE(-3,"Wrong file format: Statetype or Dimension not defined before first state definition !");
00241             }
00242               // read minimum index
00243             size_t pos_i_begin = line.find("i=")+2;
00244             size_t pos_i_end   = line.find_first_of(" \t",pos_i_begin);
00245             sstr.clear();
00246             sstr.str( line.substr( pos_i_begin, pos_i_end ) );
00247             sstr >> index_i;
00248             
00249             status = MINIMUM;
00250             
00251             s = templateState->fromString(line.substr(line.find_first_not_of(" \t",pos_i_end)));
00252             if(s == NULL) {
00253                 std::stringstream errstr;
00254                 errstr <<"State object could not be generated from line "<<lineNumber <<" !";
00255                 return RETURNTYPE(-3,errstr.str());
00256                 continue;
00257             }
00258         } 
00259         
00260         if(status==NOTHING) continue;
00261  
00262         if(status==MINIMUM) 
00263             addMin(s);  // s is a new object already
00264         
00265         delete s;
00266         
00267     }
00268 
00269     
00270     return RETURNTYPE(0,"");
00271 }
00272 
00273 void
00274 LT_MinimaSet::write(    std::ostream& out, 
00275                     const std::string & StateDescription ) const
00276 {
00277     size_t dim = vMinima.size();
00278 
00281 
00282     out << "//ELL-LT-TYPE=" <<LT_ID <<"\n";
00283     out << "//STATETYPE=" <<StateDescription<< "\n";
00284     out << "//DIMENSION="<<dim<< "\n";
00285     
00288     for (size_t i = 0; i < dim; ++i) {
00289         out << "//MINIMUM\ti="<<i<<"\t"<<vMinima.at(i)->toString()<<"\n";
00290     }
00291     
00292     out << "\n" << "//GRAPHVIZ"<<"\n";
00293     
00295     out << "graph SADDLENET {"<<"\n";
00296     for (size_t i = 0; i < dim; ++i) {
00297         out << "\t\tM"<<i<<";"<<"\n";           
00298     }   
00299     out << "label = \"\\n\\nLT_MinimaSet Diagram\\nwith "<<dim<<" minima \";"<<"\n";
00300     out << "fontsize=20;"<<"\n";
00301     out << "} "<<"\n";
00302     
00303       // flush the output stream
00304     out.flush();
00305     
00306 }
00307 
00308 
00309 }//ell
00310 
00311