6
6
import copy
7
7
import os .path
8
8
from os .path import join
9
+ import subprocess
9
10
import tempfile
10
11
import textwrap
11
12
import unittest
26
27
from io import StringIO
27
28
28
29
29
- class TestCommandHelp (unittest .TestCase ):
30
+ def test_help_parser_for_each_command ():
31
+ for cmd_name , parser_fun in metagenomics .__commands__ :
32
+ parser = parser_fun (argparse .ArgumentParser ())
33
+ helpstring = parser .format_help ()
30
34
31
- def test_help_parser_for_each_command (self ):
32
- for cmd_name , parser_fun in metagenomics .__commands__ :
33
- parser = parser_fun (argparse .ArgumentParser ())
34
- helpstring = parser .format_help ()
35
35
36
-
37
- class TestKronaCalls (TestCaseWithTmp ):
38
-
39
- def setUp (self ):
40
- super ().setUp ()
41
- patcher = patch ('tools.krona.Krona' , autospec = True )
42
- self .addCleanup (patcher .stop )
43
- self .mock_krona = patcher .start ()
44
-
45
- self .inTsv = util .file .mkstempfname ('.tsv' )
46
- self .db = tempfile .mkdtemp ('db' )
47
-
48
- def test_krona_import_taxonomy (self ):
49
- out_html = util .file .mkstempfname ('.html' )
50
- metagenomics .krona (self .inTsv , self .db , out_html , queryColumn = 3 , taxidColumn = 5 , scoreColumn = 7 ,
51
- noHits = True , noRank = True , inputType = 'tsv' )
52
- self .mock_krona ().import_taxonomy .assert_called_once_with (
53
- self .db , [self .inTsv ], out_html , query_column = 3 , taxid_column = 5 , score_column = 7 ,
54
- no_hits = True , no_rank = True , magnitude_column = None , root_name = os .path .basename (self .inTsv ))
36
+ def test_krona_import_taxonomy (mocker ):
37
+ p = mocker .patch ('tools.krona.Krona' , autospec = True )
38
+ out_html = util .file .mkstempfname ('.html' )
39
+ in_tsv = util .file .mkstempfname ('.tsv' )
40
+ db = tempfile .mkdtemp ('db' )
41
+ metagenomics .krona (in_tsv , db , out_html , queryColumn = 3 , taxidColumn = 5 , scoreColumn = 7 ,
42
+ noHits = True , noRank = True , inputType = 'tsv' )
43
+ p ().import_taxonomy .assert_called_once_with (
44
+ db , [in_tsv ], out_html , query_column = 3 , taxid_column = 5 , score_column = 7 ,
45
+ no_hits = True , no_rank = True , magnitude_column = None , root_name = os .path .basename (in_tsv ))
55
46
56
47
57
48
@pytest .fixture
@@ -123,8 +114,7 @@ def ranks():
123
114
124
115
@pytest .fixture
125
116
def simple_m8 ():
126
- test_path = join (util .file .get_test_input_path (),
127
- 'TestTaxonomy' )
117
+ test_path = join (util .file .get_test_input_path (), 'TestTaxonomy' )
128
118
return open (join (test_path , 'simple.m8' ))
129
119
130
120
@@ -170,8 +160,7 @@ def test_rank_code():
170
160
171
161
172
162
def test_blast_records (simple_m8 ):
173
- test_path = join (util .file .get_test_input_path (),
174
- 'TestTaxonomy' )
163
+ test_path = join (util .file .get_test_input_path (), 'TestTaxonomy' )
175
164
with simple_m8 as f :
176
165
records = list (metagenomics .blast_records (f ))
177
166
assert len (records ) == 110
@@ -180,8 +169,7 @@ def test_blast_records(simple_m8):
180
169
181
170
182
171
def test_blast_lca (taxa_db_simple , simple_m8 ):
183
- test_path = join (util .file .get_test_input_path (),
184
- 'TestTaxonomy' )
172
+ test_path = join (util .file .get_test_input_path (), 'TestTaxonomy' )
185
173
expected = textwrap .dedent ("""\
186
174
C\t M04004:13:000000000-AGV3H:1:1101:12068:2105\t 2
187
175
C\t M04004:13:000000000-AGV3H:1:1101:13451:2146\t 2
@@ -283,45 +271,64 @@ def test_kaiju(mocker):
283
271
p .assert_called_with ('db.fmi' , 'tax_db' , 'input.bam' , output_report = 'output.report' , num_threads = mock .ANY , output_reads = 'output.reads' )
284
272
285
273
286
- class TestBamFilter (TestCaseWithTmp ):
287
- def test_bam_filter_simple (self ):
288
- temp_dir = tempfile .gettempdir ()
289
- input_dir = util .file .get_test_input_path (self )
290
- taxonomy_dir = os .path .join (util .file .get_test_input_path (),"TestMetagenomicsSimple" ,"db" ,"taxonomy" )
274
+ @pytest .fixture
275
+ def taxonomy_dir ():
276
+ return os .path .join (util .file .get_test_input_path (),"TestMetagenomicsSimple" , "db" , "taxonomy" )
277
+
291
278
279
+ def test_kraken_unclassified (taxonomy_dir ):
280
+ input_dir = join (util .file .get_test_input_path (), 'TestBamFilter' )
281
+
282
+ for p , n_leftover in zip ([0.05 , 0.5 ], [37 , 579 ]):
292
283
filtered_bam = util .file .mkstempfname ('.bam' )
293
- args = [
294
- os .path .join (input_dir ,"input.bam" ),
295
- os .path .join (input_dir ,"input.kraken-reads.tsv.gz" ),
284
+ cmd_args = [
285
+ os .path .join (input_dir , "input.bam" ),
286
+ os .path .join (input_dir , "input.kraken-reads.tsv.gz" ),
296
287
filtered_bam ,
297
- os .path .join (taxonomy_dir ,"nodes.dmp" ),
298
- os .path .join (taxonomy_dir ,"names.dmp" ),
299
- "--taxNames" ,
300
- "Ebolavirus"
288
+ '-p' , str (p )
301
289
]
302
- args = metagenomics .parser_filter_bam_to_taxa (argparse .ArgumentParser ()).parse_args (args )
290
+ args = metagenomics .parser_kraken_unclassified (argparse .ArgumentParser ()).parse_args (cmd_args )
303
291
args .func_main (args )
304
292
305
- expected_bam = os .path .join (input_dir ,"expected.bam" )
306
- assert_equal_bam_reads (self , filtered_bam , expected_bam )
293
+ cmd = ['samtools' , 'view' , filtered_bam ]
294
+ p = subprocess .run (cmd , stdout = subprocess .PIPE )
295
+ assert len (p .stdout .decode ('utf-8' ).split ('\n ' )) == n_leftover
307
296
308
- def test_bam_filter_by_tax_id (self ):
309
- temp_dir = tempfile .gettempdir ()
310
- input_dir = util .file .get_test_input_path (self )
311
- taxonomy_dir = os .path .join (util .file .get_test_input_path (),"TestMetagenomicsSimple" ,"db" ,"taxonomy" )
312
297
313
- filtered_bam = util .file .mkstempfname ('.bam' )
314
- args = [
315
- os .path .join (input_dir ,"input.bam" ),
316
- os .path .join (input_dir ,"input.kraken-reads.tsv.gz" ),
317
- filtered_bam ,
318
- os .path .join (taxonomy_dir ,"nodes.dmp" ),
319
- os .path .join (taxonomy_dir ,"names.dmp" ),
320
- "--taxIDs" ,
321
- "186538"
322
- ]
323
- args = metagenomics .parser_filter_bam_to_taxa (argparse .ArgumentParser ()).parse_args (args )
324
- args .func_main (args )
298
+ def test_bam_filter_simple (taxonomy_dir ):
299
+ input_dir = join (util .file .get_test_input_path (), 'TestBamFilter' )
300
+
301
+ filtered_bam = util .file .mkstempfname ('.bam' )
302
+ args = [
303
+ os .path .join (input_dir , "input.bam" ),
304
+ os .path .join (input_dir , "input.kraken-reads.tsv.gz" ),
305
+ filtered_bam ,
306
+ os .path .join (taxonomy_dir , "nodes.dmp" ),
307
+ os .path .join (taxonomy_dir , "names.dmp" ),
308
+ "--taxNames" ,
309
+ "Ebolavirus"
310
+ ]
311
+ args = metagenomics .parser_filter_bam_to_taxa (argparse .ArgumentParser ()).parse_args (args )
312
+ args .func_main (args )
313
+
314
+ expected_bam = os .path .join (input_dir ,"expected.bam" )
315
+ assert_equal_bam_reads (None , filtered_bam , expected_bam )
316
+
317
+
318
+ def test_bam_filter_by_tax_id (taxonomy_dir ):
319
+ input_dir = join (util .file .get_test_input_path (), 'TestBamFilter' )
320
+ filtered_bam = util .file .mkstempfname ('.bam' )
321
+ args = [
322
+ os .path .join (input_dir , "input.bam" ),
323
+ os .path .join (input_dir , "input.kraken-reads.tsv.gz" ),
324
+ filtered_bam ,
325
+ os .path .join (taxonomy_dir , "nodes.dmp" ),
326
+ os .path .join (taxonomy_dir , "names.dmp" ),
327
+ "--taxIDs" ,
328
+ "186538"
329
+ ]
330
+ args = metagenomics .parser_filter_bam_to_taxa (argparse .ArgumentParser ()).parse_args (args )
331
+ args .func_main (args )
325
332
326
- expected_bam = os .path .join (input_dir ,"expected.bam" )
327
- assert_equal_bam_reads (self , filtered_bam , expected_bam )
333
+ expected_bam = os .path .join (input_dir ,"expected.bam" )
334
+ assert_equal_bam_reads (None , filtered_bam , expected_bam )
0 commit comments