-
Notifications
You must be signed in to change notification settings - Fork 1
/
PANZ_regional_enrich.sh
429 lines (386 loc) · 13.7 KB
/
PANZ_regional_enrich.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/env bash
# >>>>>>>>>>>>>>>>>>>>>>>> Common functions >>>>>>>>>>>>>>>>>>>>>>>>
gst_log () {
local info=$1
echo -e "\033[36m[$(date +'%y-%m-%d %H:%M')]\033[0m $info" >&2
}
gst_rcd () {
local info=$1
echo -e "\033[32m>>>------------>\033[0m $info" >&2
}
gst_err () {
local info=$1
echo -e "\033[31m\033[7m[ERROR]\033[0m --> $info" >&2
}
gst_warn () {
local info=$1
echo -e "\033[35m[WARNING]\033[0m --> $info" >&2
}
check_files_executable(){
local num_related_file=1
local related_file=""
for related_file in "$@"
do
if [[ ! -x "$related_file" ]]; then
echo -e "\033[31m\033[7m[ERROR]\033[0m --> NOT EXECUTABLE: $related_file $" >&2
let num_related_file++
fi
done
[ "$num_related_file" -ne 1 ] && exit 1
}
check_files_exists(){
local num_related_file=1
local related_file=""
for related_file in "$@"
do
if [[ ! -s "$related_file" ]]; then
echo -e "\033[31m\033[7m[ERROR]\033[0m --> No file: $related_file $" >&2
let num_related_file++
fi
done
[ "$num_related_file" -ne 1 ] && exit 1
}
check_abs_path() {
local var_cc=1
local check_file=""
for check_file in "$@";do
if [[ "${check_file:0:1}" != "/" ]]; then
echo -e "\033[31m\033[7m[ERROR]\033[0m --> $check_file was not an ABSOLUTE path." >&2
let var_cc++
fi
done
[ "$var_cc" -ne 1 ] && exit 1
}
check_sftw_path(){
local num_tp_program=1
local tp_program=""
for tp_program in "$@"
do
if ! which $tp_program >/dev/null 2>&1 ; then
echo -e "\033[31m\033[7m[ERROR]\033[0m --> Program not in PATH: $tp_program $" >&2
let num_tp_program++
fi
done
[ "$num_tp_program" -ne 1 ] && exit 1
}
check_R_lib () {
local num_R_lib=1
local tp_R_lib=""
Rscript --vanilla --slave -e '
argv=as.character(commandArgs(TRUE));
if (all(argv %in% rownames(installed.packages()))) {
quit(save="no", status=0)
} else {
quit(save="no", status=1)
}
' $*
if [ $? -ne 0 ];then gst_err "One or more of \"$*\" not installed in R"; exit 1;fi
}
check_var_empty () {
local var_cc=1
local var_name=""
local var=""
for var_name in "$@"; do
var=$(eval echo "$"$var_name)
case ${var} in
'')
echo -e "\033[31m\033[7m[ERROR]\033[0m --> $var_name is empty: '$var' " >&2
let var_cc++ ;;
*) ;;
esac >&2
done
[ "$var_cc" -ne 1 ] && exit 1
}
check_var_numeric () {
local var_cc=1
local var_name=""
local var=""
for var_name in "$@"; do
var=$(eval echo "$"$var_name)
# add ${var#prefix} substitution to trim sign
case ${var#[-+]} in
'')
echo -e "\033[31m\033[7m[ERROR]\033[0m --> $var_name is empty: '$var' " >&2
let var_cc++ ;;
*.*.*)
echo -e "\033[31m\033[7m[ERROR]\033[0m --> $var_name has more than one decimal point: '$var' " >&2
let var_cc++ ;;
*[!0-9.]*)
echo -e "\033[31m\033[7m[ERROR]\033[0m --> $var_name has a non-digit somewhere in it: '$var' " >&2
let var_cc++ ;;
*) ;;
esac >&2
done
[ "$var_cc" -ne 1 ] && exit 1
}
check_suffix () {
check_suffix_file=$( basename $1 )
check_suffix=$2
# add x incase file has no suffix
if [[ "${check_suffix_file##*.}"x != "$check_suffix"x ]];then
echo "[ERROR] --> $check_suffix_file should have suffix: '$check_suffix'." >&2
exit 1
fi
}
export -f check_R_lib gst_log gst_rcd gst_warn gst_err check_files_executable check_var_empty check_var_numeric check_sftw_path check_suffix check_files_exists check_abs_path
# <<<<<<<<<<<<<<<<<<<<<<<< Common functions <<<<<<<<<<<<<<<<<<<<<<<<
usage=$(
cat <<EOF
------------------------------------------------------------
PANZ_Regional_enrich: a wrapper of R/regioneR function, with
some DIY options.
------------------------------------------------------------
Dependence:
R/regioneR package (http://bioconductor.org/packages/release/bioc/html/regioneR.html)
------------------------------------------------------------
USAGE:
bash $(basename $0) [OPTIONS]
OPTIONS: ([R]:required [O]:optional)
-h, --help show help and exit.
-o, --out <str> [R] Output prefix.
-g, --genome <str> [R] Genome range in bed format. Used as the bondaries of region manipulating. Eg:
chr1 0 1234567
chr2 0 1345678
-a, --query <str> [R] Query region file in bed format, will do permutation based on this file.
-b, --feature <str> [R] Feature region file in bed format, will count overlaps of each permutation of query region with this file to evaluate the enrichment.
-r, --ref <str> [O] Set the range of query region permutation. Three type of parameters are supported:
1. [bed:your_bed_file.bed] --> Use a region file as the permutation boundary, so make sure the query region file is subset of the input region file.
2. [flank:<int>] --> use a flanking of <int> bp length of query region (both side) as the permutation boundary, eg: "flank:10000" will flank left 10kb and right 10kb of query region.
3. [time:<int>] --> use a flanking of <int> * <length of each query region> bp of query region as the permutation boundary, eg:
if your query region is:
# chr1 1000 1100
# chr2 5010 5020
and you set ref as "--ref time:5", the permutation boundary would be:
# chr1 500 1600 (query length=100,flanking=100*5)
# chr2 4960 5070 (query length=10,flanking=10*5)
The default behavior of --ref (if unset) is to use "--genome" file as boundary, that is permutation along the whole genome.
-n, --ntimes <int> [O] Number of permutation times.A large number of permutations will produce more accurate results and a nicer-looking plot but a permutation test can be computationally expensive.(default: 100)
--cutoff <0-1> [O] P-value cutoff to call the result as significantly non-random. (default: 0.05)
--seed <int> [O] Set random seeds to create reproducible results. (default: 1234)
--plot [O] Plot the permutation results.
--force_save [O] Force save the permutation result rdata. The default behavior is to save only the result that passed the P-value cutoff.
------------------------------------------------------------
Author: Songtao Gui
E-mail: songtaogui@sina.com
EOF
)
if [[ $# -eq 0 ]]; then
echo "$usage" >&2
exit 1
fi
# >>>>>>>>>>>>>>>>>>>>>>>> Parse Options >>>>>>>>>>>>>>>>>>>>>>>>
# Set Default Opt
export out=""
export genome=""
export query=""
export feature=""
export ref=""
export ntimes=100
export cutoff=0.05
export seed=1234
export plot="FALSE"
export force_save="FALSE"
# parse args
UNKOWN_ARGS=()
while [[ $# > 0 ]]; do
case "$1" in
-h|--help)
echo "$usage" >&2
exit 1
;;
-o|--out)
#echo "set argument \"$1\" with value: $2" >&2
out=$2
shift 2
;;
-g|--genome)
#echo "set argument \"$1\" with value: $2" >&2
genome=$2
shift 2
;;
-a|--query)
#echo "set argument \"$1\" with value: $2" >&2
query=$2
shift 2
;;
-b|--feature)
#echo "set argument \"$1\" with value: $2" >&2
feature=$2
shift 2
;;
-r|--ref)
#echo "set argument \"$1\" with value: $2" >&2
ref=$2
shift 2
;;
-n|--ntimes)
#echo "set argument \"$1\" with value: $2" >&2
ntimes=$2
shift 2
;;
--cutoff)
#echo "set argument \"$1\" with value: $2" >&2
cutoff=$2
shift 2
;;
--seed)
#echo "set argument \"$1\" with value: $2" >&2
seed=$2
shift 2
;;
--plot)
#echo "set argument \"$1\" with value: $2" >&2
plot="TRUE"
shift 1
;;
--force_save)
#echo "set argument \"$1\" with value: $2" >&2
force_save="TRUE"
shift 1
;;
*) # unknown flag/switch
UNKOWN_ARGS+=("$1")
shift
;;
esac
done
if [ "${#UNKOWN_ARGS[@]}" -gt 0 ];then
echo "[ERROR] --> Wrong options: \"${UNKOWN_ARGS[@]}\"" >&2
exit 1
fi
unset UNKOWN_ARGS # restore UNKOWN_ARGS params
# ! Check if required vars are legal
check_var_empty out genome query feature ntimes cutoff seed plot force_save
check_var_numeric ntimes cutoff seed
check_files_exists $query $genome $feature
check_R_lib regioneR
# <<<<<<<<<<<<<<<<<<<<<<<< Parse Options <<<<<<<<<<<<<<<<<<<<<<<<
# USAGE:$0
# USAGE:0 input1 input2
gst_regioneR () {
local a=$query
local b=$feature
local r=${out}.ref_file.tmp
local g=$genome
local s=$seed
local n=$ntimes
local c=$cutoff
local p=$plot
local f=$force_save
local o=$out
check_files_exists $r $g $a $b
Rscript -e '
library(regioneR)
argv=as.character(commandArgs(TRUE));
print(argv)
# ? 9 argvs
G=toGRanges(argv[4])
A=toGRanges(argv[1]);
B=toGRanges(argv[2]);
R=toGRanges(argv[3]);
set.seed(as.numeric(argv[5]));
Pt = overlapPermTest(A, B, ntimes=as.numeric(argv[6]), force.parallel=TRUE,genome=G);
Pt.table=data.frame(A=gsub("^.*/(.*)\\.bed","\\1",argv[1]),B=gsub("^.*/(.*)\\.bed","\\1",argv[2]),P_value=Pt$numOverlaps$pval,N=Pt$numOverlaps$ntimes,Z=Pt$numOverlaps$zscore)
# ? output results
write.table(Pt.table,file=paste0(argv[10],".tsv"),quote=F,row.names=F,sep="\t")
if(argv[9] == "TRUE"){
# ? force save rdata
save.image(Pt,Pt.table,file=paste0(argv[10],".rdata"))
}else{
if(Pt$numOverlaps$pval < as.numeric(argv[7])){
# ? save rdata if pass cutoff
save(Pt,Pt.table,file=paste0(argv[10],".rdata"))
}
}
if(argv[8] == "TRUE"){
# ? draw plot
pdf(paste0(argv[10],"_PermTest.pdf"))
plot(Pt)
dev.off()
}
' $a $b $r $g $s $n $c $p $f $o
if [ $? -ne 0 ];then gst_err "Rscript run failed: Non-zero exit"; exit 1;fi
}
export -f gst_regioneR
if [ ! -s "${out}.done" ];then
# ? parse ref mode and generat ref_file
gst_log "Parsing options ..."
export ref_file=$genome
export flank_bp=""
export flank_len_time=""
if [ -z "$ref" ]; then
gst_warn "No subset ref region provided, use whole genome as ref region ..."
ref="bed:$genome"
fi
case "$ref" in
bed:*)
check_files_exists ${ref##bed:}
cat ${ref##bed:} > ${out}.ref_file.tmp
;;
flank:*)
flank_bp=${ref##flank:}
check_var_numeric flank_bp
# ? generat ref_file
cat $query | perl -F"\t" -lane '
BEGIN{
$,="\t";
$inputfile="$ENV{genome}";
open(IN,"$inputfile") or die("Cannot open file: $inputfile");
while(<IN>){
chomp;
# $hash{$_}=1;
($cc,$cs,$ce)=split(/\t/,$_);
$chrlen{$cc}=$ce;
}
}
($c,$s,$e)=@F[0,1,2];
$s=$s-$ENV{flank_bp};
$e=$e+$ENV{flank_bp};
$s=0 if $s < 0;
$e=$chrlen{$c} if $chrlen{$c} && $e > $chrlen{$c};
print $c,$s,$e
' > ${out}.ref_file.tmp
;;
time:*)
flank_len_time=${ref##time:}
check_var_numeric flank_len_time
# ? generat ref_file
cat $query | perl -F"\t" -lane '
BEGIN{
$,="\t";
$inputfile="$ENV{genome}";
open(IN,"$inputfile") or die("Cannot open file: $inputfile");
while(<IN>){
chomp;
# $hash{$_}=1;
($cc,$cs,$ce)=split(/\t/,$_);
$chrlen{$cc}=$ce;
}
}
($c,$s,$e)=@F[0,1,2];
$len=($e-$s)*$ENV{flank_len_time};
die("Start larger than End for $_ !") if $len < 0;
$s=$s-$len;
$e=$e+$len;
$s=0 if $s < 0;
$e=$chrlen{$c} if $chrlen{$c} && $e > $chrlen{$c};
print $c,$s,$e
' > ${out}.ref_file.tmp
;;
*)
gst_err "Wrong --ref option: $ref"
exit 1
;;
esac
if [ $? -ne 0 ];then gst_err "parse $ref failed: Non-zero exit"; exit 1;fi
check_files_exists ${out}.ref_file.tmp
# ? run main
gst_log "Run permutation and get outputs ..."
gst_regioneR 1> ${out}.rlog 2>&1
if [ $? -ne 0 ];then gst_err "gst_regioneR failed: Non-zero exit"; exit 1;fi
echo "done" > ${out}.done
else
echo -e "\033[35m[WARNING]\033[0m --> Already done, skip running. Remove $PWD/${out}.done to force re-run." >&2
fi
rm -f ${out}.ref_file.tmp ${out}.rlog
gst_log "All done."