forked from hasindu2008/f5p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·494 lines (410 loc) · 17.3 KB
/
run.sh
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
#!/bin/bash
#================================================================
# HEADER
#================================================================
#% SYNOPSIS
#+ ${SCRIPT_NAME} -f [format] -m [directory] [options ...]
#%
#% DESCRIPTION
#% Runs realtime (or not) analysis of sequenced genomes
#% given input directory and its expected format.
#%
#% OPTIONS
#% -a, --avail Output available formats
#% -f [format], --format=[format] Follows a specified format of fast5 and fastq files
#%
#% available formats
#% --778 [directory] Old format that's not too bad
#% |-- fast5/
#% |-- [prefix].fast5.tar
#% |-- fastq/
#% |-- fastq_*.[prefix].fastq.gz
#% |-- logs/ (optional - for realistic testing
#% or automatic timeout)
#% |-- sequencing_summary.[prefix].txt.gz
#%
#% --NA [directory] Newer format with terrible folders
#% |-- fast5/
#% |-- [prefix].fast5
#% |-- fastq/
#% |-- [prefix]/
#% |-- [prefix].fastq
#% |-- sequencing_summary.txt (optional -
#% for realistic testing or automatic timeout)
#%
#% --zebra [directory] Newest format
#% |-- fast5/
#% |-- [prefix].fast5
#% |-- fastq/
#% |-- [prefix].fastq
#% |-- sequencing_summary.txt
#%
#% -h, --help Print help message
#% -i, --info Print script information
#% -l [filename], --log=[filename] Specify log filename for logs
#% default log.txt
#%
#% -m [directory], --monitor=[directory] Monitor a specific directory
#% --non-realtime Specify non-realtime analysis
#% -r, --resume Resumes from last processing position
#% --results-dir=[directory] Specify a directory to place results
#% default script location
#% -s [file], --script=[file] Custom script for processing files on the cluster
#% default scripts/fast5_pipeline.sh - Default script which calls minimap, f5c & samtools
#%
#% -t [timeout_format] [time],
#% --timeout [timeout_format]=[time] Exits after a specified time period of no new files
#% default -t -hr 1 - Default timeout of 1 hour
#%
#% timeout formats
#% -s [time], --seconds=[time] Timeout format in seconds
#% -m [time], --minutes=[time] "---------------" minutes
#% -hr [time], --hours=[time] "---------------" hours
#% -a, --automatic Timeout calculated automatically to testing data
#%
#% -8 [directory] [simulate_options],
#% --simul8=[directory] [simulate_options] Simulate sequenced files for testing (or fun!)
#%
#% -y, --yes Say yes to 'Are you sure?' message in advance
#%
#% simulate options
#% --n=[number_of_batches] Stop simulating after a certain number of batches
#% --real Simulate realistically given sequencing summary files
#% --t=[time_between_batches] Simulate batches with a certain time between them
#% default 0s
#%
#% EXAMPLES
#% play and resume
#% ${SCRIPT_NAME} -f [format] -m [directory]
#% ${SCRIPT_NAME} -f [format] -m [directory] -r
#% realtime simulation
#% ${SCRIPT_NAME} -f [format] -m [directory] -8 [directory] --real -t -a
#% non realtime
#% ${SCRIPT_NAME} -f [format] --non-realtime
#%
#================================================================
#- IMPLEMENTATION
#- authors Hasindu GAMAARACHCHI ([email protected]),
#- Sasha JENNER ([email protected])
#- license MIT
#-
#- Copyright (c) 2019 Hasindu Gamaarachchi, 2020 Sasha Jenner
#-
#- Permission is hereby granted, free of charge, to any person obtaining a copy
#- of this software and associated documentation files (the "Software"), to deal
#- in the Software without restriction, including without limitation the rights
#- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#- copies of the Software, and to permit persons to whom the Software is
#- furnished to do so, subject to the following conditions:
#-
#- The above copyright notice and this permission notice shall be included in all
#- copies or substantial portions of the Software.
#-
#- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#- SOFTWARE.
#-
#================================================================
# END_OF_HEADER
#================================================================
#== Necessary variables ==#
SCRIPT_HEADSIZE=$(head -200 ${0} | grep -n "^# END_OF_HEADER" | cut -f1 -d:)
SCRIPT_NAME="$(basename ${0})"
SCRIPT_PATH="$( cd "$(dirname "$0")" ; pwd -P )" # Scripts current path
#== Usage functions ==#
usage() { printf "Usage: "; head -${SCRIPT_HEADSIZE:-99} ${0} | grep -e "^#+" | sed -e "s/^#+[ ]*//g" -e "s/\${SCRIPT_NAME}/${SCRIPT_NAME}/g"; }
usagefull() { head -${SCRIPT_HEADSIZE:-99} ${0} | grep -e "^#[%+-]" | sed -e "s/^#[%+-]//g" -e "s/\${SCRIPT_NAME}/${SCRIPT_NAME}/g"; }
scriptinfo() { head -${SCRIPT_HEADSIZE:-99} ${0} | grep -e "^#-" | sed -e "s/^#-//g" -e "s/\${SCRIPT_NAME}/${SCRIPT_NAME}/g"; }
#== Default variables ==#
# Default script to be copied and run on the worker nodes
PIPELINE_SCRIPT="$SCRIPT_PATH/scripts/fast5_pipeline.sh"
RESULTS_DIR_PATH="$SCRIPT_PATH" # Default location for results
RESULTS_DIR_NAME="" # Default directory name for results
IP_LIST="$SCRIPT_PATH"/data/ip_list.cfg # Define file path of IP list
LOG="$SCRIPT_PATH"/log.txt # Default log filepath
# Set options off by default
resuming=false
simulate=false
real_sim=false
realtime=true
custom_log_specified=false
say_yes=false
# Default timeout of 1 hour
TIME_FACTOR="hr"
TIME_INACTIVE=1
# Simulate variables
NO_BATCHES=-1 # Default to copy all batches
TIME_BETWEEN_BATCHES=0 # Default no time between copying batches
# Assume necessary options not set
format_specified=false
monitor_dir_specified=false
## Handle flags
while [ ! $# -eq 0 ]; do # while there are arguments
case "$1" in
--avail | -a)
echo -e "--778\n--NA\n--zebra"
exit 0
;;
-f)
format_specified=true
case "$2" in
--778 | --NA | --zebra)
FORMAT=$2
;;
*)
echo "Incorrect or no format specified"
usagefull
exit 1
;;
esac
shift
;;
--format=*)
format_specified=true
format="${1#*=}"
case "$format" in
--778 | --NA | --zebra)
FORMAT=$format
;;
*)
echo "Incorrect or no format specified"
usagefull
exit 1
;;
esac
;;
--help | -h)
usagefull
exit 0
;;
--info | -i)
scriptinfo
exit 0
;;
-l)
custom_log_specified=true
LOG=$2
shift
;;
--log=*)
custom_log_specified=true
LOG="${1#*=}"
;;
# Parent directory with fast5 and fastq subdirectories
# which is monitored for new files
-m)
MONITOR_PARENT_DIR=$2
monitor_dir_specified=true
shift
;;
--monitor=*)
MONITOR_PARENT_DIR="${1#*=}"
monitor_dir_specified=true
;;
--non-realtime)
realtime=false
MONITOR_PARENT_DIR="$SCRIPT_PATH" # Place results in current path directory
;;
--results-dir=*)
RESULTS_DIR_PATH="${1#*=}"
RESULTS_DIR_NAME=$(basename "$RESULTS_DIR_PATH")
if ! $custom_log_specified; then # If a custom log hasn't been specified
LOG="$RESULTS_DIR_PATH"/log.txt # Redefine the log filepath
fi
;;
--resume | -r)
resuming=true
;;
-s)
PIPELINE_SCRIPT=$2
shift
;;
--script=*)
PIPELINE_SCRIPT="${1#*=}"
;;
-8)
simulate=true
SIMULATE_FOLDER=$2 # Folder containing dataset to simulate sequencing
while [ ! $# -eq 0 ]; do # while there are arguments
case "$3" in
--n=*)
NO_BATCHES="${3#*=}"
;;
--real)
real_sim=true
;;
--t=*)
TIME_BETWEEN_BATCHES="${3#*=}"
;;
*)
shift
break
;;
esac
shift
done
;;
--simul8=*)
simulate=true
SIMULATE_FOLDER="${1#*=}" # Folder containing dataset to simulate sequencing
while [ ! $# -eq 0 ]; do # while there are arguments
case "$2" in
--n=*)
NO_BATCHES="${2#*=}"
;;
--real)
real_sim=true
;;
--t=*)
TIME_BETWEEN_BATCHES="${2#*=}"
;;
*)
shift
break
;;
esac
shift
done
;;
--timeout | -t)
case "$2" in
-s)
TIME_FACTOR="s"
TIME_INACTIVE=$3
shift
;;
--seconds=*)
TIME_FACTOR="s"
TIME_INACTIVE="${2#*=}"
;;
-m)
TIME_FACTOR="m"
TIME_INACTIVE=$3
shift
;;
--minutes=*)
TIME_FACTOR="m"
TIME_INACTIVE="${2#*=}"
;;
-hr)
TIME_FACTOR="hr"
TIME_INACTIVE=$3
shift
;;
--hours=*)
TIME_FACTOR="hr"
TIME_INACTIVE="${2#*=}"
;;
--automatic | -a)
TIME_FACTOR="s"
if ! $format_specified; then
echo "No format specified before automatic timeout option"
usage
exit 1
elif ! $simulate; then
echo "No simulation directory specified before automatic timeout option"
usage
exit 1
else
MAX_WAIT=$(bash $SCRIPT_PATH/max_time_between_files.sh -f $FORMAT $SIMULATE_FOLDER)
fi
TIME_INACTIVE=$(python -c "print($MAX_WAIT + 600)") # Add buffer of 10 minutes (600s)
;;
*)
echo "Bad timeout option"
usagefull
exit 1
;;
esac
shift
;;
-y | --yes)
say_yes=true
;;
esac
shift
done
# If either format or monitor option not set
if ! ($format_specified && $monitor_dir_specified); then
if ! $format_specified; then echo "No format specified!"; fi
if ! $monitor_dir_specified; then echo "No monitor directory specified!"; fi
usage
exit 1
fi
#== Begin Run ==#
# Warn before cleaning logs
if ! $resuming && ! $say_yes; then # If not resuming
while true; do
read -p "This may overwrite stats from a previous run. Do you wish to continue? (y/n)" response
case $response in
[Yy]* )
make clean && make || exit 1 # Freshly compile necessary programs
cp /dev/null $LOG # Empty log file
break
;;
[Nn]* )
exit 0
;;
* )
echo "Please answer yes or no."
;;
esac
done
fi
# Clean and empty local logs directory
test -d $RESULTS_DIR_PATH/data/logs && rm -r $RESULTS_DIR_PATH/data/logs
mkdir -p $RESULTS_DIR_PATH/data/logs || exit 1
# Create folders to copy the results (SAM files, BAM files, logs and methylation calls)
test -d $MONITOR_PARENT_DIR/sam || mkdir $MONITOR_PARENT_DIR/sam || exit 1
test -d $MONITOR_PARENT_DIR/bam || mkdir $MONITOR_PARENT_DIR/bam || exit 1
test -d $MONITOR_PARENT_DIR/log2 || mkdir $MONITOR_PARENT_DIR/log2 || exit 1
test -d $MONITOR_PARENT_DIR/methylation || mkdir $MONITOR_PARENT_DIR/methylation || exit 1
# Copy the pipeline script to all worker nodes
# (todo: any complications with this and a node already accessing pipeline script?)
ansible all -m copy -a "src=$PIPELINE_SCRIPT dest=/nanopore/bin/fast5_pipeline.sh mode=0755" |& tee -a $LOG
if ! $realtime; then # If non-realtime option set
/usr/bin/time -v "$SCRIPT_PATH"/f5pl "$FORMAT" "$IP_LIST" "$SCRIPT_PATH"/data/file_list.cfg |&
tee $LOG
else # Else assume realtime analysis is desired
if $simulate; then # If the simulation option is on
# Create fast5 and fastq folders if they don't exist
test -d $MONITOR_PARENT_DIR/fast5 || mkdir $MONITOR_PARENT_DIR/fast5 || exit 1
test -d $MONITOR_PARENT_DIR/fastq || mkdir $MONITOR_PARENT_DIR/fastq || exit 1
# Execute simulator in the background giving time for monitor to set up
if $real_sim; then
(sleep 10; bash "$SCRIPT_PATH"/testing/simulator.sh -f $FORMAT -r -n $NO_BATCHES $SIMULATE_FOLDER $MONITOR_PARENT_DIR 2>&1 | tee -a $LOG) &
else
(sleep 10; bash "$SCRIPT_PATH"/testing/simulator.sh -f $FORMAT -n $NO_BATCHES -t $TIME_BETWEEN_BATCHES $SIMULATE_FOLDER $MONITOR_PARENT_DIR 2>&1 | tee -a $LOG) &
fi
fi
# Monitor the new file creation in fast5 folder and execute realtime f5-pipeline script
# Close after timeout met
if $resuming; then # If resuming option set
bash "$SCRIPT_PATH"/monitor/monitor.sh -t -$TIME_FACTOR $TIME_INACTIVE -f -e $MONITOR_PARENT_DIR/fast5/ $MONITOR_PARENT_DIR/fastq/ 2>> $LOG |
bash "$SCRIPT_PATH"/monitor/ensure.sh -r -f $FORMAT --results-dir=$RESULTS_DIR_PATH 2>> $LOG |
/usr/bin/time -v "$SCRIPT_PATH"/f5pl_realtime $FORMAT $IP_LIST $RESULTS_DIR_PATH -r |&
tee -a $LOG
else
bash "$SCRIPT_PATH"/monitor/monitor.sh -t -$TIME_FACTOR $TIME_INACTIVE -f $MONITOR_PARENT_DIR/fast5/ $MONITOR_PARENT_DIR/fastq/ 2>> $LOG |
bash "$SCRIPT_PATH"/monitor/ensure.sh -f $FORMAT 2>> $LOG |
/usr/bin/time -v "$SCRIPT_PATH"/f5pl_realtime $FORMAT $IP_LIST $RESULTS_DIR_PATH |&
tee -a $LOG
fi
fi
echo "[run.sh] handling logs" # testing
mv $RESULTS_DIR_PATH/*.cfg $RESULTS_DIR_PATH/data/logs # Move all config files
# Tar the logs
ansible all -m shell -a "cd /nanopore/scratch/'$RESULTS_DIR_NAME' && tar zcvf logs.tgz *.log"
# Copy log files from each node locally
"$SCRIPT_PATH"/scripts/gather.sh "$IP_LIST" /nanopore/scratch/"$RESULTS_DIR_NAME"/logs.tgz "$RESULTS_DIR_PATH"/data/logs/log tgz
# Copy files to logs folder
cp $LOG "$RESULTS_DIR_PATH"/data/logs/ # Copy log file
cp $0 "$RESULTS_DIR_PATH"/data/logs/ # Copy current script
cp $PIPELINE_SCRIPT "$RESULTS_DIR_PATH"/data/logs/ # Copy pipeline script
bash "$SCRIPT_PATH"/scripts/failed_device_logs.sh "$RESULTS_DIR_PATH" # Get the logs of the files where the pipeline crashed
cp -r "$RESULTS_DIR_PATH"/data "$MONITOR_PARENT_DIR"/f5pmaster # Copy entire data folder to local f5pmaster folder
echo "[run.sh] exiting" # testing