Debian Codegraph @

ariba/tests/scaffold_graph_test.py

raw
1 import unittest
2 import os
3 import filecmp
4 import pyfastaq
5 import pysam
6 from ariba import link, scaffold_graph
7
8 modules_dir = os.path.dirname(os.path.abspath(scaffold_graph.__file__))
9 data_dir = os.path.join(modules_dir, 'tests', 'data')
10
11
12 class TestScaffoldGraph(unittest.TestCase):
13 def test_update_from_sam(self):
14 '''test update_from_sam'''
15 sam_reader = pysam.Samfile(os.path.join(data_dir, 'graph_test_update_from_sam.bam'), "rb")
16 ref_lengths = {}
17 pyfastaq.tasks.lengths_from_fai(os.path.join(data_dir, 'graph_test_update_from_sam.ref.fa.fai'), ref_lengths)
18 graph = scaffold_graph.Graph(ref_lengths)
19
20 for sam in sam_reader.fetch(until_eof=True):
21 graph.update_from_sam(sam, sam_reader)
22
23 self.assertEqual(len(graph.partial_links), 0)
24 self.assertEqual(len(graph.links), 1)
25 key = ('ref1', 'ref2')
26 self.assertTrue(key in graph.links)
27 expected_links = [link.Link(None, None, None, s='\t'.join(['ref1', '506', 'R', '300', 'ref2', '500', 'L', '359']))]
28 self.assertListEqual(graph.links[key], expected_links)
29
30
31 def test_make_graph(self):
32 '''test _make_graph'''
33 ref_lengths = {'ref1':100, 'ref2':200, 'ref3':300}
34 g = scaffold_graph.Graph(ref_lengths)
35
36 g.partial_links = {42:42}
37 with self.assertRaises(scaffold_graph.Error):
38 g._make_graph(1000)
39 g.partial_links = {}
40
41 g.links[('ref1', 'ref2')] = [
42 link.Link(None, None, None, '\t'.join(['ref1', '100', 'R', '50', 'ref2', '200', 'L', '10']))
43 ]
44
45 g._make_graph(10)
46 self.assertEqual(len(g.contig_links), 0)
47
48 g._make_graph(1000)
49 self.assertEqual(len(g.contig_links), 1)
50 expected_contig_links = {
51 ('ref1', 'ref2'): {'RL': 1}
52 }
53 self.assertDictEqual(g.contig_links, expected_contig_links)
54
55 g.links[('ref1', 'ref2')].append(link.Link(None, None, None, '\t'.join(['ref1', '100', 'R', '50', 'ref2', '200', 'L', '10'])))
56 g.links[('ref1', 'ref2')].append(link.Link(None, None, None, '\t'.join(['ref1', '100', 'L', '50', 'ref2', '200', 'R', '10'])))
57 g._make_graph(1000)
58 expected_contig_links = {
59 ('ref1', 'ref2'): {'RL': 2, 'LR': 1}
60 }
61 self.assertDictEqual(g.contig_links, expected_contig_links)
62
63
64 def test_remove_low_cov_links(self):
65 '''test _remove_low_cov_links'''
66 ref_lengths = {'ref1':100, 'ref2':200, 'ref3':300}
67 g = scaffold_graph.Graph(ref_lengths)
68 g.contig_links = {
69 ('ref1', 'ref2'): {'LR': 42, 'RL':41},
70 ('ref2', 'ref3'): {'LR': 43, 'RL':42},
71 ('ref3', 'ref4'): {'LR': 1, 'RL':2}
72 }
73
74 g._remove_low_cov_links(42)
75 expected = {
76 ('ref1', 'ref2'): {'LR': 42},
77 ('ref2', 'ref3'): {'LR': 43, 'RL':42}
78 }
79
80 self.assertDictEqual(expected, g.contig_links)
81
82
83 def test_contig_graph_is_consistent(self):
84 '''test _contig_graph_is_consistent'''
85 ref_lengths = {'ref1':100, 'ref2':200, 'ref3':300}
86 g = scaffold_graph.Graph(ref_lengths)
87
88 good_links = [
89 {('ref1', 'ref2'): {'RL': 42}},
90 {('ref1', 'ref2'): {'RL': 42}, ('ref2', 'ref3'): {'RL': 42}},
91 ]
92
93 bad_links = [
94 {('ref1', 'ref2'): {'LR': 42, 'RL':42}},
95 {('ref1', 'ref2'): {'LR': 42}, ('ref1', 'ref3'): {'LR': 42}}
96 ]
97
98
99 for d in good_links:
100 g.contig_links = d
101 self.assertTrue(g._contig_graph_is_consistent())
102
103 for d in bad_links:
104 g.contig_links = d
105 self.assertFalse(g._contig_graph_is_consistent())
106
107
108 def test_write_all_links_to_file(self):
109 '''test write_all_links_to_file'''
110 ref_lengths = {'ref1':100, 'ref2':200, 'ref3':300}
111 g = scaffold_graph.Graph(ref_lengths)
112 g.links[('ref1', 'ref2')] = [
113 link.Link(None, None, None, '\t'.join(['ref1', '100', 'R', '50', 'ref2', '200', 'L', '10']))
114 ]
115
116
117 g.links[('ref1', 'ref2')].append(link.Link(None, None, None, '\t'.join(['ref1', '100', 'R', '50', 'ref2', '200', 'L', '10'])))
118 g.links[('ref1', 'ref2')].append(link.Link(None, None, None, '\t'.join(['ref1', '100', 'L', '50', 'ref2', '200', 'R', '10'])))
119
120
121 g.links[('ref3', 'ref4')] = [
122 link.Link(None, None, None, '\t'.join(['ref3', '100', 'R', '42', 'ref4', '200', 'L', '42']))
123 ]
124
125 tmp_file = 'tmp.contig_links'
126 g.write_all_links_to_file(tmp_file)
127 self.assertTrue(filecmp.cmp(os.path.join(data_dir, 'graph_test_write_all_links_to_file.out'), tmp_file, shallow=False))
128 os.unlink(tmp_file)
129
130