Skip to content

Commit

Permalink
Merge pull request #302 from cdk/bug/sgroupreplace
Browse files Browse the repository at this point in the history
Looks good to me.
  • Loading branch information
egonw committed Apr 23, 2017
2 parents 3b5a079 + bea78e7 commit d42c6c7
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 8 deletions.
Expand Up @@ -30,6 +30,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -59,6 +60,8 @@
import org.openscience.cdk.interfaces.IStereoElement;
import org.openscience.cdk.interfaces.ITetrahedralChirality;
import org.openscience.cdk.ringsearch.RingSearch;
import org.openscience.cdk.sgroup.Sgroup;
import org.openscience.cdk.sgroup.SgroupKey;
import org.openscience.cdk.stereo.DoubleBondStereochemistry;
import org.openscience.cdk.stereo.ExtendedTetrahedral;
import org.openscience.cdk.stereo.TetrahedralChirality;
Expand Down Expand Up @@ -174,6 +177,37 @@ public static boolean replaceAtomByAtom(final IAtomContainer container, final IA
stereoremapped.add(se.map(atomremap, bondremap));
container.setStereoElements(stereoremapped);

List<Sgroup> sgrougs = container.getProperty(CDKConstants.CTAB_SGROUPS);
if (sgrougs != null) {
boolean updated = false;
List<Sgroup> replaced = new ArrayList<>();
for (Sgroup org : sgrougs) {
if (org.getAtoms().contains(oldAtom)) {
updated = true;
Sgroup cpy = new Sgroup();
for (IAtom atom : org.getAtoms()) {
if (atom != oldAtom)
cpy.addAtom(atom);
else
cpy.addAtom(newAtom);
}
for (IBond bond : org.getBonds())
cpy.addBond(bond);
for (Sgroup parent : org.getParents())
cpy.addParent(parent);
for (SgroupKey key : org.getAttributeKeys())
cpy.putValue(key, org.getValue(key));
replaced.add(cpy);
} else {
replaced.add(org);
}
}
if (updated) {
container.setProperty(CDKConstants.CTAB_SGROUPS,
Collections.unmodifiableList(replaced));
}
}

return true;
}

Expand Down
Expand Up @@ -492,10 +492,6 @@ private IAtomContainer readAtomContainer(IAtomContainer molecule) throws CDKExce
// read potential SD file data between M END and $$$$
readNonStructuralData(input, outputContainer);

if (interpretHydrogenIsotopes.isSet()) {
fixHydrogenIsotopes(molecule, Isotopes.getInstance());
}

