0
|
1 #!/bin/bash
|
|
2 ########################################
|
|
3 #### Peak finding Pipeline for NGS Data
|
|
4 #### Bash script
|
|
5 ########################################
|
|
6
|
|
7 export LC_ALL=en_US.UTF-8
|
|
8
|
|
9 ##Finding out the path
|
|
10 sPath="`dirname \"$0\"`"
|
|
11 sPath="`( cd \"$sPath\" && pwd )`"
|
|
12
|
|
13
|
|
14
|
|
15 usage()
|
|
16 {
|
|
17 cat << EOF
|
|
18 Welcome to JAMM v1.0.6rev2 (GNU GPLv3). Copyright (C) 2014 Mahmoud Ibrahim.
|
|
19
|
|
20 This program comes with ABSOLUTELY NO WARRANTY; for details visit http://www.gnu.org/licenses/gpl.html. This is free software, and you are welcome to redistribute it under certain conditions; visit http://www.gnu.org/licenses/gpl.html for details.
|
|
21
|
|
22 OPTIONS:
|
|
23 -s directory containing Sample files (required)
|
|
24 -g Genome size file (required)
|
|
25 -o Output directory (required)
|
|
26 -c directory containing input or Control files
|
|
27 -m Mode, normal or narrow (default: normal)
|
|
28 -r Resolution, peak or region (default: peak)
|
|
29 -f Fragment length(s) (default: estimated)
|
|
30 -w minimum Window size (default: 2 --- Note: this means minimum_window_size = bin_size x the_value_of_-w)
|
|
31 -p Number of processors used by R scripts (default: 1)
|
|
32 -b Bin Size (default: estimated)
|
|
33 -t Type, single or paired (default: single, requires BED files. paired requires BEDPE files)
|
|
34 -e window Enrichment cutoff, auto or any numeric value (default: 1 --- Set this to auto to estimate the appropriate window enrichment cutoff)
|
|
35 EOF
|
|
36 }
|
|
37
|
|
38
|
|
39 # =========================
|
|
40 # Process Input parameters
|
|
41 # =========================
|
|
42
|
|
43
|
|
44 #Defaults -- Change those if you want
|
|
45 mode="normal"
|
|
46 resol="peak"
|
|
47 cores="1"
|
|
48 window="2"
|
|
49 type="single"
|
|
50 windowe="1"
|
|
51
|
|
52
|
|
53 #Defaults -- Do not change
|
|
54 sdir=""
|
|
55 gsize=""
|
|
56 out=""
|
|
57 binsize="ns"
|
|
58 fraglen="ns"
|
|
59 ran=$RANDOM
|
|
60 wdir=$(mktemp -d)
|
|
61
|
|
62 while getopts "s:g:o:c:m:r:f:p:w:b:t:e:" OPTION
|
|
63 do
|
|
64 case $OPTION in
|
|
65 s) sdir=$OPTARG
|
|
66 ;;
|
|
67 g) gsize=$OPTARG
|
|
68 ;;
|
|
69 o) out=$OPTARG
|
|
70 ;;
|
|
71 c) bdir=$OPTARG
|
|
72 ;;
|
|
73 m) mode=$OPTARG
|
|
74 ;;
|
|
75 r) resol=$OPTARG
|
|
76 ;;
|
|
77 f) fraglen=$OPTARG
|
|
78 ;;
|
|
79 p) cores=$OPTARG
|
|
80 ;;
|
|
81 w) window=$OPTARG
|
|
82 ;;
|
|
83 b) binsize=$OPTARG
|
|
84 ;;
|
|
85 t) type=$OPTARG
|
|
86 ;;
|
|
87 e) windowe=$OPTARG
|
|
88 ;;
|
|
89 ?)
|
|
90 usage
|
|
91 exit
|
|
92 ;;
|
|
93 esac
|
|
94 done
|
|
95 if [ "$mode" == "normal" ]; then
|
|
96 clustno="2"
|
|
97 fi
|
|
98 if [ "$mode" == "narrow" ]; then
|
|
99 clustno="3"
|
|
100 fi
|
|
101
|
|
102 if [[ -z $sdir ]] || [[ -z $gsize ]] || [[ -z $out ]]
|
|
103 then
|
|
104 usage
|
|
105 exit 1
|
|
106 fi
|
|
107 #=======================> DONE!
|
|
108
|
|
109
|
|
110
|
|
111 # =============================
|
|
112 # Step One: Initial Processing
|
|
113 # =============================
|
|
114 printf "\n\n========================================\nStarted JAMM Pipeline v1.0.6rev2...Hang on!\n========================================\n\n"
|
|
115
|
|
116 if [ ! -d "$wdir" ]; then
|
|
117 mkdir $wdir #make working directory
|
|
118 fi
|
|
119 if [ ! -d "$out" ]; then
|
|
120 mkdir $out #make output directory
|
|
121 fi
|
|
122 mkdir $wdir/bkgd.$ran/ #directory to store background files
|
|
123 mkdir $wdir/sizes.$ran/ #chromosomes and sizes
|
|
124 mkdir $wdir/samples.$ran/ #store sample files
|
|
125
|
|
126 dupnum=$(ls -1 $sdir | wc -l) #count how many sample files
|
|
127
|
|
128
|
|
129 #separate chromosome sizes
|
|
130 printf "Loading genome size file..."
|
|
131 ext="$wdir/sizes.$ran/"
|
|
132 awk -v ext="$ext" '{ print >> ext"/size." $1 ".bed" }' $gsize
|
|
133 printf "Done!\n"
|
|
134
|
|
135
|
|
136 printf "Processing sample files..."
|
|
137 #load each chromosome from each sample file
|
|
138 for i in $sdir/*.bed; do
|
|
139 samplefile=$(basename $i)
|
|
140 for f in $wdir/sizes.$ran/*; do
|
|
141 sizefile=$(basename $f)
|
|
142 chr=$(echo $sizefile | awk -F"." '{print $2}' | awk -F"." '{print $1}');
|
|
143 awk -v chr="$chr" -v ext="$wdir/samples.$ran/" -v samplefile="$samplefile" -F"\t" '$1 == chr { print $2"\t"$6 >> ext"sample."chr"."samplefile }' "$i"
|
|
144 done
|
|
145 done
|
|
146 printf "Done!\n"
|
|
147
|
|
148
|
|
149 if [ ! -z $bdir ]; then
|
|
150 #concatenate all background files into one file
|
|
151 printf "Processing control files..."
|
|
152 cat $bdir/*.bed > $wdir/bkgd.$ran/ctrl.bed
|
|
153
|
|
154 for f in $wdir/sizes.$ran/*; do
|
|
155 sizefile=$(basename $f)
|
|
156 chr=$(echo $sizefile | awk -F"." '{print $2}' | awk -F"." '{print $1}');
|
|
157 awk -v chr="$chr" -v ext="$wdir/bkgd.$ran/" -F"\t" '$1 == chr { print $2"\t"$6 >> ext"bkgd."chr".ctrl.bed" }' "$wdir/bkgd.$ran/ctrl.bed"
|
|
158 done
|
|
159
|
|
160 printf "Done!\n"
|
|
161 fi
|
|
162
|
|
163 #determine average read lengths
|
|
164 printf "Getting average read lengths..."
|
|
165 if [ ! -z $bdir ]; then
|
|
166 readC=$(awk '{a=$3-$2;print a;}' "$wdir/bkgd.$ran/ctrl.bed" | perl -lane '$a+=$_;END{print $a/$.}' | awk '{a=$1+0.5;print a;}' | cut -d"." -f1)
|
|
167 fi
|
|
168 readL=""
|
|
169 for s in $sdir/*.bed; do #and for each sample file
|
|
170 read=$(awk '{a=$3-$2;print a;}' "$s" | perl -lane '$a+=$_;END{print $a/$.}' | awk '{a=$1+0.5;print a;}' | cut -d"." -f1)
|
|
171 readL="$readL,$read"
|
|
172 done
|
|
173 readL=${readL#","}
|
|
174 printf "Done!\n"
|
|
175 #=======================> DONE!
|
|
176
|
|
177
|
|
178
|
|
179 # =============================
|
|
180 # Step Two: Fragment Length
|
|
181 # =============================
|
|
182 #single-end
|
|
183 if [ $type == "single" ]; then
|
|
184
|
|
185 if [ $fraglen == "ns" ]; then
|
|
186
|
|
187 ##Counting Where Reads Start and Calculating Cross Correlation
|
|
188 mkdir $wdir/stats.$ran/ #store count files
|
|
189 mkdir $out/xcorr #final xcorr results
|
|
190
|
|
191
|
|
192 printf "Calculating Fragment Length(s)...\n"
|
|
193 for f in $wdir/sizes.$ran/*; do #for each chromosome
|
|
194 samplelist=""
|
|
195 readlist=""
|
|
196 sizefile=$(basename $f)
|
|
197 chr=$(echo $sizefile | awk -F"." '{print $2}' | awk -F"." '{print $1}');
|
|
198
|
|
199 #list of sample bed files and read lengths
|
|
200 for s in $wdir/samples.$ran/*.bed; do #and for each sample file
|
|
201 samplefile=$(basename $s)
|
|
202 chr2=$(echo $samplefile | awk -F"." '{print $2}');
|
|
203 if [ $chr == $chr2 ] #belonging to this chromosome
|
|
204 then
|
|
205 samplelist="$samplelist,$wdir/samples.$ran/$samplefile"
|
|
206 fi
|
|
207 done
|
|
208 readlist="$readL"
|
|
209
|
|
210 #list of control bed files and read lengths
|
|
211 if [ ! -z $bdir ]; then
|
|
212 for s in $wdir/bkgd.$ran/*.bed; do #and for each sample file
|
|
213 samplefile=$(basename $s)
|
|
214 chr2=$(echo $samplefile | awk -F"." '{print $2}');
|
|
215 if [ $chr == $chr2 ] #belonging to this chromosome
|
|
216 then
|
|
217 samplelist="$samplelist,$wdir/bkgd.$ran/$samplefile"
|
|
218 readlist="$readL,$readC"
|
|
219 fi
|
|
220 done
|
|
221 fi
|
|
222
|
|
223 #remove leading comma
|
|
224 samplelist=${samplelist#","}
|
|
225
|
|
226 #call R script for xcorr calculation
|
|
227 Rscript "$sPath/xcorr.r" -ibed="$samplelist" -s="$wdir/sizes.$ran/size.$chr.bed" -rl="$readlist" -d="$wdir/stats.$ran" -p="$cores"
|
|
228 done
|
|
229
|
|
230 #report xcorr results (samples)
|
|
231 for f in $sdir/*.bed; do
|
|
232 file=$(basename $f)
|
|
233 samplefile=$(echo $file | awk -F"." '{print $1}');
|
|
234 mkdir "$out/xcorr/$samplefile" #final xcorr results
|
|
235 if [ -f "$wdir/stats.$ran/xc.$samplefile.tab" ]; then
|
|
236 cp $wdir/stats.$ran/xc.$samplefile.tab $out/xcorr/$samplefile/shifts.txt
|
|
237 fi
|
|
238 Rscript "$sPath/xcorrhelper.r" -infile="$out/xcorr/$samplefile/shifts.txt" -out="$out/xcorr/$samplefile"
|
|
239 done
|
|
240 #report xcorr results (control)
|
|
241 if [ ! -z $bdir ]; then
|
|
242 mkdir "$out/xcorr/ctrl" #final xcorr results
|
|
243 if [ -f "$wdir/stats.$ran/xc.ctrl.tab" ]; then
|
|
244 cp $wdir/stats.$ran/xc.ctrl.tab $out/xcorr/ctrl/shifts.txt
|
|
245 fi
|
|
246 Rscript "$sPath/xcorrhelper.r" -infile="$out/xcorr/ctrl/shifts.txt" -out="$out/xcorr/ctrl"
|
|
247 fi
|
|
248
|
|
249 fi
|
|
250 fi
|
|
251
|
|
252 #paired-end
|
|
253 if [ $type == "paired" ]; then
|
|
254 printf "Getting Average Fragment Length(s)...\n"
|
|
255 mkdir $out/xcorr #final xcorr results
|
|
256
|
|
257 for f in $sdir/*.bed; do
|
|
258 file=$(basename $f)
|
|
259 samplefile=$(echo $file | awk -F"." '{print $1}');
|
|
260 mkdir "$out/xcorr/$samplefile"
|
|
261 frag=$(awk '{a=$6-$2;print a;}' $f | perl -lane '$a+=$_;END{print $a/$.}' | awk '{a=$1+0.5;print a;}' | cut -d"." -f1)
|
|
262 echo "Average_from_paired $frag" > $out/xcorr/$samplefile/shifts.txt
|
|
263 Rscript "$sPath/xcorrhelper.r" -infile="$out/xcorr/$samplefile/shifts.txt" -out="$out/xcorr/$samplefile"
|
|
264 done
|
|
265 fi
|
|
266 #=======================> DONE!
|
|
267
|
|
268
|
|
269
|
|
270 # =================================
|
|
271 # Step Three: Calculating Bin Size
|
|
272 # =================================
|
|
273 if [ $binsize == "ns" ]; then
|
|
274 printf "Getting Bin Size: "
|
|
275
|
|
276 chr=$(sort -nr -k2 $gsize | head -n 1 | awk -F"\t" '{print $1}');
|
|
277 samplelist=""
|
|
278 frag=""
|
|
279 if [ $fraglen != "ns" ]; then
|
|
280 frag=$fraglen
|
|
281 k=1
|
|
282 fi
|
|
283
|
|
284 #list of sample bed files and read lengths
|
|
285 for s in $wdir/samples.$ran/*.bed; do
|
|
286 samplefile=$(basename $s)
|
|
287 chr2=$(echo $samplefile | awk -F"." '{print $2}');
|
|
288 if [ $chr == $chr2 ]
|
|
289 then
|
|
290 samplelist="$samplelist,$wdir/samples.$ran/$samplefile"
|
|
291 samplename=$(echo $samplefile | awk -F"." '{ print $3 }')
|
|
292 samplefilename=$(echo $samplefile | cut -d'.' -f 3-)
|
|
293 if [ $fraglen == "ns" ]; then
|
|
294 shift=$(awk -F":" '$1 == "Fragment Length" { print $2 }' "$out/xcorr/$samplename/xcorrsummary.txt")
|
|
295 frag="$frag,$shift"
|
|
296 fi
|
|
297 fi
|
|
298 done
|
|
299 #remove leading comma
|
|
300 samplelist=${samplelist#","}
|
|
301 frag=${frag#","}
|
|
302
|
|
303 Rscript "$sPath/bincalculator.r" -ibed="$samplelist" -s="$gsize" -rl="$readL" -d="$wdir" -p="$cores" -f="$frag" -type="$type"
|
|
304 fi
|
|
305 if [ $binsize != "ns" ]; then
|
|
306 printf "You set a Bin Size: $binsize \n"
|
|
307 fi
|
|
308 #=======================> DONE!
|
|
309
|
|
310
|
|
311
|
|
312 # ===========================
|
|
313 # Step Four: Calling Peaks
|
|
314 # ===========================
|
|
315 mkdir $wdir/peaks.$ran/ #store count files
|
|
316 mkdir $out/peaks #store peak files
|
|
317
|
|
318 printf "Calling Peaks...(mode: $mode, resolution: $resol)\n"
|
|
319
|
|
320
|
|
321 #single-end reads
|
|
322 if [ $type == "single" ]; then
|
|
323
|
|
324 if [ $binsize == "ns" ]; then
|
|
325 binsize=$(cat "$wdir/binsize.txt")
|
|
326 fi
|
|
327
|
|
328 counting=1;
|
|
329 for f in $wdir/sizes.$ran/*; do #for each chromosome
|
|
330 samplelist=""
|
|
331 frag=""
|
|
332 k=1
|
|
333 if [ $fraglen != "ns" ]; then
|
|
334 frag=$fraglen
|
|
335 fi
|
|
336
|
|
337
|
|
338 sizefile=$(basename $f)
|
|
339 chr=$(echo $sizefile | awk -F"." '{print $2}' | awk -F"." '{print $1}');
|
|
340
|
|
341 printf "Chromosome $chr: "
|
|
342
|
|
343 #list of sample bed files and fragment lengths
|
|
344 for s in $wdir/samples.$ran/*.bed; do #and for each sample file
|
|
345 samplefile=$(basename $s)
|
|
346 chr2=$(echo $samplefile | awk -F"." '{print $2}');
|
|
347 if [ $chr == $chr2 ] #belonging to this chromosome
|
|
348 then
|
|
349 samplelist="$samplelist,$wdir/samples.$ran/ext.$samplefile"
|
|
350 samplename=$(echo $samplefile | awk -F"." '{ print $3 }')
|
|
351 samplefilename=$(echo $samplefile | cut -d'.' -f 3-)
|
|
352 if [ $fraglen == "ns" ]; then
|
|
353 shift=$(awk -F":" '$1 == "Fragment Length" { print $2 }' "$out/xcorr/$samplename/xcorrsummary.txt")
|
|
354 frag="$frag,$shift"
|
|
355 read=$(echo $readL | cut -f "$k" -d ",")
|
|
356 k=$(($k+1))
|
|
357 fi
|
|
358 if [ $fraglen != "ns" ]; then
|
|
359 shift=$(echo $frag | cut -f "$k" -d ",")
|
|
360 read=$(echo $readL | cut -f "$k" -d ",")
|
|
361 k=$(($k+1))
|
|
362 fi
|
|
363 perl "$sPath/readshifter.pl" "$wdir/samples.$ran/$samplefile" $shift $read > "$wdir/samples.$ran/ext.$samplefile"
|
|
364 fi
|
|
365 done
|
|
366
|
|
367 #control file
|
|
368 bkgdfile="None"
|
|
369 if [ ! -z $bdir ]; then
|
|
370 if [ $fraglen == "ns" ]; then
|
|
371 bshift=$(awk -F":" '$1 == "Fragment Length" { print $2 }' "$out/xcorr/ctrl/xcorrsummary.txt")
|
|
372 frag="$frag,$bshift"
|
|
373 fi
|
|
374 if [ $fraglen != "ns" ]; then
|
|
375 l=$(($dupnum+1))
|
|
376 bshift=$(echo $frag | cut -f "$l" -d ",")
|
|
377 fi
|
|
378 perl "$sPath/readshifter.pl" "$wdir/bkgd.$ran/bkgd.$chr.ctrl.bed" $bshift $readC > "$wdir/bkgd.$ran/ext.bkgd.$chr.ctrl.bed"
|
|
379 bkgdfile="$wdir/bkgd.$ran/ext.bkgd.$chr.ctrl.bed"
|
|
380 fi
|
|
381
|
|
382 #remove leading comma
|
|
383 samplelist=${samplelist#","}
|
|
384 frag=${frag#","}
|
|
385
|
|
386 #call the peak calling R script
|
|
387 Rscript "$sPath/peakfinder.r" -sfile="$f" -bednames="$samplelist" -frag="$frag" -bkgd=$bkgdfile -out="$wdir/peaks.$ran/" -clustnummer="$clustno" -resolution="$resol" -window="$window" -p="$cores" -bin="$binsize" -type="$type" -chrcount="$counting" -windowe="$windowe"
|
|
388 counting=$(($counting+1));
|
|
389 cp "$wdir/peaks.$ran/$chr.peaks.bed" "$out/peaks/$chr.peaks.bed"
|
|
390 rm "$wdir/peaks.$ran/$chr.peaks.bed"
|
|
391 done
|
|
392 counting=1;
|
|
393 fi
|
|
394
|
|
395
|
|
396 #paired-end reads
|
|
397 if [ $type == "paired" ]; then
|
|
398
|
|
399 if [ $binsize == "ns" ]; then
|
|
400 binsize=$(cat "$wdir/binsize.txt")
|
|
401 fi
|
|
402
|
|
403
|
|
404 counting=1;
|
|
405 for f in $wdir/sizes.$ran/*; do #for each chromosome
|
|
406 samplelist=""
|
|
407
|
|
408 sizefile=$(basename $f)
|
|
409 chr=$(echo $sizefile | awk -F"." '{print $2}' | awk -F"." '{print $1}');
|
|
410
|
|
411 printf "Chromosome $chr: "
|
|
412
|
|
413 #list of sample bed files and fragment lengths
|
|
414 for s in $wdir/samples.$ran/*.bed; do #and for each sample file
|
|
415 samplefile=$(basename $s)
|
|
416 chr2=$(echo $samplefile | awk -F"." '{print $2}');
|
|
417 if [ $chr == $chr2 ] #belonging to this chromosome
|
|
418 then
|
|
419 samplelist="$samplelist,$wdir/samples.$ran/$samplefile"
|
|
420 samplename=$(echo $samplefile | awk -F"." '{ print $3 }')
|
|
421 samplefilename=$(echo $samplefile | cut -d'.' -f 3-)
|
|
422 x="$sdir/$samplefilename"
|
|
423 fi
|
|
424 done
|
|
425
|
|
426 #control file
|
|
427 bkgdfile="None"
|
|
428 if [ ! -z $bdir ]; then
|
|
429 bkgdfile="$wdir/bkgd.$ran/bkgd.$chr.ctrl.bed"
|
|
430 fi
|
|
431
|
|
432 #remove leading comma
|
|
433 samplelist=${samplelist#","}
|
|
434 frag=${frag#","}
|
|
435
|
|
436 #call the peak calling R script
|
|
437 Rscript "$sPath/peakfinder.r" -sfile=$f -bednames=$samplelist -frag="NA" -bkgd=$bkgdfile -out="$wdir/peaks.$ran/" -clustnummer="$clustno" -resolution="$resol" -window="$window" -p="$cores" -bin="$binsize" -type="$type" -chrcount="$counting" -windowe="$windowe"
|
|
438 counting=$(($counting+1));
|
|
439 cp "$wdir/peaks.$ran/$chr.peaks.bed" "$out/peaks/$chr.peaks.bed"
|
|
440 rm "$wdir/peaks.$ran/$chr.peaks.bed"
|
|
441 done
|
|
442 counting=1;
|
|
443 fi
|
|
444
|
|
445
|
|
446 cp $wdir/peaks.$ran/min.peaksize $out/peaks/min.peaksize
|
|
447
|
|
448
|
|
449 #concatenate, sort and filter
|
|
450 cat $out/peaks/*.bed > $out/peaks/all.narrowPeak
|
|
451 Rscript "$sPath/peakhelper.r" -filelist="$out/peaks/all.narrowPeak"
|
|
452 perl "$sPath/peakfilter.pl" $out/peaks/all.narrowPeak | sort -nr -k7 > $out/peaks/filtered.peaks.narrowPeak
|
|
453 cut -f1-10 $out/peaks/all.narrowPeak | awk -F"\t" -v j=0 '$7 > j' | sort -nr -k7 > $out/peaks/all.peaks.narrowPeak
|
|
454 rm $out/peaks/all.narrowPeak
|
|
455 rm $out/peaks/*.bed
|
|
456 rm $out/peaks/min.peaksize
|
|
457 #=======================> DONE!
|
|
458
|
|
459
|
|
460
|
|
461 rm -rf $wdir
|
|
462
|
|
463
|
|
464
|
|
465 printf "\n\n========================================\nWe're done...Congratulations!\n========================================\n\n"
|