Debian Codegraph @

test/test_choose_barcodes.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 TestBarcodes(unittest.TestCase):
25 """
26 Tests barcode demultiplexing where there are conflicting forward and reverse barcodes. In such
27 a case, Porechop should choose the more popular option.
28 """
29 def run_command(self, command, input_path):
30 runner_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'porechop-runner.py')
31 input_path = os.path.join(os.path.dirname(__file__), input_path)
32 command = command.replace('porechop', runner_path)
33 command = command.replace('INPUT', input_path)
34 self.output_dir = 'TEMP_' + str(os.getpid())
35 command = command.replace('BARCODE_DIR', self.output_dir)
36 p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
37 out, err = p.communicate()
38 return out.decode(), err.decode()
39
40 def tearDown(self):
41 if os.path.isdir(self.output_dir):
42 shutil.rmtree(self.output_dir)
43
44 def load_trimmed_reads(self, filename):
45 full_filepath = os.path.join(self.output_dir, filename)
46 trimmed_reads, read_type = porechop.misc.load_fasta_or_fastq(full_filepath)
47 if read_type == 'FASTA':
48 trimmed_reads = [(x[2], x[1], '') for x in trimmed_reads]
49 else: # FASTQ
50 trimmed_reads = [(x[4], x[1], x[3]) for x in trimmed_reads]
51 return trimmed_reads
52
53 def test_choose_forward(self):
54 """
55 In this test, forward barcodes are more common and reverse barcodes should be ignored.
56 """
57 out, _ = self.run_command('porechop -i INPUT -b BARCODE_DIR',
58 'test_choose_barcodes_1.fasta')
59
60 bc01_trimmed_reads = self.load_trimmed_reads('BC01.fasta')
61 bc02_trimmed_reads = self.load_trimmed_reads('BC02.fasta')
62 bc03_trimmed_reads = self.load_trimmed_reads('BC03.fasta')
63 bc04_trimmed_reads = self.load_trimmed_reads('BC04.fasta')
64 bc05_trimmed_reads = self.load_trimmed_reads('BC05.fasta')
65 bc06_trimmed_reads = self.load_trimmed_reads('BC06.fasta')
66 none_trimmed_reads = self.load_trimmed_reads('none.fasta')
67
68 bc01_read_names = sorted(x[0] for x in bc01_trimmed_reads)
69 bc02_read_names = sorted(x[0] for x in bc02_trimmed_reads)
70 bc03_read_names = sorted(x[0] for x in bc03_trimmed_reads)
71 bc04_read_names = sorted(x[0] for x in bc04_trimmed_reads)
72 bc05_read_names = sorted(x[0] for x in bc05_trimmed_reads)
73 bc06_read_names = sorted(x[0] for x in bc06_trimmed_reads)
74 none_read_names = sorted(x[0] for x in none_trimmed_reads)
75
76 self.assertEqual(bc01_read_names, ['1'])
77 self.assertEqual(bc02_read_names, ['2'])
78 self.assertEqual(bc03_read_names, ['3'])
79 self.assertEqual(bc04_read_names, ['4'])
80 self.assertEqual(bc05_read_names, ['5'])
81 self.assertEqual(bc06_read_names, ['6'])
82 self.assertEqual(none_read_names, ['7', '8'])
83
84 self.assertTrue('Barcodes determined to be in forward orientation' in out)
85 self.assertTrue('0 / 8 reads were discarded based on middle adapters' in out)
86
87 def test_choose_reverse(self):
88 """
89 In this test, reverse barcodes are more common and forward barcodes should be ignored.
90 """
91 out, _ = self.run_command('porechop -i INPUT -b BARCODE_DIR',
92 'test_choose_barcodes_2.fasta')
93
94 bc01_trimmed_reads = self.load_trimmed_reads('BC01.fasta')
95 bc02_trimmed_reads = self.load_trimmed_reads('BC02.fasta')
96 bc03_trimmed_reads = self.load_trimmed_reads('BC03.fasta')
97 bc04_trimmed_reads = self.load_trimmed_reads('BC04.fasta')
98 bc05_trimmed_reads = self.load_trimmed_reads('BC05.fasta')
99 bc06_trimmed_reads = self.load_trimmed_reads('BC06.fasta')
100 none_trimmed_reads = self.load_trimmed_reads('none.fasta')
101
102 bc01_read_names = sorted(x[0] for x in bc01_trimmed_reads)
103 bc02_read_names = sorted(x[0] for x in bc02_trimmed_reads)
104 bc03_read_names = sorted(x[0] for x in bc03_trimmed_reads)
105 bc04_read_names = sorted(x[0] for x in bc04_trimmed_reads)
106 bc05_read_names = sorted(x[0] for x in bc05_trimmed_reads)
107 bc06_read_names = sorted(x[0] for x in bc06_trimmed_reads)
108 none_read_names = sorted(x[0] for x in none_trimmed_reads)
109
110 self.assertEqual(bc01_read_names, ['1'])
111 self.assertEqual(bc02_read_names, ['2'])
112 self.assertEqual(bc03_read_names, ['3'])
113 self.assertEqual(bc04_read_names, ['4'])
114 self.assertEqual(bc05_read_names, ['5'])
115 self.assertEqual(bc06_read_names, ['6'])
116 self.assertEqual(none_read_names, ['7', '8'])
117
118 self.assertTrue('Barcodes determined to be in reverse orientation' in out)
119 self.assertTrue('0 / 8 reads were discarded based on middle adapters' in out)