~ubuntu-branches/ubuntu/trusty/cdk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/tools/manipulator/AtomContainerSetManipulator.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $RCSfile$
 
2
 * $Author: egonw $ 
 
3
 * $Date: 2006-07-30 22:10:12 +0200 (Sun, 30 Jul 2006) $
 
4
 * $Revision: 6706 $
 
5
 * 
 
6
 * Copyright (C) 2003-2007  The Chemistry Development Kit (CDK) project
 
7
 * 
 
8
 * Contact: cdk-devel@lists.sourceforge.net
 
9
 * 
 
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.
 
18
 * 
 
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.
 
23
 * 
 
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.
 
27
 *  */
 
28
package org.openscience.cdk.tools.manipulator;
 
29
 
 
30
import java.util.ArrayList;
 
31
import java.util.Collections;
 
32
import java.util.Iterator;
 
33
import java.util.List;
 
34
 
 
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;
 
42
 
 
43
/**
 
44
 * @cdk.module standard
 
45
 *
 
46
 * @see ChemModelManipulator
 
47
 */
 
48
public class AtomContainerSetManipulator {
 
49
    
 
50
        public static int getAtomCount(IAtomContainerSet set) {
 
51
                int count = 0;
 
52
                java.util.Iterator acs = set.atomContainers();
 
53
        while (acs.hasNext()) {
 
54
                count += ((IAtomContainer)acs.next()).getAtomCount();
 
55
        }
 
56
        return count;
 
57
        }
 
58
        
 
59
        public static int getBondCount(IAtomContainerSet set) {
 
60
                int count = 0;
 
61
                java.util.Iterator acs = set.atomContainers();
 
62
        while (acs.hasNext()) {
 
63
                count += ((IAtomContainer)acs.next()).getBondCount();
 
64
        }
 
65
        return count;
 
66
        }
 
67
        
 
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));
 
79
                        }
 
80
                }
 
81
                return;
 
82
            }
 
83
        }
 
84
    }
 
85
    
 
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));
 
97
                        }
 
98
                }
 
99
                return;
 
100
            }
 
101
        }
 
102
    }
 
103
    
 
104
        /**
 
105
     * Returns all the AtomContainer's of a MoleculeSet.
 
106
     */
 
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());
 
112
        }
 
113
        return atomContainerList;
 
114
    }
 
115
        
 
116
        /**
 
117
         * @return The summed charges of all atoms in this set.
 
118
         */
 
119
        public static double getTotalCharge(IAtomContainerSet set) {
 
120
                double charge = 0;
 
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;
 
125
                }
 
126
                return charge;
 
127
        }
 
128
        
 
129
        /**
 
130
         * @return The summed formal charges of all atoms in this set.
 
131
         */
 
132
        public static double getTotalFormalCharge(IAtomContainerSet set) {
 
133
                int charge = 0;
 
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;
 
138
                }
 
139
                return charge;
 
140
        }
 
141
        
 
142
        /**
 
143
         * @return The summed implicit hydrogens of all atoms in this set.
 
144
         */
 
145
        public static int getTotalHydrogenCount(IAtomContainerSet set) {
 
146
                int hCount = 0;
 
147
                for (int i = 0; i < set.getAtomContainerCount(); i++) {
 
148
                        hCount += AtomContainerManipulator.getTotalHydrogenCount(set.getAtomContainer(i));
 
149
                }
 
150
                return hCount;
 
151
        }
 
152
        
 
153
    public static List getAllIDs(IAtomContainerSet set) {
 
154
        List idList = new ArrayList();
 
155
        if (set != null) {
 
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)));
 
159
            }
 
160
        }
 
161
        return idList;
 
162
    }
 
163
    
 
164
    public static void setAtomProperties(IAtomContainerSet set, Object propKey, Object propVal) {
 
165
        if (set != null) {
 
166
            for (int i = 0; i < set.getAtomContainerCount(); i++) {
 
167
                AtomContainerManipulator.setAtomProperties(set.getAtomContainer(i), propKey, propVal);
 
168
            }
 
169
        }
 
170
    }
 
171
 
 
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)) {
 
177
                return ac;
 
178
            }
 
179
        }
 
180
        return null;
 
181
    }
 
182
 
 
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)) {
 
188
                return ac;
 
189
            }
 
190
        }
 
191
        return null;
 
192
    }
 
193
    
 
194
    /**
 
195
     * Does not recursively return the contents of the AtomContainer.
 
196
     * 
 
197
     * @param set
 
198
     * @return
 
199
     */
 
200
    public static List getAllChemObjects(IAtomContainerSet set) {
 
201
        ArrayList list = new ArrayList();
 
202
        list.add(set);
 
203
        java.util.Iterator acs = set.atomContainers();
 
204
        while (acs.hasNext()) {
 
205
            list.add((IAtomContainer)acs.next());
 
206
        }
 
207
        return list;
 
208
    }
 
209
        
 
210
    /**
 
211
     * <p>Sorts the IAtomContainers in the given IAtomContainerSet by the following
 
212
     * criteria with decreasing priority:</p>
 
213
     * <ul>
 
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)
 
218
     * </ul>
 
219
     * <p>If no difference can be found with the above criteria, the IAtomContainers are
 
220
     * considered equal.</p>
 
221
     */
 
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());
 
229
    }
 
230
    
 
231
}
 
232