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: NULLWe 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
There are two main ways to export data. The whole data container can be saved to a file, which preserves all of its components and allows loading it back later as the same object. Alternatively, the data can be exported as tables, which can be opened with other tools, such as spreadsheet programs or other programming languages, but which do not preserve the structure of the object.
5.2.1 Save the data container
5.2.1.1 RDS files
The simplest way to save any R object, including TreeSE and MultiAssayExperiment, is to write it to an RDS file with saveRDS(). The file can be loaded back with readRDS().
RDS files are convenient for saving and reloading data in R, for example between analysis sessions. However, they can only be read in R, and files saved with old package versions are not always fully compatible with newer versions. For long-term storage or for sharing data across tools, the alabaster format described below is more robust.
5.2.1.2 alabaster
The alabaster framework provides an alternative to RDS files. It saves the data container as a directory of files in standard formats, which are not tied to R. This makes the data easier to store long term and to share with other programming languages. The saveObject() function writes the data to a directory, and readObject() loads it back.
# Requires the alabaster.treese package to be installed
library(alabaster)
# Save TreeSE into a directory
saveObject(tse, "path/to/tse")
# Load the TreeSE back
tse <- readObject("path/to/tse")The same functions can be used to save and load MultiAssayExperiment objects.
5.2.2 Export as table
5.2.2.1 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 the feather package. The 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 more information, have a look at the FeatherFile Julia package and feather-format Python library.
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. 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)To share the data with collaborators who work with spreadsheets, the se2xls() function from the SEtools package writes a TreeSE to an Excel file without melting it first. The sample data (colData), feature data (rowData) and each assay are saved on separate sheets. Note that the phylogenetic tree is not included.
5.2.2.2 MultiAssayExperiment
Exporting a MultiAssayExperiment data container can also be done using feather package.
# Convert into a data.frame
molten_mae <- longForm(mae)
molten_mae <- data.frame(molten_mae)
write_feather(molten_mae, "path/to/mae.feather")And as a CSV file.
write.csv(molten_mae, "path/to/mae.csv", row.names = FALSE)