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) { MDLCounts 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); }
private MDLCounts ReadCtabHeader(StreamReader reader) { MDLCounts result = new MDLCounts(); // Read Line #1 - Title string title = SdFileConverter.GetNextLine(reader); if (!string.IsNullOrEmpty(title)) { if (title.ToUpper().StartsWith("$MDL")) { _molecule.Errors.Add("RGFiles are currently not supported"); throw new InvalidDataException("RGFiles are currently not supported"); } if (title.ToUpper().StartsWith("$RXN")) { _molecule.Errors.Add("RXNFiles are currently not supported"); throw new InvalidDataException("RXNFiles are currently not supported"); } if (title.ToUpper().StartsWith("$RDFILE")) { _molecule.Errors.Add("RDFiles are currently not supported"); throw new InvalidDataException("RDFiles are currently not supported"); } if (title.ToUpper().StartsWith("<XDFILE>")) { _molecule.Errors.Add("XDFiles are currently not supported"); throw new InvalidDataException("XDFiles are currently not supported"); } } // Read and discard Line #2 - Header SdFileConverter.GetNextLine(reader); // Read and discard Line #3 - Comment SdFileConverter.GetNextLine(reader); // Read Line #4 - Counts string counts = SdFileConverter.GetNextLine(reader); 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) { _molecule.Errors.Add($"Exception {ex.Message}"); result.Message = $"Exception {ex.Message}"; Debug.WriteLine(ex.Message); } } else { result.Message = "Line containing atom and bond counts not found!"; } return(result); }