Browse Source

Boleanas en la app.

main
marcelcosta 2 years ago
parent
commit
b5323436ec
1 changed files with 174 additions and 13 deletions
  1. +174
    -13
      BDAccess/app.R

+ 174
- 13
BDAccess/app.R

@ -1016,25 +1016,173 @@ server <- function(input, output) {
sampleNames(gs)<-sapply(sampleNames(gs), function(x) strsplit(x, "ICs ")[[1]][2]) %>% sampleNames(gs)<-sapply(sampleNames(gs), function(x) strsplit(x, "ICs ")[[1]][2]) %>%
gsub("[[:space:]][0-9]*.fcs_.[0-9]*","", . , perl = T) gsub("[[:space:]][0-9]*.fcs_.[0-9]*","", . , perl = T)
bool.comb<-apply(
expand.grid(c("","!"), c("","!"),c("","!"), c("","!"), c("","!")),
1,
function(x) paste0(x[1],"CTLA4 & ",x[2],"LAG3 & ",x[3],"PD1 & ",x[4], "TIGIT & ",x[5], "TIM3")
)
bool.name<-apply(
expand.grid(c("+","-"), c("+","-"),c("+","-"), c("+","-"), c("+","-")),
1,
function(x) paste0("CTLA4",x[1]," LAG3",x[2]," PD1",x[3]," TIGIT",x[4]," TIM3",x[5])
)
print("Booleanos CD8")
for (i in 1:length(bool.comb)){
call<-substitute(booleanFilter(v), list(v=as.symbol(bool.comb[i])))
boolgate<-eval(call)
gs_pop_add(gs, boolgate, parent="CD8", name = bool.name[i])
}
print("Booleanos CD4")
for (i in 1:length(bool.comb)){
call<-substitute(booleanFilter(v), list(v=as.symbol(bool.comb[i])))
boolgate<-eval(call)
gs_pop_add(gs, boolgate, parent="CD4", name = bool.name[i])
}
recompute(gs)
names<-sampleNames(gs) %>% gsub("ab|Ab|AB|iso|Iso|ISO| ","",.) %>% unique() names<-sampleNames(gs) %>% gsub("ab|Ab|AB|iso|Iso|ISO| ","",.) %>% unique()
nodes<-gs_get_pop_paths(gs) nodes<-gs_get_pop_paths(gs)
nodes_parent<-nodes[!grepl("CTLA4|LAG3|PD1|TIGIT|TIM3|root$", nodes)]
nodes_cd4<-nodes[grepl("CTLA4$|LAG3$|PD1$|TIGIT$|TIM3$", nodes) & grepl("/CD4/",nodes)]
nodes_cd8<-nodes[grepl("CTLA4$|LAG3$|PD1$|TIGIT$|TIM3$", nodes) & grepl("/CD8/",nodes)]
# nodes<-gsub("â\u0081»", "-", nodes)
# nodes<-gsub("â\u0081º", "+", nodes)
nodes<-nodes[grepl("CTLA4", nodes)]
nodes<-nodes[!grepl("CD4$|CD8$|CTLA4$|TIM3$|PD1$|LAG3$|TIGIT$", nodes)]
pop<-gs_pop_get_stats(gs, nodes=nodes,type="percent") %>% as.data.frame %>% mutate(percent=percent*100)
pop$percent<-round(pop$percent, digits=2)
# pop$pop<-gsub("â\u0081»", "n", pop$pop)
# pop$pop<-gsub("â\u0081º", "p", pop$pop)
pop$pop<-gsub("-", "n", pop$pop, fixed=T)
pop$pop<-gsub("+", "p", pop$pop, fixed=T)
pop$pop<-gsub(" ", "_", pop$pop)
pop["Type"]<-"ab"
pop[grepl("iso|ISO|Iso",pop$sample),"Type"]<-"iso"
pop$sample<-gsub("iso|ISO|Iso|ab|AB|Ab| ","",pop$sample)
pop_sp<-pop %>% spread(Type, percent)
pop_sp["Net"]<-pop_sp$ab
pop_sp[!grepl("CTLA4n_LAG3n_PD1n_TIGITn_TIM3n",pop_sp$pop),"Net"]<-pop_sp[!grepl("CTLA4n_LAG3n_PD1n_TIGITn_TIM3n",pop_sp$pop),"ab"]-pop_sp[!grepl("CTLA4n_LAG3n_PD1n_TIGITn_TIM3n",pop_sp$pop),"iso"]
pop_sp$Net[pop_sp$Net < 0]<-0
pop_sp["Population"]<-str_extract(pop_sp$pop, "/CD[4,8]{1}/") %>% gsub("/","",.)
pop_sp$pop<-sapply(strsplit(pop_sp$pop, "/"), tail, 1)
pop_sp<-pop_sp %>% select(-ab,-iso) %>% spread(pop,Net)
pop_sp$CTLA4n_LAG3n_PD1n_TIGITn_TIM3n<- pop_sp %>% select(-CTLA4n_LAG3n_PD1n_TIGITn_TIM3n) %>% group_by(sample,Population) %>%
gather(pop, value, -sample,-Population) %>% summarise(n=100-sum(value)) %>% pull(n)
if (input$dbtype == "OV"){
pop_sp <- rename(pop_sp, "samples"="sample")
}
if (input$dbtype %in% c("UM", "CC")){
pop_sp <- rename(pop_sp, "CODIGO"="sample")
}
pop_sql<-sqlFetch(dta, "IC") %>% slice(0)
pop_sp<-pop_sp %>% merge(pop_sql, all=T) %>% select(colnames(pop_sql))
for (id in names){ for (id in names){
print(id) print(id)
iso<-sampleNames(gs)[grepl(id, sampleNames(gs)) & grepl("iso|Iso|ISO",sampleNames(gs))] iso<-sampleNames(gs)[grepl(id, sampleNames(gs)) & grepl("iso|Iso|ISO",sampleNames(gs))]
ab<-sampleNames(gs)[grepl(id, sampleNames(gs)) & grepl("ab|Ab|AB",sampleNames(gs))] ab<-sampleNames(gs)[grepl(id, sampleNames(gs)) & grepl("ab|Ab|AB",sampleNames(gs))]
g1<-ggcyto_arrange(autoplot(gs[[ab]], nodes_parent, bins=128), nrow=1)
g2<-ggcyto_arrange(autoplot(gs[[iso]], nodes_cd8, bins=64), nrow=1)
g3<-ggcyto_arrange(autoplot(gs[[ab]], nodes_cd8, bins=64), nrow=1)
g4<-ggcyto_arrange(autoplot(gs[[iso]], nodes_cd4, bins=64), nrow=1)
g5<-ggcyto_arrange(autoplot(gs[[ab]], nodes_cd4, bins=64), nrow=1)
g_all<-gridExtra::gtable_rbind(g1,g2,g3,g4,g5)
ggsave(paste0(route,id,".IC.png"), g_all, width = 10, height = 10)
if (input$dbtype == "OV"){
data<-pop_sp %>% filter(samples == id)
data1<-data %>% gather(phen, value, -samples, -Population)
}
if (input$dbtype %in% c("UM", "CC")){
data<-pop_sp %>% filter(CODIGO == id)
data1<-data %>% gather(phen, value, -CODIGO, -Population)
}
data1$phen<-gsub("p","+",data1$phen)
data1$phen<-gsub("n","-",data1$phen)
data1$phen<-gsub("_"," ",data1$phen)
data1$phen<-gsub("n","-",data1$phen, fixed = T)
data1$phen<-gsub("p","+",data1$phen, fixed = T)
data1$phen<-gsub("_"," ",data1$phen)
data1[data1$value < 0.5, "phen"]<-"Other"
data1$phen<-gsub("[A-Z]*-*[0-9T]- *", "", data1$phen)
data1$phen<-gsub("+ $", "", data1$phen)
data1$phen[data1$phen == ""]<-"All Negative"
# data1<-data1 %>% filter(value > 0.5)
data1["phen1"]<-"PD1"
data1[!grepl("PD1+", data1$phen),"phen1"]<-NA
data1["phen2"]<-"TIM3"
data1[!grepl("TIM3+", data1$phen),"phen2"]<-NA
data1["phen3"]<-"CTLA4"
data1[!grepl("CTLA4+", data1$phen),"phen3"]<-NA
data1["phen4"]<-"TIGIT"
data1[!grepl("TIGIT+", data1$phen),"phen4"]<-NA
data1["phen5"]<-"LAG3"
data1[!grepl("LAG3+", data1$phen),"phen5"]<-NA
data1<-data1 %>% arrange(desc(value))
data2<-data1 %>% filter(!phen %in% c("All Negative","Other"))
data1<-rbind(data2, data1 %>% filter(phen %in% c("All Negative","Other")) %>% arrange(desc(phen)))
data_cd8<-data1 %>% filter(Population == "CD8")
data_cd4<-data1 %>% filter(Population == "CD4")
data_cd8$ymax<-cumsum(data_cd8$value)
data_cd8$ymin<-c(0, head(data_cd8$ymax, n=-1))
data_cd4$ymax<-cumsum(data_cd4$value)
data_cd4$ymin<-c(0, head(data_cd4$ymax, n=-1))
data1<-rbind(data_cd8, data_cd4)
color<-c(c("CTLA4+ LAG3+ PD1+ TIGIT+ TIM3+"="black","All Negative"="grey90","Other"="grey50", "PD1+"="#C07AFF", "CTLA4+"="#3EB3DE","TIM3+"="#5EF551","LAG3+"="#DEBB3E","TIGIT+"="#FA7055"),
c("CTLA4+ PD1+"="#6666FF","PD1+ TIM3+"="#849CA8", "LAG3+ PD1+"="#C47F9F","PD1+ TIGIT+"="#D259AA", "CTLA4+ TIM3+"="#4ED498", "CTLA4+ LAG3+"="#8EB78E", "CTLA4+ TIGIT+"="#9C929A", "LAG3+ TIM3+"="#9ED848", "TIGIT+ TIM3+"="#ACB353", "LAG3+ TIGIT+"="#EC964A"),
c("CTLA4+ PD1+ TIGIT+"="#B86B6A","CTLA4+ PD1+ TIGIT+ TIM3+"="#B81515","LAG3+ PD1+ TIGIT+"="#007D8A", "PD1+ TIGIT+ TIM3+"="#D64545", "LAG3+ PD1+ TIGIT+ TIM3+"="#0f5860", "LAG3+ TIGIT+ TIM3+"="#50cad3"))
basic.color<-color[c("PD1+","TIGIT+","TIM3+","CTLA4+","LAG3+")]
names(basic.color)<-c("PD1","TIGIT","TIM3","CTLA4","LAG3")
# Make the plot
g_coex<-ggplot(data1)+
facet_grid(factor(Population, levels=c("CD8","CD4"))~.)+
geom_rect(aes(ymax=ymax, ymin=ymin, xmax=4.5, xmin=0), fill=color[data1$phen])+
geom_rect(aes(ymax=ymax, ymin=ymin, xmax=5.4, xmin=5, fill=factor(phen1, levels=c("PD1","TIGIT","TIM3","CTLA4","LAG3"))))+
geom_rect(aes(ymax=ymax, ymin=ymin, xmax=5.9, xmin=5.5, fill=factor(phen4, levels=c("PD1","TIGIT","TIM3","CTLA4","LAG3"))))+
geom_rect(aes(ymax=ymax, ymin=ymin, xmax=6.4, xmin=6, fill=factor(phen2, levels=c("PD1","TIGIT","TIM3","CTLA4","LAG3"))))+
geom_rect(aes(ymax=ymax, ymin=ymin, xmax=6.9, xmin=6.5, fill=factor(phen3, levels=c("PD1","TIGIT","TIM3","CTLA4","LAG3"))))+
geom_rect(aes(ymax=ymax, ymin=ymin, xmax=7.4, xmin=7, fill=factor(phen5, levels=c("PD1","TIGIT","TIM3","CTLA4","LAG3"))))+
scale_fill_manual(values = basic.color, na.value="#FFFFFF00", drop=F, limits=c("PD1","TIGIT","TIM3","CTLA4","LAG3"), name="IC")+
coord_polar(theta="y") + # Try to remove that to understand how the chart is built initially
xlim(c(0, 8)) +# Try to remove that to see how to make a pie chart
theme_classic()+
theme(strip.background = element_blank(),
strip.text = element_text(size=12, face="bold"),
axis.line = element_blank(),
axis.ticks = element_blank(),
# plot.margin = margin(-200,0,0,0),
axis.text = element_blank())
# g1<-ggcyto_arrange(autoplot(gs[[ab]], nodes_parent, bins=128), nrow=1)
# g2<-ggcyto_arrange(autoplot(gs[[iso]], nodes_cd8, bins=64), nrow=1)
# g3<-ggcyto_arrange(autoplot(gs[[ab]], nodes_cd8, bins=64), nrow=1)
# g4<-ggcyto_arrange(autoplot(gs[[iso]], nodes_cd4, bins=64), nrow=1)
# g5<-ggcyto_arrange(autoplot(gs[[ab]], nodes_cd4, bins=64), nrow=1)
g1<-ggcyto_arrange(autoplot(gs[[ab]], nodes_parent), nrow=1)
g2<-ggcyto_arrange(autoplot(gs[[iso]], nodes_cd8), nrow=1)
g3<-ggcyto_arrange(autoplot(gs[[ab]], nodes_cd8), nrow=1)
g4<-ggcyto_arrange(autoplot(gs[[iso]], nodes_cd4), nrow=1)
g5<-ggcyto_arrange(autoplot(gs[[ab]], nodes_cd4), nrow=1)
g_dots<-gridExtra::gtable_rbind(g1,g2,g3,g4,g5)
g_all<-ggpubr::ggarrange(g_dots, g_coex, nrow=1, widths=c(0.65,0.35))
ggsave(paste0(route,id,".IC.png"), g_all, width = 14, height = 10)
} }
} }
}) })
@ -1136,13 +1284,26 @@ server <- function(input, output) {
pop_sp<-pop_sp %>% select(-ab,-iso) %>% spread(pop,Net) pop_sp<-pop_sp %>% select(-ab,-iso) %>% spread(pop,Net)
pop_sp$CTLA4n_LAG3n_PD1n_TIGITn_TIM3n<- pop_sp %>% select(-CTLA4n_LAG3n_PD1n_TIGITn_TIM3n) %>% group_by(sample,Population) %>% pop_sp$CTLA4n_LAG3n_PD1n_TIGITn_TIM3n<- pop_sp %>% select(-CTLA4n_LAG3n_PD1n_TIGITn_TIM3n) %>% group_by(sample,Population) %>%
gather(pop, value, -sample,-Population) %>% summarise(n=100-sum(value)) %>% pull(n) gather(pop, value, -sample,-Population) %>% summarise(n=100-sum(value)) %>% pull(n)
pop_sp <- rename(pop_sp, "samples"="sample")
if (input$dbtype == "OV"){
pop_sp <- rename(pop_sp, "samples"="sample")
}
if (input$dbtype %in% c("UM", "CC")){
pop_sp <- rename(pop_sp, "CODIGO"="sample")
}
pop_sql<-sqlFetch(dta, "IC") %>% slice(0) pop_sql<-sqlFetch(dta, "IC") %>% slice(0)
pop_sp<-pop_sp %>% merge(pop_sql, all=T) %>% select(colnames(pop_sql)) pop_sp<-pop_sp %>% merge(pop_sql, all=T) %>% select(colnames(pop_sql))
vartypes<-rep("Number", pop_sp %>% select(-samples, -Population) %>% colnames %>% length)
names(vartypes)<-pop_sp %>% select(-samples, -Population) %>% colnames
if (input$dbtype == "OV"){
vartypes<-rep("Number", pop_sp %>% select(-samples, -Population) %>% colnames %>% length)
names(vartypes)<-pop_sp %>% select(-samples, -Population) %>% colnames
}
if (input$dbtype %in% c("UM", "CC")){
vartypes<-rep("Number", pop_sp %>% select(-CODIGO, -Population) %>% colnames %>% length)
names(vartypes)<-pop_sp %>% select(-CODIGO, -Population) %>% colnames
}
sqlSave(dta, pop_sp, tablename="IC", append = T, varTypes = vartypes, rownames = F) sqlSave(dta, pop_sp, tablename="IC", append = T, varTypes = vartypes, rownames = F)
print("Tabla IC sincronizada.") print("Tabla IC sincronizada.")

Loading…
Cancel
Save