/// <summary> Tries to saturate a bond by increasing its bond orders by 1.0.
        ///
        /// </summary>
        /// <returns> true if the bond could be increased
        /// </returns>
        public virtual bool saturateByIncreasingBondOrder(IBond bond, IAtomContainer atomContainer, double increment)
        {
            IAtom[] atoms   = bond.getAtoms();
            IAtom   atom    = atoms[0];
            IAtom   partner = atoms[1];

            //logger.debug("  saturating bond: ", atom.Symbol, "-", partner.Symbol);
            IAtomType[] atomTypes1 = getAtomTypeFactory(bond.Builder).getAtomTypes(atom.Symbol);
            IAtomType[] atomTypes2 = getAtomTypeFactory(bond.Builder).getAtomTypes(partner.Symbol);
            for (int atCounter1 = 0; atCounter1 < atomTypes1.Length; atCounter1++)
            {
                IAtomType aType1 = atomTypes1[atCounter1];
                //logger.debug("  condidering atom type: ", aType1);
                if (couldMatchAtomType(atomContainer, atom, aType1))
                {
                    //logger.debug("  trying atom type: ", aType1);
                    for (int atCounter2 = 0; atCounter2 < atomTypes2.Length; atCounter2++)
                    {
                        IAtomType aType2 = atomTypes2[atCounter2];
                        //logger.debug("  condidering partner type: ", aType1);
                        if (couldMatchAtomType(atomContainer, partner, atomTypes2[atCounter2]))
                        {
                            //logger.debug("    with atom type: ", aType2);
                            if (bond.Order < aType2.MaxBondOrder && bond.Order < aType1.MaxBondOrder)
                            {
                                bond.Order = bond.Order + increment;
                                //logger.debug("Bond order now ", bond.Order);
                                return(true);
                            }
                        }
                    }
                }
            }
            return(false);
        }
Exemple #2
0
        /// <summary>
        /// Tells if a certain bond is center of a valid double bond configuration.
        /// </summary>
        /// <param name="container"> The atomcontainer.
        /// </param>
        /// <param name="bond">      The bond.
        /// </param>
        /// <returns>            true=is a potential configuration, false=is not.
        /// </returns>
        public static bool isValidDoubleBondConfiguration(IAtomContainer container, IBond bond)
        {
            IAtom[] atoms          = bond.getAtoms();
            IAtom[] connectedAtoms = container.getConnectedAtoms(atoms[0]);
            IAtom   from           = null;

            for (int i = 0; i < connectedAtoms.Length; i++)
            {
                if (connectedAtoms[i] != atoms[1])
                {
                    from = connectedAtoms[i];
                }
            }
            bool[] array = new bool[container.Bonds.Length];
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = true;
            }
            if (isStartOfDoubleBond(container, atoms[0], from, array) && isEndOfDoubleBond(container, atoms[1], atoms[0], array) && !bond.getFlag(CDKConstants.ISAROMATIC))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #3
0
 /// <summary> 
 /// Tells if a certain bond is center of a valid double bond configuration.
 /// </summary>
 /// <param name="container"> The atomcontainer.
 /// </param>
 /// <param name="bond">      The bond.
 /// </param>
 /// <returns>            true=is a potential configuration, false=is not.
 /// </returns>
 public static bool isValidDoubleBondConfiguration(IAtomContainer container, IBond bond)
 {
     IAtom[] atoms = bond.getAtoms();
     IAtom[] connectedAtoms = container.getConnectedAtoms(atoms[0]);
     IAtom from = null;
     for (int i = 0; i < connectedAtoms.Length; i++)
     {
         if (connectedAtoms[i] != atoms[1])
         {
             from = connectedAtoms[i];
         }
     }
     bool[] array = new bool[container.Bonds.Length];
     for (int i = 0; i < array.Length; i++)
     {
         array[i] = true;
     }
     if (isStartOfDoubleBond(container, atoms[0], from, array) && isEndOfDoubleBond(container, atoms[1], atoms[0], array) && !bond.getFlag(CDKConstants.ISAROMATIC))
     {
         return (true);
     }
     else
     {
         return (false);
     }
 }
        /// <summary> Returns wether a bond is saturated. A bond is saturated if
        /// <b>both</b> Atoms in the bond are saturated.
        /// </summary>
        public virtual bool isSaturated(IBond bond, IAtomContainer atomContainer)
        {
            IAtom[] atoms       = bond.getAtoms();
            bool    isSaturated = true;

            for (int i = 0; i < atoms.Length; i++)
            {
                isSaturated = isSaturated && this.isSaturated(atoms[i], atomContainer);
            }
            return(isSaturated);
        }
        /// <summary> Returns wether a bond is unsaturated. A bond is unsaturated if
        /// <b>all</b> Atoms in the bond are unsaturated.
        /// </summary>
        public virtual bool isUnsaturated(IBond bond, IAtomContainer atomContainer)
        {
            //logger.debug("isBondUnsaturated?: ", bond);
            IAtom[] atoms         = bond.getAtoms();
            bool    isUnsaturated = true;

            for (int i = 0; i < atoms.Length && isUnsaturated; i++)
            {
                isUnsaturated = isUnsaturated && !isSaturated(atoms[i], atomContainer);
            }
            //logger.debug("Bond is unsaturated?: ", isUnsaturated);
            return(isUnsaturated);
        }
        /// <summary> Returns wether a bond is saturated. A bond is saturated if
        /// <b>both</b> Atoms in the bond are saturated.
        /// </summary>
        public virtual bool isSaturated(IBond bond, IAtomContainer atomContainer)
        {
            //logger.debug("isBondSaturated?: ", bond);
            IAtom[] atoms       = bond.getAtoms();
            bool    isSaturated = true;

            for (int i = 0; i < atoms.Length; i++)
            {
                //logger.debug("isSaturated(Bond, AC): atom I=", i);
                isSaturated = isSaturated && this.isSaturated(atoms[i], atomContainer);
            }
            //logger.debug("isSaturated(Bond, AC): result=", isSaturated);
            return(isSaturated);
        }
