aboutsummaryrefslogtreecommitdiff
path: root/wqflask/scripts/textfiles_generator.py
blob: aa8f1647dc9afeda6913e1b03cc33d3b73e60051 (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
# database connection
import contextlib
import pickle
import datetime
from argparse import ArgumentParser
from typing import Any, Iterator, Protocol, Tuple
from pathlib import Path
from urllib.parse import urlparse
import MySQLdb as mdb
import lmdb
import os



"""
*script generate both metadata and probeset textfiles 
*manually commonly used datasets

** USage:
load the guix packages refer to issue:

 => https://issues.genenetwork.org/topics/guix-profiles

python3 file_name  sql_uri_path tmp_path 
flags:

    --metadata  to generate metadata files

    -- textfile to generate the probeset strain data


# set the TMPDIR for example
export TMPDIR = "/tmp/gn2"
# example  python3 

 python3 meta_data_script.py "mysql://kabui:1234@localhost/db_webqtl" $TMPDIR  --textfile
 python3 meta_data_script.py "mysql://kabui:1234@localhost/db_webqtl" $TMPDIR --metadata

python3 meta_data_script.py "mysql://kabui:1234@localhost/db_webqtl" $TMPDIR  --metadata  --textfile

"""


#! add to this list or use get_probes_meta to populate


DATASET_NAMES = [
    ("ProbeSet", "HC_M2_0606_P", "mouse"),
    ("ProbeSet", "UMUTAffyExon_0209_RMA", "mouse")
]


def get_probes_meta(sql_uri):

    # if you need to generate for all probes use this note 1000+

    with database_connection(sql_uri) as conn:
        with conn.cursor() as cursor:
            query = "SELECT Id,NAME FROM ProbeSetFreeze"
            cursor.execute(query)
            return cursor.fetchall()


def parse_db_url(sql_uri: str) -> Tuple:
    """function to parse SQL_URI env variable note:there\
    is a default value for SQL_URI so a tuple result is\
    always expected"""
    parsed_db = urlparse(sql_uri)
    return (
        parsed_db.hostname, parsed_db.username, parsed_db.password,
        parsed_db.path[1:], parsed_db.port)


class Connection(Protocol):

    def cursor(self, *args) -> Any:
        """A cursor in which queries may be performed"""
        ...


@contextlib.contextmanager
def database_connection(sql_uri: str = "") -> Iterator[Connection]:
    """Connect to MySQL database."""
    host, user, passwd, db_name, port = parse_db_url(
        sql_uri)

    connection = mdb.connect(db=db_name,
                             user=user,
                             passwd=passwd or '',
                             host=host,
                             port=port or 3306)
    try:
        yield connection
    finally:
        connection.close()


def query_probes_metadata(dataset_type, dataset_name, species, sql_uri):
    """query traits metadata in bulk for probeset"""

    if dataset_type.lower() != "probeset":
        return []
    with database_connection(sql_uri) as conn:
        with conn.cursor() as cursor:
            query = """
                    SELECT ProbeSet.Name,ProbeSet.Chr,ProbeSet.Mb,
                    ProbeSet.Symbol,ProbeSetXRef.mean,
                    CONCAT_WS('; ', ProbeSet.description, ProbeSet.Probe_Target_Description) AS description,
                    ProbeSetXRef.additive,ProbeSetXRef.LRS,Geno.Chr, Geno.Mb
                    FROM ProbeSet INNER JOIN ProbeSetXRef
                    ON ProbeSet.Id=ProbeSetXRef.ProbeSetId
                    INNER JOIN Geno
                    ON ProbeSetXRef.Locus = Geno.Name
                    INNER JOIN Species
                    ON Geno.SpeciesId = Species.Id
                    WHERE Species.Name = %s AND
                    ProbeSetXRef.ProbeSetFreezeId IN (
                      SELECT ProbeSetFreeze.Id
                      FROM ProbeSetFreeze WHERE ProbeSetFreeze.Name = %s)
                """
            cursor.execute(query, (species,) + (dataset_name,))
            return cursor.fetchall()


def get_metadata(dataset_type, dataset_name, species, sql_uri):
    """Retrieve the metadata"""
    def __location__(probe_chr, probe_mb):
        if probe_mb:
            return f"Chr{probe_chr}: {probe_mb:.6f}"
        return f"Chr{probe_chr}: ???"
    return {trait_name: {
        "name": trait_name,
        "view": True,
        "symbol": symbol,
        "dataset": dataset_name,
        "dataset_name": dataset_name,
        "mean": mean,
        "description": description,
        "additive": additive,
        "lrs_score": f"{lrs:3.1f}" if lrs else "",
        "location": __location__(probe_chr, probe_mb),
        "chr": probe_chr,
        "mb": probe_mb,
        "lrs_location": f'Chr{chr_score}: {mb:{".6f" if mb  else ""}}',
        "lrs_chr": chr_score,
        "lrs_mb": mb

    } for trait_name, probe_chr, probe_mb, symbol, mean, description,
        additive, lrs, chr_score, mb
        in query_probes_metadata(dataset_type, dataset_name, species, sql_uri)}


def cache_trait_metadata(dataset_name, dataset_type, data):
    if not data:
        return
    try:
        with lmdb.open(os.path.join(TMPDIR, f"metadata_{dataset_type}"), map_size=500971520) as env:
            with env.begin(write=True) as txn:
                metadata = {
                    "data": data,
                    "creation_date": datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
                    "dataset_name": dataset_name
                }

                txn.put(f"{dataset_name}".encode(), pickle.dumps(metadata))
    except lmdb.Error as error:
        raise error


def __sanitise_filename__(filename):
    ttable = str.maketrans({" ": "_", "/": "_", "\\": "_"})
    return str.translate(filename, ttable)


def __generate_file_name__(db_name, sql_uri):
    # todo add expiry time and checker

    with database_connection(sql_uri) as conn:
        with conn.cursor() as cursor:
            cursor.execute(
                'SELECT Id, FullName FROM ProbeSetFreeze WHERE Name = %s', (db_name,))
            results = cursor.fetchone()
            if (results):
                return __sanitise_filename__(
                    f"ProbeSetFreezeId_{results[0]}_{results[1]}")


def write_strains_data(sql_uri, dataset_name: str, data, col_names):

    if data == {}:
        return
    try:
        with lmdb.open(os.path.join(TMPDIR, "Probesets"), map_size=500971520) as env:
            with env.begin(write=True) as txn:
                meta = {
                    "strain_names": col_names,
                    "data": data,
                    "creation_date": datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                }
                txn.put(__generate_file_name__(dataset_name,
                                               sql_uri).encode(), pickle.dumps(meta))

    except lmdb.Error as error:
        raise error


def generate_probes_textfiles(db_name, db_type, sql_uri):

    def __parse_to_dict__(results):
        ids = ["ID"]
        data = {}
        for (trait, strain, val) in results:
            if strain not in ids:
                ids.append(strain)
            if trait in data:
                data[trait].append(val)
            else:
                data[trait] = [trait, val]
        return (data, ids)
    with database_connection(sql_uri) as conn:
        with conn.cursor() as cursor:

            cursor.execute(
                "SELECT ProbeSet.Name, Strain.Name, ProbeSetData.value "
                "FROM Strain LEFT JOIN ProbeSetData "
                "ON Strain.Id = ProbeSetData.StrainId "
                "LEFT JOIN ProbeSetXRef ON ProbeSetData.Id = ProbeSetXRef.DataId "
                "LEFT JOIN ProbeSet ON ProbeSetXRef.ProbeSetId = ProbeSet.Id "
                "WHERE ProbeSetXRef.ProbeSetFreezeId IN "
                "(SELECT Id FROM ProbeSetFreeze WHERE Name = %s) "
                "ORDER BY Strain.Name",
                (db_name,))
            return __parse_to_dict__(cursor.fetchall())


def argument_parser():
    parser = ArgumentParser()

    # add maybe dataset,species as args
    parser.add_argument(
        "SQL_URI",
        help="The uri to use to connect to the database",
        type=str)

    parser.add_argument(
        "TMPDIR",
        help="tmpdir to write the metadata to",
        type=str)
    parser.add_argument('--metadata', dest="metadata", action="store_true")
    parser.add_argument('--textfiles', dest='textfiles', action='store_true')

    parser.set_defaults(textfiles=False)
    parser.set_defaults(metadata=False)
    return parser.parse_args()


def run_textfiles_generator(args):
    try:
        for (d_type, dataset_name, _species) in fetch_to_generate_dataset("ProbeSet", "textfile"):
            file_name = __generate_file_name__(dataset_name, args.SQL_URI)
            if not check_file_expiry(os.path.join(
                    args.TMPDIR, "Probesets"), file_name):
                return
            write_strains_data(
                args.SQL_URI, dataset_name, *generate_probes_textfiles(dataset_name, d_type, args.SQL_URI))
    except Exception as error:
        raise error


def run_metadata_files_generator(args):
    for (dataset_type, dataset_name, species) in fetch_to_generate_dataset("ProbeSet", "metadata"):
        try:
            if not check_file_expiry(os.path.join(TMPDIR, f"metadata_{dataset_type}"), dataset_name):
                return

            cache_trait_metadata(dataset_name, dataset_type, get_metadata(
                dataset_type, dataset_name, species, args.SQL_URI))
        except Exception as error:
            raise error


def read_trait_metadata(dataset_name, dataset_type):
    try:
        with lmdb.open(os.path.join(TMPDIR, f"metadata_{dataset_type}"),
                       readonly=True, lock=False) as env:
            with env.begin() as txn:
                db_name = txn.get(dataset_name.encode())
                return (pickle.loads(db_name) if db_name else {})
    except lmdb.Error as error:
        return {}


def check_file_expiry(target_file_path, dataset_name, max_days=20):
    # return true if file has expired

    try:
        with lmdb.open(target_file_path, readonly=True, lock=False) as env:
            with env.begin() as txn:
                dataset = txn.get(dataset_name.encode())
                if dataset:
                    meta = pickle.loads(dataset)
                    creation_date = datetime.datetime.strptime(
                        meta["creation_date"], '%Y-%m-%d %H:%M:%S')
                    return ((datetime.datetime.now() - creation_date).days > max_days)
                return True
    except Exception:
        return True


def fetch_to_generate_dataset(dataset_type, gen_type):
    try:
        with lmdb.open(os.path.join("/tmp", "todolist_generate"), readonly=True, lock=False) as env:
            with env.begin() as txn:
                data = txn.get(f"{gen_type}:{dataset_type}".encode())
                if data:
                    return [result for result in pickle.loads(data).values()]
                return DATASET_NAMES
    except Exception as err:
        return DATASET_NAMES


if __name__ == '__main__':
    args = argument_parser()
    TMPDIR = args.TMPDIR
    if args.metadata:
        run_metadata_files_generator(args)
    if args.textfiles:
        run_textfiles_generator(args)