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

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/reaction/type/HydrogenRearrangementDeltaReaction.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.Iterator;
 
29
import java.util.List;
 
30
 
 
31
import org.openscience.cdk.CDKConstants;
 
32
import org.openscience.cdk.DefaultChemObjectBuilder;
 
33
import org.openscience.cdk.Molecule;
 
34
import org.openscience.cdk.Ring;
 
35
import org.openscience.cdk.SingleElectron;
 
36
import org.openscience.cdk.aromaticity.HueckelAromaticityDetector;
 
37
import org.openscience.cdk.exception.CDKException;
 
38
import org.openscience.cdk.interfaces.IAtom;
 
39
import org.openscience.cdk.interfaces.IAtomContainer;
 
40
import org.openscience.cdk.interfaces.IMapping;
 
41
import org.openscience.cdk.interfaces.IMolecule;
 
42
import org.openscience.cdk.interfaces.IMoleculeSet;
 
43
import org.openscience.cdk.interfaces.IReaction;
 
44
import org.openscience.cdk.interfaces.IReactionSet;
 
45
import org.openscience.cdk.interfaces.IRingSet;
 
46
import org.openscience.cdk.interfaces.ISingleElectron;
 
47
import org.openscience.cdk.reaction.IReactionProcess;
 
48
import org.openscience.cdk.reaction.ReactionSpecification;
 
49
import org.openscience.cdk.ringsearch.AllRingsFinder;
 
50
import org.openscience.cdk.tools.HOSECodeGenerator;
 
51
import org.openscience.cdk.tools.LoggingTool;
 
52
 
 
53
/**
 
54
 * <p>
 
55
 * This reaction could be represented as [A*]-(C)_4-C5[H] => A([H])-(C_4)-[C5*]. Due to 
 
56
 * the single electron of atom A.</p>
 
57
 * <p>Make sure that the molecule has the corresponend lone pair electrons
 
58
 * for each atom. You can use the method: <pre> LonePairElectronChecker </pre>
 
59
 * 
 
60
 * <pre>
 
61
 *  IMoleculeSet setOfReactants = DefaultChemObjectBuilder.getInstance().newMoleculeSet();
 
62
 *  setOfReactants.addMolecule(new Molecule());
 
63
 *  IReactionProcess type = new HydrogenRearrangementDeltaReaction();
 
64
 *  Object[] params = {Boolean.FALSE};
 
65
    type.setParameters(params);
 
66
 *  IReactionSet setOfReactions = type.initiate(setOfReactants, null);
 
67
 *  </pre>
 
68
 * 
 
69
 * <p>We have the possibility to localize the reactive center. Good method if you
 
70
 * want to localize the reaction in a fixed point</p>
 
71
 * <pre>atoms[0].setFlag(CDKConstants.REACTIVE_CENTER,true);</pre>
 
72
 * <p>Moreover you must put the parameter Boolean.TRUE</p>
 
73
 * <p>If the reactive center is not localized then the reaction process will
 
74
 * try to find automatically the posible reactive center.</p>
 
75
 * 
 
76
 * 
 
77
 * @author         Miguel Rojas
 
78
 * 
 
79
 * @cdk.created    2006-10-20
 
80
 * @cdk.module     reaction
 
81
 * @cdk.set        reaction-types
 
82
 * 
 
83
 **/
 
