-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalize_bias.R
64 lines (60 loc) · 4.31 KB
/
normalize_bias.R
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
required.pack = c("getopt","optparse")
for(p in required.pack){
sig=suppressMessages(require(p,character.only = TRUE,quietly = T))
if(!sig){stop(paste("package",p,"needed!"))}
}
opt.p=OptionParser(description="Normalize eSPAN bias with BrdU bias to exclude the background effects in repliactoin. Normalized bias matrix will be output and a sample pair list of normalization will be generated.")
opt.p=add_option(opt.p,c("--matrix","-i"),type="character",
help="Bias matrix files generated by 'cal_bias.R'. (Required)
\t\tPut all eSPAN and BrdU samples' matrix files here and join them by ','.")
opt.p=add_option(opt.p,c("--class","-c"),type="character",
help="Class of samples,such as 'brdu','H3K4me3_eSPAN' or 'H3K56ac_eSPAN'.(Required)
\t\tIt's used to pair samples for normalization. Length of sample 'class' should be the same length of matrix files and joined by ','.")
opt.p=add_option(opt.p,c("--condition", "-C"),type="character", default="",
help="Description of sample conditions, such as date, MNase, Sonication, mutant strains. For example, '2020_MNase_dpb3'. (Optional)
\t\tSamples with exactly same 'condition' and matched 'class' will be paired together for normalization.
\t\tIf not provied, all samples will be considered as same condition. Please check generated sample pair list to make sure normalizaton is as expected.
\t\tLength of 'condition' should be the same length of matrix files and joined by ','.")
opt.p=add_option(opt.p,c("--target"),type="character",default="H3K4me3_eSPAN, H3K56ac_eSPAN",
help="The target class is for pairing samples and should be words selected from 'Class'. (Optional, default='H3K4me3_eSPAN, H3K56ac_eSPAN')
\t\tLength of 'target' should be the same length as 'control'. In general, you don't need to change it.")
opt.p=add_option(opt.p,c("--control"),type="character",default="BrdU, BrdU",
help="The control class for pairing samples and should be words selected from 'Class'. (Optional, default='BrdU, BrdU')
\t\tLength of 'control' should be the same length as 'target'. In general, you don't need to change it.")
opt.p=add_option(opt.p,c("--label","-l"),type="character",
help=c("Sample name labeling for the bias matrix file. (Optional)
\t\tIf not provided, file name will be used. Length of labels should be the same length as matrix files and joined by ','."))
opt.p=add_option(opt.p,c("--normMatrix","-o"),type="character",default="bias_norm_matrix",
help="Output folder for normalized bias matrix. They can be used for bias plot. (Optional, default='bias_norm_matrix')")
opt.p=add_option(opt.p,c("--sumMatFile", "-s"),type="character",default="my_norm_bias_mat.xlsx",
help="Summary of output normalized matrix file. (Optional, default='my_norm_bias_mat.xlsx')")
opt=parse_args(opt.p)
if(is.null(opt$matrix)|is.null(opt$class)){print_help(opt.p);stop("--matrix and --class are required\n")}
print(opt)
where=function(){
spath <-parent.frame(2)$ofile
if (is.null(spath)) {
args <- commandArgs()
filearg <- args[grep("^--file=", args)]
fname <- strsplit(filearg, "=")[[1]][2]
} else {
fname <- spath
}
dirname(normalizePath(fname))
}
source(file.path(where(),"source/bias_function.R"))
##initialize value
mat.file=unlist(strsplit(opt$matrix,","));mat.file=trimws(mat.file)
if(is.null(opt$label)){sample.name=NULL}else{sample.name=unlist(strsplit(opt$label,","));sample.name=trimws(sample.name)}
sample.class=unlist(strsplit(opt$class,","));sample.class=trimws(sample.class)
if(opt$condition==''){sample.condition=''}else{sample.condition=unlist(strsplit(opt$condition,","));sample.condition=trimws(sample.condition)}
target.class=unlist(strsplit(opt$target,","));target.class=trimws(target.class)
control.class=unlist(strsplit(opt$control,","));control.class=trimws(control.class)
norm.matrix.dir=opt$normMatrix
sum.mat.file=opt$sumMatFile
cat("Normalize bias...\n")
sample.pair=Bias_normalize(mat.file=mat.file,sample.name = sample.name,
sample.class=sample.class, sample.condition = sample.condition,
target=target.class,control=control.class,
adjbias.dir = norm.matrix.dir, sum.adjmat.file=sum.mat.file
)