internal virtual void TestMol() { Molecule mol = new Molecule(); mol.AddAtom("N", 0, 0); mol.AddAtom("C", 1.2, 0); mol.AddAtom("O", 2, 0.8); mol.AddAtom("H", 3, - 0.8); mol.AddAtom("H", 4, 0); mol.AddBond(1, 2, 1); mol.AddBond(2, 3, 2); mol.AddBond(3, 4, 1); mol.AddBond(4, 5, 0); editor.Replace(mol); }
// returns an equivalent replica of the molecule public virtual Molecule Clone() { Molecule mol = new Molecule(); for (int n = 1; n <= NumAtoms(); n++) { Atom a = atoms[n - 1]; int num = mol.AddAtom(a.Element, a.X, a.Y, a.Charge, a.Unpaired); mol.SetAtomHExplicit(num, AtomHExplicit(n)); } for (int n = 1; n <= NumBonds(); n++) { Bond b = bonds[n - 1]; mol.AddBond(b.From, b.To, b.Order, b.Type); } return mol; }
internal static Molecule ReadXML(FileStream istr) { Molecule mol = new Molecule(); const string NAMESPACE = "http://www.xml-cml.org/schema/cml2/core"; const string ATTRIBUTENS = ""; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(istr); XPathNavigator navigator = xmlDoc.CreateNavigator(); if (navigator.MoveToFirstChild()) { navigator.MoveToChild("molecule", NAMESPACE); //navigator.MoveToFirstChild(); if (navigator.MoveToChild("atomArray", NAMESPACE)) { if (navigator.MoveToChild("atom", NAMESPACE)) { do { string elementType= navigator.GetAttribute("elementType", ATTRIBUTENS); string x2String = navigator.GetAttribute("x2", ATTRIBUTENS); if (!string.IsNullOrEmpty(x2String)) { double x2 = Utility.SafeDouble(x2String); double y2 = Utility.SafeDouble(navigator.GetAttribute("y2", ATTRIBUTENS)); int hydrogenCount = Utility.SafeInt(navigator.GetAttribute("hydrogenCount", ATTRIBUTENS)); // TODO: How do we handle hydrogen count? mol.AddAtom(elementType, x2, y2); } else { double x3 = Utility.SafeDouble(navigator.GetAttribute("x3", ATTRIBUTENS)); double y3 = Utility.SafeDouble(navigator.GetAttribute("y3", ATTRIBUTENS)); int hydrogenCount = Utility.SafeInt(navigator.GetAttribute("hydrogenCount", ATTRIBUTENS)); // TODO: How do we handle hydrogen count? mol.AddAtom(elementType, x3, y3); } } while (navigator.MoveToNext("atom", NAMESPACE)); navigator.MoveToParent(); } navigator.MoveToParent(); } if (navigator.MoveToChild("bondArray", NAMESPACE)) { if (navigator.MoveToChild("bond", NAMESPACE)) { do { string atomRefs = navigator.GetAttribute("atomRefs2", ATTRIBUTENS); string[] parts = atomRefs.Split(' '); if (parts.Length == 2) { int from = Utility.SafeInt(parts[0].Substring(1)); int to = Utility.SafeInt(parts[1].Substring(1)); int order = Utility.SafeInt(navigator.GetAttribute("order", ATTRIBUTENS)); int type = 0; // Where or is type stored? // TODO: How do we handle hydrogen count? mol.AddBond(from, to, order, type); } } while (navigator.MoveToNext("bond", NAMESPACE)); navigator.MoveToParent(); } navigator.MoveToParent(); } } return mol; }
public static Molecule ReadNative(System.IO.StreamReader in_Renamed) { Molecule mol = new Molecule(); //UPGRADE_NOTE: Final was removed from the declaration of 'GENERIC_ERROR '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" System.String GENERIC_ERROR = "Invalid SketchEl file."; try { System.String line = in_Renamed.ReadLine(); if (!line.StartsWith("SketchEl!")) throw new System.IO.IOException("Not a SketchEl file."); int p1 = line.IndexOf('('), p2 = line.IndexOf(','), p3 = line.IndexOf(')'); if (p1 == 0 || p2 == 0 || p3 == 0) throw new System.IO.IOException(GENERIC_ERROR); int numAtoms = System.Int32.Parse(line.Substring(p1 + 1, (p2) - (p1 + 1)).Trim()); int numBonds = System.Int32.Parse(line.Substring(p2 + 1, (p3) - (p2 + 1)).Trim()); for (int n = 0; n < numAtoms; n++) { line = in_Renamed.ReadLine(); // TODO: verify java's split method syntax: "[\\=\\,\\;]" System.String[] bits = line.Split('=', ',', ';'); if (bits.Length < 5) throw new System.IO.IOException(GENERIC_ERROR); int num = mol.AddAtom(bits[0], System.Double.Parse(bits[1].Trim()), System.Double.Parse(bits[2].Trim()), System.Int32.Parse(bits[3].Trim()), System.Int32.Parse(bits[4].Trim())); if (bits.Length >= 6 && bits[5].Length > 0 && bits[5][0] == 'e') mol.SetAtomHExplicit(num, System.Int32.Parse(bits[5].Substring(1))); } for (int n = 0; n < numBonds; n++) { line = in_Renamed.ReadLine(); System.String[] bits = line.Split('-', '=', ','); // "[\\-\\=\\,]"); if (bits.Length < 4) throw new System.IO.IOException(GENERIC_ERROR); mol.AddBond(System.Int32.Parse(bits[0].Trim()), System.Int32.Parse(bits[1].Trim()), System.Int32.Parse(bits[2].Trim()), System.Int32.Parse(bits[3].Trim())); } line = in_Renamed.ReadLine(); if (String.CompareOrdinal(line, "!End") != 0) throw new System.IO.IOException(GENERIC_ERROR); } catch (System.Exception) { throw new System.IO.IOException(GENERIC_ERROR); } return mol; }
public static Molecule ReadMDLMOL(StreamReader in_Renamed) { Molecule mol = new Molecule(); //UPGRADE_NOTE: Final was removed from the declaration of 'GENERIC_ERROR '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" System.String GENERIC_ERROR = "Invalid MDL MOL file."; try { System.String line = null; for (int n = 0; n < 4; n++) line = in_Renamed.ReadLine(); if (line == null || !line.Substring(34, (39) - (34)).Equals("V2000")) throw new System.IO.IOException(GENERIC_ERROR); int numAtoms = System.Int32.Parse(line.Substring(0, (3) - (0)).Trim()); int numBonds = System.Int32.Parse(line.Substring(3, (6) - (3)).Trim()); for (int n = 0; n < numAtoms; n++) { line = in_Renamed.ReadLine(); double x = System.Double.Parse(line.Substring(0, (10) - (0)).Trim()); double y = System.Double.Parse(line.Substring(10, (20) - (10)).Trim()); System.String el = line.Substring(31, (33) - (31)).Trim(); int chg = System.Int32.Parse(line.Substring(36, (39) - (36)).Trim()), rad = 0; if (chg <= 3) { } else if (chg == 4) { chg = 0; rad = 2; } else chg = 4 - chg; mol.AddAtom(el, x, y, chg, rad); } for (int n = 0; n < numBonds; n++) { line = in_Renamed.ReadLine(); int from = System.Int32.Parse(line.Substring(0, (3) - (0)).Trim()), to = System.Int32.Parse(line.Substring(3, (6) - (3)).Trim()); int type = System.Int32.Parse(line.Substring(6, (9) - (6)).Trim()), stereo = System.Int32.Parse(line.Substring(9, (12) - (9)).Trim()); if (from == to || from < 1 || from > numAtoms || to < 1 || to > numAtoms) throw new System.IO.IOException(GENERIC_ERROR); int order = type >= 1 && type <= 3?type:1; int style = Molecule.BONDTYPE_NORMAL; if (stereo == 1) style = Molecule.BONDTYPE_INCLINED; else if (stereo == 6) style = Molecule.BONDTYPE_DECLINED; else if (stereo == 3 || stereo == 4) style = Molecule.BONDTYPE_UNKNOWN; mol.AddBond(from, to, order, style); } while (true) { line = in_Renamed.ReadLine(); if (line.StartsWith("M END")) break; int type = 0; if (line.StartsWith("M CHG")) type = 1; else if (line.StartsWith("M RAD")) type = 2; if (type > 0) { int len = System.Int32.Parse(line.Substring(6, (9) - (6)).Trim()); for (int n = 0; n < len; n++) { int apos = System.Int32.Parse(line.Substring(9 + 8 * n, (13 + 8 * n) - (9 + 8 * n)).Trim()); int aval = System.Int32.Parse(line.Substring(13 + 8 * n, (17 + 8 * n) - (13 + 8 * n)).Trim()); if (type == 1) mol.SetAtomCharge(apos, aval); else mol.SetAtomUnpaired(apos, aval); } } } } catch (System.Exception) { throw new System.IO.IOException(GENERIC_ERROR); } return mol; }