Skip to content

Commit

Permalink
added count for enriched clusters alternative to precog as this query…
Browse files Browse the repository at this point in the history
… was too slow.
  • Loading branch information
proost committed Oct 26, 2017
1 parent d79f079 commit bd7bdf7
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 9 deletions.
37 changes: 37 additions & 0 deletions planet/controllers/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,43 @@ def search_json_genes(label=''):
return Response(json.dumps(list(set(output))), mimetype='application/json')


@search.route('/enriched/count', methods=['POST'])
def count_enriched_clusters():
"""
Counts the number of clusters enriched for a set of criteria
:return: json response with the count
"""

content = request.get_json(silent=True)

try:
term = content["go_term"]
method = int(content["method"])

check_enrichment = bool(content["check_enrichment"])
check_p = bool(content["check_p"])
check_corrected_p = bool(content["check_corrected_p"])

min_enrichment = float(content["min_enrichment"]) if check_enrichment else None
max_p = float(content["max_p"]) if check_p else None
max_corrected_p = float(content["max_corrected_p"]) if check_corrected_p else None

go = GO.query.filter(or_(GO.name == term,
GO.label == term)).first()

cluster_count = Search.count_enriched_clusters(go.id,
method=method,
min_enrichment=min_enrichment,
max_p=max_p,
max_corrected_p=max_corrected_p)

return Response(json.dumps({'count': cluster_count, 'error': 0}), mimetype='application/json')
except Exception as e:
# Bad data return zero
return Response(json.dumps({'count': 0, 'error': 1}), mimetype='application/json')


@search.route('/enriched/clusters', methods=['GET', 'POST'])
def search_enriched_clusters():
"""
Expand Down
29 changes: 29 additions & 0 deletions planet/models/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,35 @@ def advanced_sequence_search(species_id, gene_list, terms, term_rules, gene_fami

return sequences

@staticmethod
def count_enriched_clusters(go_id, method=-1, min_enrichment=None, max_p=None, max_corrected_p=None):
"""
Function to get enriched clusters for a specific GO term
:param go_id: Internal GO id
:param method: Internal ID of the clustering method (ignored if -1)
:param min_enrichment: minimal (log2) enrichment score (ignored if None)
:param max_p: maximum (uncorrected) p-value (ignored if None)
:param max_corrected_p: maximum corrected p-value (ignored if None)
:return: all clusters with the desired properties (ignored if None)
"""
clusters = ClusterGOEnrichment.query.filter(ClusterGOEnrichment.go_id == go_id)\

if method != -1:
clusters = clusters.filter(ClusterGOEnrichment.cluster.has(method_id=method))

if min_enrichment is not None:
clusters = clusters.filter(ClusterGOEnrichment.enrichment >= min_enrichment)

if max_p is not None:
clusters = clusters.filter(ClusterGOEnrichment.p_value <= max_p)

if max_corrected_p is not None:
clusters = clusters.filter(ClusterGOEnrichment.corrected_p_value <= max_corrected_p)

return clusters.count()

@staticmethod
def enriched_clusters(go_id, method=-1, min_enrichment=None, max_p=None, max_corrected_p=None):
"""
Expand Down
60 changes: 51 additions & 9 deletions planet/templates/find_enriched_clusters.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,27 @@ <h1>Enriched Clusters</h1>
<form method="POST" action="{{ url_for('search.search_enriched_clusters') }}" role="form">
{{ form.csrf_token }}
<label><abbr title="Click to show help" href="{{ url_for('help.help_topic', topic='go') }}" data-target="#helpModal">GO</abbr></label>
{{form.go_term(class_="form-control typeahead") }}<br />
{{form.go_term(class_="form-control typeahead update_count") }}<br />
<label>Cluster Method</label>
{{form.method(class_="form-control") }}<br />
{{form.method(class_="form-control update_count") }}<br />
<div class="checkbox-group">
<label>{{form.check_enrichment(checked=False) }} <span class="title">Enrichment</span> </label><br />
{{form.min_enrichment(class_="form-control") }}<br />
<label>{{form.check_enrichment(checked=False, class_="update_count") }} <span class="title">Enrichment</span> </label><br />
{{form.min_enrichment(class_="form-control update_count") }}<br />
</div>
<div class="checkbox-group">
<label>{{form.check_p(checked=False) }} <span class="title">p-value</span></label><br />
{{form.max_p(class_="form-control") }}<br />
<label>{{form.check_p(checked=False, class_="update_count") }} <span class="title">p-value</span></label><br />
{{form.max_p(class_="form-control update_count") }}<br />
</div>
<div class="checkbox-group">
<label>{{form.check_corrected_p(checked=True) }} <span class="title"><abbr title="Click to show help" href="{{ url_for('help.help_topic', topic='fdr') }}" data-target="#helpModal">corrected p-value</abbr></span></label><br />
{{form.max_corrected_p(class_="form-control") }}<br />
<label>{{form.check_corrected_p(checked=True, class_="update_count") }} <span class="title"><abbr title="Click to show help" href="{{ url_for('help.help_topic', topic='fdr') }}" data-target="#helpModal">corrected p-value</abbr></span></label><br />
{{form.max_corrected_p(class_="form-control update_count") }}<br />
</div>
<div class="pull-right">
{% if example and example.go_term %}
<button class="btn btn-default" id="load_example">Load example</button>
{% endif %}
<button type="submit" class="btn btn-primary" data-toggle="modal" data-target="#loaderModal">Find clusters</button>
<button class="btn btn-default" id="count_button">Count</button>
<button type="submit" class="btn btn-primary" data-toggle="modal" data-target="#loaderModal" >Show clusters</button>
</div>
</form>
</div>
Expand All @@ -111,6 +112,8 @@ <h1>Enriched Clusters</h1>
<li>Select the <strong>clusters</strong> to search</li>
<li>Set the parameters. Checkboxes enable/disable certain filters. (Recommended: enable corrected p-value and keep this below 0.05)</li>
<li>Click <strong>Find clusters</strong></li>
<br />
<li>Using the <strong>Count</strong> button you can check how many clusters meet the current criteria.</li>
</ul>
</div>
</div>
Expand All @@ -123,6 +126,45 @@ <h1>Enriched Clusters</h1>
{% block extrajs %}
<script src="{{ url_for('static', filename='js/typeahead.bundle.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/table2CSV.js') }}" > </script>
{% if form %}
<script>
$(function() {
function update_count() {
post_data = {
'method': $('#method').val(),
'go_term': $('#go_term').val(),
'condition': $('#conditions').val(),

'check_enrichment': $('#check_enrichment').is(":checked"),
'check_p': $('#check_p').is(":checked"),
'check_corrected_p': $('#check_corrected_p').is(":checked"),

'min_enrichment': $('#min_enrichment').val(),
'max_p': $('#max_p').val(),
'max_corrected_p': $('#max_corrected_p').val()
}
console.log(post_data);
$.ajax({
type: "POST",
url: "{{ url_for('search.count_enriched_clusters') }}",
contentType: 'application/json;charset=UTF-8',
data : JSON.stringify(post_data, null, '\t'),
success: function(data) {
$('#count_button').text('Count (' + data.count + ')');
}
});
}

$("#count_button").click(function( ev) {
ev.preventDefault();
update_count();
});

});
</script>


{% endif %}
<script>
$(function () {
$('[data-toggle="tooltip"]').tooltip();
Expand Down

0 comments on commit bd7bdf7

Please sign in to comment.