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.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 Annealing script.
62   * <br>
63   * Usage:
64   * <br>
65   * ffxc xray.Anneal [options] &lt;filename&gt;
66   */
67  @Command(description = " Simulated annealing on an X-ray target.", name = "xray.Anneal")
68  public class Anneal extends AlgorithmsCommand {
69  
70    @Mixin
71    private XrayOptions xrayOptions;
72  
73    @Mixin
74    private DynamicsOptions dynamicsOptions;
75  
76    @Mixin
77    private AnnealOptions annealOptions;
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  
85    private SimulatedAnnealing simulatedAnnealing = null;
86    private RefinementEnergy refinementEnergy;
87  
88    /**
89     * Anneal constructor.
90     */
91    public Anneal() {
92      super();
93    }
94  
95    /**
96     * Anneal constructor that sets the command line arguments.
97     * @param args Command line arguments.
98     */
99    public Anneal(String[] args) {
100     super(args);
101   }
102 
103   /**
104    * Anneal constructor.
105    * @param binding The Binding to use.
106    */
107   public Anneal(FFXBinding binding) {
108     super(binding);
109   }
110 
111   @Override
112   public Anneal run() {
113 
114     if (!init()) {
115       return this;
116     }
117 
118     dynamicsOptions.init();
119     xrayOptions.init();
120 
121     String filename;
122     MolecularAssembly[] molecularAssemblies;
123     if (filenames != null && !filenames.isEmpty()) {
124       molecularAssemblies = algorithmFunctions.openAll(filenames.get(0));
125       activeAssembly = molecularAssemblies[0];
126     } else if (activeAssembly == null) {
127       logger.info(helpString());
128       return this;
129     } else {
130       molecularAssemblies = new MolecularAssembly[]{activeAssembly};
131     }
132 
133     filename = activeAssembly.getFile().getAbsolutePath();
134 
135     logger.info("\n Running simulated annealing on X-ray target including " + filename + "\n");
136 
137     // Restart File
138     File dyn = new File(FilenameUtils.removeExtension(filename) + ".dyn");
139     if (!dyn.exists()) {
140       dyn = null;
141     }
142 
143     CompositeConfiguration properties = activeAssembly.getProperties();
144     xrayOptions.setProperties(parseResult, properties);
145     DiffractionData diffractionData = xrayOptions.getDiffractionData(filenames, molecularAssemblies, properties);
146     refinementEnergy = xrayOptions.toXrayEnergy(diffractionData);
147 
148     // Print the initial energy of each conformer.
149     algorithmFunctions.energy(molecularAssemblies);
150 
151     simulatedAnnealing = annealOptions.createAnnealer(dynamicsOptions, activeAssembly, refinementEnergy, algorithmListener, dyn);
152     simulatedAnnealing.setPrintInterval(dynamicsOptions.getReport());
153     simulatedAnnealing.setSaveFrequency(dynamicsOptions.getWrite());
154     simulatedAnnealing.setRestartFrequency(dynamicsOptions.getCheckpoint());
155     simulatedAnnealing.setTrajectorySteps(dynamicsOptions.getTrajSteps());
156 
157     // Run simulated annealing.
158     simulatedAnnealing.anneal();
159 
160     // Print the final refinement statistics.
161     diffractionData.scaleBulkFit();
162     diffractionData.printStats();
163 
164     // Print the final energy of each conformer.
165     algorithmFunctions.energy(molecularAssemblies);
166 
167     logger.info(" ");
168     diffractionData.writeModel(FilenameUtils.removeExtension(filename) + ".pdb");
169     diffractionData.writeData(FilenameUtils.removeExtension(filename) + ".mtz");
170 
171     return this;
172   }
173 
174   @Override
175   public List<Potential> getPotentials() {
176     return refinementEnergy == null ? Collections.emptyList() :
177         Collections.singletonList((Potential) refinementEnergy);
178   }
179 }