Skip to content

Commit 40d2fca

Browse files
committed
Checking if gene exist for tsne-plot
1 parent 85c288d commit 40d2fca

File tree

6 files changed

+62
-21
lines changed

6 files changed

+62
-21
lines changed

Diff for: NetExplorer/models.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,7 @@ def __init__(self):
15651565
self.title = str()
15661566
self.ylab = str()
15671567
self.trace_names = list()
1568+
self.units = dict()
15681569

15691570
def add_group(self, group):
15701571
if group not in self.groups_set:
@@ -1609,7 +1610,24 @@ def is_empty(self):
16091610
empty = False
16101611
break
16111612
return empty
1612-
1613+
1614+
def add_units(self, axis, units):
1615+
'''
1616+
Adds units to one axis of the plot.
1617+
1618+
Args:
1619+
axis: string cointaining 'x' or 'y'.
1620+
units: string for units.
1621+
1622+
Returns:
1623+
nothing
1624+
'''
1625+
if axis == 'x' or axis == 'y':
1626+
self.units[axis] = units
1627+
else:
1628+
raise ValueError("Axis should be a string containing 'x' or 'y'.")
1629+
1630+
16131631

16141632

16151633
# ------------------------------------------------------------------------------
@@ -1647,8 +1665,15 @@ def plot(self):
16471665
if self.ylab:
16481666
theplot['layout']['yaxis'] = dict()
16491667
theplot['layout']['yaxis']['title'] = self.ylab
1668+
1669+
if self.units:
1670+
for axis, units in self.units.items():
1671+
axis_name = axis + 'axis'
1672+
theplot['layout'][axis_name] = dict()
1673+
theplot['layout'][axis_name]['title'] = units
16501674
return theplot
1651-
1675+
1676+
16521677

16531678
# ------------------------------------------------------------------------------
16541679
class ViolinPlot(GenExpPlot):
@@ -1711,6 +1736,11 @@ def plot(self):
17111736
if self.ylab:
17121737
theplot['layout']['yaxis'] = dict()
17131738
theplot['layout']['yaxis']['title'] = self.ylab
1739+
if self.units:
1740+
for axis, units in self.units.items():
1741+
axis_name = axis + 'axis'
1742+
theplot['layout'][axis_name] = dict()
1743+
theplot['layout'][axis_name]['title'] = units
17141744
return theplot
17151745

17161746

Diff for: NetExplorer/static/css/style.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,7 @@ kbd {
20312031
background-color: #FDD4D4;
20322032
}
20332033

2034-
#tsneplot-genenotfound {
2034+
#tsne-plot-genenotfound {
20352035
display: none;
20362036
margin: auto;
20372037
font-size: 18px;

Diff for: NetExplorer/static/js/planexp.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ var PlanExp = (function() {
281281
console.log(data.data);
282282
Plotly.newPlot(plotDivId, data.data, data.layout);
283283
} else {
284-
$("#plot-genenotfound").show(250);
284+
$("#tsne-plot-genenotfound").show(250);
285285
}
286286
$("#tsne-plot-loading").hide();
287287
}
@@ -419,12 +419,12 @@ var PlanExp = (function() {
419419
withcolor = true;
420420
ctype = "Cluster";
421421
if (!geneName) {
422-
$("#tsneplot-genenotfound").show(250);
422+
$("#tsne-plot-genenotfound").show(250);
423423
return;
424424
}
425425
}
426426

427-
$("#tsneplot-genenotfound").hide();
427+
$("#tsne-plot-genenotfound").hide();
428428
plotTSNE(expName, dataset, geneName, withcolor, ctype, "tsne-plot");
429429
});
430430

Diff for: NetExplorer/templates/NetExplorer/planexp.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ <h4>Color By:</h4>
173173

174174
<br>
175175
<div id="plot-container">
176-
<div id="tsneplot-genenotfound">
176+
<div id="tsne-plot-genenotfound">
177177
<span class="glyphicon glyphicon-warning-sign"></span>
178178
Gene/Symbol not found
179179
</div>

