public void ClearProps() { IAtomContainer mol = LoadSmi("o1cccc1"); int sizeBefore = mol.GetProperties().Count; Assert.IsTrue(mmff.AssignAtomTypes(mol)); Assert.IsTrue(mmff.PartialCharges(mol)); mmff.ClearProps(mol); Assert.AreEqual(sizeBefore, mol.GetProperties().Count); }
private void WriteMolecule(IAtomContainer container) { try { // write the MDL molfile bits StringWriter stringWriter = new StringWriter(); IChemObjectWriter mdlWriter; if (WriteV3000(container)) { mdlWriter = new MDLV3000Writer(stringWriter); } else { mdlWriter = new MDLV2000Writer(stringWriter); } mdlWriter.AddSettings(IOSettings.Settings); mdlWriter.Write(container); mdlWriter.Close(); writer.Write(stringWriter.ToString()); // write non-structural data (mol properties in our case) if (paramWriteData.IsSet) { var sdFields = container.GetProperties(); bool writeAllProperties = propertiesToWrite == null; if (sdFields != null) { foreach (var propKey in sdFields.Keys) { string headerKey = propKey.ToString(); if (!IsCDKInternalProperty(headerKey)) { if (writeAllProperties || propertiesToWrite.Contains(headerKey)) { string cleanHeaderKey = ReplaceInvalidHeaderChars(headerKey); if (!cleanHeaderKey.Equals(headerKey, StringComparison.Ordinal)) { Trace.TraceInformation("Replaced characters in SDfile data header: ", headerKey, " written as: ", cleanHeaderKey); } Object val = sdFields[propKey]; if (IsPrimitiveDataValue(val)) { writer.Write("> <" + cleanHeaderKey + ">"); writer.Write('\n'); if (val != null) { writer.Write(val.ToString()); } writer.Write('\n'); writer.Write('\n'); } else { Trace.TraceInformation("Skipped property " + propKey + " because only primitive and string properties can be written by SDFWriter"); } } } } } } writer.Write("$$$$\n"); } catch (IOException exception) { throw new CDKException("Error while writing a SD file entry: " + exception.Message, exception); } }
private CMLMolecule CDKAtomContainerToCMLMolecule(IAtomContainer structure, bool setIDs, bool isRef) { var cmlMolecule = new CMLMolecule(); if (useCMLIDs && setIDs) { IDCreator.CreateIDs(structure); } this.CheckPrefix(cmlMolecule); if (!string.IsNullOrEmpty(structure.Id)) { if (!isRef) { cmlMolecule.Id = structure.Id; } else { cmlMolecule.Ref = structure.Id; } } if (structure.Title != null) { cmlMolecule.Title = structure.Title; } if (structure.GetProperty <string>(CDKPropertyName.InChI) != null) { var ident = new CMLIdentifier { Convention = "iupac:inchi" }; ident.SetAttributeValue(CMLElement.Attribute_value, structure.GetProperty <string>(CDKPropertyName.InChI)); cmlMolecule.Add(ident); } if (!isRef) { for (int i = 0; i < structure.Atoms.Count; i++) { var cdkAtom = structure.Atoms[i]; var cmlAtom = CDKAtomToCMLAtom(structure, cdkAtom); if (structure.GetConnectedSingleElectrons(cdkAtom).Count() > 0) { cmlAtom.SpinMultiplicity = structure.GetConnectedSingleElectrons(cdkAtom).Count() + 1; } cmlMolecule.Add(cmlAtom); } for (int i = 0; i < structure.Bonds.Count; i++) { var cmlBond = CDKJBondToCMLBond(structure.Bonds[i]); cmlMolecule.Add(cmlBond); } } // ok, output molecular properties, but not TITLE, INCHI, or DictRef's var props = structure.GetProperties(); foreach (var propKey in props.Keys) { if (propKey is string key) { // but only if a string if (!isRef) { object value = props[key]; switch (key) { case CDKPropertyName.Title: case CDKPropertyName.InChI: break; default: // ok, should output this var scalar = new CMLScalar(); this.CheckPrefix(scalar); scalar.DictRef = "cdk:molecularProperty"; scalar.Title = (string)key; scalar.SetValue(value.ToString()); cmlMolecule.Add(scalar); break; } } // FIXME: At the moment the order writing the formula is into properties // but it should be that IMolecularFormula is a extension of IAtomContainer if (!isRef && string.Equals(key, CDKPropertyName.Formula, StringComparison.Ordinal)) { switch (props[key]) { case IMolecularFormula cdkFormula: { var cmlFormula = new CMLFormula(); var isotopesList = MolecularFormulaManipulator.PutInOrder(MolecularFormulaManipulator.OrderEle, cdkFormula); foreach (var isotope in isotopesList) { cmlFormula.Add(isotope.Symbol, cdkFormula.GetCount(isotope)); } cmlMolecule.Add(cmlFormula); } break; case IMolecularFormulaSet cdkFormulaSet: foreach (var cdkFormula in cdkFormulaSet) { var isotopesList = MolecularFormulaManipulator.PutInOrder(MolecularFormulaManipulator.OrderEle, cdkFormula); var cmlFormula = new CMLFormula { DictRef = "cdk:possibleMachts" }; foreach (var isotope in isotopesList) { cmlFormula.Add(isotope.Symbol, cdkFormula.GetCount(isotope)); } cmlMolecule.Add(cmlFormula); } break; } } } } foreach (var element in customizers.Keys) { var customizer = customizers[element]; try { customizer.Customize(structure, cmlMolecule); } catch (Exception exception) { Trace.TraceError($"Error while customizing CML output with customizer: {customizer.GetType().Name}"); Debug.WriteLine(exception); } } return(cmlMolecule); }
/// <summary> /// Converts an 'inlined' reaction stored in a molecule back to a reaction. /// </summary> /// <param name="mol">molecule to convert</param> /// <returns>reaction</returns> /// <seealso cref="ToMolecule(IReaction)"/> public static IReaction ToReaction(IAtomContainer mol) { if (mol == null) { throw new ArgumentException("Null molecule provided"); } var bldr = mol.Builder; var rxn = bldr.NewReaction(); rxn.SetProperties(mol.GetProperties()); rxn.Id = mol.Id; var components = new Dictionary <int, IAtomContainer>(); // split atoms foreach (var atom in mol.Atoms) { var role = atom.GetProperty <ReactionRole?>(CDKPropertyName.ReactionRole); var grpIdx = atom.GetProperty <int?>(CDKPropertyName.ReactionGroup); if (role == null || role.Value == ReactionRole.None) { throw new ArgumentException("Atom " + mol.Atoms.IndexOf(atom) + " had undefined role"); } if (grpIdx == null) { throw new ArgumentException("Atom " + mol.Atoms.IndexOf(atom) + " had no reaction group id"); } // new component, and add to appropriate role if (!components.TryGetValue(grpIdx.Value, out IAtomContainer comp)) { components[grpIdx.Value] = comp = bldr.NewAtomContainer(); switch (role) { case ReactionRole.Reactant: rxn.Reactants.Add(comp); break; case ReactionRole.Product: rxn.Products.Add(comp); break; case ReactionRole.Agent: rxn.Agents.Add(comp); break; } } comp.Atoms.Add(atom); } // split bonds foreach (var bond in mol.Bonds) { var beg = bond.Begin; var end = bond.End; var begIdx = beg.GetProperty <int?>(CDKPropertyName.ReactionGroup); var endIdx = end.GetProperty <int?>(CDKPropertyName.ReactionGroup); if (begIdx == null || endIdx == null) { throw new ArgumentException("Bond " + mol.Bonds.IndexOf(bond) + " had atoms with no reaction group id"); } if (!begIdx.Equals(endIdx)) { throw new ArgumentException("Bond " + mol.Bonds.IndexOf(bond) + " had atoms with different reaction group id"); } components[begIdx.Value].Bonds.Add(bond); } // split stereochemistry foreach (var se in mol.StereoElements) { IAtom focus = null; switch (se) { case ITetrahedralChirality tc: focus = tc.ChiralAtom; break; case IDoubleBondStereochemistry ds: focus = ds.StereoBond.Begin; break; case ExtendedTetrahedral et: focus = et.Focus; break; } if (focus == null) { throw new ArgumentException("Stereochemistry had no focus"); } var grpIdx = focus.GetProperty <int>(CDKPropertyName.ReactionGroup); components[grpIdx].StereoElements.Add(se); } foreach (var se in mol.SingleElectrons) { var grpIdx = se.Atom.GetProperty <int>(CDKPropertyName.ReactionGroup); components[grpIdx].SingleElectrons.Add(se); } foreach (var lp in mol.LonePairs) { var grpIdx = lp.Atom.GetProperty <int>(CDKPropertyName.ReactionGroup); components[grpIdx].LonePairs.Add(lp); } return(rxn); }