// note: apply the valence model last so that all fixes (i.e. hydrogen
// isotopes) are in place we need to use a offset as this atoms
// could be added to a molecule which already had atoms present
Expand Down Expand Up @@ -1283,7 +1279,20 @@ static int length(final String str) {
* @throws CDKException the symbol is not allowed
*/
private IAtom createAtom(String symbol, IChemObjectBuilder builder, int lineNum) throws CDKException {
if (isPeriodicElement(symbol)) return builder.newInstance(IAtom.class, symbol);
if (isPeriodicElement(symbol))
return builder.newInstance(IAtom.class, symbol);
if (symbol.equals("D") && interpretHydrogenIsotopes.isSet()) {
if (mode == Mode.STRICT) throw new CDKException("invalid symbol: " + symbol);
IAtom atom = builder.newInstance(IAtom.class, "H");
atom.setMassNumber(2);
return atom;
}
if (symbol.equals("T") && interpretHydrogenIsotopes.isSet()) {
if (mode == Mode.STRICT) throw new CDKException("invalid symbol: " + symbol);
IAtom atom = builder.newInstance(IAtom.class, "H");
atom.setMassNumber(3);
return atom;
}

if (!isPseudoElement(symbol)) {
handleError("invalid symbol: " + symbol, lineNum, 31, 34);
Expand Down
Expand Up @@ -907,7 +907,7 @@ public void testDeuteriumProperties() throws Exception {
reader.close();
IAtom deuterium = molecule.getAtom(molecule.getAtomCount() - 1);
assertTrue(1 == deuterium.getAtomicNumber());
assertTrue(2.014101778 == deuterium.getExactMass());
assertTrue(2 == deuterium.getMassNumber());
}

@Test
Expand Down Expand Up @@ -935,7 +935,7 @@ public void testTritiumProperties() throws Exception {
reader.close();
IAtom tritium = molecule.getAtom(molecule.getAtomCount() - 1);
assertTrue(1 == tritium.getAtomicNumber());
assertTrue(3.016049278 == tritium.getExactMass());
assertTrue(3 == tritium.getMassNumber());
}

/**
Expand Down
Expand Up @@ -31,6 +31,7 @@
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;

Expand Down Expand Up @@ -265,7 +266,7 @@ public int compare(PolymerSgroup a, PolymerSgroup b) {
sb.append(':');
sb.append(sgroup.subscript);
sb.append(':');
sb.append(sgroup.supscript);
sb.append(sgroup.supscript.toLowerCase(Locale.ROOT));
}
}

Expand Down
Expand Up @@ -865,6 +865,9 @@ else if (val == CxSmilesState.Radical.Trivalent)
case CtabMultipleGroup:
// display shortcuts are not output
break;
case CtabData:
// can be generated but currently ignored
break;
default:
throw new UnsupportedOperationException("Unsupported Sgroup Polymer");

Expand Down
Expand Up @@ -24,7 +24,13 @@
package org.openscience.cdk.smiles;

import org.junit.Test;
import org.openscience.cdk.AtomContainer;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.io.MDLV2000Reader;
import org.openscience.cdk.silent.SilentChemObjectBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -82,4 +88,27 @@ public void radicals() {
is(" |^1:1,5,^2:3|"));
}

/**
* Integration - test used to fail because the D (pseudo) was swapped out with a 2H after Sgroups were
* initialized.
*/
@Test
public void chebi53695() throws Exception {
try (InputStream in = getClass().getResourceAsStream("CHEBI_53695.mol");
MDLV2000Reader mdlr = new MDLV2000Reader(in)) {
IAtomContainer mol = mdlr.read(SilentChemObjectBuilder.getInstance().newInstance(IAtomContainer.class));
SmilesGenerator smigen = new SmilesGenerator(SmiFlavor.CxSmiles | SmiFlavor.AtomicMassStrict);
assertThat(smigen.create(mol),
is("C(C(=O)OC)(C*)*C(C(C1=C(C(=C(C(=C1[2H])[2H])[2H])[2H])[2H])(*)[2H])([2H])[2H] |Sg:n:0,1,2,3,4,5:n:ht,Sg:n:8,9,10,11,12,13,14,15,16,17,18,19,20,22,23,24:m:ht|"));
}
}


@Test public void chembl367774() throws Exception {
try (MDLV2000Reader mdlr = new MDLV2000Reader(getClass().getResourceAsStream("CHEMBL367774.mol"))) {
IAtomContainer container = mdlr.read(new AtomContainer());
SmilesGenerator smigen = new SmilesGenerator(SmiFlavor.CxSmiles);
assertThat(smigen.create(container), is("OC(=O)C1=CC(F)=CC=2NC(=NC12)C3=CC=C(C=C3F)C4=CC=CC=C4"));
}
}
}
@@ -0,0 +1,69 @@

Marvin 11260917092D

25 25 0 0 0 0 999 V2000
-2.3138 0.5318 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-1.5321 1.0607 0.0000 * 0 0 0 0 0 0 0 0 0 0 0 0
-0.8177 1.4732 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.1032 1.0607 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.1032 0.2357 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.8177 -0.1768 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.8177 -1.0018 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.1032 -1.4143 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.6113 -1.0018 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.6113 -0.1768 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.3011 1.0694 0.0000 * 0 0 0 0 0 0 0 0 0 0 0 0
-2.3138 -0.2932 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-1.5994 -0.7057 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
-3.0283 -0.7057 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
-3.7428 -0.2932 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-3.0283 0.9443 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-4.0624 0.9356 0.0000 * 0 0 0 0 0 0 0 0 0 0 0 0
-0.2343 2.0566 0.0000 D 0 0 0 0 0 0 0 0 0 0 0 0
1.3258 0.2357 0.0000 D 0 0 0 0 0 0 0 0 0 0 0 0
1.3258 -1.4143 0.0000 D 0 0 0 0 0 0 0 0 0 0 0 0
-0.1032 -2.2393 0.0000 D 0 0 0 0 0 0 0 0 0 0 0 0
-1.5322 -1.4143 0.0000 D 0 0 0 0 0 0 0 0 0 0 0 0
-1.5322 0.2357 0.0000 D 0 0 0 0 0 0 0 0 0 0 0 0
-1.4011 2.0566 0.0000 D 0 0 0 0 0 0 0 0 0 0 0 0
0.3599 1.8595 0.0000 D 0 0 0 0 0 0 0 0 0 0 0 0
2 3 1 0 0 0 0
3 4 1 0 0 0 0
4 5 1 0 0 0 0
5 6 2 0 0 0 0
5 10 1 0 0 0 0
6 7 1 0 0 0 0
7 8 2 0 0 0 0
8 9 1 0 0 0 0
9 10 2 0 0 0 0
4 11 1 0 0 0 0
1 12 1 0 0 0 0
12 13 2 0 0 0 0
12 14 1 0 0 0 0
14 15 1 0 0 0 0
1 16 1 0 0 0 0
1 2 1 0 0 0 0
16 17 1 0 0 0 0
3 18 1 0 0 0 0
10 19 1 0 0 0 0
9 20 1 0 0 0 0
8 21 1 0 0 0 0
7 22 1 0 0 0 0
6 23 1 0 0 0 0
3 24 1 0 0 0 0
4 25 1 0 0 0 0
M STY 2 1 SRU 2 SRU
M SCN 1 1 HT
M SAL 1 6 1 12 13 14 15 16
M SDI 1 4 -3.4506 0.5282 -3.4576 1.3532
M SDI 1 4 -2.2231 1.0912 -1.7608 0.4079
M SBL 1 2 16 17
M SMT 1 n
M SCN 1 2 HT
M SAL 2 15 3 4 5 6 7 8 9 10 18 19 20 21 22 23 24
M SAL 2 1 25
M SDI 2 4 0.4725 1.4768 0.4776 0.6518
M SDI 2 4 -0.9056 0.9461 -1.3181 1.6606
M SBL 2 2 1 10
M SMT 2 m
M END
@@ -0,0 +1,70 @@

SciTegic05171617282D

26 29 0 0 0 0 999 V2000
0.0446 3.6095 0.0000 O 0 0
-0.9971 3.0138 0.0000 C 0 0
-2.0337 3.6184 0.0000 O 0 0
-1.0028 1.5132 0.0000 C 0 0
-2.3155 0.7475 0.0000 C 0 0
-2.3155 -0.7475 0.0000 C 0 0
-3.3560 -1.3452 0.0000 F 0 0
-1.0028 -1.5132 0.0000 C 0 0
0.2917 -0.7475 0.0000 C 0 0
1.7138 -1.2033 0.0000 N 0 0
2.5889 0.0182 0.0000 C 0 0
1.7138 1.2033 0.0000 N 0 0
0.2917 0.7475 0.0000 C 0 0
4.0896 0.0290 0.0000 C 0 0
4.7453 -1.3220 0.0000 C 0 0
6.2418 -1.4248 0.0000 C 0 0
7.0791 -0.1803 0.0000 C 0 0
6.4199 1.1671 0.0000 C 0 0
4.9234 1.2700 0.0000 C 0 0
4.3957 2.3477 0.0000 F 0 0
8.5763 -0.2831 0.0000 C 0 0
9.2377 -1.6295 0.0000 C 0 0
10.7344 -1.7299 0.0000 C 0 0
11.5697 -0.4840 0.0000 C 0 0
10.9083 0.8623 0.0000 C 0 0
9.4117 0.9628 0.0000 C 0 0
1 2 1 0
2 3 2 0
2 4 1 0
4 5 2 0
5 6 1 0
6 7 1 0
6 8 2 0
8 9 1 0
9 10 1 0
10 11 1 0
11 12 2 0
12 13 1 0
4 13 1 0
9 13 2 0
11 14 1 0
14 15 2 0
15 16 1 0
16 17 2 0
17 18 1 0
18 19 2 0
14 19 1 0
19 20 1 0
17 21 1 0
21 22 2 0
22 23 1 0
23 24 2 0
24 25 1 0
25 26 2 0
21 26 1 0
M STY 2 1 DAT 2 DAT
M SLB 2 1 1 2 2
M SAL 1 1 10
M SDT 1 MRV_IMPLICIT_H
M SDD 1 0.5304 -0.4125 DR ALL 0 0
M SED 1 IMPL_H1
M SAL 2 1 12
M SDT 2 MRV_IMPLICIT_H
M SDD 2 0.5304 -0.4125 DR ALL 0 0
M SED 2 IMPL_H1
M END

0 comments on commit d42c6c7

Please sign in to comment.