Plotting

Plotting is important and its features are currently under development and are experimental. Below is a demonstration of some current features.

Plotting currently depends on the excellent pygal package which is a python plotting that produces SVG plots. I have noticed that large SVGs tend to be heavy and choke the browser (I use Firefox, and perhaps moreso when in the context of a Jupyter notebook; I think Chrome could fare better from a few experiments), so proceed with your plotting experimentation with this warning in mind.

The plotting API is experimental.


In [1]:
%load_ext autoreload
%autoreload 2

#Load our data
from omicexperiment.experiment.microbiome import MicrobiomeExperiment

mapping = "example_map.tsv"
biom = "example_fungal.biom"
tax = "blast_tax_assignments.txt"

exp = MicrobiomeExperiment(biom, mapping,tax)

exp.data_df


Out[1]:
1234 9876 sample0 sample1 sample2
2f328e48f4252bbade0dd7f66b0d5bf1b09617dd 0 225872 0 2 0
ae0ddda08027454fdb5db77c96b94691b8274cdd 2 1 0 91911 100428
8f52abc02aed2ce6c63be04570a7e609f9cdac5f 133138 0 0 21 0
3cb3c2347cdbe128b645e432f4dcbca702e0e8e3 0 0 0 0 0
8e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe0 0 0 86870 0 0

In [2]:
#you can load your plot in the notebook directly,
#but note that large graphics for large datasets tend to consume quite
#CPU power

from lxml import etree
plot = exp.plot()   #this returns an lxml ElementTree

#you can also pass an outputfile
#plot = exp.plot('~/test_plot.html') #this saves the SVG figure to an html file

plot_string = etree.tostring(plot) #convert to string
from IPython.display import SVG
SVG(plot_string) #Hover over the bars and look at the tooltip


