aboutsummaryrefslogtreecommitdiff
path: root/gn2/wqflask/external_tools/send_to_webgestalt.py
blob: 9633e560da0ce11e412246f18d007141b747a93e (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Copyright (C) University of Tennessee Health Science Center, Memphis, TN.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# This program is available from Source Forge: at GeneNetwork Project
# (sourceforge.net/projects/genenetwork/).
#
# Contact Dr. Robert W. Williams at rwilliams@uthsc.edu
#
#
# This module is used by GeneNetwork project (www.genenetwork.org)

from gn2.wqflask.database import database_connection

from gn2.base.trait import retrieve_trait_info
from gn2.utility import helper_functions
from gn2.utility.tools import get_setting


class SendToWebGestalt:
    def __init__(self, start_vars):
        trait_db_list = [trait.strip()
                         for trait in start_vars['trait_list'].split(',')]
        helper_functions.get_trait_db_obs(self, trait_db_list)

        self.chip_name = test_chip(self.trait_list)

        self.wrong_input = "False"
        if self.chip_name == "mixed" or self.chip_name == "not_microarray" or '_NA' in self.chip_name:
            self.wrong_input = "True"
        else:
            trait_name_list, gene_id_list = gen_gene_id_list(self.trait_list)

            self.target_url = "https://www.webgestalt.org/option.php"

            id_type = "entrezgene"

            self.hidden_vars = {
                'gene_list': "\n".join(gene_id_list),
                             'id_type': "entrezgene",
                             'ref_set': "genome",
                             'enriched_database_category': "geneontology",
                             'enriched_database_name': "Biological_Process",
                             'sig_method': "fdr",
                             'sig_value': "0.05",
                             'enrich_method': "ORA",
                             'fdr_method': "BH",
                             'min_num': "2"
            }

            species = self.trait_list[0][1].group.species
            if species == "rat":
                self.hidden_vars['organism'] = "rnorvegicus"
            elif species == "human":
                self.hidden_vars['organism'] = "hsapiens"
            elif species == "mouse":
                self.hidden_vars['organism'] = "mmusculus"
            else:
                self.hidden_vars['organism'] = "others"


def test_chip(trait_list):
    final_chip_name = ""
    with database_connection(get_setting("SQL_URI")) as conn, conn.cursor() as cursor:
        for trait_db in trait_list:
            dataset = trait_db[1]
            cursor.execute(
                "SELECT GeneChip.GO_tree_value "
                "FROM GeneChip, ProbeFreeze, "
                "ProbeSetFreeze WHERE "
                "GeneChip.Id = ProbeFreeze.ChipId "
                "AND ProbeSetFreeze.ProbeFreezeId = ProbeFreeze.Id "
                "AND ProbeSetFreeze.Name = %s", (dataset.name,)
            )

            if result := cursor.fetchone():
                chip_name = result[0]
                if chip_name:
                    if chip_name != final_chip_name:
                        if final_chip_name:
                            return "mixed"
                        else:
                            final_chip_name = chip_name
                    else:
                        pass
                else:
                    cursor.execute(
                        "SELECT GeneChip.Name FROM GeneChip, ProbeFreeze, "
                        "ProbeSetFreeze WHERE "
                        "GeneChip.Id = ProbeFreeze.ChipId AND "
                        "ProbeSetFreeze.ProbeFreezeId = ProbeFreeze.Id "
                        "AND ProbeSetFreeze.Name = %s", (dataset.name,)
                    )
                    result = cursor.fetchone()
                    chip_name = f'{result[0]}_NA'
                    return chip_name
            else:
                cursor.execute(
                    "SELECT GeneChip.Name FROM GeneChip, ProbeFreeze, "
                    "ProbeSetFreeze WHERE GeneChip.Id = ProbeFreeze.ChipId "
                    "AND ProbeSetFreeze.ProbeFreezeId = ProbeFreeze.Id AND "
                    "ProbeSetFreeze.Name = %s", (dataset.name,)
                )
                result = cursor.fetchone()
                if not result:
                    return "not_microarray"
                else:
                    chip_name = f'{result[0]}_NA'
                    return chip_name
    return chip_name


def gen_gene_id_list(trait_list):
    trait_name_list = []
    gene_id_list = []
    for trait_db in trait_list:
        trait = trait_db[0]
        trait_name_list.append(trait.name)
        retrieve_trait_info(trait, trait.dataset)
        gene_id_list.append(str(trait.geneid))
    return trait_name_list, gene_id_list