refactoring brapi
[sgn.git] / js / solGS / gebvsComparison.js
blob37833d8548a07fec622fdaa5df012dddb791a907
1 /** 
2 * visualize and compare gebvs of a training population 
3 * and a selection population.
4 * normal distribution plotting using d3.
5 * uses methods from solGS.normalDistribution and solGS.linePlot js libraries
6 * Isaak Y Tecle <iyt2@cornell.edu>
8 */
11 jQuery(document).ready(function () {
12     jQuery('#compare_gebvs').on('click', function () {
13         gebvsComparison();
14     }); 
15 });
18 function gebvsComparison () {
19      
20     var gebvParams = getGebvsParams();
21    
22     var trainingGEBVs  = '';
23     var selectionGEBVs = ''; 
24     
25     var missing;
26     if (!gebvParams.training_pop_id) {
27         missing = 'training population id';
28     }
30     if (!gebvParams.selection_pop_id) {
31         missing += ', selection population id';
32     }
34     if (!gebvParams.trait_id) {
35         missing += ', trait id';
36     }
38     if (missing) {      
39         jQuery('#compare_gebvs_message')
40             .html('Can not compare GEBVs. I am missing ' + missing + '.')
41             .show();
42     }
43     else {  
44         getTrainingPopulationGEBVs(gebvParams);
45     }
48     function getGebvsParams () {
49         
50         var trainingPopId  = jQuery('#training_pop_id').val();
51         var selectionPopId = jQuery('#selection_pop_id').val();
52         var traitId        = jQuery('#trait_id').val();
53         
54         var gebvParams = { 
55             'training_pop_id'  : trainingPopId,
56             'selection_pop_id' : selectionPopId,
57             'trait_id'         : traitId
58         }
60         return gebvParams;
62     }
64     function getTrainingPopulationGEBVs (gebvParams) {
65     
66         jQuery.ajax({
67             type: 'POST',
68             dataType: 'json',
69             data: gebvParams,
70             url : '/solgs/get/gebvs/training/population',
71             success: function (res) {
72                 if (res.gebv_exists) {
73                     jQuery('#compare_gebvs_message').empty();
74                     trainingGEBVs = res.gebv_arrayref;
75                     
76                     if (trainingGEBVs) {
77                         getSelectionPopulationGEBVs(gebvParams)
78                     }
79                     
80                 } else {
81                     jQuery('#compare_gebvs_message')
82                         .html('There is no GEBV data for the training population.')
83                         .show();
84                 }
85             },
86             error: function () {
87                 jQuery('#compare_gebvs_message')
88                     .html('Error occured checking for GEBV data for the training population.')
89                     .show();
90             }
91         });
93     }
96     function getSelectionPopulationGEBVs (gebvParams) {
97         
98         jQuery.ajax({
99             type: 'POST',
100             dataType: 'json',
101             data: gebvParams,
102             url : '/solgs/get/gebvs/selection/population',
103             success: function (res) {
104                 if (res.gebv_exists) {
105                     jQuery('#compare_gebvs_message').empty();
106                     
107                     selectionGEBVs = res.gebv_arrayref;
108                     
109                     if (selectionGEBVs && trainingGEBVs) {
110                         jQuery('#compare_gebvs_message')
111                             .html('Please wait... plotting gebvs')
112                             .show();
113                     
114                         plotGEBVs(trainingGEBVs, selectionGEBVs);
115                     
116                         jQuery('#compare_gebvs_message').empty();
117                         jQuery('#compare_gebvs').hide();
118                     }
119                 } else {
120                     jQuery('#compare_gebvs_message')
121                         .html('There is no GEBV data for the selection population.')
122                         .show();
123                 }
124             },
125             error: function () {
126                 jQuery('#compare_gebvs_message')
127                     .html('Error occured checking for GEBV data for the selection population.')
128                     .show();
129             }
130         });  
132     }
135     function plotGEBVs (trainingGEBVs, selectionGEBVs) {
136         
137         var normalDistTraining = new solGS.normalDistribution();
138         
139         var trainingNormalDistData  = normalDistTraining
140             .getNormalDistData(trainingGEBVs);
141         
142         var gebvZScoresT = normalDistTraining
143             .getYValuesZScores(trainingNormalDistData);
145         var yValuesT = normalDistTraining
146             .getPValues(trainingNormalDistData);
148         var zScoresPT = normalDistTraining
149             .getZScoresP(trainingNormalDistData);
151         var xYT =  normalDistTraining
152             .getYValuesP(trainingNormalDistData);
154         var xValuesT =  normalDistTraining
155             .getYValues(trainingGEBVs);
157         var trMean = ss.mean(xValuesT);
159         var stdT = trMean <= 0 ? -1.0 : 1.0;
161         var xMT = normalDistTraining.getObsValueZScore(gebvZScoresT, stdT);
163         var normalDistSelection = new solGS.normalDistribution();
165         var selectionNormalDistData = normalDistSelection
166             .getNormalDistData(selectionGEBVs);
168         var gebvZScoresS = normalDistSelection 
169             .getYValuesZScores(selectionNormalDistData);
170                 
171         var yValuesS = normalDistSelection
172             .getPValues(selectionNormalDistData);
173         
174         var zScoresPS = normalDistSelection
175             .getZScoresP(selectionNormalDistData);
176         
177         var xYS = normalDistSelection
178             .getYValuesP(selectionNormalDistData);
179         
180         var xValuesS = normalDistSelection
181             .getYValues(selectionGEBVs);
182         
183         var slMean = ss.mean(xValuesS);
185         var stdS = slMean <= 0 ? -1.0 : 1.0;
187         var xMS = normalDistTraining.getObsValueZScore(gebvZScoresS, stdS);
189         var svgId  = '#compare_gebvs_canvas';
190         var plotId = '#compare_gebvs_plot';
192         var trColor      = '#02bcff'; 
193         var slColor      = '#ff1302'; 
194         var axLabelColor = '#ff8d02';    
195         var yLabel       = 'Probability';
196         var xLabel       = 'GEBVs';
198         var title = 'Normal distribution curves of GEBVs ' 
199             + 'for the training and selection populations.';
200         
201         var allData =  {
202             'div_id': svgId, 
203             'plot_title': title, 
204             'x_axis_label': xLabel,
205             'y_axis_label': yLabel,
206             'axis_label_color': axLabelColor,
207             'lines' : 
208             [           
209                 {
210                     'data'  : xYT,
211                     'legend': 'Training population' ,
212                     'color' : trColor,
213                 },      
214                 {
215                     'data'  : xYS,
216                     'legend': 'Selection population',
217                     'color' : slColor,
218                 },                  
219                 
220             ]    
221         };
224         var linePlot  = solGS.linePlot(allData);
226         var trainingMidlineData  = [
227             [trMean, 0], 
228             [trMean, d3.max(yValuesT)]
229         ];
230         
231         var selectionMidlineData = [
232             [slMean, 0], 
233             [slMean, d3.max(yValuesS)]
234         ];
236         var midLine = d3.svg.line()
237             .x(function(d) { 
238                 return linePlot.xScale(d[0]); 
239             })
240             .y(function(d) {                    
241                 return linePlot.yScale(d[1]); 
242             });
243            
244         linePlot.graph.append("path")
245             .attr("d", midLine(trainingMidlineData))
246             .attr("stroke", trColor)
247             .attr("stroke-width", "3")
248             .attr("fill", "none")
249             .on("mouseover", function (d) {
250                 if (d = trMean) {
251                     linePlot.graph.append("text")
252                         .attr("id", "tr_mean")
253                         .text(d3.format(".2f")(trMean))
254                         .style({
255                             "fill"       : trColor, 
256                             "font-weight": "bold"
257                         }) 
258                         .attr("x", linePlot.xScale(xMT[0]))
259                         .attr("y", linePlot.yScale(d3.max(yValuesT) * 0.5))                     
260                 }
261             })                
262             .on("mouseout", function() {
263                 d3.selectAll("text#tr_mean").remove();
264             });
266         linePlot.graph.append("path")
267             .attr("d", midLine(selectionMidlineData))
268             .attr("stroke", slColor)
269             .attr("stroke-width", "3")
270             .attr("fill", "none")
271             .on("mouseover", function (d) {
272                 if (d = slMean) {
273                     linePlot.graph.append("text")
274                         .attr("id", "sl_mean")
275                         .text(d3.format(".2f")(slMean))
276                         .style({
277                             "fill"       : slColor, 
278                             "font-weight": "bold"
279                         })  
280                         .attr("x", linePlot.xScale(xMS[0]))
281                         .attr("y", linePlot.yScale(d3.max(yValuesS) * 0.5))
282                              
283                 }
284             })                
285             .on("mouseout", function() {
286                 d3.selectAll("text#sl_mean").remove(); 
287             });
288     }
290 //////////
292 /////////