2 * breeding values vs phenotypic deviation
4 * Isaak Y Tecle <iyt2@cornell.edu>
9 jQuery(document).ready( function() {
16 function checkDataExists () {
17 var dataDetails = getDataDetails();
18 var traitId = dataDetails.trait_id;
19 var populationId = dataDetails.population_id;
24 data: {'population_id': populationId, 'trait_id': traitId},
25 url: '/heritability/check/data/',
26 success: function(response) {
27 if(response.exists === 'yes') {
31 calculateVarianceComponents();
34 error: function(response) {
35 // alert('there is error in checking the dataset for heritability analysis.');
42 function getDataDetails () {
44 var populationId = jQuery("#population_id").val();
45 var traitId = jQuery("#trait_id").val();
48 populationId = jQuery("#model_id").val();
52 populationId = jQuery("#combo_pops_id").val();
55 return {'population_id' : populationId,
62 function calculateVarianceComponents () {
63 var dataDetails = getDataDetails();
64 var traitId = dataDetails.trait_id;
65 var populationId = dataDetails.population_id;
67 var gebvUrl = '/solgs/trait/' + traitId + '/population/' + populationId;
72 data: {'source' : 'heritability'},
74 success: function(response) {
75 if(response.status === 'success') {
78 jQuery("#heritability_message").html('Error occured estimating breeding values for this trait.');
81 error: function(response) {
82 jQuery("#heritability_message").html('Error occured estimating breeding values for this trait.');
88 function getRegressionData () {
89 var dataDetails = getDataDetails();
90 var traitId = dataDetails.trait_id;
91 var populationId = dataDetails.population_id;
96 data: {'population_id': populationId, 'trait_id': traitId},
97 url: '/heritability/regression/data/',
98 success: function(response) {
99 if(response.status === 'success') {
100 var regressionData = {
101 'breeding_values' : response.gebv_data,
102 'phenotype_values' : response.pheno_data,
103 'phenotype_deviations': response.pheno_deviations,
104 'heritability' : response.heritability
107 jQuery("#heritability_message").empty();
108 plotRegressionData(regressionData);
111 error: function(response) {
112 jQuery("#heritability_message").html('Error occured getting regression data.');
118 function plotRegressionData(regressionData){
120 var breedingValues = regressionData.breeding_values;
121 var phenotypeDeviations = regressionData.phenotype_deviations;
122 var heritability = regressionData.heritability;
123 var phenotypeValues = regressionData.phenotype_values;
125 var phenoRawValues = phenotypeValues.map( function (d) {
127 return parseFloat(d);
130 var phenoXValues = phenotypeDeviations.map( function (d) {
132 return parseFloat(d);
135 var breedingYValues = breedingValues.map( function (d) {
137 return parseFloat(d);
141 var scatterData = [];
143 phenotypeDeviations.map( function (pv) {
147 jQuery.each(breedingValues, function(i, gv) {
149 if ( pv[0] === gv[0] ) {
151 sD.push({'name' : gv[0], 'gebv' : gv[1], 'pheno_dev': pv[1]} );
153 var ptY = parseFloat(gv[1]);
154 var ptX = parseFloat(pv[1]);
162 scatterData.push(sD);
167 var pad = {left:20, top:20, right:20, bottom: 20};
168 var totalH = height + pad.top + pad.bottom;
169 var totalW = width + pad.left + pad.right;
171 var svg = d3.select("#gebv_pheno_regression_canvas")
173 .attr("width", totalW)
174 .attr("height", totalH);
176 var regressionPlot = svg.append("g")
177 .attr("id", "#gebv_pheno_regression_plot")
178 .attr("transform", "translate(" + (pad.left - 5) + "," + (pad.top - 5) + ")");
180 var phenoMin = d3.min(phenoXValues);
181 var phenoMax = d3.max(phenoXValues);
183 var xLimits = d3.max([Math.abs(d3.min(phenoXValues)), d3.max(phenoXValues)]);
184 var yLimits = d3.max([Math.abs(d3.min(breedingYValues)), d3.max(breedingYValues)]);
186 var xAxisScale = d3.scale.linear()
187 .domain([0, xLimits])
188 .range([0, width/2]);
190 var xAxisLabel = d3.scale.linear()
191 .domain([(-1 * xLimits), xLimits])
194 var yAxisScale = d3.scale.linear()
195 .domain([0, yLimits])
196 .range([0, (height/2)]);
198 var xAxis = d3.svg.axis()
203 var yAxisLabel = d3.scale.linear()
204 .domain([(-1 * yLimits), yLimits])
207 var yAxis = d3.svg.axis()
212 var xAxisMid = 0.5 * (totalH);
213 var yAxisMid = 0.5 * (totalW);
215 regressionPlot.append("g")
216 .attr("class", "x axis")
217 .attr("transform", "translate(" + pad.left + "," + xAxisMid +")")
223 .attr("transform", "rotate(90)")
224 .attr("fill", "green")
225 .style({"text-anchor":"start", "fill": "#86B404"});
227 regressionPlot.append("g")
228 .attr("class", "y axis")
229 .attr("transform", "translate(" + yAxisMid + "," + pad.top + ")")
234 .attr("fill", "green")
235 .style("fill", "#86B404");
237 regressionPlot.append("g")
238 .attr("id", "x_axis_label")
240 .text("Phenotype deviations (X)")
241 .attr("y", (pad.top + (height/2)) + 50)
242 .attr("x", (width - 110))
243 .attr("font-size", 10)
244 .style("fill", "#86B404")
246 regressionPlot.append("g")
247 .attr("id", "y_axis_label")
249 .text("Breeding values (Y)")
250 .attr("y", (pad.top - 10))
251 .attr("x", ((width/2) - 80))
252 .attr("font-size", 10)
253 .style("fill", "#86B404")
255 regressionPlot.append("g")
260 .attr("fill", "#9A2EFE")
262 .attr("cx", function(d) {
263 var xVal = d[0].pheno_dev;
266 return (pad.left + (width/2)) + xAxisScale(xVal);
268 return (pad.left + (width/2)) - (-1 * xAxisScale(xVal));
271 .attr("cy", function(d) {
272 var yVal = d[0].gebv;
275 return ( pad.top + (height/2)) - yAxisScale(yVal);
277 return (pad.top + (height/2)) + (-1 * yAxisScale(yVal));
280 .on("mouseover", function(d) {
283 .style("fill", "#86B404")
284 regressionPlot.append("text")
285 .attr("id", "dLabel")
286 .style("fill", "#86B404")
287 .text( d[0].name + "(" + d[0].pheno_dev + "," + d[0].gebv + ")")
288 .attr("x", pad.left + 1)
289 .attr("y", pad.top + 80);
291 .on("mouseout", function(d) {
294 .style("fill", "#9A2EFE")
295 d3.selectAll("text#dLabel").remove();
298 var line = ss.linear_regression()
302 var lineParams = ss.linear_regression()
305 var alpha = lineParams.b();
306 alpha = Math.round(alpha*100) / 100;
308 var beta = lineParams.m();
309 beta = Math.round(beta*100) / 100;
318 var equation = 'y = ' + alpha + sign + beta + 'x';
320 var rq = ss.r_squared(lsData, line);
321 rq = Math.round(rq*100) / 100;
322 rq = 'R-squared = ' + rq;
324 var lsLine = d3.svg.line()
327 return (pad.left + (width/2)) + xAxisScale(d[0]);
329 return (pad.left + (width/2)) - (-1 * xAxisScale(d[0]));
333 return ( pad.top + (height/2)) - yAxisScale(d[1]);
335 return (pad.top + (height/2)) + (-1 * yAxisScale(d[1]));
341 jQuery.each(phenotypeDeviations, function (i, x) {
343 var y = line(parseFloat(x[1]));
344 lsPoints.push([x[1], y]);
348 regressionPlot.append("svg:path")
349 .attr("d", lsLine(lsPoints))
350 .attr('stroke', '#86B404')
351 .attr('stroke-width', 2)
352 .attr('fill', 'none');
354 regressionPlot.append("g")
355 .attr("id", "equation")
360 .style("fill", "#86B404")
361 .style("font-weight", "bold");
363 regressionPlot.append("g")
364 .attr("id", "rsquare")
369 .style("fill", "#86B404")
370 .style("font-weight", "bold");