-
Notifications
You must be signed in to change notification settings - Fork 4
/
load_metadata.R
220 lines (197 loc) · 7.82 KB
/
load_metadata.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
#!/usr/bin/R
# ------------------------------------------
# Load metadata for the multiregion project:
# ------------------------------------------
library(cbrbase)
library(RColorBrewer)
library(tidyr)
set_proj('DEVTRAJ')
# Arguments:
args = commandArgs()
if (length(args) > 0){
project = args[1]
} else {
project = 'RNA_Regions'
}
# ---------------
# Metadata files:
# ---------------
mfiles = list.files(path='Annotation', pattern='^metadata_[A-Z]*.tsv')
regions = sub(".tsv", "", sub("metadata_", "",mfiles))
reg.nomb = regions[regions != 'MB']
NREGIONS = length(regions)
reg.order = c('All','AG','MT','PFC','EC','HC','TH')
# Load in metadata:
metadatafile = 'Annotation/regions_metadata_list.Rda'
if (!file.exists(metadatafile)){
metalist = list()
for (i in 1:NREGIONS){
tab = read.delim(paste0('Annotation/', mfiles[i]), header=T, sep="\t")
tab$region = regions[i]
metalist[[regions[i]]] = tab
if (i == 1){
kcols = colnames(tab)
} else {
kcols = intersect(kcols, colnames(tab))
}
}
kcols = kcols[kcols != 'X']
redmetalist = list()
for (i in 1:NREGIONS){
redmetalist[[regions[i]]] = metalist[[regions[i]]][,kcols]
}
metadata = do.call(rbind, redmetalist)
metadata$rind = rownames(metadata)
metadata$cogdxad = 'CTRL'
metadata$cogdxad[metadata$cogdx %in% c(4,5)] = 'AD'
metadata$cogdxad = factor(metadata$cogdxad, levels=c('CTRL','AD'))
metadata$nrad = 'CTRL'
metadata$nrad[metadata$niareagansc %in% c(1,2)] = 'AD'
metadata$nrad = factor(metadata$nrad, levels=c('CTRL','AD'))
# Late and early braak stages:
metadata$braaksc.ad = 'CTRL'
metadata$braaksc.ad[metadata$braaksc >= 5] = 'AD'
metadata$braaksc.ad= factor(metadata$braaksc.ad, levels=c('CTRL','AD'))
metadata$braaksc.early = 'CTRL'
metadata$braaksc.early[metadata$braaksc >= 3] = 'AD'
metadata$braaksc.early = factor(metadata$braaksc.early, levels=c('CTRL','AD'))
save(metadata, metalist, file=metadatafile)
} else {
load(metadatafile)
}
kept.individuals = scan('Annotation/multiRegion_individuals.txt', 'c')
# -------------------
# Genome Annotations:
# -------------------
gencode_version = 'v28lift37.annotation'
gencoderda = paste0('Annotation/Processed.gene.', gencode_version, '.Rda')
if (!file.exists(gencoderda)){
geneanno = read.delim(paste0('Annotation/Gene.', gencode_version, '.bed'), header=F, stringsAsFactors=F)
anno = read.delim(paste0('Annotation/Tss.', gencode_version, '.bed'), header=F, stringsAsFactors=F)
names(geneanno) <- c('chr','start', 'end', 'strand', 'ENSG','type','symbol')
names(anno) <- c('chr','tss','ENSG','type','symbol')
anno$length_35 = geneanno$end - geneanno$start
# -----------------------
# Get transcript lengths:
# -----------------------
alt.txlenfile = paste('Annotation/ExonMerge.Homo_sapiens.GRCh37.87.gene.totals.bed')
alt.txdf = read.table(alt.txlenfile, header=F)
names(alt.txdf) = c('symbol','tx.length','version')
txlenfile = paste0('gencode.',gencode_version,'.txlen.tsv')
if (file.exists(txlenfile)){
print("Loading transcript lengths")
txdf = read.delim(txlenfile, sep="\t", header=T)
} else {
# Import GTF:
require(rtracklayer)
print("Reading in GTF to get transcript lengths")
GTFfile = paste0('Annotation/gencode.',gencode_version,'.gtf')
GTF <- import.gff(GTFfile, format="gtf", genome="GRCh37", feature.type="exon")
# Process gtf to get transcript length:
grl <- reduce(split(GTF, elementMetadata(GTF)$gene_id))
reducedGTF <- unlist(grl, use.names=T)
elementMetadata(reducedGTF)$gene_id <- rep(names(grl), elementNROWS(grl))
elementMetadata(reducedGTF)$widths <- width(reducedGTF)
calc_length <- function(x) { sum(elementMetadata(x)$widths) }
txlen <- t(sapply(split(reducedGTF, elementMetadata(reducedGTF)$gene_id), calc_length))
txdf = data.frame(ENSG=colnames(txlen), length=as.numeric(txlen))
write.table(txdf, txlenfile, sep="\t", quote=F, col.names=T, row.names=F)
}
# Update annotation:
anno = merge(anno, txdf)
save(anno, file=gencoderda)
} else {
load(gencoderda)
}
# --------------------
# Colors for plotting:
# --------------------
source(paste0(sbindir, 'set_colors.R'))
# -------------------------------
# Read in the full cell metadata:
# -------------------------------
datadir = 'multiRegion/'
prefix = 'all_brain_regions_filt_preprocessed_scanpy_norm'
lblset = 'leiden_r15_n100'
# Only for sub-clustering:
load(paste0(datadir, prefix, '.', lblset, '.ext.lbl.Rda'))
hlvls = as.character(sort(unique(celldf$lbl)))
lbl.cols = rep(snap.cols,4)[1:length(hlvls)]
names(lbl.cols) = as.character(hlvls)
if (length(grep('hdb', lblset)) > 0){ ctype = 'HDBSCAN' } else { ctype = 'Leiden' }
# --------------------------------------
# Load in the final metadata (cellmeta):
# --------------------------------------
load(file=paste0(datadir, prefix, '.final_noMB.cell_labels.Rda'))
# Colors for full:
typelvls = unique(cellmeta$cell_type_high_resolution)
type.cols = rep(snap.cols,3)[1:length(typelvls)]
names(type.cols) = as.character(typelvls)
type.cols = c(type.cols, major.col['Inh'], major.col['Exc'])
tsp.type.cols = sapply(type.cols, tsp.col)
sampmeta.rds = 'Annotation/multiregion_sample_metadata.Rds'
if (!file.exists(sampmeta.rds)){
uqrind = unique(cellmeta$rind)
saveRDS(metadata[metadata$rind %in% uqrind,], file=sampmeta.rds)
}
cellmeta.rds = 'Annotation/multiregion_cell_metadata.Rds'
if (!file.exists(cellmeta.rds)){
sel.cols = c('barcode','rind','region','projid','U1','U2',
'major.celltype','minor.celltype','neuronal.layer','inh.subtype',
'neuronal.exttype','full.exttype','cell_type_high_resolution')
saveRDS(cellmeta[,sel.cols], file=cellmeta.rds)
}
full.projids = sort(unique(cellmeta$projid))
projid.cols = snap.cols[1:length(full.projids)]
names(projid.cols) = full.projids
# All multiregion color sets, for visualization:
# ----------------------------------------------
col.rds = 'Annotation/multiregion_color_sets.Rds'
if (!file.exists(col.rds)){
mrc = list()
mrc[['major']] = major.col
mrc[['major.tsp']] = tsp.major.col
mrc[['covariates']] = colvals
mrc[['individual']] = ind.cols
mrc[['lbl']] = lbl.cols
mrc[['region']] = reg.cols[reg.nomb]
mrc[['region_label']] = reg.long[reg.nomb]
mrc[['celltype']] = tcols
mrc[['celltype.tsp']] = tsp.tcols
mrc[['Paired']] = col.paired
mrc[['Reds']] = colr
mrc[['Blues']] = colb
mrc[['RdBu']] = colrb
mrc[['extra']] = snap.cols
mrc[['projid']] = projid.cols
saveRDS(mrc, file=col.rds)
}
# Get the pathology mapped to each region:
# ----------------------------------------
pathrda = 'Annotation/multiregion_pathology_df.Rda'
if (!file.exists(pathrda)){
pqdf = NULL
for (path in c('nft','plaq_d','plaq_n')){
regmap = c('AG','HC','PFC','MT','EC')
names(regmap) = c('ag','hip','mf','mt','ec')
vars = colnames(metadata)[grep(path, colnames(metadata))]
vars = vars[vars != path]
submeta = unique(metadata[,c('projid','region', vars, 'rind')])
slong = gather(submeta, path, value, -projid, -region, -rind)
slong$path.region = regmap[sub(".*_","", slong$path)]
slong = slong[slong$region == slong$path.region,]
rownames(slong) = slong$rind
if (is.null(pqdf)){
pqdf = slong[,c('rind','value','region')]
names(pqdf)[2] = path
} else {
sub.pqdf = slong[,c('rind','value','region')]
names(sub.pqdf)[2] = path
pqdf = merge(pqdf, sub.pqdf)
}
}
save(pqdf, file=pathrda)
} else { load(pathrda) }
# Excitatory neuron to region annotations:
# ----------------------------------------
source(paste0(sbindir, 'set_neuron_subsets.R'))