Diff for: NetExplorer/views/plot_gene_expression.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def do_barplot(experiment, dataset, conditions, gene_symbols):
66
Useful when we have One Sample per Condition per Gene (one value to plot).
77
"""
88
theplot = None
9+
units = None
910
for g_idx, gene_symbol in enumerate(gene_symbols):
1011
if theplot is None:
1112
theplot = BarPlot()
@@ -20,21 +21,25 @@ def do_barplot(experiment, dataset, conditions, gene_symbols):
2021
experiment=experiment, dataset=dataset,
2122
sample__in=samples, gene_symbol=gene_symbol)
2223
if expression:
24+
if units is None:
25+
units = expression[0].units
2326
expression = expression[0].expression_value
2427
else:
2528
expression = 0
2629
theplot.add_group(condition.name)
2730
theplot.add_value(expression, condition.name, g_idx)
31+
32+
theplot.add_units("y", units)
2833
return theplot
2934

3035

31-
import time
3236
def do_violin(experiment, dataset, conditions, gene_symbols, ctype):
3337
'''
3438
THE CHECK
3539
dd_Smed_v6_7_0_1,dd_Smed_v6_702_0_1,dd_Smed_v6_659_0_1,dd_Smed_v6_920_0_1
3640
'''
3741
theplot = None
42+
units = None
3843
for g_idx, gene_symbol in enumerate(gene_symbols):
3944
if theplot is None:
4045
theplot = ViolinPlot()
@@ -55,15 +60,17 @@ def do_violin(experiment, dataset, conditions, gene_symbols, ctype):
5560
sample__in=samples, gene_symbol=gene_symbol)
5661
theplot.add_group(condname)
5762

58-
start = time.time()
5963
if expression:
64+
if units is None:
65+
units = expression[0].units
6066
for exp in expression:
6167
if exp.expression_value:
6268
theplot.add_value(exp.expression_value, condname, g_idx)
6369
else:
6470
theplot.add_value(0, condname, g_idx)
6571
else:
6672
expression = 0
73+
theplot.add_units('y', units)
6774
return theplot
6875

6976

Diff for: NetExplorer/views/plot_tsne.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@ def do_tsne(experiment, dataset, conditions, gene_symbol, ctype, with_color):
3030
experiment=experiment, dataset=dataset,
3131
sample__in=samples, gene_symbol=gene_symbol
3232
)
33-
# We need a dictionary with {cell_id : cell_expression}
34-
# so we can keep the same order as cell positions (in cell_order)
35-
thedict = dict()
36-
for cellexp in cell_expression:
37-
thedict[cellexp.sample.id] = cellexp.expression_value
38-
cell_expression = [ thedict[cell_idx] for cell_idx in cell_order ]
39-
theplot.add_color_to_trace(trace_name, cell_expression)
33+
if cell_expression:
34+
# We need a dictionary with {cell_id : cell_expression}
35+
# so we can keep the same order as cell positions (in cell_order)
36+
thedict = dict()
37+
for cellexp in cell_expression:
38+
thedict[cellexp.sample.id] = cellexp.expression_value
39+
cell_expression = [ thedict[cell_idx] for cell_idx in cell_order ]
40+
theplot.add_color_to_trace(trace_name, cell_expression)
41+
else:
42+
theplot = None
43+
break
4044
return theplot
4145

4246

@@ -66,9 +70,9 @@ def plot_tsne(request):
6670

6771
# Do the plot
6872
theplot = do_tsne(experiment, dataset, conditions, gene_symbol, ctype, with_color)
69-
response = theplot.plot()
70-
try:
71-
json.dumps(response)
72-
except Exception as err:
73-
print(err)
73+
if theplot is not None:
74+
response = theplot.plot()
75+
else:
76+
response = None
77+
7478
return HttpResponse(json.dumps(response), content_type="application/json")

0 commit comments

Comments
 (0)