aboutsummaryrefslogtreecommitdiff
path: root/gn2/wqflask/static/new/javascript/get_traits_from_collection.js
blob: c115f3b091d0438d7e50e7817e9ff2b42e11967a (about) (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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Generated by CoffeeScript 1.8.0
var add_trait_data, assemble_into_json, back_to_collections, collection_click, collection_list, color_by_trait, create_trait_data_csv, get_this_trait_vals, get_trait_data, process_traits, selected_traits, submit_click, this_trait_data, trait_click,
  __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

this_trait_data = null;

selected_traits = {};

$('#collections_list').attr("style", "width: 100%;");
$('#trait_table').dataTable( {
    "drawCallback": function( settings ) {
         $('#trait_table tr').click(function(event) {
             if (event.target.type !== 'checkbox') {
                 $(':checkbox', this).trigger('click');
             }
         });
    },
    "columns": [
        { "type": "natural", "width": "3%" },
        { "type": "natural", "width": "8%" },
        { "type": "natural", "width": "20%" },
        { "type": "natural", "width": "25%" },
        { "type": "natural", "width": "25%" },
        { "type": "natural", "width": "15%" }
    ],
    "columnDefs": [ {
        "targets": 0,
        "orderable": false
    } ],
    "order": [[1, "asc" ]],
    "sDom": "RZtr",
    "iDisplayLength": -1,
    "autoWidth": true,
    "bDeferRender": true,
    "bSortClasses": false,
    "paging": false,
    "orderClasses": true
} );

if ( ! $.fn.DataTable.isDataTable( '#collection_table' ) ) {
  $('#collection_table').dataTable( {
    "createdRow": function ( row, data, index ) {
        if ($('td', row).eq(2).text().length > 40) {
            $('td', row).eq(2).text($('td', row).eq(2).text().substring(0, 40));
            $('td', row).eq(2).text($('td', row).eq(2).text() + '...')
        }
        if ($('td', row).eq(4).text().length > 50) {
            $('td', row).eq(4).text($('td', row).eq(4).text().substring(0, 50));
            $('td', row).eq(4).text($('td', row).eq(4).text() + '...')
        }
    },
    "columnDefs": [ {
        "targets": 0,
        "orderable": false
    } ],
    "order": [[1, "asc" ]],
    "sDom": "ZRtr",
    "iDisplayLength": -1,
    "autoWidth": true,
    "bSortClasses": false,
    "paging": false,
    "orderClasses": true
  } );
}

collection_click = function() {
  var this_collection_url;

  this_collection_url = $(this).find('.collection_name').prop("href");
  this_collection_url += "&json";
  collection_list = $("#collections_holder").html();
  return $.ajax({
    dataType: "json",
    url: this_collection_url,
    success: process_traits
  });
};

submit_click = function() {
  var all_vals, sample, samples, scatter_matrix, this_trait_vals, trait, trait_names, trait_vals_csv, traits, _i, _j, _len, _len1, _ref;
  selected_traits = {};
  traits = [];
  $('#collections_holder').find('input[type=checkbox]:checked').each(function() {
    var this_dataset, this_trait, this_trait_url;
    this_trait = $(this).parents('tr').find('.trait').text();
    this_dataset = $(this).parents('tr').find('.dataset').text();
    this_trait_url = "/trait/get_sample_data?trait=" + this_trait + "&dataset=" + this_dataset;
    return $.ajax({
      dataType: "json",
      url: this_trait_url,
      async: false,
      success: add_trait_data
    });
  });
  trait_names = [];
  samples = $('input[name=allsamples]').val().split(" ");
  all_vals = [];
  this_trait_vals = get_this_trait_vals(samples);
  all_vals.push(this_trait_vals);
  _ref = Object.keys(selected_traits);
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
    trait = _ref[_i];
    trait_names.push(trait);
    this_trait_vals = [];
    for (_j = 0, _len1 = samples.length; _j < _len1; _j++) {
      sample = samples[_j];
      if (__indexOf.call(Object.keys(selected_traits[trait]), sample) >= 0) {
        this_trait_vals.push(parseFloat(selected_traits[trait][sample]));
      } else {
        this_trait_vals.push(null);
      }
    }
    all_vals.push(this_trait_vals);
  }
  trait_vals_csv = create_trait_data_csv(selected_traits);
  scatter_matrix = new ScatterMatrix(trait_vals_csv);
  scatter_matrix.render();
  return $.colorbox.close();
};

