about summary refs log tree commit diff
path: root/gn2/wqflask/external_tools
diff options
context:
space:
mode:
authorArun Isaac2023-12-29 18:55:37 +0000
committerArun Isaac2023-12-29 19:01:46 +0000
commit204a308be0f741726b9a620d88fbc22b22124c81 (patch)
treeb3cf66906674020b530c844c2bb4982c8a0e2d39 /gn2/wqflask/external_tools
parent83062c75442160427b50420161bfcae2c5c34c84 (diff)
downloadgenenetwork2-204a308be0f741726b9a620d88fbc22b22124c81.tar.gz
Namespace all modules under gn2.
We move all modules under a gn2 directory. This is important for
"correct" packaging and deployment as a Guix service.
Diffstat (limited to 'gn2/wqflask/external_tools')
-rw-r--r--gn2/wqflask/external_tools/__init__.py0
-rw-r--r--gn2/wqflask/external_tools/send_to_bnw.py70
-rw-r--r--gn2/wqflask/external_tools/send_to_geneweaver.py113
-rw-r--r--gn2/wqflask/external_tools/send_to_webgestalt.py129
4 files changed, 312 insertions, 0 deletions
diff --git a/gn2/wqflask/external_tools/__init__.py b/gn2/wqflask/external_tools/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/gn2/wqflask/external_tools/__init__.py
diff --git a/gn2/wqflask/external_tools/send_to_bnw.py b/gn2/wqflask/external_tools/send_to_bnw.py
new file mode 100644
index 00000000..fdbc0d93
--- /dev/null
+++ b/gn2/wqflask/external_tools/send_to_bnw.py
@@ -0,0 +1,70 @@
+# 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.base.trait import GeneralTrait
+from gn2.utility import helper_functions, corr_result_helpers
+
+
+class SendToBNW:
+    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)
+
+        trait_samples_list = []
+
+        for trait_db in self.trait_list:
+            trait_1 = trait_db[0]
+            this_sample_data = trait_1.data
+
+            trait1_samples = list(this_sample_data.keys())
+            trait_samples_list.append(trait1_samples)
+
+        shared_samples = list(
+            set(trait_samples_list[0]).intersection(*trait_samples_list))
+
+        self.form_value = ""  # ZS: string that is passed to BNW through form
+        values_list = []
+        for trait_db in self.trait_list:
+            this_trait = trait_db[0]
+            this_sample_data = this_trait.data
+
+            trait_vals = []
+            for sample in this_sample_data:
+                if sample in shared_samples:
+                    trait_vals.append(this_sample_data[sample].value)
+
+            values_list.append(trait_vals)
+            self.form_value += "_" + str(this_trait.name) + ","
+
+        values_list = zip(*values_list)
+        self.form_value = self.form_value[:-1]
+        self.form_value += ";"
+
+        for row in values_list:
+            has_none = False
+            for cell in row:
+                if not cell:
+                    has_none = True
+                    break
+            if has_none:
+                continue
+            self.form_value += ",".join(str(cell) for cell in row)
+            self.form_value += ";"
diff --git a/gn2/wqflask/external_tools/send_to_geneweaver.py b/gn2/wqflask/external_tools/send_to_geneweaver.py
new file mode 100644
index 00000000..76ff7302
--- /dev/null
+++ b/gn2/wqflask/external_tools/send_to_geneweaver.py
@@ -0,0 +1,113 @@
+# 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.utility import helper_functions
+from gn2.utility.tools import get_setting
+
+
+class SendToGeneWeaver:
+    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:
+            species = self.trait_list[0][1].group.species
+            if species == "rat":
+                species_name = "Rattus norvegicus"
+            elif species == "human":
+                species_name = "Homo sapiens"
+            elif species == "mouse":
+                species_name = "Mus musculus"
+            else:
+                species_name = ""
+
+            trait_name_list = get_trait_name_list(self.trait_list)
+
+            self.hidden_vars = {
+                'client': "genenetwork",
+                'species': species_name,
+                'idtype': self.chip_name,
+                'list': ",".join(trait_name_list),
+            }
+
+
+def get_trait_name_list(trait_list):
+    name_list = []
+    for trait_db in trait_list:
+        name_list.append(trait_db[0].name)
+
+    return name_list
+
+
+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,)
+                    )
+                    chip_name = f'{cursor.fetchone()[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,)
+                )
+                if result := cursor.fetchone():
+                    chip_name = f'{result[0]}_NA'
+                    return chip_name
+                return "not_microarray"
+
+    return chip_name
diff --git a/gn2/wqflask/external_tools/send_to_webgestalt.py b/gn2/wqflask/external_tools/send_to_webgestalt.py
new file mode 100644
index 00000000..9633e560
--- /dev/null
+++ b/gn2/wqflask/external_tools/send_to_webgestalt.py
@@ -0,0 +1,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