~ubuntu-branches/ubuntu/maverick/cdk/maverick

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/reaction/type/RearrangementCation1Reaction.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
/*
 
2
 *  $RCSfile$
 
3
 *  $Author: egonw $
 
4
 *  $Date: 2006-03-29 10:27:08 +0200 (Wed, 29 Mar 2006) $
 
5
 *  $Revision: 5855 $
 
6
 *
 
7
 *  Copyright (C) 2006-2007  Miguel Rojas <miguel.rojas@uni-koeln.de>
 
8
 *
 
9
 *  Contact: cdk-devel@lists.sourceforge.net
 
10
 *
 
11
 *  This program is free software; you can redistribute it and/or
 
12
 *  modify it under the terms of the GNU Lesser General Public License
 
13
 *  as published by the Free Software Foundation; either version 2.1
 
14
 *  of the License, or (at your option) any later version.
 
15
 *
 
16
 *  This program is distributed in the hope that it will be useful,
 
17
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
 *  GNU Lesser General Public License for more details.
 
20
 *
 
21
 *  You should have received a copy of the GNU Lesser General Public License
 
22
 *  along with this program; if not, write to the Free Software
 
23
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 
24
 */
 
25
package org.openscience.cdk.reaction.type;
 
26
 
 
27
 
 
28
import java.util.List;
 
29
 
 
30
import org.openscience.cdk.CDKConstants;
 
31
import org.openscience.cdk.DefaultChemObjectBuilder;
 
32
import org.openscience.cdk.exception.CDKException;
 
33
import org.openscience.cdk.interfaces.IAtom;
 
34
import org.openscience.cdk.interfaces.IAtomContainer;
 
35
import org.openscience.cdk.interfaces.IBond;
 
36
import org.openscience.cdk.interfaces.ILonePair;
 
37
import org.openscience.cdk.interfaces.IMapping;
 
38
import org.openscience.cdk.interfaces.IMolecule;
 
39
import org.openscience.cdk.interfaces.IMoleculeSet;
 
40
import org.openscience.cdk.interfaces.IReaction;
 
41
import org.openscience.cdk.interfaces.IReactionSet;
 
42
import org.openscience.cdk.interfaces.IRingSet;
 
43
import org.openscience.cdk.reaction.IReactionProcess;
 
44
import org.openscience.cdk.reaction.ReactionSpecification;
 
45
import org.openscience.cdk.ringsearch.AllRingsFinder;
 
46
import org.openscience.cdk.tools.LoggingTool;
 
47
import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;
 
48
 
 
49
/**
 
50
 * <p>IReactionProcess which participate in movement resonance. 
 
51
 * This reaction could be represented as [A+]-B| => A=[B+]. Due to 
 
52
 * deficiency of charge of the atom A, the lone pair electron of the atom A is 
 
53
 * desplaced.</p>
 
54
 * <p>Make sure that the molecule has the corresponend lone pair electrons
 
55
 * for each atom. You can use the method: <pre> LonePairElectronChecker </pre>
 
56
 * 
 
57
 * <pre>
 
58
 *  IMoleculeSet setOfReactants = DefaultChemObjectBuilder.getInstance().newMoleculeSet();
 
59
 *  setOfReactants.addMolecule(new Molecule());
 
60
 *  IReactionProcess type = new RearrangementCation1Reaction();
 
61
 *  Object[] params = {Boolean.FALSE};
 
62
    type.setParameters(params);
 
63
 *  IReactionSet setOfReactions = type.initiate(setOfReactants, null);
 
64
 *  </pre>
 
65
 * 
 
66
 * <p>We have the possibility to localize the reactive center. Good method if you
 
67
 * want to localize the reaction in a fixed point</p>
 
68
 * <pre>atoms[0].setFlag(CDKConstants.REACTIVE_CENTER,true);</pre>
 
69
 * <p>Moreover you must put the parameter Boolean.TRUE</p>
 
70
 * <p>If the reactive center is not localized then the reaction process will
 
71
 * try to find automatically the posible reactive center.</p>
 
72
 * 
 
73
 * 
 
74
 * @author         Miguel Rojas
 
75
 * 
 
76
 * @cdk.created    2006-05-05
 
77
 * @cdk.module     reaction
 
78
 * @cdk.set        reaction-types
 
79
 * 
 
80
 **/
 