create_trait_data_csv = function(selected_traits) {
  var all_vals, index, sample, sample_vals, samples, this_trait_vals, trait, trait_names, trait_vals_csv, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref;
  trait_names = [];
  trait_names.push($('input[name=trait_id]').val());
  samples = $('input[name=allsamples]').val().split(" ");
  all_vals = [];
  this_trait_vals = get_this_trait_vals(samples);
  all_vals.push(this_trait_vals);
  _ref = Object.keys(selected_traits);
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
    trait = _ref[_i];
    trait_names.push(trait);
    this_trait_vals = [];
    for (_j = 0, _len1 = samples.length; _j < _len1; _j++) {
      sample = samples[_j];
      if (__indexOf.call(Object.keys(selected_traits[trait]), sample) >= 0) {
        this_trait_vals.push(parseFloat(selected_traits[trait][sample]));
      } else {
        this_trait_vals.push(null);
      }
    }
    all_vals.push(this_trait_vals);
  }

  trait_vals_csv = trait_names.join(",");
  trait_vals_csv += "\n";
  for (index = _k = 0, _len2 = samples.length; _k < _len2; index = ++_k) {
    sample = samples[index];
    if (all_vals[0][index] === null) {
      continue;
    }
    sample_vals = [];
    for (_l = 0, _len3 = all_vals.length; _l < _len3; _l++) {
      trait = all_vals[_l];
      sample_vals.push(trait[index]);
    }
    trait_vals_csv += sample_vals.join(",");
    trait_vals_csv += "\n";
  }
  return trait_vals_csv;
};

trait_click = function() {
  var dataset, this_trait_url, trait;

  trait = $(this).parent().find('.trait').text();
  dataset = $(this).parent().find('.dataset').text();
  this_trait_url = "/trait/get_sample_data?trait=" + trait + "&dataset=" + dataset;
  $.ajax({
    dataType: "json",
    url: this_trait_url,
    success: get_trait_data
  });
  return $.colorbox.close();
};

trait_row_click = function() {
  var dataset, this_trait_url, trait;
  trait = $(this).find('.trait').text();
  dataset = $(this).find('.dataset').data("dataset");
  this_trait_url = "/trait/get_sample_data?trait=" + trait + "&dataset=" + dataset;
  $.ajax({
    dataType: "json",
    url: this_trait_url,
    success: get_trait_data
  });
  return $.colorbox.close();
};

add_trait_data = function(trait_data, textStatus, jqXHR) {
  var trait_name, trait_sample_data;
  trait_name = trait_data[0];
  trait_sample_data = trait_data[1];
  selected_traits[trait_name] = trait_sample_data;
  return console.log("selected_traits:", selected_traits);
};

