Skip to content

Commit 0301c45

Browse files
committed
end of rotation
1 parent 5049f3f commit 0301c45

7 files changed

+17907
-28
lines changed

.DS_Store

0 Bytes
Binary file not shown.

DE_5dpi_naive.pdf

539 KB
Binary file not shown.

debug.Rmd

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
---
2+
title: "debug"
3+
author: "Yifan Duan"
4+
date: "2024-10-03"
5+
output: html_document
6+
---
7+
8+
```{r setup, include=FALSE}
9+
knitr::opts_chunk$set(echo = TRUE)
10+
```
11+
12+
13+
```{r}
14+
BiocManager::install("clusterProfiler")
15+
BiocManager::install("pathview")
16+
BiocManager::install("enrichplot")
17+
library(clusterProfiler)
18+
library(enrichplot)
19+
# we use ggplot2 to add x axis labels (ex: ridgeplot)
20+
library(ggplot2)
21+
```
22+
23+
24+
```{r}
25+
Idents(filtered_mature_neuron_obj) <- "Time.Point"
26+
table(filtered_mature_neuron_obj$Time.Point)
27+
naive_5_markers_dpi <- FindMarkers(filtered_mature_neuron_obj,
28+
ident.1 = "Naive",
29+
ident.2 = "5 dpi",
30+
slot = "data",
31+
min.pct = 0,
32+
logfc.threshold = 0)
33+
34+
library(EnhancedVolcano)
35+
pdf("DE_5dpi_naive.pdf")
36+
EnhancedVolcano(naive_5_markers_dpi,
37+
rownames(naive_5_markers_dpi),
38+
x ="avg_log2FC",
39+
y ="p_val_adj")
40+
dev.off()
41+
naive_5_markers_dpi |> filter(abs(avg_log2FC) > 2 & p_val_adj < 0.01)
42+
```
43+
44+
45+
```{r}
46+
# SET THE DESIRED ORGANISM HERE
47+
organism = "org.Mm.eg.db"
48+
BiocManager::install(organism, character.only = TRUE)
49+
library(organism, character.only = TRUE)
50+
```
51+
52+
53+
```{r}
54+
# reading in data from deseq2
55+
df <- naive_5_markers_dpi
56+
57+
# we want the log2 fold change
58+
original_gene_list <- df$avg_log2FC
59+
60+
# name the vector
61+
names(original_gene_list) <- rownames(df)
62+
63+
# omit any NA values
64+
gene_list<-na.omit(original_gene_list)
65+
66+
# sort the list in decreasing order (required for clusterProfiler)
67+
gene_list = sort(gene_list, decreasing = TRUE)
68+
```
69+
70+
71+
```{r}
72+
keytypes(org.Mm.eg.db)
73+
74+
gse <- gseGO(geneList=gene_list,
75+
ont ="ALL",
76+
keyType = "SYMBOL",
77+
minGSSize = 10,
78+
maxGSSize = 500,
79+
pvalueCutoff = 0.01,
80+
pAdjustMethod = "BH",
81+
verbose = TRUE,
82+
OrgDb = organism)
83+
84+
dotplot(gse, showCategory = 10, title = "GSEA")
85+
head(gse@result)
86+
head(gse@result$NES)
87+
```
88+
89+
90+
```{r}
91+
df <- naive_5_markers_dpi |> filter(abs(avg_log2FC) > 2 & p_val_adj < 0.01)
92+
93+
# we want the log2 fold change
94+
original_gene_list <- df$avg_log2FC
95+
96+
# name the vector
97+
names(original_gene_list) <- rownames(df)
98+
99+
# omit any NA values
100+
gene_list <- na.omit(original_gene_list)
101+
102+
# sort the list in decreasing order (required for clusterProfiler)
103+
gene_list = sort(gene_list, decreasing = TRUE)
104+
105+
106+
107+
go_enrich <- enrichGO(gene = names(gene_list),
108+
OrgDb = organism,
109+
keyType = 'SYMBOL',
110+
readable = T,
111+
ont = "ALL",
112+
pvalueCutoff = 0.05,
113+
qvalueCutoff = 0.1)
114+
115+
barplot(go_enrich,
116+
drop = TRUE,
117+
showCategory = 10,
118+
title = "GO analysis",
119+
font.size = 8)
120+
121+
DoHeatmap(filtered_mature_neuron_obj, features = rownames(df))
122+
```
123+

dpi_distance.Rmd

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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

Comments
 (0)