Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

G3-521 gene counts for setting threshold #31

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "geneweaver-legacy"
version = "1.5.3"
version = "1.5.4"
description = ""
authors = ["Alexander Berger <[email protected]>"]
readme = "README.md"
Expand Down
41 changes: 36 additions & 5 deletions src/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -1972,6 +1972,7 @@ def render_set_threshold(gs_id):
threshold_type = geneset.threshold_type
threshold = str(geneset.threshold)
thresh = threshold.split(',')
threshold_gene_counts = None

minVal = None
maxVal = None
Expand All @@ -1986,9 +1987,6 @@ def render_set_threshold(gs_id):
if gsv_values is not None:
for k in gsv_values:
k_first_value = list(k.values())[0]
# maxVal = maxVal if maxVal is not None and float(k_first_value) < maxVal else float(k_first_value)
# minVal = minVal if minVal is not None and float(k_first_value) > minVal else float(k_first_value)

if maxVal is not None:
maxVal = float(k_first_value) if float(k_first_value) > maxVal else maxVal
else:
Expand All @@ -1999,8 +1997,15 @@ def render_set_threshold(gs_id):
else:
minVal = float(k_first_value)

# minVal = float(k_first_value) if float(k_first_value) < minVal else minVal
g_values.append(float(k_first_value))


# if Threshold type is 1 or 2 (p-value or q-value) then calculate gene counts for each threshold
curr_gene_count = 0;
if threshold_type == 1 or threshold_type == 2:
threshold_gene_counts = calc_genes_count_in_threshold(gsv_values, thresh)
curr_gene_count = curr_gene_count if thresh[0] == 'None' else threshold_gene_counts[float(thresh[0])]

score_types = {
1: "p-value",
2: "q-value",
Expand All @@ -2013,7 +2018,33 @@ def render_set_threshold(gs_id):
user_id=user_id, view=view, is_bimodal=is_bimodal,
threshold=thresh, threshold_type=threshold_type,
minVal=minVal, maxVal=maxVal,
scoreType=score_types[threshold_type], gsv_values=g_values)
scoreType=score_types[threshold_type], gsv_values=g_values,
threshold_gene_counts=threshold_gene_counts, curr_gene_count=curr_gene_count)

def calc_genes_count_in_threshold(gsv_values, curr_thresh) -> dict:
""" Calculate the number of genes that fall below a given threshold
for score types 1 and 2 (p-value and q-value)

:param gsv_values: list of gene set values
:return: dictionary of gene counts for each threshold
"""
threshold_gene_counts = {}
thresholds = [0.05, 0.01, 0.001, 0.0001]

# Add the current threshold to the list
if curr_thresh[0] != 'None' and float(curr_thresh[0]) not in thresholds:
thresholds.append(float(curr_thresh[0]))

for threshold in thresholds:
threshold_gene_counts[threshold] = 0
if gsv_values is not None:
for gsv_value in gsv_values:
value = list(gsv_value.values())[0]
if float(value) < threshold:
threshold_gene_counts[threshold] += 1

return threshold_gene_counts



@app.route('/setthreshold-legacy/<int:gs_id>')
Expand Down
93 changes: 65 additions & 28 deletions src/templates/viewThreshold_new.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,37 @@ <h2 class="panel-title"><strong>Set GeneSet Threshold</strong></h2>
<div class="panel-body">
{% set threshold_symbol = 'p' if threshold_type == 1 else 'q' %}
{% if is_bimodal == 'False' %}
<div class="row">
{% if threshold_type == 4 or threshold_type == 5 %}
{% set threshold_low = threshold[0] if threshold[0] != 'None' else minVal %}
{% set threshold_high = threshold[1] if threshold[1] != '0' else maxVal %}
{% set threshold_name = 'a correlation' if threshold_type == 4 else 'an effect' %}
<div><p><strong>Current Threshold Values:
</strong> {{ threshold[0] }} <i class="fa fa-arrows-h"></i>
{% if threshold[1] == '0' %}
None
{% else %}
{{ threshold[1] }}
{% endif %}
</p>
{% if gsv_values|length == 0 %}
<div class="row">
<div class="alert bg-gray-light">
This GeneSet does not contain any geneset values.
Set threshold cannot be performed.
</div>
</div>
{% else %}
<div><p><strong>Current Threshold Values:
</strong> {{ threshold_symbol }} < {{ threshold[0] }}
</p>
<div class="row">
{% if threshold_type == 4 or threshold_type == 5 %}
{% set threshold_low = threshold[0] if threshold[0] != 'None' else minVal %}
{% set threshold_high = threshold[1] if threshold[1] != '0' else maxVal %}
{% set threshold_name = 'a correlation' if threshold_type == 4 else 'an effect' %}
<div><p><strong>Current Threshold Values:
</strong> {{ threshold[0] }} <i class="fa fa-arrows-h"></i>
{% if threshold[1] == '0' %}
None
{% else %}
{{ threshold[1] }}
{% endif %}
</p>
</div>
{% else %}
<div><p><strong>Current Threshold Values:
</strong> {{ threshold_symbol }} < {{ threshold[0] }} ( genes count: {{ curr_gene_count }} )
</p>
</div>
{% endif %}

