From 951b94ad57b1f5a783ad767e610dd1712a36fdc8 Mon Sep 17 00:00:00 2001 From: Greg Caporaso Date: Tue, 8 Oct 2024 08:53:09 -0700 Subject: [PATCH 1/8] integrate legend in Visualization if `export_legend` is enabled, present the legend in the resulting Visualization. Note that there was a minor bug in the original version of the Visualizer - since the conditional was being evaluated after `export_legend` was cast to a string, it always evaluated to `True`. --- gut_to_soil_manuscript_figures/_methods.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/gut_to_soil_manuscript_figures/_methods.py b/gut_to_soil_manuscript_figures/_methods.py index 96d131b..eef69ed 100644 --- a/gut_to_soil_manuscript_figures/_methods.py +++ b/gut_to_soil_manuscript_figures/_methods.py @@ -44,12 +44,11 @@ def pcoa_2d(output_dir: str, metadata: qiime2.Metadata, swap_axes = str(swap_axes) himalaya = str(himalaya) pit_toilet = str(pit_toilet) - export_legend = str(export_legend) + export_legend_str = str(export_legend) plot_fp = os.path.join(output_dir, 'pcoa_plot.png') - if export_legend: - legend_fp = os.path.join(output_dir, 'legend.png') + legend_fp = os.path.join(output_dir, 'legend.png') command = [ 'python', script_path, @@ -64,7 +63,7 @@ def pcoa_2d(output_dir: str, metadata: qiime2.Metadata, swap_axes, himalaya, pit_toilet, - export_legend, + export_legend_str, highlighted_buckets, legend_fp ] @@ -80,6 +79,13 @@ def pcoa_2d(output_dir: str, metadata: qiime2.Metadata,

2D PCoA Plot

PCoA Plot + ''') + if export_legend: + f.write(''' +

+ PCoA Plot legend + ''') + f.write(''' ''') From b9da739701dc4af9d49d3f889d32744bc946cb41 Mon Sep 17 00:00:00 2001 From: Greg Caporaso Date: Tue, 8 Oct 2024 09:09:12 -0700 Subject: [PATCH 2/8] updates to reflect refactored metadata --- README.md | 4 +- .../scripts/plot_pcoa_2d.py | 61 ++++++++++--------- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 0029d6d..fc553c7 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ Your first step will be filtering the distance matrix you'd like to use for the qiime diversity filter-distance-matrix \ --i-distance-matrix unweighted-unifrac-distance-matrix.qza \ --m-metadata-file final-analysis-metadata.tsv \ ---p-where "[SampleType2] IN ('EMP-Soils', 'Food-Compost', 'Self Sample', 'Compost Post-Roll', 'Bulking Material')" \ +--p-where "[SampleType] IN ('Soil', 'Food Compost', 'Landscape Compost', 'Human Excrement', 'Human Excrement Compost', 'Bulking Material')" \ --o-filtered-distance-matrix filtered-unweighted-unifrac-distance-matrix.qza ``` @@ -95,7 +95,7 @@ Now we're ready to generate a pcoa plot! qiime gut-to-soil-manuscript-figures pcoa-2d \ --i-ordination filtered-unweighted-unifrac-2d-pcoa.qza \ --m-metadata-file final-analysis-metadata.tsv \ ---p-measure 'Unweighted Unifrac' \ +--p-measure 'Unweighted UniFrac' \ --p-average \ --p-export-legend \ --p-highlighted-buckets '3, 4' \ diff --git a/gut_to_soil_manuscript_figures/scripts/plot_pcoa_2d.py b/gut_to_soil_manuscript_figures/scripts/plot_pcoa_2d.py index df36685..e32d123 100644 --- a/gut_to_soil_manuscript_figures/scripts/plot_pcoa_2d.py +++ b/gut_to_soil_manuscript_figures/scripts/plot_pcoa_2d.py @@ -43,11 +43,12 @@ def _bucket_util(highlighted_buckets, md, ord_2d): # connecting time series data in order md_bucket_sorted = \ md[(md['Bucket'] == bucket) & - (md['SampleType2'] == 'Compost Post-Roll')].sort_values('Week') + (md['SampleType'] == 'Human Excrement Compost')].\ + sort_values('Composting Time Point') # week 1-52 IDs for selected bucket - bucket_ids_sorted = \ - md_bucket_sorted[md_bucket_sorted['Week'] > 0.0].index.values + bucket_ids_sorted = md_bucket_sorted[ + md_bucket_sorted['Composting Time Point'] > 0.0].index.values # making sure bucket IDs used are only ones that are present in both # the md and ordination results @@ -62,8 +63,7 @@ def _bucket_util(highlighted_buckets, md, ord_2d): # HE bucket_ids_HE_week0 = \ md[(md['Bucket'] == bucket) & - (md['SampleType2'] == 'Self Sample') & - (md['Week'] == 0.0)].index.values + (md['SampleType'] == 'Human Excrement')].index.values ids_HE_week0 = [] for i in bucket_ids_HE_week0: @@ -73,8 +73,7 @@ def _bucket_util(highlighted_buckets, md, ord_2d): # bulking bucket_ids_bulk_week0 = \ md[(md['Bucket'] == bucket) & - (md['SampleType2'] == 'Bulking Material') & - (md['Week'] == 0.0)].index.values + (md['SampleType'] == 'Bulking Material')].index.values ids_bulk_week0 = [] for i in bucket_ids_bulk_week0: @@ -82,8 +81,8 @@ def _bucket_util(highlighted_buckets, md, ord_2d): ids_bulk_week0.append(i) # week 1 i.e. end points for dotted line connecting HE & BM -> HEC - bucket_ids_HEC_week1 = \ - md_bucket_sorted[md_bucket_sorted['Week'] == 1.0].index.values + bucket_ids_HEC_week1 = md_bucket_sorted[ + md_bucket_sorted['Composting Time Point'] == 1.0].index.values ids_HEC_week1 = [] for i in bucket_ids_HEC_week1: @@ -141,8 +140,9 @@ def plot_pcoa_2d(metadata_fp, ordination_fp, measure, ord_2d[1] = ord_2d[1].multiply(-1) # allowed sample types to be pulled from the md - sample_types = ['EMP-Soils', 'Food-Compost', 'Self Sample', - 'Compost Post-Roll', 'Bulking Material'] + sample_types = ['Soil', 'Food Compost', 'Landscape Compost', + 'Human Excrement', 'Human Excrement Compost', + 'Bulking Material'] # if using himalaya and/or pit toilet data if himalaya == 'True': @@ -151,21 +151,23 @@ def plot_pcoa_2d(metadata_fp, ordination_fp, measure, sample_types.append('Pit Toilet') # sorting the filtered md (by allowed sample types) by week - md = metadata[metadata['SampleType2'] - .isin(sample_types)].sort_values('Week') + md = metadata[metadata['SampleType'] + .isin(sample_types)].sort_values('Composting Time Point') md['Bucket'] = md['Bucket'].astype(float) - md['Week'] = md['Week'].astype(float) + md['Composting Time Point'] = md['Composting Time Point'].astype(float) buckets_md = md[md['Bucket'].between(1, 16)] # ALL SUBJECT FECAL SAMPLES: IDs -> XY ordination points - fecal_ids = list(set(buckets_md[buckets_md['Week'] == 0.0].index.values) & - set(ord_2d.index.values)) + fecal_ids = list( + set(buckets_md[buckets_md['SampleType'] == 'Human Excrement'] + .index.values) & + set(ord_2d.index.values)) x_fecal, y_fecal = _swap_axis(ord_2d, fecal_ids, swap_axes) # ALL SUBJECT BULKING MATERIAL: IDs -> XY ordination points bulking_ids = \ - list(set(md[md['SampleType2'] == 'Bulking Material'].index.values) & + list(set(md[md['SampleType'] == 'Bulking Material'].index.values) & set(ord_2d.index.values)) x_bulking, y_bulking = _swap_axis(ord_2d, bulking_ids, swap_axes) @@ -176,8 +178,8 @@ def plot_pcoa_2d(metadata_fp, ordination_fp, measure, # (OPTIONAL) WEEKLY MEAN FOR ALL BUCKETS: IDs -> XY ordination points if average == 'True': - weeks_md = md[md['Week'].between(1, 52)] - weeks = list(set(weeks_md['Week'].values)) + weeks_md = md[md['Composting Time Point'].between(1, 52)] + weeks = list(set(weeks_md['Composting Time Point'].values)) # dicts for each week's mean x&y values bucket_weekly_avgs_x = {} @@ -188,9 +190,9 @@ def plot_pcoa_2d(metadata_fp, ordination_fp, measure, y_list = [] # filtering the md to only include post-roll sample types - weekly_bucket_ids = \ - md[(md['Week'] == week) & - (md['SampleType2'] == 'Compost Post-Roll')].index.values + weekly_bucket_ids = md[ + (md['Composting Time Point'] == week) & + (md['SampleType'] == 'Human Excrement Compost')].index.values # only use IDs that are present both in the md and ordination included_ids = [] @@ -233,8 +235,7 @@ def plot_pcoa_2d(metadata_fp, ordination_fp, measure, if not highlighted_buckets: # HE wk 0 mean HE_week0 = \ - md[(md['SampleType2'] == 'Self Sample') & - (md['Week'] == 0.0)].index.values + md[(md['SampleType'] == 'Human Excrement')].index.values ids_HE_week0 = [] for i in HE_week0: if i in ord_2d.index.values: @@ -245,8 +246,7 @@ def plot_pcoa_2d(metadata_fp, ordination_fp, measure, # bulk wk 0 mean bulk_week0 = \ - md[(md['SampleType2'] == 'Bulking Material') & - (md['Week'] == 0.0)].index.values + md[(md['SampleType'] == 'Bulking Material')].index.values ids_bulk_week0 = [] for i in bulk_week0: if i in ord_2d.index.values: @@ -268,13 +268,14 @@ def plot_pcoa_2d(metadata_fp, ordination_fp, measure, x_buckets, y_buckets = _swap_axis(ord_2d, bucket_ids, swap_axes) # EMP SOILS - emp_ids = md.loc[md['Bucket'] == 0.0].index.values + emp_ids = md.loc[md['SampleType'] == 'Soil'].index.values x_emp, y_emp = _swap_axis(ord_2d, emp_ids, swap_axes) # FOOD COMPOST compost_ids = \ - list(set(md.loc[md['Bucket'] == 17.0].index.values) & - set(ord_2d.index.values)) + list(set(md.loc[(md['SampleType'] == 'Food Compost') | + (md['SampleType'] == 'Landscape Compost')].index.values) + & set(ord_2d.index.values)) x_compost, y_compost = _swap_axis(ord_2d, compost_ids, swap_axes) # (OPTIONAL SAMPLE TYPES) HIMALAYA @@ -424,7 +425,7 @@ def plot_pcoa_2d(metadata_fp, ordination_fp, measure, # adding week annotations for each highlighted bucket if week_annotations == 'True': - for week, x, y in zip((md.loc[ids]['Week']), + for week, x, y in zip((md.loc[ids]['Composting Time Point']), x_bucket, y_bucket): week_int = int(week) ax.annotate(str(week_int), weight='bold', color='purple', From 0331476156d06eb2fa44c6ffa911ba86b5eb1d7b Mon Sep 17 00:00:00 2001 From: Greg Caporaso <192372+gregcaporaso@users.noreply.github.com> Date: Wed, 9 Oct 2024 06:46:55 -0700 Subject: [PATCH 3/8] Update gut_to_soil_manuscript_figures/_methods.py Co-authored-by: Liz Gehret <54517601+lizgehret@users.noreply.github.com> --- gut_to_soil_manuscript_figures/_methods.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gut_to_soil_manuscript_figures/_methods.py b/gut_to_soil_manuscript_figures/_methods.py index eef69ed..f2a0538 100644 --- a/gut_to_soil_manuscript_figures/_methods.py +++ b/gut_to_soil_manuscript_figures/_methods.py @@ -44,7 +44,7 @@ def pcoa_2d(output_dir: str, metadata: qiime2.Metadata, swap_axes = str(swap_axes) himalaya = str(himalaya) pit_toilet = str(pit_toilet) - export_legend_str = str(export_legend) + export_legend = str(export_legend) plot_fp = os.path.join(output_dir, 'pcoa_plot.png') From 04f45c929be15f90f397258babb2ca3720d54c26 Mon Sep 17 00:00:00 2001 From: Greg Caporaso <192372+gregcaporaso@users.noreply.github.com> Date: Wed, 9 Oct 2024 06:47:08 -0700 Subject: [PATCH 4/8] Update gut_to_soil_manuscript_figures/_methods.py Co-authored-by: Liz Gehret <54517601+lizgehret@users.noreply.github.com> --- gut_to_soil_manuscript_figures/_methods.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gut_to_soil_manuscript_figures/_methods.py b/gut_to_soil_manuscript_figures/_methods.py index f2a0538..8711672 100644 --- a/gut_to_soil_manuscript_figures/_methods.py +++ b/gut_to_soil_manuscript_figures/_methods.py @@ -63,7 +63,7 @@ def pcoa_2d(output_dir: str, metadata: qiime2.Metadata, swap_axes, himalaya, pit_toilet, - export_legend_str, + export_legend, highlighted_buckets, legend_fp ] From fffaaecf542f8cd6924bab7a0314de8edce68e50 Mon Sep 17 00:00:00 2001 From: Greg Caporaso <192372+gregcaporaso@users.noreply.github.com> Date: Wed, 9 Oct 2024 06:47:23 -0700 Subject: [PATCH 5/8] Update gut_to_soil_manuscript_figures/_methods.py Co-authored-by: Liz Gehret <54517601+lizgehret@users.noreply.github.com> --- gut_to_soil_manuscript_figures/_methods.py | 1 - 1 file changed, 1 deletion(-) diff --git a/gut_to_soil_manuscript_figures/_methods.py b/gut_to_soil_manuscript_figures/_methods.py index 8711672..2f1d97e 100644 --- a/gut_to_soil_manuscript_figures/_methods.py +++ b/gut_to_soil_manuscript_figures/_methods.py @@ -48,7 +48,6 @@ def pcoa_2d(output_dir: str, metadata: qiime2.Metadata, plot_fp = os.path.join(output_dir, 'pcoa_plot.png') - legend_fp = os.path.join(output_dir, 'legend.png') command = [ 'python', script_path, From aabe60ffa2f9a068df2d78d88da2d31a705e6e59 Mon Sep 17 00:00:00 2001 From: Greg Caporaso <192372+gregcaporaso@users.noreply.github.com> Date: Wed, 9 Oct 2024 06:47:36 -0700 Subject: [PATCH 6/8] Update gut_to_soil_manuscript_figures/_methods.py Co-authored-by: Liz Gehret <54517601+lizgehret@users.noreply.github.com> --- gut_to_soil_manuscript_figures/_methods.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gut_to_soil_manuscript_figures/_methods.py b/gut_to_soil_manuscript_figures/_methods.py index 2f1d97e..80c1cf5 100644 --- a/gut_to_soil_manuscript_figures/_methods.py +++ b/gut_to_soil_manuscript_figures/_methods.py @@ -79,7 +79,8 @@ def pcoa_2d(output_dir: str, metadata: qiime2.Metadata,

2D PCoA Plot

PCoA Plot ''') - if export_legend: + if export_legend == 'True': + legend_fp = os.path.join(output_dir, 'legend.png') f.write('''

PCoA Plot legend From ac3f9921b83d6eb2994cad3782c76922a8fdf1d6 Mon Sep 17 00:00:00 2001 From: Greg Caporaso Date: Wed, 9 Oct 2024 06:56:37 -0700 Subject: [PATCH 7/8] legend_fp was referenced before assignment --- gut_to_soil_manuscript_figures/_methods.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gut_to_soil_manuscript_figures/_methods.py b/gut_to_soil_manuscript_figures/_methods.py index 80c1cf5..67a42d4 100644 --- a/gut_to_soil_manuscript_figures/_methods.py +++ b/gut_to_soil_manuscript_figures/_methods.py @@ -47,7 +47,8 @@ def pcoa_2d(output_dir: str, metadata: qiime2.Metadata, export_legend = str(export_legend) plot_fp = os.path.join(output_dir, 'pcoa_plot.png') - + if export_legend == 'True': + legend_fp = os.path.join(output_dir, 'legend.png') command = [ 'python', script_path, @@ -80,7 +81,6 @@ def pcoa_2d(output_dir: str, metadata: qiime2.Metadata, PCoA Plot ''') if export_legend == 'True': - legend_fp = os.path.join(output_dir, 'legend.png') f.write('''

PCoA Plot legend From c917fee98dd1f0bdb99ad219ced21279665b7007 Mon Sep 17 00:00:00 2001 From: Greg Caporaso Date: Wed, 9 Oct 2024 07:10:49 -0700 Subject: [PATCH 8/8] address issue with missing Week 0.0 checks --- .../scripts/plot_pcoa_2d.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/gut_to_soil_manuscript_figures/scripts/plot_pcoa_2d.py b/gut_to_soil_manuscript_figures/scripts/plot_pcoa_2d.py index e32d123..e47de3c 100644 --- a/gut_to_soil_manuscript_figures/scripts/plot_pcoa_2d.py +++ b/gut_to_soil_manuscript_figures/scripts/plot_pcoa_2d.py @@ -63,7 +63,8 @@ def _bucket_util(highlighted_buckets, md, ord_2d): # HE bucket_ids_HE_week0 = \ md[(md['Bucket'] == bucket) & - (md['SampleType'] == 'Human Excrement')].index.values + (md['SampleType'] == 'Human Excrement') & + (md['Composting Time Point']).isna()].index.values ids_HE_week0 = [] for i in bucket_ids_HE_week0: @@ -73,7 +74,8 @@ def _bucket_util(highlighted_buckets, md, ord_2d): # bulking bucket_ids_bulk_week0 = \ md[(md['Bucket'] == bucket) & - (md['SampleType'] == 'Bulking Material')].index.values + (md['SampleType'] == 'Bulking Material') & + (md['Composting Time Point']).isna()].index.values ids_bulk_week0 = [] for i in bucket_ids_bulk_week0: @@ -235,7 +237,8 @@ def plot_pcoa_2d(metadata_fp, ordination_fp, measure, if not highlighted_buckets: # HE wk 0 mean HE_week0 = \ - md[(md['SampleType'] == 'Human Excrement')].index.values + md[(md['SampleType'] == 'Human Excrement') & + (md['Composting Time Point']).isna()].index.values ids_HE_week0 = [] for i in HE_week0: if i in ord_2d.index.values: @@ -246,7 +249,8 @@ def plot_pcoa_2d(metadata_fp, ordination_fp, measure, # bulk wk 0 mean bulk_week0 = \ - md[(md['SampleType'] == 'Bulking Material')].index.values + md[(md['SampleType'] == 'Bulking Material') & + (md['Composting Time Point']).isna()].index.values ids_bulk_week0 = [] for i in bulk_week0: if i in ord_2d.index.values: