Example #1
0
        public void generateGraph(long structureid, Graph graph)
        {
            GraphVizEngine structuresGraph = new GraphVizEngine();

            structuresGraph.createUndirectedGraph(structureid.ToString());

            long factor = 15000;

            //FileStream fs = new FileStream(workDirectory+"\\Files\\"+structureid+".dot", FileMode.Create, FileAccess.Write);
            //StreamWriter sw = new StreamWriter(fs);
            //sw.Write("graph " + structureid + "{\n");
            //StringBuilder level = new StringBuilder();
            double maxX = 0.0, maxZ = 0.0;
            foreach (KeyValuePair<long, Node> node in graph.Nodes)
            {
                if (maxX < node.Value.position.X)
                    maxX = node.Value.position.X;
                if (maxZ < node.Value.position.Z)
                    maxZ = node.Value.position.Z;

            }

            Console.WriteLine("MaxX =" + maxX);
            Console.WriteLine("MaxZ =" + maxZ);

            structuresGraph.graphAttribites.Add("size", Convert.ToInt32(maxZ / (300 * 5)) + "," + Convert.ToInt32(maxX / (300 * 5)) + "!");

            // sw.Write("graph[size=\"" + Convert.ToInt32(maxZ / (300 * 5)) + "," + Convert.ToInt32(maxX / (300 * 5)) + "!\"];\n");
            // sw.Write("edge[decorate=false];\n");
            // sw.Write("node[fontcolor=white]\n;");
            string color = "green";
            foreach (KeyValuePair<long, Node> node in graph.Nodes)
            {
                Nodes tmpNode = structuresGraph.addNode(node.Value.ID);
                int radii = Convert.ToInt32(Math.Ceiling(node.Value.radius / (300)));

                tmpNode.nodeAttributes.Add("style", "filled");
                tmpNode.nodeAttributes.Add("penwidth", "9.0");
                tmpNode.nodeAttributes.Add("fillcolor", color);
                tmpNode.nodeAttributes.Add("pos", (Convert.ToInt32(Math.Ceiling(node.Value.position.X / 300)) + "," + Convert.ToInt32((maxZ - node.Value.position.Z) / 300) +
                    "!").ToString());
                tmpNode.nodeAttributes.Add("shape", "box");
                tmpNode.nodeAttributes.Add("width", radii.ToString());
                tmpNode.nodeAttributes.Add("height", (radii / 4).ToString());
                tmpNode.nodeAttributes.Add("tooltip", node.Value.ID.ToString());
                tmpNode.nodeAttributes.Add("URL", "#");

                //sw.Write(node.Value.ID + "[label=\"" + node.Value.ID + "\",style=filled,penwidth=\"9.0\",color=black,fillcolor=" + color + ",pos=\"" + Convert.ToInt32(Math.Ceiling(node.Value.position.X / 300)) + "," + Convert.ToInt32((maxZ - node.Value.position.Z) / 300) +
                //    "!\",shape=circle,width=\"" + radii + "\",href=\"http://www.google.com\"];\n");

            }

            List<string> edgeList = new List<string>();

            foreach (Edge edge in graph.Edges)
            {
                Edges tmpEdge = new Edges();
                structuresGraph.addEdge(tmpEdge);
                tmpEdge.from = edge.A;
                tmpEdge.to = edge.B;
                tmpEdge.edgeAttributes.Add("label", Math.Round(edge.distance, 2).ToString() + "nm");
                tmpEdge.edgeAttributes.Add("style", "setlinewidth(5)");
                tmpEdge.edgeAttributes.Add("href", "#");
                tmpEdge.edgeAttributes.Add("fontsize", "30");

                //sw.Write(edge.A + "--" + edge.B + "[label = \"" + Math.Round(edge.distance, 2) + "nm\",style=\"setlinewidth(5)\",href=\".\",fontsize=30];\n");

            }

            string workingDirectory = Server.MapPath("~");
            ViewData["workDirectory"] = workingDirectory;

            string localDir = ViewData["workDirectory"] + "\\Files\\" + HttpContext.User.Identity.Name + "\\";
            string fileDir = localDir + structureid;
            structuresGraph.completePath_local = fileDir;
            ViewData["username"] = HttpContext.User.Identity.Name;

            string svgfile = fileDir + ".svg";
            string virtualRoot = ViewData["virtualRoot"].ToString();

            structuresGraph.completePath_URL = virtualRoot + "/Files/" + HttpContext.User.Identity.Name +"/" + structureid;

            structuresGraph.virtualRoot = virtualRoot;

            if (!System.IO.Directory.Exists(localDir))
                System.IO.Directory.CreateDirectory(localDir);

            GraphJSON sendGraph = new GraphJSON();

            foreach (KeyValuePair<long, Node> node in graph.Nodes)
            {
                NodeJSON tempNode = new NodeJSON();
                tempNode.ID = node.Value.ID;
                tempNode.radius = node.Value.radius / factor;
                tempNode.location[0] = node.Value.position.X / factor;
                tempNode.location[1] = node.Value.position.Y / factor;
                tempNode.location[2] = node.Value.position.Z / factor;
                sendGraph.Nodes.Add(tempNode);
            }

            sendGraph.Edges = graph.Edges;

            sendGraph.Synapses = graph.Synapses;

            using (FileStream fs = new FileStream(fileDir + ".json", FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    JavaScriptSerializer oSerializer = new JavaScriptSerializer();
                    sw.Write(oSerializer.Serialize(sendGraph));
                    sw.Close();
                }
                fs.Close();
            }

            structuresGraph.outputFormats.Add("svg");

            structuresGraph.layout = "dot";

            structuresGraph.Output();

            //Add scripts to svg
            if (System.IO.File.Exists(svgfile))
            {
                StringBuilder contents;
                using (FileStream file = new FileStream(svgfile, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    using (StreamReader sr = new StreamReader(file))
                    {
                        contents = new StringBuilder(sr.ReadToEnd());
                    }
                }

                System.IO.File.Delete(svgfile);

                string searchfor = "http://www.w3.org/1999/xlink\">";
                contents.Replace(searchfor, searchfor +
                    "\n<script xlink:href=\"" + virtualRoot + "/Scripts/SVGzoom.js\"/>\n<script xlink:href=\"" + virtualRoot + "/Scripts/effect.js\"/>");

                using (FileStream fl = new FileStream(svgfile, FileMode.Create))
                {
                    using (StreamWriter write = new StreamWriter(fl))
                    {
                        write.Write(contents.ToString());
                    }
                }

                //FileStream f2 = new FileStream(fileDir + "changed.txt", FileMode.Create);
                //StreamWriter wrr = new StreamWriter(f2);
                //wrr.Write(contents.ToString());
                //wrr.Close();
                //f2.Close();
            }
        }
Example #2
0
        public Graph computeGraph(long structureid)
        {
            Graph graph = new Graph();

            List<long> LocationIds = new List<long>();
            LocationIds.Add(structureid);

            Structure structure = null;
            Location[] results = new Location[0];
            using (AnnotateStructuresClient proxyStructures = State.CreateStructureClient())
            {

                structure = proxyStructures.GetStructureByID(structureid, false);

                using (AnnotateLocationsClient proxyLocations = State.CreateLocationsClient())
                {

                    results = proxyLocations.GetLocationsForStructure(structureid);
                }

                proxyStructures.Close();
            }

            using (AnnotateStructureTypesClient proxyStructureType = State.CreateStructureTypeClient())
            {

                StructureType type = proxyStructureType.GetStructureTypeByID(structure.TypeID);
                if(type != null)
                    graph.DefaultNodeColor = type.Color;
                proxyStructureType.Close();

            }

            foreach (Location location in results)
            {
                AnnotationPoint p = new AnnotationPoint();

                p.X = location.VolumePosition.X * 2.18;
                p.Y = location.VolumePosition.Y * 2.18;
                p.Z = location.VolumePosition.Z * 90;

                location.Radius *= 2.18;

                location.VolumePosition = p;

                Node node = new Node(location);

                graph.Nodes.Add(location.ID, node);
            }

            graph.originalNodes = new Dictionary<long, Node>(graph.Nodes);

            graph.createEdges();

            graph.generateStats(Convert.ToInt32(structureid));

            //FileStream temp = new FileStream("E:\\src\\info.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            //StreamWriter wr = new StreamWriter(temp);
            //wr.WriteLine(proxy.State);
            //wr.Close(); temp.Close();

            graph.reduceEdges();

            graph.createEdgeList();

            graph.createSynapses(Convert.ToInt32(structureid));

            return graph;
        }
Example #3
0
        public ActionResult Locations()
        {
            int cellID;

            if (!String.IsNullOrEmpty(Request["ctl00$MainContent$structureID"]) && Int32.Parse(Request["ctl00$MainContent$structureID"]) != 0)
            {
                cellID = Convert.ToInt32(Request["ctl00$MainContent$structureID"]);
            }
            else
            {
                cellID = Convert.ToInt32(Request["cellID"]);
            }

            ViewData["cellID"] = cellID;

            State.selectedService = Request["ctl00$MainContent$dataSource"];

            State.selectedLab = Request["ctl00$MainContent$labName"];

            ViewData["dataSource"] = State.selectedService;

            //Map the virtual path of application
            string applicationPath = HttpContext.Request.ApplicationPath;
            if (applicationPath == "/")
                applicationPath = "";

            string workDirectory = Server.MapPath("~");
            ViewData["workDirectory"] = workDirectory;

            string virtualRoot = "http://" + HttpContext.Request.Url.Authority + applicationPath;
            ViewData["virtualRoot"] = virtualRoot;

            ViewData["structureid"] = cellID;

            Graph graph = new Graph();

            graph = computeGraph(cellID);

            graph.volumeToSurfaceRatio = Math.Round((graph.cellSurfaceArea / Math.Pow(10, 6)) / (graph.cellVolume / Math.Pow(10, 9)), 3);
            generateGraph(cellID, graph);

            ViewData["area"] = Math.Round(graph.cellSurfaceArea / Math.Pow(10, 6), 3);
            ViewData["volume"] = Math.Round(graph.cellVolume / Math.Pow(10, 9), 3);
            ViewData["ratio"] = graph.volumeToSurfaceRatio;

            // If 3D is requested, store structureID and return View
            if (Request["group2"] == "3D")
            {

                string fileDir = workDirectory + "\\Files\\" + HttpContext.User.Identity.Name;

                if (!System.IO.Directory.Exists(fileDir))
                    System.IO.Directory.CreateDirectory(fileDir);

                string filepath = fileDir + "\\Structure3D.xml";

                if (System.IO.File.Exists(filepath))
                {
                    System.IO.File.Delete(filepath);
                }

                using (FileStream fs = new FileStream(filepath, FileMode.Create, FileAccess.Write))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.Write("<Structure3D>");
                        sw.Write(State.selectedLab + "," + State.selectedService + "," + cellID);
                        sw.Write("</Structure3D>");
                        sw.Flush();
                    }
                }

                return View("Locations3D");
            }

            if (Request["group1"] == "generate")
            {
                return View("Locations");
            }
            else
            {
                Response.ClearHeaders();
                Response.ClearContent();
                Response.Clear();
                Response.AddHeader("content-disposition", "inline;filename=" + cellID + ".svg");
                Response.ContentType = "image/svg+xml";
                Response.WriteFile(workDirectory + "\\Files\\" + HttpContext.User.Identity.Name + "\\" + cellID + ".svg");
                Response.Flush();
                Response.End();

            }

            return View("Index");
        }
