add patch that updates matview subqueries to include cv name constraints
[sgn.git] / js / anova.js
blob10b871480414ceb896ad5e53c1f54d87bc8e03c0
1 /** 
2 * ANOVA analysis
3 * Isaak Y Tecle <iyt2@cornell.edu>
5 */
8 jQuery(document).ready( function() { 
9   
10     var url = document.URL;
12     if (url.match(/\/breeders_toolbox\/trial|breeders\/trial|\/solgs\/population\//)) {
13             allowAnova();  
14         } 
16 });
19 function allowAnova () {
20   
21     checkDesign();
22   
26 function checkDesign () {
27     
28     var trialId = getTrialId();  
29     
30     jQuery.ajax({
31         type: 'POST',
32         dataType: 'json',
33         data: {'trial_id': trialId},
34         url: '/anova/check/design/',      
35         success: function(response) {
36              
37             if (response.Error) {
38                 showMessage(response.Error);
39                 jQuery("#run_anova").hide();
40             } else {
41                   
42                 listAnovaTraits();
43             }
44         },
45         error: function(response) {                          
46             showMessage("Error occured running the ANOVA.");            
47             jQuery("#run_anova").show();
48         }                
49     });
54 jQuery(document).ready(function () {
55     jQuery(document).on("click", "#run_anova", function() {        
56    
57         var traitId    =  jQuery("#anova_selected_trait_id").val();
58         
59         if (traitId) {
61             queryPhenoData(traitId);
62    
63             jQuery("#run_anova").hide();
65             showMessage("Running anova analysis...");
66         } else {
67             var msg = 'You need to select a trait first.'
68             anovaAlert(msg);
69         }
70                
71     });
73 });
76 function anovaAlert(msg) {
78         jQuery('<div />')
79         .html(msg)
80             .dialog({
81                 modal  : true,
82                 title  : 'Alert',
83                 buttons: {
84                     OK: {
85                         click: function () {
86                             jQuery(this).dialog('close');               
87                         },
88                         class: 'btn btn-success',
89                         text : 'OK',
90                     },
91                 }                       
92             });     
93     
97 function queryPhenoData(traitId) {
99     var trialId = getTrialId();
100    
101     jQuery.ajax({
102         type: 'POST',
103         dataType: 'json',
104         data: {'trial_id': trialId, 'traits_ids': [traitId]},
105         url: '/anova/phenotype/data/',      
106         success: function(response) {
107                     
108             if (response.Error) {
109                 showMessage(response.Error);
110                 jQuery("#run_anova").show();
111             } else {
112                 var traitsAbbrs = response.traits_abbrs;
113                 runAnovaAnalysis(traitsAbbrs);
114             }
115         },
116         error: function(response) {                          
117             showMessage("Error occured running the ANOVA.");            
118             jQuery("#run_anova").show();
119         }                
120     });
124 function showMessage (msg) {
125      jQuery("#anova_message")
126         .css({"padding-left": '0px'})
127         .html(msg);
128     
132 function runAnovaAnalysis(traits) {
134     var trialId = getTrialId();
135    
136     var captions       = jQuery('#anova_table table').find('caption').text();           
137     var analyzedTraits = captions.replace(/ANOVA result:/g, ' '); 
139     for (var i = 0; i < traits.length; i++) {
140         var traitAbbr = traits[i].trait_abbr;
142         if (analyzedTraits.match(traitAbbr) == null) {
143             var anovaTraits = JSON.stringify(traits);
145             jQuery.ajax({
146                 type: 'POST',
147                 dataType: 'json',
148                 data: {'trial_id': trialId, 'traits': [anovaTraits]},
149                 url: '/anova/analysis/',      
150                 success: function(response) {
151                    
152                     if(response.Error) {
153                         jQuery("#anova_message").empty();
154                         showMessage(response.Error);
155                         jQuery("#run_anova").show();
156                     } else {
157                         
158                         var anovaTable = response.anova_html_table;                     
159                    
160                         jQuery("#anova_table").append('<div style="margin-top: 20px">' + anovaTable + '</div>').show();
161                 
162                         var anovaFile = response.anova_table_file;
163                         var modelFile = response.anova_model_file;
164                         var meansFile = response.adj_means_file;
165                         var diagnosticsFile = response.anova_diagnostics_file;
167                         var fileNameAnova = anovaFile.split('/').pop();
168                         var fileNameModel = modelFile.split('/').pop();
169                         var fileNameMeans = meansFile.split('/').pop();
170                         var fileNameDiagnostics = diagnosticsFile.split('/').pop()
171                         
172                         anovaFile = "<a href=\"" + anovaFile +  "\" download=" + fileNameAnova + ">[Anova table]</a>";
173                         modelFile = "<a href=\"" + modelFile +  "\" download=" + fileNameModel + ">[Model Summary]</a>";
174                         meansFile = "<a href=\"" + meansFile +  "\" download=" + fileNameMeans + ">[Adjusted Means]</a>";
175                         
176                         diagnosticsFile = "<a href=\"" + diagnosticsFile
177                             +  "\" download=" + fileNameDiagnostics + ">[Model Diagnostics]</a>";
178                 
179                         jQuery("#anova_table")
180                             .append('<br /> <strong>Download:</strong> '
181                                     + anovaFile + ' | '
182                                     + modelFile + ' | '
183                                     + meansFile + ' | '
184                                     + diagnosticsFile)
185                             .show();
186                         
187                         jQuery("#anova_message").empty();
188                         
189                         jQuery("#run_anova").show();
190                     }
191                     //} else {
192                 //      showMessage("There is no anova output for this dataset.");              
193                     jQuery("#run_anova").show();
194                  //   }
195                     clearTraitSelection();
196                     
197                 },
198                 error: function(response) {                          
199                     showMessage("Error occured running the anova analysis.");           
200                     jQuery("#run_anova").show();
201                     clearTraitSelection();
202                 }                
203             });
204         } else {
205             jQuery("#anova_message").empty();
206             jQuery("#run_anova").show();
207             clearTraitSelection();
208         }
209     }
214 function listAnovaTraits ()  {
216     var trialId = getTrialId();
218      jQuery.ajax({
219         type: 'POST',
220         dataType: 'json',
221         data: {'trial_id': trialId},
222         url: '/anova/traits/list/',      
223          success: function(response) {
224              var traits = response.anova_traits;
225           
226              if (traits.length) {
227                  formatAnovaTraits(traits);
228                  jQuery("#run_anova").show();
229              } else {
230                  showMessage('This trial has no phenotyped traits.');
231                  jQuery("#run_anova").hide();            
232              }    
233         },
234         error: function(response) {                          
235             showMessage("Error occured listing anova traits.");         
236             jQuery("#run_anova").hide();
237         }                
238     });
240    
243 function formatAnovaTraits(traits) {
244     
245     var traitsList = '';
246    
247     for (var i = 0; i < traits.length; i++) {
248         var traitName = traits[i].trait_name;
250         var idName = JSON.stringify(traits[i]);
251         traitsList +='<li>'
252         + '<a href="#">' + traitName + '<span class=value>' + idName + '</span></a>'
253         + '</li>';
254     }
255    
256     var  traitsList =  '<dl id="anova_selected_trait" class="anova_dropdown">'
257         + '<dt> <a href="#"><span>Select a trait</span></a></dt>'
258         + '<dd>'
259         + '<ul>'
260         + traitsList
261         + '</ul></dd></dl>'; 
262    
263     jQuery("#anova_select_a_trait_div").empty().append(traitsList).show();
264       
265     jQuery(".anova_dropdown dt a").click(function() {
266         jQuery(".anova_dropdown dd ul").toggle();
267     });
268                  
269     jQuery(".anova_dropdown dd ul li a").click(function() {
270       
271         var text = jQuery(this).html();
272            
273         jQuery(".anova_dropdown dt a span").html(text);
274         jQuery(".anova_dropdown dd ul").hide();
275                 
276         var traitIdName = jQuery("#anova_selected_trait").find("dt a span.value").html();
277         traitIdName     = JSON.parse(traitIdName);
278       
279         var traitId   = traitIdName.trait_id;
280         var traitName = traitIdName.trait_name;
282         console.log('id ' + traitId + ' name ' + traitName)
284         jQuery("#anova_selected_trait_name").val(traitName);
285         jQuery("#anova_selected_trait_id").val(traitId);
286                                
287     });
288                        
289     jQuery(".anova_dropdown").bind('click', function(e) {
290         var clicked = jQuery(e.target);
291                
292         if (!clicked.parents().hasClass("anova_dropdown"))
293             jQuery(".anova_dropdown dd ul").hide();
295         e.preventDefault();
297         
298     });   
299         
303 function clearTraitSelection () {
305     jQuery("#anova_selected_trait_name").val('');
306     jQuery("#anova_selected_trait_id").val('');
311 function getTrialId () {
312     
313     var trialId    =  jQuery("#trial_id").val();
315     if (!trialId) {
317         trialId = jQuery("#population_id").val();
318     }
320     return trialId;