From 66a545adcfb8bb5699a8b4fcadf44d12ca131489 Mon Sep 17 00:00:00 2001 From: Dominik Kopczynski Date: Wed, 29 Nov 2023 14:55:07 +0100 Subject: [PATCH] import almost done --- .../StructureEditor/LipidStructure.cs | 174 +++++++++++++++--- .../StructureEditor/StructureEditor.cs | 51 +++-- 2 files changed, 189 insertions(+), 36 deletions(-) diff --git a/LipidCreator/StructureEditor/LipidStructure.cs b/LipidCreator/StructureEditor/LipidStructure.cs index 60fe97e..ab5b447 100644 --- a/LipidCreator/StructureEditor/LipidStructure.cs +++ b/LipidCreator/StructureEditor/LipidStructure.cs @@ -120,6 +120,16 @@ public NodeProjection(NodeProjection copy) previousShift = new SPointF(shift.X, shift.Y); } + + public NodeProjection(XElement element) + { + shift = new SPointF(Convert.ToInt32(element.Attribute("x").Value), Convert.ToInt32(element.Attribute("y").Value)); + string state = element.Attribute("state").Value; + if (state.Equals("Enabled")) nodeState = NodeState.Enabled; + else if (state.Equals("Disabled")) nodeState = NodeState.Disabled; + else nodeState = NodeState.Hidden; + } + public void toggleState(){ switch(nodeState) { @@ -137,7 +147,7 @@ public void prepareMove() public void serialize(StringBuilder sb, int id, int tabs = 0) { - sb.Append(String.Format(S.space(tabs) + "\n", id, shift.X, shift.Y, nodeState)); + sb.Append(String.Format(S.space(tabs) + "\n", id, shift.X, shift.Y, nodeState)); } } @@ -171,6 +181,31 @@ public StructureNode(LipidStructure _lipidStructure, int _id, SPointF p, Rectang } + public StructureNode(LipidStructure _lipidStructure, XElement element) + { + lipidStructure = _lipidStructure; + id = Convert.ToInt32(element.Attribute("id").Value); + text = element.Attribute("text").Value; + isCarbon = text.Equals("C"); + position = new SPointF(Convert.ToSingle(element.Attribute("x").Value), Convert.ToSingle(element.Attribute("y").Value)); + boundingBox = new RectangleF(Convert.ToSingle(element.Attribute("bx").Value), Convert.ToSingle(element.Attribute("by").Value), Convert.ToSingle(element.Attribute("bw").Value), Convert.ToSingle(element.Attribute("bh").Value)); + + + + foreach (var decorator in element.Elements().Where(el => el.Name.LocalName.Equals("Decorators")).Elements().Where(el => el.Name.LocalName.Equals("StructureNode"))) + { + decorators.Add(new StructureNode(lipidStructure, decorator)); + } + + foreach (var np in element.Elements().Where(el => el.Name.LocalName.Equals("NodeProjections")).Elements().Where(el => el.Name.LocalName.Equals("NodeProjection"))) + { + int npId = Convert.ToInt32(np.Attribute("id").Value); + NodeProjection nodeProjection = new NodeProjection(np); + nodeProjections.Add(npId, nodeProjection); + } + } + + public void prepareMove() { if (lipidStructure.currentProjection == 0) return; @@ -234,8 +269,8 @@ public void toggleAtom() public void serialize(StringBuilder sb, int tabs = 0) { - sb.Append(String.Format(S.space(tabs) + "\n", text)); if (decorators.Count > 0) { @@ -273,9 +308,15 @@ public StructureEdge(StructureEdge copy) end = new SPointF(copy.end.X, copy.end.Y); } + public StructureEdge(XElement element) + { + start = new SPointF(Convert.ToSingle(element.Attribute("sx").Value), Convert.ToSingle(element.Attribute("sy").Value)); + end = new SPointF(Convert.ToSingle(element.Attribute("ex").Value), Convert.ToSingle(element.Attribute("ey").Value)); + } + public void serialize(StringBuilder sb, int tabs = 0) { - sb.Append(S.space(tabs) + String.Format("\n", start.X, start.Y, end.X, end.Y)); + sb.Append(S.space(tabs) + String.Format("\n", start.X, start.Y, end.X, end.Y)); } } @@ -286,7 +327,7 @@ public class Bond public int startId; public int endId; public bool isDoubleBond = false; - public StructureEdge edgeSingle; + public StructureEdge edgeSingle = null; public List edgesDouble = new List(); public Dictionary bondProjections = new Dictionary(); public LipidStructure lipidStructure; @@ -306,7 +347,7 @@ public Bond(Bond copy) startId = copy.startId; endId = copy.endId; isDoubleBond = copy.isDoubleBond; - if (copy.edgeSingle != null) edgeSingle = new StructureEdge(copy.edgeSingle); + edgeSingle = new StructureEdge(copy.edgeSingle); foreach (var edge in copy.edgesDouble) { edgesDouble.Add(new StructureEdge(edge)); @@ -319,6 +360,30 @@ public Bond(Bond copy) } + public Bond(LipidStructure _lipidStructure, XElement element) + { + id = Convert.ToInt32(element.Attribute("id").Value); + startId = Convert.ToInt32(element.Attribute("start").Value); + endId = Convert.ToInt32(element.Attribute("end").Value); + isDoubleBond = element.Attribute("db").Value.Equals("True"); + + foreach (var singleEdge in element.Elements().Where(el => el.Name.LocalName.Equals("EdgeSingle")).Elements().Where(el => el.Name.LocalName.Equals("StructureEdge"))) + { + edgeSingle = new StructureEdge(singleEdge); + } + + foreach (var doubleEdge in element.Elements().Where(el => el.Name.LocalName.Equals("EdgesDouble")).Elements().Where(el => el.Name.LocalName.Equals("StructureEdge"))) + { + edgesDouble.Add(new StructureEdge(doubleEdge)); + } + + foreach (var bp in element.Elements().Where(el => el.Name.LocalName.Equals("BondProjections")).Elements().Where(el => el.Name.LocalName.Equals("BP"))) + { + bondProjections.Add(Convert.ToInt32(bp.Attribute("id").Value), bp.Attribute("enabled").Value.Equals("True")); + } + } + + public void toggleState() { if (lipidStructure.currentProjection == 0) return; @@ -329,7 +394,7 @@ public void toggleState() public void serialize(StringBuilder sb, int tabs = 0) { - sb.Append(S.space(tabs) + String.Format("\n", id, startId, endId, isDoubleBond)); + sb.Append(S.space(tabs) + String.Format("\n", id, startId, endId, isDoubleBond)); if (edgeSingle != null) { sb.Append(S.space(tabs + 1) + "\n"); @@ -347,7 +412,7 @@ public void serialize(StringBuilder sb, int tabs = 0) if (bondProjections.Count > 0) { sb.Append(S.space(tabs + 1) + "\n"); - foreach (var kvp in bondProjections) sb.Append(S.space(tabs + 2) + String.Format("\n", kvp.Key, kvp.Value)); + foreach (var kvp in bondProjections) sb.Append(S.space(tabs + 2) + String.Format("\n", kvp.Key, kvp.Value)); sb.Append(S.space(tabs + 1) + "\n"); } @@ -380,7 +445,7 @@ public void toggleCharge() public void serialize(StringBuilder sb, int tabs = 0) { - sb.Append(String.Format(S.space(tabs) + "\n", id, fragmentName, charge)); + sb.Append(String.Format(S.space(tabs) + "\n", id, fragmentName, charge)); } } @@ -394,8 +459,8 @@ public class LipidStructure public HashSet additionalBonds = new HashSet(); public Dictionary positiveFragments = new Dictionary(); public Dictionary negativeFragments = new Dictionary(); - public SPointF middlePoint; + public SPointF middlePoint; public Dictionary idToNode = new Dictionary(); public float factor = 2.5f; public float dbSpace = 1.5f; @@ -422,6 +487,9 @@ public class LipidStructure public int bondIDs = 0; + + + public void parseXML(XElement element, List nodes, List edges) { foreach (var fragment in element.Elements().Where(el => el.Name.LocalName.Equals("fragment"))) @@ -444,6 +512,8 @@ public void parseXML(XElement element, List nodes, List edge + + public LipidStructure(string file_name, Form form) { nodeFont = new Font("Arial", fontSize); @@ -453,7 +523,69 @@ public LipidStructure(string file_name, Form form) XDocument doc = XDocument.Load(file_name); graphics = form.CreateGraphics(); + + string documentType = doc.Elements().First().Name.LocalName; + if (documentType.Equals("CDXML")) loadCDXML(doc); + else if (documentType.Equals("LipidStructure")) loadLipidStructure(doc); + else throw new Exception("Unknown data format"); + + nodeFont = new Font("Arial", fontSize * factor); + decoratorFont = new Font("Arial", (float)(fontSize * factor * 0.5)); + penEnabled = new Pen(Color.Black, factor); + penDisabled = new Pen(Color.FromArgb(180, 180, 180), factor); + penHidden = new Pen(Color.FromArgb(220, 220, 220), factor); + currentProjection = 0; + computeBonds(); + changeFragment(); + countNodeConnections(); + } + + + + + + + public void loadLipidStructure(XDocument doc) + { + Dictionary bond_dict = new Dictionary(); + + foreach (var element in doc.Element("LipidStructure").Elements().Where(el => el.Name.LocalName.Equals("Nodes")).Elements().Where(el => el.Name.LocalName.Equals("StructureNode"))) + { + StructureNode sn = new StructureNode(this, element); + idToNode.Add(sn.id, sn); + nodes.Add(sn); + } + + foreach (var element in doc.Element("LipidStructure").Elements().Where(el => el.Name.LocalName.Equals("AdditionalNodes")).Elements().Where(el => el.Name.LocalName.Equals("NodeID"))) + { + int id = Convert.ToInt32(element.Attribute("id").Value); + additionalNodes.Add(idToNode[id]); + } + + + + foreach (var element in doc.Element("LipidStructure").Elements().Where(el => el.Name.LocalName.Equals("Bonds")).Elements().Where(el => el.Name.LocalName.Equals("Bond"))) + { + Bond bond = new Bond(this, element); + bond_dict.Add(bond.id, bond); + bonds.Add(bond); + } + + foreach (var element in doc.Element("LipidStructure").Elements().Where(el => el.Name.LocalName.Equals("AdditionalBonds")).Elements().Where(el => el.Name.LocalName.Equals("BondID"))) + { + int id = Convert.ToInt32(element.Attribute("id").Value); + additionalBonds.Add(bond_dict[id]); + } + } + + + + + + + public void loadCDXML(XDocument doc) + { List xnodes = new List(); List xedges = new List(); foreach (var pages in doc.Element("CDXML").Elements().Where(el => el.Name.LocalName.Equals("page"))) @@ -596,8 +728,6 @@ public LipidStructure(string file_name, Form form) float offsetY = /* (float)form.Size.Height / 2.0f */ - midY * factor; - nodeFont = new Font("Arial", fontSize * factor); - decoratorFont = new Font("Arial", (float)(fontSize * factor * 0.5)); foreach (var node in nodes) { float x = node.position.X * factor + offsetX; @@ -628,10 +758,6 @@ public LipidStructure(string file_name, Form form) } } - penEnabled = new Pen(Color.Black, factor); - penDisabled = new Pen(Color.FromArgb(180, 180, 180), factor); - penHidden = new Pen(Color.FromArgb(220, 220, 220), factor); - foreach (var node in nodes) { @@ -639,11 +765,6 @@ public LipidStructure(string file_name, Form form) foreach (var decorator in node.decorators) decorator.nodeProjections.Add(0, new NodeProjection()); } foreach (var bond in bonds) bond.bondProjections.Add(0, bond.isDoubleBond); - currentProjection = 0; - - computeBonds(); - changeFragment(); - countNodeConnections(); } @@ -658,6 +779,7 @@ public void setMiddlePoint(SPointF p) public void serialize(string file_name) { StringBuilder sb = new StringBuilder(); + sb.Append("\n"); sb.Append("\n"); // add node information @@ -669,7 +791,7 @@ public void serialize(string file_name) if (additionalNodes.Count > 0) { sb.Append(" \n"); - foreach (var node in additionalNodes) sb.Append(String.Format(" \n", node.id)); + foreach (var node in additionalNodes) sb.Append(String.Format(" \n", node.id)); sb.Append(" \n"); } @@ -682,7 +804,7 @@ public void serialize(string file_name) if (additionalBonds.Count > 0) { sb.Append(" \n"); - foreach (var bond in additionalBonds) sb.Append(String.Format(" \n", bond.id)); + foreach (var bond in additionalBonds) sb.Append(String.Format(" \n", bond.id)); sb.Append(" \n"); } @@ -1074,6 +1196,7 @@ public void drawNodes(Graphics g) int minY = (1 << 30); int maxY = -(1 << 30); + foreach (var node in nodes) { if (!node.nodeProjections.ContainsKey(currentProjection)) continue; @@ -1087,7 +1210,10 @@ public void drawNodes(Graphics g) else if (nodeProjection.nodeState == NodeState.Disabled) brush = solidBrushDisabled; RectangleF r = new RectangleF(nodeSPoint.X, nodeSPoint.Y, node.boundingBox.Width, node.boundingBox.Height); - if (!node.isCarbon || developmentView) g.DrawString(node.text, nodeFont, brush, r, drawFormat); + if (!node.isCarbon || developmentView) + { + g.DrawString(node.text, nodeFont, brush, r, drawFormat); + } if (nodeProjection.nodeState == NodeState.Enabled) { minX = Math.Min(minX, (int)nodeSPoint.X); diff --git a/LipidCreator/StructureEditor/StructureEditor.cs b/LipidCreator/StructureEditor/StructureEditor.cs index 1eac490..6aa62a5 100644 --- a/LipidCreator/StructureEditor/StructureEditor.cs +++ b/LipidCreator/StructureEditor/StructureEditor.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Xml.Linq; using System.Linq; +using System.IO; using System.Text; using System.Drawing.Drawing2D; @@ -38,23 +39,49 @@ public LipidCreatorStructureEditor() InitializeComponent(); this.DoubleBuffered = true; - string file = "/home/dominik/workspace/src/LipidCreator/LipidCreator/data/structures/PC genNeg.cdxml"; - lipidStructure = new LipidStructure(file, this); + /* + using (OpenFileDialog openFileDialog = new OpenFileDialog()) + { + openFileDialog.InitialDirectory = "c:\\"; + openFileDialog.Filter = "txt files (*.cdxml)|*.cdxml|(*.stXL)|*.stXL"; + openFileDialog.FilterIndex = 2; + openFileDialog.RestoreDirectory = true; + + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + //Get the path of specified file + string filePath = openFileDialog.FileName; + + + Console.WriteLine(filePath); + } + } + */ - positiveFragmentsListBox.Items.Add("HG 164"); - lipidStructure.addFragment("HG 164", true); - positiveFragmentsListBox.Items.Add("HG 181"); - lipidStructure.addFragment("HG 181", true); + if (true) + { + string file = "foo.stXL"; + lipidStructure = new LipidStructure(file, this); + } + else + { + string file = "/home/dominik/workspace/src/LipidCreator/LipidCreator/data/structures/PC genNeg.cdxml"; + lipidStructure = new LipidStructure(file, this); + positiveFragmentsListBox.Items.Add("HG 164"); + lipidStructure.addFragment("HG 164", true); + + positiveFragmentsListBox.Items.Add("HG 181"); + lipidStructure.addFragment("HG 181", true); + + negativeFragmentsListBox.Items.Add("HG -OH"); + lipidStructure.addFragment("HG -OH", false); + - negativeFragmentsListBox.Items.Add("HG -OH"); - lipidStructure.addFragment("HG -OH", false); + lipidStructure.serialize("foo.stXL"); + } computeFragmentMass(null, null); - - - - lipidStructure.serialize("foo.stXL"); }