Repository navigation
Expand file tree
/
Copy pathload_funs.R
More file actions
executable file
·121 lines (105 loc) · 4.15 KB
/
Copy pathload_funs.R
File metadata and controls
executable file
·121 lines (105 loc) · 4.15 KB
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
## Load data
load_foo <- function(type) {
load_file <- file.path(
'/dcl01/lieber/ajaffe/lab/brainseq_phase2/expr_cutoff',
paste0('rse_', type, '.Rdata'))
stopifnot(file.exists(load_file))
load(load_file)
## Get the appropriate object
if(type == 'gene') {
rse <- rse_gene
} else if (type == 'exon') {
## Drop those 4 exons not present in BrainSpan
rse <- rse_exon[-c(175584, 175585, 175586, 175604), ]
} else if (type == 'jxn') {
rse <- rse_jxn
} else if (type == 'tx') {
rse <- rse_tx
}
## Keep controls only
rse <- rse[, colData(rse)$Dx == 'Control']
## Set as factor
colData(rse)$Region <- relevel(factor(colData(rse)$Region), 'DLPFC')
colData(rse)$Race <- relevel(factor(colData(rse)$Race), ref = 'CAUC')
colData(rse)$Sex <- relevel(factor(colData(rse)$Sex), ref = 'F')
## Add age linear splines
fetal <- ifelse(colData(rse)$Age < 0, 1,0)
birth <- colData(rse)$Age
birth[birth < 0] <- 0 # linear spline
infant <- colData(rse)$Age - 1
infant[infant < 0] <- 0 # linear spline
child <- colData(rse)$Age - 10
child[child < 0] <- 0 # linear spline
teen <- colData(rse)$Age - 20
teen[teen < 0] <- 0 # linear spline
adult <- colData(rse)$Age - 50
adult[adult < 0] <- 0 # linear spline
colData(rse)$fetal <- fetal
colData(rse)$birth <- birth
colData(rse)$infant <- infant
colData(rse)$child <- child
colData(rse)$teen <- teen
colData(rse)$adult <- adult
## Add means
colData(rse)$mean_mitoRate <- mean(colData(rse)$mitoRate)
colData(rse)$mean_totalAssignedGene <- mean(colData(rse)$totalAssignedGene)
## Makes the design matrix not full rank in one of the models
# colData(rse)$mean_rRNA_rate <- mean(colData(rse)$rRNA_rate)
colData(rse)$mean_RIN <- mean(colData(rse)$RIN)
return(rse)
}
load_span <- function(type) {
load_file <- file.path(
'/dcl01/lieber/ajaffe/lab/brainseq_phase2/brainspan',
paste0('rse_span_', type, '.Rdata'))
stopifnot(file.exists(load_file))
load(load_file)
## Get the appropriate object
if(type == 'gene') {
rse <- rse_span_gene
} else if (type == 'exon') {
rse <- rse_span_exon
} else if (type == 'jxn') {
rse <- rse_span_jxn
} else if (type == 'tx') {
rse <- rse_span_tx
}
print('Dimensions of the data used')
print(dim(rse))
## Add age linear splines
fetal <- ifelse(colData(rse)$Age < 0, 1,0)
birth <- colData(rse)$Age
birth[birth < 0] <- 0 # linear spline
infant <- colData(rse)$Age - 1
infant[infant < 0] <- 0 # linear spline
child <- colData(rse)$Age - 10
child[child < 0] <- 0 # linear spline
teen <- colData(rse)$Age - 20
teen[teen < 0] <- 0 # linear spline
adult <- colData(rse)$Age - 50
adult[adult < 0] <- 0 # linear spline
colData(rse)$fetal <- fetal
colData(rse)$birth <- birth
colData(rse)$infant <- infant
colData(rse)$child <- child
colData(rse)$teen <- teen
## None are adult in the BrainSpan data set
colData(rse)$adult <- adult
return(rse)
}
## Define models
fm_mod <- ~Age + fetal + birth + infant + child + teen + adult + Sex + snpPC1 + snpPC2 + snpPC3 + snpPC4 + snpPC5 + mean_mitoRate + mean_totalAssignedGene + mean_RIN
fm_mod0 <- ~ Age + Sex + snpPC1 + snpPC2 + snpPC3 + snpPC4 + snpPC5 + mean_mitoRate + mean_totalAssignedGene + mean_RIN
fm_mod_all <- ~Age *Region + fetal * Region + birth *Region + infant *Region + child * Region + teen * Region + adult * Region + Sex + Region + snpPC1 + snpPC2 + snpPC3 + snpPC4 + snpPC5 + mean_mitoRate + mean_totalAssignedGene + mean_RIN
fm_mod0_all <- ~ Age + fetal + birth + infant + child + teen + adult + Sex + Region + snpPC1 + snpPC2 + snpPC3 + snpPC4 + snpPC5 + mean_mitoRate + mean_totalAssignedGene + mean_RIN
get_mods <- function(pd, int = FALSE) {
### adjust for race, sex
if(int) {
mod = model.matrix(fm_mod_all, data=pd)
mod0 = model.matrix(fm_mod0_all, data=pd)
} else {
mod = model.matrix(fm_mod, data=pd)
mod0 = model.matrix(fm_mod0, data=pd)
}
return(list(mod = mod, mod0 = mod0))
}