20package denoptim.integration.tinker;
22import java.io.BufferedReader;
24import java.io.FileReader;
25import java.io.FileWriter;
26import java.io.IOException;
27import java.util.ArrayList;
28import java.util.HashMap;
31import org.apache.commons.io.input.ReversedLinesFileReader;
33import denoptim.exception.DENOPTIMException;
34import denoptim.molecularmodeling.MMBuilderUtils;
35import denoptim.molecularmodeling.zmatrix.ZMatrix;
36import denoptim.molecularmodeling.zmatrix.ZMatrixAtom;
37import denoptim.utils.GeneralUtils;
48 private static final String
NL = System.getProperty(
"line.separator");
49 private static boolean debug =
false;
104 BufferedReader br =
null;
112 br =
new BufferedReader(
new FileReader(filename));
113 line = br.readLine().trim();
116 String[] arr = line.split(
" +");
117 natom = Integer.parseInt(arr[0]);
120 String msg =
"Invalid number of atoms in " + filename;
126 arr = line.split(
" +", 2);
130 for (
int i = 0; i < natom; i++)
132 line = br.readLine();
138 arr = line.trim().split(
" +");
139 if (arr ==
null || arr.length < 3)
141 String msg =
"Check atom " + (i + 1) +
" in " + filename;
145 int id = Integer.parseInt(arr[0])-1;
146 String symbol = arr[1];
147 String type = arr[2];
151 Double bondLength =
null;
152 Double angleValue =
null;
153 Double angle2Value =
null;
154 Integer chiralFlag =
null;
159 bndRef = zmat.
getAtom(Integer.parseInt(arr[3])-1);
160 bondLength = Double.parseDouble(arr[4]);
165 angRef = zmat.
getAtom(Integer.parseInt(arr[5])-1);
166 angleValue = Double.parseDouble(arr[6]);
169 if (arr.length >= 10)
171 ang2Ref = zmat.
getAtom(Integer.parseInt(arr[7])-1);
172 angle2Value = Double.parseDouble(arr[8]);
173 chiralFlag = Integer.parseInt(arr[9]);
176 bndRef, angRef, ang2Ref,
177 bondLength, angleValue, angle2Value,
188 line = br.readLine();
190 if (line.trim().equalsIgnoreCase(
""))
194 boolean blank =
false;
195 while (br.ready() && !blank)
197 line = br.readLine();
198 if (line.trim().equalsIgnoreCase(
""))
204 arr = line.trim().split(
" +");
207 String msg =
"Check Bond Pair to Remove: '"
208 + line +
"' in " + filename;
211 zmat.
addBond(Integer.parseInt(arr[0])-1,
212 Integer.parseInt(arr[1])-1);
218 line = br.readLine();
219 arr = line.trim().split(
" +");
222 String msg =
"Check Bond Pair to Remove: '"
223 + line +
"' in " + filename;
226 zmat.
delBond(Integer.parseInt(arr[0])-1,
227 Integer.parseInt(arr[1])-1);
232 catch (NumberFormatException | IOException nfe)
245 catch (IOException ioe)
267 List<double[]> coords =
new ArrayList<>();
269 BufferedReader br =
null;
276 br =
new BufferedReader(
new FileReader(filename));
277 line = br.readLine().trim();
280 String[] arr = line.split(
"\\s+");
281 natom = Integer.parseInt(arr[0]);
284 String msg =
"Invalid number of atoms in " + filename;
288 while ((line = br.readLine()) !=
null)
290 if (line.trim().length() == 0)
295 arr = line.trim().split(
"\\s+");
300 double[] f =
new double[3];
301 f[0] = Double.parseDouble(arr[2]);
302 f[1] = Double.parseDouble(arr[3]);
303 f[2] = Double.parseDouble(arr[4]);
309 catch (NumberFormatException | IOException nfe)
322 catch (IOException ioe)
328 if (coords.size() != natom)
351 FileWriter fw =
null;
354 fw =
new FileWriter(
new File(filename));
355 int numatoms = zmat.getAtoms().size();
357 String header = String.format(
"%6d %s%n", numatoms, zmat.getId());
363 for (
int i = 0; i < numatoms; i++)
368 line = String.format(
"%6d %-3s%6s%n",
379 line = String.format(
"%6d %-3s%6s%6d%10.5f%n",
382 zmat.getBondRefAtomIndex(i)+1,
383 zmat.getBondLength(i));
391 line = String.format(
"%6d %-3s%6s%6d%10.5f%6d%10.4f%n",
394 zmat.getBondRefAtomIndex(i)+1,
395 zmat.getBondLength(i),
396 zmat.getAngleRefAtomIndex(i)+1,
397 zmat.getAngleValue(i));
403 line = String.format(
"%6d %-3s%6s%6d%10.5f%6d%10.4f%6d%10.4f%6d%n",
406 zmat.getBondRefAtomIndex(i)+1,
407 zmat.getBondLength(i),
408 zmat.getAngleRefAtomIndex(i)+1,
409 zmat.getAngleValue(i),
410 zmat.getAngle2RefAtomIndex(i)+1,
411 zmat.getAngle2Value(i),
412 zmat.getChiralFlag(i));
421 List<int[]> zadd = zmat.getBondsToAdd();
422 List<int[]> zdel = zmat.getBondsToDel();
424 if (zadd.size() > 0 || zdel.size() > 0)
429 for (
int i = 0; i < zadd.size(); i++)
431 int[] z = zadd.get(i);
432 line = String.format(
"%6d%6d%n", z[0]+1, z[1]+1);
442 for (
int i = 0; i < zdel.size(); i++)
444 int[] z = zdel.get(i);
445 line = String.format(
"%6d%6d%n", z[0]+1, z[1]+1);
453 catch (IOException ioe)
466 catch (IOException ioe)
485 List<Double> energies =
new ArrayList<>();
487 BufferedReader br =
null;
491 br =
new BufferedReader(
new FileReader(filename));
493 while ((line = br.readLine()) !=
null)
496 if (line.contains(
"Final Function Value and Deformation"))
498 String str = line.substring(38);
500 String[] arr = str.split(
"\\s+");
501 energies.add(Double.parseDouble(arr[1]));
505 catch (NumberFormatException | IOException nfe)
518 catch (IOException ioe)
524 if (energies.isEmpty())
526 String msg =
"No data found in file: " + filename;
542 List<String> initPars, List<String> restPars)
545 BufferedReader br =
null;
551 br =
new BufferedReader(
new FileReader(filename));
552 while ((line = br.readLine()) !=
null)
554 if (line.trim().length() == 0)
556 if (line.contains(
"INIT"))
561 if (line.contains(
"REST"))
567 initPars.add(line.trim());
569 restPars.add(line.trim());
572 catch (IOException nfe)
574 String msg =
"File '" + filename +
"' not found.";
586 catch (IOException ioe)
592 if (initPars.isEmpty())
594 String msg =
"No data found in file: " + filename;
597 if (restPars.isEmpty())
599 String msg =
"No data found in file: " + filename;
617 HashMap<String, Integer> atomTypesMap =
new HashMap<>();
619 BufferedReader br =
null;
624 br =
new BufferedReader(
new FileReader(filename));
625 while ((line = br.readLine()) !=
null)
629 if (!line.startsWith(
"atom"))
640 String[] dq = line.split(
"\"");
642 String[] str1 = fp.split(
"\\s+");
645 int atomType = Integer.parseInt(str1[1]);
646 String symbol = str1[2];
657 atomTypesMap.put(symbol,atomType);
661 String msg =
"Format of Tinker's atom type definition not "
662 +
"recognized. " +
NL +
"Details: " +
NL
668 catch (NumberFormatException | IOException nfe)
681 catch (IOException ioe)
687 if (atomTypesMap.isEmpty())
689 String msg =
"No data found in file: " + filename;
715 File output =
new File(outputPathName);
716 if (output.exists() && output.canRead())
721 String errMsg =
"TINKER ERROR: ";
722 ReversedLinesFileReader fr =
null;
725 fr =
new ReversedLinesFileReader(
new File(
729 for (
int i=0; i<100; i++)
731 String line = fr.readLine();
735 if (line.trim().isEmpty())
740 errMsg = errMsg +
NL + line;
743 }
catch (IOException e)
774 String xyzfile = workDir + System.getProperty(
"file.separator") + fname
Exceptions resulting from a failure of Tinker.
Toolbox of utilities for Tinker style molecular representation.
static HashMap< String, Integer > readTinkerAtomTypes(String filename)
Read the Tinker atom mapping from Tinker Force Field.
static ZMatrix readTinkerINT(String filename)
Reads a Tinker INT file.
static String getNameLastCycleFile(String workDir, String fname, String tinkerLog, String pattern)
Identifies how many iteration Tinker has done by looking into the log file, searching for a given pat...
static void writeTinkerINT(String filename, ZMatrix zmat)
Write Tinker INT file.
static List< double[]> readTinkerXYZ(String filename)
Read the tinker XYZ coordinate representation.
static List< Double > readPSSROTOutput(String filename)
Read the PSSROT output file.
static void ensureOutputExistsOrRelayError(String outputPathName, String logPathName, String taskName)
Check for the existence of an output file for a Tinker job and, if the output file is not found,...
static void readPSSROTParams(String filename, List< String > initPars, List< String > restPars)
Read the parameter settings to be used by PSSROT.
Utilities for molecular models builder.
static int countLinesWKeywordInFile(String filename, String keyword)
Count the number of lines starting with a keyword.
Representation of an atom in the ZMatrix.
String getSymbol()
Get the symbol of the atom.
int getId()
Get the id of the atom.
String getType()
Get the type of the atom.
Representation of an atom container's geometry with internal coordinates.
void delBond(int a1, int a2)
Delete the bond between the two atoms at the given indices.
void setId(String id)
Set the id of the ZMatrix.
void addBond(int a1, int a2)
Add a bond between the two atoms at the given indices.
ZMatrixAtom getAtom(int index)
Get the atom at the given index.
void addAtom(ZMatrixAtom atom)
Add an atom to the ZMatrix.
static String getPaddedString(int count, int number)
returns the padded string with zeroes placed to the left of 'number' up to reach the desired number o...