mikemc / speedyseq

Speedy versions of phyloseq functions
https://mikemc.github.io/speedyseq/
Other
45 stars 6 forks source link

`merge_taxa_vec()` fails when there is only one tax rank #50

Closed mikemc closed 4 years ago

mikemc commented 4 years ago
library(speedyseq)
#> Loading required package: phyloseq
#> 
#> Attaching package: 'speedyseq'
#> The following objects are masked from 'package:phyloseq':
#> 
#>     filter_taxa, plot_bar, plot_heatmap, plot_tree, psmelt, tax_glom,
#>     tip_glom, transform_sample_counts
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

data(enterotype)

enterotype
#> phyloseq-class experiment-level object
#> otu_table()   OTU Table:         [ 553 taxa and 280 samples ]
#> sample_data() Sample Data:       [ 280 samples by 9 sample variables ]
#> tax_table()   Taxonomy Table:    [ 553 taxa by 1 taxonomic ranks ]
#> taxa are rows
tax_table(enterotype) %>% head
#> Taxonomy Table:     [6 taxa by 1 taxonomic ranks]:
#>                             Genus
#>                       <character>
#> -1                             NA
#> Bacteria                       NA
#> Prosthecochloris Prosthecochloris
#> Chloroflexus         Chloroflexus
#> Dehalococcoides   Dehalococcoides
#> Thermus                   Thermus

grp <- sample(1:5, ntaxa(enterotype), replace = TRUE)
merge_taxa_vec(enterotype, grp)
#> Error in dimnames(x) <- dn: length of 'dimnames' [1] not equal to array extent

tax_table(enterotype) <- tax_table(enterotype) %>%
  as("matrix") %>%
  as_tibble(rownames = "taxon") %>%
  mutate(dummy_rank = "Dummy") %>%
  tax_table
#> Assuming first column, `taxon`, contains the taxa names
enterotype
#> phyloseq-class experiment-level object
#> otu_table()   OTU Table:         [ 553 taxa and 280 samples ]
#> sample_data() Sample Data:       [ 280 samples by 9 sample variables ]
#> tax_table()   Taxonomy Table:    [ 553 taxa by 2 taxonomic ranks ]
#> taxa are rows

merge_taxa_vec(enterotype, grp)
#> phyloseq-class experiment-level object
#> otu_table()   OTU Table:         [ 5 taxa and 280 samples ]
#> sample_data() Sample Data:       [ 280 samples by 9 sample variables ]
#> tax_table()   Taxonomy Table:    [ 5 taxa by 2 taxonomic ranks ]
#> taxa are rows

Created on 2020-09-09 by the reprex package (v0.3.0)

mikemc commented 4 years ago

The issue was caused by the call to apply() in the taxonomyTable method at https://github.com/mikemc/speedyseq/blob/46b51deb113a080efcdd32f049107515e39c4664/R/merge_taxa_vec.R#L187 which, when there is only one tax rank, returns a vector instead of a data.frame.