2 * selection index form, calculation and presentation
3 * @author Isaak Y Tecle <iyt2@cornell.edu>
7 JSAN.use('jquery.blockUI');
10 jQuery(document).ready( function () {
12 setTimeout(function(){listSelectionIndexPopulations()}, 5000);
17 jQuery("#rank_genotypes").live("click", function() {
18 var modelId = jQuery("#model_id").val();
19 var selectionPopId = jQuery("#selected_population_id").val();
20 var popType = jQuery("#selected_population_type").val();
22 selectionIndex(modelId, selectionPopId);
26 function listSelectionIndexPopulations () {
28 var modelData = getTrainingPopulationData();
29 var trainingPopIdName = JSON.stringify(modelData);
31 var popsList = '<dl id="selected_population" class="si_dropdown">'
32 + '<dt> <a href="#"><span>Choose a population</span></a></dt>'
36 + '<a href="#">' + modelData.name + '<span class=value>' + trainingPopIdName + '</span></a>'
39 popsList += '</ul></dd></dl>';
41 jQuery("#select_a_population_div").empty().append(popsList).show();
44 if( modelData.id.match(/uploaded/) == null) {
45 dbSelPopsList = addSelectionPopulations();
49 jQuery("#select_a_population_div ul").append(dbSelPopsList);
52 var userUploadedSelExists = jQuery("#uploaded_selection_pops_table").doesExist();
53 if( userUploadedSelExists == true) {
54 var userSelPops = listUploadedSelPopulations();
57 jQuery("#select_a_population_div ul").append(userSelPops);
61 getSelectionPopTraits(modelData.id, modelData.id);
64 jQuery(".si_dropdown dt a").click(function() {
65 jQuery(".si_dropdown dd ul").toggle();
68 jQuery(".si_dropdown dd ul li a").click(function() {
70 var text = jQuery(this).html();
72 jQuery(".si_dropdown dt a span").html(text);
73 jQuery(".si_dropdown dd ul").hide();
75 var idPopName = jQuery("#selected_population").find("dt a span.value").html();
76 idPopName = JSON.parse(idPopName);
77 modelId = jQuery("#model_id").val();
79 selectedPopId = idPopName.id;
80 selectedPopName = idPopName.name;
81 selectedPopType = idPopName.pop_type;
83 jQuery("#selected_population_name").val(selectedPopName);
84 jQuery("#selected_population_id").val(selectedPopId);
85 jQuery("#selected_population_type").val(selectedPopType);
87 getSelectionPopTraits(modelId, selectedPopId);
91 jQuery(".si_dropdown").bind('click', function(e) {
92 var clicked = jQuery(e.target);
94 if (! clicked.parents().hasClass("si_dropdown"))
95 jQuery(".si_dropdown dd ul").hide();
102 function addSelectionPopulations(){
104 var selPopsTable = jQuery("#selection_pops_list").html();
105 var selPopsRows = jQuery(selPopsTable).find("tr");
107 var predictedPop = [];
110 for (var i = 1; i < selPopsRows.length; i++) {
111 var row = selPopsRows[i];
112 var popRow = row.innerHTML;
114 predictedPop = popRow.match(/\/solgs\/selection\//g);
117 if (predictedPop.length > 1) {
118 var selPopsInput = row.getElementsByTagName("input")[0];
119 var idPopName = selPopsInput.value;
120 var idPopNameCopy = idPopName;
121 idPopNameCopy = JSON.parse(idPopNameCopy);
122 var popName = idPopNameCopy.name;
125 + '<a href="#">' + popName + '<span class=value>' + idPopName + '</span></a>'
135 function getSelectionPopTraits (modelId, selectedPopId) {
137 if (modelId === selectedPopId) {selectedPopId=undefined;}
142 url: '/solgs/selection/index/form',
143 data: {'pred_pop_id': selectedPopId, 'training_pop_id': modelId},
144 success: function(res) {
146 if (res.status == 'success') {
148 var traits = res.traits;
150 if (traits.length > 1) {
151 table = selectionIndexForm(traits);
153 var msg = 'There is only one trait with valid GEBV predictions.';
154 jQuery("#select_a_population_div").empty();
155 jQuery("#select_a_population_div_text").empty().append(msg);
158 jQuery('#selection_index_form').empty().append(table);
166 function selectionIndexForm(predictedTraits) {
171 for (var i=0; i < predictedTraits.length; i++) {
172 var tdCell = '<td>' + predictedTraits[i] + ':</td>';
176 rowTag = '</tr><tr>';
180 + '<td><input type="text" name=' + predictedTraits[i]
185 if (cnt === 3 ) { cnt=0;}
190 var rankButton = '<tr><td>'
191 + '<input style="position:relative;" " class="button" type="submit" value="Calculate" name= "rank" id="rank_genotypes"'
194 var table = '<br /> <table id="selection_index_table" style="align:left;width:90%"><tr>'
203 function applySelectionIndex(params, legend, trainingPopId, predictionPopId) {
206 jQuery.blockUI.defaults.applyPlatformOpacityRules = false;
207 jQuery.blockUI({message: 'Please wait..'});
211 if (!predictionPopId) {
212 predictionPopId = 'undef';
215 var action = '/solgs/calculate/selection/index/' + trainingPopId + '/' + predictionPopId;
222 success: function(res){
223 var suc = res.status;
225 if (suc == 'success' ) {
227 var genos = new Object();
229 genos = res.genotypes;
230 var download_link = res.link;
231 var indexFile = res.index_file;
233 table = '<table style="text-align:left; border:0px; padding: 1px; width:75%;">';
234 table += '<tr><th>Genotypes</th><th>Selection indices</th></tr>';
237 for (var geno in genos) {
238 sorted.push([geno, genos[geno]]);
239 sorted = sorted.sort(function(a, b) {return b[1] - a[1]});
242 for (var i=0; i<sorted.length; i++) {
245 + sorted[i][0] + '</td>' + '<td>'
246 + sorted[i][1] + '</td>';
251 table += '<br>[ ' + download_link + ' ]';
252 table += '<br>' + legend + '<br/><br/>';
254 table = res.status + ' Ranking the genotypes failed..Please report the problem.';
257 jQuery('#top_genotypes').append(table).show();
258 jQuery('#selected_pop').val('');
262 if (predictionPopId && predictionPopId !== trainingPopId) {
263 popId = predictionPopId;
266 popId = trainingPopId;
270 formatGenCorInputData(popId, type, indexFile);
272 jQuery("#si_correlation_message")
273 .css({"padding-left": '0px'})
274 .html("Running correlation analysis...");
277 error: function(res){
278 alert('error occured calculating selection index.');
286 function validateRelativeWts(nm, val) {
288 if (isNaN(val) && nm != 'all') {
289 alert('the relative weight of trait '+nm+
293 } else if (!val && nm != 'all') {
294 alert('You need to assign a relative weight to trait '+nm+'.'
295 +' If you want to exclude the trait assign 0 to it.'
298 // }// else if (val < 0 && nm != 'all') {
299 // alert('The relative weight to trait '+nm+
300 // ' must be a positive number.'
303 } else if (nm == 'all' && val == 0) {
304 alert('At least two traits must be assigned relative weight.');
312 function sumElements (elements) {
314 for (var i=0; i<elements.length; i++) {
315 if (!isNaN(elements[i])) {
316 sum = parseFloat(sum) + parseFloat(elements[i]);
323 function selectionIndex ( trainingPopId, predictionPopId ) {
325 if (!predictionPopId) {
326 predictionPopId = jQuery("#default_selected_population_id").val();
329 var legendValues = legendParams();
331 var legend = legendValues.legend;
332 var params = legendValues.params;
333 var validate = legendValues.validate;
335 if (params && validate) {
336 applySelectionIndex(legendValues.params, legendValues.legend, trainingPopId, predictionPopId);
341 function legendParams () {
343 var predPopName = jQuery("#selected_population_name").val();
346 predPopName = jQuery("#default_selected_population_name").val();
349 var rel_form = document.getElementById('selection_index_form');
350 var all = rel_form.getElementsByTagName('input');
351 var params, validate;
354 var legend = "<div id=\"si_legend_"
355 + predPopName.replace(/\s/g, "")
358 legend += '<b>Relative weights</b>:';
360 for (var i = 0; i < all.length; i++) {
361 var nm = all[i].name;
362 var val = all[i].value;
364 if (val != 'Calculate') {
365 if (nm != 'prediction_pop_name') {
368 validate = validateRelativeWts(nm, val);
374 params = params +'&'+ nm + '=' + val;
376 legend += '<b> ' + nm + '</b>' + ': '+ val;
382 var sum = sumElements(allValues);
383 validate = validateRelativeWts('all', sum);
385 for (var i=0; i<allValues.length; i++) {
386 // (isNaN(allValues[i]) || allValues[i] < 0)
387 if (isNaN(allValues[i])) {
393 legend += '<br/><b>Name</b>: ' + predPopName + '<br/></div';
396 return {'legend' : legend,
398 'validate' : validate
403 function listUploadedSelPopulations () {
405 var selPopsDivUploaded = document.getElementById("uploaded_selection_populations");
406 var selPopsTableUploaded = selPopsDivUploaded.getElementsByTagName("table");
407 var selPopsRowsUploaded = selPopsTableUploaded[0].rows;
408 var predictedPopUploaded = [];
411 for (var i = 1; i < selPopsRowsUploaded.length; i++) {
412 var row = selPopsRowsUploaded[i];
413 var popRow = row.innerHTML;
415 predictedPopUploaded = popRow.match(/\/solgs\/selection\//g);
417 if (predictedPopUploaded) {
418 var selPopsInput = row.getElementsByTagName("input")[0];
419 var idPopName = selPopsInput.value;
420 var idPopNameCopy = idPopName;
421 idPopNameCopy = JSON.parse(idPopNameCopy);
422 var popName = idPopNameCopy.name;
425 + '<a href="#">' + popName + '<span class=value>' + idPopName + '</span></a>'
428 popsList = undefined;
436 function getTrainingPopulationData () {
438 var modelId = jQuery("#model_id").val();
439 var modelName = jQuery("#model_name").val();
440 var popType = jQuery("#default_selected_population_type").val();
442 return {'id' : modelId, 'name' : modelName, 'pop_type': popType};