Skip to content

Commit

Permalink
Merge pull request #22 from SchlossLab/iss-14
Browse files Browse the repository at this point in the history
Parse taxonomy file to dataframe
  • Loading branch information
kelly-sovacool authored Oct 7, 2020
2 parents a87fbf6 + deb66b5 commit ce2a94f
Show file tree
Hide file tree
Showing 23 changed files with 367 additions and 37 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
export("%>%")
export(.data)
export(read_dist)
export(read_tax)
importFrom(dplyr,"%>%")
importFrom(rlang,.data)
64 changes: 64 additions & 0 deletions R/read_tax.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#' Convert taxonomy strings into dataframe of labels based on taxnomic classification
#'
#' @param taxonomy_filename filename of taxonomy file
#'
#' @return dataframe of taxonomic labels
#' @export
#' @author Nick Lesniak, \email{nlesniak@@umich.edu}
#'
#' @examples
#' taxonomy_filepath <- system.file("extdata",
#' "test.taxonomy",
#' package = "mothuR"
#' )
#' taxonomy_tbl <- read_tax(taxonomy_filepath)
#' head(taxonomy_tbl)
read_tax <- function(taxonomy_filename) {
levels <- c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus")
taxonomy_df <- utils::read.table(taxonomy_filename,
sep = "\t",
header = T,
stringsAsFactors = F
) %>%
dplyr::mutate(Taxonomy = gsub("_", " ", .data[["Taxonomy"]])) %>%
tidyr::separate(.data[["Taxonomy"]], levels, sep = "\\(\\d{2,3}\\);", extra = "drop") %>%
dplyr::select(-.data[["Size"]])
# in older version of mothur unclassified are listed as unclassified
# without information from higher level classification
# for those cases, append with lowest identified classification
if (any(taxonomy_df$Genus == "unclassified")) {
taxonomy_df <- taxonomy_df %>%
tidyr::pivot_longer(
cols = -.data[["OTU"]],
names_to = "Level",
values_to = "Classification"
) %>%
# order classification level
dplyr::mutate(Level = factor(.data[["Level"]], levels)) %>%
dplyr::left_join(dplyr::group_by(., .data[["OTU"]]) %>%
dplyr::filter(.data[["Classification"]] != "unclassified") %>%
# select lowest level classification
dplyr::filter(.data[["Level"]] == levels[max(as.numeric(.data[["Level"]]))]) %>%
dplyr::select(.data[["OTU"]], Lowest_classified = .data[["Classification"]]),
by = "OTU"
) %>%
dplyr::mutate(Classification = ifelse(.data[["Classification"]] == "unclassified",
# append unclassified with lowest classification
paste(.data[["Lowest_classified"]], .data[["Classification"]], sep = " "),
.data[["Classification"]]
)) %>%
dplyr::select(-.data[["Lowest_classified"]]) %>%
tidyr::pivot_wider(
names_from = "Level",
values_from = "Classification"
)
}
# create label options for OTU and lowest taxonomic classification with the OTU
taxonomy_df <- taxonomy_df %>%
dplyr::mutate(
tax_otu_label = paste0(.data[["Genus"]], " (", gsub("tu0*", "TU ", .data[["OTU"]]), ")"),
tax_otu_label = gsub(" unclassified", "", .data[["tax_otu_label"]]),
otu_label = paste0(gsub("tu0*", "TU ", .data[["OTU"]]))
)
return(taxonomy_df)
}
8 changes: 8 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#' dplyr pipe
#' @importFrom dplyr %>%
#' @export
dplyr::`%>%`

## make R CMD CHECK shut up about the dot `.``
## See: \url{https://github.com/tidyverse/magrittr/issues/29}
utils::globalVariables(c("."))
2 changes: 1 addition & 1 deletion docs/404.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/CODE_OF_CONDUCT.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/CONTRIBUTING.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/LICENSE.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions docs/articles/introduction.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/authors.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/pkgdown.css
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,14 @@ nav[data-toggle='toc'] .nav .nav > .active:focus > a {

.ref-index th {font-weight: normal;}

.ref-index td {vertical-align: top;}
.ref-index td {vertical-align: top; min-width: 100px}
.ref-index .icon {width: 40px;}
.ref-index .alias {width: 40%;}
.ref-index-icons .alias {width: calc(40% - 40px);}
.ref-index .title {width: 60%;}

.ref-arguments th {text-align: right; padding-right: 10px;}
.ref-arguments th, .ref-arguments td {vertical-align: top;}
.ref-arguments th, .ref-arguments td {vertical-align: top; min-width: 100px}
.ref-arguments .name {width: 20%;}
.ref-arguments .desc {width: 80%;}

Expand Down
4 changes: 2 additions & 2 deletions docs/pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pandoc: 2.7.3
pkgdown: 1.5.1
pkgdown: 1.6.1
pkgdown_sha: ~
articles:
introduction: introduction.html
last_built: 2020-09-03T20:18Z
last_built: 2020-10-06T16:27Z

Binary file added docs/reference/Rplot001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion docs/reference/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/mothuR.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions docs/reference/read_dist.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ce2a94f

Please sign in to comment.