addProduct() public method

public addProduct ( Product, prod ) : void
prod Product,
return void
Example #1
0
        /// <summary> Returns a new Reaction object which is the reverse of the given
        /// Reaction.
        /// </summary>
        public static IReaction reverse(IReaction reaction)
        {
            IReaction reversedReaction = reaction.Builder.newReaction();

            if (reaction.Direction == IReaction_Fields.BIDIRECTIONAL)
            {
                reversedReaction.Direction = IReaction_Fields.BIDIRECTIONAL;
            }
            else if (reaction.Direction == IReaction_Fields.FORWARD)
            {
                reversedReaction.Direction = IReaction_Fields.BACKWARD;
            }
            else if (reaction.Direction == IReaction_Fields.BACKWARD)
            {
                reversedReaction.Direction = IReaction_Fields.FORWARD;
            }
            IMolecule[] reactants = reaction.Reactants.Molecules;
            for (int i = 0; i < reactants.Length; i++)
            {
                double coefficient = reaction.getReactantCoefficient(reactants[i]);
                reversedReaction.addProduct(reactants[i], coefficient);
            }
            IMolecule[] products = reaction.Products.Molecules;
            for (int i = 0; i < products.Length; i++)
            {
                double coefficient = reaction.getProductCoefficient(products[i]);
                reversedReaction.addReactant(products[i], coefficient);
            }
            return(reversedReaction);
        }
Example #2
0
 /// <summary> Procedure required by the CDOInterface. This function is only
 /// supposed to be called by the JCFL library
 /// </summary>
 public virtual void endObject(System.String objectType)
 {
     //logger.debug("END: " + objectType);
     if (objectType.Equals("Molecule"))
     {
         if (currentMolecule is IMolecule)
         {
             //logger.debug("Adding molecule to set");
             currentSetOfMolecules.addMolecule((IMolecule)currentMolecule);
             //logger.debug("#mols in set: " + currentSetOfMolecules.MoleculeCount);
         }
         else if (currentMolecule is ICrystal)
         {
             //logger.debug("Adding crystal to chemModel");
             currentChemModel.Crystal = (ICrystal)currentMolecule;
             currentChemSequence.addChemModel(currentChemModel);
         }
     }
     else if (objectType.Equals("SetOfMolecules"))
     {
         currentChemModel.SetOfMolecules = currentSetOfMolecules;
         currentChemSequence.addChemModel(currentChemModel);
     }
     else if (objectType.Equals("Frame"))
     {
         // endObject("Molecule");
     }
     else if (objectType.Equals("Animation"))
     {
         addChemSequence(currentChemSequence);
         //logger.info("This file has " + ChemSequenceCount + " sequence(s).");
     }
     else if (objectType.Equals("Atom"))
     {
         currentMolecule.addAtom(currentAtom);
     }
     else if (objectType.Equals("Bond"))
     {
         //logger.debug("Bond(" + bond_id + "): " + bond_a1 + ", " + bond_a2 + ", " + bond_order);
         if (bond_a1 > currentMolecule.AtomCount || bond_a2 > currentMolecule.AtomCount)
         {
             //logger.error("Cannot add bond between at least one non-existant atom: " + bond_a1 + " and " + bond_a2);
         }
         else
         {
             IAtom a1 = currentMolecule.getAtomAt(bond_a1);
             IAtom a2 = currentMolecule.getAtomAt(bond_a2);
             IBond b  = currentChemFile.Builder.newBond(a1, a2, bond_order);
             if (bond_id != null)
             {
                 b.ID = bond_id;
             }
             if (bond_stereo != -99)
             {
                 b.Stereo = bond_stereo;
             }
             if (bond_order == CDKConstants.BONDORDER_AROMATIC)
             {
                 b.setFlag(CDKConstants.ISAROMATIC, true);
             }
             currentMolecule.addBond(b);
         }
     }
     else if (objectType.Equals("a-axis"))
     {
         // set these variables
         if (currentMolecule is ICrystal)
         {
             ICrystal current = (ICrystal)currentMolecule;
             current.A = new Vector3d(crystal_axis_x, crystal_axis_y, crystal_axis_z);
         }
         else
         {
             //logger.warn("Current object is not a crystal");
         }
     }
     else if (objectType.Equals("b-axis"))
     {
         if (currentMolecule is ICrystal)
         {
             ICrystal current = (ICrystal)currentMolecule;
             current.B = new Vector3d(crystal_axis_x, crystal_axis_y, crystal_axis_z);
         }
         else
         {
             //logger.warn("Current object is not a crystal");
         }
     }
     else if (objectType.Equals("c-axis"))
     {
         if (currentMolecule is ICrystal)
         {
             ICrystal current = (ICrystal)currentMolecule;
             current.C = new Vector3d(crystal_axis_x, crystal_axis_y, crystal_axis_z);
         }
         else
         {
             //logger.warn("Current object is not a crystal");
         }
     }
     else if (objectType.Equals("SetOfReactions"))
     {
         currentChemModel.SetOfReactions = currentSetOfReactions;
         currentChemSequence.addChemModel(currentChemModel);
         /* FIXME: this should be when document is closed! */
     }
     else if (objectType.Equals("Reaction"))
     {
         //logger.debug("Adding reaction to SOR");
         currentSetOfReactions.addReaction(currentReaction);
     }
     else if (objectType.Equals("Reactant"))
     {
         currentReaction.addReactant((IMolecule)currentMolecule);
     }
     else if (objectType.Equals("Product"))
     {
         currentReaction.addProduct((IMolecule)currentMolecule);
     }
     else if (objectType.Equals("Crystal"))
     {
         //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
         //logger.debug("Crystal: " + currentMolecule);
     }
 }
