|
| 1 | +--- |
| 2 | +title: "debug" |
| 3 | +authdpi: "Yifan Duan" |
| 4 | +date: "2024-09-26" |
| 5 | +output: html_document |
| 6 | +--- |
| 7 | + |
| 8 | +```{r setup, include=FALSE} |
| 9 | +knitr::opts_chunk$set(echo = TRUE) |
| 10 | +``` |
| 11 | + |
| 12 | + |
| 13 | +```{r} |
| 14 | +subset_cell_metadata |
| 15 | +test <- subset_cell_metadata |> filter(OR == "Gm10310") |
| 16 | +test |
| 17 | +# access the distance matrix for these column and group by dpi, summarize distance? |
| 18 | +mat_for_or <- cos_dist[test$cell_name, test$cell_name] |
| 19 | +``` |
| 20 | + |
| 21 | + |
| 22 | +```{r} |
| 23 | +# Assuming or_list contains the list of ORs to process |
| 24 | +or_list <- unique(subset_cell_metadata$OR) |
| 25 | +
|
| 26 | +# Initialize an empty list to store the results for each OR |
| 27 | +results_list <- lapply(or_list, function(or) { |
| 28 | + |
| 29 | + # Filter the metadata for the current OR |
| 30 | + test <- subset_cell_metadata |> filter(OR == or) |
| 31 | + |
| 32 | + # Check if there are enough cells for this OR |
| 33 | + if(nrow(test) > 1) { |
| 34 | + |
| 35 | + # Access the distance matrix for these cells |
| 36 | + mat_for_or <- cos_dist[test$cell_name, test$cell_name] |
| 37 | + |
| 38 | + # Extract the dpi information from the cell names |
| 39 | + get_dpi <- as.factor(sub("_.*", "", rownames(mat_for_or))) |
| 40 | + dpi_list <- levels(get_dpi) |
| 41 | + |
| 42 | + # Calculate median distances for each dpi level |
| 43 | + dpi_medians <- sapply(dpi_list, function(dpi) { |
| 44 | + # Subset the matrix for the current dpi group |
| 45 | + mat_subset <- mat_for_or[get_dpi == dpi, get_dpi == dpi] |
| 46 | + |
| 47 | + # Get the upper triangular part of the matrix without the diagonal |
| 48 | + upper_tri <- mat_subset[upper.tri(mat_subset)] |
| 49 | + |
| 50 | + # Calculate the median of the upper triangular part |
| 51 | + median(upper_tri, na.rm = TRUE) |
| 52 | + }) |
| 53 | + |
| 54 | + # Create a dataframe to store the results for this OR |
| 55 | + data.frame( |
| 56 | + OR = or, |
| 57 | + dpi = dpi_list, |
| 58 | + median_distance = dpi_medians |
| 59 | + ) |
| 60 | + } |
| 61 | +}) |
| 62 | +
|
| 63 | +# Combine the results into a single dataframe |
| 64 | +final_results_within <- do.call(rbind, results_list) |
| 65 | +
|
| 66 | +# Display the final dataframe |
| 67 | +final_results_within |> ggplot(aes(x = median_distance)) + geom_density() + |
| 68 | + theme_cowplot() |
| 69 | +``` |
| 70 | + |
| 71 | +```{r} |
| 72 | +# Assuming or_list contains the list of ORs to process |
| 73 | +or_list <- unique(subset_cell_metadata$OR) |
| 74 | +
|
| 75 | +# Initialize an empty list to store the results for each OR |
| 76 | +results_list <- lapply(or_list, function(or) { |
| 77 | + |
| 78 | + # Filter the metadata for the current OR |
| 79 | + test <- subset_cell_metadata |> filter(OR == or) |
| 80 | + |
| 81 | + # Check if there are enough cells for this OR |
| 82 | + if(nrow(test) > 1) { |
| 83 | + |
| 84 | + # Access the distance matrix for these cells |
| 85 | + mat_for_or <- cos_dist[test$cell_name, test$cell_name] |
| 86 | + |
| 87 | + # Extract the dpi information from the cell names |
| 88 | + get_dpi <- as.factor(sub("_.*", "", rownames(mat_for_or))) |
| 89 | + dpi_list <- levels(get_dpi) |
| 90 | + |
| 91 | + # Initialize an empty list to store the pairwise dpi comparisons |
| 92 | + pairwise_results <- list() |
| 93 | + |
| 94 | + # Compare all pairs of dpi levels |
| 95 | + for(i in 1:(length(dpi_list) - 1)) { |
| 96 | + for(j in (i + 1):length(dpi_list)) { |
| 97 | + dpi1 <- dpi_list[i] |
| 98 | + dpi2 <- dpi_list[j] |
| 99 | + |
| 100 | + # Subset the matrix for the two dpi groups |
| 101 | + mat_subset <- mat_for_or[get_dpi == dpi1, get_dpi == dpi2] |
| 102 | + |
| 103 | + # Calculate the median of the matrix |
| 104 | + median_dist <- median(mat_subset, na.rm = TRUE) |
| 105 | + |
| 106 | + # Store the result in the list |
| 107 | + pairwise_results[[length(pairwise_results) + 1]] <- data.frame( |
| 108 | + OR = or, |
| 109 | + dpi1 = dpi1, |
| 110 | + dpi2 = dpi2, |
| 111 | + median_distance = median_dist |
| 112 | + ) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + # Combine the pairwise results for this OR into a dataframe |
| 117 | + do.call(rbind, pairwise_results) |
| 118 | + } |
| 119 | +}) |
| 120 | +
|
| 121 | +# Combine the results into a single dataframe |
| 122 | +final_results_across <- do.call(rbind, results_list) |
| 123 | +
|
| 124 | +
|
| 125 | +``` |
| 126 | + |
| 127 | + |
| 128 | +```{r} |
| 129 | +ggplot() + |
| 130 | + geom_density(aes(x = median_distance$median_dist, y = ..density.., fill = "Within OR"), alpha = 0.5) + |
| 131 | + geom_density(aes(x = final_results_within$median_distance, y = ..density.., fill = "Within OR; within dpi"), alpha = 0.5) + |
| 132 | + geom_density(aes(x = final_results_across$median_distance, y = ..density.., fill = "Within OR; between dpi"), alpha = 0.5) + |
| 133 | + scale_fill_manual(name = "Group", values = c("Within OR" = "blue", "Within OR; within dpi" = "red", "Within OR; between dpi" = "green")) + theme_cowplot() + xlab("Cosine distance") |
| 134 | +
|
| 135 | +mat_for_or[get_dpi == "D14", get_dpi == "D05"] |
| 136 | +
|
| 137 | +``` |
| 138 | + |
| 139 | +```{r} |
| 140 | +combined_results <- rbind( |
| 141 | + final_results_within[, c("median_distance", "OR")], |
| 142 | + final_results_across[, c("median_distance", "OR")] |
| 143 | +) |
| 144 | +
|
| 145 | +# Plot both density curves together with ggplot |
| 146 | +ggplot(combined_results, aes(x = median_distance)) + |
| 147 | + geom_density(alpha = 0.5) |
| 148 | +
|
| 149 | +ggplot() + |
| 150 | + geom_density(aes(x = median_distance$median_dist, y = ..density.., colour = "Within OR"), alpha = 0.5) + |
| 151 | + geom_density(aes(x = combined_results$median_distance, y = ..density.., colour = "addition"), alpha = 0.5) |
| 152 | +``` |
| 153 | + |
0 commit comments