var SPARQLBuilder = function(result,endpoint) { this.result = result; this.builder = this.createBuilder('http://www.sparqlbuilder.org/forms/guiform.php'); this.showBuilder(); this.drawGraph = null; this.startClass = null; this.endClass = null; this.endPoint = endpoint; }; SPARQLBuilder.prototype.createBuilder = function(form) { var builder = $('
'); var content = $(''); builder.prepend(content); var self = this; $.ajax({ type : "GET", url : form, async : false, success : function(data) { content.prepend($(data)); } }); $("body").prepend(builder); return builder; }; SPARQLBuilder.prototype.showBuilder = function() { var width = $(document).width(); var height = $(document).height(); this.builder.css("width", width); this.builder.css("height", height); this.builder.css("display", "block"); document.getElementById("sparqlBuilderAjaxload").style.width = width; document.getElementById("sparqlBuilderAjaxload").style.height = height; this.loadIcon("hide"); var self = this; this.loadEndPointList(); $("#EndPointSelect").change(function() { self.changeEndPoint(); }); //$("#SetClassButton").click(function() { // self.startClass = $("#StartClassSelect").val(); // self.endClass = $("#EndClassSelect").val(); // self.loadPathList(); //}); $("#StartClassSelect").change(function() { self.startClass = $("#StartClassSelect").val(); self.loadPathList(); }); $("#EndClassSelect").change(function() { self.endClass = $("#EndClassSelect").val(); self.loadPathList(); }); $("#sparqlBuilderViewall").click(function() { self.drawGraph.setPathLimit(0); self.drawGraph.view_map(); }); $("#GenerateSPARQLButton").click(function() { var sparql = self.generateSPARQL(self.drawGraph.pathobj); }); $("#SparqlBuilderCancel").click(function() { self.hideBuilder(); }); }; SPARQLBuilder.prototype.loadIcon = function(mode) { if(mode == "view"){ document.getElementById("sparqlBuilderAjaxload").style.display = "block"; }else{ document.getElementById("sparqlBuilderAjaxload").style.display = "none"; } }; SPARQLBuilder.prototype.loadEndPointList = function() { var url = 'http://www.sparqlbuilder.org/api/eplist'; $.ajax({ url: url, success: function(data) { var list = eval(data); var event = new $.Event('complete'); $("#EndPointSelect").empty(); $("#EndPointSelect").append(''); for (var i = 0; i < list.length; ++i) { $("#EndPointSelect").append(''); } $("#seclass").trigger(new $.Event('epcomplete')); }, }); }; SPARQLBuilder.prototype.loadClassList = function() { var url = "http://www.sparqlbuilder.org/api/clist?ep=" + encodeURIComponent(this.endpoint); //var url = "http://localhost:8080/api/clist?ep=" + encodeURIComponent(this.endpoint); $.ajax({ type : "GET", url : url, async : false, success : function(data) { var list = eval(data); var event = new $.Event('complete'); $("#StartClassSelect").empty(); $("#EndClassSelect").empty(); for (var i = 0; i < list.length; ++i) { //$("#StartClassSelect").append(''); //$("#EndClassSelect").append(''); $("#StartClassSelect").append(''); $("#EndClassSelect").append(''); } $("#seclass").trigger(new $.Event('secomplete')); } }); }; SPARQLBuilder.prototype.loadSamplePathList = function() { var url = "http://www.sparqlbuilder.org/api/plist?ep=" + encodeURIComponent('http://www.ebi.ac.uk/rdf/services/reactome/sparql') + "&startclass=" + encodeURIComponent('http://www.biopax.org/release/biopax-level3.owl#Protein') + "&endclass=" + encodeURIComponent('http://www.biopax.org/release/biopax-level3.owl#Pathway'); //var url = "http://localhost:8080/api/plist?ep=" + encodeURIComponent(this.endpoint) // + "&startclass=" + encodeURIComponent(startclass) // + "&endclass=" + encodeURIComponent(endclass); var self = this; self.loadIcon("view"); setTimeout(function(){ $.ajax({ type : "GET", url : url, async : false, timeout : 1000000, success : function(data) { var width = $(".SparqlBuilderContent").width(); self.drawGraph = new SPARQLBuilderDrawGraph(data, width, 10); self.loadIcon("hide"); self.drawGraph.view_map(); $('select[name="selectendpoint"]').val('http://www.ebi.ac.uk/rdf/services/reactome/sparql'); }, error: function(data){ self.loadIcon("hide"); alert("error: ", data); } }); }, 100) }; SPARQLBuilder.prototype.loadPathList = function() { if (this.startClass == null || this.endClass == null ){ return; } var startclass = $("#StartClassSelect").val(); var endclass = $("#EndClassSelect").val(); var url = "http://www.sparqlbuilder.org/api/plist?ep=" + encodeURIComponent(this.endpoint) + "&startclass=" + encodeURIComponent(startclass) + "&endclass=" + encodeURIComponent(endclass); //var url = "http://localhost:8080/api/plist?ep=" + encodeURIComponent(this.endpoint) // + "&startclass=" + encodeURIComponent(startclass) // + "&endclass=" + encodeURIComponent(endclass); var self = this; self.loadIcon("view"); setTimeout(function(){ $.ajax({ type : "GET", url : url, async : false, timeout : 1000000, success : function(data) { var width = $(".SparqlBuilderContent").width(); self.drawGraph = new SPARQLBuilderDrawGraph(data, width, 10); self.loadIcon("hide"); self.drawGraph.view_map(); }, error: function(data){ self.loadIcon("hide"); alert("error: ", data); } }); }, 100) }; SPARQLBuilder.prototype.changeEndPoint = function() { this.endpoint = $("#EndPointSelect").val(); this.loadClassList(); }; SPARQLBuilder.prototype.hideBuilder = function() { this.builder.hide(); }; SPARQLBuilder.prototype.generateSPARQL = function(pathobj) { var path = JSON.stringify(pathobj); var url = 'http://www.sparqlbuilder.org/api/sparql?path=' + encodeURIComponent(path); var sparql = ''; var self = this; $.ajax({ type: "GET", url : url, dataType: 'text', async: false, success : function(data) { var event = new $.Event('write'); $("#" + self.result).val(data); $("#" + self.endPoint).val(self.endpoint); self.hideBuilder(); $("#" + self.result).trigger(event, [data, pathobj]); } }); }; var SPARQLBuilderDrawGraph = function(jsontext, width, pathlimit) { this.jsontext = jsontext; this.width = width; this.pathlimit = pathlimit; this.pathobj = null; }; SPARQLBuilderDrawGraph.prototype.setPathLimit = function(pathlimit) { this.pathlimit = pathlimit; }; SPARQLBuilderDrawGraph.prototype.view_map = function(){ // make_dataメソッドの結果を取得 var json = this.make_data(0); if(json['nodes'].length != 0){ // 出来上がった結果を渡してマップ上のロケーションをセット this.set_map_location(0, json['nodes'], json['links']); // SVGの幅と高さを設定(幅:画面いっぱい 高さ:パスの数に応じ設定) var width = this.width; var height = (width * 9 / 16); var graphheight = ((this.NODEHEIGHT * 1.5) * this.PATHNUM) + (this.NODEHEIGHT / 2); var scoreleftmargin = this.NODEHEIGHT * 1.5; // カラーを取得 var color = d3.scale.category20(); // SVGの削除 d3.select("#sparqlBuilderGraph").html(""); // 画面サイズに合わせSVGの追加 var svg = d3.select("#sparqlBuilderGraph").append("svg") .attr("width", width) .attr("height", height) .attr("viewBox", "0 0 " + width + " " + height) .on("mousewheel", function(){ var vb = svg.attr("viewBox"); var spvb = vb.split(" "); var vby = (parseInt(spvb[1]) - event.wheelDelta); if(vby < 0){ vby = 0; }else if(vby > (graphheight - height)){ vby = (graphheight - height); }else{ event.preventDefault(); } svg.attr("viewBox", "0 " + vby + " " + width + " " + height); }); // 背景の追加 var bg = svg .append("rect") .attr("x", 0) .attr("y", 0) .attr("width", width) .attr("height", graphheight) .attr("fill", "#fafafa"); // links配列を渡しリンクの作成 var link = svg.selectAll(".link") .data(json.links) .enter().append("line") .attr("class", "link") .style("stroke", "#999") .style("stroke-opacity", 0.6) .style("stroke-width", function(d) { return Math.sqrt(d.value);}); // nodes配列を渡しノードの作成 var node = svg.selectAll(".node") .data(json.nodes) .enter().append("circle") .attr("class", "node") .attr("r", (this.NODEHEIGHT / 2)) .attr("cx", function(d) { return d.x;} ) .attr("cy", function(d) { return d.y; }) .style("stroke", function(d) { return '#fafafa'; }) .style("stroke-width", function(d) { return '1.5px'; }) .style("fill", function(d) { return d.nodecolor; }) .style("cursor", function(d) { return 'pointer'; }); // nodes配列を渡しノードテキストの作成 var tnode = svg.selectAll("text.node") .data(json.nodes) .enter().append("svg:text") .attr("class", "tnode") .attr("x", function(d) { return d.x; }) .attr("y", function(d) { return d.y; }) .text(function(d) { return d.name; }) .style("fill", function(d) { return '#000000'; }) .style("text-anchor", function(d) { return 'middle'; }) .style("pointer-events", "none"); // リンクテキストの作成 var tlink = svg.selectAll("text.link") .data(json.links) .enter().append("svg:text") .attr("class", "tlink") .attr("x", function(d) { return (json.nodes[d.source].x + json.nodes[d.target].x) / 2; }) .attr("y", function(d) { return (json.nodes[d.source].y + json.nodes[d.target].y) / 2; }) .style("fill", function(d) { return '#000000'; }) .style("text-anchor", function(d) { return 'middle'; }); // nodes配列を渡しノードテキストの作成 var tscore = svg.selectAll("text.score") .data(json.nodes) .enter().append("svg:text") .attr("class", "tscore") .attr("x", function(d) { return (d.x + scoreleftmargin); }) .attr("y", function(d) { return d.y; }) .text(function(d) { return d.score; }) .style("fill", function(d) { return '#FF0000'; }) .style("text-anchor", function(d) { return 'middle'; }) .style("pointer-events", "none"); // マウスの動きカウントをルートに持たせる node.data()[0].mousemoved = 0; // ノードへのオンマウスでパス探索、パス中のリンク文字を表示 var self = this; node.on("mouseover", function(d){ // マウスの動きカウントをリセット node.data()[0].mousemoved = 0; // 表示するパス保存用配列 var path = []; // ツールチップへの名前表示用配列 var pathname = []; // ルートノード以外なら if(d.nodeid != 0){ // まずオンマウスされたノードのidと名前をそれぞれ追加 path.push(d.nodeid); pathname.push(d.name); // パス探索 do{ // リンクの数だけ繰り返し for(var i = 0; i < link.data().length; i++){ // 現在の最後尾に繋がるリンクがあれば if(path[(path.length-1)] == link.data()[i].target){ // そのリンクのソース側ノードのidを追加 path.push(link.data()[i].source); // そのリンクの名前とソース側ノードの名前を追加 pathname.push(link.data()[i].property); pathname.push(node.data()[link.data()[i].source].name); } } // ルートノードに辿り着くまで繰り返す }while(path[(path.length-1)] != 0); // 末端ノードでないなら if(d.path == "notend"){ // ツールチップを非表示に document.getElementById("sparqlBuilderShowpath").style.display = "none"; }else{ // 末端ノードならツールチップの情報を更新 var resultText = "