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.potential.commands.test;
39  
40  import ffx.potential.ForceFieldEnergy;
41  import ffx.potential.MolecularAssembly;
42  import ffx.potential.cli.PotentialCommand;
43  import ffx.potential.parameters.ForceField;
44  import ffx.potential.parameters.TitrationUtils;
45  import ffx.potential.parsers.ForceFieldFilter;
46  import ffx.potential.parsers.PDBFilter;
47  import ffx.utilities.FFXBinding;
48  import ffx.utilities.Keyword;
49  import org.apache.commons.configuration2.CompositeConfiguration;
50  import org.apache.commons.io.FilenameUtils;
51  import picocli.CommandLine.Command;
52  import picocli.CommandLine.Option;
53  import picocli.CommandLine.Parameters;
54  
55  import java.io.File;
56  
57  import static java.lang.String.format;
58  
59  /**
60   * Save a PDB file that includes all titrating tautomer hydrogen atoms.
61   * <br>
62   * Usage:
63   * <br>
64   * ffxc test.SaveAsConstantPhPDB [options] &lt;filename&gt;
65   */
66  @Command(description = " Save a PDB file that includes all titrating tautomer hydrogen atoms.",
67          name = "test.SaveAsConstantPhPDB")
68  public class SaveAsConstantPhPDB extends PotentialCommand {
69  
70    @Option(names = {"--rt", "--rotamerTitration"}, paramLabel = "false",
71            description = "Prepare PDB for rotamer optimization with titration states.")
72    private boolean rotamerTitration = false;
73  
74    /**
75     * The final argument should be a PDB coordinate file.
76     */
77    @Parameters(arity = "1", paramLabel = "file",
78            description = "The atomic coordinate file in PDB format.")
79    private String filename = null;
80  
81    /**
82     * SaveAsConstantPhPDB constructor.
83     */
84    public SaveAsConstantPhPDB() {
85      super();
86    }
87  
88    /**
89     * SaveAsConstantPhPDB constructor.
90     * @param binding The Binding to use.
91     */
92    public SaveAsConstantPhPDB(FFXBinding binding) {
93      super(binding);
94    }
95  
96    /**
97     * SaveAsConstantPhPDB constructor that sets the command line arguments.
98     * @param args Command line arguments.
99     */
100   public SaveAsConstantPhPDB(String[] args) {
101     super(args);
102   }
103 
104   /**
105    * Execute the script.
106    */
107   @Override
108   public SaveAsConstantPhPDB run() {
109 
110     if (!init()) {
111       return this;
112     }
113 
114     if (rotamerTitration) {
115       logger.info("\n Adding rotamer optimization with titration protons to : " + filename + "\n");
116     } else {
117       logger.info("\n Adding constant pH protons to: " + filename + "\n");
118     }
119 
120     // Read in command line.
121     File structureFile = new File(filename);
122     int index = filename.lastIndexOf(".");
123     String name = filename.substring(0, index);
124     activeAssembly = new MolecularAssembly(name);
125     activeAssembly.setFile(structureFile);
126 
127     CompositeConfiguration properties = Keyword.loadProperties(structureFile);
128     ForceFieldFilter forceFieldFilter = new ForceFieldFilter(properties);
129     ForceField forceField = forceFieldFilter.parse();
130     String[] patches = properties.getStringArray("patch");
131     if (patches != null) {
132       for (String patch : patches) {
133         logger.info(" Attempting to read force field patch from " + patch + ".");
134         CompositeConfiguration patchConfiguration = new CompositeConfiguration();
135         patchConfiguration.addProperty("parameters", patch);
136         forceFieldFilter = new ForceFieldFilter(patchConfiguration);
137         ForceField patchForceField = forceFieldFilter.parse();
138         forceField.append(patchForceField);
139       }
140     }
141     activeAssembly.setForceField(forceField);
142 
143     PDBFilter pdbFilter = new PDBFilter(structureFile, activeAssembly, forceField, properties);
144     if (rotamerTitration) {
145       pdbFilter.setRotamerTitration(true);
146     } else {
147       pdbFilter.setConstantPH(true);
148     }
149 
150     pdbFilter.readFile();
151     pdbFilter.applyAtomProperties();
152     // Create Potential in same fashion as in PotentialsFileOpener since we manually create mola vs using getActiveAs()
153     ForceFieldEnergy potential = ForceFieldEnergy.energyFactory(activeAssembly);
154     activeAssembly.setPotential(potential);
155     activeAssembly.finalize(true, forceField);
156 
157     // Configure the base directory if it has not been set.
158     File saveDir = baseDir;
159     if (saveDir == null || !saveDir.exists() || !saveDir.isDirectory() || !saveDir.canWrite()) {
160       saveDir = new File(FilenameUtils.getFullPath(filename));
161     }
162 
163     String dirName = saveDir.getAbsolutePath() + File.separator;
164     String fileName = FilenameUtils.getBaseName(filename);
165     File modelFile = new File(dirName + fileName + ".pdb");
166     modelFile = potentialFunctions.versionFile(modelFile);
167 
168     if (!pdbFilter.writeFile(modelFile, false, false, true)) {
169       logger.info(format(" Save failed for %s", activeAssembly));
170     }
171 
172     TitrationUtils constantPhUtils = new TitrationUtils(forceField);
173 
174     return this;
175   }
176 }