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.Collections;
29import java.util.HashMap;
32import org.apache.commons.io.input.ReversedLinesFileReader;
33import org.openscience.cdk.interfaces.IAtom;
34import org.openscience.cdk.interfaces.IAtomContainer;
35import org.openscience.cdk.interfaces.IBond;
37import denoptim.constants.DENOPTIMConstants;
38import denoptim.exception.DENOPTIMException;
39import denoptim.molecularmodeling.MMBuilderUtils;
40import denoptim.utils.ConnectedLigand;
41import denoptim.utils.ConnectedLigandComparator;
42import denoptim.utils.GeneralUtils;
43import denoptim.utils.MathUtils;
44import denoptim.utils.MoleculeUtils;
45import denoptim.utils.ObjectPair;
56 private static final String
NL = System.getProperty(
"line.separator");
57 private static boolean debug =
false;
113 BufferedReader br =
null;
121 br =
new BufferedReader(
new FileReader(filename));
122 line = br.readLine().trim();
125 String[] arr = line.split(
" +");
126 natom = Integer.parseInt(arr[0]);
129 String msg =
"Invalid number of atoms in " + filename;
135 arr = line.split(
" +", 2);
142 ArrayList<int[]> zadd =
new ArrayList<>();
143 ArrayList<int[]> zdel =
new ArrayList<>();
145 ArrayList<TinkerAtom> lstAtom =
new ArrayList<>();
147 for (
int i = 0; i < natom; i++)
149 line = br.readLine();
155 arr = line.trim().split(
" +");
156 if (arr ==
null || arr.length < 3)
158 String msg =
"Check atom " + (i + 1) +
" in " + filename;
163 String name = arr[1];
164 int type = Integer.parseInt(arr[2]);
165 double zv[] =
new double[3];
166 int zi[] =
new int[4];
175 zi[0] = Integer.parseInt(arr[3]);
176 zv[0] = Double.parseDouble(arr[4]);
186 zi[1] = Integer.parseInt(arr[5]);
187 zv[1] = Double.parseDouble(arr[6]);
195 if (arr.length >= 10)
197 zi[2] = Integer.parseInt(arr[7]);
198 zv[2] = Double.parseDouble(arr[8]);
199 zi[3] = Integer.parseInt(arr[9]);
213 line = br.readLine();
215 if (line.trim().equalsIgnoreCase(
""))
219 boolean blank =
false;
220 while (br.ready() && !blank)
222 line = br.readLine();
223 if (line.trim().equalsIgnoreCase(
""))
229 arr = line.trim().split(
" +");
232 String msg =
"Check Bond Pair to Remove: "
233 + (zadd.size() + 1) +
" in " + filename;
236 int pair[] =
new int[2];
237 pair[0] = Integer.parseInt(arr[0]);
238 pair[1] = Integer.parseInt(arr[1]);
245 line = br.readLine();
246 arr = line.trim().split(
" +");
249 String msg =
"Check Bond Pair to Remove: "
250 + (zadd.size() + 1) +
" in " + filename;
253 int pair[] =
new int[2];
254 pair[0] = Integer.parseInt(arr[0]);
255 pair[1] = Integer.parseInt(arr[1]);
264 catch (NumberFormatException | IOException nfe)
277 catch (IOException ioe)
299 ArrayList<double[]> coords =
new ArrayList<>();
301 BufferedReader br =
null;
308 br =
new BufferedReader(
new FileReader(filename));
309 line = br.readLine().trim();
312 String[] arr = line.split(
"\\s+");
313 natom = Integer.parseInt(arr[0]);
316 String msg =
"Invalid number of atoms in " + filename;
320 while ((line = br.readLine()) !=
null)
322 if (line.trim().length() == 0)
327 arr = line.trim().split(
"\\s+");
332 double[] f =
new double[3];
333 f[0] = Double.parseDouble(arr[2]);
334 f[1] = Double.parseDouble(arr[3]);
335 f[2] = Double.parseDouble(arr[4]);
341 catch (NumberFormatException | IOException nfe)
354 catch (IOException ioe)
360 if (coords.size() != natom)
383 FileWriter fw =
null;
386 fw =
new FileWriter(
new File(filename));
387 int numatoms = tmol.getAtoms().size();
389 String header = String.format(
"%6d %s%n", numatoms, tmol.getName());
395 for (
int i = 0; i < numatoms; i++)
403 line = String.format(
"%6d %-3s%6d%n",
413 line = String.format(
"%6d %-3s%6d%6d%10.5f%n",
423 line = String.format(
"%6d %-3s%6d%6d%10.5f%6d%10.4f%n",
432 line = String.format(
"%6d %-3s%6d%6d%10.5f%6d%10.4f%6d%10.4f%6d%n",
435 d1[1], d2[1], d1[2], d2[2], d1[3]);
444 ArrayList<int[]> zadd = tmol.getBondAdd();
445 ArrayList<int[]> zdel = tmol.getBondDel();
447 if (zadd.size() > 0 || zdel.size() > 0)
452 for (
int i = 0; i < zadd.size(); i++)
454 int[] z = zadd.get(i);
455 line = String.format(
"%6d%6d%n", z[0], z[1]);
465 for (
int i = 0; i < zdel.size(); i++)
467 int[] z = zdel.get(i);
468 line = String.format(
"%6d%6d%n", z[0], z[1]);
476 catch (IOException ioe)
489 catch (IOException ioe)
508 ArrayList<Double> energies =
new ArrayList<>();
510 BufferedReader br =
null;
514 br =
new BufferedReader(
new FileReader(filename));
516 while ((line = br.readLine()) !=
null)
519 if (line.contains(
"Final Function Value and Deformation"))
521 String str = line.substring(38);
523 String[] arr = str.split(
"\\s+");
524 energies.add(Double.parseDouble(arr[1]));
528 catch (NumberFormatException | IOException nfe)
541 catch (IOException ioe)
547 if (energies.isEmpty())
549 String msg =
"No data found in file: " + filename;
565 ArrayList<String> initPars, ArrayList<String> restPars)
568 BufferedReader br =
null;
574 br =
new BufferedReader(
new FileReader(filename));
575 while ((line = br.readLine()) !=
null)
577 if (line.trim().length() == 0)
579 if (line.contains(
"INIT"))
584 if (line.contains(
"REST"))
590 initPars.add(line.trim());
592 restPars.add(line.trim());
595 catch (IOException nfe)
597 String msg =
"File '" + filename +
"' not found.";
609 catch (IOException ioe)
615 if (initPars.isEmpty())
617 String msg =
"No data found in file: " + filename;
620 if (restPars.isEmpty())
622 String msg =
"No data found in file: " + filename;
640 HashMap<String, Integer> atomTypesMap =
new HashMap<>();
642 BufferedReader br =
null;
647 br =
new BufferedReader(
new FileReader(filename));
648 while ((line = br.readLine()) !=
null)
652 if (!line.startsWith(
"atom"))
663 String[] dq = line.split(
"\"");
665 String[] str1 = fp.split(
"\\s+");
668 int atomType = Integer.parseInt(str1[1]);
669 String symbol = str1[2];
680 atomTypesMap.put(symbol,atomType);
684 String msg =
"Format of Tinker's atom type definition not "
685 +
"recognized. " +
NL +
"Details: " +
NL
691 catch (NumberFormatException | IOException nfe)
704 catch (IOException ioe)
710 if (atomTypesMap.isEmpty())
712 String msg =
"No data found in file: " + filename;
734 String doneBnd =
"visitedBond";
735 for (
int i=0; i<mol.getAtomCount(); i++)
745 int[] nbrs =
new int[] {0, 0, 0, 0};
747 IAtom atmI = mol.getAtom(i);
750 System.err.println(
"Atom to IC: "
758 d = atmI.getPoint3d().distance(mol.getAtom(i2).getPoint3d());
759 mol.getBond(atmI,mol.getAtom(i2)).setProperty(doneBnd,
"T");
762 System.err.println(
" i2 = " + i2 +
" d: " + d);
772 mol.getAtom(i2).getPoint3d(),
773 mol.getAtom(i3).getPoint3d());
776 System.err.println(
" i3 = "+ i3 +
" a: " + a);
791 mol.getAtom(i2).getPoint3d(),
792 mol.getAtom(i4).getPoint3d());
795 mol.getAtom(i2).getPoint3d(),
796 mol.getAtom(i3).getPoint3d(),
797 mol.getAtom(i4).getPoint3d());
806 mol.getAtom(i2).getPoint3d(),
807 mol.getAtom(i3).getPoint3d(),
808 mol.getAtom(i4).getPoint3d());
812 System.err.println(
" i4 = "+ i4 +
" t: " + t +
" " + i5);
824 new double[] {atmI.getPoint3d().x,
826 atmI.getPoint3d().z},
828 new double[] {d,a,t});
835 System.err.println(
" TinkerAtom: "+ta.
toString());
842 System.err.println(
"TinkerMolecule: ");
848 for (IBond b : mol.bonds())
850 if (b.getProperty(doneBnd) ==
null)
852 tm.
addBond(mol.indexOf(b.getAtom(0))+1,
853 mol.indexOf(b.getAtom(1))+1);
870 List<ConnectedLigand> candidates =
new ArrayList<ConnectedLigand>();
871 for (IAtom nbr : mol.getConnectedAtomsList(mol.getAtom(i1)))
873 if (mol.indexOf(nbr) < i1)
880 int i2 = mol.indexOf(candidates.get(0).getAtom());
888 List<ConnectedLigand> candidates =
new ArrayList<ConnectedLigand>();
889 for (IAtom nbr : mol.getConnectedAtomsList(mol.getAtom(i2)))
891 if ((mol.indexOf(nbr) < i1) && (nbr != mol.getAtom(i1)))
898 int i3 = mol.indexOf(candidates.get(0).getAtom());
908 IAtom atmI1 = mol.getAtom(i1);
909 IAtom atmI2 = mol.getAtom(i2);
910 IAtom atmI3 = mol.getAtom(i3);
911 List<ConnectedLigand> candidates =
new ArrayList<ConnectedLigand>();
912 if (tm.isTorsionUsed(i2+1, i3+1) ||
916 for (IAtom nbr : mol.getConnectedAtomsList(atmI2))
920 System.err.println(
" Eval. 3rd (ANG): " +
922 + mol.indexOf(nbr) +
" "
923 + (mol.indexOf(nbr) < i1) +
" "
924 + (nbr != atmI1) +
" "
927 if ((mol.indexOf(nbr) < i1) && (nbr != atmI1) &&
942 System.err.println(
" ...but collinear with "
944 +
" (i4-i2-i3: " + dbcAng
954 for (IAtom nbr : mol.getConnectedAtomsList(atmI3))
958 System.err.println(
" Eval. 3rd (TOR): "
960 + mol.indexOf(nbr) +
" "
961 + (mol.indexOf(nbr) < i1) +
" "
962 + (nbr != atmI1) +
" "
965 if ((mol.indexOf(nbr) < i1) && (nbr != atmI1) &&
974 if (candidates.size() == 0)
976 String msg =
"Unable to make internal coordinates. Please, "
977 +
"consider the use of dummy atoms in proximity "
978 +
"of atom " + tm.getAtom(i1+1);
981 int i4 = mol.indexOf(candidates.get(0).getAtom());
993 for (IAtom nbr : mol.getConnectedAtomsList(a))
995 if (mol.indexOf(nbr) < i)
1014 ArrayList<TinkerAtom> lstAtoms = tmol.getAtoms();
1015 int numberOfAtoms = lstAtoms.size();
1016 for (
int i = 0; i < numberOfAtoms; i++)
1021 if (!tMap.containsKey(st))
1023 String msg =
"Unable to assign atom type to atom '" + st +
"'. ";
1026 msg = msg +
"Unusual dummy atom symbols get atom symbol "
1027 +
"'R'. Please, add atom type 'R' in your atom type map.";
1031 Integer val = tMap.get(st);
1036 System.err.println(
"Set parameter for " + st +
" " + val);
1040 String msg =
"No valid Tinker atom type assigned for atom "
1066 File output =
new File(outputPathName);
1067 if (output.exists() && output.canRead())
1072 String errMsg =
"TINKER ERROR: ";
1073 ReversedLinesFileReader fr =
null;
1076 fr =
new ReversedLinesFileReader(
new File(
1077 logPathName),
null);
1080 for (
int i=0; i<100; i++)
1082 String line = fr.readLine();
1086 if (line.trim().isEmpty())
1091 errMsg = errMsg +
NL + line;
1094 }
catch (IOException e)
1125 String xyzfile = workDir + System.getProperty(
"file.separator") + fname
General set of constants used in DENOPTIM.
static final String ATMPROPVERTEXID
String tag of Atom property used to store the unique ID of the Vertex corresponding to the molecular ...
Based on the code from ffx.kenai.com Michael J.
int getXYZIndex()
Gets the XYZ Index.
void setVertexId(long vidx)
int[] getAtomNeighbours()
void setAtomType(int ffAtomType)
Exceptions resulting from a failure of Tinker.
void addBond(int a1, int a2)
Add one bond by appending a pair of indeces into the z-add section.
void addAtom(TinkerAtom ta)
Add one atom to this molecule.
void setAtoms(ArrayList< TinkerAtom > lstAtoms)
void setBondPairs(ArrayList< int[]> zdel, ArrayList< int[]> zadd)
void setName(String molName)
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 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 int getSecondRefAtomId(int i1, int i2, IAtomContainer mol)
static ObjectPair getThirdRefAtomId(int i1, int i2, int i3, IAtomContainer mol, TinkerMolecule tm)
static int countPredefinedNeighbours(int i, IAtom a, IAtomContainer mol)
static void writeIC(String filename, TinkerMolecule tmol)
Write Tinker INT file.
static ArrayList< Double > readPSSROTOutput(String filename)
Read the PSSROT output file.
static int getFirstRefAtomId(int i1, IAtomContainer mol)
static ArrayList< double[]> readTinkerXYZ(String filename)
Read the tinker XYZ coordinate representation.
static void readPSSROTParams(String filename, ArrayList< String > initPars, ArrayList< String > restPars)
Read the parameter settings to be used by PSSROT.
static TinkerMolecule getICFromIAC(IAtomContainer mol, HashMap< String, Integer > tMap)
Convert IAtomContainer to TinkerMolecule.
static TinkerMolecule readTinkerIC(String filename)
Reads a Tinker INT file.
static void setTinkerTypes(TinkerMolecule tmol, HashMap< String, Integer > tMap)
Conversion to tinker IC may not always have the necessary atom types.
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,...
Utilities for molecular models builder.
static int countLinesWKeywordInFile(String filename, String keyword)
Count the number of lines starting with a keyword.
Compare two ConnectedLigand according to the number of connected atoms and the mass number.
A ConnectedLigand is just an atom with an explicit field reporting the number of connected atoms.
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...
Some useful math operations.
static double angle(Point3d a, Point3d b, Point3d c)
Calculate the angle between the 3 points.
static double torsion(Point3d p1, Point3d p2, Point3d p3, Point3d p4)
Calculate the torsion angle between the 4 points.
Utilities for molecule conversion.
static String getSymbolOrLabel(IAtom atm)
Gets either the elemental symbol (for standard atoms) of the label (for pseudo-atoms).
This class is the equivalent of the Pair data structure used in C++ Although AbstractMap....