Exemple #7
0
        /// <summary> Saturate atom by adjusting its bond orders.</summary>
        public virtual bool saturate(IBond bond, IAtomContainer atomContainer)
        {
            IAtom[] atoms   = bond.getAtoms();
            IAtom   atom    = atoms[0];
            IAtom   partner = atoms[1];
            //logger.debug("  saturating bond: ", atom.Symbol, "-", partner.Symbol);
            bool bondOrderIncreased = true;

            while (bondOrderIncreased && isUnsaturated(bond, atomContainer))
            {
                //logger.debug("Can increase bond order");
                bondOrderIncreased = saturateByIncreasingBondOrder(bond, atomContainer, 1.0);
            }
            return(isSaturated(bond, atomContainer));
        }
Exemple #8
0
        /// <summary>
        /// Creates a molecule graph for use with jgrapht.
        /// Bond orders are not respected.
        /// </summary>
        /// <param name="molecule">the specified molecule</param>
        /// <returns>a graph representing the molecule</returns>
        static public SimpleGraph getMoleculeGraph(IAtomContainer molecule)
        {
            SimpleGraph graph = new SimpleGraph();

            for (int i = 0; i < molecule.AtomCount; i++)
            {
                IAtom atom = molecule.Atoms[i];
                graph.addVertex(atom);
            }

            for (int i = 0; i < molecule.getBondCount(); i++)
            {
                IBond bond = molecule.Bonds[i];

                /*
                 * int order = (int) bond.getOrder();
                 * for (int j=0; j<order; j++) {
                 *      graph.addEdge(bond.getAtoms()[0], bond.getAtoms()[1]);
                 * }
                 */
                graph.addEdge(bond.getAtoms()[0], bond.getAtoms()[1]);
            }
            return(graph);
        }
        /// <summary> Saturate atom by adjusting its bond orders.</summary>
        public virtual bool newSaturate(IBond bond, IAtomContainer atomContainer)
        {
            IAtom[] atoms   = bond.getAtoms();
            IAtom   atom    = atoms[0];
            IAtom   partner = atoms[1];

            //logger.debug("  saturating bond: ", atom.Symbol, "-", partner.Symbol);
            IAtomType[] atomTypes1         = getAtomTypeFactory(bond.Builder).getAtomTypes(atom.Symbol);
            IAtomType[] atomTypes2         = getAtomTypeFactory(bond.Builder).getAtomTypes(partner.Symbol);
            bool        bondOrderIncreased = true;

            while (bondOrderIncreased && !isSaturated(bond, atomContainer))
            {
                //logger.debug("Can increase bond order");
                bondOrderIncreased = false;
                for (int atCounter1 = 0; atCounter1 < atomTypes1.Length && !bondOrderIncreased; atCounter1++)
                {
                    IAtomType aType1 = atomTypes1[atCounter1];
                    //logger.debug("  condidering atom type: ", aType1);
                    if (couldMatchAtomType(atomContainer, atom, aType1))
                    {
                        //logger.debug("  trying atom type: ", aType1);
                        for (int atCounter2 = 0; atCounter2 < atomTypes2.Length && !bondOrderIncreased; atCounter2++)
                        {
                            IAtomType aType2 = atomTypes2[atCounter2];
                            //logger.debug("  condidering partner type: ", aType1);
                            if (couldMatchAtomType(atomContainer, partner, atomTypes2[atCounter2]))
                            {
                                //logger.debug("    with atom type: ", aType2);
                                if (bond.Order >= aType2.MaxBondOrder || bond.Order >= aType1.MaxBondOrder)
                                {
                                    //logger.debug("Bond order not increased: atoms has reached (or exceeded) maximum bond order for this atom type");
                                }
                                else if (bond.Order < aType2.MaxBondOrder && bond.Order < aType1.MaxBondOrder)
                                {
                                    bond.Order = bond.Order + 1;
                                    //logger.debug("Bond order now " + bond.Order);
                                    bondOrderIncreased = true;
                                }
                            }
                        }
                    }
                }
            }
            return(isSaturated(bond, atomContainer));
        }
 /// <summary>  Determines if this Bond contains 2D coordinates.
 /// See comment for center(IAtomContainer atomCon, Dimension areaDim, HashMap renderingCoordinates) for details on coordinate sets
 /// 
 /// </summary>
 /// <param name="b"> Description of the Parameter
 /// </param>
 /// <returns>    boolean indication that 2D coordinates are available
 /// </returns>
 public static bool has2DCoordinates(IBond b)
 {
     IAtom[] atoms = b.getAtoms();
     for (int i = 0; i < atoms.Length; i++)
     {
         if (atoms[i].getPoint2d() == null)
         {
             return false;
         }
     }
     return true;
 }
        public static void GenericBondSetup(IBond bond, bool sort, BondShadingDesc bShading,
                                            out Vector3 direction, out Vector3 directionUV,
                                            out IAtom[] atoms, out Vector3[] atomsPos,
                                            out IMoleculeMaterial matA, out IMoleculeMaterial matB)
        {
            atoms = bond.getAtoms();

            Vector3 v1 = new Vector3((float)atoms[0].X3d, (float)atoms[0].Y3d, (float)atoms[0].Z3d);
            Vector3 v2 = new Vector3((float)atoms[1].X3d, (float)atoms[1].Y3d, (float)atoms[1].Z3d);

            direction = v2 - v1;

            atomsPos = new Vector3[2];
            if (sort)
            {
                if (direction.Z < (v1 - v2).Z)
                {
                    IAtom temp = atoms[0];
                    atoms[0] = atoms[1];
                    atoms[1] = temp;

                    atomsPos[0] = v2;
                    atomsPos[1] = v1;
                }
                else
                {
                    atomsPos[0] = v1;
                    atomsPos[1] = v2;
                }

                direction = Vector3.Normalize(atomsPos[1] - atomsPos[0]);
            }
            else
            {
                atomsPos[0] = v1;
                atomsPos[1] = v2;
            }

            directionUV = Vector3.Normalize(direction);

            IMoleculeMaterialLookup   lookup  = bShading.MoleculeMaterials;
            IMoleculeMaterialTemplate matTemp = lookup.ResolveBySymbol(atoms[0].Symbol);

            if (matTemp != null)
            {
                matA = matTemp.BySymbol;
            }
            else
            {
                PeriodicTableElement pe = (PeriodicTableElement)atoms[0].Properties["PeriodicTableElement"];
                matA = lookup.GetBySeries(pe.ChemicalSerie);
            }

            matTemp = lookup.ResolveBySymbol(atoms[1].Symbol);
            if (matTemp != null)
            {
                matB = matTemp.BySymbol;
            }
            else
            {
                PeriodicTableElement pe = (PeriodicTableElement)atoms[1].Properties["PeriodicTableElement"];
                matB = lookup.GetBySeries(pe.ChemicalSerie);
            }
        }
        public static void GenericBondSetup(IBond bond, bool sort, BondShadingDesc bShading,
                                            out Vector3 direction, out Vector3 directionUV,
                                            out IAtom[] atoms, out Vector3[] atomsPos,
                                            out IMoleculeMaterial matA, out IMoleculeMaterial matB)
        {
            atoms = bond.getAtoms();

            Vector3 v1 = new Vector3((float)atoms[0].X3d, (float)atoms[0].Y3d, (float)atoms[0].Z3d);
            Vector3 v2 = new Vector3((float)atoms[1].X3d, (float)atoms[1].Y3d, (float)atoms[1].Z3d);
            direction = v2 - v1;

            atomsPos = new Vector3[2];
            if (sort)
            {
                if (direction.Z < (v1 - v2).Z)
                {
                    IAtom temp = atoms[0];
                    atoms[0] = atoms[1];
                    atoms[1] = temp;

                    atomsPos[0] = v2;
                    atomsPos[1] = v1;
                }
                else
                {
                    atomsPos[0] = v1;
                    atomsPos[1] = v2;
                }

                direction = Vector3.Normalize(atomsPos[1] - atomsPos[0]);
            }
            else
            {
                atomsPos[0] = v1;
                atomsPos[1] = v2;
            }

            directionUV = Vector3.Normalize(direction);

            IMoleculeMaterialLookup lookup = bShading.MoleculeMaterials;
            IMoleculeMaterialTemplate matTemp = lookup.ResolveBySymbol(atoms[0].Symbol);
            if (matTemp != null)
                matA = matTemp.BySymbol;
            else
            {
                PeriodicTableElement pe = (PeriodicTableElement)atoms[0].Properties["PeriodicTableElement"];
                matA = lookup.GetBySeries(pe.ChemicalSerie);
            }

            matTemp = lookup.ResolveBySymbol(atoms[1].Symbol);
            if (matTemp != null)
                matB = matTemp.BySymbol;
            else
            {
                PeriodicTableElement pe = (PeriodicTableElement)atoms[1].Properties["PeriodicTableElement"];
                matB = lookup.GetBySeries(pe.ChemicalSerie);
            }
        }
