LCOV - code coverage report
Current view: top level - core - SetupMolInfo.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 94 110 85.5 %
Date: 2018-12-19 07:49:13 Functions: 11 13 84.6 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2012-2018 The plumed team
       3             :    (see the PEOPLE file at the root of the distribution for a list of names)
       4             : 
       5             :    See http://www.plumed.org for more information.
       6             : 
       7             :    This file is part of plumed, version 2.
       8             : 
       9             :    plumed is free software: you can redistribute it and/or modify
      10             :    it under the terms of the GNU Lesser General Public License as published by
      11             :    the Free Software Foundation, either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    plumed is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU Lesser General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU Lesser General Public License
      20             :    along with plumed.  If not, see <http://www.gnu.org/licenses/>.
      21             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      22             : #include "SetupMolInfo.h"
      23             : #include "Atoms.h"
      24             : #include "ActionRegister.h"
      25             : #include "ActionSet.h"
      26             : #include "PlumedMain.h"
      27             : #include "tools/MolDataClass.h"
      28             : #include "tools/PDB.h"
      29             : 
      30             : 
      31             : namespace PLMD {
      32             : 
      33             : 
      34             : /*
      35             : This action is defined in core/ as it is used by other actions.
      36             : Anyway, it is registered in setup/, so that excluding that module from
      37             : compilation will exclude it from plumed.
      38             : */
      39             : 
      40             : 
      41          22 : void SetupMolInfo::registerKeywords( Keywords& keys ) {
      42          22 :   ActionSetup::registerKeywords(keys);
      43             :   keys.add("compulsory","STRUCTURE","a file in pdb format containing a reference structure. "
      44             :            "This is used to defines the atoms in the various residues, chains, etc . "
      45          22 :            "For more details on the PDB file format visit http://www.wwpdb.org/docs.html");
      46          22 :   keys.add("compulsory","MOLTYPE","protein","what kind of molecule is contained in the pdb file - usually not needed since protein/RNA/DNA are compatible");
      47          22 :   keys.add("atoms","CHAIN","(for masochists ( mostly Davide Branduardi ) ) The atoms involved in each of the chains of interest in the structure.");
      48          22 : }
      49             : 
      50          84 : SetupMolInfo::~SetupMolInfo() {
      51          21 :   delete &pdb;
      52          63 : }
      53             : 
      54          21 : SetupMolInfo::SetupMolInfo( const ActionOptions&ao ):
      55             :   Action(ao),
      56             :   ActionSetup(ao),
      57             :   ActionAtomistic(ao),
      58          21 :   pdb(*new(PDB))
      59             : {
      60             :   // Read what is contained in the pdb file
      61          21 :   parse("MOLTYPE",mytype);
      62             : 
      63          21 :   std::vector<SetupMolInfo*> moldat=plumed.getActionSet().select<SetupMolInfo*>();
      64          21 :   if( moldat.size()!=0 ) error("cannot use more than one MOLINFO action in input");
      65             : 
      66          42 :   std::vector<AtomNumber> backbone;
      67          21 :   parseAtomList("CHAIN",backbone);
      68          21 :   if( read_backbone.size()==0 ) {
      69          21 :     for(unsigned i=1;; ++i) {
      70          21 :       parseAtomList("CHAIN",i,backbone);
      71          21 :       if( backbone.size()==0 ) break;
      72           0 :       read_backbone.push_back(backbone);
      73           0 :       backbone.resize(0);
      74           0 :     }
      75             :   } else {
      76           0 :     read_backbone.push_back(backbone);
      77             :   }
      78          21 :   if( read_backbone.size()==0 ) {
      79          21 :     std::string reference; parse("STRUCTURE",reference);
      80             : 
      81          21 :     if( ! pdb.read(reference,plumed.getAtoms().usingNaturalUnits(),0.1/plumed.getAtoms().getUnits().getLength()))plumed_merror("missing input file " + reference );
      82             : 
      83          42 :     std::vector<std::string> chains; pdb.getChainNames( chains );
      84          21 :     log.printf("  pdb file named %s contains %u chains \n",reference.c_str(), static_cast<unsigned>(chains.size()));
      85          94 :     for(unsigned i=0; i<chains.size(); ++i) {
      86          73 :       unsigned start,end; std::string errmsg;
      87          73 :       pdb.getResidueRange( chains[i], start, end, errmsg );
      88          73 :       if( errmsg.length()!=0 ) error( errmsg );
      89          73 :       AtomNumber astart,aend;
      90          73 :       pdb.getAtomRange( chains[i], astart, aend, errmsg );
      91          73 :       if( errmsg.length()!=0 ) error( errmsg );
      92          73 :       log.printf("  chain named %s contains residues %u to %u and atoms %u to %u \n",chains[i].c_str(),start,end,astart.serial(),aend.serial());
      93          94 :     }
      94          21 :   }
      95          21 : }
      96             : 
      97          25 : void SetupMolInfo::getBackbone( std::vector<std::string>& restrings, const std::string& fortype, std::vector< std::vector<AtomNumber> >& backbone ) {
      98          25 :   if( fortype!=mytype ) error("cannot calculate a variable designed for " + fortype + " molecules for molecule type " + mytype );
      99          25 :   if( MolDataClass::numberOfAtomsPerResidueInBackbone( mytype )==0 ) error("backbone is not defined for molecule type " + mytype );
     100             : 
     101          25 :   if( read_backbone.size()!=0 ) {
     102           0 :     if( restrings.size()!=1 ) error("cannot interpret anything other than all for residues when using CHAIN keywords");
     103           0 :     if( restrings[0]!="all" ) error("cannot interpret anything other than all for residues when using CHAIN keywords");
     104           0 :     backbone.resize( read_backbone.size() );
     105           0 :     for(unsigned i=0; i<read_backbone.size(); ++i) {
     106           0 :       backbone[i].resize( read_backbone[i].size() );
     107           0 :       for(unsigned j=0; j<read_backbone[i].size(); ++j) backbone[i][j]=read_backbone[i][j];
     108             :     }
     109             :   } else {
     110          25 :     bool useter=false; // This is used to deal with terminal groups in WHOLEMOLECULES
     111          25 :     if( restrings.size()==1 ) {
     112          23 :       useter=( restrings[0].find("ter")!=std::string::npos );
     113          23 :       if( restrings[0].find("all")!=std::string::npos ) {
     114          21 :         std::vector<std::string> chains; pdb.getChainNames( chains );
     115         348 :         for(unsigned i=0; i<chains.size(); ++i) {
     116         654 :           unsigned r_start, r_end; std::string errmsg, mm, nn;
     117         327 :           pdb.getResidueRange( chains[i], r_start, r_end, errmsg );
     118         327 :           if( !useter ) {
     119         327 :             std::string resname = pdb.getResidueName( r_start );
     120         327 :             if( MolDataClass::isTerminalGroup( mytype, resname ) ) r_start++;
     121         327 :             resname = pdb.getResidueName( r_end );
     122         327 :             if( MolDataClass::isTerminalGroup( mytype, resname ) ) r_end--;
     123             :           }
     124         327 :           Tools::convert(r_start,mm); Tools::convert(r_end,nn);
     125         327 :           if(i==0) restrings[0] = mm + "-" + nn;
     126         306 :           else restrings.push_back(  mm + "-" + nn );
     127         348 :         }
     128             :       }
     129             :     }
     130          25 :     Tools::interpretRanges(restrings);
     131             : 
     132             :     // Convert the list of involved residues into a list of segments of chains
     133          25 :     int nk, nj; std::vector< std::vector<unsigned> > segments;
     134          50 :     std::vector<unsigned> thissegment;
     135          25 :     Tools::convert(restrings[0],nk); thissegment.push_back(nk);
     136        2652 :     for(unsigned i=1; i<restrings.size(); ++i) {
     137        2627 :       Tools::convert(restrings[i-1],nk);
     138        2627 :       Tools::convert(restrings[i],nj);
     139        2627 :       if( (nk+1)!=nj || pdb.getChainID(nk)!=pdb.getChainID(nj) ) {
     140         308 :         segments.push_back(thissegment);
     141         308 :         thissegment.resize(0);
     142             :       }
     143        2627 :       thissegment.push_back(nj);
     144             :     }
     145          25 :     segments.push_back( thissegment );
     146             : 
     147             :     // And now get the backbone atoms from each segment
     148          25 :     backbone.resize( segments.size() );
     149          50 :     std::vector<AtomNumber> atomnumbers;
     150         358 :     for(unsigned i=0; i<segments.size(); ++i) {
     151        2985 :       for(unsigned j=0; j<segments[i].size(); ++j) {
     152        2652 :         std::string resname=pdb.getResidueName( segments[i][j] );
     153        2652 :         if( !MolDataClass::allowedResidue(mytype, resname) ) {
     154           0 :           std::string num; Tools::convert( segments[i][j], num );
     155           0 :           error("residue " + num + " is not recognized for moltype " + mytype );
     156             :         }
     157        2652 :         if( !useter && MolDataClass::isTerminalGroup( mytype, resname ) ) {
     158           0 :           std::string num; Tools::convert( segments[i][j], num );
     159           0 :           error("residue " + num + " appears to be a terminal group");
     160             :         }
     161        2652 :         if( resname=="GLY" ) warning("GLY residues are achiral - assuming HA1 atom is in CB position");
     162        2652 :         MolDataClass::getBackboneForResidue( mytype, segments[i][j], pdb, atomnumbers );
     163        2652 :         if( atomnumbers.size()==0 ) {
     164           0 :           std::string num; Tools::convert( segments[i][j], num );
     165           0 :           error("Could not find required backbone atom in residue number " + num );
     166             :         } else {
     167        2652 :           for(unsigned k=0; k<atomnumbers.size(); ++k) backbone[i].push_back( atomnumbers[k] );
     168             :         }
     169        2652 :         atomnumbers.resize(0);
     170        2652 :       }
     171          25 :     }
     172             :   }
     173          25 : }
     174             : 
     175         364 : void SetupMolInfo::interpretSymbol( const std::string& symbol, std::vector<AtomNumber>& atoms )const {
     176         364 :   MolDataClass::specialSymbol( mytype, symbol, pdb, atoms );
     177         364 : }
     178             : 
     179        2798 : std::string SetupMolInfo::getAtomName(AtomNumber a)const {
     180        2798 :   return pdb.getAtomName(a);
     181             : }
     182             : 
     183        2798 : unsigned SetupMolInfo::getResidueNumber(AtomNumber a)const {
     184        2798 :   return pdb.getResidueNumber(a);
     185             : }
     186             : 
     187        2798 : std::string SetupMolInfo::getResidueName(AtomNumber a)const {
     188        2798 :   return pdb.getResidueName(a);
     189             : }
     190             : 
     191        2523 : }

Generated by: LCOV version 1.13