-
Notifications
You must be signed in to change notification settings - Fork 1
/
events.py
218 lines (173 loc) · 8.2 KB
/
events.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
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
import os
import shutil
import subprocess
import json
import logging
from threading import Thread, Event
from time import sleep
from flask import session
from flask_socketio import emit
from watchdog.observers import Observer
from .. import socketio
from .utils.FASTQFileHandler import FASTQFileHandler
from .utils.tasks import int_download_database
from .utils import LinuxNotification
# Setup logger
logger = logging.getLogger('micas')
fileListenerThread = Thread()
thread_stop_event = Event()
REDIRECTION_EXECUTED = False
# HELPER FUNCTIONS
def run_fastq_watcher(app_loc, minion_loc, stop_event):
"""Starts the FASTQ watcher."""
logger.debug(f"Starting FASTQ watcher on {app_loc}")
event_handler = FASTQFileHandler(app_loc)
observer = Observer()
observer.schedule(event_handler, path=minion_loc, recursive=False)
observer.start()
try:
while not stop_event.is_set():
sleep(1)
except Exception as e:
logger.error(f"Error in FASTQ watcher: {e}")
finally:
observer.stop()
observer.join()
def on_raw_message(message):
"""Handles raw messages for progress updates."""
status = message['status']
if status == "PROGRESS":
percent_done = message['result']['percent-done']
status_message = message['result']['message']
emit('download_database_status', {'percent_done': percent_done, 'status_message': status_message})
if percent_done == 100:
minion = message['result']['minion']
micas_location = message['result']['micas_location']
logger.debug("Starting the MinION Listener")
# start_fastq_file_listener(micas_location, minion)
elif status == "SUCCESS":
minion = message['result']['minion']
micas_location = message['result']['micas_location']
logger.debug(f"MinION Location: {minion}, MICAS Location: {micas_location}")
@socketio.on('connect', namespace="/analysis")
def analysis_connected():
"""Handles new connections to the analysis namespace."""
logger.debug("Unused analysis connection made.")
@socketio.on('disconnect', namespace="/analysis")
def analysis_disconnected():
"""Handles disconnections from the analysis namespace."""
micas_location = session.get('micas_location')
if micas_location:
subprocess.call(['rm', os.path.join(micas_location, 'analysis_busy')])
logger.debug("Disconnected from analysis connection.")
@socketio.on('start_fastq_file_listener')
def start_fastq_file_listener(data):
"""Starts the FASTQ file listener."""
micas_location = os.path.join(os.path.expanduser('~'), '.micas', data['projectId'])
minion_location = data['minion_location']
logger.debug("Request for FASTQ File Listener received.")
global fileListenerThread, thread_stop_event
if fileListenerThread is None or not fileListenerThread.is_alive():
logger.debug("Starting the FASTQ file listener thread.")
thread_stop_event.clear()
fileListenerThread = Thread(target=run_fastq_watcher, args=(micas_location, minion_location, thread_stop_event))
fileListenerThread.daemon = True
fileListenerThread.start()
return {"status": fileListenerThread.is_alive()}
@socketio.on('stop_fastq_file_listener')
def stop_fastq_file_listener(data):
"""Stops the FASTQ file listener."""
logger.debug("Stopping the FASTQ file listener.")
global fileListenerThread, thread_stop_event
if fileListenerThread and fileListenerThread.is_alive():
thread_stop_event.set()
fileListenerThread.join()
return {"status": fileListenerThread.is_alive()}
@socketio.on('check_file_listener_status')
def check_file_listener_status(data):
"""Checks the status of the FASTQ file listener."""
logger.debug("Getting status")
global fileListenerThread
return {"status": fileListenerThread.is_alive() if fileListenerThread else False}
@socketio.on('download_database', namespace="/")
def download_database(dbinfo):
"""Handles the database download process."""
project_id = dbinfo["projectId"]
device = dbinfo["device"]
# Location for the application data directory
micas_location = os.path.join(os.path.expanduser('~'), '.micas', project_id)
# Create or recreate the micas_location directory
if os.path.exists(micas_location):
shutil.rmtree(micas_location)
logger.debug(f"Recreating directory: {micas_location}")
os.makedirs(micas_location, mode=0o777, exist_ok=True)
queries = dbinfo["queries"]
with open(os.path.join(micas_location, 'alertinfo.cfg'), 'w+') as alert_config_file:
json.dump(dbinfo, alert_config_file)
# Emit notification to MinKNOW
alert_str = f"You can find the MICAS alert page for {project_id} at http://localhost:3000/analysis/{project_id}"
LinuxNotification.send_notification(device, alert_str, severity=1)
# Create database directory
logger.debug("Creating database directory.")
os.makedirs(os.path.join(micas_location, 'database'), mode=0o777, exist_ok=True)
# Create minimap2/runs directory
logger.debug("Creating minimap2/runs directory.")
os.makedirs(os.path.join(micas_location, 'minimap2/runs'), mode=0o777, exist_ok=True)
minion = dbinfo['minion']
project_id = dbinfo['projectId']
device = dbinfo['device']
micas_location_database = os.path.join(micas_location, 'database/')
if len(queries) == 0:
logger.debug("Debug: No database queries provided, skipping...")
else:
for i, query in enumerate(queries):
query_file = open(query['file'], 'r')
# Putting all the query sequences in one, input_sequences file.
with open(micas_location_database + 'input_sequences.fa', 'a+') as input_sequences:
input_sequences.write('\n')
# get the fasta header and add it into the alertinfo.cfg file
cmd = "grep '^>' " + query['file']
fasta_header = os.popen(cmd).read().strip().split(">")[1]
# update the alertinfo object to include fasta_header
alertinfo_cfg_file = os.path.join(micas_location, 'alertinfo.cfg')
logger.debug(f"Debug: Alert info file: {alertinfo_cfg_file}")
with open(alertinfo_cfg_file, 'r') as alertinfo_fs:
alertinfo_cfg_obj = json.load(alertinfo_fs)
queries = alertinfo_cfg_obj["queries"]
for _, q in enumerate(queries):
if q["file"] == query["file"]:
alertinfo_cfg_obj["queries"][i]["header"] = fasta_header
alertinfo_cfg_obj['device'] = device
# write the updated object into file
json.dump(alertinfo_cfg_obj, open(alertinfo_cfg_file, 'w'))
# copy the contents of query_file into input_sequences
with open(query['file'], 'rb') as query_file, open(micas_location_database + 'input_sequences.fa',
'ab+') as input_sequences:
shutil.copyfileobj(query_file, input_sequences)
logger.debug(f"Debug: Merged {query['file']} sequence into input_sequences.fa file.")
# Generate database index.
import datetime
now = datetime.datetime.now()
logger.debug("Debug: Building the index.")
dbname = \
micas_location_database + str(now.year) + str(now.month) + str(now.day) + str(now.hour) + str(now.minute) + \
str(now.second) + '.mmi'
input_sequences_path = os.path.join(micas_location_database, 'input_sequences.fa')
index_cmd = [
'minimap2 -x map-ont -d ' + dbname + ' ' + input_sequences_path
]
building_index_output = open(os.path.join(micas_location_database, 'building_index.txt'), 'w+')
try:
build_idx_cmd_output = subprocess.Popen(
index_cmd,
shell=True,
stdout=building_index_output,
stderr=building_index_output
)
build_idx_cmd_output.communicate()
build_idx_cmd_output.wait()
except (OSError, subprocess.CalledProcessError) as exception:
logger.error(str(exception))
return "ER1"
logger.debug("Debug: Database has successfully been downloaded and built.")
return {"minion": minion, "micas_location": micas_location, "device": device}