Exemple #13
0
        /// <summary> Produces an AtomContainer without explicit Hs but with H count from one with Hs.
        /// The new molecule is a deep copy.
        ///
        /// </summary>
        /// <param name="atomContainer">The AtomContainer from which to remove the hydrogens
        /// </param>
        /// <returns>              The molecule without Hs.
        /// </returns>
        /// <cdk.keyword>          hydrogen, removal </cdk.keyword>
        public static IAtomContainer removeHydrogens(IAtomContainer atomContainer)
        {
            //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'"
            System.Collections.IDictionary map    = new System.Collections.Hashtable(); // maps original atoms to clones.
            System.Collections.IList       remove = new System.Collections.ArrayList(); // lists removed Hs.

            // Clone atoms except those to be removed.
            IMolecule mol   = atomContainer.Builder.newMolecule();
            int       count = atomContainer.AtomCount;

            for (int i = 0; i < count; i++)
            {
                // Clone/remove this atom?
                IAtom atom = atomContainer.getAtomAt(i);
                if (!atom.Symbol.Equals("H"))
                {
                    IAtom clonedAtom = null;
                    try
                    {
                        clonedAtom = (IAtom)atom.Clone();
                    }
                    //UPGRADE_NOTE: Exception 'java.lang.CloneNotSupportedException' was converted to 'System.Exception' which has different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1100'"
                    catch (System.Exception e)
                    {
                        // TODO Auto-generated catch block
                        SupportClass.WriteStackTrace(e, Console.Error);
                    }
                    clonedAtom.setHydrogenCount(0);
                    mol.addAtom(clonedAtom);
                    map[atom] = clonedAtom;
                }
                else
                {
                    remove.Add(atom); // maintain list of removed H.
                }
            }

            // Clone bonds except those involving removed atoms.
            count = atomContainer.getBondCount();
            for (int i = 0; i < count; i++)
            {
                // Check bond.
                //UPGRADE_NOTE: Final was removed from the declaration of 'bond '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
                IBond   bond        = atomContainer.getBondAt(i);
                IAtom[] atoms       = bond.getAtoms();
                bool    removedBond = false;
                //UPGRADE_NOTE: Final was removed from the declaration of 'length '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
                int length = atoms.Length;
                for (int k = 0; k < length; k++)
                {
                    if (remove.Contains(atoms[k]))
                    {
                        removedBond = true;
                        break;
                    }
                }

                // Clone/remove this bond?
                if (!removedBond)
                // if (!remove.contains(atoms[0]) && !remove.contains(atoms[1]))
                {
                    IBond clone = null;
                    try
                    {
                        clone = (IBond)atomContainer.getBondAt(i).Clone();
                    }
                    //UPGRADE_NOTE: Exception 'java.lang.CloneNotSupportedException' was converted to 'System.Exception' which has different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1100'"
                    catch (System.Exception e)
                    {
                        // TODO Auto-generated catch block
                        SupportClass.WriteStackTrace(e, Console.Error);
                    }
                    clone.setAtoms(new IAtom[] { (IAtom)map[atoms[0]], (IAtom)map[atoms[1]] });
                    mol.addBond(clone);
                }
            }

            // Recompute hydrogen counts of neighbours of removed Hydrogens.
            //UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratorhasNext'"
            for (System.Collections.IEnumerator i = remove.GetEnumerator(); i.MoveNext();)
            {
                // Process neighbours.
                //UPGRADE_TODO: Method 'java.util.Iterator.next' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratornext'"
                //UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratorhasNext'"
                for (System.Collections.IEnumerator n = atomContainer.getConnectedAtomsVector((IAtom)i.Current).GetEnumerator(); n.MoveNext();)
                {
                    //UPGRADE_NOTE: Final was removed from the declaration of 'neighb '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
                    //UPGRADE_TODO: Method 'java.util.Iterator.next' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratornext'"
                    IAtom neighb = (IAtom)map[n.Current];
                    neighb.setHydrogenCount(neighb.getHydrogenCount() + 1);
                }
            }
            mol.Properties = atomContainer.Properties;
            mol.Flags      = atomContainer.Flags;

            return(mol);
        }
		/// <summary> Saturate atom by adjusting its bond orders.</summary>
		public virtual bool saturate(IBond bond, IAtomContainer atomContainer)
		{
			IAtom[] atoms = bond.getAtoms();
			IAtom atom = atoms[0];
			IAtom partner = atoms[1];
			//logger.debug("  saturating bond: ", atom.Symbol, "-", partner.Symbol);
			bool bondOrderIncreased = true;
			while (bondOrderIncreased && isUnsaturated(bond, atomContainer))
			{
				//logger.debug("Can increase bond order");
				bondOrderIncreased = saturateByIncreasingBondOrder(bond, atomContainer, 1.0);
			}
			return isSaturated(bond, atomContainer);
		}
