aboutsummaryrefslogtreecommitdiff
path: root/gn3/api/correlation.py
blob: 53ea6a703e4c8eb0731f84d1d6194cc81d27fb58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Endpoints for running correlations"""
from unittest import mock

from flask import jsonify
from flask import Blueprint
from flask import request

from gn3.computations.correlations import compute_all_sample_correlation
from gn3.computations.correlations import compute_all_lit_correlation
from gn3.computations.correlations import compute_all_tissue_correlation


correlation = Blueprint("correlation", __name__)


@correlation.route("/sample_r/<string:corr_method>", methods=["POST"])
def compute_sample_r(corr_method="pearson"):
    """correlation endpoint for computing sample r correlations\
    api expects the trait data with has the trait and also the\
    target_dataset  data"""
    correlation_input = request.get_json()

    # xtodo move code below to compute_all_sampl correlation
    this_trait_data = correlation_input.get("this_trait")
    target_datasets = correlation_input.get("target_dataset")

    correlation_results = compute_all_sample_correlation(corr_method=corr_method,
                                                         this_trait=this_trait_data,
                                                         target_dataset=target_datasets)

    return jsonify({
        "corr_results": correlation_results
    })


@correlation.route("/lit_corr/<string:species>/<int:gene_id>", methods=["POST"])
def compute_lit_corr(species=None, gene_id=None):
    """api endpoint for doing lit correlation.results for lit correlation\
    are fetched from the database this is the only case where the db\
    might be needed for actual computing of the correlation results"""

    database_instance = mock.Mock()
    target_traits_gene_ids = request.get_json()

    lit_corr_results = compute_all_lit_correlation(
        database_instance=database_instance, trait_lists=target_traits_gene_ids,
        species=species, gene_id=gene_id)

    return jsonify(lit_corr_results)


@correlation.route("/tissue_corr/<string:corr_method>", methods=["POST"])
def compute_tissue_corr(corr_method="pearson"):
    """api endpoint fr doing tissue correlation"""
    tissue_input_data = request.get_json()
    primary_tissue_dict = tissue_input_data["primary_tissue"]
    target_tissues_dict_list = tissue_input_data["target_tissues"]

    results = compute_all_tissue_correlation(primary_tissue_dict=primary_tissue_dict,
                                             target_tissues_dict_list=target_tissues_dict_list,
                                             corr_method=corr_method)

    return jsonify(results)