Debian Codegraph @

test/test_format.py

raw symbols SCIP
1 """
2 Copyright 2017 Ryan Wick ([email protected])
3 https://github.com/rrwick/Porechop
4
5 This module contains some tests for Porechop. To run them, execute `python3 -m unittest` from the
6 root Porechop directory.
7
8 This file is part of Porechop. Porechop is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by the Free Software Foundation,
10 either version 3 of the License, or (at your option) any later version. Porechop is distributed in
11 the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 details. You should have received a copy of the GNU General Public License along with Porechop. If
14 not, see <http://www.gnu.org/licenses/>.
15 """
16
17 import unittest
18 import os
19 import subprocess
20 import shutil
21 import porechop.misc
22
23
24 def get_read_type(filename):
25 _, read_type = porechop.misc.load_fasta_or_fastq(filename)
26 read_type = read_type.lower()
27 compression_type = porechop.misc.get_compression_type(filename)
28 if compression_type == 'plain':
29 return read_type
30 elif compression_type == 'gz':
31 return read_type + '.gz'
32 else:
33 assert False
34
35
36 class TestOutputFormat(unittest.TestCase):
37
38 def run_command(self, command, input_filename):
39 runner_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'porechop-runner.py')
40 input_path = os.path.join(os.path.dirname(__file__), input_filename)
41 command = command.replace('porechop', runner_path)
42 command = command.replace('IN', input_path)
43 output_name = 'TEMP_' + str(os.getpid())
44 command = command.replace('OUT', output_name)
45 try:
46 self.output_file = [x for x in command.split() if output_name in x][0]
47 except IndexError:
48 self.output_file = ''
49 p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
50 out, err = p.communicate()
51 return out.decode(), err.decode()
52
53 def tearDown(self):
54 if os.path.isfile(self.output_file):
55 os.remove(self.output_file)
56
57 # The following tests use the auto format and should determine the output type using the output
58 # filename.
59
60 def test_auto_format_fastq_to_fastq(self):
61 self.run_command('porechop -i IN -o OUT.fastq', 'test_format.fastq')
62 self.assertEqual(get_read_type(self.output_file), 'fastq')
63
64 def test_auto_format_fastq_to_fastq_gz(self):
65 self.run_command('porechop -i IN -o OUT.fastq.gz', 'test_format.fastq')
66 self.assertEqual(get_read_type(self.output_file), 'fastq.gz')
67
68 def test_auto_format_fastq_to_fasta(self):
69 self.run_command('porechop -i IN -o OUT.fasta', 'test_format.fastq')
70 self.assertEqual(get_read_type(self.output_file), 'fasta')
71
72 def test_auto_format_fastq_to_fasta_gz(self):
73 self.run_command('porechop -i IN -o OUT.fasta.gz', 'test_format.fastq')
74 self.assertEqual(get_read_type(self.output_file), 'fasta.gz')
75
76 def test_auto_format_fastq_to_fastq_gz_weird_caps(self):
77 self.run_command('porechop -i IN -o OUT.fAsTq.Gz', 'test_format.fastq')
78 self.assertEqual(get_read_type(self.output_file), 'fastq.gz')
79
80 # The following tests use the auto format and pipe to output. They determine the output format
81 # from the input filename, but won't gzip the reads.
82
83 def test_auto_format_fastq_to_pipe(self):
84 self.run_command('porechop -i IN > OUT', 'test_format.fastq')
85 self.assertEqual(get_read_type(self.output_file), 'fastq')
86
87 def test_auto_format_fastq_gz_to_pipe(self):
88 self.run_command('porechop -i IN > OUT', 'test_format.fastq.gz')
89 self.assertEqual(get_read_type(self.output_file), 'fastq')
90
91 def test_auto_format_fasta_to_pipe(self):
92 self.run_command('porechop -i IN > OUT', 'test_format.fasta')
93 self.assertEqual(get_read_type(self.output_file), 'fasta')
94
95 def test_auto_format_fasta_gz_to_pipe(self):
96 self.run_command('porechop -i IN > OUT', 'test_format.fasta.gz')
97 self.assertEqual(get_read_type(self.output_file), 'fasta')
98
99 # The following tests explicitly set the output format.
100
101 def test_explicit_format_fastq_to_fastq(self):
102 self.run_command('porechop -i IN -o OUT --format fastq', 'test_format.fastq')
103 self.assertEqual(get_read_type(self.output_file), 'fastq')
104
105 def test_explicit_format_fastq_to_fastq_gz(self):
106 self.run_command('porechop -i IN -o OUT --format fastq.gz', 'test_format.fastq')
107 self.assertEqual(get_read_type(self.output_file), 'fastq.gz')
108
109 def test_explicit_format_fastq_to_fasta(self):
110 self.run_command('porechop -i IN -o OUT --format fasta', 'test_format.fastq')
111 self.assertEqual(get_read_type(self.output_file), 'fasta')
112
113 def test_explicit_format_fastq_to_fasta_gz(self):
114 self.run_command('porechop -i IN -o OUT --format fasta.gz', 'test_format.fastq')
115 self.assertEqual(get_read_type(self.output_file), 'fasta.gz')
116
117 def test_explicit_format_fasta_to_fasta(self):
118 self.run_command('porechop -i IN -o OUT --format fasta', 'test_format.fasta')
119 self.assertEqual(get_read_type(self.output_file), 'fasta')
120
121 def test_explicit_format_fasta_to_fasta_gz(self):
122 self.run_command('porechop -i IN -o OUT --format fasta.gz', 'test_format.fasta')
123 self.assertEqual(get_read_type(self.output_file), 'fasta.gz')
124
125 # For these tests the output filename conflicts with the requested format. The requested format
126 # should be followed, not the filename.
127
128 def test_explicit_format_fastq_to_fastq_conflicting_name_1(self):
129 self.run_command('porechop -i IN -o OUT.fasta --format fastq', 'test_format.fastq')
130 self.assertEqual(get_read_type(self.output_file), 'fastq')
131
132 def test_explicit_format_fastq_to_fastq_conflicting_name_2(self):
133 self.run_command('porechop -i IN -o OUT.fasta.gz --format fastq', 'test_format.fastq.gz')
134 self.assertEqual(get_read_type(self.output_file), 'fastq')
135
136 def test_explicit_format_fastq_to_fasta_gz_conflicting_name_1(self):
137 self.run_command('porechop -i IN -o OUT.fastq --format fasta.gz', 'test_format.fastq')
138 self.assertEqual(get_read_type(self.output_file), 'fasta.gz')
139
140 def test_explicit_format_fastq_to_fasta_gz_conflicting_name_2(self):
141 self.run_command('porechop -i IN -o OUT.fastq.gz --format fasta.gz', 'test_format.fasta')
142 self.assertEqual(get_read_type(self.output_file), 'fasta.gz')
143
144
145 class TestOutputFormatBarcodes(unittest.TestCase):
146
147 def run_command(self, command, input_filename):
148 runner_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'porechop-runner.py')
149 input_path = os.path.join(os.path.dirname(__file__), input_filename)
150 command = command.replace('porechop', runner_path)
151 command = command.replace('IN', input_path)
152 self.output_dir = 'TEMP_' + str(os.getpid())
153 command = command.replace('B_DIR', self.output_dir)
154 p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
155 out, err = p.communicate()
156 return out.decode(), err.decode()
157
158 def tearDown(self):
159 if os.path.isdir(self.output_dir):
160 shutil.rmtree(self.output_dir)
161
162 def check_output_file_formats(self, output_files, expected_format):
163 for output_file in [os.path.join(self.output_dir, x) for x in output_files]:
164 self.assertTrue(os.path.isfile(output_file))
165 self.assertEqual(get_read_type(output_file), expected_format)
166
167 # The following tests use the auto format and should determine the output type using the input
168 # type.
169
170 def test_auto_format_fastq_to_fastq(self):
171 self.run_command('porechop -i IN -b B_DIR', 'test_format_barcodes.fastq')
172 self.check_output_file_formats(['BC01.fastq', 'BC02.fastq', 'BC03.fastq', 'none.fastq'],
173 'fastq')
174
175 def test_auto_format_fastq_gz_to_fastq_gz(self):
176 self.run_command('porechop -i IN -b B_DIR', 'test_format_barcodes.fastq.gz')
177 self.check_output_file_formats(['BC01.fastq.gz', 'BC02.fastq.gz', 'BC03.fastq.gz',
178 'none.fastq.gz'], 'fastq.gz')
179
180 def test_auto_format_fasta_to_fasta(self):
181 self.run_command('porechop -i IN -b B_DIR', 'test_format_barcodes.fasta')
182 self.check_output_file_formats(['BC01.fasta', 'BC02.fasta', 'BC03.fasta', 'none.fasta'],
183 'fasta')
184
185 def test_auto_format_fasta_gz_to_fasta_gz(self):
186 self.run_command('porechop -i IN -b B_DIR', 'test_format_barcodes.fasta.gz')
187 self.check_output_file_formats(['BC01.fasta.gz', 'BC02.fasta.gz', 'BC03.fasta.gz',
188 'none.fasta.gz'], 'fasta.gz')
189
190 # The following tests explicitly set the output format.
191
192 def test_explicit_format_fastq_to_fastq(self):
193 self.run_command('porechop -i IN -b B_DIR --format fastq', 'test_format_barcodes.fastq')
194 self.check_output_file_formats(['BC01.fastq', 'BC02.fastq', 'BC03.fastq', 'none.fastq'],
195 'fastq')
196
197 def test_explicit_format_fastq_to_fastq_gz(self):
198 self.run_command('porechop -i IN -b B_DIR --format fastq.gz', 'test_format_barcodes.fastq')
199 self.check_output_file_formats(['BC01.fastq.gz', 'BC02.fastq.gz', 'BC03.fastq.gz',
200 'none.fastq.gz'], 'fastq.gz')
201
202 def test_explicit_format_fastq_gz_to_fastq(self):
203 self.run_command('porechop -i IN -b B_DIR --format fastq', 'test_format_barcodes.fastq.gz')
204 self.check_output_file_formats(['BC01.fastq', 'BC02.fastq', 'BC03.fastq', 'none.fastq'],
205 'fastq')
206
207 def test_explicit_format_fastq_gz_to_fastq_gz(self):
208 self.run_command('porechop -i IN -b B_DIR --format fastq.gz',
209 'test_format_barcodes.fastq.gz')
210 self.check_output_file_formats(['BC01.fastq.gz', 'BC02.fastq.gz', 'BC03.fastq.gz',
211 'none.fastq.gz'], 'fastq.gz')
212
213 def test_explicit_format_fastq_to_fasta(self):
214 self.run_command('porechop -i IN -b B_DIR --format fasta', 'test_format_barcodes.fastq')
215 self.check_output_file_formats(['BC01.fasta', 'BC02.fasta', 'BC03.fasta', 'none.fasta'],
216 'fasta')
217
218 def test_explicit_format_fastq_to_fasta_gz(self):
219 self.run_command('porechop -i IN -b B_DIR --format fasta.gz', 'test_format_barcodes.fastq')
220 self.check_output_file_formats(['BC01.fasta.gz', 'BC02.fasta.gz', 'BC03.fasta.gz',
221 'none.fasta.gz'], 'fasta.gz')
222
223 def test_explicit_format_fasta_to_fasta(self):
224 self.run_command('porechop -i IN -b B_DIR --format fasta', 'test_format_barcodes.fasta')
225 self.check_output_file_formats(['BC01.fasta', 'BC02.fasta', 'BC03.fasta', 'none.fasta'],
226 'fasta')
227
228 def test_explicit_format_fasta_to_fasta_gz(self):
229 self.run_command('porechop -i IN -b B_DIR --format fasta.gz', 'test_format_barcodes.fasta')
230 self.check_output_file_formats(['BC01.fasta.gz', 'BC02.fasta.gz', 'BC03.fasta.gz',
231 'none.fasta.gz'], 'fasta.gz')
232
233 def test_explicit_format_fasta_gz_to_fasta(self):
234 self.run_command('porechop -i IN -b B_DIR --format fasta', 'test_format_barcodes.fasta.gz')
235 self.check_output_file_formats(['BC01.fasta', 'BC02.fasta', 'BC03.fasta', 'none.fasta'],
236 'fasta')
237
238 def test_explicit_format_fasta_gz_to_fasta_gz(self):
239 self.run_command('porechop -i IN -b B_DIR --format fasta.gz',
240 'test_format_barcodes.fasta.gz')
241 self.check_output_file_formats(['BC01.fasta.gz', 'BC02.fasta.gz', 'BC03.fasta.gz',
242 'none.fasta.gz'], 'fasta.gz')