Out[2]:
Microbiome0.00.020000.020000.040000.040000.060000.060000.080000.080000.0100000.0100000.0120000.0120000.0140000.0140000.0160000.0160000.0180000.0180000.0200000.0200000.0220000.0220000.012349876sample0sample1sample22f328e48f4252bbade0dd7f66b0d5bf1b09617dd2f328e48f4252bbade0dd7f66b0d5bf1b09617dd Sample: 1234 0.00.074.00769230769231387.01153846212342f328e48f4252bbade0dd7f66b0d5bf1b09617dd2f328e48f4252bbade0dd7f66b0d5bf1b09617dd Sample: 9876 225872.0225872.0197.35384615384618197.30083990498762f328e48f4252bbade0dd7f66b0d5bf1b09617dd2f328e48f4252bbade0dd7f66b0d5bf1b09617dd Sample: sample0 0.00.0320.70000000000005387.011538462sample02f328e48f4252bbade0dd7f66b0d5bf1b09617dd2f328e48f4252bbade0dd7f66b0d5bf1b09617dd Sample: sample1 2.02.0444.04615384615386387.009858654sample12f328e48f4252bbade0dd7f66b0d5bf1b09617dd2f328e48f4252bbade0dd7f66b0d5bf1b09617dd Sample: sample2 0.00.0567.3923076923076387.011538462sample2ae0ddda08027454fdb5db77c96b94691b8274cddae0ddda08027454fdb5db77c96b94691b8274cdd Sample: 1234 2.02.074.00769230769231387.0098586541234ae0ddda08027454fdb5db77c96b94691b8274cddae0ddda08027454fdb5db77c96b94691b8274cdd Sample: 9876 1.01.0197.353846153846187.589301442039876ae0ddda08027454fdb5db77c96b94691b8274cddae0ddda08027454fdb5db77c96b94691b8274cdd Sample: sample0 0.00.0320.70000000000005387.011538462sample0ae0ddda08027454fdb5db77c96b94691b8274cddae0ddda08027454fdb5db77c96b94691b8274cdd Sample: sample1 91911.091911.0444.04615384615386309.811802071sample1ae0ddda08027454fdb5db77c96b94691b8274cddae0ddda08027454fdb5db77c96b94691b8274cdd Sample: sample2 100428.0100428.0567.3923076923076302.661703003sample28f52abc02aed2ce6c63be04570a7e609f9cdac5f8f52abc02aed2ce6c63be04570a7e609f9cdac5f Sample: 1234 133138.0133138.074.00769230769231275.18509770112348f52abc02aed2ce6c63be04570a7e609f9cdac5f8f52abc02aed2ce6c63be04570a7e609f9cdac5f Sample: 9876 0.00.0197.353846153846187.5884615384698768f52abc02aed2ce6c63be04570a7e609f9cdac5f8f52abc02aed2ce6c63be04570a7e609f9cdac5f Sample: sample0 0.00.0320.70000000000005387.011538462sample08f52abc02aed2ce6c63be04570a7e609f9cdac5f8f52abc02aed2ce6c63be04570a7e609f9cdac5f Sample: sample1 21.021.0444.04615384615386232.59778732sample18f52abc02aed2ce6c63be04570a7e609f9cdac5f8f52abc02aed2ce6c63be04570a7e609f9cdac5f Sample: sample2 0.00.0567.3923076923076218.311867544sample23cb3c2347cdbe128b645e432f4dcbca702e0e8e33cb3c2347cdbe128b645e432f4dcbca702e0e8e3 Sample: 1234 0.00.074.00769230769231163.36201655512343cb3c2347cdbe128b645e432f4dcbca702e0e8e33cb3c2347cdbe128b645e432f4dcbca702e0e8e3 Sample: 9876 0.00.0197.353846153846187.5884615384698763cb3c2347cdbe128b645e432f4dcbca702e0e8e33cb3c2347cdbe128b645e432f4dcbca702e0e8e3 Sample: sample0 0.00.0320.70000000000005387.011538462sample03cb3c2347cdbe128b645e432f4dcbca702e0e8e33cb3c2347cdbe128b645e432f4dcbca702e0e8e3 Sample: sample1 0.00.0444.04615384615386232.580149345sample13cb3c2347cdbe128b645e432f4dcbca702e0e8e33cb3c2347cdbe128b645e432f4dcbca702e0e8e3 Sample: sample2 0.00.0567.3923076923076218.311867544sample28e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe08e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe0 Sample: 1234 0.00.074.00769230769231163.36201655512348e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe08e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe0 Sample: 9876 0.00.0197.353846153846187.5884615384698768e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe08e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe0 Sample: sample0 86870.086870.0320.70000000000005314.049115568sample08e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe08e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe0 Sample: sample1 0.00.0444.04615384615386232.580149345sample18e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe08e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe0 Sample: sample2 0.00.0567.3923076923076218.311867544sample2Microbiome2f328e48f4252b…2f328e48f4252bbade0dd7f66b0d5bf1b09617ddae0ddda0802745…ae0ddda08027454fdb5db77c96b94691b8274cdd8f52abc02aed2c…8f52abc02aed2ce6c63be04570a7e609f9cdac5f3cb3c2347cdbe1…3cb3c2347cdbe128b645e432f4dcbca702e0e8e38e9a3b9a9d91e8…8e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe0

Plot groups functionality

The plot_groups method annotates (below the bars) various groupings according to sample metadata (mapping_df).


In [3]:
plot_grp = exp.plot_groups("group")   #this returns an lxml ElementTree
plot_string = etree.tostring(plot_grp) #convert to string
from IPython.display import SVG
SVG(plot_string) #Note the groupings below the bars


