<commit_before>var Oncoprint = function(wrapper, params) {

  params.clinicalData = params.clinicalData || [];     // initialize
  params.clinical_attrs = params.clinical_attrs || [];

  var data = d3.nest()
        .key(function(d) { return d.sample; })
        .entries(params.geneData.concat(params.clinicalData));

    if (params.clinicalData === [] && params.clinical_attrs !== undefined) {
      throw {
        name: "Data Mismatch Error",
        message: "There are clinical attributes for nonexistant clinical data"
      }
    }

    var attributes = params.clinical_attrs.concat(params.genes);

    // filter out attributes that are not in the attributes list
    data = data.map(function(i) {
        return {
          key: i.key,
          values: i.values.filter(function(j) { var attr = j.gene || j.attr_id; return attributes.indexOf(attr) !== -1; })
        };
    });

    // attr2range : clinical attribute string ->
    // list of values or range of values, e.g. [min, max]
    var attr2range = params.clinicalData.reduce(function(prev, curr) {
        prev[curr.attr_id] = prev[curr.attr_id] || [];      // initialize

        var a2r =  prev[curr.attr_id];
        var val = curr.attr_val;

        if (val === "NA") {
            return prev;
        }

        if (isNaN(parseInt(val))) {
            if (a2r.indexOf(val) === -1) { a2r.push(val); }       // keep a set of unique elements
        }

        else
        {
            // just keep the min and max -- an interval of values
            val = parseInt(val);
            var min = a2r[0],
                max = a2r[1];

            if (max === undefined || val > max) {
                a2r[1] = val;
            }
            if (min === undefined || val < min) {
                a2r[0] = val;
            }
        }

        prev[curr.attr_id] = a2r;
        return prev;
    }, {});

    // convert ranges to d3 scales
    // simplistic : string -> discrete , number -> continuous
    for (var a2r in attr2range) {
        var scale = attr2range[a2r];
        var new_scale = isNaN(parseInt(scale[0])) ? d3.scale.ordinal() : d3.scale.linear();
        new_scale.domain(scale);

        new_scale.range(["#ff7f0e", "#1f77b4"]);

        attr2range[a2r] = new_scale;
    }

    var dims = {
      width: data.length * (5.5 + 3),
      height: (23 + 5) * attributes.length
    };

    var getAttr = function(d) {
        return d.gene || d.attr_id;
    };

    var margin = { top: 80, right: 80, left: 80, bottom: 80 };

    var svg = d3.select(wrapper)
        .append("svg")
        .attr('width', dims.width)
        .attr('height', dims.height);

    var cna_fills = {
        undefined: '#D3D3D3',
        AMPLIFIED: '#FF0000',
        GAINED: '#FFB6C1',
        DIPLOID: '#D3D3D3',
        HEMIZYGOUSLYDELETED: '#8FD8D8',
        HOMODELETED: '#0000FF'
    };

//    var clinical = function(d) {
//        console.log(d);
//
//        var cont_scale = d3.scale.linear()
//                .domain([0,5000])
//                .range([ "#ff7f0e", "#1f77b4"])
//            ;
//        if (d.attr_id === "OVERALL_SURVIVAL_DAYS") {
//            return d.attr_val === "NA" ? '#D3D3D3' : cont_scale(parseInt(d.attr_val));
//        }
//
//        if (d.attr_id === "VITAL_STATUS") {
//            return d.attr_val === "living" ? "#1f77b4" : "#ff7f0e";
//        }
//
//        return attr2range[d.attr_id](d.attr_val);
//    };

  var enterSample = function(sample) {
        var enter = sample.enter();

        // N.B. fill doubles as cna
        var fill = enter.append('rect')
        .attr('fill', function(d) { return d.gene !== undefined ? cna_fills[d.cna] : attr2range[d.attr_id](d.attr_val) })
        .attr('height', 23)
            .attr('width', 5.5)
            .attr('y', function(d) {
              return (23 + 4) * attributes.indexOf(getAttr(d)); });

        var mut = enter.append('rect')
            .attr('fill', 'green')
            .attr('height', 10)
            .attr('width', 5.5)
            .attr('y', function(d) {
              return (23 + 4) * attributes.indexOf(getAttr(d)); });
        mut.filter(function(d) {
            return d.mutation === undefined;
        }).remove();

    var sym = d3.svg.symbol().size(5.5 * 2);
    var rppa = enter.append('path')
        .attr('d', sym.type(function(d) {
          return d.rppa === "UPREGULATED" ? "triangle-up" : "triangle-down" }))
        .attr('transform', function(d) {return 'translate(2.75,10)'; })
    ;
    rppa.filter(function(d) {
      return d.rppa === undefined;
    }).remove();

    var mrna = enter.append('rect')
        .attr('y', function(d) { return (23 + 4) * attributes.indexOf(getAttr(d)); })
        .attr('height', 23)
        .attr('width', 5.5)
        .attr('stroke-width', 2)
        .attr('stroke-opacity', 1)
        .attr('stroke', function(d) { return d.mrna === "UPREGULATED" ? '#FF9999' : '#6699CC' })
        .attr('fill', 'none')
      mrna.filter(function(d) {
          return d.mrna === undefined;
      }).remove();
  };

  var sample = svg.selectAll('g')
      .data(data)
      .enter()
      .append('g')
      .attr('transform', function(d,i) { return "translate(" + i * (5.5 + 2) + ",0)"; })
      .selectAll('rect')
      .data(function(d) {
          return d.values;
      });

  enterSample(sample);

  $('#oncoprint').children().show();      // todo: delete me!

};

