Exemplo n.º 1
0
    def plot_sample_composition_most_abundant_taxa(
        self,
        mean_taxa: float = None,
        taxa_number: int = 20,
        taxa_level: str = "species",
        cluster_samples: bool = True,
        samples_order: List[str] = None,
        **kwargs,
    ):
        """
        Plot taxa composition of samples for most abundant taxa.

        Args:
            mean_taxa: mean threshold to be kept for analysis
            taxa_number: number of taxa to plot
            taxa_level: Taxonomy level
            cluster_samples: use clustering (skipped by samples_order)
            samples_order: list of samples to force ordering for visualization
        """
        data_df = self.df
        if mean_taxa is not None:
            data_df = TaxonomyMeanFiltering(data_df, mean_taxa).filtered_df
        data_df = self._compute_abundances_for_n_taxa(data_df, taxa_number, taxa_level)
        if samples_order is not None:
            data_df = data_df.loc[:, samples_order]
        elif cluster_samples:
            data_df = self._cluster_samples(data_df)
        # Make graph
        graph = MatrixBarGraph(data_df)
        # Plotting options
        default_plotting_options = {
            "layout": {
                "title": f"{taxa_level.capitalize()} composition for the top {data_df.drop('Others').shape[0]} most abundant species across samples",  # noqa
                "xaxis_title": "Samples",
                "yaxis_title": "Percentage",
                "legend": {"traceorder": "normal"},
            }
        }
        if mean_taxa is not None:
            default_plotting_options["layout"][
                "title"
            ] = "{} (mean among samples > {})".format(
                default_plotting_options["layout"]["title"], mean_taxa
            )
        plotting_options = merge_dict(
            kwargs.pop("plotting_options", {}), default_plotting_options
        )
        graph.plot_one_graph(plotting_options=plotting_options, **kwargs)
Exemplo n.º 2
0
    def plot_most_abundant_taxa(
        self,
        mean_taxa: float = None,
        taxa_number: int = 20,
        taxa_level: str = "species",
        **kwargs,
    ):
        """
        Plot bar chart of most abundant taxa (total sum of abundance).

        The plot represents percentage of sample with the corresponding taxa
        ordered from most abundant to less abundant.

        Args:
            mean_taxa: Mean threshold to be kept for analysis
            taxa_number: Number of taxa to plot
            taxa_level: Taxonomy level
        """
        data_df = self.df
        if mean_taxa is not None:
            data_df = TaxonomyMeanFiltering(data_df, mean_taxa).filtered_df
        percentage_presence = self._get_percentage_presence(
            data_df, taxa_level, taxa_number
        )
        taxa_number = len(percentage_presence)
        # Make graph
        graph = BarGraph(percentage_presence.iloc[::-1])
        # Plotting options
        default_plotting_options = {
            "layout": {
                "title": f"{taxa_number} most abundant {taxa_level} - Total sum of abundances",
                "xaxis_title": "Percentage Sample",
                "yaxis_title": taxa_level.capitalize(),
            }
        }
        if mean_taxa is not None:
            default_plotting_options["layout"][
                "title"
            ] = "{} (mean among samples > {})".format(
                default_plotting_options["layout"]["title"], mean_taxa
            )
        plotting_options = merge_dict(
            kwargs.pop("plotting_options", {}), default_plotting_options
        )
        graph.plot_one_graph(
            orientation="h", plotting_options=plotting_options, **kwargs
        )
Exemplo n.º 3
0
 def test_simple_dicts(self):
     first_dict = {"a": 3, "b": 4}
     second_dict = {"a": 5, "c": 7}
     expected_dict = {"a": 3, "b": 4, "c": 7}
     self.assertDictEqual(merge_dict(first_dict, second_dict),
                          expected_dict)
Exemplo n.º 4
0
 def test_with_multiple_subdict(self):
     first_dict = {"a": 3, "sub": {"b": 4, "subsub": {"z": 3}}}
     second_dict = {"a": 4, "sub": {"b": 7, "d": 6, "subsub": {"z": 5}}}
     expected_dict = {"a": 3, "sub": {"b": 4, "d": 6, "subsub": {"z": 3}}}
     self.assertDictEqual(merge_dict(first_dict, second_dict),
                          expected_dict)
Exemplo n.º 5
0
 def test_with_subdict_variant_two(self):
     first_dict = {"a": 3}
     second_dict = {"a": 4, "sub": {"b": 4, "c": 6}}
     expected_dict = {"a": 3, "sub": {"b": 4, "c": 6}}
     self.assertDictEqual(merge_dict(first_dict, second_dict),
                          expected_dict)