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