public override void CharacterData(CMLStack xpath, XElement element) { string s = element.Value.Trim(); if (xpath.ToString().EndsWith("string/", StringComparison.Ordinal) && Builtin.Equals("stereo", StringComparison.Ordinal)) { StereoGiven = true; switch (s.Trim()) { case "W": Debug.WriteLine("CML W stereo found"); BondStereo.Add("1"); break; case "H": Debug.WriteLine("CML H stereo found"); BondStereo.Add("6"); break; } } else { base.CharacterData(xpath, element); } }
public override void EndElement(CMLStack xpath, XElement element) { string name = element.Name.LocalName; if (current == ENERGY) { // cdo.SetObjectProperty("Frame", "energy", frame_energy); // + " " + units); // FIXME: does not have a ChemFileCDO equivalent current = Unknown; frame_energy = ""; } else if (string.Equals(name, "list", StringComparison.Ordinal)) { base.EndElement(xpath, element); // cdo.EndObject("Animation"); CurrentChemFile.Add(CurrentChemSequence); } else if (string.Equals(name, "molecule", StringComparison.Ordinal)) { base.EndElement(xpath, element); // cdo.EndObject("Frame"); // nothing done in the CD upon this event } else { base.EndElement(xpath, element); } }
/// <summary> /// Constructor for the CMLHandler. /// </summary> /// <param name="chemFile">The document in which data is stored</param> public CMLHandler(IChemFile chemFile) { conv = new CMLCoreModule(chemFile); userConventions = new Dictionary <string, ICMLModule>(); xpath = new CMLStack(); conventionStack = new CMLStack(); moduleStack = new CMLModuleStack(); }
public void TestEndsWith_String_String_String() { CMLStack stack = new CMLStack(); stack.Push("first"); stack.Push("second"); stack.Push("third"); Assert.IsTrue(stack.EndsWith("first", "second", "third")); }
public void TestPush_String() { // the class has a hardcoded default length. Test going beyond this. CMLStack stack = new CMLStack(); for (int i = 0; i < 100; i++) { stack.Push("element"); } }
public override void CharacterData(CMLStack xpath, XElement element) { if (current == ENERGY) { frame_energy = element.Value; } else { base.CharacterData(xpath, element); } }
public void TestSize() { CMLStack stack = new CMLStack(); Assert.AreEqual(0, stack.Count); stack.Push("first"); Assert.AreEqual(1, stack.Count); stack.Push("second"); Assert.AreEqual(2, stack.Count); stack.Push("third"); Assert.AreEqual(3, stack.Count); }
public void TestCurrent() { CMLStack stack = new CMLStack(); stack.Push("first"); Assert.AreEqual("first", stack.Peek()); stack.Push("second"); Assert.AreEqual("second", stack.Peek()); stack.Push("third"); Assert.AreEqual("third", stack.Peek()); stack.Pop(); Assert.AreEqual("second", stack.Peek()); stack.Pop(); Assert.AreEqual("first", stack.Peek()); }
public override void StartElement(CMLStack xpath, XElement element) { isBond = false; if (xpath.ToString().EndsWith("string/", StringComparison.Ordinal)) { var a_buildin = element.Attribute("buildin"); if (a_buildin != null && a_buildin.Value.Equals("order", StringComparison.Ordinal)) { isBond = true; } } else { base.StartElement(xpath, element); } }
public override void StartElement(CMLStack xpath, XElement element) { string name = element.Name.LocalName; if (string.Equals(name, "list", StringComparison.Ordinal)) { Debug.WriteLine("Oke, JMOLANIMATION seems to be kicked in :)"); // cdo.StartObject("Animation"); CurrentChemSequence = CurrentChemFile.Builder.NewChemSequence(); base.StartElement(xpath, element); } else if (string.Equals(name, "molecule", StringComparison.Ordinal)) { // cdo.StartObject("Frame"); CurrentChemModel = CurrentChemFile.Builder.NewChemModel(); Debug.WriteLine("New frame being parsed."); base.StartElement(xpath, element); } else if (string.Equals(name, "float", StringComparison.Ordinal)) { bool isEnergy = false; Debug.WriteLine("FLOAT found!"); foreach (var att in element.Attributes()) { Debug.WriteLine($" att: {att.Name.LocalName} -> {att.Value}"); if (att.Name.LocalName.Equals("title", StringComparison.Ordinal) && att.Value.Equals("FRAME_ENERGY", StringComparison.Ordinal)) { isEnergy = true; } } if (isEnergy) { // oke, this is the frames energy! current = ENERGY; } else { base.StartElement(xpath, element); } } else { base.StartElement(xpath, element); } }
public override void EndElement(CMLStack xpath, XElement element) { var local = element.Name.LocalName; switch (local) { case "reaction": CurrentReactionSet.Add(CurrentReaction); CurrentChemModel.ReactionSet = CurrentReactionSet; break; case "reactionList": CurrentChemModel.ReactionSet = CurrentReactionSet; /* FIXME: this should be when document is closed! */ break; case "reactant": CurrentReaction.Reactants.Add(CurrentMolecule); break; case "product": CurrentReaction.Products.Add(CurrentMolecule); break; case "substance": CurrentReaction.Agents.Add(CurrentMolecule); break; case "molecule": Debug.WriteLine("Storing Molecule"); //if the current molecule exists in the currentMoleculeSet means that is a reference in these. if (CurrentMoleculeSet.GetMultiplier(CurrentMolecule) == -1) { base.StoreData(); } // do nothing else but store atom/bond information break; default: base.EndElement(xpath, element); break; } }
public void TestPop() { CMLStack stack = new CMLStack(); stack.Push("first"); stack.Push("second"); stack.Push("third"); Assert.AreEqual("third", stack.Pop()); Assert.AreEqual("second", stack.Pop()); Assert.AreEqual("first", stack.Pop()); try { Assert.AreEqual("doesNotExist", stack.Pop()); Assert.Fail("Should have received an ArrayIndexOutOfBoundsException"); } catch (Exception) { // OK, should happen } }
public override void CharacterData(CMLStack xpath, XElement element) { string s = element.Value; if (isBond) { Debug.WriteLine($"CharData (bond): {s}"); var st = Strings.Tokenize(s); foreach (var border in st) { Debug.WriteLine($"new bond order: {border}"); // assume cdk bond object has already started // cdo.SetObjectProperty("Bond", "order", border); CurrentBond.Order = BondManipulator.CreateBondOrder(double.Parse(border, NumberFormatInfo.InvariantInfo)); } } else { base.CharacterData(xpath, element); } }
public override void EndElement(CMLStack xpath, XElement element) { string name = element.Name.LocalName; // OLD +++++++++++++++++++++++++++++++++++++++++++++ switch (name) { case "list": if (connectionTable && !isBond) { Debug.WriteLine("End Connection Table"); connectionTable = false; // OLD +++++++++++++++++++++++++++++++++++++++++++++ } break; case "molecule": StoreData(); if (xpath.Count == 1) { // cdo.EndObject("Molecule"); if (CurrentMolecule is IAtomContainer) { Debug.WriteLine("Adding molecule to set"); CurrentMoleculeSet.Add(CurrentMolecule); Debug.WriteLine($"#mols in set: {CurrentMoleculeSet.Count}"); } else if (CurrentMolecule is ICrystal) { Debug.WriteLine("Adding crystal to chemModel"); CurrentChemModel.Crystal = (ICrystal)CurrentMolecule; CurrentChemSequence.Add(CurrentChemModel); } } break; } isELSYM = false; isBond = false; }
public override void StartElement(CMLStack xpath, XElement element) { Debug.WriteLine("PMP element: name"); base.StartElement(xpath, element); }
/// <summary> /// Finish up parsing of elements in mdmolecule. /// </summary> /// <param name="xpath"></param> /// <param name="element"></param> public override void EndElement(CMLStack xpath, XElement element) { if (element.Name.Equals(XName_CML_molecule)) { // add chargeGroup, and then delete them if (currentChargeGroup != null) { if (CurrentMolecule is MDMolecule) { ((MDMolecule)CurrentMolecule).AddChargeGroup(currentChargeGroup); } else { Trace.TraceError("Need to store a charge group, but the current molecule is not a MDMolecule!"); } currentChargeGroup = null; } else // add chargeGroup, and then delete them if (currentResidue != null) { if (CurrentMolecule is MDMolecule) { ((MDMolecule)CurrentMolecule).AddResidue(currentResidue); } else { Trace.TraceError("Need to store a residue group, but the current molecule is not a MDMolecule!"); } currentResidue = null; } else { base.EndElement(xpath, element); } } else if (element.Name == XName_CML_atomArray) { if (xpath.Count == 2 && xpath.EndsWith("molecule", "atomArray")) { StoreAtomData(); NewAtomData(); } else if (xpath.Count > 2 && xpath.EndsWith("cml", "molecule", "atomArray")) { StoreAtomData(); NewAtomData(); } } else if (element.Name == XName_CML_bondArray) { if (xpath.Count == 2 && xpath.EndsWith("molecule", "bondArray")) { StoreBondData(); NewBondData(); } else if (xpath.Count > 2 && xpath.EndsWith("cml", "molecule", "bondArray")) { StoreBondData(); NewBondData(); } } else if (element.Name == XName_CML_scalar) { //Residue number if (string.Equals("md:resNumber", DictRef, StringComparison.Ordinal)) { int myInt = int.Parse(element.Value, NumberFormatInfo.InvariantInfo); currentResidue.SetNumber(myInt); } //ChargeGroup number else if (string.Equals("md:cgNumber", DictRef, StringComparison.Ordinal)) { int myInt = int.Parse(element.Value, NumberFormatInfo.InvariantInfo); currentChargeGroup.SetNumber(myInt); } } else { base.EndElement(xpath, element); } }
public override void StartElement(CMLStack xpath, XElement element) { string name = element.Name.LocalName; isELSYM = false; if (string.Equals("molecule", name, StringComparison.Ordinal)) { foreach (var attj in element.Attributes()) { Debug.WriteLine("StartElement"); Builtin = ""; DictRef = ""; foreach (var atti in element.Attributes()) { string qname = atti.Name.LocalName; switch (qname) { case "builtin": Builtin = atti.Value; Debug.WriteLine($"{name}->BUILTIN found: {atti.Value}"); break; case "dictRef": DictRef = atti.Value; Debug.WriteLine($"{name}->DICTREF found: {atti.Value}"); break; case "title": ElementTitle = atti.Value; Debug.WriteLine($"{name}->TITLE found: {atti.Value}"); break; default: Debug.WriteLine($"Qname: {qname}"); break; } } switch (attj.Name.LocalName) { case "convention": if (string.Equals(attj.Value, "PDB", StringComparison.Ordinal)) { // cdo.StartObject("PDBPolymer"); CurrentStrand = CurrentChemFile.Builder.NewStrand(); CurrentStrand.StrandName = "A"; CurrentMolecule = CurrentChemFile.Builder.NewPDBPolymer(); } break; case "dictRef": if (string.Equals(attj.Value, "pdb:sequence", StringComparison.Ordinal)) { NewSequence(); Builtin = ""; foreach (var atti in element.Attributes()) { if (string.Equals(atti.Name.LocalName, "id", StringComparison.Ordinal)) { // cdo.SetObjectProperty("Molecule", "id", atts.GetValue(i)); CurrentMolecule.Id = atti.Value; } else if (string.Equals(atti.Name.LocalName, "dictRef", StringComparison.Ordinal)) { // cdo.SetObjectProperty("Molecule", "dictRef", atts.GetValue(i)); // FIXME: has no equivalent in ChemFileCDO } } } break; case "title": switch (attj.Value) { case "connections": { // assume that Atom's have been read Debug.WriteLine("Assuming that Atom's have been read: storing them"); base.StoreAtomData(); connectionTable = true; Debug.WriteLine("Start Connection Table"); } break; case "connect": { Debug.WriteLine("New connection"); isBond = true; } break; default: break; } break; case "id": if (isBond) { connect_root = attj.Value; } break; default: // ignore other list items at this moment break; } } } else if (string.Equals("scalar", name, StringComparison.Ordinal)) { hasScalar = true; foreach (var atti in element.Attributes()) { if (string.Equals(atti.Name.LocalName, "dictRef", StringComparison.Ordinal)) { idValue = atti.Value; } } } else if (string.Equals("label", name, StringComparison.Ordinal)) { hasScalar = true; foreach (var atti in element.Attributes()) { if (string.Equals(atti.Name.LocalName, "dictRef", StringComparison.Ordinal)) { idValue = atti.Value; } } } else if (string.Equals("atom", name, StringComparison.Ordinal)) { base.StartElement(xpath, element); } }
public override void CharacterData(CMLStack xpath, XElement element) { string s = element.Value.Trim(); var st1tokens = s.Split(' '); string dictpdb = ""; foreach (var st1token in st1tokens) { dictpdb += st1token; dictpdb += " "; } if (dictpdb.Length > 0 && !idValue.Equals("pdb:record", StringComparison.Ordinal)) { dictpdb = dictpdb.Substring(0, dictpdb.Length - 1); } switch (idValue) { case "pdb:altLoc": altLocV.Add(dictpdb); break; case "pdb:chainID": chainIDV.Add(dictpdb); break; case "pdb:hetAtom": hetAtomV.Add(dictpdb); break; case "pdb:iCode": iCodeV.Add(dictpdb); break; case "pdb:name": nameV.Add(dictpdb); break; case "pdb:oxt": oxtV.Add(dictpdb); break; case "pdb:record": recordV.Add(dictpdb); break; case "pdb:resName": resNameV.Add(dictpdb); break; case "pdb:resSeq": resSeqV.Add(dictpdb); break; case "pdb:segID": segIDV.Add(dictpdb); break; case "pdb:serial": serialV.Add(dictpdb); break; case "pdb:tempFactor": tempFactorV.Add(dictpdb); break; } idValue = ""; if (isELSYM) { ElSym.Add(s); } else if (isBond) { Debug.WriteLine($"CD (bond): {s}"); if (connect_root.Length > 0) { var tokens = s.Split(' '); foreach (var atom in tokens) { if (!string.Equals(atom, "0", StringComparison.Ordinal)) { Debug.WriteLine("new bond: " + connect_root + "-" + atom); CurrentBond = CurrentMolecule.Builder.NewBond(CurrentMolecule.Atoms[int.Parse(connect_root, NumberFormatInfo.InvariantInfo) - 1], CurrentMolecule.Atoms[int.Parse(atom, NumberFormatInfo.InvariantInfo) - 1], BondOrder.Single); CurrentMolecule.Bonds.Add(CurrentBond); } } } } }
/// <summary> /// Add parsing of elements in mdmolecule: /// <list type="bullet"> /// <item>mdmolecule</item> /// <list type="bullet"> /// <item>chargeGroup</item> /// <list type="bullet"> /// <item>id</item> /// <item>cgNumber</item> /// <item>atomArray</item> /// <item>switchingAtom</item> /// </list> /// </list> /// <item>residue</item> /// <list type="bullet"> /// <item>id</item> /// <item>title</item> /// <item>resNumber</item> /// <item>atomArray</item> /// </list> /// </list> /// </summary> /// <param name="xpath"></param> /// <param name="element"></param> // @cdk.todo The JavaDoc of this class needs to be converted into HTML public override void StartElement(CMLStack xpath, XElement element) { // <molecule convention="md:mdMolecule" // xmlns="http://www.xml-cml.org/schema" // xmlns:md="http://www.bioclipse.org/mdmolecule"> // <atomArray> // <atom id="a1" elementType="C"/> // <atom id="a2" elementType="C"/> // </atomArray> // <molecule dictRef="md:chargeGroup" id="cg1"> // <scalar dictRef="md:cgNumber">5</scalar> // <atomArray> // <atom ref="a1"/> // <atom ref="a2"><scalar dictRef="md:switchingAtom"/></atom> // </atomArray> // </molecule> // <molecule dictRef="md:residue" id="r1" title="resName"> // <scalar dictRef="md:resNumber">3</scalar> // <atomArray> // <atom ref="a1"/> // <atom ref="a2"/> // </atomArray> // </molecule> // </molecule> // let the CMLCore convention deal with things first if (element.Name == XName_CML_molecule) { // the copy the parsed content into a new MDMolecule if (element.Attribute(Attribute_convention) != null && string.Equals(element.Attribute(Attribute_convention).Value, "md:mdMolecule", StringComparison.Ordinal)) { base.StartElement(xpath, element); CurrentMolecule = new MDMolecule(CurrentMolecule); } else { DictRef = element.Attribute(Attribute_dictRef) != null?element.Attribute(Attribute_dictRef).Value : ""; //If residue or chargeGroup, set up a new one switch (DictRef) { case "md:chargeGroup": currentChargeGroup = new ChargeGroup(); break; case "md:residue": currentResidue = new Residue(); if (element.Attribute(Attribute_title) != null) { currentResidue.Name = element.Attribute(Attribute_title).Value; } break; } } } else //We have a scalar element. Now check who it belongs to if (element.Name == XName_CML_scalar) { DictRef = element.Attribute(Attribute_dictRef).Value; //Switching Atom switch (DictRef) { case "md:switchingAtom": //Set current atom as switching atom currentChargeGroup.SetSwitchingAtom(CurrentAtom); break; default: base.StartElement(xpath, element); break; } } else if (element.Name == XName_CML_atom) { if (currentChargeGroup != null) { string id = element.Attribute(Attribute_ref).Value; if (id != null) { // ok, an atom is referenced; look it up CurrentAtom = null; foreach (var nextAtom in CurrentMolecule.Atoms) { if (string.Equals(nextAtom.Id, id, StringComparison.Ordinal)) { CurrentAtom = nextAtom; } } if (CurrentAtom == null) { Trace.TraceError($"Could not found the referenced atom '{id}' for this charge group!"); } else { currentChargeGroup.Atoms.Add(CurrentAtom); } } } else if (currentResidue != null) { string id = element.Attribute(Attribute_ref).Value; if (id != null) { // ok, an atom is referenced; look it up IAtom referencedAtom = null; foreach (var nextAtom in CurrentMolecule.Atoms) { if (string.Equals(nextAtom.Id, id, StringComparison.Ordinal)) { referencedAtom = nextAtom; } } if (referencedAtom == null) { Trace.TraceError($"Could not found the referenced atom '{id}' for this residue!"); } else { currentResidue.Atoms.Add(referencedAtom); } } } else { // ok, fine, just add it to the currentMolecule base.StartElement(xpath, element); } } else { base.StartElement(xpath, element); } }
public void CharacterData(CMLStack xpath, XElement element) { // ignore content from other namespaces }
/// <inheritdoc/> public void EndElement(CMLStack xpath, XElement element) { // ignore content from other namespaces }
public override void StartElement(CMLStack xpath, XElement element) { if (string.Equals("reaction", element.Name.LocalName, StringComparison.Ordinal)) { // cdo.StartObject("Reaction"); if (CurrentReactionSet == null) { CurrentReactionSet = CurrentChemFile.Builder.NewReactionSet(); } CurrentReaction = CurrentChemFile.Builder.NewReaction(); string id = AttGetValue(element.Attributes(), "id"); if (id != null) { CurrentReaction.Id = id; } // cdo.SetObjectProperty("Reaction", "id", id); } else if (string.Equals("reactionList", element.Name.LocalName, StringComparison.Ordinal)) { // cdo.StartObject("ReactionSet"); CurrentReactionSet = CurrentChemFile.Builder.NewReactionSet(); string id = AttGetValue(element.Attributes(), "id"); if (id != null) { CurrentReactionSet.Id = id; } // cdo.SetObjectProperty("reactionList", "id", id); } else if (string.Equals("reactant", element.Name.LocalName, StringComparison.Ordinal)) { // cdo.StartObject("Reactant"); if (CurrentReaction == null) { if (CurrentReactionSet == null) { CurrentReactionSet = CurrentChemFile.Builder.NewReactionSet(); } CurrentReaction = CurrentChemFile.Builder.NewReaction(); } CurrentMolecule = CurrentChemFile.Builder.NewAtomContainer(); #if DEBUG objectType = "Reactant"; #endif string id = AttGetValue(element.Attributes(), "id"); if (id != null) { CurrentMolecule.Id = id; } else { string ref_ = AttGetValue(element.Attributes(), "ref"); if (ref_ != null) { CurrentMolecule.Id = ref_; } } // cdo.SetObjectProperty("Reactant", "id", id); } else if (string.Equals("product", element.Name.LocalName, StringComparison.Ordinal)) { // cdo.StartObject("Product"); if (CurrentReaction == null) { if (CurrentReactionSet == null) { CurrentReactionSet = CurrentChemFile.Builder.NewReactionSet(); } CurrentReaction = CurrentChemFile.Builder.NewReaction(); } CurrentMolecule = CurrentChemFile.Builder.NewAtomContainer(); #if DEBUG objectType = "Product"; #endif string id = AttGetValue(element.Attributes(), "id"); if (id != null) { CurrentMolecule.Id = id; } else { string ref_ = AttGetValue(element.Attributes(), "ref"); if (ref_ != null) { CurrentMolecule.Id = ref_; } } // cdo.SetObjectProperty("Product", "id", id); } else if (string.Equals("substance", element.Name.LocalName, StringComparison.Ordinal)) { // cdo.StartObject("Agent"); if (CurrentReaction == null) { if (CurrentReactionSet == null) { CurrentReactionSet = CurrentChemFile.Builder.NewReactionSet(); } CurrentReaction = CurrentChemFile.Builder.NewReaction(); } CurrentMolecule = CurrentChemFile.Builder.NewAtomContainer(); #if DEBUG objectType = "Agent"; #endif string id = AttGetValue(element.Attributes(), "id"); if (id != null) { CurrentMolecule.Id = id; } else { string ref_ = AttGetValue(element.Attributes(), "ref"); if (ref_ != null) { CurrentMolecule.Id = ref_; } } // cdo.SetObjectProperty("Agent", "id", id); } else if (string.Equals("molecule", element.Name.LocalName, StringComparison.Ordinal)) { // clear existing molecule data base.NewMolecule(); string id = AttGetValue(element.Attributes(), "id"); if (id != null) { // check for existing molecule of that id IAtomContainer existing = GetMoleculeFromID(CurrentMoleculeSet, id); if (existing != null) { CurrentMolecule = existing; } else { CurrentMolecule.Id = id; } } else { string ref_ = AttGetValue(element.Attributes(), "ref"); if (ref_ != null) { IAtomContainer atomC = GetMoleculeFromID(CurrentMoleculeSet, ref_); // if there was no molecule create a new one for the reference. this // happens when the reaction is defined before the molecule set if (atomC == null) { atomC = CurrentChemFile.Builder.NewAtomContainer(); atomC.Id = ref_; CurrentMoleculeSet.Add(atomC); } base.CurrentMolecule = atomC; } } } else { base.StartElement(xpath, element); } }
public override void CharacterData(CMLStack xpath, XElement element) { string s = element.Value.Trim(); Debug.WriteLine($"Start PMP chardata ({CurrentElement}) :{s}"); Debug.WriteLine($" ElTitle: {ElementTitle}"); if (xpath.ToString().EndsWith("string/", StringComparison.Ordinal) && Builtin.Equals("spacegroup", StringComparison.Ordinal)) { string sg = "P1"; // standardize space group names (see Crystal.java) if (string.Equals("P 21 21 21 (1)", s, StringComparison.Ordinal)) { sg = "P 2_1 2_1 2_1"; } ((ICrystal)CurrentMolecule).SpaceGroup = sg; } else if (xpath.ToString().EndsWith("floatArray/", StringComparison.Ordinal)) { switch (ElementTitle) { case "a": case "b": case "c": var tokens = s.Split(' '); if (tokens.Length > 2) { if (string.Equals(ElementTitle, "a", StringComparison.Ordinal)) { ((ICrystal)CurrentMolecule).A = new Vector3( double.Parse(tokens[0], NumberFormatInfo.InvariantInfo), double.Parse(tokens[1], NumberFormatInfo.InvariantInfo), double.Parse(tokens[2], NumberFormatInfo.InvariantInfo)); } else if (string.Equals(ElementTitle, "b", StringComparison.Ordinal)) { ((ICrystal)CurrentMolecule).B = new Vector3( double.Parse(tokens[0], NumberFormatInfo.InvariantInfo), double.Parse(tokens[1], NumberFormatInfo.InvariantInfo), double.Parse(tokens[2], NumberFormatInfo.InvariantInfo)); } else if (string.Equals(ElementTitle, "c", StringComparison.Ordinal)) { ((ICrystal)CurrentMolecule).C = new Vector3( double.Parse(tokens[0], NumberFormatInfo.InvariantInfo), double.Parse(tokens[1], NumberFormatInfo.InvariantInfo), double.Parse(tokens[2], NumberFormatInfo.InvariantInfo)); } } else { Debug.WriteLine("PMP Convention error: incorrect number of cell axis fractions!"); } break; } } else { base.CharacterData(xpath, element); } Debug.WriteLine("End PMP chardata"); }