Out[3]:
Microbiome0.00.020000.020000.040000.040000.060000.060000.080000.080000.0100000.0100000.0120000.0120000.0140000.0140000.0160000.0160000.0180000.0180000.0200000.0200000.0220000.0220000.0sample0sample2sample198761234CRSsNPCRSwNPcontrol2f328e48f4252bbade0dd7f66b0d5bf1b09617dd2f328e48f4252bbade0dd7f66b0d5bf1b09617dd Sample: sample0 0.00.074.00769230769231387.011538462sample02f328e48f4252bbade0dd7f66b0d5bf1b09617dd2f328e48f4252bbade0dd7f66b0d5bf1b09617dd Sample: sample2 0.00.0197.35384615384618387.011538462sample22f328e48f4252bbade0dd7f66b0d5bf1b09617dd2f328e48f4252bbade0dd7f66b0d5bf1b09617dd Sample: sample1 2.02.0320.70000000000005387.009858654sample12f328e48f4252bbade0dd7f66b0d5bf1b09617dd2f328e48f4252bbade0dd7f66b0d5bf1b09617dd Sample: 9876 225872.0225872.0444.04615384615386197.30083990498762f328e48f4252bbade0dd7f66b0d5bf1b09617dd2f328e48f4252bbade0dd7f66b0d5bf1b09617dd Sample: 1234 0.00.0567.3923076923076387.0115384621234ae0ddda08027454fdb5db77c96b94691b8274cddae0ddda08027454fdb5db77c96b94691b8274cdd Sample: sample0 0.00.074.00769230769231387.011538462sample0ae0ddda08027454fdb5db77c96b94691b8274cddae0ddda08027454fdb5db77c96b94691b8274cdd Sample: sample2 100428.0100428.0197.35384615384618302.661703003sample2ae0ddda08027454fdb5db77c96b94691b8274cddae0ddda08027454fdb5db77c96b94691b8274cdd Sample: sample1 91911.091911.0320.70000000000005309.811802071sample1ae0ddda08027454fdb5db77c96b94691b8274cddae0ddda08027454fdb5db77c96b94691b8274cdd Sample: 9876 1.01.0444.046153846153867.589301442039876ae0ddda08027454fdb5db77c96b94691b8274cddae0ddda08027454fdb5db77c96b94691b8274cdd Sample: 1234 2.02.0567.3923076923076387.00985865412348f52abc02aed2ce6c63be04570a7e609f9cdac5f8f52abc02aed2ce6c63be04570a7e609f9cdac5f Sample: sample0 0.00.074.00769230769231387.011538462sample08f52abc02aed2ce6c63be04570a7e609f9cdac5f8f52abc02aed2ce6c63be04570a7e609f9cdac5f Sample: sample2 0.00.0197.35384615384618218.311867544sample28f52abc02aed2ce6c63be04570a7e609f9cdac5f8f52abc02aed2ce6c63be04570a7e609f9cdac5f Sample: sample1 21.021.0320.70000000000005232.59778732sample18f52abc02aed2ce6c63be04570a7e609f9cdac5f8f52abc02aed2ce6c63be04570a7e609f9cdac5f Sample: 9876 0.00.0444.046153846153867.5884615384698768f52abc02aed2ce6c63be04570a7e609f9cdac5f8f52abc02aed2ce6c63be04570a7e609f9cdac5f Sample: 1234 133138.0133138.0567.3923076923076275.18509770112343cb3c2347cdbe128b645e432f4dcbca702e0e8e33cb3c2347cdbe128b645e432f4dcbca702e0e8e3 Sample: sample0 0.00.074.00769230769231387.011538462sample03cb3c2347cdbe128b645e432f4dcbca702e0e8e33cb3c2347cdbe128b645e432f4dcbca702e0e8e3 Sample: sample2 0.00.0197.35384615384618218.311867544sample23cb3c2347cdbe128b645e432f4dcbca702e0e8e33cb3c2347cdbe128b645e432f4dcbca702e0e8e3 Sample: sample1 0.00.0320.70000000000005232.580149345sample13cb3c2347cdbe128b645e432f4dcbca702e0e8e33cb3c2347cdbe128b645e432f4dcbca702e0e8e3 Sample: 9876 0.00.0444.046153846153867.5884615384698763cb3c2347cdbe128b645e432f4dcbca702e0e8e33cb3c2347cdbe128b645e432f4dcbca702e0e8e3 Sample: 1234 0.00.0567.3923076923076163.36201655512348e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe08e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe0 Sample: sample0 86870.086870.074.00769230769231314.049115568sample08e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe08e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe0 Sample: sample2 0.00.0197.35384615384618218.311867544sample28e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe08e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe0 Sample: sample1 0.00.0320.70000000000005232.580149345sample18e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe08e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe0 Sample: 9876 0.00.0444.046153846153867.5884615384698768e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe08e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe0 Sample: 1234 0.00.0567.3923076923076163.3620165551234Microbiome2f328e48f4252b…2f328e48f4252bbade0dd7f66b0d5bf1b09617ddae0ddda0802745…ae0ddda08027454fdb5db77c96b94691b8274cdd8f52abc02aed2c…8f52abc02aed2ce6c63be04570a7e609f9cdac5f3cb3c2347cdbe1…3cb3c2347cdbe128b645e432f4dcbca702e0e8e38e9a3b9a9d91e8…8e9a3b9a9d91e86f21da1bd57b8ae4486c78bbe0

In [4]:
#Try converting the otus to 'genus' level assignments before plotting
exp_genus = exp.apply(exp.Taxonomy.groupby('genus'))
plot_string = etree.tostring(exp_genus.plot()) #convert to string
from IPython.display import SVG
SVG(plot_string)


