diff --git a/LipidCreator/StructureEditor/LipidStructure.cs b/LipidCreator/StructureEditor/LipidStructure.cs index ab5b447..e5db56f 100644 --- a/LipidCreator/StructureEditor/LipidStructure.cs +++ b/LipidCreator/StructureEditor/LipidStructure.cs @@ -82,6 +82,11 @@ public SPointF(Point p) return new SPointF(p.X - r.X, p.Y - r.Y); } + public static SPointF operator /(SPointF p, float f) + { + return new SPointF(p.X / f, p.Y / f); + } + public PointF pF() { return new PointF(X, Y); @@ -94,7 +99,8 @@ public Point pI() } - public class S { + public class S + { public static string space(int n) { return new string(' ', 2 * n); @@ -362,6 +368,7 @@ public Bond(Bond copy) public Bond(LipidStructure _lipidStructure, XElement element) { + lipidStructure = _lipidStructure; id = Convert.ToInt32(element.Attribute("id").Value); startId = Convert.ToInt32(element.Attribute("start").Value); endId = Convert.ToInt32(element.Attribute("end").Value); @@ -386,7 +393,7 @@ public Bond(LipidStructure _lipidStructure, XElement element) public void toggleState() { - if (lipidStructure.currentProjection == 0) return; + if (lipidStructure.currentProjection == 0 || !bondProjections.ContainsKey(lipidStructure.currentProjection)) return; bondProjections[lipidStructure.currentProjection] = !bondProjections[lipidStructure.currentProjection]; lipidStructure.countNodeConnections(); @@ -438,6 +445,13 @@ public LipidStructureFragment(int i, string f, int c = 0) charge = c; } + public LipidStructureFragment(XElement element) + { + id = Convert.ToInt32(element.Attribute("id").Value); + fragmentName = element.Attribute("name").Value; + charge = Convert.ToInt32(element.Attribute("charge").Value); + } + public void toggleCharge() { charge = (charge >= maxCharge) ? minCharge : charge + 1; @@ -483,8 +497,8 @@ public class LipidStructure public HashSet specialAtoms = new HashSet(){"N", "O", "P", "S"}; public Dictionary freeElectrons = new Dictionary(){{"C", 4}, {"N", 3}, {"O", 2}, {"P", 3}, {"S", 2}}; public int projectionIds = 1; - public int currentNodeId = 0; - public int bondIDs = 0; + public int nodeIds = 0; + public int bondIds = 0; @@ -525,20 +539,28 @@ public LipidStructure(string file_name, Form form) 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); + if (documentType.Equals("CDXML")) + { + loadCDXML(doc); + } + else if (documentType.Equals("LipidStructure")) + { + ((LipidCreatorStructureEditor)form).structureFile = file_name; + loadLipidStructure(doc); + } + else + { + throw new Exception("Unknown data format"); + } currentProjection = 0; computeBonds(); changeFragment(); countNodeConnections(); + + penEnabled = new Pen(Color.Black, factor); + penDisabled = new Pen(Color.FromArgb(180, 180, 180), factor); + penHidden = new Pen(Color.FromArgb(220, 220, 220), factor); } @@ -555,6 +577,7 @@ public void loadLipidStructure(XDocument doc) StructureNode sn = new StructureNode(this, element); idToNode.Add(sn.id, sn); nodes.Add(sn); + nodeIds = Math.Max(nodeIds, sn.id + 1); } foreach (var element in doc.Element("LipidStructure").Elements().Where(el => el.Name.LocalName.Equals("AdditionalNodes")).Elements().Where(el => el.Name.LocalName.Equals("NodeID"))) @@ -577,6 +600,26 @@ public void loadLipidStructure(XDocument doc) int id = Convert.ToInt32(element.Attribute("id").Value); additionalBonds.Add(bond_dict[id]); } + + + + foreach (var element in doc.Element("LipidStructure").Elements().Where(el => el.Name.LocalName.Equals("PositiveFragments")).Elements().Where(el => el.Name.LocalName.Equals("LipidStructureFragment"))) + { + LipidStructureFragment fragment = new LipidStructureFragment(element); + positiveFragments.Add(fragment.fragmentName, fragment); + } + + foreach (var element in doc.Element("LipidStructure").Elements().Where(el => el.Name.LocalName.Equals("NegativeFragments")).Elements().Where(el => el.Name.LocalName.Equals("LipidStructureFragment"))) + { + LipidStructureFragment fragment = new LipidStructureFragment(element); + negativeFragments.Add(fragment.fragmentName, fragment); + } + + nodeFont = new Font("Arial", fontSize * factor); + decoratorFont = new Font("Arial", (float)(fontSize * factor * 0.5)); + + bondIds = bonds.Count + 1; + projectionIds = negativeFragments.Count + positiveFragments.Count + 1; } @@ -645,7 +688,7 @@ public void loadCDXML(XDocument doc) idToNode.Add(nodeId, sn); } - currentNodeId = Math.Max(currentNodeId, nodeId); + nodeIds = Math.Max(nodeIds, nodeId + 1); } catch(Exception ex){ Console.WriteLine(ex); @@ -697,7 +740,7 @@ public void loadCDXML(XDocument doc) // get the edge color bool isDoubleBond = ((string)edge.Attribute("Order") != null) && edge.Attribute("Order").Value.ToString().Equals("2"); - bond = new Bond(this, bondIDs++, idB, idE, isDoubleBond); + bond = new Bond(this, bondIds++, idB, idE, isDoubleBond); bonds.Add(bond); } catch(Exception){} @@ -728,6 +771,9 @@ public void loadCDXML(XDocument doc) 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; @@ -976,7 +1022,7 @@ public void addNode(SPointF pos) float w = size.Width * 0.9f; float h = size.Height * 0.9f; RectangleF drawRect = new RectangleF(pos.X - w * 0.5f, pos.Y - h * 0.5f, w, h); - StructureNode sn = new StructureNode(this, ++currentNodeId, pos, drawRect, "C"); + StructureNode sn = new StructureNode(this, nodeIds, pos, drawRect, "C"); int fragId = currentFragment.id; @@ -984,7 +1030,7 @@ public void addNode(SPointF pos) nodes.Add(sn); additionalNodes.Add(sn); - idToNode.Add(currentNodeId, sn); + idToNode.Add(nodeIds++, sn); computeBonds(); } @@ -1012,7 +1058,7 @@ public StructureNode removeNode(StructureNode node) public void addBond(StructureNode start, StructureNode end) { if (currentProjection == 0) return; - Bond bond = new Bond(this, bondIDs++, start.id, end.id, false); + Bond bond = new Bond(this, bondIds++, start.id, end.id, false); bond.bondProjections.Add(currentProjection, false); bonds.Add(bond); additionalBonds.Add(bond); diff --git a/LipidCreator/StructureEditor/StructureEditor.Designer.cs b/LipidCreator/StructureEditor/StructureEditor.Designer.cs index 09e40cb..b27f515 100644 --- a/LipidCreator/StructureEditor/StructureEditor.Designer.cs +++ b/LipidCreator/StructureEditor/StructureEditor.Designer.cs @@ -82,6 +82,7 @@ partial class LipidCreatorStructureEditor public Button actionRemoveBond = new Button(); public Button actionFinalView = new Button(); public Button actionMoveAtom = new Button(); + public Button actionSaveStructure = new Button(); public List