Debian Codegraph @

test/test_two_adapter_sets.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 porechop.misc
21
22
23 class TestTwoAdapterSets(unittest.TestCase):
24 """
25 This test set contains a combination of two adapter sets: SQK-MAP006 and SQK-NSK007
26 """
27 def run_command(self, command):
28 runner_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'porechop-runner.py')
29 input_path = os.path.join(os.path.dirname(__file__), 'test_two_adapter_sets.fastq')
30 command = command.replace('porechop', runner_path)
31 command = command.replace('INPUT', input_path)
32 output_name = 'TEMP_' + str(os.getpid())
33 command = command.replace('OUTPUT', output_name)
34 try:
35 self.output_file = [x for x in command.split() if output_name in x][0]
36 except IndexError:
37 self.output_file = ''
38 p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
39 out, err = p.communicate()
40 return out.decode(), err.decode()
41
42 def tearDown(self):
43 if os.path.isfile(self.output_file):
44 os.remove(self.output_file)
45
46 def load_trimmed_reads(self):
47 trimmed_reads, read_type = porechop.misc.load_fasta_or_fastq(self.output_file)
48 if read_type == 'FASTA':
49 trimmed_reads = [(x[2], x[1], '') for x in trimmed_reads]
50 else: # FASTQ
51 trimmed_reads = [(x[4], x[1], x[3]) for x in trimmed_reads]
52 return trimmed_reads, read_type
53
54 def check_trimmed_reads(self):
55 """
56 This function checks all of the trimmed reads (assuming default settings).
57 """
58 trimmed_reads, read_type = self.load_trimmed_reads()
59
60 # Read 1 isn't trimmed.
61 read_1 = trimmed_reads[0]
62 self.assertEqual(read_1[0], '1')
63 self.assertEqual(len(read_1[1]), 10000)
64 self.assertTrue(read_1[1].startswith('GCGGAAGTCA'))
65 self.assertTrue(read_1[1].endswith('TCCAGCCCCA'))
66 if read_type == 'FASTQ':
67 self.assertEqual(len(read_1[2]), 10000)
68 self.assertTrue(read_1[2].startswith("4,)4+311.'"))
69 self.assertTrue(read_1[2].endswith("3-16('...,"))
70
71 # Read 2 has an adapter trimmed from the start.
72 read_2 = trimmed_reads[1]
73 self.assertEqual(read_2[0], '2')
74 self.assertEqual(len(read_2[1]), 10970)
75 self.assertTrue(read_2[1].startswith('TGCAGCACAA'))
76 self.assertTrue(read_2[1].endswith('AAAGAACTGG'))
77 if read_type == 'FASTQ':
78 self.assertEqual(len(read_2[2]), 10970)
79 self.assertTrue(read_2[2].startswith("&)*-++$+,."))
80 self.assertTrue(read_2[2].endswith("(*'%)%.2+)"))
81
82 # Read 3 has an adapter trimmed from the start.
83 read_3 = trimmed_reads[2]
84 self.assertEqual(read_3[0], '3')
85 self.assertEqual(len(read_3[1]), 7970)
86 self.assertTrue(read_3[1].startswith('TATTTCGCTT'))
87 self.assertTrue(read_3[1].endswith('GAGAGTGTGA'))
88 if read_type == 'FASTQ':
89 self.assertEqual(len(read_3[2]), 7970)
90 self.assertTrue(read_3[2].startswith("2:2.4%%%)$"))
91 self.assertTrue(read_3[2].endswith("/-((+1-/%."))
92
93 # Read 4 has two internal adapters (from different sets).
94 read_4_1 = trimmed_reads[3]
95 read_4_2 = trimmed_reads[4]
96 read_4_3 = trimmed_reads[5]
97 self.assertEqual(read_4_1[0], '4_1')
98 self.assertEqual(read_4_2[0], '4_2')
99 self.assertEqual(read_4_3[0], '4_3')
100 self.assertEqual(len(read_4_1[1]), 1990)
101 self.assertEqual(len(read_4_2[1]), 2868)
102 self.assertEqual(len(read_4_3[1]), 1878)
103 self.assertTrue(read_4_1[1].startswith('TCATGACAGT'))
104 self.assertTrue(read_4_1[1].endswith('CGAAGTAGGG'))
105 self.assertTrue(read_4_2[1].startswith('ACGTGCTCGA'))
106 self.assertTrue(read_4_2[1].endswith('CCTTCCTAGA'))
107 self.assertTrue(read_4_3[1].startswith('GGCTCCCCCC'))
108 self.assertTrue(read_4_3[1].endswith('AGTTCTCGTG'))
109 if read_type == 'FASTQ':
110 self.assertEqual(len(read_4_1[2]), 1990)
111 self.assertEqual(len(read_4_2[2]), 2868)
112 self.assertEqual(len(read_4_3[2]), 1878)
113 self.assertTrue(read_4_1[2].startswith("45.2*25-',"))
114 self.assertTrue(read_4_1[2].endswith("$**+,,&$%3"))
115 self.assertTrue(read_4_2[2].startswith("]213438::5"))
116 self.assertTrue(read_4_2[2].endswith(".3/4:23]/]"))
117 self.assertTrue(read_4_3[2].startswith("0430/6202,"))
118 self.assertTrue(read_4_3[2].endswith("+2+,853322"))
119
120 return read_type
121
122 def test_results_normal_fastq(self):
123 self.run_command('porechop -i INPUT -o OUTPUT.fastq')
124 read_type = self.check_trimmed_reads()
125 self.assertEqual(read_type, 'FASTQ')
126 self.assertEqual(porechop.misc.get_compression_type(self.output_file), 'plain')
127
128 def test_results_gz_fastq(self):
129 self.run_command('porechop -i INPUT -o OUTPUT.fastq.gz')
130 read_type = self.check_trimmed_reads()
131 self.assertEqual(read_type, 'FASTQ')
132 self.assertEqual(porechop.misc.get_compression_type(self.output_file), 'gz')
133
134 def test_results_piped_fastq(self):
135 self.run_command('porechop -i INPUT > OUTPUT.fastq')
136 read_type = self.check_trimmed_reads()
137 self.assertEqual(read_type, 'FASTQ')
138 self.assertEqual(porechop.misc.get_compression_type(self.output_file), 'plain')
139
140 def test_check_reads_1(self):
141 """
142 When only one read is checked, no adapters are found and nothing is trimmed.
143 """
144 out, _ = self.run_command('porechop -i INPUT -o OUTPUT.fastq --check_reads 1')
145 self.assertTrue('No adapters found' in out)
146 trimmed_reads, read_type = self.load_trimmed_reads()
147 read_1 = [x for x in trimmed_reads if x[0] == '1'][0]
148 self.assertEqual(len(read_1[1]), 10000)
149 read_2 = [x for x in trimmed_reads if x[0] == '2'][0]
150 self.assertEqual(len(read_2[1]), 11000)
151 read_3 = [x for x in trimmed_reads if x[0] == '3'][0]
152 self.assertEqual(len(read_3[1]), 8000)
153
154 def test_check_reads_2(self):
155 """
156 When only two reads are checked, only one adapter set is found.
157 """
158 out, _ = self.run_command('porechop -i INPUT -o OUTPUT.fastq --check_reads 2')
159 self.assertTrue('Trimming adapters' in out)
160 self.assertTrue('SQK-MAP006_Y_Top_SK63:' in out)
161 self.assertTrue('SQK-MAP006_Y_Bottom_SK64:' in out)
162 trimmed_reads, read_type = self.load_trimmed_reads()
163 read_1 = [x for x in trimmed_reads if x[0] == '1'][0]
164 self.assertEqual(len(read_1[1]), 10000)
165 read_2 = [x for x in trimmed_reads if x[0] == '2'][0]
166 self.assertEqual(len(read_2[1]), 10970)
167 read_3 = [x for x in trimmed_reads if x[0] == '3'][0]
168 self.assertEqual(len(read_3[1]), 8000)
169
170 def test_check_reads_3(self):
171 """
172 When three reads are checked, both adapter sets are found.
173 """
174 out, _ = self.run_command('porechop -i INPUT -o OUTPUT.fastq --check_reads 3')
175 self.assertTrue('Trimming adapters' in out)
176 self.assertTrue('SQK-MAP006_Y_Top_SK63:' in out)
177 self.assertTrue('SQK-MAP006_Y_Bottom_SK64:' in out)
178 self.assertTrue('SQK-NSK007_Y_Top:' in out)
179 self.assertTrue('SQK-NSK007_Y_Bottom:' in out)
180 trimmed_reads, read_type = self.load_trimmed_reads()
181 read_1 = [x for x in trimmed_reads if x[0] == '1'][0]
182 self.assertEqual(len(read_1[1]), 10000)
183 read_2 = [x for x in trimmed_reads if x[0] == '2'][0]
184 self.assertEqual(len(read_2[1]), 10970)
185 read_3 = [x for x in trimmed_reads if x[0] == '3'][0]
186 self.assertEqual(len(read_3[1]), 7970)