3
* $Date: 2006-07-30 22:10:12 +0200 (Sun, 30 Jul 2006) $
6
* Copyright (C) 2003-2007 The Chemistry Development Kit (CDK) project
8
* Contact: cdk-devel@lists.sourceforge.net
10
* This program is free software; you can redistribute it and/or
11
* modify it under the terms of the GNU Lesser General Public License
12
* as published by the Free Software Foundation; either version 2.1
13
* of the License, or (at your option) any later version.
14
* All we ask is that proper credit is given for our work, which includes
15
* - but is not limited to - adding the above copyright notice to the beginning
16
* of your source code files, and to any copyright notice that you may distribute
17
* with programs based on this work.
19
* This program is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU Lesser General Public License for more details.
24
* You should have received a copy of the GNU Lesser General Public License
25
* along with this program; if not, write to the Free Software
26
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
28
package org.openscience.cdk.tools.manipulator;
30
import java.util.ArrayList;
31
import java.util.Collections;
32
import java.util.Iterator;
33
import java.util.List;
35
import org.openscience.cdk.graph.ConnectivityChecker;
36
import org.openscience.cdk.interfaces.IAtom;
37
import org.openscience.cdk.interfaces.IAtomContainer;
38
import org.openscience.cdk.interfaces.IAtomContainerSet;
39
import org.openscience.cdk.interfaces.IBond;
40
import org.openscience.cdk.interfaces.IElectronContainer;
41
import org.openscience.cdk.interfaces.IMoleculeSet;
44
* @cdk.module standard
46
* @see ChemModelManipulator
48
public class AtomContainerSetManipulator {
50
public static int getAtomCount(IAtomContainerSet set) {
52
java.util.Iterator acs = set.atomContainers();
53
while (acs.hasNext()) {
54
count += ((IAtomContainer)acs.next()).getAtomCount();
59
public static int getBondCount(IAtomContainerSet set) {
61
java.util.Iterator acs = set.atomContainers();
62
while (acs.hasNext()) {
63
count += ((IAtomContainer)acs.next()).getBondCount();
68
public static void removeAtomAndConnectedElectronContainers(IAtomContainerSet set, IAtom atom) {
69
java.util.Iterator acs = set.atomContainers();
70
while (acs.hasNext()) {
71
IAtomContainer container = (IAtomContainer)acs.next();
72
if (container.contains(atom)) {
73
container.removeAtomAndConnectedElectronContainers(atom);
74
IMoleculeSet molecules = ConnectivityChecker.partitionIntoMolecules(container);
75
if(molecules.getAtomContainerCount()>1){
76
set.removeAtomContainer(container);
77
for(int k=0;k<molecules.getAtomContainerCount();k++){
78
set.addAtomContainer(molecules.getAtomContainer(k));
86
public static void removeElectronContainer(IAtomContainerSet set, IElectronContainer electrons) {
87
java.util.Iterator acs = set.atomContainers();
88
while (acs.hasNext()) {
89
IAtomContainer container = (IAtomContainer)acs.next();
90
if (container.contains(electrons)) {
91
container.removeElectronContainer(electrons);
92
IMoleculeSet molecules = ConnectivityChecker.partitionIntoMolecules(container);
93
if(molecules.getAtomContainerCount()>1){
94
set.removeAtomContainer(container);
95
for(int k=0;k<molecules.getAtomContainerCount();k++){
96
set.addAtomContainer(molecules.getMolecule(k));
105
* Returns all the AtomContainer's of a MoleculeSet.
107
public static List getAllAtomContainers(IAtomContainerSet set) {
108
List atomContainerList = new ArrayList();
109
Iterator acs = set.atomContainers();
110
while(acs.hasNext()){
111
atomContainerList.add((IAtomContainer)acs.next());
113
return atomContainerList;
117
* @return The summed charges of all atoms in this set.
119
public static double getTotalCharge(IAtomContainerSet set) {
121
for (int i = 0; i < set.getAtomContainerCount(); i++) {
122
int thisCharge = AtomContainerManipulator.getTotalFormalCharge(set.getAtomContainer(i));
123
double stoich = set.getMultiplier(i);
124
charge += stoich * thisCharge;
130
* @return The summed formal charges of all atoms in this set.
132
public static double getTotalFormalCharge(IAtomContainerSet set) {
134
for (int i = 0; i < set.getAtomContainerCount(); i++) {
135
int thisCharge = AtomContainerManipulator.getTotalFormalCharge(set.getAtomContainer(i));
136
double stoich = set.getMultiplier(i);
137
charge += stoich * thisCharge;
143
* @return The summed implicit hydrogens of all atoms in this set.
145
public static int getTotalHydrogenCount(IAtomContainerSet set) {
147
for (int i = 0; i < set.getAtomContainerCount(); i++) {
148
hCount += AtomContainerManipulator.getTotalHydrogenCount(set.getAtomContainer(i));
153
public static List getAllIDs(IAtomContainerSet set) {
154
List idList = new ArrayList();
156
if (set.getID() != null) idList.add(set.getID());
157
for (int i = 0; i < set.getAtomContainerCount(); i++) {
158
idList.addAll(AtomContainerManipulator.getAllIDs(set.getAtomContainer(i)));
164
public static void setAtomProperties(IAtomContainerSet set, Object propKey, Object propVal) {
166
for (int i = 0; i < set.getAtomContainerCount(); i++) {
167
AtomContainerManipulator.setAtomProperties(set.getAtomContainer(i), propKey, propVal);
172
public static IAtomContainer getRelevantAtomContainer(IAtomContainerSet containerSet, IAtom atom) {
173
java.util.Iterator acs = containerSet.atomContainers();
174
while (acs.hasNext()) {
175
IAtomContainer ac = (IAtomContainer)acs.next();
176
if (ac.contains(atom)) {
183
public static IAtomContainer getRelevantAtomContainer(IAtomContainerSet containerSet, IBond bond) {
184
java.util.Iterator acs = containerSet.atomContainers();
185
while (acs.hasNext()) {
186
IAtomContainer ac = (IAtomContainer)acs.next();
187
if (ac.contains(bond)) {
195
* Does not recursively return the contents of the AtomContainer.
200
public static List getAllChemObjects(IAtomContainerSet set) {
201
ArrayList list = new ArrayList();
203
java.util.Iterator acs = set.atomContainers();
204
while (acs.hasNext()) {
205
list.add((IAtomContainer)acs.next());
211
* <p>Sorts the IAtomContainers in the given IAtomContainerSet by the following
212
* criteria with decreasing priority:</p>
214
* <li>Compare atom count
215
* <li>Compare molecular weight (heavy atoms only)
216
* <li>Compare bond count
217
* <li>Compare sum of bond orders (heavy atoms only)
219
* <p>If no difference can be found with the above criteria, the IAtomContainers are
220
* considered equal.</p>
222
public static void sort(IAtomContainerSet atomContainerSet) {
223
List atomContainerList = AtomContainerSetManipulator.getAllAtomContainers(atomContainerSet);
224
Collections.sort(atomContainerList, new AtomContainerComparator());
225
atomContainerSet.removeAllAtomContainers();
226
Iterator iterator = atomContainerList.iterator();
227
while (iterator.hasNext())
228
atomContainerSet.addAtomContainer((IAtomContainer) iterator.next());