populate_cofactor_info = function(trait_info) {
  if ($('input[name=selecting_which_cofactor]').val() == "1"){
    $('#cofactor1_trait_link').attr("href", trait_info['url'])
    if (trait_info['type'] == "ProbeSet"){
      $('#cofactor1_trait_link').text(trait_info['species'] + " " + trait_info['group'] + " " + trait_info['tissue'] + " " + trait_info['db'] + ": " + trait_info['name'])
      $('#cofactor1_description').text("[" + trait_info['symbol'] + " on " + trait_info['location'] + " Mb]\n" + trait_info['description'])
    } else if (trait_info['type'] == "Publish") {
      $('#cofactor1_trait_link').text(trait_info['species'] + " " + trait_info['group'] + " " + trait_info['db'] + ": " + trait_info['name'])
      if ('pubmed_link' in trait_info) {
        $('#cofactor1_description').html('<a href=\"' + trait_info['pubmed_link'] + '\">PubMed: ' + trait_info['pubmed_text'] + '</a><br>' + trait_info['description'])
      } else {
        $('#cofactor1_trait_link').text(trait_info['species'] + " " + trait_info['group'] + " " + trait_info['db'] + ": " + trait_info['name'])
        $('#cofactor1_description').text("[" + trait_info['name'] + " on " + trait_info['location'] + " Mb]\n" + trait_info['description'])
      }
    } else {
      $('#cofactor1_trait_link').text(trait_info['species'] + " " + trait_info['group'] + " " + trait_info['db'] + ": " + trait_info['name'])
      $('#cofactor1_description').text("[" + trait_info['name'] + " on " + trait_info['location'] + " Mb]\n")
    }
    $('#select_cofactor1').text("Change Cofactor 1");
    $('#cofactor1_info_container').css("display", "inline");
    $('#cofactor2_button').css("display", "inline-block");
  } else if ($('input[name=selecting_which_cofactor]').val() == "2"){
    $('#cofactor2_trait_link').attr("href", trait_info['url'])
    if (trait_info['type'] == "ProbeSet"){
      $('#cofactor2_trait_link').text(trait_info['species'] + " " + trait_info['group'] + " " + trait_info['tissue'] + " " + trait_info['db'] + ": " + trait_info['name'])
      $('#cofactor2_description').text("[" + trait_info['symbol'] + " on " + trait_info['location'] + " Mb]\n" + trait_info['description'])
    } else if (trait_info['type'] == "Publish") {
      $('#cofactor2_trait_link').text(trait_info['species'] + " " + trait_info['group'] + " " + trait_info['db'] + ": " + trait_info['name'])
      if ('pubmed_link' in trait_info) {
        $('#cofactor2_description').html('<a href=\"' + trait_info['pubmed_link'] + '\">PubMed: ' + trait_info['pubmed_text'] + '</a><br>' + trait_info['description'])
      } else {
        $('#cofactor2_trait_link').text(trait_info['species'] + " " + trait_info['group'] + " " + trait_info['db'] + ": " + trait_info['name'])
        $('#cofactor2_description').text("[" + trait_info['name'] + " on " + trait_info['location'] + " Mb]\n" + trait_info['description'])
      }
    } else {
      $('#cofactor2_trait_link').text(trait_info['species'] + " " + trait_info['group'] + " " + trait_info['db'] + ": " + trait_info['name'])
      $('#cofactor2_description').text("[" + trait_info['name'] + " on " + trait_info['location'] + " Mb]\n")
    }
    $('#select_cofactor2').text("Change Cofactor 2");
    $('#cofactor2_info_container').css("display", "inline");
    $('#cofactor3_button').css("display", "inline-block");
  } else {
    $('#cofactor3_trait_link').attr("href", trait_info['url'])
    if (trait_info['type'] == "ProbeSet"){
      $('#cofactor3_trait_link').text(trait_info['species'] + " " + trait_info['group'] + " " + trait_info['tissue'] + " " + trait_info['db'] + ": " + trait_info['name'])
      $('#cofactor3_description').text("[" + trait_info['symbol'] + " on " + trait_info['location'] + " Mb]\n" + trait_info['description'])
    } else if (trait_info['type'] == "Publish") {
      $('#cofactor3_trait_link').text(trait_info['species'] + " " + trait_info['group'] + " " + trait_info['db'] + ": " + trait_info['name'])
      if ('pubmed_link' in trait_info) {
        $('#cofactor3_description').html('<a href=\"' + trait_info['pubmed_link'] + '\">PubMed: ' + trait_info['pubmed_text'] + '</a><br>' + trait_info['description'])
      } else {
        $('#cofactor3_trait_link').text(trait_info['species'] + " " + trait_info['group'] + " " + trait_info['db'] + ": " + trait_info['name'])
        $('#cofactor3_description').text("[" + trait_info['name'] + " on " + trait_info['location'] + " Mb]\n" + trait_info['description'])
      }
    } else {
      $('#cofactor3_trait_link').text(trait_info['species'] + " " + trait_info['group'] + " " + trait_info['db'] + ": " + trait_info['name'])
      $('#cofactor3_description').text("[" + trait_info['name'] + " on " + trait_info['location'] + " Mb]\n")
    }
    $('#select_cofactor3').text("Change Cofactor 3");
    $('#cofactor3_info_container').css("display", "inline");
  }
}

get_trait_data = function(trait_data, textStatus, jqXHR) {
  var sample, samples, this_trait_vals, trait_sample_data, vals, _i, _len;
  trait_sample_data = trait_data[1];
  if ( $('input[name=allsamples]').length ) {
    samples = $('input[name=allsamples]').val().split(" ");
  } else {
    samples = js_data.indIDs
  }
  sample_vals = [];
  vals = [];
  for (_i = 0, _len = samples.length; _i < _len; _i++) {
    sample = samples[_i];
    if (sample in trait_sample_data) {
      sample_vals.push(sample + ":" + parseFloat(trait_sample_data[sample]))
      vals.push(parseFloat(trait_sample_data[sample]))
    } else {
      sample_vals.push(null)
      vals.push(null)
    }
  }
  if ( $('input[name=allsamples]').length ) {
    if ($('input[name=samples]').length < 1) {
      $('#hidden_inputs').append('<input type="hidden" name="samples" value="[' + samples.toString() + ']" />');
    }
    $('#hidden_inputs').append('<input type="hidden" name="vals" value="[' + vals.toString() + ']" />');
    this_trait_vals = get_this_trait_vals(samples);
    return color_by_trait(trait_sample_data);
  } else{
    sorted = vals.slice().sort(function(a,b){return a-b})
    ranks = vals.slice().map(function(v){ return sorted.indexOf(v)+1 });
    sample_ranks = []
    for (_i = 0; _i < samples.length; _i++){
      if (samples[_i] in trait_sample_data){
        sample_ranks.push(samples[_i] + ":" + ranks[_i])
      } else {
        sample_ranks.push(null)
      }
    }

    if ($('input[name=selecting_which_cofactor]').val() == "1"){
      if ($('#cofactor1_type option:selected').val() == "symbol") {
        unique_vals = [...new Set(vals)]
        if (unique_vals.length > 45) {
          alert("If displaying cofactor as symbol, please choose a trait with 45 or fewer distinct sample values.");
          return false;
        }
      }
      $('input[name=cofactor1_vals]').val(sample_vals)
      $('input[name=ranked_cofactor1_vals]').val(sample_ranks)
    } else if ($('input[name=selecting_which_cofactor]').val() == "2"){
      if ($('#cofactor2_type option:selected').val() == "symbol") {
        unique_vals = [...new Set(vals)]
        if (unique_vals.length > 45) {
          alert("If displaying cofactor as symbol, please choose a trait with 45 or fewer distinct sample values.");
          return false;
        }
      }
      $('input[name=cofactor2_vals]').val(sample_vals)
      $('input[name=ranked_cofactor2_vals]').val(sample_ranks)
    } else{
      if ($('#cofactor3_type option:selected').val() == "symbol") {
        unique_vals = [...new Set(vals)]
        if (unique_vals.length > 45) {
          alert("If displaying cofactor as symbol, please choose a trait with 45 or fewer distinct sample values.");
          return false;
        }
      }
      $('input[name=cofactor3_vals]').val(sample_vals)
      $('input[name=ranked_cofactor3_vals]').val(sample_ranks)
    }
    populate_cofactor_info(trait_data[0])
    chartupdatedata();
    return false
  }
};

