-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpop_generator.Rmd
154 lines (120 loc) · 3.84 KB
/
pop_generator.Rmd
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
---
title: "POP generator"
output: html_document
params:
pop_number: "05_05_a"
cells: 5000
seed: 23
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Parameters
```{r}
params
```
```{r INFO}
# MFI values for NEG and POS cells
neg_mean <- 50
neg_sd <- 1000
pos_mean <- 5000
pos_sd <- 1000
# phenotypes OF INTEREST
B <- c("CD3" = "low", "CD4" = "low", "CD8" = "low", "CD56" = "low", "CD19" = "high")
NK <- c("CD3" = "low", "CD4" = "low", "CD8" = "low", "CD56" = "high", "CD19" = "low")
T4 <- c("CD3" = "high", "CD4" = "high", "CD8" = "low", "CD56" = "low", "CD19" = "low")
T8 <- c("CD3" = "high", "CD4" = "low", "CD8" = "high", "CD56" = "low", "CD19" = "low")
NKT_NN <- c("CD3" = "high", "CD4" = "low", "CD8" = "low", "CD56" = "high", "CD19" = "low")
NKT_4 <- c("CD3" = "high", "CD4" = "high", "CD8" = "low", "CD56" = "high", "CD19" = "low")
NKT_8 <- c("CD3" = "high", "CD4" = "low", "CD8" = "high", "CD56" = "high", "CD19" = "low")
U1 <- c("CD3" = "low", "CD4" = "low", "CD8" = "low", "CD56" = "low", "CD19" = "low")
U2 <- c("CD3" = "low", "CD4" = "high", "CD8" = "low", "CD56" = "low", "CD19" = "low")
U3 <- c("CD3" = "high", "CD4" = "low", "CD8" = "low", "CD56" = "low", "CD19" = "low")
U4 <- c("CD3" = "high", "CD4" = "low", "CD8" = "low", "CD56" = "low", "CD19" = "high")
pheno <- ( as.data.frame( rbind(B, NK, T4, T8, NKT_NN, NKT_4, NKT_8, U1, U2, U3, U4) ) )
# PERCENTAGE OF CELLS PER CELL TYPE
# Let it be the same for all cell types:
p <- 100/nrow(pheno)
# or specify it here:
pc <- c("B" = 15, "NK" = 6, "T4" = 44, "T8" = 17.5, "NKT_NN" = 2, "NKT_4" = 0.5, "NKT_8" = 1.5,
"U1" = 4.25, "U2" = 0.75, "U3" = 6.5, "U4" = 2)
# save the proportions to a file
saveRDS(pc, paste("props", params$pop_number, Sys.Date(), sep = "_"))
# total number of cells
n <- params$cells
# number of cells per cell type
ns <- c()
for(i in 1:length(pc)){
ns[i] <- round(n*pc[[i]]/100)
}
# final number of cells
n <- sum(ns)
# cell types
cells <- rownames(pheno)
# markers
markers <- colnames(pheno)
pheno
```
```{r}
# creating the dataframe
set.seed(params$seed)
for(i in (1:length(cells))){ # i takes values [1, number of CELL TYPES]
# For cell type i, repeat its name as many times as the number of cells
c <- rep(cells[i], ns[i])
# One column will be created for every marker with intensity values
for(j in (1:length(markers))){
if(pheno[i,j] == "low"){
p <- rnorm(ns[[i]], mean = neg_mean, sd = neg_sd)
}else{
p <- rnorm(ns[[i]], mean = pos_mean, sd = pos_sd)
}
# all the columns (one per marker) are joined:
# convert the "c" list as data frame to preserve numeric "p" values after cbind-ing
c <- cbind(as.data.frame(c), p)
}
if(i==1){
# Values for the first cell type start to fill the dataframe "pop"
pop <- c
}else{
# Values for the other cell types are joined to "pop"
pop <- rbind(pop, c)
}
}
# Name the variables:
names(pop) <- c("cells", markers)
#### REARRANGING ###
# Randomly sampling
set.seed(42)
x <- sample(1:n, n, replace = F)
pop <- pop[x,] # Rearranging the dataframe
rownames(pop) <- 1:n # Renaming the rows (in order)
# Save the column with the cell labels to a file
saveRDS(pop$cells, paste("labels", params$pop_number, Sys.Date(), sep = "_"))
head(pop)
```
```{r}
#par(mfrow = c(ceiling(length(markers)/2), 2))
for(i in 1:length(markers)){
boxplot(pop[,i+1] ~ pop$cells, xlab = markers[i])
}
```
```{r, eval = F}
plot(pop[,2:(length(markers)+1)])
```
# Conversion to `FlowFrame` class objects
```{r}
library(flowCore)
ff <- new("flowFrame", exprs = as.matrix(pop[,-1]))
head(ff@exprs)
```
```{r, eval = F}
library(ggcyto)
autoplot(ff, markers[1], markers[5])
```
```{r, eval = F}
autoplot(ff, markers[1])
```
# Export to .fcs
```{r, eval = T}
write.FCS(ff, paste("ff_", params$pop_number, "_", Sys.Date(), ".fcs", sep = ""))
```