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.potential.commands;
39  
40  import ffx.crystal.Crystal;
41  import ffx.numerics.math.SummaryStatistics;
42  import ffx.potential.MolecularAssembly;
43  import ffx.potential.cli.PotentialCommand;
44  import ffx.potential.cli.WriteoutOptions;
45  import ffx.potential.parsers.SystemFilter;
46  import ffx.utilities.FFXBinding;
47  import picocli.CommandLine.Command;
48  import picocli.CommandLine.Mixin;
49  import picocli.CommandLine.Option;
50  import picocli.CommandLine.Parameters;
51  
52  import static java.lang.String.format;
53  
54  /**
55   * Calculate the mean system density for single topology systems.
56   * <p>
57   * Usage:
58   * ffxc Density [options] &lt;filename&gt;
59   */
60  @Command(name = "Density", description = " Calculate the mean system density for single topology systems.")
61  public class Density extends PotentialCommand {
62  
63    @Mixin
64    private WriteoutOptions writeoutOptions = new WriteoutOptions();
65  
66    /**
67     * First frame to evaluate (1-indexed).
68     */
69    @Option(names = {"-s", "--start"}, paramLabel = "1", defaultValue = "1",
70        description = "First frame to evaluate (1-indexed).")
71    private int start = 1;
72  
73    /**
74     * Last frame to evaluate (1-indexed); values less than 1 evaluate to end of trajectory.
75     */
76    @Option(names = {"-f", "--final"}, paramLabel = "all frames", defaultValue = "0",
77        description = "Last frame to evaluate (1-indexed); values less than 1 evaluate to end of trajectory.")
78    private int finish = 0;
79  
80    /**
81     * Stride: evaluate density every N frames. Must be positive.
82     */
83    @Option(names = {"--st", "--stride"}, paramLabel = "1", defaultValue = "1",
84        description = "Stride: evaluate density every N frames. Must be positive.")
85    private int stride = 1;
86  
87    /**
88     * Print out a file with density adjusted to match mean calculated density.
89     */
90    @Option(names = {"-p", "--printout"}, defaultValue = "false",
91        description = "Print out a file with density adjusted to match mean calculated density.")
92    private boolean doPrint = false;
93  
94    /**
95     * The final argument is a PDB or XYZ coordinate file.
96     */
97    @Parameters(arity = "1", paramLabel = "file",
98        description = "An atomic coordinate file in PDB or XYZ format.")
99    private String filename = null;
100 
101   public Density() {
102     super();
103   }
104 
105   public Density(FFXBinding binding) {
106     super(binding);
107   }
108 
109   public Density(String[] args) {
110     super(args);
111   }
112 
113   @Override
114   public Density run() {
115     // Init the context and bind variables.
116     if (!init()) {
117       return this;
118     }
119 
120     // Load the MolecularAssembly.
121     activeAssembly = getActiveAssembly(filename);
122     if (activeAssembly == null) {
123       logger.info(helpString());
124       return this;
125     }
126 
127     // Set the filename.
128     filename = activeAssembly.getFile().getAbsolutePath();
129 
130     SystemFilter openFilter = potentialFunctions.getFilter();
131     double totMass = activeAssembly.getMass();
132     Crystal crystal = activeAssembly.getCrystal().getUnitCell();
133 
134     if (crystal.aperiodic()) {
135       logger.info(format(" System %s is aperiodic: total mass %16.7g g/mol", filename, totMass));
136     } else {
137       double volume = crystal.volume / crystal.getNumSymOps();
138       double density = crystal.getDensity(totMass);
139       int nFrames = openFilter.countNumModels();
140       double[] densities = new double[nFrames];
141       int lastFrame = (finish < 1) ? nFrames : finish;
142 
143       densities[0] = density;
144       logger.info(format("\n Evaluating density for system %s with mass %16.7g (g/mol).", filename, totMass));
145       logger.info(format(" Density at frame %9d is %16.7g (g/mL) from a volume of %16.7g (A^3)", 1, density, volume));
146 
147       int ctr = 1;
148       while (openFilter.readNext(false, false)) {
149         volume = crystal.volume / crystal.getNumSymOps();
150         density = crystal.getDensity(totMass);
151         logger.info(format(" Density at frame %9d is %16.7g (g/mL) from a volume of %16.7g (A^3)", ++ctr, density, volume));
152         int i = ctr - 1;
153         densities[i] = density;
154       }
155 
156       SummaryStatistics densStats = new SummaryStatistics(densities, start - 1, lastFrame, stride);
157       logger.info(" Summary statistics for density:");
158       logger.info(densStats.toString());
159 
160       if (doPrint) {
161         activeAssembly.setFractionalMode(MolecularAssembly.FractionalMode.MOLECULE);
162         activeAssembly.computeFractionalCoordinates();
163         crystal.setDensity(densStats.mean, totMass);
164         activeAssembly.moveToFractionalCoordinates();
165         saveByExtension(activeAssembly, filename, writeoutOptions.fileType);
166         // Update filename to the written file.
167         filename = activeAssembly.getFile().getAbsolutePath();
168         logger.info("\n Saved density adjusted coordinates: " + filename);
169         logger.info("\n Molecule based fractional coordinates were maintained.");
170       }
171     }
172 
173     return this;
174   }
175 }