aboutsummaryrefslogtreecommitdiff
// Generated by CoffeeScript 1.9.2
(function() {
  var get_z_scores, redraw_prob_plot, root;

  root = typeof exports !== "undefined" && exports !== null ? exports : this;

  get_z_scores = function(n) {
    var i, j, osm_uniform, ref, x;
    osm_uniform = new Array(n);
    osm_uniform[n - 1] = Math.pow(0.5, 1.0 / n);
    osm_uniform[0] = 1 - osm_uniform[n - 1];
    for (i = j = 1, ref = n - 2; 1 <= ref ? j <= ref : j >= ref; i = 1 <= ref ? ++j : --j) {
      osm_uniform[i] = (i + 1 - 0.3175) / (n + 0.365);
    }
    return (function() {
      var k, len, results;
      results = [];
      for (k = 0, len = osm_uniform.length; k < len; k++) {
        x = osm_uniform[k];
        results.push(jStat.normal.inv(x, 0, 1));
      }
      return results;
    })();
  };

  redraw_prob_plot = function(samples, sample_group) {
    var container, h, margin, totalh, totalw, w;
    h = 600;
    w = 500;
    margin = {
      left: 60,
      top: 40,
      right: 40,
      bottom: 40,
      inner: 5
    };
    totalh = h + margin.top + margin.bottom;
    totalw = w + margin.left + margin.right;
    container = $("#prob_plot_container");
    container.width(totalw);
    container.height(totalh);
    return nv.addGraph((function(_this) {
      return function() {
        var W, all_samples, chart, data, intercept, make_data, names, pvalue, pvalue_str, slope, sorted_names, sorted_values, sw_result, test_str, x, z_scores;
        chart = nv.models.scatterChart().width(w).height(h).showLegend(true).color(d3.scale.category10().range());
        chart.pointRange([50, 50]);
        chart.legend.updateState(false);
        chart.xAxis.axisLabel("Expected Z score").axisLabelDistance(20).tickFormat(d3.format('.02f'));
        chart.tooltipContent(function(obj) {
          return '<b style="font-size: 20px">' + obj.point.name + '</b>';
        });
        all_samples = samples[sample_group];
        names = (function() {
          var j, len, ref, results;
          ref = _.keys(all_samples);
          results = [];
          for (j = 0, len = ref.length; j < len; j++) {
            x = ref[j];
            if (all_samples[x] !== null) {
              results.push(x);
            }
          }
          return results;
        })();
        sorted_names = names.sort(function(x, y) {
          return all_samples[x].value - all_samples[y].value;
        });
        max_decimals = 0
        sorted_values = (function() {
          var j, len, results;
          results = [];
          for (j = 0, len = sorted_names.length; j < len; j++) {
            x = sorted_names[j];
            results.push(all_samples[x].value);
            if (all_samples[x].value.countDecimals() > max_decimals) {
                max_decimals = all_samples[x].value.countDecimals()-1
            }
          }
          return results;
        })();
        //ZS: 0.1 indicates buffer, increase to increase buffer
        y_domain = [sorted_values[0] - (sorted_values.slice(-1)[0] - sorted_values[0])*0.1, sorted_values.slice(-1)[0] + (sorted_values.slice(-1)[0] - sorted_values[0])*0.1]
        chart.yDomain(y_domain)
        chart.yAxis.axisLabel("Trait value").axisLabelDistance(10).tickFormat(d3.format('.0'+max_decimals.toString()+'f'));
        sw_result = ShapiroWilkW(sorted_values);
        W = sw_result.w.toFixed(3);
        pvalue = sw_result.p.toFixed(3);
        pvalue_str = pvalue > 0.05 ? pvalue.toString() : "<span style='color:red'>" + pvalue + "</span>";
        test_str = "Shapiro-Wilk test statistic is " + W + " (p = " + pvalue_str + ")";
        z_scores = get_z_scores(sorted_values.length);
        //ZS: 0.1 indicates buffer, increase to increase buffer
        x_domain = [z_scores[0] - (z_scores.slice(-1)[0] - z_scores[0])*0.1, z_scores.slice(-1)[0] + (z_scores.slice(-1)[0] - z_scores[0])*0.1]
        chart.xDomain(x_domain)
        slope = jStat.stdev(sorted_values);
        intercept = jStat.mean(sorted_values);
        make_data = function(group_name) {
          var sample, value, z_score;
          return {
            key: js_data.sample_group_types[group_name],
            slope: slope,
            intercept: intercept,
            values: (function() {
              var j, len, ref, ref1, results;
              ref = _.zip(get_z_scores(sorted_values.length), sorted_values, sorted_names);
              results = [];
              for (j = 0, len = ref.length; j < len; j++) {
                ref1 = ref[j], z_score = ref1[0], value = ref1[1], sample = ref1[2];
                if (sample in samples[group_name]) {
                  results.push({
                    x: z_score,
                    y: value,
                    name: sample
                  });
                }
              }
              return results;
            })()
          };
        };
        data = [make_data('samples_primary'), make_data('samples_other')];
        d3.select("#prob_plot_container svg").datum(data).call(chart);
        if (js_data.trait_symbol != null) {
            $("#prob_plot_title").html("<h3>" + js_data.trait_symbol + ": " + js_data.trait_id + "</h3>");
        } else {
            $("#prob_plot_title").html("<h3>" + js_data.trait_id + "</h3>");
        }
        $("#shapiro_wilk_text").html(test_str)
        $("#prob_plot_container .nv-legendWrap").toggle(sample_group === "samples_all");
        return chart;
      };
    })(this));
  };

  root.redraw_prob_plot_impl = redraw_prob_plot;

}).call(this);