View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2023.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  // ******************************************************************************
38  package ffx.algorithms;
39  
40  import static ffx.potential.bonded.RotamerLibrary.applyRotamer;
41  
42  import ffx.potential.ForceFieldEnergy;
43  import ffx.potential.ForceFieldEnergyOpenMM;
44  import ffx.potential.MolecularAssembly;
45  import ffx.potential.bonded.AminoAcidUtils;
46  import ffx.potential.bonded.Atom;
47  import ffx.potential.bonded.Residue;
48  import ffx.potential.bonded.Rotamer;
49  import ffx.potential.parameters.ForceField;
50  import ffx.potential.parameters.TitrationUtils;
51  import ffx.potential.parsers.PDBFilter;
52  import java.io.File;
53  import java.util.List;
54  import java.util.Set;
55  import java.util.logging.Level;
56  import java.util.logging.Logger;
57  import java.lang.String;
58  
59  public class TitrationManyBody {
60  
61    //private MolecularAssembly protonatedAssembly;
62    private PDBFilter protonFilter;
63    private ForceField forceField;
64    private List<Integer> resNumberList;
65    private ForceFieldEnergy potentialEnergy;
66    private final double pH;
67    private final String filename;
68    private static final Logger logger = Logger.getLogger(TitrationManyBody.class.getName());
69  
70    public TitrationManyBody(
71        String filename,
72        ForceField forceField,
73        List<Integer> resNumberList,
74        double pH) {
75      this.filename = filename;
76      this.forceField = forceField;
77      this.resNumberList = resNumberList;
78      this.pH = pH;
79    }
80  
81    public MolecularAssembly getProtonatedAssembly() {
82      MolecularAssembly protonatedAssembly = new MolecularAssembly(filename);
83      protonatedAssembly.setForceField(forceField);
84      File structureFile = new File(filename);
85      protonFilter = new PDBFilter(structureFile, protonatedAssembly, forceField,
86          forceField.getProperties(), resNumberList);
87      protonFilter.setRotamerTitration(true);
88      protonFilter.readFile();
89      protonFilter.applyAtomProperties();
90      protonatedAssembly.finalize(true, forceField);
91      potentialEnergy = ForceFieldEnergy.energyFactory(protonatedAssembly);
92      protonatedAssembly.setFile(structureFile);
93  
94      TitrationUtils titrationUtils;
95      titrationUtils = new TitrationUtils(protonatedAssembly.getForceField());
96      titrationUtils.setRotamerPhBias(298.15, pH);
97      for (Residue residue : protonatedAssembly.getResidueList()) {
98        String resName = residue.getName();
99        if (resNumberList.contains(residue.getResidueNumber())) {
100         if (resName.equalsIgnoreCase("ASH") ||
101             resName.equalsIgnoreCase("GLH") ||
102             resName.equalsIgnoreCase("LYS") ||
103             resName.equalsIgnoreCase("HIS") ||
104                 resName.equalsIgnoreCase("CYS")) {
105           residue.setTitrationUtils(titrationUtils);
106         }
107       }
108     }
109     if (potentialEnergy instanceof ForceFieldEnergyOpenMM) {
110       boolean updateBondedTerms = forceField.getBoolean("TITRATION_UPDATE_BONDED_TERMS", true);
111       ForceFieldEnergyOpenMM forceFieldEnergyOpenMM = (ForceFieldEnergyOpenMM) potentialEnergy;
112       forceFieldEnergyOpenMM.getSystem().setUpdateBondedTerms(updateBondedTerms);
113     }
114     potentialEnergy.energy();
115     return protonatedAssembly;
116   }
117 
118   public MolecularAssembly[] getProtonatedAssemblies() {
119     logger.info("Getting protonated assemblies");
120     MolecularAssembly molecularAssembly = getProtonatedAssembly();
121     List<Character> altLocs = protonFilter.getAltLocs();
122     for(int i=0; i<altLocs.size(); i++){
123       if(altLocs.get(i) >= 'A' && altLocs.get(i) <='Z'){
124         logger.info("");
125       } else {
126         altLocs.remove(altLocs.get(i));
127       }
128     }
129     MolecularAssembly[] molecularAssemblies = new MolecularAssembly[altLocs.size()];
130     molecularAssemblies[0] = molecularAssembly;
131     for(int i=0; i < altLocs.size(); i++){
132       if(i!=0){
133         logger.info(filename);
134         MolecularAssembly newAssembly = new MolecularAssembly(filename);
135         newAssembly.setForceField(forceField);
136         File structureFile = new File(filename);
137         protonFilter = new PDBFilter(structureFile, newAssembly, forceField,
138                 forceField.getProperties(), resNumberList);
139         logger.info(newAssembly.getResidueList().toString());
140         protonFilter.setRotamerTitration(true);
141         protonFilter.setAltID(newAssembly, altLocs.get(i));
142         protonFilter.readFile();
143         logger.info(newAssembly.getResidueList().get(0).getAtomList().get(0).getAltLoc().toString());
144         protonFilter.applyAtomProperties();
145         newAssembly.finalize(true, forceField);
146         potentialEnergy = ForceFieldEnergy.energyFactory(newAssembly);
147 
148         TitrationUtils titrationUtils;
149         titrationUtils = new TitrationUtils(molecularAssembly.getForceField());
150         titrationUtils.setRotamerPhBias(298.15, pH);
151         for (Residue residue : molecularAssembly.getResidueList()) {
152           String resName = residue.getName();
153           if (resNumberList.contains(residue.getResidueNumber())) {
154             if (resName.equalsIgnoreCase("ASH") ||
155                     resName.equalsIgnoreCase("GLH") ||
156                     resName.equalsIgnoreCase("LYS") ||
157                     resName.equalsIgnoreCase("HIS") ||
158                     resName.equalsIgnoreCase("CYS")) {
159               residue.setTitrationUtils(titrationUtils);
160             }
161           }
162         }
163         potentialEnergy.energy();
164         molecularAssemblies[i] = newAssembly;
165       }
166     }
167     return molecularAssemblies;
168   }
169 
170 
171   public boolean excludeExcessAtoms(Set<Atom> excludeAtoms, int[] optimalRotamers,
172       List<Residue> residueList) {
173     boolean isTitrating = false;
174     int i = 0;
175     for (Residue residue : residueList) {
176       Rotamer rotamer = residue.getRotamers()[optimalRotamers[i++]];
177       applyRotamer(residue, rotamer);
178       if (rotamer.isTitrating) {
179         isTitrating = true;
180         AminoAcidUtils.AminoAcid3 aa3 = rotamer.aminoAcid3;
181         residue.setName(aa3.name());
182         switch (aa3) {
183           case HID:
184             // No HE2
185             Atom HE2 = residue.getAtomByName("HE2", true);
186             excludeAtoms.add(HE2);
187             break;
188           case HIE:
189             // No HD1
190             Atom HD1 = residue.getAtomByName("HD1", true);
191             excludeAtoms.add(HD1);
192             break;
193           case ASP:
194             // No HD2
195             Atom HD2 = residue.getAtomByName("HD2", true);
196             excludeAtoms.add(HD2);
197             break;
198           case GLU:
199             // No HE2
200             Atom HE2_G = residue.getAtomByName("HE2", true);
201             excludeAtoms.add(HE2_G);
202             break;
203           case LYD:
204             // No HZ3
205             Atom HZ3 = residue.getAtomByName("HZ3", true);
206             excludeAtoms.add(HZ3);
207             break;
208           case CYD:
209             // No HG
210             Atom HG = residue.getAtomByName("HG", true);
211             excludeAtoms.add(HG);
212             break;
213           default:
214             // Do nothing.
215             break;
216         }
217       }
218     }
219 
220     return isTitrating;
221   }
222 
223 
224 }