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.xray.commands;
39  
40  import ffx.algorithms.cli.AlgorithmsCommand;
41  import ffx.potential.MolecularAssembly;
42  import ffx.potential.bonded.Atom;
43  import ffx.potential.bonded.Residue;
44  import ffx.utilities.FFXBinding;
45  import ffx.xray.DiffractionData;
46  import ffx.xray.cli.XrayOptions;
47  import org.apache.commons.configuration2.CompositeConfiguration;
48  import picocli.CommandLine.Command;
49  import picocli.CommandLine.Mixin;
50  import picocli.CommandLine.Parameters;
51  
52  import java.util.List;
53  
54  import static org.apache.commons.io.FilenameUtils.removeExtension;
55  
56  /**
57   * The SaveConformerPDB script saves alternate conformers with proper alt loc labels.
58   * <br>
59   * Usage:
60   * <br>
61   * ffxc xray.SaveConformerPDB [options] &lt;filename&gt;
62   */
63  @Command(description = " Discrete optimization using a many-body expansion and elimination expressions.", name = "xray.SaveConformerPDB")
64  public class SaveConformerPDB extends AlgorithmsCommand {
65  
66    @Mixin
67    private XrayOptions xrayOptions;
68  
69    /**
70     * One or more filenames.
71     */
72    @Parameters(arity = "1..*", paramLabel = "files", description = "PDB and Real Space input files.")
73    private List<String> filenames;
74    private MolecularAssembly[] molecularAssemblies;
75    private DiffractionData diffractionData;
76  
77    /**
78     * SaveConformerPDB constructor.
79     */
80    public SaveConformerPDB() {
81      super();
82    }
83  
84    /**
85     * SaveConformerPDB constructor that sets the command line arguments.
86     * @param args Command line arguments.
87     */
88    public SaveConformerPDB(String[] args) {
89      super(args);
90    }
91  
92    /**
93     * SaveConformerPDB constructor.
94     * @param binding The Binding to use.
95     */
96    public SaveConformerPDB(FFXBinding binding) {
97      super(binding);
98    }
99  
100   @Override
101   public SaveConformerPDB run() {
102     if (!init()) {
103       return this;
104     }
105 
106     xrayOptions.init();
107 
108     String filename;
109     if (filenames != null && !filenames.isEmpty()) {
110       // Each alternate conformer is returned in a separate MolecularAssembly.
111       molecularAssemblies = algorithmFunctions.openAll(filenames.get(0));
112       activeAssembly = molecularAssemblies[0];
113       filename = filenames.get(0);
114     } else if (activeAssembly == null) {
115       logger.info(helpString());
116       return this;
117     } else {
118       molecularAssemblies = new MolecularAssembly[]{activeAssembly};
119       filename = activeAssembly.getFile().getAbsolutePath();
120     }
121 
122     if (molecularAssemblies.length == 1) {
123       logger.info("No alternate conformers");
124       return this;
125     }
126 
127     // Combine script flags (in parseResult) with properties.
128     CompositeConfiguration properties = activeAssembly.getProperties();
129     xrayOptions.setProperties(parseResult, properties);
130 
131     // Set up diffraction data (can be multiple files)
132     diffractionData = xrayOptions.getDiffractionData(filenames, molecularAssemblies, properties);
133     diffractionData.scaleBulkFit();
134     diffractionData.printStats();
135     algorithmFunctions.energy(molecularAssemblies);
136 
137     List<Residue> residuesA = molecularAssemblies[0].getResidueList();
138     List<Residue> residuesB = molecularAssemblies[1].getResidueList();
139 
140     for (int j = 0; j < residuesA.size(); j++) {
141       List<Atom> atomsA = residuesA.get(j).getAtomList();
142       List<Atom> atomsB = residuesB.get(j).getAtomList();
143       for (int i = 0; i < atomsA.size(); i++) {
144         Atom atomA = atomsA.get(i);
145         Atom atomB = atomsB.get(i);
146 
147         if (atomA.getAltLoc() == null || atomA.getAltLoc() == ' ') {
148           atomA.setAltLoc('A');
149         }
150         if (atomB.getAltLoc() == null || atomB.getAltLoc() == ' ') {
151           atomB.setAltLoc('B');
152         }
153       }
154     }
155 
156     logger.info(" ");
157     diffractionData.writeModel(removeExtension(filename) + ".pdb");
158     diffractionData.writeData(removeExtension(filename) + ".mtz");
159 
160     return this;
161   }
162 }