|
23 | 23 | * */ |
24 | 24 | package org.openscience.cdk.tools.manipulator; |
25 | 25 |
|
| 26 | +import org.openscience.cdk.CDK; |
26 | 27 | import org.openscience.cdk.CDKConstants; |
27 | 28 | import org.openscience.cdk.config.AtomTypeFactory; |
28 | 29 | import org.openscience.cdk.config.Elements; |
29 | 30 | import org.openscience.cdk.config.IsotopeFactory; |
30 | 31 | import org.openscience.cdk.config.Isotopes; |
31 | 32 | import org.openscience.cdk.exception.CDKException; |
32 | | -import org.openscience.cdk.interfaces.IAtom; |
33 | | -import org.openscience.cdk.interfaces.IAtomContainer; |
34 | | -import org.openscience.cdk.interfaces.IAtomType; |
35 | | -import org.openscience.cdk.interfaces.IChemObjectBuilder; |
36 | | -import org.openscience.cdk.interfaces.IElement; |
37 | | -import org.openscience.cdk.interfaces.IIsotope; |
38 | | -import org.openscience.cdk.interfaces.IMolecularFormula; |
| 33 | +import org.openscience.cdk.interfaces.*; |
| 34 | +import org.openscience.cdk.sgroup.Sgroup; |
| 35 | +import org.openscience.cdk.sgroup.SgroupType; |
39 | 36 | import org.openscience.cdk.tools.LoggingToolFactory; |
40 | 37 |
|
41 | 38 | import java.io.IOException; |
42 | | -import java.util.ArrayList; |
43 | | -import java.util.Arrays; |
44 | | -import java.util.Collections; |
45 | | -import java.util.Comparator; |
46 | | -import java.util.List; |
47 | | -import java.util.Map; |
48 | | -import java.util.Objects; |
49 | | -import java.util.TreeMap; |
| 39 | +import java.util.*; |
| 40 | + |
50 | 41 | /** |
51 | 42 | * Class with convenience methods that provide methods to manipulate |
52 | 43 | * {@link IMolecularFormula}'s. For example: |
@@ -245,13 +236,16 @@ public static String getString(IMolecularFormula formula, String[] orderElements |
245 | 236 | } |
246 | 237 |
|
247 | 238 | private static void appendElement(StringBuilder sb, Integer mass, int elem, int count) { |
| 239 | + String symbol = Elements.ofNumber(elem).symbol(); |
| 240 | + if (symbol.isEmpty()) |
| 241 | + symbol = "R"; |
248 | 242 | if (mass != null) |
249 | 243 | sb.append('[') |
250 | 244 | .append(mass) |
251 | 245 | .append(']') |
252 | | - .append(Elements.ofNumber(elem).symbol()); |
| 246 | + .append(symbol); |
253 | 247 | else |
254 | | - sb.append(Elements.ofNumber(elem).symbol()); |
| 248 | + sb.append(symbol); |
255 | 249 | if (count != 0) |
256 | 250 | sb.append(count); |
257 | 251 | } |
@@ -1092,14 +1086,40 @@ public static IMolecularFormula getMolecularFormula(IAtomContainer atomContainer |
1092 | 1086 | * @see #getMolecularFormula(IAtomContainer) |
1093 | 1087 | */ |
1094 | 1088 | public static IMolecularFormula getMolecularFormula(IAtomContainer atomContainer, IMolecularFormula formula) { |
| 1089 | + |
| 1090 | + // mark multi-center attachments to be excluded from the formula |
| 1091 | + Set<IAtom> mattach = null; |
| 1092 | + List<Sgroup> sgroups = atomContainer.getProperty(CDKConstants.CTAB_SGROUPS); |
| 1093 | + if (sgroups != null) { |
| 1094 | + for (Sgroup sgroup : sgroups) { |
| 1095 | + if (sgroup.getType() == SgroupType.ExtMulticenter) { |
| 1096 | + for (IBond bond : sgroup.getBonds()) { |
| 1097 | + for (IAtom atom : sgroup.getAtoms()) { |
| 1098 | + if (bond.contains(atom)) { |
| 1099 | + if (mattach == null) |
| 1100 | + mattach = new HashSet<>(); |
| 1101 | + mattach.add(atom); |
| 1102 | + } |
| 1103 | + } |
| 1104 | + } |
| 1105 | + } |
| 1106 | + } |
| 1107 | + } |
| 1108 | + if (mattach == null) |
| 1109 | + mattach = Collections.emptySet(); |
| 1110 | + |
1095 | 1111 | int charge = 0; |
1096 | 1112 | int hcnt = 0; |
1097 | | - for (IAtom iAtom : atomContainer.atoms()) { |
1098 | | - formula.addIsotope(iAtom); |
1099 | | - if (iAtom.getFormalCharge() != null) |
1100 | | - charge += iAtom.getFormalCharge(); |
1101 | | - if (iAtom.getImplicitHydrogenCount() != null) |
1102 | | - hcnt += iAtom.getImplicitHydrogenCount(); |
| 1113 | + for (IAtom atm : atomContainer.atoms()) { |
| 1114 | + if ((atm instanceof IPseudoAtom && ((IPseudoAtom) atm).getAttachPointNum() != 0)) |
| 1115 | + continue; |
| 1116 | + if (mattach.contains(atm)) |
| 1117 | + continue; |
| 1118 | + formula.addIsotope(atm); |
| 1119 | + if (atm.getFormalCharge() != null) |
| 1120 | + charge += atm.getFormalCharge(); |
| 1121 | + if (atm.getImplicitHydrogenCount() != null) |
| 1122 | + hcnt += atm.getImplicitHydrogenCount(); |
1103 | 1123 | } |
1104 | 1124 | if (hcnt != 0) { |
1105 | 1125 | IAtom hAtom = atomContainer.getBuilder().newInstance(IAtom.class, "H"); |
|
0 commit comments