</div>
{% endif %}
</div>
{% else %}
<div class="row">
<div class="alert bg-gray-light">
Expand All @@ -53,7 +63,7 @@ <h2 class="panel-title"><strong>Set GeneSet Threshold</strong></h2>
<hr>
<div class="row">
<div class="col-md-9" style="margin-bottom: 1rem;">
{% if view is defined and is_bimodal == 'False' %}
{% if view is defined and is_bimodal == 'False' and gsv_values|length > 0%}
{% if threshold_type == 4 or threshold_type == 5 %}
<div class="row">
<div class="input-group">
Expand All @@ -68,24 +78,37 @@ <h2 class="panel-title"><strong>Set GeneSet Threshold</strong></h2>
</div>
<div class="row">
<div class="col-md-12" style="margin-bottom: 1rem;">
<div id="myHisPlot" style="width:100%; height:500px;"></div>
<p>** Use Box Select tool to set a threshold range on the histogram, or enter values in the input fields. </p>
<p>* Selected threshold range in red color</p>
<div class="row">
<div class="col-md-12" style="margin-top: 2rem; margin-bottom: 1rem;">
<p id="selectedGeneCount"></p>
</div>
</div>
<div class="row">
<div class="col-md-12" style="margin-bottom: 1rem;">
<div id="myHisPlot" style="width:100%; height:500px;"></div>
</div>
</div>
<div class="row">
<div class="col-md-12" style="margin-bottom: 1rem;">
<p>** Use Box Select tool to set a threshold range on the histogram, or enter values in the input fields. </p>
<p>* Selected threshold range in red color</p>
</div>
</div>
</div>
</div>
{% else %}
<div class="row">
<button type="button" class="btn btn-block btn-default setThresh" id="setThresh05" value="0.05">
Save as <code> {{ threshold_symbol }} < 0.05 </code>
Save as <code> {{ threshold_symbol }} < 0.05 </code> ( genes count: {{ threshold_gene_counts[0.05]}} )
</button>
<button type="button" class="btn btn-block btn-default setThresh" id="setThresh01" value="0.01">
Save as <code> {{ threshold_symbol }} < 0.01 </code>
Save as <code> {{ threshold_symbol }} < 0.01 </code> ( genes count: {{ threshold_gene_counts[0.01]}} )
</button>
<button type="button" class="btn btn-block btn-default setThresh" id="setThresh001" value="0.001">
Save as <code> {{ threshold_symbol }} < 0.001 </code>
Save as <code> {{ threshold_symbol }} < 0.001 </code> ( genes count:{{ threshold_gene_counts[0.001]}} )
</button>
<button type="button" class="btn btn-block btn-default setThresh" id="setThresh0001" value="0.0001">
Save as <code> {{ threshold_symbol }} < 0.0001 </code>
Save as <code> {{ threshold_symbol }} < 0.0001 </code> ( genes count: {{ threshold_gene_counts[0.0001]}} )
</button>

<hr>
Expand Down Expand Up @@ -200,6 +223,13 @@ <h2 class="panel-title"><strong>Set GeneSet Threshold</strong></h2>
}
}
});
//update gene count display
displaySelectedGeneCount(indices.length);
}

//Display selected gene count
function displaySelectedGeneCount(count) {
document.getElementById('selectedGeneCount').innerHTML = '<b>Number of Genes in Selected Threshold:</b> ' + count;
}

// Set properties for Plotly histogram
Expand Down Expand Up @@ -256,6 +286,12 @@ <h2 class="panel-title"><strong>Set GeneSet Threshold</strong></h2>

// Callback function for selected points using Box Select tool
myPlot.on('plotly_selected', function(data) {
var geneCount = 0;
for (var i = 0; i < data.points.length; i++) {
geneCount = data.points[i].pointIndices.length + geneCount;
}
//update gene count display
displaySelectedGeneCount(geneCount);

if (data !== undefined) {
threshold_min.value = data.range.x[0];
Expand Down Expand Up @@ -286,7 +322,10 @@ <h2 class="panel-title"><strong>Set GeneSet Threshold</strong></h2>
}
setSelectPoints(myPlot, selected_x)
} else {
//Do nothing -- index for values will not be found in histogram data
//Index for values will not be found in histogram data
//update histogram selection and gene count display to 0
setSelectPoints(myPlot, [])
displaySelectedGeneCount(0);
console.log('Threshold values not found in histogram', 'index_min:', index_min, 'index_max:', index_max);
}
}
Expand All @@ -308,8 +347,6 @@ <h2 class="panel-title"><strong>Set GeneSet Threshold</strong></h2>

{% endif %}



</div>

{% endif %}
Expand Down
Loading