text: define("viz/circster",["exports","utils/localization","libs/underscore","d3","viz/visualization","utils/utils","utils/config","mvc/ui/icon-button","libs/farbtastic"],function(t,e,a,n,i,r,o,s){"use strict";function l(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var a in t)Object.prototype.hasOwnProperty.call(t,a)&&(e[a]=t[a]);return e.default=t,e}function c(t){return t&&t.__esModule?t:{default:t}}Object.defineProperty(t,"__esModule",{value:!0});var u=c(e),d=l(a),_=l(n),g=c(i),h=c(r),f=c(o),m=c(s),p=Backbone.Model.extend({is_visible:function(t,e){var a=t.getBoundingClientRect(),n=$("svg")[0].getBoundingClientRect();return!(a.right<0||a.left>n.right||a.bottom<0||a.top>n.bottom)}}),v={drawTicks:function(t,e,a,n,i){var r=t.append("g").selectAll("g").data(e).enter().append("g").selectAll("g").data(a).enter().append("g").attr("class","tick").attr("transform",function(t){return"rotate("+(180*t.angle/Math.PI-90)+")translate("+t.radius+",0)"}),o=[],s=[],l=function(t){return t.angle>Math.PI?"end":null};return i?(o=[0,0,0,-4],s=[4,0,"",".35em"],l=null):(o=[1,0,4,0],s=[0,4,".35em",""]),r.append("line").attr("x1",o[0]).attr("y1",o[1]).attr("x2",o[2]).attr("y1",o[3]).style("stroke","#000"),r.append("text").attr("x",s[0]).attr("y",s[1]).attr("dx",s[2]).attr("dy",s[3]).attr("text-anchor",l).attr("transform",n).text(function(t){return t.label})},formatNum:function(t,e){if(void 0===e&&(e=2),null===t)return null;var a=null;if(Math.abs(t)<1)a=t.toPrecision(e);else{var n=Math.round(t.toPrecision(e));(t=Math.abs(t))<1e3?a=n:t<1e6?a=Math.round((n/1e3).toPrecision(3)).toFixed(0)+"K":t<1e9&&(a=Math.round((n/1e6).toPrecision(3)).toFixed(0)+"M")}return a}},b=Backbone.Model.extend({}),k=Backbone.View.extend({className:"circster",initialize:function(t){this.genome=t.genome,this.label_arc_height=50,this.scale=1,this.circular_views=null,this.chords_views=null,this.model.get("drawables").on("add",this.add_track,this),this.model.get("drawables").on("remove",this.remove_track,this);var e=this.model.get("config");e.get("arc_dataset_height").on("change:value",this.update_track_bounds,this),e.get("track_gap").on("change:value",this.update_track_bounds,this)},get_circular_tracks:function(){return this.model.get("drawables").filter(function(t){return"DiagonalHeatmapTrack"!==t.get("track_type")})},get_chord_tracks:function(){return this.model.get("drawables").filter(function(t){return"DiagonalHeatmapTrack"===t.get("track_type")})},get_tracks_bounds:function(){var t=this.get_circular_tracks(),e=this.model.get("config").get_value("arc_dataset_height"),a=this.model.get("config").get_value("track_gap"),n=Math.min(this.$el.width(),this.$el.height())-20,i=n/2-t.length*(e+a)+a-this.label_arc_height,r=_.range(i,n/2,e+a);return d.map(r,function(t){return[t,t+e]})},render:function(){var t=this,e=t.$el.width(),a=t.$el.height(),i=this.get_circular_tracks(),r=this.get_chord_tracks(),o=t.model.get("config").get_value("total_gap"),s=this.get_tracks_bounds(),l=_.select(t.$el[0]).append("svg").attr("width",e).attr("height",a).attr("pointer-events","all").append("svg:g").call(_.behavior.zoom().on("zoom",function(){var e=n.event.scale;l.attr("transform","translate("+n.event.translate+") scale("+e+")"),t.scale!==e&&(t.zoom_drag_timeout&&clearTimeout(t.zoom_drag_timeout),t.zoom_drag_timeout=setTimeout(function(){},400))})).attr("transform","translate("+e/2+","+a/2+")").append("svg:g").attr("class","tracks");this.circular_views=i.map(function(e,a){var n=new A({el:l.append("g")[0],track:e,radius_bounds:s[a],genome:t.genome,total_gap:o});return n.render(),n}),this.chords_views=r.map(function(e){var a=new z({el:l.append("g")[0],track:e,radius_bounds:s[0],genome:t.genome,total_gap:o});return a.render(),a});var c=this.circular_views[this.circular_views.length-1].radius_bounds[1],u=[c,c+this.label_arc_height];this.label_track_view=new y({el:l.append("g")[0],track:new b,radius_bounds:u,genome:t.genome,total_gap:o}),this.label_track_view.render()},add_track:function(t){var e=this.model.get("config").get_value("total_gap");if("DiagonalHeatmapTrack"===t.get("track_type")){var a=this.circular_views[0].radius_bounds,n=new z({el:_.select("g.tracks").append("g")[0],track:t,radius_bounds:a,genome:this.genome,total_gap:e});n.render(),this.chords_views.push(n)}else{var i=this.get_tracks_bounds();d.each(this.circular_views,function(t,e){t.update_radius_bounds(i[e])}),d.each(this.chords_views,function(t){t.update_radius_bounds(i[0])});var r=this.circular_views.length,o=new A({el:_.select("g.tracks").append("g")[0],track:t,radius_bounds:i[r],genome:this.genome,total_gap:e});o.render(),this.circular_views.push(o)}},remove_track:function(t,e,a){var n=this.circular_views[a.index];this.circular_views.splice(a.index,1),n.$el.remove();var i=this.get_tracks_bounds();d.each(this.circular_views,function(t,e){t.update_radius_bounds(i[e])})},update_track_bounds:function(){var t=this.get_tracks_bounds();d.each(this.circular_views,function(e,a){e.update_radius_bounds(t[a])}),d.each(this.chords_views,function(e){e.update_radius_bounds(t[0])})}}),w=Backbone.View.extend({tagName:"g",initialize:function(t){this.bg_stroke="#ddd",this.loading_bg_fill="#ffc",this.bg_fill="#ddd",this.total_gap=t.total_gap,this.track=t.track,this.radius_bounds=t.radius_bounds,this.genome=t.genome,this.chroms_layout=this._chroms_layout(),this.data_bounds=[],this.scale=1,this.parent_elt=_.select(this.$el[0])},get_fill_color:function(){var t=this.track.get("config").get_value("block_color");return t||(t=this.track.get("config").get_value("color")),t},render:function(){var t=this.parent_elt,e=this.chroms_layout,a=_.svg.arc().innerRadius(this.radius_bounds[0]).outerRadius(this.radius_bounds[1]),n=t.selectAll("g").data(e).enter().append("svg:g").append("path").attr("d",a).attr("class","chrom-background").style("stroke",this.bg_stroke).style("fill",this.loading_bg_fill);n.append("title").text(function(t){return t.data.chrom});var i=this,r=i.track.get("data_manager"),o=!r||r.data_is_ready();$.when(o).then(function(){$.when(i._render_data(t)).then(function(){n.style("fill",i.bg_fill),i.render_labels()})})},render_labels:function(){},update_radius_bounds:function(t){this.radius_bounds=t;var e=_.svg.arc().innerRadius(this.radius_bounds[0]).outerRadius(this.radius_bounds[1]);this.parent_elt.selectAll("g>path.chrom-background").transition().duration(1e3).attr("d",e),this._transition_chrom_data(),this._transition_labels()},update_scale:function(t){var e=this.scale;if(this.scale=t,!(t<=e)){var a=this,n=new p;return this.parent_elt.selectAll("path.chrom-data").filter(function(t,e){return n.is_visible(this)}).each(function(e,n){var i,r=_.select(this),o=r.attr("chrom"),s=a.genome.get_chrom_region(o);a.track.get("data_manager").can_get_more_detailed_data(s)&&(i=a.track.get("data_manager").get_more_detailed_data(s,"Coverage",0,t),$.when(i).then(function(t){r.remove(),a._update_data_bounds();var e=d.find(a.chroms_layout,function(t){return t.data.chrom===o}),n=a.get_fill_color();a._render_chrom_data(a.parent_elt,e,t).style("stroke",n).style("fill",n)}))}),a}},_transition_chrom_data:function(){var t=this.track,e=this.chroms_layout,a=this.parent_elt.selectAll("g>path.chrom-data");if(a[0].length>0){var n=this;$.when(t.get("data_manager").get_genome_wide_data(this.genome)).then(function(i){var r=d.reject(d.map(i,function(t,a){var i=null,r=n._get_path_function(e[a],t);return r&&(i=r(t.data)),i}),function(t){return null===t}),o=t.get("config").get_value("color");a.each(function(t,e){_.select(this).transition().duration(1e3).style("stroke",o).style("fill",o).attr("d",r[e])})})}},_transition_labels:function(){},_update_data_bounds:function(t){this.data_bounds=t||this.get_data_bounds(this.track.get("data_manager").get_genome_wide_data(this.genome)),this._transition_chrom_data()},_render_data:function(t){var e=this,a=this.chroms_layout,n=this.track,i=$.Deferred();return $.when(n.get("data_manager").get_genome_wide_data(this.genome)).then(function(r){e.data_bounds=e.get_data_bounds(r),n.get("config").set_value("min_value",e.data_bounds[0],{silent:!0}),n.get("config").set_value("max_value",e.data_bounds[1],{silent:!0});var o=d.zip(a,r);d.each(o,function(a){var n=a[0],i=a[1];return e._render_chrom_data(t,n,i)});var s=e.get_fill_color();e.parent_elt.selectAll("path.chrom-data").style("stroke",s).style("fill",s),i.resolve(t)}),i},_render_chrom_data:function(t,e,a){},_get_path_function:function(t,e){},_chroms_layout:function(){var t=this.genome.get_chroms_info(),e=_.layout.pie().value(function(t){return t.len}).sort(null)(t),a=2*Math.PI*this.total_gap/t.length;return d.map(e,function(t,e){var n=t.endAngle-a;return t.endAngle=n>t.startAngle?n:t.startAngle,t})}}),y=w.extend({initialize:function(t){w.prototype.initialize.call(this,t),this.innerRadius=this.radius_bounds[0],this.radius_bounds[0]=this.radius_bounds[1],this.bg_stroke="#fff",this.bg_fill="#fff",this.min_arc_len=.05},_render_data:function(t){var e=this,a=t.selectAll("g");a.selectAll("path").attr("id",function(t){return"label-"+t.data.chrom}),a.append("svg:text").filter(function(t){return t.endAngle-t.startAngle>e.min_arc_len}).attr("text-anchor","middle").append("svg:textPath").attr("class","chrom-label").attr("xlink:href",function(t){return"#label-"+t.data.chrom}).attr("startOffset","25%").text(function(t){return t.data.chrom});var n=d.filter(this.chroms_layout,function(t){return t.endAngle-t.startAngle>e.min_arc_len});this.drawTicks(this.parent_elt,n,function(t){var a=(t.endAngle-t.startAngle)/t.value,n=_.range(0,t.value,25e6).map(function(n,i){return{radius:e.innerRadius,angle:n*a+t.startAngle,label:0===i?0:i%3?null:e.formatNum(n)}});return n.length<4&&(n[n.length-1].label=e.formatNum(Math.round((n[n.length-1].angle-t.startAngle)/a))),n},function(t){return t.angle>Math.PI?"rotate(180)translate(-16)":null})}});d.extend(y.prototype,v);var x=w.extend({initialize:function(t){w.prototype.initialize.call(this,t);var e=this.track.get("config");e.get("min_value").on("change:value",this._update_min_max,this),e.get("max_value").on("change:value",this._update_min_max,this),e.get("color").on("change:value",this._transition_chrom_data,this)},_update_min_max:function(){var t=this.track.get("config"),e=[t.get_value("min_value"),t.get_value("max_value")];this._update_data_bounds(e),this.parent_elt.selectAll(".min_max").text(function(t,a){return e[a]})},_quantile:function(t,e){return t.sort(_.ascending),_.quantile(t,e)},_render_chrom_data:function(t,e,a){var n=this._get_path_function(e,a);return n?t.datum(a.data).append("path").attr("class","chrom-data").attr("chrom",e.data.chrom).attr("d",n):null},_get_path_function:function(t,e){if("string"==typeof e||!e.data||0===e.data.length)return null;var a=_.scale.linear().domain(this.data_bounds).range(this.radius_bounds).clamp(!0),n=_.scale.linear().domain([0,e.data.length]).range([t.startAngle,t.endAngle]),i=_.svg.line.radial().interpolate("linear").radius(function(t){return a(t[1])}).angle(function(t,e){return n(e)});return _.svg.area.radial().interpolate(i.interpolate()).innerRadius(a(0)).outerRadius(i.radius()).angle(i.angle())},render_labels:function(){var t=this,e=this.drawTicks(this.parent_elt,[this.chroms_layout[0]],this._data_bounds_ticks_fn(),function(){return"rotate(90)"},!0).classed("min_max",!0);d.each(e,function(e){$(e).click(function(){new f.default.ConfigSettingCollectionView({collection:t.track.get("config")}).render_in_modal("Configure Track")})})},_transition_labels:function(){if(0!==this.data_bounds.length){var t=this,e=d.filter(this.chroms_layout,function(t){return t.endAngle-t.startAngle>.08}),a=d.filter(e,function(t,e){return e%3==0}),n=d.flatten(d.map(a,function(e){return t._data_bounds_ticks_fn()(e)}));this.parent_elt.selectAll("g.tick").data(n).transition().attr("transform",function(t){return"rotate("+(180*t.angle/Math.PI-90)+")translate("+t.radius+",0)"})}},_data_bounds_ticks_fn:function(){var t=this;return function(e){return[{radius:t.radius_bounds[0],angle:e.startAngle,label:t.formatNum(t.data_bounds[0])},{radius:t.radius_bounds[1],angle:e.startAngle,label:t.formatNum(t.data_bounds[1])}]}},get_data_bounds:function(t){}});d.extend(x.prototype,v);var A=x.extend({get_data_bounds:function(t){var e=d.flatten(d.map(t,function(t){return t?d.map(t.data,function(t){return parseInt(t[1],10)||0}):0}));return[d.min(e),this._quantile(e,.98)||d.max(e)]}}),z=w.extend({render:function(){var t=this;$.when(t.track.get("data_manager").data_is_ready()).then(function(){$.when(t.track.get("data_manager").get_genome_wide_data(t.genome)).then(function(e){var a=[],n=t.genome.get_chroms_info();d.each(e,function(e,i){var r=n[i].chrom,o=d.map(e.data,function(e){var a=t._get_region_angle(r,e[1]),n=t._get_region_angle(e[3],e[4]);return{source:{startAngle:a,endAngle:a+.01},target:{startAngle:n,endAngle:n+.01}}});a=a.concat(o)}),t.parent_elt.append("g").attr("class","chord").selectAll("path").data(a).enter().append("path").style("fill",t.get_fill_color()).attr("d",_.svg.chord().radius(t.radius_bounds[0])).style("opacity",1)})})},update_radius_bounds:function(t){this.radius_bounds=t,this.parent_elt.selectAll("path").transition().attr("d",_.svg.chord().radius(this.radius_bounds[0]))},_get_region_angle:function(t,e){var a=d.find(this.chroms_layout,function(e){return e.data.chrom===t});return a.endAngle-(a.endAngle-a.startAngle)*(a.data.len-e)/a.data.len}}),M=Backbone.View.extend({initialize:function(){h.default.cssLoadFile("static/style/circster.css");var t=new g.default.Genome(window.galaxy_config.app.genome),e=new g.default.GenomeVisualization(window.galaxy_config.app.viz_config);e.get("config").add([{key:"arc_dataset_height",label:"Arc Dataset Height",type:"int",value:25,view:"circster"},{key:"track_gap",label:"Gap Between Tracks",type:"int",value:5,view:"circster"},{key:"total_gap",label:"Gap [0-1]",type:"float",value:.4,view:"circster",hidden:!0}]),new k({el:$("#center .unified-panel-body"),genome:t,model:e}).render(),$("#center .unified-panel-header-inner").append(window.galaxy_config.app.viz_config.title+" "+window.galaxy_config.app.viz_config.dbkey);var a=m.default.create_icon_buttons_menu([{icon_class:"plus-button",title:(0,u.default)("Add tracks"),on_click:function(){g.default.select_datasets({dbkey:e.get("dbkey")},function(t){e.add_tracks(t)})}},{icon_class:"gear",title:(0,u.default)("Settings"),on_click:function(){new f.default.ConfigSettingCollectionView({collection:e.get("config")}).render_in_modal("Configure Visualization")}},{icon_class:"disk--arrow",title:(0,u.default)("Save"),on_click:function(){Galaxy.modal.show({title:(0,u.default)("Saving..."),body:"progress"}),$.ajax({url:Galaxy.root+"visualization/save",type:"POST",dataType:"json",data:{id:e.get("vis_id"),title:e.get("title"),dbkey:e.get("dbkey"),type:"trackster",vis_json:JSON.stringify(e)}}).success(function(t){Galaxy.modal.hide(),e.set("vis_id",t.vis_id)}).error(function(){Galaxy.modal.show({title:(0,u.default)("Could Not Save"),body:"Could not save visualization. Please try again later.",buttons:{Cancel:function(){Galaxy.modal.hide()}}})})}},{icon_class:"cross-circle",title:(0,u.default)("Close"),on_click:function(){window.top.location=Galaxy.root+"visualizations/list"}}],{tooltip_config:{placement:"bottom"}});a.$el.attr("style","float: right"),$("#center .unified-panel-header-inner").append(a.$el),$(".menu-button").tooltip({placement:"bottom"})}});t.default={GalaxyApp:M}});