-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1336 lines (1155 loc) · 52.2 KB
/
Copy pathapp.py
File metadata and controls
1336 lines (1155 loc) · 52.2 KB
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import csv
import io
import os
import re
import sqlite3
from datetime import date, datetime
import click
from collections import defaultdict
from flask import Flask, Response, abort, flash, jsonify, redirect, render_template, request, url_for
from sqlalchemy import event, func
from sqlalchemy.engine import Engine
from sqlalchemy.exc import IntegrityError
from flask_wtf import CSRFProtect
from sqlalchemy.orm import joinedload
from config import Config
from forms import (
CellLineForm,
ExperimentForm,
AcquiredFileEditForm,
AcquiredFileForm,
MassSpecSampleForm,
ProjectForm,
SpeciesForm,
UserForm,
VirusForm,
)
from models import (
CellLine,
CrosslinkSample,
Experiment,
AcquiredFile,
IdentificationSample,
MassSpecSample,
Project,
QueuedFile,
Species,
User,
Virus,
db,
)
app = Flask(__name__)
app.config.from_object(Config)
db.init_app(app)
CSRFProtect(app)
@event.listens_for(Engine, "connect")
def _configure_sqlite_connection(dbapi_connection, connection_record):
if isinstance(dbapi_connection, sqlite3.Connection):
cur = dbapi_connection.cursor()
cur.execute("PRAGMA foreign_keys=ON")
# Make concurrent writers wait for the lock rather than immediately
# failing with "database is locked".
cur.execute("PRAGMA busy_timeout=5000")
cur.close()
# ---------------------------------------------------------------------------
# Composite-key encoding for SelectField values
# A bare code is no longer unique, so dropdown values that identify an
# experiment or sample encode the full parent chain, joined by \x1f (a char
# that never appears in a code).
# ---------------------------------------------------------------------------
TOKEN_SEP = "\x1f"
# Project holding commonly-used template samples, offered as copy sources from
# any project's new-sample form.
FAV_PROJECT_CODE = "FAV"
def _exp_token(project_code, experiment_code):
return f"{project_code}{TOKEN_SEP}{experiment_code}"
def _sample_token(project_code, experiment_code, code):
return TOKEN_SEP.join([project_code, experiment_code, code])
def _next_experiment_code(project_code):
"""Suggested code for a new experiment: 'E' + zero-padded (count + 1)."""
n = Experiment.query.filter_by(project_code=project_code).count() + 1
return f"E{n:02d}"
def _next_sample_code(project_code, experiment_code):
"""Suggested code for a new sample: 'S' + zero-padded (count + 1)."""
n = MassSpecSample.query.filter_by(
project_code=project_code, experiment_code=experiment_code
).count() + 1
return f"S{n:02d}"
def _commit_unique(form, field_name, what):
"""Commit a pending insert, turning a duplicate-key clash into a form error.
Codes are (part of) the primary key, so re-submitting an existing one — e.g.
hitting Save again after navigating back — raises an IntegrityError that would
otherwise surface as a 500. We catch the duplicate, roll back, and report it
inline on the offending field. Returns True if committed, False if it was a
duplicate (in which case the caller should re-render the form). Any other
IntegrityError is genuinely unexpected and is re-raised.
"""
try:
db.session.commit()
return True
except IntegrityError as exc:
db.session.rollback()
if "UNIQUE constraint failed" not in str(getattr(exc, "orig", exc)):
raise
field = getattr(form, field_name, None)
value = field.data if field is not None else ""
message = f"{what} “{value}” already exists — please choose a different one."
if field is not None:
field.errors.append(message)
flash(message, "error")
return False
def _split_token(token):
"""Split a \x1f-joined token into its parts, or () for an empty value."""
return tuple(token.split(TOKEN_SEP)) if token else ()
@app.template_filter('wrap_code')
def wrap_code_filter(s, code, marker, only_first=False):
if not code or not s:
return s
pattern = re.compile(re.escape(code), re.IGNORECASE)
return pattern.sub(
lambda m: f'\x01{marker}\x02{m.group(0)}\x03',
s,
count=1 if only_first else 0,
)
@app.context_processor
def inject_nav_counts():
# Runs on every page render, so fold the per-entity counts into a single
# SELECT of scalar subqueries rather than one round trip per entity.
nav_models = {
"projects": Project,
"experiments": Experiment,
"samples": MassSpecSample,
"species": Species,
"cell_lines": CellLine,
"viruses": Virus,
"files": AcquiredFile,
"users": User,
}
try:
cols = [
db.select(func.count()).select_from(model).scalar_subquery().label(key)
for key, model in nav_models.items()
]
row = db.session.execute(db.select(*cols)).one()
return {"nav_counts": dict(row._mapping)}
except Exception:
return {"nav_counts": {}}
# ---------------------------------------------------------------------------
# CLI: init-db
# ---------------------------------------------------------------------------
@app.cli.command("init-db")
def init_db():
"""Create database tables from schema.sql."""
db_path = app.config["SQLALCHEMY_DATABASE_URI"].replace("sqlite:///", "")
schema_path = os.path.join(os.path.dirname(__file__), "schema.sql")
with open(schema_path) as f:
schema_sql = f.read()
conn = sqlite3.connect(db_path)
conn.execute("PRAGMA foreign_keys=ON")
conn.executescript(schema_sql)
conn.close()
click.echo(f"Initialized database at {db_path}")
# ---------------------------------------------------------------------------
# Index
# ---------------------------------------------------------------------------
@app.route("/")
def index():
return redirect(url_for("project_list"))
@app.route("/api/tree")
def api_tree():
def file_nodes(sample):
nodes = [
{
"id": f.id,
"name": f.filename or f.location or str(f.id),
"level": "file",
"total_bytes": int(f.size_bytes or 0),
"url": url_for("file_detail", id=f.id),
}
for f in sample.acquired_files
]
nodes.sort(key=lambda n: n["total_bytes"], reverse=True)
return nodes
def sample_node(s):
children = file_nodes(s)
total = sum(c["total_bytes"] for c in children)
return {"code": s.code, "name": s.name, "level": "sample", "total_bytes": total,
"url": url_for("sample_detail", project_code=s.project_code,
experiment_code=s.experiment_code, code=s.code),
"children": children}
def experiment_node(e):
children = sorted([sample_node(s) for s in e.samples], key=lambda n: n["total_bytes"], reverse=True)
total = sum(c["total_bytes"] for c in children)
return {"id": e.code, "name": e.name, "level": "experiment", "total_bytes": total,
"url": url_for("experiment_detail", project_code=e.project_code, code=e.code),
"children": children}
def project_node(p):
children = sorted([experiment_node(e) for e in p.experiments], key=lambda n: n["total_bytes"], reverse=True)
total = sum(c["total_bytes"] for c in children)
return {"id": p.code, "name": p.name or p.code, "level": "project", "total_bytes": total,
"url": url_for("project_detail", code=p.code), "children": children}
projects = Project.query.filter_by(active=True).all()
project_nodes = sorted([project_node(p) for p in projects], key=lambda n: n["total_bytes"], reverse=True)
tree = {
"name": "Mass Spec Acquisition Tracker",
"level": "root",
"total_bytes": sum(n["total_bytes"] for n in project_nodes),
"children": project_nodes,
}
return jsonify(tree)
# ---------------------------------------------------------------------------
# Projects
# ---------------------------------------------------------------------------
@app.route("/projects")
def project_list():
show_archived = request.args.get("show_archived", "0") == "1"
if show_archived:
projects = Project.query.order_by(Project.name).all()
else:
projects = Project.query.filter_by(active=True).order_by(Project.name).all()
users = {u.initials: u.name for u in User.query.all()}
# One grouped aggregate per entity instead of a query per project. Samples
# and files carry project_code on their own composite key, so neither needs
# to join through Experiment. Projects with zero rows are simply absent from
# the result (the template defaults missing codes to 0).
exp_counts = dict(
db.session.query(Experiment.project_code, func.count())
.group_by(Experiment.project_code).all()
)
sample_counts = dict(
db.session.query(MassSpecSample.project_code, func.count())
.group_by(MassSpecSample.project_code).all()
)
file_counts = dict(
db.session.query(AcquiredFile.project_code, func.count())
.group_by(AcquiredFile.project_code).all()
)
return render_template(
"project/list.html", projects=projects, show_archived=show_archived,
users=users, exp_counts=exp_counts, sample_counts=sample_counts, file_counts=file_counts
)
@app.route("/projects/<code>/toggle-active", methods=["POST"])
def project_toggle_active(code):
project = db.get_or_404(Project, code)
project.active = not project.active
db.session.commit()
if request.accept_mimetypes.accept_json and not request.accept_mimetypes.accept_html:
return {"active": project.active}
show_archived = request.args.get("show_archived", "0")
return redirect(url_for("project_list", show_archived=show_archived))
@app.route("/projects/<code>")
def project_detail(code):
project = db.get_or_404(Project, code)
experiments = (
Experiment.query.filter_by(project_code=code).order_by(Experiment.name).all()
)
contact_user = (
User.query.filter_by(initials=project.user_initials).first()
if project.user_initials
else None
)
contact_name = contact_user.name if contact_user else project.user_initials
samples = (
MassSpecSample.query.join(Experiment)
.options(joinedload(MassSpecSample.experiment))
.filter(Experiment.project_code == code)
.order_by(MassSpecSample.name).all()
)
files = (
AcquiredFile.query.join(MassSpecSample).join(Experiment)
.options(joinedload(AcquiredFile.sample).joinedload(MassSpecSample.experiment).joinedload(Experiment.project))
.filter(Experiment.project_code == code)
.order_by(AcquiredFile.file_date.desc(), AcquiredFile.filename).all()
)
total_size_gb = sum(f.size_bytes or 0 for f in files) / 1e9
chart_rows = (
db.session.query(
AcquiredFile.file_date,
Experiment.code.label("experiment_code"),
func.sum(AcquiredFile.size_bytes).label("total_bytes"),
)
.join(AcquiredFile.sample)
.join(MassSpecSample.experiment)
.filter(Experiment.project_code == code)
.filter(AcquiredFile.file_date.isnot(None))
.group_by(AcquiredFile.file_date, Experiment.code)
.order_by(AcquiredFile.file_date)
.all()
)
chart_data = [
{"date": row.file_date.isoformat(), "experiment": row.experiment_code, "gb": round((row.total_bytes or 0) / 1e9, 4)}
for row in chart_rows
]
users = {u.initials: u.name for u in User.query.all()}
return render_template(
"project/detail.html", project=project, experiments=experiments,
contact_name=contact_name, samples=samples, files=files,
experiment_count=len(experiments), sample_count=len(samples),
file_count=len(files), total_size_gb=total_size_gb, users=users,
chart_data=chart_data,
)
@app.route("/projects/new", methods=["GET", "POST"])
def project_create():
form = ProjectForm()
form.user_initials.choices = _user_initials_choices()
if form.validate_on_submit():
project = Project()
form.populate_obj(project)
db.session.add(project)
if _commit_unique(form, "code", "A project with code"):
flash("Project created.", "success")
return redirect(url_for("project_detail", code=project.code))
return render_template("project/form.html", form=form, project=None)
@app.route("/projects/<code>/edit", methods=["GET", "POST"])
def project_edit(code):
project = db.get_or_404(Project, code)
form = ProjectForm(obj=project)
del form.code
form.user_initials.choices = _user_initials_choices()
if form.validate_on_submit():
form.populate_obj(project)
db.session.commit()
flash("Project updated.", "success")
return redirect(url_for("project_detail", code=project.code))
if request.method == "POST":
flash("Could not save — please check the fields below.", "error")
return render_template("project/form.html", form=form, project=project)
def _user_initials_choices():
users = User.query.filter_by(active=True).order_by(User.name).all()
choices = [("", "— none —")]
choices += [(u.initials, u.name) for u in users if u.initials]
return choices
def _user_name_choices():
users = User.query.filter_by(active=True).order_by(User.name).all()
choices = [("", "— none —")]
choices += [(u.name, u.name) for u in users]
return choices
# ---------------------------------------------------------------------------
# Experiments
# ---------------------------------------------------------------------------
@app.route("/experiments")
def experiment_list():
show_archived = request.args.get("show_archived", "0") == "1"
q = Experiment.query.join(Project).options(joinedload(Experiment.project))
if not show_archived:
q = q.filter(Project.active == True, Experiment.active == True) # noqa: E712
experiments = q.order_by(Experiment.name).all()
users = {u.initials: u.name for u in User.query.all()}
return render_template("experiment/list.html", experiments=experiments, users=users, show_archived=show_archived)
@app.route("/projects/<project_code>/experiments/<code>")
def experiment_detail(project_code, code):
experiment = db.get_or_404(Experiment, (project_code, code))
samples = (
MassSpecSample.query
.filter_by(project_code=project_code, experiment_code=code)
.order_by(MassSpecSample.name).all()
)
files = (
AcquiredFile.query.join(MassSpecSample)
.options(joinedload(AcquiredFile.sample).joinedload(MassSpecSample.experiment).joinedload(Experiment.project))
.filter(MassSpecSample.project_code == project_code, MassSpecSample.experiment_code == code)
.order_by(AcquiredFile.file_date.desc(), AcquiredFile.filename).all()
)
total_size_gb = sum(f.size_bytes or 0 for f in files) / 1e9
users = {u.initials: u.name for u in User.query.all()}
return render_template(
"experiment/detail.html", experiment=experiment, samples=samples, files=files,
sample_count=len(samples), file_count=len(files), total_size_gb=total_size_gb, users=users,
)
@app.route("/projects/<project_code>/experiments/<code>/delete", methods=["POST"])
def experiment_delete(project_code, code):
experiment = db.get_or_404(Experiment, (project_code, code))
if any(s.acquired_files for s in experiment.samples):
flash("Cannot delete an experiment whose samples have acquired files.", "error")
return redirect(url_for("experiment_detail", project_code=project_code, code=code))
for sample in list(experiment.samples):
_delete_sample(sample)
db.session.delete(experiment)
db.session.commit()
flash("Experiment deleted.", "success")
return redirect(url_for("project_detail", code=project_code))
@app.route("/projects/<project_code>/experiments/new", methods=["GET", "POST"])
def experiment_create(project_code):
# The parent project is fixed by the URL (you reach this page from a project),
# so there's no project selector — it's shown read-only on the form.
project = db.get_or_404(Project, project_code)
form = ExperimentForm()
del form.project_code
form.user_initials.choices = _user_initials_choices()
if request.method == "GET":
if project.user_initials:
form.user_initials.data = project.user_initials
form.code.data = _next_experiment_code(project_code)
if form.validate_on_submit():
experiment = Experiment()
form.populate_obj(experiment)
experiment.project_code = project_code
db.session.add(experiment)
if _commit_unique(form, "code", "An experiment with code"):
flash("Experiment created.", "success")
return redirect(url_for("experiment_detail", project_code=experiment.project_code, code=experiment.code))
cancel_url = url_for("project_detail", code=project_code)
return render_template(
"experiment/form.html", form=form, experiment=None, samples=[],
project=project, cancel_url=cancel_url,
)
@app.route("/projects/<project_code>/experiments/<code>/edit", methods=["GET", "POST"])
def experiment_edit(project_code, code):
experiment = db.get_or_404(Experiment, (project_code, code))
samples = (
MassSpecSample.query
.filter_by(project_code=project_code, experiment_code=code)
.order_by(MassSpecSample.name).all()
)
form = ExperimentForm(obj=experiment)
del form.code
# project_code is part of the primary key — re-parenting is not supported.
del form.project_code
form.user_initials.choices = _user_initials_choices()
if form.validate_on_submit():
form.populate_obj(experiment)
db.session.commit()
flash("Experiment updated.", "success")
return redirect(url_for("experiment_detail", project_code=experiment.project_code, code=experiment.code))
if request.method == "POST":
flash("Could not save — please check the fields below.", "error")
return render_template("experiment/form.html", form=form, experiment=experiment, samples=samples)
# ---------------------------------------------------------------------------
# Species
# ---------------------------------------------------------------------------
@app.route("/species")
def species_list():
species_list = Species.query.order_by(Species.species_name).all()
return render_template("species/list.html", species_list=species_list)
@app.route("/species/new", methods=["GET", "POST"])
def species_create():
form = SpeciesForm()
if form.validate_on_submit():
species = Species()
form.populate_obj(species)
db.session.add(species)
db.session.commit()
flash("Species created.", "success")
return redirect(url_for("species_detail", id=species.id))
return render_template("species/form.html", form=form, species=None)
@app.route("/species/<int:id>")
def species_detail(id):
species = db.get_or_404(Species, id)
return render_template("species/detail.html", species=species)
@app.route("/species/<int:id>/edit", methods=["GET", "POST"])
def species_edit(id):
species = db.get_or_404(Species, id)
form = SpeciesForm(obj=species)
if form.validate_on_submit():
form.populate_obj(species)
db.session.commit()
flash("Species updated.", "success")
return redirect(url_for("species_detail", id=species.id))
return render_template("species/form.html", form=form, species=species)
# ---------------------------------------------------------------------------
# Cell Lines
# ---------------------------------------------------------------------------
def _species_choices():
species = Species.query.order_by(Species.species_name).all()
return [(0, "")] + [(s.id, s.species_name) for s in species]
def _virus_multi_choices():
return [(v.id, v.name) for v in Virus.query.order_by(Virus.name).all()]
@app.route("/cell-lines")
def cell_line_list():
cell_lines = CellLine.query.order_by(CellLine.cell_line_name).all()
return render_template("cell_line/list.html", cell_lines=cell_lines)
@app.route("/cell-lines/new", methods=["GET", "POST"])
def cell_line_create():
form = CellLineForm()
form.species_id.choices = _species_choices()
form.virus_ids.choices = _virus_multi_choices()
if form.validate_on_submit():
cl = CellLine()
form.populate_obj(cl)
db.session.add(cl)
# Suppress autoflush so a duplicate Cellosaurus ID surfaces at commit
# (handled by _commit_unique) rather than from this query's autoflush.
with db.session.no_autoflush:
cl.viruses = Virus.query.filter(Virus.id.in_(form.virus_ids.data)).all()
if _commit_unique(form, "cellosaurus_id", "A cell line with Cellosaurus ID"):
flash("Cell line created.", "success")
return redirect(url_for("cell_line_detail", cellosaurus_id=cl.cellosaurus_id))
return render_template("cell_line/form.html", form=form, cell_line=None)
@app.route("/cell-lines/<cellosaurus_id>")
def cell_line_detail(cellosaurus_id):
cl = db.get_or_404(CellLine, cellosaurus_id)
return render_template("cell_line/detail.html", cell_line=cl)
@app.route("/cell-lines/<cellosaurus_id>/edit", methods=["GET", "POST"])
def cell_line_edit(cellosaurus_id):
cl = db.get_or_404(CellLine, cellosaurus_id)
form = CellLineForm(obj=cl)
del form.cellosaurus_id # primary key — not editable after creation
form.species_id.choices = _species_choices()
form.virus_ids.choices = _virus_multi_choices()
if request.method == "GET":
form.virus_ids.data = [v.id for v in cl.viruses]
if form.validate_on_submit():
form.populate_obj(cl)
cl.viruses = Virus.query.filter(Virus.id.in_(form.virus_ids.data)).all()
db.session.commit()
flash("Cell line updated.", "success")
return redirect(url_for("cell_line_detail", cellosaurus_id=cl.cellosaurus_id))
return render_template("cell_line/form.html", form=form, cell_line=cl)
# ---------------------------------------------------------------------------
# Viruses
# ---------------------------------------------------------------------------
@app.route("/viruses")
def virus_list():
viruses = Virus.query.order_by(Virus.name).all()
return render_template("virus/list.html", viruses=viruses)
@app.route("/viruses/new", methods=["GET", "POST"])
def virus_create():
form = VirusForm()
form.species_id.choices = _species_choices()
if form.validate_on_submit():
virus = Virus()
form.populate_obj(virus)
if virus.species_id == 0:
virus.species_id = None
db.session.add(virus)
db.session.commit()
flash("Virus created.", "success")
return redirect(url_for("virus_detail", id=virus.id))
return render_template("virus/form.html", form=form, virus=None)
@app.route("/viruses/<int:id>")
def virus_detail(id):
virus = db.get_or_404(Virus, id)
return render_template("virus/detail.html", virus=virus)
@app.route("/viruses/<int:id>/edit", methods=["GET", "POST"])
def virus_edit(id):
virus = db.get_or_404(Virus, id)
form = VirusForm(obj=virus)
form.species_id.choices = _species_choices()
if request.method == "GET" and not virus.species_id:
form.species_id.data = 0
if form.validate_on_submit():
form.populate_obj(virus)
if virus.species_id == 0:
virus.species_id = None
db.session.commit()
flash("Virus updated.", "success")
return redirect(url_for("virus_detail", id=virus.id))
return render_template("virus/form.html", form=form, virus=virus)
# ---------------------------------------------------------------------------
# Users
# ---------------------------------------------------------------------------
@app.route("/users")
def user_list():
users = User.query.order_by(User.name).all()
return render_template("user/list.html", users=users)
@app.route("/users/new", methods=["GET", "POST"])
def user_create():
form = UserForm()
if form.validate_on_submit():
user = User()
form.populate_obj(user)
db.session.add(user)
if _commit_unique(form, "initials", "A user with initials"):
flash("User created.", "success")
return redirect(url_for("user_detail", initials=user.initials))
return render_template("user/form.html", form=form, user=None)
@app.route("/users/<initials>")
def user_detail(initials):
user = db.get_or_404(User, initials)
return render_template("user/detail.html", user=user)
@app.route("/users/<initials>/edit", methods=["GET", "POST"])
def user_edit(initials):
user = db.get_or_404(User, initials)
form = UserForm(obj=user)
del form.initials # primary key — not editable after creation
if form.validate_on_submit():
form.populate_obj(user)
db.session.commit()
flash("User updated.", "success")
return redirect(url_for("user_detail", initials=user.initials))
return render_template("user/form.html", form=form, user=user)
# ---------------------------------------------------------------------------
# Samples
# ---------------------------------------------------------------------------
def _sample_copy_choices(project_code):
"""Grouped options for the 'copy from existing sample' dropdown.
Returns a list of (group_label, [(token, label), ...]) tuples: the current
project's active samples, followed by the shared FAV templates (always
included, regardless of the FAV project's active flag).
"""
def _options(samples):
return [
(
_sample_token(s.project_code, s.experiment_code, s.code),
f"{s.experiment_code} / {s.code} — {s.name}",
)
for s in samples
]
current = (
MassSpecSample.query.join(Experiment).join(Project)
.filter(Project.active == True) # noqa: E712
.filter(MassSpecSample.project_code == project_code)
.order_by(Experiment.code, MassSpecSample.code)
.all()
)
groups = [("Current project", _options(current))]
# FAV templates: always available, even if the FAV project is archived.
# Skip if the current project IS FAV (its samples are already listed above).
if project_code != FAV_PROJECT_CODE:
fav = (
MassSpecSample.query
.filter(MassSpecSample.project_code == FAV_PROJECT_CODE)
.order_by(MassSpecSample.experiment_code, MassSpecSample.code)
.all()
)
if fav:
groups.append(("FAV templates", _options(fav)))
return groups
def _species_multi_choices():
return [(s.id, s.species_name) for s in Species.query.order_by(Species.species_name).all()]
def _cell_line_multi_choices():
return [
(cl.cellosaurus_id, cl.cell_line_name)
for cl in CellLine.query.order_by(CellLine.cell_line_name).all()
]
def _coerce_select_fields(sample):
"""Convert empty-string values from optional SelectFields to None so they
satisfy the DB CHECK constraints (which allow NULL but not empty string)."""
for field in ("synthetic_peptide", "quantitation_method", "crosslinking_type"):
if getattr(sample, field) == "":
setattr(sample, field, None)
def _nullify_crosslink_fields(sample):
"""Clear crosslink fields when sample is identification type, and vice versa."""
crosslink_fields = [
"crosslinker", "crosslinking_type",
"protein_or_cell_concentration", "protein_or_cell_concentration_unit",
"crosslinker_or_compound_concentration", "crosslinker_or_compound_concentration_unit",
"organic_solvent_concentration", "organic_solvent_concentration_unit",
"reaction_temperature_in_celsius", "reaction_time_in_minutes",
"quenching_reagent", "uv_source", "uv_time_in_seconds",
"uv_wavelength_in_nanometers",
]
identification_fields = ["peptide_level_fraction"]
if sample.crosslinked_sample:
for f in identification_fields:
setattr(sample, f, None)
else:
for f in crosslink_fields:
setattr(sample, f, None)
@app.route("/samples")
def sample_list():
samples = (
MassSpecSample.query.join(Experiment)
.join(Project)
.options(joinedload(MassSpecSample.experiment).joinedload(Experiment.project))
.filter(Project.active == True) # noqa: E712
.order_by(MassSpecSample.name)
.all()
)
users = {u.initials: u.name for u in User.query.all()}
return render_template("sample/list.html", samples=samples, users=users)
@app.route("/projects/<project_code>/experiments/<experiment_code>/samples/new", methods=["GET", "POST"])
def sample_create(project_code, experiment_code):
# The parent experiment is fixed by the URL (you reach this page from an
# experiment), so there's no experiment selector — it's shown read-only.
experiment = db.get_or_404(Experiment, (project_code, experiment_code))
# Optionally pre-fill the form from an existing sample (everything except the
# identity fields code/name and the parent experiment). Only relevant on GET —
# on POST the values come from the submitted form.
copy_source = None
copy_from = request.values.get("copy_from")
if copy_from:
parts = _split_token(copy_from)
if len(parts) == 3:
copy_source = db.session.get(MassSpecSample, tuple(parts))
if request.method == "GET" and copy_source:
form = MassSpecSampleForm(obj=copy_source)
# Identity fields must stay unique — don't copy them.
form.code.data = ""
form.name.data = ""
form.crosslinked_sample.data = bool(copy_source.crosslinked_sample)
form.quantitation.data = bool(copy_source.quantitation)
form.species_ids.data = [s.id for s in copy_source.species_list]
form.cellosaurus_ids.data = [cl.cellosaurus_id for cl in copy_source.cell_lines]
else:
form = MassSpecSampleForm()
del form.experiment_code
form.user_initials.choices = _user_initials_choices()
form.species_ids.choices = _species_multi_choices()
form.cellosaurus_ids.choices = _cell_line_multi_choices()
if request.method == "GET":
if not copy_source and experiment.user_initials:
form.user_initials.data = experiment.user_initials
form.code.data = _next_sample_code(project_code, experiment_code)
if form.validate_on_submit():
is_crosslinked = form.crosslinked_sample.data
if is_crosslinked:
sample = CrosslinkSample()
else:
sample = IdentificationSample()
form.populate_obj(sample)
sample.project_code = project_code
sample.experiment_code = experiment_code
sample.crosslinked_sample = 1 if is_crosslinked else 0
sample.quantitation = 1 if form.quantitation.data else 0
_coerce_select_fields(sample)
_nullify_crosslink_fields(sample)
db.session.add(sample)
# Resolve the many-to-many selections with autoflush suppressed: the
# sample is already pending, so an autoflush here would try to INSERT it
# mid-query and a duplicate code would raise IntegrityError from the
# query (not the commit), escaping _commit_unique below. Deferring the
# flush to commit keeps duplicate handling in one place.
with db.session.no_autoflush:
sample.species_list = Species.query.filter(
Species.id.in_(form.species_ids.data)
).all()
sample.cell_lines = CellLine.query.filter(
CellLine.cellosaurus_id.in_(form.cellosaurus_ids.data)
).all()
if _commit_unique(form, "code", "A sample with code"):
flash("Sample created.", "success")
return redirect(url_for("sample_detail", project_code=sample.project_code, experiment_code=sample.experiment_code, code=sample.code))
cancel_url = url_for("experiment_detail", project_code=project_code, code=experiment_code)
return render_template(
"sample/form.html", form=form, sample=None, experiment=experiment,
copy_samples=_sample_copy_choices(project_code), copy_from=copy_from, cancel_url=cancel_url,
)
@app.route("/projects/<project_code>/experiments/<experiment_code>/samples/<code>")
def sample_detail(project_code, experiment_code, code):
sample = db.get_or_404(MassSpecSample, (project_code, experiment_code, code))
total_size_gb = sum(f.size_bytes or 0 for f in sample.acquired_files) / 1e9
users = {u.initials: u.name for u in User.query.all()}
active_users = User.query.filter_by(active=True).order_by(User.name).all()
return render_template(
"sample/detail.html", sample=sample,
file_count=len(sample.acquired_files), total_size_gb=total_size_gb, users=users,
active_users=active_users,
)
def _delete_sample(sample):
"""Delete a sample plus its queued runs. Caller guarantees no acquired files.
Many-to-many species/cell-line rows are removed automatically by SQLAlchemy."""
for qf in list(sample.queued_files):
db.session.delete(qf)
db.session.delete(sample)
@app.route("/projects/<project_code>/experiments/<experiment_code>/samples/<code>/delete", methods=["POST"])
def sample_delete(project_code, experiment_code, code):
sample = db.get_or_404(MassSpecSample, (project_code, experiment_code, code))
if sample.acquired_files:
flash("Cannot delete a sample that has acquired files.", "error")
return redirect(url_for("sample_detail", project_code=project_code,
experiment_code=experiment_code, code=code))
_delete_sample(sample)
db.session.commit()
flash("Sample deleted.", "success")
return redirect(url_for("experiment_detail", project_code=project_code, code=experiment_code))
# ---------------------------------------------------------------------------
# Queue (queued_file)
# ---------------------------------------------------------------------------
def queued_filename(qf):
"""Build the run filename for a QueuedFile row.
Sample run: {inst}_{YYYYMMDD}-{NNN}_{proj}_{user}_{exp}_{samp}_{postfix}
Blank run: {inst}_{YYYYMMDD}_BLANK-AND-CLEANING
NNN = run_number (sample runs only, blanks excluded) padded to 3 digits;
blanks carry no NNN at all.
file_name_root is the DB-generated part up to and including the '_' before the
postfix, so the full filename is just root + postfix.
"""
root = qf.file_name_root # ends with the '_' separator before the postfix
if qf.sample_code:
return f"{root}{qf.postfix}" if qf.postfix else root[:-1]
# Blank / cleaning run — postfix carries the label.
return f"{root}{qf.postfix or 'BLANK-AND-CLEANING'}"
def _parse_queue_date(date_str):
try:
return datetime.strptime(date_str, "%Y%m%d").date()
except (ValueError, TypeError):
abort(400, "date must be YYYYMMDD")
def _next_daily_counter(instrument, day):
"""Next run-order slot for an instrument on a day (append to the end).
Counts *all* rows for the day, including exported ones, on purpose: clearing
the queue marks rows exported instead of deleting them, so the counter keeps
climbing and never re-uses a run number already burned into an exported CSV.
"""
current_max = (
db.session.query(func.max(QueuedFile.daily_counter))
.filter_by(instrument_initial=instrument, date_queued=day)
.scalar()
)
return (current_max or 0) + 1
def _next_run_number(instrument, day):
"""Next sample run number for an instrument on a day.
Counts only sample runs (``run_number`` is NULL on BLANK-AND-CLEANING rows),
so blanks never consume a run number and the per-day sample numbering stays
contiguous regardless of how many blanks are interleaved.
"""
current_max = (
db.session.query(func.max(QueuedFile.run_number))
.filter_by(instrument_initial=instrument, date_queued=day)
.scalar()
)
return (current_max or 0) + 1
def _append_to_queue(instrument, day, build_rows, attempts=5):
"""Append rows to an instrument's day queue, race-safe.
``build_rows(counter_start, run_start)`` returns the ``QueuedFile`` rows to
insert, numbered from the first free ``daily_counter`` (the PK ordering slot)
and ``run_number`` (the sample run number). Two concurrent requests can read
the same maxima and pick the same slot; the loser's commit then violates the
composite PK. We catch that, roll back, recompute both, and retry the whole
(re-numbered) batch — each attempt commits atomically, so there are never
partial inserts. Recomputing run_start in lockstep with the PK slot keeps
sample run numbers unique even though they aren't themselves part of the PK.
"""
for _ in range(attempts):
rows = build_rows(
_next_daily_counter(instrument, day), _next_run_number(instrument, day)
)
db.session.add_all(rows)
try:
db.session.commit()
return rows
except IntegrityError:
db.session.rollback()
abort(409, "Could not assign a queue slot — please retry.")
def _day_queue_json(day):
"""Snapshot of the queue for one day: tab list + rows grouped by instrument."""
instruments = [
r[0] for r in db.session.query(QueuedFile.instrument_initial)
.filter(QueuedFile.exported.is_(False))
.distinct().order_by(QueuedFile.instrument_initial).all()
]
rows = (
QueuedFile.query.options(joinedload(QueuedFile.sample))
.filter_by(date_queued=day, exported=False)
.order_by(QueuedFile.instrument_initial, QueuedFile.daily_counter)
.all()
)
queues = defaultdict(list)
for qf in rows:
queues[qf.instrument_initial].append({
"daily_counter": qf.daily_counter,
"run_number": qf.run_number,
"filename": queued_filename(qf),
"postfix": qf.postfix,
"user_initials": qf.user_initials,
"project_code": qf.project_code,
"experiment_code": qf.experiment_code,
"sample_code": qf.sample_code,
"sample_url": (
url_for("sample_detail", project_code=qf.project_code,
experiment_code=qf.experiment_code, code=qf.sample_code)
if qf.sample_code else None
),
"is_blank": not qf.sample_code,
})
return {"date": day.isoformat(), "instruments": instruments, "queues": dict(queues)}
def build_day_csv(rows, day):
"""Build a Thermo Xcalibur sequence CSV for one instrument's ordered day queue.
Xcalibur sequence files are plain Windows-ANSI (cp1252) text, not UTF-7 — so
codes, the data path, etc. are written literally. One row per queued run, in
daily_counter order; blanks get a BLANK-AND-CLEANING comment. cp1252 keeps all
ASCII as-is and still handles the occasional µ/°/etc. in a sample name;
anything it can't represent is replaced rather than crashing the export.
"""
path = f"D:\\Data\\{day:%Y}\\{day:%y}{day:%m}\\{day:%y%m%d}"
buf = io.StringIO()
writer = csv.writer(buf, lineterminator="\n")