-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshareseq_mapTF.sh
397 lines (379 loc) · 18.2 KB
/
shareseq_mapTF.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
#!/bin/bash
# Author: Julia Joung <[email protected]>
# Last modified date: 2022/11/28
# Designed for mapping cells with TF ORFs for SHARE-seq using Google Cloud. Based on code from the SHARE-seq manuscript DOI: 10.1016/j.cell.2020.09.056
rawdirs=(201218_SL-NVR_0049_AHMCTNDSXY) #list of NGS run folders
writedir=/mnt/disks/10tb #output directory location
shareseqdir=shareseq #directory with share-seq files
dir=$writedir/201218_SHAREseq_200k_dialout #output directory name
Project=(TFAtlas.200k) #project name and prefix for output files
Type=(RNA)
Species=TFs
Start=Bcl #specifies where to start the pipeline. Bcl or Fastq_Merge or Fastq_SplitLane
Runtype=full #QC or full, QC only analyze 12M reads
Sequencer=Novaseq #Novaseq or Nextseq
cores=40 #number of computing cores
lanes=4 #number of sequencing lanes
myPATH=~/shareseq/
picardPATH=/opt/picard-tools/picard.jar
bowtieGenome=~/shareseq/refGenome/bowtie2/ #build a bowtie2 reference for TFs using a fasta file with all TF barcodes
echo "the number of projects is" ${#Project[@]}
echo "Running $Runtype pipeline"
echo "the name of the species is $Species"
for rawdir in ${rawdirs[@]}; do
if [ ! -d $rawdir ]; then
gsutil cp gs://shareseq_bucket/$rawdir.tgz .
tar xf $rawdir.tgz
rm $rawdir.tgz
fi
done
cd ~/
if [ ! -d ~/$shareseqdir ]; then
gsutil cp gs://shareseq_bucket/$shareseqdir.tgz
tar xf $shareseqdir.tgz
rm $shareseqdir.tgz
fi
shareseqdir=~/$shareseqdir
if [ ! -d $dir ]; then mkdir $dir; fi
if [ ! -d $dir/fastqs ]; then mkdir $dir/fastqs ; fi
if [ ! -d $dir/temp ]; then mkdir $dir/temp ; fi
cp $writedir/shareseq_mapTF.sh $dir/
cd $dir
if [ -f $dir/Run.log ]; then rm $dir/Run.log; fi
if (( $lanes > 1 )); then
for i in $( seq 1 $lanes ); do
Start=Bcl
Name=${Project[0]}.L${i}
echo "the name of the project is $Name"
# if start with bcl file
if [ "$Start" = Bcl ]; then
echo "Bcl2fastq"
for rawdir in ${rawdirs[@]}; do
rawdir=$writedir/$rawdir
if [ -f $rawdir/fastqs/Undetermined_S1_L1_R2_001.fastq.gz ]; then
echo "Found Undetermined_S0_L001_I1_001.fastq.gz, skip Bcl2Fastq"
else
echo "Converting bcl to fastq"
mkdir $rawdir/fastqs/
bcl2fastq -p $cores -R $rawdir --mask-short-adapter-reads 0 -o $rawdir/fastqs/ --create-fastq-for-index-reads 2>>$dir/Run.log
fi
cd $rawdir/fastqs/
mv Undetermined_S0_L00${i}_R1_001.fastq.gz Undetermined_S1_L${i}_R1_001.fastq.gz
mv Undetermined_S0_L00${i}_I1_001.fastq.gz Undetermined_S1_L${i}_R2_001.fastq.gz
rm filler*
done
rawdir=$writedir/${rawdirs[0]}/fastqs
fi
Start=Fastq_Merge
if [ "$Start" = Fastq_SplitLane ] || [ "$Start" = Fastq_Merge ]; then
echo "Skip bcltofastq"
if [ -f $dir/fastqs/filesi1.xls ]; then rm $dir/fastqs/filler*; fi
cd $rawdir
if [ "$Start" = Fastq_SplitLane ]; then
temp=$(ls 1_*.1.1.fastq.gz)
Run=$(echo $temp | sed -e 's/^1_//' | sed -e 's/.1.1.fastq.gz//')
echo "Run number is:" $Run
echo "Spliting to 3 million reads files"
mkdir $dir/smallfastqs/
dosplit(){
fastp -i $3/$1_$4.$1.barcode_1.fastq.gz -o $2/smallfastqs/$4.$1.I1.fastq.gz -S 12000000 --thread 1 -d 4 -A -G -L -Q
fastp -i $3/$1_$4.$1.barcode_2.fastq.gz -o $2/smallfastqs/$4.$1.I2.fastq.gz -S 12000000 --thread 1 -d 4 -A -G -L -Q
fastp -i $3/$1_$4.$1.1.fastq.gz -o $2/smallfastqs/$4.$1.R1.fastq.gz -S 12000000 --thread 1 -d 4 -A -G -L -Q
fastp -i $3/$1_$4.$1.2.fastq.gz -o $2/smallfastqs/$4.$1.R2.fastq.gz -S 12000000 --thread 1 -d 4 -A -G -L -Q
}
export -f dosplit
dosplitQC(){
zcat $3/$1_$4.$1.1.fastq.gz | head -n 12100000 | gzip > $2/temp1.$1.fastq.gz &
zcat $3/$1_$4.$1.2.fastq.gz | head -n 12100000 | gzip > $2/temp2.$1.fastq.gz &
zcat $3/$1_$4.$1.barcode_1.fastq.gz | head -n 12100000 | gzip > $2/temp3.$1.fastq.gz &
zcat $3/$1_$4.$1.barcode_2.fastq.gz | head -n 12100000 | gzip > $2/temp4.$1.fastq.gz &
wait
fastp -i $2/temp1.$1.fastq.gz -o $2/smallfastqs/$4.$1.R1.fastq.gz -S 1000000 --thread 1 -d 4 -A -G -L -Q
fastp -i $2/temp2.$1.fastq.gz -o $2/smallfastqs/$4.$1.R2.fastq.gz -S 1000000 --thread 1 -d 4 -A -G -L -Q
fastp -i $2/temp3.$1.fastq.gz -o $2/smallfastqs/$4.$1.I1.fastq.gz -S 1000000 --thread 1 -d 4 -A -G -L -Q
fastp -i $2/temp4.$1.fastq.gz -o $2/smallfastqs/$4.$1.I2.fastq.gz -S 1000000 --thread 1 -d 4 -A -G -L -Q
}
export -f dosplitQC
if [ -f $dir/smallfastqs/0001.$Run.1.I1.fastq.gz ]; then
echo "Found 0001.Undetermined.1.I1.fastq, skip split fastqs"
else
if [ "$Runtype" = QC ]; then
if [ "$Sequencer" = Novaseq ]; then
parallel --delay 1 --jobs 2 dosplitQC {} $dir $rawdir $Run ::: {1..2}
else
parallel --delay 1 --jobs 4 dosplitQC {} $dir $rawdir $Run ::: {1..4}
fi
# rm $dir/temp*.fastq.gz
elif [ "$Runtype" = full ]; then
if [ "$Sequencer" = Novaseq ]; then
parallel --delay 1 --jobs 4 dosplit {} $dir $rawdir $Run ::: {1..4}
else
parallel --delay 1 --jobs 2 dosplit {} $dir $rawdir $Run ::: {1..2}
fi
else
echo "Unknown sequencer type, exiting" && exit;
fi
fi
elif [ "$Start" = Fastq_Merge ]; then
temp=$(ls *S1_L1_R1_001.fastq.gz)
Run=$(echo $temp | sed -e 's/\_\S1\_\L1\_\R1\_\001.fastq.gz//')
echo "Run number is:" $Run
echo "Spliting to 3 million reads files"
mkdir $dir/smallfastqs/
if [ -f $dir/smallfastqs/0001.$Run.L${i}.R1.fastq.gz ]; then
echo "Found 0001.Undetermined.1.R1.fastq, skip split fastqs"
else
if [ "$Runtype" = QC ]; then
echo "Runing QC pipeline"
zcat $rawdir/"$Run"_S1_R1_001.fastq.gz | head -n 12100000 | sed 's/1:N:0:1/1:N:0:/g' | sed 's/1:N:0:0/1:N:0:/g' | sed 's/^$/N/' | gzip > $dir/temp1.fastq.gz
fastp -i $dir/temp1.fastq.gz -o $dir/smallfastqs/$Run.1.R1.fastq.gz -S 1000000 --thread 1 -d 4 -A -G -L -Q 2>>$dir/split.log
rm $dir/temp*.fastq.gz
elif [ "$Runtype" = full ]; then
echo "Runing full pipeline"
if [ ! -f $dir/temp3.L${i}.fastq.gz ]; then
echo "Modify fastqs"
zcat $rawdir/"$Run"_S1_L${i}_R1_001.fastq.gz | sed 's/1:N:0:1/1:N:0:/g' | sed 's/1:N:0:0/1:N:0:/g' | sed 's/^$/N/' | gzip > $dir/temp1.L${i}.fastq.gz &
zcat $rawdir/"$Run"_S1_L${i}_R2_001.fastq.gz | sed 's/2:N:0:1/1:N:0:/g' | sed 's/1:N:0:0/1:N:0:/g' | sed 's/^$/N/' | gzip > $dir/temp3.L${i}.fastq.gz &
wait
fi
echo "Split fastqs to small files"
fastp -i $dir/temp1.L${i}.fastq.gz -o $dir/smallfastqs/$Run.L${i}.R1.fastq.gz -S 12000000 --thread 1 -d 4 -A -G -L -Q 2>>$dir/split.log &
fastp -i $dir/temp3.L${i}.fastq.gz -o $dir/smallfastqs/$Run.L${i}.I1.fastq.gz -S 12000000 --thread 1 -d 4 -A -G -L -Q 2>>$dir/split.log &
wait
rm $dir/temp*.fastq.gz
else
echo "Unknown sequencer type, exiting" && exit;
fi
fi
fi
if [ -f $dir/fastp.json ]; then rm $dir/fastp.json $dir/fastp.html; fi
ls $dir/smallfastqs | grep L${i}.R1 > $dir/filesr1.xls
ls $dir/smallfastqs | grep L${i}.I1 > $dir/filesi1.xls
cd $dir/
mkdir $dir/Indexed/
if [ -f $dir/Indexed/Sub.0001.$Run.L${i}.R1.fastq.gz ]; then
echo "Found Sub.0001.Undetermined.1.R1.fastq.gz, skip adding index"
else
echo "Adding index to fastqs"
paste filesr1.xls filesi1.xls | awk -v OFS='\t' '{print $1, $2}'> Filelist2.xls
parallel --jobs 4 --colsep '\t' ' if [ -f '$dir'/Indexed/Sub.{1} ]; then echo "found '$dir'/Indexed/Sub.{1}"; \
else python3 '$myPATH'/shareseq_primerTrim_mapTF.py -R1 '$dir'/smallfastqs/{1} \
--out '$dir'/Indexed/Sub.{1} -Index1 '$dir'/smallfastqs/{2}; fi 2>>'$dir'/Run.log' :::: Filelist2.xls
cd $dir/Indexed
cat Sub*L${i}.R1.fastq.gz > $Name.L${i}.R1.trim.fastq.gz
gunzip $Name.L${i}.R1.trim.fastq.gz
mv $Name.L${i}.R1.trim.fastq $dir/fastqs/
rawdir=$dir/Indexed/
fi
fi
if [ ! -d $dir/$Name ]; then mkdir $dir/$Name; fi
cd $dir/$Name
if [ -f $Name.$Species.L${i}.bam ]; then
echo "Found $Name.$Species.bam, skip alignment"
else
(bowtie2 -p $cores --rg-id $Name -x $bowtieGenome/$Species/$Species -U $dir/fastqs/$Name.L${i}.R1.trim.fastq -N 1 -L 24 | \
samtools view -bS - -o $Name.$Species.L${i}.bam) 2>$Name.$Species.L${i}.align.log
fi
if [ -f $Name.$Species.L${i}.st.bam ]; then
echo "Found $Name.$Species.st.bam, skip sorting"
else
java -Xmx24g -Djava.io.tmpdir=$dir/temp/ -jar $picardPATH SortSam SO=coordinate I=$Name.$Species.L${i}.bam O=$Name.$Species.L${i}.st.bam VALIDATION_STRINGENCY=LENIENT TMP_DIR=$dir/temp/ 2>>$dir/Run.log
samtools index -@ $cores $Name.$Species.L${i}.st.bam
fi
# Update RGID's and Headers
if [ -f $Name.$Species.rigid.reheader.L${i}.st.bam.bai ]; then
echo "Found $Name.$Species.rigid.reheader.st.bam, skip update RGID"
else
echo "Update RGID for $Name.$Species.st.bam"
if [ -f $Name.$Species.rigid.L${i}.st.bam ]; then
echo "Found $Name.$Species.rigid.st.bam, skip update RG tag"
else
python3 $myPATH/shareseq_updateRGID_mapTF.py --bam $Name.$Species.L${i}.st.bam --out $Name.$Species.rigid.L${i}.st.bam --err $Name.$Species.discard.L${i}.st.bam --libtype ${Type[$index]}
fi
samtools view -H $Name.$Species.L${i}.st.bam > $Name.$Species.L${i}.st.header.sam
samtools view -@ $cores $Name.$Species.rigid.L${i}.st.bam | cut -f1 | sed 's/_/\t/g' | cut -f2 | sort | uniq | awk -v OFS='\t' '{print "@RG", "ID:"$1, "SM:Barcode"NR}' > header.temp.L${i}.sam
sed -e '/\@RG/r./header.temp.L${i}.sam' $Name.$Species.L${i}.st.header.sam > $Name.$Species.rigid.L${i}.st.header.sam
samtools reheader -P $Name.$Species.rigid.L${i}.st.header.sam $Name.$Species.rigid.L${i}.st.bam > $Name.$Species.rigid.reheader.L${i}.st.bam
samtools index -@ $cores $Name.$Species.rigid.reheader.L${i}.st.bam
rm *rigid.L${i}.st*
rm $Name.$Species.L${i}.st.header.sam header.temp.L${i}.sam
fi
#map cells to TFs
if [ -f $Name.$Species.TFmap.L${i}.csv ]; then
echo "Found $Name.$Species.TFmap.L${i}.csv, skip TFmap"
else
echo "Map cells to TFs"
python3 $myPATH/shareseq_map_cellstoTF.py --bam $Name.$Species.rigid.reheader.L${i}.st.bam --out $Name.$Species.TFmap.L${i}.csv
fi
done
else
Start=Bcl
Name=${Project[0]}
echo "the name of the project is $Name"
if [ "$Start" = Bcl ]; then
echo "Bcl2fastq"
rawdir=$writedir/$rawdir
if [ -f $rawdir/fastqs/Undetermined_S0_R1_001.fastq.gz ] || [ -f $rawdir/fastqs/Undetermined_S1_R1_001.fastq.gz ]; then
echo "Found Undetermined_S0_L001_I1_001.fastq.gz, skip Bcl2Fastq"
else
echo "Converting bcl to fastq"
mkdir $rawdir/fastqs/
bcl2fastq -p $cores -R $rawdir --no-lane-splitting --mask-short-adapter-reads 0 -o $rawdir/fastqs/ --create-fastq-for-index-reads 2>>$dir/Run.log
fi
cd $rawdir/fastqs/
mv Undetermined_S0_R1_001.fastq.gz Undetermined_S1_R1_001.fastq.gz
mv Undetermined_S0_I1_001.fastq.gz Undetermined_S1_R2_001.fastq.gz
rm filler*
if (( ${#rawdirs[@]} > 1 )); then
if [ ! -f $writedir/${rawdirs[0]}/original_fastqs/Original_S1_R2_001.fastq.gz ]; then
mkdir $writedir/${rawdirs[0]}/original_fastqs/
for i in 1 2 3 4; do
cat $writedir/${rawdirs[0]}/fastqs/Undetermined_S1_R${i}_001.fastq.gz $writedir/${rawdirs[1]}/fastqs/Undetermined_S1_R${i}_001.fastq.gz > ${writedir}/${rawdirs[0]}/fastqs/temp${i}.fastq.gz
mv $writedir/${rawdirs[0]}/fastqs/Undetermined_S1_R${i}_001.fastq.gz $writedir/${rawdirs[0]}/original_fastqs/Original_S1_R${i}_001.fastq.gz
mv $writedir/${rawdirs[0]}/fastqs/temp${i}.fastq.gz $writedir/${rawdirs[0]}/fastqs/Undetermined_S1_R${i}_001.fastq.gz
done
fi
fi
rawdir=$writedir/${rawdirs[0]}/fastqs
fi
Start=Fastq_Merge
if [ "$Start" = Fastq_SplitLane ] || [ "$Start" = Fastq_Merge ]; then
echo "Skip bcltofastq"
if [ -f $dir/fastqs/filesi1.xls ]; then rm $dir/fastqs/filler*; fi
cd $rawdir
if [ "$Start" = Fastq_SplitLane ]; then
temp=$(ls 1_*.1.1.fastq.gz)
Run=$(echo $temp | sed -e 's/^1_//' | sed -e 's/.1.1.fastq.gz//')
echo "Run number is:" $Run
echo "Spliting to 3 million reads files"
mkdir $dir/smallfastqs/
dosplit(){
fastp -i $3/$1_$4.$1.barcode_1.fastq.gz -o $2/smallfastqs/$4.$1.I1.fastq.gz -S 12000000 --thread 1 -d 4 -A -G -L -Q
fastp -i $3/$1_$4.$1.barcode_2.fastq.gz -o $2/smallfastqs/$4.$1.I2.fastq.gz -S 12000000 --thread 1 -d 4 -A -G -L -Q
fastp -i $3/$1_$4.$1.1.fastq.gz -o $2/smallfastqs/$4.$1.R1.fastq.gz -S 12000000 --thread 1 -d 4 -A -G -L -Q
fastp -i $3/$1_$4.$1.2.fastq.gz -o $2/smallfastqs/$4.$1.R2.fastq.gz -S 12000000 --thread 1 -d 4 -A -G -L -Q
}
export -f dosplit
dosplitQC(){
zcat $3/$1_$4.$1.1.fastq.gz | head -n 12100000 | gzip > $2/temp1.$1.fastq.gz &
zcat $3/$1_$4.$1.2.fastq.gz | head -n 12100000 | gzip > $2/temp2.$1.fastq.gz &
zcat $3/$1_$4.$1.barcode_1.fastq.gz | head -n 12100000 | gzip > $2/temp3.$1.fastq.gz &
zcat $3/$1_$4.$1.barcode_2.fastq.gz | head -n 12100000 | gzip > $2/temp4.$1.fastq.gz &
wait
fastp -i $2/temp1.$1.fastq.gz -o $2/smallfastqs/$4.$1.R1.fastq.gz -S 1000000 --thread 1 -d 4 -A -G -L -Q
fastp -i $2/temp2.$1.fastq.gz -o $2/smallfastqs/$4.$1.R2.fastq.gz -S 1000000 --thread 1 -d 4 -A -G -L -Q
fastp -i $2/temp3.$1.fastq.gz -o $2/smallfastqs/$4.$1.I1.fastq.gz -S 1000000 --thread 1 -d 4 -A -G -L -Q
fastp -i $2/temp4.$1.fastq.gz -o $2/smallfastqs/$4.$1.I2.fastq.gz -S 1000000 --thread 1 -d 4 -A -G -L -Q
}
export -f dosplitQC
if [ -f $dir/smallfastqs/0001.$Run.1.I1.fastq.gz ]; then
echo "Found 0001.Undetermined.1.I1.fastq, skip split fastqs"
else
if [ "$Runtype" = QC ]; then
if [ "$Sequencer" = Novaseq ]; then
parallel --delay 1 --jobs 2 dosplitQC {} $dir $rawdir $Run ::: {1..2}
else
parallel --delay 1 --jobs 4 dosplitQC {} $dir $rawdir $Run ::: {1..4}
fi
# rm $dir/temp*.fastq.gz
elif [ "$Runtype" = full ]; then
if [ "$Sequencer" = Novaseq ]; then
parallel --delay 1 --jobs 4 dosplit {} $dir $rawdir $Run ::: {1..4}
else
parallel --delay 1 --jobs 2 dosplit {} $dir $rawdir $Run ::: {1..2}
fi
else
echo "Unknown sequencer type, exiting" && exit;
fi
fi
elif [ "$Start" = Fastq_Merge ]; then
temp=$(ls *S1_R1_001.fastq.gz)
Run=$(echo $temp | sed -e 's/\_\S1\_\R1\_\001.fastq.gz//')
echo "Run number is:" $Run
echo "Spliting to 3 million reads files"
mkdir $dir/smallfastqs/
if [ -f $dir/smallfastqs/0001.$Run.1.R1.fastq.gz ]; then
echo "Found 0001.Undetermined.1.R1.fastq, skip split fastqs"
else
if [ "$Runtype" = QC ]; then
echo "Runing QC pipeline"
zcat $rawdir/"$Run"_S1_R1_001.fastq.gz | head -n 12100000 | sed 's/1:N:0:1/1:N:0:/g' | sed 's/1:N:0:0/1:N:0:/g' | sed 's/^$/N/' | gzip > $dir/temp1.fastq.gz
fastp -i $dir/temp1.fastq.gz -o $dir/smallfastqs/$Run.1.R1.fastq.gz -S 1000000 --thread 1 -d 4 -A -G -L -Q 2>>$dir/split.log
rm $dir/temp*.fastq.gz
elif [ "$Runtype" = full ]; then
echo "Runing full pipeline"
if [ ! -f $dir/temp2.fastq.gz ]; then
echo "Modify fastqs"
zcat $rawdir/"$Run"_S1_R1_001.fastq.gz | sed 's/1:N:0:1/1:N:0:/g' | sed 's/1:N:0:0/1:N:0:/g' | sed 's/^$/N/' | gzip > $dir/temp1.fastq.gz &
zcat $rawdir/"$Run"_S1_R2_001.fastq.gz | sed 's/2:N:0:1/1:N:0:/g' | sed 's/1:N:0:0/1:N:0:/g' | sed 's/^$/N/' | gzip > $dir/temp3.fastq.gz &
wait
fi
echo "Split fastqs to small files"
fastp -i $dir/temp1.fastq.gz -o $dir/smallfastqs/$Run.1.R1.fastq.gz -S 12000000 --thread 1 -d 4 -A -G -L -Q 2>>$dir/split.log &
fastp -i $dir/temp3.fastq.gz -o $dir/smallfastqs/$Run.1.I1.fastq.gz -S 12000000 --thread 1 -d 4 -A -G -L -Q 2>>$dir/split.log &
wait
rm $dir/temp*.fastq.gz
else
echo "Unknown sequencer type, exiting" && exit;
fi
fi
fi
if [ -f $dir/fastp.json ]; then rm $dir/fastp.json $dir/fastp.html; fi
ls $dir/smallfastqs | grep R1 > $dir/filesr1.xls
ls $dir/smallfastqs | grep I1 > $dir/filesi1.xls
cd $dir/
mkdir $dir/Indexed/
if [ -f $dir/Indexed/Sub.0001.$Run.1.R1.fastq.gz ]; then
echo "Found Sub.0001.Undetermined.1.R1.fastq.gz, skip adding index"
else
echo "Adding index to fastqs"
paste filesr1.xls filesi1.xls | awk -v OFS='\t' '{print $1, $2}'> Filelist2.xls
parallel --jobs 4 --colsep '\t' ' if [ -f '$dir'/Indexed/Sub.{1} ]; then echo "found '$dir'/Indexed/Sub.{1}"; \
else python3 '$myPATH'/shareseq_primerTrim_mapTF.py -R1 '$dir'/smallfastqs/{1} \
--out '$dir'/Indexed/Sub.{1} -Index1 '$dir'/smallfastqs/{2}; fi 2>>'$dir'/Run.log' :::: Filelist2.xls
cd $dir/Indexed
cat Sub*R1.fastq.gz > $Name.R1.trim.fastq.gz
gunzip $Name.R1.trim.fastq.gz
mv $Name.R1.trim.fastq $dir/fastqs/
rawdir=$dir/Indexed/
fi
fi
if [ ! -d $dir/$Name ]; then mkdir $dir/$Name; fi
cd $dir/$Name
if [ -f $Name.$Species.bam ]; then
echo "Found $Name.$Species.bam, skip alignment"
else
(bowtie2 -p $cores --rg-id $Name \
-x $bowtieGenome/$Species/$Species \
-U $dir/fastqs/$Name.R1.trim.fastq | \
samtools view -bS - -o $Name.$Species.bam) 2>$Name.$Species.align.log
fi
if [ -f $Name.$Species.st.bam ]; then
echo "Found $Name.$Species.st.bam, skip sorting"
else
java -Xmx24g -Djava.io.tmpdir=$dir/temp/ -jar $picardPATH SortSam SO=coordinate I=$Name.$Species.bam O=$Name.$Species.st.bam VALIDATION_STRINGENCY=LENIENT TMP_DIR=$dir/temp/ 2>>$dir/Run.log
samtools index -@ $cores $Name.$Species.st.bam
fi
# Update RGID's and Headers
if [ -f $dir/$Name/$Name.$Species.rigid.reheader.st.bam.bai ]; then
echo "Found $Name.$Species.rigid.reheader.st.bam, skip update RGID"
else
echo "Update RGID for $Name.$Species.st.bam"
if [ -f $Name.$Species.rigid.st.bam ]; then
echo "Found $Name.$Species.rigid.st.bam, skip update RG tag"
else
python3 $myPATH/shareseq_updateRGID_mapTF.py --bam $Name.$Species.st.bam --out $Name.$Species.rigid.st.bam --err $Name.$Species.discard.st.bam --libtype ${Type[$index]}
fi
samtools view -H $Name.$Species.st.bam > $Name.$Species.st.header.sam
samtools view -@ $cores $Name.$Species.rigid.st.bam | cut -f1 | sed 's/_/\t/g' | cut -f2 | sort | uniq | awk -v OFS='\t' '{print "@RG", "ID:"$1, "SM:Barcode"NR}' > header.temp.sam
sed -e '/\@RG/r./header.temp.sam' $Name.$Species.st.header.sam > $Name.$Species.rigid.st.header.sam
samtools reheader -P $Name.$Species.rigid.st.header.sam $Name.$Species.rigid.st.bam > $Name.$Species.rigid.reheader.st.bam
samtools index -@ $cores $Name.$Species.rigid.reheader.st.bam
rm *rigid.st*
rm $Name.$Species.st.header.sam header.temp.sam
fi
#map cells to TFs
echo "Map cells to TFs"
python3 $myPATH/shareseq_map_cellstoTF.py --bam $Name.$Species.rigid.reheader.st.bam --out $Name.$Species.TFmap.csv
fi
exit;