|
| 1 | +set.seed(seed = 12345) |
| 2 | +require(rstan) |
| 3 | + |
| 4 | +# Stan generative model |
| 5 | +sim_stan <- " |
| 6 | +functions { |
| 7 | + int zibb_rng(int y, int n, real mu, real phi, real kappa) { |
| 8 | + if (bernoulli_rng(kappa) == 1) { |
| 9 | + return (0); |
| 10 | + } else { |
| 11 | + return (beta_binomial_rng(n, mu * phi, (1 - mu) * phi)); |
| 12 | + } |
| 13 | + } |
| 14 | +} |
| 15 | +
|
| 16 | +data { |
| 17 | + int<lower=0> N_sample; // number of repertoires |
| 18 | + int<lower=0> N_gene; // gene |
| 19 | + int<lower=0> N_individual; // number of individuals |
| 20 | + int<lower=0> N_condition; // number of conditions |
| 21 | + array [N_sample] int N; // repertoire size |
| 22 | + array [N_individual] int condition_id; // id of conditions |
| 23 | + array [N_sample] int individual_id; // id of replicate |
| 24 | + vector [N_gene] alpha; |
| 25 | + real <lower=0> phi; |
| 26 | + real <lower=0, upper=1> kappa; |
| 27 | + array [N_condition] vector [N_gene] beta_condition; |
| 28 | + vector <lower=0> [N_condition] sigma_condition; |
| 29 | + vector <lower=0> [N_condition] sigma_individual; |
| 30 | + real <lower=0> sigma_replicate; |
| 31 | +} |
| 32 | +
|
| 33 | +generated quantities { |
| 34 | + array [N_sample] vector <lower=0, upper=1> [N_gene] theta; |
| 35 | + array [N_sample] vector [N_gene] beta_sample; |
| 36 | + array [N_individual] vector [N_gene] beta_individual; |
| 37 | + |
| 38 | + // generate usage |
| 39 | + array [N_gene, N_sample] int Y; |
| 40 | + |
| 41 | + for(i in 1:N_individual) { |
| 42 | + for(j in 1:N_gene) { |
| 43 | + beta_individual[i][j] = normal_rng(beta_condition[condition_id[i]][j], sigma_individual[condition_id[i]]); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + for(i in 1:N_sample) { |
| 48 | + for(j in 1:N_gene) { |
| 49 | + beta_sample[i][j] = normal_rng(beta_individual[individual_id[i]][j], sigma_replicate); |
| 50 | + theta[i][j] = inv_logit(alpha[j] + beta_sample[i][j]); |
| 51 | + Y[j, i] = zibb_rng(Y[j, i], N[i], theta[i][j], phi, kappa); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | +" |
| 56 | + |
| 57 | +# compile model |
| 58 | +m <- rstan::stan_model(model_code = sim_stan) |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +# int<lower=0> N_sample; // number of repertoires |
| 63 | +# int<lower=0> N_gene; // gene |
| 64 | +# int<lower=0> N_individual; // number of individuals |
| 65 | +# int<lower=0> N_condition; // number of conditions |
| 66 | +# array [N_sample] int N; // repertoire size |
| 67 | +# array [N_individual] int condition_id; // id of conditions |
| 68 | +# array [N_sample] int individual_id; // id of replicate |
| 69 | +# vector [N_gene] alpha; |
| 70 | +# real <lower=0> phi; |
| 71 | +# real <lower=0, upper=1> kappa; |
| 72 | +# array [N_condition] vector [N_gene] beta_condition; |
| 73 | +# vector <lower=0> [N_condition] sigma_condition; |
| 74 | +# vector <lower=0> [N_condition] sigma_individual; |
| 75 | +# real <lower=0> sigma_replicate; |
| 76 | + |
| 77 | +# generate data based on the following parameters parameters |
| 78 | +set.seed(1005001) |
| 79 | +N_gene <- 15 |
| 80 | +N_replicates <- 3 |
| 81 | +N_individual <- 9 |
| 82 | +N_condition <- 3 |
| 83 | +N_sample <- N_individual * N_replicates |
| 84 | + |
| 85 | +condition_id <- rep(x = 1:N_condition, each = N_individual/N_condition) |
| 86 | + |
| 87 | +N <- rep(x = 1000, times = N_sample) |
| 88 | + |
| 89 | +individual_id <- rep(x = 1:N_individual, each = N_replicates) |
| 90 | + |
| 91 | +alpha <- rnorm(n = N_gene, mean = -5, sd = 3) |
| 92 | + |
| 93 | +phi <- 200 |
| 94 | + |
| 95 | +kappa <- 0.02 |
| 96 | + |
| 97 | +beta_condition <- array(data = 0, dim = c(N_condition, N_gene)) |
| 98 | +for(c in 1:N_condition) { |
| 99 | + for(g in 1:N_gene) { |
| 100 | + u <- runif(n = 1, min = 0, max = 1) |
| 101 | + if(u <= 0.95) { |
| 102 | + beta_condition[c,g] <- rnorm(n = 1, mean = 0, sd = 0.5) |
| 103 | + } else { |
| 104 | + beta_condition[c,g] <- rnorm(n = 1, mean = 0, sd = 5) |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +sigma_condition <- rep(x = 1, times = N_condition) |
| 110 | +sigma_individual <- rep(x = 0.5, times = N_condition) |
| 111 | +sigma_replicate <- 0.1 |
| 112 | + |
| 113 | + |
| 114 | +l <- list(N_sample = N_sample, |
| 115 | + N_gene = N_gene, |
| 116 | + N_individual = N_individual, |
| 117 | + N_condition = N_condition, |
| 118 | + N = N, |
| 119 | + condition_id = condition_id, |
| 120 | + individual_id = individual_id, |
| 121 | + alpha = alpha, |
| 122 | + phi = phi, |
| 123 | + kappa = kappa, |
| 124 | + beta_condition = beta_condition, |
| 125 | + sigma_condition = sigma_condition, |
| 126 | + sigma_individual = sigma_individual, |
| 127 | + sigma_replicate = sigma_replicate) |
| 128 | + |
| 129 | +# simulate |
| 130 | +sim <- rstan::sampling(object = m, |
| 131 | + data = l, |
| 132 | + iter = 1, |
| 133 | + chains = 1, |
| 134 | + algorithm="Fixed_param") |
| 135 | + |
| 136 | +# extract simulation and convert into data frame which can |
| 137 | +# be used as input of IgGeneUsage |
| 138 | +ysim <- rstan::extract(object = sim, par = "Y")$Y |
| 139 | +ysim <- ysim[1,,] |
| 140 | + |
| 141 | +ysim_df <- reshape2::melt(ysim) |
| 142 | +colnames(ysim_df) <- c("gene_name", "sample_id", "gene_usage_count") |
| 143 | + |
| 144 | +m <- data.frame(sample_id = 1:l$N_sample, |
| 145 | + individual_id = l$individual_id) |
| 146 | + |
| 147 | +ysim_df <- merge(x = ysim_df, y = m, by = "sample_id", all.x = T) |
| 148 | + |
| 149 | +m <- data.frame(individual_id = 1:l$N_individual, |
| 150 | + condition_id = l$condition_id) |
| 151 | + |
| 152 | +ysim_df <- merge(x = ysim_df, y = m, by = "individual_id", all.x = T) |
| 153 | + |
| 154 | + |
| 155 | +ysim_df$condition <- paste0("C_", ysim_df$condition_id) |
| 156 | +ysim_df$gene_name <- paste0("G_", ysim_df$gene_name) |
| 157 | +ysim_df$individual_id <- paste0("I_", ysim_df$individual_id) |
| 158 | + |
| 159 | +ysim_df$replicate <- rep(rep(x = c("R_1", "R_2", "R_3"), each = 15), times = 9) |
| 160 | +ysim_df$condition_id <- NULL |
| 161 | +ysim_df$sample_id <- NULL |
| 162 | +ysim_df <- ysim_df[, c("individual_id", "condition", "gene_name", |
| 163 | + "replicate", "gene_usage_count")] |
| 164 | + |
| 165 | +d_zibb_4 <- ysim_df |
| 166 | + |
| 167 | +# save |
| 168 | +save(d_zibb_4, file = "data/d_zibb_4.RData", compress = T) |
| 169 | + |
| 170 | + |
| 171 | +# ggplot(data = d_zibb_4)+ |
| 172 | +# geom_sina(aes(x = gene_name, y = gene_usage_count, col = condition))+ |
| 173 | +# theme_bw(base_size = 10)+ |
| 174 | +# theme(legend.position = "none") |
0 commit comments