1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
# ----------------------------------------------------------------------------
# Copyright (c) 2016-2023, QIIME 2 development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
# ----------------------------------------------------------------------------
import skbio
import numpy as np
import unittest
from q2_alignment._filter import _most_conserved
from q2_alignment import mask
class MostConservedTests(unittest.TestCase):
def test_basic(self):
frequencies = [{'A': 1/3, '-': 2/3}, {'G': 1.0}, {'A': 2/3, 'C': 1/3}]
actual = _most_conserved(frequencies, skbio.DNA)
expected = [1.0, 1.0, 2./3.]
self.assertEqual(actual, expected)
frequencies = [{'A': 1/4, '-': 3/4}, {'G': 1.0}, {'A': 1/2, 'C': 1/2},
{'A': 1/4, 'C': 1/4, 'G': 1/4, 'T': 1/4}]
actual = _most_conserved(frequencies, skbio.DNA)
expected = [1.0, 1.0, 0.5, 0.25]
self.assertEqual(actual, expected)
def test_N(self):
frequencies = [{'A': 1/3, '-': 2/3}, {'G': 1.0}, {'A': 2/3, 'N': 1/3}]
actual = _most_conserved(frequencies, skbio.DNA)
expected = [1.0, 1.0, 2./3.]
self.assertEqual(actual, expected)
def test_unknown_gap_mode(self):
frequencies = [{'A': 1/3, '-': 2/3}, {'G': 1.0}, {'A': 2/3, 'C': 1/3}]
with self.assertRaises(ValueError):
_most_conserved(frequencies, skbio.DNA, gap_mode='not-real')
def test_all_gap(self):
frequencies = [{'-': 1.0}]
actual = _most_conserved(frequencies, skbio.DNA)
expected = [0.0]
self.assertEqual(actual, expected)
def test_empty(self):
frequencies = []
actual = _most_conserved(frequencies, skbio.DNA)
expected = []
self.assertEqual(actual, expected)
class MaskTests(unittest.TestCase):
def test_basic(self):
alignment = skbio.TabularMSA(
[skbio.DNA('AGA', metadata={'id': 'seq1', 'description': ''}),
skbio.DNA('-GA', metadata={'id': 'seq2', 'description': ''}),
skbio.DNA('-GC', metadata={'id': 'seq3', 'description': ''})]
)
actual = mask(alignment, max_gap_frequency=0.05, min_conservation=0.30)
expected = skbio.TabularMSA(
[skbio.DNA('GA', metadata={'id': 'seq1', 'description': ''}),
skbio.DNA('GA', metadata={'id': 'seq2', 'description': ''}),
skbio.DNA('GC', metadata={'id': 'seq3', 'description': ''})]
)
self.assertEqual(actual, expected)
def test_gap_boundaries(self):
alignment1 = skbio.TabularMSA(
[skbio.DNA('-', metadata={'id': 'seq1', 'description': ''}),
skbio.DNA('-', metadata={'id': 'seq2', 'description': ''}),
skbio.DNA('-', metadata={'id': 'seq3', 'description': ''})]
)
alignment2 = skbio.TabularMSA(
[skbio.DNA('A', metadata={'id': 'seq1', 'description': ''}),
skbio.DNA('A', metadata={'id': 'seq2', 'description': ''}),
skbio.DNA('A', metadata={'id': 'seq3', 'description': ''})]
)
actual = mask(alignment1, max_gap_frequency=1.0, min_conservation=0.0)
self.assertEqual(actual, alignment1)
actual = mask(alignment2, max_gap_frequency=0.0, min_conservation=0.0)
self.assertEqual(actual, alignment2)
def test_error_on_empty_alignment_gap_boundary(self):
alignment1 = skbio.TabularMSA(
[skbio.DNA('A', metadata={'id': 'seq1', 'description': ''}),
skbio.DNA('-', metadata={'id': 'seq2', 'description': ''}),
skbio.DNA('-', metadata={'id': 'seq3', 'description': ''})]
)
self.assertRaisesRegex(ValueError,
" 0.00% of positions were retained by the gap",
mask, alignment1, max_gap_frequency=0.1,
min_conservation=0.0)
def test_conservation_boundaries(self):
alignment1 = skbio.TabularMSA(
[skbio.DNA('A', metadata={'id': 'seq1', 'description': ''}),
skbio.DNA('A', metadata={'id': 'seq2', 'description': ''}),
skbio.DNA('A', metadata={'id': 'seq3', 'description': ''})])
alignment2 = skbio.TabularMSA(
[skbio.DNA('-', metadata={'id': 'seq1', 'description': ''}),
skbio.DNA('-', metadata={'id': 'seq2', 'description': ''}),
skbio.DNA('-', metadata={'id': 'seq3', 'description': ''})])
actual = mask(alignment1, max_gap_frequency=1.0, min_conservation=1.0)
self.assertEqual(actual, alignment1)
actual = mask(alignment2, max_gap_frequency=1.0, min_conservation=0.0)
self.assertEqual(actual, alignment2)
def test_error_on_empty_alignment_conservation_boundary(self):
alignment1 = skbio.TabularMSA(
[skbio.DNA('A', metadata={'id': 'seq1', 'description': ''}),
skbio.DNA('C', metadata={'id': 'seq2', 'description': ''}),
skbio.DNA('G', metadata={'id': 'seq3', 'description': ''})])
self.assertRaisesRegex(ValueError,
" 0.00% of positions were retained by the con",
mask, alignment1, max_gap_frequency=1.0,
min_conservation=0.5)
def test_invalid_gap_threshold(self):
alignment = skbio.TabularMSA(
[skbio.DNA('-', metadata={'id': 'seq1', 'description': ''}),
skbio.DNA('-', metadata={'id': 'seq2', 'description': ''}),
skbio.DNA('-', metadata={'id': 'seq3', 'description': ''})]
)
eps = np.finfo(float).eps
with self.assertRaises(ValueError):
mask(alignment, max_gap_frequency=0.0 - eps)
with self.assertRaises(ValueError):
mask(alignment, max_gap_frequency=1.0 + eps)
def test_invalid_conservation_threshold(self):
alignment = skbio.TabularMSA(
[skbio.DNA('-', metadata={'id': 'seq1', 'description': ''}),
skbio.DNA('-', metadata={'id': 'seq2', 'description': ''}),
skbio.DNA('-', metadata={'id': 'seq3', 'description': ''})]
)
eps = np.finfo(float).eps
with self.assertRaises(ValueError):
mask(alignment, min_conservation=0.0 - eps)
with self.assertRaises(ValueError):
mask(alignment, min_conservation=1.0 + eps)
def test_empty_input(self):
alignment = skbio.TabularMSA(
[skbio.DNA('', metadata={'id': 'seq1', 'description': ''}),
skbio.DNA('', metadata={'id': 'seq2', 'description': ''}),
skbio.DNA('', metadata={'id': 'seq3', 'description': ''})]
)
with self.assertRaises(ValueError):
mask(alignment)
alignment = skbio.TabularMSA([])
with self.assertRaises(ValueError):
mask(alignment)
|