-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstep5.FitMS.Rmd
executable file
·201 lines (134 loc) · 7.29 KB
/
step5.FitMS.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
---
title: "fitms results smoking ALL"
author: "Keren Xu"
date: "04/28/2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = T)
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(message = FALSE)
knitr::opts_chunk$set(fig.width= 7, fig.height=5)
```
# Load packages
```{r}
library(tidyverse)
require(dplyr)
require(data.table)
library(pals)
library(colorblindr)
theme_set(theme_light())
setwd("/dir/FitMS.out")
```
## R Markdown
```{r}
# read file path
all_paths <- paste0(list.dirs("/dir/FitMS.out")[-c(1)][seq(3, 152, by = 4)], "/contributions.csv")
# read file content
all_content <-
all_paths %>%
lapply(read.csv,
header = TRUE)
# read file name
all_filenames <- all_paths %>%
as.list()
# combine file content list and file name list
all_lists <- mapply(c, all_content, all_filenames, SIMPLIFY = FALSE)
# unlist all lists and change column name
all_result <- rbindlist(all_lists, fill = T)
# change column name
all_result$sample_id <- gsub("/signatures/1/contributions.csv", "", all_result$V1)
all_result$sample_id <- gsub("/dir/FitMS.out/TM_","",all_result$sample_id)
all_result <- all_result %>% select(-V1)
all_result <- all_result %>% filter(Contribution!=0)
all_result %>% count(sample_id)
```
# add unassigned bases
```{r}
# read file path
all_paths <- paste0(list.dirs("/dir/FitMS.out")[-c(1)][seq(1, 152, by = 4)], "/catalog.csv")
# read file content
all_content <-
all_paths %>%
lapply(read.csv,
header = TRUE)
function_sum <- function(df){
df %>% .[,2] %>% sum()
}
total_base <- all_content %>% map(function_sum) %>% unlist()
# read file name
all_filenames <- all_paths
df_total_base <- data.frame(File.Path = all_filenames, total_base = total_base)
df_total_base$sample_id <- gsub("/catalog.csv","", df_total_base$File.Path)
df_total_base$sample_id <- gsub("/dir/FitMS.out/TM_","",df_total_base$sample_id)
df_total_base <- df_total_base %>% select(-c("File.Path"))
df_total_base %>% arrange(-total_base)
```
# stacked bar graph
```{r}
all_result <- all_result %>% mutate(Contribution = round(Contribution,digits = 0))
all_result <- all_result %>% left_join(all_result %>% group_by(sample_id) %>% summarize(total = sum(Contribution)), by = "sample_id")
all_result <- all_result %>% left_join(df_total_base, by = "sample_id") %>% mutate(unassigned = total_base-total)
unassigned_rows <- all_result %>% select(sample_id, unassigned) %>% distinct() %>% rename("Contribution" = "unassigned") %>% mutate(Signature = "unassigned")
all_result <- rbind(all_result, unassigned_rows, fill = TRUE) %>% select(Signature, Contribution, sample_id)
all_result <- all_result %>% left_join(df_total_base, by = "sample_id")
# all_result <- all_result %>% mutate(Contribution = ifelse(Contribution<0, 0, Contribution))
all_result %>% count(Signature) %>% pull(Signature) %>% dput()
# Stacked
ggplot(all_result, aes(fill=factor(Signature, levels=c("unassigned", "GEL-Lymphoid_common_SBS127", "GEL-Lymphoid_common_SBS101", "GEL-Lymphoid_common_SBS18", "GEL-Lymphoid_common_SBS17",
"GEL-Lymphoid_common_SBS9", "GEL-Lymphoid_common_SBS5+8", "GEL-Lymphoid_common_SBS5_b", "GEL-Lymphoid_common_SBS5_a", "GEL-Lymphoid_common_SBS1")), y=Contribution, x=reorder(sample_id, -total_base))) +
geom_bar(position="stack", stat="identity")+ theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) + scale_fill_manual(values=as.vector(cols25(25))) + guides(fill = guide_legend(title = "Signature"))+labs(x = "sample id")
# compare to the tooth project, it has K but no
# percent
ggplot(all_result, aes(fill=factor(Signature, levels=c("unassigned", "GEL-Lymphoid_common_SBS127", "GEL-Lymphoid_common_SBS101", "GEL-Lymphoid_common_SBS18", "GEL-Lymphoid_common_SBS17",
"GEL-Lymphoid_common_SBS9", "GEL-Lymphoid_common_SBS5+8", "GEL-Lymphoid_common_SBS5_b", "GEL-Lymphoid_common_SBS5_a", "GEL-Lymphoid_common_SBS1")), y=Contribution, x=reorder(sample_id, -total_base))) +
geom_bar(position="fill", stat="identity")+ theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) + scale_fill_manual(values=as.vector(cols25(25))) + guides(fill = guide_legend(title = "Signature"))+labs(x = "sample id")
all_result %>% fwrite("fitms.out.contribution.txt", sep = "\t")
```
# reference contribution
```{r}
# read file path
all_paths <- paste0(list.dirs("/dir/FitMS.out")[-c(1)][seq(3, 152, by = 4)], "/reference_contributions.csv")
# read file content
all_content <-
all_paths %>%
lapply(read.csv,
header = TRUE)
# read file name
all_filenames <- all_paths %>%
as.list()
# combine file content list and file name list
all_lists <- mapply(c, all_content, all_filenames, SIMPLIFY = FALSE)
# unlist all lists and change column name
all_result <- rbindlist(all_lists, fill = T)
# change column name
all_result$sample_id <- gsub("/signatures/1/reference_contributions.csv", "", all_result$V1)
all_result$sample_id <- gsub("/dir/FitMS.out/TM_","",all_result$sample_id)
all_result <- all_result %>% select(-V1)
all_result <- all_result %>% filter(Contribution!=0)
all_result %>% count(sample_id)
```
# stacked bar graph
```{r}
all_result <- all_result %>% mutate(Contribution = round(Contribution,digits = 0))
all_result <- all_result %>% left_join(all_result %>% group_by(sample_id) %>% summarize(total = sum(Contribution)), by = "sample_id")
all_result <- all_result %>% left_join(df_total_base, by = "sample_id") %>% mutate(unassigned = total_base-total)
unassigned_rows <- all_result %>% select(sample_id, unassigned) %>% distinct() %>% rename("Contribution" = "unassigned") %>% mutate(Signature = "unassigned")
all_result <- rbind(all_result, unassigned_rows, fill = TRUE) %>% select(Signature, Contribution, sample_id)
all_result <- all_result %>% left_join(df_total_base, by = "sample_id")
all_result <- all_result %>% mutate(Contribution = ifelse(Contribution<0, 0, Contribution))
# compare to the tooth project, no RefSig 30, has RefSig N1
# Stacked
ggplot(all_result, aes(fill=factor(Signature, levels=c("unassigned","SBS127","SBS101","SBS18","SBS17","SBS9","SBS8", "SBS5", "SBS1")), y=Contribution, x=reorder(sample_id, -total_base))) +
geom_bar(position="stack", stat="identity")+ theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +scale_fill_manual(values=as.vector(cols25(25))) + guides(fill = guide_legend(title = "Signature"))+labs(x = "sample id")
# percent
ggplot(all_result, aes(fill=factor(Signature, levels=c("unassigned","SBS127","SBS101","SBS18","SBS17","SBS9","SBS8", "SBS5", "SBS1")), y=Contribution, x=reorder(sample_id, -total_base))) +
geom_bar(position="fill", stat="identity")+ theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) + scale_fill_manual(values=as.vector(cols25(25))) + guides(fill = guide_legend(title = "Signature")) + labs(x = "sample id")
# # check proportions
# all_result %>% mutate(proportion = Contribution/total_base) -> all_result
# all_result %>% filter(Signature == "RefSig 13") %>% filter(proportion > 0.1) %>% arrange(proportion) %>% pull(sample_id)
# all_result %>% filter(Signature == "RefSig 2") %>% filter(proportion > 0.1) %>% arrange(proportion) %>% pull(sample_id)
# all_result %>% filter(Signature == "RefSig 18") %>% filter(proportion > 0.1) %>% arrange(proportion) %>% pull(sample_id) %>% dput()
all_result %>% fwrite("fitms.out.reference_contribution.txt", sep = "\t")
```
ˆ