Example #3
0
        private IReaction readReaction(IChemObjectBuilder builder)
        {
            IReaction reaction = builder.newReaction();

            readLine(); // first line should be $RXN
            readLine(); // second line
            readLine(); // third line
            readLine(); // fourth line

            int  reactantCount = 0;
            int  productCount  = 0;
            bool foundCOUNTS   = false;

            while (Ready && !foundCOUNTS)
            {
                System.String command = readCommand();
                if (command.StartsWith("COUNTS"))
                {
                    SupportClass.Tokenizer tokenizer = new SupportClass.Tokenizer(command);
                    try
                    {
                        tokenizer.NextToken();
                        reactantCount = System.Int32.Parse(tokenizer.NextToken());
                        //logger.info("Expecting " + reactantCount + " reactants in file");
                        productCount = System.Int32.Parse(tokenizer.NextToken());
                        //logger.info("Expecting " + productCount + " products in file");
                    }
                    catch (System.Exception exception)
                    {
                        //logger.debug(exception);
                        throw new CDKException("Error while counts line of RXN file", exception);
                    }
                    foundCOUNTS = true;
                }
                else
                {
                    //logger.warn("Waiting for COUNTS line, but found: " + command);
                }
            }

            // now read the reactants
            for (int i = 1; i <= reactantCount; i++)
            {
                System.Text.StringBuilder molFile             = new System.Text.StringBuilder();
                System.String             announceMDLFileLine = readCommand();
                if (!announceMDLFileLine.Equals("BEGIN REACTANT"))
                {
                    System.String error = "Excepted start of reactant, but found: " + announceMDLFileLine;
                    //logger.error(error);
                    throw new CDKException(error);
                }
                System.String molFileLine = "";
                while (!molFileLine.EndsWith("END REACTANT"))
                {
                    molFileLine = readLine();
                    molFile.Append(molFileLine);
                    molFile.Append("\n");
                }
                ;

                try
                {
                    // read MDL molfile content
                    MDLV3000Reader reader   = new MDLV3000Reader(new StreamReader(molFile.ToString()));
                    IMolecule      reactant = (IMolecule)reader.read(builder.newMolecule());

                    // add reactant
                    reaction.addReactant(reactant);
                }
                catch (System.Exception exception)
                {
                    //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                    System.String error = "Error while reading reactant: " + exception.Message;
                    //logger.error(error);
                    //logger.debug(exception);
                    throw new CDKException(error, exception);
                }
            }

            // now read the products
            for (int i = 1; i <= productCount; i++)
            {
                System.Text.StringBuilder molFile             = new System.Text.StringBuilder();
                System.String             announceMDLFileLine = readCommand();
                if (!announceMDLFileLine.Equals("BEGIN PRODUCT"))
                {
                    System.String error = "Excepted start of product, but found: " + announceMDLFileLine;
                    //logger.error(error);
                    throw new CDKException(error);
                }
                System.String molFileLine = "";
                while (!molFileLine.EndsWith("END PRODUCT"))
                {
                    molFileLine = readLine();
                    molFile.Append(molFileLine);
                    molFile.Append("\n");
                }
                ;

                try
                {
                    // read MDL molfile content
                    MDLV3000Reader reader  = new MDLV3000Reader(new StreamReader(molFile.ToString()));
                    IMolecule      product = (IMolecule)reader.read(builder.newMolecule());

                    // add product
                    reaction.addProduct(product);
                }
                catch (System.Exception exception)
                {
                    //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                    System.String error = "Error while reading product: " + exception.Message;
                    //logger.error(error);
                    //logger.debug(exception);
                    throw new CDKException(error, exception);
                }
            }

            return(reaction);
        }