84
public class HydrogenRearrangementDeltaReaction implements IReactionProcess{
 
85
        private LoggingTool logger;
 
86
        private boolean hasActiveCenter;
 
87
 
 
88
        /**
 
89
         * Constructor of the HydrogenRearrangementDeltaReaction object
 
90
         *
 
91
         */
 
92
        public HydrogenRearrangementDeltaReaction(){
 
93
                logger = new LoggingTool(this);
 
94
        }
 
95
        /**
 
96
         *  Gets the specification attribute of the HydrogenRearrangementDeltaReaction object
 
97
         *
 
98
         *@return    The specification value
 
99
         */
 
100
        public ReactionSpecification getSpecification() {
 
101
                return new ReactionSpecification(
 
102
                                "http://almost.cubic.uni-koeln.de/jrg/Members/mrc/reactionDict/reactionDict#HydrogenRearrangementDeltaReaction",
 
103
                                this.getClass().getName(),
 
104
                                "$Id: HydrogenRearrangementDeltaReaction.java,v 1.6 2006/04/01 08:26:47 mrc Exp $",
 
105
                                "The Chemistry Development Kit");
 
106
        }
 
107
        
 
108
        /**
 
109
         *  Sets the parameters attribute of the HydrogenRearrangementDeltaReaction object
 
110
         *
 
111
         *@param  params            The parameter is if the molecule has already fixed the center active or not. It 
 
112
         *                                                      should be set before to inize the reaction with a setFlag:  CDKConstants.REACTIVE_CENTER
 
113
         *@exception  CDKException  Description of the Exception
 
114
         */
 
115
        public void setParameters(Object[] params) throws CDKException {
 
116
                if (params.length > 1) {
 
117
                        throw new CDKException("HydrogenRearrangementDeltaReaction only expects one parameter");
 
118
                }
 
119
                if (!(params[0] instanceof Boolean)) {
 
120
                        throw new CDKException("The parameter 1 must be of type boolean");
 
121
                }
 
122
                hasActiveCenter = ((Boolean) params[0]).booleanValue();
 
123
        }
 
124
 
 
125
 
 
126
        /**
 
127
         *  Gets the parameters attribute of the HydrogenRearrangementDeltaReaction object
 
128
         *
 
129
         *@return    The parameters value
 
130
         */
 
131
        public Object[] getParameters() {
 
132
                Object[] params = new Object[1];
 
133
                params[0] = new Boolean (hasActiveCenter);
 
134
                return params;
 
135
        }
 
136
        
 
137
        /**
 
138
         *  Initiate process.
 
139
         *  It is needed to call the addExplicitHydrogensToSatisfyValency
 
140
         *  from the class tools.HydrogenAdder.
 
141
         *
 
142
         *@param  reactants         reactants of the reaction.
 
143
         *@param  agents            agents of the reaction (Must be in this case null).
 
144
         *
 
145
         *@exception  CDKException  Description of the Exception
 
146
         */
 
147
        public IReactionSet initiate(IMoleculeSet reactants, IMoleculeSet agents) throws CDKException{
 
148
 
 
149
                logger.debug("initiate reaction: HydrogenRearrangementDeltaReaction");
 
150
                
 
151
                if (reactants.getMoleculeCount() != 1) {
 
152
                        throw new CDKException("HydrogenRearrangementDeltaReaction only expects one reactant");
 
153
                }
 
154
                if (agents != null) {
 
155
                        throw new CDKException("HydrogenRearrangementDeltaReaction don't expects agents");
 
156
                }
 
157
                
 
158
                IReactionSet setOfReactions = DefaultChemObjectBuilder.getInstance().newReactionSet();
 
159
                IMolecule reactant = reactants.getMolecule(0);
 
160
 
 
161
                /* if the parameter hasActiveCenter is not fixed yet, set the active centers*/
 
162
                if(!hasActiveCenter){
 
163
                        setActiveCenters(reactant);
 
164
                }
 
165
                
 
166
                HOSECodeGenerator hcg = new HOSECodeGenerator();
 
167
                for(int i = 0; i < reactant.getAtomCount(); i++) {
 
168
                        IReaction reaction = DefaultChemObjectBuilder.getInstance().newReaction();
 
169
                        reaction.addReactant(reactant);
 
170
 
 
171
                        IAtom  atomi = reactant.getAtom(i);
 
172
                        if(reactant.getConnectedSingleElectronsCount(atomi) == 1 && atomi.getFlag(CDKConstants.REACTIVE_CENTER)) {
 
173
                                hcg.getSpheres((Molecule) reactant, atomi, 5, true);
 
174
                                List atoms = hcg.getNodesInSphere(5);
 
175
                                for(int j = 0 ; j < atoms.size() ; j++){
 
176
                                        IAtom atom4 = (IAtom)atoms.get(j);
 
177
                                        if(atom4 != null)
 
178
                                        if(atom4.getFormalCharge() == 0 && !atom4.equals("H")  && 
 
179
                                                        reactant.getMaximumBondOrder(atom4) == 1 && atom4.getFlag(CDKConstants.REACTIVE_CENTER)){
 
180
                                                Iterator iterat = reactant.getConnectedAtomsList(atom4).iterator();
 
181
                                                while(iterat.hasNext()){
 
182
                                                        IAtom hydrogen = (IAtom) iterat.next();
 
183
                                                        if(hydrogen.getSymbol().equals("H") && hydrogen.getFlag(CDKConstants.REACTIVE_CENTER)){
 
184
                                                                /* positions atoms and bonds */
 
185
                                                                int atom0P = reactant.getAtomNumber(atomi);
 
186
                                                                int atom4P = reactant.getAtomNumber(atom4);
 
187
                                                                int atomHP = reactant.getAtomNumber(hydrogen);
 
188
                                                                
 
189
                                                                /* action */
 
190
                                                                IAtomContainer acCloned;
 
191
                                                                try {
 
192
                                                                        acCloned = (IAtomContainer)reactant.clone();
 
193
                                                                } catch (CloneNotSupportedException e) {
 
194
                                                                        throw new CDKException("Could not clone IMolecule!", e);
 
195
                                                                }
 
196
                                                                                
 
197
                                                                
 
198
                                                                List selectron = acCloned.getConnectedSingleElectronsList(acCloned.getAtom(atom0P));
 
199
                                                                acCloned.removeSingleElectron((ISingleElectron)selectron.get(selectron.size() -1));
 
200
                                                                acCloned.addBond(atom0P,atomHP, 1);
 
201
                                                                acCloned.removeBond(acCloned.getAtom(atom4P), acCloned.getAtom(atomHP));
 
202
        
 
203
                                                                acCloned.addSingleElectron(new SingleElectron(acCloned.getAtom(atom4P)));       
 
204
                                                                
 
205
        
 
206
                                                                /* mapping */
 
207
                                                                IMapping mapping = DefaultChemObjectBuilder.getInstance().newMapping(atomi, acCloned.getAtom(atom0P));
 
208
                                                        reaction.addMapping(mapping);
 
209
                                                        mapping = DefaultChemObjectBuilder.getInstance().newMapping(atom4, acCloned.getAtom(atom4P));
 
210
                                                        reaction.addMapping(mapping);
 
211
                                                        mapping = DefaultChemObjectBuilder.getInstance().newMapping(hydrogen, acCloned.getAtom(atomHP));
 
212
                                                        reaction.addMapping(mapping);
 
213
        
 
214
                                                                reaction.addProduct((IMolecule) acCloned);
 
215
                                                                setOfReactions.addReaction(reaction);
 
216
                                                        }
 
217
 
 
218
                                                }
 
219
                                                
 
220
                                        }
 
221
                                }
 
222
                        }
 
223
                }
 
224
                return setOfReactions;  
 
225
        }
 
226
        /**
 
227
         * set the active center for this molecule. 
 
228
         * The active center will be those which correspond with [A*]-(C)_4-C5[H]
 
229
         * <pre>
 
230
         * C: Atom with single electron
 
231
         * C5: Atom with Hydrogen
 
232
         *  </pre>
 
233
         * 
 
234
         * @param reactant The molecule to set the activity
 
235
         * @throws CDKException 
 
236
         */
 
237
        private void setActiveCenters(IMolecule reactant) throws CDKException {
 
238
                HOSECodeGenerator hcg = new HOSECodeGenerator();
 
239
                IRingSet ringSet = null;
 
240
                AllRingsFinder arf = new AllRingsFinder();
 
241
                for(int i = 0; i < reactant.getAtomCount(); i++) {
 
242
                        IAtom  atomi = reactant.getAtom(i);
 
243
                        if(reactant.getConnectedSingleElectronsCount(atomi) == 1) {
 
244
                                HueckelAromaticityDetector.detectAromaticity(reactant);
 
245
                                hcg.getSpheres((Molecule) reactant, atomi, 5, true);
 
246
                                
 
247
                                /* no rearrangement if H belongs to ring*/
 
248
                                ringSet = arf.findAllRings((Molecule) reactant);
 
249
                                for (int ir = 0; ir < ringSet.getAtomContainerCount(); ir++) {
 
250
                                        Ring ring = (Ring)ringSet.getAtomContainer(ir);
 
251
                                        for (int jr = 0; jr < ring.getAtomCount(); jr++) {
 
252
                                                IAtom aring = ring.getAtom(jr);
 
253
                                                aring.setFlag(CDKConstants.ISINRING, true);
 
254
                                        }
 
255
                                }
 
256
                                List atoms = hcg.getNodesInSphere(5);
 
257
                                for(int j = 0 ; j < atoms.size() ; j++){
 
258
                                        IAtom atom4 = (IAtom)atoms.get(j);
 
259
                                        
 
260
                                        if(atom4 != null)
 
261
                                        if(!atom4.getFlag(CDKConstants.ISINRING))
 
262
                                        if(atom4.getFormalCharge() == 0 && !atom4.equals("H") && 
 
263
                                                        reactant.getMaximumBondOrder(atom4) == 1){
 
264
                                                Iterator iterat = reactant.getConnectedAtomsList(atom4).iterator();
 
265
                                                while(iterat.hasNext()){
 
266
                                                        IAtom hydrogen = (IAtom) iterat.next();
 
267
                                                        if(hydrogen.getSymbol().equals("H")){
 
268
                                                                atomi.setFlag(CDKConstants.REACTIVE_CENTER,true);
 
269
                                                                atom4.setFlag(CDKConstants.REACTIVE_CENTER,true);
 
270
                                                                hydrogen.setFlag(CDKConstants.REACTIVE_CENTER,true);
 
271
                                                                break; /*is only necessary one hydrogen */
 
272
                                                        }
 
273
                                                }
 
274
                                        }
 
275
                                }
 
276
                        }
 
277
                }
 
278
        }
 
279
        /**
 
280
         *  Gets the parameterNames attribute of the HydrogenRearrangementDeltaReaction object
 
281
         *
 
282
         *@return    The parameterNames value
 
283
         */
 
284
        public String[] getParameterNames() {
 
285
                String[] params = new String[1];
 
286
                params[0] = "hasActiveCenter";
 
287
                return params;
 
288
        }
 
289
 
 
290
 
 
291
        /**
 
292
         *  Gets the parameterType attribute of the HydrogenRearrangementDeltaReaction object
 
293
         *
 
294
         *@param  name  Description of the Parameter
 
295
         *@return       The parameterType value
 
296
         */
 
297
        public Object getParameterType(String name) {
 
298
                return new Boolean(false);
 
299
        }}