All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SetupMolInfo.cpp
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 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-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
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/PDB.h"
28 
29 
30 namespace PLMD {
31 
32 
33 /*
34 This action is defined in core/ as it is used by other actions.
35 Anyway, it is registered in setup/, so that excluding that module from
36 compilation will exclude it from plumed.
37 */
38 
39 
42  keys.add("compulsory","STRUCTURE","a file in pdb format containing a reference structure. "
43  "This is used to defines the atoms in the various residues, chains, etc . "
44  "For more details on the PDB file format visit http://www.wwpdb.org/docs.html");
45  keys.add("atoms","CHAIN","(for masochists ( mostly Davide Branduardi ) ) The atoms involved in each of the chains of interest in the structure.");
46 }
47 
49  delete &pdb;
50 }
51 
53 Action(ao),
54 ActionSetup(ao),
55 ActionAtomistic(ao),
56 pdb(*new(PDB))
57 {
58  std::vector<SetupMolInfo*> moldat=plumed.getActionSet().select<SetupMolInfo*>();
59  if( moldat.size()!=0 ) error("cannot use more than one MOLINFO action in input");
60 
61  std::vector<AtomNumber> backbone;
62  parseAtomList("CHAIN",backbone);
63  if( read_backbone.size()==0 ){
64  for(unsigned i=1;;++i){
65  parseAtomList("CHAIN",i,backbone);
66  if( backbone.size()==0 ) break;
67  read_backbone.push_back(backbone);
68  backbone.resize(0);
69  }
70  } else {
71  read_backbone.push_back(backbone);
72  }
73  if( read_backbone.size()==0 ){
74  std::string reference; parse("STRUCTURE",reference);
75 
76 
77  if( ! pdb.read(reference,plumed.getAtoms().usingNaturalUnits(),0.1/plumed.getAtoms().getUnits().getLength()))plumed_merror("missing input file " + reference );
78  std::vector<std::string> chains; pdb.getChainNames( chains );
79  log.printf(" pdb file named %s contains %d chains \n",reference.c_str(), chains.size() );
80  for(unsigned i=0;i<chains.size();++i){
81  unsigned start,end; std::string errmsg;
82  pdb.getResidueRange( chains[i], start, end, errmsg );
83  if( errmsg.length()!=0 ) error( errmsg );
84  AtomNumber astart,aend;
85  pdb.getAtomRange( chains[i], astart, aend, errmsg );
86  if( errmsg.length()!=0 ) error( errmsg );
87  log.printf(" chain named %s contains residues %u to %u and atoms %d to %d \n",chains[i].c_str(),start,end,astart.serial(),aend.serial());
88  }
89  }
90  pdb.renameAtoms("HA1","CB"); // This is a hack to make this work with GLY residues
91 }
92 
93 void SetupMolInfo::getBackbone( std::vector<std::string>& restrings, const std::vector<std::string>& atnames, std::vector< std::vector<AtomNumber> >& backbone ){
94  if( read_backbone.size()!=0 ){
95  if( restrings.size()!=1 ) error("cannot interpret anything other than all for residues when using CHAIN keywords");
96  if( restrings[0]!="all" ) error("cannot interpret anything other than all for residues when using CHAIN keywords");
97  backbone.resize( read_backbone.size() );
98  for(unsigned i=0;i<read_backbone.size();++i){
99  backbone[i].resize( read_backbone[i].size() );
100  for(unsigned j=0;j<read_backbone[i].size();++j) backbone[i][j]=read_backbone[i][j];
101  }
102  } else {
103  if( restrings.size()==1 ){
104  if( restrings[0]=="all" ){
105  std::vector<std::string> chains; pdb.getChainNames( chains );
106  for(unsigned i=0;i<chains.size();++i){
107  unsigned r_start, r_end; std::string errmsg, mm, nn;
108  pdb.getResidueRange( chains[i], r_start, r_end, errmsg );
109  Tools::convert(r_start,mm); Tools::convert(r_end,nn);
110  if(i==0) restrings[0] = mm + "-" + nn;
111  else restrings.push_back( mm + "-" + nn );
112  }
113  }
114  }
115  Tools::interpretRanges(restrings);
116 
117  // Convert the list of involved residues into a list of segments of chains
118  int nk, nj; std::vector< std::vector<unsigned> > segments;
119  std::vector<unsigned> thissegment;
120  Tools::convert(restrings[0],nk); thissegment.push_back(nk);
121  for(unsigned i=1;i<restrings.size();++i){
122  Tools::convert(restrings[i-1],nk);
123  Tools::convert(restrings[i],nj);
124  if( (nk+1)!=nj || pdb.getChainID(nk)!=pdb.getChainID(nj) ){
125  segments.push_back(thissegment);
126  thissegment.resize(0);
127  }
128  thissegment.push_back(nj);
129  }
130  segments.push_back( thissegment );
131 
132  // And now get the backbone atoms from each segment
133  backbone.resize( segments.size() );
134  std::vector<AtomNumber> atomnumbers( atnames.size() );
135  for(unsigned i=0;i<segments.size();++i){
136  for(unsigned j=0;j<segments[i].size();++j){
137  bool terminus=( j==0 || j==segments[i].size()-1 );
138  bool foundatoms=pdb.getBackbone( segments[i][j], atnames, atomnumbers );
139  if( terminus && !foundatoms ){
140  std::string num; Tools::convert( segments[i][j], num );
141  warning("Assuming residue " + num + " is a terminal group");
142  } else if( !foundatoms ){
143  std::string num; Tools::convert( segments[i][j], num );
144  error("Could not find required backbone atom in residue number " + num );
145  } else if( foundatoms ){
146  for(unsigned k=0;k<atomnumbers.size();++k) backbone[i].push_back( atomnumbers[k] );
147  }
148  }
149  }
150  }
151  for(unsigned i=0;i<backbone.size();++i){
152  if( backbone[i].size()%atnames.size()!=0 ) error("number of atoms in one of the segments of backbone is not a multiple of the number of atoms in each residue");
153  }
154 }
155 
156 
158  return pdb.getAtomName(a);
159 }
160 
162  return pdb.getResidueNumber(a);
163 }
164 
166  return pdb.getResidueName(a);
167 }
168 
169 }
Simple class to store the index of an atom.
Definition: AtomNumber.h:39
bool read(const std::string &file, bool naturalUnits, double scale)
Read the pdb from a file, scaling positions by a factor scale.
Definition: PDB.cpp:144
unsigned getResidueNumber(AtomNumber a) const
return the residue number for a specific atom
Definition: PDB.cpp:58
Log & log
Reference to the log stream.
Definition: Action.h:93
void getAtomRange(const std::string &chainname, AtomNumber &a_start, AtomNumber &a_end, std::string &errmsg) const
Get the atoms in each of the chains.
Definition: PDB.cpp:177
void warning(const std::string &msg)
Issue a warning.
Definition: Action.cpp:201
unsigned serial() const
Returns the serial number.
Definition: AtomNumber.h:84
SetupMolInfo(const ActionOptions &ao)
static bool convert(const std::string &str, double &t)
Convert a string to a double, reading it.
Definition: Tools.cpp:74
void error(const std::string &msg) const
Crash calculation and print documentation.
Definition: Action.cpp:195
std::string getAtomName(AtomNumber a) const
return the name of a specific atom
Definition: PDB.cpp:51
void parseAtomList(const std::string &key, std::vector< AtomNumber > &t)
Parse a list of atoms without a numbered keyword.
void add(const std::string &t, const std::string &k, const std::string &d)
Add a new keyword of type t with name k and description d.
Definition: Keywords.cpp:167
void getChainNames(std::vector< std::string > &chains) const
Get the names of all the chains in the pdb file.
Definition: PDB.cpp:152
void renameAtoms(const std::string &old_name, const std::string &new_name)
This allows you to give atoms a new name - this is used to rename the HB1 atoms in GLY residues CB so...
Definition: PDB.cpp:219
std::string getAtomName(AtomNumber a) const
Action used to create a PLMD::Action that do something during setup only e.g.
Definition: ActionSetup.h:33
void parse(const std::string &key, T &t)
Parse one keyword as generic type.
Definition: Action.h:273
This class holds the keywords and their documentation.
Definition: Keywords.h:36
static void registerKeywords(Keywords &keys)
Creator of keywords.
Definition: ActionSetup.cpp:39
void getBackbone(std::vector< std::string > &resstrings, const std::vector< std::string > &atnames, std::vector< std::vector< AtomNumber > > &backbone)
This class is used to bring the relevant information to the Action constructor.
Definition: Action.h:41
Action used to create objects that access the positions of the atoms from the MD code.
unsigned getResidueNumber(AtomNumber a) const
int printf(const char *fmt,...)
Formatted output with explicit format - a la printf.
Definition: OFile.cpp:82
Base class for all the input Actions.
Definition: Action.h:60
std::string getResidueName(AtomNumber a) const
return the residue name for a specific atom
Definition: PDB.cpp:65
std::vector< std::vector< AtomNumber > > read_backbone
Definition: SetupMolInfo.h:38
static void interpretRanges(std::vector< std::string > &)
Interpret atom ranges.
Definition: Tools.cpp:231
Minimalistic pdb parser.
Definition: PDB.h:38
std::string getResidueName(AtomNumber a) const
Main plumed object.
Definition: Plumed.h:201
bool getBackbone(const unsigned &resnumber, const std::vector< std::string > &backnames, std::vector< AtomNumber > &backnumbers) const
Get the atoms in the backbone of a particular residue.
Definition: PDB.cpp:194
static void registerKeywords(Keywords &keys)
std::string getChainID(const unsigned &resnumber) const
Get the chain ID that a particular residue is a part of.
Definition: PDB.cpp:212
void const char const char int double * a
Definition: Matrix.h:42
void getResidueRange(const std::string &chainname, unsigned &res_start, unsigned &res_end, std::string &errmsg) const
Get the residues in each of the chains.
Definition: PDB.cpp:160