0
|
1 ## How to run tool
|
|
2 # $ Rscript my_r_tool.R
|
|
3 # --input1 input1.csv
|
|
4 # --input2 input2.csv
|
|
5 # --output1 output.csv
|
|
6 # --output2 output2.csv
|
|
7
|
|
8 # Setup R error handling to go to stderr
|
|
9 options(show.error.messages=F, error=function(){cat(geterrmessage(),file=stderr());q("no",1,F)})
|
|
10 # We need to not crash galaxy with an UTF8 error on German LC settings.
|
|
11 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
|
|
12
|
|
13
|
|
14 library("csaw")
|
|
15 library("stringr")
|
|
16 library("data.table")
|
|
17 library("getopt")
|
3
|
18 library("Rsamtools")
|
0
|
19
|
|
20
|
|
21 options(stringAsfactors = FALSE, useFancyQuotes = FALSE)
|
|
22 # Take in trailing command line arguments
|
|
23
|
|
24 output <- commandArgs(trailingOnly=TRUE)[2]
|
|
25 inputs <- commandArgs(trailingOnly=TRUE)[1]
|
|
26
|
|
27 print(output)
|
|
28 print(inputs)
|
|
29
|
|
30 # Separate multiple input files into a list of individual files
|
|
31 files <- unlist(strsplit(inputs, ','))
|
|
32
|
3
|
33 # Index bamfiles
|
|
34 indexBam(files = files)
|
|
35
|
0
|
36 # Create windows and count reads in them ----
|
|
37 Sys.time()
|
|
38 windows <- windowCounts(files, spacing=150, width=200, bin=F)
|
|
39 Sys.time()
|
|
40
|
|
41 df <- data.frame(rowRanges(windows), stringsAsFactors = F)
|
|
42 df <- df[,c(1:3)]
|
|
43
|
|
44 file_names <- basename(data.frame(colData(windows))$bam.files)
|
|
45
|
|
46
|
|
47 # Final table with all windows and read counts ----
|
|
48 table <- data.frame(df, assay(windows), stringsAsFactors = F, check.names = F)
|
|
49 colnames(table)[4:ncol(table)] <- file_names
|
|
50
|
|
51 # Remove spaces in the table ----
|
|
52 setDT(table)
|
|
53 for (j in names(table)) set(table, j = j, value = table[[trimws(j)]])
|
6
|
54 table_sp <- data.frame(table)
|
|
55
|
0
|
56 # Save final table ----
|
6
|
57 fwrite(x = table_sp, file = output, quote = F, row.names = F, sep = "\t")
|
5
|
58 dt <- table[,regions:=paste0(seqnames,"-", start, "-", end)]
|
|
59 table_sp <- data.frame(dt)
|
|
60
|
|
61 for(i in 4:(ncol(table_sp)-1)){
|
|
62 tmp <- table_sp[,c(ncol(table_sp), i)]
|
7
|
63 n <- paste0(colnames(table_sp)[i],".tsv")
|
6
|
64 fwrite(x = tmp, file = n, quote = F, row.names = F, sep = "\t")
|
5
|
65 }
|
0
|
66
|
|
67 # # Save individual files ----
|
|
68 # Sys.time()
|
|
69 # r <- paste(table_sp[,1], table_sp[,2], table_sp[,3], sep = "-")
|
|
70 # Sys.time()
|
|
71 # # r <- apply( table_sp[ ,c(1:3)] , 1 , paste , sep = "-" )
|
|
72 #
|
|
73 # dir <- paste(opt$outdir, "counts_each_sample", sep = "/")
|
|
74 # dir.create(dir)
|
|
75 #
|
|
76 # # cores <- detectCores()
|
|
77 # # cl <- makeCluster(cores)
|
|
78 # # registerDoParallel(cl)
|
|
79 #
|
|
80 # tab <- data.frame(regions = r, table_sp[,4:ncol(table_sp)], stringsAsFactors = F, check.names = F)
|
|
81 #
|
|
82 # # foreach(i = 2:ncol(tab)) %dopar% {
|
|
83 # for(i in 2:ncol(tab)){
|
|
84 # print(i)
|
|
85 # tmp <- data.frame(tab[,c(1,i)], stringsAsFactors = F, check.names = F)
|
|
86 # n <- paste(dir, "/", colnames(tab)[i], ".txt", sep = "")
|
|
87 # # write.table(tmp, xzfile(paste(dir, "/", n, ".txt.xz", sep = "")), sep = "\t", quote = F, row.names = F)
|
|
88 # fwrite(x = tmp, file = n, quote = F, row.names = F, sep = "\t")
|
|
89 # system(paste0("xz -3 -T 12 ", n))
|
|
90 # }
|
|
91 # # stopCluster(cl)
|
|
92
|
|
93 sessionInfo() |