Debian Codegraph @

ariba/tests/summary_test.py

raw
1 import unittest
2 import filecmp
3 import os
4 from ariba import summary, summary_sample
5
6 modules_dir = os.path.dirname(os.path.abspath(summary.__file__))
7 data_dir = os.path.join(modules_dir, 'tests', 'data')
8
9 class TestSummary(unittest.TestCase):
10 def test_init(self):
11 '''Test init'''
12 fofn = os.path.join(data_dir, 'summary_test_init.fofn')
13 s = summary.Summary('out', fofn=fofn)
14 self.assertEqual(s.filenames, {'file1': None, 'file2': None})
15 s = summary.Summary('out', filenames={'file42': None})
16 self.assertEqual(s.filenames, {'file42': None})
17 s = summary.Summary('out', fofn=fofn, filenames={'file42': None})
18 self.assertEqual(s.filenames, {'file42': None, 'file1': None, 'file2': None})
19
20
21 def test_determine_cluster_cols(self):
22 col_strings = [
23 'assembled,match,ref_seq,pct_id,ctg_cov,known_var,novel_var',
24 'assembled,match,ref_seq,pct_id,known_var,novel_var',
25 'ref_seq,pct_id,known_var,novel_var',
26 'assembled,pct_id,known_var,novel_var',
27 'assembled',
28 '',
29 None,
30 ]
31
32 expected = [
33 {'assembled': True, 'match': True, 'ref_seq': True, 'pct_id': True, 'ctg_cov': True, 'known_var': True, 'novel_var': True},
34 {'assembled': True, 'match': True, 'ref_seq': True, 'pct_id': True, 'ctg_cov': False, 'known_var': True, 'novel_var': True},
35 {'assembled': False, 'match': False, 'ref_seq': True, 'pct_id': True, 'ctg_cov': False, 'known_var': True, 'novel_var': True},
36 {'assembled': True, 'match': False, 'ref_seq': False, 'pct_id': True, 'ctg_cov': False, 'known_var': True, 'novel_var': True},
37 {'assembled': True, 'match': False, 'ref_seq': False, 'pct_id': False, 'ctg_cov': False, 'known_var': False, 'novel_var': False},
38 {'assembled': False, 'match': False, 'ref_seq': False, 'pct_id': False, 'ctg_cov': False, 'known_var': False, 'novel_var': False},
39 {'assembled': False, 'match': False, 'ref_seq': False, 'pct_id': False, 'ctg_cov': False, 'known_var': False, 'novel_var': False},
40 ]
41
42 assert len(col_strings) == len(expected)
43
44 for i in range(len(col_strings)):
45 self.assertEqual(expected[i], summary.Summary._determine_cluster_cols(col_strings[i]))
46
47
48 def test_load_fofn(self):
49 '''Test _load_fofn'''
50 infile = os.path.join(data_dir, 'summary_test_load_fofn')
51 got = summary.Summary._load_fofn(infile)
52 expected = {
53 '/foo/bar/abc.tsv': None,
54 '/spam/eggs/file1.tsv': 'short_name1',
55 '/spam/eggs/file2.tsv': 'short_name2',
56 '/spam/eggs/file3.tsv': None
57 }
58 self.assertEqual(expected, got)
59
60
61 def test_load_input_files(self):
62 '''Test _load_input_files'''
63 file1 = os.path.join(data_dir, 'summary_test_load_input_files.1.tsv')
64 file2 = os.path.join(data_dir, 'summary_test_load_input_files.2.tsv')
65 sample1 = summary_sample.SummarySample(file1)
66 sample2 = summary_sample.SummarySample(file2)
67 sample1.run()
68 sample2.run()
69 got = summary.Summary._load_input_files([file1, file2], 90)
70 expected = {file1: sample1, file2: sample2}
71 self.assertEqual(expected, got)
72
73 sample1 = summary_sample.SummarySample(file1, only_clusters={'noncoding1'})
74 sample2 = summary_sample.SummarySample(file2, only_clusters={'noncoding1'})
75 sample1.run()
76 sample2.run()
77 expected = {file1: sample1, file2: sample2}
78 got = summary.Summary._load_input_files([file1, file2], 90, only_clusters={'noncoding1'})
79 self.assertEqual(expected, got)
80
81
82 def test_gather_unfiltered_output_data(self):
83 '''test gather_unfiltered_output_data'''
84 infiles = [
85 os.path.join(data_dir, 'summary_gather_unfiltered_output_data.in.1.tsv'),
86 os.path.join(data_dir, 'summary_gather_unfiltered_output_data.in.2.tsv')
87 ]
88
89 expected_all = {
90 infiles[0]: {
91 'noncoding1': {
92 'summary': {
93 'assembled': 'yes',
94 'known_var': 'yes',
95 'match': 'yes',
96 'novel_var': 'no',
97 'pct_id': '98.33',
98 'ctg_cov': '10.0',
99 'ref_seq': 'noncoding_ref1'
100 },
101 'groups': {},
102 'vars': {},
103 },
104 'noncoding2': {
105 'summary': {
106 'assembled': 'yes',
107 'known_var': 'yes',
108 'match': 'yes',
109 'novel_var': 'no',
110 'pct_id': '98.33',
111 'ctg_cov': '10.0',
112 'ref_seq': 'noncoding_ref2'
113 },
114 'groups': {},
115 'vars': {},
116 },
117 'presence_absence1': {
118 'summary': {
119 'assembled': 'yes',
120 'known_var': 'no',
121 'match': 'yes',
122 'novel_var': 'yes',
123 'pct_id': '98.96',
124 'ctg_cov': '20.1',
125 'ref_seq': 'presence_absence_ref1'
126 },
127 'groups': {},
128 'vars': {},
129 },
130 'presence_absence2': {
131 'summary': {
132 'assembled': 'partial',
133 'known_var': 'no',
134 'match': 'no',
135 'novel_var': 'yes',
136 'pct_id': '99.1',
137 'ctg_cov': '22.3',
138 'ref_seq': 'presence_absence_ref2'
139 },
140 'groups': {},
141 'vars': {}
142 }
143 },
144 infiles[1]: {
145 'noncoding1': {
146 'summary': {'assembled': 'yes',
147 'known_var': 'yes',
148 'match': 'yes',
149 'novel_var': 'no',
150 'pct_id': '98.33',
151 'ctg_cov': '50.1',
152 'ref_seq': 'noncoding_ref1'
153 },
154 'groups': {},
155 'vars': {},
156 },
157 'noncoding2': {
158 'summary': {
159 'assembled': 'yes',
160 'known_var': 'yes',
161 'match': 'yes',
162 'novel_var': 'no',
163 'pct_id': '98.33',
164 'ctg_cov': '10.0',
165 'ref_seq': 'noncoding_ref2'
166 },
167 'groups': {},
168 'vars': {},
169 },
170 'presence_absence1': {
171 'summary': {
172 'assembled': 'yes',
173 'known_var': 'no',
174 'match': 'yes',
175 'novel_var': 'yes',
176 'pct_id': '98.96',
177 'ctg_cov': '51.1',
178 'ref_seq': 'presence_absence1'
179 },
180 'groups': {},
181 'vars': {}
182 },
183 }
184 }
185
186 expected_potential_cols = {
187 'noncoding1': {
188 'summary': {
189 'assembled',
190 'known_var',
191 'match',
192 'novel_var',
193 'pct_id',
194 'ctg_cov',
195 'ref_seq'
196 },
197 'groups': set(),
198 'vars': set()
199 },
200 'noncoding2': {
201 'summary': {
202 'assembled',
203 'known_var',
204 'match',
205 'novel_var',
206 'pct_id',
207 'ctg_cov',
208 'ref_seq'
209 },
210 'groups': set(),
211 'vars': set()
212 },
213 'presence_absence1': {
214 'summary': {
215 'assembled',
216 'known_var',
217 'match',
218 'novel_var',
219 'pct_id',
220 'ctg_cov',
221 'ref_seq'
222 },
223 'groups': set(),
224 'vars': set()
225 },
226 'presence_absence2': {
227 'summary': {
228 'assembled',
229 'known_var',
230 'match',
231 'novel_var',
232 'pct_id',
233 'ctg_cov',
234 'ref_seq'
235 },
236 'groups': set(),
237 'vars': set()
238 }
239 }
240
241 self.maxDiff = None
242 s = summary.Summary('out', filenames=infiles)
243 s.samples = summary.Summary._load_input_files(s.filenames, 90)
244 s._gather_unfiltered_output_data()
245 self.assertEqual(expected_potential_cols, s.all_potential_columns)
246 self.assertEqual(expected_all, s.all_data)
247
248 expected_potential_cols['noncoding1']['groups'] = {'id3', 'id1', 'id1.%', 'id3.%'}
249 expected_potential_cols['noncoding2']['groups'] = {'id2.%', 'id2'}
250 expected_all[infiles[0]]['noncoding1']['groups'] = {'id1': 'yes', 'id1.%': 100.0}
251 expected_all[infiles[0]]['noncoding2']['groups'] = {'id2': 'yes_multi_het', 'id2.%': 'NA'}
252 expected_all[infiles[1]]['noncoding1']['groups'] = {'id1': 'het', 'id1.%': 80.0, 'id3': 'yes', 'id3.%': 100.0}
253 expected_all[infiles[1]]['noncoding2']['groups'] = {'id2': 'het', 'id2.%': 40.0}
254 s = summary.Summary('out', filenames=infiles, show_var_groups=True)
255 s.samples = summary.Summary._load_input_files(s.filenames, 90)
256 s._gather_unfiltered_output_data()
257 self.assertEqual(expected_potential_cols, s.all_potential_columns)
258 self.assertEqual(expected_all, s.all_data)
259
260 expected_potential_cols['noncoding1']['vars'] = {'14T', '14T.%', '14GT', '14GT.%', '6G', '6G.%'}
261 expected_potential_cols['noncoding2']['vars'] = {'52GT', '52GT.%', '42T', '42T.%'}
262
263 expected_all[infiles[0]]['noncoding1']['vars'] = {'14T': 'yes', '14T.%': 100.0}
264 expected_all[infiles[0]]['noncoding2']['vars'] = {'42T': 'yes', '42T.%': 100.0, '52GT': 'het', '52GT.%': 40.0}
265 expected_all[infiles[1]]['noncoding1']['vars'] = {'14GT': 'het', '14GT.%': 80.0, '6G': 'yes', '6G.%': 100.0}
266 expected_all[infiles[1]]['noncoding2']['vars'] = {'52GT': 'het', '52GT.%': 40.0}
267 s = summary.Summary('out', filenames=infiles, show_var_groups=True, show_known_vars=True)
268 s.samples = summary.Summary._load_input_files(s.filenames, 90)
269 s._gather_unfiltered_output_data()
270 self.assertEqual(expected_potential_cols, s.all_potential_columns)
271 self.assertEqual(expected_all, s.all_data)
272
273 expected_potential_cols['presence_absence1']['vars'] = {'A10V'}
274 expected_potential_cols['presence_absence2']['vars'] = {'V175L'}
275 expected_all[infiles[0]]['presence_absence1']['vars'] = {'A10V': 'yes'}
276 expected_all[infiles[0]]['presence_absence2']['vars'] = {'V175L': 'yes'}
277 expected_all[infiles[1]]['presence_absence1']['vars'] = {'A10V': 'yes'}
278 s = summary.Summary('out', filenames=infiles, show_var_groups=True, show_known_vars=True, show_novel_vars=True)
279 s.samples = summary.Summary._load_input_files(s.filenames, 90)
280 s._gather_unfiltered_output_data()
281 self.assertEqual(expected_potential_cols, s.all_potential_columns)
282 self.assertEqual(expected_all, s.all_data)
283
284
285 def test_to_matrix_all_cols(self):
286 '''Test _to_matrix all columns'''
287 infiles = [
288 os.path.join(data_dir, 'summary_to_matrix.1.tsv'),
289 os.path.join(data_dir, 'summary_to_matrix.2.tsv')
290 ]
291
292 fofn = 'tmp.summary_to_matrix_all_cols'
293 with open(fofn, 'w') as f:
294 print(infiles[0], 'sample1', file=f)
295 print(infiles[1], file=f)
296
297
298 s = summary.Summary('out', fofn=fofn, show_var_groups=True, show_known_vars=True, show_novel_vars=True)
299 os.unlink(fofn)
300 s.samples = summary.Summary._load_input_files(s.filenames, 90)
301 s._gather_unfiltered_output_data()
302 got_phandango_header, got_csv_header, got_matrix = summary.Summary._to_matrix(s.filenames, s.all_data, s.all_potential_columns, s.cluster_columns)
303
304 expected_phandango_header = ['name', 'noncoding1.assembled:o1', 'noncoding1.match:o1', 'noncoding1.ref_seq:o2', 'noncoding1.pct_id:c1', 'noncoding1.ctg_cov:c3', 'noncoding1.known_var:o1', 'noncoding1.novel_var:o1', 'noncoding1.id1:o1', 'noncoding1.id1.%:c2', 'noncoding1.id3:o1', 'noncoding1.id3.%:c2', 'noncoding1.14GT:o1', 'noncoding1.14GT.%:c2', 'noncoding1.14T:o1', 'noncoding1.14T.%:c2', 'noncoding1.6G:o1', 'noncoding1.6G.%:c2', 'noncoding2.assembled:o1', 'noncoding2.match:o1', 'noncoding2.ref_seq:o3', 'noncoding2.pct_id:c1', 'noncoding2.ctg_cov:c3', 'noncoding2.known_var:o1', 'noncoding2.novel_var:o1', 'noncoding2.id2:o1', 'noncoding2.id2.%:c2', 'noncoding2.42T:o1', 'noncoding2.42T.%:c2', 'noncoding2.52GT:o1', 'noncoding2.52GT.%:c2', 'presence_absence1.assembled:o1', 'presence_absence1.match:o1', 'presence_absence1.ref_seq:o4', 'presence_absence1.pct_id:c1', 'presence_absence1.ctg_cov:c3', 'presence_absence1.known_var:o1', 'presence_absence1.novel_var:o1', 'presence_absence1.A10V:o1']
305 expected_csv_header = ['name', 'noncoding1.assembled', 'noncoding1.match', 'noncoding1.ref_seq', 'noncoding1.pct_id', 'noncoding1.ctg_cov', 'noncoding1.known_var', 'noncoding1.novel_var', 'noncoding1.id1', 'noncoding1.id1.%', 'noncoding1.id3', 'noncoding1.id3.%', 'noncoding1.14GT', 'noncoding1.14GT.%', 'noncoding1.14T', 'noncoding1.14T.%', 'noncoding1.6G', 'noncoding1.6G.%', 'noncoding2.assembled', 'noncoding2.match', 'noncoding2.ref_seq', 'noncoding2.pct_id', 'noncoding2.ctg_cov', 'noncoding2.known_var', 'noncoding2.novel_var', 'noncoding2.id2', 'noncoding2.id2.%', 'noncoding2.42T', 'noncoding2.42T.%', 'noncoding2.52GT', 'noncoding2.52GT.%', 'presence_absence1.assembled', 'presence_absence1.match', 'presence_absence1.ref_seq', 'presence_absence1.pct_id', 'presence_absence1.ctg_cov', 'presence_absence1.known_var', 'presence_absence1.novel_var', 'presence_absence1.A10V']
306 expected_matrix = [
307 ['sample1', 'yes', 'yes', 'noncoding_ref1', '98.33', '10.0', 'yes', 'no', 'yes', 100.0, 'no', 'NA', 'no', 'NA', 'yes', 100.0, 'no', 'NA', 'yes', 'yes', 'noncoding_ref2', '98.33', '10.0', 'yes', 'no', 'yes_multi_het', 'NA', 'yes', 100.0, 'het', 40.0, 'yes', 'yes', 'presence_absence_ref1', '98.96', '20.1', 'no', 'yes', 'yes'],
308 [infiles[1], 'yes', 'yes', 'noncoding_ref1', '98.33', '50.1', 'yes', 'no', 'het', 80.0, 'yes', 100.0, 'het', 80.0, 'no', 'NA', 'yes', 100.0, 'yes', 'yes', 'noncoding_ref2', '98.33', '10.0', 'yes', 'no', 'het', 40.0, 'no', 'NA', 'het', 40.0, 'yes', 'yes', 'presence_absence1', '98.96', '51.1', 'no', 'yes', 'yes']
309 ]
310
311 self.assertEqual(expected_phandango_header, got_phandango_header)
312 self.assertEqual(expected_csv_header, got_csv_header)
313 self.assertEqual(expected_matrix, got_matrix)
314
315
316 def test_to_matrix_with_groups(self):
317 '''Test _to_matrix with groups'''
318 infiles = [
319 os.path.join(data_dir, 'summary_to_matrix.1.tsv'),
320 os.path.join(data_dir, 'summary_to_matrix.2.tsv')
321 ]
322
323 s = summary.Summary('out', filenames=infiles, show_var_groups=True)
324 s.samples = summary.Summary._load_input_files(s.filenames, 90)
325 s._gather_unfiltered_output_data()
326 got_phandango_header, got_csv_header, got_matrix = summary.Summary._to_matrix(s.filenames, s.all_data, s.all_potential_columns, s.cluster_columns)
327
328 expected_phandango_header = ['name', 'noncoding1.assembled:o1', 'noncoding1.match:o1', 'noncoding1.ref_seq:o2', 'noncoding1.pct_id:c1', 'noncoding1.ctg_cov:c3', 'noncoding1.known_var:o1', 'noncoding1.novel_var:o1', 'noncoding1.id1:o1', 'noncoding1.id1.%:c2', 'noncoding1.id3:o1', 'noncoding1.id3.%:c2', 'noncoding2.assembled:o1', 'noncoding2.match:o1', 'noncoding2.ref_seq:o3', 'noncoding2.pct_id:c1', 'noncoding2.ctg_cov:c3', 'noncoding2.known_var:o1', 'noncoding2.novel_var:o1', 'noncoding2.id2:o1', 'noncoding2.id2.%:c2', 'presence_absence1.assembled:o1', 'presence_absence1.match:o1', 'presence_absence1.ref_seq:o4', 'presence_absence1.pct_id:c1', 'presence_absence1.ctg_cov:c3', 'presence_absence1.known_var:o1', 'presence_absence1.novel_var:o1']
329 expected_csv_header = ['name', 'noncoding1.assembled', 'noncoding1.match', 'noncoding1.ref_seq', 'noncoding1.pct_id', 'noncoding1.ctg_cov', 'noncoding1.known_var', 'noncoding1.novel_var', 'noncoding1.id1', 'noncoding1.id1.%', 'noncoding1.id3', 'noncoding1.id3.%', 'noncoding2.assembled', 'noncoding2.match', 'noncoding2.ref_seq', 'noncoding2.pct_id', 'noncoding2.ctg_cov', 'noncoding2.known_var', 'noncoding2.novel_var', 'noncoding2.id2', 'noncoding2.id2.%', 'presence_absence1.assembled', 'presence_absence1.match', 'presence_absence1.ref_seq', 'presence_absence1.pct_id', 'presence_absence1.ctg_cov', 'presence_absence1.known_var', 'presence_absence1.novel_var']
330 expected_matrix = [
331 [infiles[0], 'yes', 'yes', 'noncoding_ref1', '98.33', '10.0', 'yes', 'no', 'yes', 100.0, 'no', 'NA', 'yes', 'yes', 'noncoding_ref2', '98.33', '10.0', 'yes', 'no', 'yes_multi_het', 'NA', 'yes', 'yes', 'presence_absence_ref1', '98.96', '20.1', 'no', 'yes'],
332 [infiles[1], 'yes', 'yes', 'noncoding_ref1', '98.33', '50.1', 'yes', 'no', 'het', 80.0, 'yes', 100.0, 'yes', 'yes', 'noncoding_ref2', '98.33', '10.0', 'yes', 'no', 'het', 40.0, 'yes', 'yes', 'presence_absence1', '98.96', '51.1', 'no', 'yes']
333 ]
334
335 self.assertEqual(expected_phandango_header, got_phandango_header)
336 self.assertEqual(expected_csv_header, got_csv_header)
337 self.assertEqual(expected_matrix, got_matrix)
338
339
340 def test_to_matrix_with_vars(self):
341 '''Test _to_matrix with vars'''
342 infiles = [
343 os.path.join(data_dir, 'summary_to_matrix.1.tsv'),
344 os.path.join(data_dir, 'summary_to_matrix.2.tsv')
345 ]
346
347 s = summary.Summary('out', filenames=infiles, show_known_vars=True, show_novel_vars=True)
348 s.samples = summary.Summary._load_input_files(s.filenames, 90)
349 s._gather_unfiltered_output_data()
350 got_phandango_header, got_csv_header, got_matrix = summary.Summary._to_matrix(s.filenames, s.all_data, s.all_potential_columns, s.cluster_columns)
351
352 expected_phandango_header = ['name', 'noncoding1.assembled:o1', 'noncoding1.match:o1', 'noncoding1.ref_seq:o2', 'noncoding1.pct_id:c1', 'noncoding1.ctg_cov:c3', 'noncoding1.known_var:o1', 'noncoding1.novel_var:o1', 'noncoding1.14GT:o1', 'noncoding1.14GT.%:c2', 'noncoding1.14T:o1', 'noncoding1.14T.%:c2', 'noncoding1.6G:o1', 'noncoding1.6G.%:c2', 'noncoding2.assembled:o1', 'noncoding2.match:o1', 'noncoding2.ref_seq:o3', 'noncoding2.pct_id:c1', 'noncoding2.ctg_cov:c3', 'noncoding2.known_var:o1', 'noncoding2.novel_var:o1', 'noncoding2.42T:o1', 'noncoding2.42T.%:c2', 'noncoding2.52GT:o1', 'noncoding2.52GT.%:c2', 'presence_absence1.assembled:o1', 'presence_absence1.match:o1', 'presence_absence1.ref_seq:o4', 'presence_absence1.pct_id:c1', 'presence_absence1.ctg_cov:c3', 'presence_absence1.known_var:o1', 'presence_absence1.novel_var:o1', 'presence_absence1.A10V:o1']
353 expected_csv_header = ['name', 'noncoding1.assembled', 'noncoding1.match', 'noncoding1.ref_seq', 'noncoding1.pct_id', 'noncoding1.ctg_cov', 'noncoding1.known_var', 'noncoding1.novel_var', 'noncoding1.14GT', 'noncoding1.14GT.%', 'noncoding1.14T', 'noncoding1.14T.%', 'noncoding1.6G', 'noncoding1.6G.%', 'noncoding2.assembled', 'noncoding2.match', 'noncoding2.ref_seq', 'noncoding2.pct_id', 'noncoding2.ctg_cov', 'noncoding2.known_var', 'noncoding2.novel_var', 'noncoding2.42T', 'noncoding2.42T.%', 'noncoding2.52GT', 'noncoding2.52GT.%', 'presence_absence1.assembled', 'presence_absence1.match', 'presence_absence1.ref_seq', 'presence_absence1.pct_id', 'presence_absence1.ctg_cov', 'presence_absence1.known_var', 'presence_absence1.novel_var', 'presence_absence1.A10V']
354 expected_matrix = [
355 [infiles[0], 'yes', 'yes', 'noncoding_ref1', '98.33', '10.0', 'yes', 'no', 'no', 'NA', 'yes', 100.0, 'no', 'NA', 'yes', 'yes', 'noncoding_ref2', '98.33', '10.0', 'yes', 'no', 'yes', 100.0, 'het', 40.0, 'yes', 'yes', 'presence_absence_ref1', '98.96', '20.1', 'no', 'yes', 'yes'],
356 [infiles[1], 'yes', 'yes', 'noncoding_ref1', '98.33', '50.1', 'yes', 'no', 'het', 80.0, 'no', 'NA', 'yes', 100.0, 'yes', 'yes', 'noncoding_ref2', '98.33', '10.0', 'yes', 'no', 'no', 'NA', 'het', 40.0, 'yes', 'yes', 'presence_absence1', '98.96', '51.1', 'no', 'yes', 'yes']
357 ]
358
359 self.assertEqual(expected_phandango_header, got_phandango_header)
360 self.assertEqual(expected_csv_header, got_csv_header)
361 self.assertEqual(expected_matrix, got_matrix)
362
363
364 def test_to_matrix_cluster_only(self):
365 '''Test _to_matrix with cluster columns only'''
366 infiles = [
367 os.path.join(data_dir, 'summary_to_matrix.1.tsv'),
368 os.path.join(data_dir, 'summary_to_matrix.2.tsv')
369 ]
370
371 s = summary.Summary('out', filenames=infiles)
372 s.samples = summary.Summary._load_input_files(s.filenames, 90)
373 s._gather_unfiltered_output_data()
374 got_phandango_header, got_csv_header, got_matrix = summary.Summary._to_matrix(s.filenames, s.all_data, s.all_potential_columns, s.cluster_columns)
375
376 expected_phandango_header = ['name', 'noncoding1.assembled:o1', 'noncoding1.match:o1', 'noncoding1.ref_seq:o2', 'noncoding1.pct_id:c1', 'noncoding1.ctg_cov:c3', 'noncoding1.known_var:o1', 'noncoding1.novel_var:o1', 'noncoding2.assembled:o1', 'noncoding2.match:o1', 'noncoding2.ref_seq:o3', 'noncoding2.pct_id:c1', 'noncoding2.ctg_cov:c3', 'noncoding2.known_var:o1', 'noncoding2.novel_var:o1', 'presence_absence1.assembled:o1', 'presence_absence1.match:o1', 'presence_absence1.ref_seq:o4', 'presence_absence1.pct_id:c1', 'presence_absence1.ctg_cov:c3', 'presence_absence1.known_var:o1', 'presence_absence1.novel_var:o1']
377 expected_csv_header = ['name', 'noncoding1.assembled', 'noncoding1.match', 'noncoding1.ref_seq', 'noncoding1.pct_id', 'noncoding1.ctg_cov', 'noncoding1.known_var', 'noncoding1.novel_var', 'noncoding2.assembled', 'noncoding2.match', 'noncoding2.ref_seq', 'noncoding2.pct_id', 'noncoding2.ctg_cov', 'noncoding2.known_var', 'noncoding2.novel_var', 'presence_absence1.assembled', 'presence_absence1.match', 'presence_absence1.ref_seq', 'presence_absence1.pct_id', 'presence_absence1.ctg_cov', 'presence_absence1.known_var', 'presence_absence1.novel_var']
378 expected_matrix = [
379 [infiles[0], 'yes', 'yes', 'noncoding_ref1', '98.33', '10.0', 'yes', 'no', 'yes', 'yes', 'noncoding_ref2', '98.33', '10.0', 'yes', 'no', 'yes', 'yes', 'presence_absence_ref1', '98.96', '20.1', 'no', 'yes'],
380 [infiles[1], 'yes', 'yes', 'noncoding_ref1', '98.33', '50.1', 'yes', 'no', 'yes', 'yes', 'noncoding_ref2', '98.33', '10.0', 'yes', 'no', 'yes', 'yes', 'presence_absence1', '98.96', '51.1', 'no', 'yes']
381 ]
382
383 self.assertEqual(expected_phandango_header, got_phandango_header)
384 self.assertEqual(expected_csv_header, got_csv_header)
385 self.assertEqual(expected_matrix, got_matrix)
386
387
388 def test_to_matrix_assembled_only(self):
389 '''Test _to_matrix with assembled column only'''
390 infiles = [
391 os.path.join(data_dir, 'summary_to_matrix.1.tsv'),
392 os.path.join(data_dir, 'summary_to_matrix.2.tsv')
393 ]
394
395 s = summary.Summary('out', filenames=infiles, cluster_cols='assembled')
396 s.samples = summary.Summary._load_input_files(s.filenames, 90)
397 s._gather_unfiltered_output_data()
398 got_phandango_header, got_csv_header, got_matrix = summary.Summary._to_matrix(s.filenames, s.all_data, s.all_potential_columns, s.cluster_columns)
399
400 expected_phandango_header = ['name', 'noncoding1.assembled:o1', 'noncoding2.assembled:o1', 'presence_absence1.assembled:o1']
401 expected_csv_header = ['name', 'noncoding1.assembled', 'noncoding2.assembled', 'presence_absence1.assembled']
402 expected_matrix = [
403 [infiles[0], 'yes', 'yes', 'yes'],
404 [infiles[1], 'yes', 'yes', 'yes']
405 ]
406
407 self.assertEqual(expected_phandango_header, got_phandango_header)
408 self.assertEqual(expected_csv_header, got_csv_header)
409 self.assertEqual(expected_matrix, got_matrix)
410
411
412 def test_filter_matrix_rows(self):
413 '''Test _filter_matrix_rows'''
414 matrix = [
415 ['yes', 'yes'],
416 ['yes', 'no'],
417 ['no', 'no'],
418 ['yes_nonunique', 'no'],
419 ['NA', 'no'],
420 ['no', 'NA'],
421 ['NA', 'NA']
422 ]
423
424 expected = [
425 ['yes', 'yes'],
426 ['yes', 'no'],
427 ['yes_nonunique', 'no'],
428 ]
429 got = summary.Summary._filter_matrix_rows(matrix)
430 self.assertEqual(expected, got)
431
432
433 def test_filter_matrix_columns(self):
434 '''Test _filter_matrix_columns'''
435 matrix = [
436 ['yes', 'yes', 'no', 'yes_nonunique', 'NA', 'no', 'NA'],
437 ['yes', 'no', 'no', 'no', 'no', 'NA', 'NA']
438 ]
439 phandango_header = ['p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7']
440 csv_header = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7']
441
442 got_phandago_header, got_csv_header, got_matrix = summary.Summary._filter_matrix_columns(matrix, phandango_header, csv_header)
443 expected_phandango_header = ['p1', 'p2', 'p4']
444 expected_csv_header = ['h1', 'h2', 'h4']
445 expected_matrix = [
446 ['yes', 'yes', 'yes_nonunique'],
447 ['yes', 'no', 'no'],
448 ]
449 self.assertEqual(expected_phandango_header, got_phandago_header)
450 self.assertEqual(expected_csv_header, got_csv_header)
451 self.assertEqual(expected_matrix, got_matrix)
452
453
454 def test_add_phandango_colour_columns(self):
455 '''Test _add_phandango_colour_columns'''
456 header = ['head1', 'head2:o1', 'head3:o1', 'head4', 'head5:o1']
457 matrix = [
458 ['yes', 'yes', 'yes_nonunique', 'yes', 'no'],
459 ['yes', 'yes_nonunique', 'no', 'yes', 'NA'],
460 ['yes', 'no', 'NA', 'yes', 'yes'],
461 ['yes', 'NA', 'yes', 'yes', 'yes_nonunique'],
462 ]
463
464 expected_header = ['head1', 'head2', 'head2:colour', 'head3', 'head3:colour', 'head4', 'head5', 'head5:colour']
465 expected_matrix = [
466 ['yes', 'yes', '#33a02c', 'yes_nonunique', '#b2df8a', 'yes', 'no', '#fb9a99'],
467 ['yes', 'yes_nonunique', '#b2df8a', 'no', '#fb9a99', 'yes', 'NA', '#ffffff'],
468 ['yes', 'no', '#fb9a99', 'NA', '#ffffff', 'yes', 'yes', '#33a02c'],
469 ['yes', 'NA', '#ffffff', 'yes', '#33a02c', 'yes', 'yes_nonunique', '#b2df8a']
470 ]
471 got_header, got_matrix = summary.Summary._add_phandango_colour_columns(header, matrix, nonunique_same_colour=False)
472 self.assertEqual(expected_header, got_header)
473 self.assertEqual(expected_matrix, got_matrix)
474
475 expected_matrix = [
476 ['yes', 'yes', '#33a02c', 'yes_nonunique', '#33a02c', 'yes', 'no', '#fb9a99'],
477 ['yes', 'yes_nonunique', '#33a02c', 'no', '#fb9a99', 'yes', 'NA', '#ffffff'],
478 ['yes', 'no', '#fb9a99', 'NA', '#ffffff', 'yes', 'yes', '#33a02c'],
479 ['yes', 'NA', '#ffffff', 'yes', '#33a02c', 'yes', 'yes_nonunique', '#33a02c']
480 ]
481 got_header, got_matrix = summary.Summary._add_phandango_colour_columns(header, matrix, nonunique_same_colour=True)
482 self.assertEqual(expected_header, got_header)
483 self.assertEqual(expected_matrix, got_matrix)
484
485
486 def test_matrix_to_csv(self):
487 '''Test _matrix_to_csv'''
488 matrix = [
489 ['line1_1', 'line1_2'],
490 ['line2_1', 'line2_2'],
491 ]
492 header = ['head1', 'head2']
493 tmpfile = 'tmp.test.matrix_to_csv.csv'
494 summary.Summary._matrix_to_csv(matrix, header, tmpfile)
495 with open(tmpfile) as f:
496 got = f.read()
497
498 expected = 'head1,head2\nline1_1,line1_2\nline2_1,line2_2\n'
499 self.assertEqual(expected, got)
500 os.unlink(tmpfile)
501
502
503 def test_matrix_to_csv_remove_nas(self):
504 '''Test _matrix_to_csv with remove_nas '''
505 matrix = [
506 ['line1_1', 'line1_2', 'NA', 'foo'],
507 ['NA', 'NA', 'bar', 'NA'],
508 ]
509 header = ['head1', 'head2', 'head3', 'head4']
510 tmpfile = 'tmp.test.matrix_to_csv_remove_nas.csv'
511 summary.Summary._matrix_to_csv(matrix, header, tmpfile, remove_nas=True)
512 with open(tmpfile) as f:
513 got = f.read()
514
515 expected = 'head1,head2,head3,head4\nline1_1,line1_2,,foo\n,,bar,\n'
516 self.assertEqual(expected, got)
517 os.unlink(tmpfile)
518
519
520 def test_distance_score_bewteen_values(self):
521 '''Test _distance_score_bewteen_values'''
522 tests = [
523 (('no', 'no'), 0),
524 (('no', 'yes'), 1),
525 (('no', 'yes_nonunique'), 1),
526 (('no', 'fragmented'), 1),
527 (('yes', 'no'), 1),
528 (('yes', 'yes'), 0),
529 (('yes', 'yes_nonunique'), 1),
530 (('yes', 'fragmented'), 1),
531 (('yes_nonunique', 'no'), 1),
532 (('yes_nonunique', 'yes'), 1),
533 (('yes_nonunique', 'yes_nonunique'), 0),
534 (('yes_nonunique', 'fragmented'), 1),
535 (('fragmented', 'no'), 1),
536 (('fragmented', 'yes'), 1),
537 (('fragmented', 'yes_nonunique'), 1),
538 (('fragmented', 'fragmented'), 0),
539 (('NA', 'no'), 0),
540 (('NA', 'yes'), 1),
541 (('NA', 'yes_nonunique'), 1),
542 (('NA', 'fragmented'), 1),
543 ]
544
545 for (val1, val2), expected in tests:
546 self.assertEqual(expected, summary.Summary._distance_score_between_values(val1, val2))
547 self.assertEqual(expected, summary.Summary._distance_score_between_values(val2, val1))
548
549
550
551 def test_distance_score_between_lists(self):
552 '''Test _distance_score_between_lists'''
553 list1 = ['NA', 'no', 'yes']
554 list2 = ['NA', 'no', 'no']
555 self.assertEqual(1, summary.Summary._distance_score_between_lists(list1, list2))
556
557
558 def test_write_distance_matrix(self):
559 '''Test _write_distance_matrix'''
560 rows = [
561 ['file1', 'no', 'yes', 'no'],
562 ['file2', 'yes', 'no', 'yes'],
563 ['file3', 'no', 'no', 'yes'],
564 ]
565
566 tmp_distances = 'tmp.test.write_distance_matrix.distances'
567 summary.Summary._write_distance_matrix(rows, tmp_distances)
568 expected = os.path.join(data_dir, 'summary_test_write_distance_matrix.distances')
569 self.assertTrue(filecmp.cmp(expected, tmp_distances, shallow=False))
570 os.unlink(tmp_distances)
571
572
573 def test_newick_from_dist_matrix(self):
574 '''Test _newick_from_dist_matrix'''
575 tmp_tree = 'tmp.test.newick_from_dist_matrix.tre'
576 dist_file = os.path.join(data_dir, 'summary_test_newick_from_dist_matrix.distances')
577 summary.Summary._newick_from_dist_matrix(dist_file, tmp_tree)
578 # the exact ordering of the nodes is not predictable, so we'll trust dendropy
579 # and just check that an output file got written
580 self.assertTrue(os.path.exists(tmp_tree))
581 os.unlink(tmp_tree)
582
583
584 def test_whole_run(self):
585 '''Test whole run to check csv ok (skip making tree)'''
586 tmp_out = 'tmp.summary_test_whole_run.out'''
587 infiles = [
588 os.path.join(data_dir, 'summary_test_whole_run.in.1.tsv'),
589 os.path.join(data_dir, 'summary_test_whole_run.in.2.tsv'),
590 ]
591
592 s = summary.Summary(
593 tmp_out,
594 filenames=infiles,
595 make_phandango_tree=False,
596 show_var_groups=True,
597 show_known_vars=True,
598 show_novel_vars=True
599 )
600
601 s.run()
602 expected_file = os.path.join(data_dir, 'summary_test_whole_run.out.csv')
603 # we don't know the full path of the input files, so check all the other columns
604 with open(expected_file) as f:
605 expected = [line.rstrip().split(',', maxsplit=1)[1] for line in f]
606 with open(tmp_out + '.csv') as f:
607 got = [line.rstrip().split(',', maxsplit=1)[1] for line in f]
608
609 self.assertEqual(expected, got)
610 os.unlink(tmp_out + '.csv')
611 os.unlink(tmp_out + '.phandango.csv')