/
krona.R
139 lines (129 loc) · 4.75 KB
/
krona.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
130
131
132
133
134
135
136
137
138
139
################################################################################
#' Make Krona files using [KronaTools](https://github.com/marbl/Krona/wiki).
#' @description
#' `r lifecycle::badge("maturing")`
#'
#' Need the installation of kronatools on the computer ([installation instruction](https://github.com/marbl/Krona/wiki/Installing)).
#'
#' @inheritParams clean_pq
#' @param file (required) the location of the html file to save
#' @param nb_seq (logical) If true, Krona set the distribution
#' of sequences in the taxonomy. If False, Krona set the distribution of ASVs
#' in the taxonomy.
#' @param ranks Number of the taxonomic ranks to plot
#' (num of the column in `tax_table` slot of your `physeq` object).
#' Default setting plot all the ranks (argument 'All').
#' @param add_unassigned_rank (int) Add unassigned for rank
#' inferior to 'add_unassigned_rank' when necessary.
#' @param name A name for intermediary files, Useful to name
#' your krona result files before merging using [merge_krona()]
#'
#' @examplesIf tolower(Sys.info()[["sysname"]]) != "windows" && MiscMetabar::is_krona_installed()
#' data("GlobalPatterns", package = "phyloseq")
#' GA <- subset_taxa(GlobalPatterns, Phylum == "Acidobacteria")
#' \dontrun{
#' krona(GA, "Number.of.sequences.html")
#' krona(GA, "Number.of.ASVs.html", nb_seq = FALSE)
#' merge_krona(c("Number.of.sequences.html", "Number.of.ASVs.html"))
#' }
#' @return A html file
#' @export
#' @seealso \code{\link{merge_krona}}
#' @author Adrien Taudière
#' @details
#' This function is mainly a wrapper of the work of others.
#' Please cite [Krona](https://github.com/marbl/Krona) if
#' you use this function.
krona <-
function(physeq,
file = "krona.html",
nb_seq = TRUE,
ranks = "All",
add_unassigned_rank = 0,
name = NULL) {
if (ranks[1] == "All") {
ranks <- seq_along(physeq@tax_table[1, ])
}
df <- data.frame(unclass(physeq@tax_table[, ranks]))
df$ASVs <- rownames(physeq@tax_table)
if (is.null(name)) {
if (nb_seq) {
name <- "Number.of.sequences_temp"
} else {
name <- "Number.of.ASVs_temp"
}
}
if (nb_seq) {
df$nb_seq <- taxa_sums(physeq)
} else {
df$nb_otu <- rep(1, length(taxa_sums(physeq)))
}
df <- df[c(ncol(df), 2:ncol(df) - 1)]
res <-
lapply(split(df, seq_along(physeq@tax_table[, 1])), function(x) {
as.vector(as.matrix(x))[!is.na(unlist(x))]
})
res <-
lapply(res, function(x) {
if (length(x) < add_unassigned_rank) {
x <-
c(x, "unassigned")[c(seq_len(x) - 1, length(x) + 1, length(x))]
} else {
x
}
})
interm_txt <- paste(tempdir(), "/", name, ".html", sep = "")
lapply(res,
cat,
"\n",
file = interm_txt,
append = TRUE,
sep = "\t"
)
cmd <- paste("ktImportText ", interm_txt, " -o ", file, sep = "")
system(command = cmd)
system(command = paste("rm", interm_txt))
}
###############################################################################
#' Merge Krona files using [KronaTools](https://github.com/marbl/Krona/wiki).
#' @description
#' `r lifecycle::badge("maturing")`
#'
#' Need the installation of kronatools on the computer
#' ([installation instruction](https://github.com/marbl/Krona/wiki/Installing)).
#' Function merge_krona allows merging multiple html files in one interactive
#' krona file
#'
#' Note that you need to use the name args in `krona()` function before `merge_krona()`
#' in order to give good name to each krona pie in the output.
#' @param files (required) path to html files to merged
#' @param output path to the output file
#'
#' @examplesIf tolower(Sys.info()[["sysname"]]) != "windows" && MiscMetabar::is_krona_installed()
#' \dontrun{
#' data("GlobalPatterns", package = "phyloseq")
#' GA <- subset_taxa(GlobalPatterns, Phylum == "Acidobacteria")
#' krona(GA, "Number.of.sequences.html", name = "Nb_seq_GP_acidobacteria")
#' krona(GA, "Number.of.ASVs.html", nb_seq = FALSE, name = "Nb_asv_GP_acidobacteria")
#' merge_krona(c("Number.of.sequences.html", "Number.of.ASVs.html"), "mergeKrona.html")
#' unlink(c("Number.of.sequences.html", "Number.of.ASVs.html", "mergeKrona.html"))
#' }
#' @return A html file
#' @seealso \code{\link{krona}}
#' @export
#' @author Adrien Taudière
#' @details
#' This function is mainly a wrapper of the work of others.
#' Please cite [Krona](https://github.com/marbl/Krona) if
#' you use this function.
merge_krona <- function(files = NULL, output = "mergeKrona.html") {
cmd <-
paste("ktImportKrona ",
paste(files, collapse = " "),
" -o ",
output,
sep = ""
)
system(command = cmd)
}
################################################################################