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.numerics.Potential;
41  import ffx.potential.MolecularAssembly;
42  import ffx.potential.cli.PotentialCommand;
43  import ffx.potential.parsers.CIFFilter;
44  import ffx.potential.parsers.SystemFilter;
45  import ffx.utilities.FFXBinding;
46  import picocli.CommandLine.Command;
47  import picocli.CommandLine.Option;
48  import picocli.CommandLine.Parameters;
49  
50  import java.io.File;
51  import java.util.ArrayList;
52  import java.util.List;
53  
54  import static org.apache.commons.io.FilenameUtils.getExtension;
55  import static org.apache.commons.io.FilenameUtils.getName;
56  import static org.apache.commons.io.FilenameUtils.removeExtension;
57  
58  /**
59   * Convert a CIF file to PDB/XYZ format.
60   *
61   * Usage:
62   *   ffxc ImportCIF <filename.cif> <filename>
63   */
64  @Command(name = "ImportCIF", description = " Convert a CIF file to PDB/XYZ format.")
65  public class ImportCIF extends PotentialCommand {
66  
67    /** --zp or --zPrime Manually specify Z' (only affects writing CIF files). */
68    @Option(names = {"--zp", "--zPrime"}, paramLabel = "-1", defaultValue = "-1",
69        description = "Specify Z' when writing a CIF file.")
70    private int zPrime = -1;
71  
72    /** --sg or --spaceGroupNumber Override the CIF space group. */
73    @Option(names = {"--sg", "--spaceGroupNumber"}, paramLabel = "-1", defaultValue = "-1",
74        description = "Override the CIF space group.")
75    private int sgNum = -1;
76  
77    /** --name or --spaceGroupName Override the CIF space group. */
78    @Option(names = {"--name", "--spaceGroupName"}, paramLabel = "", defaultValue = "",
79        description = "Override the CIF space group.")
80    private String sgName = "";
81  
82    /** --bt or --bondTolerance Tolerance added to covalent radius to bond atoms. */
83    @Option(names = {"--bt", "--bondTolerance"}, paramLabel = "0.2", defaultValue = "0.2",
84        description = "Tolerance added to covalent radius to determine if atoms should be bonded.")
85    private double bondTolerance = 0.2;
86  
87    /** --fl or --fixLattice Override CIF parameters to satisfy lattice conditions. */
88    @Option(names = {"--fl", "--fixLattice"}, defaultValue = "false",
89        description = "Override CIF parameters to satisfy lattice conditions (Otherwise error).")
90    private boolean fixLattice = false;
91  
92    /** --sc or --saveCIF Save file as a basic CIF. */
93    @Option(names = {"--sc", "--saveCIF"}, defaultValue = "false",
94        description = "Attempt to save file in CIF format (input(s) in XYZ format).")
95    private boolean saveCIF = false;
96  
97    /** --ca or --cifAppend Append cif files. */
98    @Option(names = {"--ca", "--cifAppend"}, defaultValue = "false",
99        description = "Append structures.")
100   private boolean cifAppend = false;
101 
102   /** The final argument(s) should be a CIF file and a PDB or XYZ file that has been parameterized. */
103   @Parameters(arity = "1..2", paramLabel = "files",
104       description = "A CIF file and a PDB or XYZ file (already parameterized) containing one of each molecule from the CIF.")
105   private List<String> filenames = null;
106 
107   /** Array of strings containing files created. */
108   public String[] createdFiles = null;
109 
110   public ImportCIF() { super(); }
111   public ImportCIF(FFXBinding binding) { super(binding); }
112   public ImportCIF(String[] args) { super(args); }
113 
114   @Override
115   public ImportCIF run() {
116     // Turn off CDK logging.
117     System.setProperty("cdk.logging.level", "fatal");
118     // Turn off non-bonded interactions.
119     System.setProperty("vdwterm", "false");
120 
121     if (!init()) {
122       return this;
123     }
124 
125     if (filenames != null) {
126       int fileInputs = filenames.size();
127       System.clearProperty("mpoleterm");
128       File saveFile;
129       String dirString = getBaseDirString(filenames.get(0));
130       String name = removeExtension(getName(filenames.get(0)));
131 
132       if (saveCIF) {
133         // Open all coordinate files specified (XYZ/PDB trajectory) and write CIF.
134         MolecularAssembly[] opened = potentialFunctions.openAll(filenames.toArray(new String[0]));
135         SystemFilter systemFilter = potentialFunctions.getFilter();
136         if (systemFilter == null) {
137           logger.warning(" No systems were opened to save as CIF.");
138           return this;
139         }
140         do {
141           activeAssembly = systemFilter.getActiveMolecularSystem();
142           saveFile = new File(dirString + name + ".cif");
143           CIFFilter cifFilter = new CIFFilter(saveFile, activeAssembly, activeAssembly.getForceField(), activeAssembly.getProperties(), true);
144           cifFilter.setBondTolerance(bondTolerance);
145           cifFilter.setFixLattice(fixLattice);
146           cifFilter.setSgName(sgName);
147           cifFilter.setSgNum(sgNum);
148           cifFilter.setZprime(zPrime);
149           if (cifFilter.writeFile(saveFile, cifAppend)) {
150             createdFiles = cifFilter.getCreatedFileNames();
151           } else {
152             logger.warning(" Assembly " + activeAssembly.getName() + " was not successful...");
153           }
154         } while (systemFilter.readNext());
155       } else if (fileInputs == 2) {
156         // Read CIF and write to template (XYZ/PDB) extension.
157         getActiveAssembly(filenames.get(1));
158         String ext = getExtension(filenames.get(1));
159         saveFile = new File(dirString + name + "." + ext);
160         CIFFilter cifFilter = new CIFFilter(saveFile, activeAssembly, activeAssembly.getForceField(), activeAssembly.getProperties(), false);
161         cifFilter.setBondTolerance(bondTolerance);
162         cifFilter.setFixLattice(fixLattice);
163         cifFilter.setSgName(sgName);
164         cifFilter.setSgNum(sgNum);
165         cifFilter.setZprime(zPrime);
166         if (cifFilter.readFile()) {
167           createdFiles = cifFilter.getCreatedFileNames();
168         } else {
169           logger.info(" Error occurred during conversion.");
170         }
171       } else {
172         logger.info(helpString());
173         logger.info(" Expected 2 files as input to convert CIF file(s).");
174         return this;
175       }
176     } else {
177       logger.info(helpString());
178       logger.info(" Expected 1 or 2 file(s) as input to ImportCIF.");
179       return this;
180     }
181 
182     return this;
183   }
184 
185   @Override
186   public List<Potential> getPotentials() {
187     return new ArrayList<>();
188   }
189 }