-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtest_mafft.py
293 lines (232 loc) · 11.6 KB
/
test_mafft.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# ----------------------------------------------------------------------------
# Copyright (c) 2016-2025, QIIME 2 development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
# ----------------------------------------------------------------------------
import os
import unittest
from unittest.mock import patch, ANY
import subprocess
import skbio
from qiime2.plugin.testing import TestPluginBase
from q2_types.feature_data import DNAFASTAFormat, AlignedDNAFASTAFormat
from qiime2.util import redirected_stdio
from q2_alignment import mafft, mafft_add
from q2_alignment._mafft import run_command
class MafftTests(TestPluginBase):
package = 'q2_alignment.tests'
def _prepare_sequence_data(self):
input_fp = self.get_data_path('unaligned-dna-sequences-1.fasta')
input_sequences = DNAFASTAFormat(input_fp, mode='r')
exp = skbio.TabularMSA(
[skbio.DNA('AGGGGGG', metadata={'id': 'seq1', 'description': ''}),
skbio.DNA('-GGGGGG', metadata={'id': 'seq2', 'description': ''})]
)
return input_sequences, exp
def test_mafft(self):
input_sequences, exp = self._prepare_sequence_data()
with redirected_stdio(stderr=os.devnull):
result = mafft(input_sequences)
obs = skbio.io.read(str(result), into=skbio.TabularMSA,
constructor=skbio.DNA)
self.assertEqual(obs, exp)
def test_multithreaded_mafft(self):
input_sequences, exp = self._prepare_sequence_data()
with redirected_stdio(stderr=os.devnull):
result = mafft(input_sequences, n_threads=0)
obs = skbio.io.read(str(result), into=skbio.TabularMSA,
constructor=skbio.DNA)
self.assertEqual(obs, exp)
def test_long_ids_are_not_truncated(self):
input_fp = self.get_data_path('unaligned-long-ids.fasta')
input_sequences = DNAFASTAFormat(input_fp, mode='r')
with redirected_stdio(stderr=os.devnull):
result = mafft(input_sequences)
with open(str(result), 'r') as fh:
obs = fh.read()
self.assertIn('a'*250, obs)
self.assertIn('b'*250, obs)
self.assertIn('c'*250, obs)
def test_duplicate_input_ids(self):
input_fp = self.get_data_path('unaligned-duplicate-ids.fasta')
input_sequences = DNAFASTAFormat(input_fp, mode='r')
with self.assertRaisesRegex(ValueError, 'the unaligned.*id1'):
with redirected_stdio(stderr=os.devnull):
mafft(input_sequences)
def test_mafft_parttree_exception(self):
input_fp = os.path.join(self.temp_dir.name, 'million.fasta')
with open(input_fp, "w") as f:
for i in range(0, 1000002):
f.write('>%d\nAAGCAAGC\n' % i)
input_sequences = DNAFASTAFormat(input_fp, mode='r')
with self.assertRaisesRegex(ValueError, '1 million'):
with redirected_stdio(stderr=os.devnull):
mafft(input_sequences)
class MafftAddTests(TestPluginBase):
package = 'q2_alignment.tests'
def _prepare_sequence_data(self):
sequences_fp = self.get_data_path('unaligned-dna-sequences-1.fasta')
sequences = DNAFASTAFormat(sequences_fp, mode='r')
alignment_fp = self.get_data_path('aligned-dna-sequences-1.fasta')
alignment = AlignedDNAFASTAFormat(alignment_fp, mode='r')
exp = skbio.TabularMSA(
[skbio.DNA('AGGGGG-',
metadata={'id': 'aln-seq-1', 'description': ''}),
skbio.DNA('AGGGGGG',
metadata={'id': 'aln-seq-2', 'description': ''}),
skbio.DNA('AGGGGGG',
metadata={'id': 'seq1', 'description': ''}),
skbio.DNA('-GGGGGG',
metadata={'id': 'seq2', 'description': ''})]
)
return alignment, sequences, exp
def _prepare_sequence_data_2(self):
# for new alignment using `--keeplength` parameter
sequences_fp = self.get_data_path('unaligned-dna-sequences-2.fasta')
sequences = DNAFASTAFormat(sequences_fp, mode='r')
alignment_fp = self.get_data_path('aligned-dna-sequences-2.fasta')
alignment = AlignedDNAFASTAFormat(alignment_fp, mode='r')
exp = skbio.TabularMSA(
[skbio.DNA('AGGG-GGC',
metadata={'id': 'aln-seq-1', 'description': ''}),
skbio.DNA('AGGGTGGC',
metadata={'id': 'aln-seq-2', 'description': ''}),
skbio.DNA('AGGTTGGC',
metadata={'id': 'seq-3', 'description': ''}),
skbio.DNA('AGGATGGC',
metadata={'id': 'seq-4', 'description': ''})]
)
return alignment, sequences, exp
def _prepare_sequence_data_3(self):
# NOT using `--keeplength` parameter. To compare with
# _prepare_sequence_data_2 output
sequences_fp = self.get_data_path('unaligned-dna-sequences-2.fasta')
sequences = DNAFASTAFormat(sequences_fp, mode='r')
alignment_fp = self.get_data_path('aligned-dna-sequences-2.fasta')
alignment = AlignedDNAFASTAFormat(alignment_fp, mode='r')
exp = skbio.TabularMSA(
[skbio.DNA('AGG--G---GGC',
metadata={'id': 'aln-seq-1', 'description': ''}),
skbio.DNA('AGG--G--TGGC',
metadata={'id': 'aln-seq-2', 'description': ''}),
skbio.DNA('AGG--TTTTGGC',
metadata={'id': 'seq-3', 'description': ''}),
skbio.DNA('AGGTTA--TGGC',
metadata={'id': 'seq-4', 'description': ''})]
)
return alignment, sequences, exp
def test_mafft_add(self):
alignment, sequences, exp = self._prepare_sequence_data()
with redirected_stdio(stderr=os.devnull):
result = mafft_add(alignment, sequences)
obs = skbio.io.read(str(result), into=skbio.TabularMSA,
constructor=skbio.DNA)
self.assertEqual(obs, exp)
def test_mafft_add_fragments(self):
alignment, sequences, exp = self._prepare_sequence_data()
with redirected_stdio(stderr=os.devnull):
result = mafft_add(alignment, sequences, addfragments=True)
obs = skbio.io.read(str(result), into=skbio.TabularMSA,
constructor=skbio.DNA)
self.assertEqual(obs, exp)
def test_mafft_add_fragments_keeplength(self):
alignment, sequences, exp = self._prepare_sequence_data_2()
with redirected_stdio(stderr=os.devnull):
result = mafft_add(alignment, sequences, addfragments=True,
keeplength=True)
obs = skbio.io.read(str(result), into=skbio.TabularMSA,
constructor=skbio.DNA)
self.assertEqual(obs, exp)
def test_mafft_add_fragments_no_keeplength(self):
alignment, sequences, exp = self._prepare_sequence_data_3()
with redirected_stdio(stderr=os.devnull):
result = mafft_add(alignment, sequences, addfragments=True,
keeplength=False)
obs = skbio.io.read(str(result), into=skbio.TabularMSA,
constructor=skbio.DNA)
self.assertEqual(obs, exp)
def test_mafft_add_flags(self):
alignment, sequences, exp = self._prepare_sequence_data()
with patch('q2_alignment._mafft.run_command') as patched_run_cmd:
with patch('q2_alignment._mafft.skbio.TabularMSA.read',
return_value=exp):
_ = mafft_add(alignment, sequences)
patched_run_cmd.assert_called_with(
["mafft", "--preservecase", "--inputorder", "--thread",
"1", "--add", ANY, ANY], ANY)
_ = mafft_add(alignment, sequences, addfragments=True)
patched_run_cmd.assert_called_with(
["mafft", "--preservecase", "--inputorder", "--thread",
"1", "--addfragments", ANY, ANY], ANY)
def test_duplicate_input_ids_in_unaligned(self):
input_fp = self.get_data_path('unaligned-duplicate-ids.fasta')
sequences = DNAFASTAFormat(input_fp, mode='r')
alignment, _, _ = self._prepare_sequence_data()
with self.assertRaisesRegex(ValueError, 'the unaligned.*id1'):
with redirected_stdio(stderr=os.devnull):
mafft_add(alignment, sequences)
def test_duplicate_input_ids_in_aligned(self):
input_fp = self.get_data_path('aligned-duplicate-ids-1.fasta')
alignment = DNAFASTAFormat(input_fp, mode='r')
_, sequences, _ = self._prepare_sequence_data()
with self.assertRaisesRegex(ValueError, 'the aligned.*id1'):
with redirected_stdio(stderr=os.devnull):
mafft_add(alignment, sequences)
def test_duplicate_input_ids_across_aligned_and_unaligned(self):
input_fp = self.get_data_path('aligned-duplicate-ids-2.fasta')
alignment = DNAFASTAFormat(input_fp, mode='r')
_, sequences, _ = self._prepare_sequence_data()
with self.assertRaisesRegex(ValueError, 'aligned and unaligned.*seq1'):
with redirected_stdio(stderr=os.devnull):
mafft_add(alignment, sequences)
def test_long_ids_are_not_truncated_unaligned(self):
input_fp = self.get_data_path('unaligned-long-ids.fasta')
sequences = DNAFASTAFormat(input_fp, mode='r')
alignment, _, _ = self._prepare_sequence_data()
with redirected_stdio(stderr=os.devnull):
result = mafft_add(alignment, sequences)
with open(str(result), 'r') as fh:
obs = fh.read()
self.assertIn('a'*250, obs)
self.assertIn('b'*250, obs)
self.assertIn('c'*250, obs)
self.assertIn('aln-seq-1', obs)
self.assertIn('aln-seq-2', obs)
def test_long_ids_are_not_truncated_aligned(self):
input_fp = self.get_data_path('aligned-long-ids.fasta')
alignment = DNAFASTAFormat(input_fp, mode='r')
_, sequences, _ = self._prepare_sequence_data()
with redirected_stdio(stderr=os.devnull):
result = mafft_add(alignment, sequences)
with open(str(result), 'r') as fh:
obs = fh.read()
self.assertIn('a'*250, obs)
self.assertIn('b'*250, obs)
self.assertIn('seq1', obs)
self.assertIn('seq2', obs)
class RunCommandTests(TestPluginBase):
package = 'q2_alignment.tests'
def test_failed_run(self):
input_fp = self.get_data_path('unaligned-dna-sequences-1.fasta')
input_sequences = DNAFASTAFormat(input_fp, mode='r')
output_alignment = AlignedDNAFASTAFormat()
unaligned_fp = str(input_sequences)
aligned_fp = str(output_alignment)
cmd = ["mafft", "--not-a-real-parameter", unaligned_fp]
with self.assertRaises(subprocess.CalledProcessError):
with redirected_stdio(stderr=os.devnull):
run_command(cmd, aligned_fp)
def test_failed_run_not_verbose(self):
input_fp = self.get_data_path('unaligned-dna-sequences-1.fasta')
input_sequences = DNAFASTAFormat(input_fp, mode='r')
output_alignment = AlignedDNAFASTAFormat()
unaligned_fp = str(input_sequences)
aligned_fp = str(output_alignment)
cmd = ["mafft", "--not-a-real-parameter", unaligned_fp]
with self.assertRaises(subprocess.CalledProcessError):
with redirected_stdio(stderr=os.devnull):
run_command(cmd, aligned_fp, verbose=False)
if __name__ == "__main__":
unittest.main()