aboutsummaryrefslogtreecommitdiff
path: root/gn3/db/correlations.py
blob: 5c3e7b89a93dfde198db37c9d03ac4b76bfe88ed (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
"""
This module will hold functions that are used in the (partial) correlations
feature to access the database to retrieve data needed for computations.
"""

from functools import reduce
from typing import Any, Dict, Tuple

from gn3.random import random_string
from gn3.data_helpers import partition_all
from gn3.db.species import translate_to_mouse_gene_id

from gn3.computations.partial_correlations import correlations_of_all_tissue_traits

def get_filename(target_db_name: str, conn: Any) -> str:
    """
    Retrieve the name of the reference database file with which correlations are
    computed.

    This is a migration of the
    `web.webqtl.correlation.CorrelationPage.getFileName` function in
    GeneNetwork1.
    """
    with conn.cursor() as cursor:
        cursor.execute(
            "SELECT Id, FullName from ProbeSetFreeze WHERE Name-%s",
            target_db_name)
        result = cursor.fetchone()
        if result:
            return "ProbeSetFreezeId_{tid}_FullName_{fname}.txt".format(
                tid=result[0],
                fname=result[1].replace(' ', '_').replace('/', '_'))

    return ""

def build_temporary_literature_table(
        conn: Any, species: str, gene_id: int, return_number: int) -> str:
    """
    Build and populate a temporary table to hold the literature correlation data
    to be used in computations.

    "This is a migration of the
    `web.webqtl.correlation.CorrelationPage.getTempLiteratureTable` function in
    GeneNetwork1.
    """
    def __translated_species_id(row, cursor):
        if species == "mouse":
            return row[1]
        query = {
            "rat": "SELECT rat FROM GeneIDXRef WHERE mouse=%s",
            "human": "SELECT human FROM GeneIDXRef WHERE mouse=%d"}
        if species in query.keys():
            cursor.execute(query[species], row[1])
            record = cursor.fetchone()
            if record:
                return record[0]
            return None
        return None

    temp_table_name = f"TOPLITERATURE{random_string(8)}"
    with conn.cursor as cursor:
        mouse_geneid = translate_to_mouse_gene_id(species, gene_id, conn)
        data_query = (
            "SELECT GeneId1, GeneId2, value FROM LCorrRamin3 "
            "WHERE GeneId1 = %(mouse_gene_id)s "
            "UNION ALL "
            "SELECT GeneId2, GeneId1, value FROM LCorrRamin3 "
            "WHERE GeneId2 = %(mouse_gene_id)s "
            "AND GeneId1 != %(mouse_gene_id)s")
        cursor.execute(
            (f"CREATE TEMPORARY TABLE {temp_table_name} ("
             "GeneId1 int(12) unsigned, "
             "GeneId2 int(12) unsigned PRIMARY KEY, "
             "value double)"))
        cursor.execute(data_query, mouse_gene_id=mouse_geneid)
        literature_data = [
            {"GeneId1": row[0], "GeneId2": row[1], "value": row[2]}
            for row in cursor.fetchall()
            if __translated_species_id(row, cursor)]

        cursor.execute(
            (f"INSERT INTO {temp_table_name} "
             "VALUES (%(GeneId1)s, %(GeneId2)s, %(value)s)"),
            literature_data[0:(2 * return_number)])

    return temp_table_name

def fetch_geno_literature_correlations(temp_table: str) -> str:
    """
    Helper function for `fetch_literature_correlations` below, to build query
    for `Geno*` tables.
    """
    return (
        f"SELECT Geno.Name, {temp_table}.value "
        "FROM Geno, GenoXRef, GenoFreeze "
        f"LEFT JOIN {temp_table} ON {temp_table}.GeneId2=ProbeSet.GeneId "
        "WHERE ProbeSet.GeneId IS NOT NULL "
        f"AND {temp_table}.value IS NOT NULL "
        "AND GenoXRef.GenoFreezeId = GenoFreeze.Id "
        "AND GenoFreeze.Name = %(db_name)s "
        "AND Geno.Id=GenoXRef.GenoId "
        "ORDER BY Geno.Id")

def fetch_probeset_literature_correlations(temp_table: str) -> str:
    """
    Helper function for `fetch_literature_correlations` below, to build query
    for `ProbeSet*` tables.
    """
    return (
        f"SELECT ProbeSet.Name, {temp_table}.value "
        "FROM ProbeSet, ProbeSetXRef, ProbeSetFreeze "
        "LEFT JOIN {temp_table} ON {temp_table}.GeneId2=ProbeSet.GeneId "
        "WHERE ProbeSet.GeneId IS NOT NULL "
        "AND {temp_table}.value IS NOT NULL "
        "AND ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id "
        "AND ProbeSetFreeze.Name = %(db_name)s "
        "AND ProbeSet.Id=ProbeSetXRef.ProbeSetId "
        "ORDER BY ProbeSet.Id")

def fetch_literature_correlations(
        species: str, gene_id: int, dataset: dict, return_number: int,
        conn: Any) -> dict:
    """
    Gather the literature correlation data and pair it with trait id string(s).

    This is a migration of the
    `web.webqtl.correlation.CorrelationPage.fetchLitCorrelations` function in
    GeneNetwork1.
    """
    temp_table = build_temporary_literature_table(
        conn, species, gene_id, return_number)
    query_fns = {
        "Geno": fetch_geno_literature_correlations,
        # "Temp": fetch_temp_literature_correlations,
        # "Publish": fetch_publish_literature_correlations,
        "ProbeSet": fetch_probeset_literature_correlations}
    with conn.cursor as cursor:
        cursor.execute(
            query_fns[dataset["dataset_type"]](temp_table),
            db_name=dataset["dataset_name"])
        results = cursor.fetchall()
        cursor.execute("DROP TEMPORARY TABLE %s", temp_table)
        return dict(results)

def fetch_symbol_value_pair_dict(
        symbol_list: Tuple[str, ...], data_id_dict: dict,
        conn: Any) -> Dict[str, Tuple[float, ...]]:
    """
    Map each gene symbols to the corresponding tissue expression data.

    This is a migration of the
    `web.webqtl.correlation.correlationFunction.getSymbolValuePairDict` function
    in GeneNetwork1.
    """
    data_ids = {
        symbol: data_id_dict.get(symbol) for symbol in symbol_list
        if data_id_dict.get(symbol) is not None
    }
    query = "SELECT Id, value FROM TissueProbeSetData WHERE Id IN %(data_ids)s"
    with conn.cursor() as cursor:
        cursor.execute(
            query,
            data_ids=tuple(data_ids.values()))
        value_results = cursor.fetchall()
        return {
            key: tuple(row[1] for row in value_results if row[0] == key)
            for key in data_ids.keys()
        }

    return {}

def fetch_gene_symbol_tissue_value_dict(
        symbol_list: Tuple[str, ...], data_id_dict: dict, conn: Any,
        limit_num: int = 1000) -> dict:#getGeneSymbolTissueValueDict
    """
    Wrapper function for `gn3.db.correlations.fetch_symbol_value_pair_dict`.

    This is a migrations of the
    `web.webqtl.correlation.correlationFunction.getGeneSymbolTissueValueDict` in
    GeneNetwork1.
    """
    count = len(symbol_list)
    if count != 0 and count <= limit_num:
        return fetch_symbol_value_pair_dict(symbol_list, data_id_dict, conn)

    if count > limit_num:
        return {
            key: value for dct in [
                fetch_symbol_value_pair_dict(sl, data_id_dict, conn)
                for sl in partition_all(limit_num, symbol_list)]
            for key, value in dct.items()
        }

    return {}

def fetch_tissue_probeset_xref_info(
        gene_name_list: Tuple[str, ...], probeset_freeze_id: int,
        conn: Any) -> Tuple[tuple, dict, dict, dict, dict, dict, dict]:
    """
    Retrieve the ProbeSet XRef information for tissues.

    This is a migration of the
    `web.webqtl.correlation.correlationFunction.getTissueProbeSetXRefInfo`
    function in GeneNetwork1."""
    with conn.cursor() as cursor:
        if len(gene_name_list) == 0:
            query = (
                "SELECT t.Symbol, t.GeneId, t.DataId, t.Chr, t.Mb, "
                "t.description, t.Probe_Target_Description "
                "FROM "
                "("
                "  SELECT Symbol, max(Mean) AS maxmean "
                "  FROM TissueProbeSetXRef "
                "  WHERE TissueProbeSetFreezeId=%(probeset_freeze_id)s "
                "  AND Symbol != '' "
                "  AND Symbol IS NOT NULL "
                "  GROUP BY Symbol"
                ") AS x "
                "INNER JOIN TissueProbeSetXRef AS t ON t.Symbol = x.Symbol "
                "AND t.Mean = x.maxmean")
            cursor.execute(query, probeset_freeze_id=probeset_freeze_id)
        else:
            query = (
                "SELECT t.Symbol, t.GeneId, t.DataId, t.Chr, t.Mb, "
                "t.description, t.Probe_Target_Description "
                "FROM "
                "("
                "  SELECT Symbol, max(Mean) AS maxmean "
                "  FROM TissueProbeSetXRef "
                "  WHERE TissueProbeSetFreezeId=%(probeset_freeze_id)s "
                "  AND Symbol in %(symbols)s "
                "  GROUP BY Symbol"
                ") AS x "
                "INNER JOIN TissueProbeSetXRef AS t ON t.Symbol = x.Symbol "
                "AND t.Mean = x.maxmean")
            cursor.execute(
                query, probeset_freeze_id=probeset_freeze_id,
                symbols=tuple(gene_name_list))

        results = cursor.fetchall()

    return reduce(
        lambda acc, item: (
            acc[0] + (item[0],),
            {**acc[1], item[0].lower(): item[1]},
            {**acc[1], item[0].lower(): item[2]},
            {**acc[1], item[0].lower(): item[3]},
            {**acc[1], item[0].lower(): item[4]},
            {**acc[1], item[0].lower(): item[5]},
            {**acc[1], item[0].lower(): item[6]}),
        results or tuple(),
        (tuple(), {}, {}, {}, {}, {}, {}))

def fetch_gene_symbol_tissue_value_dict_for_trait(
        gene_name_list: Tuple[str, ...], probeset_freeze_id: int,
        conn: Any) -> dict:
    """
    Fetches a map of the gene symbols to the tissue values.

    This is a migration of the
    `web.webqtl.correlation.correlationFunction.getGeneSymbolTissueValueDictForTrait`
    function in GeneNetwork1.
    """
    xref_info = fetch_tissue_probeset_xref_info(
        gene_name_list, probeset_freeze_id, conn)
    if xref_info[0]:
        return fetch_gene_symbol_tissue_value_dict(xref_info[0], xref_info[2], conn)
    return {}

def build_temporary_tissue_correlations_table(
        conn: Any, trait_symbol: str, probeset_freeze_id: int, method: str,
        return_number: int) -> str:
    """
    Build a temporary table to hold the tissue correlations data.

    This is a migration of the
    `web.webqtl.correlation.CorrelationPage.getTempTissueCorrTable` function in
    GeneNetwork1."""
    # We should probably pass the `correlations_of_all_tissue_traits` function
    # as an argument to this function and get rid of the one call immediately
    # following this comment.
    symbol_corr_dict, symbol_p_value_dict = correlations_of_all_tissue_traits(
        fetch_gene_symbol_tissue_value_dict_for_trait(
            (trait_symbol,), probeset_freeze_id, conn),
        fetch_gene_symbol_tissue_value_dict_for_trait(
            tuple(), probeset_freeze_id, conn),
        method)

    symbol_corr_list = sorted(
        symbol_corr_dict.items(), key=lambda key_val: key_val[1])

    temp_table_name = f"TOPTISSUE{random_string(8)}"
    create_query = (
        "CREATE TEMPORARY TABLE {temp_table_name}"
        "(Symbol varchar(100) PRIMARY KEY, Correlation float, PValue float)")
    insert_query = (
        f"INSERT INTO {temp_table_name}(Symbol, Correlation, PValue) "
        " VALUES (%(symbol)s, %(correlation)s, %(pvalue)s)")

    with conn.cursor() as cursor:
        cursor.execute(create_query)
        cursor.execute(
            insert_query,
            tuple({
                "symbol": symbol,
                "correlation": corr,
                "pvalue": symbol_p_value_dict[symbol]
            } for symbol, corr in symbol_corr_list[0: 2 * return_number]))

    return temp_table_name

def fetch_tissue_correlations(# pylint: disable=R0913
        dataset: dict, trait_symbol: str, probeset_freeze_id: int, method: str,
        return_number: int, conn: Any) -> dict:
    """
    Pair tissue correlations data with a trait id string.

    This is a migration of the
    `web.webqtl.correlation.CorrelationPage.fetchTissueCorrelations` function in
    GeneNetwork1.
    """
    temp_table = build_temporary_tissue_correlations_table(
        conn, trait_symbol, probeset_freeze_id, method, return_number)
    with conn.cursor() as cursor:
        cursor.execute(
            (
                f"SELECT ProbeSet.Name, {temp_table}.Correlation, "
                f"{temp_table}.PValue "
                "FROM (ProbeSet, ProbeSetXRef, ProbeSetFreeze) "
                "LEFT JOIN {temp_table} ON {temp_table}.Symbol=ProbeSet.Symbol "
                "WHERE ProbeSetFreeze.Name = %(db_name) "
                "AND ProbeSetFreeze.Id=ProbeSetXRef.ProbeSetFreezeId "
                "AND ProbeSet.Id = ProbeSetXRef.ProbeSetId "
                "AND ProbeSet.Symbol IS NOT NULL "
                "AND %s.Correlation IS NOT NULL"),
            db_name=dataset["dataset_name"])
        results = cursor.fetchall()
        cursor.execute("DROP TEMPORARY TABLE %s", temp_table)
        return {
            trait_name: (tiss_corr, tiss_p_val)
            for trait_name, tiss_corr, tiss_p_val in results}

def check_for_literature_info(conn: Any, geneid: int) -> bool:
    """
    Checks the database to find out whether the trait with `geneid` has any
    associated literature.

    This is a migration of the
    `web.webqtl.correlation.CorrelationPage.checkForLitInfo` function in
    GeneNetwork1.
    """
    query = "SELECT 1 FROM LCorrRamin3 WHERE GeneId1=%s LIMIT 1"
    with conn.cursor() as cursor:
        cursor.execute(query, geneid)
        result = cursor.fetchone()
        if result:
            return True

    return False

def check_symbol_for_tissue_correlation(
        conn: Any, tissue_probeset_freeze_id: int, symbol: str = "") -> bool:
    """
    Checks whether a symbol has any associated tissue correlations.

    This is a migration of the
    `web.webqtl.correlation.CorrelationPage.checkSymbolForTissueCorr` function
    in GeneNetwork1.
    """
    query = (
        "SELECT 1 FROM  TissueProbeSetXRef "
        "WHERE TissueProbeSetFreezeId=%(probeset_freeze_id)s "
        "AND Symbol=%(symbol)s LIMIT 1")
    with conn.cursor() as cursor:
        cursor.execute(
            query, probeset_freeze_id=tissue_probeset_freeze_id, symbol=symbol)
        result = cursor.fetchone()
        if result:
            return True

    return False

def fetch_sample_ids(
        conn: Any, sample_names: Tuple[str, ...], species_name: str) -> Tuple[
            int, ...]:
    """
    Given a sequence of sample names, and a species name, return the sample ids
    that correspond to both.

    This is a partial migration of the
    `web.webqtl.correlation.CorrelationPage.fetchAllDatabaseData` function in
    GeneNetwork1.
    """
    query = (
        "SELECT Strain.Id FROM Strain, Species "
        "WHERE Strain.Name IN %(samples_names)s "
        "AND Strain.SpeciesId=Species.Id "
        "AND Species.name=%(species_name)s")
    with conn.cursor() as cursor:
        cursor.execute(
            query, samples_names=tuple(samples),
            species_name=species)
        return cursor.fetchall()

def build_query_sgo_lit_corr(
        db_type: str, temp_table: str, sample_id_columns: str,
        joins: Tuple[str, ...]) -> str:
    """
    Build query for `SGO Literature Correlation` data, when querying the given
    `temp_table` temporary table.

    This is a partial migration of the
    `web.webqtl.correlation.CorrelationPage.fetchAllDatabaseData` function in
    GeneNetwork1.
    """
    return (
        (f"SELECT {db_type}.Name, {temp_table}.value, " +
         sample_id_columns +
         f" FROM ({db_type}, {db_type}XRef, {db_type}Freeze) " +
         f"LEFT JOIN {temp_table} ON {temp_table}.GeneId2=ProbeSet.GeneId " +
         " ".join(joins) +
         f" WHERE ProbeSet.GeneId IS NOT NULL " +
         f"AND {temp_table}.value IS NOT NULL " +
         f"AND {db_type}XRef.{db_type}FreezeId = {db_type}Freeze.Id " +
         f"AND {db_type}Freeze.Name = %(db_name)s " +
         f"AND {db_type}.Id = {db_type}XRef.{db_type}Id " +
         f"ORDER BY {db_type}.Id"),
        2)

def build_query_tissue_corr(db_type, temp_table, sample_id_columns, joins):
    """
    Build query for `Tissue Correlation` data, when querying the given
    `temp_table` temporary table.

    This is a partial migration of the
    `web.webqtl.correlation.CorrelationPage.fetchAllDatabaseData` function in
    GeneNetwork1.
    """
    return (
        (f"SELECT {db_type}.Name, {temp_table}.Correlation, " +
         f"{temp_table}.PValue, " +
         sample_id_columns +
         f" FROM ({db_type}, {db_type}XRef, {db_type}Freeze) " +
         f"LEFT JOIN {temp_table} ON {temp_table}.Symbol=ProbeSet.Symbol " +
         " ".join(joins) +
         f" WHERE ProbeSet.Symbol IS NOT NULL " +
         f"AND {temp_table}.Correlation IS NOT NULL " +
         f"AND {db_type}XRef.{db_type}FreezeId = {db_type}Freeze.Id " +
         f"AND {db_type}Freeze.Name = %(db_name)s " +
         f"AND {db_type}.Id = {db_type}XRef.{db_type}Id "
         f"ORDER BY {db_type}.Id"),
        3)

def fetch_all_database_data(
        conn: Any, species: str, gene_id: int, gene_symbol: str,
        samples: Tuple[str, ...], db_type: str, db_name: str, method: str,
        returnNumber: int, tissueProbeSetFreezeId: int) -> Tuple[Any, Any]:
    """
    This is a migration of the
    `web.webqtl.correlation.CorrelationPage.fetchAllDatabaseData` function in
    GeneNetwork1.
    """
    def __build_query__(sample_ids, temp_table):
        sample_id_columns = ", ".join(f"T{smpl}.value" for smpl in samples_ids)
        if db_type == "Publish":
            joins = tuple(
                ("LEFT JOIN PublishData AS T{item} "
                 "ON T{item}.Id = PublishXRef.DataId "
                 "AND T{item}.StrainId = %(T{item}_sample_id)s")
                for item in sample_ids)
            return (
                ("SELECT PublishXRef.Id, " +
                 sample_id_columns +
                 "FROM (PublishXRef, PublishFreeze) " +
                 " ".join(joins) +
                 " WHERE PublishXRef.InbredSetId = PublishFreeze.InbredSetId "
                 "AND PublishFreeze.Name = %(db_name)s"),
                1)
        if temp_table is not None:
            joins = tuple(
                (f"LEFT JOIN {db_type}Data AS T{item} "
                 f"ON T{item}.Id = {db_type}XRef.DataId "
                 f"AND T{item}.StrainId=%(T{item}_sample_id)s")
                for item in sample_ids)
            if method.lower() == "sgo literature correlation":
                return build_query_sgo_lit_corr(
                    sample_ids, temp_table, sample_id_columns)
            if method.lower() in (
                    "tissue correlation, pearson's r",
                    "tissue correlation, spearman's rho"):
                return build_query_tissue_corr(
                    sample_ids, temp_table, sample_id_columns)
        joins = tuple(
            (f"LEFT JOIN {db_type}Data AS T{item} "
             f"ON T{item}.Id = {db_type}XRef.DataId "
             f"AND T{item}.StrainId = %(T{item}_sample_id)s")
            for item in sample_ids)
        return (
            (
                f"SELECT {db_type}.Name, " +
                sample_id_columns +
                f" FROM ({db_type}, {db_type}XRef, {db_type}Freeze) " +
                " ".join(joins) +
                f" WHERE {db_type}XRef.{db_type}FreezeId = {db_type}Freeze.Id " +
                f"AND {db_type}Freeze.Name = %(db_name)s " +
                f"AND {db_type}.Id = {db_type}XRef.{db_type}Id " +
                f"ORDER BY {db_type}.Id"),
            1)

    def __fetch_data__(sample_ids, temp_table):
        query, data_start_pos = __build_query__(sample_ids, temp_table)
        with conn.cursor() as cursor:
            cursor.execute(
                query, db_name=db_name,
                **{f"T{item}_sample_id": item for item in sample_ids})
            return cursor.fetchall()

    sample_ids = tuple(
        # look into graduating this to an argument and removing the `samples`
        # and `species` argument: function currying and compositions might help
        # with this
        f"{sample_id}" for sample_id in
        fetch_sample_ids(conn, samples, species))

    temp_table = None
    if gene_id and db_type == "probeset":
        if method.lower() == "sgo literature correlation":
            temp_table = build_temporary_literature_table(
                conn, species, gene_id, return_number)
        if method.lower() in (
                "tissue correlation, pearson's r",
                "tissue correlation, spearman's rho"):
            temp_table = build_temporary_tissue_correlations_table(
                conn, trait_symbol, probeset_freeze_id, method, return_number)

    trait_database = tuple(
        item for sublist in
        (__fetch_data__(ssample_ids, temp_table)
         for ssample_ids in partition_all(25, sample_ids))
        for item in sublist)

    if temp_table:
        with conn.cursor() as cursor:
            cursor.execute(f"DROP TEMPORARY TABLE {temp_table}")

    return trait_database, data_start_pos