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

1 line
6.9 KiB
JavaScript

define("utils/graph",["exports"],function(e){"use strict";function t(e,t){for(var r in t)if(t.hasOwnProperty(r)&&(!e.hasOwnProperty(r)||e[r]!==t[r]))return!1;return!0}function r(e,r){var n="function"==typeof r?r:void 0,o="object"===(void 0===r?"undefined":i(r))?r:void 0,s=[],c=0;for(var a in e)if(e.hasOwnProperty(a)){var u=e[a];n?s.push(n.call(u,u,a,c)):o?"object"===(void 0===u?"undefined":i(u))&&t(u,o)&&s.push(u):s.push(u),c+=1}return s}function n(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 o(e,t){var r=this;return r.name=void 0!==e?e:"(unnamed)",r.data=t||null,r.edges={},r.degree=0,r}function s(e,t,r){return this.directed=e||!1,this.init(r).read(t)}Object.defineProperty(e,"__esModule",{value:!0});var i="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};n.prototype.toString=function(){return this.source+"->"+this.target},n.prototype.toJSON=function(){var e={source:this.source,target:this.target};return this.data&&(e.data=this.data),e},o.prototype.toString=function(){return"Vertex("+this.name+")"},o.prototype.eachEdge=function(e){return r(this.edges,e)},o.prototype.toJSON=function(){return{name:this.name,data:this.data}};var c=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};c.prototype.search=function(e){var t=this;return e in t._cache?t._cache[e]:(e instanceof o||(e=t.graph.vertices[e]),t._cache[e.name]=t._search(e))},c.prototype._search=function(e,t){return t=t||{discovered:{},edges:[]}},c.prototype.searchTree=function(e){return this._searchTree(this.search(e))},c.prototype._searchTree=function(e){var t=this;return new s(!0,{edges:e.edges,vertices:Object.keys(e.discovered).map(function(e){return t.graph.vertices[e].toJSON()})})};var a=function(e,t){var r=this;return c.call(this,e,t),r};(a.prototype=new c).constructor=a,a.prototype._search=function(e,t){var r=this,n=[];for((t=t||{discovered:{},edges:[]}).discovered[e.name]=!0,n.push(e);n.length;){var o=n.shift();r.processFns.vertexEarly&&r.processFns.vertexEarly.call(r,o,t),r.graph.eachAdjacent(o,function(e,o){var s=this;r.processFns.edge&&r.processFns.edge.call(r,s,o,t),t.discovered[e.name]||(t.discovered[e.name]=!0,t.edges.push({source:s.name,target:e.name}),n.push(e))}),r.processFns.vertexLate&&r.processFns.vertexLate.call(r,o,t)}return t};var u=function(e,t){var r=this;return c.call(this,e,t),r};u.prototype=new c,u.prototype.constructor=u,u.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=s,s.prototype.init=function(e){e=e||{};var t=this;return t.allowReflexiveEdges=e.allowReflexiveEdges||!1,t.vertices={},t.numEdges=0,t},s.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},s.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},s.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},s.prototype.createVertex=function(e,t){return this.vertices[e]?this.vertices[e]:this.vertices[e]=new o(e,t)},s.prototype.createEdge=function(e,t,r,o){var s=e===t;if(!this.allowReflexiveEdges&&s)return null;var i=this.vertices[e],c=this.vertices[t];if(!i||!c)return null;var a=this,u=new n(e,t,o);return i.edges[t]=u,i.degree+=1,a.numEdges+=1,s||r||a.createEdge(t,e,!0),u},s.prototype.edges=function(e){return Array.prototype.concat.apply([],this.eachVertex(function(t){return t.eachEdge(e)}))},s.prototype.eachVertex=function(e){return r(this.vertices,e)},s.prototype.adjacent=function(e){var t=this;return r(e.edges,function(e){return t.vertices[e.target]})},s.prototype.eachAdjacent=function(e,t){var n=this;return r(e.edges,function(r){var o=n.vertices[r.target];return t.call(e,o,r)})},s.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},s.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")},s.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})}},s.prototype.toVerticesAndEdges=function(){var e=this;return{vertices:e.eachVertex(function(e,t){return e.toJSON()}),edges:e.edges(function(e){return e.toJSON()})}},s.prototype.breadthFirstSearch=function(e,t){return new a(this).search(e)},s.prototype.breadthFirstSearchTree=function(e,t){return new a(this).searchTree(e)},s.prototype.depthFirstSearch=function(e,t){return new u(this).search(e)},s.prototype.depthFirstSearchTree=function(e,t){return new u(this).searchTree(e)},s.prototype.weakComponents=function(){var e,t=this,r=this,n=[];for(t.directed&&(r=new s(!1,t.toNodesAndLinks())),e=Object.keys(r.vertices);e.length;){var o=r.vertices[e.shift()];n.push(function(n){var o=new u(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})}}(o))}return n},s.prototype.weakComponentGraph=function(){var e=this.weakComponents();return new s(this.directed,{vertices:e.reduce(function(e,t){return e.concat(t.vertices)},[]),edges:e.reduce(function(e,t){return e.concat(t.edges)},[])})},s.prototype.weakComponentGraphArray=function(){var e=this;return this.weakComponents().map(function(t){return new s(e.directed,t)})},e.default={Vertex:o,Edge:n,BreadthFirstSearch:a,DepthFirstSearch:u,Graph:s,randGraph:function(e,t,r){function n(e){return Math.floor(Math.random()*e)}for(var o={nodes:[],links:[]},i=0;i<t;i++)o.nodes.push({name:i});for(i=0;i<r;i++)o.links.push({source:n(t),target:n(t)});return new s(e,o)}}});