From 6fc0b92ad14bfbc8c752b7730f70c6b22294f667 Mon Sep 17 00:00:00 2001
From: Zachary Sloan
Date: Fri, 25 Oct 2013 18:16:54 -0500
Subject: Began working on color coding bar chart by extra attributes

---
 wqflask/base/data_set.py                           |  1 +
 wqflask/base/trait.py                              |  6 +++--
 wqflask/wqflask/show_trait/SampleList.py           |  2 +-
 .../static/new/javascript/show_trait.coffee        | 23 ++++++++++++++----
 .../wqflask/static/new/javascript/show_trait.js    | 27 ++++++++++++++++++----
 .../templates/show_trait_statistics_new.html       | 10 ++++++++
 6 files changed, 57 insertions(+), 12 deletions(-)

(limited to 'wqflask')

diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py
index f25e7974..cd8c1ac1 100755
--- a/wqflask/base/data_set.py
+++ b/wqflask/base/data_set.py
@@ -224,6 +224,7 @@ class DatasetGroup(object):
     """
     def __init__(self, dataset):
         """This sets self.group and self.group_id"""
+        print("dataset name:", dataset.name)
         self.name, self.id = g.db.execute(dataset.query_for_group).fetchone()
         if self.name == 'BXD300':
             self.name = "BXD"
diff --git a/wqflask/base/trait.py b/wqflask/base/trait.py
index 6a64eeaf..77e451a1 100755
--- a/wqflask/base/trait.py
+++ b/wqflask/base/trait.py
@@ -382,6 +382,7 @@ class GeneralTrait(object):
                     #trait_qtl = self.cursor.fetchone()
                     if trait_qtl:
                         self.locus, self.lrs, self.pvalue, self.mean = trait_qtl
+                        print("self.locus:", self.locus)
                         if self.locus:
                             query = """
                                 select Geno.Chr, Geno.Mb from Geno, Species
@@ -390,8 +391,9 @@ class GeneralTrait(object):
                                 Geno.SpeciesId = Species.Id
                                 """.format(self.dataset.group.species, self.locus)
                             result = g.db.execute(query).fetchone()
-                            self.locus_chr = result[0]
-                            self.locus_mb = result[1]
+                            if result:
+                                self.locus_chr = result[0]
+                                self.locus_mb = result[1]
                     else:
                         self.locus = self.locus_chr = self.locus_mb = self.lrs = self.pvalue = self.mean = ""
                 
diff --git a/wqflask/wqflask/show_trait/SampleList.py b/wqflask/wqflask/show_trait/SampleList.py
index 1130fb60..9cd7d895 100644
--- a/wqflask/wqflask/show_trait/SampleList.py
+++ b/wqflask/wqflask/show_trait/SampleList.py
@@ -138,7 +138,7 @@ class SampleList(object):
                                                 StrainId = %s AND
                                                 CaseAttributeId = %s
                                         group by CaseAttributeXRef.CaseAttributeId""", (
-                                            self.this_trait.db.id, sample_id, str(attribute)))
+                                            self.this_trait.dataset.id, sample_id, str(attribute)))
 
                 attribute_value = result.fetchone().Value #Trait-specific attributes, if any
 
diff --git a/wqflask/wqflask/static/new/javascript/show_trait.coffee b/wqflask/wqflask/static/new/javascript/show_trait.coffee
index 2e049e6a..bf01ee5d 100644
--- a/wqflask/wqflask/static/new/javascript/show_trait.coffee
+++ b/wqflask/wqflask/static/new/javascript/show_trait.coffee
@@ -83,6 +83,10 @@ $ ->
             @create_scales()
             @create_graph()
             
+            d3.select("#color_attribute").on("change", =>
+                
+            )
+            
             d3.select("#update_bar_chart").on("click", =>
                 if $("#update_bar_chart").html() == 'Sort By Value' 
                     $("#update_bar_chart").html('Sort By Name')
@@ -99,10 +103,14 @@ $ ->
                         .attr("height", (d) =>
                             return @plot_height - @y_scale(d[1])
                         )
+                        .select("title")
+                        .text((d) =>
+                            return d[1]
+                        )
                     sorted_sample_names = (sample[0] for sample in @sorted_samples())
                     x_scale = d3.scale.ordinal()
                         .domain(sorted_sample_names)
-                        .rangeRoundBands([0, @plot_width], .1)
+                        .rangeBands([0, @plot_width], .1)
                     $('.x.axis').remove()
                     @add_x_axis(x_scale)
                 else
@@ -117,17 +125,24 @@ $ ->
                         .attr("height", (d) =>
                             return @plot_height - @y_scale(d)
                         )
+                        .select("title")
+                        .text((d) =>
+                            return d
+                        )
                     x_scale = d3.scale.ordinal()
                         .domain(@sample_names)
-                        .rangeRoundBands([0, @plot_width], .1)
+                        .rangeBands([0, @plot_width], .1)
                     $('.x.axis').remove()
                     @add_x_axis(x_scale)
-                    
             )
 
         get_samples: () ->
             @sample_names = (sample.name for sample in @sample_list when sample.value != null)
             @sample_vals = (sample.value for sample in @sample_list when sample.value != null)
