src/ell/rna/S_RNAfe_SingleM_TB.cc
Go to the documentation of this file.00001 #include "ell/rna/S_RNAfe_SingleM_TB.hh"
00002 #include <cmath>
00003 #include <biu/assertbiu.hh>
00004 #include "biu/RandomNumberFactory.hh"
00005
00006
00007 namespace ell
00008 {
00009
00010
00011 const std::string S_RNAfe_SingleM_TB::ID = std::string("ell::S_RNAfe_SingleM_TB");
00012
00013
00014 const size_t
00015 S_RNAfe_SingleM_TB::RandomNeighborList::ItState::SWITCH_MODE_THRESHOLD = 20;
00016
00018
00020
00021 S_RNAfe_SingleM_TB::S_RNAfe_SingleM_TB(
00022 const std::string& rnaSeqStr,
00023 const std::string& rnaStructBracketNotStr)
00024 : rnaFreeEnergy(rnaSeqStr, rnaStructBracketNotStr)
00025 {
00026 assertbiu(rnaFreeEnergy.isValid(),
00027 "S_RNAfe_SingleM_TB has to be a valid state.");
00028
00029 }
00030
00031 S_RNAfe_SingleM_TB::S_RNAfe_SingleM_TB(const RNAFreeEnergy_TB& rna)
00032 : rnaFreeEnergy(rna)
00033 {
00034 assertbiu(rnaFreeEnergy.isValid(),
00035 "S_RNAfe_SingleM_TB has to be a valid state.");
00036
00037 }
00038
00039 S_RNAfe_SingleM_TB::S_RNAfe_SingleM_TB(
00040 const S_RNAfe_SingleM_TB& rnaFESMS)
00041 : rnaFreeEnergy(rnaFESMS.rnaFreeEnergy)
00042 {
00043 }
00044
00045 S_RNAfe_SingleM_TB::~S_RNAfe_SingleM_TB()
00046 {
00047 }
00048
00049
00050
00051
00052
00053 const std::string&
00054 S_RNAfe_SingleM_TB::getID( void ) const {
00055 return S_RNAfe_SingleM_TB::ID;
00056 }
00057
00058
00059 S_RNAfe_SingleM_TB::SingleMove
00060 S_RNAfe_SingleM_TB::firstValidSingleMove() const {
00061 SingleMove m( biu::RNAStructure_TB::INVALID_INDEX
00062 , biu::RNAStructure_TB::INVALID_INDEX );
00063 if (rnaFreeEnergy.getNextSingleMove(m.first, m.second)) {
00064 return m;
00065 } else {
00066 return SingleMove(0,biu::RNAStructure_TB::INVALID_INDEX);
00067 }
00068 }
00069
00070 S_RNAfe_SingleM_TB::SingleMove
00071 S_RNAfe_SingleM_TB::nextValidSingleMove(
00072 const SingleMove lastSingleMove) const
00073 {
00074 SingleMove ret(lastSingleMove);
00075 if (rnaFreeEnergy.getNextSingleMove( ret.first, ret.second ))
00076 {
00077 return ret;
00078 } else {
00079 return SingleMove(0,biu::RNAStructure_TB::INVALID_INDEX);
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 }
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 bool
00144 S_RNAfe_SingleM_TB::operator== (const State& state2) const {
00145
00146 const S_RNAfe_SingleM_TB * tmp
00147 = dynamic_cast< const S_RNAfe_SingleM_TB*>(&state2);
00148 return (tmp==NULL? false : rnaFreeEnergy == tmp->rnaFreeEnergy);
00149 }
00150
00151 bool
00152 S_RNAfe_SingleM_TB::operator!= (const State& state2) const {
00153 return !(this->operator==(state2));
00154 }
00155
00156 bool
00157 S_RNAfe_SingleM_TB::operator< (const State& rna2) const
00158 {
00159 if (this == &rna2) {
00160 return false;
00161 }
00162 assertbiu(dynamic_cast<const S_RNAfe_SingleM_TB*>(&rna2)!=NULL
00163 , "rna2 is no S_RNAfe_SingleM_TB object");
00164 const S_RNAfe_SingleM_TB* s2 = static_cast<const S_RNAfe_SingleM_TB*>(&rna2);
00165 assertbiu(rnaFreeEnergy.getLength() == s2->rnaFreeEnergy.getLength()
00166 , "the two RNA molecules differ in length");
00167
00168 return (this->getEnergy() < s2->getEnergy())
00169 ||
00170 (this->getEnergy() == s2->getEnergy()
00171 && rnaFreeEnergy.getStructureStringRef().compare(
00172 s2->rnaFreeEnergy.getStructureStringRef() ) < 0 ) ;
00173 }
00174
00175 double
00176 S_RNAfe_SingleM_TB::getEnergy(void) const {
00177 return rnaFreeEnergy.getEnergy();
00178 }
00179
00180 unsigned int
00181 S_RNAfe_SingleM_TB::getMinimalDistance( const State& _state2) const
00182 {
00183 assertbiu(dynamic_cast<const S_RNAfe_SingleM_TB*>(&_state2) != NULL
00184 , "state to calculate distance to is no S_RNAfe_SingleM_TB");
00185 const S_RNAfe_SingleM_TB* state2 =
00186 static_cast<const S_RNAfe_SingleM_TB*>(&_state2);
00187
00188 const std::string& state1Struct = this->rnaFreeEnergy.getStructureStringRef();
00189 const std::string& state2Struct = state2->rnaFreeEnergy.getStructureStringRef();
00190
00191 assertbiu(state2Struct.size() == state1Struct.size(),
00192 "Length of both RNA has to be equal to compute the distance.");
00193
00194 unsigned int distance = 0;
00195
00196 const char BOND_OPEN = this->rnaFreeEnergy.getStructureAlphabet()->getString(biu::RNAStructure_TB::STRUCT_BND_OPEN).at(0);
00197
00198 for (size_t i = 0; i < state1Struct.size(); i++){
00199
00200 if ( BOND_OPEN == state1Struct[i]
00201 && BOND_OPEN == state2Struct[i] )
00202 {
00203
00204 if ( this->rnaFreeEnergy.getCorrespondingBase(i)
00205 != state2->rnaFreeEnergy.getCorrespondingBase(i))
00206 {
00207 distance += 2;
00208 }
00209 }
00210
00211 else if ( BOND_OPEN == state1Struct[i]
00212 || BOND_OPEN == state2Struct[i] )
00213 {
00214 distance += 1;
00215 }
00216 }
00217 return distance;
00218 }
00219
00220 S_RNAfe_SingleM_TB*
00221 S_RNAfe_SingleM_TB::clone(State* toFill) const {
00222 if (toFill == NULL) {
00223 return new S_RNAfe_SingleM_TB(*this);
00224 }
00225
00226 assertbiu( dynamic_cast<S_RNAfe_SingleM_TB*>(toFill) != NULL
00227 , "given State is no S_RNAfe_SingleM_TB instance");
00228 S_RNAfe_SingleM_TB* s = static_cast<S_RNAfe_SingleM_TB*>(toFill);
00229
00230
00231 s->rnaFreeEnergy = this->rnaFreeEnergy;
00232
00233 return s;
00234 }
00235
00236
00237 State*
00238 S_RNAfe_SingleM_TB::fromString(const std::string& stringRep) const {
00239
00240 std::string backboneRep = ".()";
00241 size_t pos = stringRep.find_first_not_of(backboneRep);
00242 const std::string rnaStructBracketDotStr = stringRep.substr(0, pos-1);
00243 assertbiu(rnaStructBracketDotStr.size() != 0, "string does not contain a dot-bracket structure encoding");
00244 assertbiu(rnaStructBracketDotStr.size() == this->rnaFreeEnergy.getLength(), "string is too short for this RNA molecule");
00245
00246 S_RNAfe_SingleM_TB* ret = this->clone();
00247
00248 ret->rnaFreeEnergy.setStructure(
00249 ret->rnaFreeEnergy.getStructureAlphabet()->getSequence(
00250 rnaStructBracketDotStr));
00251 return ret;
00252 }
00253
00254 State*
00255 S_RNAfe_SingleM_TB::fromString(const std::string& stringRep, const double energy) const {
00256
00257 State* ret = this->fromString(stringRep);
00258
00259 static_cast<S_RNAfe_SingleM_TB*>(ret)->rnaFreeEnergy.setEnergy(energy);
00260
00261 return ret;
00262 }
00263
00264 std::string
00265 S_RNAfe_SingleM_TB::toString() const{
00266 return rnaFreeEnergy.getStructureString();
00267 }
00268
00269 std::string&
00270 S_RNAfe_SingleM_TB::toString( std::string & toFill ) const {
00271
00272 toFill = rnaFreeEnergy.getStructureStringRef();
00273 return toFill;
00274 }
00275
00276 std::string
00277 S_RNAfe_SingleM_TB::getSequence() const {
00278 return rnaFreeEnergy.getSequenceString();
00279 }
00280
00281 std::string
00282 S_RNAfe_SingleM_TB::getStructure() const {
00283 return rnaFreeEnergy.getStructureString();
00284 }
00285
00286 State::NeighborListPtr
00287 S_RNAfe_SingleM_TB::getNeighborList() const {
00288 return NeighborListPtr(new NeighborList(this));
00289 }
00290
00291 State::NeighborListPtr
00292 S_RNAfe_SingleM_TB::getRandomNeighborList() const
00293 {
00294 return NeighborListPtr(new RandomNeighborList(this));
00295 }
00296
00297
00298 S_RNAfe_SingleM_TB::NeighborList::NeighborList(
00299 const S_RNAfe_SingleM_TB* _origin)
00300 : origin(_origin)
00301 {
00302 }
00303
00304 S_RNAfe_SingleM_TB::NeighborList::~NeighborList() {
00305 }
00306
00307 State*
00308 S_RNAfe_SingleM_TB::NeighborList::first(
00309 State::NeighborList::ItState** itstate) const {
00310 if (*itstate != NULL) delete *itstate;
00311
00312 ItState* myitstate = new ItState();
00313 *itstate = myitstate;
00314
00315 S_RNAfe_SingleM_TB* rnaState = NULL;
00316
00317 SingleMove firstMove = origin->firstValidSingleMove();
00318
00319
00320 if (firstMove != SingleMove(0,biu::RNAStructure_TB::INVALID_INDEX)) {
00321 rnaState = new S_RNAfe_SingleM_TB(*origin);
00322 rnaState->rnaFreeEnergy.applySingleMoveInPlace(firstMove);
00323 myitstate->lastMove = firstMove;
00324 }
00325 return rnaState;
00326 }
00327
00328 State*
00329 S_RNAfe_SingleM_TB::NeighborList::next(
00330 State::NeighborList::ItState* itstate, State* elem) const {
00331 assertbiu(elem != NULL, "elem is not allowed to be NULL");
00332 assertbiu(dynamic_cast<S_RNAfe_SingleM_TB*>(elem) != NULL
00333 , "elem is no instance of S_RNAfe_SingleM_TB");
00334
00335 ItState* myitstate = static_cast<ItState*>(itstate);
00336
00337
00338 SingleMove nextMove = origin->nextValidSingleMove(myitstate->lastMove);
00339
00340 if (nextMove == SingleMove(0,biu::RNAStructure_TB::INVALID_INDEX)) {
00341 return NULL;
00342 }
00343 else {
00344 S_RNAfe_SingleM_TB* rnaState
00345 = static_cast<S_RNAfe_SingleM_TB*>(elem);
00346
00347 rnaState->rnaFreeEnergy.applySingleMoveInPlace(myitstate->lastMove);
00348
00349 rnaState->rnaFreeEnergy.applySingleMoveInPlace(nextMove);
00350 myitstate->lastMove = nextMove;
00351 return rnaState;
00352 }
00353 }
00354
00355
00356 S_RNAfe_SingleM_TB::RandomNeighborList::RandomNeighborList(
00357 const S_RNAfe_SingleM_TB* _origin)
00358 : origin(_origin)
00359 {
00360 }
00361
00362 S_RNAfe_SingleM_TB::RandomNeighborList::~RandomNeighborList() {
00363 }
00364
00365 void
00366 S_RNAfe_SingleM_TB::RandomNeighborList::switchMode(
00367 ItState* itstate) const
00368 {
00369
00370 ItState* myitstate = static_cast<ItState*>(itstate);
00371
00372 SingleMove nextMove = origin->firstValidSingleMove();
00373
00374
00375 while (nextMove != SingleMove(0,biu::RNAStructure_TB::INVALID_INDEX)) {
00376
00377 if (myitstate->chosenMoves.find(nextMove) ==
00378 myitstate->chosenMoves.end()) {
00379 myitstate->unchosenValidMoves.push_back(nextMove);
00380 }
00381 nextMove = origin->nextValidSingleMove(nextMove);
00382 }
00383 }
00384
00385 State*
00386 S_RNAfe_SingleM_TB::RandomNeighborList::first(
00387 State::NeighborList::ItState** itstate) const
00388 {
00389 if (*itstate != NULL) delete *itstate;
00390
00391 *itstate = new ItState();
00392 S_RNAfe_SingleM_TB* rnaState =
00393 new S_RNAfe_SingleM_TB(*origin);
00394
00395 return next(*itstate, rnaState);
00396 }
00397
00398 State*
00399 S_RNAfe_SingleM_TB::RandomNeighborList::next(
00400 State::NeighborList::ItState* itstate, State* elem) const
00401 {
00402 assertbiu(elem != NULL, "Elem is not allowed to be NULL");
00403 assertbiu(dynamic_cast<S_RNAfe_SingleM_TB*>(elem) != NULL
00404 , "elem is no instance of S_RNAfe_SingleM_TB");
00405
00406 ItState* myitstate = static_cast<ItState*>(itstate);
00407 S_RNAfe_SingleM_TB* rnaState = static_cast<S_RNAfe_SingleM_TB*>(elem);
00408
00409 if (myitstate->switchModeValue > 0) {
00410
00411 size_t pos1, pos2;
00412 SingleMove nextMove;
00413 bool foundValidMove = false;
00414
00415 while (!foundValidMove) {
00416 pos1 = biu::RNF::getRN() % origin->rnaFreeEnergy.getLength();
00417 pos2 = biu::RNF::getRN() % origin->rnaFreeEnergy.getLength();
00418 if (pos1 == pos2) {
00419 continue;
00420 }
00421 nextMove = (pos1 < pos2) ? SingleMove(pos1, pos2) : SingleMove(pos2, pos1);
00422
00423 if (origin->rnaFreeEnergy.isValidSingleMove(nextMove)) {
00424 if (!(myitstate->chosenMoves.insert(nextMove).second)) {
00425
00426 myitstate->switchModeValue--;
00427 if (myitstate->switchModeValue == 0) {
00428 switchMode(myitstate);
00429 break;
00430 }
00431 }
00432
00433
00434 rnaState->rnaFreeEnergy = origin->rnaFreeEnergy;
00435 rnaState->rnaFreeEnergy.applySingleMoveInPlace(
00436 nextMove);
00437 foundValidMove = true;
00438 }
00439 }
00440 }
00441 if (myitstate->switchModeValue == 0) {
00442 size_t n = myitstate->unchosenValidMoves.size();
00443 if (n == 0) {
00444 return NULL;
00445 }
00446 else {
00447 size_t i = biu::RNF::getRN() % n;
00448
00449 rnaState->rnaFreeEnergy = origin->rnaFreeEnergy;
00450 rnaState->rnaFreeEnergy.applySingleMoveInPlace(
00451 myitstate->unchosenValidMoves[i]);
00452 myitstate->unchosenValidMoves[i] =
00453 myitstate->unchosenValidMoves[n-1];
00454 myitstate->unchosenValidMoves.resize(n-1);
00455 }
00456 }
00457 return rnaState;
00458 }
00459
00460 State*
00461 S_RNAfe_SingleM_TB::getRandomNeighbor(State* inPlaceNeigh) const
00462 {
00463 assertbiu(dynamic_cast<S_RNAfe_SingleM_TB*>(inPlaceNeigh) != NULL
00464 , "inPlaceNeigh is no S_RNAfe_SingleM_TB");
00465 S_RNAfe_SingleM_TB* neigh = NULL;
00466
00467 if (inPlaceNeigh != NULL) {
00468 neigh = static_cast<S_RNAfe_SingleM_TB*>(inPlaceNeigh);
00469 (*neigh) = (*this);
00470 } else {
00471 neigh = new S_RNAfe_SingleM_TB(*this);
00472 }
00473
00474 size_t pos1, pos2;
00475 SingleMove nextMove;
00476
00477 while (true) {
00478 pos1 = biu::RNF::getRN() % rnaFreeEnergy.getLength();
00479 pos2 = biu::RNF::getRN() % rnaFreeEnergy.getLength();
00480 if (pos1 == pos2) {
00481 continue;
00482 }
00483 nextMove = (pos1 < pos2) ? SingleMove(pos1, pos2) : SingleMove(pos2, pos1);
00484
00485 if (rnaFreeEnergy.isValidSingleMove(nextMove)) {
00486
00487 neigh->rnaFreeEnergy.applySingleMoveInPlace( nextMove );
00488 break;
00489 }
00490 }
00491 return neigh;
00492 }
00493
00494
00495
00497
00498
00499
00500
00501 CSequence
00502 S_RNAfe_SingleM_TB::compress(void) const {
00503 return rnaFreeEnergy.getStructureAlphabet()->compressS(rnaFreeEnergy.getStructureStringRef());
00504 }
00505
00506
00507
00508
00509
00510
00511 CSequence&
00512 S_RNAfe_SingleM_TB::compress(CSequence& toFill) const {
00513 toFill = rnaFreeEnergy.getStructureAlphabet()->compressS(rnaFreeEnergy.getStructureStringRef());
00514 return toFill;
00515 }
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525 State*
00526 S_RNAfe_SingleM_TB::uncompress(const CSequence& cseq, State* toFill) const {
00527
00528 assertbiu(toFill == NULL || dynamic_cast<S_RNAfe_SingleM_TB*>(toFill) != NULL
00529 , "toFill is no S_RNAfe_SingleM_TB");
00530
00531 biu::Structure str = rnaFreeEnergy.getStructureAlphabet()->decompress(cseq,rnaFreeEnergy.getStructureStringRef().size());
00532
00533 if (str.size() != rnaFreeEnergy.getStructureStringRef().size())
00534 return NULL;
00535
00536 S_RNAfe_SingleM_TB* ret = NULL;
00537
00538 if (toFill == NULL) {
00539 ret = this->clone();
00540 } else {
00541 ret = static_cast<S_RNAfe_SingleM_TB*>(toFill);
00542 assertbiu(ret!=NULL, "given State toFill is no S_RNAfe_SingleM_TB instance");
00543 }
00544
00545 ret->rnaFreeEnergy.setStructure(str);
00546
00547 return ret;
00548 }
00549
00550
00551
00552
00553
00554
00555 State*
00556 S_RNAfe_SingleM_TB::uncompress(const CSequence& cseq) {
00557
00558 biu::Structure str = rnaFreeEnergy.getStructureAlphabet()->decompress(cseq,rnaFreeEnergy.getStructureStringRef().size());
00559
00560 if (str.size() != rnaFreeEnergy.getStructureStringRef().size())
00561 return NULL;
00562
00563 rnaFreeEnergy.setStructure(str);
00564
00565 return this;
00566 }
00567
00568
00569 }