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.test;
39  
40  import ffx.potential.cli.PotentialCommand;
41  import ffx.potential.parameters.ChargeType;
42  import ffx.potential.parameters.ForceField;
43  import ffx.potential.parameters.MultipoleType;
44  import ffx.potential.parameters.VDWType;
45  import ffx.potential.parsers.ForceFieldFilter;
46  import ffx.utilities.FFXBinding;
47  import ffx.utilities.Keyword;
48  import org.apache.commons.configuration2.CompositeConfiguration;
49  import picocli.CommandLine.Command;
50  import picocli.CommandLine.Option;
51  import picocli.CommandLine.Parameters;
52  
53  import java.util.List;
54  import java.util.Map;
55  
56  /**
57   * The PrmToProperty script converts a TINKER *.prm file to Java properties.
58   * Usage: ffxc test.PrmToProperty <filename>
59   */
60  @Command(description = "PrmToProperty converts a TINKER *.prm file to Java properties.",
61          name = "test.PrmToProperty")
62  public class PrmToProperty extends PotentialCommand {
63  
64      /**
65       * -t or --tinker Remove line continuation characters from multi-line force field types (i.e. Tinker prm format).
66       */
67      @Option(names = {"-t", "--tinker"}, paramLabel = "false", defaultValue = "false",
68              description = "Remove line continuation characters from multi-line force field types (i.e. Tinker prm format).")
69      private boolean tinker = false;
70  
71      /**
72       * -c or --useCharges Output charge keywords instead of multipoles.
73       */
74      @Option(names = {"-c", "--useCharges"}, paramLabel = "false", defaultValue = "false",
75              description = "Output charge keywords instead of multipole keywords.")
76      private boolean useCharges = false;
77  
78      /**
79       * -z or --zeroVDW Add a zero VDW class with zero parameters.
80       */
81      @Option(names = {"-z", "--zeroVDW"}, paramLabel = "false", defaultValue = "false",
82              description = "Add a zero VDW class with zero parameters.")
83      private boolean zeroVDW = false;
84  
85      /**
86       * The final argument(s) should be one or more filenames.
87       */
88      @Parameters(arity = "1", paramLabel = "files",
89              description = "TINKER *.prm file(s).")
90      private List<String> filenames = null;
91  
92      /**
93       * PrmToProperty Constructor.
94       */
95      public PrmToProperty() {
96          super();
97      }
98  
99      /**
100      * PrmToProperty Constructor.
101      * @param binding Binding to use.
102      */
103     public PrmToProperty(FFXBinding binding) {
104         super(binding);
105     }
106 
107     /**
108      * PrmToProperty constructor that sets the command line arguments.
109      * @param args Command line arguments.
110      */
111     public PrmToProperty(String[] args) {
112         super(args);
113     }
114 
115     /**
116      * Execute the script.
117      */
118     @Override
119     public PrmToProperty run() {
120 
121         if (!init()) {
122             return this;
123         }
124 
125         if (filenames == null || filenames.size() < 1) {
126             logger.info(helpString());
127             return this;
128         }
129 
130         // Read in the command line file.
131         String prmName = filenames.get(0);
132         CompositeConfiguration properties = Keyword.loadProperties(null);
133         properties.setProperty("parameters", prmName);
134         ForceFieldFilter forceFieldFilter = new ForceFieldFilter(properties);
135 
136         ForceField forceField = forceFieldFilter.parse();
137 
138         for (int i = 1; i < filenames.size(); i++) {
139             String filename = filenames.get(i);
140             properties = Keyword.loadProperties(null);
141             properties.setProperty("parameters", filename);
142             forceFieldFilter = new ForceFieldFilter(properties);
143             ForceField forceField2 = forceFieldFilter.parse();
144             forceField.append(forceField2);
145         }
146 
147         if (useCharges) {
148             // Convert multipole types to charge types.
149             @SuppressWarnings("unchecked")
150             Map<String, MultipoleType> multipoleTypes = (Map<String, MultipoleType>) (Map<?, ?>)
151                     forceField.getTypes(ForceField.ForceFieldType.MULTIPOLE);
152             for (String key : multipoleTypes.keySet()) {
153                 MultipoleType multipoleType = multipoleTypes.get(key);
154                 ChargeType chargeType = new ChargeType(multipoleType.frameAtomTypes[0], multipoleType.getCharge());
155                 forceField.addForceFieldType(chargeType);
156             }
157             // Clear multipole types.
158             forceField.clearForceFieldType(ForceField.ForceFieldType.MULTIPOLE);
159         }
160 
161         if (zeroVDW) {
162             // Add a zero VDW class with zero parameters.
163             forceField.addForceFieldType(new VDWType(0, 0.0, 0.0, -1.0));
164         }
165 
166         if (forceField != null) {
167             StringBuffer sb = forceField.toStringBuffer();
168             if (tinker) {
169                 logger.info(sb.toString().replace('\\', ' '));
170             } else {
171                 logger.info(sb.toString());
172             }
173         }
174 
175         return this;
176     }
177 }