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.algorithms.commands.test;
39  
40  import ffx.algorithms.cli.AlgorithmsCommand;
41  import ffx.algorithms.misc.GenerateRotamers;
42  import ffx.potential.MolecularAssembly;
43  import ffx.potential.bonded.Polymer;
44  import ffx.potential.bonded.Residue;
45  import ffx.potential.bonded.RotamerLibrary;
46  import ffx.utilities.FFXBinding;
47  import org.apache.commons.io.FilenameUtils;
48  import picocli.CommandLine.Command;
49  import picocli.CommandLine.Option;
50  import picocli.CommandLine.Parameters;
51  
52  import java.io.File;
53  import java.util.List;
54  
55  /**
56   * The ApplyTorsions script is used to apply a custom set of torsions to a side chain.
57   * <br>
58   * Usage:
59   * <br>
60   * ffxc test.ApplyTorsions [options] &lt;filename&gt;
61   *
62   * @author Jacob Litman
63   * @author Michael Schnieders
64   */
65  @Command(description = " Apply a set of torsions to a system.", name = "test.ApplyTorsions")
66  public class ApplyTorsions extends AlgorithmsCommand {
67  
68    /**
69     * -c or --chain selects the chain name to use.
70     */
71    @Option(names = {"-c", "--chain"}, paramLabel = " ",
72        description = "Single character chain name (default is \" \").")
73    private String chain = " ";
74    /**
75     * -r or --resID selects the residue to apply torsions to.
76     */
77    @Option(names = {"-r", "--resID"}, paramLabel = "1",
78        description = "Residue number.")
79    private int resID = 1;
80    /**
81     * -t or --torsionSets is a variable-length, colon-delimited set of comma-separated torsion sets; e.g. colons separate rotamers, commas individual values. Should not be left as final argument, as then it attempts to include the filename.
82     */
83    @Option(names = {"-t", "--torsionSets"}, paramLabel = "0,0:180,0", arity = "1..*", split = ":",
84        description = "Torsion sets to apply (torsions comma-separated, sets colon-separated).")
85    private String[] torSets = null;
86    /**
87     * -n or --nChi is the number of torsions available to this side chain; unspecified torsions are filled with 0.
88     */
89    @Option(names = {"-n", "--nChi"}, paramLabel = "1",
90        description = "Number of torsions (unspecified torsions are filled with 0).")
91    private int nChi = 1;
92    /**
93     * -v or --videoFile is the name of the file to print torsion snapshots to; defaults to filename_rots.pdb
94     */
95    @Option(names = {"-v", "--videoFile"},
96        description = "File to print torsion snapshots to.")
97    private String vidFileName = null;
98    /**
99     * One or more filenames.
100    */
101   @Parameters(arity = "1", paramLabel = "files",
102       description = "XYZ or PDB input files.")
103   private List<String> filenames = null;
104 
105   /**
106    * ApplyTorsions Constructor.
107    */
108   public ApplyTorsions() {
109     super();
110   }
111 
112   /**
113    * ApplyTorsions Constructor.
114    * @param binding The Binding to use.
115    */
116   public ApplyTorsions(FFXBinding binding) {
117     super(binding);
118   }
119 
120   /**
121    * ApplyTorsions constructor that sets the command line arguments.
122    * @param args Command line arguments.
123    */
124   public ApplyTorsions(String[] args) {
125     super(args);
126   }
127 
128   /**
129    * {@inheritDoc}
130    */
131   @Override
132   public ApplyTorsions run() {
133 
134     if (!init()) {
135       return this;
136     }
137 
138     String modelFilename;
139     if (filenames != null && !filenames.isEmpty()) {
140       MolecularAssembly[] assemblies = new MolecularAssembly[]{algorithmFunctions.open(filenames.get(0))};
141       activeAssembly = assemblies[0];
142       modelFilename = filenames.get(0);
143     } else if (activeAssembly == null) {
144       logger.info(helpString());
145       return this;
146     } else {
147       modelFilename = activeAssembly.getFile().getAbsolutePath();
148     }
149 
150     String newName = FilenameUtils.getBaseName(modelFilename);
151 
152     String videoFile;
153     if (vidFileName != null) {
154       videoFile = vidFileName;
155     } else {
156       videoFile = newName + "_rots.pdb";
157     }
158 
159     File outFile = new File(newName + ".rotout.tmp");
160     outFile.deleteOnExit();
161 
162     logger.info("\n Saving torsions for residue number " + resID + " of chain " + chain + ".");
163 
164     RotamerLibrary.initializeDefaultAtomicCoordinates(activeAssembly.getChains());
165     Polymer polymer = activeAssembly.getChain(chain);
166     if (polymer == null) {
167       logger.info(" Polymer " + chain + " does not exist.");
168       return this;
169     }
170     Residue residue = polymer.getResidue(resID);
171     if (residue == null) {
172       logger.info(" Residue " + resID + " does not exist.");
173       return this;
174     }
175 
176     GenerateRotamers generateRotamers = new GenerateRotamers(activeAssembly,
177         activeAssembly.getPotentialEnergy(), residue, outFile, nChi, algorithmListener);
178     generateRotamers.setVideo(videoFile);
179 
180     if (torSets != null) {
181       generateRotamers.applyAndSaveTorsions(torSets);
182     } else {
183       logger.info(" No torsion sets specified. Use -t option to apply torsions.");
184     }
185 
186     return this;
187   }
188 }