aboutsummaryrefslogtreecommitdiff
// Generated by CoffeeScript 1.9.2
var Chr_Lod_Chart;

Chr_Lod_Chart = (function() {
  function Chr_Lod_Chart(plot_height, plot_width, chr, manhattanPlot, mappingScale) {
    this.plot_height = plot_height;
    this.plot_width = plot_width;
    this.chr = chr;
    this.manhattanPlot = manhattanPlot;
    this.mappingScale = mappingScale;
    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_Lod_Chart.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_Lod_Chart.prototype.filter_qtl_results = function() {
    var i, len, ref, result, results, this_chr;
    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;
      }
      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_Lod_Chart.prototype.get_qtl_count = function() {
    var high_qtl_count, i, len, ref, result;
    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_Lod_Chart.prototype.create_coordinates = function() {
    var i, len, ref, result, results;
    ref = this.these_results;
    console.log("THESE_RESULTS:", ref)
    results = [];
    for (i = 0, len = ref.length; i < len; i++) {
      result = ref[i];
      this.x_coords.push(parseFloat(result.Mb));
      if (js_data.result_score_type == "LOD") {
        this.y_coords.push(result.lod_score);
      }
      else {
        console.log("LRS VALUE:", result['lrs_value'])
        this.y_coords.push(result['lrs_value']);
      }
      results.push(this.marker_names.push(result.name));
    }
    return results;
  };

  Chr_Lod_Chart.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_Lod_Chart.prototype.create_scales = function() {
    if (this.mappingScale == "morgan") {
        max_pos = 0
        for (i = 0, len = this.these_results.length; i < len; i++) {
           marker = this.these_results[i]
           if (parseFloat(marker['Mb']) > max_pos){
               max_pos = parseFloat(marker.Mb)
           }
        }
        this.x_scale = d3.scale.linear().domain([0, max_pos]).range([this.x_buffer, this.plot_width]);
    }
    else {
        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_Lod_Chart.prototype.get_lod_threshold = function() {
    if (this.y_max / 2 > 2) {
      return this.y_max / 2;
    } else {
      return 2;
    }
  };

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

  Chr_Lod_Chart.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_Lod_Chart.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_Lod_Chart.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_Lod_Chart.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_Lod_Chart.prototype.add_back_button = function() {
    return $("#return_to_full_view").show().click((function(_this) {
      return function() {
        return _this.return_to_full_view();
      };
    })(this));
  };

  Chr_Lod_Chart.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_Lod_Chart.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_Lod_Chart.prototype.return_to_full_view = function() {
    $("#return_to_full_view").hide();
    $('#topchart').remove();
    $('#chart_container').append('<div class="qtlcharts" id="topchart"></div>');
    return create_lod_chart();
  };

  Chr_Lod_Chart.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_Lod_Chart;

})();