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 fractional to Cartesian coordinates.
61   *
62   * Usage:
63   *   ffxc Frac2Cart <filename>
64   */
65  @Command(name = "Frac2Cart", description = " Convert from fractional to Cartesian coordinates.")
66  public class Frac2Cart 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 output. */
77    private double[][] cartCoordinates = null;
78    /** Fractional coordinate input. */
79    private double[][] fracCoordinates = null;
80  
81    public Frac2Cart() { super(); }
82    public Frac2Cart(FFXBinding binding) { super(binding); }
83    public Frac2Cart(String[] args) { super(args); }
84  
85    /** Return Cartesian Coordinate output. */
86    public double[][] getCart() { return cartCoordinates; }
87    /** Return Fractional Coordinate input. */
88    public double[][] getFrac() { return fracCoordinates; }
89  
90    @Override
91    public Frac2Cart run() {
92      if (!init()) {
93        return this;
94      }
95  
96      // Load one or more MolecularAssembly instances.
97      molecularAssemblies = getActiveAssemblies(filename);
98      if (activeAssembly == null) {
99        logger.info(helpString());
100       return this;
101     }
102 
103     // Set the filename.
104     filename = activeAssembly.getFile().getAbsolutePath();
105 
106     logger.info("\n Converting from fractional to Cartesian coordinates for " + filename);
107 
108     // Loop over each system. The original Groovy implementation only exposed the last system's
109     // coordinates as 2D arrays; we preserve that API for test compatibility.
110     for (MolecularAssembly ma : molecularAssemblies) {
111       Crystal crystal = ma.getCrystal().getUnitCell();
112 
113       Atom[] atoms = ma.getAtomArray();
114       int nAtoms = atoms.length;
115       fracCoordinates = new double[nAtoms][3];
116       cartCoordinates = new double[nAtoms][3];
117 
118       double[] frac = new double[3];
119       double[] cart = new double[3];
120 
121       for (int index = 0; index < nAtoms; index++) {
122         Atom atom = atoms[index];
123         atom.getXYZ(frac);
124         crystal.toCartesianCoordinates(frac, cart);
125 
126         // If the atom is at a special position, make sure it's active so the coordinates are updated.
127         boolean active = atom.isActive();
128         atom.setActive(true);
129         atom.moveTo(cart);
130         atom.setActive(active);
131 
132         cartCoordinates[index][0] = cart[0];
133         cartCoordinates[index][1] = cart[1];
134         cartCoordinates[index][2] = cart[2];
135 
136         fracCoordinates[index][0] = frac[0];
137         fracCoordinates[index][1] = frac[1];
138         fracCoordinates[index][2] = frac[2];
139       }
140     }
141 
142     // Get the base name of the file and its extension.
143     String name = getName(filename);
144     String ext = getExtension(name);
145     name = removeExtension(name);
146 
147     // Use the current base directory, or update if necessary based on the given filename.
148     String dirString = getBaseDirString(filename);
149 
150     if (ext.toUpperCase().contains("XYZ")) {
151       potentialFunctions.saveAsXYZ(molecularAssemblies[0], new File(dirString + name + ".xyz"));
152     } else {
153       potentialFunctions.saveAsPDB(molecularAssemblies, new File(dirString + name + ".pdb"));
154     }
155 
156     // Export results via Binding for compatibility (match Cart2Frac binding names).
157     binding.setVariable("cart", cartCoordinates);
158     binding.setVariable("frac", fracCoordinates);
159 
160     return this;
161   }
162 
163   @Override
164   public List<Potential> getPotentials() {
165     if (molecularAssemblies == null) {
166       return new ArrayList<>();
167     } else {
168       return Arrays.stream(molecularAssemblies)
169           .filter(a -> a != null)
170           .map(MolecularAssembly::getPotentialEnergy)
171           .filter(e -> e != null)
172           .collect(Collectors.toList());
173     }
174   }
175 }