예제 #1
0
        public override SdfState ImportFromStream(StreamReader reader, Molecule molecule, out string message)
        {
            _molecule    = molecule;
            atomByNumber = new Dictionary <int, Atom>();
            bondByNumber = new Dictionary <int, Bond>();
            numberByAtom = new Dictionary <Atom, int>();

            message = null;
            SdfState result = SdfState.Null;

            try
            {
                while (!reader.EndOfStream)
                {
                    Counts counts = ReadCtabHeader(reader);
                    if (!string.IsNullOrEmpty(counts.Message))
                    {
                        if (counts.Message.Contains("Unsupported"))
                        {
                            result = SdfState.Unsupported;
                        }
                        else
                        {
                            result = SdfState.Error;
                        }
                        message = counts.Message;
                        break;
                    }
                    else
                    {
                        ReadAtoms(reader, counts.Atoms);
                        ReadBonds(reader, counts.Bonds);
                        result = ReadCtabFooter(reader);
                        break;
                    }
                }
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine(ex.Message);
                message = ex.Message;
                result  = SdfState.Error;
            }

            return(result);
        }
예제 #2
0
        private Counts ReadCtabHeader(StreamReader reader)
        {
            Counts result = new Counts();

            // Title
            string title = SdFileConverter.GetNextLine(reader); //reader.ReadLine();

            if (!string.IsNullOrEmpty(title))
            {
                if (title.StartsWith("$MDL"))
                {
                    _molecule.Errors.Add("RGFiles are currently not supported");
                    throw new Exception("RGFiles are currently not supported");
                }
                if (title.StartsWith("$RXN"))
                {
                    _molecule.Errors.Add("RXNFiles are currently not supported");
                    throw new Exception("RXNFiles are currently not supported");
                }
                if (title.StartsWith("$RDFILE"))
                {
                    _molecule.Errors.Add("RDFiles are currently not supported");
                    throw new Exception("RDFiles are currently not supported");
                }
                if (title.StartsWith("<XDfile>"))
                {
                    _molecule.Errors.Add("XDFiles are currently not supported");
                    throw new Exception("XDFiles are currently not supported");
                }
            }

            // Header
            string header = SdFileConverter.GetNextLine(reader); //reader.ReadLine();

            // Comment
            string comment = SdFileConverter.GetNextLine(reader); //reader.ReadLine();

            // Counts
            string counts = SdFileConverter.GetNextLine(reader); //reader.ReadLine();

            if (counts.ToLower().Contains("v2000"))
            {
                try
                {
                    result.Atoms   = ParseInteger(counts, 0, 3);
                    result.Bonds   = ParseInteger(counts, 3, 3);
                    result.Version = GetSubString(counts, 34, 5).ToUpper();
                }
                catch (Exception ex)
                {
                    result.Message = $"Exception {ex.Message}";
                    Debug.WriteLine(ex.Message);
                }
            }
            else
            {
                result.Message = "Line containing atom and bond counts not found!";
            }

            return(result);
        }