forked from alexjvr1/UKButterflies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01a_parallel_cutadapt_bluecp3.sh
296 lines (249 loc) · 8.31 KB
/
01a_parallel_cutadapt_bluecp3.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
#!/bin/bash
# (c) Romain Villoutreix
# romain.villoutreix@gmail.com
# Last modified: 10/10/2016
# Modified by Alexandra Jansen van Rensburg
# Date: 24/10/2018
# Description:
# Given an input directory with raw fastq files (gzip compression allowed)
# it will run cutadapt in parallel
# Changelog
# !!!! The script is only designed for paired-end reads at the moment !!! try something else at your own risk !!!
# 1.0.1 - AJvR 24/10/2018
# - Change script to make compatible with blue crystal p3 server
# 1.0.2 - AJvR 5/2/2019
# - Remove references to scratch directory. Tmp directory now made in local working directory.
# - write outdir to local dir
VERSION='1.0.1-2018.10.24'
CMD="cutadapt"
# Default values for optional variables
EXTRA=''
EMAIL=''
NCORES=1
HRS=8
MEM=16
#FWADAPT1='AGATCGGAAGAGCACACGTCTGAACTCCAGTC';
#FWADAPT2='CTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT';
#FWADAPT3='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
#RVADAPT1='AGATCGGAAGAGCGTCGTGTAGGGAAAGAGTGT';
#RVADAPT2='CTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT';
#RVADAPT3='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
MINLEN=35;
PHREDSCORE=33;
PHREDQUAL=20;
JOBNAME='Cutadapt';
function author {
echo
echo "#########################################"
echo " $(basename $0)"
echo " version $VERSION"
echo " (c) Romain Villoutreix"
echo " romain.villoutreix@gmail.com"
echo "#########################################"
echo
}
function usage {
echo
echo "Usage:"
echo " $(basename $0)"
echo " !!! cutadapt is processing task in a given order. The cutadapt help order is task order here !!! Not all options have been implemented here, but modifying this script should not be too painful !!!"
echo " -i <input directory> => Folder with fastq.gz files !! only work for paired-end reads at the moment !! Paired files shall have the same name with following extension: *R1.fastq* - *R2.fastq*"
echo " -o <output directory> => Output folder to save trimmed files"
echo " -n <number of processors> => processors per trimming (optional, default=$NCORES)"
echo " -t <allocated time> => Allocated time (in hours) for each analysis (optional: default=$HRS)"
echo " -m <allocated memory> => Allocated memory (in gigabytes) for each analysis (optional: default=$MEM)"
echo " -ph <phred score> => Phred score encoding (either 33 or 64 ; optional: default=phred$PHREDSCORE)"
echo " -fwad1 <1st adaptor sequence to look in forward read/R1> (optional: default=$FWADAPT1)"
echo " -fwad2 <2nd adaptor sequence to look in forward read/R1> (optional: default=$FWADAPT2)"
echo " -fwad3 <3rd adaptor sequence to look in forward read/R1> (optional: default=$FWADAPT3)"
echo " -rvad1 <1st adaptor sequence to look in reverse read/R2> (optional: default=$RVADAPT1)"
echo " -rvad2 <2nd adaptor sequence to look in reverse read/R2> (optional: default=$RVADAPT2)"
echo " -rvad3 <3rd adaptor sequence to look in reverse read/R2> (optional: default=$RVADAPT3)"
echo " -minl <--minimum-length option> => see cutadapt manual (optional: default=$MINLEN)"
echo " -phredq <Quality trimming -q option> => see cutadapt manual (optional: default=$PHREDQUAL)"
echo " -job <name of job> => Name of current job"
echo " -q <queue> => SGE queue: iceberg | popgenom | molecol (optional: default=$QUEUE)"
echo " -e <email> => Notification email address (default=$EMAIL)"
echo " -h => show this help"
echo ""
echo " Example:"
echo " $(basename $0) -i raw_reads -o trimmed_reads -q popgenom"
echo ""
echo ""
exit 0
}
author
if [ "$#" -ge "2" ]; # min 2 args: 1 for -i <input directory>, 1 for -o <output directory>
then
while [ $# -gt 0 ]; do
case "$1" in
-h|-help) usage
;;
-i) shift
INDIR=$(readlink -f $1)
;;
-o) shift
OUTDIR=$(readlink -f $1)
;;
-n)shift
NCORES=$1
;;
-t) shift
HRS=$1
;;
-m) shift
MEM=$1
;;
-ph) shift
PHREDSCORE=$1
;;
-fwad1) shift
FWADAPT1=$1
;;
-fwad2) shift
FWADAPT2=$1
;;
-fwad3) shift
FWADAPT3=$1
;;
-rvad1) shift
RVADAPT1=$1
;;
-rvad2) shift
RVADAPT2=$1
;;
-rvad3) shift
RVADAPT3=$1
;;
-minl) shift
MINLEN=$1
;;
-phredq) shift
PHREDQUAL=$1
;;
-job) shift
JOBNAME=$1
;;
-q) shift
QUEUE=$1
;;
-e) shift
EMAIL=$1
;;
*) echo
echo "ERROR - Invalid option: $1"
echo
usage
;;
esac
shift
done
else
usage
fi
N=$(find $INDIR -maxdepth 1 -name "*R1*.fastq*" | wc -l | cut -f1 -d" ")
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
TMPDIR="parallel_cutadapt_"$TIMESTAMP"_"$RANDOM
INPUT_LOCALDIR=$INDIR/"$TMPDIR""_in"
#if [[ $QUEUE == "popgenom" || $QUEUE == "molecol" ]];
#then
# INPUT_LOCALDIR="/local/tmp/$TMPDIR""_in"
#fi
SMSJOB="$OUTDIR/parallel_cutadapt.$TIMESTAMP.smsjob.sh"
LOG="$OUTDIR/parallel_cutadapt.$TIMESTAMP.smsjob.log"
# Initialize submission script
mkdir -p $OUTDIR
echo '#!/bin/bash' > $SMSJOB
echo ""
echo "Load necessary modules"
echo "module load languages/python-2.7.10"
echo "cutadapt installed locally"
echo ""
# PBS OPTIONS
# -----------------------------------------
echo '#PBS -N '$JOBNAME'' >> $SMSJOB
echo '#PBS -l nodes=1:ppn=1' >> $SMSJOB
echo '#PBS -l mem='$MEM'gb' >> $SMSJOB
echo '#PBS -l walltime='$HRS':00:00' >> $SMSJOB
echo '#PBS -j oe' >> $SMSJOB #concatenates error and output files (with prefix job1)
echo '#PBS -t 1-'$N >> $SMSJOB
echo '#PBS -o '$LOG >> $SMSJOB
# -----------------------------------------
echo ""
echo '# Run in local directory' >> $SMSJOB
echo 'cd $PBS_O_WORKDIR' >> $SMSJOB
echo ''
echo ''
cat >> $SMSJOB <<EOF
INDIR=$INDIR
OUTDIR=$OUTDIR
FQFILES1=(\$INDIR/*R1*.fastq*)
FQFILES2=(\$INDIR/*R2*.fastq*)
INDEX=\$((PBS_ARRAYID-1))
FQ1=\${FQFILES1[\$INDEX]}
FQ2=\${FQFILES2[\$INDEX]}
INPUT_TMPDIR=$INPUT_LOCALDIR
# Create temporary local directories
if [ ! -e \$INPUT_TMPDIR ];
then
mkdir -p \$INPUT_TMPDIR
fi
# copy to local temporary directory
cp \$FQ1 \$INPUT_TMPDIR/
cp \$FQ2 \$INPUT_TMPDIR/
FQ1=\$(basename \$FQ1)
FQ2=\$(basename \$FQ2)
LOG="\$OUTDIR/"\${FQ1%%_R1.*}".log"
echo "cutadapt \$FQ1 \$FQ2 files..." > \$LOG
echo >> \$LOG
echo "CMD: " >> \$LOG
echo "$CMD -a $FWADAPT1 -a $FWADAPT2 -a $FWADAPT3 -A $RVADAPT1 -A $RVADAPT2 -A $RVADAPT3 \\\">> \$LOG
echo "-m $MINLEN -q $PHREDQUAL \\\">> \$LOG
EOF
if [[ $PHREDSCORE == 64 ]];
then
echo 'echo "--quality-base=64 \\\">> \$LOG' >> $SMSJOB;
fi
cat >> $SMSJOB <<EOF
echo "-o \${FQ1%%R1.fastq*}cutadapt_filtered_R1.fastq.gz -p \${FQ2%%R2.fastq*}cutadapt_filtered_R2.fastq.gz \\\">> \$LOG
echo "\$FQ1 \$FQ2 \\\">> \$LOG
echo >> \$LOG
echo "---------------------------------------------------" >> \$LOG
echo >> \$LOG
cd \$INPUT_TMPDIR
EOF
if [[ $PHREDSCORE == 33 ]];
then
echo "$CMD -a $FWADAPT1 -a $FWADAPT2 -a $FWADAPT3 -A $RVADAPT1 -A $RVADAPT2 -A $RVADAPT3 -m $MINLEN -q $PHREDQUAL -o \${FQ1%%R1.fastq*}cutadapt_filtered_R1.fastq.gz -p \${FQ2%%R2.fastq*}cutadapt_filtered_R2.fastq.gz \$FQ1 \$FQ2 >> \$LOG 2>&1" >> $SMSJOB;
fi
if [[ $PHREDSCORE == 64 ]];
then
echo "$CMD -a $FWADAPT1 -a $FWADAPT2 -a $FWADAPT3 -A $RVADAPT1 -A $RVADAPT2 -A $RVADAPT3 -m $MINLEN -q $PHREDQUAL --quality-base=64 -o \${FQ1%%R1.fastq*}cutadapt_filtered_R1.fastq.gz -p \${FQ2%%R2.fastq*}cutadapt_filtered_R2.fastq.gz" >> $SMSJOB;
fi
cat >> $SMSJOB <<EOF
# Copy results back to output directory
# and check all files are good
PAIRED1=\${FQ1%%R1.fastq*}cutadapt_filtered_R1.fastq.gz;
PAIRED2=\${FQ2%%R2.fastq*}cutadapt_filtered_R2.fastq.gz;
#paired 1
while [[ ! -e \$INPUT_TMPDIR/\$PAIRED1 || ! -e \$OUTDIR/\$PAIRED1 || \
"\$(md5sum \$INPUT_TMPDIR/\$PAIRED1 | awk '{print \$1}')" != "\$(md5sum \$OUTDIR/\$PAIRED1 | awk '{print \$1}')" ]];
do
sleep \$((30+\$RANDOM%90)) # wait 30-120 secs to try again
cp \$INPUT_TMPDIR/\$PAIRED1 \$OUTDIR/ >& /dev/null
done
#paired 2
while [[ ! -e \$INPUT_TMPDIR/\$PAIRED2 || ! -e \$OUTDIR/\$PAIRED2 || \
"\$(md5sum \$INPUT_TMPDIR/\$PAIRED2 | awk '{print \$1}')" != "\$(md5sum \$OUTDIR/\$PAIRED2 | awk '{print \$1}')" ]];
do
sleep \$((30+\$RANDOM%90)) # wait 30-120 secs to try again
cp \$INPUT_TMPDIR/\$PAIRED2 \$OUTDIR/ >& /dev/null
done
#remove temporary files
rm -f \$INPUT_TMPDIR/\$PAIRED1 \$INPUT_TMPDIR/\$PAIRED2
EOF
chmod u+x $SMSJOB
echo "Command to submit the job to BlueCrystal p3:"
echo
echo "qsub $SMSJOB"
echo