19package denoptim.graph;
21import java.io.StringReader;
22import java.lang.reflect.Type;
23import java.util.ArrayList;
24import java.util.Comparator;
25import java.util.HashMap;
26import java.util.LinkedHashMap;
29import java.util.Map.Entry;
30import java.util.TreeMap;
31import java.util.logging.Logger;
33import javax.vecmath.Point3d;
35import org.openscience.cdk.interfaces.IAtom;
36import org.openscience.cdk.interfaces.IAtomContainer;
38import com.google.gson.Gson;
39import com.google.gson.JsonObject;
40import com.google.gson.internal.Streams;
41import com.google.gson.reflect.TypeToken;
42import com.google.gson.stream.JsonReader;
44import denoptim.constants.DENOPTIMConstants;
45import denoptim.exception.DENOPTIMException;
46import denoptim.graph.Edge.BondType;
47import denoptim.json.DENOPTIMgson;
48import denoptim.molecularmodeling.ThreeDimTreeBuilder;
49import denoptim.utils.MoleculeUtils;
50import denoptim.utils.MutationType;
51import denoptim.utils.Randomizer;
94 private IAtomContainer
mol =
null;
162 return new String[]{a2b,b2a};
190 template.setInnerGraph(g);
219 this.contractLevel = contract;
299 throws IllegalArgumentException
308 throw new IllegalArgumentException(
"inner graph does not have all" +
329 for (Entry<Integer, AttachmentPoint> e : map.entrySet())
334 this.innerToOuterAPs.
put(innerAP, outerAP);
431 Comparator<AttachmentPoint> apClassComparator
433 Comparator.nullsLast(Comparator.naturalOrder()));
434 innerAPs.sort(apClassComparator);
436 reqAPs.sort(apClassComparator);
437 int matchesLeft = reqAPs.size();
438 for (
int i = 0, j = 0; matchesLeft > 0 && i < innerAPs.size(); i++)
440 if (apClassComparator.compare(innerAPs.get(i), reqAPs.get(j)) == 0)
446 return matchesLeft == 0;
461 return new ArrayList<>();
497 List<SymmetricAPs> allSymSets =
new ArrayList<SymmetricAPs>();
499 List<AttachmentPoint> doneAPs =
new ArrayList<AttachmentPoint>();
502 if (doneAPs.contains(innerAP))
508 Vertex vrtx = innerAP.getOwner();
509 int innerAPIdx = innerAP.getIndexInOwner();
511 if (sAPsOnVrtx.size()!=0)
515 if (doneAPs.contains(symInnerAP))
520 doneAPs.add(symInnerAP);
526 for (
Vertex symVrtx : symVrtxs)
532 if (doneAPs.contains(innerApOnSymVrtx))
537 if (sAPsOnSymVrtx.size()!=0)
541 if (doneAPs.contains(symInnerAPOnSymVrtx))
546 symInnerAPOnSymVrtx));
547 doneAPs.add(symInnerAPOnSymVrtx);
559 doneAPs.add(innerApOnSymVrtx);
561 if (!doneAPs.contains(innerAP))
564 doneAPs.add(innerAP);
568 if (symSetForThisAP.size()>1)
569 allSymSets.add(symSetForThisAP);
625 Map<AttachmentPoint,AttachmentPoint>
626 apInnerGraphToApOnMol =
new HashMap<>();
627 if (updateAPsAccordingToIAC)
629 for (IAtom a :
mol.atoms())
636 @SuppressWarnings(
"unchecked")
637 ArrayList<AttachmentPoint> apLst =
638 (ArrayList<AttachmentPoint>) p;
648 apInnerGraphToApOnMol.put(linkedAPOnGraph, apOnMol);
657 LinkedHashMap<Integer,List<AttachmentPoint>> apsPerAtom =
658 new LinkedHashMap<>();
662 int atmIndexInMol = outAP.getAtomPositionNumber();
663 if (updateAPsAccordingToIAC
664 && apInnerGraphToApOnMol.containsKey(inAPOnGraph))
670 outAP.setAtomPositionNumber(atmIndexInMol);
671 if (apsPerAtom.containsKey(atmIndexInMol))
673 apsPerAtom.get(atmIndexInMol).add(outAP);
675 List<AttachmentPoint> list =
676 new ArrayList<AttachmentPoint>();
678 apsPerAtom.put(atmIndexInMol, list);
682 for (
int i = 0; i <
mol.getAtomCount(); i++) {
683 IAtom a =
mol.getAtom(i);
717 Logger logger = Logger.getLogger(
"DummyLogger");
719 boolean removeUsedRCAs =
true;
742 Randomizer rng,
boolean removeUsedRCAs,
boolean rebuild)
744 if (
mol!=
null && !rebuild)
758 LinkedHashMap<Integer,List<AttachmentPoint>> apsPerAtom =
759 new LinkedHashMap<>();
765 outAP.setDirectionVector(
null);
770 outAP.setAtomPositionNumber(atmIndexInMol);
771 if (apsPerAtom.containsKey(atmIndexInMol))
773 apsPerAtom.get(atmIndexInMol).add(outAP);
775 List<AttachmentPoint> list =
776 new ArrayList<AttachmentPoint>();
778 apsPerAtom.put(atmIndexInMol, list);
818 if (outerAP == entry.getValue())
820 return entry.getKey();
841 List<MutationType> scaffCompatTypes =
new ArrayList<MutationType>();
852 scaffCompatTypes.removeAll(ignoredTypes);
853 return scaffCompatTypes;
855 return super.getMutationTypes(ignoredTypes);
874 List<Vertex> lst =
new ArrayList<Vertex>();
910 List<MutationType> toBeRemoved =
new ArrayList<MutationType>();
938 throw new IllegalArgumentException(
"cannot add more required APs " +
939 "after setting the inner graph");
960 reason.append(
"Different contract level ("
966 if (this.requiredAPs.size() == other.
requiredAPs.size())
972 if (!tAP.sameAs(oAP))
974 reason.append(
"No required AP corresponding to "+tAP);
980 reason.append(
"Different size of required APs("
981 + this.requiredAPs.size()+
":"
1016 String jsonOutput = gson.toJson(
this);
1038 JsonObject jsonObject = (JsonObject) Streams.parse(
new JsonReader(
1039 new StringReader(json)));
1041 if (jsonObject.has(
"innerGraph"))
1043 JsonObject innerGraphJson = jsonObject.getAsJsonObject(
1048 if (jsonObject.has(
"innerToOuterAPs"))
1050 Type type =
new TypeToken<TreeMap<Integer,
1052 TreeMap<Integer,AttachmentPoint> map = gson.fromJson(
1053 jsonObject.getAsJsonObject(
"innerToOuterAPs"), type);
General set of constants used in DENOPTIM.
static final String GRAPHTAG
SDF tag containing graph encoding.
static final String VERTEXJSONTAG
SDF tag containing vertex encoding in JSON format.
static final String ATMPROPAPS
String tag of Atom property used to store attachment points.
static final String PROVENANCE
SDF tag containing provenance data for a graph.
static final String APSTAG
SDF tag defining attachment points.
static final String ATMPROPVERTEXID
String tag of Atom property used to store the unique ID of the Vertex corresponding to the molecular ...
static final String GCODETAG
SDF tag containing graph ID.
static final String GRAPHJSONTAG
SDF tag containing graph encoding in JSON format.
static final Object LINKAPS
Key of property used to records references of APs.
static final String ATMPROPORIGINALATMID
Name of Atom property used to store the original position of an atom in the atom list of the fragment...
Attachment point mapping where keys are sorted by natural ordering, i.e., by AttachmentPoint#compareT...
AttachmentPoint put(AttachmentPoint key, AttachmentPoint value)
AttachmentPoint remove(Object key)
AttachmentPoint get(Object key)
An attachment point (AP) is a possibility to attach a Vertex onto the vertex holding the AP (i....
AttachmentPoint getLinkedAP()
Gets the attachment point (AP) that is connected to this AP via the edge user.
void setAPClass(String apClass)
Set the Attachment Point class.
APClass getAPClass()
Returns the Attachment Point class.
void setAtomPositionNumber(int atomPositionNumber)
Set the index of the source atom in the list of atoms of the fragment.
void setDirectionVector(Point3d dirVec)
Sets the end of the 3D vector defining the direction of the AP in 3D.
void setOwner(Vertex owner)
Sets the reference to the vertex that owns this attachment point.
boolean isAvailable()
Check availability of this attachment point.
int getAtomPositionNumberInMol()
The index of the source atom in the atom list of the entire molecule.
int getAtomPositionNumber()
The index of the source atom in the atom list of the fragment.
AttachmentPoint clone()
Returns a deep clone of this attachment point.
Point3d getDirectionVector()
Returns the end of the direction vector.
static String getAPDefinitionsForSDF(LinkedHashMap< Integer, List< AttachmentPoint > > apsPerIndex)
Prepares the two strings that can be used to define AttachmentPoints in SDF files.
Container for the list of vertices and the edges that connect them.
void setTemplateJacket(Template template)
Sets the reference to a template that embeds this graph, i.e., this graph's "jacket" template.
ArrayList< AttachmentPoint > getAvailableAPs()
Returns the list of available attachment points contained in this graph.
void addVertex(Vertex vertex)
Appends a vertex to this graph without creating any edge.
AttachmentPoint getAPWithId(int id)
Returns the attachment point with the given identifier, or null if no AP is found with the given iden...
List< Vertex > gVertices
The vertices belonging to this graph.
boolean containsAtoms()
Returns true if this graph has any vertex that contains atoms.
List< Vertex > getSymVerticesForVertex(Vertex v)
static DGraph fromJson(String json)
Reads a JSON string and returns an instance of this class.
Template templateJacket
Reference to the Template embedding this graph.
boolean removeBranchStartingAt(Vertex v, boolean symmetry)
Deletes the branch, i.e., the specified vertex and its children.
int getHeavyAtomsCount()
Calculate the number of atoms from the graph representation.
void addEdge(Edge edge)
Adds the edge to the list of edges belonging to this graph.
This class represents the edge between two vertices.
An empty vertex has the behaviors of a vertex, but has no molecular structure.
void addAP()
Adds an attachment point with no APClass or other attribute.
A collection of AttachmentPoints that are related by a relation that we call "symmetry",...
boolean add(T item)
Adds an item to this list, if not already present.
ContractLevel getContractLevel()
Returns the contract level of this template, i.e., to what extent the content of this template can be...
void removeProjectionOfInnerAP(AttachmentPoint oldInnerAP)
Removes the mapping of the given inner AP from this template's surface, if such mapping exists.
List< Vertex > getMutationSites(List< MutationType > ignoredTypes)
A list of mutation sites from within this vertex.
static Template fromJson(String json)
Reads a JSON string and returns an instance of this class.
boolean isValidInnerGraph(DGraph g)
void updateMutTypeToFixedSTructure()
void setInnerGraph(DGraph innerGraph)
String[] getPathIDs(AttachmentPoint apA, AttachmentPoint apB)
Produces a pair of strings that identify the "path" between two given attachment points.
APTreeMap innerToOuterAPs
void updateInnerApID(AttachmentPoint oldInnerAP, AttachmentPoint newInnerAP)
Replaces a given link between APs on the surface of this template (i.e., outerAP) and the correspondi...
boolean sameAs(Template other, StringBuilder reason)
Compares this and another template ignoring vertex IDs.
void addInnerToOuterAPMapping(AttachmentPoint newInnerAP)
Adds the projection of an AP in the template's inner graph (i.e., innerAP) to the list of APs visible...
boolean freezeTemplate()
Promotes the contract level of this template to the most constrained one (i.e., ContractLevel#FIXED),...
IAtomContainer getIAtomContainer(Logger logger, Randomizer rng, boolean removeUsedRCAs, boolean rebuild)
The molecular representation, if any, is generated by this method and stored until further changes in...
IAtomContainer mol
Molecular representation of the content of this template.
ArrayList< AttachmentPoint > getAttachmentPoints()
Return the list of attachment points visible from outside the template, i.e., the so-called outer APs...
void clearIAtomContainer()
Removes the molecular representation.
void updateInnerToOuter(TreeMap< Integer, AttachmentPoint > map)
void setSymmetricAPSets(List< SymmetricAPs > sAPs)
This method exists by contract, but does not do anything because the concept of setting the symmetric...
void addRequiredAP(Point3d pt, APClass apClass)
Adds attachment point (AP) to the list of required APs on this template.
List< SymmetricAPs > getSymmetricAPSets()
The SymmetricSet produced from this method contain indexes of the AttachmentPoints in the list return...
static Template getTestTemplate(ContractLevel contractLevel)
Method meant for devel phase only.
List< MutationType > getMutationTypes(List< MutationType > ignoredTypes)
Returns the list of mutation types.
Template(Vertex.BBType bbType)
ContractLevel contractLevel
Denotes the constants in the template.
void setContractLevel(ContractLevel contract)
Imposes the given contract to this template.
IAtomContainer getIAtomContainer()
The molecular representation, if any, is generated by this method and stored until further changes in...
Template clone()
Returns a deep copy of this template.
AttachmentPoint getOuterAPFromInnerAP(AttachmentPoint innerAP)
void addSymmetricAPSet(SymmetricAPs symAPs)
List< AttachmentPoint > requiredAPs
void setIAtomContainer(IAtomContainer mol, boolean updateAPsAccordingToIAC)
Attaches a molecular representation to this template.
AttachmentPoint getInnerAPFromOuterAP(AttachmentPoint outerAP)
DGraph innerGraph
Graph that is embedded in this vertex.
List< AttachmentPoint > getRequiredAPs()
String toJson()
Produces a string that represents this vertex and that adheres to the JSON format.
A vertex is a data structure that has an identity and holds a list of AttachmentPoints.
Set< String > uniquefyingPropertyKeys
List of properties required to make Vertex#sameAs(Vertex, StringBuilder) method return false when pro...
void setMutationTypes(List< MutationType > lst)
List< MutationType > getMutationTypes()
Returns the list of mutation types.
void setProperties(Map< Object, Object > properties)
int getBuildingBlockId()
Returns the index of the building block that should correspond to the position of the building block ...
void setVertexId(long vertexId2)
int getIndexOfAP(AttachmentPoint ap)
Returns the position of the given AP in the list of APs of this vertex.
Vertex.BBType getBuildingBlockType()
List< MutationType > getUnfilteredMutationTypes()
Returns the mutation types that are constitutionally configures for this vertex irrespectively on the...
List< Vertex > getMutationSites()
A list of mutation sites from within this vertex.
DGraph getGraphOwner()
Returns the graph this vertex belongs to or null.
Map< Object, Object > copyStringBasedProperties()
Copies all the string-based properties and properties defined in the Vertex#uniquefyingPropertyKeys s...
boolean sameVertexFeatures(Vertex other, StringBuilder reason)
Compares this and another vertex ignoring vertex IDs.
ArrayList< Vertex > getChilddren()
Looks into the edges that use any of the APs that belong to this vertex and returns the list of verti...
void setBuildingBlockId(int buildingBlockId)
SymmetricAPs getSymmetricAPs(AttachmentPoint ap)
For the given attachment point index locate the symmetric partners i.e.
boolean removeMutationType(MutationType mt)
Removes the specified mutation type.
void setBuildingBlockType(Vertex.BBType buildingBlockType)
AttachmentPoint getAP(int i)
Get attachment point i on this vertex.
Class for de/serializing DENOPTIM graphs from/to JSON format.
Tool to build build three-dimensional (3D) tree-like molecular structures from DGraph.
IAtomContainer convertGraphTo3DAtomContainer(DGraph graph)
Created a three-dimensional molecular representation from a given DGraph.
Utilities for molecule conversion.
static IAtomContainer makeSameAs(IAtomContainer mol)
Constructs a copy of an atom container, i.e., a molecule that reflects the one given in the input arg...
Tool to generate random numbers and random decisions.
Possible chemical bond types an edge can represent.
Enum specifying to what extent the template's inner graph can be changed.
FREE
Inner graphs are free to change within the confines of the required AttachmentPoints.
FIXED
Inner graphs are effectively equivalent to the Fragment class, as no change in the inner structure is...
FIXED_STRUCT
Inner graph keep the same structure, but the identify of vertices can change.
The type of building block.
Flag declaring the type of Vertex implementation.
Types of mutation defined in relation to what happens to the target vertex (i.e., the actual mutation...
DELETE
Removes the target vertex and all child vertices.
DELETELINK
Removes a vertex from a tree and merges remaining branches into the remaining trunk.
CHANGELINK
Replace the target vertex keeping all the child structure.
EXTEND
append vertices on the target vertex according to substitution probability.
CHANGEBRANCH
Replace the target vertex and any of the child vertices.
ADDLINK
Adds a vertex between two previously connected vertexes.
DELETECHAIN
Removes a vertex and all its neighbors recursively until a branching point, i.e., until a vertex that...