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-2025.
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.xray.commands;
39  
40  import edu.rit.pj.Comm;
41  import ffx.algorithms.cli.AlgorithmsCommand;
42  import ffx.algorithms.cli.ManyBodyOptions;
43  import ffx.algorithms.optimize.RotamerOptimization;
44  import ffx.algorithms.optimize.TitrationManyBody;
45  import ffx.numerics.Potential;
46  import ffx.potential.ForceFieldEnergy;
47  import ffx.potential.MolecularAssembly;
48  import ffx.potential.bonded.Atom;
49  import ffx.potential.bonded.Residue;
50  import ffx.potential.bonded.RotamerLibrary;
51  import ffx.potential.parsers.PDBFilter;
52  import ffx.utilities.FFXBinding;
53  import ffx.xray.DiffractionData;
54  import ffx.xray.RefinementEnergy;
55  import ffx.xray.RefinementMinimize.RefinementMode;
56  import ffx.xray.cli.XrayOptions;
57  import org.apache.commons.configuration2.CompositeConfiguration;
58  import org.apache.commons.io.FilenameUtils;
59  import picocli.CommandLine.Command;
60  import picocli.CommandLine.Mixin;
61  import picocli.CommandLine.Parameters;
62  
63  import java.io.File;
64  import java.util.ArrayList;
65  import java.util.Collections;
66  import java.util.HashSet;
67  import java.util.List;
68  import java.util.Set;
69  
70  import static java.lang.String.format;
71  import static org.apache.commons.io.FilenameUtils.removeExtension;
72  
73  /**
74   * The ManyBody script performs a discrete optimization using a many-body expansion and elimination expressions.
75   * <br>
76   * Usage:
77   * <br>
78   * ffxc xray.ManyBody [options] &lt;filename&gt;
79   */
80  @Command(description = " Discrete optimization using a many-body expansion and elimination expressions.", name = "xray.ManyBody")
81  public class ManyBody extends AlgorithmsCommand {
82  
83    @Mixin
84    private XrayOptions xrayOptions;
85  
86    @Mixin
87    private ManyBodyOptions manyBodyOptions;
88  
89    /**
90     * One or more filenames.
91     */
92    @Parameters(arity = "1..*", paramLabel = "files", description = "PDB and Real Space input files.")
93    private List<String> filenames;
94  
95    private RefinementEnergy refinementEnergy;
96  
97    ForceFieldEnergy potentialEnergy;
98    private MolecularAssembly[] molecularAssemblies;
99    TitrationManyBody titrationManyBody;
100 
101   /**
102    * ManyBody constructor.
103    */
104   public ManyBody() {
105     super();
106   }
107 
108   /**
109    * ManyBody constructor that sets the command line arguments.
110    * @param args Command line arguments.
111    */
112   public ManyBody(String[] args) {
113     super(args);
114   }
115 
116   /**
117    * ManyBody constructor.
118    * @param binding The Binding to use.
119    */
120   public ManyBody(FFXBinding binding) {
121     super(binding);
122   }
123 
124   @Override
125   public ManyBody run() {
126 
127     if (!init()) {
128       return this;
129     }
130 
131     xrayOptions.init();
132 
133     // This flag is for ForceFieldEnergyOpenMM and must be set before reading files.
134     // It enforces that all torsions include a Fourier series with 6 terms.
135     // Otherwise, during titration the number of terms for each torsion may change and
136     // causing updateParametersInContext to throw an exception.
137     // Note that OpenMM is not usually used for crystals (it doesn't handle space groups).
138     double titrationPH = manyBodyOptions.getTitrationPH();
139     if (titrationPH > 0) {
140       System.setProperty("manybody-titration", "true");
141     }
142 
143     // Many-Body expansion of the X-ray target converges much more quickly with the NEA.
144     String nea = System.getProperty("native-environment-approximation", "true");
145     System.setProperty("native-environment-approximation", nea);
146 
147     String modelFilename;
148     if (filenames != null && !filenames.isEmpty()) {
149       molecularAssemblies = algorithmFunctions.openAll(filenames.get(0));
150       activeAssembly = molecularAssemblies[0];
151       modelFilename = filenames.get(0);
152     } else if (activeAssembly == null) {
153       logger.info(helpString());
154       return this;
155     } else {
156       molecularAssemblies = new MolecularAssembly[]{activeAssembly};
157       modelFilename = activeAssembly.getFile().getAbsolutePath();
158     }
159 
160     CompositeConfiguration properties = activeAssembly.getProperties();
161     activeAssembly.getPotentialEnergy().setPrintOnFailure(false, false);
162     potentialEnergy = activeAssembly.getPotentialEnergy();
163 
164     // The refinement mode must be coordinates.
165     if (xrayOptions.refinementMode != RefinementMode.COORDINATES) {
166       logger.info(" Refinement mode set to COORDINATES.");
167       xrayOptions.refinementMode = RefinementMode.COORDINATES;
168     }
169 
170     // Collect residues to optimize.
171     List<Residue> residues = manyBodyOptions.collectResidues(activeAssembly);
172     if (residues == null || residues.isEmpty()) {
173       logger.info(" There are no residues in the active system to optimize.");
174       return this;
175     }
176 
177     // Handle rotamer optimization with titration.
178     if (titrationPH > 0) {
179       logger.info(format("\n Adding titration hydrogen to: %s\n", filenames.get(0)));
180       List<Integer> resNumberList = new ArrayList<>();
181       for (Residue residue : residues) {
182         resNumberList.add(residue.getResidueNumber());
183       }
184 
185       // Create new MolecularAssembly with additional protons and update the ForceFieldEnergy
186       titrationManyBody = new TitrationManyBody(filenames.get(0), activeAssembly.getForceField(),
187           resNumberList, titrationPH, manyBodyOptions);
188       MolecularAssembly[] protonatedAssemblies = titrationManyBody.getProtonatedAssemblies();
189       setActiveAssembly(protonatedAssemblies[0]);
190       potentialEnergy = protonatedAssemblies[0].getPotentialEnergy();
191       molecularAssemblies = protonatedAssemblies;
192     }
193 
194     // Load parsed X-ray properties.
195     xrayOptions.setProperties(parseResult, properties);
196 
197     // Set up the diffraction data, which could be multiple files.
198     DiffractionData diffractionData = xrayOptions.getDiffractionData(filenames, molecularAssemblies, properties);
199     refinementEnergy = xrayOptions.toXrayEnergy(diffractionData);
200     refinementEnergy.setScaling(null);
201 
202     boolean isTitrating = false;
203     Set<Atom> excludeAtoms = new HashSet<>();
204     for (MolecularAssembly currentMolecularAssembly : molecularAssemblies) {
205       setActiveAssembly(currentMolecularAssembly);
206       currentMolecularAssembly.setFile(new File(filenames.get(0)));
207       if (currentMolecularAssembly.getAtomList().get(0).getAltLoc() == 'A' && molecularAssemblies.length > 1) {
208         for (int i = 0; i < molecularAssemblies[0].getAtomList().size(); i++) {
209           molecularAssemblies[0].getAtomList().get(i).setOccupancy(1.0);
210           molecularAssemblies[1].getAtomList().get(i).setOccupancy(0.0);
211         }
212         logger.info(" Occupancy of 1st Molecular Assembly Atoms: " + molecularAssemblies[0].getAtomList().get(0).getOccupancy());
213         logger.info(" Occupancy of 2nd Molecular Assembly Atoms: " + molecularAssemblies[1].getAtomList().get(0).getOccupancy());
214 
215       } else if (currentMolecularAssembly.getAtomList().get(0).getAltLoc() == 'B' && molecularAssemblies.length > 1) {
216         for (int i = 0; i < molecularAssemblies[0].getAtomList().size(); i++) {
217           molecularAssemblies[0].getAtomList().get(i).setOccupancy(0.5);
218           molecularAssemblies[1].getAtomList().get(i).setOccupancy(0.5);
219         }
220         logger.info(" Occupancy of 1st Molecular Assembly Atoms: " + molecularAssemblies[0].getAtomList().get(0).getOccupancy());
221         logger.info(" Occupancy of 2nd Molecular Assembly Atoms: " + molecularAssemblies[1].getAtomList().get(0).getOccupancy());
222       }
223 
224       RotamerOptimization rotamerOptimization = new RotamerOptimization(activeAssembly, refinementEnergy, algorithmListener);
225       manyBodyOptions.initRotamerOptimization(rotamerOptimization, activeAssembly);
226 
227       double[] x = new double[refinementEnergy.getNumberOfVariables()];
228       x = refinementEnergy.getCoordinates(x);
229       double e = refinementEnergy.energy(x, true);
230       logger.info(format("\n Initial target energy: %16.8f ", e));
231 
232       List<Residue> residueList = rotamerOptimization.getResidues();
233       RotamerLibrary.measureRotamers(residueList, false);
234 
235       rotamerOptimization.optimize(manyBodyOptions.getAlgorithm(residueList.size()));
236 
237       int[] optimalRotamers = rotamerOptimization.getOptimumRotamers();
238 
239 
240       if (titrationPH > 0) {
241         isTitrating = titrationManyBody.excludeExcessAtoms(excludeAtoms, optimalRotamers, residueList);
242       }
243       logger.info(" Final Minimum Energy");
244       // Get final parameters and compute the target function.
245       x = refinementEnergy.getCoordinates(x);
246       e = refinementEnergy.energy(x, true);
247 
248       if (isTitrating) {
249         double phBias = rotamerOptimization.getEnergyExpansion().getTotalRotamerPhBias(residueList, optimalRotamers, titrationPH, manyBodyOptions.getPHRestraint());
250         logger.info(format("\n  Rotamer pH Bias      %16.8f", phBias));
251         logger.info(format("  Xray Target with Bias%16.8f\n", phBias + e));
252       } else {
253         logger.info(format("\n  Xray Target          %16.8f\n", e));
254       }
255       diffractionData.scaleBulkFit();
256       diffractionData.printStats();
257     }
258 
259     if (molecularAssemblies.length > 1) {
260       List<Residue> residueListA = molecularAssemblies[0].getResidueList();
261       List<Residue> residueListB = molecularAssemblies[1].getResidueList();
262       int firstRes = residueListA.get(0).getResidueNumber();
263       for (Residue residue : residueListA) {
264         int resNum = residue.getResidueNumber();
265         List<Atom> atomList = residue.getAtomList();
266         for (int i = 0; i < residue.getAtomList().size(); i++) {
267           Atom atom = atomList.get(i);
268           String resNameA = atom.getResidueName();
269           //logger.info("Residue A: " + resNameA);
270           double coorAX = atom.getX();
271           double coorAY = atom.getY();
272           double coorAZ = atom.getZ();
273           Residue residueB = residueListB.get(resNum - firstRes);
274           List<Atom> atomListB = residueB.getAtomList();
275           Atom atomB = atomListB.get(i);
276           String resNameB = atomB.getResidueName();
277           //logger.info("Residue B: " + resNameB);
278           double coorBX = atomB.getX();
279           double coorBY = atomB.getY();
280           double coorBZ = atomB.getZ();
281           if (coorAX == coorBX && coorAY == coorBY && coorAZ == coorBZ && resNameA.equals(resNameB)) {
282             atom.setAltLoc(' ');
283             atomB.setAltLoc(' ');
284             atom.setOccupancy(1.0);
285           }
286         }
287       }
288       if (titrationPH > 0) {
289         diffractionData.writeModel(removeExtension(filenames.get(0)) + ".pdb", excludeAtoms, titrationPH);
290       } else {
291         diffractionData.writeModel(removeExtension(filenames.get(0)) + ".pdb");
292       }
293       diffractionData.writeData(removeExtension(filenames.get(0)) + ".mtz");
294     } else if (Comm.world().rank() == 0) {
295       String ext = FilenameUtils.getExtension(modelFilename);
296       modelFilename = FilenameUtils.removeExtension(modelFilename);
297       if (ext.toUpperCase().contains("XYZ")) {
298         algorithmFunctions.saveAsXYZ(activeAssembly, new File(modelFilename + ".xyz"));
299       } else {
300         properties.setProperty("standardizeAtomNames", "false");
301         File modelFile = saveDirFile(activeAssembly.getFile());
302         PDBFilter pdbFilter = new PDBFilter(modelFile, activeAssembly,
303             activeAssembly.getForceField(), properties);
304         if (titrationPH > 0) {
305           String remark = format("Titration pH: %6.3f", titrationPH);
306           if (!pdbFilter.writeFile(modelFile, false, excludeAtoms, true, true, new String[]{remark})) {
307             logger.info(format(" Save failed for %s", activeAssembly));
308           }
309         } else {
310           if (!pdbFilter.writeFile(modelFile, false, excludeAtoms, true, true)) {
311             logger.info(format(" Save failed for %s", activeAssembly));
312           }
313         }
314       }
315     }
316     return this;
317   }
318 
319 
320   /**
321    * Get the ManyBodyOptions.
322    * @return The ManyBodyOptions.
323    */
324   public ManyBodyOptions getManyBodyOptions() {
325     return manyBodyOptions;
326   }
327 
328   @Override
329   public List<Potential> getPotentials() {
330     return refinementEnergy == null ? Collections.emptyList() :
331         Collections.singletonList((Potential) refinementEnergy);
332   }
333 }