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.numerics.Potential;
42  import ffx.potential.MolecularAssembly;
43  import ffx.potential.bonded.Atom;
44  import ffx.potential.cli.AtomSelectionOptions;
45  import ffx.utilities.FFXBinding;
46  import ffx.xray.DiffractionData;
47  import ffx.xray.cli.XrayOptions;
48  import org.apache.commons.configuration2.CompositeConfiguration;
49  import picocli.CommandLine.Command;
50  import picocli.CommandLine.Mixin;
51  import picocli.CommandLine.Option;
52  import picocli.CommandLine.Parameters;
53  
54  import java.util.ArrayList;
55  import java.util.Arrays;
56  import java.util.List;
57  import java.util.stream.Collectors;
58  
59  import static java.lang.String.format;
60  import static org.apache.commons.io.FilenameUtils.removeExtension;
61  
62  /**
63   * The X-ray ModelvsData script.
64   * <br>
65   * Usage:
66   * <br>
67   * ffxc xray.ModelvsData [options] &lt;filename [file2...]&gt;
68   */
69  @Command(description = " Compare the PDB model to the diffraction data.", name = "xray.ModelvsData")
70  public class ModelvsData extends AlgorithmsCommand {
71  
72    @Mixin
73    private XrayOptions xrayOptions;
74  
75    @Mixin
76    AtomSelectionOptions atomSelectionOptions;
77  
78    /**
79     * -p or --maps Output sigmaA weighted 2Fo-Fc and Fo-Fc maps.
80     */
81    @Option(names = {"-p", "--maps"}, paramLabel = "false",
82        description = "Output sigmaA weighted 2Fo-Fc and Fo-Fc maps.")
83    private boolean maps = false;
84  
85    /**
86     * -n or --normalize Normalize maps to a standard deviation of 1.0.
87     */
88    @Option(names = {"-n", "--normalize"}, paramLabel = "false",
89        description = "Normalize maps to a standard deviation of 1.0.")
90    private boolean normalize = false;
91  
92    /**
93     * -t or --timings Perform FFT timings.
94     */
95    @Option(names = {"-t", "--timings"}, paramLabel = "false",
96        description = "Perform FFT timings.")
97    private boolean timings = false;
98  
99    /**
100    * -w or --mtz Write out MTZ containing structure factor coefficients.
101    */
102   @Option(names = {"-w", "--mtz"}, paramLabel = "false",
103       description = "write out MTZ containing structure factor coefficients.")
104   private boolean mtz = false;
105 
106   /**
107    * One or more filenames.
108    */
109   @Parameters(arity = "1..*", paramLabel = "files", description = "PDB and Diffraction input files.")
110   private List<String> filenames;
111   private DiffractionData diffractionData;
112   private MolecularAssembly[] molecularAssemblies;
113 
114   /**
115    * ModelvsData constructor.
116    */
117   public ModelvsData() {
118     super();
119   }
120 
121   /**
122    * ModelvsData constructor that sets the command line arguments.
123    *
124    * @param args Command line arguments.
125    */
126   public ModelvsData(String[] args) {
127     super(args);
128   }
129 
130   /**
131    * ModelvsData constructor.
132    *
133    * @param binding The Binding to use.
134    */
135   public ModelvsData(FFXBinding binding) {
136     super(binding);
137   }
138 
139   @Override
140   public ModelvsData run() {
141 
142     if (!init()) {
143       return this;
144     }
145 
146     xrayOptions.init();
147 
148     String filename;
149     if (filenames != null && !filenames.isEmpty()) {
150       // Each alternate conformer is returned in a separate MolecularAssembly.
151       molecularAssemblies = algorithmFunctions.openAll(filenames.get(0));
152       activeAssembly = molecularAssemblies[0];
153       filename = filenames.get(0);
154     } else if (activeAssembly == null) {
155       logger.info(helpString());
156       return this;
157     } else {
158       molecularAssemblies = new MolecularAssembly[]{activeAssembly};
159       filename = activeAssembly.getFile().getAbsolutePath();
160     }
161 
162     logger.info(format("\n Running xray.ModelvsData on %s", filename));
163 
164     // Apply active atom flags.
165     for (MolecularAssembly molecularAssembly : molecularAssemblies) {
166       atomSelectionOptions.setActiveAtoms(molecularAssembly);
167     }
168 
169     // Combine script flags (in parseResult) with properties.
170     CompositeConfiguration properties = activeAssembly.getProperties();
171     xrayOptions.setProperties(parseResult, properties);
172 
173     // Set up diffraction data (can be multiple files)
174     diffractionData = xrayOptions.getDiffractionData(filenames, molecularAssemblies, properties);
175 
176     boolean useHydrogen = properties.getBoolean("use-hydrogen", true);
177     if (!useHydrogen) {
178       Atom[] atoms = activeAssembly.getAtomArray();
179       for (Atom atom : atoms) {
180         if (atom.isHydrogen()) {
181           atom.setOccupancy(0.0);
182           // atom.setUse(false);
183         }
184       }
185     }
186 
187     diffractionData.scaleBulkFit();
188     diffractionData.printStats();
189     algorithmFunctions.energy(molecularAssemblies);
190 
191     if (mtz) {
192       diffractionData.writeData(removeExtension(filename) + "_ffx.mtz");
193     }
194 
195     if (maps) {
196       diffractionData.writeMaps(removeExtension(filename), normalize);
197     }
198 
199     if (timings) {
200       diffractionData.timings();
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 }