|
| 1 | +import contextlib |
| 2 | +import io |
| 3 | +import unittest |
| 4 | +from tempfile import NamedTemporaryFile |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +from sbin.filter_exonset_transcripts import filter_exonset |
| 8 | + |
| 9 | + |
| 10 | +class TestFilterExonsetTranscripts(unittest.TestCase): |
| 11 | + |
| 12 | + @patch('sbin.filter_exonset_transcripts.logger') |
| 13 | + def test_filter_exonset(self, mock_logger): |
| 14 | + # Test NR_046571.1 is filtered out |
| 15 | + lines = [ |
| 16 | + "tx_ac\talt_ac\tmethod\tstrand\texons_se_i\n", |
| 17 | + "NR_122113.1\tNC_000022.10\tsplign\t-1\t16192905,16193009;16190680,16190791;16189263,16189378;16189031,16189143;16187164,16187302;16186810,16186953;16162396,16162487;16150528,16151821\n", |
| 18 | + "NR_133911.1\tNC_000022.10\tsplign\t1\t16157078,16157342;16164481,16164569;16171951,16172265\n", |
| 19 | + "NR_046571.1\tNC_000022.10\tsplign\t1\t16274608,16275003;16276480,16277577\n" |
| 20 | + ] |
| 21 | + with NamedTemporaryFile(delete=False) as temp_exonsets: |
| 22 | + with open(temp_exonsets.name, "wt") as f: |
| 23 | + for line in lines: |
| 24 | + f.write(line) |
| 25 | + temp_exonsets.seek(0) |
| 26 | + missing_ids_file = NamedTemporaryFile() |
| 27 | + |
| 28 | + transcript_ids = {"NR_122113.1", "NR_133911.1"} |
| 29 | + stdout = io.StringIO() |
| 30 | + with contextlib.redirect_stdout(stdout): |
| 31 | + filter_exonset(temp_exonsets.name, transcript_ids, missing_ids_file.name) |
| 32 | + |
| 33 | + # Assert the record for NR_046571.1 is filtered out |
| 34 | + self.assertEqual(stdout.getvalue(), ''.join(lines[0:3])) |
| 35 | + |
| 36 | + # Confirm filtered transcript is present in missing_ids_file |
| 37 | + with open(missing_ids_file.name, 'r') as f: |
| 38 | + contents = f.read() |
| 39 | + self.assertEqual(contents, 'NR_046571.1\n') |
| 40 | + |
| 41 | + mock_logger.warning.assert_called_with('Exon set transcript NR_046571.1 not found in txinfo file. Filtering out.') |
| 42 | + mock_logger.info.assert_called_with('Filtered out exon sets for 1 transcript(s): NR_046571.1') |
| 43 | + |
| 44 | + |
| 45 | +if __name__ == "__main__": |
| 46 | + unittest.main() |
0 commit comments