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.parsers;
39  
40  import ffx.potential.bonded.Residue;
41  
42  import java.io.BufferedReader;
43  import java.io.BufferedWriter;
44  import java.io.File;
45  import java.io.FileReader;
46  import java.io.FileWriter;
47  import java.io.IOException;
48  import java.util.List;
49  import java.util.logging.Level;
50  import java.util.logging.Logger;
51  
52  import static java.lang.Double.parseDouble;
53  import static java.lang.String.format;
54  
55  /**
56   * The ESVFilter class parses Extended System Restart (*.ESV) files.
57   *
58   * @author Andrew Thiel
59   * @since 1.0
60   */
61  public class ESVFilter {
62  
63    private static final Logger logger = Logger.getLogger(ESVFilter.class.getName());
64    private final String label;
65  
66    /**
67     * Constructor for ESVFilter.
68     *
69     * @param label a Label for this restart file.
70     */
71    public ESVFilter(String label) {
72      this.label = label;
73    }
74  
75    public String getLambdaHistogram(List<Residue> titratingResidueList, final int[][][] esvHistogram,
76        double pH) {
77      int nTitr = titratingResidueList.size();
78  
79      StringBuilder tautomerHeader = new StringBuilder("        X");
80      for (int k = 0; k < 10; k++) {
81        tautomerHeader.append(String.format(" %1$10s", "[" + k / 10.0 + "-" + (k + 1) / 10.0 + "]"));
82      }
83      tautomerHeader.append("\n λ\n");
84  
85      StringBuilder[] histogram = new StringBuilder[nTitr];
86      for (int i = 0; i < nTitr; i++) {
87        StringBuilder hist = new StringBuilder();
88        hist.append(format(" ESV: %s (%d) pH: %4.2f\n", titratingResidueList.get(i), i, pH));
89        hist.append(tautomerHeader);
90        for (int j = 0; j < 10; j++) {
91          hist.append(" [").append(j / 10.0).append("-").append((j + 1) / 10.0).append("]");
92          for (int k = 0; k < 10; k++) {
93            hist.append(String.format("%1$10s", esvHistogram[i][j][k]));
94          }
95          hist.append("\n");
96        }
97        histogram[i] = hist.append("\n");
98      }
99  
100     StringBuilder histograms = new StringBuilder();
101     for (int i = 0; i < nTitr; i++) {
102       histograms.append(histogram[i]);
103     }
104     return String.valueOf(histograms);
105   }
106 
107   /**
108    * readDYN
109    *
110    * @param esvFile a {@link File} object.
111    * @param x an array of double.
112    * @param v an array of double.
113    * @param a an array of double.
114    * @return a boolean.
115    */
116   public boolean readESV(File esvFile, double[] x, double[] v, double[] a, final int[][][] esvHist) {
117     if (!esvFile.exists() || !esvFile.canRead()) {
118       return false;
119     }
120     try (BufferedReader br = new BufferedReader(new FileReader(esvFile))) {
121       br.readLine();
122       String data = br.readLine().trim();
123       String[] tokens = data.split(" +");
124       if (tokens.length == 0) {
125         return false;
126       }
127       int numESVs = Integer.parseInt(tokens[0]);
128 
129       // Atomic coordinates
130       br.readLine();
131       for (int i = 0; i < numESVs; i++) {
132         data = br.readLine().trim();
133         tokens = data.split(" +");
134         if (tokens.length != 1) {
135           return false;
136         }
137         x[i] = parseDouble(tokens[0]);
138       }
139 
140       // Velocities
141       br.readLine();
142       for (int i = 0; i < numESVs; i++) {
143         data = br.readLine().trim();
144         tokens = data.split(" +");
145         if (tokens.length != 1) {
146           return false;
147         }
148         v[i] = parseDouble(tokens[0]);
149       }
150 
151       // Accelerations
152       br.readLine();
153       for (int i = 0; i < numESVs; i++) {
154         data = br.readLine().trim();
155         tokens = data.split(" +");
156         if (tokens.length != 1) {
157           return false;
158         }
159         a[i] = parseDouble(tokens[0]);
160       }
161 
162       // Histograms
163       for (int i = 0; i < esvHist.length; i++) {
164         for (int j = 0; j < 4; j++) {
165           br.readLine();
166         }
167         for (int j = 0; j < esvHist[i].length; j++) {
168           data = br.readLine().trim();
169           tokens = data.split(" +");
170           for (int k = 0; k < esvHist[i][j].length; k++) {
171             esvHist[i][j][k] = Integer.parseInt(tokens[k + 1]);
172           }
173         }
174       }
175     } catch (Exception e) {
176       String message = "Exception reading ESV restart file: " + esvFile;
177       logger.log(Level.WARNING, message, e);
178     }
179     return true;
180   }
181 
182   /**
183    * Write the extended system variables to a file.
184    *
185    * @param dynFile The file to write.
186    * @param x The extended system variables.
187    * @param v The extended system variable velocities.
188    * @param a The extended system variable accelerations.
189    * @param titrResList The List of titrating residues.
190    * @param esvHist The ESV histogram.
191    * @param pH The current pH.
192    * @return True if the file was written successfully.
193    */
194   public boolean writeESV(File dynFile, double[] x, double[] v, double[] a,
195       List<Residue> titrResList, final int[][][] esvHist, double pH) {
196     try (FileWriter fw = new FileWriter(dynFile); BufferedWriter bw = new BufferedWriter(fw)) {
197       bw.write(" Number of ESVs and Title :\n");
198       int numberOfAtoms = x.length;
199       String output = format("%7d  %s\n", numberOfAtoms, label);
200       bw.write(output);
201       bw.write(" Current Theta Positions :\n");
202       for (int i = 0; i < numberOfAtoms; i++) {
203         bw.write(format("%26.16E\n", x[i]));
204       }
205       bw.write(" Current Atomic Velocities :\n");
206       for (int i = 0; i < numberOfAtoms; i++) {
207         bw.write(format("%26.16E\n", v[i]));
208       }
209       bw.write(" Current Atomic Accelerations :\n");
210       for (int i = 0; i < numberOfAtoms; i++) {
211         bw.write(format("%26.16E\n", a[i]));
212       }
213       bw.write(" Current Lambda Histogram(s) :\n");
214       bw.write(this.getLambdaHistogram(titrResList, esvHist, pH));
215     } catch (IOException e) {
216       String message = " Exception writing dynamic restart file " + dynFile;
217       logger.log(Level.SEVERE, message, e);
218       return false;
219     }
220     return true;
221   }
222 }