-
Notifications
You must be signed in to change notification settings - Fork 66
/
template_script_edgeR_CL.r
executable file
·174 lines (146 loc) · 7.89 KB
/
template_script_edgeR_CL.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
################################################################################
### R script to compare several conditions with the SARTools and edgeR packages
### Hugo Varet
### March 23rd, 2022
### designed to be executed with SARTools 1.8.1
### run "Rscript template_script_edgeR_CL.r --help" to get some help
################################################################################
rm(list=ls()) # remove all the objects from the R session
library(optparse) # to run the script in command lines
# options list with associated default value.
option_list <- list(
make_option(c("-P", "--projectName"),
default=basename(getwd()),
dest="projectName",
help="name of the project used for the report [default: name of the current directory]."),
make_option(c("-A", "--author"),
default=Sys.info()[7],
dest="author",
help="name of the report author [default: %default]."),
make_option(c("-t", "--targetFile"),
default="target.txt",
dest="targetFile",
help="path to the design/target file [default: %default]."),
make_option(c("-r", "--rawDir"),
default="raw",
dest="rawDir",
help="path to the directory containing the HTSeq files [default: %default]."),
make_option(c("-F", "--featuresToRemove"),
default="alignment_not_unique,ambiguous,no_feature,not_aligned,too_low_aQual",
dest="FTR",
help="names of the features to be removed, more than once can be specified [default: %default]"),
make_option(c("-v", "--varInt"),
default="group",
dest="varInt",
help="factor of interest [default: %default]"),
make_option(c("-c", "--condRef"),
default="WT",
dest="condRef",
help="reference biological condition [default: %default]"),
make_option(c("-b", "--batch"),
default=NULL,
dest="batch",
help="blocking factor [default: %default] or \"batch\" for example"),
make_option(c("-a", "--alpha"),
default=0.05,
dest="alpha",
help="threshold of statistical significance [default: %default]"),
make_option(c("-p", "--pAdjustMethod"),
default="BH",
dest="pAdjustMethod",
help="p-value adjustment method: \"BH\" or \"BY\" [default: %default]"),
make_option(c("-m", "--cpmCutoff"),
default=1,
dest="cpmCutoff",
help="counts-per-million cut-off to filter low counts"),
make_option(c("-g", "--gene.selection"),
default="pairwise",
dest="gene.selection",
help="selection of the features in MDSPlot [default: %default]"),
make_option(c("-n", "--normalizationMethod"),
default="TMM",
dest="normalizationMethod",
help="normalization method in calcNormFactors: \"TMM\", \"RLE\" or \"upperquartile\" [default: %default]"),
make_option(c("-C", "--colors"),
default="#f3c300,#875692,#f38400,#a1caf1,#be0032,#c2b280,#848482,#008856,#e68fac,#0067a5",
dest="cols",
help="colors of each biological condition on the plots\n\t\t\"col1,col2,col3,col4\"\n\t\t[default: %default]"),
make_option(c("-f", "--forceCairoGraph"),
action="store_true",
default=FALSE,
dest="forceCairoGraph",
help="activate cairo type")
)
# now parse the command line to check which option is given and get associated values
parser <- OptionParser(usage="usage: %prog [options]",
option_list=option_list,
description="Compare two or more biological conditions in a RNA-Seq framework with edgeR.",
epilogue="For comments, bug reports etc... please contact Hugo Varet <[email protected]>")
opt <- parse_args(parser, args=commandArgs(trailingOnly=TRUE), positional_arguments=0)$options
# get options and arguments
workDir <- getwd()
projectName <- opt$projectName # name of the project
author <- opt$author # author of the statistical analysis/report
targetFile <- opt$targetFile # path to the design/target file
rawDir <- opt$rawDir # path to the directory containing raw counts files
featuresToRemove <- unlist(strsplit(opt$FTR, ",")) # names of the features to be removed (specific HTSeq-count information and rRNA for example)
varInt <- opt$varInt # factor of interest
condRef <- opt$condRef # reference biological condition
batch <- opt$batch # blocking factor: NULL (default) or "batch" for example
alpha <- as.numeric(opt$alpha) # threshold of statistical significance
pAdjustMethod <- opt$pAdjustMethod # p-value adjustment method: "BH" (default) or "BY"
gene.selection <- opt$gene.selection # selection of the features in MDSPlot
normalizationMethod <- opt$normalizationMethod # normalization method in calcNormFactors
cpmCutoff <- opt$cpmCutoff # counts-per-million cut-off to filter low counts
colors <- unlist(strsplit(opt$cols, ",")) # vector of colors of each biologicial condition on the plots
forceCairoGraph <- opt$forceCairoGraph # force cairo as plotting device if enabled
# print(paste("workDir", workDir))
# print(paste("projectName", projectName))
# print(paste("author", author))
# print(paste("targetFile", targetFile))
# print(paste("rawDir", rawDir))
# print(paste("varInt", varInt))
# print(paste("condRef", condRef))
# print(paste("batch", batch))
# print(paste("alpha", alpha))
# print(paste("pAdjustMethod", pAdjustMethod))
# print(paste("featuresToRemove", featuresToRemove))
# print(paste("colors", colors))
# print(paste("gene.selection", gene.selection))
# print(paste("normalizationMethod", normalizationMethod))
# print(paste("cpmCutoff", cpmCutoff))
################################################################################
### running script ###
################################################################################
# setwd(workDir)
library(SARTools)
if (forceCairoGraph) options(bitmapType="cairo")
# checking parameters
problem <- checkParameters.edgeR(projectName=projectName,author=author,targetFile=targetFile,
rawDir=rawDir,featuresToRemove=featuresToRemove,varInt=varInt,
condRef=condRef,batch=batch,alpha=alpha,pAdjustMethod=pAdjustMethod,
cpmCutoff=cpmCutoff,gene.selection=gene.selection,
normalizationMethod=normalizationMethod,colors=colors)
if (problem) quit(save="yes")
# loading target file
target <- loadTargetFile(targetFile=targetFile, varInt=varInt, condRef=condRef, batch=batch)
# loading counts
counts <- loadCountData(target=target, rawDir=rawDir, featuresToRemove=featuresToRemove)
# description plots
majSequences <- descriptionPlots(counts=counts, group=target[,varInt], col=colors)
# edgeR analysis
out.edgeR <- run.edgeR(counts=counts, target=target, varInt=varInt, condRef=condRef,
batch=batch, cpmCutoff=cpmCutoff, normalizationMethod=normalizationMethod,
pAdjustMethod=pAdjustMethod)
# MDS + clustering
exploreCounts(object=out.edgeR$dge, group=target[,varInt], gene.selection=gene.selection, col=colors)
# summary of the analysis (boxplots, dispersions, export table, nDiffTotal, histograms, MA plot)
summaryResults <- summarizeResults.edgeR(out.edgeR, group=target[,varInt], counts=counts, alpha=alpha, col=colors)
# save image of the R session
save.image(file=paste0(projectName, ".RData"))
# generating HTML report
writeReport.edgeR(target=target, counts=counts, out.edgeR=out.edgeR, summaryResults=summaryResults,
majSequences=majSequences, workDir=workDir, projectName=projectName, author=author,
targetFile=targetFile, rawDir=rawDir, featuresToRemove=featuresToRemove, varInt=varInt,
condRef=condRef, batch=batch, alpha=alpha, pAdjustMethod=pAdjustMethod, cpmCutoff=cpmCutoff,
colors=colors, gene.selection=gene.selection, normalizationMethod=normalizationMethod)