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

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/reaction/type/RearrangementRadical2Reaction.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.SingleElectron;
 
33
import org.openscience.cdk.exception.CDKException;
 
34
import org.openscience.cdk.interfaces.IAtom;
 
35
import org.openscience.cdk.interfaces.IAtomContainer;
 
36
import org.openscience.cdk.interfaces.IBond;
 
37
import org.openscience.cdk.interfaces.IMapping;
 
38
import org.openscience.cdk.interfaces.IMolecule;
 
39
import org.openscience.cdk.interfaces.IReaction;
 
40
import org.openscience.cdk.interfaces.IMoleculeSet;
 
41
import org.openscience.cdk.interfaces.IReactionSet;
 
42
import org.openscience.cdk.interfaces.ISingleElectron;
 
43
import org.openscience.cdk.reaction.IReactionProcess;
 
44
import org.openscience.cdk.reaction.ReactionSpecification;
 
45
import org.openscience.cdk.tools.LoggingTool;
 
46
import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;
 
47
 
 
48
/**
 
49
 * <p>IReactionProcess which participate in movement resonance. 
 
50
 * This reaction could be represented as [A*]-B=C => A=B-[c*]. Due to 
 
51
 * excess of charge of the atom B, the sinble electron of the atom A is 
 
52
 * desplaced through the double bond.</p>
 
53
 * <p>Make sure that the molecule has the corresponend lone pair electrons
 
54
 * for each atom. You can use the method: <pre> LonePairElectronChecker </pre>
 
55
 * 
 
56
 * <pre>
 
57
 *  IMoleculeSet setOfReactants = DefaultChemObjectBuilder.getInstance().newMoleculeSet();
 
58
 *  setOfReactants.addMolecule(new Molecule());
 
59
 *  IReactionProcess type = new RearrangementRadical2Reaction();
 
60
 *  Object[] params = {Boolean.FALSE};
 
61
    type.setParameters(params);
 
62
 *  IReactionSet setOfReactions = type.initiate(setOfReactants, null);
 
63
 *  </pre>
 
64
 * 
 
65
 * <p>We have the possibility to localize the reactive center. Good method if you
 
66
 * want to localize the reaction in a fixed point</p>
 
67
 * <pre>atoms[0].setFlag(CDKConstants.REACTIVE_CENTER,true);</pre>
 
68
 * <p>Moreover you must put the parameter Boolean.TRUE</p>
 
69
 * <p>If the reactive center is not localized then the reaction process will
 
70
 * try to find automatically the posible reactive center.</p>
 
71
 * 
 
72
 * 
 
73
 * @author         Miguel Rojas
 
74
 * 
 
75
 * @cdk.created    2006-05-05
 
76
 * @cdk.module     reaction
 
77
 * @cdk.set        reaction-types
 
78
 * 
 
79
 **/
 