get_this_trait_vals = function(samples) {
  var sample, this_trait_vals, this_val, this_vals_json, _i, _len;
  this_trait_vals = [];
  for (_i = 0, _len = samples.length; _i < _len; _i++) {
    sample = samples[_i];
    this_val = parseFloat($("input[name='value:" + sample + "']").val());
    if (!isNaN(this_val)) {
      this_trait_vals.push(this_val);
    } else {
      this_trait_vals.push(null);
    }
  }
  this_vals_json = '[' + this_trait_vals.toString() + ']';
  return this_trait_vals;
};

assemble_into_json = function(this_trait_vals) {
  var json_data, json_ids, num_traits, samples;
  num_traits = $('input[name=vals]').length;
  samples = $('input[name=samples]').val();
  json_ids = samples;
  json_data = '[' + this_trait_vals;
  $('input[name=vals]').each((function(_this) {
    return function(index, element) {
      return json_data += ',' + $(element).val();
    };
  })(this));
  json_data += ']';
  return [json_ids, json_data];
};

color_by_trait = function(trait_sample_data, textStatus, jqXHR) {
  return root.bar_chart.color_by_trait(trait_sample_data);
};

process_traits = function(trait_data, textStatus, jqXHR) {
  var the_html, trait, _i, _len;
  the_html = "<button id='back_to_collections' class='btn btn-inverse btn-small'>";
  the_html += "<i class='icon-white icon-arrow-left'></i> Back </button>";
  the_html += "    <button id='submit_cofactors' class='btn btn-primary btn-small submit'> Submit </button>";
  the_html += "<table id='collection_table' style='padding-top: 10px;' class='table table-hover'>";
  the_html += "<thead><tr><th></th><th>Record</th><th>Data Set</th><th>Description</th></tr></thead>";
  the_html += "<tbody>";
  for (_i = 0, _len = trait_data.length; _i < _len; _i++) {
    trait = trait_data[_i];
    the_html += "<tr class='trait_line'>";
    the_html += "<td class='select_trait'><input type='checkbox' name='selectCheck' class='checkbox edit_sample_checkbox'></td>";
    if ("abbreviation" in trait) {
        the_html += "<td class='trait' data-display_name='" + trait.name + " - " + trait.abbreviation + "'>" + trait.name + "</td>";
    } else if ("symbol" in trait) {
      the_html += "<td class='trait' data-display_name='" + trait.name + " - " + trait.symbol + "'>" + trait.name + "</td>";
    } else {
      the_html += "<td class='trait' data-display_name='" + trait.name + "'>" + trait.name + "</td>";
    }
    the_html += "<td class='dataset' data-dataset='" + trait.dataset + "'>" + trait.dataset_name + "</td>";
    the_html += "<td class='description'>" + trait.description + "</td>";
  }
  the_html += "</tbody>";
  the_html += "</table>";
  the_html += "<script type='text/javascript' src='/static/new/javascript/get_traits_from_collection.js'></script>"
  $("#collections_holder").html(the_html);
  return $('#collections_holder').colorbox.resize();
};

back_to_collections = function() {
  $("#collections_holder").html(collection_list);
  $(document).on("click", ".collection_line", collection_click);
  return $('#collections_holder').colorbox.resize();
};

$(".collection_line").on("click", collection_click);
$("#submit_cofactors").on("click", submit_click);
if ($('#scatterplot2').length){
  $(".trait_line").on("click", trait_row_click);
} else {
  $(".trait").on("click", trait_click);
}
$("#back_to_collections").on("click", back_to_collections);