-
Notifications
You must be signed in to change notification settings - Fork 1
/
extended.data.4e.pca.R
executable file
·216 lines (183 loc) · 10.7 KB
/
extended.data.4e.pca.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
###################################################################
# Code for preparation of normalized count tables for PCA plot
# in extended data 4 e
# visible under:
# https://www.nature.com/articles/s41556-019-0423-1/figures/11
###################################################################
# content of meta file: '/media/josephus/Elements/metas/meta-wt-k2-eo-5Ad-br-eo-5AD-br-GFPAD.txt'
#
# sampleName fileName condition testing
# wtn-1 /media/josephus/archive_big/kworkspace/count/JT-cells-hybrid-new/rsubread/grcm38/WTnewrep1-sym-fcount.tab wtn denom
# wtn-2 /media/josephus/archive_big/kworkspace/count/JT-cells-hybrid-new/rsubread/grcm38/WTnewrep2-sym-fcount.tab wtn denom
# wtn-3 /media/josephus/archive_big/kworkspace/count/JT-cells-hybrid-new/rsubread/grcm38/WTnewrep3-sym-fcount.tab wtn denom
# br-1 /media/josephus/archive_big/kworkspace/count/JT-cells-hybrid-new/rsubread/grcm38/br-1-sym-fcount.tab br
# br-2 /media/josephus/archive_big/kworkspace/count/JT-cells-hybrid-new/rsubread/grcm38/br-2-sym-fcount.tab br
# br-3 /media/josephus/archive_big/kworkspace/count/JT-cells-hybrid-new/rsubread/grcm38/br-3-sym-fcount.tab br
# eon-1 /media/josephus/archive_big/kworkspace/count/JT-cells-hybrid-new/rsubread/grcm38/EomesKOnewrep1-sym-fcount.tab eon
# eon-2 /media/josephus/archive_big/kworkspace/count/JT-cells-hybrid-new/rsubread/grcm38/EomesKOnewrep2-sym-fcount.tab eon
# eon-3 /media/josephus/archive_big/kworkspace/count/JT-cells-hybrid-new/rsubread/grcm38/EomesKOnewrep3-sym-fcount.tab eon
# k2-1 /media/josephus/archive_big/kworkspace/count/JT-cells-hybrid-new/rsubread/grcm38/k2-1-sym-fcount.tab k2 num
# k2-2 /media/josephus/archive_big/kworkspace/count/JT-cells-hybrid-new/rsubread/grcm38/k2-2-sym-fcount.tab k2 num
# k2-3 /media/josephus/archive_big/kworkspace/count/JT-cells-hybrid-new/rsubread/grcm38/k2-3-sym-fcount.tab k2 num
# br-5AD-1 /media/josephus/archive_big/kworkspace/count/JT-cells-second-new/subread/BraV5Actdoxrep1-sym-fcount.tab br5AD
# br-5AD-2 /media/josephus/archive_big/kworkspace/count/JT-cells-second-new/subread/BraV5Actdoxrep2-sym-fcount.tab br5AD
# br-5AD-3 /media/josephus/archive_big/kworkspace/count/JT-cells-second-new/subread/BraV5Actdoxrep3-sym-fcount.tab br5AD
# eo-5AD-1 /media/josephus/archive_big/kworkspace/count/JT-cells-second-new/subread/EoV5newActdoxrep1-sym-fcount.tab eo5AD
# eo-5AD-2 /media/josephus/archive_big/kworkspace/count/JT-cells-second-new/subread/EoV5newActdoxrep2-sym-fcount.tab eo5AD
# eo-5AD-3 /media/josephus/archive_big/kworkspace/count/JT-cells-second-new/subread/EoV5newActdoxrep3-sym-fcount.tab eo5AD
# brGFPD-1 /media/josephus/archive_big/kworkspace/count/BraChIP2018/rsubread/grcm38/BraGFPdox1-sym-fcount.tab brGFPD
# brGFPD-2 /media/josephus/archive_big/kworkspace/count/BraChIP2018/rsubread/grcm38/BraGFPdox2-sym-fcount.tab brGFPD
# brGFPD-3 /media/josephus/archive_big/kworkspace/count/BraChIP2018/rsubread/grcm38/BraGFPdox3-sym-fcount.tab brGFPD
# eoGFPD-1 /media/josephus/archive_big/kworkspace/count/BraChIP2018/rsubread/grcm38/EoGFPdox1-sym-fcount.tab eoGFPD
# eoGFPD-2 /media/josephus/archive_big/kworkspace/count/BraChIP2018/rsubread/grcm38/EoGFPdox2-sym-fcount.tab eoGFPD
# eoGFPD-3 /media/josephus/archive_big/kworkspace/count/BraChIP2018/rsubread/grcm38/EoGFPdox3-sym-fcount.tab eoGFPD
####################################################################
# load functions
####################################################################
source("/home/josephus/Dropbox/a_myRNAseq/rdownstream/190507.rdownstream.central.R")
####################################################################
# set thresholds
####################################################################
alpha <- 0.05 # padj BH limit
FDR <- 0.05 # false discovery rate, q-value
lFC <- 1 # log2FC limit
for (i in 1:1) {
####################################################################
# set basic variables
####################################################################
indirpath <- ""
dataname <- "wt-vs-k2-breo5AD-breoGFPAD"
outdirpath <- "/media/josephus/Elements/DEanalysis/wt-vs-k2-breo5AD-breoGFPAD"
metapath <- "/media/josephus/Elements/metas/meta-wt-k2-eo-5Ad-br-eo-5AD-br-GFPAD.txt"
####################################################################
# get meta file information
####################################################################
meta.df <- read.table(metapath, sep = '\t',
header = TRUE,
stringsAsFactors = FALSE)
meta.df$condition <- factor(meta.df$condition, # ensures preferred order
levels = unique(meta.df$condition))
denom <- function(meta.df) as.character(meta.df$condition[meta.df$testing == "denom"][1])
num <- function(meta.df) as.character(meta.df$condition[meta.df$testing == "num"][1])
core.name <- function(meta.df) paste0(num(meta.df), "-vs-", denom(meta.df), collapse = '')
####################################################################
# inferred paths
####################################################################
outdirpath <- file.path(outdirpath, core.name(meta.df))
meta.outdir <- file.path(outdirpath, "meta")
cnts.outdir <- file.path(outdirpath, "count-table")
DE.outdir <- file.path(outdirpath, "DE-table")
#####################################################################
# ensure existence of output paths
#####################################################################
dir.create(path=outdirpath, recursive = TRUE, showWarnings = FALSE)
dir.create(path=meta.outdir, recursive = TRUE, showWarnings = FALSE)
dir.create(path=cnts.outdir, recursive = TRUE, showWarnings = FALSE)
dir.create(path=DE.outdir, recursive = TRUE, showWarnings = FALSE)
#######################################################################
# Create meta table (for documentation)
#######################################################################
write.table(meta.df,
file = file.path(meta.outdir,
basename(metapath)),
sep = "\t",
row.names = FALSE,
quote = FALSE)
#######################################################################
# Create DESeq2 objects
#######################################################################
DESeq2.obj <- meta2DESeq2.obj(meta.df,
indirpath)
DESeq2.obj.disp <- meta2DESeq2.obj(meta.df,
indirpath,
normalized = TRUE)
#######################################################################
# Create the count tables (raw counts, normalized and averaged normalized counts)
#######################################################################
cnts.raw <- meta2cnts(meta.df,
DESeq2.obj,
outdirpath = cnts.outdir,
dataname = dataname,
printp = TRUE,
normalized = FALSE,
averaged = FALSE,
sheetName = "raw.all")
cnts.nrm <- meta2cnts(meta.df,
DESeq2.obj.disp,
outdirpath = cnts.outdir,
dataname = dataname,
printp = TRUE,
normalized = TRUE,
averaged = FALSE,
sheetName = "normalized.all")
cnts.avg.nrm <- meta2cnts(meta.df,
DESeq2.obj.disp,
outdirpath = cnts.outdir,
dataname = dataname,
printp = TRUE,
normalized = TRUE,
averaged = TRUE,
sheetName = "avg.normalized.all")
#######################################################################
# Create DE table
#######################################################################
res <- DEanalysis(meta.df,
DESeq2.obj.disp,
outdirpath = DE.outdir,
dataname = dataname,
printp = TRUE)
resSig <- DEanalysis(meta.df,
DESeq2.obj.disp,
outdirpath = DE.outdir,
dataname = dataname,
printp = TRUE,
filterp = TRUE,
alpha = alpha,
lFC = lFC)
# for reference, save name
cnts.DE.sig.fname <- paste0("DE-cnts-sig-",
dataname,
"_",
core.name(meta.df),
"_",
time.now(),
"-",
alpha,
"-",
lFC,
".xlsx")
# print sorting by name and p values
print.cnts.DE.sortings(cnts.nrm,
resSig,
cnts.DE.sig.fname,
dir = DE.outdir)
print.cnts.DE.sortings(cnts.avg.nrm$`nrm-counts-avg`,
resSig,
file.path(paste0("DE-cnts-avg-sig-",
dataname,
"_",
core.name(meta.df),
"_",
time.now(),
"-",
alpha,
"-",
lFC,
".xlsx")),
dir = DE.outdir)
#######################################################################
# Create Scaled Avg DE sig table (averaged, significant DE genes table)
#######################################################################
scaled.avg.DE.sig <- scale.raw.counts.with.SD(cnts.DE.sig.fpath = file.path(DE.outdir,
cnts.DE.sig.fname),
meta.fpath = metapath,
out.fpath="")
#######################################################################
# backup session image
#######################################################################
fpath <- file.path(outdirpath,
paste0("run",
time.now(),
".RData"))
save.image(file = fpath)
}