3
* $Date: 2007-01-04 18:46:10 +0100 (Thu, 04 Jan 2007) $
6
* Copyright (C) 2005-2007 Christian Hoppe <chhoppe@users.sf.net>
8
* Contact: cdk-devel@list.sourceforge.net
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.
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.
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.
25
package org.openscience.cdk.charges;
27
import java.util.regex.Matcher;
28
import java.util.regex.Pattern;
30
import org.openscience.cdk.interfaces.IAtomContainer;
31
import org.openscience.cdk.exception.CDKException;
32
import org.openscience.cdk.tools.HOSECodeGenerator;
35
* Assigns charges to atom types.
38
* @cdk.created 2004-11-03
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].?+[-].*+");
51
* Constructor for the AtomTypeCharges object
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
66
*@param atomContainer AtomContainer
67
*@return AtomContainer with set charges
68
*@exception Exception Description of the Exception
70
public IAtomContainer setCharges(IAtomContainer atomContainer) throws Exception {
72
atomContainer = setInitialCharges(atomContainer);
77
private String removeAromaticityFlagsFromHoseCode(String hoseCode){
80
for (int i=0;i<hoseCode.length();i++){
81
if (hoseCode.charAt(i)== '*'){
83
hosecode=hosecode+hoseCode.charAt(i);
90
* Sets the initialCharges attribute of the AtomTypeCharges object
92
*@param ac AtomContainer
93
*@return AtomContainer with (new) partial charges
94
*@exception CDKException Description of the Exception
96
private IAtomContainer setInitialCharges(IAtomContainer ac) throws CDKException {
100
Matcher mat_p = null;
101
Matcher mat_n = null;
102
String hoseCode = "";
104
for (int i = 0; i < ac.getAtomCount(); i++) {
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);
110
hoseCode=removeAromaticityFlagsFromHoseCode(hoseCode);
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);
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);
129
ac.getAtom(i).setCharge(ac.getAtom(i).getFormalCharge());