<commit_before>Ensembl.CafeTree = {};

Ensembl.CafeTree.displayTree = function(json,species_name, panel) {  
  var tree_vis = tnt.tree();  
  var theme = Ensembl.CafeTree.tnt_theme_tree_cafe_tree()
                 .json_data(json)
                 .highlight(species_name);
  theme(tree_vis, document.getElementById('widget'), panel);  
};

Ensembl.CafeTree.tnt_theme_tree_cafe_tree = function() {
    "use strict";

    var json_data;
    var full;
    var lca;
    var color_grad;
    var min_n_members, max_n_members;
    var highlight;
    
    var width = Math.floor(d3.select("#widget").style("width").replace(/px/g,'') / 100) * 100;
    var pics_path = "/i/species/48/";

    var theme = function (tree_vis, div, panel) {      
        var full_tree;        //full tree of the current set of species
        var icons_classes = ["tree_switch", "layout_switch vertical", "resize"];        
        panel.imageToolbar(tree_vis, icons_classes);    //drawing the main toolbar and adding the icons, icons functionality below    
                
        // Switch between full tree and minimal tree
        var tree_icon = d3.select(".tree_switch")          
          .attr("title", "Choose between Full and Minimal tree")
          .on("click", function() {
              if(d3.select(".tree_menu").style("display") == 'none') {
                //just making sure all other menu are closed
                d3.selectAll(".toolbar_menu").each(function(d,i) {
                  d3.select(this).style("display", "none");
                  d3.select(".toolbar_menu").style("display", "none");
                  d3.select(".share_page").style("display", "none");
                });
                d3.select(".tree_menu").style("display", "block");
              } else {
                d3.select(".tree_menu").style("display", "none");
              }
          });
      var tree_menu = d3.select("#widget")
          .append("div")
          .attr("class", "toolbar_menu tree_menu d3_menu");

      tree_menu.append("div")
          .attr("class", "header")
          .text("Choose tree");

      tree_menu.append("div")
          .attr("class", "full current")
          .text("Full Tree")
          .on("click", function() {
              if(d3.select(".full").attr("class").match(/current/g)) {
                d3.select(".tree_menu").style("display", "none");
                return;
              } else {
                d3.select(".full").classed("current", true);
                d3.select(".minimal").classed("current", false);
                tree_vis.data(full.data());
                // We also expand all parts of the tree that are collapsed
                full_tree = tree_vis.root();
                full_tree.apply (function (node) {
                    if (node.is_collapsed()) {
                        node.toggle();
                    }
                });
                tree_vis.update();
                update_tree_label();
                d3.select(".tree_menu").style("display", "none");
              }
          });

      tree_menu.append("div")
          .attr("class", "minimal")
          .text("Minimal Tree")
          .on("click", function() {
              if(d3.select(".minimal").attr("class").match(/current/g)) {
                d3.select(".tree_menu").style("display", "none");
                return; 
              } else {
                d3.select(".minimal").classed("current", true);
                d3.select(".full").classed("current", false); 
                tree_vis.data(lca.data());
                // We also collapse all parts of the tree without members
                full_tree = tree_vis.root();
                full_tree.apply (function (node) {
                    var has_members = false;
                    node.apply (function (n) {
                        if (n.property('n_members') > 0) {
                            has_members = true;
                        }
                    });
                    if (!node.is_leaf() && !has_members) {
                        node.toggle();
                    }
                });
                tree_vis.update();
                update_tree_label();
                d3.select(".tree_menu").style("display", "none");
              }
          });          

        // Label object for the tree
         var image_label = tnt.tree.label.img()          
          .src(function(d) {
            if(d.is_leaf()) {
//TODO: change production_name to url_name once compara added it to the json 
              var species_icon = d.is_collapsed() ? "" : d.data().tax.production_name.charAt(0).toUpperCase() + d.data().tax.production_name.substr(1).toLowerCase();;  //capitalize first letter and replacing space with _ to get species image
              return d.is_collapsed() ? "" : pics_path + species_icon + ".png"; //don't return an img path for collapsed node as we dont have image for them
            }
          })
          .width(function() {
            return 30;
          })
          .height(function() {
            return 40;
          }); 
          
        var node_label = tnt.tree.label.text()
	        .color(function (node) {
            var d = node.data();
		        if (d.n_members === 0) {
		            return 'lightgrey'
		        }
            if (d.tax.scientific_name === highlight) {            
              return "red";
            }
		        return 'black';
	        })
          .text(function (node) {
            return node.data().n_members;
          })
          .fontsize(14);
    
      var root = tnt.tree.node(json_data.tree);
      var max_width_text1 = d3.max(root.get_all_leaves(), function (node) {
          return node_label.width()(node);
      });          
      
      var species_label = tnt.tree.label.text()
          .color(function (node) {
            var d = node.data();
		        if (d.n_members === 0) {
		            return 'lightgrey'
		        }
            if (d.tax.scientific_name === highlight) {            
              return "red";
            }
		        return 'black';          
          })
          .text(function (node) {
            if(node.is_leaf()) {
              return node.data().tax.alias_name;
            }
          })          
          .fontsize(14);
          
      var label = tnt.tree.label.composite()        
        .add_label(node_label.width(function () {return max_width_text1}))
        .add_label(image_label)
        .add_label(species_label);
	      
	    var cafe_tooltip = function (node) {
	        var node_data = node.data();
	        var obj = {};
          obj.header = "Taxon: " + node_data.tax.alias_name + (node_data.tax.timetree_mya ? (" ~ " + node_data.tax.timetree_mya + " MYA " ) : " ") + "(" + node_data.tax.scientific_name + ")";
          
	        obj.rows = [
		        { label : "Node ID",
		          value : node_data.id
		        },
		        { label : "Members",
		          value : node_data.n_members
		        },
		        { label : "p-value",<commit_msg>ENSWEB-3608,ENSWEB-3656: update species name and icon name based on compara api update<commit_after>Ensembl.CafeTree = {};

Ensembl.CafeTree.displayTree = function(json,species_name, panel) {  
  var tree_vis = tnt.tree();
  var theme = Ensembl.CafeTree.tnt_theme_tree_cafe_tree()
                 .json_data(json)
                 .highlight(species_name);
  theme(tree_vis, document.getElementById('widget'), panel);  
};

Ensembl.CafeTree.tnt_theme_tree_cafe_tree = function() {
    "use strict";

    var json_data;
    var full;
    var lca;
    var color_grad;
    var min_n_members, max_n_members;
    var highlight;
    
    var width = Math.floor(d3.select("#widget").style("width").replace(/px/g,'') / 100) * 100;
    var pics_path = "/i/species/48/";

    var theme = function (tree_vis, div, panel) {      
        var full_tree;        //full tree of the current set of species
        var icons_classes = ["tree_switch", "layout_switch vertical", "resize"];        
        panel.imageToolbar(tree_vis, icons_classes);    //drawing the main toolbar and adding the icons, icons functionality below    
                
        // Switch between full tree and minimal tree
        var tree_icon = d3.select(".tree_switch")          
          .attr("title", "Choose between Full and Minimal tree")
          .on("click", function() {
              if(d3.select(".tree_menu").style("display") == 'none') {
                //just making sure all other menu are closed
                d3.selectAll(".toolbar_menu").each(function(d,i) {
                  d3.select(this).style("display", "none");
                  d3.select(".toolbar_menu").style("display", "none");
                  d3.select(".share_page").style("display", "none");
                });
                d3.select(".tree_menu").style("display", "block");
              } else {
                d3.select(".tree_menu").style("display", "none");
              }
          });
      var tree_menu = d3.select("#widget")
          .append("div")
          .attr("class", "toolbar_menu tree_menu d3_menu");

      tree_menu.append("div")
          .attr("class", "header")
          .text("Choose tree");

      tree_menu.append("div")
          .attr("class", "full current")
          .text("Full Tree")
          .on("click", function() {
              if(d3.select(".full").attr("class").match(/current/g)) {
                d3.select(".tree_menu").style("display", "none");
                return;
              } else {
                d3.select(".full").classed("current", true);
                d3.select(".minimal").classed("current", false);
                tree_vis.data(full.data());
                // We also expand all parts of the tree that are collapsed
                full_tree = tree_vis.root();
                full_tree.apply (function (node) {
                    if (node.is_collapsed()) {
                        node.toggle();
                    }
                });
                tree_vis.update();
                update_tree_label();
                d3.select(".tree_menu").style("display", "none");
              }
          });

      tree_menu.append("div")
          .attr("class", "minimal")
          .text("Minimal Tree")
          .on("click", function() {
              if(d3.select(".minimal").attr("class").match(/current/g)) {
                d3.select(".tree_menu").style("display", "none");
                return; 
              } else {
                d3.select(".minimal").classed("current", true);
                d3.select(".full").classed("current", false); 
                tree_vis.data(lca.data());
                // We also collapse all parts of the tree without members
                full_tree = tree_vis.root();
                full_tree.apply (function (node) {
                    var has_members = false;
                    node.apply (function (n) {
                        if (n.property('n_members') > 0) {
                            has_members = true;
                        }
                    });
                    if (!node.is_leaf() && !has_members) {
                        node.toggle();
                    }
                });
                tree_vis.update();
                update_tree_label();
                d3.select(".tree_menu").style("display", "none");
              }
          });          

        // Label object for the tree
         var image_label = tnt.tree.label.img()          
          .src(function(d) {
            if(d.is_leaf()) {
//TODO: change production_name to url_name once compara added it to the json 
              var species_icon = d.is_collapsed() ? "" : d.data().tax.url_name;;  //url_name is name web used
              return d.is_collapsed() ? "" : pics_path + species_icon + ".png"; //don't return an img path for collapsed node as we dont have image for them
            }
          })
          .width(function() {
            return 30;
          })
          .height(function() {
            return 40;
          }); 
          
        var node_label = tnt.tree.label.text()
	        .color(function (node) {
            var d = node.data();
		        if (d.n_members === 0) {
		            return 'lightgrey'
		        }
            if (d.tax.scientific_name === highlight) {            
              return "red";
            }
		        return 'black';
	        })
          .text(function (node) {
            return node.data().n_members;
          })
          .fontsize(14);
    
      var root = tnt.tree.node(json_data.tree);
      var max_width_text1 = d3.max(root.get_all_leaves(), function (node) {
          return node_label.width()(node);
      });          
      
      var species_label = tnt.tree.label.text()
          .color(function (node) {
            var d = node.data();
		        if (d.n_members === 0) {
		            return 'lightgrey'
		        }
            if (d.tax.scientific_name === highlight) {            
              return "red";
            }
		        return 'black';          
          })
          .text(function (node) {
            if(node.is_leaf()) {
              return node.data().tax.common_name;
            }
          })          
          .fontsize(14);
          
      var label = tnt.tree.label.composite()        
        .add_label(node_label.width(function () {return max_width_text1}))
        .add_label(image_label)
        .add_label(species_label);
	      
	    var cafe_tooltip = function (node) {
	        var node_data = node.data();
	        var obj = {};
          obj.header = "Taxon: " + node_data.tax.common_name + (node_data.tax.timetree_mya ? (" ~ " + node_data.tax.timetree_mya + " MYA " ) : " ") + "(" + node_data.tax.scientific_name + ")";
          
	        obj.rows = [
		        { label : "Node ID",
		          value : node_data.id
		        },
		        { label : "Members",
		          value : node_data.n_members
		        },
		        { label : "p-value",
{'fraction_non_alphanumeric': 0.1380839951348644, 'fraction_numerical': 0.0068684267010088, 'mean_word_length': 1.470484270060092, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3208720', 'n_tokens_mistral': 4105, 'n_tokens_neox': 3827, 'n_words': 767}