Files
galaxy/static/scripts/utils/graph.js
T

3 lines
7.0 KiB
JavaScript

"use strict";var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};define([],function(){function e(e,t){for(var r in t)if(t.hasOwnProperty(r)&&(!e.hasOwnProperty(r)||e[r]!==t[r]))return!1;return!0}function t(t,r){var n="function"==typeof r?r:void 0,o="object"===(void 0===r?"undefined":_typeof(r))?r:void 0,s=[],i=0;for(var c in t)if(t.hasOwnProperty(c)){var a=t[c];n?s.push(n.call(a,a,c,i)):o?"object"===(void 0===a?"undefined":_typeof(a))&&e(a,o)&&s.push(a):s.push(a),i+=1}return s}function r(e,t,r){var n=this;return n.source=void 0!==e?e:null,n.target=void 0!==t?t:null,n.data=r||null,n}function n(e,t){var r=this;return r.name=void 0!==e?e:"(unnamed)",r.data=t||null,r.edges={},r.degree=0,r}function o(e,t,r){return this.directed=e||!1,this.init(r).read(t)}function s(e,t,r){function n(e){return Math.floor(Math.random()*e)}for(var s={nodes:[],links:[]},i=0;i<t;i++)s.nodes.push({name:i});for(i=0;i<r;i++)s.links.push({source:n(t),target:n(t)});return new o(e,s)}r.prototype.toString=function(){return this.source+"->"+this.target},r.prototype.toJSON=function(){var e={source:this.source,target:this.target};return this.data&&(e.data=this.data),e},n.prototype.toString=function(){return"Vertex("+this.name+")"},n.prototype.eachEdge=function(e){return t(this.edges,e)},n.prototype.toJSON=function(){return{name:this.name,data:this.data}};var i=function(e,t){var r=this;return r.graph=e,r.processFns=t||{vertexEarly:function(e,t){},edge:function(e,t,r){},vertexLate:function(e,t){}},r._cache={},r};i.prototype.search=function(e){var t=this;return e in t._cache?t._cache[e]:(e instanceof n||(e=t.graph.vertices[e]),t._cache[e.name]=t._search(e))},i.prototype._search=function(e,t){return t=t||{discovered:{},edges:[]}},i.prototype.searchTree=function(e){return this._searchTree(this.search(e))},i.prototype._searchTree=function(e){var t=this;return new o(!0,{edges:e.edges,vertices:Object.keys(e.discovered).map(function(e){return t.graph.vertices[e].toJSON()})})};var c=function(e,t){var r=this;return i.call(this,e,t),r};c.prototype=new i,c.prototype.constructor=c,c.prototype._search=function(e,t){function r(e,r){var s=this;n.processFns.edge&&n.processFns.edge.call(n,s,r,t),t.discovered[e.name]||(t.discovered[e.name]=!0,t.edges.push({source:s.name,target:e.name}),o.push(e))}t=t||{discovered:{},edges:[]};var n=this,o=[];for(t.discovered[e.name]=!0,o.push(e);o.length;){var s=o.shift();n.processFns.vertexEarly&&n.processFns.vertexEarly.call(n,s,t),n.graph.eachAdjacent(s,r),n.processFns.vertexLate&&n.processFns.vertexLate.call(n,s,t)}return t};var a=function(e,t){var r=this;return i.call(this,e,t),r};return a.prototype=new i,a.prototype.constructor=a,a.prototype._search=function(e,t){function r(e,r){var s=this;o.processFns.edge&&o.processFns.edge.call(o,s,r,t),t.discovered[e.name]||(t.edges.push({source:s.name,target:e.name}),n(e))}function n(e){t.discovered[e.name]=!0,o.processFns.vertexEarly&&o.processFns.vertexEarly.call(o,e,t),t.entryTimes[e.name]=s++,o.graph.eachAdjacent(e,r),o.processFns.vertexLate&&o.processFns.vertexLate.call(o,e,t),t.exitTimes[e.name]=s++}t=t||{discovered:{},edges:[],entryTimes:{},exitTimes:{}};var o=this,s=0;return n(e),t},window.Graph=o,o.prototype.init=function(e){e=e||{};var t=this;return t.allowReflexiveEdges=e.allowReflexiveEdges||!1,t.vertices={},t.numEdges=0,t},o.prototype.read=function(e){if(!e)return this;var t=this;return e.hasOwnProperty("nodes")?t.readNodesAndLinks(e):e.hasOwnProperty("vertices")?t.readVerticesAndEdges(e):t},o.prototype.readNodesAndLinks=function(e){if(!e||!e.hasOwnProperty("nodes"))return this;var t=this;return e.nodes.forEach(function(e){t.createVertex(e.name,e.data)}),(e.links||[]).forEach(function(r,n){var o=e.nodes[r.source].name,s=e.nodes[r.target].name;t.createEdge(o,s,t.directed)}),t},o.prototype.readVerticesAndEdges=function(e){if(!e||!e.hasOwnProperty("vertices"))return this;var t=this;return e.vertices.forEach(function(e){t.createVertex(e.name,e.data)}),(e.edges||[]).forEach(function(e,r){t.createEdge(e.source,e.target,t.directed)}),t},o.prototype.createVertex=function(e,t){return this.vertices[e]?this.vertices[e]:this.vertices[e]=new n(e,t)},o.prototype.createEdge=function(e,t,n,o){var s=e===t;if(!this.allowReflexiveEdges&&s)return null;if(sourceVertex=this.vertices[e],targetVertex=this.vertices[t],!sourceVertex||!targetVertex)return null;var i=this,c=new r(e,t,o);return sourceVertex.edges[t]=c,sourceVertex.degree+=1,i.numEdges+=1,s||n||i.createEdge(t,e,!0),c},o.prototype.edges=function(e){return Array.prototype.concat.apply([],this.eachVertex(function(t){return t.eachEdge(e)}))},o.prototype.eachVertex=function(e){return t(this.vertices,e)},o.prototype.adjacent=function(e){var r=this;return t(e.edges,function(e){return r.vertices[e.target]})},o.prototype.eachAdjacent=function(e,r){var n=this;return t(e.edges,function(t){var o=n.vertices[t.target];return r.call(e,o,t)})},o.prototype.print=function(){var e=this;return console.log("Graph has "+Object.keys(e.vertices).length+" vertices"),e.eachVertex(function(e){console.log(e.toString()),e.eachEdge(function(e){console.log("\t "+e)})}),e},o.prototype.toDOT=function(){var e=this,t=[];return t.push("graph bler {"),e.edges(function(e){t.push("\t"+e.from+" -- "+e.to+";")}),t.push("}"),t.join("\n")},o.prototype.toNodesAndLinks=function(){var e=this,t={};return{nodes:e.eachVertex(function(e,r,n){return t[e.name]=n,e.toJSON()}),links:e.edges(function(e){var r=e.toJSON();return r.source=t[e.source],r.target=t[e.target],r})}},o.prototype.toVerticesAndEdges=function(){var e=this;return{vertices:e.eachVertex(function(e,t){return e.toJSON()}),edges:e.edges(function(e){return e.toJSON()})}},o.prototype.breadthFirstSearch=function(e,t){return new c(this).search(e)},o.prototype.breadthFirstSearchTree=function(e,t){return new c(this).searchTree(e)},o.prototype.depthFirstSearch=function(e,t){return new a(this).search(e)},o.prototype.depthFirstSearchTree=function(e,t){return new a(this).searchTree(e)},o.prototype.weakComponents=function(){var e,t=this,r=this,n=[];for(t.directed&&(r=new o(!1,t.toNodesAndLinks())),e=Object.keys(r.vertices);e.length;){var s=r.vertices[e.shift()];n.push(function(n){var o=new a(r)._search(n);return e=e.filter(function(e){return!(e in o.discovered)}),{vertices:Object.keys(o.discovered).map(function(e){return t.vertices[e].toJSON()}),edges:o.edges.map(function(e){var r=void 0!==t.vertices[e.target].edges[e.source];if(t.directed&&r){var n=e.source;e.source=e.target,e.target=n}return e})}}(s))}return n},o.prototype.weakComponentGraph=function(){var e=this.weakComponents();return new o(this.directed,{vertices:e.reduce(function(e,t){return e.concat(t.vertices)},[]),edges:e.reduce(function(e,t){return e.concat(t.edges)},[])})},o.prototype.weakComponentGraphArray=function(){var e=this;return this.weakComponents().map(function(t){return new o(e.directed,t)})},{Vertex:n,Edge:r,BreadthFirstSearch:c,DepthFirstSearch:a,Graph:o,randGraph:s}});
//# sourceMappingURL=../../maps/utils/graph.js.map