5  Convert & export

5.1 Conversions between data formats in R

If the data has already been imported in R in another format, it can be readily converted into TreeSE, as shown in our next example. Note that similar conversion functions to TreeSE are available for multiple data formats via the mia package (see convertFrom* for phyloseq, Biom, and DADA2).

library(mia)

# phyloseq example data
data(GlobalPatterns, package = "phyloseq")
GlobalPatterns_phyloseq <- GlobalPatterns
GlobalPatterns_phyloseq
##  phyloseq-class experiment-level object
##  otu_table()   OTU Table:         [ 19216 taxa and 26 samples ]
##  sample_data() Sample Data:       [ 26 samples by 7 sample variables ]
##  tax_table()   Taxonomy Table:    [ 19216 taxa by 7 taxonomic ranks ]
##  phy_tree()    Phylogenetic Tree: [ 19216 tips and 19215 internal nodes ]
# convert phyloseq to TSE
GlobalPatterns_TSE <- convertFromPhyloseq(GlobalPatterns_phyloseq)
GlobalPatterns_TSE
##  class: TreeSummarizedExperiment 
##  dim: 19216 26 
##  metadata(0):
##  assays(1): counts
##  rownames(19216): 549322 522457 ... 200359 271582
##  rowData names(7): Kingdom Phylum ... Genus Species
##  colnames(26): CL3 CC1 ... Even2 Even3
##  colData names(7): X.SampleID Primer ... SampleType Description
##  reducedDimNames(0):
##  mainExpName: NULL
##  altExpNames(0):
##  rowLinks: a LinkDataFrame (19216 rows)
##  rowTree: 1 phylo tree(s) (19216 leaves)
##  colLinks: NULL
##  colTree: NULL

We can also convert TreeSE objects into phyloseq with respect to the shared components that are supported by both formats (i.e. taxonomic abundance table, sample metadata, taxonomic table, phylogenetic tree, sequence information). This is useful for instance when additional methods are available for phyloseq.

# convert TSE to phyloseq
GlobalPatterns_phyloseq2 <- convertToPhyloseq(GlobalPatterns_TSE)
GlobalPatterns_phyloseq2
##  phyloseq-class experiment-level object
##  otu_table()   OTU Table:         [ 19216 taxa and 26 samples ]
##  sample_data() Sample Data:       [ 26 samples by 7 sample variables ]
##  tax_table()   Taxonomy Table:    [ 19216 taxa by 7 taxonomic ranks ]
##  phy_tree()    Phylogenetic Tree: [ 19216 tips and 19215 internal nodes ]

Conversion is possible between other data formats. Interested readers can refer to the following functions:

5.2 Exporting data container

5.2.1 Export TreeSummarizedExperiment

Transforming a TreeSE object into a dataframe is straightforward with the mia package. The meltSE function is particularly handy for this purpose. It allows you to melt various parts of a TreeSE object into a dataframe based on the parameters you specify.

Exporting a TreeSE data container can be done using feather package. TreeSE object has to be converted into a dataframe (data.frame and not DataFrame). The output file is a .feather file, which can be imported in other languages such as Julia or Python. For information, have a look at the FeatherFile Julia package and feather-format Python library.

data(GlobalPatterns, package = "mia")
tse <- GlobalPatterns

molten_tse <- meltSE(
    tse,
    add_row_data = TRUE,
    add_col_data = TRUE,
    assay.type = "counts")

# Export as a feather file
library(feather)
path <- "path/to/tse.feather"
write_feather(molten_tse, path)

Another way could be using a CSV file. This works the same as for a feather file, make sure you have converted your TreeSE data container as a dataframe Here note that you can decide whether you want to write the row names or not.

write.csv(molten_tse, "path/to/tse.csv", row.names=FALSE)

5.2.2 Export MultiAssayExperiment

Exporting a MultiAssayExperiment data container can also be done using feather package.

# Convert into a data.frame
molten_mae <- longFormat(mae)
molten_mae <- data.frame(molten_mae)
path <- "path/to/mae.feather"
write_feather(molten_mae,path)

And as a CSV file.

write.csv(molten_mae, "path/to/mae.csv", row.names=FALSE)
Back to top