aboutsummaryrefslogtreecommitdiff
path: root/gn2/wqflask/static/new/javascript/chr_manhattan_plot.js
blob: c661edc783686c0db9d272872c97b1bbd7aecdb4 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Generated by CoffeeScript 1.8.0
var Chr_Manhattan_Plot;

Chr_Manhattan_Plot = (function() {
  function Chr_Manhattan_Plot(plot_height, plot_width, chr, manhattanPlot) {
    this.plot_height = plot_height;
    this.plot_width = plot_width;
    this.chr = chr;
    this.qtl_results = js_data.qtl_results;
    console.log("qtl_results are:", this.qtl_results);
    console.log("chr is:", this.chr);
    this.get_max_chr();
    this.filter_qtl_results();
    console.log("filtered results:", this.these_results);
    this.get_qtl_count();
    this.x_coords = [];
    this.y_coords = [];
    this.marker_names = [];
    console.time('Create coordinates');
    this.create_coordinates();
    console.log("@x_coords: ", this.x_coords);
    console.log("@y_coords: ", this.y_coords);
    console.timeEnd('Create coordinates');
    this.x_buffer = this.plot_width / 30;
    this.y_buffer = this.plot_height / 20;
    this.x_max = d3.max(this.x_coords);
    this.y_max = d3.max(this.y_coords) * 1.2;
    this.y_threshold = this.get_lod_threshold();
    this.svg = this.create_svg();
    this.plot_coordinates = _.zip(this.x_coords, this.y_coords, this.marker_names);
    console.log("coordinates:", this.plot_coordinates);
    this.plot_height -= this.y_buffer;
    this.create_scales();
    console.time('Create graph');
    this.create_graph();
    console.timeEnd('Create graph');
  }

  Chr_Manhattan_Plot.prototype.get_max_chr = function() {
    var key, _results;
    this.max_chr = 0;
    _results = [];
    for (key in js_data.chromosomes) {
      console.log("key is:", key);
      if (parseInt(key) > this.max_chr) {
        _results.push(this.max_chr = parseInt(key));
      } else {
        _results.push(void 0);
      }
    }
    return _results;
  };

  Chr_Manhattan_Plot.prototype.filter_qtl_results = function() {
    var result, this_chr, _i, _len, _ref, _results;
    this.these_results = [];
    this_chr = 100;
    _ref = this.qtl_results;
    _results = [];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      result = _ref[_i];
      if (result.chr === "X") {
        this_chr = this.max_chr;
      } else {
        this_chr = result.chr;
      }
      console.log("this_chr is:", this_chr);
      console.log("@chr[0] is:", parseInt(this.chr[0]));
      if (this_chr > parseInt(this.chr[0])) {
        break;
      }
      if (parseInt(this_chr) === parseInt(this.chr[0])) {
        _results.push(this.these_results.push(result));
      } else {
        _results.push(void 0);
      }
    }
    return _results;
  };

  Chr_Manhattan_Plot.prototype.get_qtl_count = function() {
    var high_qtl_count, result, _i, _len, _ref;
    high_qtl_count = 0;
    _ref = this.these_results;
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      result = _ref[_i];
      if (result.lod_score > 1) {
        high_qtl_count += 1;
      }
    }
    console.log("high_qtl_count:", high_qtl_count);
    return this.y_axis_filter = 2;
  };

  Chr_Manhattan_Plot.prototype.create_coordinates = function() {
    var result, _i, _len, _ref, _results;
    _ref = this.these_results;
    _results = [];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      result = _ref[_i];
      this.x_coords.push(parseFloat(result.Mb));
      this.y_coords.push(result.lod_score);
      _results.push(this.marker_names.push(result.name));
    }
    return _results;
  };

  Chr_Manhattan_Plot.prototype.create_svg = function() {
    var svg;
    svg = d3.select("#topchart").append("svg").attr("class", "chr_manhattan_plot").attr("width", this.plot_width + this.x_buffer).attr("height", this.plot_height + this.y_buffer).append("g");
    return svg;
  };

  Chr_Manhattan_Plot.prototype.create_scales = function() {
    this.x_scale = d3.scale.linear().domain([0, this.chr[1]]).range([this.x_buffer, this.plot_width]);
    return this.y_scale = d3.scale.linear().domain([0, this.y_max]).range([this.plot_height, this.y_buffer]);
  };

  Chr_Manhattan_Plot.prototype.get_lod_threshold = function() {
    if (this.y_max / 2 > 2) {
      return this.y_max / 2;
    } else {
      return 2;
    }
  };

  Chr_Manhattan_Plot.prototype.create_graph = function() {
    this.add_border();
    this.add_x_axis();
    this.add_y_axis();
    this.add_title();
    this.add_back_button();
    if (manhattanPlot) {
      return this.add_plot_points();
    } else {
      return this.add_path();
    }
  };

  Chr_Manhattan_Plot.prototype.add_border = function() {
    var border_coords;
    border_coords = [[this.y_buffer, this.plot_height, this.x_buffer, this.x_buffer], [this.y_buffer, this.plot_height, this.plot_width, this.plot_width], [this.y_buffer, this.y_buffer, this.x_buffer, this.plot_width], [this.plot_height, this.plot_height, this.x_buffer, this.plot_width]];
    return this.svg.selectAll("line").data(border_coords).enter().append("line").attr("y1", (function(_this) {
      return function(d) {
        return d[0];
      };
    })(this)).attr("y2", (function(_this) {
      return function(d) {
        return d[1];
      };
    })(this)).attr("x1", (function(_this) {
      return function(d) {
        return d[2];
      };
    })(this)).attr("x2", (function(_this) {
      return function(d) {
        return d[3];
      };
    })(this)).style("stroke", "#000");
  };

  Chr_Manhattan_Plot.prototype.add_x_axis = function() {
    this.xAxis = d3.svg.axis().scale(this.x_scale).orient("bottom").ticks(20);
    this.xAxis.tickFormat((function(_this) {
      return function(d) {
        d3.format("d");
        return d;
      };
    })(this));
    return this.svg.append("g").attr("class", "x_axis").attr("transform", "translate(0," + this.plot_height + ")").call(this.xAxis).selectAll("text").attr("text-anchor", "right").attr("font-size", "12px").attr("dx", "-1.6em").attr("transform", (function(_this) {
      return function(d) {
        return "translate(-12,0) rotate(-90)";
      };
    })(this));
  };

  Chr_Manhattan_Plot.prototype.add_y_axis = function() {
    this.yAxis = d3.svg.axis().scale(this.y_scale).orient("left").ticks(5);
    return this.svg.append("g").attr("class", "y_axis").attr("transform", "translate(" + this.x_buffer + ",0)").call(this.yAxis);
  };

  Chr_Manhattan_Plot.prototype.add_title = function() {
    return this.svg.append("text").attr("class", "title").text("Chr " + this.chr[0]).attr("x", (function(_this) {
      return function(d) {
        return (_this.plot_width + _this.x_buffer) / 2;
      };
    })(this)).attr("y", this.y_buffer + 20).attr("dx", "0em").attr("text-anchor", "middle").attr("font-family", "sans-serif").attr("font-size", "18px").attr("fill", "black");
  };

  Chr_Manhattan_Plot.prototype.add_back_button = function() {
    return this.svg.append("text").attr("class", "back").text("Return to full view").attr("x", this.x_buffer * 2).attr("y", this.y_buffer / 2).attr("dx", "0em").attr("text-anchor", "middle").attr("font-family", "sans-serif").attr("font-size", "18px").attr("cursor", "pointer").attr("fill", "black").on("click", this.return_to_full_view);
  };

  Chr_Manhattan_Plot.prototype.add_path = function() {
    var line_function, line_graph;
    line_function = d3.svg.line().x((function(_this) {
      return function(d) {
        return _this.x_scale(d[0]);
      };
    })(this)).y((function(_this) {
      return function(d) {
        return _this.y_scale(d[1]);
      };
    })(this)).interpolate("linear");
    return line_graph = this.svg.append("path").attr("d", line_function(this.plot_coordinates)).attr("stroke", "blue").attr("stroke-width", 1).attr("fill", "none");
  };

  Chr_Manhattan_Plot.prototype.add_plot_points = function() {
    return this.plot_point = this.svg.selectAll("circle").data(this.plot_coordinates).enter().append("circle").attr("cx", (function(_this) {
      return function(d) {
        return _this.x_scale(d[0]);
      };
    })(this)).attr("cy", (function(_this) {
      return function(d) {
        return _this.y_scale(d[1]);
      };
    })(this)).attr("r", (function(_this) {
      return function(d) {
        return 2;
      };
    })(this)).attr("fill", (function(_this) {
      return function(d) {
        return "black";
      };
    })(this)).attr("stroke", "black").attr("stroke-width", "1").attr("id", (function(_this) {
      return function(d) {
        return "point_" + String(d[2]);
      };
    })(this)).classed("circle", true).on("mouseover", (function(_this) {
      return function(d) {
        var this_id;
        console.log("d3.event is:", d3.event);
        console.log("d is:", d);
        this_id = "point_" + String(d[2]);
        return d3.select("#" + this_id).classed("d3_highlight", true).attr("r", 5).attr("stroke", "none").attr("fill", "blue").call(_this.show_marker_in_table(d));
      };
    })(this)).on("mouseout", (function(_this) {
      return function(d) {
        var this_id;
        this_id = "point_" + String(d[2]);
        return d3.select("#" + this_id).classed("d3_highlight", false).attr("r", function(d) {
          return 2;
        }).attr("fill", function(d) {
          return "black";
        }).attr("stroke", "black").attr("stroke-width", "1");
      };
    })(this)).append("svg:title").text((function(_this) {
      return function(d) {
        return d[2];
      };
    })(this));
  };

  Chr_Manhattan_Plot.prototype.return_to_full_view = function() {
    $('#topchart').remove();
    $('#chart_container').append('<div class="qtlcharts" id="topchart"></div>');
    return create_manhattan_plot();
  };

  Chr_Manhattan_Plot.prototype.show_marker_in_table = function(marker_info) {
    var marker_name;
    console.log("in show_marker_in_table");

    /* Searches for the select marker in the results table below */
    if (marker_info) {
      marker_name = marker_info[2];
      return $("#qtl_results_filter").find("input:first").val(marker_name).change();
    }
  };

  return Chr_Manhattan_Plot;

})();