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.

193 lines
5.2 KiB

  1. ---
  2. title: "CytoR"
  3. author: "Marcel Costa-Garcia"
  4. date: "`r Sys.Date()`"
  5. output: html_document
  6. ---
  7. ```{r setup, include=FALSE}
  8. knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE)
  9. ```
  10. First we load the required libreries and import functions from `functionsCyto.R`:
  11. ```{r}
  12. library(tidyverse)
  13. library(flowWorkspace)
  14. library(Biobase)
  15. library(flowGate)
  16. source("functionsCyto.R")
  17. ```
  18. We import the .LMD or .fcs files to a FlowSet object that we convert into a GatingSet (in the case of LMD files, they include both the format FCS2 and FCS3, which is accessed as dataset=2):
  19. ```{r}
  20. fs<-read.ncdfFlowSet(files=list.files("BcellPhenotype-Files/",".LMD", full.names = T), readonly = F, dataset=2)
  21. gs <- GatingSet(fs)
  22. gs
  23. ```
  24. Next, we will compensate with the compensation matrix of the adquisition, which is embed into the file. We can import another compensation matrix:
  25. ```{r}
  26. comp<-spillover(fs[[1]])$`$SPILLOVER`
  27. colnames(comp)<-colnames(gs)[5:14]
  28. rownames(comp)<-colnames(gs)[5:14]
  29. comp
  30. gs<-compensate(gs, comp)
  31. ```
  32. To transform the axis in an interactive and visual way, I have created the following function:
  33. ```{r, eval=FALSE}
  34. trans_params<-transform_gs(gs)
  35. ```
  36. ![](trans_params.jpg)
  37. ```{r, include=F}
  38. trans_params<-readRDS("BcellPhenotype-trans_params.rds")
  39. trans_apply(gs, trans_params = trans_params)
  40. ```
  41. ```{r}
  42. trans_params$`FL3-A`
  43. ```
  44. We can save the transformation params in a file and latter we can import and apply directly (including in other experiments):
  45. ```{r, eval=F}
  46. saveRDS(trans_params, "BcellPhenotype-trans_params.rds")
  47. ```
  48. ```{r, eval=F}
  49. trans_params<-readRDS("BcellPhenotype-trans_params.rds")
  50. trans_apply(gs, trans_params = trans_params)
  51. ```
  52. As it wasn't done during the acquisition, we will define the marker name for the channels of interest:
  53. ```{r}
  54. markers<-colnames(gs)
  55. markers[c(7,13)]<-c("CD19","L&D")
  56. names(markers)<-colnames(gs)
  57. markernames(gs)<-markers
  58. markernames(gs)
  59. ```
  60. Finally, we will clean a bit the sample names:
  61. ```{r}
  62. sampleNames(gs)
  63. sampleNames(gs)<-gsub("\\s[0-9]*.LMD","",sampleNames(gs))
  64. sampleNames(gs)<-gsub(".*\\s","",sampleNames(gs))
  65. pData(gs)$name<-rownames(pData(gs))
  66. sampleNames(gs)
  67. ```
  68. And we are ready to gate! We will be using the `gs_interactive_gate` function from `flowGate` package.
  69. ```{r, include=F}
  70. gates<-readRDS("BcellPhenotype-gates.rds")
  71. gs<-gates_apply(gs, gates)
  72. ```
  73. ```{r, eval=F}
  74. gs_gate_interactive(gs,
  75. filterId = "Leukocytes",
  76. dims = list("FS-A", "SS-A"))
  77. ```
  78. ![](Gates.jpg)
  79. ```{r, eval=F}
  80. gs_gate_interactive(gs,
  81. subset = "Leukocytes",
  82. filterId = "CD19 L&D",
  83. dims = list("CD19", "L&D"))
  84. ```
  85. We can save the created gates into a file to import latter on (which may be also used to apply the gating strategy into a new experiment):
  86. ```{r, eval=F}
  87. gates<-gates_save(gs, file = "BcellPhenotype-gates.rds")
  88. gates<-readRDS("BcellPhenotype-gates.rds")
  89. gs<-gates_apply(gs, gates)
  90. ```
  91. ```{r}
  92. plot(gs)
  93. ```
  94. We can rapidly explore the results using the `autoplot` function:
  95. ```{r}
  96. autoplot(gs, "Leukocytes", bins=128, nrow=1)
  97. autoplot(gs[["aCDE19"]], bins=128, nrow=1)
  98. ```
  99. We can further personalize the plot with similar sintaxis as `ggplot` with the `ggcyto` package:
  100. ```{r}
  101. g<-ggcyto(gs, subset = "Leukocytes", bins=128, aes(CD19,`L&D`))+
  102. facet_grid(.~factor(name, levels=c("Unst","aCDE19")))+
  103. geom_hex(bins=128)+
  104. geom_gate()+
  105. geom_stats()+
  106. scale_fill_gradient(low="black", high="violet")
  107. g
  108. ```
  109. Finally, we can export the stats and plot them:
  110. ```{r, fig.width=4}
  111. stats<-gs_pop_get_stats(gs, nodes=gs_get_pop_paths(gs, path = "auto")[3:6], type="perc")
  112. stats
  113. g2<-ggplot(stats, aes(factor(sample, levels=c("Unst","aCDE19")), percent, fill=pop))+
  114. geom_bar(stat="identity", color="black")+xlab("Samples")
  115. ggpubr::ggarrange(as.ggplot(g), g2, ncol=1)
  116. ```
  117. Code:
  118. ```r
  119. library(tidyverse)
  120. library(flowWorkspace)
  121. library(Biobase)
  122. library(flowGate)
  123. source("functionsCyto.R")
  124. fs<-read.ncdfFlowSet(files=list.files("BcellPhenotype-Files/",".LMD", full.names = T), readonly = F, dataset=2)
  125. gs <- GatingSet(fs)
  126. comp<-spillover(fs[[1]])$`$SPILLOVER`
  127. colnames(comp)<-colnames(gs)[5:14]
  128. rownames(comp)<-colnames(gs)[5:14]
  129. gs<-compensate(gs, comp)
  130. trans_params<-transform_gs(gs)
  131. markers<-colnames(gs)
  132. markers[c(7,13)]<-c("CD19","L&D")
  133. names(markers)<-colnames(gs)
  134. markernames(gs)<-markers
  135. sampleNames(gs)<-gsub("\\s[0-9]*.LMD","",sampleNames(gs))
  136. sampleNames(gs)<-gsub(".*\\s","",sampleNames(gs))
  137. pData(gs)$name<-rownames(pData(gs))
  138. gs_gate_interactive(gs,
  139. filterId = "Leukocytes",
  140. dims = list("FS-A", "SS-A"))
  141. gs_gate_interactive(gs,
  142. subset = "Leukocytes",
  143. filterId = "CD19 L&D",
  144. dims = list("CD19", "L&D"))
  145. g<-ggcyto(gs, subset = "Leukocytes", bins=128, aes(CD19,`L&D`))+
  146. facet_grid(.~factor(name, levels=c("Unst","aCDE19")))+
  147. geom_hex(bins=128)+
  148. geom_gate()+
  149. geom_stats()+
  150. scale_fill_gradient(low="black", high="violet")
  151. stats<-gs_pop_get_stats(gs, nodes=gs_get_pop_paths(gs, path = "auto")[3:6], type="perc")
  152. stats
  153. g2<-ggplot(stats, aes(factor(sample, levels=c("Unst","aCDE19")), percent, fill=pop))+
  154. geom_bar(stat="identity", color="black")+xlab("Samples")
  155. ggpubr::ggarrange(as.ggplot(g), g2, ncol=1)
  156. ```