+            attributes = (key for key of @sample_list[0]["extra_attributes"])
+            console.log("attributes:", attributes)
+            if attributes.length > 0
+                alert("TEST")
             
         create_svg: () ->
             svg = d3.select("#bar_chart")
@@ -143,7 +158,7 @@ $ ->
         create_scales: () ->
             @x_scale = d3.scale.ordinal()
                 .domain(@sample_names)
-                .rangeRoundBands([0, @plot_width], .1)
+                .rangeBands([0, @plot_width], .1)
 
             @y_scale = d3.scale.linear()
                 .domain([@y_min * 0.75, @y_max])
diff --git a/wqflask/wqflask/static/new/javascript/show_trait.js b/wqflask/wqflask/static/new/javascript/show_trait.js
index e6fcbd7b..7b5e4b88 100644
--- a/wqflask/wqflask/static/new/javascript/show_trait.js
+++ b/wqflask/wqflask/static/new/javascript/show_trait.js
@@ -92,6 +92,7 @@
         this.plot_height -= this.y_buffer;
         this.create_scales();
         this.create_graph();
+        d3.select("#color_attribute").on("change", function() {});
         d3.select("#update_bar_chart").on("click", function() {
           var sortItems, sorted_sample_names, x_scale;
           if ($("#update_bar_chart").html() === 'Sort By Value') {
@@ -103,6 +104,8 @@
               return _this.y_scale(d[1]);
             }).attr("height", function(d) {
               return _this.plot_height - _this.y_scale(d[1]);
+            }).select("title").text(function(d) {
+              return d[1];
             });
             sorted_sample_names = (function() {
               var _i, _len, _ref, _results;
@@ -114,7 +117,7 @@
               }
               return _results;
             }).call(_this);
-            x_scale = d3.scale.ordinal().domain(sorted_sample_names).rangeRoundBands([0, _this.plot_width], .1);
+            x_scale = d3.scale.ordinal().domain(sorted_sample_names).rangeBands([0, _this.plot_width], .1);
             $('.x.axis').remove();
             return _this.add_x_axis(x_scale);
           } else {
@@ -123,8 +126,10 @@
               return _this.y_scale(d);
             }).attr("height", function(d) {
               return _this.plot_height - _this.y_scale(d);
+            }).select("title").text(function(d) {
+              return d;
             });
-            x_scale = d3.scale.ordinal().domain(_this.sample_names).rangeRoundBands([0, _this.plot_width], .1);
+            x_scale = d3.scale.ordinal().domain(_this.sample_names).rangeBands([0, _this.plot_width], .1);
             $('.x.axis').remove();
             return _this.add_x_axis(x_scale);
           }
@@ -132,7 +137,7 @@
       }
 
       Histogram.prototype.get_samples = function() {
-        var sample;
+        var attributes, key, sample;
         this.sample_names = (function() {
           var _i, _len, _ref, _results;
           _ref = this.sample_list;
@@ -145,7 +150,7 @@
           }
           return _results;
         }).call(this);
-        return this.sample_vals = (function() {
+        this.sample_vals = (function() {
           var _i, _len, _ref, _results;
           _ref = this.sample_list;
           _results = [];
@@ -157,6 +162,18 @@
           }
           return _results;
         }).call(this);
+        attributes = (function() {
+          var _results;
+          _results = [];
+          for (key in this.sample_list[0]["extra_attributes"]) {
+            _results.push(key);
+          }
+          return _results;
+        }).call(this);
+        console.log("attributes:", attributes);
+        if (attributes.length > 0) {
+          return alert("TEST");
+        }
       };
 
       Histogram.prototype.create_svg = function() {
@@ -166,7 +183,7 @@
       };
 
       Histogram.prototype.create_scales = function() {
-        this.x_scale = d3.scale.ordinal().domain(this.sample_names).rangeRoundBands([0, this.plot_width], .1);
+        this.x_scale = d3.scale.ordinal().domain(this.sample_names).rangeBands([0, this.plot_width], .1);
         return this.y_scale = d3.scale.linear().domain([this.y_min * 0.75, this.y_max]).range([this.plot_height, this.y_buffer]);
       };
 
diff --git a/wqflask/wqflask/templates/show_trait_statistics_new.html b/wqflask/wqflask/templates/show_trait_statistics_new.html
index acd6cc62..58389ce4 100644
--- a/wqflask/wqflask/templates/show_trait_statistics_new.html
+++ b/wqflask/wqflask/templates/show_trait_statistics_new.html
@@ -7,6 +7,16 @@
                 <option value="{{ group }}">{{ pretty_group }}</option>
             {% endfor %}
         </select>
+        {% if sample_groups[0].attributes %}
+        <div class="input-append">
+            <select id="color_attribute" size=1>
+              {% for attribute in sample_groups[0].attributes %}
+              <option value="{{ sample_groups[0].attributes[attribute].name.replace(' ', '_') }}">
+                  {{ sample_groups[0].attributes[attribute].name }}</option>
+              {% endfor %}
+            </select>
+        </div>
+        {% endif %}
         <button type="button" id="update_bar_chart">Sort By Value</button>
         <div id="bar_chart_container">
             <div id="bar_chart"></div>
-- 
cgit v1.2.3