80
public class RearrangementRadical2Reaction implements IReactionProcess{
 
81
        private LoggingTool logger;
 
82
        private boolean hasActiveCenter;
 
83
        private static final int BONDTOFLAG1 = 8;
 
84
        private static final int BONDTOFLAG2 = 9;
 
85
 
 
86
        /**
 
87
         * Constructor of the RearrangementRadical2Reaction object
 
88
         *
 
89
         */
 
90
        public RearrangementRadical2Reaction(){
 
91
                logger = new LoggingTool(this);
 
92
        }
 
93
        /**
 
94
         *  Gets the specification attribute of the RearrangementRadical2Reaction 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#RearrangementRadical2Reaction",
 
101
                                this.getClass().getName(),
 
102
                                "$Id: RearrangementRadical2Reaction.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 RearrangementRadical2Reaction 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("RearrangementRadical2Reaction 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 RearrangementRadical2Reaction 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: RearrangementRadical2Reaction");
 
148
                
 
149
                if (reactants.getMoleculeCount() != 1) {
 
150
                        throw new CDKException("RearrangementRadical2Reaction only expects one reactant");
 
151
                }
 
152
                if (agents != null) {
 
153
                        throw new CDKException("RearrangementRadical2Reaction don't expects agents");
 
154
                }
 
155
                
 
156
                IReactionSet setOfReactions = DefaultChemObjectBuilder.getInstance().newReactionSet();
 
157
                IMolecule reactant = reactants.getMolecule(0);
 
158
 
 
159
                /* if the parameter hasActiveCenter is not fixed yet, set the active centers*/
 
160
                if(!hasActiveCenter){
 
161
                        setActiveCenters(reactant);
 
162
                }
 
163
                
 
164
                IAtom atomi = null;
 
165
                IBond bondj = null;
 
166
                IBond bondk = null;
 
167
                for(int i = 0 ; i < reactant.getAtomCount() ; i++){
 
168
                        atomi = reactant.getAtom(i);
 
169
                        if(atomi.getFlag(CDKConstants.REACTIVE_CENTER)&& reactant.getConnectedSingleElectronsCount(atomi) == 1){
 
170
                                
 
171
                                java.util.List bonds = reactant.getConnectedBondsList(atomi);
 
172
                                
 
173
                                for(int j = 0 ; j < bonds.size() ; j++){
 
174
                                        bondj = (IBond)bonds.get(j);
 
175
                                        if(bondj.getFlag(CDKConstants.REACTIVE_CENTER)&& bondj.getOrder() == 1.0){
 
176
                                                IAtom atom = bondj.getConnectedAtom(reactant.getAtom(i));
 
177
                                                java.util.List bondsI = reactant.getConnectedBondsList(atom);
 
178
                                                for(int k = 0 ; k < bondsI.size() ; k++){
 
179
                                                        bondk = (IBond)bondsI.get(k);
 
180
                                                        if(bondk.getFlag(CDKConstants.REACTIVE_CENTER) && bondk.getOrder() == 2.0){
 
181
                                                                IAtom atom1 = bondk.getConnectedAtom(atom);
 
182
                                                                if(atom1.getFlag(CDKConstants.REACTIVE_CENTER)&& atom1.getFormalCharge() == 0 ){
 
183
                                                                        IReaction reaction = DefaultChemObjectBuilder.getInstance().newReaction();
 
184
                                                                        reaction.addReactant(reactant);
 
185
                                                                        
 
186
                                                                        cleanFlagBOND(reactants.getMolecule(0));
 
187
                                                                        /* positions atoms and bonds */
 
188
                                                                        int atom0P = reactant.getAtomNumber(atomi);
 
189
                                                                        bondj.setFlag(BONDTOFLAG1, true);
 
190
                                                                        bondk.setFlag(BONDTOFLAG2, true);
 
191
                                                                        int atom1P = reactant.getAtomNumber(atom);
 
192
                                                                        int atom2P = reactant.getAtomNumber(atom1);
 
193
                                                                        
 
194
                                                                        /* action */
 
195
                                                                        IAtomContainer acCloned;
 
196
                                                                        try {
 
197
                                                                                acCloned = (IAtomContainer)reactant.clone();
 
198
                                                                        } catch (CloneNotSupportedException e) {
 
199
                                                                                throw new CDKException("Could not clone IMolecule!", e);
 
200
                                                                        }
 
201
                                                                        
 
202
                                                                        List selectron = acCloned.getConnectedSingleElectronsList(acCloned.getAtom(atom0P));
 
203
                                                                        acCloned.removeSingleElectron((ISingleElectron)selectron.get(selectron.size() -1));
 
204
                                                                        
 
205
                                                                        acCloned.addSingleElectron(new SingleElectron(acCloned.getAtom(atom2P)));       
 
206
 
 
207
                                                                        IBond bondjClon = null, bondkClon = null;
 
208
                                                                        for(int l = 0 ; l<acCloned.getBondCount();l++){
 
209
                                                                                IBond bb = acCloned.getBond(l);
 
210
                                                                                if(bb.getFlag(BONDTOFLAG1)){
 
211
                                                                                        
 
212
                                                                                        double order = bb.getOrder();
 
213
                                                                                        bb.setOrder(order+1);
 
214
                                                                                        bondjClon = bb;
 
215
                                                                                        
 
216
                                                                                }else if(acCloned.getBond(l).getFlag(BONDTOFLAG2)){
 
217
                                                                                        double order = bb.getOrder();
 
218
                                                                                        bb.setOrder(order-1);
 
219
                                                                                        bondkClon = bb;
 
220
                                                                                }
 
221
                                                                        }
 
222
                                                                        
 
223
                                                                        
 
224
                                                                        /* mapping */
 
225
                                                                        IMapping mapping = DefaultChemObjectBuilder.getInstance().newMapping(atomi, acCloned.getAtom(atom0P));
 
226
                                                                reaction.addMapping(mapping);
 
227
                                                                mapping = DefaultChemObjectBuilder.getInstance().newMapping(atom, acCloned.getAtom(atom1P));
 
228
                                                                reaction.addMapping(mapping);
 
229
                                                                mapping = DefaultChemObjectBuilder.getInstance().newMapping(atom1, acCloned.getAtom(atom2P));
 
230
                                                                reaction.addMapping(mapping);
 
231
                                                                mapping = DefaultChemObjectBuilder.getInstance().newMapping(bondj, bondjClon);
 
232
                                                                reaction.addMapping(mapping);
 
233
                                                                mapping = DefaultChemObjectBuilder.getInstance().newMapping(bondk, bondkClon);
 
234
                                                                reaction.addMapping(mapping);
 
235
                                                                        
 
236
                                                                        reaction.addProduct((IMolecule) acCloned);
 
237
                                                                        setOfReactions.addReaction(reaction);
 
238
 
 
239
 
 
240
                                                                        bondj.setFlag(BONDTOFLAG1, false);
 
241
                                                                        bondk.setFlag(BONDTOFLAG2, false);
 
242
                                                                }
 
243
                                                        }
 
244
                                                }
 
245
                                        }
 
246
                                }
 
247
                        }
 
248
                }
 
249
                return setOfReactions;  
 
250
                
 
251
                
 
252
        }
 
