-
Notifications
You must be signed in to change notification settings - Fork 0
/
t1d_proteomics_project.r
389 lines (275 loc) · 10.8 KB
/
t1d_proteomics_project.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
load.meta.t1d.pieper <- function(file.name) {
meta =read.delim(file.name, header=TRUE,stringsAsFactors=T, sep="\t")
make.global(meta)
meta$ProteomicsID = factor(sub("-",".",as.character(meta$ProteomicsID),fixed=T))
rownames(meta) = meta$ProteomicsID
##Setting Control to base level plays nice with DESeq2 defaults
meta$T1D = relevel(meta$T1D,"Control")
return (meta)
}
read.pieper.t1d <- function(count.file="Original Collapesed APEX (All information).AT.tsv",
meta.file="metadata.Family_groups_T1D-C.txt",
load.meta.method=NULL,
taxa.level=NULL) {
require(data.table)
file_name = count.file
#data = read.csv(file_name, as.is=TRUE, header=TRUE,stringsAsFactors=T)
#data = read.delim(file_name, header=T,sep="\t",stringsAsFactors=FALSE)
data = fread(file_name, header=T, sep="\t", stringsAsFactors=FALSE, data.table=F)
#row.names(meta) = meta$ID
id.ind = 13
feat.attr.ind.last = 15
feat.attr = data[,c(1:feat.attr.ind.last)]
row.names(feat.attr) = data[,id.ind]
row.names(data) = data[,id.ind]
data = data[,-c(1:feat.attr.ind.last)]
data = t(data)
make.global(data)
meta = as.data.frame(t(as.data.frame(strsplit(row.names(data),"[-_]"))))
make.global(meta)
row.names(meta) = row.names(data)
names(meta) = c("Group","DummyID","TechReplRelID")
meta = within(meta,{
ProteomicsID = factor(paste(Group,DummyID,sep="."))
})
meta$SampleID = row.names(meta)
meta.bio = do.call(load.meta.method,
list(file.name=meta.file)
)
meta = join(meta,meta.bio,by="ProteomicsID",match="first")
meta = within(meta,{
UnitID = factor(paste(FamilyID,Group,sep="."))
})
m_a = list(count=data,attr=meta,feat.attr=feat.attr)
make.global(m_a)
return(m_a)
}
## This function should carry out analysis specific to metadata fields by themselves, without
## relation to the abundance profiles. You can write it to do nothing (empty body).
summary.meta.t1d.proteomics <- function(m_a) {
require(doBy)
report$add.header("Summary of metadata variables")
meta = m_a$attr
report$add.printed(summary(meta),caption="Summary of metadata variables")
#xtabs.formulas = list("Group~","Group~FamilyID","FamilyID~.")
xtabs.formulas = list("TechReplRelID~FamilyID+Group+SubjectID",
"SubjectID~FamilyID+Group",
"Group~FamilyID"
)
fact.xtabs = meta
#xtabs.formulas = list("~SubjectID","~FamilyID+SubjectID","~SubjectID+TechReplRelID")
for(xtabs.formula in xtabs.formulas) {
fact.xtabs = summaryBy(as.formula(xtabs.formula),data=fact.xtabs,FUN=length,keep.names=T)
report$add(fact.xtabs,caption=paste("Sample cross tabulation",xtabs.formula))
#report$add.printed(summary(fact.xtabs))
}
}
## This function must generate a list with analysis tasks
gen.tasks.t1d.prot <- function() {
norm.count.task.drop.other = within(list(), {
method = "norm.ident"
})
mgsat.proteomics.task.template = within(mgsat.16s.task.template, {
label.base = "prot"
read.data.method=read.pieper.t1d
read.data.task = list(
count.file=NULL,
meta.file=NULL,
load.meta.method=load.meta.t1d.pieper
)
summary.meta.method=summary.meta.t1d.proteomics
taxa.levels = c("prot")
count.filter.sample.options=list()
test.counts.task = within(test.counts.task, {
do.deseq2 = F
do.genesel=T
do.stabsel=F
do.glmer=F
do.adonis=F
do.divrich=c()
do.plot.profiles.abund=F
do.heatmap.abund=F
do.select.samples=c()
feature.ranking = "genesel"
count.filter.feature.options=list(min_incidence_frac=0.25)
#count.filter.feature.options=list()
norm.count.task = norm.count.task.drop.other
plot.profiles.abund.task = within(plot.profiles.abund.task, {
norm.count.task = norm.count.task.drop.other
})
})
})
get.taxa.meta.aggr.base<-function(m_a) {
m_a = aggregate.by.meta.data.m_a(m_a,
group_col="UnitID",
count_aggr=mean)
report$add.p(paste("After aggregating by averaging samples per family/condition:",nrow(m_a$count)))
return (m_a)
}
task0 = within( mgsat.proteomics.task.template, {
descr = "All samples, aggregated by UnitID (family/condition)"
read.data.task = within(read.data.task, {
count.file="Original Collapesed APEX (All information).AT.tsv"
meta.file="metadata.Family_groups_T1D-C.txt"
})
get.taxa.meta.aggr<-function(m_a) {
return (get.taxa.meta.aggr.base(m_a))
}
})
task1 = within( task0, {
descr = "All samples, aggregated by UnitID (family/condition), paired Wilcoxon test"
do.summary.meta = F
do.tests = T
summary.meta.task = within(summary.meta.task, {
meta.x.vars = NULL
group.vars = main.meta.var
})
test.counts.task = within(test.counts.task, {
do.stabsel=F
do.adonis=F
do.plot.profiles.abund=F
do.heatmap.abund=F
genesel.task = within(genesel.task, {
genesel.param = within(genesel.param, {
block.attr = "FamilyID"
type="paired"
replicates=400
maxrank=20
samp.fold.ratio=0.5
comp.log.fold.change=T
})
})
adonis.task = within(adonis.task, {
#dist.metr="euclidean"
#col.trans="standardize"
norm.count.task=NULL
data.descr="normalized counts"
tasks = list(
list(formula.rhs=main.meta.var,
strata=NULL,
descr="Association with the patient/control status unpaired"),
list(formula.rhs=main.meta.var,
strata="FamilyID",
descr="Association with the patient/control status paired by family")
)
})
})
})
task2 = within( task1, {
descr = "All samples, aggregated by UnitID (family/condition), unpaired Wilcoxon test"
do.summary.meta = T
do.tests = T
test.counts.task = within(test.counts.task, {
do.genesel=T
do.stabsel=T
do.adonis=T
do.plot.profiles.abund=T
do.heatmap.abund=T
do.extra.method = taxa.levels
genesel.task = within(genesel.task, {
genesel.param = within(genesel.param, {
type="unpaired"
})
})
extra.method.task = within(extra.method.task, {
func = function(m_a,m_a.norm,res.tests) {
test.dist.matr.within.between(m_a=m_a.norm,
group.attr="Group",
block.attr="FamilyID",
n.perm=4000)
}
})
})
})
task.verify.biomarkers.power = within( task1, {
descr = "All samples, aggregated by UnitID (family/condition), power analysis for biomarker verification study"
do.summary.meta = F
do.tests = T
test.counts.task = within(test.counts.task, {
do.genesel=F
do.stabsel=F
do.adonis=F
do.plot.profiles.abund=F
do.heatmap.abund=F
do.extra.method = taxa.levels
extra.method.task = within(extra.method.task, {
func = function(m_a,m_a.norm,res.tests,id.markers) {
verification.power(m_a=m_a.norm,
group.attr="Group",
id.markers=id.markers)
}
id.markers = c("P17050","O00754","P53634",
"P20774","Q9BTY2","P02750",
"Q14393","P40197","Q96HD9",
"P14151","Q13421","Q6UXB8",
"P19320","Q9BYF1","P08195",
"Q13740","P33151","P04066",
"P13473","Q9H0E2")
})
})
})
task.assoc.power = within( task1, {
descr = "All samples, aggregated by UnitID (family/condition), power analysis for association study"
do.summary.meta = F
do.tests = T
test.counts.task = within(test.counts.task, {
do.genesel=F
do.stabsel=F
do.adonis=F
do.plot.profiles.abund=F
do.heatmap.abund=F
do.extra.method = taxa.levels
count.filter.feature.options=list(min_incidence_frac=0.25)
extra.method.task = within(extra.method.task, {
func = function(m_a,m_a.norm,res.tests,id.markers) {
wilcox.power(m_a=m_a.norm,
group.attr="Group",
id.markers=id.markers,R=2000,n=85)
}
id.markers = c("P17050","O00754","P53634",
"P20774","Q9BTY2","P02750",
"Q14393","P40197","Q96HD9",
"P14151","Q13421","Q6UXB8",
"P19320","Q9BYF1","P08195",
"Q13740","P33151","P04066",
"P13473","Q9H0E2")
})
})
})
return (list(task.assoc.power))
return (list(task.verify.biomarkers.power))
return (list(task1,task2))
}
## number of cores to use on multicore machines
options(mc.cores=4)
options(boot.ncpus=4)
## parallel backend
options(boot.parallel="snow")
library("BiocParallel")
register(SnowParam(4))
## location of MGSAT code
MGSAT_SRC = "~/work/mgsat"
source(paste(MGSAT_SRC,"dependencies.r",sep="/"),local=T)
## Uncomment next line to install packages needed by MGSAT (!!!comment it out
## in all subsequent runs once the packages have been installed!!!).
## Note: you should also pre-install Pandoc program from http://johnmacfarlane.net/pandoc/
## or using your OS package manager (if running on Linux)
#install_required_packages()
## loads dependency packages (which already must be installed)
load_required_packages()
## loads MGSAT code
source(paste(MGSAT_SRC,"report_pandoc.r",sep="/"),local=T)
source(paste(MGSAT_SRC,"power_and_tests.r",sep="/"),local=T)
## leave with try.debug=F for production runs
set_trace_options(try.debug=T)
## set incremental.save=T only for debugging or demonstration runs - it forces
## report generation after adding every header section, thus slowing down
## a long run. But then incremental.save=T, you can open HTML report file in
## a Web browser and refresh it periodically to see it grow.
report <- PandocAT$new(author="atovtchi@jcvi.org",
title="Analysis of T1D proteomics data",
incremental.save=F)
res = proc.project(
task.generator.method=gen.tasks.t1d.prot
)
report$save()