Example #4
0
        /// <summary> Read a Reaction from a file in MDL RXN format
        ///
        /// </summary>
        /// <returns>  The Reaction that was read from the MDL file.
        /// </returns>
        private IReaction readReaction(IChemObjectBuilder builder)
        {
            IReaction reaction = builder.newReaction();

            try
            {
                input.ReadLine(); // first line should be $RXN
                input.ReadLine(); // second line
                input.ReadLine(); // third line
                input.ReadLine(); // fourth line
            }
            catch (System.IO.IOException exception)
            {
                //logger.debug(exception);
                throw new CDKException("Error while reading header of RXN file", exception);
            }

            int reactantCount = 0;
            int productCount  = 0;

            try
            {
                System.String countsLine = input.ReadLine();

                /* this line contains the number of reactants
                 * and products */
                SupportClass.Tokenizer tokenizer = new SupportClass.Tokenizer(countsLine);
                reactantCount = System.Int32.Parse(tokenizer.NextToken());
                //logger.info("Expecting " + reactantCount + " reactants in file");
                productCount = System.Int32.Parse(tokenizer.NextToken());
                //logger.info("Expecting " + productCount + " products in file");
            }
            catch (System.Exception exception)
            {
                //logger.debug(exception);
                throw new CDKException("Error while counts line of RXN file", exception);
            }

            // now read the reactants
            try
            {
                for (int i = 1; i <= reactantCount; i++)
                {
                    System.Text.StringBuilder molFile = new System.Text.StringBuilder();
                    input.ReadLine(); // announceMDLFileLine
                    System.String molFileLine = "";
                    do
                    {
                        molFileLine = input.ReadLine();
                        molFile.Append(molFileLine);
                        molFile.Append("\n");
                    }while (!molFileLine.Equals("M  END"));

                    // read MDL molfile content
                    MDLReader reader   = new MDLReader(new StreamReader(molFile.ToString()));
                    IMolecule reactant = (IMolecule)reader.read(builder.newMolecule());

                    // add reactant
                    reaction.addReactant(reactant);
                }
            }
            catch (CDKException exception)
            {
                // rethrow exception from MDLReader
                throw exception;
            }
            catch (System.Exception exception)
            {
                //logger.debug(exception);
                throw new CDKException("Error while reading reactant", exception);
            }

            // now read the products
            try
            {
                for (int i = 1; i <= productCount; i++)
                {
                    System.Text.StringBuilder molFile = new System.Text.StringBuilder();
                    input.ReadLine(); // String announceMDLFileLine =
                    System.String molFileLine = "";
                    do
                    {
                        molFileLine = input.ReadLine();
                        molFile.Append(molFileLine);
                        molFile.Append("\n");
                    }while (!molFileLine.Equals("M  END"));

                    // read MDL molfile content
                    MDLReader reader  = new MDLReader(new StreamReader(molFile.ToString()));
                    IMolecule product = (IMolecule)reader.read(builder.newMolecule());

                    // add reactant
                    reaction.addProduct(product);
                }
            }
            catch (CDKException exception)
            {
                // rethrow exception from MDLReader
                throw exception;
            }
            catch (System.Exception exception)
            {
                //logger.debug(exception);
                throw new CDKException("Error while reading products", exception);
            }

            // now try to map things, if wanted
            //logger.info("Reading atom-atom mapping from file");
            // distribute all atoms over two AtomContainer's
            IAtomContainer reactingSide = builder.newAtomContainer();

            IMolecule[] molecules = reaction.Reactants.Molecules;
            for (int i = 0; i < molecules.Length; i++)
            {
                reactingSide.add(molecules[i]);
            }
            IAtomContainer producedSide = builder.newAtomContainer();

            molecules = reaction.Products.Molecules;
            for (int i = 0; i < molecules.Length; i++)
            {
                producedSide.add(molecules[i]);
            }

            // map the atoms
            int mappingCount = 0;

            IAtom[] reactantAtoms = reactingSide.Atoms;
            IAtom[] producedAtoms = producedSide.Atoms;
            for (int i = 0; i < reactantAtoms.Length; i++)
            {
                for (int j = 0; j < producedAtoms.Length; j++)
                {
                    if (reactantAtoms[i].ID != null && reactantAtoms[i].ID.Equals(producedAtoms[j].ID))
                    {
                        reaction.addMapping(builder.newMapping(reactantAtoms[i], producedAtoms[j]));
                        mappingCount++;
                        break;
                    }
                }
            }
            //logger.info("Mapped atom pairs: " + mappingCount);

            return(reaction);
        }