fragmentDB.h

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // $Id: fragmentDB.h,v 1.35.18.1 2007/03/25 21:25:25 oliver Exp $
00005 //
00006 
00007 #ifndef BALL_STRUCTURE_FRAGMENTDB_H
00008 #define BALL_STRUCTURE_FRAGMENTDB_H
00009 
00010 #ifndef BALL_DATATYPE_STRINGHASHMAP_H
00011 # include <BALL/DATATYPE/stringHashMap.h>
00012 #endif
00013 
00014 #ifndef BALL_KERNEL_RESIDUE_H
00015 # include <BALL/KERNEL/residue.h>
00016 #endif
00017 
00018 #ifndef BALL_KERNEL_MOLECULE_H
00019 # include <BALL/KERNEL/molecule.h>
00020 #endif
00021 
00022 #ifndef BALL_KERNEL_BOND_H
00023 # include <BALL/KERNEL/bond.h>
00024 #endif
00025 
00026 #ifndef BALL_STRUCTURE_RECONSTRUCTFRAGMENTPROCESSOR_H
00027 # include <BALL/STRUCTURE/reconstructFragmentProcessor.h>
00028 #endif
00029 
00030 #include <vector>
00031 #include <list>
00032 
00033 namespace BALL 
00034 {
00035 
00036   class ResourceEntry;
00037 
00046   class BALL_EXPORT FragmentDB 
00047   {
00048     public:
00049 
00050     class AddHydrogensProcessor;
00051     friend class FragmentDB::AddHydrogensProcessor;
00052 
00053     BALL_CREATE_DEEP(FragmentDB)
00054   
00055     
00058 
00061     enum FragmentTypes 
00062     {
00063       TYPE__UNKNOWN = -1,
00064       TYPE__FRAGMENT,
00065       TYPE__RESIDUE,
00066       TYPE__MOLECULE
00067     };
00069 
00073 
00076     typedef short Type;
00078     typedef StringHashMap<String> NameMap;
00079 
00081 
00084     
00089     class BALL_EXPORT NoFragmentNode
00090       : public Exception::GeneralException
00091     {
00092       public:
00093       NoFragmentNode(const char* file, int line, const string& filename) ;
00094       ~NoFragmentNode() throw() {}
00095 
00096       protected:
00097       string filename_;
00098     };
00099 
00101 
00104   
00107     FragmentDB();
00108 
00113     FragmentDB(const String& filename)
00114       throw(Exception::FileNotFound);
00115 
00118     FragmentDB(const FragmentDB& db, bool deep = true); 
00119 
00121     FragmentDB& operator = (const FragmentDB& db)
00122       ;
00123 
00126     virtual ~FragmentDB();
00127 
00130     void destroy();
00131 
00133 
00136 
00139     void init()
00140       throw(Exception::FileNotFound, NoFragmentNode);
00141     
00143 
00146 
00149     void setFilename(const String& filename)
00150       throw(Exception::FileNotFound);
00151     
00154     const String& getFilename() const;
00155     
00158     bool has(const String& fragment_name) const;
00159 
00161     const std::vector<Residue*>&  getFragments() { return fragments_;}
00162     
00165     FragmentDB::Type getFragmentType(const String& fragment_name) const;
00166 
00169     list<String> getVariantNames(const String& name) const;
00170     
00173     const Fragment* getFragment(const String& fragment_name) const;
00174 
00181     const Fragment* getReferenceFragment(const Fragment& fragment) const;
00182 
00185     const Residue* getResidue(const String& fragment_name) const;
00186 
00195     Fragment* getFragmentCopy(const String& fragment_name) const;
00196 
00205     Molecule* getMoleculeCopy(const String& fragment_name) const;
00206 
00215     Residue* getResidueCopy(const String& fragment_name) const;
00216 
00219     const String& getDefaultNamingStandard() const;
00220 
00223     StringHashMap<NameMap>& getNamingStandards();
00224 
00231     const StringHashMap<String>& getNamingStandard(const String& std) const
00232       throw(StringHashMap<String>::IllegalKey);
00233 
00237     std::vector<String> getAvailableNamingStandards() const;
00238 
00240 
00244 
00247     bool isValid() const;
00248 
00250 
00253 
00258     class BALL_EXPORT NormalizeNamesProcessor 
00259       : public UnaryProcessor<Fragment>
00260     {
00261     
00262       public:
00263 
00267 
00270       NormalizeNamesProcessor();
00271     
00272     
00275       NormalizeNamesProcessor(FragmentDB& db);
00276 
00279       virtual ~NormalizeNamesProcessor();
00280     
00282 
00285       
00288       void setFragmentDB(FragmentDB& db);
00289 
00292       void setNamingStandard(const String& naming_standard);
00293     
00296       const String& getNamingStandard();
00297 
00300       bool matchName(String& res_name, String& atom_name, const NameMap& map) const;
00301    
00303 
00306       
00309       virtual bool start();
00310 
00313       virtual bool finish();
00314     
00317       virtual Processor::Result operator () (Fragment& fragment);
00318 
00320 
00321       private:
00322         enum CountingMode { ADD, OVERWRITE };
00323         String getSuffix_(const Fragment* frag) const;
00324         bool doMatch_(String& res_name, const String& res_name_suffix, String& atom_name, const NameMap& map) const;
00325         void countHits_(HashMap<NameMap*, Index>& maps, const std::list<Fragment*>& frags);
00326         void countHits_(HashMap<NameMap*, Index>& maps, const Fragment* frag, CountingMode mode = OVERWRITE);
00327         const NameMap* getBestMap_(const HashMap<NameMap*, Index>& maps) const;
00328         void normalizeFragments_(const NameMap* map, const std::list<Fragment*>& frags);
00329         void normalizeFragment_ (const NameMap* map, Fragment* frag);
00330 
00331       String                naming_standard_;
00332 
00333       FragmentDB*           fragment_db_;
00334     
00335       std::list<Fragment*>  fragments_;
00336 
00337     };
00338 
00339 
00342     class BALL_EXPORT BuildBondsProcessor 
00343       : public UnaryProcessor<Fragment> 
00344     {
00345 
00346       public:
00347 
00351 
00352       struct Connection
00353       {
00354         Atom*       atom;
00355         String      type_name;
00356         String      connect_to;
00357         Bond::Order order;
00358         float       dist;
00359         float       delta;
00360       };
00361 
00363       typedef std::list<Connection> ConnectionList;
00365 
00366 
00370     
00372       BuildBondsProcessor();
00373       
00375       BuildBondsProcessor(const FragmentDB& db);
00376 
00378       virtual ~BuildBondsProcessor();
00380 
00384 
00386       virtual bool finish();
00387 
00389       virtual bool start();
00390 
00392       virtual Processor::Result operator () (Fragment& fragment);
00394 
00398       
00400       Size getNumberOfBondsBuilt();
00401 
00403       void setFragmentDB(const FragmentDB& fragment_db);
00404 
00406 
00410 
00416       Size buildFragmentBonds(Fragment& fragment) const;
00417 
00424       Size buildFragmentBonds(Fragment& fragment, const Fragment& tplate) const
00425         throw(Exception::TooManyBonds);
00426 
00432       Size buildInterFragmentBonds(Fragment& first, Fragment& second) const
00433         throw(Exception::TooManyBonds);
00434 
00436 
00437       protected:
00438 
00444       void storeConnections_(Fragment& fragment);
00445 
00448       bool buildConnection_(Connection& con1, Connection& con2)
00449         throw(Exception::TooManyBonds);
00450     
00453       FragmentDB*     fragment_db_;
00454       
00459       std::list<Fragment*>  fragment_list_;
00460 
00461       /*_ The number of bonds built.
00462           This value is reset in the start method, so each application of 
00463           the processor, so <TT>  getNumberOfBuiltBonds </TT> always returns
00464           the number of bonds built in the last application.
00465       */
00466       Size  bonds_built_;
00467 
00468       /*_ The list of connects between(!) fragments still to be examined
00469       */
00470       ConnectionList  connections_;
00471     };
00472 
00474 
00477 
00480     NormalizeNamesProcessor   normalize_names;
00481 
00484     ReconstructFragmentProcessor  add_hydrogens;
00485 
00488     BuildBondsProcessor       build_bonds;
00489 
00492     ResourceEntry*  tree;
00493 
00495 
00496     private:
00497       
00498     /*_ @name Parse functions
00499         These functions parse different sections of the fragment DB resource
00500         tree and translate everything into the correct data structures.
00501     */
00502     //_@{
00503 
00504     /*_ Parses the Atoms entry and creates the atoms of the fragment
00505     */
00506     void parseAtoms_(ResourceEntry& entry, Fragment& fragment);
00507 
00508     /*_ Parses the Bonds entry and creates the bonds of the fragment
00509     */
00510     void parseBonds_(ResourceEntry& entry, Fragment& fragment);
00511 
00512     /*_ Parses the properties of a fragment/variant and sets the corresponding properties.
00513         Properties are set or reset (if the property name starts with "!") for the current 
00514         fragment. All properties of fragment and residue are recognized, if they
00515         are written exactly as in the header file (case insensitive) and set.
00516         Unknown properties are set as name/value pairs as bool properties and set to 
00517         <b>  true </b>.
00518     */
00519     void parseProperties_(ResourceEntry& entry, PropertyManager& property_man);
00520 
00521     /*_ Parses the Delete section.
00522         All atoms given in this section are removed from the fragment.
00523     */
00524     void parseDelete_(ResourceEntry& entry, Fragment& fragment);
00525 
00526     /*_ Parses the Rename section.
00527         All atoms given in this section are renamed to the given new name.
00528     */
00529     void parseRename_(ResourceEntry& entry, Fragment& fragment);
00530 
00531     //_@}
00532 
00534     Position addNewFragment_(Residue* fragment);
00535 
00536     /*_ Expands all include directives in the resource file.
00537         This method calls expandFirst_ until it returns true. 
00538     */
00539     void expandTree_(ResourceEntry& root_entry);
00540    
00541     /*_ Expands the first occuring include directive.
00542         If no include directive is found, <b>  false </b> is returned, otherwise <b>  true </b>.
00543     */
00544     bool expandFirst_(ResourceEntry& root_entry)
00545       throw(Exception::FileNotFound);
00546 
00547     // The status of the FragmentDB
00548     bool            valid_;
00549 
00550     // The filename of the master fragment file.
00551     String          filename_;
00552 
00553     // The naming standard we default to.
00554     String          default_standard_;
00555 
00556     // An array containing all allocated residues.
00557     std::vector<Residue*>           fragments_;
00558 
00559     // Maps a fragment name back to a path in the database
00560     NameMap                         name_to_path_;
00561 
00562     // Maps a fragment name back to the array index in fragments_
00563     StringHashMap<Position>         name_to_frag_index_;
00564 
00565     // Maps all variants of a specific fragment back to array indices.
00566     StringHashMap<list<Position> >  name_to_variants_;
00567 
00568     // Contains the naming standards as a nested map.
00569     StringHashMap<NameMap>          standards_;
00570   };
00571   
00572 } // namespace BALL 
00573 
00574 
00575 #endif // BALL_STRUCTURE_FRAGMENTDB_H