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

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/reaction/type/RadicalSiteInitiationReaction.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
import java.util.List;
 
28
 
 
29
import org.openscience.cdk.CDKConstants;
 
30
import org.openscience.cdk.DefaultChemObjectBuilder;
 
31
import org.openscience.cdk.SingleElectron;
 
32
import org.openscience.cdk.exception.CDKException;
 
33
import org.openscience.cdk.graph.ConnectivityChecker;
 
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.IMoleculeSet;
 
40
import org.openscience.cdk.interfaces.IReaction;
 
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
 
 
47
/**
 
48
 * <p>IReactionProcess which participate mass spectrum process. Homolitic dissocitation. 
 
49
 * This reaction could be represented as A-B-[c*] => [A*] + B=C.</p>
 
50
 * <p>Make sure that the molecule has the corresponend lone pair electrons
 
51
 * for each atom. You can use the method: <pre> LonePairElectronChecker </pre>
 
52
 * 
 
53
 * <pre>
 
54
 *  IMoleculeSet setOfReactants = DefaultChemObjectBuilder.getInstance().newMoleculeSet();
 
55
 *  setOfReactants.addMolecule(new Molecule());
 
56
 *  IReactionProcess type = new RadicalSiteInitiationReaction();
 
57
 *  Object[] params = {Boolean.FALSE};
 
58
    type.setParameters(params);
 
59
 *  IReactionSet setOfReactions = type.initiate(setOfReactants, null);
 
60
 *  </pre>
 
61
 * 
 
62
 * <p>We have the possibility to localize the reactive center. Good method if you
 
63
 * want to localize the reaction in a fixed point</p>
 
64
 * <pre>atoms[0].setFlag(CDKConstants.REACTIVE_CENTER,true);</pre>
 
65
 * <p>Moreover you must put the parameter Boolean.TRUE</p>
 
66
 * <p>If the reactive center is not localized then the reaction process will
 
67
 * try to find automatically the posible reactive center.</p>
 
68
 * 
 
69
 * 
 
70
 * @author         Miguel Rojas
 
71
 * 
 
72
 * @cdk.created    2006-05-05
 
73
 * @cdk.module     reaction
 
74
 * @cdk.set        reaction-types
 
75
 * 
 
76
 **/
 
