1package denoptim.fragmenter;
3import static org.junit.jupiter.api.Assertions.assertEquals;
4import static org.junit.jupiter.api.Assertions.assertTrue;
7import java.util.ArrayList;
10import javax.vecmath.Matrix4d;
11import javax.vecmath.Point3d;
12import javax.vecmath.Vector3d;
14import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
15import org.junit.jupiter.api.Test;
16import org.openscience.cdk.Atom;
17import org.openscience.cdk.PseudoAtom;
18import org.openscience.cdk.interfaces.IAtom;
19import org.openscience.cdk.interfaces.IAtomContainer;
20import org.openscience.cdk.interfaces.IBond;
21import org.openscience.cdk.interfaces.IChemObjectBuilder;
22import org.openscience.cdk.silent.SilentChemObjectBuilder;
24import denoptim.files.FileFormat;
25import denoptim.fragmenter.FragmentClusterer.DistanceAsRMSD;
26import denoptim.graph.APClass;
27import denoptim.graph.AttachmentPoint;
28import denoptim.graph.Fragment;
29import denoptim.graph.Vertex;
30import denoptim.graph.Vertex.BBType;
31import denoptim.io.DenoptimIO;
32import denoptim.programs.fragmenter.FragmenterParameters;
33import denoptim.utils.Randomizer;
44 private IChemObjectBuilder
builder = SilentChemObjectBuilder.getInstance();
58 double[] pA =
new double[] {0,0,0, 0,0,0};
59 double[] pB =
new double[] {0,0,0, 1,0,0};
60 double[] pC =
new double[] {0,0,0, 2,0,0};
63 double val = measure.
compute(pA, pB);
64 assertTrue(Math.abs(val - 0.5) < 0.0001);
65 assertTrue(Math.abs(measure.
compute(pB, pA) - 0.5) < 0.0001);
66 assertTrue(Math.abs(measure.
compute(pA, pA) - 0) < 0.0001);
67 assertTrue(Math.abs(measure.
compute(pB, pB) - 0) < 0.0001);
69 assertTrue(Math.abs(measure.
compute(pA, pC) - 1.0) < 0.0001);
70 assertTrue(Math.abs(measure.
compute(pC, pA) - 1.0) < 0.0001);
72 assertTrue(Math.abs(measure.
compute(pB, pC) - 0.5) < 0.0001);
73 assertTrue(Math.abs(measure.
compute(pC, pB) - 0.5) < 0.0001);
75 pA =
new double[] {0,0,0, 0,0,0};
76 pB =
new double[] {0,0,0, 0,1,0};
77 pC =
new double[] {0,0,0, 0,2,0};
78 assertTrue(Math.abs(measure.
compute(pA, pB) - 0.5) < 0.0001);
79 assertTrue(Math.abs(measure.
compute(pB, pA) - 0.5) < 0.0001);
80 assertTrue(Math.abs(measure.
compute(pA, pA) - 0) < 0.0001);
81 assertTrue(Math.abs(measure.
compute(pB, pB) - 0) < 0.0001);
82 assertTrue(Math.abs(measure.
compute(pA, pC) - 1.0) < 0.0001);
83 assertTrue(Math.abs(measure.
compute(pC, pA) - 1.0) < 0.0001);
84 assertTrue(Math.abs(measure.
compute(pB, pC) - 0.5) < 0.0001);
85 assertTrue(Math.abs(measure.
compute(pC, pB) - 0.5) < 0.0001);
87 pA =
new double[] {0,0,0, 0,0,0};
88 pB =
new double[] {0,0,0, 0,0,1};
89 pC =
new double[] {0,0,0, 0,0,2};
90 assertTrue(Math.abs(measure.
compute(pA, pB) - 0.5) < 0.0001);
91 assertTrue(Math.abs(measure.
compute(pB, pA) - 0.5) < 0.0001);
92 assertTrue(Math.abs(measure.
compute(pA, pA) - 0) < 0.0001);
93 assertTrue(Math.abs(measure.
compute(pB, pB) - 0) < 0.0001);
94 assertTrue(Math.abs(measure.
compute(pA, pC) - 1.0) < 0.0001);
95 assertTrue(Math.abs(measure.
compute(pC, pA) - 1.0) < 0.0001);
96 assertTrue(Math.abs(measure.
compute(pB, pC) - 0.5) < 0.0001);
97 assertTrue(Math.abs(measure.
compute(pC, pB) - 0.5) < 0.0001);
99 pA =
new double[] {0,0,0, 0,0,0};
100 pB =
new double[] {0,0,1, 0,0,0};
101 pC =
new double[] {0,0,2, 0,0,0};
102 assertTrue(Math.abs(measure.
compute(pA, pB) - 0.5) < 0.0001);
103 assertTrue(Math.abs(measure.
compute(pB, pA) - 0.5) < 0.0001);
104 assertTrue(Math.abs(measure.
compute(pA, pA) - 0) < 0.0001);
105 assertTrue(Math.abs(measure.
compute(pB, pB) - 0) < 0.0001);
106 assertTrue(Math.abs(measure.
compute(pA, pC) - 1.0) < 0.0001);
107 assertTrue(Math.abs(measure.
compute(pC, pA) - 1.0) < 0.0001);
108 assertTrue(Math.abs(measure.
compute(pB, pC) - 0.5) < 0.0001);
109 assertTrue(Math.abs(measure.
compute(pC, pB) - 0.5) < 0.0001);
111 pA =
new double[] {0,0,0, 1,0,0};
112 pB =
new double[] {0,0,0, 6,0,0};
113 assertTrue(Math.abs(measure.
compute(pA, pB) - 2.5) < 0.0001);
114 assertTrue(Math.abs(measure.
compute(pB, pA) - 2.5) < 0.0001);
122 double[] center =
new double[] {
129 double[] center2 =
new double[] {
136 assertTrue(Math.abs(s1.getMean()-s2.getMean()) < 0.05);
138 center =
new double[] {
160 center2 =
new double[] {
182 assertTrue(Math.abs(s3.getMean()-s4.getMean()) < 0.05);
192 boolean manualDebug =
false;
199 List<ClusterableFragment> sample =
new ArrayList<ClusterableFragment>();
200 Point3d[] pointsA =
new Point3d[] {
201 new Point3d(-0.4574,-0.0273,0.3953),
202 new Point3d(1.2914,-0.0103,-0.0437),
203 new Point3d(-1.0737,-1.1960,-0.1490),
204 new Point3d(-0.5595,-0.0346,1.4805),
205 new Point3d(-1.0796,1.1129,-0.1241)};
206 for (
int i=0; i<10; i++)
208 IAtomContainer mol =
builder.newAtomContainer();
214 Matrix4d tM =
new Matrix4d();
218 tM.setTranslation(
new Vector3d(i,i,i));
224 mol.addAtom(
new Atom(
"C", pA));
225 mol.addAtom(
new Atom(
"H", pB));
226 mol.addAtom(
new Atom(
"O", pC));
227 mol.addBond(0,1,IBond.Order.SINGLE);
228 mol.addBond(0,2,IBond.Order.SINGLE);
239 ArrayList<IAtomContainer> mols =
new ArrayList<IAtomContainer>();
240 for (
int i=0; i<sample.size(); i++)
242 mols.add(
getMol(sample.get(i).getOriginalFragment()));
261 ArrayList<IAtomContainer> mols =
new ArrayList<IAtomContainer>();
268 ArrayList<IAtomContainer> mols2 =
new ArrayList<IAtomContainer>();
269 for (
int i=0; i<sample.size(); i++)
271 mols2.add(
getMol(sample.get(i).getOriginalFragment()));
276 Point3d[] pointsB =
new Point3d[] {
277 new Point3d(0.4574,-0.0273,0.3953),
278 new Point3d(-1.2914,-0.0103,-0.0437),
279 new Point3d(1.0737,-1.1960,-0.1490),
280 new Point3d(0.5595,-0.0346,1.4805),
281 new Point3d(1.0796,1.1129,-0.1241)};
282 for (
int i=0; i<5; i++)
284 IAtomContainer mol =
builder.newAtomContainer();
288 mol.addBond(0,1,IBond.Order.SINGLE);
289 mol.addBond(0,2,IBond.Order.SINGLE);
302 Point3d[] pointsC =
new Point3d[] {
307 new Point3d(0,-2,0)};
308 for (
int i=0; i<5; i++)
310 IAtomContainer mol =
builder.newAtomContainer();
314 mol.addBond(0,1,IBond.Order.SINGLE);
315 mol.addBond(0,2,IBond.Order.SINGLE);
328 Point3d[] pointsD =
new Point3d[] {
330 new Point3d(6.5,0,0),
334 for (
int i=0; i<5; i++)
336 IAtomContainer mol =
builder.newAtomContainer();
340 mol.addBond(0,1,IBond.Order.SINGLE);
341 mol.addBond(0,2,IBond.Order.SINGLE);
354 Point3d[] pointsE =
new Point3d[] {
356 new Point3d(6.5,0,0),
360 for (
int i=0; i<5; i++)
362 IAtomContainer mol =
builder.newAtomContainer();
366 mol.addBond(0,1,IBond.Order.SINGLE);
367 mol.addBond(0,2,IBond.Order.SINGLE);
380 Point3d[] pointsF =
new Point3d[] {
382 new Point3d(6.5,0,0),
386 for (
int i=0; i<5; i++)
388 IAtomContainer mol =
builder.newAtomContainer();
392 mol.addBond(0,1,IBond.Order.SINGLE);
393 mol.addBond(0,2,IBond.Order.SINGLE);
407 List<DynamicCentroidCluster> clusters = fc.
getClusters();
409 for (
int iCluster=0; iCluster<centroids.size(); iCluster++)
411 assertEquals(trnsClusters.get(iCluster).size(),
412 clusters.get(iCluster).getPoints().size(),
413 "Number of members in the cluster and transformed cluster");
414 int numMembers = trnsClusters.get(iCluster).size();
415 for (
int iMember=0; iMember<numMembers; iMember++)
417 Fragment centroid = centroids.get(iCluster);
419 iCluster).getPoints().get(iMember);
420 Fragment trnsClustFrag = clusters.get(
421 iCluster).getPoints().get(iMember).getTransformedCopy();
422 Fragment trnsFrag = trnsClusters.get(iCluster).get(iMember);
425 for (IAtom atm : centroid.
atoms())
428 Point3d trnsFragPt = trnsFrag.
getAtom(iPoint).getPoint3d();
429 Point3d trnsCopyPt = trnsClustFrag.
getAtom(iPoint).getPoint3d();
432 assertTrue(trnsFragPt.distance(trnsCopyPt) < 0.001);
433 assertTrue(trnsFragPt.distance(clustFragPt) < 0.001);
441 Point3d centroidPt = atm.getPoint3d();
442 assertTrue(centroidPt.distance(trnsFragPt) < noise*1.5);
456 assertTrue(trnsFragPt.distance(trnsCopyPt) < 0.001);
457 assertTrue(trnsFragPt.distance(clustFragPt) < 0.001);
459 Point3d centroidPt = ap.getDirectionVector();
460 assertTrue(centroidPt.distance(trnsFragPt) < noise*1.5);
472 ArrayList<Vertex> lstVrtx =
new ArrayList<Vertex>();
474 lstVrtx.add(cf.getOriginalFragment());
480 ArrayList<IAtomContainer> mols =
new ArrayList<IAtomContainer>();
483 mols.add(
getMol(cf.getTransformedCopy()));
488 ArrayList<IAtomContainer> molsTrs =
new ArrayList<IAtomContainer>();
495 IAtomContainer center =
getMol(centroids.get(i));
509 IAtomContainer mol =
builder.newAtomContainer();
510 for (IAtom a : frag.
atoms())
511 mol.addAtom(
new Atom(a.getSymbol(),a.getPoint3d()));
513 mol.addAtom(
new PseudoAtom(
"W",ap.getDirectionVector()));
526 List<ClusterableFragment> sample =
new ArrayList<ClusterableFragment>();
528 Point3d[] points =
new Point3d[] {
531 for (
int i=0; i<5; i++)
533 IAtomContainer mol =
builder.newAtomContainer();
552 points =
new Point3d[] {
554 new Point3d(0,0,-2)};
555 for (
int i=0; i<5; i++)
557 IAtomContainer mol =
builder.newAtomContainer();
570 for (
int k=2; k<10; k++)
572 points =
new Point3d[] {
575 for (
int i=0; i<5; i++)
577 IAtomContainer mol =
builder.newAtomContainer();
610 return new Point3d(p.x+noise.x, p.y+noise.y, p.z+noise.z);
Represents a fragment that can be clustered based on the 3*N coordinate of atoms and attachment point...
double[] allCoords
Ordered list of coordinated reflecting the ordered list of atoms/APs.
void setNaturalNodeOrder()
Define the node order from the list of atoms and attachment points.
static Point3d[] convertToPointArray(double[] coords)
Converts an array of 3*N-dimensional coordinates into an array of 3-dimensional points assuming coord...
Distance in terms of RMSD between sets of 3D points expressed as a single vector of coordinates [x1,...
double compute(double[] coordsA, double[] coordsB)
List< Fragment > getClusterCentroids()
Once the clustering is done, this method return the list of cluster centroids.
static SummaryStatistics getRMSDStatsOfNoisyDistorsions(double[] center, int sampleSize, double maxNoise)
Computes statistics for a unimodal, normally noise-distorted population of points generated by distor...
void cluster()
Runs the clustering algorithm:
List< DynamicCentroidCluster > getClusters()
Once the clustering is done, this method return the list of resulting clusters.
List< List< Fragment > > getTransformedClusters()
Once the clustering is done, this method return the list of clusters.
Unit test for fragmenter's tools.
Point3d getNoisyPoint(Point3d p, double noiseMagnitude)
The noise magnitude is to be interpreted as half of the overall magnitude.
IAtomContainer getMol(Fragment frag)
Converts the fragment into a collection of atoms where attachment points are converted into atoms tha...
void testGetRMSDStatsOfNoisyDistorsions()
IChemObjectBuilder builder
Private builder of atom containers.
Randomizer rng
Random number generator.
void testDistanceAsRMSD()
static APClass make(String ruleAndSubclass)
Creates an APClass if it does not exist already, or returns the reference to the existing instance.
An attachment point (AP) is a possibility to attach a Vertex onto the vertex holding the AP (i....
Point3d getDirectionVector()
Returns the end of the direction vector.
Class representing a continuously connected portion of chemical object holding attachment points.
void addAP(int atomPositionNumber)
Adds an attachment point with a dummy APClass.
List< AttachmentPoint > getAttachmentPoints()
Iterable< IAtom > atoms()
IAtom getAtom(int number)
AttachmentPoint getAP(int i)
Get attachment point i on this vertex.
Utility methods for input/output.
static File writeVertexesToFile(File file, FileFormat format, List< Vertex > vertexes)
Writes vertexes to file.
static void writeSDFFile(String fileName, IAtomContainer mol)
Writes IAtomContainer to SDF file.
Logger startConsoleLogger(String loggerIdentifier)
Starts a program-specific logger that prints to System.err stream.
void setVerbosity(int l)
Set the level of verbosity.
Parameters controlling execution of the fragmenter.
Tool to generate random numbers and random decisions.
Point3d getNoisyPoint(double maxAbsValue)
Returns a point in three-dimensional space with a random set of coordinates, the absolute value of wh...
The type of building block.