-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnanopolish_processing.sh
executable file
·255 lines (214 loc) · 9.88 KB
/
nanopolish_processing.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
#!/bin/bash
## Processing direct RNA seq by nanopore with Nanopolish
#######################################################################################
### ###
### Copyright (C) 2020 Pawel Krawczyk ([email protected]) ###
### ###
### This program is free software: you can redistribute it and/or modify ###
### it under the terms of the GNU General Public License as published by ###
### the Free Software Foundation, either version 3 of the License, or ###
### (at your option) any later version. ###
### ###
### This program is distributed in the hope that it will be useful, ###
### but WITHOUT ANY WARRANTY; without even the implied warranty of ###
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ###
### GNU General Public License for more details. ###
### ###
### You should have received a copy of the GNU General Public License ###
### along with this program. If not, see <http://www.gnu.org/licenses/>. ###
### ###
#######################################################################################
# define parameters
threads=20
nanopolish_output_folder="nanopolish"
guppy_folder="guppy_out_3.6.0"
merged_reads='merged_reads.fastq'
nanopolish_index=$merged_reads".index"
seqkit_chunks=1
parallel_nanopolish=4
genome='human'
#load all required modules
module load minimap2
module load samtools/1.9
module load nanopolish
module load bedtools
#Get variables from command line
while getopts ":n:t:g:s:o:r:h::" optname
do
case "$optname" in
"h")
echo "Nanopore analysis pipeline"
echo ""
echo "Options :"
echo " -n : sample name (Required)"
echo " -t: threads"
echo " -o: nanopolish output folder"
echo " -g: guppy output folder"
echo " -s: split input into n chunks [provide as an argument to s] using seqkit"
echo " -r: reference to use for Nanopolish [human|mouse|yeast|MHV|arabidopsis|worm|covid|pombe]"
echo " -h : THIS HELP"
echo ""
exit 1
;;
"n")
sample_name=$OPTARG
;;
"s")
seqkit_chunks=$OPTARG
;;
"t")
threads=$OPTARG
;;
"o")
nanopolish_output_folder=$OPTARG
;;
"g")
guppy_folder=$OPTARG
;;
"r")
genome=$OPTARG
;;
"?")
echo "Unknown option $OPTARG"
exit 1
;;
":")
echo "No argument value for option $OPTARG"
exit 1
;;
*)
# Should not occur
echo "Unknown error while processing options"
;;
esac
done
if [ -e $sample_name ]; then
echo "Missing sample name [-n] (required)"
exit 1
fi
# Define references
if [ $genome == 'human' ]; then
reference_fasta="/home/smaegol/data/indexes/Hsapiens/annotation/gencode_32/gencode.v32.transcripts.with_ribosomal_from_full.fasta"
reference_minimap2=$reference_fasta".mmidx"
analysis_suffix="_hsapiens_gencode32_prompts_rna"
elif [ $genome == 'mouse' ]; then
reference_fasta="/home/smaegol/data/indexes/Mus/annotation/gencode.vM22.transcripts.fa"
reference_minimap2=$reference_fasta".mmidx"
analysis_suffix="_mouse_gencode22"
elif [ $genome == 'yeast' ]; then
reference_fasta="/home/smaegol/data/indexes/Scerevisiae/new_annot/proper_one/SacCer3_custom_annotation_ORFs_ncRNA_CUTs_SUTs_XUTs.fasta"
reference_minimap2=$reference_fasta".mmidx"
analysis_suffix="_yeast_new_PK_annot_CUTs_XUTs_ncRNA"
elif [ $genome == 'MHV' ]; then
reference_fasta="/home/smaegol/data/indexes/coronaviruses/MHV/MHV.fasta"
reference_minimap2=$reference_fasta".mmidx"
analysis_suffix="_MHV"
elif [ $genome == 'arabidopsis' ]; then
reference_fasta="/home/smaegol/data/indexes/Athaliana/TAIR10/annotation/Arabidopsis_thaliana.TAIR10.cdna.ncrna.all.fa"
reference_minimap2=$reference_fasta".mmidx"
analysis_suffix="_Athaliana"
elif [ $genome == 'worm' ]; then
reference_fasta="/home/smaegol/data/indexes/Celegans/annotation/Caenorhabditis_elegans.WBcel235.cdna_ncrna.all.fa"
reference_minimap2=$reference_fasta".mmidx"
analysis_suffix="_celegan"
elif [ $genome == 'pombe' ]; then
reference_fasta="/home/smaegol/data/indexes/SPombe/cds_introns_utrs.fa"
reference_minimap2=$reference_fasta".mmidx"
analysis_suffix="_spombe"
elif [ $genome == 'covid' ]; then
reference_fasta="/home/smaegol/data/indexes/coronaviruses/SARS-Cov-2/MT007544.1.fasta"
reference_minimap2=$reference_fasta".mmidx"
analysis_suffix="_sars_cov_2"
else
echo "Unknown reference type provided. Quiting..."
exit 1
fi
current_folder=`pwd`
# Print input parameters
echo -e "\e[1mProvided parameters:\e[0m"
echo -e "sample_name: $sample_name"
echo -e "threads to use: $threads"
echo -e "output folder: $nanopolish_output_folder"
echo -e "guppy output folder: $guppy_folder"
echo -e "current folder: $current_folder"
echo -e "split reads into chunks: $seqkit_chunks"
bam_output=$sample_name'_'$analysis_suffix'.bam'
nanopolish_output=$sample_name'_'$analysis_suffix'_nanopolish.tsv'
if [ ! -d $nanopolish_output_folder ]; then
mkdir $nanopolish_output_folder
fi
# merge reads from guppy output
echo -e "\e[1mMerging reads\e[0m"
if [ -e $nanopolish_output_folder/$merged_reads ]; then
echo -e "Reads seem to be already merged. Skipping..."
else
cat $guppy_folder/*.fastq > $nanopolish_output_folder/$merged_reads
fi
#cd $nanopolish_output_folder
# make symbolic links of workspace and sequencing_summary.txt - required for nanopolish index and polya functions
if [ -e $nanopolish_output_folder/workspace ]; then
echo -e "Workspace is already present in the nanopolish folder"
else
ln -s $guppy_folder/workspace $nanopolish_output_folder/
fi
if [ -e $nanopolish_output_folder/sequencing_summary.txt ]; then
echo -e "Sequencing_summary is already present in the nanopolish folder"
else
ln -s $guppy_folder/sequencing_summary.txt $nanopolish_output_folder/
fi
# Index reads with Nanopolish
cd $nanopolish_output_folder
echo -e "\e[1mIndexing reads with nanopolish\e[0m"
if [ ! -e $nanopolish_index ]; then
nanopolish index -d workspace -s sequencing_summary.txt $merged_reads
else
echo -e "Nanopolish index is already present. Skipping indexing..."
fi
if [ $seqkit_chunks -gt 1 ]; then
# if reads have to be splitted using seqkit
module load seqkit
echo -e "\e[1mSplitting reads into $seqkit_chunks chunks to improve processing time\e[0m"
if [ ! -d $merged_reads".split" ]; then
seqkit split -p $seqkit_chunks $merged_reads
else
number_fastq_files_present=`find merged_reads.fastq.split/ -name "*fastq" | wc -l`
if [ $number_fastq_files_present == $seqkit_chunks ]; then
echo -e "Sequences seem to be already splitted into chunks. Skipping seqkit split part"
else
echo -e "Number of fastq files in $merged_reads.split folder does not equal specified number of chunks ($seqkit_chunks). Deleting and rerunning..."
rm -rf $merged_reads".split"
seqkit split -p $seqkit_chunks $merged_reads
fi
fi
echo -e "\e[1mparallel mapping with minimap2\e[0m"
number_bam_files_present=`find merged_reads.fastq.split/ -name "*$analysis_suffix.bam" | wc -l`
if [ $number_bam_files_present == $seqkit_chunks ]; then
echo -e "$number_bam_files_present bam files already present in the folder. Skipping mapping..."
else
find . -name "*.fastq" | parallel -j 2 minimap2 -ax map-ont -t $threads --secondary=no $reference_minimap2 {} '|' samtools view -b -F 2320 -@ $threads '|' samtools sort -o {}"_$analysis_suffix.bam" -@ $threads
fi
find . -name "*$analysis_suffix.bam" | parallel -j $threads samtools index {}
bedtools bamtobed -cigar -i $merged_reads"_"$analysis_suffix".bam" > "merged_reads.fastq__"$analysis_suffix".bam.bed"
echo -e "\e[1mestimating poly(A) length in parallel using $parallel_nanopolish simultaneous Nanopolish threads\e[0m"
find $merged_reads".split"/ -name "*$analysis_suffix.bam" | parallel -j $parallel_nanopolish nanopolish polya -t $threads -b {} -r $merged_reads -g $reference_fasta '>' {}'_'$analysis_suffix'_nanopolish.tsv'
echo -e "\e[1mFinished parallel Nanopolish processing. Merging predictions\e[0m".
cat */*$analysis_suffix'_nanopolish.tsv' > "tmp."$sample_name"_"$analysis_suffix"_nanopolish.tsv"
head -n 1 "tmp."$sample_name"_"$analysis_suffix"_nanopolish.tsv" > $sample_name"_"$analysis_suffix"_nanopolish.tsv"
tail -n +2 "tmp."$sample_name"_"$analysis_suffix"_nanopolish.tsv" | grep -v "^read" >> $sample_name"_"$analysis_suffix"_nanopolish.tsv"
echo -e "\e[1mFinished...\e[0m"
else
echo -e "\e[1mProcessing single file - no splitting into chunks\e[0m"
echo -e "\e[1mmapping reads with minimap2\e[0m"
if [ ! -e $bam_output ]; then
minimap2 -ax map-ont -t $threads --secondary=no $reference_minimap2 $merged_reads | samtools view -b -@ $threads -F 2320 | samtools sort -@ $threads -o $bam_output
echo -e "\e[1mindexing bam file with samtools\e[0m"
samtools index $bam_output
echo -e "\e[1mProducing bed output\e[0m"
bedtools bamtobed -cigar -i $bam_output > $bam_output".bed"
else
echo -e "Bam file with mapping is already present. Skipping mapping..."
fi
echo -e "\e[1mEstimating polyA length\e[0m"
nanopolish polya -t $threads -g $reference_fasta -r $merged_reads -b $bam_output > $nanopolish_output
fi
echo -e "\e[1mFinished\e[0m"