annotate minfi_pipeline.R @ 0:84361ce36a11 draft

planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
author nturaga
date Tue, 19 Apr 2016 11:10:25 -0400
parents
children a78f84fc4873
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
1 # setup R error handling to go to stderr
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
2 options(show.error.messages=F, error=function(){cat(geterrmessage(),file=stderr());q("no",1,F)})
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
3
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
4 # we need that to not crash galaxy with an UTF8 error on German LC settings.
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
5 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
6
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
7 library("getopt")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
8 options(stringAsfactors = FALSE, useFancyQuotes = FALSE)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
9 args <- commandArgs(trailingOnly = TRUE)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
10
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
11 # get options, using the spec as defined by the enclosed list.
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
12 # we read the options from the default: commandArgs(TRUE).
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
13 spec <- matrix(c(
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
14 'quiet', 'q', 2, "logical",
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
15 'help' , 'h', 0, "logical",
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
16 "preprocess","p",1,"character",
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
17 "cores","c",1,"integer",
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
18 "numPositions","n",2,"integer",
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
19 "shrinkVar","s",2,"logical",
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
20 "b_permutations","b",2,"integer",
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
21 "smooth","m",2,"logical",
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
22 "cutoff","t",2,"double",
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
23 "l_value","l",2,"integer")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
24 ,byrow=TRUE, ncol=4)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
25 opt <- getopt(spec)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
26
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
27 # If help was asked for print a friendly message
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
28 # and exit with a non-zero error code
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
29 if (!is.null(opt$help)) {
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
30 cat(getopt(spec, usage=TRUE))
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
31 q(status=1)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
32 }
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
33
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
34
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
35 ## Set verbose mode
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
36 verbose = if(is.null(opt$quiet)){TRUE}else{FALSE}
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
37 if(verbose){
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
38 cat("Verbose mode is ON\n\n")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
39 }
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
40
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
41 # Enforce the following required arguments
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
42 if (is.null(opt$preprocess)) {
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
43 cat("'--preprocess' is required\n")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
44 q(status=1)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
45 }
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
46 cat("verbose = ", opt$quiet,"\n")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
47 cat("preprocess = ",opt$preprocess,"\n")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
48 cat("cores = ", opt$cores, "\n")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
49 cat("b_permutations = ",opt$b_permutations,"\n")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
50 cat("smooth = ",opt$smooth,"\n")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
51 cat("cutoff = ",opt$cutoff,"\n")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
52 cat("l_value = ",opt$l_value,"\n")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
53 cat("numPositions = ",opt$numPositions,"\n")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
54 cat("shrinkVar = ",opt$shrinkVar,"\n")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
55
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
56
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
57 # Load required libraries
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
58 suppressPackageStartupMessages({
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
59 library("minfi")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
60 library("FlowSorted.Blood.450k")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
61 library("TxDb.Hsapiens.UCSC.hg19.knownGene")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
62 library("doParallel")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
63 })
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
64
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
65
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
66 ## Parse cheetah code and make dataframe for creating tmp dir
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
67 minfi_config_file = paste0("minfi_temp","/minfi_config.txt")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
68 minfi_config = read.table(minfi_config_file)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
69 colnames(minfi_config) = c("status","green","red","name")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
70
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
71
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
72 ## Make the tmpdir for symlinking data
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
73 base_dir = paste0("minfi_temp","/base")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
74 system(paste0("mkdir ",base_dir))
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
75
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
76
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
77 ## Make symlinks of files
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
78 for (i in 1:nrow(minfi_config)){
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
79 stopifnot(nrow(minfi_config) == nrow(minfi_config["name"]))
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
80
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
81 ## Make green idat file symlinks
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
82 file_green = paste0(base_dir,"/",as.character(minfi_config[i,"name"]),"_Grn.idat")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
83 cmd_green = paste("ln -s",as.character(minfi_config[i,"green"]),file_green,sep=" ")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
84 cat("Reading file ",i,"GREEN Channel ", file_green)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
85 system(cmd_green)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
86
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
87 ## Make red idat file symlinks
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
88 file_red = paste0(base_dir,"/",as.character(minfi_config[i,"name"]),"_Red.idat")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
89 cmd_red = paste("ln -s",as.character(minfi_config[i,"red"]),file_red,sep=" ")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
90 cat("Reading file ",i,"RED Channel ", file_red)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
91 system(cmd_red)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
92 }
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
93
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
94 ## Make dataframe with Basenames
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
95 Basename = paste0(base_dir,"/",unique(substr(list.files(base_dir),1,17)))
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
96 status = minfi_config[match(gsub(".+/","",Basename), minfi_config$name),"status"]
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
97 targets = data.frame(Basename=Basename,status=status)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
98
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
99 if ( verbose ) {
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
100 cat("Minfi targets file:\n\n ")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
101 print(targets)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
102 }
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
103
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
104 ## Read 450k files
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
105 RGset = read.450k.exp(targets=targets,verbose=T)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
106
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
107 if (verbose){
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
108 cat("RGset has been read: \n\n")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
109 print(RGset)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
110 }
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
111
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
112 pd = pData(RGset)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
113
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
114 ## NOTE: QC report is for samples before normalization
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
115 ## QC REPORT
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
116 files = gsub(".+/","",pd$filenames)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
117 ## Produce PDF file
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
118 if (!is.null(RGset)) {
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
119 # Make PDF of QC report
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
120 minfi::qcReport(rgSet=RGset,sampNames=files,sampGroups=pd$status,pdf="qc_report.pdf")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
121 }
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
122
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
123
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
124 ## MDS Plot
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
125 ## Set phenotype data
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
126 files = gsub(".+/","",pd$filenames)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
127 #numPositions=as.integer("${numPositions}")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
128
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
129 ## Produce PDF file
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
130 if (!is.null(RGset)) {
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
131 ## Make PDF of MDS plot
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
132 pdf("mds_plot.pdf")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
133 minfi::mdsPlot(dat=RGset,sampNames=files,sampGroups=pd$status,main="Beta MDS",numPositions = opt$numPositions,pch=19)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
134 dev.off()
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
135 }
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
136
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
137 if(verbose){
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
138 cat("Made plot of QC report and MDS plot\n\n")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
139 }
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
140
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
141
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
142 ## Preprocess data with the normalization method chosen
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
143 if(opt$preprocess == "quantile"){
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
144 normalized_RGset = preprocessQuantile(RGset)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
145 if (verbose){cat("Preprocessed using Quantile normalization")};
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
146 } else if (opt$preprocess == "funnorm"){
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
147 normalized_RGset = preprocessFunnorm(RGset)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
148 if(verbose){print("Preprocessed using Functional normalization")}
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
149 } else if (opt$preprocess == "noob"){
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
150 normalized_RGset = preprocessNoob(RGset)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
151 if (verbose){cat("Preprocessed using Noob normalization")};
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
152 } else if (opt$preprocess == "illumina"){
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
153 normalized_RGset = preprocessIllumina(RGset,bg.correct = TRUE, normalize = c("controls", "no"),reference = 1)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
154 if(verbose){print("Preprocessed using Illumina normalization")}
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
155 } else if (opt$preprocess == "swan"){
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
156 normalized_RGset = preprocessSWAN(RGset)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
157 if(verbose){print("Preprocessed using SWAN normalization")}
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
158 }else {
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
159 normalized_RGset = RGset
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
160 if(verbose){print("Preprocessed using No normalization")}
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
161 }
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
162
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
163
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
164
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
165 ## Get beta values from Proprocessed data
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
166 beta = getBeta(normalized_RGset)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
167 ## Set phenotype data
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
168 pd = pData(normalized_RGset)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
169
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
170
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
171 ## DMP finder
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
172 dmp = dmpFinder(dat=beta,pheno=pd$status,type="categorical",shrinkVar=opt$shrinkVar)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
173 write.csv(dmp,file="dmps.csv",quote=FALSE,row.names=TRUE)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
174 if(verbose){
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
175 cat("\n","DMP Finder successful \n")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
176 }
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
177
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
178
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
179 # Model Matrix to pass into the bumphunter function
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
180 T1= levels(pd$status)[2]
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
181 T2= levels(pd$status)[1]
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
182
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
183 # Introduce error if levels are different
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
184 stopifnot(T1!=T2)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
185
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
186 keep=pd$status%in%c(T1,T2)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
187 tt=factor(pd$status[keep],c(T1,T2))
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
188 design=model.matrix(~tt)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
189
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
190 if(verbose){
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
191 cat("Model matrix is: \n")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
192 design
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
193 }
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
194
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
195 # Start bumphunter in a parallel environment
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
196 # Parallelize over cores on machine
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
197 registerDoParallel(cores = opt$cores)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
198
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
199 ## Bumphunter Run with normalized_RGset processed with Quantile Normalization
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
200 if (is(normalized_RGset,"GenomicRatioSet")) {
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
201 res=bumphunter(normalized_RGset[,keep],design,B=opt$b_permutations,smooth=opt$smooth,cutoff= opt$cutoff,type="Beta")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
202 bumps= res$tab
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
203 } else if(is(normalized_RGset,"MethylSet")) {
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
204 # convert MethylSet (norm.swan) into GenomicRatioSet through Ratioset
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
205 normalized_RGset = ratioConvert(normalized_RGset, what = "both", keepCN = TRUE)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
206 normalized_RGset = mapToGenome(normalized_RGset)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
207 res = bumphunter(normalized_RGset[,keep],design=design,B=opt$b_permutations,smooth=opt$smooth,cutoff=opt$cutoff)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
208 bumps = res$tab
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
209 } else {
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
210 # This else case is never supposed to run,
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
211 # it will run only if the normalized_RGset object
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
212 # did not have the expected class type
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
213 cat("Bumphunter did not run properly","\n")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
214 stopifnot(!is.null(bumps))
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
215 }
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
216
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
217 if(verbose){
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
218 cat("Bumphunter result", "\n")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
219 head(bumps)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
220 }
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
221
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
222
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
223 ## Choose DMR's of a certain length threshold.
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
224 ## This helps reduce the size of DMRs early, and match
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
225 ## with genes closest to region
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
226 bumps = bumps[bumps$L > opt$l_value,]
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
227 genes <- annotateTranscripts(TxDb.Hsapiens.UCSC.hg19.knownGene)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
228 tab=matchGenes(bumps,genes)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
229 annotated_dmrs=cbind(bumps,tab)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
230
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
231 if(verbose){
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
232 cat("Match with annotation\n")
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
233 head(annotated_dmrs)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
234 }
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
235
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
236 # Save result, which contains DMR's and closest genes
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
237 write.csv(annotated_dmrs,file = "dmrs.csv",quote=FALSE,row.names=FALSE)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
238
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
239 # Garbage collect
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
240 gc()
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
241
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
242
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
243 ## TODO: FIX BLOCK FINDER
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
244 # Block finder
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
245 #library(sva)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
246 #pheno <- pData(GRset)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
247 #mod <- model.matrix(~as.factor(status), data=pheno)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
248 #mod0 <- model.matrix(~1, data=pheno)
84361ce36a11 planemo upload commit fb90aafc93e5e63acfcdac4c27cfd865cdf06c5a-dirty
nturaga
parents:
diff changeset
249 #sva.results <- sva(mval, mod, mod0)