From bd7bdf71037aa006c365ed804f578a6ed9a872ba Mon Sep 17 00:00:00 2001 From: sepro Date: Thu, 26 Oct 2017 17:09:07 +0200 Subject: [PATCH] added count for enriched clusters alternative to precog as this query was too slow. --- planet/controllers/search.py | 37 ++++++++++++ planet/models/search.py | 29 ++++++++++ planet/templates/find_enriched_clusters.html | 60 +++++++++++++++++--- 3 files changed, 117 insertions(+), 9 deletions(-) diff --git a/planet/controllers/search.py b/planet/controllers/search.py index d4e73f3..5756c27 100644 --- a/planet/controllers/search.py +++ b/planet/controllers/search.py @@ -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(): """ diff --git a/planet/models/search.py b/planet/models/search.py index 020c66d..8efa8e5 100644 --- a/planet/models/search.py +++ b/planet/models/search.py @@ -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): """ diff --git a/planet/templates/find_enriched_clusters.html b/planet/templates/find_enriched_clusters.html index f4b6162..f63dd24 100644 --- a/planet/templates/find_enriched_clusters.html +++ b/planet/templates/find_enriched_clusters.html @@ -77,26 +77,27 @@

Enriched Clusters

{{ form.csrf_token }} - {{form.go_term(class_="form-control typeahead") }}
+ {{form.go_term(class_="form-control typeahead update_count") }}
- {{form.method(class_="form-control") }}
+ {{form.method(class_="form-control update_count") }}
-
- {{form.min_enrichment(class_="form-control") }}
+
+ {{form.min_enrichment(class_="form-control update_count") }}
-
- {{form.max_p(class_="form-control") }}
+
+ {{form.max_p(class_="form-control update_count") }}
-
- {{form.max_corrected_p(class_="form-control") }}
+
+ {{form.max_corrected_p(class_="form-control update_count") }}
{% if example and example.go_term %} {% endif %} - + +
@@ -111,6 +112,8 @@

Enriched Clusters

  • Select the clusters to search
  • Set the parameters. Checkboxes enable/disable certain filters. (Recommended: enable corrected p-value and keep this below 0.05)
  • Click Find clusters
  • +
    +
  • Using the Count button you can check how many clusters meet the current criteria.
  • @@ -123,6 +126,45 @@

    Enriched Clusters

    {% block extrajs %} +{% if form %} + + + +{% endif %}