Commit 17efa4c1 authored by Jerome Mariette's avatar Jerome Mariette
Browse files

allow javascript for analyses

parent e2f2c88b
/***************************************************************
* Copyright notice
*
* (c) 2009 PF bioinformatique de Toulouse <>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Plugin 'nG6' for the 'nG6' extension.
*
* @author PF bioinformatique de Toulouse <>
*/
$(function () {
/*
* Define functions in charge to visualize phylogenic bar charts
*/
$(".phylogeny-bar-view-btn").click(function() {
if ($(":checked[id^=chk_sample_]").size() == 0) {
$("#user_information_dialog").dialog("option", "title", $("#information_error_title").val());
$("#user_information_dialog").html("<div class='tx-nG6-pi1-error'>" + $("#information_dialog_zero_check").val() + "</div>").dialog("open");
var buttons = {};
buttons[$("#ok_btn_label").val()] = function(){ $(this).dialog("close"); }
$("#user_information_dialog").dialog('option', 'buttons', buttons);
} else if ($(":checked[id^=chk_sample_]").size() == 1) {
$("#img_dialog").dialog("option", "title", "nG6 - " + $("#analyse_name").val());
$("#img_dialog").html('<div id="highcharts_container"></div>').dialog("open");
$("#img_dialog").dialog("option", "width", 850);
var index = $(":checked[id^=chk_sample_]").attr("id").split("_")[2];
$.get($("#phylogeny_file_"+index).val(), function(data) {
// First convert mothur_phylo to hash table
var lines = data.split("\n");
var current_level = 0;
var full_phylo = "";
var taxonomy = new Array();
level_1_keys = new Array();
level_1_values = new Array();
for (i=0; i<lines.length; i++) {
parts = lines[i].split("\t");
if (parts[0] != "taxlevel" && parts[0] != "0") {
// If this is a 1st level
if (parts[0] == "1") {
level_1_keys.push(parts[2]);
level_1_values.push(parseInt(parts[4]));
}
// If it's a sub level: children
if (parseInt(parts[0]) > current_level) {
// If it's a leaf
if (parseInt(parts[3]) == 0) {
full_phylo += parts[2];
taxonomy.push(full_phylo+"\t"+parts[3]+"\t"+parts[4]);
full_phylo += ";";
// It's a node
} else {
full_phylo += parts[2];
taxonomy.push(full_phylo+"\t"+parts[3]+"\t"+parts[4]);
full_phylo += ";";
}
// If it's the same level: siblings
} else if (parseInt(parts[0]) == current_level) {
var phylo_tab = full_phylo.split(";");
full_phylo = "";
var indice = phylo_tab.length-2;
for (j=0;j<indice;j++) {
full_phylo += phylo_tab[j] + ";";
}
// If it's a leaf
if (parseInt(parts[3]) == 0) {
full_phylo += parts[2];
taxonomy.push(full_phylo+"\t"+parts[3]+"\t"+parts[4]);
full_phylo += ";";
// It's a node
} else {
full_phylo += parts[2];
taxonomy.push(full_phylo+"\t"+parts[3]+"\t"+parts[4]);
full_phylo += ";";
}
// If it's a higher level
} else {
var phylo_tab = full_phylo.split(";");
full_phylo = "";
var indice = phylo_tab.length-(current_level-parseInt(parts[0]))-2;
for (j=0;j<indice;j++) {
full_phylo += phylo_tab[j] + ";";
}
// If it's a leaf
if (parseInt(parts[3]) == 0) {
full_phylo += parts[2];
taxonomy.push(full_phylo+"\t"+parts[3]+"\t"+parts[4]);
full_phylo += ";";
// It's a node
} else {
full_phylo += parts[2];
taxonomy.push(full_phylo+"\t"+parts[3]+"\t"+parts[4]);
full_phylo += ";";
}
}
current_level = parseInt(parts[0]);
}
}
var colors = new Array("#4572A7","#AA4643","#89A54E","#80699B","#3D96AE","#DB843D","#92A8CD","#A47D7C","#B5CA92", "#FFF718", "#ED00D4", "#D5B0FF", "#91E2F9", "#FFB87E", "#FFFCA3", "#FFA0F5");//Highcharts.getOptions().colors;
var categories = level_1_keys;
var name = '';
data = []
for (i=0; i<level_1_keys.length; i++) {
var sub_taxonomy = new Array();
for (j=0; j<taxonomy.length; j++){
// Produce the sub table
var value = taxonomy[j].split("\t")[0];
if (taxonomy[j].indexOf(level_1_keys[i]) === 0 && level_1_keys[i] != value) {
sub_taxonomy.push(taxonomy[j]);
}
}
data.push({
y: level_1_values[i],
color: colors[i],
drilldown: {
name: level_1_keys[i],
categories: get_categories(sub_taxonomy, 1),
data: get_data(sub_taxonomy, 1, colors),
color: colors[i]
}
});
}
function setChart(name, categories, data, color) {
chart.xAxis[0].setCategories(categories);
chart.series[0].remove();
chart.addSeries({
name: name,
data: data,
color: 'white'
});
}
chart = new Highcharts.Chart({
chart: {
renderTo: 'highcharts_container',
type: 'column'
},
title: {
text: $("#bar_title").val().replace("###DB_NAME###", $("#phylogeny_file_"+index).val().split(".").slice(-3)[0])
},
subtitle: {
text: ''
},
xAxis: {
categories: categories,
labels: {
rotation: -45,
align: 'right'
}
},
yAxis: {
title: {
text: $("#nb_sequences").val()
}
},
legend: {
enabled: true
},
credits: { enabled: false },
plotOptions: {
column: {
cursor: 'pointer',
point: {
events: {
click: function() {
var drilldown = this.drilldown;
if (drilldown) { // drill down
setChart(drilldown.name, drilldown.categories, drilldown.data, drilldown.color);
} else { // restore
setChart(name, categories, data);
}
}
}
},
dataLabels: {
enabled: true,
color: colors[0],
style: {
fontWeight: 'bold'
},
formatter: function() {
return this.y;
}
}
}
},
tooltip: {
formatter: function() {
var point = this.point,
s = this.x +':<b>'+ this.y +' ' + $("#sequences").val() + '</b><br/>';
if (point.drilldown) {
s += $("#click_to_view").val().replace("###CATEGORY###", point.category);
} else {
s += $("#click_to_return").val();
}
return s;
}
},
series: [{
name: name,
data: data,
color: 'white'
}]
});
});
} else {
/* Function in charge to create the chart */
function updateChart(rankid) {
var ajax = new Array();
var samples = new Array();
var index = "";
$(":checked[id^=chk_sample_]").each(function(){
index = $(this).attr("id").split("_")[2];
ajax.push($.ajax($("#phylogeny_file_"+index).val()));
samples.push($("#sample_id_"+index).html());
});
$.when.apply($, ajax).done(function(){
var taxonomy = new Array();
var taxonomy_val = new Array();
var samples_taxonomy = new Array();
var taxlevel = rankid.split(".").length - 1;
// for each taxonomy files
for( var i = 0; i < arguments.length; i++ ) {
samples_taxonomy.push(new Array());
var rank_found = false;
var lines = arguments[i][0].split("\n");
for (j=0; j<lines.length; j++) {
parts = lines[j].split("\t");
if (rank_found && parseInt(parts[0]) == taxlevel) {
break;
}
if (rank_found && parseInt(parts[0]) == taxlevel + 1) {
if (taxonomy.indexOf(parts[2].replace(/"/g, '')) === -1) {
taxonomy.push(parts[2].replace(/"/g, ''));
taxonomy_val.push(parts[1]);
}
samples_taxonomy[i][parts[1]] = parts[4];
}
if (parts[1] == rankid) {
rank_found = true;
}
}
}
var series_table = new Array();
for (i in samples) {
data_table = new Array();
for (j in taxonomy_val) {
if (taxonomy_val[j] in samples_taxonomy[i]) {
data_table.push(parseInt(samples_taxonomy[i][taxonomy_val[j]]))
} else {
data_table.push(0);
}
}
series_table.push({name:samples[i], data:data_table});
}
chart = new Highcharts.Chart({
chart: {
renderTo: 'highcharts_container',
defaultSeriesType: 'column'
},
credits: { enabled: false },
title: {
text: $("#bar_title").val().replace("###DB_NAME###", $("#phylogeny_file_"+index).val().split(".").slice(-3)[0])
},
subtitle: {
text: ''
},
xAxis: {
categories: taxonomy,
labels: {
rotation: -45,
align: 'right'
}
},
yAxis: {
min: 0,
title: {
text: $("#nb_sequences").val()
}
},
legend: {
layout: 'vertical',
backgroundColor: '#FFFFFF',
align: 'left',
verticalAlign: 'top',
x: 100,
y: 70,
floating: true,
shadow: true
},
tooltip: {
formatter: function() {
return ''+
this.x +': '+ this.y +' '+ $("#sequences").val();
}
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0
}
},
series: series_table
});
});
}
$("#img_dialog").dialog("option", "title", "nG6 - " + $("#analyse_name").val());
$("#img_dialog").dialog("option", "width", 1200);
$("#img_dialog").html('<div id="navigation_container" style="float: left; width: 280px;"></div><div id="highcharts_container" style="width: 900px;"></div>').dialog("open");
var ajax = new Array();
var samples = new Array();
$(":checked[id^=chk_sample_]").each(function(){
var index = $(this).attr("id").split("_")[2];
ajax.push($.ajax($("#phylogeny_file_"+index).val()));
samples.push($("#phylogeny_file_"+index).val());
});
// Init the navigation
$.when.apply($, ajax).done(function(){
$("#navigation_container").html('<p class="information">'+$("#navigation_txt").val() + '</p><div class="underline"> </div><br /><div id="jstree_navigation"></div>');
var html_data = $("<ul></ul>");
var current_level = -1;
var parentl = ""
for( var i = 0; i < arguments.length; i++ ) {
var lines = arguments[i][0].split("\n");
for (j=0; j<lines.length; j++) {
if (lines[j] != "") {
mparts = lines[j].split("\t");
if (mparts[0] != "taxlevel" && mparts[0] != "9") {
// If this guy doesnt exist yet
if (html_data.find("#li_"+mparts[1].replace(/\./g, '_')).length == 0) {
parentl = mparts[1].split(".").slice(0,-1).join("_");
// If no parent yet, this is the root
if (html_data.find("#li_"+parentl).length == 0) {
html_data.append('<li id=li_'+mparts[1].replace(/\./g, '_')+'><a class="taxonomy_click" id="'+mparts[1].replace(/\./g, '_')+'">'+mparts[2]+'</a><ul></ul></li>');
// Otherwise append it to the parent
} else {
if (mparts[0] != "8") {
html_data.find("#li_"+parentl+" > ul").append('<li id=li_'+mparts[1].replace(/\./g, '_')+'><a class="taxonomy_click" id="'+mparts[1].replace(/\./g, '_')+'">'+mparts[2]+'</a><ul></ul></li>');
} else {
html_data.find("#li_"+parentl+" > ul").append('<li id=li_'+mparts[1].replace(/\./g, '_')+'><a class="taxonomy_click" id="'+mparts[1].replace(/\./g, '_')+'">'+mparts[2]+'</a></li>');
}
}
}
}
}
}
}
$("#jstree_navigation").jstree({
"themes" : {
"theme" : "default",
"icons" : false
},
"ui" : {
"select_limit" : 1,
"selected_parent_close" : "select_parent",
"initially_select" : [ "li_0" ]
},
"html_data" : {
"data" : html_data
},
"plugins" : [ "themes", "html_data", "ui" ]
});
$("a.taxonomy_click").click(function(){
updateChart($(this).attr("id").replace(/_/g, '.'));
});
});
// Init the chart
updateChart("0");
}
});
function get_categories(lines, depth) {
var cathegories = new Array();
for (var x=0;x<lines.length;x++) {
parts = lines[x].split(";");
// If this is the right depth
if (depth+1 == parts.length) {
var values = lines[x].split("\t");
var final_value = values[0].split(";");
cathegories.push(final_value[final_value.length-1].replace(/"/g, ''));
}
}
return cathegories;
}
function get_data(lines, depth, colors) {
var data = new Array();
var current_level = new Array();
for (var i=0;i<lines.length;i++) {
parts = lines[i].split(";");
// If this is the right depth
if (depth+1 == parts.length) {
current_level.push(lines[i]);
}
}
for (var i=0;i<current_level.length;i++) {
parts = current_level[i].split("\t");
// If it's a leaf
if (parts[1] == "0") {
data.push({y:parseInt(parts[2]), color: colors[i]});
// It's a node
} else {
var sub_taxonomy = new Array();
for (j=0; j<lines.length; j++){
// Produce the sub table
var value = lines[j].split("\t")[0];
if (lines[j].indexOf(parts[0]) === 0 && parts[0] != value) {
sub_taxonomy.push(lines[j]);
}
}
var final_name = parts[0].replace(/"/g, '');
data.push({y:parseInt(parts[2]), color: colors[i], drilldown:{name:final_name, categories:get_categories(sub_taxonomy, depth+1), data:get_data(sub_taxonomy, depth+1, colors)}});
}
}
return data;
}
/*$(".phylogeny-tree-view-btn").click(function() {
if ($(":checked[id^=chk_sample_]").size() == 0) {
$("#user_information_dialog").dialog("option", "title", $("#information_error_title").val());
$("#user_information_dialog").html("<div class='tx-nG6-pi1-error'>" + $("#information_dialog_zero_check").val() + "</div>").dialog("open");
var buttons = {};
buttons[$("#ok_btn_label").val()] = function(){ $(this).dialog("close"); }
$("#user_information_dialog").dialog('option', 'buttons', buttons);
} else if ($(":checked[id^=chk_sample_]").size() == 1) {
$("#img_dialog").dialog("option", "title", "nG6 - " + $("#analyse_name").val());
var init_control = false;
if ($("#svgCanvas_control").html() == "") {init_control=true;}
$("#img_dialog").html('<div id="svgCanvas_control" class="tx-nG6-wait"></div><div id="svgCanvas"></div>').dialog("open");
var index = $(":checked[id^=chk_sample_]").attr("id").split("_")[2];
$.ajax({
url: $("#phylogeny_file_"+index).val(),
success: function(data){
var xml_data = '<phyloxml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.phyloxml.org http://www.phyloxml.org/1.10/phyloxml.xsd" xmlns="http://www.phyloxml.org">';
xml_data += '<phylogeny rooted="false">';
xml_data += '<render>';
xml_data += ' <parameters>';
xml_data += ' <circular>';
xml_data += ' <bufferRadius>0.5</bufferRadius>';
xml_data += ' </circular>';
xml_data += ' <rectangular>';
xml_data += ' <alignRight>1</alignRight>';
xml_data += ' <bufferX>300</bufferX>';
xml_data += ' </rectangular>';
xml_data += ' </parameters>';
xml_data += ' <charts>';
xml_data += ' <content type="bar" fill="#666" width="0.2" />';
xml_data += ' </charts>';
xml_data += ' <styles>';
xml_data += ' <barChart fill="#333" stroke-width="0" />';
xml_data += ' </styles>';
xml_data += '</render>';
xml_data += '<clade>';
var current_level = 0;
var annot = '';
var lines = data.split("\n");
var lists = new Array();
for (i=0; i<lines.length; i++) {
parts = lines[i].split("\t");
if (lines[i] != "" && parts[0] != "taxlevel" && parts[0] != "0") {
var indent = Array(parseInt(parts[0])*4).join(" ");
// If it's a sub level: children
if (parseInt(parts[0]) >= current_level) {
// If it's a leaf
if (parseInt(parts[3]) == 0) {
annot += parts[2];
xml_data += indent + '<clade>';
xml_data += indent + ' <name>'+parts[2]+'</name>';
xml_data += indent + ' <branch_length>1.0</branch_length>';
xml_data += indent + ' <annotation>';
xml_data += indent + ' <desc>'+annot+'</desc>';
xml_data += indent + ' </annotation>';
xml_data += indent + ' <chart>';
xml_data += indent + ' <content>'+parts[4]+'</content>';
xml_data += indent + ' </chart>';
xml_data += indent + '</clade>';
var annot_parts = annot.split(";");
annot_parts.pop();
annot = annot_parts.join(";");
// It's a node
} else {
annot += parts[2] + ";";
xml_data += indent + ' <clade>';
xml_data += indent + ' <branch_length>1.0</branch_length>';
}
// If it's a higher level
} else {
for (j=0; j<current_level-parseInt(parts[0]);j++) {
var ind = Array((current_level-j-1)*4).join(" ");
xml_data += ind + '</clade>';
var annot_parts = annot.split(";");
annot_parts.pop();
annot = annot_parts.join(";");
}
// If it's a leaf
if (parseInt(parts[3]) == 0) {
annot += parts[2];
xml_data += indent + '<clade>';
xml_data += indent + ' <name>'+parts[2]+'</name>';
xml_data += indent + ' <branch_length>1.0</branch_length>';
xml_data += indent + ' <annotation>';
xml_data += indent + ' <desc>'+annot+'</desc>';
xml_data += indent + ' </annotation>';
xml_data += indent + ' <chart>';
xml_data += indent + ' <content>'+parts[4]+'</content>';
xml_data += indent + ' </chart>';
xml_data += indent + '</clade>';
var annot_parts = annot.split(";");
annot_parts.pop();
annot = annot_parts.join(";");
// It's a node
} else {
annot += parts[2] + ';';
xml_data += indent + ' <clade>';
xml_data += indent + ' <branch_length>1.0</branch_length>';
}
}
current_level = parseInt(parts[0]);
}
}
for (j=0; j<current_level-1;j++) {
var ind = Array((current_level-1)*4).join(" ");
xml_data += ind + '</clade>';
}
xml_data += '</clade>';
xml_data += '</phylogeny>';
xml_data += '</phyloxml>';
var lists = new Array();
lists[] = new Array("Bacteria")
var controler_html = 'Display tree for ';
controler_html += '<select>';
var dataObject = {
xml: xml_data,
fileSource: false
};
phylocanvas = new Smits.PhyloCanvas(
dataObject,
'svgCanvas',
800, 800,
'linear'
);
$("#svgCanvas_control").removeClass("tx-nG6-wait");
$("#svgCanvas_control").html(controler_html);
init(); //unitip
}
});
} else {
alert("no more than 1");
}
});*/
});
/***************************************************************