button to generate plot pheno from plants
[sgn.git] / js / solGS / combineTrials.js
blobf6ddf7b67a8fc1b3fbaf425f76bf8763ed87614a
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 function getPopIds () {
13     var searchedPopsList = jQuery("#searched_trials_table tr").length;
14   
15     var tableId;
17     if (searchedPopsList) {
18         tableId = 'searched_trials_table';
19     } else {    
20         tableId = 'all_trials_table';
21     }
23     jQuery('#' +tableId + ' tr').filter(':has(:checkbox:checked)')
24         .bind('click',  function() {
25      
26             jQuery("#done_selecting").val('Done selecting');
27             var td =  jQuery(this).html();
28         
29             var selectedTrial = '<tr>' + td + '</tr>';
30         
31             jQuery("#selected_trials_table tr:last").after(selectedTrial);
32        
33             jQuery("#selected_trials_table tr").each( function() {
34                 jQuery(this).find("input[type=checkbox]")
35                     .attr('onclick', 'removeSelectedTrial()')
36                     .prop('checked', true); 
37             });
38         });
39    
40     jQuery("#selected_trials").show();  
41     jQuery("#combine_trials_div").show();
42     jQuery("#search_again_div").show();
43    
47 jQuery(document).ready(function() {
48     jQuery('#done_selecting').on('click', function() {
49         hideTrialsList();
50     });  
52 });
55 function hideTrialsList() {
56     jQuery("#all_trials_div").empty();
57     jQuery("#searched_trials_div").empty();
58     jQuery("#done_selecting_div").hide();
59     jQuery("#all_trials_search_message").hide();
60     
64 function removeSelectedTrial() {
65     
66     jQuery("#selected_trials_table tr").on("change", function() {    
67         
68         jQuery(this).remove();
69         
70         if (jQuery("#selected_trials_table td").length == 0) {
71             jQuery("#selected_trials").hide();
72             jQuery("#combine_trials_div").hide();
73             jQuery("#search_again_div").hide();
74             jQuery("#done_selecting").val('Select');            
75             
76             searchAgain();           
77         }
78     });
83 jQuery(document).ready(function() {
84     jQuery('#search_again').on('click', function() {
85         searchAgain();
86     });  
88 });
91 function searchAgain () {
93     var url = window.location.pathname;
95     if (url.match(/solgs\/search\/trials\/trait\//) != null) {
96         var traitId = jQuery("input[name='trait_id']").val();
97         url = '/solgs/search/result/populations/' + traitId;
98     } else {
99         url = '/solgs/search/trials/';
100     }
101   
102     jQuery('#all_trials_div').empty();
103     jQuery("#searched_trials_div").empty();
104     searchAllTrials(url);  
105     jQuery("#all_trials_search_message").show();
106     jQuery("#done_selecting_div").show();
107     jQuery("#done_selecting").val('Select');
108     
112 jQuery(document).ready(function() {
113     jQuery('#combine_trait_trials').on('click', function() {
114         //combineTraitTrials();
115         getCombinedPopsId();
116     });  
121 function combineTraitTrials () {
122     var trId = getTraitId();
123   
124     var trialIds = getSelectedTrials();  
126     var action = "/solgs/combine/populations/trait/" + trId;
127     var selectedPops = trId + "=" + trialIds + '&' + 'combine=combine';
128     
129     jQuery.blockUI.defaults.applyPlatformOpacityRules = false;
130     jQuery.blockUI({message: 'Please wait..'});
131     
132     jQuery.ajax({  
133         type: 'POST',
134         dataType: "json",
135         url: action,
136         data: selectedPops,
137         success: function(res) {                       
138              
139             if (res.status) {
140                   
141                 var comboPopsId = res.combo_pops_id;
142                 var newUrl = '/solgs/model/combined/populations/' + comboPopsId + '/trait/' + trId;
143                     
144                 if (comboPopsId) {
145                     window.location.href = newUrl;
146                     jQuery.unblockUI();
147                 } else if (res.redirect_url) {
148                     goToSingleTrialPage(res.redirect_url);
149                     jQuery.unblockUI();
150                 } 
151                     
152             } else {
153                     
154                 if (res.not_matching_pops){                        
155                     alert('populations ' + res.not_matching_pops + 
156                           ' were genotyped using different marker sets. ' + 
157                           'Please make new selections to combine.' );
158                     window.location.href =  '/solgs/search/result/populations/' + trId;
159                 }
161                 if (res.redirect_url) {
162                     window.location.href = res.redirect_url;
163                 }
164             } 
165         }
166     });
171 jQuery(document).ready(function() {
172     jQuery('#combine_trials').on('click', function() {
173         getCombinedPopsId();
174     });  
179 function getCombinedPopsId() {
181     var comboPopsList = getSelectedTrials();
182     var trialsIds     = comboPopsList.join(","); 
183     var traitId       = getTraitId();
184     var action        = "/solgs/get/combined/populations/id";
185   
186     jQuery.ajax({  
187         type: 'POST',
188         dataType: "json",
189         url: action,
190         data: {'trials': trialsIds},
191         success: function(res) {                         
192             if (res.status) {               
193                 var comboPopsId = res.combo_pops_id;
195                 if (window.Prototype) {
196                     delete Array.prototype.toJSON;
197                 }
199                  var args = {
200                      'combo_pops_id'   : [ comboPopsId ],
201                      'combo_pops_list' : comboPopsList,
202                      'analysis_type'   : 'combine populations',
203                      'data_set_type'   : 'multiple populations',
204                      'trait_id'        : traitId,
205                     };
206                 
207                 var referer = window.location.href;
208                 var page;
209         
210                 if (referer.match(/search\/trials\/trait\//)) {
211                      page = '/solgs/model/combined/trials/' + comboPopsId + '/trait/' + traitId;
212                     
213                 } else {
214                     page = '/solgs/populations/combined/' + comboPopsId;
215                 }
216                 
217                 solGS.waitPage(page, args);
218             } 
219         },
220         error: function(res) {
221             //combinedPopsId = 0;   
222         }       
223     }); 
225    // return combinedPopsId;
226     
230 function retrievePopsData() {
232     var trialsIds = getSelectedTrials();
233     trialsIds = trialsIds.join(",");
234     
235     var action = "/solgs/retrieve/populations/data";
236    
237     jQuery.blockUI.defaults.applyPlatformOpacityRules = false;
238     jQuery.blockUI({message: 'Please wait..'});
239     
240     jQuery.ajax({  
241         type: 'POST',
242         dataType: "json",
243         url: action,
244         data: {'trials': trialsIds},
245         success: function(res) {                         
246             if (res.not_matching_pops == null) {
247                
248                 var combinedPopsId = res.combined_pops_id;
249                
250                 if(combinedPopsId) {
251                     goToCombinedTrialsPage(combinedPopsId);
252                     jQuery.unblockUI();
253                 }else if (res.redirect_url) {
254                     goToSingleTrialPage(res.redirect_url);
255                     jQuery.unblockUI();
256                 } 
257                                      
258             } else if(res.not_matching_pops )  {
259                             
260                 jQuery.unblockUI();
261                 alert('populations ' + res.not_matching_pops + 
262                       ' were genotyped using different marker sets. ' + 
263                       'Please make new selections to combine.' );
264         
265             } 
266         },
267         error: function(res) { 
268             jQuery.unblockUI();
269             alert('An error occured retrieving phenotype' +
270                   'and genotype data for trials..');
271         }       
272     });   
276 function getSelectedTrials () {
277    
278     var trialIds = [];
279   
280     if (jQuery("#selected_trials_table").length) {      
281         jQuery("#selected_trials_table tr").each(function () {       
282             var trialId = jQuery(this).find("input[type=checkbox]").val();
283            
284             if (trialId) {
285                 trialIds.push(trialId);
286             }            
287         });       
288     }
290     trialIds = trialIds.sort();
292     return trialIds;
297 function goToCombinedTrialsPage(combinedPopsId) {
298      
299     var action = '/solgs/populations/combined/' + combinedPopsId;
300    
301     if (combinedPopsId) {      
302         window.location.href = action;
303     } 
307 function goToSingleTrialPage(url) {
308     
309     if (url) {      
310         window.location.href = url;
311     }    
315 function  getTraitId() {
316    
317     var id = jQuery("input[name='trait_id']").val();   
318     return id;
322 Array.prototype.unique =
323     function() {
324     var a = [];
325     var l = this.length;
326     for(var i=0; i<l; i++) {
327       for(var j=i+1; j<l; j++) {
328         // If this[i] is fo3und later in the array
329         if (this[i] === this[j])
330           j = ++i;
331       }
332       a.push(this[i]);
333     }
334     return a;
335   };