aboutsummaryrefslogtreecommitdiff
path: root/scripts/migrate_existing_data.py
blob: 1261462d962041e0fcf6cf303cf1612329338afc (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
"""
Migrate existing data that is not assigned to any group to the default sys-admin
group for accessibility purposes.
"""
import sys
import json
import time
import random
import logging
from pathlib import Path
from uuid import UUID, uuid4

import click
from MySQLdb.cursors import DictCursor

from gn_auth.auth.db import mariadb as biodb

import gn_auth.auth.db.sqlite3 as authdb
from gn_auth.auth.authentication.users import User
from gn_auth.auth.authorisation.roles.models import (
    revoke_user_role_by_name, assign_user_role_by_name)

from gn_auth.auth.authorisation.resources.groups.models import (
    Group, save_group, add_resources_to_group)
from gn_auth.auth.authorisation.resources.models import (
    Resource, ResourceCategory, __assign_resource_owner_role__)


class DataNotFound(Exception):
    """Raise if no admin user exists."""


def sys_admins(conn: authdb.DbConnection) -> tuple[User, ...]:
    """Retrieve all the existing system admins."""
    with authdb.cursor(conn) as cursor:
        cursor.execute(
            "SELECT u.* FROM users AS u "
            "INNER JOIN user_roles AS ur ON u.user_id=ur.user_id "
            "INNER JOIN roles AS r ON ur.role_id=r.role_id "
            "WHERE r.role_name='system-administrator'")
        return tuple(User.from_sqlite3_row(row) for row in cursor.fetchall())
    return tuple()


def choose_admin(enum_admins: dict[int, User]) -> int:
    """Prompt and read user choice."""
    while True:
        try:
            print("\n===========================\n")
            print("We found the following system administrators:")
            for idx, admin in enum_admins.items():
                print(f"\t{idx}: {admin.name} ({admin.email})")
            choice = input(f"Choose [1 .. {len(enum_admins)}]: ")
            return int(choice)
        except ValueError as _verr:
            if choice.lower() == "quit":
                print("Goodbye!")
                sys.exit(0)
            print(f"\nERROR: Invalid choice '{choice}'!")


def select_sys_admin(admins: tuple[User, ...]) -> User:
    """Pick one admin out of list."""
    if len(admins) > 0:
        if len(admins) == 1:
            print(f"-> Found Admin: {admins[0].name} ({admins[0].email})")
            return admins[0]
        enum_admins = dict(enumerate(admins, start=1))
        chosen = enum_admins[choose_admin(enum_admins)]
        print(f"-> Chosen Admin: {chosen.name} ({chosen.email})")
        return chosen
    raise DataNotFound(
        "No administrator user found. Create an administrator user first.")


def admin_group(conn: authdb.DbConnection, admin: User) -> Group:
    """Retrieve the admin's user group. If none exist, create one."""
    with authdb.cursor(conn) as cursor:
        cursor.execute(
            "SELECT g.* FROM users AS u "
            "INNER JOIN group_users AS gu ON u.user_id=gu.user_id "
            "INNER JOIN groups AS g on gu.group_id=g.group_id "
            "WHERE u.user_id = ?",
            (str(admin.user_id),))
        row = cursor.fetchone()
        if row:
            return Group(UUID(row["group_id"]),
                         row["group_name"],
                         json.loads(row["group_metadata"]))
        new_group = save_group(cursor, "AutoAdminGroup", {
            "group_description": (
                "Created by script for existing data visibility. "
                "Existing data was migrated into this group and assigned "
                "to publicly visible resources according to type.")
        })

        cursor.execute(
            "SELECT * FROM resource_categories WHERE "
            "resource_category_key='group'")
        res_cat_id = cursor.fetchone()["resource_category_id"]
        grp_res = {
            "group_id": str(new_group.group_id),
            "resource_id": str(uuid4()),
            "resource_name": new_group.group_name,
            "resource_category_id": res_cat_id,
            "public": 0
        }
        cursor.execute(
            "INSERT INTO resources VALUES "
            "(:resource_id, :resource_name, :resource_category_id, :public)",
            grp_res)
        cursor.execute(
            "INSERT INTO group_resources(resource_id, group_id) "
            "VALUES(:resource_id, :group_id)",
            grp_res)
        cursor.execute("INSERT INTO group_users VALUES (?, ?)",
                       (str(new_group.group_id), str(admin.user_id)))
        revoke_user_role_by_name(cursor, admin, "group-creator")
        assign_user_role_by_name(
            cursor, admin, UUID(grp_res["resource_id"]), "group-leader")
        return new_group


def __resource_category_by_key__(
        cursor: authdb.DbCursor, category_key: str) -> ResourceCategory:
    """Retrieve a resource category by its ID."""
    cursor.execute(
        "SELECT * FROM resource_categories WHERE resource_category_key = ?",
        (category_key,))
    row = cursor.fetchone()
    if not bool(row):
        raise DataNotFound(
            f"Could not find resource category with key {category_key}")
    return ResourceCategory(UUID(row["resource_category_id"]),
                            row["resource_category_key"],
                            row["resource_category_description"])


def __create_resources__(cursor: authdb.DbCursor) -> tuple[Resource, ...]:
    """Create default resources."""
    resources = tuple(Resource(
        uuid4(), name, __resource_category_by_key__(cursor, catkey),
        True, tuple()
    ) for name, catkey in (
        ("mRNA-euhrin", "mrna"),
        ("pheno-xboecp", "phenotype"),
        ("geno-welphd", "genotype")))
    cursor.executemany(
        "INSERT INTO resources VALUES (:rid, :rname, :rcid, :pub)",
        tuple({
            "rid": str(res.resource_id),
            "rname": res.resource_name,
            "rcid": str(res.resource_category.resource_category_id),
            "pub": 1
        } for res in resources))
    return resources


def default_resources(conn: authdb.DbConnection, group: Group) -> tuple[
        Resource, ...]:
    """Create default resources, or return them if they exist."""
    with authdb.cursor(conn) as cursor:
        cursor.execute(
            "SELECT r.resource_id, r.resource_name, r.public, rc.* "
            "FROM resource_ownership AS ro INNER JOIN resources AS r "
            "ON ro.resource_id=r.resource_id "
            "INNER JOIN resource_categories AS rc "
            "ON r.resource_category_id=rc.resource_category_id "
            "WHERE ro.group_id=? AND r.resource_name IN "
            "('mRNA-euhrin', 'pheno-xboecp', 'geno-welphd')",
            (str(group.group_id),))
        rows = cursor.fetchall()
        if len(rows) == 0:
            return __create_resources__(cursor)

        return tuple(Resource(
            UUID(row["resource_id"]),
            row["resource_name"],
            ResourceCategory(
                UUID(row["resource_category_id"]),
                row["resource_category_key"],
                row["resource_category_description"]),
            bool(row["public"]),
            tuple()
        ) for row in rows)


def delay():
    """Delay a while: anything from 2 seconds to 15 seconds."""
    time.sleep(random.choice(range(2,16)))


def __assigned_mrna__(authconn):
    """Retrieve assigned mRNA items."""
    with authdb.cursor(authconn) as cursor:
        cursor.execute(
            "SELECT SpeciesId, InbredSetId, ProbeFreezeId, ProbeSetFreezeId "
            "FROM linked_mrna_data")
        return tuple(
            (row["SpeciesId"], row["InbredSetId"], row["ProbeFreezeId"],
             row["ProbeSetFreezeId"]) for row in cursor.fetchall())


def __unassigned_mrna__(bioconn, assigned):
    """Retrieve unassigned mRNA data items."""
    query = (
        "SELECT s.SpeciesId, iset.InbredSetId, pf.ProbeFreezeId, "
        "psf.Id AS ProbeSetFreezeId, psf.Name AS dataset_name, "
        "psf.FullName AS dataset_fullname, psf.ShortName AS dataset_shortname "
        "FROM Species AS s INNER JOIN InbredSet AS iset "
        "ON s.SpeciesId=iset.SpeciesId INNER JOIN ProbeFreeze AS pf "
        "ON iset.InbredSetId=pf.InbredSetId INNER JOIN ProbeSetFreeze AS psf "
        "ON pf.ProbeFreezeId=psf.ProbeFreezeId ")
    if len(assigned) > 0:
        paramstr = ", ".join(["(%s, %s, %s, %s)"] * len(assigned))
        query = query + (
            "WHERE (s.SpeciesId, iset.InbredSetId, pf.ProbeFreezeId, psf.Id) "
            f"NOT IN ({paramstr}) ")

    query = query + "LIMIT 100000"
    with bioconn.cursor(DictCursor) as cursor:
        cursor.execute(query, tuple(item for row in assigned for item in row))
        return (row for row in cursor.fetchall())


def __assign_mrna__(authconn, bioconn, resource, group):
    "Assign any unassigned mRNA data to resource."
    while True:
        unassigned = tuple({
            "data_link_id": str(uuid4()),
            "group_id": str(group.group_id),
            "resource_id": str(resource.resource_id),
            **row
        } for row in __unassigned_mrna__(
            bioconn, __assigned_mrna__(authconn)))

        if len(unassigned) <= 0:
            print("-> mRNA: Completed!")
            break
        with authdb.cursor(authconn) as cursor:
            cursor.executemany(
                "INSERT INTO linked_mrna_data VALUES "
                "(:data_link_id, :group_id, :SpeciesId, :InbredSetId, "
                ":ProbeFreezeId, :ProbeSetFreezeId, :dataset_name, "
                ":dataset_fullname, :dataset_shortname)",
                unassigned)
            cursor.executemany(
                "INSERT INTO mrna_resources VALUES "
                "(:resource_id, :data_link_id)",
                unassigned)
            print(f"-> mRNA: Linked {len(unassigned)}")
            delay()


def __assigned_geno__(authconn):
    """Retrieve assigned genotype data."""
    with authdb.cursor(authconn) as cursor:
        cursor.execute(
            "SELECT SpeciesId, InbredSetId, GenoFreezeId "
            "FROM linked_genotype_data")
        return tuple((row["SpeciesId"], row["InbredSetId"], row["GenoFreezeId"])
                     for row in cursor.fetchall())

def __unassigned_geno__(bioconn, assigned):
    """Fetch unassigned genotype data."""
    query = (
        "SELECT s.SpeciesId, iset.InbredSetId, iset.InbredSetName, "
        "gf.Id AS GenoFreezeId, gf.Name AS dataset_name, "
        "gf.FullName AS dataset_fullname, "
        "gf.ShortName AS dataset_shortname "
        "FROM Species AS s INNER JOIN InbredSet AS iset "
        "ON s.SpeciesId=iset.SpeciesId INNER JOIN GenoFreeze AS gf "
        "ON iset.InbredSetId=gf.InbredSetId ")
    if len(assigned) > 0:
        paramstr = ", ".join(["(%s, %s, %s)"] * len(assigned))
        query = query + (
            "WHERE (s.SpeciesId, iset.InbredSetId, gf.Id) "
            f"NOT IN ({paramstr}) ")

    query = query + "LIMIT 100000"
    with bioconn.cursor(DictCursor) as cursor:
        cursor.execute(query, tuple(item for row in assigned for item in row))
        return (row for row in cursor.fetchall())


def __assign_geno__(authconn, bioconn, resource, group):
    "Assign any unassigned Genotype data to resource."
    while True:
        unassigned = tuple({
            "data_link_id": str(uuid4()),
            "group_id": str(group.group_id),
            "resource_id": str(resource.resource_id),
            **row
        } for row in __unassigned_geno__(
            bioconn, __assigned_geno__(authconn)))

        if len(unassigned) <= 0:
            print("-> Genotype: Completed!")
            break
        with authdb.cursor(authconn) as cursor:
            cursor.executemany(
                "INSERT INTO linked_genotype_data VALUES "
                "(:data_link_id, :group_id, :SpeciesId, :InbredSetId, "
                ":GenoFreezeId, :dataset_name, :dataset_fullname, "
                ":dataset_shortname)",
                unassigned)
            cursor.executemany(
                "INSERT INTO genotype_resources VALUES "
                "(:resource_id, :data_link_id)",
                unassigned)
            print(f"-> Genotype: Linked {len(unassigned)}")
            delay()


def __assigned_pheno__(authconn):
    """Retrieve assigned phenotype data."""
    with authdb.cursor(authconn) as cursor:
        cursor.execute(
            "SELECT SpeciesId, InbredSetId, PublishFreezeId, PublishXRefId "
            "FROM linked_phenotype_data")
        return tuple((
            row["SpeciesId"], row["InbredSetId"], row["PublishFreezeId"],
            row["PublishXRefId"]) for row in cursor.fetchall())


def __unassigned_pheno__(bioconn, assigned):
    """Retrieve all unassigned Phenotype data."""
    query = (
            "SELECT spc.SpeciesId, iset.InbredSetId, "
            "pf.Id AS PublishFreezeId, pf.Name AS dataset_name, "
            "pf.FullName AS dataset_fullname, "
            "pf.ShortName AS dataset_shortname, pxr.Id AS PublishXRefId "
            "FROM "
            "Species AS spc "
            "INNER JOIN InbredSet AS iset "
            "ON spc.SpeciesId=iset.SpeciesId "
            "INNER JOIN PublishFreeze AS pf "
            "ON iset.InbredSetId=pf.InbredSetId "
            "INNER JOIN PublishXRef AS pxr "
            "ON pf.InbredSetId=pxr.InbredSetId ")
    if len(assigned) > 0:
        paramstr = ", ".join(["(%s, %s, %s, %s)"] * len(assigned))
        query = query + (
            "WHERE (spc.SpeciesId, iset.InbredSetId, pf.Id, pxr.Id) "
            f"NOT IN ({paramstr}) ")

    query = query + "LIMIT 100000"
    with bioconn.cursor(DictCursor) as cursor:
        cursor.execute(query, tuple(item for row in assigned for item in row))
        return (row for row in cursor.fetchall())


def __assign_pheno__(authconn, bioconn, resource, group):
    """Assign any unassigned Phenotype data to resource."""
    while True:
        unassigned = tuple({
            "data_link_id": str(uuid4()),
            "group_id": str(group.group_id),
            "resource_id": str(resource.resource_id),
            **row
        } for row in __unassigned_pheno__(
            bioconn, __assigned_pheno__(authconn)))

        if len(unassigned) <= 0:
            print("-> Phenotype: Completed!")
            break
        with authdb.cursor(authconn) as cursor:
            cursor.executemany(
                "INSERT INTO linked_phenotype_data VALUES "
                "(:data_link_id, :group_id, :SpeciesId, :InbredSetId, "
                ":PublishFreezeId, :dataset_name, :dataset_fullname, "
                ":dataset_shortname, :PublishXRefId)",
                unassigned)
            cursor.executemany(
                "INSERT INTO phenotype_resources VALUES "
                "(:resource_id, :data_link_id)",
                unassigned)
            print(f"-> Phenotype: Linked {len(unassigned)}")
            delay()


def assign_data_to_resource(
        authconn, bioconn, resource: Resource, group: Group):
    """Assign existing data, not linked to any group to the resource."""
    assigner_fns = {
        "mrna": __assign_mrna__,
        "genotype": __assign_geno__,
        "phenotype": __assign_pheno__
    }
    return assigner_fns[resource.resource_category.resource_category_key](
        authconn, bioconn, resource, group)


def entry(authdbpath, mysqldburi):
    """Entry-point for data migration."""
    if not Path(authdbpath).exists():
        print(
            f"ERROR: Auth db file `{authdbpath}` does not exist.",
            file=sys.stderr)
        sys.exit(2)
    try:
        with (authdb.connection(authdbpath) as authconn,
              biodb.database_connection(mysqldburi) as bioconn):
            admin = select_sys_admin(sys_admins(authconn))
            the_admin_group = admin_group(authconn, admin)
            resources = default_resources(authconn, the_admin_group)
            add_resources_to_group(authconn, resources, the_admin_group)
            for resource in resources:
                assign_data_to_resource(
                    authconn, bioconn, resource, the_admin_group)
                with authdb.cursor(authconn) as cursor:
                    __assign_resource_owner_role__(cursor, resource, admin)
    except DataNotFound as dnf:
        print(dnf.args[0], file=sys.stderr)
        sys.exit(1)


@click.command()
@click.argument("authdbpath") # "Path to the Auth(entic|oris)ation database"
@click.argument("mysqldburi") # "URI to the MySQL database with the biology data"
@click.option("--loglevel", default="WARNING", show_default=True,
              type=click.Choice(["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"]))
def run(authdbpath, mysqldburi, loglevel):
    """Setup command-line arguments."""
    globallogger = logging.getLogger()
    globallogger.setLevel(loglevel)
    entry(authdbpath, mysqldburi)


if __name__ == "__main__":
    run() # pylint: disable=[no-value-for-parameter]