Generated on Tue Dec 16 12:49:16 2008 for BIU-2.2.0 by doxygen 1.5.1

src/biu/LatticeProtein_Ipnt.cc

Go to the documentation of this file.
00001 
00002 #include "LatticeProtein_Ipnt.hh"
00003 #include <limits.h>
00004 
00005 
00006 namespace biu
00007 {
00008 
00009     // initialization of the indifferent value of double variables
00010     const double LatticeProtein_Ipnt::NAN_DOUBLE = (double)INT_MAX;
00011 
00012     // construction
00013     LatticeProtein_Ipnt::LatticeProtein_Ipnt(
00014                         const LatticeModel* lattice, 
00015                         const DistanceEnergyFunction* energyFunc, 
00016                         const Sequence* seq,
00017                         const bool seqShared,
00018                         const std::string& moveString, 
00019                         const bool isAbsoluteMove)
00020      :  LatticeProtein_I(lattice, energyFunc, seq, seqShared),
00021         points( new IPointVec() ), 
00022         energy(NAN_DOUBLE), 
00023         selfavoiding(MyNaN),
00024         connected(MyNaN) 
00025     {
00026         if (isAbsoluteMove) {
00027             *points = lattice->absMovesToPoints(
00028                             lattice->parseMoveString(moveString));
00029         } else {
00030             *points = lattice->relMovesToPoints(
00031                             lattice->parseMoveString(moveString));
00032         }
00033         assertbiu ( seq->size() == points->size(),
00034             "sequence and structure differ in size");
00035          // inform the object that the structure has changed
00036         updateProperties();
00037     }
00038     
00039     LatticeProtein_Ipnt::LatticeProtein_Ipnt(const biu::LatticeProtein& latPr) 
00040      :  LatticeProtein_I(latPr.getLatticeModel(), 
00041             latPr.getEnergyFunction(), 
00042             latPr.getSequenceRef(), 
00043             latPr.isSequenceShared()),   
00044         points( new IPointVec()),
00045         energy(NAN_DOUBLE), 
00046         selfavoiding(MyNaN),
00047         connected(MyNaN) 
00048     {
00049         assertbiu (lattice != NULL && energyFunc != NULL, 
00050             "no lattice model or energy function available");
00051             // check if data is based on points too
00052         const LatticeProtein_Ipnt* l2 = dynamic_cast<const LatticeProtein_Ipnt*>(&latPr);
00053         if (l2 != NULL) {   // direct point access possible
00054             points->resize(l2->getPointsRef()->size()); // resize vector
00055                 // fill vector
00056             std::copy<IPointVec::const_iterator, IPointVec::iterator>(
00057                 l2->getPointsRef()->begin(), l2->getPointsRef()->end(),
00058                 points->begin());
00059                 // copy maybe already calculated properties
00060             energy = l2->energy;
00061             selfavoiding = l2->selfavoiding;
00062             connected = l2->connected;
00063         } else {    // via copied objects
00064             *points = latPr.getPoints();
00065              // inform the object that the structure has changed
00066             updateProperties();
00067         }
00068     }
00069     
00070     LatticeProtein_Ipnt::LatticeProtein_Ipnt(const biu::LatticeProtein_Ipnt& latPr) 
00071      :  LatticeProtein_I(latPr), 
00072         points( new IPointVec(latPr.points->size())),
00073         energy(latPr.energy), 
00074         selfavoiding(latPr.selfavoiding),
00075         connected(latPr.connected) 
00076     {
00077         assertbiu (lattice != NULL && energyFunc != NULL, 
00078             "no lattice model or energy function available");
00079             // copy structure
00080         std::copy<IPointVec::const_iterator, IPointVec::iterator>(
00081                 latPr.points->begin(), latPr.points->end(),
00082                 points->begin());
00083     }
00084     
00085     LatticeProtein_I* LatticeProtein_Ipnt::clone() {
00086         return new LatticeProtein_Ipnt(*this);
00087     }
00088     
00089     LatticeProtein_I* LatticeProtein_Ipnt::fromString(const std::string& stringRep) const {
00090         size_t pos = stringRep.find_first_of("(");
00091         std::string absMoveStr = stringRep.substr(0, pos);
00092         std::string seqStr = stringRep.substr(pos+1, stringRep.size()-pos-2);
00093         biu::Sequence seq = energyFunc->getAlphabet()->getSequence(seqStr);
00094         const bool seqShared = false;
00095         const bool isAbsMove = true;
00096         return new LatticeProtein_Ipnt(lattice, energyFunc, &seq, seqShared, absMoveStr, isAbsMove);
00097     }
00098     
00099     LatticeProtein_Ipnt::~LatticeProtein_Ipnt()
00100     {
00101         if (points != NULL) {
00102             delete points;      points = NULL;
00103         }
00104     }
00105     
00106     LatticeProtein_I&
00107     LatticeProtein_Ipnt::operator =(const LatticeProtein_I& latProt2) {
00108         const LatticeProtein_Ipnt* l2 = dynamic_cast<const LatticeProtein_Ipnt*>(&latProt2);
00109         if (l2 != NULL) {   // direct point access possible
00110             return this->operator=(*l2);
00111         } else
00112             return LatticeProtein_I::operator=(latProt2);
00113     }
00114     
00115     LatticeProtein_Ipnt&
00116     LatticeProtein_Ipnt::operator =(const LatticeProtein_Ipnt& latProt2) {
00117         if (this != &latProt2 && *this != latProt2) {
00118             assertbiu(sequence != NULL && points != NULL,
00119                 "sequence and structure not initialized");
00120                 
00121                 // copy data
00122             lattice = latProt2.getLatticeModel();
00123             energyFunc = latProt2.getEnergyFunction();
00124             if (!seqShared) { // delete sequence if not shared
00125                 delete sequence;
00126             }
00127             seqShared = latProt2.isSequenceShared();
00128               // copy sequence
00129             if (seqShared) {
00130                 sequence = latProt2.getSequenceRef();
00131             } else {
00132                 sequence = new Sequence(*(latProt2.getSequenceRef()));
00133             }
00134             points->resize(latProt2.points->size());
00135             std::copy<IPointVec::const_iterator, IPointVec::iterator>(
00136                     latProt2.points->begin(), latProt2.points->end(),
00137                 points->begin());
00138                 // copy maybe already calculated properties
00139             energy = latProt2.energy;
00140             selfavoiding = latProt2.selfavoiding;
00141             connected = latProt2.connected;
00142              // inform the object that the structure has changed
00143             updateProperties();
00144         }
00145         return *this;
00146     }
00147     
00148     bool
00149     LatticeProtein_Ipnt::operator ==(const LatticeProtein& latProt2) const {
00150         assertbiu (lattice != NULL, "no lattice available");
00151         assertbiu (energyFunc != NULL, "no energy function available");
00152         assertbiu (sequence != NULL, "no sequence available");
00153         assertbiu (points != NULL, "no structure available");
00154         bool equal = 
00155                 (lattice == latProt2.getLatticeModel() 
00156                         || *lattice == *(latProt2.getLatticeModel()))
00157                 && (energyFunc == latProt2.getEnergyFunction()
00158                         || *energyFunc == *(latProt2.getEnergyFunction()))
00159                 && (seqShared == latProt2.isSequenceShared())
00160                 && ((seqShared && sequence == latProt2.getSequenceRef())
00161                         || *sequence == *(latProt2.getSequenceRef()));
00162         if (equal) {
00163                 // check if data is based on points too
00164             const LatticeProtein_Ipnt* l2 = dynamic_cast<const LatticeProtein_Ipnt*>(&latProt2);
00165             if (l2 != NULL) {   // direct point access possible
00166                 equal &= *points == *l2->points;
00167             } else {    // comparison based on relative move strings
00168                 equal &= lattice->pointsToRelMoves(*points) == latProt2.getMoveSeqRel();
00169             }
00170         }
00171         return equal;
00172     }
00173     
00174     bool
00175     LatticeProtein_Ipnt::operator !=(const LatticeProtein& latProt2) const {
00176         return  ! operator == (latProt2);
00177     }
00178     
00179     
00180     bool 
00181     LatticeProtein_Ipnt::isSelfAvoiding() const {
00182         assertbiu(points != NULL, "no structure available");
00183         
00184             // if selfavoidingness already tested
00185         if (selfavoiding != MyNaN) {
00186             return ( selfavoiding == MyTrue ? true : false );
00187         } 
00188         
00189         // else selfavoidingness has to be tested
00190         
00191             // check by set insertion to the cost of memory allocation
00192 //      IPointSet tmp;
00193             // pruefe via einfuegen, ob alle punkte unique sind
00194 //      for (IPointVec::const_iterator it = pData->begin();  
00195 //              it != pData->end(); it++) {
00196 //          if (tmp.insert(*it).second == false) // already inside (not inserted)
00197 //              return false;   // not selfavoiding
00198 //      }
00199         
00200             // checking via (n^2)/2 comparisons  ==  inplace  
00201             // (faster than insertion for small structures)
00202         for (size_t k=0; k<points->size(); k++) {
00203             for (size_t l=k+1; l<points->size(); l++) {
00204                 if (points->at(k) == points->at(l)) {
00205                     selfavoiding = MyFalse;
00206                     return false;
00207                 }
00208             }
00209         }
00210         selfavoiding = MyTrue;
00211         return true;
00212     }
00213 
00214     // abstract functions 
00215     
00216     DPointVec   
00217     LatticeProtein_Ipnt::get3Ddata() const  {
00218         assertbiu (points != NULL, "no structure available");
00219             // generate return vector
00220         DPointVec dVec(points->size());
00221             // fill return vector via copy and type cast
00222         std::copy<IPointVec::const_iterator, DPointVec::iterator> (
00223                 points->begin(), points->end(),
00224                 dVec.begin() );
00225         return dVec;
00226     }
00227     
00228     double 
00229     LatticeProtein_Ipnt::getDRMSD(const BackboneStructure3D& other) const {
00230         DPointVec this3Ddata = get3Ddata(), other3Ddata = other.get3Ddata();
00231 
00232         assertbiu(this3Ddata.size() == other3Ddata.size(),
00233          "Both lattice proteins have to have the same length.");
00234 
00235         double rmsd = 0.0;
00236 
00237         for(size_t i=0; i < this3Ddata.size()-1; i++) {
00238             for(size_t j=i+1; j < this3Ddata.size(); j++) {
00239                 rmsd += std::pow( this3Ddata[i].distance(this3Ddata[j])
00240                          - other3Ddata[i].distance(other3Ddata[j]), 2 );
00241             }
00242         }
00243 
00244         rmsd /= (this3Ddata.size() * (this3Ddata.size()-1) /2);
00245 
00246         return std::sqrt(rmsd);
00247 
00248     }
00249 
00250     double  
00251     LatticeProtein_Ipnt::getEnergy() const {
00252         if (energy == NAN_DOUBLE) {
00253             assertbiu(lattice != NULL, "no lattice model available");
00254             assertbiu(energyFunc != NULL, "no energy function available");
00255             energy = 0.0;
00256             IPointVec::size_type i=0,j=0;
00257             for (i=0; i < points->size(); i++) {
00258                 for (j = i+2; j < points->size(); j++) {
00259                       // distance based energy evaluation
00260                     energy += energyFunc->getEnergy(    sequence->at(i),
00261                                                         sequence->at(j),
00262                                                         points->at(i), 
00263                                                         points->at(j));
00264                 }
00265             }
00266         }
00267         return energy;
00268     }
00269     
00272     Structure   
00273     LatticeProtein_Ipnt::getStructure() const {
00274         assertbiu(lattice != NULL, "no lattice model available");
00275         return lattice->pointsToRelMoves(*points);
00276     }
00277     
00282     std::string 
00283     LatticeProtein_Ipnt::getStringRepresentation() const {
00284         assertbiu(energyFunc != NULL, "no energy function available");
00285         
00286         return  getMoveStrAbs()
00287                 + std::string("(")
00288                 + energyFunc->getAlphabet()->getString(*sequence)
00289                 + std::string(")");
00290     }
00291     
00292     
00293     IPointVec 
00294     LatticeProtein_Ipnt::getPoints() const {
00295         assertbiu(points != NULL, "no structure available");
00296         return *points;
00297     }
00298     
00299     const IPointVec* const
00300     LatticeProtein_Ipnt::getPointsRef() const {
00301         return points;
00302     }
00303     
00304     IPointVec*
00305     LatticeProtein_Ipnt::getPointsRef() {
00306         updateProperties();
00307         return points;
00308     }
00309     
00310 
00311     bool        
00312     LatticeProtein_Ipnt::isValid() const {
00313         return isConnected() && isSelfAvoiding();
00314     }
00315 
00318     std::string 
00319     LatticeProtein_Ipnt::getMoveStrAbs() const {
00320         assertbiu(lattice != NULL, "no lattice model available");
00321         return lattice->getString(lattice->pointsToAbsMoves(*points));
00322     }
00323     
00325     void
00326     LatticeProtein_Ipnt::setMoveStrAbs(const std::string& moveString) {
00327         *points = lattice->absMovesToPoints(
00328                             lattice->parseMoveString(moveString));
00329         assertbiu ( sequence->size() == points->size(),
00330             "sequence and structure differ in size");
00331          // inform the object that the structure has changed
00332         updateProperties();
00333     }
00334     
00337     std::string 
00338     LatticeProtein_Ipnt::getMoveStrRel() const {
00339         assertbiu(lattice != NULL, "no lattice model available");
00340         return lattice->getString(lattice->pointsToRelMoves(*points));
00341     }
00342     
00345     MoveSequence 
00346     LatticeProtein_Ipnt::getMoveSeqAbs() const {
00347         assertbiu(lattice != NULL, "no lattice model available");
00348         return lattice->pointsToAbsMoves(*points);
00349     }
00350     
00353     MoveSequence 
00354     LatticeProtein_Ipnt::getMoveSeqRel() const {
00355         assertbiu(lattice != NULL, "no lattice model available");
00356         return lattice->pointsToRelMoves(*points);
00357     }
00358     
00361     bool    
00362     LatticeProtein_Ipnt::isConnected() const {
00363         assertbiu(points != NULL, "no structure available");
00364         assertbiu(lattice != NULL, "no lattice model available");
00365         
00366         if (connected != MyNaN ) {
00367             return ( connected == MyTrue ? true : false );
00368         }
00369             // check if there are some unneighbored successive points
00370         for(size_t i=1; i<points->size(); i++) {
00371             if (! lattice->areNeighbored(points->at(i-1),points->at(i)) ) { 
00372                 connected = MyFalse;
00373                 return false;
00374             }
00375         }
00376             // everything all right .. ;)
00377         connected = MyTrue;
00378         return true;
00379     }
00380     
00381     
00382     
00383         // additional functions (LatticeProtein_Ipnt)
00384     
00385     // Sets all calculated properties (energy, selfavoidingness and 
00386     // connectedness to an indifferent state.
00387     void
00388     LatticeProtein_Ipnt::updateProperties() {
00389             // set all properties that are calculated on demand to an 
00390             // indifferent state
00391         energy = NAN_DOUBLE;
00392         selfavoiding = MyNaN;
00393         connected = MyNaN;
00394     }
00395 
00396 }