253
        /**
 
254
         * set the active center for this molecule. 
 
255
         * The active center will be those which correspond with  [A*]-B=C . 
 
256
         * <pre>
 
257
         * A: Atom with single electron
 
258
         * -: Single bond
 
259
         * B: Atom 
 
260
         * =: Double bond
 
261
         * C: Atom
 
262
         *  </pre>
 
263
         * 
 
264
         * @param reactant The molecule to set the activity
 
265
         * @throws CDKException 
 
266
         */
 
267
        private void setActiveCenters(IMolecule reactant) throws CDKException {
 
268
                if(AtomContainerManipulator.getTotalNegativeFormalCharge(reactant) != 0 /*|| AtomContainerManipulator.getTotalPositiveFormalCharge(reactant) != 0*/)
 
269
                        return;
 
270
                IAtom atomi = null;
 
271
                IBond bondj = null;
 
272
                IBond bondk = null;
 
273
                for(int i = 0; i < reactant.getAtomCount(); i++) {
 
274
                        atomi = reactant.getAtom(i);
 
275
                        if(reactant.getConnectedSingleElectronsCount(atomi) == 1 ){
 
276
                                java.util.List bonds = reactant.getConnectedBondsList(atomi);
 
277
                                for(int j = 0 ; j < bonds.size() ; j++){
 
278
                                        bondj = (IBond)bonds.get(j);
 
279
                                        if(bondj.getOrder() == 1.0){
 
280
                                                IAtom atom = bondj.getConnectedAtom(reactant.getAtom(i));
 
281
                                                java.util.List bondsI = reactant.getConnectedBondsList(atom);
 
282
                                                for(int k = 0 ; k < bondsI.size() ; k++){
 
283
                                                        bondk = (IBond)bondsI.get(k);
 
284
                                                        if(bondk.getOrder() == 2.0){
 
285
                                                                IAtom atom1 = bondk.getConnectedAtom(atom);
 
286
                                                                if(atom1.getFormalCharge() == 0 ){
 
287
                                                                        atomi.setFlag(CDKConstants.REACTIVE_CENTER,true);
 
288
                                                                        atom.setFlag(CDKConstants.REACTIVE_CENTER,true);
 
289
                                                                        atom1.setFlag(CDKConstants.REACTIVE_CENTER,true);
 
290
                                                                        bondk.getConnectedAtom(atom).setFlag(CDKConstants.REACTIVE_CENTER,true);
 
291
                                                                        bondj.setFlag(CDKConstants.REACTIVE_CENTER,true);
 
292
                                                                        bondk.setFlag(CDKConstants.REACTIVE_CENTER,true);
 
293
                                                                }
 
294
                                                        }
 
295
                                                }
 
296
                                        }
 
297
                                }
 
298
                        }
 
299
                }
 
300
        }
 
301
        /**
 
302
         *  Gets the parameterNames attribute of the RearrangementRadical2Reaction object
 
303
         *
 
304
         *@return    The parameterNames value
 
305
         */
 
306
        public String[] getParameterNames() {
 
307
                String[] params = new String[1];
 
308
                params[0] = "hasActiveCenter";
 
309
                return params;
 
310
        }
 
311
 
 
312
 
 
313
        /**
 
314
         *  Gets the parameterType attribute of the RearrangementRadical2Reaction object
 
315
         *
 
316
         *@param  name  Description of the Parameter
 
317
         *@return       The parameterType value
 
318
         */
 
319
        public Object getParameterType(String name) {
 
320
                return new Boolean(false);
 
321
        }
 
322
 
 
323
        /**
 
324
     * clean the flags BONDTOFLAG from the molecule
 
325
     * 
 
326
         * @param mol
 
327
         */
 
328
        public void cleanFlagBOND(IAtomContainer ac){
 
329
                for(int j = 0 ; j < ac.getBondCount(); j++){
 
330
                        ac.getBond(j).setFlag(BONDTOFLAG1, false);
 
331
                        ac.getBond(j).setFlag(BONDTOFLAG2, false);
 
332
                }
 
333
        }
 
334
}