forked from britishcoffee/MeH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tobed.R
48 lines (40 loc) · 1.88 KB
/
tobed.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
# Loading packages
foo <- function(x){
for( i in x ){
# require returns TRUE invisibly if it was able to load package
if( ! require( i , character.only = TRUE , quietly = TRUE) ){
# If package was not able to be loaded then re-install
install.packages( i , dependencies = TRUE )
# Load package after installing
require( i , character.only = TRUE , quietly = TRUE)
}
}
}
foo( c("argparser" , "roperators", "dplyr", "foreach","doParallel") )
cat("[",format(Sys.time(), "%X"),"]","Start proccessing","\n")
p <- arg_parser("MeH result file to .bedGraph")
# Add command line arguments
p <- add_argument(p,"-m","Meh", help="input Meh resultes csv file")
# p <- add_argument(p,"-o", "--output", help="output to bedGraph file")
p <- add_argument(p,"-r", "reverse", default="all" ,help="reverse strand as negative MeH")
# Parse the command line arguments
args <- parse_args(p)
CG <- read.csv(args$m,header=TRUE)
CG=CG[which(apply(CG,1,function(x) sum(is.na(x)))==0),]
if( args$r == "all") {
for (i in 1:dim(CG)[2]){
if (!colnames(CG)[i] %in% c("chrom","bin","strand")){
write.table(x = cbind(CG$chrom,format(CG$bin+(CG$strand=="r"), scientific = FALSE),
format(CG$bin+1+(CG$strand=="r"), scientific = FALSE),CG[,i]),
file= gsub(" ","",paste("PW_",colnames(CG)[i],".bedGraph")), row.names = FALSE, sep = " ",col.names = FALSE,quote = FALSE)
}}
}
if (args$r == "n") {
for (i in 1:dim(CG)[2]){
if (!colnames(CG)[i] %in% c("chrom","bin","strand")){
write.table(x = cbind(CG$chrom,format(CG$bin, scientific = FALSE),
format(CG$bin+1, scientific = FALSE),CG[,i]*(2*(CG$strand=="f")-1)),
file= gsub(" ","",paste("PW_",colnames(CG)[i],".bedGraph")), row.names = FALSE, sep = " ",col.names = FALSE,quote = FALSE)
}}
}
cat("[",format(Sys.time(), "%X"),"]","Done.","\n")