-
Notifications
You must be signed in to change notification settings - Fork 11
/
plot_corrplot_rg.R
92 lines (73 loc) · 2.81 KB
/
plot_corrplot_rg.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
library(corrplot)
library(reshape2)
# quantitative traits
TRAIT_CATEGORY1 = c(
'Anthropometric', 'Metabolic', 'Protein', 'Kidney-related', 'Electrolyte',
'Liver-related', 'Other biochemical', 'Hematological', 'Blood pressure', 'Echocardiographic'
)
# diseases
TRAIT_CATEGORY2 = c(
'Metabolic disease', 'Cardiovascular disease', 'Allergic disease', 'Autoimmune disease', 'Infectious disease',
'Hematologic disease', 'Psychiatric disease', 'Musculoskeletal disease', 'Tumor', 'Other'
)
################################################################################
args = commandArgs(trailingOnly=T)
if (identical(args, character(0))) {
args = paste0("./input_example/", c("input_rg.txt", "traitlist.txt"))
}
rg_fname = args[1]
traitlist_fname = args[2]
################################################################################
# load data
rg = read.table(rg_fname, T, sep='\t', as.is = T)
trait_all = read.table(traitlist_fname, T, sep = '\t', as.is = T, quote = '', fileEncoding='utf-8', comment.char="")
traitlist1 = subset(trait_all, CATEGORY %in% TRAIT_CATEGORY1)
traitlist2 = subset(trait_all, CATEGORY %in% TRAIT_CATEGORY2)
# duplicate lines
tmp = rg
tmp$p1 = rg$p2
tmp$p2 = rg$p1
tmp$p1_category = rg$p2_category
tmp$p2_category = rg$p1_category
rg = rbind(rg, tmp)
################################################################################
corrplot_nsquare = function(rg, trait1, trait2, traits_use = NULL, order = "original", landscape=FALSE) {
if (landscape & length(trait1) > length(trait2)) {
tmp = trait1
trait1 = trait2
trait2 = tmp
}
if (!is.null(traits_use)) {
rg = subset(rg, p1 %in% traits_use & p2 %in% traits_use)
}
x2 = dcast(rg, p1 ~ p2, value.var = "rg")
mat2 = as.matrix(x2[, 2:ncol(x2)])
rownames(mat2) = x2$p1
mat2[mat2 > 1] = 1
mat2[mat2 < -1] = -1
mat2[is.na(mat2)] = 0
x2 = dcast(rg, p1 ~ p2, value.var = "q")
qmat2 = as.matrix(x2[, 2:ncol(x2)])
rownames(qmat2) = x2$p1
qmat2[is.na(qmat2)] = 1
if (nrow(mat2) == ncol(mat2)) {
diag(mat2) = 1
diag(qmat2) = -1
}
trait1 = match(trait1, rownames(mat2))
trait2 = match(trait2, colnames(mat2))
mat2 = mat2[trait1, trait2]
qmat2 = qmat2[trait1, trait2]
corrplot(mat2, method = "psquare", order = order,
p.mat = qmat2, sig.level = 0.05, sig = "pch",
pch = "*", pch.cex = 1.5, full_col=FALSE,
na.label = "square", na.label.col = "grey30")
# return(list(mat = mat2, qmat = qmat2))
}
cairo_pdf("./output/corrplot_rg_disease.pdf", width = 16, height = 9, family = "Helvetica")
corrplot_nsquare(rg, traitlist1$TRAIT, traitlist2$TRAIT, landscape=TRUE)
dev.off()
png("./output/corrplot_rg_disease.png", width = 16, height = 9, units = 'in', res = 300, family = "Helvetica")
corrplot_nsquare(rg, traitlist1$TRAIT, traitlist2$TRAIT, landscape=TRUE)
dev.off()
warnings()