You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
10 KiB

7 months ago
  1. gs_export_pop<-function(gs, pop, trim.channels=T){
  2. pop.dt<-c()
  3. # Initializes the progress bar
  4. pb <- txtProgressBar(min = 0, # Minimum value of the progress bar
  5. max = length(gs), # Maximum value of the progress bar
  6. style = 3, # Progress bar style (also available style = 1 and style = 2)
  7. width = 50, # Progress bar width. Defaults to getOption("width")
  8. char = "=") # Character used to create the bar
  9. for (i in 1:length(gs)){
  10. samp<-sampleNames(gs)[i]
  11. data<-as.data.frame(flowCore::exprs(gh_pop_get_data(gs[[samp]], pop))) %>% add_column("Sample"=samp)
  12. if (length(pop.dt) > 0){
  13. pop.dt<-rbind(pop.dt, data)
  14. }else{
  15. pop.dt<-data
  16. }
  17. setTxtProgressBar(pb, i)
  18. }
  19. close(pb)
  20. if (trim.channels){
  21. colnames(pop.dt)<-gsub(" [A-Za-z0-9.-]*","",colnames(pop.dt))
  22. pop.dt<-pop.dt[colnames(pop.dt)[!colnames(pop.dt) %in% c("FSC-H","SSC-H","FSC-Width","FVS")]]
  23. }
  24. return(pop.dt)
  25. }
  26. get_cut<-function(x, markers, ref, thr){
  27. x<-x %>%
  28. dplyr::filter(Condition == ref) %>%
  29. gather(Marker, Value, all_of(markers)) %>%
  30. group_by(`ID.SAR-EM`, Marker, Tiempo) %>%
  31. summarise(cut=predict_cut(Value, thr))
  32. }
  33. get_positive<-function(x, cut=NULL, markers, ref, thr){
  34. if (is.null(cut)){
  35. cut<-get_cut(cd8, markers = markers, ref = ref, thr = thr)
  36. }
  37. x<-merge(
  38. x %>% gather(Marker, Value, all_of(unique(cut$Marker))),
  39. cut
  40. )
  41. x<-x %>%
  42. mutate(Marker_cut=Value >= cut)
  43. x<-x %>%
  44. select(`ID.SAR-EM`, Tiempo, Condition, CellID, Marker, Marker_cut) %>%
  45. mutate(Marker_pos=paste0(Marker,".",Marker_cut)) %>%
  46. group_by(`ID.SAR-EM`,Tiempo, Condition, CellID) %>%
  47. summarise(Pheno=paste0(Marker_pos, collapse = " ")) %>%
  48. # mutate(Pheno=paste0("IFNg.",IFNg_p," ","TNFa.",TNFa_p," ","CD69.",CD69_p," ","CD107a.",CD107a_p)) %>%
  49. mutate(Pheno=gsub("[A-Za-z0-9]*.FALSE |[A-Za-z0-9]*.FALSE$| [A-Za-z0-9]*.FALSE","",Pheno),
  50. Pheno=gsub(".TRUE","+",Pheno),
  51. Pheno=gsub("^$","Neg",Pheno))
  52. x.dt<-x %>%
  53. group_by(`ID.SAR-EM`, Tiempo, Condition, Pheno) %>%
  54. count() %>%
  55. group_by(`ID.SAR-EM`, Tiempo, Condition) %>%
  56. mutate(perc=n*100/sum(n))
  57. return(x.dt)
  58. }
  59. transform_gs<-function(gs, saveparams=NULL, trans_params_obj=NULL, index=1){
  60. if (is.null(trans_params_obj)){
  61. log_mark<-colnames(gs)[!grepl("FS|SS|Time", colnames(gs))]
  62. trans_params<-lapply(log_mark, function(x) list(channel=x, scale="biexp",maxvalue=250000,pos=4, widthBasis=-400, max=5, min=0))
  63. names(trans_params)<-log_mark
  64. log_mark_lin<-colnames(gs)[grepl("FS|SS|Time", colnames(gs))]
  65. trans_params_lin<-lapply(log_mark_lin, function(x) list(channel=x, scale="lin",
  66. max=max(flowCore::exprs(gh_pop_get_data(gs, "root"))[,x])*1.1, min=0))
  67. names(trans_params_lin)<-log_mark_lin
  68. trans_params<-c(trans_params_lin, trans_params)
  69. }else{
  70. log_mark<-colnames(gs)[!grepl("FSC|SSC|Time", colnames(gs))]
  71. log_mark_lin<-colnames(gs)[grepl("FSC|SSC|Time", colnames(gs))]
  72. }
  73. shiny::runApp(shiny::shinyApp(
  74. ui = fluidPage(
  75. sidebarLayout(
  76. sidebarPanel(selectInput("channel", "Channel", colnames(gs), multiple = F),
  77. uiOutput("maxvalue"),
  78. uiOutput("pos"),
  79. uiOutput("widthBasis"),
  80. uiOutput("max"),
  81. uiOutput("min"),
  82. actionButton("applyBut", "Apply"),
  83. actionButton("stopBut", "Quit")),
  84. mainPanel(plotOutput("hist"))
  85. )
  86. ),
  87. server = function(input, output) {
  88. output$hist <- renderPlot({
  89. gs.trans<-gs_clone(gs[index])
  90. max<-input$max
  91. if (trans_params[[input$channel]]$scale == "biexp"){
  92. trans.obj<-flowjo_biexp_trans(maxValue = input$maxvalue, pos=input$pos,
  93. widthBasis=input$widthBasis)
  94. transList <- transformerList(input$channel, trans.obj)
  95. gs.trans <- transform(gs.trans, transList)
  96. max<-10^input$max
  97. }
  98. ggcyto(gs.trans, aes(x = .data[[input$channel]]), subset = "root")+
  99. geom_density(fill = "blue", alpha= 0.5)+
  100. ggcyto_par_set(limits = list(x = c(input$min,max)))
  101. })
  102. output$maxvalue<-renderUI({if(trans_params[[input$channel]]$scale == "biexp"){
  103. sliderInput("maxvalue", "maxValue", min=0, max=1000000, value=trans_params[[input$channel]]$maxvalue)}})
  104. output$pos<-renderUI({if(trans_params[[input$channel]]$scale == "biexp"){
  105. sliderInput("pos", "pos", min=0, max=10, value=trans_params[[input$channel]]$pos, step = 0.5)}})
  106. output$widthBasis<-renderUI({if(trans_params[[input$channel]]$scale == "biexp"){
  107. sliderInput("widthBasis", "widthBasis", min=-1000, max=0, value=trans_params[[input$channel]]$widthBasis)}})
  108. output$max<-renderUI({
  109. if(trans_params[[input$channel]]$scale == "biexp"){
  110. sliderInput("max", "Max X", min=0, max=6, value=log10(trans_params[[input$channel]]$max), step=0.1)
  111. }else{
  112. sliderInput("max", "Max X", min=0, max=trans_params[[input$channel]]$max,
  113. value=trans_params[[input$channel]]$max)
  114. }
  115. })
  116. output$min<-renderUI({
  117. if(trans_params[[input$channel]]$scale == "biexp"){
  118. sliderInput("min", "Min X", min=-1000, max=100000, value=trans_params[[input$channel]]$min)
  119. }else{
  120. sliderInput("min", "Min X", min=0, max=trans_params[[input$channel]]$max, value=trans_params[[input$channel]]$min)
  121. }
  122. })
  123. observeEvent(input$applyBut, {
  124. print("Transformation and Scaling Updated!")
  125. if (isolate(input$channel) %in% log_mark){
  126. trans_params[[isolate(input$channel)]]$maxvalue<<-isolate(input$maxvalue)
  127. trans_params[[isolate(input$channel)]]$pos<<-isolate(input$pos)
  128. trans_params[[isolate(input$channel)]]$widthBasis<<-isolate(input$widthBasis)
  129. trans_params[[isolate(input$channel)]]$max<<-10^isolate(input$max)
  130. trans_params[[isolate(input$channel)]]$min<<-isolate(input$min)
  131. }
  132. if (isolate(input$channel) %in% log_mark_lin){
  133. trans_params[[isolate(input$channel)]]$max<<-isolate(input$max)
  134. trans_params[[isolate(input$channel)]]$min<<-isolate(input$min)
  135. }
  136. })
  137. observeEvent(input$stopBut, {
  138. stopApp()
  139. })
  140. }
  141. ))
  142. trans_apply(gs, trans_params)
  143. print("Transformation Applied")
  144. if (!is.null(saveparams)){
  145. saveRDS(trans_params, file=saveparams)
  146. }
  147. return(trans_params)
  148. }
  149. trans_apply<-function(gs, trans_params){
  150. for (i in 1:length(trans_params)){
  151. if (trans_params[[i]]$scale == "biexp"){
  152. print(trans_params[[i]]$channel)
  153. trans.obj<-flowjo_biexp_trans(maxValue = trans_params[[i]]$maxvalue,
  154. pos=trans_params[[i]]$pos,
  155. widthBasis=trans_params[[i]]$widthBasis)
  156. transList <- transformerList(trans_params[[i]]$channel, trans.obj)
  157. gs <- transform(gs, transList)
  158. }
  159. }
  160. }
  161. ggcyto_trans<-function(gs, x, y, trans_params_obj=trans_params, subset, gates=T, stats=T, ...){
  162. g<-ggcyto(gs, aes(.data[[x]], .data[[y]]), subset=subset, ...)+
  163. ggcyto_par_set(limits = list(x = c(trans_params_obj[[x]]$min,trans_params_obj[[x]]$max),
  164. y=c(trans_params_obj[[y]]$min,trans_params_obj[[y]]$max)))
  165. if(gates==T){
  166. if (subset == "root"){
  167. gates<-gs_get_pop_paths(gs)[grepl(paste0("^/","[^/]*$"), gs_get_pop_paths(gs))]
  168. }else{
  169. gates<-gs_get_pop_paths(gs)[grepl(paste0(subset,"/","[^/]*$"), gs_get_pop_paths(gs))]
  170. }
  171. g<-g+geom_point(size=0.1, alpha=0.1)+
  172. geom_gate(gates)
  173. if (stats== T){
  174. g<-g+geom_stats(gates)
  175. }
  176. }
  177. return(g)
  178. }
  179. ggcyto_trans_all<-function(gs, index=1, trans_params_obj=trans_params, stats=T,...){
  180. subsets<-unlist(sapply(gs_get_pop_paths(gs), function(x) strsplit(x, "/")[[1]][length(strsplit(x, "/")[[1]])-1]))
  181. subsets<-c("root",unique(subsets[subsets != ""]))
  182. g.list<-list()
  183. for (subset in subsets){
  184. if (subset == "root"){
  185. gates<-gs_get_pop_paths(gs)[grepl(paste0("^/","[^/]*$"), gs_get_pop_paths(gs))]
  186. }else{
  187. gates<-gs_get_pop_paths(gs)[grepl(paste0(subset,"/","[^/]*$"), gs_get_pop_paths(gs))]
  188. }
  189. subset.x<-names(gs_pop_get_gate(gs, gates[1])[[1]]@parameters)[1]
  190. subset.y<-names(gs_pop_get_gate(gs, gates[1])[[1]]@parameters)[2]
  191. g.list[[subset]]<-ggcyto::as.ggplot(ggcyto_trans(gs[[index]], subset.x, subset.y,
  192. trans_params_obj, subset, stats=stats))
  193. }
  194. g<-do.call(ggpubr::ggarrange, c(g.list, ...))
  195. g<-ggpubr::annotate_figure(g, top = ggpubr::text_grob(sampleNames(gs)[index],
  196. color = "black", face = "bold", size = 14))
  197. return(g)
  198. }
  199. LMD2FCS<-function(files, output.dir=NULL){
  200. if(is.null(output.dir)){
  201. route<-paste0(gsub("/[^/]*$","",files[1]),"/")
  202. }
  203. for (lmd in files){
  204. lmd<-gsub(".*/","",lmd)
  205. fcs<-read.FCS(paste0(route,lmd), dataset = 2)
  206. keyword(fcs)['$FIL']<-paste0(gsub(".LMD","",lmd), ".fcs")
  207. write.FCS(fcs, paste0(route, gsub(".LMD","",lmd), ".fcs"))
  208. }
  209. print("Conversión completada")
  210. }
  211. gates_save<-function(gs, file="gates.rds", save=T){
  212. gates.list<-list()
  213. pop_paths<-gs_get_pop_paths(gs)[gs_get_pop_paths(gs) != "root"]
  214. pop_paths<-lapply(pop_paths, function(x) rev(rev(strsplit(x, "/")[[1]])[1:2]))
  215. for (gate in pop_paths){
  216. parent<-if (gate[1] == ""){"root"}else{gate[1]}
  217. gated_pop<-gate[2]
  218. gates.list[[paste0(parent,"/",gated_pop)]]<-gs_pop_get_gate(gs, gated_pop)
  219. }
  220. if(save){saveRDS(gates.list, file)}
  221. return(gates.list)
  222. }
  223. gates_apply<-function(gs, gates, exact=T){
  224. if (exact){
  225. for (gate in names(gates)){
  226. parent<-strsplit(gate, "/")[[1]][1]
  227. gated_pop<-strsplit(gate, "/")[[1]][2]
  228. gs_pop_add(gs, gates[[gate]], parent=parent, name=gated_pop)
  229. print(paste0("Gate ",gated_pop," applied!"))
  230. }
  231. }else{
  232. for (gate in names(gates)){
  233. parent<-strsplit(gate, "/")[[1]][1]
  234. gated_pop<-strsplit(gate, "/")[[1]][2]
  235. gs_pop_add(gs, gates[[gate]][[1]], parent=parent, name=gated_pop)
  236. print(paste0("Gate ",gated_pop," applied!"))
  237. }
  238. }
  239. recompute(gs)
  240. return(gs)
  241. }