Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2012-2023 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 "Colvar.h"
23 : #include "tools/Communicator.h"
24 : #include "tools/NeighborList.h"
25 : #include "core/ActionRegister.h"
26 : #include "tools/SwitchingFunction.h"
27 :
28 : namespace PLMD {
29 : namespace colvar {
30 :
31 : //+PLUMEDOC COLVAR CONTACTMAP
32 : /*
33 : Calculate the distances between a number of pairs of atoms and transform each distance by a switching function.
34 :
35 : This CV calculates a series of distances between pairs of atoms and transforms them by a switching function.
36 : The transformed distance can be compared with a reference value in order to calculate the squared distance
37 : between two contact maps. Each distance can also be weighted for a given value. CONTACTMAP can be used together
38 : with [FUNCPATHMSD](FUNCPATHMSD.md) to define a path in contactmap space as was done in the 2008 paper by Bonomi that
39 : is cited below.
40 :
41 : The individual contact map distances related to each contact can be accessed as components
42 : named `cm.contact-1`, `cm.contact-2`, etc, assuming that the label of the CONTACTMAP is `cm`.
43 :
44 : ## Examples
45 :
46 : The following example calculates switching functions based on the distances between atoms
47 : 1 and 2, 3 and 4 and 4 and 5. The values of these three switching functions are then output
48 : to a file named colvar.
49 :
50 : ```plumed
51 : f1: CONTACTMAP ATOMS1=1,2 ATOMS2=3,4 ATOMS3=4,5 ATOMS4=5,6 SWITCH={RATIONAL R_0=1.5}
52 : PRINT ARG=f1.* FILE=colvar
53 : ```
54 :
55 : The following example calculates the difference of the current contact map with respect
56 : to a reference provided. In this case REFERENCE is the fraction of contact that is formed
57 : (i.e. the distance between two atoms transformed with the SWITCH), while R_0 is the contact
58 : distance. WEIGHT gives the relative weight of each contact to the final distance measure.
59 :
60 : ```plumed
61 : cmap: CONTACTMAP ...
62 : ATOMS1=1,2 REFERENCE1=0.1 WEIGHT1=0.5
63 : ATOMS2=3,4 REFERENCE2=0.5 WEIGHT2=1.0
64 : ATOMS3=4,5 REFERENCE3=0.25 WEIGHT3=1.0
65 : ATOMS4=5,6 REFERENCE4=0.0 WEIGHT4=0.5
66 : SWITCH={RATIONAL R_0=1.5}
67 : CMDIST
68 : ...
69 :
70 : PRINT ARG=cmap FILE=colvar
71 : ```
72 :
73 : The next example calculates calculates fraction of native contacts (Q)
74 : for Trp-cage mini-protein. R_0 is the distance at which the switch function is guaranteed to
75 : be 1.0 – it doesn't really matter for Q and should be something very small, like 1 A.
76 : REF is the reference distance for the contact, e.g. the distance from a crystal structure.
77 : LAMBDA is the tolerance for the distance – if set to 1.0, the contact would have to have exactly
78 : the reference value to be formed; instead for lambda values of 1.5–1.8 are usually used to allow some slack.
79 : BETA is the softness of the switch function, default is 50nm.
80 : WEIGHT is the 1/(number of contacts) giving equal weight to each contact.
81 :
82 : When using native contact Q switch function, please cite the 2013 paper from Best from the bibliography below
83 :
84 : ```plumed
85 : # The full (much-longer) example available in regtest/basic/rt72/
86 :
87 : cmap: CONTACTMAP ...
88 : ATOMS1=1,67 SWITCH1={Q R_0=0.01 BETA=50.0 LAMBDA=1.5 REF=0.4059} WEIGHT1=0.003597
89 : ATOMS2=1,68 SWITCH2={Q R_0=0.01 BETA=50.0 LAMBDA=1.5 REF=0.4039} WEIGHT2=0.003597
90 : ATOMS3=1,69 SWITCH3={Q R_0=0.01 BETA=50.0 LAMBDA=1.5 REF=0.3215} WEIGHT3=0.003597
91 : ATOMS4=5,61 SWITCH4={Q R_0=0.01 BETA=50.0 LAMBDA=1.5 REF=0.4277} WEIGHT4=0.003597
92 : ATOMS5=5,67 SWITCH5={Q R_0=0.01 BETA=50.0 LAMBDA=1.5 REF=0.3851} WEIGHT5=0.003597
93 : ATOMS6=5,68 SWITCH6={Q R_0=0.01 BETA=50.0 LAMBDA=1.5 REF=0.3811} WEIGHT6=0.003597
94 : ATOMS7=5,69 SWITCH7={Q R_0=0.01 BETA=50.0 LAMBDA=1.5 REF=0.3133} WEIGHT7=0.003597
95 : SUM
96 : ...
97 :
98 : PRINT ARG=cmap FILE=colvar
99 : ```
100 :
101 : */
102 : //+ENDPLUMEDOC
103 :
104 : class ContactMap : public Colvar {
105 : private:
106 : bool pbc, serial, docomp, dosum, docmdist;
107 : std::unique_ptr<NeighborList> nl;
108 : std::vector<SwitchingFunction> sfs;
109 : std::vector<double> reference, weight;
110 : public:
111 : static void registerKeywords( Keywords& keys );
112 : explicit ContactMap(const ActionOptions&);
113 : // active methods:
114 : void calculate() override;
115 0 : void checkFieldsAllowed() override {}
116 : };
117 :
118 : PLUMED_REGISTER_ACTION(ContactMap,"CONTACTMAP")
119 :
120 14 : void ContactMap::registerKeywords( Keywords& keys ) {
121 14 : Colvar::registerKeywords( keys );
122 14 : keys.add("numbered","ATOMS","the atoms involved in each of the contacts you wish to calculate. "
123 : "Keywords like ATOMS1, ATOMS2, ATOMS3,... should be listed and one contact will be "
124 : "calculated for each ATOM keyword you specify.");
125 28 : keys.reset_style("ATOMS","atoms");
126 14 : keys.add("numbered","SWITCH","The switching functions to use for each of the contacts in your map. "
127 : "You can either specify a global switching function using SWITCH or one "
128 : "switching function for each contact. Details of the various switching "
129 : "functions you can use are provided on \\ref switchingfunction.");
130 14 : keys.add("numbered","REFERENCE","A reference value for a given contact, by default is 0.0 "
131 : "You can either specify a global reference value using REFERENCE or one "
132 : "reference value for each contact.");
133 14 : keys.add("numbered","WEIGHT","A weight value for a given contact, by default is 1.0 "
134 : "You can either specify a global weight value using WEIGHT or one "
135 : "weight value for each contact.");
136 28 : keys.reset_style("SWITCH","compulsory");
137 28 : keys.linkActionInDocs("SWITCH","LESS_THAN");
138 14 : keys.addFlag("SUM",false,"calculate the sum of all the contacts in the input");
139 14 : keys.addFlag("CMDIST",false,"calculate the distance with respect to the provided reference contact map");
140 14 : keys.addFlag("SERIAL",false,"Perform the calculation in serial - for debug purpose");
141 28 : keys.addOutputComponent("contact","default","scalar","By not using SUM or CMDIST each contact will be stored in a component");
142 28 : keys.setValueDescription("scalar","the sum of all the switching function on all the distances");
143 14 : keys.addDOI("10.1021/ja803652f");
144 14 : keys.addDOI("10.1073/pnas.1311599110");
145 14 : }
146 :
147 12 : ContactMap::ContactMap(const ActionOptions&ao):
148 : PLUMED_COLVAR_INIT(ao),
149 12 : pbc(true),
150 12 : serial(false),
151 12 : docomp(true),
152 12 : dosum(false),
153 12 : docmdist(false) {
154 12 : parseFlag("SERIAL",serial);
155 12 : parseFlag("SUM",dosum);
156 12 : parseFlag("CMDIST",docmdist);
157 12 : if(docmdist==true&&dosum==true) {
158 0 : error("You cannot use SUM and CMDIST together");
159 : }
160 12 : bool nopbc=!pbc;
161 12 : parseFlag("NOPBC",nopbc);
162 12 : pbc=!nopbc;
163 :
164 : // Read in the atoms
165 : std::vector<AtomNumber> t, ga_lista, gb_lista;
166 12 : for(int i=1;; ++i ) {
167 1778 : parseAtomList("ATOMS", i, t );
168 889 : if( t.empty() ) {
169 : break;
170 : }
171 :
172 877 : if( t.size()!=2 ) {
173 : std::string ss;
174 0 : Tools::convert(i,ss);
175 0 : error("ATOMS" + ss + " keyword has the wrong number of atoms");
176 : }
177 877 : ga_lista.push_back(t[0]);
178 877 : gb_lista.push_back(t[1]);
179 877 : t.resize(0);
180 :
181 : // Add a value for this contact
182 : std::string num;
183 877 : Tools::convert(i,num);
184 877 : if(!dosum&&!docmdist) {
185 10 : addComponentWithDerivatives("contact-"+num);
186 10 : componentIsNotPeriodic("contact-"+num);
187 : }
188 877 : }
189 : // Create neighbour lists
190 24 : nl=Tools::make_unique<NeighborList>(ga_lista,gb_lista,serial,true,pbc,getPbc(),comm);
191 :
192 : // Read in switching functions
193 : std::string errors;
194 12 : sfs.resize( ga_lista.size() );
195 : unsigned nswitch=0;
196 889 : for(unsigned i=0; i<ga_lista.size(); ++i) {
197 : std::string num, sw1;
198 877 : Tools::convert(i+1, num);
199 1754 : if( !parseNumbered( "SWITCH", i+1, sw1 ) ) {
200 : break;
201 : }
202 877 : nswitch++;
203 877 : sfs[i].set(sw1,errors);
204 877 : if( errors.length()!=0 ) {
205 0 : error("problem reading SWITCH" + num + " keyword : " + errors );
206 : }
207 : }
208 12 : if( nswitch==0 ) {
209 : std::string sw;
210 0 : parse("SWITCH",sw);
211 0 : if(sw.length()==0) {
212 0 : error("no switching function specified use SWITCH keyword");
213 : }
214 0 : for(unsigned i=0; i<ga_lista.size(); ++i) {
215 0 : sfs[i].set(sw,errors);
216 0 : if( errors.length()!=0 ) {
217 0 : error("problem reading SWITCH keyword : " + errors );
218 : }
219 : }
220 12 : } else if( nswitch!=sfs.size() ) {
221 : std::string num;
222 0 : Tools::convert(nswitch+1, num);
223 0 : error("missing SWITCH" + num + " keyword");
224 : }
225 :
226 : // Read in reference values
227 : nswitch=0;
228 12 : reference.resize(ga_lista.size(), 0.);
229 22 : for(unsigned i=0; i<ga_lista.size(); ++i) {
230 40 : if( !parseNumbered( "REFERENCE", i+1, reference[i] ) ) {
231 : break;
232 : }
233 10 : nswitch++;
234 : }
235 12 : if( nswitch==0 ) {
236 10 : parse("REFERENCE",reference[0]);
237 867 : for(unsigned i=1; i<ga_lista.size(); ++i) {
238 857 : reference[i]=reference[0];
239 857 : nswitch++;
240 : }
241 : }
242 12 : if(nswitch == 0 && docmdist) {
243 0 : error("with CMDIST one must use REFERENCE to setup the reference contact map");
244 : }
245 :
246 : // Read in weight values
247 : nswitch=0;
248 12 : weight.resize(ga_lista.size(), 1.0);
249 884 : for(unsigned i=0; i<ga_lista.size(); ++i) {
250 1746 : if( !parseNumbered( "WEIGHT", i+1, weight[i] ) ) {
251 : break;
252 : }
253 872 : nswitch++;
254 : }
255 12 : if( nswitch==0 ) {
256 1 : parse("WEIGHT",weight[0]);
257 5 : for(unsigned i=1; i<ga_lista.size(); ++i) {
258 4 : weight[i]=weight[0];
259 : }
260 : nswitch = ga_lista.size();
261 : }
262 :
263 : // Output details of all contacts
264 889 : for(unsigned i=0; i<sfs.size(); ++i) {
265 877 : log.printf(" The %uth contact is calculated from atoms : %d %d. Inflection point of switching function is at %s. Reference contact value is %f\n",
266 1754 : i+1, ga_lista[i].serial(), gb_lista[i].serial(), ( sfs[i].description() ).c_str(), reference[i] );
267 : }
268 :
269 12 : if(dosum) {
270 9 : addValueWithDerivatives();
271 9 : setNotPeriodic();
272 9 : log.printf(" colvar is sum of all contacts in contact map\n");
273 : }
274 12 : if(docmdist) {
275 2 : addValueWithDerivatives();
276 2 : setNotPeriodic();
277 2 : log.printf(" colvar is distance between the contact map matrix and the provided reference matrix\n");
278 : }
279 :
280 12 : if(dosum || docmdist) {
281 11 : docomp=false;
282 : } else {
283 1 : serial=true;
284 1 : docomp=true;
285 : }
286 :
287 : // Set up if it is just a list of contacts
288 12 : requestAtoms(nl->getFullAtomList());
289 12 : checkRead();
290 12 : }
291 :
292 4019 : void ContactMap::calculate() {
293 :
294 4019 : double ncoord=0.;
295 4019 : Tensor virial;
296 4019 : std::vector<Vector> deriv(getNumberOfAtoms());
297 :
298 : unsigned stride;
299 : unsigned rank;
300 4019 : if(serial) {
301 : // when using components the parallelisation do not work
302 : stride=1;
303 : rank=0;
304 : } else {
305 4014 : stride=comm.Get_size();
306 4014 : rank=comm.Get_rank();
307 : }
308 :
309 : // sum over close pairs
310 296931 : for(unsigned i=rank; i<nl->size(); i+=stride) {
311 292912 : Vector distance;
312 292912 : unsigned i0=nl->getClosePair(i).first;
313 292912 : unsigned i1=nl->getClosePair(i).second;
314 292912 : if(pbc) {
315 292912 : distance=pbcDistance(getPosition(i0),getPosition(i1));
316 : } else {
317 0 : distance=delta(getPosition(i0),getPosition(i1));
318 : }
319 :
320 292912 : double dfunc=0.;
321 292912 : double coord = weight[i]*(sfs[i].calculate(distance.modulo(), dfunc) - reference[i]);
322 292912 : Vector tmpder = weight[i]*dfunc*distance;
323 292912 : Tensor tmpvir = weight[i]*dfunc*Tensor(distance,distance);
324 292912 : if(!docmdist) {
325 292887 : deriv[i0] -= tmpder;
326 292887 : deriv[i1] += tmpder;
327 292887 : virial -= tmpvir;
328 292887 : ncoord += coord;
329 : } else {
330 25 : tmpder *= 2.*coord;
331 25 : tmpvir *= 2.*coord;
332 25 : deriv[i0] -= tmpder;
333 25 : deriv[i1] += tmpder;
334 25 : virial -= tmpvir;
335 25 : ncoord += coord*coord;
336 : }
337 :
338 292912 : if(docomp) {
339 25 : Value* val=getPntrToComponent( i );
340 25 : setAtomsDerivatives( val, i0, deriv[i0] );
341 25 : setAtomsDerivatives( val, i1, deriv[i1] );
342 25 : setBoxDerivatives( val, -tmpvir );
343 : val->set(coord);
344 : }
345 : }
346 :
347 4019 : if(!serial) {
348 4014 : comm.Sum(&ncoord,1);
349 4014 : if(!deriv.empty()) {
350 4014 : comm.Sum(&deriv[0][0],3*deriv.size());
351 : }
352 4014 : comm.Sum(&virial[0][0],9);
353 : }
354 :
355 4019 : if( !docomp ) {
356 589788 : for(unsigned i=0; i<deriv.size(); ++i) {
357 585774 : setAtomsDerivatives(i,deriv[i]);
358 : }
359 4014 : setValue (ncoord);
360 4014 : setBoxDerivatives (virial);
361 : }
362 4019 : }
363 :
364 : }
365 : }
|