Diff of /6-Figure scripts/Fig 3.r [000000] .. [16eabd]

Switch to unified view

a b/6-Figure scripts/Fig 3.r
1
library(data.table)
2
library(dplyr)
3
library(reshape2)
4
library(ggplot2)
5
library(ggdendro)
6
library(readxl)
7
library(ggpubr)
8
9
list.files()
10
11
excel_sheets("Fig 3 Source Data.xlsx")
12
13
#Figure 3a ---------------------------------
14
CorrDf_all <- read_excel("Fig 3 Source Data.xlsx", sheet = "Fig 3 Source Data")
15
CorrDf_all
16
17
18
# only keep overlapped modules in each file
19
# NEU 
20
metaB.common <- intersect((CorrDf_all %>% filter(Mediation_type == 'MetaG-MetaB-NEU'))$Node2,
21
                          (CorrDf_all %>% filter(Mediation_type == 'MetaB-HostT-NEU'))$Node1)
22
trans.common <- intersect((CorrDf_all %>% filter(Mediation_type == 'MetaB-HostT-NEU'))$Node2,
23
                          (CorrDf_all %>% filter(Mediation_type == 'HostT-Spuprot-NEU'))$Node1)
24
25
CorrDf_NEU <-bind_rows(
26
  CorrDf_all %>% 
27
    filter(Mediation_type == 'MetaG-MetaB-NEU') %>%
28
    filter(Node2 %in% metaB.common),
29
  CorrDf_all %>% 
30
    filter(Mediation_type == 'MetaB-HostT-NEU') %>%
31
    filter(Node1 %in% metaB.common) %>%
32
    filter(Node2 %in% trans.common),
33
  CorrDf_all %>% 
34
    filter(Mediation_type == 'HostT-Spuprot-NEU') %>%
35
    filter(Node1 %in% trans.common)
36
)
37
38
39
# EOS
40
metaB.common <- intersect((CorrDf_all %>% filter(Mediation_type == 'MetaG-MetaB-EOS'))$Node2,
41
                          (CorrDf_all %>% filter(Mediation_type == 'MetaB-HostT-EOS'))$Node1)
42
trans.common <- intersect((CorrDf_all %>% filter(Mediation_type == 'MetaB-HostT-EOS'))$Node2,
43
                          (CorrDf_all %>% filter(Mediation_type == 'HostT-Spuprot-EOS'))$Node1)
44
45
CorrDf_EOS <- bind_rows(
46
  CorrDf_all %>% 
47
    filter(Mediation_type == 'MetaG-MetaB-EOS') %>%
48
    filter(Node2 %in% metaB.common),
49
  CorrDf_all %>% 
50
    filter(Mediation_type == 'MetaB-HostT-EOS') %>%
51
    filter(Node1 %in% metaB.common) %>%
52
    filter(Node2 %in% trans.common),
53
  CorrDf_all %>% 
54
    filter(Mediation_type == 'HostT-Spuprot-EOS') %>%
55
    filter(Node1 %in% trans.common))
