make_mdt: Make melted dataframe of relative abundance of each taxa

Description Usage Arguments Examples

Description

Using as input the OTU table biom produced by QIIME analysis, make melted dataframe of relative abundance of each taxa.

Usage

1
make_mdt(biom_file)

Arguments

biom_file

Full path name of OTU table biom file

Examples

 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
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function(biom_file){
  orig_phylo <- import_biom(biom_file)
  otutab = as(otu_table(orig_phylo), "matrix")
    otudt = data.table(otutab, keep.rownames = TRUE)
    setnames(otudt, "rn", "TaxaID")
    otudt[, TaxaIDchar := as.character(TaxaID)]
    otudt[, TaxaID := NULL]
    setnames(otudt, "TaxaIDchar", "TaxaID")
    # Melt count table
    mdt = melt.data.table(otudt,
                          id.vars = "TaxaID",
                          variable.name = "SampleID",
                          value.name = "count")

    mdt <- mdt[count > 0]
    # Omit NAs
    mdt <- mdt[!is.na(count)]
    mdt[, RelativeAbundance := count / sum(count), by = SampleID] #find relative abundance by sample IDs
    taxdt = data.table(as(tax_table(orig_phylo, errorIfNULL = TRUE), "matrix"), keep.rownames = TRUE)
    setnames(taxdt, "rn", "TaxaID")
    # Enforce character TaxaID key
    taxdt[, TaxaIDchar := as.character(TaxaID)]
    taxdt[, TaxaID := NULL]
    setnames(taxdt, "TaxaIDchar", "TaxaID")
    # Join with tax table
    setkey(taxdt, "TaxaID")
    setkey(mdt, "TaxaID")
    mdt <- taxdt[mdt] #dataframe, 11 columns = last 2 columns = count, RelativeAbundance, columns 1-7 = taxonomy
    return(mdt)
}

ConesaLab/MDM documentation built on Aug. 1, 2020, 11:47 a.m.