Exemplo n.º 1
0
        /// <summary>
        /// Get molecule string in correct format
        /// </summary>
        /// <param name="vo"></param>
        /// <param name="structureExportFormat"></param>
        /// <returns></returns>

        string GetMoleculeString(
            object vo,
            ExportStructureFormat structureExportFormat)
        {
            string     molString = "";
            MoleculeMx cs        = (MoleculeMx)vo;

            if (structureExportFormat == ExportStructureFormat.Chime)
            {
                molString = cs.GetChimeString();                 // save structure as Chime
            }
            else if (structureExportFormat == ExportStructureFormat.Smiles)
            {
                molString = cs.GetSmilesString();                 // save structure as Smiles
            }
            else if (structureExportFormat == ExportStructureFormat.Molfile)
            {
                molString = cs.GetMolfileString(); // save structure as molfile
            }
            else                                   // save structure as molfile as default
            {
                molString = cs.GetMolfileString();
            }

            return(molString);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Perform a full structure search of query molecule against target molecule
        /// </summary>
        /// <param name="query"></param>
        /// <param name="target"></param>
        /// <returns></returns>

        public bool IsFSSMatch(
            MoleculeMx query,
            MoleculeMx target)
        {
            if (query.IsBiopolymerFormat || target.IsBiopolymerFormat)
            {
                if (!query.IsBiopolymerFormat || !target.IsBiopolymerFormat)
                {
                    return(false);
                }

                if (Lex.Eq(query.PrimaryValue, target.PrimaryValue))
                {
                    return(true);
                }

                else
                {
                    return(false);
                }
            }

            SetFSSQueryMolecule(query);
            bool b = IsFSSMatch(target);

            return(b);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Convert this molecule to a new type
        /// </summary>
        /// <param name="newType"></param>

        public void ConvertToNewType(
            MoleculeFormat newType)
        {
            if (PrimaryFormat == newType)
            {
                return;                                       // no change in type
            }
            else if (PrimaryFormat == MoleculeFormat.Molfile && newType == MoleculeFormat.Chime)
            {
                if (!Lex.IsNullOrEmpty(MolfileString))
                {
                    ChimeString = MolfileStringToChimeString(MolfileString);
                }
                PrimaryFormat = MoleculeFormat.Chime;
            }

            else if (PrimaryFormat == MoleculeFormat.Chime && newType == MoleculeFormat.Molfile)
            {
                if (!Lex.IsNullOrEmpty(ChimeString))
                {
                    MolfileString = ChimeStringToMolfileString(ChimeString);
                }
                PrimaryFormat = MoleculeFormat.Molfile;
            }

            else
            {
                MoleculeMx cs2 = ConvertTo(newType);
                cs2.MemberwiseCopy(this);
            }

            return;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Convert from one format to another
        /// </summary>
        /// <param name="newFormat"></param>
        /// <returns></returns>

        public MoleculeMx ConvertTo(
            MoleculeFormat molFormatType)
        {
            string molString, msg;
            bool   conversionFailed = false;

            MoleculeMx mol = new MoleculeMx(molFormatType);

            try
            {
                // If no change in type, just copy value

                if (this.PrimaryFormat == molFormatType)                 // any change in type?
                {
                    molString = this.PrimaryValue;
                }

                else
                {
                    molString = GetMoleculeString(molFormatType);                  // convert from old type if possible
                }
                mol.SetPrimaryTypeAndValue(molFormatType, molString);
                return(mol);
            }

            catch (Exception ex)             // handle structures that don't convert - issue #216
            {
                //DebugLog.Message("ChemicalStructure.Convert exception for structure: \r\n" + Value + "\r\n" +
                //  DebugLog.FormatExceptionMessage(ex));
                //throw new Exception(ex.Message, ex);
                return(new MoleculeMx(PrimaryFormat, ""));                // return blank structure
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Compare two molecules for full equality
        /// </summary>
        /// <param name="query"></param>
        /// <param name="target"></param>
        /// <returns></returns>

        public bool Equal(
            MoleculeMx query,
            MoleculeMx target)
        {
            string switches = "All";

            return(FullStructureMatch(query, target, switches));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Set substructure search query molecule
        /// </summary>
        /// <param name="cs"></param>

        public void SetSSSQueryMolecule(
            MoleculeMx cs)
        {
            QueryMol    = cs.Clone();
            SSSQueryMol = CdkMolFactory.NewCdkMol(cs);
            CdkMolUtil.SetSSSQueryMolecule(SSSQueryMol);

            return;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Do a SSS using the predefined query molecule and supplied target molecule
        /// </summary>
        /// <returns></returns>

        public bool IsSSSMatch(MoleculeMx cs)
        {
            if (QueryMol == null || cs.IsBiopolymerFormat)             // fix later
            {
                return(false);
            }

            TargetMol    = cs;
            SSSTargetMol = CdkMolFactory.NewCdkMol(cs);
            bool b = CdkMolUtil.IsSSSMatch(SSSTargetMol);

            return(b);
        }
Exemplo n.º 8
0
        public static string HelmToSvg(
            MoleculeMx mol)
        {
            string svg = mol.SvgString;

            if (mol.PrimaryFormat == MoleculeFormat.Helm && Lex.IsDefined(mol.SvgString))
            {
                return(svg);
            }

            string helm = mol.HelmString;

            svg = HelmControl.GetSvg(helm);
            return(svg);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Set full structure search query molecule
        /// </summary>
        /// <param name="cs"></param>

        public void SetFSSQueryMolecule(
            MoleculeMx cs)
        {
            if (cs.IsBiopolymerFormat)             // fix later
            {
                QueryMol = cs;
                return;
            }

            QueryMol = cs.Clone();

            FSSQueryMol = CdkMolFactory.NewCdkMol(cs);
            CdkMolUtil.SetFSSQueryMolecule(FSSQueryMol);

            return;
        }
Exemplo n.º 10
0
        /// <summary>
        /// Perform a substructure search
        /// </summary>
        /// <param name="query"></param>
        /// <param name="target"></param>
        /// <returns></returns>

        public bool IsSSSMatch(
            MoleculeMx query,
            MoleculeMx target)
        {
            if (query.IsBiopolymerFormat || target.IsBiopolymerFormat)             // fix later
            {
                return(false);
            }

            QueryMol     = query;
            SSSQueryMol  = CdkMolFactory.NewCdkMol(query);
            SSSTargetMol = CdkMolFactory.NewCdkMol(target);
            bool b = CdkMolUtil.IsSSSMatch(SSSQueryMol, SSSTargetMol);

            return(b);
        }
Exemplo n.º 11
0
        public static void SetMoleculeToTestHelmString(
            string cid,
            MoleculeMx mol)
        {
            if (!Lex.IsInteger(cid))
            {
                return;
            }
            int i1 = int.Parse(cid);

            if (i1 > 0 && i1 < 10)
            {
                string helm = GetTestHelmString(i1);
                mol.SetPrimaryTypeAndValue(MoleculeFormat.Helm, helm);
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Do a SSS using the predefined query molecule and supplied target molecule
        /// </summary>
        /// <returns></returns>

        public bool IsFSSMatch(MoleculeMx cs)
        {
            if (MoleculeMx.IsUndefined(QueryMol) || MoleculeMx.IsUndefined(cs))
            {
                return(false);
            }

            if (cs.IsBiopolymerFormat)
            {
                return(IsFSSMatch(QueryMol, cs));
            }

            TargetMol    = cs;
            FSSTargetMol = CdkMolFactory.NewCdkMol(cs);
            bool b = CdkMolUtil.IsFSSMatch(FSSTargetMol);

            return(b);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Map query against structure & return highlighted match
        /// </summary>
        /// <param name="cs"></param>
        /// <returns></returns>

        public MoleculeMx HighlightMatchingSubstructure(
            MoleculeMx cs)
        {
            if (cs.IsBiopolymerFormat)             // fix later
            {
                return(cs);
            }

            MoleculeMx       cs2 = null;
            PerformanceTimer pt  = PT.Start("HighlightMatchingSubstructure");

            string molfile = CdkMolUtil.HilightSSSMatch(cs.GetMolfileString());

            cs2 = new MoleculeMx(MoleculeFormat.Molfile, molfile);

            pt.Update();
            return(cs2);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Do a similarity match
        /// </summary>
        /// <param name="query"></param>
        /// <param name="target"></param>
        /// <returns></returns>

        public bool FullStructureMatch(
            MoleculeMx query,
            MoleculeMx target,
            string switches)
        {
            bool newQuery = (QueryMol == null || QueryMol.PrimaryValue != query.PrimaryValue);

            if (newQuery)
            {
                QueryMol = query.Clone();

                SSSQueryMol = CdkMolFactory.NewCdkMol(query);
            }

            SSSTargetMol = CdkMolFactory.NewCdkMol(target);
            bool b = CdkMolUtil.FullStructureMatch(SSSQueryMol, SSSTargetMol, switches);

            return(b);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Align supplied structure to query structure
        /// </summary>
        /// <param name="skid"></param>
        /// <returns></returns>

        public MoleculeMx AlignToMatchingSubstructure(
            MoleculeMx target)
        {
            if (target.IsBiopolymerFormat)             // fix later
            {
                return(target);
            }

            MoleculeMx       cs2 = null;
            PerformanceTimer pt  = PT.Start("AlignToMatchingSubstructure");
            Stopwatch        sw  = Stopwatch.StartNew();

            string molfile = CdkMolUtil.OrientToMatchingSubstructure(target.GetMolfileString());

            cs2 = new MoleculeMx(MoleculeFormat.Molfile, molfile);

            pt.Update();
            return(cs2);
        }
Exemplo n.º 16
0
        /// <summary>
        /// SMILES generation for  the whole HELM molecule
        /// </summary>
        /// <param name="helm"></param>
        /// <returns></returns>

        public static string HelmToSmiles(
            MoleculeMx mol,
            string helm)
        {
            string smiles = null;

            if (UseWebEditor && DebugMx.False)             // don't use this, only gets molfile at monomer level, not atoms/bonds
            {
                Browser.SetHelm(helm);
                smiles = Browser.GetSmiles();
            }

            else
            {
                smiles = HelmService.GenerateSMILESForHELM(helm);
            }

            return(smiles);
        }
Exemplo n.º 17
0
/// <summary>
/// Match a structure to conditional formatting & return the first matching item
/// </summary>
/// <param name="rules"></param>
/// <param name="structure"></param>
/// <returns></returns>

        public static CondFormatRule Match(
            CondFormat condFormat,
            MoleculeMx structure)
        {
            CondFormatRules rules = condFormat.Rules;
            CondFormatRule  rule  = null;
            int             ri;

            if (structure == null || structure.PrimaryFormat == MoleculeFormat.Unknown)
            {
                return(MatchNull(condFormat));
            }

            StructureMatcher matcher = new StructureMatcher();

            for (ri = 0; ri < rules.Count; ri++)             // match rules one at a time until get a hit
            {
                rule = rules[ri];
                if (String.IsNullOrEmpty(rule.Value))
                {
                    continue;
                }

                MoleculeMx query = new MoleculeMx(rule.Value);
                matcher.SetSSSQueryMolecule(query);
                bool matches = matcher.IsSSSMatch(structure);
                //DebugLog.Message("Rule: " + ri + ", " + query.SmilesString + ", " + structure.SmilesString + ", " + matches);
                if (matches)
                {
                    break;
                }
            }

            if (ri < rules.Count)
            {
                return(rule);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 18
0
        /// <summary>
        /// Convert any ChemicalStructures containing a sketch value to a ChimeString for proper serialization
        /// </summary>
        /// <param name="oa"></param>

        public static void ConvertMoleculesToChimeStrings(object[] oa)
        {
            for (int ci = 0; ci < oa.Length; ci++)
            {
                if (!(oa[ci] is MoleculeMx))
                {
                    continue;
                }
                MoleculeMx cs = oa[ci] as MoleculeMx;
                if (Math.Abs(1) == 1)
                {
                    continue;                                   // noop for now
                }
                //if (cs.Type != StructureFormat.Sketch) continue; // obsolete
                MoleculeFormat type  = MoleculeFormat.Chime;
                string         value = cs.GetChimeString();

                cs.SetPrimaryTypeAndValue(type, value);
            }
        }
Exemplo n.º 19
0
        public static Bitmap HelmToBitmap(
            MoleculeMx mol,
            int pixWidth)
        {
            Bitmap bm = null;

            string svg = mol?.SvgString;
            string msg = String.Format("mol.Id: {0}, mol.Helm: {1}", mol?.Id, mol?.HelmString);

            try
            {
                if (mol.PrimaryFormat == MoleculeFormat.Helm && Lex.IsDefined(svg))
                {                 // if SVG available use it
                    bm = SvgUtil.GetBitmapFromSvgXml(svg, pixWidth);

                    if (Debug)
                    {
                        DebugLog.Message(msg + " used existing SVG");
                    }
                    return(bm);
                }

                else
                {
                    string helm = mol.HelmString;
                    bm = HelmControl.GetBitmap(helm, pixWidth);
                    if (Debug)
                    {
                        DebugLog.Message(msg + " generated SVG");
                    }
                    return(bm);
                }
            }

            catch (Exception ex)
            {
                msg += "\r\n" + DebugLog.FormatExceptionMessage(ex);
                DebugLog.Message(msg);                 // log it
                return(null);
            }
        }
Exemplo n.º 20
0
        /// <summary>
        /// Convert Helm to Molfile format
        /// </summary>
        /// <param name="helm"></param>
        /// <returns></returns>

        public static string HelmToMolfile(
            MoleculeMx mol,
            string helm)
        {
            string molfile = null;

            if (UseWebEditor)
            {
                lock (Browser)                 // lock to avoid reentrancy
                {
                    Browser.SetHelm(helm);
                    molfile = Browser.GetMolfile();
                }
            }

            else
            {
                return("Not Implemented");
            }

            return(molfile);
        }
Exemplo n.º 21
0
        /// <summary>
        /// Convert object to a MobiusDataType
        /// </summary>
        /// <param name="o"></param>
        /// <param name="type"></param>
        /// <returns></returns>

        public static MobiusDataType ConvertToMobiusDataType(
            MetaColumnType type,
            Object o)
        {
            switch (type)
            {
            case MetaColumnType.Integer:
            case MetaColumnType.Number:
            case MetaColumnType.DictionaryId:
                return(NumberMx.ConvertTo(o));

            case MetaColumnType.QualifiedNo:
                return(QualifiedNumber.ConvertTo(o));

            case MetaColumnType.String:
            case MetaColumnType.MolFormula:
            case MetaColumnType.Hyperlink:
            case MetaColumnType.Html:
            case MetaColumnType.Binary:
                return(StringMx.ConvertTo(o));

            case MetaColumnType.Date:
                return(DateTimeMx.ConvertTo(o));

            case MetaColumnType.CompoundId:
                return(CompoundId.ConvertTo(o));

            case MetaColumnType.Structure:
                return(MoleculeMx.ConvertTo(o));

            case MetaColumnType.Image:
                return(ImageMx.ConvertTo(o));                        // assume text is dblink

            default:
                throw new Exception("Unrecognized data type: " + type);
            }
        }
Exemplo n.º 22
0
        /// <summary>
        /// Calculate similarity score
        /// </summary>
        /// <param name="query"></param>
        /// <param name="target"></param>
        /// <returns></returns>

        public double Molsim(
            MoleculeMx query,
            MoleculeMx target,
            SimilaritySearchType type)
        {
            FingerprintType fpType = FingerprintType.Undefined;

            if (type == SimilaritySearchType.Normal)
            {
                fpType = FingerprintType.MACCS;
            }

            else if (type == SimilaritySearchType.ECFP4)
            {
                fpType = FingerprintType.Circular;
            }

            else
            {
                throw new Exception("Similarity type not supported in this context: " + type);
            }

            bool newQuery = (QueryMol == null || QueryMol.PrimaryValue != query.PrimaryValue);

            if (newQuery)             // recalc query keys if query has changed
            {
                QueryMol         = query.Clone();
                QueryFingerprint = query.BuildBitSetFingerprint(fpType);
            }

            object targetFingerprint = target.BuildBitSetFingerprint(fpType);

            double score = MoleculeMx.CalculateBitSetFingerprintSimilarity(QueryFingerprint, targetFingerprint);

            return(score);
        }
Exemplo n.º 23
0
/// <summary>
/// Deserialize a single value object
/// </summary>
/// <param name="ba"></param>
/// <returns></returns>

        public static object ReadBinaryItem(
            BinaryReader br)
        {
            object     vo     = null;
            VoDataType voType = (VoDataType)br.ReadByte();

            switch (voType)
            {
            case VoDataType.Null:
                return(null);

            case VoDataType.DbNull:
                return(DBNull.Value);

            case VoDataType.Byte:
                return(br.ReadByte());

            case VoDataType.Int16:
                return(br.ReadInt16());

            case VoDataType.Int32:
                return(br.ReadInt32());

            case VoDataType.Int64:
                return(br.ReadInt64());

            case VoDataType.Float:
                return(br.ReadSingle());

            case VoDataType.Double:
                return(br.ReadDouble());

            case VoDataType.Char:
                return(br.ReadChar());

            case VoDataType.String:
                return(br.ReadString());

            case VoDataType.DateTime:
                return(new DateTime(br.ReadInt64()));

            case VoDataType.CompoundId:
                return(CompoundId.DeserializeBinary(br));

            case VoDataType.NumberMx:
                return(NumberMx.DeserializeBinary(br));

            case VoDataType.QualifiedNo:
                return(QualifiedNumber.DeserializeBinary(br));

            case VoDataType.StringMx:
                return(StringMx.DeserializeBinary(br));

            case VoDataType.DateTimeMx:
                return(DateTimeMx.DeserializeBinary(br));

            case VoDataType.ImageMx:
                return(ImageMx.DeserializeBinary(br));

            case VoDataType.ChemicalStructure:
                return(MoleculeMx.DeserializeBinary(br));

            default:
                throw new Exception("Unrecognized type: " + voType);
            }
        }
Exemplo n.º 24
0
        /////////////////////////////////////////////////////////////////
        //////////////////////////// Methods ////////////////////////////
        /////////////////////////////////////////////////////////////////

/// <summary>
/// Create a Mobius data type based on the supplied MetaColumn type
/// </summary>
/// <param name="type"></param>
/// <returns></returns>

        public static MobiusDataType New(
            MetaColumnType type)
        {
            MobiusDataType o;

            if (type == MetaColumnType.Integer)
            {
                o = new NumberMx();
            }
            else if (type == MetaColumnType.Number)
            {
                o = new NumberMx();
            }
            else if (type == MetaColumnType.QualifiedNo)
            {
                o = new QualifiedNumber();
            }
            else if (type == MetaColumnType.String)
            {
                o = new StringMx();
            }
            else if (type == MetaColumnType.Date)
            {
                o = new DateTimeMx();
            }
            else if (type == MetaColumnType.Binary)
            {
                o = new StringMx();
            }
            else if (type == MetaColumnType.CompoundId)
            {
                o = new CompoundId();
            }
            else if (type == MetaColumnType.Structure)
            {
                o = new MoleculeMx();
            }
            else if (type == MetaColumnType.MolFormula)
            {
                o = new StringMx();
            }
            else if (type == MetaColumnType.Image)
            {
                o = new ImageMx();
            }
            else if (type == MetaColumnType.DictionaryId)
            {
                o = new NumberMx();
            }
            else if (type == MetaColumnType.Hyperlink)
            {
                o = new StringMx();
            }
            else if (type == MetaColumnType.Html)
            {
                o = new StringMx();
            }
            else
            {
                throw new Exception("Unrecognized data type: " + type);
            }

            return(o);
        }
Exemplo n.º 25
0
        /// <summary>
        /// Create CdkMol instance from MoleculeMx
        /// </summary>
        /// <param name="molMx"></param>
        /// <returns></returns>

        public static INativeMolMx NewCdkMol(MoleculeMx molMx)
        {
            return(I.NewCdkMol(molMx));
        }
Exemplo n.º 26
0
        /// <summary>
        /// Custom compact Deserialization of MobiusDataType
        /// </summary>
        /// <returns></returns>

        public static MobiusDataType Deserialize(string[] sa)
        {
            int sai;

            MobiusDataType mdt  = null;
            char           type = sa[0][0];

            sai = 5;             // first array entry for specific subclass

            switch (type)
            {
            case 'M':                     // ChemicalStructure (Molecule)
                mdt = MoleculeMx.Deserialize(sa, sai);
                break;

            case 'C':                     // CompoundId
                mdt = CompoundId.Deserialize(sa, sai);
                break;

            case 'D':                     // DateTimeEx
                mdt = DateTimeMx.Deserialize(sa, sai);
                break;

            case 'I':                     // ImageEx
                mdt = ImageMx.Deserialize(sa, sai);
                break;

            case 'N':                     //NumberEx
                mdt = NumberMx.Deserialize(sa, sai);
                break;

            case 'Q':                     // QualifiedNumber
                mdt = QualifiedNumber.Deserialize(sa, sai);
                break;

            case 'S':                     // StringEx
                mdt = StringMx.Deserialize(sa, sai);
                break;

            default:
                throw new Exception("Unexpected MobiusDataType: " + type);
            }

            if (!Lex.IsNullOrEmpty(sa[1]))
            {
                mdt.BackColor = Color.FromArgb(int.Parse(sa[1]));
            }

            if (!Lex.IsNullOrEmpty(sa[2]))
            {
                mdt.ForeColor = Color.FromArgb(int.Parse(sa[2]));
            }

            if (!Lex.IsNullOrEmpty(sa[3]))
            {
                mdt.DbLink = sa[3];
            }

            if (!Lex.IsNullOrEmpty(sa[4]))
            {
                mdt.Hyperlink = sa[4];
            }

            return(mdt);
        }