-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrna.preprocess_seq.sh
312 lines (296 loc) · 7.9 KB
/
scrna.preprocess_seq.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
#!/bin/bash
# @author Slim Fourati
# @version 0.1
# load modules
module load picard/2.11
module load STAR/2.5.3a
# read arguments
while getopts d:g: option
do
case "$option" in
d) dataDir=$OPTARG;;
g) genome=$OPTARG;;
esac
done
# set global variables for the script
bin=/mnt/projects/SOM_PATH_RXS745U/bin
seqDependencies="/mnt/projects/SOM_PATH_RXS745U/genome/$genome"
genomeFasta="$seqDependencies/Sequence/genome.fa"
refFlat="$seqDependencies/Annotation/genes.refFlat"
maxProc=8
# 0. Determine mate length and sample id
flag=true
if $flag
then
currentDate=$(date +"%Y-%m-%d %X")
echo -ne "$currentDate: determining mate length..."
file=$(find $dataDir -name "*_2.fq.gz" | head -n 1)
sampleID=$(echo $file | sed -r 's|.+/([^/]+)_2.fq.gz|\1|g')
mateLength=$(zcat $file | \
head -n 4000 | \
awk 'NR%2==0 {print length($1)}' | \
sort -rn | \
head -n 1)
# echo $mateLength
echo "done"
fi
genomeDir="$seqDependencies/ggOverhang$(($mateLength -1))"
# 1. convert FASTQ to BAM
flag=true
if $flag
then
currentDate=$(date +"%Y-%m-%d %X")
echo -ne "$currentDate: convert fastq to bam..."
java -jar $PICARD FastqToSam \
F1=$dataDir/${sampleID}_1.fq.gz \
F2=$dataDir/${sampleID}_2.fq.gz \
O=$dataDir/${sampleID}.unalign.bam \
SM=$sampleID &>/dev/null
if [ $? != 0 ]
then
echo -ne "error\n unable to convert fastq"
exit 1
fi
# removed unused files
rm $dataDir/${sampleID}_1.fq.gz
rm $dataDir/${sampleID}_2.fq.gz
echo "done"
fi
# 2. tag cell barcodes
flag=true
if $flag
then
currentDate=$(date +"%Y-%m-%d %X")
echo -ne "$currentDate: add cell tag to bam..."
$bin/Drop-seq_tools-1.13/TagBamWithReadSequenceExtended \
INPUT=$dataDir/${sampleID}.unalign.bam \
OUTPUT=$dataDir/${sampleID}.unalign_tag.bam \
SUMMARY=$dataDir/${sampleID}.unalign_tag.summary.txt \
BASE_RANGE=1-12 \
BASE_QUALITY=10 \
BARCODED_READ=1 \
DISCARD_READ=False \
TAG_NAME=XC \
NUM_BASES_BELOW_QUALITY=1 &>/dev/null
if [ $? != 0 ]
then
echo -ne "error\n unable to add cell tag"
exit 1
fi
# remove unused files
rm $dataDir/${sampleID}.unalign.bam
echo "done"
fi
# 3. tag molecular barcodes (UMI)
flag=true
if $flag
then
currentDate=$(date +"%Y-%m-%d %X")
echo -ne "$currentDate: add umi to bam..."
$bin/Drop-seq_tools-1.13/TagBamWithReadSequenceExtended \
INPUT=$dataDir/${sampleID}.unalign_tag.bam \
OUTPUT=$dataDir/${sampleID}.unalign_tag_umi.bam \
SUMMARY=$dataDir/${sampleID}.unalign_tag_umi.summary.txt \
BASE_RANGE=13-20 \
BASE_QUALITY=10 \
BARCODED_READ=1 \
DISCARD_READ=True \
TAG_NAME=XM \
NUM_BASES_BELOW_QUALITY=1 &>/dev/null
if [ $? != 0 ]
then
echo -ne "error\n unable to add umi"
exit 1
fi
# remove unused files
rm $dataDir/${sampleID}.unalign_tag.bam
echo "done"
fi
# 4. filter BAM
flag=true
if $flag
then
currentDate=$(date +"%Y-%m-%d %X")
echo -ne "$currentDate: filter bam based on base quality..."
$bin/Drop-seq_tools-1.13/FilterBAM \
TAG_REJECT=XQ \
INPUT=$dataDir/${sampleID}.unalign_tag_umi.bam \
OUTPUT=$dataDir/${sampleID}.unalign_filter.bam &>/dev/null
if [ $? != 0 ]
then
echo -ne "error\n unable to filter bam"
exit 1
fi
# remove unused files
rm $dataDir/${sampleID}.unalign_tag_umi.bam
echo "done"
fi
# 5. trim 5’ primer sequence
flag=true
if $flag
then
currentDate=$(date +"%Y-%m-%d %X")
echo -ne "$currentDate: triming 5p primer..."
$bin/Drop-seq_tools-1.13/TrimStartingSequence \
INPUT=$dataDir/${sampleID}.unalign_filter.bam \
OUTPUT=$dataDir/${sampleID}.unalign_trim_5p.bam \
OUTPUT_SUMMARY=$dataDir/${sampleID}.adapter_trimming_report.txt \
SEQUENCE=AAGCAGTGGTATCAACGCAGAGTGAATGGG \
MISMATCHES=0 \
NUM_BASES=5 &>/dev/null
if [ $? != 0 ]
then
echo -ne "error\n unable to trim 5p"
exit 1
fi
# remove unused files
rm $dataDir/${sampleID}.unalign_filter.bam
echo "done"
fi
# 6. trim 3’ polyA sequence
flag=true
if $flag
then
currentDate=$(date +"%Y-%m-%d %X")
echo -ne "$currentDate: triming 3p polyA..."
$bin/Drop-seq_tools-1.13/PolyATrimmer \
INPUT=$dataDir/${sampleID}.unalign_trim_5p.bam \
OUTPUT=$dataDir/${sampleID}.unalign_trimmed.bam \
OUTPUT_SUMMARY=$dataDir/${sampleID}.polyA_trimming_report.txt \
MISMATCHES=0 \
NUM_BASES=6 &>/dev/null
if [ $? != 0 ]
then
echo -ne "error\n unable to trim 3p"
exit 1
fi
# remove unused files
rm $dataDir/${sampleID}.unalign_trim_5p.bam
echo "done"
fi
# 7. conver bam to fastq
flag=true
if $flag
then
currentDate=$(date +"%Y-%m-%d %X")
echo -ne "$currentDate: convert bam to fastq..."
java -jar $PICARD SamToFastq \
INPUT=$dataDir/${sampleID}.unalign_trimmed.bam \
FASTQ=$dataDir/${sampleID}.unalign_trimmed.fq &>/dev/null
if [ $? != 0 ]
then
echo -ne "error\n unable to convert bam"
exit 1
fi
echo "done"
fi
# 8. STAR alignment
flag=true
if $flag
then
currentDate=$(date +"%Y-%m-%d %X")
echo -ne "$currentDate: aligning reads..."
STAR --genomeDir $genomeDir \
--readFilesIn $dataDir/${sampleID}.unalign_trimmed.fq \
--genomeLoad LoadAndRemove \
--runThreadN $maxProc \
--outSAMtype SAM \
--outFileNamePrefix $dataDir/${sampleID}_star &>/dev/null
if [ $? != 0 ]
then
echo -ne "error\n unable to aligned"
exit 1
fi
# removed unused files
rm $dataDir/${sampleID}.unalign_trimmed.fq
echo "done"
fi
# 9. sort STAR alignment in queryname order
flag=true
if $flag
then
currentDate=$(date +"%Y-%m-%d %X")
echo -ne "$currentDate: sorting sam..."
java -jar $PICARD SortSam \
I=$dataDir/${sampleID}_starAligned.out.sam \
O=$dataDir/${sampleID}.aligned_sorted.bam \
SO=queryname &>/dev/null
if [ $? != 0 ]
then
echo -ne "error\n unable to sort sam"
exit 1
fi
# removed unused files
rm $dataDir/${sampleID}_starAligned.out.sam
echo "done"
fi
# 10. merge STAR alignment tagged SAM to recover cell/molecular barcodes
flag=true
if $flag
then
currentDate=$(date +"%Y-%m-%d %X")
echo -ne "$currentDate: merging bbam..."
java -jar $PICARD MergeBamAlignment \
REFERENCE_SEQUENCE=$genomeFasta \
UNMAPPED_BAM=$dataDir/${sampleID}.unalign_trimmed.bam \
ALIGNED_BAM=$dataDir/${sampleID}.aligned_sorted.bam \
OUTPUT=$dataDir/${sampleID}.merged.bam \
INCLUDE_SECONDARY_ALIGNMENTS=false \
PAIRED_RUN=false &>/dev/null
if [ $? != 0 ]
then
echo -ne "error\n unable to merge bam"
exit 1
fi
# removed unused files
rm $dataDir/${sampleID}.unalign_trimmed.bam
rm $dataDir/${sampleID}.aligned_sorted.bam
echo "done"
fi
# 11. Add gene/exon and other annotation tags
flag=true
if $flag
then
currentDate=$(date +"%Y-%m-%d %X")
echo -ne "$currentDate: tag exonic reads..."
$bin/Drop-seq_tools-1.13/TagReadWithGeneExon \
I=$dataDir/${sampleID}.merged.bam \
O=$dataDir/${sampleID}.star_gene_exon_tagged.bam \
ANNOTATIONS_FILE=$refFlat \
TAG=GE &>/dev/null
if [ $? != 0 ]
then
echo -ne "error\n unable to tag reads"
exit 1
fi
# remove unused files
rm $dataDir/${sampleID}.merged.bam
echo "done"
fi
# 12. generate Digital Gene Expression
flag=true
if $flag
then
currentDate=$(date +"%Y-%m-%d %X")
echo -ne "$currentDate: generate digital expression..."
java -Xmx32g \
-XX:+UseParallelOldGC \
-XX:ParallelGCThreads=1 \
-XX:GCTimeLimit=50 \
-XX:GCHeapFreeLimit=10 \
-XX:+HeapDumpOnOutOfMemoryError \
-jar $bin/Drop-seq_tools-1.13/jar/dropseq.jar \
DigitalExpression \
I=$dataDir/${sampleID}.star_gene_exon_tagged.bam \
O=$dataDir/${sampleID}.dge.txt.gz \
SUMMARY=$dataDir/${sampleID}.dge.summary.txt \
MIN_NUM_READS_PER_CELL=1 &>/dev/null
if [ $? != 0 ]
then
echo -ne "error\n unable to generate digital expression"
exit 1
fi
# remove unused files
rm $dataDir/${sampleID}.star_gene_exon_tagged.bam
echo "done"
fi