-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathunitTest_GG.py
executable file
·266 lines (158 loc) · 7.3 KB
/
unitTest_GG.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Dependencies
import networkx as nx
# Built in
import unittest
from subprocess import call
import argparse
import sys
import csv
import copy
# Import GenGraph functions
from gengraph import *
''' Basic tests '''
class BasicFunctionsTestCase(unittest.TestCase):
"""Tests for basic functions in `phil_0_4.py`."""
def test_reverse(self):
"""Is the string correctly reversed?"""
self.assertEqual(reverse('ATGTTA'),'ATTGTA')
def test_compliment_Base(self):
"""Is the base correctly complimented?"""
self.assertEqual(compliment_Base('A'),'T')
self.assertEqual(compliment_Base('T'),'A')
self.assertEqual(compliment_Base('G'),'C')
self.assertEqual(compliment_Base('C'),'G')
self.assertEqual(compliment_Base('N'),'N')
def test_compliment_DNA(self):
"""Is the string correctly complimented?"""
self.assertEqual(compliment_DNA('ATGTTA'),'TACAAT')
def test_reverse_compliment(self):
"""Is the string correctly reverse complimented?"""
self.assertEqual(reverse_compliment('ATGTTA'),'TAACAT')
def test_is_even(self):
"""Is the int even?"""
self.assertTrue(is_even(8))
self.assertFalse(is_even(7))
self.assertTrue(is_even(-8))
self.assertFalse(is_even(-7))
''' Graph utility function tests'''
'''
class GraphUtilityFunctionsTestCase(unittest.TestCase):
"""Tests for graph utility functions in `phil_0_4.py`."""
#test_graph = './test_files/unit_tests/aln_add3_seq.xml'
#imported_genome = nx.read_graphml(test_graph)
def test_nodes_connected(self):
"""Are linked nodes correctly detected? (directly linked neighbours)"""
test_graph = './test_files/unit_tests/aln_add3_seq.xml'
imported_genome = nx.read_graphml(test_graph)
self.assertTrue(nodes_connected('X_20', 'X_18', imported_genome))
self.assertFalse(nodes_connected('X_18', 'X_20', imported_genome))
self.assertFalse(nodes_connected('X_21', 'X_18', imported_genome))
self.assertFalse(nodes_connected('X_20', 'X_20', imported_genome))
'''
''' Graph generation function tests'''
class GraphGeneratingFunctionsTestCase(unittest.TestCase):
"""Tests for graph generation functions in `phil_0_4.py`."""
def test_alignment_to_graph_conversion(self):
"""Are fasta alignment files correctly converted to graphs?"""
test_fasta_aln = './unitTestFiles/alignedSeq.fa'
test_start_dict = {'seq1':1,'seq2':-50,'seq3':1}
fasta_object = input_parser(test_fasta_aln)
new_graph = fasta_alignment_to_subnet(test_fasta_aln, true_start=test_start_dict, node_prefix='X', orientation={}, re_link_nodes=True, add_seq=True)
nx.write_graphml(new_graph, './unitTestFiles/alignedSeq.xml')
seq1_seq = extract_original_seq(new_graph, 'seq1')
seq2_seq = extract_original_seq(new_graph, 'seq2')
seq3_seq = extract_original_seq(new_graph, 'seq3')
does_pass = True
print seq1_seq
print 'GAGATTAGGAGTAGATAGATAGATATTTAGAGCCCGGAAAATTTATATTATTTAAT'
print seq2_seq
print reverse_compliment('GATTAGGAGTAGATAGATAGATATTTAGAGAGAGAAAATTTATATTATTT')
print seq3_seq
print 'GAGATTAGGAGTAGATAGATAGTATTTAGAGAGAGAAAATTTATATTATTTAAT'
if seq1_seq != 'GAGATTAGGAGTAGATAGATAGATATTTAGAGCCCGGAAAATTTATATTATTTAAT':
does_pass = False
print 'seq1 fails'
if seq2_seq != reverse_compliment('GATTAGGAGTAGATAGATAGATATTTAGAGAGAGAAAATTTATATTATTT'):
does_pass = False
print 'seq2 fails'
if seq3_seq != 'GAGATTAGGAGTAGATAGATAGTATTTAGAGAGAGAAAATTTATATTATTTAAT':
does_pass = False
print 'seq3 fails'
self.assertTrue(does_pass)
def test_create_genome_alignment_graph(self):
"""Is the correct block graph created?"""
test_bbone_file = './unitTestFiles/globalAlignment_phyloTest.backbone'
parsed_input_dict = parse_seq_file('./unitTestFiles/multiGenome.txt')
created_test_graph = create_genome_alignment_graph(test_bbone_file, parsed_input_dict[0], parsed_input_dict[1])
#nx.write_graphml(created_test_graph, './test_files/unit_tests/bbone_to_graph_test_out.xml')
test_pass = True
#print test_pass
self.assertTrue(test_pass)
'''
def test_split_node(self):
"""Are nodes being correctly split?"""
curr_in_graph = '/Users/panix/Dropbox/Programs/tools/genome_alignment_graph_tool/test_files/two_genome/test_graph_2.xml'
node_to_split = 'H37Rv_seq_19'
test_length = 10000
imported_genome = nx.read_graphml(curr_in_graph)
print 'genome import complete'
#split_g = split_node(imported_genome, node_to_split, test_length)
#split_g = split_all_long_nodes(imported_genome, test_length)
Rv_seq = input_parser('/Volumes/HDD/Genomes/M_tuberculosis/H37Rv/mycobacterium_tuberculosis_h37rv_2_supercontigs.fasta')
Ra_seq = input_parser('/Users/panix/M_tuberculosis/H37Ra/mycobacterium_tuberculosis_h37ra_1_contigs.fasta')
print '\n--------------------- Recall test ---------------------'
print len(Rv_seq[0]['DNA_seq'])
print len(Ra_seq[0]['DNA_seq'])
print Rv_seq[0]['DNA_seq'][483399:483409]
#print Ra_seq[0]['DNA_seq'][2132526:2132536].upper()
seq_added_file = '/Users/panix/Dropbox/Programs/tools/genome_alignment_graph_tool/seq_added_TEST_LALALA.xml'
seq_added_genome = nx.read_graphml(seq_added_file)
print 'Origional length:'
print len(Rv_seq[0]['DNA_seq'])
print 'extracted length:'
#extracted_seq = extract_seq(seq_added_genome, 'H37Rv')
#print len(extracted_seq)
base_count = 0
while base_count < len(extracted_seq):
if Rv_seq[0]['DNA_seq'][base_count:base_count+1] != extracted_seq[base_count:base_count+1]:
print 'Error: ' + str(base_count)
base_count += 1
else:
base_count += 1
#if extracted_seq == Rv_seq[0]['DNA_seq']:
# print 'WIN!!!'
for iso in imported_genome.graph['isolates'].split(','):
print iso
imported_genome = link_nodes_2(imported_genome, iso)
nx.write_graphml(imported_genome, 'linked_test_all.xml')
prob_node_file = '/Users/panix/Dropbox/Programs/tools/genome_alignment_graph_tool/test_files/two_genome/intermediate_split_Graph.xml'
seq_data_file = '/Users/panix/Dropbox/Programs/tools/genome_alignment_graph_tool/test_files/two_genome/mtb_2genome_file.txt'
prob_genome_obj = nx.read_graphml(prob_node_file)
seq_fasta_paths_dict = parse_seq_file(seq_data_file)[1]
prob_node = 'Aln_region_2_42'
realign_prob_g = local_node_realign(prob_genome_obj, prob_node, seq_fasta_paths_dict)
nx.write_graphml(realign_prob_g, 'prob_graph_2_42_test.xml')
is_split = True
self.assertTrue(is_split)
def fasta_alignment_to_subnet(self):
"""Are the fasta files correctly converted to graphs?"""
print 'HDFGSDFGADFGADFGASFGASDGASDF'
curr_in_graph = '/Users/panix/Dropbox/Programs/tools/genome_alignment_graph_tool/test_files/two_genome/temp_aln.fasta'
new_graph = fasta_alignment_to_subnet(curr_in_graph)
nx.write_graphml(new_graph, 'graph_fasta_file.xml')
is_working = True
self.assertTrue(is_working)
def test_link_nodes(self):
"""Are nodes correctly linked?"""
test_graph_unlinked = './test_files/unit_tests/unlinked_node_test.xml'
test_graph_linked = './test_files/unit_tests/bbone_to_graph_test_comparison.xml'
imported_unlinked_genome = nx.read_graphml(test_graph_unlinked)
imported_linked_genome = nx.read_graphml(test_graph_linked)
iso_list = ['Alpha','Beta','Cappa']
for isolate in iso_list:
imported_unlinked_genome = link_nodes(imported_unlinked_genome, isolate)
test_pass = nx.is_isomorphic(imported_unlinked_genome, imported_linked_genome)
self.assertTrue(test_pass)
'''
if __name__ == '__main__':
unittest.main()