coarse_grain.h
Go to the documentation of this file.
00001 // Copyright (c) 2009-2010  INRIA Sophia-Antipolis (France).
00002 // All rights reserved.
00003 //
00004 //This file is part of ESBTL.
00005 //
00006 //ESBTL is free software: you can redistribute it and/or modify
00007 //it under the terms of the GNU General Public License as published by
00008 //the Free Software Foundation, either version 3 of the License, or
00009 //(at your option) any later version.
00010 //
00011 //ESBTL is distributed in the hope that it will be useful,
00012 //but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //GNU General Public License for more details.
00015 //
00016 //You should have received a copy of the GNU General Public License
00017 //along with ESBTL.  If not, see <http://www.gnu.org/licenses/>.
00018 //
00019 //
00020 //Additional permission under GNU GPL version 3 section 7
00021 //
00022 //If you modify this Library, or any covered work, by linking or
00023 //combining it with CGAL (or a modified version of that library), the
00024 //licensors of this Library grant you additional permission to convey
00025 //the resulting work. Corresponding Source for a non-source form of
00026 //such a combination shall include the source code for the parts of CGAL
00027 //used as well as that of the covered work. 
00028 //
00029 //
00030 //
00031 // Author(s)     :  Sébastien Loriot
00032 
00033 
00034 
00035 #ifndef ESBTL_COARSE_GRAIN_H
00036 #define ESBTL_COARSE_GRAIN_H
00037 
00038 #include <ESBTL/molecular_system.h>
00039 
00040 namespace ESBTL{
00041 
00046 template <class Atom,class Point>
00047 class Coarse_atom: public Point{
00048   typedef typename Atom::System::Residue Residue;
00049   //list of atoms that contributes to the coarse atom
00050   std::list<const Atom*> atoms_;
00051   unsigned index_;
00052   const Residue* residue_;
00053   
00054 public:
00055   typedef Point Point_3;
00056 
00058   void add(const Atom& atom){atoms_.push_back(&atom);}
00059   
00060   Coarse_atom(const Point_3& p,unsigned i,const Residue& res):Point_3(p),index_(i),residue_(&res){}
00061   Coarse_atom(unsigned i,const Residue& res):Point_3(),index_(i),residue_(&res){}
00062     
00063   Coarse_atom():Point_3(),index_(0),residue_(NULL){}
00064   Coarse_atom(float x,float y,float z):Point_3(x,y,z),index_(0),residue_(NULL){}    
00065   
00066   const Residue& residue() const {return *residue_;}
00067   
00068   unsigned index() const {return index_;}
00069 };
00070 
00076 template <class Residue,class Chain,class Coarse_atom_>
00077 class Coarse_residue:public Residue{
00078   std::vector<Coarse_atom_> coarse_atoms_container;
00079   typedef Coarse_residue<Residue,Chain,Coarse_atom_> Self;
00080 public:
00081   typedef std::vector<Coarse_atom_> Coarse_atom_container;
00082   typedef Coarse_atom_ Coarse_atom;
00083   typedef typename Residue::Atom  Atom;
00084 
00085   template<class Line_format>
00086   Coarse_residue(const Line_format& line_format, const std::string& line,const Chain& ch):
00087     Residue(line_format,line,ch){}
00088   Coarse_residue(const std::string& resname,int index,char insc,const Chain& ch):
00089     Residue(resname,index,insc,ch){}
00090 
00091       
00097   template <class Coarse_creator>
00098   int create_coarse_atoms(const Coarse_creator& creator){
00099     return creator(*this,std::back_inserter(coarse_atoms_container));
00100   }
00101 
00107   void add_coarse_atom(const typename Residue::Atom::Point_3& pt,unsigned i=0){
00108     this->coarse_atoms_container.push_back(Coarse_atom(pt,i,*this));
00109   }
00110   
00111   const Coarse_atom get_coarse_atom(unsigned i) const {
00112     return coarse_atoms_container[i];
00113   }
00114   
00115   //TODO find another way to to this except a simple copy from the base class
00116   //iterators
00117   //---------
00118   //~ //functions needed by generic iterator in iterators.h 
00119   //~ static const Atom& dereference (typename Atom_container::const_iterator it) {return it->second;}
00120   //~ static Atom& dereference (typename Atom_container::iterator it) {return it->second;}
00122   typedef internal::Atoms_iterator_from_residue<Self,true>  Atoms_const_iterator;
00124   typedef internal::Atoms_iterator_from_residue<Self,false> Atoms_iterator;
00125   
00127   Atoms_iterator atoms_begin() {return Atoms_iterator(this->atom_container_.begin(),this->atom_container_.end());}
00129   Atoms_iterator atoms_end()   {return Atoms_iterator(this->atom_container_.end());}  
00130 
00132   Atoms_const_iterator atoms_begin() const {return Atoms_const_iterator(this->atom_container_.begin(),this->atom_container_.end());}
00134   Atoms_const_iterator atoms_end()   const {return Atoms_const_iterator(this->atom_container_.end());}
00135   //-------    
00137   typedef typename std::vector<Coarse_atom_>::const_iterator Coarse_atom_const_iterator;
00139   typedef typename std::vector<Coarse_atom_>::iterator Coarse_atom_iterator;
00141   Coarse_atom_const_iterator coarse_atoms_begin() const {return coarse_atoms_container.begin();}
00143   Coarse_atom_const_iterator coarse_atoms_end()   const {return coarse_atoms_container.end();}
00145   Coarse_atom_iterator coarse_atoms_begin() {return coarse_atoms_container.begin();}
00147   Coarse_atom_iterator coarse_atoms_end()   {return coarse_atoms_container.end();}  
00148   
00149   using Residue::dereference;
00150   static const Coarse_atom& dereference(Coarse_atom_const_iterator it){ return *it;};
00151   static Coarse_atom& dereference(Coarse_atom_iterator it){ return *it;};
00152 };
00153 
00154 
00168 template<class Input_iterator,class System>
00169 void insert_coarse_atoms(Input_iterator first,Input_iterator last,System& system,int modelid=1,char chainid='Z',std::string resname="SOL",int starting_res_index=1){
00170   typename System::Model& model=system.get_or_create_model(modelid);
00171   typename System::Chain& chain=model.get_or_create_chain(chainid);
00172   int res_index=starting_res_index-1;
00173   char insc=' ';
00174   for (Input_iterator it=first; it!=last;++it){
00175     typename System::Residue& residue=chain.get_or_create_residue(resname,++res_index,insc);
00176     residue.add_coarse_atom(*it,0);
00177   }
00178 }
00179 
00180 
00185 template <class Model>
00186 struct Coarse_atoms_iterators{
00187   typedef internal::Coarse_atom_from_model<Model,true>  const_iterator;
00188   typedef internal::Coarse_atom_from_model<Model,false>  iterator;
00189 };
00190 
00193 template <class Model>
00194 typename Coarse_atoms_iterators<Model>::const_iterator coarse_atoms_begin(const Model& model){
00195   return typename Coarse_atoms_iterators<Model>::const_iterator(model);
00196 }
00197 
00200 template <class Model>
00201 typename Coarse_atoms_iterators<Model>::const_iterator coarse_atoms_end(const Model& model){
00202   return typename Coarse_atoms_iterators<Model>::const_iterator(model,true);
00203 }
00204 
00207 template <class Model>
00208 typename Coarse_atoms_iterators<Model>::iterator  coarse_atoms_begin(Model& model){
00209   return typename Coarse_atoms_iterators<Model>::iterator(model);
00210 }
00211 
00214 template <class Model>
00215 typename Coarse_atoms_iterators<Model>::iterator coarse_atoms_end(Model& model){
00216   return typename Coarse_atoms_iterators<Model>::iterator(model,true);
00217 }
00218 
00224 struct System_items_with_coarse_grain{
00225   template <class System,class Point_3>
00226   struct Model_wrapper{
00227     typedef Molecular_model<System>         Type;
00228   };
00229   
00230   template <class System,class Point_3>
00231   struct Chain_wrapper{
00232     typedef Molecular_chain<System>         Type;
00233   };
00234   
00235   template <class System,class Point_3>
00236   struct Atom_wrapper{
00237     typedef Molecular_atom<System,Point_3>  Type;
00238   };  
00239   
00240   template <class System,class Point_3>
00241   class Residue_wrapper{
00242     typedef Molecular_residue<System>         Residue;
00243     typedef Molecular_atom<System,Point_3>    Atom;
00244     typedef Molecular_chain<System>           Chain;
00245   public:
00246     typedef Coarse_residue<Residue,Chain,Coarse_atom<Atom,Point_3> > Type;
00247   };
00248   
00249 };
00250 
00251 }//namespace ESBTL
00252 
00253 #endif //ESBTL_COARSE_GRAIN_H