-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcombine.pca.r
140 lines (103 loc) · 4.31 KB
/
combine.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
library(data.table)
library(ggplot2)
setwd("~/SCRATCH/tsne")
source("./tsne.util.r")
load("./rna/TCGA.rna.filter1.allgene.pca.rda")
pca.rna = pca
rm(list=c("pca"))
patient.rna = substr(rownames(pca.rna$x), 1, 12)
load("./mirna/TCGA.mirna.filter1.allgene.pca.rda")
pca.mirna = pca2
rm(list=c("pca2"))
patient.mirna = substr(rownames(pca.mirna$x), 1, 12)
load("./meth/TCGA.met.pca.perm100.rda")
pca.met = pca
rm(list=c("pca"))
patient.met = substr(rownames(pca.met$x), 1, 12)
load("./cnv/TCGA.cnv.1m.pca.rda")
pca.cnv = pca2
rm(list=c("pca2"))
patient.cnv = substr(rownames(pca.cnv$x), 1, 12)
# Merge PCs
use = 200
types = c("rna", "met", "cnv", "mirna")
weight = data.frame(type = types)
weight$weight = c(3, 3, 1, 1)
varexp = rbind( cbind(var=pca.met$varexp, type="met", index=1:length(pca.met$varexp)),
cbind(var=pca.rna$varexp, type="rna", index=1:length(pca.rna$varexp)),
cbind(var=pca.mirna$varexp, type="mirna", index=1:length(pca.mirna$varexp)),
cbind(var=pca.cnv$varexp, type="cnv", index=1:length(pca.cnv$varexp)))
varexp = data.table(varexp)
varexp$var = as.numeric(varexp$var)
varexp$index = as.integer(varexp$index)
# put on weight
varexp = merge(varexp, weight, by="type")
varexp$adj.var = varexp$var * varexp$weight
# order by variation explained
varexp = varexp[order(-varexp$adj.var), ]
# pick top ones
varexp = varexp[1:use, ]
temp = aggregate(varexp$var, by=list(varexp$type), FUN=length)
names(temp) = c("type", "numPC")
weight = merge(weight, temp, by="type")
temp = aggregate(varexp$var, by=list(varexp$type), FUN=sum)
names(temp) = c("type", "varexp")
weight = merge(weight, temp, by="type")
rownames(weight) = weight$type
# common patient
patients = Reduce(intersect, list(patient.met, patient.rna, patient.mirna, patient.cnv))
weight = weight[match(types, weight$type), ]
weight$patient = list(patient.rna, patient.met, patient.cnv, patient.mirna)
weight$patient.index = lapply(weight$patient, function(x) match(patients, x))
weight$sumvar = c( sum(pca.rna$sdev[1: weight$numPC[weight$type=="rna"]] ^2),
sum(pca.met$sdev[1: weight$numPC[weight$type=="met"]] ^2),
sum(pca.cnv$sdev[1: weight$numPC[weight$type=="cnv"]] ^2),
sum(pca.mirna$sdev[1: weight$numPC[weight$type=="mirna"]] ^2)
)
weight$adj.factor = sqrt(weight$weight / weight$sumvar)
PC = cbind( pca.rna$x[unlist(weight$patient.index[weight$type=="rna"]), 1:weight$numPC[weight$type=="rna"]] * weight$adj.factor[weight$type=="rna"],
pca.met$x[unlist(weight$patient.index[weight$type=="met"]), 1:weight$numPC[weight$type=="met"]] * weight$adj.factor[weight$type=="met"],
pca.cnv$x[unlist(weight$patient.index[weight$type=="cnv"]), 1:weight$numPC[weight$type=="cnv"]] * weight$adj.factor[weight$type=="cnv"],
pca.mirna$x[unlist(weight$patient.index[weight$type=="mirna"]), 1:weight$numPC[weight$type=="mirna"]] * weight$adj.factor[weight$type=="mirna"]
)
rownames(PC) = patients
save(PC, file="combined.PC.20161108_3_3_1_1.rda")
library(Rtsne)
ptm <- proc.time()
tsne3 = tsne(PC, permute=1000, use=use, dims=3)
proc.time() - ptm
# set expected contribution
# variable name "count"
# replace sample names to barcode
meta = fread("./meta/mirna.meta.txt")
m = match(rownames(tsne3), substr(meta$barcode, 1, 12))
tsne3$project = meta$disease[m]
save(tsne3, file="combined.tsne3.PC200.dim3.1_1_1_1.perm1000.rda")
png("combined.tsne2.3311.PC200.perm1000.png", width=1280, height=800)
p = ggplot(tsne2, aes(x=feature_1, y=feature_2, col=project, shape = project)) +
geom_point(size = 2) +
scale_shape_manual(values=c(rep(c(0, 2, 8, 16, 17), 6), 0, 2, 8) )
p
dev.off()
#####
low expr
m = apply(tpm2, 1, mean)
w = which(m > 1e-6)
tpm3 = tpm2[w, ]
pca3 = pca(tpm3)
save(pca3, file="TCGA.rna.filter1.lowexpr.pca.rda")
# t-SNE
library(Rtsne)
tsne3 = tsne(pca3$x, permute=100, use=50)
save(tsne3, file="TCGA.rna.lowexpr.tsne.rda")
meta$aliquot = meta$barcode
pdata3 = annotate.tsne(tsne3, meta)
pdata3 = pdata3[, c("aliquot", "feature_1", "feature_2", "disease", "center")]
colnames(pdata3)[4] = "project"
write.table(pdata3, "TCGA.rna.lowexpr.pdata.txt", col.names=T, row.names=F, sep="\t", quote=F)
png("TCGA.rna.lowexpr.pdata.perm100.png", width=1200, height=800)
p = ggplot(pdata3, aes(x=feature_1, y=feature_2, col=project, shape = project)) +
geom_point(size = 2) +
scale_shape_manual(values=c(rep(c(0, 2, 8, 16, 17), 6), 0, 2, 8) )
p
dev.off()