Example #4
0
        public string getCellHop(int cellID, int hops)
        {
            CircuitClient circuitClient = State.CreateCircuitClient();
            circuitClient.Open();

            //string s = circuitClient.GetWorkingResponse(10);

            Graphx graphx = circuitClient.getGraph(cellID, hops+1);

            List<Edge> tmpEdgeList = new List<Edge>();

            foreach (Edgex edgex in graphx.EdgeList)
            {
                Edge tmp = new Edge(edgex.SourceID, edgex.TargetID, edgex.SourceParentID, edgex.TargetParentID, edgex.Link, edgex.SourceTypeName, 0.0f);
                tmpEdgeList.Add(tmp);
            }

            Graph graph = new Graph(tmpEdgeList, graphx.NodeList, graphx.InvolvedCells.ToList<long>(), graphx._FrontierNodes.ToList<long>(),
                            graphx.FrontierNodes.ToList<long>(), graphx.ReducedEdges.ToList<long>(), graphx.locationInfo, graphx.zLocationForSynapses);

            Dictionary<string, string> colorTable = new Dictionary<string, string>();
            colorTable.Add("ribbon synapse", "chartreuse4");
            colorTable.Add("conventional", "red3");
            colorTable.Add("bc conventional", "chartreuse4");
            colorTable.Add("gap junction", "goldenrod4");
            colorTable.Add("unknown", "gray50");
            colorTable.Add("frontier", "white");

            List<object> edgesJson = new List<object>();
            int edgeCount = 0;

            foreach (Edge edge in graph.EdgeList)
            {

                if (false == graph.NodeList.ContainsKey(edge.SourceParentID) ||
                    false == graph.NodeList.ContainsKey(edge.TargetParentID))
                {

                }
                else
                {
                    Boolean activeEdge = true;

                    if (false == graph.InvolvedCells.Contains(edge.SourceParentID))
                    {
                        graph.InvolvedCells.Add(edge.SourceParentID);
                        activeEdge = false;

                    }

                    if (false == graph.InvolvedCells.Contains(edge.TargetParentID))
                    {
                        graph.InvolvedCells.Add(edge.TargetParentID);
                        activeEdge = false;

                    }

                }
            }

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            foreach (long nodeID in graph.InvolvedCells)
            {

                sb.Append(nodeID + ",");

            }

            return sb.ToString().Substring(0, sb.Length - 1);
        }