This repository has been archived by the owner on Nov 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rna2d.min.js
1 lines (1 loc) · 17.4 KB
/
rna2d.min.js
1
(function(){var a=window.Rna2D||function(b){var c=function(g,f){var e=g["class"](),h=g.classOf();a.utils.attachHandlers(f,g);return f.attr("id",g.elementID).attr("class",function(k,j){return h(k,j).concat(e).join(" ")}).attr("visibility",g.visibility)};var d=function(){d.nucleotides.computeOrder();d.view.setup();var f=d.margin(),e=d3.select(d.selection()),h=function(j,i){return d3.scale.linear().domain(j()).range([0,i])};e.select("svg").remove();var g=e.append("svg").attr("width",d.width()+f.left+f.right).attr("height",d.height()+f.above+f.below);d.vis=g.append("g").attr("transform","translate("+f.left+","+f.above+")");d.xScale(h(d.xDomain,d.width()-f.right));d.yScale(h(d.yDomain,d.height()-f.above));d.components();d.coordinates(function(j){var i=d.views[d.view()].xCoord(),k=d.views[d.view()].yCoord();c(d.nucleotides,j).datum(function(m,l){m.__x=i(m,l);m.__y=k(m,l);return m}).attr("data-sequence",d.nucleotides.getSequence());return j});d.connections(function(i){var j=d.interactions.getNTs();c(d.interactions,i).attr("data-nts",function(l,k){return j(l).join(",")}).attr("nt1",function(l,k){return j(l)[0]}).attr("nt2",function(l,k){return j(l)[1]});return i});d.groups(function(i){var j=d.motifs.getNTs();c(d.motifs,i).attr("data-nts",function(k){return d.motifs.getNTs()(k).join(",")});return i});return d};a.config(d,b);a.components(d);a.views(d);return d};window.Rna2D=a;a.components=function(b){var c=false;b.components=function(){$.each(a.components,function(d,f){if(f.hasOwnProperty("actions")&&!c){if(f.togglable){a.togglable(b,d)}f.actions(b)}if(f.hasOwnProperty("generate")){try{f.generate(b)}catch(e){console.log("Error generating component "+d);console.log(e)}}});c=true};$.each(a.components,function(d,e){(function(g){var f=null;b[g]=function(h){if(!arguments.length){return f}f=h;return b[g]}}(d));a.utils.generateAccessors(b[d],e.config(b));if(e.hasOwnProperty("sideffects")){e.sideffects(b)}if(b[d].hasOwnProperty("encodeID")&&b[d].hasOwnProperty("getID")){b[d].elementID=function(j,f){var h=b[d].encodeID(),g=b[d].getID();return h(g(j,f))}}if(b[d].hasOwnProperty("getNTs")){(function(f){b[f].ntElements=function(j,g){var h=b[f].getNTs();return $.map(h(j,g),b.nucleotides.encodeID())}}(d))}b.components[d]=e});return a};a.config=function(c,d){var b={labels:[],margin:{left:10,right:10,above:10,below:10},view:"circular",width:500,height:1000,selection:null,xScale:null,yScale:null};a.utils.generateAccessors(c,$.extend(b,d));return c};a.togglable=function(e,c){var d=e[c],b={};d.all=function(f){f=(f&&f!=="all"?f:d["class"]());return e.vis.selectAll("."+f)};d.visible=function(){$.each(arguments,function(g,f){b[f]=true})};d.hidden=function(){$.each(arguments,function(g,f){b[f]=null})};d.show=function(f){b[f]=true;return d.all(f).attr("visibility",function(){return"visible"})};d.hide=function(f){b[f]=null;return d.all(f).attr("visibility",function(){return"hidden"})};d.toggle=function(f){return(b[f]?d.hide(f):d.show(f))};d.visibility=function(j,g){var f=d.classOf()(j),h=$.map(f,function(l,m){return b[l]});return(h.length?"visible":"hidden")}};a.utils=(function(){var b={};b.distance=function(d,c){return Math.sqrt(Math.pow(c.x-d.x,2)+Math.pow(c.y-d.y,2))};b.generateAccessors=function(d,c,e){$.each(c,function(f,g){d[f]=(function(){return function(h){if(!arguments.length){return c[f]}var i=c[f];c[f]=h;if(e&&e[f]){e[f](i,h)}return d}}())})};b.attachHandlers=function(d,e){var c=["click","mouseover","mouseout"];if(e.mouseover()==="highlight"){d.on(c.pop(),e.normalize()).on(c.pop(),e.highlight())}$.each(c,function(f,g){d.on(g,e[g]())});return d};b.element=function(c){return document.getElementById(c)};b.compose=function(){var c=arguments;return function(){var d=arguments;$.each(c,function(e,f){d=[f.apply(this,d)]});return d}};return b}());a.views=function(b){b.view.setup=function(){var c=a.views[b.view()];if(c===undefined){console.log("Unknown view "+b.view());return false}b.coordinates=c.coordinates;b.connections=c.connections;b.groups=c.groups;b.xDomain=c.xDomain;b.yDomain=c.yDomain;c.sideffects()};b.views={};$.each(a.views,function(e,c){c=c(b);var d=c.config;if(typeof(d)==="function"){d=d(b)}b.views[e]={};a.utils.generateAccessors(b.views[e],d);a.views[e]=c})};a.components.brush=(function(){return{config:function(b){return{enabled:true,"class":"brush",update:Object,clear:Object}},sideffects:function(b){b.brush.jmol=function(e){var c=b.nucleotides.getID(),d=$.map(e,c);return b.jmol.showNTs(d)}},actions:function(b){b.brush.enable=function(){b.vis.append("g").classed(b.brush["class"](),true).call(b.brush());b.brush.enabled(true);return b.brush};b.brush.disable=function(){b.vis.selectAll("."+b.brush["class"]()).remove();b.brush.enabled(false);return b.brush};b.brush.toggle=function(){if(b.brush.enabled()){return b.brush.disable()}return b.brush.enable()}},generate:function(c){var b=function(){var d=[],f=c.brush().extent();if(c.brush().empty()){return c.brush.clear()()}c.vis.selectAll("."+c.nucleotides["class"]()).attr("selected",function(e){if(f[0][0]<=e.__x&&e.__x<=f[1][0]&&f[0][1]<=e.__y&&e.__y<=f[1][1]){d.push(e);return"selected"}return""});return c.brush.update()(d)};c.brush(d3.svg.brush().on("brushend",b).x(c.xScale()).y(c.yScale()));if(c.brush.enabled()){c.brush.enable()}return c.brush}}}());a.components.frame={config:function(b){return{add:true,"class":"frame"}},generate:function(b){if(!b.frame.add()){return b.vis}return b.vis.append("svg:rect").classed(b.frame["class"](),true).attr("x",0).attr("y",0).attr("width",b.width()+b.margin().left+b.margin().right).attr("height",b.height()+b.margin().below+b.margin().above).style("pointer-events","none")}};a.components.interactions=(function(){return{togglable:true,config:function(b){return{getFamily:function(c){return c.family},getNTs:function(c){return[c.nt1,c.nt2]},mouseover:null,mouseout:null,click:Object,"class":"interaction",classOf:function(c){return[c.family]},highlightColor:function(){return"red"},highlight:Object,normalize:Object,isForward:function(f){var c=b.interactions.getFamily(),e=c(f);if(e.length===3){e=e.slice(1,3).toUpperCase()}else{e=e.slice(2,4).toUpperCase()}return e==="WW"||e==="WH"||e==="WS"||e==="HH"||e==="SH"||e==="SS"},isSymmetric:function(g,e){var c=b.interactions.getFamily(),f=c(g);return f[1]===f[2]},getID:function(f){var c=b.interactions.getFamily()(f),e=b.interactions.getNTs()(f);if(b.interactions.isSymmetric()(f)){e.sort()}e.push(c);return e.join("-")},encodeID:function(c){return c},color:"black"}},sideffects:function(b){b.interactions.valid=function(){var i=b.interactions(),f=b.interactions.getID(),e=b.interactions.getNTs(),h=b.interactions.isForward(),g=[],c={},d=b.nucleotides.indexOf;$.each(i,function(j,l){var m=f(l),k=e(l);if(h(l)&&!c[m]&&k.length&&d(k[0])!==null&&d(k[1])!==null){c[m]=true;g.push(l)}});return g};b.interactions.jmol=function(f,c){var e=b.interactions.getNTs();return b.jmol.showNTs(e(f,c))}},actions:function(b){b.interactions.visible("cWW","ncWW");b.interactions.nucleotides=function(f){f=f||this;var e=d3.select(f).datum(),d=b.interactions.ntElements(e),c="#"+d.join(", #");return b.vis.selectAll(c)}}}}());a.components.jmol={config:function(b){return{divID:"jmol",appID:"jmolApplet0",tmpID:"tempJmolToolsObj",neighborhoodID:"neighborhood",numbersID:"showNtNums",stereoID:"stero",maxSize:200,overflow:Object,windowSize:400,windowBuild:function(c){c.append('<label><input type="checkbox" id="showNtNums">Numbers</label>').append('<input type="button" id="neighborhood" value="Show neighborhood">').append('<input type="button" id="stereo" value="Stereo">')}}},sideffects:function(b){b.jmol.setup=function(){var c=$("#"+b.jmol.appID()),d=$("#"+b.jmol.divID());if(c.length===0){d.html(jmolApplet(b.jmol.windowSize(),"",0));b.jmol.windowBuild()(d);d.show()}jmolScript("zap;");$.jmolTools.numModels=0;$.jmolTools.stereo=false;$.jmolTools.neighborhood=false;$("#"+b.jmol.neighborhoodID()).val("Show neighborhood");$.jmolTools.models={};$("#"+b.jmol.stereoID()).unbind();$("#"+b.jmol.neighborhoodID()).unbind();$("#"+b.jmol.numbersID()).unbind();return b.jmol};b.jmol.showNTs=function(c){b.jmol.setup();if(c.length>b.jmol.maxSize()){return b.jmol.overflow()}$("#"+b.jmol.tmpID()).remove();$("body").append("<input type='radio' id='"+b.jmol.tmpID()+"' data-coord='"+c.join(",")+"'>");$("#"+b.jmol.tmpID()).hide();$("#"+b.jmol.tmpID()).jmolTools({showNeighborhoodId:b.jmol.neighborhoodID(),showNumbersId:b.jmol.numbersID(),showStereoId:b.jmol.stereoID()}).jmolToggle();return b.jmol}}};a.components.labels={config:function(b){return{majorTickClass:"major-tick",minorTickClass:"minor-tick",labelClass:"label",width:5,fontSize:12,majorTickCount:30,minorTickCount:150,}}};a.components.motifs=(function(){return{togglable:true,config:function(b){return{classOf:function(c){return[c.id.split("_")[0]]},"class":"motif",highlightColor:function(){return"red"},click:Object,mouseover:null,mouseout:null,getID:function(c){return c.id},encodeID:function(c){return c},getNTs:function(c){return c.nts},highlight:Object,normalize:Object}},sideffects:function(b){b.motifs.jmol=function(f,c){var e=b.motifs.getNTs();return b.jmol.showNTs(e(f,c))}},actions:function(b){b.motifs.visible("IL","HL","J3");b.motifs.nucleotides=function(e){var f=d3.select(e).datum(),d=b.motifs.ntElements(f),c="#"+d.join(", #");return b.vis.selectAll(c)}}}}());a.components.nucleotides=(function(){var b={};return{togglable:true,config:function(c){return{highlightColor:function(){return"red"},"class":"nucleotide",classOf:function(f,e){return[f.sequence]},color:"black",click:Object,mouseover:null,mouseout:null,getID:function(e){return e.id},getX:function(e){return e.x},getY:function(e){return e.y},encodeID:function(d){return d},getSequence:function(e){return e.sequence},highlight:Object,normalize:Object,toggleLetters:Object}},sideffects:function(c){c.nucleotides.computeOrder=function(){var e=c.nucleotides(),d=c.nucleotides.getID();$.each(e,function(g,f){b[d(f)]=g});return c.nucleotides};c.nucleotides.jmol=function(g,f){var e=c.nucleotides.getID();return c.jmol.showNTs([e(g,f)])};c.nucleotides.indexOf=function(d){if(!b.hasOwnProperty(d)){return null}return b[d]};c.nucleotides.ordered=function(d){if(!arguments.length){return b}b=d;return c.nucleotides}},actions:function(c){c.nucleotides.visible("A","C","G","U");c.nucleotides.interactions=function(g,f){var h=c.nucleotides.getID()(g,f),e="[nt1="+h+"], [nt2="+h+"]";return c.vis.selectAll(e)};c.nucleotides.doColor=function(){return c.nucleotides.all().attr("fill",c.nucleotides.color())}}}}());a.views.airport=function(g){var f={};var c=function(u,t,q){var l=u.getBBox(),k=t.getBBox(),w=l.x,j=l.y,v=k.x,i=k.y,p=v-w,o=i-j,m=0.004;var B=function(r){return(r<0?-1:1)},n=function(r){return{x:r.x+r.width/2,y:r.y+r.height/2}},s=function(r,C){return Math.sqrt(Math.pow(r,2)+Math.pow(C,2))};if(Math.abs(o)<m){j=j+l.height/2;if(w<v){return{x:w+l.width+q,y:j}}return{x:w-q,y:j}}if(Math.abs(p)<m){w=w+l.width/2;if(j>i){return{x:w,y:j}}return{x:w,y:j+l.height}}q=1;var y=n(l),x=s(p,o),A=B(p)*Math.abs(p*q/x),z=B(o)*s(q,A);return{x:y.x+A,y:y.y+z}};var e=function(i){return function(){var j=d3.max(g.nucleotides(),function(k){return k[i]});f[i]=j;return[0,j]}};var h=function(i){g.vis.selectAll(g.nucleotides["class"]()).data(g.nucleotides()).enter().append("svg:text").call(i).attr("x",function(k,j){return g.xScale()(g.nucleotides.getX()(k,j))}).attr("y",function(k,j){return g.yScale()(g.nucleotides.getY()(k,j))}).attr("font-size",g.views.airport.fontSize()).text(g.nucleotides.getSequence()).attr("fill",g.nucleotides.color());return g};var d=function(i){var k=g.interactions.valid(),j=g.interactions.ntElements;k=$.map(k,function(r,l){try{var p=j(r),o=a.utils.element(p[0]),m=a.utils.element(p[1]),s=c(o,m,g.views.airport.gap()),q=c(m,o,g.views.airport.gap());r.line={x1:s.x,y1:s.y,x2:q.x,y2:q.y}}catch(n){console.log("Could not compute interaction line for",r);return null}return r});g.vis.selectAll(g.interactions["class"]()).data(k).enter().append("svg:line").call(i).attr("stroke",g.interactions.color()).attr("x1",function(l){return l.line.x1}).attr("y1",function(l){return l.line.y1}).attr("x2",function(l){return l.line.x2}).attr("y2",function(l){return l.line.y2});return g};var b=function(l){var k=g.motifs(),o=0,n=0;if(!k||!k.length){return g}$.each(k,function(q,u){var t=0,p=f.x,s=f.y,j=0;u.missing=[];var r=g.motifs.ntElements(u);$.each(r,function(i,x){var v=a.utils.element(x);if(v===null){console.log("Missing nt "+x+" in motif: ",u);u.missing.push(x)}else{var w=v.getBBox();if(w.x<p){p=w.x}if(w.x+w.width>t){t=w.x+w.width}if(w.y+w.height>j){j=w.y+w.height}if(w.y<s){s=w.y}}});if(j===0||t===0||p===f.x||s===f.y){console.log("Unlikely bounding box found for "+u.id);u.bounding=[{x:0,y:0},{x:0,y:0},{x:0,y:0},{x:0,y:0}]}else{u.bounding=[{x:t,y:s},{x:t,y:j},{x:p,y:j},{x:p,y:s}]}});var m=d3.svg.line().x(function(i){return i.x}).y(function(i){return i.y});g.vis.selectAll(g.motifs["class"]()).data(g.motifs()).enter().append("svg:path").call(l).attr("missing-nts",function(i){return i.missing.join(" ")}).attr("d",function(i){return m(i.bounding)+"Z"});return g};return{config:{fontSize:11,gap:1,xCoord:function(k,j){return g.xScale()(g.nucleotides.getX()(k,j))},yCoord:function(k,j){return g.yScale()(g.nucleotides.getY()(k,j))}},xDomain:e("x"),yDomain:e("y"),connections:d,coordinates:h,groups:b,sideffects:function(){g.interactions.highlight(function(){var i=this,j=g.interactions.highlightColor();d3.select(i).style("stroke",j(i));return g.interactions.nucleotides(i).style("stroke",j(i))});g.interactions.normalize(function(){var i=this;d3.select(i).style("stroke",null);return g.interactions.nucleotides(i).style("stroke",null)});g.nucleotides.highlight(function(l,j){var k=this,m=g.nucleotides.highlightColor();d3.select(k).style("stroke",m());return g.nucleotides.interactions(l,j).style("stroke",m())});g.nucleotides.normalize(function(l,j){var k=this;d3.select(k).style("stroke",null);return g.nucleotides.interactions(l,j).style("stroke",null)});g.motifs.highlight(function(){var i=this,j=g.motifs.highlightColor();return g.motifs.nucleotides(i).style("stroke",j(i))});g.motifs.normalize(function(){var i=this;return g.motifs.nucleotides(i).style("stroke",null)})}}};a.views.circular=function(i){var m,o,c,k,b,j,e;var g;var f;var h=function(r){var p=f.centroid(null,i.nucleotides.indexOf(r)),q=i.views.circular.center()();return{x:q.x+p[0],y:q.y+p[1]}};var d=function(u,q){var p=i.nucleotides().length,v=i.nucleotides.indexOf,s=i.interactions.getNTs()(u).sort(function(B,z){var A=v(B),y=v(z);if(Math.abs(A-y)>p/2){return y-A}return A-y});var w=h(s[0]),x=h(s[1]),t=j(null,v(s[0]))-j(null,v(s[1])),r=Math.abs(f.innerRadius()()*Math.tan(t/2));return"M "+w.x+" "+w.y+" A "+r+","+r+" 0 0,0 "+x.x+","+x.y};var l=function(p){m=i.views.circular.radius()();o=m-i.views.circular.width();c=i.views.circular.center()();k=(2*Math.PI-i.views.circular.arcGap())/i.nucleotides().length;b=i.views.circular.arcGap()/2;j=function(r,q){return q*k+b};e=function(r,q){return(q+1)*k+b};g=d3.svg.arc().outerRadius(m).innerRadius(o).startAngle(j).endAngle(e);i.xScale(d3.scale.identity().domain([0,i.width()])).yScale(d3.scale.identity().domain([0,i.height()]));i.vis.selectAll(i.nucleotides["class"]()).append("g").data(i.nucleotides()).enter().append("svg:path").call(p).attr("d",g).attr("transform","translate("+c.x+","+c.y+")").attr("fill",i.nucleotides.color());return i};var n=function(p){f=d3.svg.arc().outerRadius(o).innerRadius(o-i.views.circular.interactionGap()).startAngle(j).endAngle(e);return i.vis.selectAll(i.interactions["class"]()).data(i.interactions.valid()).enter().append("path").call(p).attr("d",d).attr("fill","none").attr("stroke",i.interactions.color())};return{xDomain:function(){return[0,1000]},yDomain:function(){return[0,1000]},coordinates:l,connections:n,groups:function(p){return i},config:{radius:function(){return i.width()/4},width:4,arcGap:0.2,interactionGap:3,letterClass:"nucleotide-letter",xCoord:function(q,p){return g.centroid(null,p).x},yCoord:function(q,p){return g.centroid(null,p).y},center:function(){return{x:i.width()/2,y:i.height()/2}},letterID:function(p){return p.getAttribute("id")+"-letter"},letterSize:20,letterPosition:function(t){var s=d3.select(t).datum(),r=i.nucleotides.indexOf(i.nucleotides.getID()(s)),q=g.centroid(null,r),p=i.views.circular.center()();return{x:p.x+q[0],y:p.y+q[1]}},addLetters:function(q){var p=i.views.circular.letterPosition(),r=i.nucleotides.highlightColor();i.vis.selectAll(i.views.circular.letterClass()).data(q).enter().append("svg:text").attr("id",i.views.circular.letterID()).attr("class",i.views.circular.letterClass()).attr("x",function(s){return p(s).x}).attr("y",function(s){return p(s).y}).attr("font-size",i.views.circular.letterSize()).attr("pointer-events","none").text(function(s){return s.getAttribute("data-sequence")}).attr("fill",function(s){return r(s)});return i},clearLetters:function(){return i.vis.selectAll("."+i.views.circular.letterClass()).remove()}},sideffects:function(){i.nucleotides.highlight(function(r,p){var q=this,s=i.nucleotides.highlightColor();d3.select(q).style("stroke",s(q));i.views.circular.addLetters()([q]);return i.nucleotides.interactions(r,p).style("stroke",s(q))});i.nucleotides.normalize(function(r,p){var q=this;d3.select(q).style("stroke",null);i.views.circular.clearLetters()();return i.nucleotides.interactions(r,p).style("stroke",null)});i.interactions.highlight(function(s,p){var r=this,t=i.interactions.highlightColor(),q=i.interactions.nucleotides(r);d3.select(r).style("stroke",t(r));i.views.circular.addLetters()(q[0]);return q.style("stroke",t(r))});i.interactions.normalize(function(r,p){var q=this;d3.select(q).style("stroke",null);i.views.circular.clearLetters()();i.interactions.nucleotides(q).style("stroke",null);return i.interactions})}}}}());