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