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 ffx.algorithms.cli.AlgorithmsCommand;
41  import ffx.algorithms.cli.AnnealOptions;
42  import ffx.algorithms.cli.DynamicsOptions;
43  import ffx.algorithms.optimize.anneal.SimulatedAnnealing;
44  import ffx.numerics.Potential;
45  import ffx.potential.MolecularAssembly;
46  import ffx.potential.cli.AtomSelectionOptions;
47  import ffx.utilities.FFXBinding;
48  import ffx.xray.DiffractionData;
49  import ffx.xray.RefinementEnergy;
50  import ffx.xray.cli.XrayOptions;
51  import ffx.xray.refine.RefinementMode;
52  import org.apache.commons.configuration2.CompositeConfiguration;
53  import org.apache.commons.io.FilenameUtils;
54  import picocli.CommandLine.Command;
55  import picocli.CommandLine.Mixin;
56  import picocli.CommandLine.Option;
57  import picocli.CommandLine.Parameters;
58  
59  import java.io.File;
60  import java.util.List;
61  
62  /**
63   * The X-ray Annealing script.
64   * <br>
65   * Usage:
66   * <br>
67   * ffxc xray.Anneal [options] &lt;filename&gt;
68   */
69  @Command(description = " Simulated annealing on an X-ray target.", name = "xray.Anneal")
70  public class Anneal extends AlgorithmsCommand {
71  
72    @Mixin
73    private XrayOptions xrayOptions;
74  
75    @Mixin
76    private DynamicsOptions dynamicsOptions;
77  
78    @Mixin
79    AtomSelectionOptions atomSelectionOptions;
80  
81    @Mixin
82    private AnnealOptions annealOptions;
83  
84    /**
85     * --mtz Write out MTZ containing structure factor coefficients.
86     */
87    @Option(names = {"--mtz"}, paramLabel = "false",
88        description = "Write out an MTZ containing structure factor coefficients.")
89    private boolean mtz = false;
90  
91    /**
92     * One or more filenames.
93     */
94    @Parameters(arity = "1..*", paramLabel = "files", description = "PDB and Diffraction input files.")
95    private List<String> filenames;
96  
97    private MolecularAssembly[] molecularAssemblies;
98    private DiffractionData diffractionData;
99    private SimulatedAnnealing simulatedAnnealing = null;
100   private RefinementEnergy refinementEnergy;
101 
102   /**
103    * Anneal constructor.
104    */
105   public Anneal() {
106     super();
107   }
108 
109   /**
110    * Anneal constructor that sets the command line arguments.
111    *
112    * @param args Command line arguments.
113    */
114   public Anneal(String[] args) {
115     super(args);
116   }
117 
118   /**
119    * Anneal constructor.
120    *
121    * @param binding The Binding to use.
122    */
123   public Anneal(FFXBinding binding) {
124     super(binding);
125   }
126 
127   @Override
128   public Anneal run() {
129 
130     if (!init()) {
131       return this;
132     }
133 
134     dynamicsOptions.init();
135     xrayOptions.init();
136 
137     String filename;
138     if (filenames != null && !filenames.isEmpty()) {
139       // Each alternate conformer is returned in a separate MolecularAssembly.
140       molecularAssemblies = algorithmFunctions.openAll(filenames.getFirst());
141       activeAssembly = molecularAssemblies[0];
142     } else if (activeAssembly == null) {
143       logger.info(helpString());
144       return this;
145     } else {
146       molecularAssemblies = new MolecularAssembly[]{activeAssembly};
147     }
148 
149     // Update the active filename
150     filename = activeAssembly.getFile().getAbsolutePath();
151 
152     // Apply active atom flags.
153     for (MolecularAssembly molecularAssembly : molecularAssemblies) {
154       atomSelectionOptions.setActiveAtoms(molecularAssembly);
155     }
156 
157     logger.info("\n Running simulated annealing on X-ray target including " + filename + "\n");
158 
159     // Combine script flags (in parseResult) with properties.
160     CompositeConfiguration properties = activeAssembly.getProperties();
161     xrayOptions.setProperties(parseResult, properties);
162 
163     // Set up diffraction data (can be multiple files)
164     diffractionData = xrayOptions.getDiffractionData(filenames, molecularAssemblies, properties);
165     refinementEnergy = xrayOptions.toXrayEnergy(diffractionData);
166 
167     // Log the energy of each MolecularAssembly
168     algorithmFunctions.energy(molecularAssemblies);
169 
170     // Restart is currently only supported for COORDINATES mode.
171     File dyn = null;
172     if (xrayOptions.refinementMode == RefinementMode.COORDINATES) {
173       dyn = new File(FilenameUtils.removeExtension(filename) + ".dyn");
174       if (!dyn.exists()) {
175         dyn = null;
176       }
177     }
178 
179     simulatedAnnealing = annealOptions.createAnnealer(dynamicsOptions,
180         activeAssembly, refinementEnergy, algorithmListener, dyn);
181     simulatedAnnealing.setPrintInterval(dynamicsOptions.getReport());
182     simulatedAnnealing.setSaveFrequency(dynamicsOptions.getWrite());
183     simulatedAnnealing.setRestartFrequency(dynamicsOptions.getCheckpoint());
184     simulatedAnnealing.setTrajectorySteps(dynamicsOptions.getTrajSteps());
185 
186     // Run simulated annealing.
187     simulatedAnnealing.anneal();
188 
189     // Print the final refinement statistics.
190     diffractionData.scaleBulkFit();
191     diffractionData.printStats();
192 
193     // Print the final energy of each conformer.
194     algorithmFunctions.energy(molecularAssemblies);
195 
196     logger.info(" ");
197     diffractionData.writeModel(FilenameUtils.removeExtension(filename) + ".pdb");
198 
199     if (mtz) {
200       diffractionData.writeData(FilenameUtils.removeExtension(filename) + ".mtz");
201     }
202 
203     return this;
204   }
205 
206   @Override
207   public List<Potential> getPotentials() {
208     return getPotentialsFromAssemblies(molecularAssemblies);
209   }
210 
211   @Override
212   public boolean destroyPotentials() {
213     return diffractionData == null ? true : diffractionData.destroy();
214   }
215 }