annotate csaw.R @ 0:96f6fb4babd0 draft

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