Generated on Tue Dec 16 13:34:01 2008 for ell-3.0.0 by doxygen 1.5.1

src/ell/rna/DW_RNA.cc

Go to the documentation of this file.
00001 
00002 #include "ell/rna/DW_RNA.hh"
00003 
00004 #include <biu/assertbiu.hh>
00005 #include <biu/RandomNumberFactory.hh>
00006 
00007 #include "ell/rna/S_RNAfe_SingleM.hh"
00008 #include "ell/StateCollector.hh"
00009 
00010 #include <algorithm>
00011 
00012 namespace ell {
00013 
00015     DW_RNA_MorganHiggs::
00016 	DW_RNA_MorganHiggs( const size_t iterations_)
00018      :  iterations(iterations_)
00019      {
00020      }
00021     
00023     DW_RNA_MorganHiggs::
00024 	~DW_RNA_MorganHiggs()
00026     {
00027     }
00028     
00030     size_t
00031     DW_RNA_MorganHiggs::
00032 	getIterations( void ) const
00034     {
00035         return iterations;
00036     }
00037     
00038 
00039 
00041     double 
00042     DW_RNA_MorganHiggs::
00043 	addBond(   std::string& sequence
00044                 , std::vector<size_t>& v_struct
00045                 , size_t start
00046                 , size_t end
00047                 , StateCollector* sc) 
00048     
00049     {
00050         assertbiu(sc != NULL, "no StateCollector sc given (==NULL)");
00051         v_struct[start] = end;
00052         
00054         std::string structure(v_struct.size(),'.');
00055         
00056         for (size_t i = 0; i < v_struct.size(); ++i) {
00057             if(v_struct[i]!=0) {
00058                 structure[i]='(';
00059                 structure[v_struct[i]]=')';
00060             }
00061         }
00062         
00063         S_RNAfe_SingleM* rna = new S_RNAfe_SingleM(sequence,structure);
00064         double energy = rna->getEnergy();
00065         
00066         sc->add(*rna);
00067         
00068         delete rna;
00069         return energy;
00070     }
00071 
00073     double 
00074     DW_RNA_MorganHiggs::
00075 	deleteBond(    std::string& sequence
00076                 , std::vector<size_t>& v_struct
00077                 , size_t start
00078                 , StateCollector* sc) 
00079     
00080     {
00081         assertbiu(sc != NULL, "no StateCollector sc given (==NULL)");
00082         
00083         v_struct[start] = 0;
00085         std::string structure(v_struct.size(),'.');
00086             
00087         for (size_t i = 0; i < v_struct.size(); ++i) {
00088             if(v_struct[i]!=0) {
00089                 structure[i]='(';
00090                 structure[v_struct[i]]=')';
00091             }
00092         }
00093         
00094         S_RNAfe_SingleM* rna = new S_RNAfe_SingleM(sequence,structure);
00095         double energy = rna->getEnergy();
00096         
00097         sc->add(*rna);
00098         
00099         delete rna;
00100         return energy;
00101     }
00102 
00104     StateCollector*
00105     DW_RNA_MorganHiggs::
00106 	walk(  const State* const begin
00107             , const State* const end
00108             , StateCollector* const scWalk
00109         ) const
00111     {
00112         using namespace std;
00113         
00114         typedef vector<size_t>::iterator vit;
00115         
00116     //  start = clock();
00118         assertbiu(begin != NULL, "State A is NULL");
00119         assertbiu(end != NULL, "State B is NULL");
00120         assertbiu(scWalk != NULL, "StateCollector is NULL");
00121         
00122         const S_RNAfe_SingleM* rna_A 
00123                     = dynamic_cast<const S_RNAfe_SingleM*>(begin);
00124         const S_RNAfe_SingleM* rna_B 
00125                     = dynamic_cast<const S_RNAfe_SingleM*>(end);
00126         
00127         assertbiu(rna_A != NULL, "state A is no RNA");
00128         assertbiu(rna_B != NULL, "state B is no RNA");
00129 
00131         assertbiu(rna_A->getSequence().compare(rna_B->getSequence()) == 0
00132                 , "state A and B have different sequences");
00133         
00135         string sequence = rna_A->getSequence();
00136         string str_A = rna_A->getStructure();
00137         string str_B = rna_B->getStructure();
00138         size_t length = str_A.size();
00139         
00140         vector<size_t> structA, structB;
00141         vector<size_t> tempA, tempB;
00142             
00144         for (size_t i = 0; i < length; ++i) {
00145             structA.push_back(0);
00146             structB.push_back(0);
00147             
00148             if(str_A[i]=='(') tempA.push_back(i);
00149             if(str_A[i]==')') {
00150                 structA[tempA.back()]=i;
00151                 tempA.pop_back();
00152             }
00153                     
00154             if(str_B[i]=='(') tempB.push_back(i);
00155             if(str_B[i]==')') {
00156                 structB[tempB.back()]=i;
00157                 tempB.pop_back();
00158             }
00159         }
00160         
00161         vector<size_t> indexA, indexB;
00162         
00163         for (size_t i = 0; i < length; ++i) {
00164             if(structA[i] != structB[i]) {
00165                 if(structA[i] != 0) indexA.push_back(i);
00166                 if(structB[i] != 0) indexB.push_back(i);
00167             }
00168         }
00169 
00170         
00172         vector<vector<size_t> > conflicts;
00173         vector<size_t> noconflicts;
00174         vector<size_t> temp;
00175         vector<size_t> temp2(indexA.size(),0);
00176             
00177         for (size_t i = 0; i < indexB.size(); ++i) {
00178             temp.clear();
00179             for (size_t j = 0; j < indexA.size(); ++j) {
00180                 if( !((indexB[i] < indexA[j] 
00181                        &&  structB[indexB[i]] > structA[indexA[j]]) 
00182                     || (indexB[i] > indexA[j] 
00183                        &&  structB[indexB[i]] < structA[indexA[j]]))) 
00184                 {
00185                     temp.push_back(j);
00186                     temp2[j] = 1;
00187                 }
00188             }       
00189             conflicts.push_back(temp);
00190         }
00191         
00193         for (size_t i = 0; i < indexA.size(); ++i) {
00194             if(temp2[i]==0) noconflicts.push_back(i); 
00195         }
00196         
00198         vector<size_t> choices;
00200         vector<size_t> addedBonds, deletedNonConflBonds;
00202         vector<size_t> temp_struct;
00203         vector<size_t> temp_struct_best;
00205         vector<vector<size_t> > temp_conflicts;
00207         vector<size_t> temp_noconflicts;
00209         size_t min_conflicts;
00211         size_t choice_add, choice_delete, toAdd, toDelete;
00213         SC_Listing *scl = NULL, *scl_best = NULL;
00215         double energy = 0, peakEnergy = -INT_MAX, Emax = INT_MAX;
00216         
00217     //  bucket1 = clock() - start;
00218         
00219         
00221         for (size_t count = 0; count < iterations; ++count) {
00222     //      cerr << "iteration "<<count<<endl;
00223             scl = new SC_Listing();
00224     //      scl->add(*rna_A);
00225             
00226             temp_struct = structA;
00227             temp_conflicts = conflicts;
00228             addedBonds.clear();
00229             peakEnergy = -INT_MAX;
00230     //      start = clock();
00231                     
00233             while(addedBonds.size() != conflicts.size()) {
00234                 min_conflicts = INT_MAX;
00236                 for (size_t i = 0; i < temp_conflicts.size(); ++i) {
00238                     if(find(addedBonds.begin(),addedBonds.end(),i) 
00239                             != addedBonds.end()) 
00240                     {
00241                         continue;
00242                     }
00243     //              if(count < 1) cerr << " bond "<<i<< " with size "<<temp_conflicts[i].size()<<endl;
00244                     if(temp_conflicts[i].size() < min_conflicts) { 
00245                         choices.clear(); 
00246                         min_conflicts = temp_conflicts[i].size();
00247                     } 
00248                     if(temp_conflicts[i].size() == min_conflicts) {
00249                         choices.push_back(i);
00250                     }
00251                 }
00252     //          cerr << "choices to Add are:\t";
00253     //          for (size_t i = 0; i < choices.size(); ++i) {
00254     //              cerr << choices[i] << " ";              
00255     //          }
00256     //          cerr << endl;
00258                 choice_add = (size_t)((double)(choices.size()) * 
00259                              (double)(biu::RNF::getRN(100)) / (double)100);
00261                 toAdd = choices[choice_add];
00262     //          if(count<1) {cerr << "toAdd: " << toAdd<<endl;}
00263                 
00264                 while(temp_conflicts[toAdd].size() != 0) {
00266                     choice_delete = (size_t)(
00267                                     (double)(temp_conflicts[toAdd].size()) 
00268                                     * (double)(biu::RNF::getRN(100))
00269                                     / 100.0);
00271                     toDelete = temp_conflicts[toAdd][choice_delete];
00273                     energy = deleteBond(    sequence, temp_struct, 
00274                                             indexA[toDelete], scl);
00276                     if(energy > Emax) break;
00277                     if(energy > peakEnergy) peakEnergy = energy;
00278                     
00280     //              if(count<1) {cerr << "toDelete: " << toDelete<<endl;}
00281                     for (size_t i = 0; i < temp_conflicts.size(); ++i) {
00282                                             
00283                         for (size_t j = 0; j < temp_conflicts[i].size(); ++j) {
00284                             if(toDelete == temp_conflicts[i][j]) { 
00285                                 temp_conflicts[i].erase(
00286                                         temp_conflicts[i].begin()+j);
00287                             }
00288                         }
00289     //                  if(count<1) {
00290     //                      cerr << "temp_conflicts["<<i<<"]:\t";
00291     //                      for (size_t k = 0; k < temp_conflicts[i].size(); ++k) {
00292     //                          cerr << temp_conflicts[i][k] << " ";
00293     //                      }
00294     //                      cerr << endl;
00295     //                  }
00296                     }
00297                 }
00298 
00299                 if(energy > Emax) break;
00300                             
00301                 energy = addBond(   sequence, temp_struct, indexB[toAdd]
00302                                     , structB[indexB[toAdd]], scl);
00303                 addedBonds.push_back(toAdd);
00304                 
00305                 if(energy > peakEnergy) peakEnergy = energy;
00306                 if(energy > Emax) break;                
00307             }
00308     //      bucket2 += clock() - start;
00309 
00310             if(energy > Emax) { delete scl; continue;}
00311             
00312     //      start = clock();
00313             temp_noconflicts = noconflicts;
00314             deletedNonConflBonds.clear();
00316             while(temp_noconflicts.size() != 0) {
00317                 choice_delete = (size_t)((double)(temp_noconflicts.size()) * 
00318                                 (double)(biu::RNF::getRN(100)) / (double)100);
00319                 toDelete = temp_noconflicts[choice_delete];
00320                 energy = deleteBond(    sequence, temp_struct
00321                                         , indexA[toDelete], scl);
00322                 if(energy > Emax) break;
00323                 if(energy > peakEnergy) peakEnergy = energy;
00324                 temp_noconflicts.erase( temp_noconflicts.begin() 
00325                                         + choice_delete);
00326             }
00327                 
00328     //      bucket3 += clock() - start;
00329             if(energy > Emax) { delete scl; continue;}
00330 
00331             Emax = peakEnergy;
00332             delete scl_best;
00333             scl_best = scl;
00334             temp_struct_best = temp_struct;
00335         }
00336         
00337         list<State*> l = scl_best->getList();
00338         
00339         for(list<State*>::const_iterator cit = l.begin(); cit!=l.end(); ++cit)
00340         {
00341             scWalk->add(**cit);
00342         }
00343         
00344 
00345         string str_temp(temp_struct_best.size(),'.');
00346             
00347         for (size_t i = 0; i < temp_struct_best.size(); ++i) {
00348             if(temp_struct_best[i]!=0) {
00349                 str_temp[i]='(';
00350                 str_temp[temp_struct_best[i]]=')';
00351             }
00352         }
00353         
00354     //  cerr << "str_A:\t\t" << str_A<<endl;
00355     //  cerr << "str_temp:\t" << str_temp<<endl;
00356     //  cerr << "str_B:\t\t" << str_B<<endl;
00357         
00358         delete scl_best;
00359         
00360     //  cerr << "1: " << double(bucket1)/CLOCKS_PER_SEC<<endl;
00361     //  cerr << "2: " << double(bucket2)/CLOCKS_PER_SEC<<endl;
00362     //  cerr << "3: " << double(bucket3)/CLOCKS_PER_SEC<<endl;
00363         
00364         
00365         return scWalk;
00366     }
00367 
00368 
00369 } // namespace ell
00370