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.ui;
39  
40  import static java.lang.String.format;
41  
42  import ffx.potential.ForceFieldEnergy;
43  import ffx.potential.MolecularAssembly;
44  import ffx.potential.Utilities;
45  import ffx.potential.parsers.FileOpener;
46  import ffx.potential.parsers.PDBFilter;
47  import ffx.potential.parsers.SystemFilter;
48  import ffx.utilities.Resources;
49  
50  import java.awt.Cursor;
51  import java.util.List;
52  import java.util.logging.Level;
53  import java.util.logging.Logger;
54  
55  import org.apache.commons.configuration2.CompositeConfiguration;
56  import org.apache.commons.io.FilenameUtils;
57  
58  /**
59   * The UIFileOpener class opens a file into Force Field X using a filter from the
60   * ffx.potential.parsers package. To avoid freezing the FFX GUI, it implements the FileOpener
61   * interface, which extends Runnable.
62   *
63   * @author Michael J. Schnieders
64   */
65  public class UIFileOpener implements FileOpener {
66  
67    private static final Logger logger = Logger.getLogger(UIFileOpener.class.getName());
68    private static final long KB = 1024;
69    SystemFilter systemFilter;
70    MainPanel mainPanel;
71    private boolean timer = true;
72    private boolean gc = true;
73    private long time;
74    private int nThreads = -1;
75  
76    /**
77     * Constructor for UIFileOpener.
78     *
79     * @param systemFilter a {@link ffx.potential.parsers.SystemFilter} object.
80     * @param mainPanel    a {@link ffx.ui.MainPanel} object.
81     */
82    UIFileOpener(SystemFilter systemFilter, MainPanel mainPanel) {
83      this.systemFilter = systemFilter;
84      this.mainPanel = mainPanel;
85      if (System.getProperty("ffx.timer", "false").equalsIgnoreCase("true")) {
86        timer = true;
87        if (System.getProperty("ffx.timer.gc", "false").equalsIgnoreCase("true")) {
88          gc = true;
89        }
90      }
91    }
92  
93    /**
94     * Returns all MolecularAssemblys in the user interface hierarchy.
95     *
96     * @return All MolecularAssembly objects stored by the hierarchy.
97     * @throws NullPointerException If hierarchy has a null or empty list of assemblies.
98     */
99    @Override
100   public MolecularAssembly[] getAllAssemblies() throws NullPointerException {
101     MolecularAssembly[] assemblies = mainPanel.getHierarchy().getSystems();
102     if (assemblies == null) {
103       throw new NullPointerException(" FFX hierarchy has a null list of assemblies.");
104     } else if (assemblies.length == 0) {
105       throw new NullPointerException(" FFX hierarchy has an empty list of assemblies.");
106     } else {
107       return assemblies;
108     }
109   }
110 
111   /**
112    * Returns the properties of all FFXSystems in the hierarchy.
113    *
114    * @return Properties for all systems.
115    */
116   @Override
117   public CompositeConfiguration[] getAllProperties() {
118     FFXSystem[] allSystems = mainPanel.getHierarchy().getSystems();
119     int numSystems = allSystems.length;
120     CompositeConfiguration[] allProperties = new CompositeConfiguration[numSystems];
121     for (int i = 0; i < numSystems; i++) {
122       allProperties[i] = allSystems[i].getProperties();
123     }
124     return allProperties;
125   }
126 
127   /**
128    * Returns the active MolecularAssembly from the user interface hierarchy.
129    *
130    * @return Active MolecularAssembly
131    * @throws NullPointerException If no active MolecularAssembly
132    */
133   @Override
134   public MolecularAssembly getAssembly() throws NullPointerException {
135     MolecularAssembly assembly = mainPanel.getHierarchy().getActive();
136     if (assembly == null) {
137       throw new NullPointerException(" FFX hierarchy does not have an active assembly.");
138     }
139     return assembly;
140   }
141 
142   /**
143    * Returns the properties of the hierarchy's active FFXSystem.
144    *
145    * @return Active properties
146    */
147   @Override
148   public CompositeConfiguration getProperties() {
149     return mainPanel.getHierarchy().getActive().getProperties();
150   }
151 
152   /**
153    * {@inheritDoc}
154    */
155   @Override
156   public void run() {
157     if (mainPanel != null && systemFilter != null) {
158       open();
159     }
160   }
161 
162   void setNThreads(int nThreads) {
163     this.nThreads = nThreads;
164   }
165 
166   private void open() {
167     if (timer) {
168       startTimer();
169     }
170     FFXSystem ffxSystem = null;
171     // Continue if the file was read in successfully.
172     if (systemFilter.readFile()) {
173       ffxSystem = (FFXSystem) systemFilter.getActiveMolecularSystem();
174       if (!(systemFilter instanceof PDBFilter)) {
175         Utilities.biochemistry(ffxSystem, systemFilter.getAtomList());
176       }
177       systemFilter.applyAtomProperties();
178       // Add the system to the multiscale hierarchy.
179       mainPanel.getHierarchy().addSystemNode(ffxSystem);
180       ForceFieldEnergy energy;
181       if (nThreads > 0) {
182         energy = ForceFieldEnergy.energyFactory(ffxSystem, nThreads);
183       } else {
184         energy = ForceFieldEnergy.energyFactory(ffxSystem);
185       }
186       ffxSystem.setPotential(energy);
187       mainPanel.getHierarchy().setActive(ffxSystem);
188 
189       // Check if there are alternate conformers
190       if (systemFilter instanceof PDBFilter) {
191         PDBFilter pdbFilter = (PDBFilter) systemFilter;
192         List<Character> altLocs = pdbFilter.getAltLocs();
193         if (altLocs.size() > 1 || altLocs.get(0) != ' ') {
194           StringBuilder altLocString = new StringBuilder("\n Alternate locations found [ ");
195           for (Character c : altLocs) {
196             // Do not report the root conformer.
197             if (c == ' ') {
198               continue;
199             }
200             altLocString.append(format("(%s) ", c));
201           }
202           altLocString.append("]\n");
203           logger.info(altLocString.toString());
204         }
205 
206         // Alternate conformers may have different chemistry,
207         // so they each need to be their own FFX system.
208         for (Character c : altLocs) {
209           if (c.equals(' ') || c.equals('A')) {
210             continue;
211           }
212           FFXSystem newSystem = new FFXSystem(
213               ffxSystem.getFile(), "Alternate Location " + c, ffxSystem.getProperties());
214           newSystem.setForceField(ffxSystem.getForceField());
215           pdbFilter.setAltID(newSystem, c);
216           pdbFilter.clearSegIDs();
217           if (pdbFilter.readFile()) {
218             pdbFilter.applyAtomProperties();
219             String fileName = ffxSystem.getFile().getAbsolutePath();
220             newSystem.setName(FilenameUtils.getBaseName(fileName) + " " + c);
221             mainPanel.getHierarchy().addSystemNode(newSystem);
222             if (nThreads > 0) {
223               energy = ForceFieldEnergy.energyFactory(newSystem, nThreads);
224             } else {
225               energy = ForceFieldEnergy.energyFactory(newSystem);
226             }
227             newSystem.setPotential(energy);
228           }
229         }
230       }
231     } else {
232       logger.warning(String.format(" Failed to read file %s", systemFilter.getFile().getName()));
233     }
234     mainPanel.setCursor(Cursor.getDefaultCursor());
235     if (timer) {
236       stopTimer(ffxSystem);
237     }
238   }
239 
240   /**
241    * Rather verbose output for timed File Operations makes it easy to grep log files for specific
242    * information.
243    */
244   private void startTimer() {
245     Runtime runtime = Runtime.getRuntime();
246     if (gc) {
247       runtime.gc();
248     }
249     time -= System.nanoTime();
250   }
251 
252   private void stopTimer(FFXSystem ffxSystem) {
253     time += System.nanoTime();
254     logger.log(
255         Level.INFO,
256         "\n Opened {0} in {1} sec.",
257         new Object[]{ffxSystem.toString(), time * 1.0e-9});
258     Resources.logResources();
259   }
260 }