Exemple #15
0
        /// <summary> Writes a Molecule to an OutputStream in MDL sdf format.
        ///
        /// </summary>
        /// <param name="container"> Molecule that is written to an OutputStream
        /// </param>
        /// <param name="isVisible">Should a certain atom be written to mdl?
        /// </param>
        public virtual void writeMolecule(IMolecule container, bool[] isVisible)
        {
            System.String line = "";
            // taking care of the $$$$ signs:
            // we do not write such a sign at the end of the first molecule, thus we have to write on BEFORE the second molecule
            if (moleculeNumber == 2)
            {
                writer.Write("$$$$");
                //UPGRADE_TODO: Method 'java.io.BufferedWriter.newLine' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
                writer.WriteLine();
            }
            // write header block
            // lines get shortened to 80 chars, that's in the spec
            System.String title = (System.String)container.getProperty(CDKConstants.TITLE);
            if (title == null)
            {
                title = "";
            }
            if (title.Length > 80)
            {
                title = title.Substring(0, (80) - (0));
            }
            writer.Write(title + "\n");

            /* From CTX spec
             * This line has the format:
             * IIPPPPPPPPMMDDYYHHmmddSSssssssssssEEEEEEEEEEEERRRRRR
             * (FORTRAN: A2<--A8--><---A10-->A2I2<--F10.5-><---F12.5--><-I6-> )
             * User's first and last initials (l), program name (P),
             * date/time (M/D/Y,H:m), dimensional codes (d), scaling factors (S, s),
             * energy (E) if modeling program input, internal registry number (R)
             * if input through MDL form.
             * A blank line can be substituted for line 2.
             */
            writer.Write("  CDK    ");
            //UPGRADE_ISSUE: Constructor 'java.text.SimpleDateFormat.SimpleDateFormat' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javatextSimpleDateFormat'"
            //UPGRADE_TODO: The equivalent in .NET for method 'java.util.Calendar.getTime' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
            System.TimeZone generatedAux3 = System.TimeZone.CurrentTimeZone;
            //writer.Write(SupportClass.FormatDateTime(new SimpleDateFormat("M/d/y,H:m", new System.Globalization.CultureInfo("en-US")), SupportClass.CalendarManager.manager.GetDateTime(new System.Globalization.GregorianCalendar())));
            writer.Write('\n');

            System.String comment = (System.String)container.getProperty(CDKConstants.REMARK);
            if (comment == null)
            {
                comment = "";
            }
            if (comment.Length > 80)
            {
                comment = comment.Substring(0, (80) - (0));
            }
            writer.Write(comment + "\n");

            // write Counts line
            int upToWhichAtom = 0;

            for (int i = 0; i < isVisible.Length; i++)
            {
                if (isVisible[i])
                {
                    upToWhichAtom++;
                }
            }
            line += formatMDLInt(upToWhichAtom, 3);
            int numberOfBonds = 0;

            if (upToWhichAtom < container.AtomCount)
            {
                for (int i = 0; i < container.getBondCount(); i++)
                {
                    if (isVisible[container.getAtomNumber(container.getBondAt(i).getAtoms()[0])] && isVisible[container.getAtomNumber(container.getBondAt(i).getAtoms()[1])])
                    {
                        numberOfBonds++;
                    }
                }
            }
            else
            {
                numberOfBonds = container.getBondCount();
            }
            line += formatMDLInt(numberOfBonds, 3);
            line += "  0  0  0  0  0  0  0  0999 V2000\n";
            writer.Write(line);

            // write Atom block
            IAtom[] atoms = container.Atoms;
            for (int f = 0; f < atoms.Length; f++)
            {
                if (isVisible[f])
                {
                    IAtom atom = atoms[f];
                    line = "";
                    if (atom.getPoint3d() != null)
                    {
                        //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
                        line += formatMDLFloat((float)atom.X3d);
                        //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
                        line += formatMDLFloat((float)atom.Y3d);
                        //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
                        line += (formatMDLFloat((float)atom.Z3d) + " ");
                    }
                    else if (atom.getPoint2d() != null)
                    {
                        //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
                        line += formatMDLFloat((float)atom.X2d);
                        //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
                        line += formatMDLFloat((float)atom.Y2d);
                        line += "    0.0000 ";
                    }
                    else
                    {
                        // if no coordinates available, then output a number
                        // of zeros
                        line += formatMDLFloat((float)0.0);
                        line += formatMDLFloat((float)0.0);
                        line += (formatMDLFloat((float)0.0) + " ");
                    }
                    if (container.getAtomAt(f) is IPseudoAtom)
                    {
                        line += formatMDLString(((IPseudoAtom)container.getAtomAt(f)).Label, 3);
                    }
                    else
                    {
                        line += formatMDLString(container.getAtomAt(f).Symbol, 3);
                    }
                    line += " 0  0  0  0  0  0  0  0  0  0  0  0";
                    writer.Write(line);
                    //UPGRADE_TODO: Method 'java.io.BufferedWriter.newLine' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
                    writer.WriteLine();
                }
            }

            // write Bond block
            IBond[] bonds = container.Bonds;
            for (int g = 0; g < bonds.Length; g++)
            {
                if (upToWhichAtom == container.AtomCount || (isVisible[container.getAtomNumber(container.getBondAt(g).getAtoms()[0])] && isVisible[container.getAtomNumber(container.getBondAt(g).getAtoms()[1])]))
                {
                    IBond bond = bonds[g];
                    if (bond.getAtoms().Length != 2)
                    {
                        //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.warn("Skipping bond with more/less than two atoms: " + bond);
                    }
                    else
                    {
                        if (bond.Stereo == CDKConstants.STEREO_BOND_UP_INV || bond.Stereo == CDKConstants.STEREO_BOND_DOWN_INV)
                        {
                            // turn around atom coding to correct for inv stereo
                            line  = formatMDLInt(container.getAtomNumber(bond.getAtomAt(1)) + 1, 3);
                            line += formatMDLInt(container.getAtomNumber(bond.getAtomAt(0)) + 1, 3);
                        }
                        else
                        {
                            line  = formatMDLInt(container.getAtomNumber(bond.getAtomAt(0)) + 1, 3);
                            line += formatMDLInt(container.getAtomNumber(bond.getAtomAt(1)) + 1, 3);
                        }
                        //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
                        line += formatMDLInt((int)bond.Order, 3);
                        line += "  ";
                        switch (bond.Stereo)
                        {
                        case CDKConstants.STEREO_BOND_UP:
                            line += "1";
                            break;

                        case CDKConstants.STEREO_BOND_UP_INV:
                            line += "1";
                            break;

                        case CDKConstants.STEREO_BOND_DOWN:
                            line += "6";
                            break;

                        case CDKConstants.STEREO_BOND_DOWN_INV:
                            line += "6";
                            break;

                        default:
                            line += "0";
                            break;
                        }
                        line += "  0  0  0 ";
                        writer.Write(line);
                        //UPGRADE_TODO: Method 'java.io.BufferedWriter.newLine' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
                        writer.WriteLine();
                    }
                }
            }

            // write formal atomic charges
            for (int i = 0; i < atoms.Length; i++)
            {
                IAtom atom   = atoms[i];
                int   charge = atom.getFormalCharge();
                if (charge != 0)
                {
                    writer.Write("M  CHG  1 ");
                    writer.Write(formatMDLInt(i + 1, 3));
                    writer.Write(" ");
                    writer.Write(formatMDLInt(charge, 3));
                    //UPGRADE_TODO: Method 'java.io.BufferedWriter.newLine' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
                    writer.WriteLine();
                }
            }

            // write formal isotope information
            for (int i = 0; i < atoms.Length; i++)
            {
                IAtom atom = atoms[i];
                if (!(atom is IPseudoAtom))
                {
                    int atomicMass = atom.MassNumber;
                    int majorMass  = IsotopeFactory.getInstance(atom.Builder).getMajorIsotope(atom.Symbol).MassNumber;
                    if (atomicMass != 0 && atomicMass != majorMass)
                    {
                        writer.Write("M  ISO  1 ");
                        writer.Write(formatMDLInt(i + 1, 3));
                        writer.Write(" ");
                        writer.Write(formatMDLInt(atomicMass, 3));
                        //UPGRADE_TODO: Method 'java.io.BufferedWriter.newLine' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
                        writer.WriteLine();
                    }
                }
            }

            // close molecule
            writer.Write("M  END");
            //UPGRADE_TODO: Method 'java.io.BufferedWriter.newLine' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
            writer.WriteLine();
            //write sdfields, if any
            if (sdFields != null)
            {
                //UPGRADE_TODO: Method 'java.util.Map.keySet' was converted to 'CSGraphT.SupportClass.HashSetSupport' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilMapkeySet'"
                CSGraphT.SupportClass.SetSupport set_Renamed = new CSGraphT.SupportClass.HashSetSupport(sdFields.Keys);
                System.Collections.IEnumerator   iterator    = set_Renamed.GetEnumerator();
                //UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratorhasNext'"
                while (iterator.MoveNext())
                {
                    //UPGRADE_TODO: Method 'java.util.Iterator.next' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratornext'"
                    System.Object element = iterator.Current;
                    writer.Write("> <" + ((System.String)element) + ">");
                    //UPGRADE_TODO: Method 'java.io.BufferedWriter.newLine' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
                    writer.WriteLine();
                    //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'"
                    writer.Write(sdFields[element].ToString());
                    //UPGRADE_TODO: Method 'java.io.BufferedWriter.newLine' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
                    writer.WriteLine();
                    //UPGRADE_TODO: Method 'java.io.BufferedWriter.newLine' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
                    writer.WriteLine();
                }
            }
            // taking care of the $$$$ signs:
            // we write such a sign at the end of all except the first molecule
            if (moleculeNumber != 1)
            {
                writer.Write("$$$$");
                //UPGRADE_TODO: Method 'java.io.BufferedWriter.newLine' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'"
                writer.WriteLine();
            }
            moleculeNumber++;
            writer.Flush();
        }
		/// <summary> Returns wether a bond is saturated. A bond is saturated if 
		/// <b>both</b> Atoms in the bond are saturated.
		/// </summary>
		public virtual bool isSaturated(IBond bond, IAtomContainer atomContainer)
		{
			//logger.debug("isBondSaturated?: ", bond);
			IAtom[] atoms = bond.getAtoms();
			bool isSaturated = true;
			for (int i = 0; i < atoms.Length; i++)
			{
				//logger.debug("isSaturated(Bond, AC): atom I=", i);
				isSaturated = isSaturated && this.isSaturated(atoms[i], atomContainer);
			}
			//logger.debug("isSaturated(Bond, AC): result=", isSaturated);
			return isSaturated;
		}
		/// <summary> Tries to saturate a bond by increasing its bond orders by 1.0.
		/// 
		/// </summary>
		/// <returns> true if the bond could be increased
		/// </returns>
		public virtual bool saturateByIncreasingBondOrder(IBond bond, IAtomContainer atomContainer, double increment)
		{
			IAtom[] atoms = bond.getAtoms();
			IAtom atom = atoms[0];
			IAtom partner = atoms[1];
			//logger.debug("  saturating bond: ", atom.Symbol, "-", partner.Symbol);
			IAtomType[] atomTypes1 = getAtomTypeFactory(bond.Builder).getAtomTypes(atom.Symbol);
			IAtomType[] atomTypes2 = getAtomTypeFactory(bond.Builder).getAtomTypes(partner.Symbol);
			for (int atCounter1 = 0; atCounter1 < atomTypes1.Length; atCounter1++)
			{
				IAtomType aType1 = atomTypes1[atCounter1];
				//logger.debug("  condidering atom type: ", aType1);
				if (couldMatchAtomType(atomContainer, atom, aType1))
				{
					//logger.debug("  trying atom type: ", aType1);
					for (int atCounter2 = 0; atCounter2 < atomTypes2.Length; atCounter2++)
					{
						IAtomType aType2 = atomTypes2[atCounter2];
						//logger.debug("  condidering partner type: ", aType1);
						if (couldMatchAtomType(atomContainer, partner, atomTypes2[atCounter2]))
						{
							//logger.debug("    with atom type: ", aType2);
							if (bond.Order < aType2.MaxBondOrder && bond.Order < aType1.MaxBondOrder)
							{
								bond.Order = bond.Order + increment;
								//logger.debug("Bond order now ", bond.Order);
								return true;
							}
						}
					}
				}
			}
			return false;
		}
		/// <summary> Returns wether a bond is unsaturated. A bond is unsaturated if 
		/// <b>all</b> Atoms in the bond are unsaturated.
		/// </summary>
		public virtual bool isUnsaturated(IBond bond, IAtomContainer atomContainer)
		{
			//logger.debug("isBondUnsaturated?: ", bond);
			IAtom[] atoms = bond.getAtoms();
			bool isUnsaturated = true;
			for (int i = 0; i < atoms.Length && isUnsaturated; i++)
			{
				isUnsaturated = isUnsaturated && !isSaturated(atoms[i], atomContainer);
			}
			//logger.debug("Bond is unsaturated?: ", isUnsaturated);
			return isUnsaturated;
		}
