-
Notifications
You must be signed in to change notification settings - Fork 13
/
SignalGenerator.sh
333 lines (279 loc) · 9.78 KB
/
SignalGenerator.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
########################################################################
# JAMMv1.0.8 is a peak finder for joint analysis of NGS replicates.
# Copyright (C) 2014-2019 Mahmoud Ibrahim
#
# 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/>.
#
# Contact: [email protected]
########################################################################
##Finding out the path
sPath="`dirname \"$0\"`"
sPath="`( cd \"$sPath\" && pwd )`"
usage()
{
cat << EOF
Welcome to JAMM v1.0.7rev5 Signal Generator Script (GNU GPLv3). Copyright (C) 2014-2019 Mahmoud Ibrahim.
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.
OPTIONS:
-s Directory containing sample files (required)
-g Genome size file (required)
-o Output Directory (required)
-c directory containing input or Control files
-r file with Regions to get signal for (required)
-b Bin size for signal generation (default: 10)
-f Fragment lengths (required if -t is "single")
-p Number of processors used by R scripts (default: 1)
-t Alignment type, paired or single (default: single)
-n Normalization method, chromAverage or depth (default: chromAverage)
EOF
}
# =========================
# Process Input parameters
# =========================
sdir=""
gsize=""
out=""
signal="10"
regs=""
fraglen=""
wdir=$(mktemp -d)
ran=$RANDOM
cores="1"
type="single"
normal="chromAverage"
while getopts "s:o:c:r:b:f:g:p:t:n:" OPTION
do
case $OPTION in
s) sdir=$OPTARG
;;
g) gsize=$OPTARG
;;
o) out=$OPTARG
;;
c) bdir=$OPTARG
;;
r) regs=$OPTARG
;;
b) signal=$OPTARG
;;
f) fraglen=$OPTARG
;;
p) cores=$OPTARG
;;
t) type=$OPTARG
;;
n) normal=$OPTARG
;;
?)
usage
exit
;;
esac
done
if [[ -z $sdir ]] || [[ -z $regs ]] || [[ -z $out ]]
then
usage
exit 1
fi
if [[ -z $fraglen ]]
then
if [ $type == "single" ]
then
usage
exit 1
fi
fi
if [[ -d "$out/signal" ]]; then
printf "\n\nOutput directory $out/signal already exists. I can't override existing results!\n\n"
exit 0
fi
#=======================> DONE!
# =============================
# Step One: Initial Processing
# =============================
printf "\n\n==========================================================\nStarted JAMM1.0.7rev5 Signal Generator Pipeline...Hang on!\n==========================================================\n\n"
if [ ! -d "$wdir" ]; then
mkdir $wdir #make working directory
fi
if [ ! -d "$out" ]; then
mkdir $out #make working directory
fi
mkdir $wdir/bkgd.$ran/ #directory to store background files
mkdir $wdir/sizes.$ran/ #chromosomes and sizes
mkdir $wdir/samples.$ran/ #store sample files
dupnum=$(ls -1 $sdir | wc -l) #count how many sample files
#separate chromosome sizes
printf "Loading genome size file..."
ext="$wdir/sizes.$ran/"
awk -v ext="$ext" '{ print >> ext"/size." $1 ".bed" }' $gsize
printf "Done!\n"
printf "Processing sample files..."
#load each chromosome from each sample file
for i in $sdir/*.bed; do
samplefile=$(basename $i)
for f in $wdir/sizes.$ran/*; do
sizefile=$(basename $f)
chr=$(echo $sizefile | awk -F"." '{print $2}' | awk -F"." '{print $1}');
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"
done
done
printf "Done!\n"
if [ ! -z $bdir ]; then
#concatenate all background files into one file
printf "Processing control files..."
cat $bdir/*.bed > $wdir/bkgd.$ran/ctrl.bed
for f in $wdir/sizes.$ran/*; do
sizefile=$(basename $f)
chr=$(echo $sizefile | awk -F"." '{print $2}' | awk -F"." '{print $1}');
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"
done
printf "Done!\n"
fi
#determine average read lengths
if [ $type == "single" ]; then
printf "Getting average read lengths..."
if [ ! -z $bdir ]; then
readC=$(awk '{a=$3-$2;print a;}' "$wdir/bkgd.$ran/ctrl.bed" | perl -lane '$a+=$_;END{print $a/$.}' | awk '{print int($1)}')
fi
readL=""
for s in $sdir/*.bed; do #and for each sample file
read=$(awk '{a=$3-$2;print a;}' "$s" | perl -lane '$a+=$_;END{print $a/$.}' | awk '{print int($1)}')
readL="$readL,$read"
done
readL=${readL#","}
printf "Done!\n"
fi
#=======================> DONE!
# ===========================
# Step Three: Getting Signal
# ===========================
mkdir $wdir/signal.$ran/
mkdir $out/signal #store signal
printf "Generating Signal...(bin size: $signal)\n"
if [ $type == "single" ]; then
counting=1;
for f in $wdir/sizes.$ran/*; do #for each chromosome
samplelist=""
frag=""
frag=$fraglen
k=1
sizefile=$(basename $f)
chr=$(echo $sizefile | awk -F"." '{print $2}' | awk -F"." '{print $1}');
chrSize=$(cat $f | cut -f2);
printf "Chromosome $chr: "
#list of sample bed files and fragment lengths
for s in $wdir/samples.$ran/*.bed; do #and for each sample file
samplefile=$(basename $s)
chr2=$(echo $samplefile | awk -F"." '{print $2}');
if [ $chr == $chr2 ] #belonging to this chromosome
then
samplelist="$samplelist,$wdir/samples.$ran/ext.$samplefile"
samplename=$(echo $samplefile | awk -F"." '{ print $3 }')
samplefilename=$(echo $samplefile | cut -d'.' -f 3-)
shift=$(echo "$frag" | cut -f "$k" -d ",")
read=$(echo "$readL" | cut -f "$k" -d ",")
k=$(($k+1))
perl "$sPath/readshifter.pl" "$wdir/samples.$ran/$samplefile" $shift $read > "$wdir/samples.$ran/ext.$samplefile"
fi
done
#control file
bkgdfile="None"
if [ ! -z $bdir ]; then
l=$(($dupnum+1))
bshift=$(echo $frag | cut -f "$l" -d ",")
perl "$sPath/readshifter.pl" "$wdir/bkgd.$ran/bkgd.$chr.ctrl.bed" $bshift $readC > "$wdir/bkgd.$ran/ext.bkgd.$chr.ctrl.bed"
bkgdfile="$wdir/bkgd.$ran/ext.bkgd.$chr.ctrl.bed"
fi
#remove leading comma
samplelist=${samplelist#","}
frag=${frag#","}
depth="0";
if [ $normal == "depth" ]; then
genomeSize=$(awk '{a=$2;print a;}' "$gsize" | perl -lane '$a+=$_;END{print $a}' | awk '{print int($1)}');
depthTemp="";
for s in $sdir/*.bed; do #and for each sample file
readnum=$(wc -l $s | cut -d" " -f1);
depthTemping=$(awk -v rn=$readnum -v gs=$genomeSize 'BEGIN { print rn / gs }');
depthTemp="$depthTemp,$depthTemping";
done
if [ ! -z $bdir ]; then
readnum=$(wc -l "$wdir/bkgd.$ran/ctrl.bed" | cut -d" " -f1);
depthTemping=$(awk -v rn=$readnum -v gs=$genomeSize 'BEGIN { print rn / gs }');
depthTemp="$depthTemp,$depthTemping";
fi
depth=${depthTemp#","}
fi
#call the peak calling R script
Rscript "$sPath/signalmaker.r" -chromoS="$chrSize" -chromo="$chr" -bednames=$samplelist -frag=$frag -bkgd=$bkgdfile -out="$wdir/signal.$ran/" -sig="$signal" -regions="$regs" -p="$cores" -chrcount="$counting" -t="$type" -normal="$normal" -depth="$depth"
cat $wdir/signal.$ran/*.bedSignal | awk -F"\t" -v j=0 '$4 > j' > "$out/signal/$chr.bedGraph"
rm $wdir/signal.$ran/*.bedSignal
counting=$(($counting+1));
done
counting=1;
fi
if [ $type == "paired" ]; then
counting=1;
for f in $wdir/sizes.$ran/*; do #for each chromosome
samplelist=""
sizefile=$(basename $f)
chr=$(echo $sizefile | awk -F"." '{print $2}' | awk -F"." '{print $1}');
chrSize=$(cat $f | cut -f2);
printf "Chromosome $chr: "
#list of sample bed files and fragment lengths
for s in $wdir/samples.$ran/*.bed; do #and for each sample file
samplefile=$(basename $s)
chr2=$(echo $samplefile | awk -F"." '{print $2}');
if [ $chr == $chr2 ] #belonging to this chromosome
then
samplelist="$samplelist,$wdir/samples.$ran/$samplefile"
samplename=$(echo $samplefile | awk -F"." '{ print $3 }')
samplefilename=$(echo $samplefile | cut -d'.' -f 3-)
x="$sdir/$samplefilename"
fi
done
#control file
bkgdfile="None"
if [ ! -z $bdir ]; then
bkgdfile="$wdir/bkgd.$ran/bkgd.$chr.ctrl.bed"
fi
#remove leading comma
samplelist=${samplelist#","}
frag=${frag#","}
depth="0";
if [ $normal == "depth" ]; then
genomeSize=$(awk '{a=$2;print a;}' "$gsize" | perl -lane '$a+=$_;END{print $a}' | awk '{print int($1)}');
depthTemp="";
for s in $sdir/*.bed; do #and for each sample file
readnum=$(wc -l $s | cut -d" " -f1);
depthTemping=$(awk -v rn=$readnum -v gs=$genomeSize 'BEGIN { print rn / gs }');
depthTemp="$depthTemp,$depthTemping";
done
if [ ! -z $bdir ]; then
readnum=$(wc -l "$wdir/bkgd.$ran/ctrl.bed" | cut -d" " -f1);
depthTemping=$(awk -v rn=$readnum -v gs=$genomeSize 'BEGIN { print rn / gs }');
depthTemp="$depthTemp,$depthTemping";
fi
depth=${depthTemp#","}
fi
#call the peak calling R script
Rscript "$sPath/signalmaker.r" -chromoS="$chrSize" -chromo="$chr" -bednames=$samplelist -frag=$frag -bkgd=$bkgdfile -out="$wdir/signal.$ran/" -sig="$signal" -regions="$regs" -p="$cores" -chrcount="$counting" -t="$type" -normal="$normal" -depth="$depth"
cat $wdir/signal.$ran/*.bedSignal | awk -F"\t" -v j=0 '$4 > j' > "$out/signal/$chr.bedGraph"
rm $wdir/signal.$ran/*.bedSignal
counting=$(($counting+1));
done
counting=1;
fi
#=======================> DONE!
rm -rf $wdir
printf "\n\n========================================\nWe're done...Congratulations!\n========================================\n\n"