aboutsummaryrefslogtreecommitdiff
path: root/uploader/population/views.py
blob: 39a576244e3f67f6ecf7aee80c29145acbe98d99 (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
"""Views dealing with populations/inbredsets"""
import re
import json
import base64
import traceback

from requests.models import Response
from MySQLdb.cursors import DictCursor
from flask import (flash,
                   request,
                   url_for,
                   redirect,
                   Blueprint,
                   current_app as app)

from uploader.samples.views import samplesbp
from uploader.oauth2.client import oauth2_post
from uploader.ui import make_template_renderer
from uploader.authorisation import require_login
from uploader.genotypes.views import genotypesbp
from uploader.db_utils import database_connection
from uploader.datautils import enumerate_sequence
from uploader.species.models import (all_species,
                                     species_by_id,
                                     order_species_by_family)

from .models import (save_population,
                     population_families,
                     populations_by_species,
                     population_genetic_types,
                     population_by_species_and_id)

__active_link__ = "populations"
popbp = Blueprint("populations", __name__)
popbp.register_blueprint(samplesbp, url_prefix="/")
popbp.register_blueprint(genotypesbp, url_prefix="/")
render_template = make_template_renderer("populations")


@popbp.route("/populations", methods=["GET", "POST"])
@require_login
def index():
    """Entry point for populations."""
    with database_connection(app.config["SQL_URI"]) as conn:
        if not bool(request.args.get("species_id")):
            return render_template(
                "populations/index.html",
                species=order_species_by_family(all_species(conn)))
        species = species_by_id(conn, request.args.get("species_id"))
        if not bool(species):
            flash("Invalid species identifier provided!", "alert-danger")
            return redirect(url_for("species.populations.index"))
        return redirect(url_for("species.populations.list_species_populations",
                                species_id=species["SpeciesId"]))

@popbp.route("/<int:species_id>/populations", methods=["GET"])
@require_login
def list_species_populations(species_id: int):
    """List a particular species' populations."""
    with database_connection(app.config["SQL_URI"]) as conn:
        species = species_by_id(conn, species_id)
        if not bool(species):
            flash("No species was found for given ID.", "alert-danger")
            return redirect(url_for("species.populations.index"))
        return render_template(
            "populations/list-populations.html",
            species=species,
            populations=enumerate_sequence(populations_by_species(
                conn, species_id)),
            activelink="list-populations")


def valid_population_name(population_name: str) -> bool:
    """
    Check whether the given name is a valid population name.

    Parameters
    ----------
    population_name: a string of characters.

    Checks For
    ----------
    * The name MUST start with an alphabet [a-zA-Z]
    * The name MUST end with an alphabet [a-zA-Z] or number [0-9]
    * The name MUST be composed of alphabets [a-zA-Z], numbers [0-9],
      underscores (_) and/or hyphens (-).

    Returns
    -------
    Boolean indicating whether or not the name is valid.
    """
    pattern = re.compile(r"^[a-zA-Z]+[a-zA-Z0-9_-]*[a-zA-Z0-9]$")
    return bool(pattern.match(population_name))


@popbp.route("/<int:species_id>/populations/create", methods=["GET", "POST"])
@require_login
def create_population(species_id: int):
    """Create a new population."""
    with (database_connection(app.config["SQL_URI"]) as conn,
          conn.cursor(cursorclass=DictCursor) as cursor):
        species = species_by_id(conn, species_id)

        if request.method == "GET":
            error_values = request.args.get("error_values")
            if not bool(error_values):
                error_values = base64.b64encode(
                    '{"errors":{}, "error_values": {}}'.encode("utf8")
                ).decode("utf8")

            error_values = json.loads(base64.b64decode(
                error_values.encode("utf8")).decode("utf8"))# type: ignore[union-attr]
            return render_template(
                "populations/create-population.html",
                species=species,
                families = population_families(conn),
                genetic_types = population_genetic_types(conn),
                mapping_methods=(
                    {"id": "0", "value": "No mapping support"},
                    {"id": "1", "value": "GEMMA, QTLReaper, R/qtl"},
                    {"id": "2", "value": "GEMMA"},
                    {"id": "3", "value": "R/qtl"},
                    {"id": "4", "value": "GEMMA, PLINK"}),
                activelink="create-population",
                **error_values)

        if not bool(species):
            flash("You must select a species.", "alert-danger")
            return redirect(url_for("species.populations.index"))

        errors: tuple[tuple[str, str], ...] = tuple()

        population_name = (request.form.get(
            "population_name") or "").strip()
        if not bool(population_name):
            errors = errors + (("population_name",
                                "You must provide a name for the population!"),)

        if not valid_population_name(population_name):
            errors = errors + ((
                "population_name",
                "The population name can only contain letters, numbers, "
                "hyphens and underscores."),)

        population_fullname = (request.form.get(
            "population_fullname") or "").strip()
        if not bool(population_fullname):
            errors = errors + (
                ("population_fullname", "Full Name MUST be provided."),)

        if bool(errors):
            values = base64.b64encode(
                json.dumps({
                    "errors": dict(errors),
                    "error_values": dict(request.form)
                }).encode("utf8"))
            return redirect(url_for("species.populations.create_population",
                                    species_id=species["SpeciesId"],
                                    error_values=values))

        new_population = save_population(cursor, {
            "SpeciesId": species["SpeciesId"],
            "Name": population_name,
            "InbredSetName": population_fullname,
            "FullName": population_fullname,
            "InbredSetCode": request.form.get("population_code") or None,
            "Description": request.form.get("population_description") or None,
            "Family": request.form.get("population_family") or None,
            "MappingMethodId": request.form.get("population_mapping_method_id"),
            "GeneticType": request.form.get("population_genetic_type") or None
        })

        def __handle_error__(error):
            error_format = (
                "\n\nThere was an error creating the population:\n\t%s\n\n")
            if issubclass(type(error), Exception):
                app.logger.debug(error_format, traceback.format_exc())
                raise error
            if issubclass(type(error), Response):
                try:
                    _data = error.json()
                except Exception as _exc:
                    raise Exception(error.content) from _exc
                raise Exception(_data)

            app.logger.debug(error_format, error)
            raise Exception(error)

        def __flash_success__(_success):
            flash("Successfully created resource.", "alert-success")
            return redirect(url_for(
                "species.populations.view_population",
                species_id=species["SpeciesId"],
                population_id=new_population["InbredSetId"]))

        app.logger.debug("We begin setting up the privileges here…")
        return oauth2_post(
            "auth/resource/populations/create",
            json={
                **dict(request.form),
                "species_id": species_id,
                "population_id": new_population["Id"],
                "public": "on"
            }
        ).either(__handle_error__, __flash_success__)


@popbp.route("/<int:species_id>/populations/<int:population_id>",
             methods=["GET"])
@require_login
def view_population(species_id: int, population_id: int):
    """View the details of a population."""
    with database_connection(app.config["SQL_URI"]) as conn:
        species = species_by_id(conn, species_id)
        population = population_by_species_and_id(conn, species_id, population_id)
        error = False

        if not bool(species):
            flash("You must select a species.", "alert-danger")
            error = True

        if not bool(population):
            flash("You must select a population.", "alert-danger")
            error = True

        if error:
            return redirect(url_for("species.populations.index"))

        return render_template("populations/view-population.html",
                               species=species,
                               population=population,
                               activelink="view-population")