1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package ffx.potential.parsers;
39
40 import java.io.BufferedReader;
41 import java.io.BufferedWriter;
42 import java.io.File;
43 import java.io.FileReader;
44 import java.io.FileWriter;
45 import java.io.IOException;
46 import java.util.ArrayList;
47 import java.util.logging.Logger;
48
49 import static ffx.potential.parsers.SystemFilter.version;
50 import static java.lang.Double.isNaN;
51 import static java.lang.Double.parseDouble;
52 import static java.lang.Integer.parseInt;
53 import static java.lang.String.format;
54
55
56
57
58
59
60
61
62 public class BARFilter {
63
64 private static final Logger logger = Logger.getLogger(XYZFilter.class.getName());
65 private final File barFile;
66
67 private int snaps1;
68 private double temperature1;
69 private double[] e1l1;
70 private double[] e1l2;
71 private double[] volume1;
72
73 private int snaps2;
74 private double temperature2;
75 private double[] e2l1;
76 private double[] e2l2;
77 private double[] volume2;
78
79 private int startingSnap = 0;
80 private int endingSnap = 0;
81 private int count = 0;
82
83
84
85
86
87
88
89 public BARFilter(File barFile) {
90 this.barFile = barFile;
91 }
92
93
94
95
96
97
98
99
100 public BARFilter(File barFile, int startingSnap, int endingSnap) {
101 this.barFile = barFile;
102 this.startingSnap = startingSnap;
103 this.endingSnap = endingSnap;
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 public BARFilter(File xyzFile, double[] e1l1, double[] e1l2, double[] e2l1, double[] e2l2,
120 double[] volume1, double[] volume2, double temperature1, double temperature2) {
121 this.barFile = xyzFile;
122
123 this.temperature1 = temperature1;
124 this.e1l1 = e1l1;
125 this.e1l2 = e1l2;
126 this.volume1 = volume1;
127
128 this.e2l1 = e2l1;
129 this.e2l2 = e2l2;
130 this.volume2 = volume2;
131 this.temperature2 = temperature2;
132 }
133
134
135
136
137
138
139
140 public boolean readFile() {
141 ArrayList<Double> ens1lam1 = new ArrayList<>();
142 ArrayList<Double> ens1lam2 = new ArrayList<>();
143 ArrayList<Double> ens2lam1 = new ArrayList<>();
144 ArrayList<Double> ens2lam2 = new ArrayList<>();
145 ArrayList<Double> vol1 = new ArrayList<>();
146 ArrayList<Double> vol2 = new ArrayList<>();
147
148 int snapshots = 0;
149 int xyzCount = 0;
150 try (BufferedReader br = new BufferedReader(new FileReader(barFile))) {
151 String data;
152 while ((data = br.readLine()) != null) {
153 String[] tokens = data.trim().split(" +");
154 int numTokens = tokens.length;
155 if (data.contains(".xyz") || data.contains(".pdb") || numTokens < 3) {
156 xyzCount++;
157 if (xyzCount == 1) {
158 snaps1 = parseInt(tokens[0]);
159 temperature1 = parseDouble(tokens[1]);
160 } else if (xyzCount == 2) {
161 snaps2 = parseInt(tokens[0]);
162 temperature2 = parseDouble(tokens[1]);
163 }
164 } else if (endingSnap != 0) {
165 count++;
166 snapshots = (endingSnap - startingSnap) + 1;
167 if (count >= startingSnap + 1 && count <= endingSnap + 1) {
168 if (count <= snaps1) {
169 if (numTokens == 4) {
170 vol1.add(parseDouble(tokens[3]));
171 }
172 ens1lam1.add(parseDouble(tokens[1]));
173 ens1lam2.add(parseDouble(tokens[2]));
174 } else {
175 logger.warning(format(" BAR entry of (%3d) is larger than total entries (%3d).", count, snaps1));
176 }
177 } else if (count >= snaps1 + startingSnap + 1 && count <= snaps1 + endingSnap + 1) {
178 if (count <= snaps1 + snaps2 + 1) {
179 if (numTokens == 4) {
180 vol2.add(parseDouble(tokens[3]));
181 }
182 ens2lam1.add(parseDouble(tokens[1]));
183 ens2lam2.add(parseDouble(tokens[2]));
184 } else {
185 logger.warning(format(" BAR entry of (%3d) is larger than total entries (%3d).", count, snaps1 + snaps2));
186 }
187 }
188 } else {
189 count++;
190 if (count <= snaps1) {
191 if (numTokens == 4) {
192 vol1.add(parseDouble(tokens[3]));
193 }
194 ens1lam1.add(parseDouble(tokens[1]));
195 ens1lam2.add(parseDouble(tokens[2]));
196 } else {
197 if (numTokens == 4) {
198 vol2.add(parseDouble(tokens[3]));
199 }
200 ens2lam1.add(parseDouble(tokens[1]));
201 ens2lam2.add(parseDouble(tokens[2]));
202 }
203 }
204 }
205 if (snapshots != 0) {
206 e1l1 = new double[snapshots];
207 e1l2 = new double[snapshots];
208 e2l1 = new double[snapshots];
209 e2l2 = new double[snapshots];
210 volume1 = new double[snapshots];
211 volume2 = new double[snapshots];
212 snaps1 = snapshots;
213 } else {
214 e1l1 = new double[snaps1];
215 e1l2 = new double[snaps1];
216 e2l1 = new double[snaps2];
217 e2l2 = new double[snaps2];
218 volume1 = new double[snaps1];
219 volume2 = new double[snaps2];
220 }
221 for (int i = 0; i < ens1lam1.size(); i++) {
222 e1l1[i] = ens1lam1.get(i);
223 e1l2[i] = ens1lam2.get(i);
224 if (!vol1.isEmpty()) {
225 volume1[i] = vol1.get(i);
226 }
227 }
228 for (int i = 0; i < ens2lam1.size(); i++) {
229 e2l1[i] = ens2lam1.get(i);
230 e2l2[i] = ens2lam2.get(i);
231 if (!vol1.isEmpty()) {
232 volume2[i] = vol2.get(i);
233 }
234 }
235
236 if (data == null) {
237 return false;
238 }
239 } catch (IOException fileNotFoundException) {
240 logger.warning(format(" Exception reading %s:\n %s", barFile, fileNotFoundException));
241 }
242 return true;
243 }
244
245
246
247
248
249
250
251
252 public boolean writeFile(String saveFile, boolean isPBC) {
253 return writeFile(saveFile, isPBC, true);
254 }
255
256
257
258
259
260
261
262
263
264 public boolean writeFile(String saveFile, boolean isPBC, boolean append) {
265 int snaps = e1l1.length;
266 int snaps2 = e2l1.length;
267 String name = barFile.getName();
268
269 File newFile = new File(saveFile);
270 if (!append) {
271 newFile = version(newFile);
272 }
273
274 logger.info(format(" Writing BAR file: %s", newFile));
275 try (FileWriter fw = new FileWriter(newFile,
276 append && newFile.exists()); BufferedWriter bw = new BufferedWriter(fw)) {
277 bw.write(format("%8d %9.3f %s\n", snaps, temperature1, name));
278 for (int i = 0; i < snaps; i++) {
279 if (isNaN(e1l1[i]) || isNaN(e1l2[i])) {
280 continue;
281 }
282 if (isPBC) {
283 bw.write(format("%8d %20.10f %20.10f %20.10f\n", i + 1, e1l1[i], e1l2[i], volume2[i]));
284 } else {
285 bw.write(format("%8d %20.10f %20.10f\n", i + 1, e1l1[i], e1l2[i]));
286 }
287 }
288
289 bw.write(format("%8d %9.3f %s\n", snaps2, temperature1, name));
290 for (int i = 0; i < snaps2; i++) {
291 if (isNaN(e2l1[i]) || isNaN(e2l2[i])) {
292 continue;
293 }
294 if (isPBC) {
295 bw.write(format("%8d %20.10f %20.10f %20.10f\n", i + 1, e2l1[i], e2l2[i], volume2[i]));
296 } else {
297 bw.write(format("%8d %20.10f %20.10f\n", i + 1, e2l1[i], e2l2[i]));
298 }
299 }
300 } catch (IOException e) {
301 logger.warning(format(" Exception writing %s", newFile));
302 return false;
303 }
304 return true;
305 }
306
307
308
309
310
311
312 public double getTemperature1() {
313 return temperature1;
314 }
315
316
317
318
319
320
321 public double getTemperature2() {
322 return temperature2;
323 }
324
325
326
327
328
329
330 public double[] getE1l1() {
331 return e1l1;
332 }
333
334
335
336
337
338
339 public double[] getE1l2() {
340 return e1l2;
341 }
342
343
344
345
346
347
348 public double[] getE2l1() {
349 return e2l1;
350 }
351
352
353
354
355
356
357 public double[] getE2l2() {
358 return e2l2;
359 }
360
361
362
363
364
365
366 public double[] getVolume1() {
367 return volume1;
368 }
369
370
371
372
373
374
375 public double[] getVolume2() {
376 return volume2;
377 }
378
379
380
381
382
383
384 public int getNumberOfSnapshots() {
385 return snaps1;
386 }
387
388
389
390
391
392
393 public File getFile() {
394 return barFile;
395 }
396 }