Out[4]:
Microbiome0.00.020000.020000.040000.040000.060000.060000.080000.080000.0100000.0100000.0120000.0120000.0140000.0140000.0160000.0160000.0180000.0180000.0200000.0200000.0220000.0220000.012349876sample0sample1sample2g__Aspergillusg__Aspergillus Sample: 1234 2.02.074.00769230769231387.0098586541234g__Aspergillusg__Aspergillus Sample: 9876 225873.0225873.0197.35384615384618197.39876g__Aspergillusg__Aspergillus Sample: sample0 0.00.0320.70000000000005387.011538462sample0g__Aspergillusg__Aspergillus Sample: sample1 91913.091913.0444.04615384615386309.813481878sample1g__Aspergillusg__Aspergillus Sample: sample2 100428.0100428.0567.3923076923076302.661703003sample2g__Lewiag__Lewia Sample: 1234 0.00.074.00769230769231387.0081788471234g__Lewiag__Lewia Sample: 9876 0.00.0197.353846153846187.588461538469876g__Lewiag__Lewia Sample: sample0 86870.086870.0320.70000000000005314.049115568sample0g__Lewiag__Lewia Sample: sample1 0.00.0444.04615384615386232.615425295sample1g__Lewiag__Lewia Sample: sample2 0.00.0567.3923076923076218.311867544sample2g__unidentified (f__Pleosporaceae)g__unidentified (f__Pleosporaceae) Sample: 1234 133138.0133138.074.00769230769231275.1850977011234g__unidentified (f__Pleosporaceae)g__unidentified (f__Pleosporaceae) Sample: 9876 0.00.0197.353846153846187.588461538469876g__unidentified (f__Pleosporaceae)g__unidentified (f__Pleosporaceae) Sample: sample0 0.00.0320.70000000000005241.086692675sample0g__unidentified (f__Pleosporaceae)g__unidentified (f__Pleosporaceae) Sample: sample1 21.021.0444.04615384615386232.59778732sample1g__unidentified (f__Pleosporaceae)g__unidentified (f__Pleosporaceae) Sample: sample2 0.00.0567.3923076923076218.311867544sample2Microbiomeg__Aspergillusg__Lewiag__unidentifie…g__unidentified (f__Pleosporaceae)

In [5]:
#Grouping by some sample metadata to get average relative abundances for sample groups
exp_disease_group = exp_genus.apply(exp.Sample.groupby('group'))
plot_string = etree.tostring(exp_disease_group.plot()) #convert to string
from IPython.display import SVG
SVG(plot_string)


/home/ahmed/dev/biomenv3/lib/python3.5/site-packages/pandas/core/index.py:4281: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  return np.sum(name == np.asarray(self.names)) > 1
Out[5]:
Microbiome0.00.010000.010000.020000.020000.030000.030000.040000.040000.050000.050000.060000.060000.070000.070000.080000.080000.090000.090000.0100000.0100000.0110000.0110000.0120000.0120000.0130000.0130000.0140000.0140000.0150000.0150000.0CRSsNPCRSwNPcontrolg__Aspergillusg__Aspergillus Sample: CRSsNP 50214.050214.0115.12307692307691327.062102532CRSsNPg__Aspergillusg__Aspergillus Sample: CRSwNP 158893.0158893.0320.69999999999993197.312535729CRSwNPg__Aspergillusg__Aspergillus Sample: control 2.02.0526.2769230769231387.009150704controlg__Lewiag__Lewia Sample: CRSsNP 43435.043435.0115.12307692307691215.256535849CRSsNPg__Lewiag__Lewia Sample: CRSwNP 0.00.0320.699999999999937.61353299572CRSwNPg__Lewiag__Lewia Sample: control 0.00.0526.2769230769231387.006762946controlg__unidentified (f__Pleosporaceae)g__unidentified (f__Pleosporaceae) Sample: CRSsNP 0.00.0115.12307692307691163.400405096CRSsNPg__unidentified (f__Pleosporaceae)g__unidentified (f__Pleosporaceae) Sample: CRSwNP 10.510.5320.699999999999937.60099726709CRSwNPg__unidentified (f__Pleosporaceae)g__unidentified (f__Pleosporaceae) Sample: control 133138.0133138.0526.2769230769231228.056111716controlMicrobiomeg__Aspergillusg__Lewiag__unidentifie…g__unidentified (f__Pleosporaceae)

Missing plotting functionality

This is a list to track missing plotting functionality.

  • Customizable plot titles