Debian Codegraph @

test/test_albacore_directory.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 shutil
20 import subprocess
21 import porechop.misc
22
23
24 class TestAlbacoreDirectory(unittest.TestCase):
25 """
26 Tests barcode demultiplexing from an Albacore output directory.
27 """
28 def run_command(self, command, input_path):
29 runner_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'porechop-runner.py')
30 input_path = os.path.join(os.path.dirname(__file__), input_path)
31 command = command.replace('porechop', runner_path)
32 command = command.replace('INPUT', input_path)
33 self.output_dir = 'TEMP_' + str(os.getpid())
34 command = command.replace('BARCODE_DIR', self.output_dir)
35 p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
36 out, err = p.communicate()
37 return out.decode(), err.decode()
38
39 def tearDown(self):
40 if os.path.isdir(self.output_dir):
41 shutil.rmtree(self.output_dir)
42
43 def load_trimmed_reads(self, filename):
44 full_filepath = os.path.join(self.output_dir, filename)
45 trimmed_reads, read_type = porechop.misc.load_fasta_or_fastq(full_filepath)
46 if read_type == 'FASTA':
47 trimmed_reads = [(x[2], x[1], '') for x in trimmed_reads]
48 else: # FASTQ
49 trimmed_reads = [(x[4], x[1], x[3]) for x in trimmed_reads]
50 return trimmed_reads
51
52 def test_albacore_directory_1(self):
53 """
54 When just the barcode01 reads are given as a single file, Porechop doesn't treat it as
55 Albacore output.
56 """
57 out, _ = self.run_command('porechop -i INPUT -b BARCODE_DIR',
58 'test_albacore_directory/workspace/barcode01/' +
59 'fastq_runid_e1df3d311cc267665db81df4aa863d073b0494ae_0.fastq')
60
61 bc01_trimmed_reads = self.load_trimmed_reads('BC01.fastq')
62 bc03_trimmed_reads = self.load_trimmed_reads('BC03.fastq')
63 none_trimmed_reads = self.load_trimmed_reads('none.fastq')
64
65 bc01_read_names = sorted(x[0] for x in bc01_trimmed_reads)
66 bc03_read_names = sorted(x[0] for x in bc03_trimmed_reads)
67 none_read_names = sorted(x[0] for x in none_trimmed_reads)
68
69 self.assertEqual(bc01_read_names, ['432342b4-d6fb-4915-96ca-1b4a570779cf',
70 'a4a5afa1-747e-47ea-abb9-c54029c315ec',
71 'd82a6ff9-99cf-4f23-9246-cedfc6e8c88b'])
72 self.assertEqual(bc03_read_names, ['b8db6753-a98a-40c3-8f80-aadedcf4d1e4'])
73 self.assertEqual(none_read_names, ['04a00c9c-4a3a-4ba6-8161-ba01d9a77f22',
74 '9dc8c47d-e574-497b-b24d-5cf8f09cb189',
75 'ba8d33e4-7b53-42e8-8e6f-1d017698a21e',
76 'f38d7656-735d-4be9-92ce-94a568bada56',
77 'ffa40714-6027-4ebc-9f49-ecde4f6511f3'])
78
79 def test_albacore_directory_2(self):
80 """
81 When just the barcode02 reads are given as a single file, Porechop doesn't treat it as
82 Albacore output.
83 """
84 out, _ = self.run_command('porechop -i INPUT -b BARCODE_DIR',
85 'test_albacore_directory/workspace/barcode02/' +
86 'fastq_runid_e1df3d311cc267665db81df4aa863d073b0494ae_0.fastq')
87
88 bc02_trimmed_reads = self.load_trimmed_reads('BC02.fastq')
89 bc03_trimmed_reads = self.load_trimmed_reads('BC03.fastq')
90 none_trimmed_reads = self.load_trimmed_reads('none.fastq')
91
92 bc02_read_names = sorted(x[0] for x in bc02_trimmed_reads)
93 bc03_read_names = sorted(x[0] for x in bc03_trimmed_reads)
94 none_read_names = sorted(x[0] for x in none_trimmed_reads)
95
96 self.assertEqual(bc02_read_names, ['15c8df94-6795-451b-8f71-bc32580068f2',
97 '28e41a6a-25db-4023-a27e-f3ffb59f238a',
98 '4dd6464b-5cea-4f3a-8f26-740983ca2a57'])
99 self.assertEqual(bc03_read_names, ['ca5efc50-1e67-4df4-95e9-afe4a5dbf35c'])
100 self.assertEqual(none_read_names, ['1c25347a-7a36-4f3a-a5a2-5339ae585d4a',
101 '5b0c7ed8-c292-4948-a235-c3644a15855b',
102 'de7b3819-a55c-4c6c-88ea-383de089ebab'])
103
104 def test_albacore_directory_3(self):
105 """
106 When just the barcode03 reads are given as a single file, Porechop doesn't treat it as
107 Albacore output.
108 """
109 out, _ = self.run_command('porechop -i INPUT -b BARCODE_DIR',
110 'test_albacore_directory/workspace/barcode03/' +
111 'fastq_runid_e1df3d311cc267665db81df4aa863d073b0494ae_0.fastq')
112
113 bc01_trimmed_reads = self.load_trimmed_reads('BC01.fastq')
114 bc03_trimmed_reads = self.load_trimmed_reads('BC03.fastq')
115 none_trimmed_reads = self.load_trimmed_reads('none.fastq')
116
117 bc01_read_names = sorted(x[0] for x in bc01_trimmed_reads)
118 bc03_read_names = sorted(x[0] for x in bc03_trimmed_reads)
119 none_read_names = sorted(x[0] for x in none_trimmed_reads)
120
121 self.assertEqual(bc01_read_names, ['283ab5e9-492e-4b28-a200-0362b4755ced'])
122 self.assertEqual(bc03_read_names, ['47277a55-611f-415e-b0d4-3d3326e0e125',
123 '73d0e477-fbfe-49c2-abc9-4974dd765bc8',
124 'd75205b6-be37-4a5c-bed3-d6b218e7579b'])
125 self.assertEqual(none_read_names, ['618187f9-2f66-4bf0-a1b6-e735b7024e86',
126 '6f612e35-8174-4dc9-a239-503b10915267',
127 'd30719ed-c801-48db-939c-98b10137b414',
128 'fb5162ca-4645-4a0d-9336-3aa5fdf18d74'])
129
130 def test_albacore_directory_unclassified(self):
131 """
132 When just the unclassified reads are given as a single file, Porechop doesn't treat it as
133 Albacore output.
134 """
135 out, _ = self.run_command('porechop -i INPUT -b BARCODE_DIR',
136 'test_albacore_directory/workspace/unclassified/' +
137 'fastq_runid_e1df3d311cc267665db81df4aa863d073b0494ae_0.fastq')
138
139 bc01_trimmed_reads = self.load_trimmed_reads('BC01.fastq')
140 bc02_trimmed_reads = self.load_trimmed_reads('BC02.fastq')
141 bc03_trimmed_reads = self.load_trimmed_reads('BC03.fastq')
142 none_trimmed_reads = self.load_trimmed_reads('none.fastq')
143
144 bc01_read_names = sorted(x[0] for x in bc01_trimmed_reads)
145 bc02_read_names = sorted(x[0] for x in bc02_trimmed_reads)
146 bc03_read_names = sorted(x[0] for x in bc03_trimmed_reads)
147 none_read_names = sorted(x[0] for x in none_trimmed_reads)
148
149 self.assertEqual(bc01_read_names, ['a7ffe707-2733-43b9-a9df-950566189723'])
150 self.assertEqual(bc02_read_names, ['111fc53c-1941-4c5b-82e2-c80dc0962bd7',
151 '50e78d16-e5f9-4b77-8ce4-dde6fc6eea39'])
152 self.assertEqual(bc03_read_names, ['7557893f-a3a1-4294-96b7-89f9e9d4470c'])
153 self.assertEqual(none_read_names, ['4d1ff7d1-b2f1-4ecc-8990-09edf0aa1970',
154 '5e656a69-bd50-45c9-8177-a2aa38d0b04f',
155 '7680810e-db1f-4cde-b442-642d7ac024ca'])
156
157 def test_albacore_directory_all(self):
158 """
159 When the whole directory is given as input, it's treated as an Albacore directory and reads
160 where Porechop and Albacore disagree are put into 'none'.
161 """
162 out, _ = self.run_command('porechop -i INPUT -b BARCODE_DIR', 'test_albacore_directory')
163
164 bc01_trimmed_reads = self.load_trimmed_reads('BC01.fastq')
165 bc02_trimmed_reads = self.load_trimmed_reads('BC02.fastq')
166 bc03_trimmed_reads = self.load_trimmed_reads('BC03.fastq')
167 none_trimmed_reads = self.load_trimmed_reads('none.fastq')
168
169 bc01_read_names = sorted(x[0] for x in bc01_trimmed_reads)
170 bc02_read_names = sorted(x[0] for x in bc02_trimmed_reads)
171 bc03_read_names = sorted(x[0] for x in bc03_trimmed_reads)
172 none_read_names = sorted(x[0] for x in none_trimmed_reads)
173
174 self.assertEqual(bc01_read_names, ['432342b4-d6fb-4915-96ca-1b4a570779cf',
175 'a4a5afa1-747e-47ea-abb9-c54029c315ec',
176 'd82a6ff9-99cf-4f23-9246-cedfc6e8c88b'])
177 self.assertEqual(bc02_read_names, ['15c8df94-6795-451b-8f71-bc32580068f2',
178 '28e41a6a-25db-4023-a27e-f3ffb59f238a',
179 '4dd6464b-5cea-4f3a-8f26-740983ca2a57'])
180 self.assertEqual(bc03_read_names, ['47277a55-611f-415e-b0d4-3d3326e0e125',
181 '73d0e477-fbfe-49c2-abc9-4974dd765bc8',
182 'd75205b6-be37-4a5c-bed3-d6b218e7579b'])
183 self.assertEqual(none_read_names, ['04a00c9c-4a3a-4ba6-8161-ba01d9a77f22',
184 '111fc53c-1941-4c5b-82e2-c80dc0962bd7',
185 '1c25347a-7a36-4f3a-a5a2-5339ae585d4a',
186 '283ab5e9-492e-4b28-a200-0362b4755ced',
187 '4d1ff7d1-b2f1-4ecc-8990-09edf0aa1970',
188 '50e78d16-e5f9-4b77-8ce4-dde6fc6eea39',
189 '5b0c7ed8-c292-4948-a235-c3644a15855b',
190 '5e656a69-bd50-45c9-8177-a2aa38d0b04f',
191 '618187f9-2f66-4bf0-a1b6-e735b7024e86',
192 '6f612e35-8174-4dc9-a239-503b10915267',
193 '7557893f-a3a1-4294-96b7-89f9e9d4470c',
194 '7680810e-db1f-4cde-b442-642d7ac024ca',
195 '9dc8c47d-e574-497b-b24d-5cf8f09cb189',
196 'a7ffe707-2733-43b9-a9df-950566189723',
197 'b8db6753-a98a-40c3-8f80-aadedcf4d1e4',
198 'ba8d33e4-7b53-42e8-8e6f-1d017698a21e',
199 'ca5efc50-1e67-4df4-95e9-afe4a5dbf35c',
200 'd30719ed-c801-48db-939c-98b10137b414',
201 'de7b3819-a55c-4c6c-88ea-383de089ebab',
202 'f38d7656-735d-4be9-92ce-94a568bada56',
203 'fb5162ca-4645-4a0d-9336-3aa5fdf18d74',
204 'ffa40714-6027-4ebc-9f49-ecde4f6511f3'])