19package denoptim.utils;
21import java.io.BufferedReader;
23import java.io.FileReader;
24import java.io.IOException;
25import java.util.ArrayList;
26import java.util.HashMap;
28import java.util.logging.Level;
29import java.util.logging.Logger;
30import java.util.TreeMap;
32import org.openscience.cdk.graph.SpanningTree;
33import org.openscience.cdk.interfaces.IAtom;
34import org.openscience.cdk.interfaces.IAtomContainer;
35import org.openscience.cdk.interfaces.IBond;
36import org.openscience.cdk.interfaces.IChemObjectBuilder;
37import org.openscience.cdk.interfaces.IRingSet;
38import org.openscience.cdk.isomorphism.Mappings;
39import org.openscience.cdk.silent.RingSet;
40import org.openscience.cdk.silent.SilentChemObjectBuilder;
42import denoptim.constants.DENOPTIMConstants;
43import denoptim.exception.DENOPTIMException;
44import denoptim.io.DenoptimIO;
56 private static final IChemObjectBuilder
builder =
57 SilentChemObjectBuilder.getInstance();
87 String defRotBndsFile,
boolean addIterfragBonds,
boolean excludeRings,
90 ArrayList<ObjectPair> rotatableBonds =
new ArrayList<ObjectPair>();
93 for (IBond b : mol.bonds())
106 IAtomContainer locMol =
builder.newAtomContainer();
108 locMol = mol.clone();
109 for (
int iAtm=0; iAtm<locMol.getAtomCount(); iAtm++)
113 }
catch (Throwable t) {
118 if (!defRotBndsFile.equals(
""))
128 String msg =
"WARNING! Attempt to match rotatable bonds returned "
129 +
"an error! Selecting only fragment-fragment "
131 logger.log(Level.WARNING, msg);
134 for (String name : listQueries.keySet())
144 for (
int[] singleMatch : matches)
147 if (singleMatch.length != 2)
149 throw new Error(
"DENOPTIM can only deal with bonds "
150 +
"involving 2 atoms. Check bond "
162 boolean alreadyThere =
false;
165 int a1 = ((Integer)op.getFirst()).intValue();
166 int a2 = ((Integer)op.getSecond()).intValue();
167 if (((a1 == idAtmA) && (a2 == idAtmB)) ||
168 ((a2 == idAtmA) && (a1 == idAtmB)))
177 Integer.valueOf(idAtmA),
178 Integer.valueOf(idAtmB));
179 rotatableBonds.add(newRotBnd);
187 SpanningTree st =
new SpanningTree(mol);
188 IRingSet allRings =
new RingSet();
192 allRings = st.getAllRings();
193 }
catch (Exception ex) {
199 ArrayList<ObjectPair> toRemove =
new ArrayList<ObjectPair>();
202 int a1 = ((Integer)op.getFirst()).intValue();
203 int a2 = ((Integer)op.getSecond()).intValue();
205 IBond bnd = mol.getBond(mol.getAtom(a1),mol.getAtom(a2));
209 IRingSet rs = allRings.getRings(bnd);
212 logger.log(Level.FINE,
"Ignoring cyclic bond: "+bnd);
224 rotatableBonds.remove(op);
228 return rotatableBonds;
244 ArrayList<ObjectPair> interfragBonds =
new ArrayList<ObjectPair>();
247 for (IBond bnd : mol.bonds())
249 if (bnd.getAtomCount() == 2)
251 IAtom atmA = bnd.getAtom(0);
252 IAtom atmB = bnd.getAtom(1);
253 String fragIdA = atmA.getProperty(p).toString();
254 String fragIdB = atmB.getProperty(p).toString();
256 if (!fragIdA.equals(fragIdB) &&
257 (mol.getConnectedBondsCount(atmA) != 1) &&
258 (mol.getConnectedBondsCount(atmB) != 1))
261 int idAtmA = mol.indexOf(atmA);
262 int idAtmB = mol.indexOf(atmB);
265 newRotBnd =
new ObjectPair(Integer.valueOf(idAtmA),
266 Integer.valueOf(idAtmB));
268 newRotBnd =
new ObjectPair(Integer.valueOf(idAtmB),
269 Integer.valueOf(idAtmA));
271 interfragBonds.add(newRotBnd);
276 String str =
"ERROR! Unable to handle bonds involving other "
281 return interfragBonds;
296 Map<String,String> mapOfSMARTS =
new HashMap<String,String>();
299 if (filename.equals(
null))
302 +
" null! Cannot read definition of rotational space.");
305 File f =
new File(filename);
309 +
"' does not exist! Cannot find definition of rotational "
313 BufferedReader br =
null;
317 br =
new BufferedReader(
new FileReader(filename));
318 while ((line = br.readLine()) !=
null)
320 if (line.trim().length() == 0)
323 if (line.trim().startsWith(
"#"))
326 String[] parts = line.split(
"\\s+");
327 if (parts.length != 2)
330 +
"rotational Space definition. "
331 +
"Check line '"+ line +
"' in file "
334 String key = parts[0];
335 String smarts = parts[1];
336 if (mapOfSMARTS.keySet().contains(key))
339 +
"rotatabe bond named '" + key +
"'. "
340 +
"Check line '"+ line +
"' in file "
345 mapOfSMARTS.put(key,smarts);
349 catch (IOException nfe)
362 catch (IOException ioe)
388 IAtomContainer mol, ArrayList<ObjectPair> rotatableBonds,
393 IAtomContainer locMol =
builder.newAtomContainer();
395 locMol = mol.clone();
396 for (
int iAtm=0; iAtm<locMol.getAtomCount(); iAtm++)
400 }
catch (Throwable t) {
405 if (defRotBndContrFile.equals(
""))
415 TreeMap<String,String> listQueries =
new TreeMap<String,String>();
418 listQueries.put(e.getKey(),e.getValue().getSmarts());
428 String msg =
"ERROR! Attempt to match constrained rotatable bonds returned "
430 throw new Error(msg);
433 for (Map.Entry<String,String> e : listQueries.entrySet())
435 String name = e.getKey();
445 for (
int[] singleMatch : matches)
448 if (singleMatch.length != 4)
450 throw new Error(
"Expecting a constrain to be defined by 4 atoms. "
451 +
"Check this match of constraint '" + name +
"': "
470 int a1 = ((Integer)op.getFirst()).intValue();
471 int a2 = ((Integer)op.getSecond()).intValue();
472 if (((a1 == idAtmB) && (a2 == idAtmC)) ||
473 ((a2 == idAtmB) && (a1 == idAtmC)))
479 if (opToRemove!=
null)
481 rotatableBonds.remove(opToRemove);
482 IBond bnd = mol.getBond(mol.getAtom(idAtmB), mol.getAtom(idAtmC));
484 mol.getAtom(idAtmA), mol.getAtom(idAtmB),
485 mol.getAtom(idAtmC), mol.getAtom(idAtmD)});
487 mapOfConstraints.get(name).getValue());
489 logger.log(Level.INFO,
"Constraining dihedral along bond "
493 +
" (rotatable bond constrain: '" + name +
"')");
511 TreeMap<String,RotBndConstraint> mapOfConstraints =
new TreeMap<String,RotBndConstraint>();
514 if (filename.equals(
null))
517 +
" null! Cannot read definition of rotational constraints.");
520 File f =
new File(filename);
524 +
"' does not exist! Cannot find definition of rotational "
528 BufferedReader br =
null;
532 br =
new BufferedReader(
new FileReader(filename));
533 while ((line = br.readLine()) !=
null)
535 if (line.trim().length() == 0)
538 if (line.trim().startsWith(
"#"))
541 String[] parts = line.split(
"\\s+");
543 if (parts.length != 3)
546 +
"rotational constraints definition. "
547 +
"Check line '"+ line +
"' in file "
550 String key = parts[0];
551 String smarts = parts[1];
552 double value = Double.parseDouble(parts[2]);
553 if (mapOfConstraints.keySet().contains(key))
556 +
"rotational contraint named '" + key +
"'. "
557 +
"Check line '"+ line +
"' in file "
566 catch (IOException nfe)
579 catch (IOException ioe)
584 return mapOfConstraints;
General set of constants used in DENOPTIM.
static final String ATMPROPVERTEXPATH
Name of Atom property used to store the unique ID of the Vertex that owns the atom and the IDs of any...
static final String BONDPROPROTATABLE
String tag of Bond's property used to store the property of being rotatable.
Utility methods for input/output.
static void writeSDFFile(String fileName, IAtomContainer mol)
Writes IAtomContainer to SDF file.
Container of lists of atoms matching a list of SMARTS.
Mappings getMatchesOfSMARTS(String ref)
int getNumMatchesOfQuery(String query)
Utilities for molecule conversion.
static void removeRCA(IAtomContainer mol)
Replace any PseudoAtoms representing ring closing attractors with H.
static String getAtomRef(IAtom atm, IAtomContainer mol)
This class is the equivalent of the Pair data structure used in C++ Although AbstractMap....
The definition of a constraint applying to rotatable bonds.
Tool box for definition and management of the rotational space, which is given by the list of rotatab...
static final IChemObjectBuilder builder
static TreeMap< String, RotBndConstraint > getRotationalConstraintsDefinition(String filename)
Reads the definition of constraints on the rotatable bonds.
static ArrayList< ObjectPair > getInterVertexBonds(IAtomContainer mol)
Search for all bonds connecting atoms corresponding to two different vertices in the DENOPTIMGraph.
static final String PROPERTY_ROTDBDCSTR_DEF
static final String PROPERTY_RSU_ATMID
static void processConstrainedRotatableBonds(IAtomContainer mol, ArrayList< ObjectPair > rotatableBonds, String defRotBndContrFile, Logger logger)
Process the constrained rotatable bonds.
static final String PROPERTY_ROTDBDCSTR_VALUE
static Map< String, String > getRotationalSpaceDefinition(String filename)
Read a formatted file and return a map with all the SMARTS queries identifying rotatable bonds.
static ArrayList< ObjectPair > defineRotatableBonds(IAtomContainer mol, String defRotBndsFile, boolean addIterfragBonds, boolean excludeRings, Logger logger)
Define the rotational space (a.k.a.