-
Notifications
You must be signed in to change notification settings - Fork 0
/
tissue_troponin_abundance.R
129 lines (110 loc) · 4.4 KB
/
tissue_troponin_abundance.R
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
troponin_filter_tissue <- function(df, meta){
left_join(df, meta) %>%
filter(!str_detect(umap_name, 'Doublets')) %>%
filter(on_steroids == 'False') %>%
filter(days_from_collection <= 3) %>%
filter(!donor %in% c('SIC_182', 'SIC_333')) %>%
mutate(lineage_names = umap_name,
cluster_names = lineage_subcluster_name)
}
# get percentages for model
troponin_get_percents_per_level <- function(df, level = 'cluster'){
# get donor troponin info
troponin_donor <- df %>%
dplyr::select(donor, nearest_troponin) %>%
distinct()
# get total number of donor cells
ncells_donor <- df %>%
group_by(donor) %>%
summarize(ncells_donor = n())
# get total number of donor,level cells
ncells_level <- df %>%
group_by(donor, !!sym(glue('{level}_names'))) %>%
summarize(!!sym(glue('ncells_{level}')) := n()) %>%
ungroup() %>%
complete(donor, !!sym(glue('{level}_names')),
fill = list2(!!sym(glue("ncells_{level}")) := 0))
# put data together and get level percentages
model_df <- ncells_level %>%
left_join(ncells_donor) %>%
left_join(troponin_donor) %>%
mutate(!!sym(glue('{level}_prop_by_all')) := !!sym(glue('ncells_{level}'))/ncells_donor)
return(model_df)
}
troponin_fit_model <- function(df, level = 'cluster'){
# define model to fit
fit_lm_by_level <- function(df){
f <- paste(glue("log({level}_prop_by_all + 1)"), " ~ log(nearest_troponin)")
lm(f, data=df)
}
# fit model
model_df <- df %>%
group_by_at(glue("{level}_names")) %>%
nest() %>%
mutate(model = map(data, fit_lm_by_level)) %>%
mutate(trop_coef = map(model, function(model){summary(model)$coefficients[2,1]}),
trop_se = map(model, function(model){summary(model)$coefficients[2,2]}),
trop_pval = map(model, function(model){summary(model)$coefficients[2,4]}))
# adjust pvalues using FDR
pvals <- p.adjust(model_df$trop_pval, method='fdr')
model_df$padj <- pvals
return(model_df)
}
set_factor_order <- function(df, col_name, order){
df %>%
mutate(!!sym(col_name) := factor(!!sym(col_name), levels = order))
}
troponin_plot_model <- function(model_res, model_data, title,
level='cluster', type='simple', point_size=2.2){
# format columns for printing
model_annot <- model_res %>%
dplyr::select(!!sym(glue("{level}_names")), trop_coef, trop_se, trop_pval, padj) %>%
mutate(
trop_coef = round(as.numeric(trop_coef), 2),
trop_se = round(as.numeric(trop_se), 2),
trop_pval = signif(as.numeric(trop_pval), 2),
padj = signif(as.numeric(padj), 2),
trop_label = paste0("Coef: ", trop_coef, " +/- ", trop_se),
pval_label = paste0("pval = ", trop_pval, "; padj = ", padj)
)
# plot
if (type == 'simple'){
p <- model_data %>%
ggplot(aes(x = nearest_troponin,
y = !!sym(glue("{level}_prop_by_all")))) +
geom_point(size=point_size) +
geom_smooth(method = lm, se = FALSE, color='black', linetype="dashed") +
theme_bw() +
theme(text = element_text(size = 16),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
scale_x_log10() +
ylim(0, NA) +
facet_wrap(as.formula(glue("~ {level}_names")),
scales = "free_y") +
labs(x = "Nearest Troponin",
y = paste("Proportion by All Donor Cells"),
title = title,
subtitle = glue("Log({str_to_title(level)} Proportion + 1) ~ Log(Nearest Troponin)"))
} else if (type == 'detailed'){
p <- model_data %>%
ggplot(aes(x = nearest_troponin,
y = !!sym(glue("{level}_prop_by_all")))) +
geom_point(size=point_size) +
geom_text(data = model_annot,aes(label = trop_label),
x = -Inf, y = -Inf, hjust = -0.1, vjust = -2 , size=4.5) +
geom_text(data = model_annot,aes(label = pval_label),
x = -Inf, y = -Inf, hjust = -0.1, vjust = -1, size=4.5) +
geom_smooth(method = lm, se = FALSE) +
theme_bw() +
theme(text = element_text(size = 16)) +
scale_x_log10() +
facet_wrap(as.formula(glue("~ {level}_names")),
scales = "free_y") +
labs(x = "Nearest Troponin",
y = paste("Proportion by All Donor Cells"),
title = title,
subtitle = glue("Log({str_to_title(level)} Proportion + 1) ~ Log(Nearest Troponin)"))
}
print(p)
}