annotate MT_for_iMQ.R @ 0:16608175f23b draft

planemo upload
author pravs
date Thu, 18 Jun 2020 01:59:27 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
16608175f23b planemo upload
pravs
parents:
diff changeset
1 # MT for iMQ: prepares metatranscriptomic outputs from ASaiM (HUMAnN2 and metaphlan) for metaquantome
16608175f23b planemo upload
pravs
parents:
diff changeset
2
16608175f23b planemo upload
pravs
parents:
diff changeset
3 # Load libraries
16608175f23b planemo upload
pravs
parents:
diff changeset
4 library(tidyverse)
16608175f23b planemo upload
pravs
parents:
diff changeset
5 library(readr)
16608175f23b planemo upload
pravs
parents:
diff changeset
6
16608175f23b planemo upload
pravs
parents:
diff changeset
7 # Set parameters from arguments
16608175f23b planemo upload
pravs
parents:
diff changeset
8 args = commandArgs(trailingOnly = TRUE)
16608175f23b planemo upload
pravs
parents:
diff changeset
9 data <- args[1]
16608175f23b planemo upload
pravs
parents:
diff changeset
10 # data: full path to file or directory:
16608175f23b planemo upload
pravs
parents:
diff changeset
11 # - if in functional or f-t mode, should be a tsv file of HUMAnN2 gene families, after joining, renaming to GO, and renormalizing to CPM.
16608175f23b planemo upload
pravs
parents:
diff changeset
12 # - if in taxonomic mode, should be a directory of tsv files of metaphlan genus-level results
16608175f23b planemo upload
pravs
parents:
diff changeset
13 mode <- args[2]
16608175f23b planemo upload
pravs
parents:
diff changeset
14 # mode:
16608175f23b planemo upload
pravs
parents:
diff changeset
15 # -"f": function
16608175f23b planemo upload
pravs
parents:
diff changeset
16 # -"t": taxonomy
16608175f23b planemo upload
pravs
parents:
diff changeset
17 # -"ft": function-taxonomy
16608175f23b planemo upload
pravs
parents:
diff changeset
18 ontology <- unlist(strsplit(args[3], split = ","))
16608175f23b planemo upload
pravs
parents:
diff changeset
19 # ontology: only for function or f-t mode. A string of the GO namespace(s) to include, separated by spaces.
16608175f23b planemo upload
pravs
parents:
diff changeset
20 # ex: to include all: "molecular_function biological_process cellular_component"
16608175f23b planemo upload
pravs
parents:
diff changeset
21 outfile <- args[4]
16608175f23b planemo upload
pravs
parents:
diff changeset
22 # outfile: full path with pathname and extension for output
16608175f23b planemo upload
pravs
parents:
diff changeset
23
16608175f23b planemo upload
pravs
parents:
diff changeset
24 # Functional mode
16608175f23b planemo upload
pravs
parents:
diff changeset
25 if (mode == "f"){
16608175f23b planemo upload
pravs
parents:
diff changeset
26 out <- read_delim(data, "\t", escape_double = FALSE, trim_ws = TRUE) %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
27 filter(!grepl(".+g__.+",`# Gene Family`)) %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
28 separate(col=`# Gene Family`, into=c("id", "Extra"), sep=": ", fill="left") %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
29 separate(col=Extra, into = c("namespace", "name"), sep = " ", fill="left", extra="merge") %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
30 mutate(namespace = if_else(namespace == "[MF]", true = "molecular_function", false = if_else(namespace == "[BP]", true = "biological_process", false = "cellular_component"))) %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
31 filter(namespace %in% ontology) %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
32 select(id, name, namespace, 4:ncol(.))
16608175f23b planemo upload
pravs
parents:
diff changeset
33 }
16608175f23b planemo upload
pravs
parents:
diff changeset
34
16608175f23b planemo upload
pravs
parents:
diff changeset
35 # Taxonomic mode
16608175f23b planemo upload
pravs
parents:
diff changeset
36 if (mode == "t"){
16608175f23b planemo upload
pravs
parents:
diff changeset
37 files <- dir(path = data)
16608175f23b planemo upload
pravs
parents:
diff changeset
38 out <- data_frame(filename = files) %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
39 mutate(file_contents= map(filename, ~read_delim(file.path(data, .),delim = "\t"))) %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
40 unnest %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
41 rename(sample = filename) %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
42 separate(col = sample, into = c("sample",NA), sep=".tsv") %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
43 pivot_wider(names_from = sample, values_from = abundance) %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
44 mutate(rank = "genus") %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
45 rename(name = genus) %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
46 mutate(id = row_number(name)) %>% # filler for taxon id but should eventually find a way to get id from database
16608175f23b planemo upload
pravs
parents:
diff changeset
47 select(id, name, rank, 2:ncol(.))
16608175f23b planemo upload
pravs
parents:
diff changeset
48 }
16608175f23b planemo upload
pravs
parents:
diff changeset
49
16608175f23b planemo upload
pravs
parents:
diff changeset
50 # Function-taxonomy mode
16608175f23b planemo upload
pravs
parents:
diff changeset
51 if (mode == "ft"){
16608175f23b planemo upload
pravs
parents:
diff changeset
52 out <- read_delim(data,"\t", escape_double = FALSE, trim_ws = TRUE) %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
53 filter(grepl(".+g__.+",`# Gene Family`)) %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
54 separate(col=`# Gene Family`, into=c("id", "Extra"), sep=": ", fill="left") %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
55 separate(col=Extra, into = c("namespace", "name"), sep = " ", fill="left", extra="merge") %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
56 separate(col = name, into = c("name", "taxa"), sep="\\|", extra = "merge") %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
57 separate(col = taxa, into = c("Extra", "genus", "species"), sep = "__") %>% select(-"Extra") %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
58 mutate_if(is.character, str_replace_all, pattern = "\\.s", replacement = "") %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
59 mutate_at(c("species"), str_replace_all, pattern = "_", replacement = " ") %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
60 mutate(namespace = if_else(namespace == "[MF]", true = "molecular_function", false = if_else(namespace == "[BP]", true = "biological_process", false = "cellular_component"))) %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
61 filter(namespace %in% ontology) %>%
16608175f23b planemo upload
pravs
parents:
diff changeset
62 select(id, name, namespace, 4:ncol(.))
16608175f23b planemo upload
pravs
parents:
diff changeset
63 }
16608175f23b planemo upload
pravs
parents:
diff changeset
64
16608175f23b planemo upload
pravs
parents:
diff changeset
65 # Write file
16608175f23b planemo upload
pravs
parents:
diff changeset
66 write_delim(x = out, path = outfile, delim = "\t", quote_escape = FALSE)