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.potential.cli.PotentialCommand;
41  import ffx.potential.cli.SaveOptions;
42  import ffx.potential.parsers.SystemFilter;
43  import ffx.potential.utils.PotentialsUtils;
44  import ffx.utilities.FFXBinding;
45  import picocli.CommandLine.Command;
46  import picocli.CommandLine.Mixin;
47  import picocli.CommandLine.Option;
48  import picocli.CommandLine.Parameters;
49  
50  import java.io.File;
51  
52  import static org.apache.commons.io.FilenameUtils.getExtension;
53  import static org.apache.commons.io.FilenameUtils.getName;
54  import static org.apache.commons.io.FilenameUtils.removeExtension;
55  
56  /**
57   * Expand the system to P1 and then save it.
58   *
59   * Usage:
60   *   ffxc SaveAsP1 [options] <filename>
61   */
62  @Command(name = "SaveAsP1", description = " Expand the system to P1 and then save it.")
63  public class SaveAsP1 extends PotentialCommand {
64  
65    @Mixin
66    private SaveOptions saveOptions = new SaveOptions();
67  
68    /** --lmn or --replicatesVector Number of unit cells in replicates crystal. */
69    @Option(names = {"--lmn", "--replicatesVector"}, paramLabel = "", defaultValue = "",
70        description = "Dimension of replicates crystal (e.g., \"2,2,2\" for a 2 x 2 x 2).")
71    private String lmn = "";
72  
73    /** The final argument is a PDB or XYZ coordinate file. */
74    @Parameters(arity = "1", paramLabel = "file",
75        description = "The atomic coordinate file in PDB or XYZ format.")
76    private String filename = null;
77  
78    public SaveAsP1() { super(); }
79    public SaveAsP1(FFXBinding binding) { super(binding); }
80    public SaveAsP1(String[] args) { super(args); }
81  
82    @Override
83    public SaveAsP1 run() {
84      if (!init()) {
85        return this;
86      }
87  
88      activeAssembly = getActiveAssembly(filename);
89      if (activeAssembly == null) {
90        logger.info(helpString());
91        return this;
92      }
93  
94      // Set the filename.
95      filename = activeAssembly.getFile().getAbsolutePath();
96  
97      logger.info("\n Expanding to P1 for " + filename);
98  
99      // Use the current base directory, or update if necessary based on the given filename.
100     String dirString = getBaseDirString(filename);
101 
102     String name = getName(filename);
103     String ext = getExtension(name);
104     name = removeExtension(name);
105 
106     String[] tokens = lmn.trim().replaceAll(" +", "").split(",");
107     int numTokens = tokens.length;
108     int[] replicatesVector = new int[3];
109     boolean noReplicate;
110     if (numTokens == 0 || (numTokens == 1 && tokens[0].isEmpty())) {
111       noReplicate = true;
112     } else if (numTokens == 1) {
113       noReplicate = false;
114       replicatesVector[0] = Integer.parseInt(tokens[0]);
115       replicatesVector[1] = Integer.parseInt(tokens[0]);
116       replicatesVector[2] = Integer.parseInt(tokens[0]);
117     } else if (numTokens == 3) {
118       noReplicate = false;
119       replicatesVector[0] = Integer.parseInt(tokens[0]);
120       replicatesVector[1] = Integer.parseInt(tokens[1]);
121       replicatesVector[2] = Integer.parseInt(tokens[2]);
122     } else {
123       logger.warning(" Replicates indices could not be parsed (should be a comma separated list of integers). Saving as P1.");
124       noReplicate = true;
125     }
126 
127     if (ext.toUpperCase().contains("XYZ")) {
128       File saveLocation = SystemFilter.version(new File(dirString + name + ".xyz"));
129       logger.info(" Saving P1 file to: " + saveLocation);
130       if (noReplicate) {
131         potentialFunctions.saveAsXYZinP1(activeAssembly, saveLocation);
132       } else {
133         potentialFunctions.saveAsXYZasReplicates(activeAssembly, saveLocation, replicatesVector);
134       }
135     } else {
136       File saveLocation = SystemFilter.version(new File(dirString + name + ".pdb"));
137       logger.info(" Saving symmetry mates file to: " + saveLocation);
138       if (noReplicate) {
139         potentialFunctions.saveAsPDBinP1(activeAssembly, saveLocation);
140       } else {
141         if (potentialFunctions instanceof PotentialsUtils) {
142           // LMN support via PotentialsUtils overload.
143           ((PotentialsUtils) potentialFunctions).saveAsPDBinP1(activeAssembly, saveLocation, replicatesVector);
144         }
145       }
146     }
147     return this;
148   }
149 }