forked from hasindu2008/f5p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast5_pipeline.sh
304 lines (244 loc) · 9.94 KB
/
fast5_pipeline.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
#!/bin/bash
# @author: Hasindu Gamaarachchi ([email protected])
# @coauthor: Sasha Jenner ([email protected])
#
# MIT License
#
# 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.
###############################################################################
USAGE="Usage: $0 -f [format] [options ...] [fast5_filepath]"
: ${2?$USAGE} # require at least 2 args else give usage message
HELP=$"Flags:
-f [format], --format=[format] Follows a specified format of fast5 and fastq files
--778 [directory]
|-- fast5/
|-- <prefix>.fast5.tar
|-- fastq/
|-- fastq_*.<prefix>.fastq.gz
--NA [directory]
|-- fast5/
|-- <prefix>.fast5
|-- fastq/
|-- <prefix>/
|-- <prefix>.fastq
--zebra [directory] Newest format
|-- fast5/
|-- [prefix].fast5
|-- fastq/
|-- [prefix].fastq
--results-dir=[directory] Directory to place results
-h, --help Help message"
# Assume necessary options not specified
format_specified=false
results_dir_name_specified=false
## Handle flags
while [ ! $# -eq 0 ]; do # while there are arguments
case "$1" in
-f)
format_specified=true
case "$2" in
--778 | --NA | --zebra)
FORMAT=$2
;;
*)
echo "Incorrect or no format specified"
echo $USAGE
echo "$HELP"
exit 1
;;
esac
shift
;;
--format=*)
format_specified=true
format="${1#*=}"
case "$format" in
--778 | --NA | --zebra)
FORMAT=$format
;;
*)
echo "Incorrect or no format specified"
echo $USAGE
echo "$HELP"
exit 1
;;
esac
;;
--help | -h)
echo $USAGE
echo "$HELP"
exit 0
;;
--results-dir=*)
results_dir_name_specified=true
RESULTS_DIR="${1#*=}"
RESULTS_DIR_NAME=$(basename "$RESULTS_DIR")
;;
*)
FILE=$1
;;
esac
shift
done
if ! ($format_specified && $results_dir_name_specified); then # exit if no format specified
if ! $format_specified; then echo "No format specified!"; fi
if ! $results_dir_name_specified; then echo "No results directory name specified!"; fi
echo "$USAGE"
echo "$HELP"
exit 1
fi
## Changeable definitions
# program paths
MINIMAP=/nanopore/bin/minimap2-arm
METHCALL=/nanopore/bin/f5c # or nanopolish
SAMTOOLS=/nanopore/bin/samtools
if [ "$FORMAT" = "--zebra" ]; then # Zebrafish reference genome
REF_FA=/mnt/zebrafish/zebrafish_genome.fa # reference fasta for methylation call
REF_IDX=/mnt/zebrafish/zebrafish_genome.idx # reference index for minimap2
else # Human reference genome
REF_FA=/nanopore/reference/hg38noAlt.fa # reference fasta for methylation call
REF_IDX=/nanopore/reference/hg38noAlt.idx # reference index for minimap2
fi
# temporary space on the local storage or the network mount
SCRATCH=/nanopore/scratch/"$RESULTS_DIR_NAME"
test -d "$SCRATCH" || mkdir "$SCRATCH" # make temporary directory if non-existent
###############################################################################
if [ "$FORMAT" = "--778" ]; then
F5_TAR_FILEPATH=$FILE # first argument
F5_DIR=${F5_TAR_FILEPATH%/*} # strip filename from .fast5.tar filepath
PARENT_DIR=${F5_DIR%/*} # get folder one heirarchy higher
# name of the .fast5.tar file (strip the path and get only the name with extension)
F5_TAR_FILENAME=$(basename $F5_TAR_FILEPATH)
# name of the .fast5.tar file without extensions
F5_PREFIX=${F5_TAR_FILENAME%%.*}
# derive the locations of input and output files
FQ_GZ_FILEPATH=$PARENT_DIR/fastq/fastq_*.$F5_PREFIX.fastq.gz
SAM_DIR=$PARENT_DIR/sam
BAM_DIR=$PARENT_DIR/bam
METH_DIR=$PARENT_DIR/methylation
LOG_DIR=$PARENT_DIR/log2
# derive the locations of temporary files
F5_DIR_LOCAL=$SCRATCH/$F5_PREFIX
FQ_LOCAL=$SCRATCH/$F5_PREFIX.fastq
SAM_LOCAL=$SCRATCH/$F5_PREFIX.sam
BAM_LOCAL=$SCRATCH/$F5_PREFIX.bam
METH_LOCAL=$SCRATCH/$F5_PREFIX.tsv
LOG_LOCAL=$SCRATCH/$F5_PREFIX.log
MINIMAP_LOCAL=$SCRATCH/$F5_PREFIX.minimap
test -d $F5_DIR_LOCAL && rm -rf $F5_DIR_LOCAL # remove local fast5 directory if it exists
mkdir -p $F5_DIR_LOCAL # make local fast5 directory and create parent directories if needed
tar -xf $F5_TAR_FILEPATH -C $F5_DIR_LOCAL # untar fast5 file into local fast5 directory
gunzip -dc < $FQ_GZ_FILEPATH > $FQ_LOCAL # unzip fastq file locally
elif [ "$FORMAT" = "--NA" ]; then
F5_FILEPATH=$FILE # first argument
F5_DIR=${F5_FILEPATH%/*} # strip filename from .fast5 filepath
PARENT_DIR=${F5_DIR%/*} # get folder one heirarchy higher
# name of the .fast5 file (strip the path and get only the name with extension)
F5_FILENAME=$(basename $F5_FILEPATH)
# name of the .fast5 file without the extension
F5_PREFIX=${F5_FILENAME%.*}
# derive the locations of input and output files
FQ_FILEPATH=$PARENT_DIR/fastq/$F5_PREFIX/$F5_PREFIX.fastq
SAM_DIR=$PARENT_DIR/sam
BAM_DIR=$PARENT_DIR/bam
METH_DIR=$PARENT_DIR/methylation
LOG_DIR=$PARENT_DIR/log2
# derive the locations of temporary files
F5_DIR_LOCAL=$SCRATCH/$F5_PREFIX
FQ_LOCAL=$SCRATCH/$F5_PREFIX.fastq
SAM_LOCAL=$SCRATCH/$F5_PREFIX.sam
BAM_LOCAL=$SCRATCH/$F5_PREFIX.bam
METH_LOCAL=$SCRATCH/$F5_PREFIX.tsv
LOG_LOCAL=$SCRATCH/$F5_PREFIX.log
MINIMAP_LOCAL=$SCRATCH/$F5_PREFIX.minimap
test -d $F5_DIR_LOCAL && rm -rf $F5_DIR_LOCAL # remove local fast5 directory if it exists
mkdir -p $F5_DIR_LOCAL # make local fast5 directory and create parent directories if needed
cp $F5_FILEPATH $F5_DIR_LOCAL # copy fast5 file into local fast5 directory
cat $FQ_FILEPATH > $FQ_LOCAL
elif [ "$FORMAT" = "--zebra" ]; then
F5_FILEPATH=$FILE # first argument
F5_DIR=${F5_FILEPATH%/*} # strip filename from .fast5 filepath
PARENT_DIR=${F5_DIR%/*} # get folder one heirarchy higher
# name of the .fast5 file (strip the path and get only the name with extension)
F5_FILENAME=$(basename $F5_FILEPATH)
# name of the .fast5 file without the extension
F5_PREFIX=${F5_FILENAME%.*}
# derive the locations of input and output files
FQ_FILEPATH=$PARENT_DIR/fastq/$F5_PREFIX.fastq
SAM_DIR=$PARENT_DIR/sam
BAM_DIR=$PARENT_DIR/bam
METH_DIR=$PARENT_DIR/methylation
LOG_DIR=$PARENT_DIR/log2
# derive the locations of temporary files
F5_DIR_LOCAL=$SCRATCH/$F5_PREFIX
FQ_LOCAL=$SCRATCH/$F5_PREFIX.fastq
SAM_LOCAL=$SCRATCH/$F5_PREFIX.sam
BAM_LOCAL=$SCRATCH/$F5_PREFIX.bam
METH_LOCAL=$SCRATCH/$F5_PREFIX.tsv
LOG_LOCAL=$SCRATCH/$F5_PREFIX.log
MINIMAP_LOCAL=$SCRATCH/$F5_PREFIX.minimap
test -d $F5_DIR_LOCAL && rm -rf $F5_DIR_LOCAL # remove local fast5 directory if it exists
mkdir -p $F5_DIR_LOCAL # make local fast5 directory and create parent directories if needed
cp $F5_FILEPATH $F5_DIR_LOCAL # copy fast5 file into local fast5 directory
cat $FQ_FILEPATH > $FQ_LOCAL
fi
exit_status=0 # assume success
# index
/usr/bin/time -v $METHCALL index -d $F5_DIR_LOCAL $FQ_LOCAL 2> $LOG_LOCAL
if [ $? -ne 0 ]; then
exit_status=1
echo "index failed" >> $LOG_LOCAL
fi
# minimap
/usr/bin/time -v $MINIMAP -x map-ont -a -t4 -K5M --secondary=no --multi-prefix=$MINIMAP_LOCAL $REF_IDX $FQ_LOCAL > $SAM_LOCAL 2>> $LOG_LOCAL
if [ $? -ne 0 ]; then
exit_status=1
echo "minimap failed" >> $LOG_LOCAL
fi
# sorting
/usr/bin/time -v $SAMTOOLS sort -@3 $SAM_LOCAL > $BAM_LOCAL 2>> $LOG_LOCAL
if [ $? -ne 0 ]; then
exit_status=1
echo "samtools sorting failed" >> $LOG_LOCAL
fi
/usr/bin/time -v $SAMTOOLS index $BAM_LOCAL 2>> $LOG_LOCAL
if [ $? -ne 0 ]; then
exit_status=1
echo "samtools index failed" >> $LOG_LOCAL
fi
# methylation
/usr/bin/time -v $METHCALL call-methylation -t 4 -r $FQ_LOCAL -g $REF_FA -b $BAM_LOCAL -K 256 > $METH_LOCAL 2>> $LOG_LOCAL
if [ $? -ne 0 ]; then
exit_status=1
echo "methylation failed" >> $LOG_LOCAL
fi
# copy results to the correct place and create directories first if they do not exist
mkdir -p $METH_DIR && cp $METH_LOCAL "$_"
mkdir -p $SAM_DIR && cp $SAM_LOCAL "$_"
mkdir -p $BAM_DIR && cp $BAM_LOCAL "$_"
mkdir -p $LOG_DIR && cp $LOG_LOCAL "$_"
# remove the rest
rm -rf $F5_DIR_LOCAL # remove all from the local fast5 directory
# remove all fastq files
rm -f $FQ_LOCAL $FQ_LOCAL.index $FQ_LOCAL.index.fai $FQ_LOCAL.index.gzi $FQ_LOCAL.index.readdb
rm -f $SAM_LOCAL $BAM_LOCAL $BAM_LOCAL.bai $METH_LOCAL # remove SAM, BAM and methylation files
echo $exit_status >> $LOG_LOCAL
exit $exit_status # return the exit status