Merge pull request #5205 from solgenomics/topic/generic_trial_upload
[sgn.git] / js / source / legacy / solGS / combineTrials.js
blob95e1022ed284a72c7073214b6ca378ad61424449
1 /**
2 * trials search, selections to combine etc...
3 * @author Isaak Y Tecle <iyt2@cornell.edu>
5 */
7 JSAN.use("Prototype");
8 JSAN.use('jquery.blockUI');
11 var solGS = solGS || function solGS () {};
13 solGS.combinedTrials = {
15      getPopIds: function () {
16                 var searchedPopsList = jQuery("#searched_trials_table tr").length;
17          var tableId;
19          if (searchedPopsList) {
20              tableId = 'searched_trials_table';
21          } else {
22              tableId = 'all_trials_table';
23          }
25         
26          jQuery("#trial_search_result_select").show();
27         
29          jQuery('#' +tableId + ' tr')
30              .filter(':has(:checkbox:checked)')
31              .bind('click',  function() {
33                  jQuery("#select_trials_btn").val('Done selecting');
34                  var td =  jQuery(this).html();
36                  var selectedTrial = '<tr>' + td + '</tr>';
38                  jQuery("#selected_trials_table tr:last").after(selectedTrial);
40                  jQuery("#selected_trials_table tr").each( function() {
41                      jQuery(this).find("input[type=checkbox]")
42                          .attr('onclick', 'solGS.combinedTrials.removeSelectedTrial()')
43                          .prop('checked', true);
44                  });
45              });
47          jQuery("#selected_trials").show();
48          jQuery("#combine_trials_div").show();
49          jQuery("#search_again_div").hide();
51      },
54     hideTrialsList: function() {
55         jQuery("#trial_search_result").empty();
56         // jQuery("#trial_search_result").empty();
57         jQuery("#trial_search_result_select").hide();
58         jQuery("#trial_search_result_message").hide();
60     },
63     removeSelectedTrial: function() {
65         jQuery("#selected_trials_table tr").on("change", function() {
67         jQuery(this).remove();
69         if (jQuery("#selected_trials_table td").length == 0) {
70                 jQuery("#selected_trials").hide();
71                 jQuery("#combine_trials_div").hide();
72                 jQuery("#search_again_div").hide();
73                 jQuery("#select_trials_btn").val('Select');
75                 //  searchAgain();
76         }
77         });
79     },
82     searchAgain: function () {
84         var url = window.location.pathname;
86         if (url.match(/solgs\/search\/trials\/trait\//) != null) {
87             var traitId = jQuery("input[name='trait_id']").val();
88             url = '/solgs/search/result/populations/' + traitId;
89         } else {
90             url = '/solgs/search/trials/';
91         }
93         // jQuery('#trial_search_result').empty();
94         jQuery("#trial_search_result").empty();
95         searchAllTrials(url);
96         jQuery("#trial_search_result_message").show();
97         jQuery("#trial_search_result_select").show();
98         jQuery("#select_trials_btn").val('Select');
100     },
103     combineTraitTrials: function () {
104         var trId = this.getTraitId();
105         var protocolId = jQuery('#genotyping_protocol_id').val();
107         var trialIds = this.getSelectedTrials();
109         var action = "/solgs/combine/trials/trait/" + trId + '/gp/' + protocolId;
110         var selectedPops = trId + "=" + trialIds + '&' + 'combine=combine';
112         jQuery.blockUI.defaults.applyPlatformOpacityRules = false;
113         jQuery.blockUI({message: 'Please wait..'});
115         jQuery.ajax({
116             type: 'POST',
117             dataType: "json",
118             url: action,
119             data: selectedPops,
120             success: function(res) {
122                 if (res.status) {
124             var comboPopsId = res.combo_pops_id;
125             var newUrl = '/solgs/model/combined/trials/' + comboPopsId + '/trait/' + trId  + '/gp/' + protocolId;;
127                     if (comboPopsId) {
128                         window.location.href = newUrl;
129                         jQuery.unblockUI();
130                     } else if (res.pop_id) {
131                         var args = {'pop_id': res.pop_id,
132                                     'trait_id': trId,
133                                     'genotyping_protocol_id': res.genotyping_protocol_id
134                                    };
136                         this.goToSingleTrialTrainingPopPage(args);
137                         jQuery.unblockUI();
138                     }
140                 } else {
142                     if (res.not_matching_pops){
143                         alert('populations ' + res.not_matching_pops +
144                               ' were genotyped using different marker sets. ' +
145                               'Please make new selections to combine.' );
146                         window.location.href =  '/solgs/search/result/populations/' + trId;
147                     }
149                     if (res.redirect_url) {
150                         window.location.href = res.redirect_url;
151                     }
152                 }
153             }
154         });
156     },
159     getCombinedPopsId: function (comboPopsList) {
161         if (!comboPopsList) {
162             comboPopsList = this.getSelectedTrials();
163         }
165         comboPopsList = comboPopsList.unique();
167         var protocolId = jQuery('#genotyping_protocol_id').val();
168         var traitId = this.getTraitId();
169         var referer = window.location.href;
171         var page;
173         var args = {
174             'trials': comboPopsList,
175             'genotyping_protocol_id': protocolId
176         };
178         if (comboPopsList.length > 1) {
179             jQuery.ajax({
180                 type: 'POST',
181                 dataType: "json",
182                 url: "/solgs/get/combined/populations/id",
183                 data: args,
184                 success: function(res) {
185                     if (res.status) {
186                         var comboPopsId = res.combo_pops_id;
188                         if (window.Prototype) {
189                             delete Array.prototype.toJSON;
190                         }
192                         var args = {
193                             'combo_pops_id'   : [ comboPopsId ],
194                             'combo_pops_list' : comboPopsList,
195                             'trait_id'        : traitId,
196                             'genotyping_protocol_id': res.genotyping_protocol_id
197                         };
199                         solGS.combinedTrials.downloadCombinedTrialsTrainingPopData(args);
200                     }
202                 },
203                 error: function(res) {
204                     alert('Error occured getting combined trials unique id');
205                 }
207             });
209         } else {
210             var popId = comboPopsList;
211             var args = {
212                 'trial_id': popId,
213                 'trait_id': traitId,
214                 'genotyping_protocol_id': protocolId
215             };
217             this.downloadSingleTrialTrainingPopData(args);
218         }
220     },
223     downloadCombinedTrialsTrainingPopData: function (args) {
225         if (window.Prototype) {
226             delete Array.prototype.toJSON;
227         }
229         args['analysis_type'] = 'combine_populations';
230         args['data_set_type'] = 'multiple_populations';
232         var comboPopsId = args.combo_pops_id;
233         comboPopsId = comboPopsId[0];
234         var protocolId = args.genotyping_protocol_id;
236         var referer = window.location.href;
237         var page;
239         if (referer.match(/search\/trials\/trait\//)) {
240             var traitId = args.trait_id;
242             page = '/solgs/model/combined/trials/' + comboPopsId
243                 + '/trait/' + traitId
244                 + '/gp/' + protocolId;
246         } else {
247             page = '/solgs/populations/combined/' + comboPopsId + '/gp/' + protocolId;
248         }
250         solGS.waitPage(page, args);
252     },
255     displayCombinedTrialsTrainingPopPage: function(args) {
257         var trialsIds = args.combo_pops_list;
258         var protocolId = args.genotyping_protocol_id;
260         if (!trialsIds) {
261             trialsIds = this.getSelectedTrials();
262         }
264         var action = "/solgs/retrieve/populations/data";
266         jQuery.blockUI.defaults.applyPlatformOpacityRules = false;
267         jQuery.blockUI({message: 'Please wait..'});
269         jQuery.ajax({
270             type: 'POST',
271             dataType: "json",
272             url: action,
273             data: {'trials': trialsIds, 'genotyping_protocol_id': protocolId},
274             success: function(res) {
275                 if (res.not_matching_pops == null) {
277                     var combinedPopsId = res.combined_pops_id;
278                     var protocolId     = res.genotyping_protocol_id;
280                     if (combinedPopsId) {
281                         solGS.combinedTrials.goToCombinedTrialsTrainingPopPage(combinedPopsId, protocolId);
282                         jQuery.unblockUI();
283                     } else if (res.pop_id) {
285                         var args = {
286                             'pop_id': res.pop_id,
287                             'trait_id': trId,
288                             'genotyping_protocol_id': res.genotyping_protocol_id
289                         };
291                         solGS.combinedTrials.goToSingleTrialTrainingPopPage(args);
292                         jQuery.unblockUI();
293                     }
295                 } else if(res.not_matching_pops )  {
297                     jQuery.unblockUI();
298                     alert('populations ' + res.not_matching_pops +
299                           ' were genotyped using different marker sets. ' +
300                           'Please make new selections to combine.' );
302                 }
303             },
304             error: function(res) {
305                 jQuery.unblockUI();
306                 alert('An error occured retrieving phenotype' +
307                       'and genotype data for trials..');
308             }
309         });
311     },
314     getSelectedTrials: function () {
316         var trialIds = [];
318         if (jQuery("#selected_trials_table").length) {
319             jQuery("#selected_trials_table tr")
320                 .each(function () {
322                     var trialId = jQuery(this)
323                         .find("input[type=checkbox]")
324                         .val();
326                 if (trialId) {
327                     trialIds.push(trialId);
328                 }
329             });
330         }
332         return trialIds.sort();
334     },
337     goToCombinedTrialsTrainingPopPage: function (comboPopsId, protocolId) {
339         var page = '/solgs/populations/combined/' + comboPopsId + '/gp/' + protocolId;
341         if (comboPopsId) {
342             window.location = page;
343         } else {
344             alert('combined Trials id missing.')
345         }
346     },
349     goToSingleTrialTrainingPopPage: function (args) {
351         var referer = window.location.href;
352         var page;
353         var protocolId = args.genotyping_protocol_id;
355         if (referer.match(/search\/trials\/trait\//)) {
356             page = '/solgs/trait/' + args.trait_id + '/population/' + args.pop_id + '/gp/' + protocolId;
358         } else {
360             //var hostName = window.location.protocol + '//' + window.location.host;
361             page = '/solgs/population/' + args.pop_id + '/gp/' + protocolId ;
362         }
364         window.location = page;
366     },
369     downloadSingleTrialTrainingPopData: function (args) {
371         var referer = window.location.href;
372         var page;
373         var popId = args.trial_id;
374         var traitId = args.trait_id;
375         var protocolId = args.genotyping_protocol_id;
377         if (referer.match(/search\/trials\/trait\//)) {
378             page = '/solgs/trait/' + traitId + '/population/' + popId + '/gp/' + protocolId;
380         } else {
381             //var hostName = window.location.protocol + '//' + window.location.host;
382             page = '/solgs/population/' + popId + '/gp/' + protocolId;
383         }
385         var pageArgs = {
386             'population_id'   : [ popId],
387             'analysis_type'   : 'training_dataset',
388             'data_set_type'   : 'single_population',
389             'trait_id'        :  traitId,
390             'genotyping_protocol_id': args.genotyping_protocol_id,
391         };
393         solGS.waitPage(page, pageArgs);
395     },
398     getTraitId: function() {
400         var id = jQuery("input[name='trait_id']").val();
402         return id;
403     }
405 /////
407 /////
410 Array.prototype.unique =
411     function() {
412     var a = [];
413     var l = this.length;
414     for(var i=0; i<l; i++) {
415       for(var j=i+1; j<l; j++) {
416         // If this[i] is fo3und later in the array
417         if (this[i] === this[j])
418           j = ++i;
419       }
420       a.push(this[i]);
421     }
422     return a;
423     };
426 jQuery(document).ready(function() {
427     jQuery('#select_trials_btn').on('click', function() {
428         solGS.combinedTrials.hideTrialsList();
429     });
434 jQuery(document).ready(function() {
435     jQuery('#combine_trait_trials').on('click', function() {
436         //combineTraitTrials();
437         solGS.combinedTrials.getCombinedPopsId();
438     });
443 jQuery(document).ready(function() {
444     jQuery('#combine_trials').on('click', function() {
446         solGS.combinedTrials.getCombinedPopsId();
447     });
451 // jQuery(document).ready(function() {
452 //     jQuery('#search_again').on('click', function() {
453 //      searchAgain();
454 //     });
456 // });
458 jQuery(document).ready(function() {
460 var tableId = "#searched_trials_table";
461 var table = document.querySelector(tableId);
463 if (table) {
464   var rowsCount = table.rows.length;
466   if (rowsCount > 1) {
467         jQuery("#trial_search_result_select").show();
468   }