aboutsummaryrefslogtreecommitdiff
path: root/gn2/wqflask/static/new/javascript/draw_probability_plot.js
blob: 1b944d4fead3b978abcbba4ecd0104be04a12ff7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// 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);