sbslee / dokdo

A Python package for microbiome sequencing analysis with QIIME 2
https://dokdo.readthedocs.io
MIT License
42 stars 12 forks source link

Plot options for `beta_2d/3d_plot` and `taxa_abundance_box_plot` methods #39

Closed khemlalnirmalkar closed 2 years ago

khemlalnirmalkar commented 2 years ago

Hi @sbslee

Recently, I noticed two minor things on Dokdo and thought you could help

  1. beta_3D_plot doesn't have an option for colors, if possible please can you add
  2. initially, I used to use the add_datapoints option for box plots, it's not working anymore

Thanks

sbslee commented 2 years ago

@khemlalnirmalkar,

Thanks for the issue.

As for beta_3d_plot, the hue option already gives you the control to color data points according to the specified group (e.g. hue='body-site'):

import dokdo
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set()
qza_file = '/Users/sbslee/Desktop/dokdo/data/moving-pictures-tutorial/unweighted_unifrac_pcoa_results.qza'
metadata_file = '/Users/sbslee/Desktop/dokdo/data/moving-pictures-tutorial/sample-metadata.tsv'
dokdo.beta_3d_plot(qza_file,
                   metadata=metadata_file,
                   hue='body-site',
                   figsize=(8, 8))
plt.tight_layout()

beta-3d-plot

Is this what you want?

And for add_datapoints of taxa_abundance_box_plot, this option was deprecated in 1.11.0 (see CHANGELOG) because it was making the code unnecessarily complicated. If you think having this option is important, I will consider bringing it back. Could you please explain to me what's your use case?

khemlalnirmalkar commented 2 years ago

@sbslee For the beta_3D plot, i can't control the colors, e.g. if i want "gut" in red and "tongue" in blue, i can't do that. If I compare with bar plot where it allows me to use RGB color codes to choose the bacteria. it would be great to control the colors for other plots too. For box_plot it was a useful option adding data points. It helps a lot to see the variation in samples within a plot, specifically for longitudinal studies for microbiota, but if you think it makes the code complicated, then never mind thanks for the response.

sbslee commented 2 years ago

For the beta_3D plot, i can't control the colors, e.g. if i want "gut" in red and "tongue" in blue, i can't do that. If I compare with bar plot where it allows me to use RGB color codes to choose the bacteria. it would be great to control the colors for other plots too.

I see, thanks for the explanation. One way to work around this is to use the hue_order option as follows:

dokdo.beta_3d_plot(qza_file,
                   metadata=metadata_file,
                   hue='body-site',
                   hue_order=['tongue', 'left palm', 'right palm', 'gut'],
                   figsize=(8, 8))
plt.tight_layout()

beta-3d-plot-2

This gives you "gut in red and tongue in blue". But I will look into ways to more robustly control group color.

As for add_datapoints, again, thanks for the details. While the option was making the code more complicated, it's certainly not impossible :) I will try to bring it back in the next release.

Please leave this issue open until I resolve above issues.

khemlalnirmalkar commented 2 years ago

Thanks @sbslee

sbslee commented 2 years ago

New in 1.14.0-dev (3ba0b63): A new optional argument, palette, has been added to the beta_2d_plot method so that users can manually select the group-to-color mapping (documentation).

sbslee commented 2 years ago

Example usage:

import dokdo
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set()
qza_file = '/Users/sbslee/Desktop/dokdo/data/moving-pictures-tutorial/unweighted_unifrac_pcoa_results.qza'
metadata_file = '/Users/sbslee/Desktop/dokdo/data/moving-pictures-tutorial/sample-metadata.tsv'
palette = {'gut': 'yellow', 'left palm': 'green', 'right palm': 'blue', 'tongue': 'red'}
dokdo.beta_2d_plot(qza_file, metadata_file, hue='body-site', palette=palette, figsize=(5, 5))
plt.tight_layout()

beta_2d_plot-3

sbslee commented 2 years ago

Also updated the beta_3d_plot method.