Exemple #19
0
 /// <summary>  This makes a map of matching atoms out of a map of matching bonds as produced by the get(Subgraph|Ismorphism)Map methods.
 ///
 /// </summary>
 /// <param name="l">  The list produced by the getMap method.
 /// </param>
 /// <param name="g1"> The first atom container.
 /// </param>
 /// <param name="g2"> The second one (first and second as in getMap)
 /// </param>
 /// <returns>     The mapping found projected on g1. This is a List of RMap objects containing Ids of matching atoms.
 /// </returns>
 public static System.Collections.IList makeAtomsMapOfBondsMap(System.Collections.IList l, IAtomContainer g1, IAtomContainer g2)
 {
     if (l == null)
     {
         return(l);
     }
     IBond[] bonds1 = g1.Bonds;
     IBond[] bonds2 = g2.Bonds;
     System.Collections.IList result = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
     for (int i = 0; i < l.Count; i++)
     {
         IBond   bond1 = bonds1[((RMap)l[i]).Id1];
         IBond   bond2 = bonds2[((RMap)l[i]).Id2];
         IAtom[] atom1 = bond1.getAtoms();
         IAtom[] atom2 = bond2.getAtoms();
         for (int j = 0; j < 2; j++)
         {
             IBond[] bondsConnectedToAtom1j = g1.getConnectedBonds(atom1[j]);
             for (int k = 0; k < bondsConnectedToAtom1j.Length; k++)
             {
                 if (bondsConnectedToAtom1j[k] != bond1)
                 {
                     IBond testBond = bondsConnectedToAtom1j[k];
                     for (int m = 0; m < l.Count; m++)
                     {
                         IBond testBond2;
                         if (((RMap)l[m]).Id1 == g1.getBondNumber(testBond))
                         {
                             testBond2 = bonds2[((RMap)l[m]).Id2];
                             for (int n = 0; n < 2; n++)
                             {
                                 System.Collections.IList bondsToTest = g2.getConnectedBondsVector(atom2[n]);
                                 if (bondsToTest.Contains(testBond2))
                                 {
                                     RMap map;
                                     if (j == n)
                                     {
                                         map = new RMap(g1.getAtomNumber(atom1[0]), g2.getAtomNumber(atom2[0]));
                                     }
                                     else
                                     {
                                         map = new RMap(g1.getAtomNumber(atom1[1]), g2.getAtomNumber(atom2[0]));
                                     }
                                     if (!result.Contains(map))
                                     {
                                         result.Add(map);
                                     }
                                     RMap map2;
                                     if (j == n)
                                     {
                                         map2 = new RMap(g1.getAtomNumber(atom1[1]), g2.getAtomNumber(atom2[1]));
                                     }
                                     else
                                     {
                                         map2 = new RMap(g1.getAtomNumber(atom1[0]), g2.getAtomNumber(atom2[1]));
                                     }
                                     if (!result.Contains(map2))
                                     {
                                         result.Add(map2);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return(result);
 }
		/// <summary> Saturate atom by adjusting its bond orders.</summary>
		public virtual bool newSaturate(IBond bond, IAtomContainer atomContainer)
		{
			IAtom[] atoms = bond.getAtoms();
			IAtom atom = atoms[0];
			IAtom partner = atoms[1];
			//logger.debug("  saturating bond: ", atom.Symbol, "-", partner.Symbol);
			IAtomType[] atomTypes1 = getAtomTypeFactory(bond.Builder).getAtomTypes(atom.Symbol);
			IAtomType[] atomTypes2 = getAtomTypeFactory(bond.Builder).getAtomTypes(partner.Symbol);
			bool bondOrderIncreased = true;
			while (bondOrderIncreased && !isSaturated(bond, atomContainer))
			{
				//logger.debug("Can increase bond order");
				bondOrderIncreased = false;
				for (int atCounter1 = 0; atCounter1 < atomTypes1.Length && !bondOrderIncreased; atCounter1++)
				{
					IAtomType aType1 = atomTypes1[atCounter1];
					//logger.debug("  condidering atom type: ", aType1);
					if (couldMatchAtomType(atomContainer, atom, aType1))
					{
						//logger.debug("  trying atom type: ", aType1);
						for (int atCounter2 = 0; atCounter2 < atomTypes2.Length && !bondOrderIncreased; atCounter2++)
						{
							IAtomType aType2 = atomTypes2[atCounter2];
							//logger.debug("  condidering partner type: ", aType1);
							if (couldMatchAtomType(atomContainer, partner, atomTypes2[atCounter2]))
							{
								//logger.debug("    with atom type: ", aType2);
								if (bond.Order >= aType2.MaxBondOrder || bond.Order >= aType1.MaxBondOrder)
								{
									//logger.debug("Bond order not increased: atoms has reached (or exceeded) maximum bond order for this atom type");
								}
								else if (bond.Order < aType2.MaxBondOrder && bond.Order < aType1.MaxBondOrder)
								{
									bond.Order = bond.Order + 1;
									//logger.debug("Bond order now " + bond.Order);
									bondOrderIncreased = true;
								}
							}
						}
					}
				}
			}
			return isSaturated(bond, atomContainer);
		}
		/// <summary> Returns wether a bond is saturated. A bond is saturated if 
		/// <b>both</b> Atoms in the bond are saturated.
		/// </summary>
		public virtual bool isSaturated(IBond bond, IAtomContainer atomContainer)
		{
			IAtom[] atoms = bond.getAtoms();
			bool isSaturated = true;
			for (int i = 0; i < atoms.Length; i++)
			{
				isSaturated = isSaturated && this.isSaturated(atoms[i], atomContainer);
			}
			return isSaturated;
		}