var _Oncoprint = function(wrapper, params) {
    var that = {};

    var RECT_HEIGHT = 23;
    var TRACK_PADDING = 4;
    var LABEL_PADDING = 40;
    var LITTLE_RECT_HEIGHT = RECT_HEIGHT / 3;
    var MRNA_STROKE_WIDTH = 2;
    var UPREGULATED = "UPREGULATED";
    var DOWNREGULATED = "DOWNREGULATED";
    var MRNA_UP_COLOR = "#FF9999";
    var MRNA_DOWN_COLOR = "#6699CC";
    var mrna_fills = {
        UPREGULATED: "#FF9999",
        DOWNREGULATED: "#6699CC"
    };
    var MUT_COLOR = "#008000";
    var RPPA_LIGHT = "#D3D3D3";
    var RPPA_DARK = "#000000";
    var cna_fills = {
        none: '#D3D3D3',
        AMPLIFIED: '#FF0000',<commit_msg>did some factoring. playing with some interactive. sorting is next<commit_after>var Oncoprint = function(wrapper, params) {
    params.clinicalData = params.clinicalData || [];     // initialize
    params.clinical_attrs = params.clinical_attrs || [];

    var data = d3.nest()
        .key(function(d) { return d.sample; })
        .entries(params.geneData.concat(params.clinicalData));

    if (params.clinicalData === [] && params.clinical_attrs !== undefined) {
        throw {
            name: "Data Mismatch Error",
            message: "There are clinical attributes for nonexistant clinical data"
        }
    }

    var attributes = params.clinical_attrs.concat(params.genes);

    // filter out attributes that are not in the attributes list
    data = data.map(function(i) {
        return {
            key: i.key,
            values: i.values.filter(function(j) { var attr = j.gene || j.attr_id; return attributes.indexOf(attr) !== -1; })
        };
    });

    // attr2range : clinical attribute string ->
    // list of values or range of values, e.g. [min, max]
    var attr2range = params.clinicalData.reduce(function(prev, curr) {
        prev[curr.attr_id] = prev[curr.attr_id] || [];      // initialize

        var a2r =  prev[curr.attr_id];
        var val = curr.attr_val;

        if (val === "NA") {
            return prev;
        }

        if (isNaN(parseInt(val))) {
            if (a2r.indexOf(val) === -1) { a2r.push(val); }       // keep a set of unique elements
        }

        else
        {
            // just keep the min and max -- an interval of values
            val = parseInt(val);
            var min = a2r[0],
                max = a2r[1];

            if (max === undefined || val > max) {
                a2r[1] = val;
            }
            if (min === undefined || val < min) {
                a2r[0] = val;
            }
        }

        prev[curr.attr_id] = a2r;
        return prev;
    }, {});

    // convert ranges to d3 scales
    // simplistic : string -> discrete , number -> continuous
    for (var a2r in attr2range) {
        var scale = attr2range[a2r];
        var new_scale = isNaN(parseInt(scale[0])) ? d3.scale.ordinal() : d3.scale.linear();
        new_scale.domain(scale);

        new_scale.range(["#ff7f0e", "#1f77b4"]);

        attr2range[a2r] = new_scale;
    }

    var dims = {
        width: data.length * (5.5 + 3),
        height: (23 + 5) * attributes.length
    };

    var getAttr = function(d) {
        return d.gene || d.attr_id;
    };

    var margin = { top: 80, right: 80, left: 80, bottom: 80 };

    var svg = d3.select(wrapper)
        .append("svg")
        .attr('width', dims.width)
        .attr('height', dims.height);

    var cna_fills = {
        undefined: '#D3D3D3',
        AMPLIFIED: '#FF0000',
        GAINED: '#FFB6C1',
        DIPLOID: '#D3D3D3',
        HEMIZYGOUSLYDELETED: '#8FD8D8',
        HOMODELETED: '#0000FF'
    };

//    var clinical = function(d) {
//        console.log(d);
//
//        var cont_scale = d3.scale.linear()
//                .domain([0,5000])
//                .range([ "#ff7f0e", "#1f77b4"])
//            ;
//        if (d.attr_id === "OVERALL_SURVIVAL_DAYS") {
//            return d.attr_val === "NA" ? '#D3D3D3' : cont_scale(parseInt(d.attr_val));
//        }
//
//        if (d.attr_id === "VITAL_STATUS") {
//            return d.attr_val === "living" ? "#1f77b4" : "#ff7f0e";
//        }
//
//        return attr2range[d.attr_id](d.attr_val);
//    };

    var translate = function(x,y) {
        return "translate(" + x + "," + y + ")";
    };

    var dims = (function() {
        var rect_height = 23;

        var mut_height = rect_height / 3;

        return {
            rect_height: rect_height,
            rect_width: 5.5,
            vert_padding: 4,
            hor_padding: 2,
            mut_height: mut_height
        }
    }());

    // it's entering time
    var enterSample = function(sample) {
        var enter = sample.enter();

        // N.B. fill doubles as cna
        var fill = enter.append('rect')
            .attr('fill', function(d) { return d.gene !== undefined ?
                cna_fills[d.cna] :                  // gene data
                attr2range[d.attr_id](d.attr_val)   // clinical data
            })
            .attr('height', function(d) {return d.gene !== undefined ? dims.rect_height : dims.rect_height / 3})
            .attr('width', dims.rect_width)
            .attr('y', function(d) {
                return d.gene !== undefined ?
                    (dims.rect_height + dims.vert_padding) * (1 + params.genes.indexOf(getAttr(d))) :
                    (dims.rect_height / 3) * attributes.indexOf(getAttr(d));});

        var mut = enter.append('rect')
            .attr('fill', 'green')
            .attr('height', dims.mut_height)
            .attr('width', dims.rect_width)
            .attr('y', function(d) {
                return dims.mut_height + (dims.rect_height + dims.vert_padding) * (1 + params.genes.indexOf(getAttr(d))); });
        mut.filter(function(d) {
            return d.mutation === undefined;
        }).remove();

        var sym = d3.svg.symbol().size(dims.rect_width * 2);
        var rppa = enter.append('path')
                .attr('d', sym.type(function(d) {
                    return d.rppa === "UPREGULATED" ? "triangle-up" : "triangle-down" }))
                .attr('transform', function(d) {
                    var topbottom = dims.rect_height / 2;
                    return translate(dims.rect_width / 2, topbottom + (dims.rect_height + dims.vert_padding) * (1 + params.genes.indexOf(getAttr(d)))); });
        rppa.filter(function(d) {
            return d.rppa === undefined;
        }).remove();

        var mrna = enter.append('rect')
            .attr('y', function(d) { return (dims.rect_height + dims.vert_padding) * (1 + params.genes.indexOf(getAttr(d))); })
            .attr('height', dims.rect_height)
            .attr('width', dims.rect_width)
            .attr('stroke-width', 2)
            .attr('stroke-opacity', 1)
            .attr('stroke', function(d) { return d.mrna === "UPREGULATED" ? '#FF9999' : '#6699CC' })
            .attr('fill', 'none');
        mrna.filter(function(d) {
            return d.mrna === undefined;
        }).remove();
    };

    var sample = svg.selectAll('g')
        .data(data)
        .enter()
        .append('g')
        .attr('class', 'sample')
        .attr('transform', function(d,i) { return translate(i * (dims.rect_width + dims.hor_padding), 0); })
            .selectAll('rect')
            .data(function(d) {
                return d.values;
            });

    enterSample(sample);

    // remove white space
//    d3.selectAll('.sample').transition()
//        .duration(1000)
//        .attr('transform', function(d,i) { return translate(i * dims.rect_width, 0); })

//    var state = (function() {
//    }())

    // change width
//    d3.selectAll('.sample *').transition()
//        .duration(1000)
//        .attr('width', dims.rect_width - 3)
//    d3.selectAll('.sample path').transition(); // ... do something to these triangles

    $('#oncoprint').children().show();      // todo: delete me!
};

var _Oncoprint = function(wrapper, params) {
    var that = {};

    var RECT_HEIGHT = 23;
    var TRACK_PADDING = 4;
    var LABEL_PADDING = 40;
    var LITTLE_RECT_HEIGHT = RECT_HEIGHT / 3;
    var MRNA_STROKE_WIDTH = 2;
    var UPREGULATED = "UPREGULATED";
    var DOWNREGULATED = "DOWNREGULATED";
    var MRNA_UP_COLOR = "#FF9999";
    var MRNA_DOWN_COLOR = "#6699CC";
    var mrna_fills = {
        UPREGULATED: "#FF9999",
        DOWNREGULATED: "#6699CC"
    };
    var MUT_COLOR = "#008000";
    var RPPA_LIGHT = "#D3D3D3";
    var RPPA_DARK = "#000000";
    var cna_fills = {
        none: '#D3D3D3',
        AMPLIFIED: '#FF0000',
{'fraction_non_alphanumeric': 0.16282918405525976, 'fraction_numerical': 0.025543243632177293, 'mean_word_length': 1.8944189920866306, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2257097', 'n_tokens_mistral': 4892, 'n_tokens_neox': 4577, 'n_words': 956}