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-2024.
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.optimize;
39  
40  import static ffx.potential.bonded.RotamerLibrary.applyRotamer;
41  
42  import ffx.potential.ForceFieldEnergy;
43  import ffx.potential.openmm.OpenMMEnergy;
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.openmm.OpenMMEnergy;
50  import ffx.potential.parameters.ForceField;
51  import ffx.potential.parameters.TitrationUtils;
52  import ffx.potential.parsers.PDBFilter;
53  
54  import java.io.File;
55  import java.util.List;
56  import java.util.Set;
57  import java.util.logging.Logger;
58  import java.lang.String;
59  
60  public class TitrationManyBody {
61  
62    private static final Logger logger = Logger.getLogger(TitrationManyBody.class.getName());
63  
64    private final ForceField forceField;
65    private final List<Integer> resNumberList;
66    private final double pH;
67    private final String filename;
68    private PDBFilter protonFilter;
69    private ForceFieldEnergy potentialEnergy;
70  
71    public TitrationManyBody(String filename, ForceField forceField, List<Integer> resNumberList,
72        double pH) {
73      this.filename = filename;
74      this.forceField = forceField;
75      this.resNumberList = resNumberList;
76      this.pH = pH;
77    }
78  
79    public MolecularAssembly getProtonatedAssembly() {
80      MolecularAssembly protonatedAssembly = new MolecularAssembly(filename);
81      protonatedAssembly.setForceField(forceField);
82      File structureFile = new File(filename);
83      protonFilter = new PDBFilter(structureFile, protonatedAssembly, forceField,
84          forceField.getProperties(), resNumberList);
85      protonFilter.setRotamerTitration(true);
86      protonFilter.readFile();
87      protonFilter.applyAtomProperties();
88      protonatedAssembly.finalize(true, forceField);
89      potentialEnergy = ForceFieldEnergy.energyFactory(protonatedAssembly);
90      protonatedAssembly.setFile(structureFile);
91  
92      TitrationUtils titrationUtils;
93      titrationUtils = new TitrationUtils(protonatedAssembly.getForceField());
94      titrationUtils.setRotamerPhBias(298.15, pH);
95      for (Residue residue : protonatedAssembly.getResidueList()) {
96        String resName = residue.getName();
97        if (resNumberList.contains(residue.getResidueNumber())) {
98          if (resName.equalsIgnoreCase("ASH") || resName.equalsIgnoreCase("GLH")
99              || resName.equalsIgnoreCase("LYS") || resName.equalsIgnoreCase("HIS")
100             || resName.equalsIgnoreCase("CYS")) {
101           residue.setTitrationUtils(titrationUtils);
102         }
103       }
104     }
105     if (potentialEnergy instanceof OpenMMEnergy openMMEnergy) {
106       boolean updateBondedTerms = forceField.getBoolean("TITRATION_UPDATE_BONDED_TERMS", true);
107       openMMEnergy.getSystem().setUpdateBondedTerms(updateBondedTerms);
108     }
109     potentialEnergy.energy();
110     return protonatedAssembly;
111   }
112 
113   public MolecularAssembly[] getProtonatedAssemblies() {
114     logger.info("Getting protonated assemblies");
115     MolecularAssembly molecularAssembly = getProtonatedAssembly();
116     List<Character> altLocs = protonFilter.getAltLocs();
117     int locs = 1;
118     if(altLocs!=null){
119       locs = altLocs.size();
120       for (int i = 0; i < locs; i++) {
121         if (altLocs.get(i) == null) {
122           altLocs.remove(altLocs.get(i));
123         }
124       }
125     }
126     MolecularAssembly[] molecularAssemblies = new MolecularAssembly[locs];
127     molecularAssemblies[0] = molecularAssembly;
128     for (int i = 0; i < altLocs.size(); i++) {
129       if (i != 0) {
130         logger.info(filename);
131         MolecularAssembly newAssembly = new MolecularAssembly(filename);
132         newAssembly.setForceField(forceField);
133         File structureFile = new File(filename);
134         protonFilter = new PDBFilter(structureFile, newAssembly, forceField,
135             forceField.getProperties(), resNumberList);
136         logger.info(newAssembly.getResidueList().toString());
137         protonFilter.setRotamerTitration(true);
138         protonFilter.setAltID(newAssembly, altLocs.get(i));
139         protonFilter.readFile();
140         logger.info(newAssembly.getResidueList().get(0).getAtomList().get(0).getAltLoc().toString());
141         protonFilter.applyAtomProperties();
142         newAssembly.finalize(true, forceField);
143         potentialEnergy = ForceFieldEnergy.energyFactory(newAssembly);
144 
145         TitrationUtils titrationUtils;
146         titrationUtils = new TitrationUtils(molecularAssembly.getForceField());
147         titrationUtils.setRotamerPhBias(298.15, pH);
148         for (Residue residue : molecularAssembly.getResidueList()) {
149           String resName = residue.getName();
150           if (resNumberList.contains(residue.getResidueNumber())) {
151             if (resName.equalsIgnoreCase("ASH") || resName.equalsIgnoreCase("GLH")
152                 || resName.equalsIgnoreCase("LYS") || resName.equalsIgnoreCase("HIS")
153                 || resName.equalsIgnoreCase("CYS")) {
154               residue.setTitrationUtils(titrationUtils);
155             }
156           }
157         }
158         potentialEnergy.energy();
159         molecularAssemblies[i] = newAssembly;
160       }
161     }
162     return molecularAssemblies;
163   }
164 
165 
166   public boolean excludeExcessAtoms(Set<Atom> excludeAtoms, int[] optimalRotamers,
167       List<Residue> residueList) {
168     boolean isTitrating = false;
169     int i = 0;
170     for (Residue residue : residueList) {
171       Rotamer rotamer = residue.getRotamers()[optimalRotamers[i++]];
172       applyRotamer(residue, rotamer);
173       if (rotamer.isTitrating) {
174         isTitrating = true;
175         AminoAcidUtils.AminoAcid3 aa3 = rotamer.aminoAcid3;
176         residue.setName(aa3.name());
177         switch (aa3) {
178           case HID, GLU -> {
179             // No HE2
180             Atom HE2 = residue.getAtomByName("HE2", true);
181             excludeAtoms.add(HE2);
182           }
183           case HIE -> {
184             // No HD1
185             Atom HD1 = residue.getAtomByName("HD1", true);
186             excludeAtoms.add(HD1);
187           }
188           case ASP -> {
189             // No HD2
190             Atom HD2 = residue.getAtomByName("HD2", true);
191             excludeAtoms.add(HD2);
192           }
193           case LYD -> {
194             // No HZ3
195             Atom HZ3 = residue.getAtomByName("HZ3", true);
196             excludeAtoms.add(HZ3);
197           }
198           case CYD -> {
199             // No HG
200             Atom HG = residue.getAtomByName("HG", true);
201             excludeAtoms.add(HG);
202           }
203           default -> {
204           }
205           // Do nothing.
206         }
207       }
208     }
209 
210     return isTitrating;
211   }
212 
213 
214 }