/// <summary> /// Get the topological weight factor for each atomContainer. /// </summary> /// <param name="atomContainer">The IAtomContainer to study.</param> /// <param name="ac">The IAtomContainer to study.</param> /// <returns>The value</returns> private static double GetTopologicalFactors(IAtomContainer atomContainer, IAtomContainer ac) { /* factor for separation of charge */ int totalNCharge1 = AtomContainerManipulator.GetTotalNegativeFormalCharge(atomContainer); int totalPCharge1 = AtomContainerManipulator.GetTotalPositiveFormalCharge(atomContainer); double fQ = 1.0; if (totalNCharge1 != 0.0) { fQ = 0.5; for (int i = 0; i < atomContainer.Bonds.Count; i++) { IBond bond = atomContainer.Bonds[i]; if (bond.Atoms[0].FormalCharge != 0.0 && bond.Atoms[1].FormalCharge != 0.0) { fQ = 0.25; break; } } } /* factor, if the number of covalents bonds is decreased */ double fB = 1.0; int numBond1 = 0; int numBond2 = 0; for (int i = 0; i < atomContainer.Bonds.Count; i++) { if (atomContainer.Bonds[i].Order == BondOrder.Double) { numBond1 += 1; } if (ac.Bonds[i].Order == BondOrder.Double) { numBond2 += 1; } } if (numBond1 < /* > */ numBond2) { fB = 0.8; } double fPlus = 1.0; if (totalNCharge1 == 0.0 && totalPCharge1 == 0.0) { fPlus = 0.1; } /* aromatic */ double fA = 1.0; try { if (Aromaticity.CDKLegacy.Apply(ac)) { if (!Aromaticity.CDKLegacy.Apply(atomContainer)) { fA = 0.3; } } } catch (CDKException e) { Console.Out.WriteLine(e.StackTrace); } Debug.WriteLine("return " + fQ * fB * fPlus * fA + "= sp:" + fQ + ", dc:" + fB + ", fPlus:" + fPlus + ", fA:" + fA); return(fQ * fB * fPlus * fA); }
/// <summary> /// Initiates the process for the given mechanism. The atoms and bonds to apply are mapped between /// reactants and products. /// </summary> /// <param name="atomContainerSet"></param> /// <param name="atomList">The list of atoms taking part in the mechanism. Only allowed three atoms</param> /// <param name="bondList">The list of bonds taking part in the mechanism. Only allowed one bond</param> /// <returns>The Reaction mechanism</returns> public IReaction Initiate(IChemObjectSet <IAtomContainer> atomContainerSet, IList <IAtom> atomList, IList <IBond> bondList) { var atMatcher = CDK.AtomTypeMatcher; if (atomContainerSet.Count != 2) { throw new CDKException("AdductionPBMechanism expects two IAtomContainer's"); } if (atomList.Count != 3) { throw new CDKException("AdductionPBMechanism expects two atoms in the List"); } if (bondList.Count != 1) { throw new CDKException("AdductionPBMechanism don't expect bonds in the List"); } IAtomContainer molecule1 = atomContainerSet[0]; IAtomContainer molecule2 = atomContainerSet[1]; IAtomContainer reactantCloned; reactantCloned = (IAtomContainer)atomContainerSet[0].Clone(); reactantCloned.Add((IAtomContainer)atomContainerSet[1].Clone()); IAtom atom1 = atomList[0]; // Atom 1: to be deficient in charge IAtom atom1C = reactantCloned.Atoms[molecule1.Atoms.IndexOf(atom1)]; IAtom atom2 = atomList[1]; // Atom 2: receive the adduct IAtom atom2C = reactantCloned.Atoms[molecule1.Atoms.IndexOf(atom2)]; IAtom atom3 = atomList[2]; // Atom 2: deficient in charge IAtom atom3C = reactantCloned.Atoms[molecule1.Atoms.Count + molecule2.Atoms.IndexOf(atom3)]; IBond bond1 = bondList[0]; int posBond1 = atomContainerSet[0].Bonds.IndexOf(bond1); BondManipulator.DecreaseBondOrder(reactantCloned.Bonds[posBond1]); IBond newBond = molecule1.Builder.NewBond(atom2C, atom3C, BondOrder.Single); reactantCloned.Bonds.Add(newBond); int charge = atom1C.FormalCharge.Value; atom1C.FormalCharge = charge + 1; atom1C.Hybridization = Hybridization.Unset; AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(reactantCloned); IAtomType type = atMatcher.FindMatchingAtomType(reactantCloned, atom1C); if (type == null || type.AtomTypeName.Equals("X", StringComparison.Ordinal)) { return(null); } atom2C.Hybridization = Hybridization.Unset; AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(reactantCloned); type = atMatcher.FindMatchingAtomType(reactantCloned, atom2C); if (type == null || type.AtomTypeName.Equals("X", StringComparison.Ordinal)) { return(null); } charge = atom3C.FormalCharge.Value; atom3C.FormalCharge = charge - 1; atom3C.Hybridization = Hybridization.Unset; AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(reactantCloned); type = atMatcher.FindMatchingAtomType(reactantCloned, atom3C); if (type == null || type.AtomTypeName.Equals("X", StringComparison.Ordinal)) { return(null); } IReaction reaction = atom1C.Builder.NewReaction(); reaction.Reactants.Add(molecule1); /* mapping */ foreach (var atom in molecule1.Atoms) { IMapping mapping = atom1C.Builder.NewMapping(atom, reactantCloned.Atoms[molecule1.Atoms.IndexOf(atom)]); reaction.Mappings.Add(mapping); } foreach (var atom in molecule2.Atoms) { IMapping mapping = atom1C.Builder.NewMapping(atom, reactantCloned.Atoms[molecule2.Atoms.IndexOf(atom)]); reaction.Mappings.Add(mapping); } reaction.Products.Add(reactantCloned); return(reaction); }
public void Test1Sphere() { string[] result = { "O-1;=C(//)", "C-3;=OCC(//)", "C-3;=CC(//)", "C-3;=CC(//)", "C-3;*C*CC(//)", "C-3;*C*C(//)", "C-3;*C*C(//)", "C-3;*C*CC(//)", "C-3;*C*CC(//)", "C-3;*C*C(//)", "C-3;*C*C(//)", "C-3;*C*C(//)", "C-3;*C*C(//)", "C-3;*C*CO(//)", "O-2;CC(//)", "C-3;*C*CO(//)", "C-3;*C*CO(//)", "O-2;CC(//)", "C-4;O(//)", "C-3;*C*C(//)", "C-3;*C*CC(//)", "C-3;*C*C*C(//)", "C-3;*C*C*C(//)" }; var mol = builder.NewAtomContainer(); IAtom a1 = mol.Builder.NewAtom("O"); a1.Point2D = new Vector2(502.88457268119913, 730.4999999999999); mol.Atoms.Add(a1); IAtom a2 = mol.Builder.NewAtom("C"); a2.Point2D = new Vector2(502.8845726811991, 694.4999999999999); mol.Atoms.Add(a2); IAtom a3 = mol.Builder.NewAtom("C"); a3.Point2D = new Vector2(534.0614872174388, 676.4999999999999); mol.Atoms.Add(a3); IAtom a4 = mol.Builder.NewAtom("C"); a4.Point2D = new Vector2(534.0614872174388, 640.4999999999999); mol.Atoms.Add(a4); IAtom a5 = mol.Builder.NewAtom("C"); a5.Point2D = new Vector2(502.8845726811991, 622.4999999999999); mol.Atoms.Add(a5); IAtom a6 = mol.Builder.NewAtom("C"); a6.Point2D = new Vector2(502.8845726811991, 586.4999999999999); mol.Atoms.Add(a6); IAtom a7 = mol.Builder.NewAtom("C"); a7.Point2D = new Vector2(471.7076581449593, 568.4999999999999); mol.Atoms.Add(a7); IAtom a8 = mol.Builder.NewAtom("C"); a8.Point2D = new Vector2(440.5307436087194, 586.5); mol.Atoms.Add(a8); IAtom a9 = mol.Builder.NewAtom("C"); a9.Point2D = new Vector2(409.35382907247964, 568.5); mol.Atoms.Add(a9); IAtom a10 = mol.Builder.NewAtom("C"); a10.Point2D = new Vector2(409.3538290724796, 532.5); mol.Atoms.Add(a10); IAtom a11 = mol.Builder.NewAtom("C"); a11.Point2D = new Vector2(378.1769145362398, 514.5); mol.Atoms.Add(a11); IAtom a12 = mol.Builder.NewAtom("C"); a12.Point2D = new Vector2(347.0, 532.5); mol.Atoms.Add(a12); IAtom a13 = mol.Builder.NewAtom("C"); a13.Point2D = new Vector2(347.0, 568.5); mol.Atoms.Add(a13); IAtom a14 = mol.Builder.NewAtom("C"); a14.Point2D = new Vector2(378.17691453623985, 586.5); mol.Atoms.Add(a14); IAtom a15 = mol.Builder.NewAtom("O"); a15.Point2D = new Vector2(378.17691453623985, 622.5); mol.Atoms.Add(a15); IAtom a16 = mol.Builder.NewAtom("C"); a16.Point2D = new Vector2(409.3538290724797, 640.5); mol.Atoms.Add(a16); IAtom a17 = mol.Builder.NewAtom("C"); a17.Point2D = new Vector2(409.3538290724797, 676.5); mol.Atoms.Add(a17); IAtom a18 = mol.Builder.NewAtom("O"); a18.Point2D = new Vector2(378.17691453623996, 694.5); mol.Atoms.Add(a18); IAtom a19 = mol.Builder.NewAtom("C"); a19.Point2D = new Vector2(378.17691453624, 730.5); mol.Atoms.Add(a19); IAtom a20 = mol.Builder.NewAtom("C"); a20.Point2D = new Vector2(440.5307436087195, 694.4999999999999); mol.Atoms.Add(a20); IAtom a21 = mol.Builder.NewAtom("C"); a21.Point2D = new Vector2(471.7076581449593, 676.4999999999999); mol.Atoms.Add(a21); IAtom a22 = mol.Builder.NewAtom("C"); a22.Point2D = new Vector2(471.7076581449593, 640.4999999999999); mol.Atoms.Add(a22); IAtom a23 = mol.Builder.NewAtom("C"); a23.Point2D = new Vector2(440.53074360871943, 622.4999999999999); mol.Atoms.Add(a23); IBond b1 = mol.Builder.NewBond(a2, a1, BondOrder.Double); mol.Bonds.Add(b1); IBond b2 = mol.Builder.NewBond(a3, a2, BondOrder.Single); mol.Bonds.Add(b2); IBond b3 = mol.Builder.NewBond(a4, a3, BondOrder.Double); mol.Bonds.Add(b3); IBond b4 = mol.Builder.NewBond(a5, a4, BondOrder.Single); mol.Bonds.Add(b4); IBond b5 = mol.Builder.NewBond(a6, a5, BondOrder.Single); mol.Bonds.Add(b5); IBond b6 = mol.Builder.NewBond(a7, a6, BondOrder.Double); mol.Bonds.Add(b6); IBond b7 = mol.Builder.NewBond(a8, a7, BondOrder.Single); mol.Bonds.Add(b7); IBond b8 = mol.Builder.NewBond(a9, a8, BondOrder.Single); mol.Bonds.Add(b8); IBond b9 = mol.Builder.NewBond(a10, a9, BondOrder.Single); mol.Bonds.Add(b9); IBond b10 = mol.Builder.NewBond(a11, a10, BondOrder.Double); mol.Bonds.Add(b10); IBond b11 = mol.Builder.NewBond(a12, a11, BondOrder.Single); mol.Bonds.Add(b11); IBond b12 = mol.Builder.NewBond(a13, a12, BondOrder.Double); mol.Bonds.Add(b12); IBond b13 = mol.Builder.NewBond(a14, a13, BondOrder.Single); mol.Bonds.Add(b13); IBond b14 = mol.Builder.NewBond(a14, a9, BondOrder.Double); mol.Bonds.Add(b14); IBond b15 = mol.Builder.NewBond(a15, a14, BondOrder.Single); mol.Bonds.Add(b15); IBond b16 = mol.Builder.NewBond(a16, a15, BondOrder.Single); mol.Bonds.Add(b16); IBond b17 = mol.Builder.NewBond(a17, a16, BondOrder.Double); mol.Bonds.Add(b17); IBond b18 = mol.Builder.NewBond(a18, a17, BondOrder.Single); mol.Bonds.Add(b18); IBond b19 = mol.Builder.NewBond(a19, a18, BondOrder.Single); mol.Bonds.Add(b19); IBond b20 = mol.Builder.NewBond(a20, a17, BondOrder.Single); mol.Bonds.Add(b20); IBond b21 = mol.Builder.NewBond(a21, a20, BondOrder.Double); mol.Bonds.Add(b21); IBond b22 = mol.Builder.NewBond(a21, a2, BondOrder.Single); mol.Bonds.Add(b22); IBond b23 = mol.Builder.NewBond(a22, a21, BondOrder.Single); mol.Bonds.Add(b23); IBond b24 = mol.Builder.NewBond(a22, a5, BondOrder.Double); mol.Bonds.Add(b24); IBond b25 = mol.Builder.NewBond(a23, a22, BondOrder.Single); mol.Bonds.Add(b25); IBond b26 = mol.Builder.NewBond(a23, a16, BondOrder.Single); mol.Bonds.Add(b26); IBond b27 = mol.Builder.NewBond(a23, a8, BondOrder.Double); mol.Bonds.Add(b27); AddImplicitHydrogens(mol); //MoleculeViewer2D.Display(molecule, true); AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(mol); Aromaticity.CDKLegacy.Apply(mol); HOSECodeGenerator hcg = new HOSECodeGenerator(); string s = null; for (int f = 0; f < 23; f++) { s = hcg.GetHOSECode(mol, mol.Atoms[f], 1); if (standAlone) { Console.Out.Write("|" + s + "| -> " + result[f]); } Assert.AreEqual(result[f], s); if (standAlone) { Console.Out.WriteLine(" OK"); } } }
public void TestButadiene() { var mol = new AtomContainer(); var carbon = new AtomType(ChemicalElement.C); var a0 = new Atom("C") { Hybridization = Hybridization.SP2, ImplicitHydrogenCount = 2 }; AtomTypeManipulator.ConfigureUnsetProperties(a0, carbon); var a1 = new Atom("C") { Hybridization = Hybridization.SP2, ImplicitHydrogenCount = 1 }; AtomTypeManipulator.ConfigureUnsetProperties(a1, carbon); var a2 = new Atom("C") { Hybridization = Hybridization.SP2, ImplicitHydrogenCount = 1 }; AtomTypeManipulator.ConfigureUnsetProperties(a2, carbon); var a3 = new Atom("C") { Hybridization = Hybridization.SP2, ImplicitHydrogenCount = 2 }; AtomTypeManipulator.ConfigureUnsetProperties(a3, carbon); mol.Atoms.Add(a0); mol.Atoms.Add(a1); mol.Atoms.Add(a2); mol.Atoms.Add(a3); var b0 = new Bond(a0, a1) { IsSingleOrDouble = true }; mol.Bonds.Add(b0); var b1 = new Bond(a1, a2) { IsSingleOrDouble = true }; mol.Bonds.Add(b1); IBond b2 = new Bond(a2, a3) { IsSingleOrDouble = true }; mol.Bonds.Add(b2); AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(mol); atasc.DecideBondOrder(mol, true); Assert.AreEqual(BondOrder.Double, mol.Bonds[0].Order); Assert.AreEqual(BondOrder.Single, mol.Bonds[1].Order); Assert.AreEqual(BondOrder.Double, mol.Bonds[2].Order); }
public static IReadOnlyList <IBitFingerprint> MakeFingerprintsFromSdf(bool anyAtom, bool anyAtomAnyBond, Dictionary <string, int> timings, TextReader fin, int limit) { var fingerPrinter = new HybridizationFingerprinter(HybridizationFingerprinter.DefaultSize, HybridizationFingerprinter.DefaultSearchDepth); fingerPrinter.SetHashPseudoAtoms(true); IAtomContainer query = null; var data = new List <IBitFingerprint>(); try { Trace.TraceInformation("Read data file in ..."); using (var imdl = new EnumerableSDFReader(fin, builder)) { Trace.TraceInformation("ready"); int moleculeCounter = 0; int fingerprintCounter = 0; Trace.TraceInformation($"Generated Fingerprints: {fingerprintCounter} "); foreach (var m in imdl) { if (!(moleculeCounter < limit || limit == -1)) { break; } moleculeCounter++; if (anyAtom && !anyAtomAnyBond) { query = QueryAtomContainerCreator.CreateAnyAtomContainer(m, false); } else { query = AtomContainerManipulator.Anonymise(m); } try { var time = -DateTime.Now.Ticks / 10000; if (anyAtom || anyAtomAnyBond) { data.Add(fingerPrinter.GetBitFingerprint(query)); fingerprintCounter = fingerprintCounter + 1; } else { data.Add(fingerPrinter.GetBitFingerprint(query)); fingerprintCounter = fingerprintCounter + 1; } time += (DateTime.Now.Ticks / 10000); // store the time var bin = ((int)Math.Floor(time / 10.0)).ToString(NumberFormatInfo.InvariantInfo); if (timings.ContainsKey(bin)) { timings[bin] = (timings[bin]) + 1; } else { timings[bin] = 1; } } catch (Exception exc1) { Trace.TraceInformation($"QueryFingerprintError: from molecule:{moleculeCounter} due to:{exc1.Message}"); // OK, just adds a fingerprint with all ones, so that any // structure will match this template, and leave it up // to substructure match to figure things out var allOnesFingerprint = new BitSetFingerprint(fingerPrinter.Length); for (int i = 0; i < fingerPrinter.Length; i++) { allOnesFingerprint.Set(i); } data.Add(allOnesFingerprint); fingerprintCounter = fingerprintCounter + 1; } if (fingerprintCounter % 2 == 0) { Trace.TraceInformation("\b" + "/"); } else { Trace.TraceInformation("\b" + "\\"); } if (fingerprintCounter % 100 == 0) { Trace.TraceInformation("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" + "Generated Fingerprints: " + fingerprintCounter + " \n"); } }// while Trace.TraceInformation($"...ready with:{moleculeCounter} molecules\nWrite data...of data vector:{data.Count} fingerprintCounter:{fingerprintCounter}"); } } catch (Exception exc) { Console.Out.WriteLine("Could not read Molecules from file" + " due to: " + exc.Message); } return(data); }
public Result Calculate(IAtomContainer container) { // we don't make a clone, since removeHydrogens returns a deep copy container = AtomContainerManipulator.RemoveHydrogens(container); var matcher = CDK.AtomTypeMatcher; foreach (var atom in container.Atoms) { var type = matcher.FindMatchingAtomType(container, atom); AtomTypeManipulator.Configure(atom, type); } var hAdder = CDK.HydrogenAdder; hAdder.AddImplicitHydrogens(container); var subgraph0 = Order0(container); var subgraph1 = Order1(container); var subgraph2 = Order2(container); var subgraph3 = Order3(container); var subgraph4 = Order4(container); var subgraph5 = Order5(container); var subgraph6 = Order6(container); var subgraph7 = Order7(container); try { var order0s = ChiIndexUtils.EvalSimpleIndex(container, subgraph0); var order1s = ChiIndexUtils.EvalSimpleIndex(container, subgraph1); var order2s = ChiIndexUtils.EvalSimpleIndex(container, subgraph2); var order3s = ChiIndexUtils.EvalSimpleIndex(container, subgraph3); var order4s = ChiIndexUtils.EvalSimpleIndex(container, subgraph4); var order5s = ChiIndexUtils.EvalSimpleIndex(container, subgraph5); var order6s = ChiIndexUtils.EvalSimpleIndex(container, subgraph6); var order7s = ChiIndexUtils.EvalSimpleIndex(container, subgraph7); var order0v = ChiIndexUtils.EvalValenceIndex(container, subgraph0); var order1v = ChiIndexUtils.EvalValenceIndex(container, subgraph1); var order2v = ChiIndexUtils.EvalValenceIndex(container, subgraph2); var order3v = ChiIndexUtils.EvalValenceIndex(container, subgraph3); var order4v = ChiIndexUtils.EvalValenceIndex(container, subgraph4); var order5v = ChiIndexUtils.EvalValenceIndex(container, subgraph5); var order6v = ChiIndexUtils.EvalValenceIndex(container, subgraph6); var order7v = ChiIndexUtils.EvalValenceIndex(container, subgraph7); return(new Result( order0s, order1s, order2s, order3s, order4s, order5s, order6s, order7s, order0v, order1v, order2v, order3v, order4v, order5v, order6v, order7v)); } catch (CDKException e) { return(new Result(e)); } }
public override void TestInitiate_IAtomContainerSet_IAtomContainerSet() { //CreateFromSmiles("CC=C") IAtomContainer molecule = builder.NewAtomContainer(); molecule.Atoms.Add(builder.NewAtom("C")); molecule.Atoms.Add(builder.NewAtom("C")); molecule.Atoms.Add(builder.NewAtom("C")); molecule.AddBond(molecule.Atoms[0], molecule.Atoms[1], BondOrder.Single); molecule.AddBond(molecule.Atoms[1], molecule.Atoms[2], BondOrder.Double); molecule.Atoms.Add(builder.NewAtom("H")); molecule.Atoms.Add(builder.NewAtom("H")); molecule.Atoms.Add(builder.NewAtom("H")); molecule.Atoms.Add(builder.NewAtom("H")); molecule.Atoms.Add(builder.NewAtom("H")); molecule.Atoms.Add(builder.NewAtom("H")); molecule.AddBond(molecule.Atoms[0], molecule.Atoms[3], BondOrder.Single); molecule.AddBond(molecule.Atoms[0], molecule.Atoms[4], BondOrder.Single); molecule.AddBond(molecule.Atoms[0], molecule.Atoms[5], BondOrder.Single); molecule.AddBond(molecule.Atoms[1], molecule.Atoms[6], BondOrder.Single); molecule.AddBond(molecule.Atoms[2], molecule.Atoms[7], BondOrder.Single); molecule.AddBond(molecule.Atoms[2], molecule.Atoms[8], BondOrder.Single); AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(molecule); molecule.Atoms[1].IsReactiveCenter = true; molecule.Atoms[2].IsReactiveCenter = true; molecule.Bonds[1].IsReactiveCenter = true; var setOfReactants = ChemObjectBuilder.Instance.NewAtomContainerSet(); setOfReactants.Add(molecule); IReactionProcess type = new HeterolyticCleavagePBReaction(); var paramList = new List <IParameterReaction>(); var param = new SetReactionCenter(); param.IsSetParameter = true; paramList.Add(param); type.ParameterList = paramList; /* initiate */ var setOfReactions = type.Initiate(setOfReactants, null); Assert.AreEqual(2, setOfReactions.Count); // expected products //CreateFromSmiles("C[C+][C-]") IAtomContainer expected1 = builder.NewAtomContainer(); expected1.Atoms.Add(builder.NewAtom("C")); expected1.Atoms.Add(builder.NewAtom("C")); expected1.Atoms[1].FormalCharge = +1; expected1.Atoms.Add(builder.NewAtom("C")); expected1.Atoms[2].FormalCharge = -1; expected1.AddBond(expected1.Atoms[0], expected1.Atoms[1], BondOrder.Single); expected1.AddBond(expected1.Atoms[1], expected1.Atoms[2], BondOrder.Single); expected1.Atoms.Add(builder.NewAtom("H")); expected1.Atoms.Add(builder.NewAtom("H")); expected1.Atoms.Add(builder.NewAtom("H")); expected1.Atoms.Add(builder.NewAtom("H")); expected1.Atoms.Add(builder.NewAtom("H")); expected1.Atoms.Add(builder.NewAtom("H")); expected1.AddBond(expected1.Atoms[0], expected1.Atoms[3], BondOrder.Single); expected1.AddBond(expected1.Atoms[0], expected1.Atoms[4], BondOrder.Single); expected1.AddBond(expected1.Atoms[0], expected1.Atoms[5], BondOrder.Single); expected1.AddBond(expected1.Atoms[1], expected1.Atoms[6], BondOrder.Single); expected1.AddBond(expected1.Atoms[2], expected1.Atoms[7], BondOrder.Single); expected1.AddBond(expected1.Atoms[2], expected1.Atoms[8], BondOrder.Single); AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(expected1); CDK.LonePairElectronChecker.Saturate(expected1); IAtomContainer product1 = setOfReactions[0].Products[0]; QueryAtomContainer queryAtom = QueryAtomContainerCreator.CreateSymbolAndChargeQueryContainer(expected1); Assert.IsTrue(uiTester.IsIsomorph(product1, queryAtom)); //CreateFromSmiles("C[C-][C+]") expected1.Atoms[1].FormalCharge = -1; expected1.Atoms[2].FormalCharge = +1; AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(expected1); CDK.LonePairElectronChecker.Saturate(expected1); product1 = setOfReactions[1].Products[0]; queryAtom = QueryAtomContainerCreator.CreateSymbolAndChargeQueryContainer(expected1); Assert.IsTrue(uiTester.IsIsomorph(product1, queryAtom)); }
/// <summary> /// Transfers the CXSMILES state onto the CDK atom/molecule data-structures. /// </summary> /// <param name="bldr">chem-object builder</param> /// <param name="atoms">atoms parsed from the molecule or reaction. Reaction molecules are list left to right.</param> /// <param name="atomToMol">look-up of atoms to molecules when connectivity/sgroups need modification</param> /// <param name="cxstate">the CXSMILES state to read from</param> private void AssignCxSmilesInfo(IChemObjectBuilder bldr, IChemObject chemObj, List <IAtom> atoms, Dictionary <IAtom, IAtomContainer> atomToMol, CxSmilesState cxstate) { // atom-labels - must be done first as we replace atoms if (cxstate.atomLabels != null) { foreach (var e in cxstate.atomLabels) { // bounds check if (e.Key >= atoms.Count) { continue; } var old = atoms[e.Key]; var pseudo = bldr.NewPseudoAtom(); var val = e.Value; // specialised label handling if (val.EndsWith("_p", StringComparison.Ordinal)) // pseudo label { val = val.Substring(0, val.Length - 2); } else if (val.StartsWith("_AP", StringComparison.Ordinal)) // attachment point { pseudo.AttachPointNum = ParseIntSafe(val.Substring(3)); } pseudo.Label = val; pseudo.AtomicNumber = 0; pseudo.ImplicitHydrogenCount = 0; var mol = atomToMol[old]; AtomContainerManipulator.ReplaceAtomByAtom(mol, old, pseudo); atomToMol.Add(pseudo, mol); atoms[e.Key] = pseudo; } } // atom-values - set as comment, mirrors Molfile reading behavior if (cxstate.atomValues != null) { foreach (var e in cxstate.atomValues) { atoms[e.Key].SetProperty(CDKPropertyName.Comment, e.Value); } } // atom-coordinates if (cxstate.atomCoords != null) { var numAtoms = atoms.Count; var numCoords = cxstate.atomCoords.Count; var lim = Math.Min(numAtoms, numCoords); if (cxstate.coordFlag) { for (int i = 0; i < lim; i++) { atoms[i].Point3D = new Vector3( cxstate.atomCoords[i][0], cxstate.atomCoords[i][1], cxstate.atomCoords[i][2]); } } else { for (int i = 0; i < lim; i++) { atoms[i].Point2D = new Vector2( cxstate.atomCoords[i][0], cxstate.atomCoords[i][1]); } } } // atom radicals if (cxstate.atomRads != null) { foreach (var e in cxstate.atomRads) { // bounds check if (e.Key >= atoms.Count) { continue; } int count = 0; var aa = e.Value; switch (e.Value) { case CxSmilesState.Radical.Monovalent: count = 1; break; // no distinction in CDK between singled/triplet case CxSmilesState.Radical.Divalent: case CxSmilesState.Radical.DivalentSinglet: case CxSmilesState.Radical.DivalentTriplet: count = 2; break; // no distinction in CDK between doublet/quartet case CxSmilesState.Radical.Trivalent: case CxSmilesState.Radical.TrivalentDoublet: case CxSmilesState.Radical.TrivalentQuartet: count = 3; break; } var atom = atoms[e.Key]; var mol = atomToMol[atom]; while (count-- > 0) { mol.SingleElectrons.Add(bldr.NewSingleElectron(atom)); } } } var sgroupMap = new MultiDictionary <IAtomContainer, Sgroup>(); // positional-variation if (cxstate.positionVar != null) { foreach (var e in cxstate.positionVar) { var sgroup = new Sgroup { Type = SgroupType.ExtMulticenter }; var beg = atoms[e.Key]; var mol = atomToMol[beg]; var bonds = mol.GetConnectedBonds(beg); if (bonds.Count() == 0) { continue; // bad } sgroup.Add(beg); sgroup.Add(bonds.First()); foreach (var endpt in e.Value) { sgroup.Add(atoms[endpt]); } sgroupMap.Add(mol, sgroup); } } // data sgroups if (cxstate.dataSgroups != null) { foreach (var dsgroup in cxstate.dataSgroups) { if (dsgroup.Field != null && dsgroup.Field.StartsWith("cdk:", StringComparison.Ordinal)) { chemObj.SetProperty(dsgroup.Field, dsgroup.Value); } } } // polymer Sgroups if (cxstate.sgroups != null) { foreach (var psgroup in cxstate.sgroups) { var sgroup = new Sgroup(); var atomset = new HashSet <IAtom>(); IAtomContainer mol = null; foreach (var idx in psgroup.AtomSet) { if (idx >= atoms.Count) { continue; } var atom = atoms[idx]; var amol = atomToMol[atom]; if (mol == null) { mol = amol; } else if (amol != mol) { goto C_PolySgroup; } atomset.Add(atom); } if (mol == null) { continue; } foreach (var atom in atomset) { foreach (var bond in mol.GetConnectedBonds(atom)) { if (!atomset.Contains(bond.GetOther(atom))) { sgroup.Add(bond); } } sgroup.Add(atom); } sgroup.Subscript = psgroup.Subscript; sgroup.PutValue(SgroupKey.CtabConnectivity, psgroup.Supscript); switch (psgroup.Type) { case "n": sgroup.Type = SgroupType.CtabStructureRepeatUnit; break; case "mon": sgroup.Type = SgroupType.CtabMonomer; break; case "mer": sgroup.Type = SgroupType.CtabMer; break; case "co": sgroup.Type = SgroupType.CtabCopolymer; break; case "xl": sgroup.Type = SgroupType.CtabCrossLink; break; case "mod": sgroup.Type = SgroupType.CtabModified; break; case "mix": sgroup.Type = SgroupType.CtabMixture; break; case "f": sgroup.Type = SgroupType.CtabFormulation; break; case "any": sgroup.Type = SgroupType.CtabAnyPolymer; break; case "gen": sgroup.Type = SgroupType.CtabGeneric; break; case "c": sgroup.Type = SgroupType.CtabComponent; break; case "grf": sgroup.Type = SgroupType.CtabGraft; break; case "alt": sgroup.Type = SgroupType.CtabCopolymer; sgroup.PutValue(SgroupKey.CtabSubType, "ALT"); break; case "ran": sgroup.Type = SgroupType.CtabCopolymer; sgroup.PutValue(SgroupKey.CtabSubType, "RAN"); break; case "blk": sgroup.Type = SgroupType.CtabCopolymer; sgroup.PutValue(SgroupKey.CtabSubType, "BLO"); break; } sgroupMap.Add(mol, sgroup); C_PolySgroup: ; } } // assign Sgroups foreach (var e in sgroupMap) { e.Key.SetCtabSgroups(new List <Sgroup>(e.Value)); } }
/// <summary> /// Calculates the weighted path descriptors. /// </summary> /// <returns>A value representing the weighted path values</returns> public Result Calculate(IAtomContainer container) { container = AtomContainerManipulator.RemoveHydrogens(container); int natom = container.Atoms.Count; var retval = new List <double>(5); var pathList = new List <IList <IAtom> >(); // unique paths for (int i = 0; i < natom - 1; i++) { var a = container.Atoms[i]; for (int j = i + 1; j < natom; j++) { var b = container.Atoms[j]; pathList.AddRange(PathTools.GetAllPaths(container, a, b)); } } // heteroatoms var pathWts = GetPathWeights(pathList, container); double mid = 0.0; foreach (var pathWt3 in pathWts) { mid += pathWt3; } mid += natom; // since we don't calculate paths of length 0 above retval.Add(mid); retval.Add(mid / (double)natom); pathList.Clear(); int count = 0; for (int i = 0; i < natom; i++) { var a = container.Atoms[i]; if (a.AtomicNumber.Equals(AtomicNumbers.C)) { continue; } count++; for (int j = 0; j < natom; j++) { var b = container.Atoms[j]; if (a.Equals(b)) { continue; } pathList.AddRange(PathTools.GetAllPaths(container, a, b)); } } pathWts = GetPathWeights(pathList, container); mid = 0.0; foreach (var pathWt2 in pathWts) { mid += pathWt2; } mid += count; retval.Add(mid); // oxygens pathList.Clear(); count = 0; for (int i = 0; i < natom; i++) { var a = container.Atoms[i]; if (!a.AtomicNumber.Equals(AtomicNumbers.O)) { continue; } count++; for (int j = 0; j < natom; j++) { var b = container.Atoms[j]; if (a.Equals(b)) { continue; } pathList.AddRange(PathTools.GetAllPaths(container, a, b)); } } pathWts = GetPathWeights(pathList, container); mid = 0.0; foreach (var pathWt1 in pathWts) { mid += pathWt1; } mid += count; retval.Add(mid); // nitrogens pathList.Clear(); count = 0; for (int i = 0; i < natom; i++) { var a = container.Atoms[i]; if (!a.AtomicNumber.Equals(AtomicNumbers.N)) { continue; } count++; for (int j = 0; j < natom; j++) { var b = container.Atoms[j]; if (a.Equals(b)) { continue; } pathList.AddRange(PathTools.GetAllPaths(container, a, b)); } } pathWts = GetPathWeights(pathList, container); mid = 0.0; foreach (var pathWt in pathWts) { mid += pathWt; } mid += count; retval.Add(mid); return(new Result(retval)); }
public void XtestBenzene() { var enol = builder.NewAtomContainer(); // atom block var atom1 = builder.NewAtom(ChemicalElement.C); atom1.Hybridization = Hybridization.SP2; var atom2 = builder.NewAtom(ChemicalElement.C); atom2.Hybridization = Hybridization.SP2; var atom3 = builder.NewAtom(ChemicalElement.C); atom3.Hybridization = Hybridization.SP2; var atom4 = builder.NewAtom(ChemicalElement.C); atom4.Hybridization = Hybridization.SP2; var atom5 = builder.NewAtom(ChemicalElement.C); atom5.Hybridization = Hybridization.SP2; var atom6 = builder.NewAtom(ChemicalElement.C); atom6.Hybridization = Hybridization.SP2; // bond block var bond1 = builder.NewBond(atom1, atom2); var bond2 = builder.NewBond(atom2, atom3); var bond3 = builder.NewBond(atom3, atom4); var bond4 = builder.NewBond(atom4, atom5); var bond5 = builder.NewBond(atom5, atom6); var bond6 = builder.NewBond(atom6, atom1); enol.Atoms.Add(atom1); enol.Atoms.Add(atom2); enol.Atoms.Add(atom3); enol.Atoms.Add(atom4); enol.Atoms.Add(atom5); enol.Atoms.Add(atom6); enol.Bonds.Add(bond1); enol.Bonds.Add(bond2); enol.Bonds.Add(bond3); enol.Bonds.Add(bond4); enol.Bonds.Add(bond5); enol.Bonds.Add(bond6); // perceive atom types AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(enol); // now have the algorithm have a go at it enol = dbst.FixAromaticBondOrders(enol); Assert.IsNotNull(enol); Assert.IsTrue(dbst.IsOK(enol)); // now check whether it did the right thing Assert.AreEqual(BondOrder.Single.Numeric() + BondOrder.Double.Numeric(), enol .Bonds[0].Order.Numeric() + enol.Bonds[5].Order.Numeric()); // around atom1 Assert.AreEqual(BondOrder.Single.Numeric() + BondOrder.Double.Numeric(), enol .Bonds[0].Order.Numeric() + enol.Bonds[1].Order.Numeric()); // around atom2 Assert.AreEqual(BondOrder.Single.Numeric() + BondOrder.Double.Numeric(), enol .Bonds[1].Order.Numeric() + enol.Bonds[2].Order.Numeric()); // around atom3 Assert.AreEqual(BondOrder.Single.Numeric() + BondOrder.Double.Numeric(), enol .Bonds[2].Order.Numeric() + enol.Bonds[3].Order.Numeric()); // around atom4 Assert.AreEqual(BondOrder.Single.Numeric() + BondOrder.Double.Numeric(), enol .Bonds[3].Order.Numeric() + enol.Bonds[4].Order.Numeric()); // around atom5 Assert.AreEqual(BondOrder.Single.Numeric() + BondOrder.Double.Numeric(), enol .Bonds[4].Order.Numeric() + enol.Bonds[5].Order.Numeric()); // around atom6 }
/// <summary> /// Initiates the process for the given mechanism. The atoms to apply are mapped between /// reactants and products. /// </summary> /// <param name="atomContainerSet"></param> /// <param name="atomList">The list of atoms taking part in the mechanism. Only allowed two atoms. Both atoms acquire a ISingleElectron</param> /// <param name="bondList">The list of bonds taking part in the mechanism. Only allowed one bond</param> /// <returns>The Reaction mechanism</returns> public IReaction Initiate(IChemObjectSet <IAtomContainer> atomContainerSet, IList <IAtom> atomList, IList <IBond> bondList) { var atMatcher = CDK.AtomTypeMatcher; if (atomContainerSet.Count != 1) { throw new CDKException("TautomerizationMechanism only expects one IAtomContainer"); } if (atomList.Count != 2) { throw new CDKException("HomolyticCleavageMechanism expects two atoms in the List"); } if (bondList.Count != 1) { throw new CDKException("HomolyticCleavageMechanism only expect one bond in the List"); } IAtomContainer molecule = atomContainerSet[0]; IAtomContainer reactantCloned; reactantCloned = (IAtomContainer)molecule.Clone(); IAtom atom1 = atomList[0]; IAtom atom1C = reactantCloned.Atoms[molecule.Atoms.IndexOf(atom1)]; IAtom atom2 = atomList[1]; IAtom atom2C = reactantCloned.Atoms[molecule.Atoms.IndexOf(atom2)]; IBond bond1 = bondList[0]; int posBond1 = molecule.Bonds.IndexOf(bond1); if (bond1.Order == BondOrder.Single) { reactantCloned.Bonds.Remove(reactantCloned.Bonds[posBond1]); } else { BondManipulator.DecreaseBondOrder(reactantCloned.Bonds[posBond1]); } reactantCloned.SingleElectrons.Add(bond1.Builder.NewSingleElectron(atom1C)); reactantCloned.SingleElectrons.Add(bond1.Builder.NewSingleElectron(atom2C)); AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(reactantCloned); // check if resulting atom type is reasonable atom1C.Hybridization = Hybridization.Unset; IAtomType type = atMatcher.FindMatchingAtomType(reactantCloned, atom1C); if (type == null || type.AtomTypeName.Equals("X", StringComparison.Ordinal)) { return(null); } // check if resulting atom type is reasonable: an acceptor atom cannot be charged positive*/ atom2C.Hybridization = Hybridization.Unset; type = atMatcher.FindMatchingAtomType(reactantCloned, atom2C); if (type == null || type.AtomTypeName.Equals("X", StringComparison.Ordinal)) { return(null); } IReaction reaction = atom2C.Builder.NewReaction(); reaction.Reactants.Add(molecule); /* mapping */ foreach (var atom in molecule.Atoms) { IMapping mapping = atom2C.Builder.NewMapping(atom, reactantCloned.Atoms[molecule.Atoms.IndexOf(atom)]); reaction.Mappings.Add(mapping); } if (bond1.Order != BondOrder.Single) { reaction.Products.Add(reactantCloned); } else { var moleculeSetP = ConnectivityChecker.PartitionIntoMolecules(reactantCloned); foreach (var moleculeP in moleculeSetP) { reaction.Products.Add(moleculeP); } } return(reaction); }
public void TestFspSingleB() { //CreateFromSmiles("FC") var molecule = builder.NewAtomContainer(); molecule.Atoms.Add(builder.NewAtom("F")); molecule.Atoms.Add(builder.NewAtom("C")); molecule.AddBond(molecule.Atoms[0], molecule.Atoms[1], BondOrder.Single); molecule.Atoms.Add(builder.NewAtom("H")); molecule.Atoms.Add(builder.NewAtom("H")); molecule.Atoms.Add(builder.NewAtom("H")); molecule.AddBond(molecule.Atoms[1], molecule.Atoms[2], BondOrder.Single); molecule.AddBond(molecule.Atoms[1], molecule.Atoms[3], BondOrder.Single); molecule.AddBond(molecule.Atoms[1], molecule.Atoms[4], BondOrder.Single); AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(molecule); CDK.LonePairElectronChecker.Saturate(molecule); molecule.Atoms[0].IsReactiveCenter = true; molecule.Atoms[1].IsReactiveCenter = true; molecule.Bonds[0].IsReactiveCenter = true; var setOfReactants = CDK.Builder.NewAtomContainerSet(); setOfReactants.Add(molecule); var type = new HeterolyticCleavageSBReaction(); var paramList = new List <IParameterReaction>(); var param = new SetReactionCenter(); param.IsSetParameter = true; paramList.Add(param); type.ParameterList = paramList; /* initiate */ var setOfReactions = type.Initiate(setOfReactants, null); Assert.AreEqual(1, setOfReactions.Count); //CreateFromSmiles("[F-]") var expected1 = builder.NewAtomContainer(); expected1.Atoms.Add(builder.NewAtom("F")); expected1.Atoms[0].FormalCharge = -1; AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(expected1); CDK.LonePairElectronChecker.Saturate(expected1); var product1 = setOfReactions[0].Products[0]; var queryAtom = QueryAtomContainerCreator.CreateSymbolAndChargeQueryContainer(expected1); Assert.IsTrue(uiTester.IsIsomorph(product1, queryAtom)); //CreateFromSmiles("[C+]") var expected2 = builder.NewAtomContainer(); expected2.Atoms.Add(builder.NewAtom("C")); expected2.Atoms[0].FormalCharge = +1; expected2.Atoms.Add(builder.NewAtom("H")); expected2.Atoms.Add(builder.NewAtom("H")); expected2.Atoms.Add(builder.NewAtom("H")); expected2.AddBond(expected2.Atoms[0], expected2.Atoms[1], BondOrder.Single); expected2.AddBond(expected2.Atoms[0], expected2.Atoms[2], BondOrder.Single); expected2.AddBond(expected2.Atoms[0], expected2.Atoms[3], BondOrder.Single); AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(expected2); var product2 = setOfReactions[0].Products[1]; queryAtom = QueryAtomContainerCreator.CreateSymbolAndChargeQueryContainer(expected2); Assert.IsTrue(uiTester.IsIsomorph(product2, queryAtom)); }
public void TestNsp3ChargeSingleB() { //CreateFromSmiles("C[N+]C") IAtomContainer molecule = builder.NewAtomContainer(); molecule.Atoms.Add(builder.NewAtom("C")); molecule.Atoms.Add(builder.NewAtom("N")); molecule.Atoms[1].FormalCharge = +1; molecule.Atoms.Add(builder.NewAtom("C")); molecule.AddBond(molecule.Atoms[0], molecule.Atoms[1], BondOrder.Single); molecule.AddBond(molecule.Atoms[1], molecule.Atoms[2], BondOrder.Single); molecule.Atoms.Add(builder.NewAtom("H")); molecule.Atoms.Add(builder.NewAtom("H")); molecule.Atoms.Add(builder.NewAtom("H")); molecule.Atoms.Add(builder.NewAtom("H")); molecule.Atoms.Add(builder.NewAtom("H")); molecule.Atoms.Add(builder.NewAtom("H")); molecule.Atoms.Add(builder.NewAtom("H")); molecule.Atoms.Add(builder.NewAtom("H")); molecule.AddBond(molecule.Atoms[0], molecule.Atoms[3], BondOrder.Single); molecule.AddBond(molecule.Atoms[0], molecule.Atoms[4], BondOrder.Single); molecule.AddBond(molecule.Atoms[0], molecule.Atoms[5], BondOrder.Single); molecule.AddBond(molecule.Atoms[1], molecule.Atoms[6], BondOrder.Single); molecule.AddBond(molecule.Atoms[1], molecule.Atoms[7], BondOrder.Single); molecule.AddBond(molecule.Atoms[2], molecule.Atoms[8], BondOrder.Single); molecule.AddBond(molecule.Atoms[2], molecule.Atoms[9], BondOrder.Single); molecule.AddBond(molecule.Atoms[2], molecule.Atoms[10], BondOrder.Single); AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(molecule); CDK.LonePairElectronChecker.Saturate(molecule); molecule.Atoms[1].IsReactiveCenter = true; molecule.Atoms[2].IsReactiveCenter = true; molecule.Bonds[1].IsReactiveCenter = true; var setOfReactants = ChemObjectBuilder.Instance.NewAtomContainerSet(); setOfReactants.Add(molecule); IReactionProcess type = new SharingChargeSBReaction(); var paramList = new List <IParameterReaction>(); var param = new SetReactionCenter(); param.IsSetParameter = true; paramList.Add(param); type.ParameterList = paramList; /* initiate */ var setOfReactions = type.Initiate(setOfReactants, null); Assert.AreEqual(1, setOfReactions.Count); // expected products //CreateFromSmiles("CN") IAtomContainer expected1 = builder.NewAtomContainer(); expected1.Atoms.Add(builder.NewAtom("C")); expected1.Atoms.Add(builder.NewAtom("N")); expected1.AddBond(expected1.Atoms[0], expected1.Atoms[1], BondOrder.Single); expected1.Atoms.Add(builder.NewAtom("H")); expected1.Atoms.Add(builder.NewAtom("H")); expected1.Atoms.Add(builder.NewAtom("H")); expected1.Atoms.Add(builder.NewAtom("H")); expected1.Atoms.Add(builder.NewAtom("H")); expected1.AddBond(expected1.Atoms[0], expected1.Atoms[2], BondOrder.Single); expected1.AddBond(expected1.Atoms[0], expected1.Atoms[3], BondOrder.Single); expected1.AddBond(expected1.Atoms[0], expected1.Atoms[4], BondOrder.Single); expected1.AddBond(expected1.Atoms[1], expected1.Atoms[5], BondOrder.Single); expected1.AddBond(expected1.Atoms[1], expected1.Atoms[6], BondOrder.Single); AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(expected1); CDK.LonePairElectronChecker.Saturate(expected1); IAtomContainer product1 = setOfReactions[0].Products[0]; QueryAtomContainer queryAtom = QueryAtomContainerCreator.CreateSymbolAndChargeQueryContainer(expected1); Assert.IsTrue(new UniversalIsomorphismTester().IsIsomorph(product1, queryAtom)); //CreateFromSmiles("[C+]") IAtomContainer expected2 = builder.NewAtomContainer(); expected2.Atoms.Add(builder.NewAtom("C")); expected2.Atoms[0].FormalCharge = +1; expected2.Atoms.Add(builder.NewAtom("H")); expected2.Atoms.Add(builder.NewAtom("H")); expected2.Atoms.Add(builder.NewAtom("H")); expected2.AddBond(expected2.Atoms[0], expected2.Atoms[1], BondOrder.Single); expected2.AddBond(expected2.Atoms[0], expected2.Atoms[2], BondOrder.Single); expected2.AddBond(expected2.Atoms[0], expected2.Atoms[3], BondOrder.Single); AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(expected2); IAtomContainer product2 = setOfReactions[0].Products[1]; queryAtom = QueryAtomContainerCreator.CreateSymbolAndChargeQueryContainer(expected2); Assert.IsTrue(new UniversalIsomorphismTester().IsIsomorph(product2, queryAtom)); }
/// <summary> /// Select the best scoring template + offset for the given macrocycle. /// </summary> /// <param name="macrocycle">macrocycle</param> /// <param name="ringset">entire ring system</param> /// <param name="wind">winding of ring CW/CCW</param> /// <param name="winding">winding of each turn in the ring</param> /// <returns>the best scoring configuration</returns> private static MacroScore BestScore(IRing macrocycle, IRingSet ringset, int wind, int[] winding) { var numAtoms = macrocycle.Atoms.Count; var heteroIdxs = new List <int>(); var ringAttachs = new List <IList <int> >(); // hetero atoms for (int i = 0; i < numAtoms; i++) { if (macrocycle.Atoms[i].AtomicNumber != 6) { heteroIdxs.Add(i); } } foreach (var other in ringset) { if (other == macrocycle) { continue; } var shared = AtomContainerManipulator.GetIntersection(macrocycle, other); if (shared.Atoms.Count >= 2 && shared.Atoms.Count <= 4) { ringAttachs.Add(GetAttachedInOrder(macrocycle, shared)); } } // convex and concave are relative var convex = wind; var concave = -wind; MacroScore best = null; for (int i = 0; i < winding.Length; i++) { // score ring attachs int nRingClick = 0; foreach (var ringAttach in ringAttachs) { int r1, r2, r3, r4; switch (ringAttach.Count) { case 2: r1 = (ringAttach[0] + i) % numAtoms; r2 = (ringAttach[1] + i) % numAtoms; if (winding[r1] == winding[r2]) { if (winding[r1] == convex) { nRingClick += 5; } else { nRingClick++; } } break; case 3: r1 = (ringAttach[0] + i) % numAtoms; r2 = (ringAttach[1] + i) % numAtoms; r3 = (ringAttach[2] + i) % numAtoms; if (winding[r1] == convex && winding[r2] == concave && winding[r3] == convex) { nRingClick += 5; } else if (winding[r1] == concave && winding[r2] == convex && winding[r3] == concave) { nRingClick++; } break; case 4: r1 = (ringAttach[0] + i) % numAtoms; r2 = (ringAttach[1] + i) % numAtoms; r3 = (ringAttach[2] + i) % numAtoms; r4 = (ringAttach[3] + i) % numAtoms; if (winding[r1] == convex && winding[r2] == concave && winding[r3] == concave && winding[r4] == convex) { nRingClick++; } else if (winding[r1] == concave && winding[r2] == convex && winding[r3] == convex && winding[r4] == concave) { nRingClick++; } break; } } // score hetero atoms in concave positions int nConcaveHetero = 0; foreach (var heteroIdx in heteroIdxs) { var k = (heteroIdx + i) % numAtoms; if (winding[k] == concave) { nConcaveHetero++; } } int nCorrectStereo = 0; int nIncorrectStereo = 0; foreach (var se in macrocycle.StereoElements) { if (se.Class == StereoClass.CisTrans) { var bond = (IBond)se.Focus; var beg = bond.Begin; var end = bond.End; StereoConfigurations cfg; if (winding[(macrocycle.Atoms.IndexOf(beg) + i) % numAtoms] == winding[(macrocycle.Atoms.IndexOf(end) + i) % numAtoms]) { cfg = StereoConfigurations.Together; } else { cfg = StereoConfigurations.Opposite; } if (cfg == se.Configure) { nCorrectStereo++; } else { nIncorrectStereo++; } } } var score = new MacroScore(i, nConcaveHetero, nCorrectStereo, nRingClick); if (score.CompareTo(best) < 0) { best = score; } } return(best); }
/// <summary> /// Initiates the process for the given mechanism. The atoms to apply are mapped between /// reactants and products. /// </summary> /// <param name="atomContainerSet"></param> /// <param name="atomList"> The list of atoms taking part in the mechanism. Only allowed two atoms</param> /// <param name="bondList">The list of bonds taking part in the mechanism. Only allowed one bond</param> /// <returns>The Reaction mechanism</returns> public IReaction Initiate(IChemObjectSet <IAtomContainer> atomContainerSet, IList <IAtom> atomList, IList <IBond> bondList) { var atMatcher = CDK.AtomTypeMatcher; if (atomContainerSet.Count != 1) { throw new CDKException("SharingElectronMechanism only expects one IAtomContainer"); } if (atomList.Count != 2) { throw new CDKException("SharingElectronMechanism expects two atoms in the List"); } if (bondList.Count != 1) { throw new CDKException("SharingElectronMechanism only expect one bond in the List"); } IAtomContainer molecule = atomContainerSet[0]; IAtomContainer reactantCloned; reactantCloned = (IAtomContainer)molecule.Clone(); IAtom atom1 = atomList[0]; // Atom containing the lone pair to share IAtom atom1C = reactantCloned.Atoms[molecule.Atoms.IndexOf(atom1)]; IAtom atom2 = atomList[1]; // Atom to neutralize the deficiency of charge IAtom atom2C = reactantCloned.Atoms[molecule.Atoms.IndexOf(atom2)]; IBond bond1 = bondList[0]; int posBond1 = molecule.Bonds.IndexOf(bond1); BondManipulator.IncreaseBondOrder(reactantCloned.Bonds[posBond1]); var lonePair = reactantCloned.GetConnectedLonePairs(atom1C); reactantCloned.LonePairs.Remove(lonePair.Last()); int charge = atom1C.FormalCharge.Value; atom1C.FormalCharge = charge + 1; charge = atom2C.FormalCharge.Value; atom2C.FormalCharge = charge - 1; atom1C.Hybridization = Hybridization.Unset; AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(reactantCloned); IAtomType type = atMatcher.FindMatchingAtomType(reactantCloned, atom1C); if (type == null || type.AtomTypeName.Equals("X", StringComparison.Ordinal)) { return(null); } atom2C.Hybridization = Hybridization.Unset; AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(reactantCloned); type = atMatcher.FindMatchingAtomType(reactantCloned, atom2C); if (type == null || type.AtomTypeName.Equals("X", StringComparison.Ordinal)) { return(null); } IReaction reaction = atom2C.Builder.NewReaction(); reaction.Reactants.Add(molecule); /* mapping */ foreach (var atom in molecule.Atoms) { IMapping mapping = atom2C.Builder.NewMapping(atom, reactantCloned.Atoms[molecule.Atoms.IndexOf(atom)]); reaction.Mappings.Add(mapping); } reaction.Products.Add(reactantCloned); return(reaction); }
/// <summary> /// Initiates the process for the given mechanism. The atoms to apply are mapped between /// reactants and products. /// </summary> /// <param name="atomContainerSet"></param> /// <param name="atomList">The list of atoms taking part in the mechanism. Only allowed two three. /// The first atom is the atom which must contain the charge to be moved, the second /// is the atom which is in the middle and the third is the atom which acquires the new charge /// <param name="bondList">The list of bonds taking part in the mechanism. Only allowed two bond.</param> /// The first bond is the bond to increase the order and the second is the bond /// to decrease the order /// It is the bond which is moved</param> /// <returns>The Reaction mechanism</returns> public IReaction Initiate(IChemObjectSet <IAtomContainer> atomContainerSet, IList <IAtom> atomList, IList <IBond> bondList) { var atMatcher = CDK.AtomTypeMatcher; if (atomContainerSet.Count != 1) { throw new CDKException("RearrangementChargeMechanism only expects one IAtomContainer"); } if (atomList.Count != 3) { throw new CDKException("RearrangementChargeMechanism expects three atoms in the List"); } if (bondList.Count != 2) { throw new CDKException("RearrangementChargeMechanism only expect one bond in the List"); } IAtomContainer molecule = atomContainerSet[0]; IAtomContainer reactantCloned; reactantCloned = (IAtomContainer)molecule.Clone(); IAtom atom1 = atomList[0]; // Atom with the charge IAtom atom1C = reactantCloned.Atoms[molecule.Atoms.IndexOf(atom1)]; IAtom atom3 = atomList[2]; // Atom which acquires the charge IAtom atom3C = reactantCloned.Atoms[molecule.Atoms.IndexOf(atom3)]; IBond bond1 = bondList[0]; // Bond with single bond int posBond1 = molecule.Bonds.IndexOf(bond1); IBond bond2 = bondList[1]; // Bond with double bond int posBond2 = molecule.Bonds.IndexOf(bond2); BondManipulator.IncreaseBondOrder(reactantCloned.Bonds[posBond1]); if (bond2.Order == BondOrder.Single) { reactantCloned.Bonds.Remove(reactantCloned.Bonds[posBond2]); } else { BondManipulator.DecreaseBondOrder(reactantCloned.Bonds[posBond2]); } //Depending of the charge moving (radical, + or -) there is a different situation if (reactantCloned.GetConnectedSingleElectrons(atom1C).Any()) { var selectron = reactantCloned.GetConnectedSingleElectrons(atom1C); reactantCloned.SingleElectrons.Remove(selectron.Last()); reactantCloned.SingleElectrons.Add(bond2.Builder.NewSingleElectron(atom3C)); } else if (atom1C.FormalCharge > 0) { int charge = atom1C.FormalCharge.Value; atom1C.FormalCharge = charge - 1; charge = atom3C.FormalCharge.Value; atom3C.FormalCharge = charge + 1; } else if (atom1C.FormalCharge < 1) { int charge = atom1C.FormalCharge.Value; atom1C.FormalCharge = charge + 1; var ln = reactantCloned.GetConnectedLonePairs(atom1C); reactantCloned.LonePairs.Remove(ln.Last()); atom1C.IsAromatic = false; charge = atom3C.FormalCharge.Value; atom3C.FormalCharge = charge - 1; reactantCloned.LonePairs.Add(bond2.Builder.NewLonePair(atom3C)); atom3C.IsAromatic = false; } else { return(null); } atom1C.Hybridization = Hybridization.Unset; atom3C.Hybridization = Hybridization.Unset; AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(reactantCloned); IAtomType type = atMatcher.FindMatchingAtomType(reactantCloned, atom1C); if (type == null || type.AtomTypeName.Equals("X", StringComparison.Ordinal)) { return(null); } type = atMatcher.FindMatchingAtomType(reactantCloned, atom3C); if (type == null || type.AtomTypeName.Equals("X", StringComparison.Ordinal)) { return(null); } IReaction reaction = bond2.Builder.NewReaction(); reaction.Reactants.Add(molecule); /* mapping */ foreach (var atom in molecule.Atoms) { IMapping mapping = bond2.Builder.NewMapping(atom, reactantCloned.Atoms[molecule.Atoms.IndexOf(atom)]); reaction.Mappings.Add(mapping); } if (bond2.Order != BondOrder.Single) { reaction.Products.Add(reactantCloned); } else { IChemObjectSet <IAtomContainer> moleculeSetP = ConnectivityChecker.PartitionIntoMolecules(reactantCloned); for (int z = 0; z < moleculeSetP.Count(); z++) { reaction.Products.Add((IAtomContainer)moleculeSetP[z]); } } return(reaction); }
/// <summary> /// Layout all rings in the given RingSet that are connected to a given Ring /// </summary> /// <param name="rs">The RingSet to be searched for rings connected to Ring</param> /// <param name="ring">The Ring for which all connected rings in RingSet are to be layed out.</param> public void PlaceConnectedRings(IRingSet rs, IRing ring, int handleType, double bondLength) { var connectedRings = rs.GetConnectedRings(ring); // Debug.WriteLine(rs.ReportRingList(Molecule)); foreach (var container in connectedRings) { IRing connectedRing = (IRing)container; if (!connectedRing.IsPlaced) { // Debug.WriteLine(ring.ToString(Molecule)); // Debug.WriteLine(connectedRing.ToString(Molecule)); IAtomContainer sharedAtoms = AtomContainerManipulator.GetIntersection(ring, connectedRing); int numSharedAtoms = sharedAtoms.Atoms.Count; Debug.WriteLine("placeConnectedRings-> connectedRing: " + (ring.ToString())); if ((numSharedAtoms == 2 && handleType == Fused) || (numSharedAtoms == 1 && handleType == Spiro) || (numSharedAtoms > 2 && handleType == Bridged)) { Vector2 sharedAtomsCenter = GeometryUtil.Get2DCenter(sharedAtoms); Vector2 oldRingCenter = GeometryUtil.Get2DCenter(ring); Vector2 tempVector = sharedAtomsCenter; Vector2 newRingCenterVector = tempVector; newRingCenterVector -= oldRingCenter; // zero (or v. small ring center) if (Math.Abs(newRingCenterVector.X) < 0.001 && Math.Abs(newRingCenterVector.Y) < 0.001) { // first see if we can use terminal bonds IAtomContainer terminalOnly = Molecule.Builder.NewAtomContainer(); foreach (IAtom atom in ring.Atoms) { if (ring.GetConnectedBonds(atom).Count() == 1) { terminalOnly.Atoms.Add(atom); } } if (terminalOnly.Atoms.Count == 2) { newRingCenterVector = GeometryUtil.Get2DCenter(terminalOnly); newRingCenterVector = oldRingCenter; connectedRing.SetProperty(RingPlacer.SnapHint, true); } else { // project coordinates on 12 axis (30 degree snaps) and choose one with most spread Vector2 vec = new Vector2(0, 1); double bestLen = -double.MaxValue; for (int i = 0; i < 12; i++) { Vector2 orth = new Vector2(-vec.Y, vec.X); orth = Vector2.Normalize(orth); double min = double.MaxValue, max = -double.MaxValue; foreach (IAtom atom in sharedAtoms.Atoms) { // s: scalar projection double s = Vector2.Dot(orth, atom.Point2D.Value); if (s < min) { min = s; } if (s > max) { max = s; } } double len = max - min; if (len > bestLen) { bestLen = len; newRingCenterVector = vec; } Rotate(vec, RAD_30); } } } Vector2 oldRingCenterVector = newRingCenterVector; Debug.WriteLine($"placeConnectedRing -> tempVector: {tempVector}, tempVector.Length: {tempVector.Length()}"); Debug.WriteLine($"placeConnectedRing -> bondCenter: {sharedAtomsCenter}"); Debug.WriteLine($"placeConnectedRing -> oldRingCenterVector.Length: {oldRingCenterVector.Length()}"); Debug.WriteLine($"placeConnectedRing -> newRingCenterVector.Length: {newRingCenterVector.Length()}"); Vector2 tempPoint = sharedAtomsCenter; tempPoint += newRingCenterVector; PlaceRing(connectedRing, sharedAtoms, sharedAtomsCenter, newRingCenterVector, bondLength); connectedRing.IsPlaced = true; PlaceConnectedRings(rs, connectedRing, handleType, bondLength); } } } }
// // public static Molecule CreateNeopentane() { // Molecule result = new DefaultMolecule(); // Atom c0 = result.Atoms.Add("C"); // Atom c1 = result.Atoms.Add("C"); // Atom c2 = result.Atoms.Add("C"); // Atom c3 = result.Atoms.Add("C"); // Atom c4 = result.Atoms.Add("C"); // // result.Connect(c0, c1, 1); // result.Connect(c0, c2, 1); // result.Connect(c0, c3, 1); // result.Connect(c0, c4, 1); // // return result; // } // public static IAtomContainer CreateCubane() { IAtomContainer result = builder.NewAtomContainer(); IAtom c1 = builder.NewAtom("C"); c1.Id = "1"; IAtom c2 = builder.NewAtom("C"); c2.Id = "2"; IAtom c3 = builder.NewAtom("C"); c3.Id = "3"; IAtom c4 = builder.NewAtom("C"); c4.Id = "4"; IAtom c5 = builder.NewAtom("C"); c5.Id = "5"; IAtom c6 = builder.NewAtom("C"); c6.Id = "6"; IAtom c7 = builder.NewAtom("C"); c7.Id = "7"; IAtom c8 = builder.NewAtom("C"); c8.Id = "8"; result.Atoms.Add(c1); result.Atoms.Add(c2); result.Atoms.Add(c3); result.Atoms.Add(c4); result.Atoms.Add(c5); result.Atoms.Add(c6); result.Atoms.Add(c7); result.Atoms.Add(c8); IBond bond1 = builder.NewBond(c1, c2, BondOrder.Single); IBond bond2 = builder.NewBond(c2, c3, BondOrder.Single); IBond bond3 = builder.NewBond(c3, c4, BondOrder.Single); IBond bond4 = builder.NewBond(c4, c1, BondOrder.Single); IBond bond5 = builder.NewBond(c5, c6, BondOrder.Single); IBond bond6 = builder.NewBond(c6, c7, BondOrder.Single); IBond bond7 = builder.NewBond(c7, c8, BondOrder.Single); IBond bond8 = builder.NewBond(c8, c5, BondOrder.Single); IBond bond9 = builder.NewBond(c1, c5, BondOrder.Single); IBond bond10 = builder.NewBond(c2, c6, BondOrder.Single); IBond bond11 = builder.NewBond(c3, c7, BondOrder.Single); IBond bond12 = builder.NewBond(c4, c8, BondOrder.Single); result.Bonds.Add(bond1); result.Bonds.Add(bond2); result.Bonds.Add(bond3); result.Bonds.Add(bond4); result.Bonds.Add(bond5); result.Bonds.Add(bond6); result.Bonds.Add(bond7); result.Bonds.Add(bond8); result.Bonds.Add(bond9); result.Bonds.Add(bond10); result.Bonds.Add(bond11); result.Bonds.Add(bond12); AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(result); var adder = CDK.HydrogenAdder; adder.AddImplicitHydrogens(result); Aromaticity.CDKLegacy.Apply(result); return(result); }
public void TestFluorobenzene() { var molecule = builder.NewAtomContainer(); molecule.Atoms.Add(builder.NewAtom("F")); molecule.Atoms.Add(builder.NewAtom("C")); molecule.AddBond(molecule.Atoms[0], molecule.Atoms[1], BondOrder.Single); molecule.Atoms.Add(builder.NewAtom("C")); molecule.AddBond(molecule.Atoms[1], molecule.Atoms[2], BondOrder.Double); molecule.Atoms.Add(builder.NewAtom("C")); molecule.AddBond(molecule.Atoms[2], molecule.Atoms[3], BondOrder.Single); molecule.Atoms.Add(builder.NewAtom("C")); molecule.AddBond(molecule.Atoms[3], molecule.Atoms[4], BondOrder.Double); molecule.Atoms.Add(builder.NewAtom("C")); molecule.AddBond(molecule.Atoms[4], molecule.Atoms[5], BondOrder.Single); molecule.Atoms.Add(builder.NewAtom("C")); molecule.AddBond(molecule.Atoms[5], molecule.Atoms[6], BondOrder.Double); molecule.AddBond(molecule.Atoms[6], molecule.Atoms[1], BondOrder.Single); AddExplicitHydrogens(molecule); AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(molecule); CDK.LonePairElectronChecker.Saturate(molecule); var type = new RearrangementLonePairReaction(); var setOfReactants = CDK.Builder.NewAtomContainerSet(); setOfReactants.Add(molecule); /* automatic search of the center active */ var paramList = new List <IParameterReaction>(); var param = new SetReactionCenter(); param.IsSetParameter = false; paramList.Add(param); type.ParameterList = paramList; /* initiate */ var setOfReactions = type.Initiate(setOfReactants, null); Assert.AreEqual(1, setOfReactions.Count); Assert.AreEqual(1, setOfReactions[0].Products.Count); var product1 = setOfReactions[0].Products[0]; var molecule1 = builder.NewAtomContainer(); molecule1.Atoms.Add(builder.NewAtom("F")); molecule1.Atoms[0].FormalCharge = 1; molecule1.Atoms.Add(builder.NewAtom("C")); molecule1.AddBond(molecule1.Atoms[0], molecule1.Atoms[1], BondOrder.Double); molecule1.Atoms.Add(builder.NewAtom("C")); molecule1.Atoms[2].FormalCharge = -1; molecule1.AddBond(molecule1.Atoms[1], molecule1.Atoms[2], BondOrder.Single); molecule1.Atoms.Add(builder.NewAtom("C")); molecule1.AddBond(molecule1.Atoms[2], molecule1.Atoms[3], BondOrder.Single); molecule1.Atoms.Add(builder.NewAtom("C")); molecule1.AddBond(molecule1.Atoms[3], molecule1.Atoms[4], BondOrder.Double); molecule1.Atoms.Add(builder.NewAtom("C")); molecule1.AddBond(molecule1.Atoms[4], molecule1.Atoms[5], BondOrder.Single); molecule1.Atoms.Add(builder.NewAtom("C")); molecule1.AddBond(molecule1.Atoms[5], molecule1.Atoms[6], BondOrder.Double); molecule1.AddBond(molecule1.Atoms[6], molecule1.Atoms[1], BondOrder.Single); AddExplicitHydrogens(molecule1); AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(molecule1); CDK.LonePairElectronChecker.Saturate(molecule1); var qAC = QueryAtomContainerCreator.CreateSymbolAndChargeQueryContainer(molecule1); Assert.IsTrue(new UniversalIsomorphismTester().IsIsomorph(product1, qAC)); }
public override void TestInitiate_IAtomContainerSet_IAtomContainerSet() { /* ionize >C=C< , set the reactive center */ IAtomContainer reactant = builder.NewAtomContainer();//CreateFromSmiles("C=CCC(=O)CC") reactant.Atoms.Add(builder.NewAtom("C")); reactant.Atoms.Add(builder.NewAtom("C")); reactant.Atoms.Add(builder.NewAtom("C")); reactant.Atoms.Add(builder.NewAtom("C")); reactant.Atoms.Add(builder.NewAtom("O")); reactant.Atoms.Add(builder.NewAtom("C")); reactant.Atoms.Add(builder.NewAtom("C")); reactant.AddBond(reactant.Atoms[0], reactant.Atoms[1], BondOrder.Double); reactant.AddBond(reactant.Atoms[1], reactant.Atoms[2], BondOrder.Single); reactant.AddBond(reactant.Atoms[2], reactant.Atoms[3], BondOrder.Single); reactant.AddBond(reactant.Atoms[3], reactant.Atoms[4], BondOrder.Double); reactant.AddBond(reactant.Atoms[3], reactant.Atoms[5], BondOrder.Single); reactant.AddBond(reactant.Atoms[5], reactant.Atoms[6], BondOrder.Single); AddExplicitHydrogens(reactant); foreach (var bond in reactant.Bonds) { IAtom atom1 = bond.Atoms[0]; IAtom atom2 = bond.Atoms[1]; if (bond.Order == BondOrder.Double && atom1.Symbol.Equals("C") && atom2.Symbol.Equals("C")) { bond.IsReactiveCenter = true; atom1.IsReactiveCenter = true; atom2.IsReactiveCenter = true; } } var setOfReactants = ChemObjectBuilder.Instance.NewAtomContainerSet(); setOfReactants.Add(reactant); /* initiate */ AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(reactant); MakeSureAtomTypesAreRecognized(reactant); var type = new ElectronImpactPDBReaction(); var paramList = new List <IParameterReaction>(); var param = new SetReactionCenter { IsSetParameter = true }; paramList.Add(param); type.ParameterList = paramList; var setOfReactions = type.Initiate(setOfReactants, null); Assert.AreEqual(2, setOfReactions.Count); IAtomContainer molecule = setOfReactions[0].Products[0]; Assert.AreEqual(1, molecule.Atoms[0].FormalCharge.Value); Assert.AreEqual(1, molecule.GetConnectedSingleElectrons(molecule.Atoms[1]).Count()); Assert.AreEqual(1, molecule.SingleElectrons.Count); molecule = setOfReactions[1].Products[0]; Assert.AreEqual(1, molecule.Atoms[1].FormalCharge.Value); Assert.AreEqual(1, molecule.GetConnectedSingleElectrons(molecule.Atoms[0]).Count()); Assert.AreEqual(1, molecule.SingleElectrons.Count); Assert.AreEqual(17, setOfReactions[0].Mappings.Count); }
public void TestASimpleCarbonRing() { // First we create a simple carbon ring to play with... var mol = new AtomContainer(); var carbon = new AtomType(ChemicalElement.C); var a0 = new Atom("C") { Hybridization = Hybridization.SP2, ImplicitHydrogenCount = 1 }; AtomTypeManipulator.ConfigureUnsetProperties(a0, carbon); var a1 = new Atom("C") { Hybridization = Hybridization.SP2, ImplicitHydrogenCount = 1 }; AtomTypeManipulator.ConfigureUnsetProperties(a1, carbon); var a2 = new Atom("C") { Hybridization = Hybridization.SP2, ImplicitHydrogenCount = 1 }; AtomTypeManipulator.ConfigureUnsetProperties(a2, carbon); var a3 = new Atom("C") { Hybridization = Hybridization.SP2, ImplicitHydrogenCount = 1 }; AtomTypeManipulator.ConfigureUnsetProperties(a3, carbon); var a4 = new Atom("C") { Hybridization = Hybridization.SP2, ImplicitHydrogenCount = 1 }; AtomTypeManipulator.ConfigureUnsetProperties(a4, carbon); var a5 = new Atom("C") { Hybridization = Hybridization.SP2, ImplicitHydrogenCount = 1 }; AtomTypeManipulator.ConfigureUnsetProperties(a5, carbon); mol.Atoms.Add(a0); mol.Atoms.Add(a1); mol.Atoms.Add(a2); mol.Atoms.Add(a3); mol.Atoms.Add(a4); mol.Atoms.Add(a5); var b0 = new Bond(a0, a1) { IsSingleOrDouble = true }; mol.Bonds.Add(b0); var b1 = new Bond(a1, a2) { IsSingleOrDouble = true }; mol.Bonds.Add(b1); var b2 = new Bond(a2, a3) { IsSingleOrDouble = true }; mol.Bonds.Add(b2); var b3 = new Bond(a3, a4) { IsSingleOrDouble = true }; mol.Bonds.Add(b3); var b4 = new Bond(a4, a5) { IsSingleOrDouble = true }; mol.Bonds.Add(b4); var b5 = new Bond(a5, a0) { IsSingleOrDouble = true }; mol.Bonds.Add(b5); AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(mol); // ...then we send it to the method we want to test... atasc.DecideBondOrder(mol, false); Assert.AreEqual(BondOrder.Double, b0.Order); Assert.AreEqual(BondOrder.Single, b1.Order); Assert.AreEqual(BondOrder.Double, b2.Order); Assert.AreEqual(BondOrder.Single, b3.Order); Assert.AreEqual(BondOrder.Double, b4.Order); Assert.AreEqual(BondOrder.Single, b5.Order); Assert.IsTrue(satcheck.IsSaturated(a0, mol)); }
/// <summary> /// Calculates the TPSA for an atom container. /// </summary> /// <remarks> /// Prior to calling this method it is necessary to either add implicit or explicit hydrogens /// using <see cref="Tools.CDKHydrogenAdder.AddImplicitHydrogens(IAtomContainer)"/> or /// <see cref="AtomContainerManipulator.ConvertImplicitToExplicitHydrogens(IAtomContainer)"/>. /// </remarks> /// <returns>A double containing the topological surface area</returns> public Result Calculate(IAtomContainer container) { container = (IAtomContainer)container.Clone(); // don't mod original var rs = (new AllRingsFinder()).FindAllRings(container); // do aromaticity detection if (checkAromaticity) { AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(container); Aromaticity.CDKLegacy.Apply(container); } var profiles = new List <string>(); // iterate over all atoms of container foreach (var atom in container.Atoms) { switch (atom.AtomicNumber) { case AtomicNumbers.N: case AtomicNumbers.O: case AtomicNumbers.S: case AtomicNumbers.P: int singleBondCount = 0; int doubleBondCount = 0; int tripleBondCount = 0; int aromaticBondCount = 0; double maxBondOrder = 0; double bondOrderSum = 0; int hCount = 0; int isIn3MemberRing = 0; // counting the number of single/double/triple/aromatic bonds var connectedBonds = container.GetConnectedBonds(atom); foreach (var connectedBond in connectedBonds) { if (connectedBond.IsAromatic) { aromaticBondCount++; } else if (connectedBond.Order == BondOrder.Single) { singleBondCount++; } else if (connectedBond.Order == BondOrder.Double) { doubleBondCount++; } else if (connectedBond.Order == BondOrder.Triple) { tripleBondCount++; } } var formalCharge = atom.FormalCharge.Value; var connectedAtoms = container.GetConnectedAtoms(atom).ToReadOnlyList(); var numberOfNeighbours = connectedAtoms.Count; // EXPLICIT hydrogens: count the number of hydrogen atoms for (int neighbourIndex = 0; neighbourIndex < numberOfNeighbours; neighbourIndex++) { if ((connectedAtoms[neighbourIndex]).AtomicNumber.Equals(AtomicNumbers.H)) { hCount++; } } // IMPLICIT hydrogens: count the number of hydrogen atoms and adjust other atom profile properties var implicitHAtoms = atom.ImplicitHydrogenCount ?? 0; for (int hydrogenIndex = 0; hydrogenIndex < implicitHAtoms; hydrogenIndex++) { hCount++; numberOfNeighbours++; singleBondCount++; } // Calculate bond order sum using the counters of single/double/triple/aromatic bonds bondOrderSum += singleBondCount * 1.0; bondOrderSum += doubleBondCount * 2.0; bondOrderSum += tripleBondCount * 3.0; bondOrderSum += aromaticBondCount * 1.5; // setting maxBondOrder if (singleBondCount > 0) { maxBondOrder = 1.0; } if (aromaticBondCount > 0) { maxBondOrder = 1.5; } if (doubleBondCount > 0) { maxBondOrder = 2.0; } if (tripleBondCount > 0) { maxBondOrder = 3.0; } // isIn3MemberRing checker if (rs.Contains(atom)) { var rsAtom = rs.GetRings(atom); foreach (var ring in rsAtom) { if (ring.RingSize == 3) { isIn3MemberRing = 1; } } } // create a profile of the current atom (atoms[atomIndex]) according to the profile definition in the constructor string profile = atom.Symbol + "+" + maxBondOrder.ToString("F1", NumberFormatInfo.InvariantInfo) + "+" + bondOrderSum.ToString("F1", NumberFormatInfo.InvariantInfo) + "+" + numberOfNeighbours + "+" + hCount + "+" + formalCharge + "+" + aromaticBondCount + "+" + isIn3MemberRing + "+" + singleBondCount + "+" + doubleBondCount + "+" + tripleBondCount; profiles.Add(profile); break; } } // END OF ATOM LOOP // calculate the tpsa for the AtomContainer container double tpsa = 0; for (int profileIndex = 0; profileIndex < profiles.Count; profileIndex++) { if (map.TryGetValue(profiles[profileIndex], out double psa)) { tpsa += psa; } } return(new Result(tpsa)); }
/// <summary> /// Initiates the process for the given mechanism. The atoms to apply are mapped between /// reactants and products. /// </summary> /// <param name="atomContainerSet"></param> /// <param name="atomList">The list of atoms taking part in the mechanism. Only allowed two atoms. The first atom receives the charge and the second the single electron</param> /// <param name="bondList">The list of bonds taking part in the mechanism. Only allowed one bond</param> /// <returns>The Reaction mechanism</returns> public IReaction Initiate(IChemObjectSet <IAtomContainer> atomContainerSet, IList <IAtom> atomList, IList <IBond> bondList) { var atMatcher = CDKAtomTypeMatcher.GetInstance(CDKAtomTypeMatcher.Mode.RequireExplicitHydrogens); if (atomContainerSet.Count != 1) { throw new CDKException("RemovingSEofBMechanism only expects one IAtomContainer"); } if (atomList.Count != 2) { throw new CDKException("RemovingSEofBMechanism expects two atoms in the List"); } if (bondList.Count != 1) { throw new CDKException("RemovingSEofBMechanism only expects one bond in the List"); } var molecule = atomContainerSet[0]; var reactantCloned = (IAtomContainer)molecule.Clone(); var atom1 = atomList[0]; var atom1C = reactantCloned.Atoms[molecule.Atoms.IndexOf(atom1)]; var atom2 = atomList[1]; var atom2C = reactantCloned.Atoms[molecule.Atoms.IndexOf(atom2)]; var bond1 = bondList[0]; var posBond1 = molecule.Bonds.IndexOf(bond1); if (bond1.Order == BondOrder.Single) { reactantCloned.Bonds.Remove(reactantCloned.Bonds[posBond1]); } else { BondManipulator.DecreaseBondOrder(reactantCloned.Bonds[posBond1]); } var charge = atom1C.FormalCharge.Value; atom1C.FormalCharge = charge + 1; reactantCloned.SingleElectrons.Add(atom1C.Builder.NewSingleElectron(atom2C)); // check if resulting atom type is reasonable atom1C.Hybridization = Hybridization.Unset; AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(reactantCloned); var type = atMatcher.FindMatchingAtomType(reactantCloned, atom1C); if (type == null || type.AtomTypeName.Equals("X", StringComparison.Ordinal)) { return(null); } atom2C.Hybridization = Hybridization.Unset; AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(reactantCloned); type = atMatcher.FindMatchingAtomType(reactantCloned, atom2C); if (type == null || type.AtomTypeName.Equals("X", StringComparison.Ordinal)) { return(null); } var reaction = atom1C.Builder.NewReaction(); reaction.Reactants.Add(molecule); /* mapping */ foreach (var atom in molecule.Atoms) { IMapping mapping = atom1C.Builder.NewMapping(atom, reactantCloned.Atoms[molecule.Atoms.IndexOf(atom)]); reaction.Mappings.Add(mapping); } if (bond1.Order != BondOrder.Single) { reaction.Products.Add(reactantCloned); } else { var moleculeSetP = ConnectivityChecker.PartitionIntoMolecules(reactantCloned); foreach (var moleculeP in moleculeSetP) { reaction.Products.Add(moleculeP); } } return(reaction); }
/// <summary> /// <para>Compares two IAtomContainers for order with the following criteria /// with decreasing priority: /// <list type="number"> /// <item>Compare atom count</item> /// <item>Compare molecular weight (heavy atoms only)</item> /// <item>Compare bond count</item> /// <item>Compare sum of bond orders (heavy atoms only)</item> /// </list> /// </para> /// <para>If no difference can be /// found with the above criteria, the IAtomContainers are considered /// equal.</para> /// <para>Returns a negative integer, zero, or a positive integer as /// the first argument is less than, equal to, or greater than the /// second.</para> /// </summary> /// <para>This method is null safe.</para> /// <param name="o1">the first IAtomContainer</param> /// <param name="o2">the second IAtomContainer</param> /// <returns>a negative integer, zero, or a positive integer as the first /// argument is less than, equal to, or greater than the second.</returns> public int Compare(T o1, T o2) { // Check for nulls if (o1 == null && o2 == null) { return(0); } if (o1 == null) { return(1); } if (o2 == null) { return(-1); } T atomContainer1 = o1; T atomContainer2 = o2; // 1. Compare atom count if (atomContainer1.Atoms.Count > atomContainer2.Atoms.Count) { return(1); } else if (atomContainer1.Atoms.Count < atomContainer2.Atoms.Count) { return(-1); } else { // 2. Atom count equal, compare molecular weight (heavy atoms only) double mw1 = 0; double mw2 = 0; try { mw1 = GetMolecularWeight(atomContainer1); mw2 = GetMolecularWeight(atomContainer2); } catch (CDKException) { Trace.TraceWarning("Exception in molecular mass calculation."); return(0); } if (mw1 > mw2) { return(1); } else if (mw1 < mw2) { return(-1); } else { // 3. Molecular weight equal, compare bond count if (atomContainer1.Bonds.Count > atomContainer2.Bonds.Count) { return(1); } else if (atomContainer1.Bonds.Count < atomContainer2.Bonds.Count) { return(-1); } else { // 4. Bond count equal, compare sum of bond orders (heavy atoms only) double bondOrderSum1 = AtomContainerManipulator.GetSingleBondEquivalentSum(atomContainer1); double bondOrderSum2 = AtomContainerManipulator.GetSingleBondEquivalentSum(atomContainer2); if (bondOrderSum1 > bondOrderSum2) { return(1); } else if (bondOrderSum1 < bondOrderSum2) { return(-1); } } } } // AtomContainers are equal in terms of this comparator return(0); }
/// <summary> /// Initiates the process for the given mechanism. The atoms to apply are mapped between /// reactants and products. /// </summary> /// <param name="atomContainerSet"></param> /// <param name="atomList"> /// The list of atoms taking part in the mechanism. Only allowed two atoms. /// The first atom is the atom which contains the ISingleElectron and the second /// third is the atom which will be removed /// the first atom</param> /// <param name="bondList">The list of bonds taking part in the mechanism. Only allowed one bond. /// It is the bond which is moved</param> /// <returns>The Reaction mechanism</returns> public IReaction Initiate(IChemObjectSet <IAtomContainer> atomContainerSet, IList <IAtom> atomList, IList <IBond> bondList) { var atMatcher = CDK.AtomTypeMatcher; if (atomContainerSet.Count != 1) { throw new CDKException("RadicalSiteIonizationMechanism only expects one IAtomContainer"); } if (atomList.Count != 3) { throw new CDKException("RadicalSiteIonizationMechanism expects three atoms in the List"); } if (bondList.Count != 2) { throw new CDKException("RadicalSiteIonizationMechanism only expect one bond in the List"); } IAtomContainer molecule = atomContainerSet[0]; IAtomContainer reactantCloned; reactantCloned = (IAtomContainer)molecule.Clone(); IAtom atom1 = atomList[0]; // Atom containing the ISingleElectron IAtom atom1C = reactantCloned.Atoms[molecule.Atoms.IndexOf(atom1)]; IAtom atom2 = atomList[1]; // Atom IAtom atom2C = reactantCloned.Atoms[molecule.Atoms.IndexOf(atom2)]; IAtom atom3 = atomList[2]; // Atom to be saved IAtom atom3C = reactantCloned.Atoms[molecule.Atoms.IndexOf(atom3)]; IBond bond1 = bondList[0]; // Bond to increase the order int posBond1 = molecule.Bonds.IndexOf(bond1); IBond bond2 = bondList[1]; // Bond to remove int posBond2 = molecule.Bonds.IndexOf(bond2); BondManipulator.IncreaseBondOrder(reactantCloned.Bonds[posBond1]); reactantCloned.Bonds.Remove(reactantCloned.Bonds[posBond2]); var selectron = reactantCloned.GetConnectedSingleElectrons(atom1C); reactantCloned.SingleElectrons.Remove(selectron.Last()); atom1C.Hybridization = Hybridization.Unset; AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(reactantCloned); IAtomType type = atMatcher.FindMatchingAtomType(reactantCloned, atom1C); if (type == null || type.AtomTypeName.Equals("X", StringComparison.Ordinal)) { return(null); } atom2C.Hybridization = Hybridization.Unset; AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(reactantCloned); type = atMatcher.FindMatchingAtomType(reactantCloned, atom2C); if (type == null || type.AtomTypeName.Equals("X", StringComparison.Ordinal)) { return(null); } reactantCloned.SingleElectrons.Add(atom2C.Builder.NewSingleElectron(atom3C)); atom3C.Hybridization = Hybridization.Unset; AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(reactantCloned); type = atMatcher.FindMatchingAtomType(reactantCloned, atom3C); if (type == null || type.AtomTypeName.Equals("X", StringComparison.Ordinal)) { return(null); } IReaction reaction = atom2C.Builder.NewReaction(); reaction.Reactants.Add(molecule); /* mapping */ foreach (var atom in molecule.Atoms) { IMapping mapping = atom2C.Builder.NewMapping(atom, reactantCloned.Atoms[molecule.Atoms.IndexOf(atom)]); reaction.Mappings.Add(mapping); } var moleculeSetP = ConnectivityChecker.PartitionIntoMolecules(reactantCloned); foreach (var moleculeP in moleculeSetP) { reaction.Products.Add(moleculeP); } return(reaction); }
public void TestCID() { var mol = CDK.Builder.NewAtomContainer(); var a1 = mol.Builder.NewAtom("S"); a1.FormalCharge = 0; a1.Point2D = new Vector2(9.4651, 0.25); mol.Atoms.Add(a1); var a2 = mol.Builder.NewAtom("O"); a2.FormalCharge = 0; a2.Point2D = new Vector2(6.001, 1.25); mol.Atoms.Add(a2); var a3 = mol.Builder.NewAtom("O"); a3.FormalCharge = 0; a3.Point2D = new Vector2(5.135, -1.25); mol.Atoms.Add(a3); var a4 = mol.Builder.NewAtom("O"); a4.FormalCharge = 0; a4.Point2D = new Vector2(6.8671, -1.25); mol.Atoms.Add(a4); var a5 = mol.Builder.NewAtom("O"); a5.FormalCharge = 0; a5.Point2D = new Vector2(8.5991, -0.25); mol.Atoms.Add(a5); var a6 = mol.Builder.NewAtom("O"); a6.FormalCharge = 0; a6.Point2D = new Vector2(4.269, 1.25); mol.Atoms.Add(a6); var a7 = mol.Builder.NewAtom("O"); a7.FormalCharge = 0; a7.Point2D = new Vector2(2.5369, 0.25); mol.Atoms.Add(a7); var a8 = mol.Builder.NewAtom("O"); a8.FormalCharge = 0; a8.Point2D = new Vector2(3.403, -1.25); mol.Atoms.Add(a8); var a9 = mol.Builder.NewAtom("O"); a9.FormalCharge = 0; a9.Point2D = new Vector2(10.3312, 0.75); mol.Atoms.Add(a9); var a10 = mol.Builder.NewAtom("O"); a10.FormalCharge = 0; a10.Point2D = new Vector2(9.9651, -0.616); mol.Atoms.Add(a10); var a11 = mol.Builder.NewAtom("O"); a11.FormalCharge = 0; a11.Point2D = new Vector2(8.9651, 1.116); mol.Atoms.Add(a11); var a12 = mol.Builder.NewAtom("C"); a12.FormalCharge = 0; a12.Point2D = new Vector2(6.001, 0.25); mol.Atoms.Add(a12); var a13 = mol.Builder.NewAtom("C"); a13.FormalCharge = 0; a13.Point2D = new Vector2(5.135, -0.25); mol.Atoms.Add(a13); var a14 = mol.Builder.NewAtom("C"); a14.FormalCharge = 0; a14.Point2D = new Vector2(6.8671, -0.25); mol.Atoms.Add(a14); var a15 = mol.Builder.NewAtom("C"); a15.FormalCharge = 0; a15.Point2D = new Vector2(4.269, 0.25); mol.Atoms.Add(a15); var a16 = mol.Builder.NewAtom("C"); a16.FormalCharge = 0; a16.Point2D = new Vector2(7.7331, 0.25); mol.Atoms.Add(a16); var a17 = mol.Builder.NewAtom("C"); a17.FormalCharge = 0; a17.Point2D = new Vector2(3.403, -0.25); mol.Atoms.Add(a17); var a18 = mol.Builder.NewAtom("H"); a18.FormalCharge = 0; a18.Point2D = new Vector2(6.538, 0.56); mol.Atoms.Add(a18); var a19 = mol.Builder.NewAtom("H"); a19.FormalCharge = 0; a19.Point2D = new Vector2(5.672, -0.56); mol.Atoms.Add(a19); var a20 = mol.Builder.NewAtom("H"); a20.FormalCharge = 0; a20.Point2D = new Vector2(6.3301, -0.56); mol.Atoms.Add(a20); var a21 = mol.Builder.NewAtom("H"); a21.FormalCharge = 0; a21.Point2D = new Vector2(4.8059, 0.56); mol.Atoms.Add(a21); var a22 = mol.Builder.NewAtom("H"); a22.FormalCharge = 0; a22.Point2D = new Vector2(8.1316, 0.7249); mol.Atoms.Add(a22); var a23 = mol.Builder.NewAtom("H"); a23.FormalCharge = 0; a23.Point2D = new Vector2(7.3346, 0.7249); mol.Atoms.Add(a23); var a24 = mol.Builder.NewAtom("H"); a24.FormalCharge = 0; a24.Point2D = new Vector2(6.538, 1.56); mol.Atoms.Add(a24); var a25 = mol.Builder.NewAtom("H"); a25.FormalCharge = 0; a25.Point2D = new Vector2(4.5981, -1.56); mol.Atoms.Add(a25); var a26 = mol.Builder.NewAtom("H"); a26.FormalCharge = 0; a26.Point2D = new Vector2(7.404, -1.56); mol.Atoms.Add(a26); var a27 = mol.Builder.NewAtom("H"); a27.FormalCharge = 0; a27.Point2D = new Vector2(3.732, 1.56); mol.Atoms.Add(a27); var a28 = mol.Builder.NewAtom("H"); a28.FormalCharge = 0; a28.Point2D = new Vector2(2.0, -0.06); mol.Atoms.Add(a28); var a29 = mol.Builder.NewAtom("H"); a29.FormalCharge = 0; a29.Point2D = new Vector2(10.8681, 0.44); mol.Atoms.Add(a29); var b1 = mol.Builder.NewBond(a1, a5, BondOrder.Single); mol.Bonds.Add(b1); var b2 = mol.Builder.NewBond(a1, a9, BondOrder.Single); mol.Bonds.Add(b2); var b3 = mol.Builder.NewBond(a1, a10, BondOrder.Double); mol.Bonds.Add(b3); var b4 = mol.Builder.NewBond(a1, a11, BondOrder.Double); mol.Bonds.Add(b4); var b5 = mol.Builder.NewBond(a2, a12, BondOrder.Single); mol.Bonds.Add(b5); var b6 = mol.Builder.NewBond(a2, a24, BondOrder.Single); mol.Bonds.Add(b6); var b7 = mol.Builder.NewBond(a3, a13, BondOrder.Single); mol.Bonds.Add(b7); var b8 = mol.Builder.NewBond(a3, a25, BondOrder.Single); mol.Bonds.Add(b8); var b9 = mol.Builder.NewBond(a4, a14, BondOrder.Single); mol.Bonds.Add(b9); var b10 = mol.Builder.NewBond(a4, a26, BondOrder.Single); mol.Bonds.Add(b10); var b11 = mol.Builder.NewBond(a5, a16, BondOrder.Single); mol.Bonds.Add(b11); var b12 = mol.Builder.NewBond(a6, a15, BondOrder.Single); mol.Bonds.Add(b12); var b13 = mol.Builder.NewBond(a6, a27, BondOrder.Single); mol.Bonds.Add(b13); var b14 = mol.Builder.NewBond(a7, a17, BondOrder.Single); mol.Bonds.Add(b14); var b15 = mol.Builder.NewBond(a7, a28, BondOrder.Single); mol.Bonds.Add(b15); var b16 = mol.Builder.NewBond(a8, a17, BondOrder.Double); mol.Bonds.Add(b16); var b17 = mol.Builder.NewBond(a9, a29, BondOrder.Single); mol.Bonds.Add(b17); var b18 = mol.Builder.NewBond(a12, a13, BondOrder.Single); mol.Bonds.Add(b18); var b19 = mol.Builder.NewBond(a12, a14, BondOrder.Single); mol.Bonds.Add(b19); var b20 = mol.Builder.NewBond(a12, a18, BondOrder.Single); mol.Bonds.Add(b20); var b21 = mol.Builder.NewBond(a13, a15, BondOrder.Single); mol.Bonds.Add(b21); var b22 = mol.Builder.NewBond(a13, a19, BondOrder.Single); mol.Bonds.Add(b22); var b23 = mol.Builder.NewBond(a14, a16, BondOrder.Single); mol.Bonds.Add(b23); var b24 = mol.Builder.NewBond(a14, a20, BondOrder.Single); mol.Bonds.Add(b24); var b25 = mol.Builder.NewBond(a15, a17, BondOrder.Single); mol.Bonds.Add(b25); var b26 = mol.Builder.NewBond(a15, a21, BondOrder.Single); mol.Bonds.Add(b26); var b27 = mol.Builder.NewBond(a16, a22, BondOrder.Single); mol.Bonds.Add(b27); var b28 = mol.Builder.NewBond(a16, a23, BondOrder.Single); mol.Bonds.Add(b28); AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(mol); AddImplicitHydrogens(mol); var result = CreateDescriptor().Calculate(mol); Assert.AreEqual(2, result.Value); }
internal IReactionSet Initiate(IChemObjectSet <IAtomContainer> reactants, IChemObjectSet <IAtomContainer> agents, int length, bool checkPrev, AtomCheck atomCheck) { CheckInitiateParams(reactants, agents); IReactionSet setOfReactions = reactants.Builder.NewReactionSet(); IAtomContainer reactant = reactants[0]; AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(reactant); Aromaticity.CDKLegacy.Apply(reactant); AllRingsFinder arf = new AllRingsFinder(); IRingSet ringSet = arf.FindAllRings(reactant); for (int ir = 0; ir < ringSet.Count; ir++) { IRing ring = (IRing)ringSet[ir]; for (int jr = 0; jr < ring.Atoms.Count; jr++) { IAtom aring = ring.Atoms[jr]; aring.IsInRing = true; } } // if the parameter hasActiveCenter is not fixed yet, set the active centers IParameterReaction ipr = base.GetParameterClass(typeof(SetReactionCenter)); if (ipr != null && !ipr.IsSetParameter) { SetActiveCenters(reactant, length, checkPrev, atomCheck); } HOSECodeGenerator hcg = new HOSECodeGenerator(); foreach (var atomi in reactant.Atoms) { if (atomi.IsReactiveCenter && reactant.GetConnectedSingleElectrons(atomi).Count() == 1) { IEnumerable <IAtom> atom1s = null; if (checkPrev) { hcg.GetSpheres(reactant, atomi, length - 1, true); atom1s = hcg.GetNodesInSphere(length - 1); } hcg.GetSpheres(reactant, atomi, length, true); foreach (var atoml in hcg.GetNodesInSphere(length)) { if (atoml != null && atoml.IsReactiveCenter && !atoml.IsInRing && (atoml.FormalCharge ?? 0) == 0 && !atoml.AtomicNumber.Equals(AtomicNumbers.H) && reactant.GetMaximumBondOrder(atoml) == BondOrder.Single) { foreach (var atomR in reactant.GetConnectedAtoms(atoml)) { if (atom1s != null && atom1s.Contains(atomR)) { continue; } if (reactant.GetBond(atomR, atoml).IsReactiveCenter && atomR.IsReactiveCenter && atomCheck(atomR)) { var atomList = new List <IAtom> { atomR, atomi, atoml }; var bondList = new List <IBond> { reactant.GetBond(atomR, atoml) }; var moleculeSet = reactant.Builder.NewChemObjectSet <IAtomContainer>(); moleculeSet.Add(reactant); var reaction = Mechanism.Initiate(moleculeSet, atomList, bondList); if (reaction == null) { continue; } else { setOfReactions.Add(reaction); } } } } } } } return(setOfReactions); }
public void TestTwoGroup() { var mol = CDK.Builder.NewAtomContainer(); var a1 = mol.Builder.NewAtom("O"); a1.FormalCharge = 0; a1.Point2D = new Vector2(5.9019, 0.5282); mol.Atoms.Add(a1); var a2 = mol.Builder.NewAtom("O"); a2.FormalCharge = 0; a2.Point2D = new Vector2(5.3667, -1.1191); mol.Atoms.Add(a2); var a3 = mol.Builder.NewAtom("N"); a3.FormalCharge = 0; a3.Point2D = new Vector2(3.3987, -0.4197); mol.Atoms.Add(a3); var a4 = mol.Builder.NewAtom("N"); a4.FormalCharge = 0; a4.Point2D = new Vector2(2.5896, 0.1681); mol.Atoms.Add(a4); var a5 = mol.Builder.NewAtom("N"); a5.FormalCharge = 0; a5.Point2D = new Vector2(3.8987, 1.1191); mol.Atoms.Add(a5); var a6 = mol.Builder.NewAtom("N"); a6.FormalCharge = 0; a6.Point2D = new Vector2(2.8987, 1.1191); mol.Atoms.Add(a6); var a7 = mol.Builder.NewAtom("C"); a7.FormalCharge = 0; a7.Point2D = new Vector2(4.2077, 0.1681); mol.Atoms.Add(a7); var a8 = mol.Builder.NewAtom("C"); a8.FormalCharge = 0; a8.Point2D = new Vector2(5.1588, -0.141); mol.Atoms.Add(a8); var a9 = mol.Builder.NewAtom("H"); a9.FormalCharge = 0; a9.Point2D = new Vector2(2.0, -0.0235); mol.Atoms.Add(a9); var a10 = mol.Builder.NewAtom("H"); a10.FormalCharge = 0; a10.Point2D = new Vector2(6.4916, 0.3366); mol.Atoms.Add(a10); var b1 = mol.Builder.NewBond(a1, a8, BondOrder.Single); mol.Bonds.Add(b1); var b2 = mol.Builder.NewBond(a1, a10, BondOrder.Single); mol.Bonds.Add(b2); var b3 = mol.Builder.NewBond(a2, a8, BondOrder.Double); mol.Bonds.Add(b3); var b4 = mol.Builder.NewBond(a3, a4, BondOrder.Single); mol.Bonds.Add(b4); var b5 = mol.Builder.NewBond(a3, a7, BondOrder.Double); mol.Bonds.Add(b5); var b6 = mol.Builder.NewBond(a4, a6, BondOrder.Single); mol.Bonds.Add(b6); var b7 = mol.Builder.NewBond(a4, a9, BondOrder.Single); mol.Bonds.Add(b7); var b8 = mol.Builder.NewBond(a5, a6, BondOrder.Double); mol.Bonds.Add(b8); var b9 = mol.Builder.NewBond(a5, a7, BondOrder.Single); mol.Bonds.Add(b9); var b10 = mol.Builder.NewBond(a7, a8, BondOrder.Single); mol.Bonds.Add(b10); AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(mol); AddImplicitHydrogens(mol); var result = CreateDescriptor().Calculate(mol); Assert.AreEqual(3, result.Value); }
public static void FixAromaticityForXLogP(IAtomContainer m) { // need to find rings and aromaticity again since added H's IRingSet rs = null; try { AllRingsFinder arf = new AllRingsFinder(); rs = arf.FindAllRings(m); // SSSRFinder s = new SSSRFinder(m); // srs = s.FindEssentialRings(); } catch (Exception e) { Console.Out.WriteLine(e.StackTrace); } try { // figure out which atoms are in aromatic rings: AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(m); Aromaticity.CDKLegacy.Apply(m); // figure out which rings are aromatic: RingSetManipulator.MarkAromaticRings(rs); // figure out which simple (non cycles) rings are aromatic: // HueckelAromaticityDetector.DetectAromaticity(m, srs); } catch (Exception e) { Console.Out.WriteLine(e.StackTrace); } // only atoms in 6 membered rings are aromatic // determine largest ring that each atom is a part of for (int i = 0; i <= m.Atoms.Count - 1; i++) { m.Atoms[i].IsAromatic = false; for (int j = 0; j <= rs.Count - 1; j++) { //Debug.WriteLine(i+"\t"+j); IRing r = (IRing)rs[j]; if (!r.IsAromatic) { goto continue_jloop; } bool haveatom = r.Contains(m.Atoms[i]); //Debug.WriteLine("haveatom="+haveatom); if (haveatom && r.Atoms.Count == 6) { m.Atoms[i].IsAromatic = true; } continue_jloop: ; } } }
static IAtomContainer Percieve(IAtomContainer molecule) { AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(molecule); return(molecule); }