This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 268
/
test_parse.py
1470 lines (1336 loc) · 70.1 KB
/
test_parse.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# file test_parse.py
__author__ = "Rob Knight"
__copyright__ = "Copyright 2011, The QIIME Project"
__credits__ = ["Rob Knight", "Justin Kuczynski", "Greg Caporaso",
"Cathy Lozupone", "Jens Reeder", "Daniel McDonald",
"Jai Ram Rideout", "Will Van Treuren", "Yoshiki Vazquez-Baeza",
"Jose Antonio Navas Molina"] # remember to add yourself
__license__ = "GPL"
__version__ = "1.9.0"
__maintainer__ = "Greg Caporaso"
__email__ = "gregcaporaso@gmail.com"
from os import close
from tempfile import mkstemp
from numpy import array, nan
from StringIO import StringIO
from unittest import TestCase, main
from numpy.testing import assert_almost_equal
from skbio.util import remove_files
from skbio.io import FileFormatError
from qiime.parse import (group_by_field, group_by_fields,
parse_distmat, parse_rarefaction_record, parse_rarefaction, parse_coords,
parse_classic_otu_table, make_envs_dict, fields_to_dict,
parse_rarefaction_fname, parse_qiime_parameters, parse_qiime_config_files,
parse_bootstrap_support, parse_distmat_to_dict, parse_taxonomy,
parse_mapping_file, parse_metadata_state_descriptions,
parse_rarefaction_data, parse_illumina_line, parse_qual_score,
parse_qual_scores, QiimeParseError, parse_newick, parse_trflp,
parse_taxa_summary_table, parse_prefs_file, parse_mapping_file_to_dict,
mapping_file_to_dict, MinimalQualParser, parse_denoiser_mapping,
parse_otu_map, parse_sample_id_map, parse_taxonomy_to_otu_metadata,
is_casava_v180_or_later, MinimalSamParser)
class TopLevelTests(TestCase):
"""Tests of top-level functions"""
def setUp(self):
"""define some top-level data"""
self.l19_data = array([
[7, 1, 0, 0, 0, 0, 0, 0, 0],
[4, 2, 0, 0, 0, 1, 0, 0, 0],
[2, 4, 0, 0, 0, 1, 0, 0, 0],
[1, 7, 0, 0, 0, 0, 0, 0, 0],
[0, 8, 0, 0, 0, 0, 0, 0, 0],
[0, 7, 1, 0, 0, 0, 0, 0, 0],
[0, 4, 2, 0, 0, 0, 2, 0, 0],
[0, 2, 4, 0, 0, 0, 1, 0, 0],
[0, 1, 7, 0, 0, 0, 0, 0, 0],
[0, 0, 8, 0, 0, 0, 0, 0, 0],
[0, 0, 7, 1, 0, 0, 0, 0, 0],
[0, 0, 4, 2, 0, 0, 0, 3, 0],
[0, 0, 2, 4, 0, 0, 0, 1, 0],
[0, 0, 1, 7, 0, 0, 0, 0, 0],
[0, 0, 0, 8, 0, 0, 0, 0, 0],
[0, 0, 0, 7, 1, 0, 0, 0, 0],
[0, 0, 0, 4, 2, 0, 0, 0, 4],
[0, 0, 0, 2, 4, 0, 0, 0, 1],
[0, 0, 0, 1, 7, 0, 0, 0, 0]
])
self.l19_sample_names = [
'sam1', 'sam2', 'sam3', 'sam4', 'sam5', 'sam6',
'sam7', 'sam8', 'sam9', 'sam_middle', 'sam11', 'sam12', 'sam13',
'sam14', 'sam15', 'sam16', 'sam17', 'sam18', 'sam19']
self.l19_taxon_names = ['tax1', 'tax2', 'tax3', 'tax4', 'endbigtaxon',
'tax6', 'tax7', 'tax8', 'tax9']
self.legacy_otu_table1 = legacy_otu_table1
self.otu_table1 = otu_table1
self.otu_table_without_leading_comment = \
otu_table_without_leading_comment
self.expected_lineages1 = expected_lineages1
self.taxa_summary1 = taxa_summary1
self.taxa_summary1_expected = taxa_summary1_expected
self.otu_table1_floats = otu_table1_floats
self.files_to_remove = []
self.denoiser_mapping1 = denoiser_mapping1.split('\n')
self.sam_data1 = sam_data1.split("\n")
self.sam1_expected = sam1_expected
def tearDown(self):
remove_files(self.files_to_remove)
def test_MinimalSamParser(self):
"""MinimalSamParser functions as expected"""
actual = list(MinimalSamParser(self.sam_data1))
expected = self.sam1_expected
self.assertEqual(actual, expected)
def test_is_casava_v180_or_later(self):
""" is_casava_v180_or_later functions as expected """
# handles trailing \n
header_line = "@M00176:17:000000000-A0CNA:1:1:15487:1773 1:N:0:0\n"
self.assertTrue(is_casava_v180_or_later(header_line))
# same w no trailing \n
header_line = "@M00176:17:000000000-A0CNA:1:1:15487:1773 1:N:0:0"
self.assertTrue(is_casava_v180_or_later(header_line))
header_line = "@HWUSI-EAS552R_0357:8:1:10040:6364#0/1"
self.assertFalse(is_casava_v180_or_later(header_line))
header_line = "@ some misc junk..."
self.assertFalse(is_casava_v180_or_later(header_line))
# non-header line raises error
header_line = "HWUSI-EAS552R_0357:8:1:10040:6364#0/1"
self.assertRaises(AssertionError, is_casava_v180_or_later, header_line)
header_line = "M00176:17:000000000-A0CNA:1:1:15487:1773 1:N:0:0"
self.assertRaises(AssertionError, is_casava_v180_or_later, header_line)
def test_parse_taxa_summary_table(self):
""" parse_taxa_summary_table functions as expected """
actual = parse_taxa_summary_table(self.taxa_summary1.split('\n'))
self.assertItemsEqual(actual[0], self.taxa_summary1_expected[0])
self.assertItemsEqual(actual[1], self.taxa_summary1_expected[1])
assert_almost_equal(actual[2], self.taxa_summary1_expected[2])
def test_parse_newick(self):
"""parse_newick correctly matches escaped tip names to otu ids
"""
# confirm that it works without escaped names
t1 = ('((((tax7:0.1,tax3:0.2):.98,tax8:.3, tax4:.3):.4,'
'((tax1:0.3, tax6:.09):0.43,tax2:0.4):0.5):.2,'
'(tax9:0.3, endbigtaxon:.08));')
expected1 = ['tax7', 'tax3', 'tax8', 'tax4', 'tax1',
'tax6', 'tax2', 'tax9', 'endbigtaxon']
self.assertEqual(set(parse_newick(t1).getTipNames()), set(expected1))
self.assertEqual(set([tip.Name for tip in parse_newick(t1).tips()]),
set(expected1))
# throw some screwed up names in
t2 = ('((((tax7:0.1,tax3:0.2):.98,tax8:.3, \'tax4\':.3):.4,'
"(('ta_______ x1':0.3, tax6:.09):0.43,tax2:0.4):0.5):.2,"
'(tax9:0.3, endbigtaxon:.08));')
expected2 = ['tax7', 'tax3', 'tax8', 'tax4', 'ta_______ x1',
'tax6', 'tax2', 'tax9', 'endbigtaxon']
self.assertEqual(set(parse_newick(t2).getTipNames()), set(expected2))
self.assertEqual(set([tip.Name for tip in parse_newick(t2).tips()]),
set(expected2))
def test_parse_mapping_file(self):
"""parse_mapping_file functions as expected"""
s1 = ['#sample\ta\tb', '#comment line to skip',
'x \t y \t z ', ' ', '#more skip', 'i\tj\tk']
exp = ([['x', 'y', 'z'], ['i', 'j', 'k']],
['sample', 'a', 'b'],
['comment line to skip', 'more skip'])
obs = parse_mapping_file(s1)
self.assertEqual(obs, exp)
# We don't currently support this, but we should soon...
# check that first non-comment, non-blank line is used as
# header
# s1 = ['sample\ta\tb', '#comment line to skip',\
# 'x \t y \t z ', ' ', '#more skip', 'i\tj\tk']
# exp = ([['x','y','z'],['i','j','k']],\
# ['sample','a','b'],\
# ['comment line to skip','more skip'])
# obs = parse_mapping_file(s1)
# self.assertEqual(obs, exp)
# check that we strip double quotes by default
s2 = ['#sample\ta\tb', '#comment line to skip',
'"x "\t" y "\t z ', ' ', '"#more skip"', 'i\t"j"\tk']
obs = parse_mapping_file(s2)
self.assertEqual(obs, exp)
def test_mapping_file_to_dict(self):
"""parse_mapping_file functions as expected"""
s1 = ['#sample\ta\tb', '#comment line to skip',
'x \t y \t z ', ' ', '#more skip', 'i\tj\tk']
exp = ([['x', 'y', 'z'], ['i', 'j', 'k']],
['sample', 'a', 'b'],
['comment line to skip', 'more skip'])
mapres = parse_mapping_file(s1) # map_data, header, comments
mapdict = mapping_file_to_dict(*mapres[:2])
expdict = {'x': {'a': 'y', 'b': 'z'}, 'i': {'a': 'j', 'b': 'k'}}
self.assertEqual(mapdict, expdict)
def test_parse_mapping_file_to_dict(self):
"""parse_mapping_file functions as expected"""
s1 = ['#sample\ta\tb', '#comment line to skip',
'x \t y \t z ', ' ', '#more skip', 'i\tj\tk']
exp = ([['x', 'y', 'z'], ['i', 'j', 'k']],
['sample', 'a', 'b'],
['comment line to skip', 'more skip'])
mapdict, comments = parse_mapping_file_to_dict(s1)
expdict = {'x': {'a': 'y', 'b': 'z'}, 'i': {'a': 'j', 'b': 'k'}}
self.assertEqual(mapdict, expdict)
self.assertEqual(comments, ['comment line to skip', 'more skip'])
def test_parse_mapping_file_handles_filepath(self):
""" parse_mapping_file handles being passed a mapping filepath
"""
fd, fp = mkstemp(prefix='test_parse_mapping_file',
suffix='.txt')
close(fd)
self.files_to_remove.append(fp)
open(fp, 'w').write('\n'.join(['#sample\ta\tb',
'#comment line to skip',
'x \t y \t z ', ' ',
'#more skip',
'i\tj\tk']))
obs = parse_mapping_file(fp)
exp = ([['x', 'y', 'z'], ['i', 'j', 'k']],
['sample', 'a', 'b'],
['comment line to skip', 'more skip'])
self.assertEqual(obs, exp)
def test_parse_mapping_file_handles_file_handle(self):
""" parse_mapping_file handles being passed a mapping filepath
"""
fd, fp = mkstemp(prefix='test_parse_mapping_file',
suffix='.txt')
close(fd)
self.files_to_remove.append(fp)
open(fp, 'w').write('\n'.join(['#sample\ta\tb',
'#comment line to skip',
'x \t y \t z ', ' ',
'#more skip',
'i\tj\tk']))
obs = parse_mapping_file(open(fp))
exp = ([['x', 'y', 'z'], ['i', 'j', 'k']],
['sample', 'a', 'b'],
['comment line to skip', 'more skip'])
self.assertEqual(obs, exp)
def test_parse_mapping_file_handles_errors(self):
"""parse_mapping_file handles bad mapping files"""
# Empty file
self.assertRaises(QiimeParseError,
parse_mapping_file,
[])
# string
self.assertRaises(QiimeParseError,
parse_mapping_file,
'my_mapping_file.txt')
# invalid format (no header line with leading # sign)
self.assertRaises(QiimeParseError,
parse_mapping_file,
['sampleID\ta\tb',
'1\tf\t43',
'2\tt\t44'])
# invalid format (no non-header lines)
self.assertRaises(QiimeParseError,
parse_mapping_file,
['#sampleID\ta\tb'])
# invalid format (no header line)
self.assertRaises(QiimeParseError,
parse_mapping_file,
['1\tf\t43',
'2\tt\t44'])
def test_parse_prefs_file(self):
"""parse_prefs_file should correctly eval prefs string.
"""
# Test good input
ps1 = """{'bgcolor':'white','colors':
{'id':'blue','name':'green'},'list':[1,2,3]}"""
exp1 = {'bgcolor': 'white', 'colors': {'id': 'blue', 'name': 'green'},
'list': [1, 2, 3]}
self.assertEqual(parse_prefs_file(ps1), exp1)
# Test bad input
# list of valid input rather than multiline string should fail.
ps_bad_1 = ["{'bgcolor':'white',",
"'colors':{'id':'blue','name':'green'}",
",'list':[1,2,3]}"]
self.assertRaises(QiimeParseError, parse_prefs_file, ps_bad_1)
# bad data. Can be evaluated but not a dict.
ps_bad_2 = "[1,2,3]"
self.assertRaises(QiimeParseError, parse_prefs_file, ps_bad_2)
def test_group_by_field(self):
"""group_by_field should group table by fields"""
t = [
['#sample', 'loc', 'age'],
['a', 'US', '5'],
['b', 'US', '10'],
['c', 'Mal', '5'],
['d', 'Mal', '10'],
['e', 'Ven', '5'],
]
self.assertEqual(group_by_field(t, 'loc'),
{'US': ['a', 'b'], 'Mal': ['c', 'd'], 'Ven': ['e']})
self.assertEqual(group_by_field(t, 'age'),
{'5': ['a', 'c', 'e'], '10': ['b', 'd']})
def test_group_by_fields(self):
"""group_by_fields should group table by fields"""
t = [
['#sample', 'loc', 'age', 'mal'],
['a', 'US', '5', 'n'],
['b', 'US', '10', 'n'],
['c', 'Mal', '5', 'y'],
['d', 'Mal', '10', 'n'],
['e', 'Mal', '5', 'y'],
]
self.assertEqual(group_by_fields(t, ['age', 'loc']),
{('5', 'US'): ['a'], ('10', 'US'): ['b'], ('5', 'Mal'): ['c', 'e'],
('10', 'Mal'): ['d']})
def test_parse_distmat(self):
"""parse_distmat should read distmat correctly"""
lines = """\ta\tb\tc
a\t0\t1\t2
b\t1\t0\t3.5
c\t1\t3.5\t0
""".splitlines()
exp = (['a', 'b', 'c'], array([[0, 1, 2], [1, 0, 3.5], [1, 3.5, 0]]))
obs = parse_distmat(lines)
self.assertEqual(obs[0], exp[0])
assert_almost_equal(obs[1], exp[1])
def test_parse_distmat_to_dict(self):
"""parse_distmat should return dict of distmat"""
lines = """\ta\tb\tc
a\t0\t1\t2
b\t1\t0\t3.5
c\t1\t3.5\t0
""".splitlines()
exp = {'a': {'a': 0.0, 'c': 2.0, 'b': 1.0},
'c': {'a': 1.0, 'c': 0.0, 'b': 3.5},
'b': {'a': 1.0, 'c': 3.5, 'b': 0.0}}
obs = parse_distmat_to_dict(lines)
self.assertEqual(obs, exp)
# should raise error because row and column headers don't match
wrong_dist_mat = """\ta\ty\tx
a\t0\t1\t2
b\t1\t0\t3.5
c\t1\t3.5\t0
""".splitlines()
self.failUnlessRaises(
AssertionError,
parse_distmat_to_dict,
wrong_dist_mat)
def test_parse_bootstrap_support(self):
"""parse_distmat should read distmat correctly"""
input_txt = """#\ta\tb\tc.
#more comments here
node2\t0
17node\t0.11922
"""
lines = input_txt.splitlines()
exp = {'17node': 0.11922, 'node2': 0.00}
obs = parse_bootstrap_support(lines)
self.assertItemsEqual(obs, exp)
def test_parse_rarefaction_data(self):
self.data = {}
self.data['headers'] = ['PD_whole_tree.txt', 'Antibiotics']
self.data['error'] = {'NA': [0.099969643842700004],
'Y':
[0.105669693476,
1.08546135424,
1.5626248357999999],
'N': [0.101173002974]}
self.data['options'] = ['Y', 'NA', 'N']
self.data['xaxis'] = [10.0, 310.0, 610.0, 910.0, 1210.0, 1510.0,
1810.0, 2110.0, 2410.0, 2710.0, 3010.0]
self.data['series'] = {'NA': [0.88581050485400004],
'Y':
[0.918845147059,
7.1758656176500004,
9.9186072941199992],
'N': [0.92636763785999998]}
self.data['color'] = {'NA': '#00ff00', 'Y': '#ff0000', 'N': '#0000ff'}
self.rarefaction_series_data = ['# PD_whole_tree.txt',
'# Antibiotics',
'xaxis: 10.0\t310.0\t610.0\t910.0\t1210.0\t1510.0\t1810.0\t2110.0\
\t2410.0\t2710.0\t3010.0\t',
'xmax: 3310.0',
'>> Y',
'color #ff0000',
'series 0.918845147059\t7.17586561765\t9.91860729412\t',
'error 0.105669693476\t1.08546135424\t1.5626248358\t',
'>> NA',
'color #00ff00',
'series 0.885810504854\t',
'error 0.0999696438427\t',
'>> N',
'color #0000ff',
'series 0.92636763786\t',
'error 0.101173002974'
]
test = parse_rarefaction_data(self.rarefaction_series_data)
self.assertEqual(test, self.data)
def test_parse_rarefaction_record(self):
self.rarefactionline1 = 'rare10.txt\t10\t0\t1.99181\t0.42877\t2.13996'
test1 = parse_rarefaction_record(self.rarefactionline1)
self.rarefactiondata1 = ('rare10.txt', [10.0, 0.0,
1.9918100000000001, 0.42876999999999998, 2.1399599999999999])
self.assertEqual(self.rarefactiondata1, test1)
self.rarefactionline2 = 'rare10.txt\t10\t0\t1.99181\t0.42877\tNA'
test2 = parse_rarefaction_record(self.rarefactionline2)
self.rarefactiondata2 = ('rare10.txt', [10.0, 0.0, 1.9918100000000001,
0.42876999999999998, nan])
self.assertEqual(self.rarefactiondata2, test2)
def test_parse_rarefaction_fname(self):
""" parse_rarefaction_fname should return base, seqs/sam, iters, etc."""
fname = "alpha_rarefaction_900_3.txt"
base, seqs, iter, ext = parse_rarefaction_fname(fname)
self.assertEqual((base, seqs, iter, ext),
("alpha_rarefaction", 900, 3, ".txt"))
def test_parse_rarefaction(self):
self.rarefactionfile = [
'\tsequences per sample\titeration\t123\t234\t345',
'rare10.txt\t10\t0\t1.99181\t0.42877\t2.13996',
'rare10.txt\t10\t1\t2.07163\t0.42877\t2.37055',
'rare310.txt\t310\t0\t8.83115\t0.42877\t11.00725',
'rare310.txt\t310\t1\t10.05242\t0.42877\t8.24474',
'rare610.txt\t610\t0\t12.03067\t0.42877\t11.58928',
'rare610.txt\t610\t1\t12.9862\t0.42877\t11.58642']
self.col_headers = [
'',
'sequences per sample',
'iteration',
'123',
'234',
'345']
self.comments = []
self.rarefaction_fns = [
'rare10.txt',
'rare10.txt',
'rare310.txt',
'rare310.txt',
'rare610.txt',
'rare610.txt']
self.rarefaction_data = [[10.0,
0.0,
1.9918100000000001,
0.42876999999999998,
2.1399599999999999],
[10.0,
1.0,
2.0716299999999999,
0.42876999999999998,
2.3705500000000002],
[310.0,
0.0,
8.8311499999999992,
0.42876999999999998,
11.007250000000001],
[310.0,
1.0,
10.05242,
0.42876999999999998,
8.2447400000000002],
[610.0,
0.0,
12.030670000000001,
0.42876999999999998,
11.58928],
[610.0,
1.0,
12.9862,
0.42876999999999998,
11.58642]]
test_col_headers, test_comments, test_rarefaction_fns, test_rarefaction_data = parse_rarefaction(
self.rarefactionfile)
self.assertEqual(test_col_headers, self.col_headers)
self.assertEqual(test_comments, self.comments)
self.assertEqual(test_rarefaction_fns, self.rarefaction_fns)
self.assertEqual(test_rarefaction_data, self.rarefaction_data)
# def test_parse_rarefaction(self):
# """parse_rarefaction should handle multiple recs"""
# recs ="""#HEADER 97.0 NFkeyRightShift 1000
# CHAO1 288.12903 241.27093 371.41733
# ACE 294.74813 252.75930 361.30606 0.68654
# SHANNON 3.71822 3.61146 3.82499
# SIMPSON 0.08021
# n rare rare_lci rare_hci
# 1 1.000000 1.000000 1.000000
# 51 26.878000 26.032290 27.723710
# HEADER 97.0 DMkeySpace 1000
# CHAO1 90.20000 53.81120 196.98011
# ACE 122.66234 68.48901 264.46888 1.53571
# SHANNON 1.35156 1.12549 1.57762
# SIMPSON 0.56002
# n rare rare_lci rare_hci
# 1 1.000000 1.000000 1.000000
# 51 10.707000 10.085986 11.328014
# 101 17.547000 17.046632 18.047368
# 151 23.410000 23.009805 23.810195
# HEADER 97.0 RKkeyW 1000
# CHAO1 251.57143 176.86438 401.83780
# ACE 264.44294 192.23364 395.08515 0.89714
# SHANNON 1.56521 1.44411 1.68630
# SIMPSON 0.54772
# n rare rare_lci rare_hci
# 1 1.000000 1.000000 1.000000
# 51 11.739000 11.060658 12.417342
# 101 19.054000 18.458137 19.649863
# 151 24.994000 24.447174 25.540826
# """.splitlines()
# obs = parse_rarefaction(recs)
# self.assertEqual(set(obs.keys()), \
# set(['NFkeyRightShift','DMkeySpace','RKkeyW']))
def test_parse_coords(self):
"""parse_coords should handle coords file"""
coords = ["Eigvals\t3",
"4.94\t1.79\t1.50",
"",
"Proportion explained\t3",
"14.3\t5.2\t4.3",
"",
"Species\t0\t0",
"",
"Site\t3\t3",
"A\t.11\t.09\t.23",
"B\t.03\t.07\t-.26",
"C\t.12\t.06\t-.32",
"",
"Biplot\t0\t0",
"",
"Site constraints\t0\t0"]
obs = parse_coords(StringIO("\n".join(coords)))
exp = (['A', 'B', 'C'],
array([[.11, .09, .23], [.03, .07, -.26], [.12, .06, -.32]]),
array([4.94, 1.79, 1.50]),
array([14.3, 5.2, 4.3]))
self.assertEqual(obs[0], exp[0])
assert_almost_equal(obs[1], exp[1])
def test_parse_coords_exceptions(self):
"""Check exceptions are raised accordingly with missing information"""
# missing eigenvalues line
with self.assertRaises(FileFormatError):
out = parse_coords(StringIO(COORDS_NO_EIGENVALS))
# missing percentages explained line
with self.assertRaises(FileFormatError):
out = parse_coords(StringIO(COORDS_NO_PCNTS))
# missing vector number line
with self.assertRaises(FileFormatError):
out = parse_coords(StringIO(COORDS_NO_VECTORS))
# a whole different file (taxa summary)
with self.assertRaises(FileFormatError):
out = parse_coords(StringIO(taxa_summary1))
def test_parse_classic_otu_table_legacy(self):
"""parse_classic_otu_table functions as expected with legacy OTU table
"""
data = self.legacy_otu_table1
data_f = (data.split('\n'))
obs = parse_classic_otu_table(data_f)
exp = (['Fing', 'Key', 'NA'],
['0', '1', '2', '3', '4'],
array([[19111, 44536, 42], [1216, 3500, 6], [1803, 1184, 2],
[1722, 4903, 17], [589, 2074, 34]]),
self.expected_lineages1)
self.assertEqual(obs[0], exp[0])
self.assertEqual(obs[1], exp[1])
assert_almost_equal(obs[2], exp[2])
self.assertEqual(obs[3], exp[3])
def test_parse_classic_otu_table(self):
"""parse_classic_otu_table functions as expected with new-style OTU table
"""
data = self.otu_table1
data_f = (data.split('\n'))
obs = parse_classic_otu_table(data_f)
exp = (['Fing', 'Key', 'NA'],
['0', '1', '2', '3', '4'],
array([[19111, 44536, 42], [1216, 3500, 6], [1803, 1184, 2],
[1722, 4903, 17], [589, 2074, 34]]),
self.expected_lineages1)
self.assertEqual(obs[0], exp[0])
self.assertEqual(obs[1], exp[1])
assert_almost_equal(obs[2], exp[2])
self.assertEqual(obs[3], exp[3])
# test that the modified parse_classic performs correctly on OTU tables
# without leading comments
data = self.otu_table_without_leading_comment
data_f = (data.split('\n'))
obs = parse_classic_otu_table(data_f)
sams = ['let-7i', 'miR-7', 'miR-17n', 'miR-18a', 'miR-19a', 'miR-22',
'miR-25', 'miR-26a']
otus = ['A2M', 'AAAS', 'AACS', 'AADACL1']
vals = array([
[-0.2, 0.03680505, 0.205, 0.23, 0.66, 0.08, -0.373, 0.26],
[-0.09, -0.25, 0.274, 0.15, 0.12, 0.29, 0.029, -0.1148452],
[0.33, 0.19, 0.27, 0.28, 0.19, 0.25, 0.089, 0.14],
[0.49, -0.92, -0.723, -0.23, 0.08, 0.49, -0.386, -0.64]])
exp = (sams, otus, vals, []) # no lineages
# because float comps in arrays always errors
self.assertEqual(obs[0], exp[0])
self.assertEqual(obs[1], exp[1])
self.assertEqual(obs[3], exp[3])
self.assertTrue(all((obs[2] == exp[2]).tolist()))
def test_parse_classic_otu_table_floats_in_table(self):
"""parse_classic_otu_table functions using an OTU table containing floats
but cast as int....this will automatically cast into floats"""
data = self.otu_table1_floats
data_f = (data.split('\n'))
obs = parse_classic_otu_table(data_f)
exp = (['Fing', 'Key', 'NA'],
['0', '1', '2', '3', '4'],
array([[19111.0, 44536.0, 42.0], [1216.0, 3500.0, 6.0],
[1803.0, 1184.0, 2.0], [1722.1, 4903.2, 17.0],
[589.6, 2074.4, 34.5]]),
self.expected_lineages1)
self.assertEqual(obs[0], exp[0])
self.assertEqual(obs[1], exp[1])
assert_almost_equal(obs[2], exp[2])
self.assertEqual(obs[3], exp[3])
def test_parse_classic_otu_table_float_counts(self):
"""parse_classic_otu_table should return correct result from small table"""
data = """#Full OTU Counts
#OTU ID Fing Key NA Consensus Lineage
0 19111 44536 42 Bacteria; Actinobacteria; Actinobacteridae; Propionibacterineae; Propionibacterium
1 1216 3500 6 Bacteria; Firmicutes; Alicyclobacillaceae; Bacilli; Lactobacillales; Lactobacillales; Streptococcaceae; Streptococcus
2 1803 1184 2 Bacteria; Actinobacteria; Actinobacteridae; Gordoniaceae; Corynebacteriaceae
3 1722 4903 17 Bacteria; Firmicutes; Alicyclobacillaceae; Bacilli; Staphylococcaceae
4 589 2074 34 Bacteria; Cyanobacteria; Chloroplasts; vectors"""
data_f = (data.split('\n'))
obs = parse_classic_otu_table(data_f, count_map_f=float)
exp = (['Fing', 'Key', 'NA'],
['0', '1', '2', '3', '4'],
array(
[[19111., 44536., 42.], [1216., 3500., 6.], [1803., 1184., 2.],
[1722., 4903., 17.], [589, 2074., 34.]]),
[['Bacteria', 'Actinobacteria', 'Actinobacteridae', 'Propionibacterineae', 'Propionibacterium'],
['Bacteria',
'Firmicutes',
'Alicyclobacillaceae',
'Bacilli',
'Lactobacillales',
'Lactobacillales',
'Streptococcaceae',
'Streptococcus'],
['Bacteria',
'Actinobacteria',
'Actinobacteridae',
'Gordoniaceae',
'Corynebacteriaceae'],
['Bacteria',
'Firmicutes',
'Alicyclobacillaceae',
'Bacilli',
'Staphylococcaceae'],
['Bacteria', 'Cyanobacteria', 'Chloroplasts', 'vectors']])
self.assertEqual(obs[0], exp[0])
self.assertEqual(obs[1], exp[1])
assert_almost_equal(obs[2], exp[2])
self.assertEqual(obs[3], exp[3])
def test_parse_classic_otu_table_file(self):
"""parse_classic_otu_table should return correct result on fileio format object"""
data = """#Full OTU Counts
#OTU ID Fing Key NA Consensus Lineage
0 19111 44536 42 Bacteria; Actinobacteria; Actinobacteridae; Propionibacterineae; Propionibacterium
1 1216 3500 6 Bacteria; Firmicutes; Alicyclobacillaceae; Bacilli; Lactobacillales; Lactobacillales; Streptococcaceae; Streptococcus
2 1803 1184 2 Bacteria; Actinobacteria; Actinobacteridae; Gordoniaceae; Corynebacteriaceae
3 1722 4903 17 Bacteria; Firmicutes; Alicyclobacillaceae; Bacilli; Staphylococcaceae
4 589 2074 34 Bacteria; Cyanobacteria; Chloroplasts; vectors"""
data_f = StringIO(data)
obs = parse_classic_otu_table(data_f)
exp = (['Fing', 'Key', 'NA'],
['0', '1', '2', '3', '4'],
array([[19111, 44536, 42], [1216, 3500, 6], [1803, 1184, 2],
[1722, 4903, 17], [589, 2074, 34]]),
[['Bacteria', 'Actinobacteria', 'Actinobacteridae', 'Propionibacterineae', 'Propionibacterium'],
['Bacteria',
'Firmicutes',
'Alicyclobacillaceae',
'Bacilli',
'Lactobacillales',
'Lactobacillales',
'Streptococcaceae',
'Streptococcus'],
['Bacteria',
'Actinobacteria',
'Actinobacteridae',
'Gordoniaceae',
'Corynebacteriaceae'],
['Bacteria',
'Firmicutes',
'Alicyclobacillaceae',
'Bacilli',
'Staphylococcaceae'],
['Bacteria', 'Cyanobacteria', 'Chloroplasts', 'vectors']])
self.assertEqual(obs[0], exp[0])
self.assertEqual(obs[1], exp[1])
assert_almost_equal(obs[2], exp[2])
self.assertEqual(obs[3], exp[3])
def test_parse_classic_otu_table_consensus_lineage(self):
"""parse_classic_otu_table should accept 'consensusLineage'"""
data = """#Full OTU Counts
#OTU ID Fing Key NA consensusLineage
0 19111 44536 42 Bacteria; Actinobacteria; Actinobacteridae; Propionibacterineae; Propionibacterium
1 1216 3500 6 Bacteria; Firmicutes; Alicyclobacillaceae; Bacilli; Lactobacillales; Lactobacillales; Streptococcaceae; Streptococcus
2 1803 1184 2 Bacteria; Actinobacteria; Actinobacteridae; Gordoniaceae; Corynebacteriaceae
3 1722 4903 17 Bacteria; Firmicutes; Alicyclobacillaceae; Bacilli; Staphylococcaceae
4 589 2074 34 Bacteria; Cyanobacteria; Chloroplasts; vectors"""
data_f = StringIO(data)
obs = parse_classic_otu_table(data_f)
exp = (['Fing', 'Key', 'NA'],
['0', '1', '2', '3', '4'],
array([[19111, 44536, 42], [1216, 3500, 6], [1803, 1184, 2],
[1722, 4903, 17], [589, 2074, 34]]),
[['Bacteria', 'Actinobacteria', 'Actinobacteridae', 'Propionibacterineae', 'Propionibacterium'],
['Bacteria',
'Firmicutes',
'Alicyclobacillaceae',
'Bacilli',
'Lactobacillales',
'Lactobacillales',
'Streptococcaceae',
'Streptococcus'],
['Bacteria',
'Actinobacteria',
'Actinobacteridae',
'Gordoniaceae',
'Corynebacteriaceae'],
['Bacteria',
'Firmicutes',
'Alicyclobacillaceae',
'Bacilli',
'Staphylococcaceae'],
['Bacteria', 'Cyanobacteria', 'Chloroplasts', 'vectors']])
self.assertEqual(obs[0], exp[0])
self.assertEqual(obs[1], exp[1])
assert_almost_equal(obs[2], exp[2])
self.assertEqual(obs[3], exp[3])
def test_make_envs_dict(self):
""" make_envs_dict should have the same abundance for each taxon
as the matrix that made the dict"""
envs = make_envs_dict(self.l19_data, self.l19_sample_names,
self.l19_taxon_names)
for key in envs.keys():
col_idx = self.l19_taxon_names.index(key)
self.assertEqual(sum(envs[key].values()),
self.l19_data[:, col_idx].sum())
def test_fields_to_dict(self):
"""fields_to_dict should make first field key, rest val"""
test_data = \
"""0 R27DLI_4812 R27DLI_600 R27DLI_727 U1PLI_403 U1PLI_8969 U1PLI_9080 U1PLI_9526 W3Cecum_6642 W3Cecum_8992
1 U1PLI_7889
2 W3Cecum_4858
3 R27DLI_3243 R27DLI_4562 R27DLI_6828 R27DLI_9097 U1PLI_2780 U1PLI_67 U9PSI_10475 U9PSI_4341 W3Cecum_5191""".splitlines() # output from cd-hit
obs = fields_to_dict(test_data)
exp = {
'0': ['R27DLI_4812', 'R27DLI_600', 'R27DLI_727', 'U1PLI_403',
'U1PLI_8969', 'U1PLI_9080', 'U1PLI_9526', 'W3Cecum_6642', 'W3Cecum_8992'],
'1': ['U1PLI_7889'],
'2': ['W3Cecum_4858'],
'3': ['R27DLI_3243', 'R27DLI_4562', 'R27DLI_6828', 'R27DLI_9097', 'U1PLI_2780', 'U1PLI_67', 'U9PSI_10475', 'U9PSI_4341', 'W3Cecum_5191']}
self.assertEqual(obs, exp)
def test_parse_qiime_parameters(self):
"""parse_qiime_parameters: functions with valid input """
lines = ["#Don't edit this file!",
"pick_otus:similarity 0.94#this is not a comment...",
"pick_otus:otu_picking_method\tcdhit # useful comment ",
"align_seqs:verbose",
"assign_taxonomy:use_rdp\ttRuE # another great ## comment!",
"assign_taxonomy:something\tNone",
"",
"#some_script:fake_parameter\t99.0",
'summarize_taxa:md_identifier "Consensus Lineage"']
actual = parse_qiime_parameters(lines)
expected = {'pick_otus':
{'similarity': '0.94#this is not a comment...',
'otu_picking_method': 'cdhit'},
'assign_taxonomy':
{'use_rdp': None},
'summarize_taxa':
{'md_identifier': '"Consensus Lineage"'}}
self.assertEqual(actual, expected)
# default dict functions as expected -- looking up non-existant key
# returns empty dict
self.assertEqual(actual['some_other_script'], {})
def test_parse_taxonomy(self):
""" should parse taxonomy example, keeping otu id only"""
example_tax = \
"""412 PC.635_647 Root;Bacteria;Firmicutes; "Clostridia";Clostridiales 0.930
319 PC.355_281 Root;Bacteria;Bacteroidetes 0.970
353 PC.634_154 Root; Bacteria ; Bacteroidetes 0.830
17 PC.607_302 Root;Bacteria;Bacteroidetes 0.960
13 PC.481_1214 Root;Bacteria;Firmicutes;"Clostridia";Clostridiales 0.870
338 PC.593_1314 Root; Bacteria 0.990 42556 Additional fields ignored"""
res = parse_taxonomy(example_tax.split('\n'))
self.assertEqual(res['412'],
["Root", "Bacteria", "Firmicutes", "\"Clostridia\"", "Clostridiales"])
self.assertEqual(res['338'],
["Root", "Bacteria"])
def test_parse_taxonomy_to_otu_metadata(self):
"""parsing of taxonomy file to otu metadata format functions as expected
"""
example_tax = \
"""412 PC.635_647 Root;Bacteria;Firmicutes;"Clostridia";Clostridiales 0.930
319 PC.355_281 Root;Bacteria;Bacteroidetes 0.970
353 PC.634_154 Root;Bacteria;Bacteroidetes 0.830
17 PC.607_302 Root;Bacteria;Bacteroidetes 0.960
13 Root;Bacteria;Firmicutes;"Clostridia";Clostridiales 0.870
338 PC.593_1314 Root;Bacteria 0.990"""
actual = parse_taxonomy_to_otu_metadata(example_tax.split('\n'))
expected = {
'412': {'taxonomy': ['Root', 'Bacteria', 'Firmicutes',
'"Clostridia"', 'Clostridiales'], 'score': 0.930},
'319':
{'taxonomy': ['Root',
'Bacteria',
'Bacteroidetes'],
'score': 0.970},
'353':
{'taxonomy': ['Root',
'Bacteria',
'Bacteroidetes'],
'score': 0.830},
'17':
{'taxonomy': ['Root',
'Bacteria',
'Bacteroidetes'],
'score': 0.960},
'13': {'taxonomy': ['Root', 'Bacteria', 'Firmicutes',
'"Clostridia"', 'Clostridiales'], 'score': 0.870},
'338': {'taxonomy': ['Root', 'Bacteria'], 'score': 0.990}}
self.assertEqual(actual, expected)
def test_parse_taxonomy_to_otu_metadata_alt_labels(self):
"""parsing of taxonomy file to otu metadata format functions as expected
"""
def f(v):
return 1. + float(v)
example_tax = \
"""412 PC.635_647 0.0
319 PC.355_281 0.970
353 PC.634_154 0.830
17 PC.607_302 0.960
13 0.870
338 PC.593_1314 0.990"""
actual = parse_taxonomy_to_otu_metadata(
example_tax.split('\n'),
labels=['something'],
process_fs=[f])
expected = {'412': {'something': 1.0},
'319': {'something': 1.970},
'353': {'something': 1.830},
'17': {'something': 1.960},
'13': {'something': 1.870},
'338': {'something': 1.990}}
self.assertEqual(actual, expected)
def test_parse_taxonomy_to_otu_metadata_extra_fields_ignored(self):
"""parsing of taxonomy file to otu metadata format functions as expected
"""
example_tax = \
"""412 PC.635_647 Root;Bacteria;Firmicutes;"Clostridia";Clostridiales 0.930
319 PC.355_281 Root;Bacteria;Bacteroidetes some text
353 PC.634_154 Root;Bacteria;Bacteroidetes 0.830
17 PC.607_302 Root;Bacteria;Bacteroidetes 0.960
13 Root;Bacteria;Firmicutes;"Clostridia";Clostridiales 0.870
338 PC.593_1314 Root;Bacteria 0.990 42556 Additional fields ignored"""
expected = {
'412':
{'taxonomy': ['Root',
'Bacteria',
'Firmicutes',
'"Clostridia"',
'Clostridiales']},
'319': {'taxonomy': ['Root', 'Bacteria', 'Bacteroidetes']},
'353': {'taxonomy': ['Root', 'Bacteria', 'Bacteroidetes']},
'17': {'taxonomy': ['Root', 'Bacteria', 'Bacteroidetes']},
'13':
{'taxonomy': ['Root',
'Bacteria',
'Firmicutes',
'"Clostridia"',
'Clostridiales']},
'338': {'taxonomy': ['Root', 'Bacteria']}}
actual = parse_taxonomy_to_otu_metadata(
example_tax.split('\n'),
labels=['taxonomy'])
self.assertEqual(actual, expected)
def test_parse_taxonomy_to_otu_metadata_invalid_input(self):
"""parsing of taxonomy file to otu metadata format fails when too few functions
"""
example_tax = \
"""412 PC.635_647 Root;Bacteria;Firmicutes;"Clostridia";Clostridiales 0.930
319 PC.355_281 Root;Bacteria;Bacteroidetes 0.970
353 PC.634_154 Root;Bacteria;Bacteroidetes 0.830
17 PC.607_302 Root;Bacteria;Bacteroidetes 0.960
13 Root;Bacteria;Firmicutes;"Clostridia";Clostridiales 0.870
338 PC.593_1314 Root;Bacteria 0.990"""
self.assertRaises(
ValueError,
parse_taxonomy_to_otu_metadata,
example_tax.split('\n'),
labels=['taxonomy',
'score'],
process_fs=[str])
def test_parse_qiime_config_files(self):
""" parse_qiime_config_files functions as expected """
fake_file1 = ['key1\tval1', 'key2 val2']
fake_file2 = ['key2\tval3']
actual = parse_qiime_config_files([fake_file1, fake_file2])
expected = {'key1': 'val1', 'key2': 'val3'}
self.assertEqual(actual, expected)
# looking up a non-existant value returns None
self.assertEqual(actual['fake_key'], None)
# empty dict on empty input
self.assertEqual(parse_qiime_config_files([]), {})
# test with an env variable - if it gets expanded
# there won't be a $ in the output
fake_file3 = ['key2\t$HOME', 'key3\thello $HOME']
actual = parse_qiime_config_files([fake_file3])
self.assertTrue('$' not in actual['key2'])
self.assertTrue('$' not in actual['key3'])
def test_parse_metadata_state_descriptions(self):
"""parse_metadata_state_descriptions should return correct states from string."""
s = ''
self.assertEqual(parse_metadata_state_descriptions(s), {})
s = 'Study:Twin,Hand,Dog;BodySite:Palm,Stool'
self.assertEqual(
parse_metadata_state_descriptions(
s), {'Study': set(['Twin', 'Hand', 'Dog']),
'BodySite': set(['Palm', 'Stool'])})
# category names with colons i. e. ontology-derived
s = 'Study:Twin,Hand,Dog;site:UBERON:feces,UBERON:ear canal;' +\
'env_feature:ENVO:farm soil,ENVO:national park'
self.assertEqual(parse_metadata_state_descriptions(s), {'Study':
set([
'Twin', 'Hand', 'Dog']), 'site': set(['UBERON:feces',
'UBERON:ear canal']), 'env_feature': set(['ENVO:farm soil',
'ENVO:national park'])})
s = "Treatment:A,B,C;env_matter:ENVO:nitsol,ENVO:farm soil;env_biom:" +\
"ENVO:Tropical dry (including Monsoon forests) and woodlands," +\
"ENVO:Forest: including woodlands;country:GAZ:Persnickety Islands" +\
",St. Kitt's and Nevis"
self.assertEqual(parse_metadata_state_descriptions(s), {"country":
set([
"GAZ:Persnickety Islands", "St. Kitt's and Nevis"]),
"env_biom": set(["ENVO:Tropical dry (including Monsoon forests) " +
"and woodlands", "ENVO:Forest: including woodlands"]), "env_matter":
set([
"ENVO:nitsol", "ENVO:farm soil"]), 'Treatment': set(["A", "B",
"C"])})
def test_parse_illumina_line_barcode_in_header(self):
"""parse_illumina_line: handles barcode in header correctly """
illumina_line0 = illumina_read1[0]
illumina_line1 = illumina_read1[1]
actual = parse_illumina_line(
illumina_line0, barcode_length=6, rev_comp_barcode=True)
expected = {
'Full description': 'HWI-6X_9267:1:1:4:1699#ACCACCC/1',
'Machine Name': 'HWI-6X_9267',
'Channel Number': 1,
'Tile Number': 1,
'X Position': 4,
'Y Position': 1699,
'Barcode': 'GGTGGT',
'Full Y Position Field': '1699#ACCACCC/1',
'Sequence':
'TACGGAGGGTGCGAGCGTTAATCGCCCCCCCCCCCCCCCCCCCCCCCCCCCC' +
'CCCCCCCCCCCCCCCCCCCCCCCGAAAAAAAAAAAAAAAAAAAAAAA',
'Quality Score':