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

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/charges/AtomTypeCharges.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: 2007-01-04 18:46:10 +0100 (Thu, 04 Jan 2007) $
 
4
 *  $Revision: 7636 $
 
5
 *
 
6
 *  Copyright (C) 2005-2007  Christian Hoppe <chhoppe@users.sf.net>
 
7
 *
 
8
 *  Contact: cdk-devel@list.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
 *
 
15
 *  This program is distributed in the hope that it will be useful,
 
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 *  GNU Lesser General Public License for more details.
 
19
 *
 
20
 *  You should have received a copy of the GNU Lesser General Public License
 
21
 *  along with this program; if not, write to the Free Software
 
22
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 
23
 */
 
24
 
 
25
package org.openscience.cdk.charges;
 
26
 
 
27
import java.util.regex.Matcher;
 
28
import java.util.regex.Pattern;
 
29
 
 
30
import org.openscience.cdk.interfaces.IAtomContainer;
 
31
import org.openscience.cdk.exception.CDKException;
 
32
import org.openscience.cdk.tools.HOSECodeGenerator;
 
33
 
 
34
/**
 
35
 * Assigns charges to atom types.
 
36
 *
 
37
 * @author      chhoppe
 
38
 * @cdk.created 2004-11-03
 
39
 * @cdk.module  charges
 
40
 */
 
41
public class AtomTypeCharges {
 
42
        HOSECodeGenerator hcg = new HOSECodeGenerator();
 
43
        Pattern pOC = Pattern.compile("O-[1][-];=?+C[(]=?+O.*+");
 
44
        Pattern pOP = Pattern.compile("O-[1][-];=?+P.*+");
 
45
        Pattern pOS = Pattern.compile("O-[1][-];=?+S.*+");
 
46
        Pattern p_p = Pattern.compile("[A-Za-z]{1,2}+[-][0-6].?+[+].*+");
 
47
        Pattern p_n = Pattern.compile("[A-Za-z]{1,2}+[-][0-6].?+[-].*+");
 
48
 
 
49
 
 
50
        /**
 
51
         *  Constructor for the AtomTypeCharges object
 
52
         */
 
53
        AtomTypeCharges() { }
 
54
 
 
55
 
 
56
        /**
 
57
         *  Sets initial charges for atom types
 
58
         * +1 for cationic atom types
 
59
         * -1 for anionic atom types
 
60
         * carboxylic oxygen -0.5
 
61
         * phosphorylic oxygen -0.66
 
62
         * sulfanilic oxygen -0.5
 
63
         * or to formal charge (which must be determined elsewhere or set manually)
 
64
         * polycations are not handled by this approach
 
65
         *
 
66
         *@param  atomContainer  AtomContainer
 
67
         *@return                AtomContainer with set charges
 
68
         *@exception  Exception  Description of the Exception
 
69
         */
 
70
        public IAtomContainer setCharges(IAtomContainer atomContainer) throws Exception {
 
71
                
 
72
                atomContainer = setInitialCharges(atomContainer);
 
73
                return atomContainer;
 
74
        }
 
75
 
 
76
 
 
77
        private String removeAromaticityFlagsFromHoseCode(String hoseCode){
 
78
                //clean hosecode
 
79
                String hosecode="";
 
80
                for (int i=0;i<hoseCode.length();i++){
 
81
                        if (hoseCode.charAt(i)== '*'){
 
82
                        }else{
 
83
                                hosecode=hosecode+hoseCode.charAt(i);
 
84
                        }
 
85
                }
 
86
                return hosecode;
 
87
        }
 
88
        
 
89
        /**
 
90
         *  Sets the initialCharges attribute of the AtomTypeCharges object
 
91
         *
 
92
         *@param  ac                AtomContainer
 
93
         *@return                   AtomContainer with (new) partial charges
 
94
         *@exception  CDKException  Description of the Exception
 
95
         */
 
96
        private IAtomContainer setInitialCharges(IAtomContainer ac) throws CDKException {
 
97
                Matcher matOC = null;
 
98
                Matcher matOP = null;
 
99
                Matcher matOS = null;
 
100
                Matcher mat_p = null;
 
101
                Matcher mat_n = null;
 
102
                String hoseCode = "";
 
103
 
 
104
                for (int i = 0; i < ac.getAtomCount(); i++) {
 
105
                        try {
 
106
                                hoseCode = hcg.getHOSECode(ac, ac.getAtom(i), 3);
 
107
                        } catch (CDKException ex1) {
 
108
                                throw new CDKException("Could not build HOSECode from atom " + i + " due to " + ex1.toString(), ex1);
 
109
                        }
 
110
                        hoseCode=removeAromaticityFlagsFromHoseCode(hoseCode);
 
111
 
 
112
                        matOC = pOC.matcher(hoseCode);
 
113
                        matOP = pOP.matcher(hoseCode);
 
114
                        matOS = pOS.matcher(hoseCode);
 
115
                        mat_p = p_p.matcher(hoseCode);
 
116
                        mat_n = p_n.matcher(hoseCode);
 
117
 
 
118
                        if (matOC.matches()) {
 
119
                                ac.getAtom(i).setCharge(-0.500);
 
120
                        } else if (matOP.matches()) {
 
121
                                ac.getAtom(i).setCharge(-0.666);
 
122
                        } else if (matOS.matches()) {
 
123
                                ac.getAtom(i).setCharge(-0.500);
 
124
                        } else if (mat_p.matches()) {
 
125
                                ac.getAtom(i).setCharge(+1.000);
 
126
                        } else if (mat_n.matches()) {
 
127
                                ac.getAtom(i).setCharge(-1.000);
 
128
                        } else {
 
129
                                ac.getAtom(i).setCharge(ac.getAtom(i).getFormalCharge());
 
130
                        }
 
131
                }
 
132
                return ac;
 
133
        }
 
134
}
 
135