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.DynamicsOptions;
42  import ffx.algorithms.dynamics.MolecularDynamics;
43  import ffx.numerics.Potential;
44  import ffx.potential.MolecularAssembly;
45  import ffx.potential.cli.WriteoutOptions;
46  import ffx.utilities.FFXBinding;
47  import ffx.xray.DiffractionData;
48  import ffx.xray.RefinementEnergy;
49  import ffx.xray.cli.XrayOptions;
50  import org.apache.commons.configuration2.CompositeConfiguration;
51  import org.apache.commons.io.FilenameUtils;
52  import picocli.CommandLine.Command;
53  import picocli.CommandLine.Mixin;
54  import picocli.CommandLine.Parameters;
55  
56  import java.io.File;
57  import java.util.Collections;
58  import java.util.List;
59  
60  /**
61   * The X-ray Dynamics script.
62   * <br>
63   * Usage:
64   * <br>
65   * ffxc xray.Dynamics [options] &lt;filename&gt;
66   */
67  @Command(description = " Run Dynamics on an X-ray target.", name = "xray.Dynamics")
68  public class Dynamics extends AlgorithmsCommand {
69  
70    @Mixin
71    private XrayOptions xrayOptions;
72  
73    @Mixin
74    private DynamicsOptions dynamicsOptions;
75  
76    @Mixin
77    private WriteoutOptions writeoutOptions;
78  
79    /**
80     * One or more filenames.
81     */
82    @Parameters(arity = "1..*", paramLabel = "files", description = "PDB and Diffraction input files.")
83    private List<String> filenames;
84    private RefinementEnergy refinementEnergy;
85  
86    /**
87     * Dynamics constructor.
88     */
89    public Dynamics() {
90      super();
91    }
92  
93    /**
94     * Dynamics constructor that sets the command line arguments.
95     * @param args Command line arguments.
96     */
97    public Dynamics(String[] args) {
98      super(args);
99    }
100 
101   /**
102    * Dynamics constructor.
103    * @param binding The Binding to use.
104    */
105   public Dynamics(FFXBinding binding) {
106     super(binding);
107   }
108 
109   @Override
110   public Dynamics run() {
111 
112     if (!init()) {
113       return this;
114     }
115 
116     dynamicsOptions.init();
117     xrayOptions.init();
118 
119     String filename;
120     MolecularAssembly[] molecularAssemblies;
121     if (filenames != null && !filenames.isEmpty()) {
122       molecularAssemblies = algorithmFunctions.openAll(filenames.get(0));
123       activeAssembly = molecularAssemblies[0];
124       filename = filenames.get(0);
125     } else if (activeAssembly == null) {
126       logger.info(helpString());
127       return this;
128     } else {
129       molecularAssemblies = new MolecularAssembly[]{activeAssembly};
130       filename = activeAssembly.getFile().getAbsolutePath();
131     }
132 
133     logger.info("\n Running xray.Dynamics on " + filename);
134 
135     // Load parsed X-ray properties.
136     CompositeConfiguration properties = molecularAssemblies[0].getProperties();
137     xrayOptions.setProperties(parseResult, properties);
138 
139     // Set up diffraction data (can be multiple files)
140     DiffractionData diffractionData = xrayOptions.getDiffractionData(filenames, molecularAssemblies, properties);
141     refinementEnergy = xrayOptions.toXrayEnergy(diffractionData);
142     algorithmFunctions.energy(molecularAssemblies);
143 
144     // Restart File
145     File dyn = new File(FilenameUtils.removeExtension(filename) + ".dyn");
146     if (!dyn.exists()) {
147       dyn = null;
148     }
149 
150     MolecularDynamics molecularDynamics =
151         dynamicsOptions.getDynamics(writeoutOptions, refinementEnergy, activeAssembly, algorithmListener);
152     refinementEnergy.setThermostat(molecularDynamics.getThermostat());
153     boolean initVelocities = true;
154     molecularDynamics.dynamic(dynamicsOptions.getSteps(), dynamicsOptions.getDt(), dynamicsOptions.getReport(),
155         dynamicsOptions.getWrite(), dynamicsOptions.getTemperature(), initVelocities, dyn);
156 
157     // Print the final refinement statistics.
158     diffractionData.scaleBulkFit();
159     diffractionData.printStats();
160 
161     // Print the final energy of each conformer.
162     algorithmFunctions.energy(molecularAssemblies);
163 
164     logger.info(" ");
165     diffractionData.writeModel(FilenameUtils.removeExtension(filename) + ".pdb");
166     diffractionData.writeData(FilenameUtils.removeExtension(filename) + ".mtz");
167 
168     return this;
169   }
170 
171   @Override
172   public List<Potential> getPotentials() {
173     return refinementEnergy == null ? Collections.emptyList() :
174         Collections.singletonList((Potential) refinementEnergy);
175   }
176 }