Skip to content

Commit

Permalink
add summarize-alignment visualizer (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregcaporaso committed Apr 2, 2024
1 parent e54d743 commit 1e802ea
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
41 changes: 41 additions & 0 deletions q2_dwq2/_visualizers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# ----------------------------------------------------------------------------
# Copyright (c) 2024, Greg Caporaso.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
# ----------------------------------------------------------------------------

import os.path

from skbio.alignment import TabularMSA


def summarize_alignment(output_dir: str, msa: TabularMSA) -> None:
with open(os.path.join(output_dir, "index.html"), "w") as fh:
fh.write(_html_template % repr(msa))


_html_template = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Alignment summary</title>
<style>
body {
padding: 20px;
}
p.alignment {
font-family: 'Courier New', Courier, monospace;
}
</style>
</head>
<body>
<pre>
%s
</pre>
</body>
</html>
"""
11 changes: 11 additions & 0 deletions q2_dwq2/plugin_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from q2_types.feature_data import FeatureData, Sequence, AlignedSequence
from q2_dwq2 import __version__
from q2_dwq2._methods import nw_align
from q2_dwq2._visualizers import summarize_alignment

citations = Citations.load("citations.bib", package="q2_dwq2")

Expand Down Expand Up @@ -54,3 +55,13 @@
"This action is for demonstration purposes only. 🐌"),
citations=[citations['Needleman1970']]
)

plugin.visualizers.register_function(
function=summarize_alignment,
inputs={'msa': FeatureData[AlignedSequence]},
input_descriptions={'msa': 'The multiple sequence alignment to summarize.'},
parameters={},
parameter_descriptions={},
name='Summarize an alignment.',
description='Summarize a multiple sequence alignment.',
)
34 changes: 34 additions & 0 deletions q2_dwq2/tests/test_visualizers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# ----------------------------------------------------------------------------
# Copyright (c) 2024, Greg Caporaso.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
# ----------------------------------------------------------------------------

import os.path

from skbio.alignment import TabularMSA
from skbio.sequence import DNA

from qiime2.plugin.testing import TestPluginBase

from q2_dwq2._visualizers import summarize_alignment


class SummarizeAlignmentTests(TestPluginBase):
package = 'q2_dwq2.tests'

def test_simple1(self):
aligned_sequence1 = DNA('AAAAAAAAGGTGGCCTTTTTTTT')
aligned_sequence2 = DNA('AAAAAAAAGG-GGCCTTTTTTTT')
msa = TabularMSA([aligned_sequence1, aligned_sequence2])

with self.temp_dir as output_dir:
summarize_alignment(output_dir, msa)

with open(os.path.join(output_dir, 'index.html'), 'r') as fh:
observed = fh.read()

self.assertIn('AAAAAAAAGGTGGCCTTTTTTTT', observed)
self.assertIn('AAAAAAAAGG-GGCCTTTTTTTT', observed)

0 comments on commit 1e802ea

Please sign in to comment.