Example #1
0
        /// <summary>
        /// Converts a reaction to an 'inlined' reaction stored as a molecule. All
        /// reactants, agents, products are added to the molecule as disconnected
        /// components with atoms flagged as to their role <see cref="ReactionRole"/> and
        /// component group.
        /// </summary>
        /// <remarks>
        /// The inlined reaction, stored in a molecule can be converted back to an explicit
        /// reaction with <see cref="ToReaction(IAtomContainer)"/>. Data stored on the individual components (e.g.
        /// titles is lost in the conversion).
        /// </remarks>
        /// <param name="rxn">reaction to convert</param>
        /// <returns>inlined reaction stored in a molecule</returns>
        /// <seealso cref="ToReaction(IAtomContainer)"/>
        public static IAtomContainer ToMolecule(IReaction rxn)
        {
            if (rxn == null)
            {
                throw new ArgumentNullException(nameof(rxn), "Null reaction provided");
            }
            var bldr = rxn.Builder;
            var mol  = bldr.NewAtomContainer();

            mol.SetProperties(rxn.GetProperties());
            mol.Id = rxn.Id;
            int grpId = 0;

            foreach (IAtomContainer comp in rxn.Reactants)
            {
                AssignRoleAndGroup(comp, ReactionRole.Reactant, ++grpId);
                mol.Add(comp);
            }
            foreach (IAtomContainer comp in rxn.Agents)
            {
                AssignRoleAndGroup(comp, ReactionRole.Agent, ++grpId);
                mol.Add(comp);
            }
            foreach (IAtomContainer comp in rxn.Products)
            {
                AssignRoleAndGroup(comp, ReactionRole.Product, ++grpId);
                mol.Add(comp);
            }
            return(mol);
        }
Example #2
0
        private CMLReaction CDKReactionToCMLReaction(IReaction reaction, bool setIDs)
        {
            var cmlReaction = new CMLReaction();

            if (useCMLIDs && setIDs)
            {
                IDCreator.CreateIDs(reaction);
            }
            if (!string.IsNullOrEmpty(reaction.Id))
            {
                cmlReaction.Id = reaction.Id;
            }

            var props = reaction.GetProperties();

            foreach (var key in props.Keys)
            {
                var value = props[key];
                if (value is string)
                {
                    if (!string.Equals(key.ToString(), CDKPropertyName.Title, StringComparison.Ordinal))
                    {
                        var scalar = new CMLScalar();
                        this.CheckPrefix(scalar);
                        scalar.DictRef = "cdk:reactionProperty";
                        scalar.Title   = key.ToString();
                        scalar.SetValue(value.ToString());
                        cmlReaction.Add(scalar);
                    }
                }
            }

            // reactants
            var cmlReactants = new CMLReactantList();

            foreach (var reactant in reaction.Reactants)
            {
                var cmlReactant = new CMLReactant();
                cmlReactant.Add(CDKAtomContainerToCMLMolecule(reactant));
                cmlReactants.Add(cmlReactant);
            }

            // products
            var cmlProducts = new CMLProductList();

            foreach (var product in reaction.Products)
            {
                var cmlProduct = new CMLProduct();
                cmlProduct.Add(CDKAtomContainerToCMLMolecule(product));
                cmlProducts.Add(cmlProduct);
            }

            // substance
            var cmlSubstances = new CMLSubstanceList();

            foreach (var agent in reaction.Agents)
            {
                var cmlSubstance = new CMLSubstance();
                cmlSubstance.Add(CDKAtomContainerToCMLMolecule(agent));
                cmlSubstances.Add(cmlSubstance);
            }
            if (reaction.Id != null)
            {
                cmlReaction.Id = reaction.Id;
            }

            cmlReaction.Add(cmlReactants);
            cmlReaction.Add(cmlProducts);
            cmlReaction.Add(cmlSubstances);
            return(cmlReaction);
        }