3
|
1 ################################################################################
|
|
2 ### R script to compare several conditions with the SARTools and DESeq2 packages
|
|
3 ### Hugo Varet
|
|
4 ### March 20th, 2018
|
|
5 ### designed to be executed with SARTools 1.6.3
|
|
6 ### run "Rscript template_script_DESeq2_CL.r --help" to get some help
|
|
7 ################################################################################
|
|
8
|
|
9 rm(list=ls()) # remove all the objects from the R session
|
|
10 library(optparse) # to run the script in command lines
|
|
11
|
|
12 # options list with associated default value.
|
|
13 option_list <- list(
|
|
14 make_option(c("-P", "--projectName"),
|
|
15 default=basename(getwd()),
|
|
16 dest="projectName",
|
|
17 help="name of the project used for the report [default: name of the current directory]."),
|
|
18
|
|
19 make_option(c("-A", "--author"),
|
|
20 default=Sys.info()[7],
|
|
21 dest="author",
|
|
22 help="name of the report author [default: %default]."),
|
|
23
|
|
24 make_option(c("-t", "--targetFile"),
|
|
25 default="target.txt",
|
|
26 dest="targetFile",
|
|
27 help="path to the design/target file [default: %default]."),
|
|
28
|
|
29 make_option(c("-r", "--rawDir"),
|
|
30 default="raw",
|
|
31 dest="rawDir",
|
|
32 help="path to the directory containing the HTSeq files [default: %default]."),
|
|
33
|
|
34 make_option(c("-F", "--featuresToRemove"),
|
|
35 default="alignment_not_unique,ambiguous,no_feature,not_aligned,too_low_aQual",
|
|
36 dest="FTR",
|
|
37 help="names of the features to be removed, more than once can be specified [default: %default]"),
|
|
38
|
|
39 make_option(c("-v", "--varInt"),
|
|
40 default="group",
|
|
41 dest="varInt",
|
|
42 help="factor of interest [default: %default]"),
|
|
43
|
|
44 make_option(c("-c", "--condRef"),
|
|
45 default="WT",
|
|
46 dest="condRef",
|
|
47 help="reference biological condition [default: %default]"),
|
|
48
|
|
49 make_option(c("-b", "--batch"),
|
|
50 default=NULL,
|
|
51 dest="batch",
|
|
52 help="blocking factor [default: %default] or \"batch\" for example"),
|
|
53
|
|
54 make_option(c("-f", "--fitType"),
|
|
55 default="parametric",
|
|
56 dest="fitType",
|
|
57 help="mean-variance relationship: [default: %default], local or mean"),
|
|
58
|
|
59 make_option(c("-o", "--cooksCutoff"),
|
|
60 default=TRUE,
|
|
61 dest="cooksCutoff",
|
|
62 help="perform the outliers detection (default is TRUE)"),
|
|
63
|
|
64 make_option(c("-i", "--independentFiltering"),
|
|
65 default=TRUE,
|
|
66 dest="independentFiltering",
|
|
67 help="perform independent filtering (default is TRUE)"),
|
|
68
|
|
69 make_option(c("-a", "--alpha"),
|
|
70 default=0.05,
|
|
71 dest="alpha",
|
|
72 help="threshold of statistical significance [default: %default]"),
|
|
73
|
|
74 make_option(c("-p", "--pAdjustMethod"),
|
|
75 default="BH",
|
|
76 dest="pAdjustMethod",
|
|
77 help="p-value adjustment method: \"BH\" or \"BY\" [default: %default]"),
|
|
78
|
|
79 make_option(c("-T", "--typeTrans"),
|
|
80 default="VST",
|
|
81 dest="typeTrans",
|
|
82 help="transformation for PCA/clustering: \"VST\" ou \"rlog\" [default: %default]"),
|
|
83
|
|
84 make_option(c("-l", "--locfunc"),
|
|
85 default="median",
|
|
86 dest="locfunc",
|
|
87 help="median or shorth to estimate the size factors [default: %default]"),
|
|
88
|
|
89 make_option(c("-C", "--colors"),
|
|
90 default="dodgerblue,firebrick1,MediumVioletRed,SpringGreen,chartreuse,cyan,darkorchid,darkorange",
|
|
91 dest="cols",
|
|
92 help="colors of each biological condition on the plots\n\t\t\"col1,col2,col3,col4\"\n\t\t[default: %default]"),
|
|
93
|
|
94 make_option(c("-g", "--forceCairoGraph"),
|
|
95 action="store_true",
|
|
96 default=FALSE,
|
|
97 dest="forceCairoGraph",
|
|
98 help="activate cairo type")
|
|
99
|
|
100 )
|
|
101
|
|
102 # now parse the command line to check which option is given and get associated values
|
|
103 parser <- OptionParser(usage="usage: %prog [options]",
|
|
104 option_list=option_list,
|
|
105 description="Compare two or more biological conditions in a RNA-Seq framework with DESeq2.",
|
|
106 epilogue="For comments, bug reports etc... please contact Hugo Varet <hugo.varet@pasteur.fr>")
|
|
107 opt <- parse_args(parser, args=commandArgs(trailingOnly=TRUE), positional_arguments=0)$options
|
|
108
|
|
109 # get options and arguments
|
|
110 workDir <- getwd()
|
|
111 projectName <- opt$projectName # name of the project
|
|
112 author <- opt$author # author of the statistical analysis/report
|
|
113 targetFile <- opt$targetFile # path to the design/target file
|
|
114 rawDir <- opt$rawDir # path to the directory containing raw counts files
|
|
115 featuresToRemove <- unlist(strsplit(opt$FTR, ",")) # names of the features to be removed (specific HTSeq-count information and rRNA for example)
|
|
116 varInt <- opt$varInt # factor of interest
|
|
117 condRef <- opt$condRef # reference biological condition
|
|
118 batch <- opt$batch # blocking factor: NULL (default) or "batch" for example
|
|
119 fitType <- opt$fitType # mean-variance relationship: "parametric" (default), "local" or "mean"
|
|
120 cooksCutoff <- opt$cooksCutoff # outliers detection threshold (NULL to let DESeq2 choosing it)
|
|
121 independentFiltering <- opt$independentFiltering # TRUE/FALSE to perform independent filtering (default is TRUE)
|
|
122 alpha <- as.numeric(opt$alpha) # threshold of statistical significance
|
|
123 pAdjustMethod <- opt$pAdjustMethod # p-value adjustment method: "BH" (default) or "BY"
|
|
124 typeTrans <- opt$typeTrans # transformation for PCA/clustering: "VST" ou "rlog"
|
|
125 locfunc <- opt$locfunc # "median" (default) or "shorth" to estimate the size factors
|
|
126 colors <- unlist(strsplit(opt$cols, ",")) # vector of colors of each biologicial condition on the plots
|
|
127 forceCairoGraph <- opt$forceCairoGraph # force cairo as plotting device if enabled
|
|
128 # print(paste("workDir", workDir))
|
|
129 # print(paste("projectName", projectName))
|
|
130 # print(paste("author", author))
|
|
131 # print(paste("targetFile", targetFile))
|
|
132 # print(paste("rawDir", rawDir))
|
|
133 # print(paste("varInt", varInt))
|
|
134 # print(paste("condRef", condRef))
|
|
135 # print(paste("batch", batch))
|
|
136 # print(paste("fitType", fitType))
|
|
137 # print(paste("cooksCutoff", cooksCutoff))
|
|
138 # print(paste("independentFiltering", independentFiltering))
|
|
139 # print(paste("alpha", alpha))
|
|
140 # print(paste("pAdjustMethod", pAdjustMethod))
|
|
141 # print(paste("typeTrans", typeTrans))
|
|
142 # print(paste("locfunc", locfunc))
|
|
143 # print(paste("featuresToRemove", featuresToRemove))
|
|
144 # print(paste("colors", colors))
|
|
145
|
|
146 ################################################################################
|
|
147 ### running script ###
|
|
148 ################################################################################
|
|
149 # setwd(workDir)
|
|
150 library(SARTools)
|
|
151 if (forceCairoGraph) options(bitmapType="cairo")
|
|
152
|
|
153 # checking parameters
|
|
154 problem <- checkParameters.DESeq2(projectName=projectName,author=author,targetFile=targetFile,
|
|
155 rawDir=rawDir,featuresToRemove=featuresToRemove,varInt=varInt,
|
|
156 condRef=condRef,batch=batch,fitType=fitType,cooksCutoff=cooksCutoff,
|
|
157 independentFiltering=independentFiltering,alpha=alpha,pAdjustMethod=pAdjustMethod,
|
|
158 typeTrans=typeTrans,locfunc=locfunc,colors=colors)
|
|
159 if (problem) quit(save="yes")
|
|
160
|
|
161 # loading target file
|
|
162 target <- loadTargetFile(targetFile=targetFile, varInt=varInt, condRef=condRef, batch=batch)
|
|
163
|
|
164 # loading counts
|
|
165 counts <- loadCountData(target=target, rawDir=rawDir, featuresToRemove=featuresToRemove)
|
|
166
|
|
167 # description plots
|
|
168 majSequences <- descriptionPlots(counts=counts, group=target[,varInt], col=colors)
|
|
169
|
|
170 # analysis with DESeq2
|
|
171 out.DESeq2 <- run.DESeq2(counts=counts, target=target, varInt=varInt, batch=batch,
|
|
172 locfunc=locfunc, fitType=fitType, pAdjustMethod=pAdjustMethod,
|
|
173 cooksCutoff=cooksCutoff, independentFiltering=independentFiltering, alpha=alpha)
|
|
174
|
|
175 # PCA + clustering
|
|
176 exploreCounts(object=out.DESeq2$dds, group=target[,varInt], typeTrans=typeTrans, col=colors)
|
|
177
|
|
178 # summary of the analysis (boxplots, dispersions, diag size factors, export table, nDiffTotal, histograms, MA plot)
|
|
179 summaryResults <- summarizeResults.DESeq2(out.DESeq2, group=target[,varInt], col=colors,
|
|
180 independentFiltering=independentFiltering,
|
|
181 cooksCutoff=cooksCutoff, alpha=alpha)
|
|
182
|
|
183 # save image of the R session
|
|
184 save.image(file=paste0(projectName, ".RData"))
|
|
185
|
|
186 # generating HTML report
|
4
|
187 Sys.setenv(HOME = getwd())
|
3
|
188 writeReport.DESeq2(target=target, counts=counts, out.DESeq2=out.DESeq2, summaryResults=summaryResults,
|
|
189 majSequences=majSequences, workDir=workDir, projectName=projectName, author=author,
|
|
190 targetFile=targetFile, rawDir=rawDir, featuresToRemove=featuresToRemove, varInt=varInt,
|
|
191 condRef=condRef, batch=batch, fitType=fitType, cooksCutoff=cooksCutoff,
|
|
192 independentFiltering=independentFiltering, alpha=alpha, pAdjustMethod=pAdjustMethod,
|
|
193 typeTrans=typeTrans, locfunc=locfunc, colors=colors)
|