81
public class RearrangementCation1Reaction implements IReactionProcess{
 
82
        private LoggingTool logger;
 
83
        private boolean hasActiveCenter;
 
84
        private static final int BONDTOFLAG1 = 8;
 
85
 
 
86
        /**
 
87
         * Constructor of the RearrangementCation1Reaction object
 
88
         *
 
89
         */
 
90
        public RearrangementCation1Reaction(){
 
91
                logger = new LoggingTool(this);
 
92
        }
 
93
        /**
 
94
         *  Gets the specification attribute of the RearrangementCation1Reaction object
 
95
         *
 
96
         *@return    The specification value
 
97
         */
 
98
        public ReactionSpecification getSpecification() {
 
99
                return new ReactionSpecification(
 
100
                                "http://almost.cubic.uni-koeln.de/jrg/Members/mrc/reactionDict/reactionDict#RearrangementCation1Reaction",
 
101
                                this.getClass().getName(),
 
102
                                "$Id: RearrangementCation1Reaction.java,v 1.6 2006/04/01 08:26:47 mrc Exp $",
 
103
                                "The Chemistry Development Kit");
 
104
        }
 
105
        
 
106
        /**
 
107
         *  Sets the parameters attribute of the RearrangementCation1Reaction object
 
108
         *
 
109
         *@param  params            The parameter is if the molecule has already fixed the center active or not. It 
 
110
         *                                                      should be set before to inize the reaction with a setFlag:  CDKConstants.REACTIVE_CENTER
 
111
         *@exception  CDKException  Description of the Exception
 
112
         */
 
113
        public void setParameters(Object[] params) throws CDKException {
 
114
                if (params.length > 1) {
 
115
                        throw new CDKException("RearrangementCation1Reaction only expects one parameter");
 
116
                }
 
117
                if (!(params[0] instanceof Boolean)) {
 
118
                        throw new CDKException("The parameter 1 must be of type boolean");
 
119
                }
 
120
                hasActiveCenter = ((Boolean) params[0]).booleanValue();
 
121
        }
 
122
 
 
123
 
 
124
        /**
 
125
         *  Gets the parameters attribute of the RearrangementCation1Reaction object
 
126
         *
 
127
         *@return    The parameters value
 
128
         */
 
129
        public Object[] getParameters() {
 
130
                Object[] params = new Object[1];
 
131
                params[0] = new Boolean (hasActiveCenter);
 
132
                return params;
 
133
        }
 
134
        
 
135
        /**
 
136
         *  Initiate process.
 
137
         *  It is needed to call the addExplicitHydrogensToSatisfyValency
 
138
         *  from the class tools.HydrogenAdder.
 
139
         *
 
140
         *@param  reactants         reactants of the reaction.
 
141
         *@param  agents            agents of the reaction (Must be in this case null).
 
142
         *
 
143
         *@exception  CDKException  Description of the Exception
 
144
         */
 
145
        public IReactionSet initiate(IMoleculeSet reactants, IMoleculeSet agents) throws CDKException{
 
146
 
 
147
                logger.debug("initiate reaction: RearrangementCation1Reaction");
 
148
                
 
149
                if (reactants.getMoleculeCount() != 1) {
 
150
                        throw new CDKException("RearrangementCation1Reaction only expects one reactant");
 
151
                }
 
152
                if (agents != null) {
 
153
                        throw new CDKException("RearrangementCation1Reaction don't expects agents");
 
154
                }
 
155
                
 
156
                IReactionSet setOfReactions = DefaultChemObjectBuilder.getInstance().newReactionSet();
 
157
                IMolecule reactant = reactants.getMolecule(0);
 
158
                /* if the parameter hasActiveCenter is not fixed yet, set the active centers*/
 
159
                if(!hasActiveCenter){
 
160
                        setActiveCenters(reactant);
 
161
                }
 
162
                
 
163
                /*control of the volume of number of charge - it will be made possible if
 
164
                 * if the atoms[i] is the only atom with charge*/
 
165
                int negCharge = AtomContainerManipulator.getTotalNegativeFormalCharge((IAtomContainer)reactant);
 
166
                int posCharge = AtomContainerManipulator.getTotalPositiveFormalCharge((IAtomContainer)reactant);
 
167
                if(posCharge > 1 || Math.abs(negCharge) > 1)
 
168
                        return setOfReactions;
 
169
                
 
170
                IAtom atomi = null;
 
171
                IBond bondj = null;
 
172
                for(int i = 0 ; i < reactant.getAtomCount() ; i++){
 
173
                        atomi = reactant.getAtom(i);
 
174
                        if(atomi.getFlag(CDKConstants.REACTIVE_CENTER) && atomi.getFormalCharge() == 1 ){
 
175
                                
 
176
                                
 
177
                                java.util.List bonds = reactant.getConnectedBondsList(atomi);
 
178
                                
 
179
                                for(int j = 0 ; j < bonds.size() ; j++){
 
180
                                        bondj = (IBond)bonds.get(j);
 
181
                                        if(bondj.getFlag(CDKConstants.REACTIVE_CENTER) && bondj.getOrder() == 1.0){
 
182
                                                IAtom atom1 = bondj.getConnectedAtom(atomi);
 
183
                                                List lp = reactant.getConnectedLonePairsList(atom1);
 
184
                                                if(atom1.getFlag(CDKConstants.REACTIVE_CENTER) && lp.size() > 0 )
 
185
                                                        if(reactant.getConnectedSingleElectronsCount(atom1) == 0){
 
186
                                                        
 
187
                                                                IReaction reaction = DefaultChemObjectBuilder.getInstance().newReaction();
 
188
                                                                reaction.addReactant(reactant);
 
189
                                                                
 
190
 
 
191
                                                                cleanFlagBOND(reactants.getMolecule(0));
 
192
                                                        
 
193
                                                        /* positions atoms and bonds */
 
194
                                                        int atom0P = reactant.getAtomNumber(atomi);
 
195
                                                        bondj.setFlag(BONDTOFLAG1, true);
 
196
                                                        int atom1P = reactant.getAtomNumber(atom1);
 
197
                                                        
 
198
                                                        /* action */
 
199
                                                        IAtomContainer acCloned;
 
200
                                                        try {
 
201
                                                                acCloned = (IAtomContainer)reactant.clone();
 
202
                                                        } catch (CloneNotSupportedException e) {
 
203
                                                                throw new CDKException("Could not clone IMolecule!", e);
 
204
                                                        }
 
205
                                                        
 
206
                                                        int charge = acCloned.getAtom(atom0P).getFormalCharge();
 
207
                                                        acCloned.getAtom(atom0P).setFormalCharge(charge-1);
 
208
                                                        
 
209
                                                        IBond bondjClon = null;
 
210
                                                        for(int l = 0 ; l<acCloned.getBondCount();l++){
 
211
                                                                IBond bb = acCloned.getBond(l);
 
212
                                                                if(bb.getFlag(BONDTOFLAG1)){
 
213
                                                                        double order = bb.getOrder();
 
214
                                                                        bb.setOrder(order+1);
 
215
                                                                        bondjClon = bb;
 
216
                                                                        break;
 
217
                                                                }
 
218
                                                        }
 
219
                                                        charge = acCloned.getAtom(atom1P).getFormalCharge();
 
220
                                                        acCloned.getAtom(atom1P).setFormalCharge(charge+1);
 
221
        
 
222
                                                        List selectron = acCloned.getConnectedLonePairsList(acCloned.getAtom(atom1P));
 
223
                                                        acCloned.removeLonePair((ILonePair)selectron.get(selectron.size() -1));
 
224
                                                        
 
225
                                                        /* mapping */
 
226
                                                        IMapping mapping = DefaultChemObjectBuilder.getInstance().newMapping(atomi, acCloned.getAtom(atom0P));
 
227
                                                reaction.addMapping(mapping);
 
228
                                                mapping = DefaultChemObjectBuilder.getInstance().newMapping(atom1, acCloned.getAtom(atom1P));
 
229
                                                reaction.addMapping(mapping);
 
230
                                                mapping = DefaultChemObjectBuilder.getInstance().newMapping(bondj, bondjClon);
 
231
                                                reaction.addMapping(mapping);
 
232
                                                
 
233
                                                        reaction.addProduct((IMolecule) acCloned);
 
234
                                                        setOfReactions.addReaction(reaction);
 
235
                                                        bondj.setFlag(BONDTOFLAG1, false);
 
236
                                                }
 
237
                                        }
 
238
                                }
 
239
                        }
 
240
                }
 
241
                
 
242
                return setOfReactions;  
 
243
                
 
244
                
 
245
        }
 
246
        /**
 
247
         * set the active center for this molecule. 
 
248
         * The active center will be those which correspond with [A+]-B|. 
 
249
         * <pre>
 
250
         * A: Atom with positive charge
 
251
         * -: Single bond
 
252
         * B: Atom with lone pair electrons
 
253
         *  </pre>
 
254
         * 
 
255
         * @param reactant The molecule to set the activity
 
256
         * @throws CDKException 
 
257
         */
 
258
        private void setActiveCenters(IMolecule reactant) throws CDKException {
 
259
                AllRingsFinder ssrF = new AllRingsFinder();
 
260
                IRingSet rings = ssrF.findAllRings(reactant);
 
261
                IAtom atomi = null;
 
262
                IBond bondj = null;
 
263
                for(int i = 0; i < reactant.getAtomCount(); i++) {
 
264
                        atomi = reactant.getAtom(i);
 
265
                        if(atomi.getFormalCharge() == 1 ){
 
266
                                java.util.List bonds = reactant.getConnectedBondsList(atomi);
 
267
                                for(int j = 0 ; j < bonds.size() ; j++){
 
268
                                        bondj = (IBond)bonds.get(j);
 
269
                                        if(bondj.getOrder() == 1.0){
 
270
                                                IAtom atom = bondj.getConnectedAtom(atomi);
 
271
                                                List lp = reactant.getConnectedLonePairsList(atom);
 
272
                                                if((lp.size() > 0 )&& (atom.getFormalCharge() == 0))
 
273
                                                        if(reactant.getConnectedSingleElectronsCount(atom) == 0){
 
274
                                                        if(rings.contains(atom))
 
275
                                                                continue;
 
276
                                                        atomi.setFlag(CDKConstants.REACTIVE_CENTER,true);
 
277
                                                        atom.setFlag(CDKConstants.REACTIVE_CENTER,true);
 
278
                                                        bondj.setFlag(CDKConstants.REACTIVE_CENTER,true);
 
279
                                                }
 
280
                                        }
 
281
                                }
 
282
                        }
 
283
                }
 
284
        }
 
285
        /**
 
286
         *  Gets the parameterNames attribute of the RearrangementCation1Reaction object
 
287
         *
 
288
         *@return    The parameterNames value
 
289
         */
 
290
        public String[] getParameterNames() {
 
291
                String[] params = new String[1];
 
292
                params[0] = "hasActiveCenter";
 
293
                return params;
 
294
        }
 
295
 
 
296
 
 
297
        /**
 
298
         *  Gets the parameterType attribute of the RearrangementCation1Reaction object
 
299
         *
 
300
         *@param  name  Description of the Parameter
 
301
         *@return       The parameterType value
 
302
         */
 
303
        public Object getParameterType(String name) {
 
304
                return new Boolean(false);
 
305
        }
 
306
        /**
 
307
     * clean the flags BONDTOFLAG from the molecule
 
308
     * 
 
309
         * @param mol
 
310
         */
 
311
        public void cleanFlagBOND(IAtomContainer ac){
 
312
                for(int j = 0 ; j < ac.getBondCount(); j++){
 
313
                        ac.getBond(j).setFlag(BONDTOFLAG1, false);
 
314
                }
 
315
        }
 
316
}