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.Potential;
42  import ffx.potential.MolecularAssembly;
43  import ffx.potential.bonded.Atom;
44  import ffx.potential.cli.PotentialCommand;
45  import ffx.utilities.FFXBinding;
46  import picocli.CommandLine.Command;
47  import picocli.CommandLine.Parameters;
48  
49  import java.io.File;
50  import java.util.ArrayList;
51  import java.util.Arrays;
52  import java.util.List;
53  import java.util.stream.Collectors;
54  
55  import static org.apache.commons.io.FilenameUtils.getExtension;
56  import static org.apache.commons.io.FilenameUtils.getName;
57  import static org.apache.commons.io.FilenameUtils.removeExtension;
58  
59  /**
60   * Convert from Cartesian to fractional coordinates.
61   *
62   * Usage:
63   *   ffxc Cart2Frac <filename>
64   */
65  @Command(name = "Cart2Frac", description = " Convert from Cartesian to fractional coordinates.")
66  public class Cart2Frac extends PotentialCommand {
67  
68    /** The final argument should be a file in PDB or XYZ format. */
69    @Parameters(arity = "1", paramLabel = "file",
70        description = "The atomic coordinate file in PDB or XYZ format.")
71    private String filename = null;
72  
73    /** Save a reference to the MolecularAssembly instances to destroy their potentials. */
74    private MolecularAssembly[] molecularAssemblies;
75  
76    /** Cartesian coordinate input. */
77    private double[][][] cartCoordinates = null;
78  
79    /** Fractional coordinate output. */
80    private double[][][] fracCoordinates = null;
81  
82    public Cart2Frac() {
83      super();
84    }
85  
86    public Cart2Frac(FFXBinding binding) {
87      super(binding);
88    }
89  
90    public Cart2Frac(String[] args) {
91      super(args);
92    }
93  
94    /** Return Cartesian Coordinate input. */
95    public double[][][] getCart() {
96      return cartCoordinates;
97    }
98  
99    /** Return Fractional Coordinate output. */
100   public double[][][] getFrac() {
101     return fracCoordinates;
102   }
103 
104   @Override
105   public Cart2Frac run() {
106     if (!init()) {
107       return this;
108     }
109 
110     // Load one or more MolecularAssembly instances.
111     molecularAssemblies = getActiveAssemblies(filename);
112     if (activeAssembly == null) {
113       logger.info(helpString());
114       return this;
115     }
116 
117     // Set the filename.
118     filename = activeAssembly.getFile().getAbsolutePath();
119 
120     int num = molecularAssemblies.length;
121     cartCoordinates = new double[num][][];
122     fracCoordinates = new double[num][][];
123 
124     // Loop over each system.
125     for (int i = 0; i < num; i++) {
126       MolecularAssembly ma = molecularAssemblies[i];
127       logger.info("\n Converting from Cartesian to fractional coordinates for " + ma);
128       Crystal crystal = ma.getCrystal().getUnitCell();
129 
130       Atom[] atoms = ma.getAtomArray();
131       int nAtoms = atoms.length;
132       fracCoordinates[i] = new double[nAtoms][3];
133       cartCoordinates[i] = new double[nAtoms][3];
134 
135       double[] frac = new double[3];
136       double[] cart = new double[3];
137 
138       for (int index = 0; index < nAtoms; index++) {
139         Atom atom = atoms[index];
140         atom.getXYZ(cart);
141         crystal.toFractionalCoordinates(cart, frac);
142 
143         // If the atom is at a special position, make sure it's active so the coordinates are updated.
144         boolean active = atom.isActive();
145         atom.setActive(true);
146         atom.moveTo(frac);
147         atom.setActive(active);
148 
149         cartCoordinates[i][index][0] = cart[0];
150         cartCoordinates[i][index][1] = cart[1];
151         cartCoordinates[i][index][2] = cart[2];
152 
153         fracCoordinates[i][index][0] = frac[0];
154         fracCoordinates[i][index][1] = frac[1];
155         fracCoordinates[i][index][2] = frac[2];
156       }
157     }
158 
159     // Get the base name of the file and its extension.
160     String name = getName(filename);
161     String ext = getExtension(name);
162     name = removeExtension(name);
163 
164     // Use the current base directory, or update if necessary based on the given filename.
165     String dirString = getBaseDirString(filename);
166 
167     if (ext.toUpperCase().contains("XYZ")) {
168       potentialFunctions.saveAsXYZ(molecularAssemblies[0], new File(dirString + name + ".xyz"));
169     } else {
170       potentialFunctions.saveAsPDB(molecularAssemblies, new File(dirString + name + ".pdb"));
171     }
172 
173     // Export results via Binding for compatibility.
174     binding.setVariable("cart", cartCoordinates);
175     binding.setVariable("frac", fracCoordinates);
176 
177     return this;
178   }
179 
180   @Override
181   public List<Potential> getPotentials() {
182     if (molecularAssemblies == null) {
183       return new ArrayList<>();
184     } else {
185       return Arrays.stream(molecularAssemblies)
186           .filter(a -> a != null)
187           .map(MolecularAssembly::getPotentialEnergy)
188           .filter(e -> e != null)
189           .collect(Collectors.toList());
190     }
191   }
192 }