<commit_before>/**
 *  widget to display a pangenome object
 * 
 */

(function( $, undefined ) {
    $.KBWidget({
        name: "kbasePanGenome",
        parent: "kbaseAuthenticatedWidget",
        version: "1.0.0",
        options: {
        	ws: null,
        	name: null,
            loadingImage: "static/kbase/images/ajax-loader.gif",
            withExport: false
        },

        pref: null,
        wsUrl: "https://kbase.us/services/ws/",  //"http://dev04.berkeley.kbase.us:7058",
        token: null,
        kbws: null,
        geneIndex: {},   // {genome_ref -> {feature_id -> feature_index}}
        genomeNames: {}, // {genome_ref -> genome_name}
        genomeRefs: {},  // {genome_ref -> workspace/genome_object_name}
        loaded: false,
        	
        init: function(options) {
            this._super(options);
            this.pref = this.genUUID();
            this.token = this.authToken();
        	var container = this.$elem;
        	container.empty();
        	container.append("<div><img src=\""+this.options.loadingImage+"\">&nbsp;&nbsp;loading pan-genome data...</div>");
            this.render();
            return this;
        },

        render: function() {
        	var self = this;
        	var ws = this.options.ws;
        	var name = this.options.name;            

        	var container = this.$elem;

        	if (!this.token) {
            	container.empty();
        		container.append("<div>[Error] You're not logged in</div>");
        		return;
        	}

        	this.kbws = new Workspace(self.wsUrl, {'token': self.token});

        	var prom = this.kbws.get_objects([{workspace:ws, name: name}])
        	$.when(prom).done(function(data) {
        		if (self.loaded)
        			return;
        		var data = data[0].data;
        		self.cacheGeneFunctions(data.genome_refs, function() {
        			buildTable(data);
        		});
        	}).fail(function(e){
        		container.empty();
        		container.append('<div class="alert alert-danger">'+
        				e.error.message+'</div>');
        	});

        	function buildTable(data) {
        		//console.log(data);

        		self.loaded = true;
        		container.empty();
        		var tabPane = $('<div id="'+self.pref+'tab-content">');
        		container.append(tabPane);
        		tabPane.kbaseTabs({canDelete : true, tabs : []});

        		var showOverview = true;
        		if (self.options.withExport)
        			showOverview = false;
        		///////////////////////////////////// Statistics ////////////////////////////////////////////
        		var tabStat = $("<div/>");
    			tabPane.kbaseTabs('addTab', {tab: 'Overview', content: tabStat, canDelete : false, show: showOverview});
        		var tableOver = $('<table class="table table-striped table-bordered" '+
        				'style="margin-left: auto; margin-right: auto;" id="'+self.pref+'overview-table"/>');
        		tabStat.append(tableOver);
        		tableOver.append('<tr><td>Pan-genome object ID</td><td>'+self.options.name+'</td></tr>');
        		tableOver.append('<tr><td>Orthologs</td><td><b>'+data.orthologs.length+'</b></td></tr>');
        		
        		var genomeStat = {};  // genome_ref -> [ortholog_count,{ortholog_id -> gene_count},genes_covered_by_orthologs]
        		for (var i in data.orthologs) {
        			var orth = data.orthologs[i];
        			var orth_id = orth.id;
        			for (var j in orth.orthologs) {
        				var gene = orth.orthologs[j];
                		var genomeRef = gene[2];
                		if (!genomeStat[genomeRef])
                			genomeStat[genomeRef] = [0, {}, 0];
                		if (!genomeStat[genomeRef][1][orth_id]) {
                			genomeStat[genomeRef][1][orth_id] = 0;
                			genomeStat[genomeRef][0]++;
                		}
                		genomeStat[genomeRef][1][orth_id]++;
                		genomeStat[genomeRef][2]++;
        			}
        		}
        		
        		for (var genomeRef in self.genomeNames) {
        			var genomeName = self.genomeNames[genomeRef];
        			var orthCount = 0;
    				var genesInOrth = 0;
        			if (genomeStat[genomeRef]) {
        				var stat = genomeStat[genomeRef];
        				orthCount = stat[0];
        				genesInOrth = stat[2];
        			}
        			var genesAll = 0;
        			for (var i in self.geneIndex[genomeRef])
        				genesAll++;
            		tableOver.append('<tr><td>'+genomeName+'</td><td><b>'+genesAll+'</b> genes, <b>'+
            				genesInOrth+'</b> of them are covered by <b>'+orthCount+'</b> orthologs</td></tr>');
        		}
        		
        		///////////////////////////////////// Orthologs /////////////////////////////////////////////
        		var tableOrth = $('<table cellpadding="0" cellspacing="0" border="0" class="table table-bordered ' +
        				'table-striped" style="width: 100%; margin-left: 0px; margin-right: 0px;">');<commit_msg>Temporary changes in pangenome widget (not tested).<commit_after>/**
 *  widget to display a pangenome object
 * 
 */

(function( $, undefined ) {
    $.KBWidget({
        name: "kbasePanGenome",
        parent: "kbaseAuthenticatedWidget",
        version: "1.0.0",
        options: {
        	ws: null,
        	name: null,
            loadingImage: "static/kbase/images/ajax-loader.gif",
            withExport: false
        },

        pref: null,
        wsUrl: "https://kbase.us/services/ws/",  //"http://dev04.berkeley.kbase.us:7058",
        token: null,
        kbws: null,
        geneIndex: {},   // {genome_ref -> {feature_id -> feature_index}}
        genomeNames: {}, // {genome_ref -> genome_name}
        genomeRefs: {},  // {genome_ref -> workspace/genome_object_name}
        loaded: false,
        	
        init: function(options) {
            this._super(options);
            this.pref = this.genUUID();
            this.token = this.authToken();
        	var container = this.$elem;
        	container.empty();
        	container.append("<div><img src=\""+this.options.loadingImage+"\">&nbsp;&nbsp;loading pan-genome data...</div>");
            this.render();
            return this;
        },

        render: function() {
        	var self = this;
        	var ws = this.options.ws;
        	var name = this.options.name;            

        	var container = this.$elem;

        	if (!this.token) {
            	container.empty();
        		container.append("<div>[Error] You're not logged in</div>");
        		return;
        	}

        	this.kbws = new Workspace(self.wsUrl, {'token': self.token});

        	var prom = this.kbws.get_objects([{workspace:ws, name: name}])
        	$.when(prom).done(function(data) {
        		if (self.loaded)
        			return;
        		var data = data[0].data;
        		self.cacheGeneFunctions(data.genome_refs, function() {
        			buildTable(data);
        		});
        	}).fail(function(e){
        		container.empty();
        		container.append('<div class="alert alert-danger">'+
        				e.error.message+'</div>');
        	});

        	function buildTable(data) {
        		//console.log(data);

        		self.loaded = true;
        		container.empty();
        		var tabPane = $('<div id="'+self.pref+'tab-content">');
        		container.append(tabPane);
        		tabPane.kbaseTabs({canDelete : true, tabs : []});

        		var showOverview = true;
        		if (self.options.withExport)
        			showOverview = false;
        		///////////////////////////////////// Statistics ////////////////////////////////////////////
        		var tabStat = $("<div/>");
    			tabPane.kbaseTabs('addTab', {tab: 'Overview', content: tabStat, canDelete : false, show: showOverview});
        		var tableOver = $('<table class="table table-striped table-bordered" '+
        				'style="margin-left: auto; margin-right: auto;" id="'+self.pref+'overview-table"/>');
        		tabStat.append(tableOver);
        		tableOver.append('<tr><td>Pan-genome object ID</td><td>'+self.options.name+'</td></tr>');
        		
        		var genomeStat = {};  // genome_ref -> [ortholog_count,{ortholog_id -> gene_count},genes_covered_by_orthologs]
        		var orthologStat ={};  // ortholog_id -> {genome_ref -> gene_count(>0)}
        		for (var i in data.orthologs) {
        			var orth = data.orthologs[i];
        			var orth_id = orth.id;
        			if (!orthologStat[orth_id])
        				orthologStat[orth_id] = {};
        			for (var j in orth.orthologs) {
        				var gene = orth.orthologs[j];
                		var genomeRef = gene[2];
                		if (!genomeStat[genomeRef])
                			genomeStat[genomeRef] = [0, {}, 0];
                		if (!genomeStat[genomeRef][1][orth_id]) {
                			genomeStat[genomeRef][1][orth_id] = 0;
                			genomeStat[genomeRef][0]++;
                		}
                		genomeStat[genomeRef][1][orth_id]++;
                		genomeStat[genomeRef][2]++;
                		if (orthologStat[orth_id][genomeRef]) {
                			orthologStat[orth_id][genomeRef]++;
                		} else {
                			orthologStat[orth_id][genomeRef] = 1;
                		}
        			}
        		}
        		var totalGenes = 0;
        		var totalGenomes = 0;
        		var genomeOrder = [];
        		for (var genomeRef in self.geneIndex) {
        			totalGenomes++;
        			for (var i in self.geneIndex[genomeRef])
        				totalGenes++;
        			genomeOrder.push([genomeRef, self.genomeNames[genomeRef]]);
        		}
        		genomeOrder.sort(function(a, b) {
                    if (a[1] < b[1]) return -1;
                    if (a[1] > b[1]) return 1;
                    return 0;
                });
        		tableOver.append('<tr><td>Total # of genomes</td><td><b>'+totalGenomes+'</b></td></tr>');
        		tableOver.append('<tr><td>Total # of genes</td><td><b>'+totalGenes+'</b></td></tr>');
        		tableOver.append('<tr><td>Total # of orthologs</td><td><b>'+data.orthologs.length+'</b></td></tr>');        		
        		for (var genomePos in genomeOrder) {
        			var genomeRef = genomeOrder[genomePos][0];
        			var genomeName = self.genomeNames[genomeRef];
        			var orthCount = 0;
    				var genesInOrth = 0;
        			if (genomeStat[genomeRef]) {
        				var stat = genomeStat[genomeRef];
        				orthCount = stat[0];
        				genesInOrth = stat[2];
        			}
        			var genesAll = 0;
        			for (var i in self.geneIndex[genomeRef])
        				genesAll++;
            		tableOver.append('<tr><td>'+genomeName+'</td><td><b>'+genesAll+'</b> genes, <b>'+
            				genesInOrth+'</b> of them are covered by <b>'+orthCount+'</b> orthologs</td></tr>');
        		}
        		
        		///////////////////////////////////// Shared orthologs ////////////////////////////////////////////
        		var tabShared = $("<div/>");
    			tabPane.kbaseTabs('addTab', {tab: 'Shared orthologs', content: tabShared, canDelete : false, show: false});
        		var tableShared = $('<table class="table table-striped table-bordered" '+
        				'style="margin-left: auto; margin-right: auto;" id="'+self.pref+'shared-table"/>');
        		tabShared.append(tableShared);
        		var header = "";
        		for (var genomePos in genomeOrder)
        			header += '<td width="50">G' + (genomePos + 1) + '</td>';
        		tableShared.append('<tr>'+header+'<td/></tr>');
        		for (var genomePos in genomeOrder) {
        			var genomeRef = genomeOrder[genomePos][0];
            		var row = "";
            		for (var genomePos2 in genomeOrder) {
            			var genomeRef2 = genomeOrder[genomePos2][0];
            			var count = 0;
            			for (var orth_id in orthologStat) {
            				if (orthologStat[orth_id][genomeRef] && orthologStat[orth_id][genomeRef2])
            					count++;
            			}
            			row += '<td width="50">' + count + '</td>';
            		}
            		tableShared.append('<tr>'+row+'<td>G'+(genomePos + 1)+', '+genomeOrder[genomePos][1]+'</td></tr>');
        		}

        		///////////////////////////////////// Orthologs /////////////////////////////////////////////
        		var tableOrth = $('<table cellpadding="0" cellspacing="0" border="0" class="table table-bordered ' +
        				'table-striped" style="width: 100%; margin-left: 0px; margin-right: 0px;">');
{'fraction_non_alphanumeric': 0.1839080459770115, 'fraction_numerical': 0.007285089849441476, 'mean_word_length': 1.6661631419939578, 'pattern_counts': {'":': 0, '<': 107, '<?xml version=': 0, '>': 122, 'https://': 2, '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': '265938', 'n_tokens_mistral': 4145, 'n_tokens_neox': 3832, 'n_words': 667}