Skip to content

Instantly share code, notes, and snippets.

@siero5335
Last active February 21, 2023 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siero5335/bb1e39df80d6e222b40dc2eb33109ef6 to your computer and use it in GitHub Desktop.
Save siero5335/bb1e39df80d6e222b40dc2eb33109ef6 to your computer and use it in GitHub Desktop.
library(tidyverse)
ms_dial_filter <- function(df, adduct, annotatelevel, cv) {
df %>% dplyr::select(`Metabolite name`, `Adduct type`, `Annotation tag (VS1.0)`, `Total score`,
`RT similarity`, `Average Rt(min)`, INCHIKEY , 33:ncol(df)) %>%
filter(`Annotation tag (VS1.0)` %in% annotatelevel) %>%
filter(`Adduct type` %in% adduct) %>%
mutate(qc_mean = rowMeans(dplyr::select(., contains("QC")))) %>%
filter(qc_mean != 0) %>%
mutate(qc_cv = apply(dplyr::select(., contains("QC")), 1, sd)/ qc_mean * 100) %>%
group_by(`Metabolite name`, qc_mean) %>%
arrange(desc(qc_mean)) %>%
ungroup() %>%
distinct(`Metabolite name`, .keep_all = T) %>%
group_by(INCHIKEY, qc_mean) %>%
arrange(desc(qc_mean)) %>%
ungroup() %>%
distinct(INCHIKEY, .keep_all = T) %>%
filter(qc_cv < cv)
}
### Demo
# df <- read_tsv("hoge.txt", skip = 4) #exported file from MS-dial
# posi_adduct <- unique(df1$`Adduct type`)
# annotate_level <- c(400, 410, 420, 430)
# tmp <- ms_dial_filter(df, annotatelevel = annotate_level, adduct = posi_adduct, cv = 30)
@siero5335
Copy link
Author

siero5335 commented Mar 5, 2022

This is a function to filter aligned text output from MS-dial (Ver4.8.0).
Requires tidyverse package.
File name for QC must contain "QC"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment