-
Notifications
You must be signed in to change notification settings - Fork 24
/
api.py
161 lines (109 loc) · 4.43 KB
/
api.py
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
"""
Provides request handlers for managing and viewing analyses.
"""
import os
import aiojobs.aiohttp
import virtool.analyses.format
import virtool.analyses.utils
import virtool.bio
import virtool.analyses.db
import virtool.samples.db
import virtool.errors
import virtool.http.routes
import virtool.samples.utils
import virtool.utils
from virtool.api import bad_request, conflict, insufficient_rights, json_response, no_content, not_found
routes = virtool.http.routes.Routes()
@routes.get("/api/analyses/{analysis_id}")
async def get(req):
"""
Get a complete analysis document.
"""
db = req.app["db"]
analysis_id = req.match_info["analysis_id"]
document = await db.analyses.find_one(analysis_id)
if document is None:
return not_found()
sample = await db.samples.find_one({"_id": document["sample"]["id"]}, {"quality": False})
if not sample:
return bad_request("Parent sample does not exist")
read, _ = virtool.samples.utils.get_sample_rights(sample, req["client"])
if not read:
return insufficient_rights()
if document["ready"]:
document = await virtool.analyses.format.format_analysis(db, req.app["settings"], document)
document["subtraction"] = {
"id": sample["subtraction"]["id"]
}
return json_response(virtool.utils.base_processor(document))
@routes.delete("/api/analyses/{analysis_id}")
async def remove(req):
"""
Remove an analysis document by its id.
"""
db = req.app["db"]
analysis_id = req.match_info["analysis_id"]
document = await db.analyses.find_one({"_id": analysis_id}, ["job", "ready", "sample"])
if not document:
return not_found()
sample_id = document["sample"]["id"]
sample = await db.samples.find_one({"_id": sample_id}, virtool.samples.db.PROJECTION)
if not sample:
return bad_request("Parent sample does not exist")
read, write = virtool.samples.utils.get_sample_rights(sample, req["client"])
if not read or not write:
return insufficient_rights()
if not document["ready"]:
return conflict("Analysis is still running")
await db.analyses.delete_one({"_id": analysis_id})
path = os.path.join(req.app["settings"]["data_path"], "samples", sample_id, "analysis", analysis_id)
await req.app["run_in_thread"](virtool.utils.rm, path, True)
await virtool.samples.db.recalculate_algorithm_tags(db, sample_id)
return no_content()
@routes.put("/api/analyses/{analysis_id}/{sequence_index}/blast")
async def blast(req):
"""
BLAST a contig sequence that is part of a NuVs result record. The resulting BLAST data will be attached to that
sequence.
"""
db = req.app["db"]
settings = req.app["settings"]
analysis_id = req.match_info["analysis_id"]
sequence_index = int(req.match_info["sequence_index"])
document = await db.analyses.find_one({"_id": analysis_id}, ["ready", "algorithm", "results", "sample"])
if not document:
return not_found("Analysis not found")
if document["algorithm"] != "nuvs":
return conflict("Not a NuVs analysis")
if not document["ready"]:
return conflict("Analysis is still running")
sequence = virtool.analyses.utils.find_nuvs_sequence_by_index(document, sequence_index)
if sequence is None:
return not_found("Sequence not found")
sample = await db.samples.find_one({"_id": document["sample"]["id"]}, virtool.samples.db.PROJECTION)
if not sample:
return bad_request("Parent sample does not exist")
_, write = virtool.samples.utils.get_sample_rights(sample, req["client"])
if not write:
return insufficient_rights()
# Start a BLAST at NCBI with the specified sequence. Return a RID that identifies the BLAST run.
rid, _ = await virtool.bio.initialize_ncbi_blast(req.app["settings"], sequence)
blast_data, document = await virtool.analyses.db.update_nuvs_blast(
db,
settings,
analysis_id,
sequence_index,
rid
)
# Wait on BLAST request as a Task until the it completes on NCBI. At that point the sequence in the DB will be
# updated with the BLAST result.
await aiojobs.aiohttp.spawn(req, virtool.bio.wait_for_blast_result(
req.app,
analysis_id,
sequence_index,
rid
))
headers = {
"Location": f"/api/analyses/{analysis_id}/{sequence_index}/blast"
}
return json_response(blast_data, headers=headers, status=201)