annotate csaw.R @ 0:66356a1014b1 draft

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