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;
46import denoptim.utils.RotationalSpaceUtils;
57 private static final String
NL = System.getProperty(
"line.separator");
58 private static boolean debug =
false;
114 BufferedReader br =
null;
122 br =
new BufferedReader(
new FileReader(filename));
123 line = br.readLine().trim();
126 String[] arr = line.split(
" +");
127 natom = Integer.parseInt(arr[0]);
130 String msg =
"Invalid number of atoms in " + filename;
136 arr = line.split(
" +", 2);
143 ArrayList<int[]> zadd =
new ArrayList<>();
144 ArrayList<int[]> zdel =
new ArrayList<>();
146 ArrayList<TinkerAtom> lstAtom =
new ArrayList<>();
148 for (
int i = 0; i < natom; i++)
150 line = br.readLine();
156 arr = line.trim().split(
" +");
157 if (arr ==
null || arr.length < 3)
159 String msg =
"Check atom " + (i + 1) +
" in " + filename;
164 String name = arr[1];
165 int type = Integer.parseInt(arr[2]);
166 double zv[] =
new double[3];
167 int zi[] =
new int[4];
176 zi[0] = Integer.parseInt(arr[3]);
177 zv[0] = Double.parseDouble(arr[4]);
187 zi[1] = Integer.parseInt(arr[5]);
188 zv[1] = Double.parseDouble(arr[6]);
196 if (arr.length >= 10)
198 zi[2] = Integer.parseInt(arr[7]);
199 zv[2] = Double.parseDouble(arr[8]);
200 zi[3] = Integer.parseInt(arr[9]);
214 line = br.readLine();
216 if (line.trim().equalsIgnoreCase(
""))
220 boolean blank =
false;
221 while (br.ready() && !blank)
223 line = br.readLine();
224 if (line.trim().equalsIgnoreCase(
""))
230 arr = line.trim().split(
" +");
233 String msg =
"Check Bond Pair to Remove: "
234 + (zadd.size() + 1) +
" in " + filename;
237 int pair[] =
new int[2];
238 pair[0] = Integer.parseInt(arr[0]);
239 pair[1] = Integer.parseInt(arr[1]);
246 line = br.readLine();
247 arr = line.trim().split(
" +");
250 String msg =
"Check Bond Pair to Remove: "
251 + (zadd.size() + 1) +
" in " + filename;
254 int pair[] =
new int[2];
255 pair[0] = Integer.parseInt(arr[0]);
256 pair[1] = Integer.parseInt(arr[1]);
265 catch (NumberFormatException | IOException nfe)
278 catch (IOException ioe)
300 ArrayList<double[]> coords =
new ArrayList<>();
302 BufferedReader br =
null;
309 br =
new BufferedReader(
new FileReader(filename));
310 line = br.readLine().trim();
313 String[] arr = line.split(
"\\s+");
314 natom = Integer.parseInt(arr[0]);
317 String msg =
"Invalid number of atoms in " + filename;
321 while ((line = br.readLine()) !=
null)
323 if (line.trim().length() == 0)
328 arr = line.trim().split(
"\\s+");
333 double[] f =
new double[3];
334 f[0] = Double.parseDouble(arr[2]);
335 f[1] = Double.parseDouble(arr[3]);
336 f[2] = Double.parseDouble(arr[4]);
342 catch (NumberFormatException | IOException nfe)
355 catch (IOException ioe)
361 if (coords.size() != natom)
384 FileWriter fw =
null;
387 fw =
new FileWriter(
new File(filename));
388 int numatoms = tmol.getAtoms().size();
390 String header = String.format(
"%6d %s%n", numatoms, tmol.getName());
396 for (
int i = 0; i < numatoms; i++)
404 line = String.format(
"%6d %-3s%6d%n",
414 line = String.format(
"%6d %-3s%6d%6d%10.5f%n",
424 line = String.format(
"%6d %-3s%6d%6d%10.5f%6d%10.4f%n",
433 line = String.format(
"%6d %-3s%6d%6d%10.5f%6d%10.4f%6d%10.4f%6d%n",
436 d1[1], d2[1], d1[2], d2[2], d1[3]);
445 ArrayList<int[]> zadd = tmol.getBondAdd();
446 ArrayList<int[]> zdel = tmol.getBondDel();
448 if (zadd.size() > 0 || zdel.size() > 0)
453 for (
int i = 0; i < zadd.size(); i++)
455 int[] z = zadd.get(i);
456 line = String.format(
"%6d%6d%n", z[0], z[1]);
466 for (
int i = 0; i < zdel.size(); i++)
468 int[] z = zdel.get(i);
469 line = String.format(
"%6d%6d%n", z[0], z[1]);
477 catch (IOException ioe)
490 catch (IOException ioe)
509 ArrayList<Double> energies =
new ArrayList<>();
511 BufferedReader br =
null;
515 br =
new BufferedReader(
new FileReader(filename));
517 while ((line = br.readLine()) !=
null)
520 if (line.contains(
"Final Function Value and Deformation"))
522 String str = line.substring(38);
524 String[] arr = str.split(
"\\s+");
525 energies.add(Double.parseDouble(arr[1]));
529 catch (NumberFormatException | IOException nfe)
542 catch (IOException ioe)
548 if (energies.isEmpty())
550 String msg =
"No data found in file: " + filename;
566 ArrayList<String> initPars, ArrayList<String> restPars)
569 BufferedReader br =
null;
575 br =
new BufferedReader(
new FileReader(filename));
576 while ((line = br.readLine()) !=
null)
578 if (line.trim().length() == 0)
580 if (line.contains(
"INIT"))
585 if (line.contains(
"REST"))
591 initPars.add(line.trim());
593 restPars.add(line.trim());
596 catch (IOException nfe)
598 String msg =
"File '" + filename +
"' not found.";
610 catch (IOException ioe)
616 if (initPars.isEmpty())
618 String msg =
"No data found in file: " + filename;
621 if (restPars.isEmpty())
623 String msg =
"No data found in file: " + filename;
641 HashMap<String, Integer> atomTypesMap =
new HashMap<>();
643 BufferedReader br =
null;
648 br =
new BufferedReader(
new FileReader(filename));
649 while ((line = br.readLine()) !=
null)
653 if (!line.startsWith(
"atom"))
664 String[] dq = line.split(
"\"");
666 String[] str1 = fp.split(
"\\s+");
669 int atomType = Integer.parseInt(str1[1]);
670 String symbol = str1[2];
681 atomTypesMap.put(symbol,atomType);
685 String msg =
"Format of Tinker's atom type definition not "
686 +
"recognized. " +
NL +
"Details: " +
NL
692 catch (NumberFormatException | IOException nfe)
705 catch (IOException ioe)
711 if (atomTypesMap.isEmpty())
713 String msg =
"No data found in file: " + filename;
735 String doneBnd =
"visitedBond";
736 for (
int i=0; i<mol.getAtomCount(); i++)
746 int[] nbrs =
new int[] {0, 0, 0, 0};
748 IAtom atmI = mol.getAtom(i);
751 System.err.println(
"Atom to IC: "
759 d = atmI.getPoint3d().distance(mol.getAtom(i2).getPoint3d());
760 mol.getBond(atmI,mol.getAtom(i2)).setProperty(doneBnd,
"T");
763 System.err.println(
" i2 = " + i2 +
" d: " + d);
773 mol.getAtom(i2).getPoint3d(),
774 mol.getAtom(i3).getPoint3d());
777 System.err.println(
" i3 = "+ i3 +
" a: " + a);
792 mol.getAtom(i2).getPoint3d(),
793 mol.getAtom(i4).getPoint3d());
796 mol.getAtom(i2).getPoint3d(),
797 mol.getAtom(i3).getPoint3d(),
798 mol.getAtom(i4).getPoint3d());
806 IAtom atmJ = mol.getAtom(i2);
807 IAtom atmK = mol.getAtom(i3);
808 IAtom atmL = mol.getAtom(i4);
810 atmI.getPoint3d(), atmJ.getPoint3d(),
811 atmK.getPoint3d(), atmL.getPoint3d());
815 IBond bnd = mol.getBond(atmJ, atmK);
818 Object cnstrDefObj = bnd.getProperty(
820 if (cnstrDefObj!=
null)
826 IAtom[] atomsABCD = (IAtom[]) cnstrDefObj;
827 double cnstrABCD = (double) bnd.getProperty(
829 if (atmJ==atomsABCD[2] && atmK==atomsABCD[1])
833 atomsABCD =
new IAtom[]{
834 atomsABCD[3], atomsABCD[2],
835 atomsABCD[1], atomsABCD[0]};
838 cnstrABCD = 360.0 - cnstrABCD;
840 cnstrABCD = cnstrABCD - 360.0;
845 atomsABCD[0].getPoint3d(),
846 atomsABCD[1].getPoint3d(),
847 atomsABCD[2].getPoint3d(),
848 atomsABCD[3].getPoint3d());
849 double correctionABCD = cnstrABCD - valueABCD;
851 t = valueIJKL + correctionABCD;
856 }
else if (t < -360) {
862 System.err.println(
" dihedral constrain along "
863 + i2 +
"-" + i3 +
": "
864 + valueIJKL +
" -> " + t
865 +
" (changed by " + correctionABCD +
")");
872 System.err.println(
" i4 = "+ i4 +
" t: " + t +
" " + i5);
884 new double[] {atmI.getPoint3d().x,
886 atmI.getPoint3d().z},
888 new double[] {d,a,t});
895 System.err.println(
" TinkerAtom: "+ta.
toString());
902 System.err.println(
"TinkerMolecule: ");
908 for (IBond b : mol.bonds())
910 if (b.getProperty(doneBnd) ==
null)
912 tm.
addBond(mol.indexOf(b.getAtom(0))+1,
913 mol.indexOf(b.getAtom(1))+1);
930 List<ConnectedLigand> candidates =
new ArrayList<ConnectedLigand>();
931 for (IAtom nbr : mol.getConnectedAtomsList(mol.getAtom(i1)))
933 if (mol.indexOf(nbr) < i1)
940 int i2 = mol.indexOf(candidates.get(0).getAtom());
948 List<ConnectedLigand> candidates =
new ArrayList<ConnectedLigand>();
949 for (IAtom nbr : mol.getConnectedAtomsList(mol.getAtom(i2)))
951 if ((mol.indexOf(nbr) < i1) && (nbr != mol.getAtom(i1)))
958 int i3 = mol.indexOf(candidates.get(0).getAtom());
968 IAtom atmI1 = mol.getAtom(i1);
969 IAtom atmI2 = mol.getAtom(i2);
970 IAtom atmI3 = mol.getAtom(i3);
971 List<ConnectedLigand> candidates =
new ArrayList<ConnectedLigand>();
972 if (tm.isTorsionUsed(i2+1, i3+1) ||
976 for (IAtom nbr : mol.getConnectedAtomsList(atmI2))
980 System.err.println(
" Eval. 3rd (ANG): " +
982 + mol.indexOf(nbr) +
" "
983 + (mol.indexOf(nbr) < i1) +
" "
984 + (nbr != atmI1) +
" "
987 if ((mol.indexOf(nbr) < i1) && (nbr != atmI1) &&
1002 System.err.println(
" ...but collinear with "
1004 +
" (i4-i2-i3: " + dbcAng
1014 for (IAtom nbr : mol.getConnectedAtomsList(atmI3))
1018 System.err.println(
" Eval. 3rd (TOR): "
1020 + mol.indexOf(nbr) +
" "
1021 + (mol.indexOf(nbr) < i1) +
" "
1022 + (nbr != atmI1) +
" "
1025 if ((mol.indexOf(nbr) < i1) && (nbr != atmI1) &&
1034 if (candidates.size() == 0)
1036 String msg =
"Unable to make internal coordinates. Please, "
1037 +
"consider the use of dummy atoms in proximity "
1038 +
"of atom " + tm.getAtom(i1+1);
1041 int i4 = mol.indexOf(candidates.get(0).getAtom());
1053 for (IAtom nbr : mol.getConnectedAtomsList(a))
1055 if (mol.indexOf(nbr) < i)
1074 ArrayList<TinkerAtom> lstAtoms = tmol.getAtoms();
1075 int numberOfAtoms = lstAtoms.size();
1076 for (
int i = 0; i < numberOfAtoms; i++)
1081 if (!tMap.containsKey(st))
1083 String msg =
"Unable to assign atom type to atom '" + st +
"'. ";
1086 msg = msg +
"Unusual dummy atom symbols get atom symbol "
1087 +
"'R'. Please, add atom type 'R' in your atom type map.";
1091 Integer val = tMap.get(st);
1096 System.err.println(
"Set parameter for " + st +
" " + val);
1100 String msg =
"No valid Tinker atom type assigned for atom "
1126 File output =
new File(outputPathName);
1127 if (output.exists() && output.canRead())
1132 String errMsg =
"TINKER ERROR: ";
1133 ReversedLinesFileReader fr =
null;
1136 fr =
new ReversedLinesFileReader(
new File(
1137 logPathName),
null);
1140 for (
int i=0; i<100; i++)
1142 String line = fr.readLine();
1146 if (line.trim().isEmpty())
1151 errMsg = errMsg +
NL + line;
1154 }
catch (IOException e)
1185 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....
Tool box for definition and management of the rotational space, which is given by the list of rotatab...
static final String PROPERTY_ROTDBDCSTR_DEF
static final String PROPERTY_ROTDBDCSTR_VALUE