AncomBC differentials results to dataframe

Hi. I've been using the new ancombc plugin within qiiime2 python notebooks, and I'd like to customize the plotting of results. I know I can tabulate the differentials artifact and then extract/export to file, but is there a simpler way to read/view the differentials in a dataframe?

Here is an example I use to generate differentials results

from qiime2.plugins.composition.methods import ancombc

ancombc_formula = "Treatment"
reference_levels = ["Treatment::Positive_Control"]

ancombc_differentials = ancombc(table=feature_table, metadata=metadata_table, formula=ancombc_formula,
                               p_adj_method='holm', prv_cut=0.1, lib_cut=0,
                               reference_levels=reference_levels, # comment out this argument if no reference is used
                               neg_lb=False, tol=1e-05, max_iter=100,
                               conserve=False, 
                               alpha=0.05).differentials

With metadata or some feature table artifacts it is possible to view the data in a dataframe, e.g.,

feature_table.view(pd.DataFrame)
metadata.view(qiime2.Metadata).to_dataframe()

Is there an option to visualize the differentials results as well. Or the tabulated artifact of differentials?

Thank you very much for the feedback.

1 Like

Hi @Alxdu,

There's a slightly roundabout way to make this happen, the difficulty is there's not just one table, but multiple, so we're going to loop over the directory format itself:

# import the class from the python package itself
from q2_composition import DataLoafPackageDirFmt

# view it as that directory format
dirfmt = ancombc_differentials.view(DataLoafPackageDirFmt)

# this directory format has a model attribute called `data_slices`
# each of which represents a CSV in the directory

slices = {}
for relpath, view in dirfmt.data_slices.iter_views(pd.DataFrame)
    slices[relpath] = view

# Do something with slices now :)
3 Likes

This is excellent and works great. Thank you very much.

The four slices are, very conveniently, pandas dataframes.

Tables can be viewed with either of the following:

list(slices.values())[0]

# OR  

slices[list(slices.keys())[0]]

# OR
  
from pathlib import Path
slices[Path('lfc_slice.csv')]

Thank you. Much appreciated

1 Like

Ah, I forgot that relpath was a pathlib object, you can use slices[str(relpath)] = view instead to make the key sane :slight_smile:

2 Likes

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.