LCOV - code coverage report
Current view: top level - isdb - CS2Backbone.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 1093 1145 95.5 %
Date: 2019-08-13 10:15:31 Functions: 33 34 97.1 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2013-2019 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             : 
      23             : #define cutOffNB      0.60      // buffer distance for neighbour-lists 
      24             : #define cutOffDist    0.50      // cut off distance for non-bonded pairwise forces
      25             : #define cutOnDist     0.32      // cut off distance for non-bonded pairwise forces
      26             : #define cutOffNB2     cutOffNB*cutOffNB // squared buffer distance for neighbour-lists 
      27             : #define cutOffDist2   cutOffDist*cutOffDist
      28             : #define cutOnDist2    cutOnDist*cutOnDist
      29             : #define invswitch     1.0/((cutOffDist2-cutOnDist2)*(cutOffDist2-cutOnDist2)*(cutOffDist2-cutOnDist2))
      30             : #define cutOffDist4   cutOffDist2*cutOffDist2
      31             : #define cutMixed      cutOffDist2*cutOffDist2*cutOffDist2 -3.*cutOffDist2*cutOffDist2*cutOnDist2
      32             : 
      33             : #include <string>
      34             : #include <fstream>
      35             : #include <iterator>
      36             : #include <sstream>
      37             : 
      38             : #include "MetainferenceBase.h"
      39             : #include "core/ActionRegister.h"
      40             : #include "tools/Pbc.h"
      41             : #include "tools/PDB.h"
      42             : #include "tools/Torsion.h"
      43             : 
      44             : using namespace std;
      45             : 
      46             : namespace PLMD {
      47             : namespace isdb {
      48             : 
      49             : //+PLUMEDOC ISDB_COLVAR CS2BACKBONE
      50             : /*
      51             : Calculates the backbone chemical shifts for a protein.
      52             : 
      53             : The functional form is that of CamShift \cite Kohlhoff:2009us. The chemical shift
      54             : of the selected nuclei can be saved as components. Alternatively one can calculate either
      55             : the CAMSHIFT score (useful as a collective variable \cite Granata:2013dk or as a scoring
      56             : function \cite Robustelli:2010dn) or a \ref METAINFERENCE score (using DOSCORE).
      57             : For these two latter cases experimental chemical shifts must be provided.
      58             : 
      59             : CS2BACKBONE calculation can be relatively heavy because it often uses a large number of atoms, it can
      60             : be run in parallel using MPI and \ref Openmp.
      61             : 
      62             : As a general rule, when using \ref CS2BACKBONE or other experimental restraints it may be better to
      63             : increase the accuracy of the constraint algorithm due to the increased strain on the bonded structure.
      64             : In the case of GROMACS it is safer to use lincs-iter=2 and lincs-order=6.
      65             : 
      66             : In general the system for which chemical shifts are calculated must be completely included in
      67             : ATOMS and a TEMPLATE pdb file for the same atoms should be provided as well in the folder DATADIR.
      68             : The system is made automatically whole unless NOPBC is used, in particular if the system is made
      69             : by multiple chains it is usually better to use NOPBC and make the molecule whole \ref WHOLEMOLECULES
      70             : selecting an appropriate order of the atoms. The pdb file is needed to the generate a simple topology of the protein.
      71             : For histidine residues in protonation states different from D the HIE/HSE HIP/HSP name should be used.
      72             : GLH and ASH can be used for the alternative protonation of GLU and ASP. Non-standard amino acids and other
      73             : molecules are not yet supported, but in principle they can be named UNK. If multiple chains are present
      74             : the chain identifier must be in the standard PDB format, together with the TER keyword at the end of each chain.
      75             : Termini groups like ACE or NME should be removed from the TEMPLATE pdb because they are not recognized by
      76             : CS2BACKBONE.
      77             : 
      78             : In addition to a pdb file one needs to provide a list of chemical shifts to be calculated using one
      79             : file per nucleus type (CAshifts.dat, CBshifts.dat, Cshifts.dat, Hshifts.dat, HAshifts.dat, Nshifts.dat),
      80             : add only the files for the nuclei you need, but each file should include all protein residues.
      81             : A chemical shift for a nucleus is calculated if a value greater than 0 is provided.
      82             : For practical purposes the value can correspond to the experimental value.
      83             : Residues numbers should match that used in the pdb file, but must be positive, so double check the pdb.
      84             : The first and last residue of each chain should be preceded by a # character.
      85             : 
      86             : \verbatim
      87             : CAshifts.dat:
      88             : #1 0.0
      89             : 2 55.5
      90             : 3 58.4
      91             : .
      92             : .
      93             : #last 0.0
      94             : #first of second chain
      95             : .
      96             : #last of second chain
      97             : \endverbatim
      98             : 
      99             : The default behavior is to store the values for the active nuclei in components (ca-#, cb-#,
     100             : co-#, ha-#, hn-#, nh-# and expca-#, expcb-#, expco-#, expha-#, exphn-#, exp-nh#) with NOEXP it is possible
     101             : to only store the back-calculated values, where # includes a chain and residue number.
     102             : 
     103             : One additional file is always needed in the folder DATADIR: camshift.db. This file includes all the parameters needed to
     104             : calculate the chemical shifts and can be found in regtest/isdb/rt-cs2backbone/data/ .
     105             : 
     106             : Additional material and examples can be also found in the tutorial \ref belfast-9 as well as in the cs2backbone regtests
     107             : in the isdb folder.
     108             : 
     109             : \par Examples
     110             : 
     111             : In this first example the chemical shifts are used to calculate a collective variable to be used
     112             : in NMR driven Metadynamics \cite Granata:2013dk :
     113             : 
     114             : \plumedfile
     115             : whole: GROUP ATOMS=2612-2514:-1,961-1:-1,2466-962:-1,2513-2467:-1
     116             : WHOLEMOLECULES ENTITY0=whole
     117             : cs: CS2BACKBONE ATOMS=1-2612 DATADIR=data/ TEMPLATE=template.pdb CAMSHIFT NOPBC
     118             : metad: METAD ARG=cs HEIGHT=0.5 SIGMA=0.1 PACE=200 BIASFACTOR=10
     119             : PRINT ARG=cs,metad.bias FILE=COLVAR STRIDE=100
     120             : \endplumedfile
     121             : 
     122             : In this second example the chemical shifts are used as replica-averaged restrained as in \cite Camilloni:2012je \cite Camilloni:2013hs.
     123             : 
     124             : \plumedfile
     125             : cs: CS2BACKBONE ATOMS=1-174 DATADIR=data/
     126             : encs: ENSEMBLE ARG=(cs\.hn-.*),(cs\.nh-.*)
     127             : stcs: STATS ARG=encs.* SQDEVSUM PARARG=(cs\.exphn-.*),(cs\.expnh-.*)
     128             : RESTRAINT ARG=stcs.sqdevsum AT=0 KAPPA=0 SLOPE=24
     129             : 
     130             : PRINT ARG=(cs\.hn-.*),(cs\.nh-.*) FILE=RESTRAINT STRIDE=100
     131             : 
     132             : \endplumedfile
     133             : 
     134             : This third example show how to use chemical shifts to calculate a \ref METAINFERENCE score .
     135             : 
     136             : \plumedfile
     137             : cs: CS2BACKBONE ATOMS=1-174 DATADIR=data/ SIGMA_MEAN0=1.0 DOSCORE
     138             : csbias: BIASVALUE ARG=cs.score
     139             : 
     140             : PRINT ARG=(cs\.hn-.*),(cs\.nh-.*) FILE=CS.dat STRIDE=1000
     141             : PRINT ARG=cs.score FILE=BIAS STRIDE=100
     142             : \endplumedfile
     143             : 
     144             : */
     145             : //+ENDPLUMEDOC
     146             : 
     147             : class CS2BackboneDB {
     148             :   enum { STD, GLY, PRO};
     149             :   enum { HA_ATOM, H_ATOM, N_ATOM, CA_ATOM, CB_ATOM, C_ATOM };
     150             :   static const unsigned aa_kind = 3;
     151             :   static const unsigned atm_kind = 6;
     152             :   static const unsigned numXtraDists = 27;
     153             : 
     154             :   // ALA, ARG, ASN, ASP, CYS, GLU, GLN, GLY, HIS, ILE, LEU, LYS, MET, PHE, PRO, SER, THR, TRP, TYR, VAL
     155             :   double c_aa[aa_kind][atm_kind][20];
     156             :   double c_aa_prev[aa_kind][atm_kind][20];
     157             :   double c_aa_succ[aa_kind][atm_kind][20];
     158             :   double co_bb[aa_kind][atm_kind][16];
     159             :   double co_sc_[aa_kind][atm_kind][20][20];
     160             :   double co_xd[aa_kind][atm_kind][numXtraDists];
     161             :   double co_sphere[aa_kind][atm_kind][2][8];
     162             :   // for ring current effects
     163             :   // Phe, Tyr, Trp_1, Trp_2, His
     164             :   double co_ring[aa_kind][atm_kind][5];
     165             :   // for dihedral angles
     166             :   // co * (a * cos(3 * omega + c) + b * cos(omega + d))
     167             :   double co_da[aa_kind][atm_kind][3];
     168             :   double pars_da[aa_kind][atm_kind][3][5];
     169             : 
     170             : public:
     171             : 
     172       10926 :   inline unsigned kind(const string &s) {
     173       10926 :     if(s=="GLY") return GLY;
     174        9324 :     else if(s=="PRO") return PRO;
     175        9108 :     return STD;
     176             :   }
     177             : 
     178       10926 :   inline unsigned atom_kind(const string &s) {
     179       10926 :     if(s=="HA")return HA_ATOM;
     180       10872 :     else if(s=="H") return H_ATOM;
     181        8010 :     else if(s=="N") return N_ATOM;
     182        5148 :     else if(s=="CA")return CA_ATOM;
     183        2160 :     else if(s=="CB")return CB_ATOM;
     184          54 :     else if(s=="C") return C_ATOM;
     185           0 :     return -1;
     186             :   }
     187             : 
     188             :   unsigned get_numXtraDists() {return numXtraDists;}
     189             : 
     190             :   //PARAMETERS
     191             :   inline double * CONSTAACURR(const unsigned a_kind, const unsigned at_kind) {return c_aa[a_kind][at_kind];}
     192             :   inline double * CONSTAANEXT(const unsigned a_kind, const unsigned at_kind) {return c_aa_succ[a_kind][at_kind];}
     193             :   inline double * CONSTAAPREV(const unsigned a_kind, const unsigned at_kind) {return c_aa_prev[a_kind][at_kind];}
     194             :   inline double * CONST_BB2(const unsigned a_kind, const unsigned at_kind) {return co_bb[a_kind][at_kind];}
     195             :   inline double * CONST_SC2(const unsigned a_kind, const unsigned at_kind, unsigned res_type) { return co_sc_[a_kind][at_kind][res_type];}
     196             :   inline double * CONST_XD(const unsigned a_kind, const unsigned at_kind) { return co_xd[a_kind][at_kind];}
     197             :   inline double * CO_SPHERE(const unsigned a_kind, const unsigned at_kind, unsigned exp_type) { return co_sphere[a_kind][at_kind][exp_type];}
     198             :   inline double * CO_RING(const unsigned a_kind, const unsigned at_kind) { return co_ring[a_kind][at_kind];}
     199             :   inline double * CO_DA(const unsigned a_kind, const unsigned at_kind) { return co_da[a_kind][at_kind];}
     200             :   inline double * PARS_DA(const unsigned a_kind, const unsigned at_kind, const unsigned ang_kind) { return pars_da[a_kind][at_kind][ang_kind];}
     201             : 
     202          18 :   void parse(const string &file, const double dscale) {
     203          18 :     ifstream in;
     204          18 :     in.open(file.c_str());
     205          18 :     if(!in) plumed_merror("Unable to open DB file: " + file);
     206             : 
     207             :     unsigned c_kind = 0;
     208             :     unsigned c_atom = 0;
     209             :     unsigned nline = 0;
     210             : 
     211         324 :     for(unsigned i=0; i<3; i++) for(unsigned j=0; j<6; j++) {
     212        6480 :         for(unsigned k=0; k<20; k++) {
     213        6480 :           c_aa[i][j][k]=0.;
     214        6480 :           c_aa_prev[i][j][k]=0.;
     215        6480 :           c_aa_succ[i][j][k]=0.;
     216        6480 :           for(unsigned m=0; m<20; m++) co_sc_[i][j][k][m]=0.;
     217             :         }
     218        5184 :         for(unsigned k=0; k<16; k++) {co_bb[i][j][k]=0.; }
     219        2592 :         for(unsigned k=0; k<8; k++) { co_sphere[i][j][0][k]=0.; co_sphere[i][j][1][k]=0.; }
     220         972 :         for(unsigned k=0; k<3; k++) {
     221         972 :           co_da[i][j][k]=0.;
     222         972 :           for(unsigned l=0; l<5; l++) pars_da[i][j][k][l]=0.;
     223             :         }
     224        1620 :         for(unsigned k=0; k<5; k++) co_ring[i][j][k]=0.;
     225        8748 :         for(unsigned k=0; k<numXtraDists; k++) co_xd[i][j][k]=0.;
     226             :       }
     227             : 
     228       37710 :     while(!in.eof()) {
     229             :       string line;
     230       37692 :       getline(in,line);
     231             :       ++nline;
     232       37692 :       if(line.compare(0,1,"#")==0) continue;
     233           0 :       vector<string> tok;
     234           0 :       vector<string> tmp;
     235       40860 :       tok = split(line,' ');
     236      523656 :       for(unsigned q=0; q<tok.size(); q++)
     237      241398 :         if(tok[q].size()) tmp.push_back(tok[q]);
     238       20430 :       tok = tmp;
     239       40860 :       if(tok.size()==0) continue;
     240       20412 :       if(tok[0]=="PAR") {
     241         324 :         c_kind = kind(tok[2]);
     242         324 :         c_atom = atom_kind(tok[1]);
     243             :         continue;
     244             :       }
     245       20088 :       else if(tok[0]=="WEIGHT") {
     246             :         continue;
     247             :       }
     248       19764 :       else if(tok[0]=="FLATBTM") {
     249             :         continue;
     250             :       }
     251       19440 :       else if (tok[0] == "SCALEHARM") {
     252             :         continue;
     253             :       }
     254       19116 :       else if (tok[0] == "TANHAMPLI") {
     255             :         continue;
     256             :       }
     257       18792 :       else if (tok[0] == "ENDHARMON") {
     258             :         continue;
     259             :       }
     260       18468 :       else if (tok[0] == "MAXRCDEVI") {
     261             :         continue;
     262             :       }
     263       18144 :       else if (tok[0] == "RANDCOIL") {
     264             :         continue;
     265             :       }
     266       17820 :       else if (tok[0] == "CONST") {
     267             :         continue;
     268             :       }
     269       17496 :       else if (tok[0] == "CONSTAA") {
     270         324 :         assign(c_aa[c_kind][c_atom],tok,1);
     271             :         continue;
     272             :       }
     273       17172 :       else if (tok[0] == "CONSTAA-1") {
     274         324 :         assign(c_aa_prev[c_kind][c_atom],tok,1);
     275             :         continue;
     276             :       }
     277       16848 :       else if (tok[0] == "CONSTAA+1") {
     278         324 :         assign(c_aa_succ[c_kind][c_atom],tok,1);
     279             :         continue;
     280             :       }
     281       16524 :       else if (tok[0] == "COBB1") {
     282             :         continue;
     283             :       }
     284       16200 :       else if (tok[0] == "COBB2") {
     285             :         //angstrom to nm
     286         324 :         assign(co_bb[c_kind][c_atom],tok,dscale);
     287             :         continue;
     288             :       }
     289       15876 :       else if (tok[0] == "SPHERE1") {
     290             :         // angstrom^-3 to nm^-3
     291         324 :         assign(co_sphere[c_kind][c_atom][0],tok,1./(dscale*dscale*dscale));
     292             :         continue;
     293             :       }
     294       15552 :       else if (tok[0] == "SPHERE2") {
     295             :         //angstrom to nm
     296         324 :         assign(co_sphere[c_kind][c_atom][1],tok,dscale);
     297             :         continue;
     298             :       }
     299       15228 :       else if (tok[0] == "DIHEDRALS") {
     300         324 :         assign(co_da[c_kind][c_atom],tok,1);
     301             :         continue;
     302             :       }
     303       14904 :       else if (tok[0] == "RINGS") {
     304             :         // angstrom^-3 to nm^-3
     305         324 :         assign(co_ring[c_kind][c_atom],tok,1./(dscale*dscale*dscale));
     306        3888 :         for(unsigned i=1; i<tok.size(); i++)
     307        1620 :           co_ring[c_kind][c_atom][i-1] *= 1000;
     308             :         continue;
     309             :       }
     310       14580 :       else if (tok[0] == "HBONDS") {
     311             :         continue;
     312             :       }
     313       14256 :       else if (tok[0] == "XTRADISTS") {
     314             :         //angstrom to nm
     315         324 :         assign(co_xd[c_kind][c_atom],tok,dscale);
     316             :         continue;
     317             :       }
     318       13932 :       else if(tok[0]=="DIHEDPHI") {
     319         324 :         assign(pars_da[c_kind][c_atom][0],tok,1);
     320             :         continue;
     321             :       }
     322       13608 :       else if(tok[0]=="DIHEDPSI") {
     323         324 :         assign(pars_da[c_kind][c_atom][1],tok,1);
     324             :         continue;
     325             :       }
     326       13284 :       else if(tok[0]=="DIHEDCHI1") {
     327         324 :         assign(pars_da[c_kind][c_atom][2],tok,1);
     328             :         continue;
     329             :       }
     330             : 
     331             :       bool ok = false;
     332             :       string scIdent1 [] = {"COSCALA1", "COSCARG1", "COSCASN1", "COSCASP1", "COSCCYS1", "COSCGLN1", "COSCGLU1",
     333             :                             "COSCGLY1", "COSCHIS1", "COSCILE1", "COSCLEU1", "COSCLYS1", "COSCMET1", "COSCPHE1",
     334             :                             "COSCPRO1", "COSCSER1", "COSCTHR1", "COSCTRP1", "COSCTYR1", "COSCVAL1"
     335       12960 :                            };
     336             : 
     337      204120 :       for(unsigned scC = 0; scC < 20; scC++) {
     338      395280 :         if(tok[0]==scIdent1[scC]) {
     339             :           ok = true;
     340             :           break;
     341             :         }
     342             :       }
     343      285120 :       if(ok) continue;
     344             : 
     345             :       string scIdent2 [] = {"COSCALA2", "COSCARG2", "COSCASN2", "COSCASP2", "COSCCYS2", "COSCGLN2", "COSCGLU2",
     346             :                             "COSCGLY2", "COSCHIS2", "COSCILE2", "COSCLEU2", "COSCLYS2", "COSCMET2", "COSCPHE2",
     347             :                             "COSCPRO2", "COSCSER2", "COSCTHR2", "COSCTRP2", "COSCTYR2", "COSCVAL2"
     348        6480 :                            };
     349             : 
     350       68040 :       for(unsigned scC = 0; scC < 20; scC++) {
     351      136080 :         if(tok[0]==scIdent2[scC]) {
     352             :           //angstrom to nm
     353        6480 :           assign(co_sc_[c_kind][c_atom][scC],tok,dscale);
     354        6480 :           ok = true; break;
     355             :         }
     356             :       }
     357      142560 :       if(ok) continue;
     358             : 
     359           0 :       if(tok.size()) {
     360           0 :         string str_err = "DB WARNING: unrecognized token: " + tok[0];
     361           0 :         plumed_merror(str_err);
     362             :       }
     363             :     }
     364          18 :     in.close();
     365          18 :   }
     366             : 
     367             : private:
     368             : 
     369       20430 :   vector<string> &split(const string &s, char delim, vector<string> &elems) {
     370       20430 :     stringstream ss(s);
     371             :     string item;
     372      523656 :     while (getline(ss, item, delim)) {
     373      241398 :       elems.push_back(item);
     374             :     }
     375       20430 :     return elems;
     376             :   }
     377             : 
     378       20430 :   vector<string> split(const string &s, char delim) {
     379             :     vector<string> elems;
     380       20430 :     split(s, delim, elems);
     381       20430 :     return elems;
     382             :   }
     383             : 
     384       10368 :   void assign(double * f, const vector<string> & v, const double scale) {
     385      245592 :     for(unsigned i=1; i<v.size(); i++) {
     386      224856 :       f[i-1] = scale*(atof(v[i].c_str()));
     387      112428 :       if(fabs(f[i-1])<0.000001) f[i-1]=0.;
     388             :     }
     389       10368 :   }
     390             : };
     391             : 
     392          54 : class CS2Backbone : public MetainferenceBase {
     393      145080 :   struct ChemicalShift {
     394             :     double exp_cs;              // a reference chemical shifts
     395             :     Value *comp;                // a pointer to the component
     396             :     unsigned res_kind;          // residue type (STD/GLY/PRO)
     397             :     unsigned atm_kind;          // nuclues (HA/CA/CB/CO/NH/HN)
     398             :     unsigned res_type_prev;     // previuos residue (ALA/VAL/..)
     399             :     unsigned res_type_curr;     // current residue (ALA/VAL/..)
     400             :     unsigned res_type_next;     // next residue (ALA/VAL/..)
     401             :     string res_name;            // residue name
     402             :     string nucleus;             // chemical shift
     403             :     bool has_chi1;              // does we have a chi1
     404             :     unsigned csatoms;           // fixed number of atoms used
     405             :     unsigned totcsatoms;        // number of atoms used
     406             :     unsigned res_num;           // residue number
     407             :     unsigned chain;             // chain number
     408             :     unsigned ipos;              // index of the atom for which we are calculating the chemical shifts
     409             :     vector<unsigned> bb;        // atoms for the previous, current and next backbone
     410             :     vector<unsigned> side_chain;// atoms for the current sidechain
     411             :     vector<int> xd1;            // additional couple of atoms
     412             :     vector<int> xd2;            // additional couple of atoms
     413             :     vector<unsigned> box_nb;    // non-bonded atoms
     414             : 
     415       10602 :     ChemicalShift():
     416             :       exp_cs(0.),
     417             :       comp(NULL),
     418             :       res_kind(0),
     419             :       atm_kind(0),
     420             :       res_type_prev(0),
     421             :       res_type_curr(0),
     422             :       res_type_next(0),
     423             :       res_name(""),
     424             :       nucleus(""),
     425             :       has_chi1(true),
     426             :       csatoms(0),
     427             :       totcsatoms(0),
     428             :       res_num(0),
     429             :       chain(0),
     430       10602 :       ipos(0)
     431             :     {
     432       10602 :       xd1.reserve(26);
     433       10602 :       xd2.reserve(26);
     434       10602 :       box_nb.reserve(150);
     435       10602 :     }
     436             :   };
     437             : 
     438             :   struct RingInfo {
     439             :     enum {R_PHE, R_TYR, R_TRP1, R_TRP2, R_HIS};
     440             :     unsigned rtype;    // one out of five different types
     441             :     unsigned atom[6];  // up to six member per ring
     442             :     unsigned numAtoms; // number of ring members (5 or 6)
     443             :     Vector position;   // center of ring coordinates
     444             :     Vector normVect;   // ring plane normal vector
     445             :     Vector g[6];       // vector of the vectors used for normVect
     446             :     double lengthN2;   // square of length of normVect
     447             :     double lengthNV;   // length of normVect
     448         360 :     RingInfo():
     449             :       rtype(0),
     450             :       numAtoms(0),
     451             :       lengthN2(NAN),
     452         360 :       lengthNV(NAN)
     453             :     {
     454         360 :       for(unsigned i=0; i<6; i++) atom[i]=0;
     455         360 :     }
     456             :   };
     457             : 
     458             :   enum aa_t {ALA, ARG, ASN, ASP, CYS, GLN, GLU, GLY, HIS, ILE, LEU, LYS, MET, PHE, PRO, SER, THR, TRP, TYR, VAL, UNK};
     459             :   enum sequence_t {Np, CAp, HAp, Cp, Op, Nc, Hc, CAc, HAc, Cc, Oc, Nn, Hn, CAn, HAn, Cn, CBc, CGc};
     460             : 
     461             :   CS2BackboneDB    db;
     462             :   vector<ChemicalShift> chemicalshifts;
     463             : 
     464             :   vector<RingInfo> ringInfo;
     465             :   vector<unsigned> type;
     466             :   vector<unsigned> res_num;
     467             :   unsigned         max_cs_atoms;
     468             :   unsigned         box_nupdate;
     469             :   unsigned         box_count;
     470             :   bool             camshift;
     471             :   bool             pbc;
     472             :   bool             serial;
     473             : 
     474             :   void init_cs(const string &file, const string &k, const PDB &pdb);
     475             :   void update_neighb();
     476             :   void compute_ring_parameters();
     477             :   void init_types(const PDB &pdb);
     478             :   void init_rings(const PDB &pdb);
     479             :   aa_t frag2enum(const string &aa);
     480             :   vector<string> side_chain_atoms(const string &s);
     481             :   bool isSP2(const string & resType, const string & atomName);
     482             :   bool is_chi1_cx(const string & frg, const string & atm);
     483             :   void xdist_name_map(string & name);
     484             : 
     485             : public:
     486             : 
     487             :   explicit CS2Backbone(const ActionOptions&);
     488             :   static void registerKeywords( Keywords& keys );
     489             :   void calculate() override;
     490             :   void update() override;
     491             : };
     492             : 
     493        7868 : PLUMED_REGISTER_ACTION(CS2Backbone,"CS2BACKBONE")
     494             : 
     495          19 : void CS2Backbone::registerKeywords( Keywords& keys ) {
     496          19 :   componentsAreNotOptional(keys);
     497          19 :   MetainferenceBase::registerKeywords( keys );
     498          57 :   keys.addFlag("NOPBC",false,"ignore the periodic boundary conditions when calculating distances");
     499          57 :   keys.addFlag("SERIAL",false,"Perform the calculation in serial - for debug purpose");
     500          76 :   keys.add("atoms","ATOMS","The atoms to be included in the calculation, e.g. the whole protein.");
     501          95 :   keys.add("compulsory","DATADIR","data/","The folder with the experimental chemical shifts.");
     502          95 :   keys.add("compulsory","TEMPLATE","template.pdb","A PDB file of the protein system to initialize ALMOST.");
     503          95 :   keys.add("compulsory","NEIGH_FREQ","20","Period in step for neighbor list update.");
     504          57 :   keys.addFlag("CAMSHIFT",false,"Set to TRUE if you to calculate a single CamShift score.");
     505          57 :   keys.addFlag("NOEXP",false,"Set to TRUE if you don't want to have fixed components with the experimental values.");
     506          76 :   keys.addOutputComponent("ha","default","the calculated Ha hydrogen chemical shifts");
     507          76 :   keys.addOutputComponent("hn","default","the calculated H hydrogen chemical shifts");
     508          76 :   keys.addOutputComponent("nh","default","the calculated N nitrogen chemical shifts");
     509          76 :   keys.addOutputComponent("ca","default","the calculated Ca carbon chemical shifts");
     510          76 :   keys.addOutputComponent("cb","default","the calculated Cb carbon chemical shifts");
     511          76 :   keys.addOutputComponent("co","default","the calculated C' carbon chemical shifts");
     512          76 :   keys.addOutputComponent("expha","default","the experimental Ha hydrogen chemical shifts");
     513          76 :   keys.addOutputComponent("exphn","default","the experimental H hydrogen chemical shifts");
     514          76 :   keys.addOutputComponent("expnh","default","the experimental N nitrogen chemical shifts");
     515          76 :   keys.addOutputComponent("expca","default","the experimental Ca carbon chemical shifts");
     516          76 :   keys.addOutputComponent("expcb","default","the experimental Cb carbon chemical shifts");
     517          76 :   keys.addOutputComponent("expco","default","the experimental C' carbon chemical shifts");
     518          19 : }
     519             : 
     520          18 : CS2Backbone::CS2Backbone(const ActionOptions&ao):
     521             :   PLUMED_METAINF_INIT(ao),
     522             :   max_cs_atoms(0),
     523             :   camshift(false),
     524             :   pbc(true),
     525          36 :   serial(false)
     526             : {
     527             :   vector<AtomNumber> used_atoms;
     528          36 :   parseAtomList("ATOMS",used_atoms);
     529             : 
     530          36 :   parseFlag("CAMSHIFT",camshift);
     531          23 :   if(camshift&&getDoScore()) plumed_merror("It is not possible to use CAMSHIFT and DOSCORE at the same time");
     532             : 
     533          18 :   bool nopbc=!pbc;
     534          36 :   parseFlag("NOPBC",nopbc);
     535          18 :   pbc=!nopbc;
     536             : 
     537          36 :   parseFlag("SERIAL",serial);
     538             : 
     539          18 :   bool noexp=false;
     540          36 :   parseFlag("NOEXP",noexp);
     541             : 
     542             :   string stringa_data;
     543          36 :   parse("DATADIR",stringa_data);
     544             : 
     545             :   string stringa_template;
     546          36 :   parse("TEMPLATE",stringa_template);
     547             : 
     548          18 :   box_count=0;
     549          18 :   box_nupdate=20;
     550          36 :   parse("NEIGH_FREQ", box_nupdate);
     551             : 
     552          36 :   string stringadb  = stringa_data + string("/camshift.db");
     553          54 :   string stringapdb = stringa_data + string("/") + stringa_template;
     554             : 
     555             :   /* Lenght conversion (parameters are tuned for angstrom) */
     556             :   double scale=1.;
     557          36 :   if(!plumed.getAtoms().usingNaturalUnits()) {
     558          18 :     scale = 10.*atoms.getUnits().getLength();
     559             :   }
     560             : 
     561          18 :   log.printf("  Initialization of the predictor ...\n");
     562          18 :   db.parse(stringadb,scale);
     563             : 
     564          36 :   PDB pdb;
     565          36 :   if( !pdb.read(stringapdb,plumed.getAtoms().usingNaturalUnits(),1./scale) ) plumed_merror("missing input file " + stringapdb);
     566             : 
     567             :   // first of all we build the list of chemical shifts we want to predict
     568          18 :   log.printf("  Reading experimental data ...\n"); log.flush();
     569          54 :   stringadb = stringa_data + string("/CAshifts.dat");
     570          18 :   log.printf("  Initializing CA shifts %s\n", stringadb.c_str());
     571          36 :   init_cs(stringadb, "CA", pdb);
     572          54 :   stringadb = stringa_data + string("/CBshifts.dat");
     573          18 :   log.printf("  Initializing CB shifts %s\n", stringadb.c_str());
     574          36 :   init_cs(stringadb, "CB", pdb);
     575          54 :   stringadb = stringa_data + string("/Cshifts.dat");
     576          18 :   log.printf("  Initializing C' shifts %s\n", stringadb.c_str());
     577          36 :   init_cs(stringadb, "C", pdb);
     578          54 :   stringadb = stringa_data + string("/HAshifts.dat");
     579          18 :   log.printf("  Initializing HA shifts %s\n", stringadb.c_str());
     580          36 :   init_cs(stringadb, "HA", pdb);
     581          54 :   stringadb = stringa_data + string("/Hshifts.dat");
     582          18 :   log.printf("  Initializing H shifts %s\n", stringadb.c_str());
     583          36 :   init_cs(stringadb, "H", pdb);
     584          54 :   stringadb = stringa_data + string("/Nshifts.dat");
     585          18 :   log.printf("  Initializing N shifts %s\n", stringadb.c_str());
     586          36 :   init_cs(stringadb, "N", pdb);
     587             : 
     588          18 :   if(chemicalshifts.size()==0) plumed_merror("There are no chemical shifts to calculate, there must be at least a not empty file (CA|CB|C|HA|H|N|shifts.dat)");
     589             : 
     590          18 :   init_types(pdb);
     591          18 :   init_rings(pdb);
     592             : 
     593          18 :   log<<"  Bibliography "
     594          72 :      <<plumed.cite("Kohlhoff K, Robustelli P, Cavalli A, Salvatella A, Vendruscolo M, J. Am. Chem. Soc. 131, 13894 (2009)");
     595          28 :   if(camshift) log<<plumed.cite("Granata D, Camilloni C, Vendruscolo M, Laio A, Proc. Natl. Acad. Sci. USA 110, 6817 (2013)");
     596          39 :   else log<<plumed.cite("Camilloni C, Robustelli P, De Simone A, Cavalli A, Vendruscolo M, J. Am. Chem. Soc. 134, 3968 (2012)");
     597          54 :   log<<plumed.cite("Bonomi M, Camilloni C, Bioinformatics, 33, 3999 (2017)");
     598          18 :   log<<"\n";
     599             : 
     600          18 :   if(camshift) {
     601           5 :     noexp = true;
     602           5 :     addValueWithDerivatives();
     603           5 :     setNotPeriodic();
     604             :   } else {
     605       15327 :     for(unsigned cs=0; cs<chemicalshifts.size(); cs++) {
     606        7657 :       std::string num; Tools::convert(chemicalshifts[cs].res_num,num);
     607        7657 :       std::string chain_num; Tools::convert(chemicalshifts[cs].chain,chain_num);
     608        7657 :       if(getDoScore()) {
     609       11780 :         addComponent(chemicalshifts[cs].nucleus+chain_num+"-"+num);
     610       11780 :         componentIsNotPeriodic(chemicalshifts[cs].nucleus+chain_num+"-"+num);
     611       11780 :         chemicalshifts[cs].comp = getPntrToComponent(chemicalshifts[cs].nucleus+chain_num+"-"+num);
     612        2356 :         setParameter(chemicalshifts[cs].exp_cs);
     613             :       } else {
     614       26505 :         addComponentWithDerivatives(chemicalshifts[cs].nucleus+chain_num+"-"+num);
     615       26505 :         componentIsNotPeriodic(chemicalshifts[cs].nucleus+chain_num+"-"+num);
     616       26505 :         chemicalshifts[cs].comp = getPntrToComponent(chemicalshifts[cs].nucleus+chain_num+"-"+num);
     617             :       }
     618             :     }
     619          13 :     if(getDoScore()) Initialise(chemicalshifts.size());
     620             :   }
     621             : 
     622          18 :   if(!noexp) {
     623       15327 :     for(unsigned cs=0; cs<chemicalshifts.size(); cs++) {
     624        7657 :       std::string num; Tools::convert(chemicalshifts[cs].res_num,num);
     625        7657 :       std::string chain_num; Tools::convert(chemicalshifts[cs].chain,chain_num);
     626       45942 :       addComponent("exp"+chemicalshifts[cs].nucleus+chain_num+"-"+num);
     627       45942 :       componentIsNotPeriodic("exp"+chemicalshifts[cs].nucleus+chain_num+"-"+num);
     628       45942 :       Value* comp=getPntrToComponent("exp"+chemicalshifts[cs].nucleus+chain_num+"-"+num);
     629        7657 :       comp->set(chemicalshifts[cs].exp_cs);
     630             :     }
     631             :   }
     632             : 
     633          18 :   requestAtoms(used_atoms, false);
     634          18 :   setDerivatives();
     635          18 :   checkRead();
     636          18 : }
     637             : 
     638         108 : void CS2Backbone::init_cs(const string &file, const string &nucl, const PDB &pdb) {
     639             :   // number of chains
     640             :   vector<string> chains;
     641         108 :   pdb.getChainNames( chains );
     642             :   unsigned ichain=0;
     643             : 
     644         216 :   ifstream in;
     645         108 :   in.open(file.c_str());
     646         108 :   if(!in) return;
     647         108 :   istream_iterator<string> iter(in), end;
     648             :   unsigned begin=0;
     649             : 
     650       19116 :   while(iter!=end) {
     651       19008 :     string tok = *iter;
     652             :     ++iter;
     653       19008 :     if(tok[0]=='#') {
     654             :       ++iter;
     655         432 :       if(begin==1) {
     656             :         begin=0;
     657         216 :         ichain++;
     658             :       } else begin=1;
     659             :       continue;
     660             :     }
     661             :     int ro = atoi(tok.c_str());
     662       18576 :     if(ro<0) plumed_merror("Residue numbers should be positive\n");
     663       18576 :     unsigned resnum = static_cast<unsigned> (ro);
     664             :     tok = *iter;
     665             :     ++iter;
     666             :     double cs = atof(tok.c_str());
     667       18576 :     if(cs==0) continue;
     668             : 
     669             :     unsigned fres, lres;
     670             :     string errmsg;
     671       22140 :     pdb.getResidueRange(chains[ichain], fres, lres, errmsg);
     672       11070 :     if(resnum==fres||resnum==lres) plumed_merror("First and Last residue of each chain should be annotated as # in " + file + " Remember that residue numbers should match");
     673             : 
     674             :     // check in the PDB for the chain/residue/atom and enable the chemical shift
     675       11070 :     string RES = pdb.getResidueName(resnum, chains[ichain]);
     676       98766 :     if(RES=="HIE"||RES=="HIP"||RES=="HIS"||RES=="HSP"||RES=="HSE"||RES=="CYS"||RES=="GLH"||RES=="ASH"||RES=="UNK") continue;
     677       10944 :     if(RES=="GLN"&&nucl=="CB") continue;
     678       11322 :     if(RES=="ILE"&&nucl=="CB") continue;
     679       10998 :     if(RES=="PRO"&&nucl=="N") continue;
     680       10998 :     if(RES=="PRO"&&nucl=="H") continue;
     681       10998 :     if(RES=="PRO"&&nucl=="CB") continue;
     682       12240 :     if(RES=="GLY"&&nucl=="HA") continue;
     683       12240 :     if(RES=="GLY"&&nucl=="CB") continue;
     684             : 
     685       21204 :     ChemicalShift tmp_cs;
     686             : 
     687       10602 :     tmp_cs.exp_cs = cs;
     688       10602 :     if(nucl=="CA")      tmp_cs.nucleus = "ca-";
     689        7668 :     else if(nucl=="CB") tmp_cs.nucleus = "cb-";
     690        5616 :     else if(nucl=="C")  tmp_cs.nucleus = "co-";
     691        5616 :     else if(nucl=="HA") tmp_cs.nucleus = "ha-";
     692        5616 :     else if(nucl=="H")  tmp_cs.nucleus = "hn-";
     693        2808 :     else if(nucl=="N")  tmp_cs.nucleus = "nh-";
     694       10602 :     tmp_cs.chain = ichain;
     695       10602 :     tmp_cs.res_num = resnum;
     696       10602 :     tmp_cs.res_type_curr = frag2enum(RES);
     697       21204 :     tmp_cs.res_type_prev = frag2enum(pdb.getResidueName(resnum-1, chains[ichain]));
     698       21204 :     tmp_cs.res_type_next = frag2enum(pdb.getResidueName(resnum+1, chains[ichain]));
     699             :     tmp_cs.res_name = RES;
     700       10602 :     tmp_cs.res_kind = db.kind(RES);
     701       10602 :     tmp_cs.atm_kind = db.atom_kind(nucl);
     702       20556 :     if(RES!="ALA"&&RES!="GLY") {tmp_cs.bb.resize(18); tmp_cs.has_chi1=true;}
     703        2142 :     else {tmp_cs.bb.resize(16); tmp_cs.has_chi1=false;}
     704             : 
     705       10602 :     vector<AtomNumber> res_atoms = pdb.getAtomsInResidue(resnum, chains[ichain]);
     706             :     // find the position of the nucleus and of the other backbone atoms as well as for phi/psi/chi
     707      335934 :     for(unsigned a=0; a<res_atoms.size(); a++) {
     708      162666 :       string AN = pdb.getAtomName(res_atoms[a]);
     709      162666 :       if(nucl=="HA"&&(AN=="HA"||AN=="HA1"||AN=="HA3")) tmp_cs.ipos = res_atoms[a].index();
     710      247338 :       else if(nucl=="H"&&(AN=="H"||AN=="HN"))          tmp_cs.ipos = res_atoms[a].index();
     711      205056 :       else if(nucl=="N"&&AN=="N")                      tmp_cs.ipos = res_atoms[a].index();
     712      203796 :       else if(nucl=="CA"&&AN=="CA")                    tmp_cs.ipos = res_atoms[a].index();
     713      190296 :       else if(nucl=="CB"&&AN=="CB")                    tmp_cs.ipos = res_atoms[a].index();
     714      152064 :       else if(nucl=="C"&&AN=="C" )                     tmp_cs.ipos = res_atoms[a].index();
     715             :     }
     716             : 
     717       10602 :     vector<AtomNumber> prev_res_atoms = pdb.getAtomsInResidue(resnum-1, chains[ichain]);
     718             :     // find the position of the previous residues backbone atoms
     719      336996 :     for(unsigned a=0; a<prev_res_atoms.size(); a++) {
     720      157896 :       string AN = pdb.getAtomName(prev_res_atoms[a]);
     721      168498 :       if(AN=="N")                             { tmp_cs.bb[Np]  = prev_res_atoms[a].index(); }
     722      157896 :       else if(AN=="CA")                       { tmp_cs.bb[CAp] = prev_res_atoms[a].index(); }
     723      401292 :       else if(AN=="HA"||AN=="HA1"||AN=="HA3") { tmp_cs.bb[HAp] = prev_res_atoms[a].index(); }
     724      136692 :       else if(AN=="C" )                       { tmp_cs.bb[Cp]  = prev_res_atoms[a].index(); }
     725      126090 :       else if(AN=="O" )                       { tmp_cs.bb[Op]  = prev_res_atoms[a].index(); }
     726             :     }
     727             : 
     728      335934 :     for(unsigned a=0; a<res_atoms.size(); a++) {
     729      162666 :       string AN = pdb.getAtomName(res_atoms[a]);
     730      173268 :       if(AN=="N")                                         { tmp_cs.bb[Nc]  = res_atoms[a].index(); }
     731      448884 :       else if(AN=="H" ||AN=="HN"||(AN=="CD"&&RES=="PRO")) { tmp_cs.bb[Hc]  = res_atoms[a].index(); }
     732      152064 :       else if(AN=="CA")                                   { tmp_cs.bb[CAc] = res_atoms[a].index(); }
     733      383472 :       else if(AN=="HA"||AN=="HA1"||AN=="HA3")             { tmp_cs.bb[HAc] = res_atoms[a].index(); }
     734      130860 :       else if(AN=="C" )                                   { tmp_cs.bb[Cc]  = res_atoms[a].index(); }
     735      120258 :       else if(AN=="O" )                                   { tmp_cs.bb[Oc]  = res_atoms[a].index(); }
     736             : 
     737      318852 :       if(RES!="ALA"&&RES!="GLY") {
     738      154188 :         if(AN=="CB") tmp_cs.bb[CBc] = res_atoms[a].index();
     739      154188 :         if(is_chi1_cx(RES,AN)) tmp_cs.bb[CGc] = res_atoms[a].index();
     740             :       }
     741             :     }
     742             : 
     743       10602 :     vector<AtomNumber> next_res_atoms = pdb.getAtomsInResidue(resnum+1, chains[ichain]);
     744       10602 :     string NRES = pdb.getResidueName(resnum+1, chains[ichain]);
     745             :     // find the position of the previous residues backbone atoms
     746      337896 :     for(unsigned a=0; a<next_res_atoms.size(); a++) {
     747      158346 :       string AN = pdb.getAtomName(next_res_atoms[a]);
     748      168948 :       if(AN=="N")                                          { tmp_cs.bb[Nn]  = next_res_atoms[a].index(); }
     749      436698 :       else if(AN=="H" ||AN=="HN"||(AN=="CD"&&NRES=="PRO")) { tmp_cs.bb[Hn]  = next_res_atoms[a].index(); }
     750      147744 :       else if(AN=="CA")                                    { tmp_cs.bb[CAn] = next_res_atoms[a].index(); }
     751      370800 :       else if(AN=="HA"||AN=="HA1"||AN=="HA3")              { tmp_cs.bb[HAn] = next_res_atoms[a].index(); }
     752      126540 :       else if(AN=="C" )                                    { tmp_cs.bb[Cn]  = next_res_atoms[a].index(); }
     753             :     }
     754             : 
     755             :     // set sidechain atoms
     756       21204 :     vector<string> sc_atm = side_chain_atoms(RES);
     757             : 
     758      271566 :     for(unsigned sc=0; sc<sc_atm.size(); sc++) {
     759     4871934 :       for(unsigned aa=0; aa<res_atoms.size(); aa++) {
     760     4741452 :         if(pdb.getAtomName(res_atoms[aa])==sc_atm[sc]) {
     761      198324 :           tmp_cs.side_chain.push_back(res_atoms[aa].index());
     762             :         }
     763             :       }
     764             :     }
     765             : 
     766             :     // find atoms for extra distances
     767      296856 :     const string atomsP1[] =  {"H", "H", "H", "C", "C", "C", "O", "O", "O", "N", "N", "N", "O", "O", "O", "N", "N", "N", "CG", "CG", "CG", "CG", "CG", "CG", "CG", "CA"};
     768       10602 :     const int resOffsetP1[] = { 0,   0,   0,  -1,  -1,  -1,   0,   0,   0,   1,   1,   1,   -1,  -1,  -1,  0,   0,   0,   0,    0,    0,    0,    0,    -1,   1,    -1};
     769             : 
     770      296856 :     const string atomsP2[] =  {"HA", "C", "CB", "HA", "C", "CB", "HA", "N", "CB", "HA", "N", "CB", "HA", "N", "CB", "HA", "N", "CB", "HA", "N", "C", "C", "N", "CA", "CA", "CA"};
     771       10602 :     const int resOffsetP2[] = { 0,    0,   0,    0,    0,   0,    0,    0,   0,    0,    0,   0,    -1,  -1,   -1,   -1,  -1,   -1,   0,    0,   0,   -1,  1,   0,    0,    1};
     772             : 
     773      286254 :     for(unsigned q=0; q<db.get_numXtraDists()-1; q++) {
     774             :       vector<AtomNumber> at1;
     775      275652 :       if(resOffsetP1[q]== 0) at1 = res_atoms;
     776      275652 :       if(resOffsetP1[q]==-1) at1 = prev_res_atoms;
     777      275652 :       if(resOffsetP1[q]==+1) at1 = next_res_atoms;
     778             : 
     779             :       vector<AtomNumber> at2;
     780      275652 :       if(resOffsetP2[q]== 0) at2 = res_atoms;
     781      275652 :       if(resOffsetP2[q]==-1) at2 = prev_res_atoms;
     782      275652 :       if(resOffsetP2[q]==+1) at2 = next_res_atoms;
     783             : 
     784      275652 :       int tmp1 = -1;
     785     4395132 :       for(unsigned a=0; a<at1.size(); a++) {
     786     2181708 :         string name = pdb.getAtomName(at1[a]);
     787     2181708 :         xdist_name_map(name);
     788             : 
     789     2181708 :         if(name==atomsP1[q]) {
     790      259794 :           tmp1 = at1[a].index();
     791             :           break;
     792             :         }
     793             :       }
     794             : 
     795      275652 :       int tmp2 = -1;
     796     2855376 :       for(unsigned a=0; a<at2.size(); a++) {
     797     1418076 :         string name = pdb.getAtomName(at2[a]);
     798     1418076 :         xdist_name_map(name);
     799             : 
     800     1418076 :         if(name==atomsP2[q]) {
     801      266040 :           tmp2 = at2[a].index();
     802             :           break;
     803             :         }
     804             :       }
     805             : 
     806      275652 :       tmp_cs.xd1.push_back(tmp1);
     807      275652 :       tmp_cs.xd2.push_back(tmp2);
     808             :     }
     809             : 
     810             :     // ready to add a new chemical shifts
     811       21204 :     tmp_cs.csatoms = 1 + 16 + tmp_cs.side_chain.size() + 2*tmp_cs.xd1.size();
     812       20556 :     if(tmp_cs.res_name!="ALA"&&tmp_cs.res_name!="GLY") tmp_cs.csatoms += 2;
     813       10602 :     chemicalshifts.push_back(tmp_cs);
     814             :   }
     815             : 
     816         216 :   in.close();
     817             : }
     818             : 
     819             : // this assigns an atom-type to each atom of the pdb
     820          18 : void CS2Backbone::init_types(const PDB &pdb) {
     821             :   enum atom_t {D_C, D_H, D_N, D_O, D_S, D_C2, D_N2, D_O2};
     822          18 :   vector<AtomNumber> aa = pdb.getAtomNumbers();
     823       94050 :   for(unsigned i=0; i<aa.size(); i++) {
     824       47016 :     unsigned frag = pdb.getResidueNumber(aa[i]);
     825       47016 :     string fragName = pdb.getResidueName(aa[i]);
     826       47016 :     string atom_name = pdb.getAtomName(aa[i]);
     827       47016 :     char atom_type = atom_name[0];
     828       51282 :     if(isdigit(atom_name[0])) atom_type = atom_name[1];
     829       47016 :     res_num.push_back(frag);
     830       47016 :     unsigned t = 0;
     831       47016 :     if (!isSP2(fragName, atom_name)) {
     832       36936 :       if (atom_type == 'C') t = D_C;
     833       27918 :       else if (atom_type == 'O') t = D_O;
     834       27450 :       else if (atom_type == 'H') t = D_H;
     835        4194 :       else if (atom_type == 'N') t = D_N;
     836         162 :       else if (atom_type == 'S') t = D_S;
     837           0 :       else plumed_merror("Unknown atom type: " + atom_name);
     838             :     } else {
     839       10080 :       if (atom_type == 'C') t = D_C2;
     840        4104 :       else if (atom_type == 'O') t = D_O2;
     841           0 :       else if (atom_type == 'N') t = D_N2;
     842           0 :       else plumed_merror("Unknown atom type: " + atom_name);
     843             :     }
     844       47016 :     type.push_back(t);
     845             :   }
     846          18 : }
     847             : 
     848          18 : void CS2Backbone::init_rings(const PDB &pdb)
     849             : {
     850          18 :   const string pheTyr_n[] = {"CG","CD1","CE1","CZ","CE2","CD2"};
     851         144 :   const string trp1_n[]   = {"CD2","CE2","CZ2","CH2","CZ3","CE3"};
     852         126 :   const string trp2_n[]   = {"CG","CD1","NE1","CE2","CD2"};
     853         126 :   const string his_n[]    = {"CG","ND1","CD2","CE1","NE2"};
     854             : 
     855             :   // number of chains
     856          18 :   vector<string> chains;
     857          18 :   pdb.getChainNames( chains );
     858             :   unsigned total_rings_atoms = 0;
     859             : 
     860             :   // cycle over chains
     861          90 :   for(unsigned i=0; i<chains.size(); i++) {
     862             :     unsigned start, end;
     863             :     string errmsg;
     864          36 :     pdb.getResidueRange( chains[i], start, end, errmsg );
     865             :     // cycle over residues
     866        3168 :     for(unsigned res=start; res<end; res++) {
     867        3132 :       string frg = pdb.getResidueName(res, chains[i]);
     868       11574 :       if(!((frg=="PHE")||(frg=="TYR")||(frg=="TRP")||
     869        8370 :            (frg=="HIS")||(frg=="HIP")||(frg=="HID")||
     870        5580 :            (frg=="HIE")||(frg=="HSD")||(frg=="HSE")||
     871        5922 :            (frg=="HSP"))) continue;
     872             : 
     873         342 :       vector<AtomNumber> frg_atoms = pdb.getAtomsInResidue(res,chains[i]);
     874             : 
     875         396 :       if(frg=="PHE"||frg=="TYR") {
     876         324 :         RingInfo ri;
     877       13680 :         for(unsigned a=0; a<frg_atoms.size(); a++) {
     878             :           unsigned atm = frg_atoms[a].index();
     879       38808 :           for(unsigned aa=0; aa<6; aa++) {
     880      102708 :             if(pdb.getAtomName(frg_atoms[a])==pheTyr_n[aa]) {
     881        1944 :               ri.atom[aa] = atm;
     882        1944 :               break;
     883             :             }
     884             :           }
     885             :         }
     886         324 :         ri.numAtoms = 6;
     887         324 :         total_rings_atoms += 6;
     888         324 :         if(frg=="PHE") ri.rtype = RingInfo::R_PHE;
     889         324 :         if(frg=="TYR") ri.rtype = RingInfo::R_TYR;
     890         324 :         ringInfo.push_back(ri);
     891             : 
     892          18 :       } else if(frg=="TRP") {
     893             :         //First ring
     894          18 :         RingInfo ri;
     895         900 :         for(unsigned a=0; a<frg_atoms.size(); a++) {
     896             :           unsigned atm = frg_atoms[a].index();
     897        2646 :           for(unsigned aa=0; aa<6; aa++) {
     898        6966 :             if(pdb.getAtomName(frg_atoms[a])==trp1_n[aa]) {
     899         108 :               ri.atom[aa] = atm;
     900         108 :               break;
     901             :             }
     902             :           }
     903             :         }
     904          18 :         ri.numAtoms = 6;
     905             :         total_rings_atoms += 6;
     906          18 :         ri.rtype = RingInfo::R_TRP1;
     907          18 :         ringInfo.push_back(ri);
     908             :         //Second Ring
     909          18 :         RingInfo ri2;
     910         900 :         for(unsigned a=0; a<frg_atoms.size(); a++) {
     911             :           unsigned atm = frg_atoms[a].index();
     912        2322 :           for(unsigned aa=0; aa<5; aa++) {
     913        5940 :             if(pdb.getAtomName(frg_atoms[a])==trp2_n[aa]) {
     914          90 :               ri2.atom[aa] = atm;
     915          90 :               break;
     916             :             }
     917             :           }
     918             :         }
     919          18 :         ri2.numAtoms = 5;
     920          18 :         total_rings_atoms += 3;
     921          18 :         ri2.rtype = RingInfo::R_TRP2;
     922          18 :         ringInfo.push_back(ri2);
     923             : 
     924           0 :       } else if((frg=="HIS")||(frg=="HIP")||(frg=="HID")||
     925           0 :                 (frg=="HIE")||(frg=="HSD")||(frg=="HSE")||
     926             :                 (frg=="HSP")) {//HIS case
     927           0 :         RingInfo ri;
     928           0 :         for(unsigned a=0; a<frg_atoms.size(); a++) {
     929             :           unsigned atm = frg_atoms[a].index();
     930           0 :           for(unsigned aa=0; aa<5; aa++) {
     931           0 :             if(pdb.getAtomName(frg_atoms[a])==his_n[aa]) {
     932           0 :               ri.atom[aa] = atm;
     933           0 :               break;
     934             :             }
     935             :           }
     936             :         }
     937           0 :         ri.numAtoms = 5;
     938           0 :         total_rings_atoms += 3;
     939           0 :         ri.rtype = RingInfo::R_HIS;
     940           0 :         ringInfo.push_back(ri);
     941             :       } else {
     942           0 :         plumed_merror("Unknown Ring Fragment: " + frg);
     943             :       }
     944             :     }
     945             :   }
     946             : 
     947       21348 :   for(unsigned cs=0; cs<chemicalshifts.size(); cs++) chemicalshifts[cs].csatoms += total_rings_atoms;
     948          18 : }
     949             : 
     950          18 : void CS2Backbone::calculate()
     951             : {
     952          18 :   if(pbc) makeWhole();
     953          18 :   if(getExchangeStep()) box_count=0;
     954          18 :   if(box_count==0) update_neighb();
     955          18 :   compute_ring_parameters();
     956             : 
     957          18 :   vector<double> camshift_sigma2(6);
     958          18 :   camshift_sigma2[0] = 0.08; // HA
     959          18 :   camshift_sigma2[1] = 0.30; // HN
     960          18 :   camshift_sigma2[2] = 9.00; // NH
     961          18 :   camshift_sigma2[3] = 1.30; // CA
     962          18 :   camshift_sigma2[4] = 1.56; // CB
     963          18 :   camshift_sigma2[5] = 1.70; // CO
     964             : 
     965             :   vector<Vector>   cs_derivs;
     966             :   vector<Vector>   aa_derivs;
     967             :   vector<unsigned> cs_atoms;
     968             :   vector<double>   all_shifts;
     969             : 
     970          36 :   cs_derivs.resize(chemicalshifts.size()*max_cs_atoms,Vector(0,0,0));
     971          36 :   cs_atoms.resize(chemicalshifts.size()*max_cs_atoms,0);
     972          36 :   all_shifts.resize(chemicalshifts.size(),0);
     973          40 :   if(camshift||getDoScore()) aa_derivs.resize(getNumberOfAtoms(),Vector(0,0,0));
     974             : 
     975          18 :   unsigned stride = comm.Get_size();
     976          18 :   unsigned rank   = comm.Get_rank();
     977          18 :   if(serial) {
     978             :     stride = 1;
     979             :     rank   = 0;
     980             :   }
     981             : 
     982          18 :   unsigned nt=OpenMP::getNumThreads();
     983          36 :   if(nt*stride*2>chemicalshifts.size()) nt=1;
     984             : 
     985             :   // a single loop over all chemical shifts
     986          54 :   #pragma omp parallel num_threads(nt)
     987             :   {
     988          36 :     #pragma omp for schedule(dynamic)
     989          36 :     for(unsigned cs=rank; cs<chemicalshifts.size(); cs+=stride) {
     990        5870 :       const unsigned kdx=cs*max_cs_atoms;
     991        5870 :       const ChemicalShift *myfrag = &chemicalshifts[cs];
     992        5870 :       const unsigned aa_kind = myfrag->res_kind;
     993        5870 :       const unsigned at_kind = myfrag->atm_kind;
     994             : 
     995       11740 :       double shift = db.CONSTAAPREV(aa_kind,at_kind)[myfrag->res_type_prev] +
     996        5870 :                      db.CONSTAACURR(aa_kind,at_kind)[myfrag->res_type_curr] +
     997        5870 :                      db.CONSTAANEXT(aa_kind,at_kind)[myfrag->res_type_next];
     998             : 
     999        5870 :       const unsigned ipos = myfrag->ipos;
    1000       11740 :       cs_atoms[kdx+0] = ipos;
    1001             :       unsigned atom_counter = 1;
    1002             : 
    1003             :       //BACKBONE (PREV CURR NEXT)
    1004        5870 :       const double * CONST_BB2 = db.CONST_BB2(aa_kind,at_kind);
    1005             :       const unsigned bbsize = 16;
    1006       99849 :       for(unsigned q=0; q<bbsize; q++) {
    1007       93961 :         const double cb2q = CONST_BB2[q];
    1008      110892 :         if(cb2q==0.) continue;
    1009       76994 :         const unsigned jpos = myfrag->bb[q];
    1010       76994 :         if(ipos==jpos) continue;
    1011      231090 :         const Vector distance = delta(getPosition(jpos),getPosition(ipos));
    1012       77075 :         const double d = distance.modulo();
    1013       77171 :         const double fact = cb2q/d;
    1014             : 
    1015       77171 :         shift += cb2q*d;
    1016       77171 :         const Vector der = fact*distance;
    1017             : 
    1018      154582 :         cs_derivs[kdx+0] += der;
    1019      154576 :         cs_derivs[kdx+q+atom_counter] = -der;
    1020      154096 :         cs_atoms[kdx+q+atom_counter] = jpos;
    1021             :       }
    1022             : 
    1023             :       atom_counter += bbsize;
    1024             : 
    1025             :       //DIHEDRAL ANGLES
    1026        5888 :       const double *CO_DA = db.CO_DA(aa_kind,at_kind);
    1027             :       //Phi
    1028             :       {
    1029       17664 :         const Vector d0 = delta(getPosition(myfrag->bb[Nc]), getPosition(myfrag->bb[Cp]));
    1030       17664 :         const Vector d1 = delta(getPosition(myfrag->bb[CAc]), getPosition(myfrag->bb[Nc]));
    1031       17667 :         const Vector d2 = delta(getPosition(myfrag->bb[Cc]), getPosition(myfrag->bb[CAc]));
    1032             :         Torsion t;
    1033        5889 :         Vector dd0, dd1, dd2;
    1034        5890 :         const double t_phi = t.compute(d0,d1,d2,dd0,dd1,dd2);
    1035        5889 :         const double *PARS_DA = db.PARS_DA(aa_kind,at_kind,0);
    1036        5889 :         const double val1 = 3.*t_phi+PARS_DA[3];
    1037        5889 :         const double val2 = t_phi+PARS_DA[4];
    1038        5889 :         shift += CO_DA[0]*(PARS_DA[0]*cos(val1)+PARS_DA[1]*cos(val2)+PARS_DA[2]);
    1039        5889 :         const double fact = -CO_DA[0]*(+3.*PARS_DA[0]*sin(val1)+PARS_DA[1]*sin(val2));
    1040             : 
    1041       11779 :         cs_derivs[kdx+Cp+1] += fact*dd0;
    1042       11779 :         cs_derivs[kdx+Nc+1] += fact*(dd1-dd0);
    1043       11777 :         cs_derivs[kdx+CAc+1]+= fact*(dd2-dd1);
    1044       11779 :         cs_derivs[kdx+Cc+1] += -fact*dd2;
    1045       11780 :         cs_atoms[kdx+Cp+1] = myfrag->bb[Cp];
    1046       11780 :         cs_atoms[kdx+Nc+1] = myfrag->bb[Nc];
    1047       11780 :         cs_atoms[kdx+CAc+1]= myfrag->bb[CAc];
    1048       11780 :         cs_atoms[kdx+Cc+1] = myfrag->bb[Cc];
    1049             :       }
    1050             : 
    1051             :       //Psi
    1052             :       {
    1053       17670 :         const Vector d0 = delta(getPosition(myfrag->bb[CAc]), getPosition(myfrag->bb[Nc]));
    1054       17655 :         const Vector d1 = delta(getPosition(myfrag->bb[Cc]), getPosition(myfrag->bb[CAc]));
    1055       17670 :         const Vector d2 = delta(getPosition(myfrag->bb[Nn]), getPosition(myfrag->bb[Cc]));
    1056             :         Torsion t;
    1057        5890 :         Vector dd0, dd1, dd2;
    1058        5890 :         const double t_psi = t.compute(d0,d1,d2,dd0,dd1,dd2);
    1059        5890 :         const double *PARS_DA = db.PARS_DA(aa_kind,at_kind,1);
    1060        5890 :         const double val1 = 3.*t_psi+PARS_DA[3];
    1061        5890 :         const double val2 = t_psi+PARS_DA[4];
    1062        5890 :         shift += CO_DA[1]*(PARS_DA[0]*cos(val1)+PARS_DA[1]*cos(val2)+PARS_DA[2]);
    1063        5890 :         const double fact = -CO_DA[1]*(+3.*PARS_DA[0]*sin(val1)+PARS_DA[1]*sin(val2));
    1064             : 
    1065       11779 :         cs_derivs[kdx+Nc+1] += fact*dd0;
    1066       11776 :         cs_derivs[kdx+CAc+1] += fact*(dd1-dd0);
    1067       11780 :         cs_derivs[kdx+Cc+1] += fact*(dd2-dd1);
    1068       11780 :         cs_derivs[kdx+Nn+1] += -fact*dd2;
    1069       12524 :         cs_atoms[kdx+Nc+1] = myfrag->bb[Nc];
    1070       12524 :         cs_atoms[kdx+CAc+1]= myfrag->bb[CAc];
    1071       12524 :         cs_atoms[kdx+Cc+1] = myfrag->bb[Cc];
    1072       12524 :         cs_atoms[kdx+Nn+1] = myfrag->bb[Nn];
    1073             :       }
    1074             : 
    1075             :       //Chi
    1076        6262 :       if(myfrag->has_chi1) {
    1077       14088 :         const Vector d0 = delta(getPosition(myfrag->bb[CAc]), getPosition(myfrag->bb[Nc]));
    1078       14082 :         const Vector d1 = delta(getPosition(myfrag->bb[CBc]), getPosition(myfrag->bb[CAc]));
    1079       14094 :         const Vector d2 = delta(getPosition(myfrag->bb[CGc]), getPosition(myfrag->bb[CBc]));
    1080             :         Torsion t;
    1081        4700 :         Vector dd0, dd1, dd2;
    1082        4700 :         const double t_chi1 = t.compute(d0,d1,d2,dd0,dd1,dd2);
    1083        4700 :         const double *PARS_DA = db.PARS_DA(aa_kind,at_kind,2);
    1084        4700 :         const double val1 = 3.*t_chi1+PARS_DA[3];
    1085        4700 :         const double val2 = t_chi1+PARS_DA[4];
    1086        4700 :         shift += CO_DA[2]*(PARS_DA[0]*cos(val1)+PARS_DA[1]*cos(val2)+PARS_DA[2]);
    1087        4700 :         const double fact = -CO_DA[2]*(+3.*PARS_DA[0]*sin(val1)+PARS_DA[1]*sin(val2));
    1088             : 
    1089        9400 :         cs_derivs[kdx+Nc+1] += fact*dd0;
    1090        9400 :         cs_derivs[kdx+CAc+1] += fact*(dd1-dd0);
    1091        9400 :         cs_derivs[kdx+CBc+1] += fact*(dd2-dd1);
    1092        9400 :         cs_derivs[kdx+CGc+1] += -fact*dd2;
    1093        9400 :         cs_atoms[kdx+Nc+1]  = myfrag->bb[Nc];
    1094        9400 :         cs_atoms[kdx+CAc+1] = myfrag->bb[CAc];
    1095        9400 :         cs_atoms[kdx+CBc+1] = myfrag->bb[CBc];
    1096        9400 :         cs_atoms[kdx+CGc+1] = myfrag->bb[CGc];
    1097             : 
    1098             :         atom_counter += 2;
    1099             :       }
    1100             :       //END OF DIHE
    1101             : 
    1102             :       //SIDE CHAIN
    1103        6266 :       const double * CONST_SC2 = db.CONST_SC2(aa_kind,at_kind,myfrag->res_type_curr);
    1104        6266 :       const unsigned sidsize = myfrag->side_chain.size();
    1105       61319 :       for(unsigned q=0; q<sidsize; q++) {
    1106       55061 :         const double cs2q = CONST_SC2[q];
    1107       86520 :         if(cs2q==0.) continue;
    1108       23599 :         const unsigned jpos = myfrag->side_chain[q];
    1109       23599 :         if(ipos==jpos) continue;
    1110       70806 :         const Vector distance = delta(getPosition(jpos),getPosition(ipos));
    1111       23605 :         const double d = distance.modulo();
    1112       23604 :         const double fact = cs2q/d;
    1113             : 
    1114       23604 :         shift += cs2q*d;
    1115       23604 :         const Vector der = fact*distance;
    1116       47238 :         cs_derivs[kdx+0] += der;
    1117       47240 :         cs_derivs[kdx+q+atom_counter] = -der;
    1118       47188 :         cs_atoms[kdx+q+atom_counter] = jpos;
    1119             :       }
    1120             : 
    1121        6258 :       atom_counter += sidsize;
    1122             : 
    1123             :       //EXTRA DIST
    1124        6258 :       const double * CONST_XD  = db.CONST_XD(aa_kind,at_kind);
    1125        6258 :       const unsigned xdsize=myfrag->xd1.size();
    1126      158951 :       for(unsigned q=0; q<xdsize; q++) {
    1127      152890 :         const double cxdq = CONST_XD[q];
    1128      167040 :         if(cxdq==0.) continue;
    1129      290100 :         if(myfrag->xd1[q]==-1||myfrag->xd2[q]==-1) continue;
    1130      277480 :         const Vector distance = delta(getPosition(myfrag->xd1[q]),getPosition(myfrag->xd2[q]));
    1131      138767 :         const double d = distance.modulo();
    1132      138912 :         const double fact = cxdq/d;
    1133             : 
    1134      138912 :         shift += cxdq*d;
    1135      138912 :         const Vector der = fact*distance;
    1136      277940 :         cs_derivs[kdx+2*q+atom_counter  ] = der;
    1137      277940 :         cs_derivs[kdx+2*q+atom_counter+1] = -der;
    1138      277086 :         cs_atoms[kdx+2*q+atom_counter] = myfrag->xd2[q];
    1139      277086 :         cs_atoms[kdx+2*q+atom_counter+1] = myfrag->xd1[q];
    1140             :       }
    1141             : 
    1142        6061 :       atom_counter += 2*xdsize;
    1143             : 
    1144             :       //RINGS
    1145        6061 :       const double *rc = db.CO_RING(aa_kind,at_kind);
    1146        6061 :       const unsigned rsize = ringInfo.size();
    1147             :       // cycle over the list of rings
    1148      123749 :       for(unsigned q=0; q<rsize; q++) {
    1149             :         // compute angle from ring middle point to current atom position
    1150             :         // get distance vector from query atom to ring center and normal vector to ring plane
    1151      235360 :         const Vector n   = ringInfo[q].normVect;
    1152      117680 :         const double nL  = ringInfo[q].lengthNV;
    1153      117680 :         const double inL2 = ringInfo[q].lengthN2;
    1154             : 
    1155      235360 :         const Vector d = delta(ringInfo[q].position, getPosition(ipos));
    1156      117715 :         const double dL2 = d.modulo2();
    1157      117793 :         double dL  = sqrt(dL2);
    1158      117793 :         const double idL3 = 1./(dL2*dL);
    1159             : 
    1160      117793 :         const double dn    = dotProduct(d,n);
    1161      117799 :         const double dn2   = dn*dn;
    1162      117799 :         const double dLnL  = dL*nL;
    1163      117799 :         const double dL_nL = dL/nL;
    1164             : 
    1165      117799 :         const double ang2 = dn2*inL2/dL2;
    1166      117799 :         const double u    = 1.-3.*ang2;
    1167      117799 :         const double cc   = rc[ringInfo[q].rtype];
    1168             : 
    1169      117799 :         shift += cc*u*idL3;
    1170             : 
    1171      117799 :         const double fUU    = -6.*dn*inL2;
    1172      117799 :         const double fUQ    = fUU/dL;
    1173      117799 :         const Vector gradUQ = fUQ*(dL2*n - dn*d);
    1174      117796 :         const Vector gradVQ = (3.*dL*u)*d;
    1175             : 
    1176      117785 :         const double fact   = cc*idL3*idL3;
    1177      235573 :         cs_derivs[kdx+0] += fact*(gradUQ - gradVQ);
    1178             : 
    1179      117789 :         const double fU       = fUU/nL;
    1180             :         double OneOverN = 1./6.;
    1181      117789 :         if(ringInfo[q].numAtoms==5) OneOverN=1./3.;
    1182      117789 :         const Vector factor2  = OneOverN*n;
    1183      117784 :         const Vector factor4  = (OneOverN/dL_nL)*d;
    1184             : 
    1185      117787 :         const Vector gradV    = -OneOverN*gradVQ;
    1186             : 
    1187      118540 :         if(ringInfo[q].numAtoms==6) {
    1188             :           // update forces on ring atoms
    1189     1445624 :           for(unsigned at=0; at<6; at++) {
    1190      667339 :             const Vector ab = crossProduct(d,ringInfo[q].g[at]);
    1191      669051 :             const Vector c  = crossProduct(n,ringInfo[q].g[at]);
    1192      670978 :             const Vector factor3 = 0.5*dL_nL*c;
    1193      671328 :             const Vector factor1 = 0.5*ab;
    1194      671133 :             const Vector gradU   = fU*( dLnL*(factor1 - factor2) -dn*(factor3 - factor4) );
    1195     1340362 :             cs_derivs[kdx+at+atom_counter] = fact*(gradU - gradV);
    1196     1332974 :             cs_atoms[kdx+at+atom_counter] = ringInfo[q].atom[at];
    1197             :           }
    1198      111798 :           atom_counter += 6;
    1199             :         }  else {
    1200       41226 :           for(unsigned at=0; at<3; at++) {
    1201       17668 :             const Vector ab = crossProduct(d,ringInfo[q].g[at]);
    1202       17670 :             const Vector c  = crossProduct(n,ringInfo[q].g[at]);
    1203       17670 :             const Vector factor3 = dL_nL*c;
    1204       17669 :             const Vector factor1 = ab;
    1205       17669 :             const Vector gradU   = fU*( dLnL*(factor1 - factor2) -dn*(factor3 - factor4) );
    1206       35340 :             cs_derivs[kdx+at+atom_counter] = fact*(gradU - gradV);
    1207             :           }
    1208       11780 :           cs_atoms[kdx+atom_counter] = ringInfo[q].atom[0];
    1209       11780 :           cs_atoms[kdx+atom_counter+1] = ringInfo[q].atom[2];
    1210       11780 :           cs_atoms[kdx+atom_counter+2] = ringInfo[q].atom[3];
    1211        5890 :           atom_counter += 3;
    1212             :         }
    1213             :       }
    1214             :       //END OF RINGS
    1215             : 
    1216             :       //NON BOND
    1217        6069 :       const double * CONST_CO_SPHERE3 = db.CO_SPHERE(aa_kind,at_kind,0);
    1218        6069 :       const double * CONST_CO_SPHERE  = db.CO_SPHERE(aa_kind,at_kind,1);
    1219        6069 :       const unsigned boxsize = myfrag->box_nb.size();
    1220      250854 :       for(unsigned q=0; q<boxsize; q++) {
    1221      489928 :         const unsigned jpos = myfrag->box_nb[q];
    1222      734892 :         const Vector distance = delta(getPosition(jpos),getPosition(ipos));
    1223      245094 :         const double d2 = distance.modulo2();
    1224             : 
    1225      244869 :         if(d2<cutOffDist2) {
    1226      108356 :           double factor1  = sqrt(d2);
    1227      108356 :           double dfactor1 = 1./factor1;
    1228      108356 :           double factor3  = dfactor1*dfactor1*dfactor1;
    1229      108356 :           double dfactor3 = -3.*factor3*dfactor1*dfactor1;
    1230             : 
    1231      108356 :           if(d2>cutOnDist2) {
    1232      101573 :             const double af = cutOffDist2 - d2;
    1233      101573 :             const double bf = cutOffDist2 - 3.*cutOnDist2 + 2.*d2;
    1234      101573 :             const double cf = invswitch*af;
    1235      101573 :             const double df = cf*af*bf;
    1236      101573 :             factor1 *= df;
    1237      101573 :             factor3 *= df;
    1238             : 
    1239      101573 :             const double d4  = d2*d2;
    1240      101573 :             const double af1 = 15.*cutOnDist2*d2;
    1241      101573 :             const double bf1 = -14.*d4;
    1242      101573 :             const double cf1 = -3.*cutOffDist2*cutOnDist2 + cutOffDist2*d2;
    1243      101573 :             const double df1 = af1+bf1+cf1;
    1244      101573 :             dfactor1 *= cf*(cutOffDist4+df1);
    1245             : 
    1246             :             const double af3 = +2.*cutOffDist2*cutOnDist2;
    1247      101573 :             const double bf3 = d2*(cutOffDist2+cutOnDist2);
    1248      101573 :             const double cf3 = -2.*d4;
    1249      101573 :             const double df3 = (af3+bf3+cf3)*d2;
    1250      101573 :             dfactor3 *= invswitch*(cutMixed+df3);
    1251             :           }
    1252             : 
    1253      216712 :           const unsigned t = type[jpos];
    1254      108356 :           shift += factor1*CONST_CO_SPHERE[t] + factor3*CONST_CO_SPHERE3[t] ;
    1255      108356 :           const double fact = dfactor1*CONST_CO_SPHERE[t]+dfactor3*CONST_CO_SPHERE3[t];
    1256      108356 :           const Vector der  = fact*distance;
    1257             : 
    1258      216874 :           cs_derivs[kdx+0] += der;
    1259      216874 :           cs_derivs[kdx+q+atom_counter] = -der;
    1260      216544 :           cs_atoms[kdx+q+atom_counter] = jpos;
    1261             :         }
    1262             :       }
    1263             :       //END NON BOND
    1264             : 
    1265             :       atom_counter += boxsize;
    1266       11780 :       all_shifts[cs] = shift;
    1267             :     }
    1268             :   }
    1269             : 
    1270          18 :   ++box_count;
    1271          18 :   if(box_count == box_nupdate) box_count = 0;
    1272             : 
    1273          18 :   if(!camshift) {
    1274          13 :     if(!serial) {
    1275          13 :       if(!getDoScore()) {
    1276           9 :         comm.Sum(&cs_derivs[0][0], 3*cs_derivs.size());
    1277           9 :         comm.Sum(&cs_atoms[0], cs_atoms.size());
    1278             :       }
    1279          26 :       comm.Sum(&all_shifts[0], chemicalshifts.size());
    1280             :     }
    1281       15327 :     for(unsigned cs=0; cs<chemicalshifts.size(); cs++) {
    1282        7657 :       Value *comp = chemicalshifts[cs].comp;
    1283        7657 :       comp->set(all_shifts[cs]);
    1284       10013 :       if(getDoScore()) setCalcData(cs, all_shifts[cs]);
    1285             :       else {
    1286        5301 :         const unsigned kdx=cs*max_cs_atoms;
    1287        5301 :         Tensor csvirial;
    1288     2540254 :         for(unsigned i=0; i<chemicalshifts[cs].totcsatoms; i++) {
    1289     2529652 :           setAtomsDerivatives(comp,cs_atoms[kdx+i],cs_derivs[kdx+i]);
    1290     2529652 :           csvirial-=Tensor(getPosition(cs_atoms[kdx+i]),cs_derivs[kdx+i]);
    1291             :         }
    1292        5301 :         setBoxDerivatives(comp,csvirial);
    1293             :       }
    1294             :     }
    1295          31 :     if(!getDoScore()) return;
    1296             :   }
    1297             : 
    1298           9 :   double score = 0.;
    1299             : 
    1300             :   /* Metainference */
    1301           9 :   if(getDoScore()) {
    1302           4 :     score = getScore();
    1303        2364 :     for(unsigned cs=rank; cs<chemicalshifts.size(); cs+=stride) {
    1304        1178 :       const unsigned kdx=cs*max_cs_atoms;
    1305             :       const double fact = getMetaDer(cs);
    1306      564430 :       for(unsigned i=0; i<chemicalshifts[cs].totcsatoms; i++) {
    1307     1124148 :         aa_derivs[cs_atoms[kdx+i]] += cs_derivs[kdx+i]*fact;
    1308             :       }
    1309             :     }
    1310             :   }
    1311             : 
    1312             :   /* camshift */
    1313           9 :   if(camshift) {
    1314        3539 :     for(unsigned cs=rank; cs<chemicalshifts.size(); cs+=stride) {
    1315        1767 :       const unsigned kdx=cs*max_cs_atoms;
    1316        5301 :       score += (all_shifts[cs] - chemicalshifts[cs].exp_cs)*(all_shifts[cs] - chemicalshifts[cs].exp_cs)/camshift_sigma2[chemicalshifts[cs].atm_kind];
    1317        1767 :       double fact = 2.0*(all_shifts[cs] - chemicalshifts[cs].exp_cs)/camshift_sigma2[chemicalshifts[cs].atm_kind];
    1318      846964 :       for(unsigned i=0; i<chemicalshifts[cs].totcsatoms; i++) {
    1319     1686860 :         aa_derivs[cs_atoms[kdx+i]] += cs_derivs[kdx+i]*fact;
    1320             :       }
    1321             :     }
    1322             :   }
    1323             : 
    1324           9 :   if(!serial) {
    1325           9 :     comm.Sum(&aa_derivs[0][0], 3*aa_derivs.size());
    1326           9 :     if(camshift) comm.Sum(&score, 1);
    1327             :   }
    1328             : 
    1329           9 :   Tensor virial;
    1330       26138 :   for(unsigned i=rank; i<getNumberOfAtoms(); i+=stride) {
    1331       39180 :     virial += Tensor(getPosition(i), aa_derivs[i]);
    1332             :   }
    1333             : 
    1334           9 :   if(!serial) {
    1335           9 :     comm.Sum(&virial[0][0], 9);
    1336             :   }
    1337             : 
    1338             :   /* calculate final derivatives */
    1339             :   Value* val;
    1340           9 :   if(getDoScore()) {
    1341           8 :     val=getPntrToComponent("score");
    1342           4 :     setScore(score);
    1343             :   } else {
    1344           5 :     val=getPntrToValue();
    1345           5 :     setValue(score);
    1346             :   }
    1347             : 
    1348             :   /* at this point we cycle over all atoms */
    1349       47034 :   for(unsigned i=0; i<getNumberOfAtoms(); i++) setAtomsDerivatives(val, i,  aa_derivs[i]);
    1350           9 :   setBoxDerivatives(val,-virial);
    1351             : }
    1352             : 
    1353          18 : void CS2Backbone::update_neighb() {
    1354          18 :   max_cs_atoms=0;
    1355             :   // cycle over chemical shifts
    1356       21240 :   for(unsigned cs=0; cs<chemicalshifts.size(); cs++) {
    1357             :     const unsigned boxsize = getNumberOfAtoms();
    1358             :     chemicalshifts[cs].box_nb.clear();
    1359       10602 :     chemicalshifts[cs].box_nb.reserve(150);
    1360       21204 :     const unsigned res_curr = res_num[chemicalshifts[cs].ipos];
    1361    27703026 :     for(unsigned bat=0; bat<boxsize; bat++) {
    1362    55384848 :       const unsigned res_dist = abs(static_cast<int>(res_curr-res_num[bat]));
    1363    28171332 :       if(res_dist<2) continue;
    1364    81640548 :       const Vector distance = delta(getPosition(bat),getPosition(chemicalshifts[cs].ipos));
    1365    27213516 :       const double d2=distance.modulo2();
    1366    27655114 :       if(d2<cutOffNB2) chemicalshifts[cs].box_nb.push_back(bat);
    1367             :     }
    1368       21204 :     chemicalshifts[cs].totcsatoms = chemicalshifts[cs].csatoms + chemicalshifts[cs].box_nb.size();
    1369       10602 :     if(chemicalshifts[cs].totcsatoms>max_cs_atoms) max_cs_atoms = chemicalshifts[cs].totcsatoms;
    1370             :   }
    1371          18 : }
    1372             : 
    1373          18 : void CS2Backbone::compute_ring_parameters() {
    1374         756 :   for(unsigned i=0; i<ringInfo.size(); i++) {
    1375         360 :     const unsigned size = ringInfo[i].numAtoms;
    1376         360 :     if(size==6) {
    1377        1026 :       ringInfo[i].g[0] = delta(getPosition(ringInfo[i].atom[4]),getPosition(ringInfo[i].atom[2]));
    1378        1026 :       ringInfo[i].g[1] = delta(getPosition(ringInfo[i].atom[5]),getPosition(ringInfo[i].atom[3]));
    1379        1026 :       ringInfo[i].g[2] = delta(getPosition(ringInfo[i].atom[0]),getPosition(ringInfo[i].atom[4]));
    1380        1026 :       ringInfo[i].g[3] = delta(getPosition(ringInfo[i].atom[1]),getPosition(ringInfo[i].atom[5]));
    1381        1026 :       ringInfo[i].g[4] = delta(getPosition(ringInfo[i].atom[2]),getPosition(ringInfo[i].atom[0]));
    1382        1026 :       ringInfo[i].g[5] = delta(getPosition(ringInfo[i].atom[3]),getPosition(ringInfo[i].atom[1]));
    1383         342 :       vector<Vector> a(6);
    1384         684 :       a[0] = getPosition(ringInfo[i].atom[0]);
    1385             :       // ring center
    1386         342 :       Vector midP = a[0];
    1387        2052 :       for(unsigned j=1; j<size; j++) {
    1388        5130 :         a[j] = getPosition(ringInfo[i].atom[j]);
    1389        1710 :         midP += a[j];
    1390             :       }
    1391         342 :       ringInfo[i].position = midP/6.;
    1392             :       // compute normal vector to plane
    1393         684 :       Vector n1 = crossProduct(delta(a[0],a[4]), delta(a[0],a[2]));
    1394         684 :       Vector n2 = crossProduct(delta(a[3],a[1]), delta(a[3],a[5]));
    1395         342 :       ringInfo[i].normVect = 0.5*(n1 + n2);
    1396             :     }  else {
    1397          54 :       ringInfo[i].g[0] = delta(getPosition(ringInfo[i].atom[3]),getPosition(ringInfo[i].atom[2]));
    1398          54 :       ringInfo[i].g[1] = delta(getPosition(ringInfo[i].atom[0]),getPosition(ringInfo[i].atom[3]));
    1399          54 :       ringInfo[i].g[2] = delta(getPosition(ringInfo[i].atom[2]),getPosition(ringInfo[i].atom[0]));
    1400          18 :       vector<Vector> a(size);
    1401         108 :       for(unsigned j=0; j<size; j++) {
    1402         270 :         a[j] = getPosition(ringInfo[i].atom[j]);
    1403             :       }
    1404             :       // ring center
    1405          18 :       ringInfo[i].position = (a[0]+a[2]+a[3])/3.;
    1406             :       // ring plane normal vector
    1407          36 :       ringInfo[i].normVect = crossProduct(delta(a[0],a[3]), delta(a[0],a[2]));
    1408             : 
    1409             :     }
    1410             :     // calculate squared length and length of normal vector
    1411         360 :     ringInfo[i].lengthN2 = 1./ringInfo[i].normVect.modulo2();
    1412         360 :     ringInfo[i].lengthNV = 1./sqrt(ringInfo[i].lengthN2);
    1413             :   }
    1414          18 : }
    1415             : 
    1416       31806 : CS2Backbone::aa_t CS2Backbone::frag2enum(const string &aa) {
    1417             :   aa_t type = ALA;
    1418       31806 :   if (aa == "ALA") type = ALA;
    1419       29934 :   else if (aa == "ARG") type = ARG;
    1420       28548 :   else if (aa == "ASN") type = ASN;
    1421       26910 :   else if (aa == "ASP") type = ASP;
    1422       25380 :   else if (aa == "ASH") type = ASP;
    1423       25380 :   else if (aa == "CYS") type = CYS;
    1424       24858 :   else if (aa == "CYM") type = CYS;
    1425       24858 :   else if (aa == "GLN") type = GLN;
    1426       24390 :   else if (aa == "GLU") type = GLU;
    1427       22068 :   else if (aa == "GLH") type = GLU;
    1428       22068 :   else if (aa == "GLY") type = GLY;
    1429       16974 :   else if (aa == "HIS") type = HIS;
    1430       16974 :   else if (aa == "HSE") type = HIS;
    1431       16974 :   else if (aa == "HIE") type = HIS;
    1432       16974 :   else if (aa == "HSP") type = HIS;
    1433       16974 :   else if (aa == "HIP") type = HIS;
    1434       16974 :   else if (aa == "HSD") type = HIS;
    1435       16974 :   else if (aa == "HID") type = HIS;
    1436       16974 :   else if (aa == "ILE") type = ILE;
    1437       15174 :   else if (aa == "LEU") type = LEU;
    1438       13536 :   else if (aa == "LYS") type = LYS;
    1439       10746 :   else if (aa == "MET") type = MET;
    1440        9936 :   else if (aa == "PHE") type = PHE;
    1441        6876 :   else if (aa == "PRO") type = PRO;
    1442        5940 :   else if (aa == "SER") type = SER;
    1443        4302 :   else if (aa == "THR") type = THR;
    1444        2196 :   else if (aa == "TRP") type = TRP;
    1445        1980 :   else if (aa == "TYR") type = TYR;
    1446        1602 :   else if (aa == "VAL") type = VAL;
    1447           0 :   else if (aa == "UNK") type = UNK;
    1448           0 :   else plumed_merror("Error converting string " + aa + " into amino acid index: not a valid 3-letter code");
    1449       31806 :   return type;
    1450             : }
    1451             : 
    1452       10602 : vector<string> CS2Backbone::side_chain_atoms(const string &s) {
    1453             :   vector<string> sc;
    1454             : 
    1455       10602 :   if(s=="ALA") {
    1456        1296 :     sc.push_back( "CB" );
    1457        1296 :     sc.push_back( "HB1" );
    1458        1296 :     sc.push_back( "HB2" );
    1459        1296 :     sc.push_back( "HB3" );
    1460         648 :     return sc;
    1461        9954 :   } else if(s=="ARG") {
    1462         936 :     sc.push_back( "CB" );
    1463         936 :     sc.push_back( "CG" );
    1464         936 :     sc.push_back( "CD" );
    1465         936 :     sc.push_back( "NE" );
    1466         936 :     sc.push_back( "CZ" );
    1467         936 :     sc.push_back( "NH1" );
    1468         936 :     sc.push_back( "NH2" );
    1469         936 :     sc.push_back( "NH3" );
    1470         936 :     sc.push_back( "HB1" );
    1471         936 :     sc.push_back( "HB2" );
    1472         936 :     sc.push_back( "HB3" );
    1473         936 :     sc.push_back( "HG1" );
    1474         936 :     sc.push_back( "HG2" );
    1475         936 :     sc.push_back( "HG3" );
    1476         936 :     sc.push_back( "HD1" );
    1477         936 :     sc.push_back( "HD2" );
    1478         936 :     sc.push_back( "HD3" );
    1479         936 :     sc.push_back( "HE" );
    1480         936 :     sc.push_back( "HH11" );
    1481         936 :     sc.push_back( "HH12" );
    1482         936 :     sc.push_back( "HH21" );
    1483         936 :     sc.push_back( "HH22" );
    1484         936 :     sc.push_back( "1HH1" );
    1485         936 :     sc.push_back( "2HH1" );
    1486         936 :     sc.push_back( "1HH2" );
    1487         936 :     sc.push_back( "2HH2" );
    1488         468 :     return sc;
    1489        9486 :   } else if(s=="ASN") {
    1490        1188 :     sc.push_back( "CB" );
    1491        1188 :     sc.push_back( "CG" );
    1492        1188 :     sc.push_back( "OD1" );
    1493        1188 :     sc.push_back( "ND2" );
    1494        1188 :     sc.push_back( "HB1" );
    1495        1188 :     sc.push_back( "HB2" );
    1496        1188 :     sc.push_back( "HB3" );
    1497        1188 :     sc.push_back( "HD21" );
    1498        1188 :     sc.push_back( "HD22" );
    1499        1188 :     sc.push_back( "1HD2" );
    1500        1188 :     sc.push_back( "2HD2" );
    1501         594 :     return sc;
    1502       17226 :   } else if(s=="ASP"||s=="ASH") {
    1503        1116 :     sc.push_back( "CB" );
    1504        1116 :     sc.push_back( "CG" );
    1505        1116 :     sc.push_back( "OD1" );
    1506        1116 :     sc.push_back( "OD2" );
    1507        1116 :     sc.push_back( "HB1" );
    1508        1116 :     sc.push_back( "HB2" );
    1509        1116 :     sc.push_back( "HB3" );
    1510         558 :     return sc;
    1511       16668 :   } else if(s=="CYS"||s=="CYM") {
    1512           0 :     sc.push_back( "CB" );
    1513           0 :     sc.push_back( "SG" );
    1514           0 :     sc.push_back( "HB1" );
    1515           0 :     sc.push_back( "HB2" );
    1516           0 :     sc.push_back( "HB3" );
    1517           0 :     sc.push_back( "HG1" );
    1518           0 :     sc.push_back( "HG" );
    1519           0 :     return sc;
    1520        8334 :   } else if(s=="GLN") {
    1521         324 :     sc.push_back( "CB" );
    1522         324 :     sc.push_back( "CG" );
    1523         324 :     sc.push_back( "CD" );
    1524         324 :     sc.push_back( "OE1" );
    1525         324 :     sc.push_back( "NE2" );
    1526         324 :     sc.push_back( "HB1" );
    1527         324 :     sc.push_back( "HB2" );
    1528         324 :     sc.push_back( "HB3" );
    1529         324 :     sc.push_back( "HG1" );
    1530         324 :     sc.push_back( "HG2" );
    1531         324 :     sc.push_back( "HG3" );
    1532         324 :     sc.push_back( "HE21" );
    1533         324 :     sc.push_back( "HE22" );
    1534         324 :     sc.push_back( "1HE2" );
    1535         324 :     sc.push_back( "2HE2" );
    1536         162 :     return sc;
    1537       15552 :   } else if(s=="GLU"||s=="GLH") {
    1538        1584 :     sc.push_back( "CB" );
    1539        1584 :     sc.push_back( "CG" );
    1540        1584 :     sc.push_back( "CD" );
    1541        1584 :     sc.push_back( "OE1" );
    1542        1584 :     sc.push_back( "OE2" );
    1543        1584 :     sc.push_back( "HB1" );
    1544        1584 :     sc.push_back( "HB2" );
    1545        1584 :     sc.push_back( "HB3" );
    1546        1584 :     sc.push_back( "HG1" );
    1547        1584 :     sc.push_back( "HG2" );
    1548        1584 :     sc.push_back( "HG3" );
    1549         792 :     return sc;
    1550        7380 :   } else if(s=="GLY") {
    1551        2988 :     sc.push_back( "HA2" );
    1552        1494 :     return sc;
    1553       41202 :   } else if(s=="HIS"||s=="HSE"||s=="HIE"||s=="HSD"||s=="HID"||s=="HIP"||s=="HSP") {
    1554           0 :     sc.push_back( "CB" );
    1555           0 :     sc.push_back( "CG" );
    1556           0 :     sc.push_back( "ND1" );
    1557           0 :     sc.push_back( "CD2" );
    1558           0 :     sc.push_back( "CE1" );
    1559           0 :     sc.push_back( "NE2" );
    1560           0 :     sc.push_back( "HB1" );
    1561           0 :     sc.push_back( "HB2" );
    1562           0 :     sc.push_back( "HB3" );
    1563           0 :     sc.push_back( "HD1" );
    1564           0 :     sc.push_back( "HD2" );
    1565           0 :     sc.push_back( "HE1" );
    1566           0 :     sc.push_back( "HE2" );
    1567           0 :     return sc;
    1568        5886 :   } else if(s=="ILE") {
    1569        1080 :     sc.push_back( "CB" );
    1570        1080 :     sc.push_back( "CG1" );
    1571        1080 :     sc.push_back( "CG2" );
    1572        1080 :     sc.push_back( "CD" );
    1573        1080 :     sc.push_back( "HB" );
    1574        1080 :     sc.push_back( "HG11" );
    1575        1080 :     sc.push_back( "HG12" );
    1576        1080 :     sc.push_back( "HG21" );
    1577        1080 :     sc.push_back( "HG22" );
    1578        1080 :     sc.push_back( "HG23" );
    1579        1080 :     sc.push_back( "1HG1" );
    1580        1080 :     sc.push_back( "2HG1" );
    1581        1080 :     sc.push_back( "1HG2" );
    1582        1080 :     sc.push_back( "2HG2" );
    1583        1080 :     sc.push_back( "3HG2" );
    1584        1080 :     sc.push_back( "HD1" );
    1585        1080 :     sc.push_back( "HD2" );
    1586        1080 :     sc.push_back( "HD3" );
    1587         540 :     return sc;
    1588        5346 :   } else if(s=="LEU") {
    1589        1296 :     sc.push_back( "CB" );
    1590        1296 :     sc.push_back( "CG" );
    1591        1296 :     sc.push_back( "CD1" );
    1592        1296 :     sc.push_back( "CD2" );
    1593        1296 :     sc.push_back( "HB1" );
    1594        1296 :     sc.push_back( "HB2" );
    1595        1296 :     sc.push_back( "HB3" );
    1596        1296 :     sc.push_back( "HG" );
    1597        1296 :     sc.push_back( "HD11" );
    1598        1296 :     sc.push_back( "HD12" );
    1599        1296 :     sc.push_back( "HD13" );
    1600        1296 :     sc.push_back( "HD21" );
    1601        1296 :     sc.push_back( "HD22" );
    1602        1296 :     sc.push_back( "HD23" );
    1603        1296 :     sc.push_back( "1HD1" );
    1604        1296 :     sc.push_back( "2HD1" );
    1605        1296 :     sc.push_back( "3HD1" );
    1606        1296 :     sc.push_back( "1HD2" );
    1607        1296 :     sc.push_back( "2HD2" );
    1608        1296 :     sc.push_back( "3HD2" );
    1609         648 :     return sc;
    1610        4698 :   } else if(s=="LYS") {
    1611        2016 :     sc.push_back( "CB" );
    1612        2016 :     sc.push_back( "CG" );
    1613        2016 :     sc.push_back( "CD" );
    1614        2016 :     sc.push_back( "CE" );
    1615        2016 :     sc.push_back( "NZ" );
    1616        2016 :     sc.push_back( "HB1" );
    1617        2016 :     sc.push_back( "HB2" );
    1618        2016 :     sc.push_back( "HB3" );
    1619        2016 :     sc.push_back( "HG1" );
    1620        2016 :     sc.push_back( "HG2" );
    1621        2016 :     sc.push_back( "HG3" );
    1622        2016 :     sc.push_back( "HD1" );
    1623        2016 :     sc.push_back( "HD2" );
    1624        2016 :     sc.push_back( "HD3" );
    1625        2016 :     sc.push_back( "HE1" );
    1626        2016 :     sc.push_back( "HE2" );
    1627        2016 :     sc.push_back( "HE3" );
    1628        2016 :     sc.push_back( "HZ1" );
    1629        2016 :     sc.push_back( "HZ2" );
    1630        2016 :     sc.push_back( "HZ3" );
    1631        1008 :     return sc;
    1632        3690 :   } else if(s=="MET") {
    1633         576 :     sc.push_back( "CB" );
    1634         576 :     sc.push_back( "CG" );
    1635         576 :     sc.push_back( "SD" );
    1636         576 :     sc.push_back( "CE" );
    1637         576 :     sc.push_back( "HB1" );
    1638         576 :     sc.push_back( "HB2" );
    1639         576 :     sc.push_back( "HB3" );
    1640         576 :     sc.push_back( "HG1" );
    1641         576 :     sc.push_back( "HG2" );
    1642         576 :     sc.push_back( "HG3" );
    1643         576 :     sc.push_back( "HE1" );
    1644         576 :     sc.push_back( "HE2" );
    1645         576 :     sc.push_back( "HE3" );
    1646         288 :     return sc;
    1647        3402 :   } else if(s=="PHE") {
    1648        2196 :     sc.push_back( "CB" );
    1649        2196 :     sc.push_back( "CG" );
    1650        2196 :     sc.push_back( "CD1" );
    1651        2196 :     sc.push_back( "CD2" );
    1652        2196 :     sc.push_back( "CE1" );
    1653        2196 :     sc.push_back( "CE2" );
    1654        2196 :     sc.push_back( "CZ" );
    1655        2196 :     sc.push_back( "HB1" );
    1656        2196 :     sc.push_back( "HB2" );
    1657        2196 :     sc.push_back( "HB3" );
    1658        2196 :     sc.push_back( "HD1" );
    1659        2196 :     sc.push_back( "HD2" );
    1660        2196 :     sc.push_back( "HD3" );
    1661        2196 :     sc.push_back( "HE1" );
    1662        2196 :     sc.push_back( "HE2" );
    1663        2196 :     sc.push_back( "HE3" );
    1664        2196 :     sc.push_back( "HZ" );
    1665        1098 :     return sc;
    1666        2304 :   } else if(s=="PRO") {
    1667         216 :     sc.push_back( "CB" );
    1668         216 :     sc.push_back( "CG" );
    1669         216 :     sc.push_back( "CD" );
    1670         216 :     sc.push_back( "HB1" );
    1671         216 :     sc.push_back( "HB2" );
    1672         216 :     sc.push_back( "HB3" );
    1673         216 :     sc.push_back( "HG1" );
    1674         216 :     sc.push_back( "HG2" );
    1675         216 :     sc.push_back( "HG3" );
    1676         216 :     sc.push_back( "HD1" );
    1677         216 :     sc.push_back( "HD2" );
    1678         216 :     sc.push_back( "HD3" );
    1679         108 :     return sc;
    1680        2196 :   } else if(s=="SER") {
    1681        1260 :     sc.push_back( "CB" );
    1682        1260 :     sc.push_back( "OG" );
    1683        1260 :     sc.push_back( "HB1" );
    1684        1260 :     sc.push_back( "HB2" );
    1685        1260 :     sc.push_back( "HB3" );
    1686        1260 :     sc.push_back( "HG1" );
    1687        1260 :     sc.push_back( "HG" );
    1688         630 :     return sc;
    1689        1566 :   } else if(s=="THR") {
    1690        1548 :     sc.push_back( "CB" );
    1691        1548 :     sc.push_back( "OG1" );
    1692        1548 :     sc.push_back( "CG2" );
    1693        1548 :     sc.push_back( "HB" );
    1694        1548 :     sc.push_back( "HG1" );
    1695        1548 :     sc.push_back( "HG21" );
    1696        1548 :     sc.push_back( "HG22" );
    1697        1548 :     sc.push_back( "HG23" );
    1698        1548 :     sc.push_back( "1HG2" );
    1699        1548 :     sc.push_back( "2HG2" );
    1700        1548 :     sc.push_back( "3HG2" );
    1701         774 :     return sc;
    1702         792 :   } else if(s=="TRP") {
    1703         144 :     sc.push_back( "CB" );
    1704         144 :     sc.push_back( "CG" );
    1705         144 :     sc.push_back( "CD1" );
    1706         144 :     sc.push_back( "CD2" );
    1707         144 :     sc.push_back( "NE1" );
    1708         144 :     sc.push_back( "CE2" );
    1709         144 :     sc.push_back( "CE3" );
    1710         144 :     sc.push_back( "CZ2" );
    1711         144 :     sc.push_back( "CZ3" );
    1712         144 :     sc.push_back( "CH2" );
    1713         144 :     sc.push_back( "HB1" );
    1714         144 :     sc.push_back( "HB2" );
    1715         144 :     sc.push_back( "HB3" );
    1716         144 :     sc.push_back( "HD1" );
    1717         144 :     sc.push_back( "HE1" );
    1718         144 :     sc.push_back( "HE3" );
    1719         144 :     sc.push_back( "HZ2" );
    1720         144 :     sc.push_back( "HZ3" );
    1721         144 :     sc.push_back( "HH2" );
    1722          72 :     return sc;
    1723         720 :   } else if(s=="TYR") {
    1724         288 :     sc.push_back( "CB" );
    1725         288 :     sc.push_back( "CG" );
    1726         288 :     sc.push_back( "CD1" );
    1727         288 :     sc.push_back( "CD2" );
    1728         288 :     sc.push_back( "CE1" );
    1729         288 :     sc.push_back( "CE2" );
    1730         288 :     sc.push_back( "CZ" );
    1731         288 :     sc.push_back( "OH" );
    1732         288 :     sc.push_back( "HB1" );
    1733         288 :     sc.push_back( "HB2" );
    1734         288 :     sc.push_back( "HB3" );
    1735         288 :     sc.push_back( "HD1" );
    1736         288 :     sc.push_back( "HD2" );
    1737         288 :     sc.push_back( "HD3" );
    1738         288 :     sc.push_back( "HE1" );
    1739         288 :     sc.push_back( "HE2" );
    1740         288 :     sc.push_back( "HE3" );
    1741         288 :     sc.push_back( "HH" );
    1742         144 :     return sc;
    1743         576 :   } else if(s=="VAL") {
    1744        1152 :     sc.push_back( "CB" );
    1745        1152 :     sc.push_back( "CG1" );
    1746        1152 :     sc.push_back( "CG2" );
    1747        1152 :     sc.push_back( "HB" );
    1748        1152 :     sc.push_back( "HG11" );
    1749        1152 :     sc.push_back( "HG12" );
    1750        1152 :     sc.push_back( "HG13" );
    1751        1152 :     sc.push_back( "HG21" );
    1752        1152 :     sc.push_back( "HG22" );
    1753        1152 :     sc.push_back( "HG23" );
    1754        1152 :     sc.push_back( "1HG1" );
    1755        1152 :     sc.push_back( "2HG1" );
    1756        1152 :     sc.push_back( "3HG1" );
    1757        1152 :     sc.push_back( "1HG2" );
    1758        1152 :     sc.push_back( "2HG2" );
    1759        1152 :     sc.push_back( "3HG2" );
    1760         576 :     return sc;
    1761           0 :   } else plumed_merror("Sidechain atoms unknown: " + s);
    1762             : }
    1763             : 
    1764       47016 : bool CS2Backbone::isSP2(const string & resType, const string & atomName) {
    1765             :   bool sp2 = false;
    1766       47016 :   if (atomName == "C") return true;
    1767       43848 :   if (atomName == "O") return true;
    1768             : 
    1769       40716 :   if(resType == "TRP") {
    1770         396 :     if      (atomName == "CG")  sp2 = true;
    1771         378 :     else if (atomName == "CD1") sp2 = true;
    1772         360 :     else if (atomName == "CD2") sp2 = true;
    1773         342 :     else if (atomName == "CE2") sp2 = true;
    1774         324 :     else if (atomName == "CE3") sp2 = true;
    1775         306 :     else if (atomName == "CZ2") sp2 = true;
    1776         288 :     else if (atomName == "CZ3") sp2 = true;
    1777         270 :     else if (atomName == "CH2") sp2 = true;
    1778       40320 :   } else if (resType == "ASP") {
    1779        1656 :     if      (atomName == "CG")  sp2 = true;
    1780        1494 :     else if (atomName == "OD1") sp2 = true;
    1781        1332 :     else if (atomName == "OD2") sp2 = true;
    1782       38664 :   } else if (resType == "GLU") {
    1783        2844 :     if      (atomName == "CD")  sp2 = true;
    1784        2628 :     else if (atomName == "OE1") sp2 = true;
    1785        2412 :     else if (atomName == "OE2") sp2 = true;
    1786       35820 :   } else if (resType == "ARG") {
    1787        2772 :     if (atomName == "CZ") sp2 = true;
    1788       33048 :   } else if (resType == "HIS") {
    1789           0 :     if      (atomName == "CG")  sp2 = true;
    1790           0 :     else if (atomName == "ND1") sp2 = true;
    1791           0 :     else if (atomName == "CD2") sp2 = true;
    1792           0 :     else if (atomName == "CE1") sp2 = true;
    1793           0 :     else if (atomName == "NE2") sp2 = true;
    1794       33048 :   } else if (resType == "PHE") {
    1795        5184 :     if      (atomName == "CG")  sp2 = true;
    1796        4896 :     else if (atomName == "CD1") sp2 = true;
    1797        4608 :     else if (atomName == "CD2") sp2 = true;
    1798        4320 :     else if (atomName == "CE1") sp2 = true;
    1799        4032 :     else if (atomName == "CE2") sp2 = true;
    1800        3744 :     else if (atomName == "CZ")  sp2 = true;
    1801       27864 :   } else if (resType == "TYR") {
    1802         684 :     if      (atomName == "CG")  sp2 = true;
    1803         648 :     else if (atomName == "CD1") sp2 = true;
    1804         612 :     else if (atomName == "CD2") sp2 = true;
    1805         576 :     else if (atomName == "CE1") sp2 = true;
    1806         540 :     else if (atomName == "CE2") sp2 = true;
    1807         504 :     else if (atomName == "CZ")  sp2 = true;
    1808       27180 :   } else if (resType == "ASN") {
    1809        1944 :     if      (atomName == "CG")  sp2 = true;
    1810        1782 :     else if (atomName == "OD1") sp2 = true;
    1811       25236 :   } else if (resType == "GLN") {
    1812         810 :     if      (atomName == "CD")  sp2 = true;
    1813         756 :     else if (atomName == "OE1") sp2 = true;
    1814             :   }
    1815             : 
    1816       40716 :   return sp2;
    1817             : }
    1818             : 
    1819      145728 : bool CS2Backbone::is_chi1_cx(const string & frg, const string & atm) {
    1820      145728 :   if(atm=="CG")                                        return true;
    1821      139788 :   if((frg == "CYS")&&(atm =="SG"))                     return true;
    1822      288792 :   if(((frg == "ILE")||(frg == "VAL"))&&(atm == "CG1")) return true;
    1823      145602 :   if((frg == "SER")&&(atm == "OG"))                    return true;
    1824      148878 :   if((frg == "THR")&&(atm == "OG1"))                   return true;
    1825             : 
    1826      137268 :   return false;
    1827             : }
    1828             : 
    1829     3599784 : void CS2Backbone::xdist_name_map(string & name) {
    1830     7199568 :   if((name == "OT1")||(name == "OC1")) name = "O";
    1831    10799352 :   else if ((name == "HN") || (name == "HT1") || (name == "H1")) name = "H";
    1832    10737144 :   else if ((name == "CG1")|| (name == "OG")||
    1833     7159572 :            (name == "SG") || (name == "OG1")) name = "CG";
    1834     7040070 :   else if ((name == "HA1")|| (name == "HA3")) name = "HA";
    1835     3599784 : }
    1836             : 
    1837          18 : void CS2Backbone::update() {
    1838             :   // write status file
    1839          36 :   if(getWstride()>0&& (getStep()%getWstride()==0 || getCPT()) ) writeStatus();
    1840          18 : }
    1841             : 
    1842             : }
    1843        5874 : }

Generated by: LCOV version 1.14