public static void RemoveAtomAndConnectedElectronContainers(IReaction reaction, IAtom atom) { IChemObjectSet <IAtomContainer> reactants = reaction.Reactants; for (int i = 0; i < reactants.Count; i++) { IAtomContainer mol = reactants[i]; if (mol.Contains(atom)) { mol.RemoveAtom(atom); } } var agents = reaction.Reactants; for (int i = 0; i < agents.Count; i++) { IAtomContainer mol = agents[i]; if (mol.Contains(atom)) { mol.RemoveAtom(atom); } } IChemObjectSet <IAtomContainer> products = reaction.Products; for (int i = 0; i < products.Count; i++) { IAtomContainer mol = products[i]; if (mol.Contains(atom)) { mol.RemoveAtom(atom); } } }
/// <summary> /// Writes a MoleculeSet to an Stream for the reaction. /// </summary> /// <param name="som">The MoleculeSet that is written to an Stream</param> private void WriteAtomContainerSet(IChemObjectSet <IAtomContainer> som) { for (int i = 0; i < som.Count; i++) { IAtomContainer mol = som[i]; for (int j = 0; j < som.GetMultiplier(i); j++) { StringWriter sw = new StringWriter(); writer.Write("$MOL"); writer.Write('\n'); MDLV2000Writer mdlwriter = null; try { mdlwriter = new MDLV2000Writer(sw); } catch (Exception ex) { Trace.TraceError(ex.Message); Debug.WriteLine(ex); throw new CDKException("Exception while creating MDLWriter: " + ex.Message, ex); } mdlwriter.Write(mol); mdlwriter.Close(); writer.Write(sw.ToString()); } } }
/// <summary> /// Returns a new Reaction object which is the reverse of the given /// Reaction. /// </summary> /// <param name="reaction">the reaction being considered</param> /// <returns>the reverse reaction</returns> public static IReaction Reverse(IReaction reaction) { IReaction reversedReaction = reaction.Builder.NewReaction(); if (reaction.Direction == ReactionDirection.Bidirectional) { reversedReaction.Direction = ReactionDirection.Bidirectional; } else if (reaction.Direction == ReactionDirection.Forward) { reversedReaction.Direction = ReactionDirection.Backward; } else if (reaction.Direction == ReactionDirection.Backward) { reversedReaction.Direction = ReactionDirection.Forward; } IChemObjectSet <IAtomContainer> reactants = reaction.Reactants; for (int i = 0; i < reactants.Count; i++) { double coefficient = reaction.Reactants.GetMultiplier(reactants[i]).Value; reversedReaction.Products.Add(reactants[i], coefficient); } IChemObjectSet <IAtomContainer> products = reaction.Products; for (int i = 0; i < products.Count; i++) { double coefficient = reaction.Products.GetMultiplier(products[i]).Value; reversedReaction.Reactants.Add(products[i], coefficient); } return(reversedReaction); }
public static IEnumerable <string> GetAllIDs(IReaction reaction) { if (reaction.Id != null) { yield return(reaction.Id); } IChemObjectSet <IAtomContainer> reactants = reaction.Reactants; for (int i = 0; i < reactants.Count; i++) { IAtomContainer mol = reactants[i]; foreach (var id in AtomContainerManipulator.GetAllIDs(mol)) { yield return(id); } } IChemObjectSet <IAtomContainer> products = reaction.Products; for (int i = 0; i < products.Count; i++) { IAtomContainer mol = products[i]; foreach (var id in AtomContainerManipulator.GetAllIDs(mol)) { yield return(id); } } yield break; }
public static void RemoveElectronContainer(IReaction reaction, IElectronContainer electrons) { IChemObjectSet <IAtomContainer> reactants = reaction.Reactants; for (int i = 0; i < reactants.Count; i++) { IAtomContainer mol = reactants[i]; if (mol.Contains(electrons)) { mol.Remove(electrons); } } var agents = reaction.Reactants; for (int i = 0; i < agents.Count; i++) { IAtomContainer mol = agents[i]; if (mol.Contains(electrons)) { mol.Remove(electrons); } } IChemObjectSet <IAtomContainer> products = reaction.Products; for (int i = 0; i < products.Count; i++) { IAtomContainer mol = products[i]; if (mol.Contains(electrons)) { mol.Remove(electrons); } } }
static void Main() { { #region SmilesParser sp = new SmilesParser(); IAtomContainer atomContainer = sp.ParseSmiles("CC(=O)OC(=O)C"); SMARTSQueryTool querytool = new SMARTSQueryTool("O=CO"); bool status = querytool.Matches(atomContainer); if (status) { int nmatch = querytool.MatchesCount; var mappings = querytool.GetMatchingAtoms(); foreach (var atomIndices in mappings) { // do something } } #endregion } { string someSmartsPattern = null; IChemObjectSet <IAtomContainer> molecules = null; #region SetAromaticity SMARTSQueryTool sqt = new SMARTSQueryTool(someSmartsPattern); sqt.SetAromaticity(new Aromaticity(ElectronDonation.CDKModel, Cycles.CDKAromaticSetFinder)); foreach (var molecule in molecules) { // CDK Aromatic model needs atom types AtomContainerManipulator.PercieveAtomTypesAndConfigureAtoms(molecule); sqt.Matches(molecule); } #endregion } }
/// <summary> /// get all Molecules object from a set of Reactions. /// </summary> /// <param name="set">The set of reaction to inspect</param> /// <returns>The IAtomContanerSet</returns> public static IChemObjectSet <IAtomContainer> GetAllMolecules(IReactionSet set) { IChemObjectSet <IAtomContainer> moleculeSet = set.Builder.NewAtomContainerSet(); foreach (var reaction in set) { var molecules = ReactionManipulator.GetAllMolecules(reaction); foreach (var ac in molecules) { bool contain = false; foreach (var atomContainer in moleculeSet) { if (atomContainer.Equals(ac)) { contain = true; break; } } if (!contain) { moleculeSet.Add(ac); } } } return(moleculeSet); }
public virtual void TestSort_BrokenComparator() { IChemObjectSet <T> set = (IChemObjectSet <T>)NewChemObject(); IChemObjectBuilder builder = set.Builder; T a = NewContainerObject(); T b = NewContainerObject(); a.Atoms.Add(builder.NewAtom("C")); a.Atoms.Add(builder.NewAtom("C")); b.Atoms.Add(builder.NewAtom("C")); set.Add(a); set.Add(b); // this comparator is deliberately broken but serves for the test // - null should be a high value (Interger.MAX) // - also, avoid boxed primitives in comparators set.Sort(new ComparerByBroken()); // despite null being low, the two atom containers should // still be in the first slot Assert.IsNotNull(set[0]); Assert.IsNotNull(set[1]); Assert.IsTrue(set.Count == 2 || set[2] == null); }
/// <summary> /// Remove an Atom and the connected ElectronContainers from all AtomContainers /// inside an IChemModel. /// </summary> /// <param name="chemModel">The IChemModel object.</param> /// <param name="atom">The Atom object to remove.</param> public static void RemoveAtomAndConnectedElectronContainers(IChemModel chemModel, IAtom atom) { ICrystal crystal = chemModel.Crystal; if (crystal != null) { if (crystal.Contains(atom)) { crystal.RemoveAtom(atom); } return; } IChemObjectSet <IAtomContainer> moleculeSet = chemModel.MoleculeSet; if (moleculeSet != null) { MoleculeSetManipulator.RemoveAtomAndConnectedElectronContainers(moleculeSet, atom); } IReactionSet reactionSet = chemModel.ReactionSet; if (reactionSet != null) { ReactionSetManipulator.RemoveAtomAndConnectedElectronContainers(reactionSet, atom); } }
public override void StartDocument() { ChemFile = CDK.Builder.NewChemFile(); chemSequence = CDK.Builder.NewChemSequence(); chemModel = CDK.Builder.NewChemModel(); setOfMolecules = CDK.Builder.NewChemObjectSet <IAtomContainer>(); }
/// <inheritdoc/> public override IReactionSet Initiate(IChemObjectSet <IAtomContainer> reactants, IChemObjectSet <IAtomContainer> agents) { return(base.Initiate(reactants, agents, null, (mol, atom) => atom.FormalCharge == 1, atom => (atom.FormalCharge ?? 0) == 0)); }
public void SetUp() { molecule1 = new AtomContainer(); atomInMol1 = new Atom("Cl") { Charge = -1.0, FormalCharge = -1, ImplicitHydrogenCount = 1 }; molecule1.Atoms.Add(atomInMol1); molecule1.Atoms.Add(new Atom("Cl")); bondInMol1 = new Bond(atomInMol1, molecule1.Atoms[1]); molecule1.Bonds.Add(bondInMol1); molecule2 = new AtomContainer(); atomInMol2 = new Atom("O") { ImplicitHydrogenCount = 2 }; molecule2.Atoms.Add(atomInMol2); moleculeSet = ChemObjectBuilder.Instance.NewAtomContainerSet(); moleculeSet.Add(molecule1); moleculeSet.Add(molecule2); reaction = new Reaction(); reaction.Reactants.Add(molecule1); reaction.Products.Add(molecule2); reactionSet = new ReactionSet { reaction }; chemModel = new ChemModel { MoleculeSet = moleculeSet, ReactionSet = reactionSet }; }
/// <summary> /// Read the file content into a <see cref="IAtomContainerSet"/>. /// </summary> /// <param name="molSet">an <see cref="IAtomContainerSet"/> to store the structures</param> /// <returns>the <see cref="IAtomContainerSet"/> containing the molecules read in</returns> /// <exception cref="IOException">if there is an error during reading</exception> private IChemObjectSet <IAtomContainer> ReadAtomContainerSet(IChemObjectSet <IAtomContainer> molSet) { SmilesParser parser = new SmilesParser(molSet.Builder, false); string line = input.ReadLine(); line = input.ReadLine(); // skip the first line while (line != null) { string[] cols = line.Split(','); try { var mol = parser.ParseSmiles(cols[1]); mol.SetProperty("focusSupport", cols[5]); mol.SetProperty("complementSupport", cols[7]); mol.SetProperty("atomCount", cols[2]); mol.SetProperty("bondCount", cols[3]); molSet.Add(mol); } catch (InvalidSmilesException exception) { Trace.TraceError($"Skipping invalid SMILES: {cols[1]}"); Debug.WriteLine(exception); } line = input.ReadLine(); } return(molSet); }
/// <summary> /// Adds a new Molecule to the MoleculeSet inside a given ChemModel. /// Creates a MoleculeSet if none is contained. /// </summary> /// <param name="chemModel">The ChemModel object.</param> /// <returns>The created Molecule object.</returns> public static IAtomContainer CreateNewMolecule(IChemModel chemModel) { // Add a new molecule either the set of molecules IAtomContainer molecule = chemModel.Builder.NewAtomContainer(); if (chemModel.MoleculeSet != null) { IChemObjectSet <IAtomContainer> moleculeSet = chemModel.MoleculeSet; for (int i = 0; i < moleculeSet.Count; i++) { if (moleculeSet[i].Atoms.Count == 0) { moleculeSet.RemoveAt(i); i--; } } moleculeSet.Add(molecule); } else { var moleculeSet = chemModel.Builder.NewAtomContainerSet(); moleculeSet.Add(molecule); chemModel.MoleculeSet = moleculeSet; } return(molecule); }
/// <inheritdoc/> public override IReactionSet Initiate(IChemObjectSet <IAtomContainer> reactants, IChemObjectSet <IAtomContainer> agents) { return(base.Initiate(reactants, agents, null, (mol, atom) => (atom.FormalCharge ?? 0) == 0 && mol.GetConnectedLonePairs(atom).Any(), atom => (atom.FormalCharge ?? 0) >= 0)); }
public override void Write(IChemObject obj) { if (obj is IAtomContainer) { try { IChemObjectSet <IAtomContainer> som = obj.Builder.NewAtomContainerSet(); som.Add((IAtomContainer)obj); WriteAtomContainer(som); } catch (Exception ex) { if (!(ex is ArgumentException | ex is IOException)) { throw; } throw new CDKException("Error while writing HIN file: " + ex.Message, ex); } } else if (obj is IEnumerableChemObject <IAtomContainer> ) { try { WriteAtomContainer((IEnumerableChemObject <IAtomContainer>)obj); } catch (IOException) { // } } else { throw new CDKException("HINWriter only supports output of Molecule or SetOfMolecule classes."); } }
/// <inheritdoc/> public override IReactionSet Initiate(IChemObjectSet <IAtomContainer> reactants, IChemObjectSet <IAtomContainer> agents) { return(base.Initiate(reactants, agents, reactant => AtomContainerManipulator.GetTotalNegativeFormalCharge(reactant) == 0, (mol, atom) => mol.GetConnectedSingleElectrons(atom).Count() == 1, atom => (atom.FormalCharge ?? 0) == 0)); }
internal IReactionSet Initiate(IChemObjectSet <IAtomContainer> reactants, IChemObjectSet <IAtomContainer> agents, BondCheck bondChecker) { CheckInitiateParams(reactants, agents); IReactionSet setOfReactions = reactants.Builder.NewReactionSet(); IAtomContainer reactant = reactants[0]; // 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, bondChecker); } foreach (var bondi in reactant.Bonds) { IAtom atom1 = bondi.Begin; IAtom atom2 = bondi.End; if (bondi.IsReactiveCenter && bondChecker(bondi) && atom1.IsReactiveCenter && atom2.IsReactiveCenter && (atom1.FormalCharge ?? 0) == 0 && (atom2.FormalCharge ?? 0) == 0 && !reactant.GetConnectedSingleElectrons(atom1).Any() && !reactant.GetConnectedSingleElectrons(atom2).Any()) { for (int j = 0; j < 2; j++) { var atomList = new List <IAtom>(); if (j == 0) { atomList.Add(atom1); atomList.Add(atom2); } else { atomList.Add(atom2); atomList.Add(atom1); } var bondList = new List <IBond> { bondi }; IChemObjectSet <IAtomContainer> moleculeSet = reactant.Builder.NewAtomContainerSet(); moleculeSet.Add(reactant); IReaction reaction = Mechanism.Initiate(moleculeSet, atomList, bondList); if (reaction == null) { continue; } else { setOfReactions.Add(reaction); } } } } return(setOfReactions); }
/// <summary> /// This badly named methods tries to determine which AtomContainer in the /// ChemModel is best suited to contain added Atom's and Bond's. /// </summary> public static IAtomContainer GetRelevantAtomContainer(IChemModel chemModel, IAtom atom) { IAtomContainer result = null; if (chemModel.MoleculeSet != null) { IChemObjectSet <IAtomContainer> moleculeSet = chemModel.MoleculeSet; result = MoleculeSetManipulator.GetRelevantAtomContainer(moleculeSet, atom); if (result != null) { return(result); } } if (chemModel.ReactionSet != null) { IReactionSet reactionSet = chemModel.ReactionSet; return(ReactionSetManipulator.GetRelevantAtomContainer(reactionSet, atom)); } if (chemModel.Crystal != null && chemModel.Crystal.Contains(atom)) { return(chemModel.Crystal); } if (chemModel.RingSet != null) { return(AtomContainerSetManipulator.GetRelevantAtomContainer(chemModel.RingSet, atom)); } throw new ArgumentException("The provided atom is not part of this IChemModel."); }
public void TestSetMoleculeSet_IAtomContainerSet() { IChemModel chemModel = (IChemModel)NewChemObject(); IChemObjectSet<IAtomContainer> crystal = chemModel.Builder.NewAtomContainerSet(); chemModel.MoleculeSet = crystal; Assert.AreEqual(crystal, chemModel.MoleculeSet); }
/// <summary> /// Remove an ElectronContainer from all AtomContainers /// inside an IChemModel. /// </summary> /// <param name="chemModel">The IChemModel object.</param> /// <param name="electrons">The ElectronContainer to remove.</param> public static void RemoveElectronContainer(IChemModel chemModel, IElectronContainer electrons) { ICrystal crystal = chemModel.Crystal; if (crystal != null) { if (crystal.Contains(electrons)) { crystal.Remove(electrons); } return; } IChemObjectSet <IAtomContainer> moleculeSet = chemModel.MoleculeSet; if (moleculeSet != null) { MoleculeSetManipulator.RemoveElectronContainer(moleculeSet, electrons); } IReactionSet reactionSet = chemModel.ReactionSet; if (reactionSet != null) { ReactionSetManipulator.RemoveElectronContainer(reactionSet, electrons); } }
public void SetUp() { molecule1 = builder.NewAtomContainer(); atomInMol1 = builder.NewAtom("Cl"); molecule1.Atoms.Add(atomInMol1); molecule1.Atoms.Add(builder.NewAtom("Cl")); bondInMol1 = builder.NewBond(atomInMol1, molecule1.Atoms[1]); molecule1.Bonds.Add(bondInMol1); molecule2 = builder.NewAtomContainer(); atomInMol2 = builder.NewAtom("O"); atomInMol2.ImplicitHydrogenCount = 2; molecule2.Atoms.Add(atomInMol2); moleculeSet = builder.NewChemObjectSet <IAtomContainer>(); moleculeSet.Add(molecule1); moleculeSet.Add(molecule2); reaction = builder.NewReaction(); reaction.Reactants.Add(molecule1); reaction.Products.Add(molecule2); reactionSet = builder.NewReactionSet(); reactionSet.Add(reaction); chemModel = builder.NewChemModel(); chemModel.MoleculeSet = moleculeSet; chemModel.ReactionSet = reactionSet; chemSequence1 = builder.NewChemSequence(); chemSequence1.Add(chemModel); chemSequence2 = builder.NewChemSequence(); chemFile = builder.NewChemFile(); chemFile.Add(chemSequence1); chemFile.Add(chemSequence2); }
public static void Main() { { IChemObjectSet <IAtomContainer> ms = null; #region AllRingsFinder arf = new AllRingsFinder(); foreach (var m in ms) { try { IRingSet rs = arf.FindAllRings(m); } catch (CDKException) { // molecule was too complex, handle error } } #endregion } { #region UsingThreshold // using static NCDK.RingSearches.AllRingsFinder.Threshold; AllRingsFinder arf = AllRingsFinder.UsingThreshold(Threshold.PubChem99); #endregion } }
public virtual void TestSort_Coefficients() { IChemObjectSet <T> set = (IChemObjectSet <T>)NewChemObject(); IChemObjectBuilder builder = set.Builder; T a = NewContainerObject(); T b = NewContainerObject(); a.Atoms.Add(builder.NewAtom("C")); a.Atoms.Add(builder.NewAtom("C")); b.Atoms.Add(builder.NewAtom("C")); set.Add(a, 1); set.Add(b, 2); Assert.AreEqual(a, set[0]); Assert.AreEqual(1D, set.GetMultiplier(0)); Assert.AreEqual(b, set[1]); Assert.AreEqual(2D, set.GetMultiplier(1)); // sort by atom container count var orderedSet = set.OrderBy(n => n, new ComparerByCoefficients()); AssertAreOrderLessEqual( new[] { a, b }.Cast <IChemObject>(), set.Cast <IChemObject>()); Assert.IsTrue(Compares.AreDeepEqual( new[] { 1D, 2D }, new[] { set.GetMultiplier(0), set.GetMultiplier(1) })); }
/// <inheritdoc/> public Rect CalculateDiagramBounds(IChemObjectSet <IAtomContainer> moleculeSet) { if (moleculeSet == null) { return(this.CalculateScreenBounds(new Rect())); } return(this.CalculateScreenBounds(BoundsCalculator.CalculateBounds(moleculeSet))); }
/// <summary> /// Set the scale for an <see cref="IChemObjectSet{T}"/>. It calculates the average bond length /// of the model and calculates the multiplication factor to transform this /// to the bond length that is set in the RendererModel. /// </summary> /// <param name="moleculeSet">the <see cref="IChemObjectSet{T}"/> for what to set the scale</param> public void SetScale(IChemObjectSet <IAtomContainer> moleculeSet) { double bondLength = AverageBondLengthCalculator.CalculateAverageBondLength(moleculeSet); double scale = this.CalculateScaleForBondLength(bondLength); // store the scale so that other components can access it this.rendererModel.SetScale(scale); }
public virtual void TestAddAtomContainer_IAtomContainer_Double() { IChemObjectSet <T> som = (IChemObjectSet <T>)NewChemObject(); som.Add(NewContainerObject(), 2.0); Assert.AreEqual(1, som.Count); Assert.AreEqual(2.0, som.GetMultiplier(0).Value, 0.00001); }
public override void TestClone() { IChemObjectSet <IAtomContainer> containerSet = (IChemObjectSet <IAtomContainer>)NewChemObject(); object clone = containerSet.Clone(); Assert.IsTrue(clone is IChemObjectSet <IAtomContainer>); Assert.AreNotSame(containerSet, clone); }
/// <summary> /// Constructs an empty, forward reaction. /// </summary> public Reaction() { this.reactants = Builder.NewAtomContainerSet(); this.products = Builder.NewAtomContainerSet(); this.agents = Builder.NewAtomContainerSet(); this.mappings = new List <IMapping>(); direction = ReactionDirection.Forward; }
public virtual void TestGetMultiplier_IAtomContainer() { IChemObjectSet <T> som = (IChemObjectSet <T>)NewChemObject(); som.Add(NewContainerObject()); Assert.AreEqual(-1.0, som.GetMultiplier(NewContainerObject()).Value, 0.00001); }