Example #1
0
        /// <summary>
        /// MolfileV3000ToAtomContainer
        /// </summary>
        /// <param name="molfile"></param>
        /// <returns></returns>
        public static IAtomContainer MolfileV3000ToAtomContainer(string molfile)
        {
            // Extract any mass info before conversion to avoid losing our custom info in conversion
            Dictionary <int, int> map = new Dictionary <int, int>();

            if (molfile.Contains(" MASS="))
            {
                map = ExtractMassAttributes(ref molfile);
            }

            cdk.io.DefaultChemObjectReader cor;
            java.io.StringReader           sr = new java.io.StringReader(molfile);
            cor = new MDLV3000Reader(sr);
            cor.setReaderMode(IChemObjectReader.Mode.RELAXED);

            IAtomContainer mol = (IAtomContainer)cor.read(new AtomContainer());

            cor.close();

            for (int ai = 0; ai < mol.getAtomCount(); ai++)
            {
                IAtom a = mol.getAtom(ai);
                if (map.ContainsKey(ai + 1))
                {
                    a.setMassNumber(new java.lang.Integer(map[ai + 1]));
                }
                else
                {
                    a.setMassNumber(null);
                }
            }

            ConfigureAtomContainer(mol);
            return(mol);
        }
Example #2
0
        /// <summary>
        /// Set integer MassNumber (isotope value) for an atom
        /// </summary>
        /// <param name="atom"></param>
        /// <param name="mass"></param>

        public static void SetMassNumber(
            IAtom atom,
            int mass)
        {
            atom.setMassNumber(new java.lang.Integer(mass));
            return;
        }
Example #3
0
        /// <summary>
        /// Convert isotope labels to hilighting.
        /// </summary>
        /// <param name="mol"></param>
        /// <returns></returns>

        public string ConvertIsotopeValuesToHilighting(
            IAtomContainer mol)
        {
            bool   hilight;
            string molfile2 = "";
            bool   clearAttachmentPointLabels = true;
            bool   hilightAttachmentBond      = true;

            List <int> atomSet = new List <int>();
            List <int> bondSet = new List <int>();

            for (int bi = 0; bi < mol.getBondCount(); bi++)
            {
                IBond b = mol.getBond(bi);
                if (b.getAtomCount() != 2)
                {
                    continue;
                }

                IAtom a1 = b.getAtom(0);
                IAtom a2 = b.getAtom(1);

                if (hilightAttachmentBond)
                {
                    hilight = (GetMassNumber(a1) != 0 && GetMassNumber(a2) != 0);
                }
                else
                {
                    hilight = (GetMassNumber(a1) < 0 && GetMassNumber(a2) < 0);
                }
                if (hilight)
                {
                    bondSet.Add(bi);
                }
            }

            //String posMap = ""; // debug (note that hydrogens can get repositioned)
            for (int ai = 0; ai < mol.getAtomCount(); ai++)             // scan atoms for hilighting and reset isotope values
            {
                IAtom a = mol.getAtom(ai);

                hilight = GetMassNumber(a) < 0;
                if (hilight)
                {
                    atomSet.Add(ai);
                }

                //posMap += ai.ToString() + ", " + a.getSymbol() + ", " + hilight + "\r\n"; // debug

                int massNo = GetMassNumber(a);
                if (massNo == hilightIsotopeValue)
                {
                    a.setMassNumber(null);                     // set back to original value
                }
                else if (massNo < 0)
                {
                    if (clearAttachmentPointLabels)
                    {
                        a.setMassNumber(null);
                    }

                    else
                    {
                        SetMassNumber(a, -massNo);                      // set back to positive
                    }
                }
            }

            //mol = GenerateCoordinates(mol); // (should already be done)

            try { molfile2 = AtomContainerToMolFileV3000(mol); }
            catch (Exception ex) { ex = ex; }

            if (Lex.IsUndefined(molfile2))             // couldn't convert to v3000, just return unhilighted v2000 file
            {
                molfile2 = AtomContainerToMolfile(mol);
                return(molfile2);
            }

            string txt = "MDLV30/HILITE";

            if (atomSet.Count > 0)
            {
                txt += " " + BuildV3000KeywordList("ATOMS", atomSet);
            }

            if (bondSet.Count > 0)
            {
                txt += " " + BuildV3000KeywordList("BONDS", bondSet);
            }

            txt = BuildV3000Lines(txt);

            bool hasCollection = Lex.Contains(molfile2, "M  V30 END COLLECTION");

            if (hasCollection)             // already have collection begin and end
            {
                txt = txt +
                      "M  V30 END COLLECTION";
                molfile2 = Lex.Replace(molfile2, "M  V30 END COLLECTION", txt);
            }

            else             // add collection begin & end
            {
                txt =
                    "M  V30 BEGIN COLLECTION\n" +
                    txt +
                    "M  V30 END COLLECTION\n" +
                    "M  V30 END CTAB";
                molfile2 = Lex.Replace(molfile2, "M  V30 END CTAB", txt);
            }

            return(molfile2);
        }