|
| 1 | +# ---------------------------------------------------------------------------- |
| 2 | +# Copyright (c) 2023, QIIME 2 development team. |
| 3 | +# |
| 4 | +# Distributed under the terms of the Modified BSD License. |
| 5 | +# |
| 6 | +# The full license is in the file LICENSE, distributed with this software. |
| 7 | +# ---------------------------------------------------------------------------- |
| 8 | +import os |
| 9 | + |
| 10 | +import pandas as pd |
| 11 | +import qiime2 as q2 |
| 12 | +import skbio |
| 13 | +from q2_types.per_sample_sequences import ContigSequencesDirFmt |
| 14 | +from qiime2.plugin.testing import TestPluginBase |
| 15 | + |
| 16 | +from q2_assembly.filter import filter_contigs |
| 17 | + |
| 18 | + |
| 19 | +class TestFilterContigs(TestPluginBase): |
| 20 | + package = "q2_assembly.tests" |
| 21 | + |
| 22 | + def setUp(self): |
| 23 | + super().setUp() |
| 24 | + self.contigs = ContigSequencesDirFmt( |
| 25 | + self.get_data_path("contigs-with-empty"), 'r' |
| 26 | + ) |
| 27 | + self.metadata_df = pd.DataFrame( |
| 28 | + data={'col1': ['yes', 'no', 'yes']}, |
| 29 | + index=pd.Index(['sample1', 'sample2', 'sample3'], name='id') |
| 30 | + ) |
| 31 | + self.metadata = q2.Metadata(self.metadata_df) |
| 32 | + |
| 33 | + def test_filter_metadata(self): |
| 34 | + obs = filter_contigs( |
| 35 | + contigs=self.contigs, metadata=self.metadata, where="col1='yes'" |
| 36 | + ) |
| 37 | + |
| 38 | + self.assertDictEqual( |
| 39 | + obs.sample_dict(), |
| 40 | + {'sample1': os.path.join(obs.path, 'sample1_contigs.fa'), |
| 41 | + 'sample3': os.path.join(obs.path, 'sample3_contigs.fa')} |
| 42 | + ) |
| 43 | + |
| 44 | + def test_filter_metadata_exclude_ids(self): |
| 45 | + obs = filter_contigs( |
| 46 | + contigs=self.contigs, metadata=self.metadata, |
| 47 | + where="col1='yes'", exclude_ids=True |
| 48 | + ) |
| 49 | + |
| 50 | + self.assertDictEqual( |
| 51 | + obs.sample_dict(), |
| 52 | + {'sample2': os.path.join(obs.path, 'sample2_contigs.fa')} |
| 53 | + ) |
| 54 | + |
| 55 | + def test_filter_metadata_no_query_no_metadata(self): |
| 56 | + with self.assertRaisesRegex( |
| 57 | + ValueError, |
| 58 | + 'At least one of the following parameters must be provided' |
| 59 | + ): |
| 60 | + filter_contigs(contigs=self.contigs) |
| 61 | + |
| 62 | + def test_filter_metadata_no_query(self): |
| 63 | + with self.assertRaisesRegex( |
| 64 | + ValueError, |
| 65 | + 'A filter query must be provided' |
| 66 | + ): |
| 67 | + filter_contigs(contigs=self.contigs, metadata=self.metadata) |
| 68 | + |
| 69 | + def test_filter_by_length(self): |
| 70 | + obs = filter_contigs(contigs=self.contigs, length_threshold=320) |
| 71 | + |
| 72 | + self.assertEqual(len(obs.sample_dict()), 3) |
| 73 | + |
| 74 | + exp_counts = (6, 1, 0) |
| 75 | + for (_id, fp), count in zip(obs.sample_dict().items(), exp_counts): |
| 76 | + with (open(fp) as f): |
| 77 | + self.assertEqual( |
| 78 | + len(list(skbio.io.read(f, format='fasta'))), count |
| 79 | + ) |
| 80 | + |
| 81 | + def test_filter_remove_empty(self): |
| 82 | + obs = filter_contigs(contigs=self.contigs, remove_empty=True) |
| 83 | + |
| 84 | + self.assertDictEqual( |
| 85 | + obs.sample_dict(), |
| 86 | + {'sample1': os.path.join(obs.path, 'sample1_contigs.fa'), |
| 87 | + 'sample2': os.path.join(obs.path, 'sample2_contigs.fa')} |
| 88 | + ) |
| 89 | + |
| 90 | + def test_filter_by_length_and_remove_empty(self): |
| 91 | + obs = filter_contigs( |
| 92 | + contigs=self.contigs, length_threshold=400, remove_empty=True |
| 93 | + ) |
| 94 | + |
| 95 | + self.assertEqual(len(obs.sample_dict()), 1) |
| 96 | + |
| 97 | + with (open(os.path.join(obs.path, 'sample1_contigs.fa')) as f): |
| 98 | + self.assertEqual( |
| 99 | + len(list(skbio.io.read(f, format='fasta'))), 2 |
| 100 | + ) |
| 101 | + |
| 102 | + def test_filter_everything(self): |
| 103 | + with self.assertRaisesRegex( |
| 104 | + ValueError, 'No samples remain after filtering' |
| 105 | + ): |
| 106 | + filter_contigs( |
| 107 | + contigs=self.contigs, length_threshold=1000, remove_empty=True |
| 108 | + ) |
0 commit comments