import dokdo
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set()
qza_file = '/Users/sbslee/Desktop/dokdo/data/moving-pictures-tutorial/unweighted_unifrac_pcoa_results.qza'
metadata_file = '/Users/sbslee/Desktop/dokdo/data/moving-pictures-tutorial/sample-metadata.tsv'
palette = {'gut': 'yellow', 'left palm': 'green', 'right palm': 'blue', 'tongue': 'red'}
dokdo.beta_3d_plot(qza_file, metadata=metadata_file, hue='body-site', palette=palette, figsize=(8, 8))
plt.tight_layout()

beta_3d_plot-3

sbslee commented 2 years ago

@khemlalnirmalkar,

I brought back the add_datapoints option to the taxa_abundance_box_plot method! Hope this helps.

import dokdo
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set()

qzv_file = '/Users/sbslee/Desktop/dokdo/data/moving-pictures-tutorial/taxa-bar-plots.qzv'
dokdo.taxa_abundance_box_plot(
    qzv_file,
    level=2,
    count=4,
    pretty_taxa=True,
    figsize=(8, 7),
    add_datapoints=True
)
plt.tight_layout()

taxa_abundance_box_plot-6

sbslee commented 2 years ago

I believe all of the requested features have been added to the 1.14.0-dev branch. Please try these when you get a chance and let me know if they all behave as expected. I will close this issue once testing is finished.

khemlalnirmalkar commented 2 years ago

Hi @sbslee This new version v1.14 is working great with all updated features Thank you so much for doing it

khemlalnirmalkar commented 2 years ago

Hi @sbslee One strange thing i observed with beta_2D plot, when i am making only one 2D plot, color combination is working well, but when i am 4 plots together, colors are chnaging, please can you suggest, whats going on ***my code was

import dokdo
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set()

weg_file = '/mnt/storage//run1/placebo_donors//core-metrics-results_30k/weighted_unifrac_pcoa_results.qza'
unweg_file = '/mnt/storage//run1/placebo_donors//core-metrics-results_30k/unweighted_unifrac_pcoa_results.qza'
jacc_file = '/mnt/storage//run1/placebo_donors//core-metrics-results_30k/jaccard_pcoa_results.qza'
bray_file = '/mnt/storage//run1/placebo_donors//core-metrics-results_30k/bray_curtis_pcoa_results.qza'

metadata_file = '/mnt/storage//run1/placebo_donors/sample-metadata.tsv'
palette = {'pl_Baseline':'red','pl_Vancomycin':'blue','pl_wk3':'cyan','pl_EndPart1':'lime','Donor':'black'}

fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots(2, 2, figsize=(15, 15))
dokdo.beta_2d_plot(weg_file,
                   metadata_file,
                   ax=ax1,
                   hue='TreatPoints')
dokdo.beta_2d_plot(unweg_file,
                   metadata_file,
                   ax=ax2,
                   hue='TreatPoints')
dokdo.beta_2d_plot(jacc_file,
                   metadata_file,
                   ax=ax3,
                   hue='TreatPoints')
dokdo.beta_2d_plot(bray_file,
                   metadata_file,
                   ax=ax4,
                   hue='TreatPoints',
                   )
ax1.set_title("Weighted_unifrac'", fontsize=20)
ax2.set_title("Uneighted_unifrac", fontsize=20)
ax3.set_title("Jaccard_distance", fontsize=20)
ax4.set_title('Bray-Curtis', fontsize=20)
for ax in [ax1, ax2, ax3, ax4]:
    ax.xaxis.label.set_size(20)
    ax.yaxis.label.set_size(20)
    ax.tick_params(axis='both', which='major', labelsize=15)
![Screenshot from 2022-04-23 19-39-46](https://user-images.githubusercontent.com/32944151/164953761-e975acd1-df69-4d0f-a003-2c3b53a0808d.png)

    ax.legend(loc='upper left')
plt.tight_layout()

fig1= plt.gcf()

see the attached image, baseline suppose to be red but it turned into blue

sbslee commented 2 years ago

@khemlalnirmalkar,

I see that you've defined the palette variable, but you've never used it. For example,

dokdo.beta_2d_plot(weg_file,
                   metadata_file,
                   ax=ax1,
                   palette= palette, # <----- Add this
                   hue='TreatPoints')

Try again with palette added.

khemlalnirmalkar commented 2 years ago

A silly mistake, my apologies, Thanks