/// <summary> /// Method will return an appropriate reader for the provided format. Each reader is stored /// in a map, if no reader is available for the specified format a new reader is created. The /// <see cref="IChemObjectReader.ErrorHandler"/> and /// <see cref="IChemObjectReader.ReaderMode"/> are set. /// </summary> /// <param name="format">The format to obtain a reader for</param> /// <returns>instance of a reader appropriate for the provided format</returns> private ISimpleChemObjectReader GetReader(IChemFormat format, TextReader input) { ISimpleChemObjectReader reader; if (format is MDLV2000Format) { reader = new MDLV2000Reader(input); } else if (format is MDLV3000Format) { reader = new MDLV3000Reader(input); } else if (format is MDLFormat) { reader = new MDLReader(input); } else { throw new ArgumentException("Unexpected format: " + format); } reader.ErrorHandler = this.ErrorHandler; reader.ReaderMode = this.ReaderMode; if (currentFormat is MDLV2000Format) { reader.AddSettings(IOSettings.Settings); } return(reader); }
/// <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); }
/// <summary> /// Method will return an appropriate reader for the provided format. Each reader is stored /// in a map, if no reader is available for the specified format a new reader is created. The /// <see cref="IChemObjectReader.ErrorHandler"/> and /// <see cref="IChemObjectReader.ReaderMode"/> are set. /// </summary> /// <param name="format">The format to obtain a reader for</param> /// <returns>instance of a reader appropriate for the provided format</returns> private ISimpleChemObjectReader GetReader(IChemFormat format, TextReader input) { ISimpleChemObjectReader reader; switch (format) { case MDLV2000Format _: reader = new MDLV2000Reader(input); break; case MDLV3000Format _: reader = new MDLV3000Reader(input); break; case MDLFormat _: reader = new MDLReader(input); break; default: throw new ArgumentException($"Unexpected format: {format}"); } reader.ErrorHandler = this.ErrorHandler; reader.ReaderMode = this.ReaderMode; if (currentFormat is MDLV2000Format) { reader.AddSettings(IOSettings.Settings); } return(reader); }
/// <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); } DefaultChemObjectReader cor; StringReader sr = new StringReader(molfile); cor = new MDLV3000Reader(sr); cor.ReaderMode = ChemObjectReaderMode.Relaxed; IAtomContainer mol = cor.Read(new AtomContainer()); cor.Close(); for (int ai = 0; ai < mol.Atoms.Count; ai++) { IAtom a = mol.Atoms[ai]; if (map.ContainsKey(ai + 1)) { a.MassNumber = map[ai + 1]; } //a.setMassNumber(new java.lang.Integer(map[ai + 1])); else { a.MassNumber = null; } } ConfigureAtomContainer(mol); return(mol); }
static IAtomContainer RawParse(string text, out string notationType) { IAtomContainer mol = null; if (text == null) { notationType = "None"; return(null); } if (text.IndexOf('\r') >= 0) { goto Go_Mol; } if (text.IndexOf('\n') >= 0) { goto Go_Mol; } mol = ParseSmiles(text); if (mol != null) { notationType = "SMILES"; return(mol); } mol = ParseInChI(text); if (mol != null) { notationType = "InChI"; return(mol); } Go_Mol: using (var r = new MDLV2000Reader(new StringReader(text))) { var m = CDK.Builder.NewAtomContainer(); try { r.Read(m); mol = m; } catch (Exception) { } } if (mol != null) { notationType = "MolBlock"; return(mol); } using (var r = new MDLV3000Reader(new StringReader(text))) { var m = CDK.Builder.NewAtomContainer(); try { r.Read(m); mol = m; } catch (Exception) { } } if (mol != null) { notationType = "MolBlock"; return(mol); } notationType = "None"; return(null); }