-
Notifications
You must be signed in to change notification settings - Fork 1
/
.Rhistory
138 lines (138 loc) · 6.42 KB
/
.Rhistory
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
library(WriteXLS)
library(readxl)
library(biomaRt)
library(pheatmap)
library(RColorBrewer)
library(jaffelab)
library(reshape2)
library(parallel)
library(ggplot2)
library(beeswarm)
library(nlme)
library(lmerTest)
cleaningY = function(y, mod, P=ncol(mod)) {
Hat=solve(t(mod)%*%mod)%*%t(mod)
beta=(Hat%*%t(y))
cleany=y-t(as.matrix(mod[,-c(1:P)])%*%beta[-c(1:P),])
return(cleany)
}
reverselog_trans <- function(base = exp(1)) {
trans <- function(x) -log(x, base)
inv <- function(x) base^(-x)
trans_new(paste0("reverselog-", format(base)), trans, inv,
log_breaks(base = base),
domain = c(1e-100, Inf))
}
#################################
#get Ensembl mouse to human genes
ensembl=useMart("ensembl",dataset = 'mmusculus_gene_ensembl')
MMtoHG = getBM(attributes = c('ensembl_gene_id','hsapiens_homolog_ensembl_gene'),mart = ensembl)
###########################
#load human ASD/Dup15q data
load('asd/rdas/asd_DE_objects_DESeq2_Qual.rda',envir = asd <- new.env())
asd$outGeneList = lapply(asd$outGeneList[grep('Qual',names(asd$outGeneList))],function(g){
tmpRowNames = ss(rownames(g),'\\.')
ind = !duplicated(tmpRowNames)
tmp = g[ind,]; rownames(tmp) = tmpRowNames[ind]
return(tmp)
})
##########################
# load mouse tcf4 DEG data
load("tcf4_mouse/rdas/mega_tcf4_ages_DE_objects_DESeq2.rda") #PTHS mouse data
outGeneList = lapply(outGeneList, function(g) {
g$hsapien_homolog = MMtoHG$hsapiens_homolog_ensembl_gene[match(rownames(g),MMtoHG$ensembl_gene_id)]
g[grepl('ENSG',g$hsapien_homolog),]})
load('mecp2/rdas/mecp2_DE_objects_DESeq2.rda',envir = mecp2 <- new.env()) #Rett mouse data
mecp2$outGene = with(mecp2$outGene, mecp2$outGene[padj<0.05 & !is.na(padj),])
load('pten/rdas/pten_ages_DE_objects_DESeq2.rda',envir = pten <- new.env()) #PTEN mouse data
pten$outGene = with(pten$outGeneList[['Adult']], pten$outGeneList[['Adult']][padj<0.05 & !is.na(padj),])
############################################
# load 34 convergent genes from mouse models
load('tcf4_mouse/rdas/overlap_tcf4_mecp2_pten.rda')
outGene1 = outGeneList[['Adult']][sigGenes,] #myelination genes
outGene1 = outGene1[complete.cases(outGene1),]
outGene2 = outGeneList[['Adult']] #all PTHS mouse DEGs
outGene2 = outGene2[outGene2$padj<0.05 & !is.na(outGene2$padj),]
outGene2 = outGene2[complete.cases(outGene1),]
# genes
outGene3 = outGeneList[['Adult']][!rownames(outGeneList[['Adult']]) %in% rownames(mecp2$outGene) &
!rownames(outGeneList[['Adult']]) %in% rownames(pten$outGene) &
outGeneList[['Adult']]$padj <0.05 & !is.na(outGeneList[['Adult']]$padj),]
# number of 34 genes DE in human ASD/Dup15q
sapply(asd$outGeneList,function(g) with(g,sum(pvalue<0.05 & rownames(g) %in% outGene1$hsapien_homolog)))
######################################
# load in expression RPKM of human ASD
if(FALSE){
load('/dcl01/lieber/ajaffe/Brady/asd/rpkmCounts_asd_dec9_n204.rda')
save(geneRpkm, pd, geneMap,file = '/dcl01/lieber/ajaffe/Brady/asd/geneRpkm_asd_dec9_n204.rda')
} else {
load('/dcl01/lieber/ajaffe/Brady/asd/geneRpkm_asd_dec9_n204.rda')
}
load('asd/rdas/pheno.rda',envir = dat<-new.env())
pd = cbind(pd,dat$pd)
all.equal(pd$SampleID,pd$SAMPLE_ID) #samples line up
rownames(pd) = pd$SAMPLE_ID
pd$Diagnosis = factor(ifelse(pd$Detailed.Diagnosis =="Chromosome 15q Duplication Syndrome",
'Dup15',pd$Diagnosis), levels = c("CTL", "ASD",'Dup15'))
########################################
# residualize on confounding and qSVs
# load qSV's
load('asd/rdas/qSVAs-geschwind_asd.rdas')
mod = model.matrix(~Diagnosis + totalAssignedGene + Sequencing.Batch +
Brain.Bank + RIN + Age + Sex, data =pd)
modAdj = cbind(mod,qSVs)
pd = pd[order(pd$Diagnosis),]
#yGene = cleaningY(log2(geneRpkm+1),modAdj,3)
yGene = log2(geneRpkm+1)
yGene = yGene[,rownames(pd)]
geneRpkm = geneRpkm[,rownames(pd)]
######################
# pca on the CAG genes
whichGenes = rownames(geneMap)[match(outGene1$hsapien_homolog,geneMap$ensemblID)]
whichGenes = whichGenes[!is.na(whichGenes)]
#pca1 = prcomp(t(yGene[whichGenes,]))
pca1 = prcomp(t(log2(geneRpkm+1)[whichGenes,]))
# pca1resid = prcomp(t(yGene[whichGenes,]))
## clean the PCs, not expression
pca1resid = t(cleaningY(t(pca1$x), modAdj, P=3))
boxplot(pca1resid[,1]~pd$Diagnosis,main = 'Covergent Mouse Gene Set PCA',xlab = '',outline=FALSE,
ylab= paste0('Eigengene (',signif(summary(pca1)$importance[2,1]*100,2),'% of Variance)'))
col = brewer.pal(3,'Set1')
beeswarm(pca1resid[,1]~pd$Diagnosis,corral = 'wrap',add = T,pch = 20, pwbg = col[as.numeric(factor(pd$Region))])
###############################################
# pca on the all PTHS DEGs in human ASD and 15q
whichGenes = rownames(geneMap)[match(outGene2$hsapien_homolog,geneMap$ensemblID)]
whichGenes = whichGenes[!is.na(whichGenes)]
pca2 = prcomp(t(log2(geneRpkm+1)[whichGenes,]))
# pca2resid = prcomp(t(yGene[whichGenes,]))
## clean the PCs, not expression
pca2resid = t(cleaningY(t(pca2$x), modAdj, P=3))
boxplot(pca2resid[,1]~pd$Diagnosis,main = 'All PTHS Gene Set PCA',xlab = '',outline = FALSE,
ylab= paste0('Eigengene (',signif(summary(pca2)$importance[2,1]*100,2),'% of Variance)'))
col = brewer.pal(3,'Set1')
beeswarm(pca2resid[,1]~pd$Diagnosis,corral = 'wrap',add = T,pch = 20, pwbg = col[as.numeric(factor(pd$Region))])
########################################
# residualize on confounding and qSVs
# load qSV's
load('asd/rdas/qSVAs-geschwind_asd.rdas')
mod = model.matrix(~Diagnosis + totalAssignedGene + Sequencing.Batch +
Brain.Bank + RIN + Age + Sex, data =pd)
modAdj = cbind(mod,qSVs)
pd = pd[order(pd$Diagnosis),]
#yGene = cleaningY(log2(geneRpkm+1),modAdj,3)
yGene = log2(geneRpkm+1)
yGene = yGene[,rownames(pd)]
geneRpkm = geneRpkm[,rownames(pd)]
###############################################
# pca on the all PTHS DEGs in human ASD and 15q
whichGenes = rownames(geneMap)[match(outGene2$hsapien_homolog,geneMap$ensemblID)]
whichGenes = whichGenes[!is.na(whichGenes)]
pca2 = prcomp(t(log2(geneRpkm+1)[whichGenes,]))
# pca2resid = prcomp(t(yGene[whichGenes,]))
## clean the PCs, not expression
pca2resid = t(cleaningY(t(pca2$x), modAdj, P=3))
boxplot(pca2resid[,1]~pd$Diagnosis,main = 'All PTHS Gene Set PCA',xlab = '',outline = FALSE,
ylab= paste0('Eigengene (',signif(summary(pca2)$importance[2,1]*100,2),'% of Variance)'))
col = brewer.pal(3,'Set1')
beeswarm(pca2resid[,1]~pd$Diagnosis,corral = 'wrap',add = T,pch = 20, pwbg = col[as.numeric(factor(pd$Region))])
rm(list = ls())