/
PartialFilledStructureMerger.java
165 lines (153 loc) · 5.63 KB
/
PartialFilledStructureMerger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* $Revision$ $Author$ $Date$
*
* Copyright (C) 1997-2007 The CDK project
*
* Contact: cdk-devel@lists.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
* All we ask is that proper credit is given for our work, which includes
* - but is not limited to - adding the above copyright notice to the beginning
* of your source code files, and to any copyright notice that you may distribute
* with programs based on this work.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.openscience.cdk.structgen.stochastic;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IAtomContainerSet;
import org.openscience.cdk.tools.ILoggingTool;
import org.openscience.cdk.tools.LoggingToolFactory;
import org.openscience.cdk.tools.SaturationChecker;
import org.openscience.cdk.tools.manipulator.AtomContainerSetManipulator;
import org.openscience.cdk.tools.manipulator.BondManipulator;
/**
* Randomly generates a single, connected, correctly bonded structure from
* a number of fragments.
* <p>Assign hydrogen counts to each heavy atom. The hydrogens should not be
* in the atom pool but should be assigned implicitly to the heavy atoms in
* order to reduce computational cost.
*
* @author steinbeck
* @cdk.created 2001-09-04
* @cdk.module structgen
* @cdk.githash
*/
public class PartialFilledStructureMerger {
private ILoggingTool logger =
LoggingToolFactory.createLoggingTool(PartialFilledStructureMerger.class);
SaturationChecker satCheck;
/**
* Constructor for the PartialFilledStructureMerger object.
*/
public PartialFilledStructureMerger()
{
satCheck = new SaturationChecker();
}
/**
* Randomly generates a single, connected, correctly bonded structure from
* a number of fragments.
*
* @param atomContainers The fragments to generate for. IMPORTANT: The AtomContainers in the set must be connected.
* If an AtomContainer is disconnected, the generated structure is not guaranteed to be connected.
* @return
* @throws CDKException
*/
public IAtomContainerSet generate(IAtomContainerSet atomContainers) throws CDKException
{
int iteration = 0;
boolean structureFound = false;
do
{
iteration++;
boolean bondFormed;
do{
bondFormed=false;
for(IAtomContainer ac : atomContainers.atomContainers()){
for(IAtom atom : ac.atoms()){
if (!satCheck.isSaturated(atom, ac))
{
IAtom partner = getAnotherUnsaturatedNode(atom, atomContainers);
if (partner != null)
{
IAtomContainer toadd = AtomContainerSetManipulator.getRelevantAtomContainer(atomContainers, partner);
double cmax1 = satCheck.getCurrentMaxBondOrder(atom, ac);
double cmax2 = satCheck.getCurrentMaxBondOrder(partner, toadd);
double max = Math.min(cmax1, cmax2);
double order = Math.min(Math.max(1.0, max), 3.0);//(double)Math.round(Math.random() * max)
logger.debug("cmax1, cmax2, max, order: " + cmax1 + ", " + cmax2 + ", " + max + ", " + order);
if(toadd!=ac){
atomContainers.removeAtomContainer(toadd);
ac.add(toadd);
}
ac.addBond(
ac.getBuilder().newBond(
atom, partner, BondManipulator.createBondOrder(order)
)
);
bondFormed = true;
}
}
}
}
}while(bondFormed);
if (atomContainers.getAtomContainerCount()==1 && satCheck.allSaturated(atomContainers.getAtomContainer(0)))
{
structureFound = true;
}
} while (!structureFound && iteration < 5);
if (atomContainers.getAtomContainerCount()==1 && satCheck.allSaturated(atomContainers.getAtomContainer(0)))
{
structureFound = true;
}
if(!structureFound)
throw new CDKException("Could not combine the fragments to combine a valid, satured structure");
return atomContainers;
}
/**
* Gets a randomly selected unsaturated atom from the set. If there are any, it will be from another
* container than exclusionAtom.
*
* @return The unsaturated atom.
*/
private IAtom getAnotherUnsaturatedNode(IAtom exclusionAtom, IAtomContainerSet atomContainers) throws CDKException
{
IAtom atom;
for(IAtomContainer ac : atomContainers.atomContainers()){
if(!ac.contains(exclusionAtom)){
int next = 0;//(int) (Math.random() * ac.getAtomCount());
for (int f = next; f < ac.getAtomCount(); f++)
{
atom = ac.getAtom(f);
if (!satCheck.isSaturated(atom, ac) && exclusionAtom != atom && !ac.getConnectedAtomsList(exclusionAtom).contains(atom))
{
return atom;
}
}
}
}
for(IAtomContainer ac : atomContainers.atomContainers()){
int next = ac.getAtomCount();//(int) (Math.random() * ac.getAtomCount());
for (int f = 0; f < next; f++)
{
atom = ac.getAtom(f);
if (!satCheck.isSaturated(atom, ac) && exclusionAtom != atom && !ac.getConnectedAtomsList(exclusionAtom).contains(atom))
{
return atom;
}
}
}
return null;
}
}