77
public class RadicalSiteInitiationReaction implements IReactionProcess{
 
78
        private LoggingTool logger;
 
79
        private boolean hasActiveCenter;
 
80
        private static final int BONDTOFLAG = 8;
 
81
        
 
82
        /**
 
83
         * Constructor of the RadicalSiteInitiationReaction object
 
84
         *
 
85
         */
 
86
        public RadicalSiteInitiationReaction(){
 
87
                logger = new LoggingTool(this);
 
88
        }
 
89
        /**
 
90
         *  Gets the specification attribute of the RadicalSiteInitiationReaction object
 
91
         *
 
92
         *@return    The specification value
 
93
         */
 
94
        public ReactionSpecification getSpecification() {
 
95
                return new ReactionSpecification(
 
96
                                "http://almost.cubic.uni-koeln.de/jrg/Members/mrc/reactionDict/reactionDict#RearrangementRadical3Reaction",
 
97
                                this.getClass().getName(),
 
98
                                "$Id: RadicalSiteInitiationReaction.java,v 1.6 2006/04/01 08:26:47 mrc Exp $",
 
99
                                "The Chemistry Development Kit");
 
100
        }
 
101
        
 
102
        /**
 
103
         *  Sets the parameters attribute of the RadicalSiteInitiationReaction object
 
104
         *
 
105
         *@param  params            The parameter is if the molecule has already fixed the center active or not. It 
 
106
         *                                                      should be set before to inize the reaction with a setFlag:  CDKConstants.REACTIVE_CENTER
 
107
         *@exception  CDKException  Description of the Exception
 
108
         */
 
109
        public void setParameters(Object[] params) throws CDKException {
 
110
                if (params.length > 1) {
 
111
                        throw new CDKException("RadicalSiteInitiationReaction only expects one parameter");
 
112
                }
 
113
                if (!(params[0] instanceof Boolean)) {
 
114
                        throw new CDKException("The parameter 1 must be of type boolean");
 
115
                }
 
116
                hasActiveCenter = ((Boolean) params[0]).booleanValue();
 
117
        }
 
118
 
 
119
 
 
120
        /**
 
121
         *  Gets the parameters attribute of the RadicalSiteInitiationReaction object
 
122
         *
 
123
         *@return    The parameters value
 
124
         */
 
125
        public Object[] getParameters() {
 
126
                Object[] params = new Object[1];
 
127
                params[0] = new Boolean (hasActiveCenter);
 
128
                return params;
 
129
        }
 
130
        
 
131
        /**
 
132
         *  Initiate process.
 
133
         *
 
134
         *@param  reactants         reactants of the reaction.
 
135
         *@param  agents            agents of the reaction (Must be in this case null).
 
136
         *
 
137
         *@exception  CDKException  Description of the Exception
 
138
         */
 
139
        public IReactionSet initiate(IMoleculeSet reactants, IMoleculeSet agents) throws CDKException{
 
140
                logger.debug("initiate reaction: RadicalSiteInitiationReaction");
 
141
                
 
142
                if (reactants.getMoleculeCount() != 1) {
 
143
                        throw new CDKException("RadicalSiteInitiationReaction only expects one reactant");
 
144
                }
 
145
                if (agents != null) {
 
146
                        throw new CDKException("RadicalSiteInitiationReaction don't expects agents");
 
147
                }
 
148
                
 
149
                IReactionSet setOfReactions = reactants.getBuilder().newReactionSet();
 
150
                IMolecule reactant = reactants.getMolecule(0);
 
151
 
 
152
                /* if the parameter hasActiveCenter is not fixed yet, set the active centers*/
 
153
                if(!hasActiveCenter){
 
154
                        setActiveCenters(reactant);
 
155
                }
 
156
                
 
157
                IMolecule reactant0 = reactants.getMolecule(0);
 
158
                IAtom atomi = null;
 
159
                IBond bondj;
 
160
                IBond bondk;
 
161
                for(int i = 0 ; i < reactant0.getAtomCount() ; i++){
 
162
                        atomi = reactant0.getAtom(i);
 
163
                        if(atomi.getFlag(CDKConstants.REACTIVE_CENTER)&& reactant.getConnectedSingleElectronsCount(atomi) == 1 ){
 
164
                                
 
165
                                java.util.List bonds = reactant.getConnectedBondsList(atomi);
 
166
                                
 
167
                                for(int j = 0 ; j < bonds.size() ; j++){
 
168
                                        bondj = (IBond)bonds.get(j);
 
169
                                        if(bondj.getFlag(CDKConstants.REACTIVE_CENTER)&& bondj.getOrder() < 3.0 ){
 
170
                                                IAtom atom = bondj.getConnectedAtom(reactant.getAtom(i));
 
171
                                                if(atom.getFormalCharge() != 0)
 
172
                                                        continue;
 
173
                                                java.util.List bondsI = reactant.getConnectedBondsList(atom);
 
174
                                                for(int k = 0 ; k < bondsI.size() ; k++){
 
175
                                                        bondk = (IBond)bondsI.get(k);
 
176
                                                        if(bondk.getFlag(CDKConstants.REACTIVE_CENTER) && bondk.getOrder() ==  1.0 && !bondk.equals(bondj)){
 
177
                                                                IAtom atomConn = bondk.getConnectedAtom(atom);
 
178
                                                                if(atomConn.getFlag(CDKConstants.REACTIVE_CENTER) && atomConn.getFormalCharge() == 0
 
179
                                                                                && !atomConn.equals(atomi) && !atomConn.getSymbol().equals("H")){
 
180
                                                                        
 
181
                                                                        IReaction reaction = DefaultChemObjectBuilder.getInstance().newReaction();
 
182
                                                                        reaction.addReactant(reactant);
 
183
 
 
184
                                                                        cleanFlagBOND(reactants.getMolecule(0));
 
185
                                                                        /* positions atoms and bonds */
 
186
                                                                        int atom0P = reactant.getAtomNumber(atomi);
 
187
                                                                        bondj.setFlag(BONDTOFLAG, true);
 
188
                                                                        int bond2P = reactant.getBondNumber(bondk);
 
189
                                                                        int atom1P = reactant.getAtomNumber(atom);
 
190
                                                                        int atom2P = reactant.getAtomNumber(atomConn);
 
191
                                                                        /* action */
 
192
                                                                        IMolecule acCloned;
 
193
                                                                        try {
 
194
                                                                                acCloned = (IMolecule) reactant.clone();
 
195
                                                                        } catch (CloneNotSupportedException e) {
 
196
                                                                                throw new CDKException("Could not clone IMolecule!", e);
 
197
                                                                        }
 
198
 
 
199
                                                                        acCloned.removeBond(bond2P);
 
200
                                                                        
 
201
                                                                        List selectron = acCloned.getConnectedSingleElectronsList(acCloned.getAtom(atom0P));
 
202
                                                                        acCloned.removeSingleElectron((ISingleElectron)selectron.get(selectron.size() -1));
 
203
                                                                        selectron = acCloned.getConnectedSingleElectronsList(acCloned.getAtom(atom0P));
 
204
                                                                        
 
205
                                                                        acCloned.addSingleElectron(new SingleElectron(acCloned.getAtom(atom2P)));       
 
206
 
 
207
                                                                        
 
208
                                                                        double order = 0;
 
209
                                                                        IBond bondjClon = null;
 
210
                                                                        for(int l = 0 ; l < acCloned.getBondCount();l++){
 
211
                                                                                if(acCloned.getBond(l).getFlag(BONDTOFLAG)){
 
212
                                                                                        order = acCloned.getBond(l).getOrder();
 
213
                                                                                        acCloned.getBond(l).setOrder(order+1);
 
214
                                                                                        bondjClon = acCloned.getBond(l);
 
215
                                                                                        break;
 
216
                                                                                }
 
217
                                                                        }
 
218
 
 
219
                                                                        
 
220
                                                                        /* mapping */
 
221
                                                                        IMapping mapping = atom.getBuilder().newMapping(atomi, acCloned.getAtom(atom0P));
 
222
                                                                reaction.addMapping(mapping);
 
223
                                                                mapping = atom.getBuilder().newMapping(atom, acCloned.getAtom(atom1P));
 
224
                                                                reaction.addMapping(mapping);
 
225
                                                                mapping = atom.getBuilder().newMapping(atomConn, acCloned.getAtom(atom2P));
 
226
                                                                reaction.addMapping(mapping);
 
227
                                                                mapping = atom.getBuilder().newMapping(bondj, bondjClon);
 
228
                                                                reaction.addMapping(mapping);
 
229
                                                                /*breaked bond*/
 
230
//                                                              mapping = atom.getBuilder().newMapping(bondk, acCloned.getBond(bond2P));
 
231
//                                                              reaction.addMapping(mapping);
 
232
                                                                
 
233
                                                                        IMoleculeSet moleculeSet = ConnectivityChecker.partitionIntoMolecules(acCloned);
 
234
                                                                        for(int z = 0; z < moleculeSet.getAtomContainerCount() ; z++){
 
235
                                                                                reaction.addProduct(moleculeSet.getMolecule(z));
 
236
                                                                        }
 
237
                                                                        
 
238
                                                                        setOfReactions.addReaction(reaction);
 
239
 
 
240
                                                                        bondj.setFlag(BONDTOFLAG, false);
 
241
                                                                }
 
242
                                                        }
 
243
                                                }
 
244
                                        }
 
245
                                }
 
246
                        }
 
247
                }
 
248
                return setOfReactions;
 
249
                
 
250
        }
 
251
        /**
 
252
         * set the active center for this molecule. 
 
253
         * The active center will be those which correspond with A-B-[C*]. 
 
254
         * <pre>
 
255
         * A: Atom
 
256
         * -: bond
 
257
         * B: Atom
 
258
         * -: bond
 
259
         * C: Atom with single electron
 
260
         *  </pre>
 
261
         * 
 
262
         * @param reactant The molecule to set the activity
 
263
         * @throws CDKException 
 
264
         */
 
265
        private void setActiveCenters(IMolecule reactant) throws CDKException {
 
266
                IAtom atomi = null;
 
267
                IBond bondj = null;
 
268
                IBond bondk = null;
 
269
                for(int i = 0 ; i < reactant.getAtomCount() ; i++) {
 
270
                        atomi = reactant.getAtom(i);
 
271
                        if(reactant.getConnectedSingleElectronsCount(atomi) == 1 ){
 
272
                                java.util.List bonds = reactant.getConnectedBondsList(atomi);
 
273
                                for(int j = 0 ; j < bonds.size() ; j++){
 
274
                                        bondj = (IBond)bonds.get(j);
 
275
                                        if(bondj.getOrder() < 3.0){
 
276
                                                IAtom atom = bondj.getConnectedAtom(atomi);
 
277
                                                if(atom.getFormalCharge() != 0)
 
278
                                                        continue;
 
279
                                                java.util.List bondsI = reactant.getConnectedBondsList(atom);
 
280
                                                for(int k = 0 ; k < bondsI.size() ; k++){
 
281
                                                        bondk = (IBond)bondsI.get(k);
 
282
                                                        if(bondk.getOrder() == 1 && !bondk.equals(bondj)){
 
283
                                                        IAtom atomConn = bondk.getConnectedAtom(atom);
 
284
                                                        if(atomConn.getFormalCharge() == 0 && !atomConn.getSymbol().equals("H")){
 
285
                                                                atomi.setFlag(CDKConstants.REACTIVE_CENTER,true);
 
286
                                                                atom.setFlag(CDKConstants.REACTIVE_CENTER,true);
 
287
                                                                atomConn.setFlag(CDKConstants.REACTIVE_CENTER,true);
 
288
                                                                bondj.setFlag(CDKConstants.REACTIVE_CENTER,true);
 
289
                                                                bondk.setFlag(CDKConstants.REACTIVE_CENTER,true); 
 
290
                                                        }
 
291
                                                }
 
292
                                                }
 
293
                                        }
 
294
                                }
 
295
                        }
 
296
                }
 
297
        }
 
298
        /**
 
299
         *  Gets the parameterNames attribute of the RadicalSiteInitiationReaction object
 
300
         *
 
301
         *@return    The parameterNames value
 
302
         */
 
303
        public String[] getParameterNames() {
 
304
                String[] params = new String[1];
 
305
                params[0] = "hasActiveCenter";
 
306
                return params;
 
307
        }
 
308
 
 
309
 
 
310
        /**
 
311
         *  Gets the parameterType attribute of the RadicalSiteInitiationReaction object
 
312
         *
 
313
         *@param  name  Description of the Parameter
 
314
         *@return       The parameterType value
 
315
         */
 
316
        public Object getParameterType(String name) {
 
317
                return new Boolean(false);
 
318
        }
 
319
        /**
 
320
     * clean the flags BONDTOFLAG from the molecule
 
321
     * 
 
322
         * @param mol
 
323
         */
 
324
        public void cleanFlagBOND(IAtomContainer ac){
 
325
                for(int j = 0 ; j < ac.getBondCount(); j++)
 
326
                        ac.getBond(j).setFlag(BONDTOFLAG, false);
 
327
        }
 
328
}