Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI: Removed visualize-annotation-stats visualizer because of compatibility issues with altair and pyarrow #24

Merged
merged 15 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ find an overview of actions available in the plugin.
| annotate-mags-card | Annotate MAGs with antimicrobial resistance gene information from CARD. | [rgi](https://github.com/arpcard/rgi) | main, load |
| annotate-reads-card | Annotate metagenomic reads with antimicrobial resistance gene information from CARD. | [rgi](https://github.com/arpcard/rgi) | bwt, load |
| heatmap | Create a heatmap from annotate-mags-card output files. | [rgi](https://github.com/arpcard/rgi) | heatmap |
| visualize-annotation-stats | Plot annotate-reads-card annotation statistics. | | |


## Dev environment
This repository follows the _black_ code style. To make the development slightly easier
Expand Down
1 change: 0 additions & 1 deletion ci/recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ requirements:
- q2templates {{ qiime2_epoch }}.*
- q2cli {{ qiime2_epoch }}.*
- rgi
- altair

test:
requires:
Expand Down
36 changes: 0 additions & 36 deletions q2_amr/assets/rgi/annotation_stats/index.html

This file was deleted.

94 changes: 0 additions & 94 deletions q2_amr/card/reads.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@
import shutil
import subprocess
import tempfile
from distutils.dir_util import copy_tree
from typing import Union

import altair as alt
import pandas as pd
import pkg_resources
import q2templates
from q2_types.per_sample_sequences import (
SingleLanePerSamplePairedEndFastqDirFmt,
SingleLanePerSampleSingleEndFastqDirFmt,
Expand Down Expand Up @@ -144,93 +140,3 @@ def run_rgi_bwt(
f"(return code {e.returncode}), please inspect "
"stdout and stderr to learn more."
)


def plot_sample_stats(sample_stats: dict, output_dir: str):
sample_stats_df = pd.DataFrame.from_dict(sample_stats, orient="index")
sample_stats_df.reset_index(inplace=True)

mapped_reads_plot = (
alt.Chart(sample_stats_df)
.mark_bar()
.encode(
x=alt.X(
"index:N",
title=None,
axis=alt.Axis(labels=False, ticks=False),
bandPosition=0.1,
),
y=alt.Y(
"mapped_reads",
title="Mapped Reads",
scale=alt.Scale(
domain=(0, sample_stats_df["mapped_reads"].max() * 1.1)
),
),
tooltip=[
alt.Tooltip("index", title="Sample"),
alt.Tooltip("mapped_reads", title="Mapped Reads"),
alt.Tooltip("total_reads", title="Total Reads"),
],
)
.properties(width=alt.Step(80), height=200)
)

percentage_plot = (
alt.Chart(sample_stats_df)
.mark_bar()
.encode(
x=alt.X(
"index:N", title=None, axis=alt.Axis(labelAngle=15), bandPosition=0.1
),
y=alt.Y(
"percentage",
title="Mapped Reads (%)",
scale=alt.Scale(domain=(0, sample_stats_df["percentage"].max() * 1.1)),
),
tooltip=[
alt.Tooltip("index", title="Sample"),
alt.Tooltip("percentage", title="Mapped Reads (%)"),
alt.Tooltip("total_reads", title="Total Reads"),
],
)
.properties(width=alt.Step(80), height=200)
)
combined_chart = alt.vconcat(mapped_reads_plot, percentage_plot, spacing=0)
combined_chart.save(os.path.join(output_dir, "sample_stats_plot.html"))


def extract_sample_stats(samp_dir: str):
with open(os.path.join(samp_dir, "overall_mapping_stats.txt"), "r") as f:
for line in f:
if "Total reads:" in line:
total_reads = int(line.split()[2])
elif "Mapped reads:" in line:
mapped_reads = int(line.split()[2])
percentage = float(line.split()[3].strip("()").strip("%"))
sample_stats_dict = {
"total_reads": total_reads,
"mapped_reads": mapped_reads,
"percentage": percentage,
}
return sample_stats_dict


def visualize_annotation_stats(
output_dir: str,
amr_reads_annotation: Union[
CARDGeneAnnotationDirectoryFormat, CARDAlleleAnnotationDirectoryFormat
],
):
directory = str(amr_reads_annotation)
sample_stats = {}
for samp in os.listdir(directory):
samp_dir = os.path.join(directory, samp)
sample_stats[samp] = extract_sample_stats(samp_dir)
plot_sample_stats(sample_stats, output_dir)
TEMPLATES = pkg_resources.resource_filename("q2_amr", "assets")
copy_tree(os.path.join(TEMPLATES, "rgi", "annotation_stats"), output_dir)
context = {"tabs": [{"title": "Mapped Reads", "url": "index.html"}]}
index = os.path.join(TEMPLATES, "rgi", "annotation_stats", "index.html")
templates = [index]
q2templates.render(templates, output_dir, context=context)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class TestAnnotateMagsCard(TestPluginBase):
package = "q2_amr.tests"
package = "q2_amr.card.tests"

def mock_preprocess(self, dir, operation):
if operation == "card":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


class TestHeatmap(TestPluginBase):
package = "q2_amr.tests"
package = "q2_amr.card.tests"

def test_heatmap(self):
amr_annotation = CARDAnnotationDirectoryFormat()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class TestAnnotateMagsCard(TestPluginBase):
package = "q2_amr.tests"
package = "q2_amr.card.tests"

def mock_run_rgi_main(
self,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import shutil
import subprocess
import tempfile
from unittest.mock import ANY, MagicMock, call, patch

from q2_types.per_sample_sequences import (
Expand All @@ -10,13 +9,7 @@
)
from qiime2.plugin.testing import TestPluginBase

from q2_amr.card.reads import (
annotate_reads_card,
extract_sample_stats,
plot_sample_stats,
run_rgi_bwt,
visualize_annotation_stats,
)
from q2_amr.card.reads import annotate_reads_card, run_rgi_bwt
from q2_amr.types import (
CARDAlleleAnnotationDirectoryFormat,
CARDDatabaseDirectoryFormat,
Expand All @@ -25,7 +18,7 @@


class TestAnnotateReadsCARD(TestPluginBase):
package = "q2_amr.tests"
package = "q2_amr.card.tests"

@classmethod
def setUpClass(cls):
Expand Down Expand Up @@ -203,43 +196,3 @@ def test_exception_raised(self):
mock_run_command.side_effect = subprocess.CalledProcessError(1, "cmd")
run_rgi_bwt()
self.assertEqual(str(cm.exception), expected_message)

def test_extract_sample_stats(self):
with tempfile.TemporaryDirectory() as tmp:
mapping_stats_path = self.get_data_path("output.overall_mapping_stats.txt")
new_mapping_stats_path = os.path.join(tmp, "overall_mapping_stats.txt")
shutil.copy(mapping_stats_path, new_mapping_stats_path)
sample_stats = extract_sample_stats(tmp)

self.assertEqual(sample_stats, self.sample_stats["sample1"])

def test_plot_sample_stats(self):
with tempfile.TemporaryDirectory() as tmp:
plot_sample_stats(self.sample_stats, tmp)
self.assertTrue(os.path.exists(os.path.join(tmp, "sample_stats_plot.html")))

def mock_plot_sample_stats(self, sample_stats, output_dir):
# Create a dummy HTML file and copy it to the output_dir
with open(os.path.join(output_dir, "sample_stats_plot.html"), "w") as file:
file.write("file")

def test_visualize_annotation_stats(self):
# Create a CARDGeneAnnotation object
amr_reads_annotation = CARDGeneAnnotationDirectoryFormat()

# Create two sample directories in the CARDGeneAnnotation object
for num in range(1, 3):
os.makedirs(os.path.join(str(amr_reads_annotation), f"sample{num}"))

# Patch extract_sample_stats and plot_sample_stats with side effect
# mock_plot_sample_stats
with patch("q2_amr.card.reads.extract_sample_stats"), patch(
"q2_amr.card.reads.plot_sample_stats",
side_effect=self.mock_plot_sample_stats,
), tempfile.TemporaryDirectory() as tmp:
# Run visualize_annotation_stats function
visualize_annotation_stats(tmp, amr_reads_annotation)

# Assert if all expected files are created
for file in ["sample_stats_plot.html", "index.html", "q2templateassets"]:
self.assertTrue(os.path.exists(os.path.join(tmp, file)))
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class TestAnnotateReadsCARD(TestPluginBase):
package = "q2_amr.tests"
package = "q2_amr.card.tests"

@classmethod
def setUpClass(cls):
Expand Down
13 changes: 1 addition & 12 deletions q2_amr/plugin_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from q2_amr.card.database import fetch_card_db
from q2_amr.card.heatmap import heatmap
from q2_amr.card.mags import annotate_mags_card
from q2_amr.card.reads import annotate_reads_card, visualize_annotation_stats
from q2_amr.card.reads import annotate_reads_card
from q2_amr.types import (
CARDAnnotationJSONFormat,
CARDAnnotationTXTFormat,
Expand Down Expand Up @@ -194,17 +194,6 @@
citations=[citations["alcock_card_2023"]],
)

plugin.visualizers.register_function(
function=visualize_annotation_stats,
inputs={"amr_reads_annotation": SampleData[CARDAlleleAnnotation]},
parameters={},
input_descriptions={"amr_reads_annotation": "AMR annotations on the allele level."},
parameter_descriptions={},
name="Visualize mapping statistics.",
description="Visualize mapping statistics of an annotate-reads-card output.",
citations=[citations["alcock_card_2023"]],
)

# Registrations
plugin.register_semantic_types(
CARDDatabase,
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
package_data={
"q2_amr": [
"citations.bib",
"tests/data/*",
"card/tests/data/*",
misialq marked this conversation as resolved.
Show resolved Hide resolved
"assets/rgi/annotation_stats/*",
"assets/rgi/heatmap/*",
],
Expand All @@ -34,7 +34,7 @@
"data/annotate_mags_output/*/*/*",
"data/annotate_reads_output/*/*",
],
"q2_amr.tests": ["data/*"],
"q2_amr.card.tests": ["data/*"],
},
zip_safe=False,
)