56
57
58
# nodes and types
59
nodeXs <- c("MetaB","Spuprot")  #do not change the sequences of elements in nodeXs 
60
nodeYs <- c("MetaG","HostT")  #do not change the sequences of elements in nodeYs 
61
ENtypes <- c("EOS","NEU")
62
63
64
# create heatmap from combinations of figures :
65
66
67
for(enType in ENtypes){
68
  #  enType = ENtypes[2]
69
  
70
  CorrDf_tmp <- eval(parse(text = paste0("CorrDf_",enType)))
71
  
72
  for(nodex in nodeXs){
73
    # nodex = nodeXs[1]
74
    
75
    for(nodey in nodeYs){
76
      # nodey = nodeYs[2]
77
      
78
      CorrDf <- CorrDf_tmp %>% filter(grepl(nodex, Mediation_type)) %>% filter(grepl(nodey, Mediation_type))
79
      if(nrow(CorrDf) == 0) next
80
      
81
      mediatp <- CorrDf$Mediation_type %>% unique()
82
      
83
      NodeXCol <- which(strsplit(mediatp, "-", fixed = T)[[1]] == nodex)
84
      colnames(CorrDf)[NodeXCol] <- "NodeX"
85
      
86
      NodeYCol <- which(strsplit(mediatp, "-", fixed = T)[[1]] == nodey)
87
      colnames(CorrDf)[NodeYCol] <- "NodeY"
88
      
89
      
90
      # organize the orders of nodex and nodey
91
      dat_r.w <- CorrDf %>% reshape2::dcast(NodeY ~ NodeX, value.var = "Correlation")
92
      rownames(dat_r.w) <- dat_r.w$NodeY; dat_r.w <- dat_r.w[-1]
93
      
94
      if(T){
95
        df <- t(dat_r.w) 
96
        x <- as.matrix(scale(df))
97
        dd.col <- as.dendrogram(hclust(dist(x)))
98
        col.ord <- order.dendrogram(dd.col)
99
        
100
        dd.row <- as.dendrogram(hclust(dist(t(x))))
101
        row.ord <- order.dendrogram(dd.row)
102
        
103
        xx <- scale(df)[col.ord, row.ord] 
104
        xx_names <- attr(xx, "dimnames") 
105
        #df <- as.data.frame(xx)
106
        ddata_x <- dendro_data(dd.row) 
107
        ddata_y <- dendro_data(dd.col) 
108
      } 
109
      if(!exists(paste("order_",nodey,sep = ""))) assign(paste("order_",nodey,sep = ""), xx_names[[2]],envir = .GlobalEnv)
110
      if(!exists(paste("order_",nodex,sep = ""))) assign(paste("order_",nodex,sep = ""), xx_names[[1]],envir = .GlobalEnv)
111
      
112
      
113
      order_nodex <- eval(parse(text = paste("order_",nodex,sep = "")))
114
      order_nodey <- eval(parse(text = paste("order_",nodey,sep = "")))
115
      
116
      if(!all(unique(CorrDf$NodeX) %in% order_nodex) ) {print(paste("not all nodes of ", nodex," were in predefined order so stop",sep = ""));break}
117
      if(!all(unique(CorrDf$NodeY) %in% order_nodey) ) {print(paste("not all nodes of ", nodey," were in predefined order so stop",sep = ""));break}
118
      
119
      
120
      CorrDf$NodeX <- factor(CorrDf$NodeX, levels = order_nodex)
121
      CorrDf$NodeY <- factor(CorrDf$NodeY, levels = order_nodey)
122
      
123
      CorrDf$ColorType <- sapply(c(1:nrow(CorrDf)), 
124
                                 function(i) {
125
                                   if(CorrDf$Linked[i] == "Y") return("Y") else if(CorrDf$`Mediation P-val`[i] >= 0.05) return("N_ns") else if(CorrDf$Correlation[i]>0) return("N_sig_posCorr") else return("N_sig_negCorr")
126
                                 })
127
      
128
      CorrDf$absCorr <- abs(CorrDf$Correlation)
129
      
130
      
131
      CorrDf <- CorrDf %>% filter( NodeX %in% order_nodex) %>% filter(NodeY %in% order_nodey)
132
      CorrDf$NodeX <- factor(CorrDf$NodeX, levels = order_nodex)
133
      CorrDf$NodeY <- factor(CorrDf$NodeY, levels = order_nodey)
134
      
135
      
136
      p <- ggplot(data = CorrDf, aes(x=NodeX,y=NodeY))+
137
        geom_tile(aes(fill=ColorType, alpha=absCorr),color="white") +
138
        theme(axis.text.x = element_text(angle = 90))+
139
        #scale_fill_manual(values=c("white","#e2e2e2","#cc0202"))+
140
        scale_fill_manual(values = c("white","#c1c1ff","#ffb6b6","#cc0202")) +
141
        #scale_alpha(limits = c(0.0,1.0), range = c(0,0.6))+
142
        theme(  panel.grid.major = element_blank(),
143
                panel.grid.minor = element_blank(),
144
                panel.background = element_blank())
145
               # axis.title.x = element_text(colour=NA),
146
               # axis.title.y = element_blank())
147
      
148
      assign(paste("HeatP_",nodex,".",nodey,"_",enType,sep = ""), p, envir = .GlobalEnv)
149
      
150
    }
151
    
152
    
153
  }
154
  
155
  
156
  assign(paste("order_MetaB_",enType,sep = ""), order_MetaB, envir = .GlobalEnv)
157
  assign(paste("order_MetaG_",enType,sep = ""), order_MetaG, envir = .GlobalEnv)
158
  assign(paste("order_HostT_",enType,sep = ""), order_HostT, envir = .GlobalEnv)
159
  assign(paste("order_Spuprot_",enType,sep = ""), order_Spuprot, envir = .GlobalEnv)
160
  
161
  remove(order_MetaB,order_MetaG,order_HostT, order_Spuprot)
162
}
163
164
165
166
167
# Fig 3a. NEU integrated plot 
168
ggarrange(ggarrange(HeatP_MetaB.HostT_NEU + 
169
                      theme(legend.position = "none", axis.text.x = element_blank(),axis.text.y = element_blank()) +
170
                      xlab("MetaB") + ylab("Trans"), 
171
                    HeatP_Spuprot.HostT_NEU + 
172
                      theme(legend.position = "none", axis.text.x = element_blank(),axis.text.y = element_blank()) +
173
                      xlab("Sputum") + ylab("Trans"),
174
                    nrow = 1, widths = c(0.65,0.35)),
175
          ggarrange(HeatP_MetaB.MetaG_NEU + 
176
                      theme(legend.position = "none", axis.text.x = element_blank(),axis.text.y = element_blank()) +
177
                      xlab("MetaB") + ylab("MetaG"), 
178
                    ggplot() + geom_text(aes(x=0,y=0),label="NEU") + theme_dendro(), 
179
                    nrow = 1, widths = c(0.65,0.35)),
180
          nrow = 2)
181
182
183
# Fig 3a. EOS integrated plot
184
ggarrange(ggarrange(HeatP_Spuprot.HostT_EOS + 
185
                      theme(legend.position = "none", axis.text.x = element_blank(),axis.text.y = element_blank()) +
186
                      xlab("Sputum") + ylab("Trans"),
187
                    HeatP_MetaB.HostT_EOS + 
188
                      theme(legend.position = "none", axis.text.x = element_blank(),axis.text.y = element_blank()) +
189
                      xlab("MetaB") + ylab("Trans"), 
190
                    nrow = 1, widths = c(0.45,0.35)),
191
          ggarrange(ggplot() + geom_text(aes(x=0,y=0),label="EOS") + theme_dendro(),
192
                    HeatP_MetaB.MetaG_EOS + 
193
                      theme(legend.position = "none", axis.text.x = element_blank(),axis.text.y = element_blank()) +
194
                      xlab("MetaB") + ylab("MetaG"), 
195
                    nrow = 1, widths = c(0.45,0.35)),
196
          nrow = 2, heights = c(0.6,0.4))