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