This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
/
test_validate_mapping_file.py
executable file
·1566 lines (1256 loc) · 83 KB
/
test_validate_mapping_file.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
__author__ = "William Walters"
__copyright__ = "Copyright 2011, The QIIME Project" # consider project name
__credits__ = ["Rob Knight", "William Walters"] # remember to add yourself
__license__ = "GPL"
__version__ = "1.9.0"
__maintainer__ = "William Walters"
__email__ = "william.a.walters@colorado.edu"
from os import close
from os.path import isdir, isfile, exists, join, basename
from tempfile import mkdtemp
from shutil import rmtree
from collections import defaultdict
from tempfile import mkstemp, mkdtemp
from unittest import TestCase, main
from skbio.util import remove_files
from skbio.util import create_dir
from qiime.check_id_map import (check_mapping_file, process_id_map,
check_data_fields, check_fields_past_bounds, check_chars_data_fields,
check_dna_chars_primers, check_dna_chars_bcs, check_bcs_lengths,
check_bc_duplicates, check_fixed_len_bcs_dups, check_variable_len_bcs_dups,
check_added_demultiplex_dups, check_sampleid_duplicates, check_header,
check_header_dups, check_header_chars, check_header_required_fields,
correct_mapping_data)
class CheckIdMapTests(TestCase):
def setUp(self):
# create the temporary input files that will be used
self._files_to_remove = []
# Input data
self.sample_correct_mapping_data = sample_correct_mapping_data
self.sample_errors_mapping_data = sample_errors_mapping_data
self.sample_warnings_mapping_data = sample_warnings_mapping_data
self.sample_errors_warnings_mapping_data =\
sample_errors_warnings_mapping_data
self.empty_fields_mapping_data = empty_fields_mapping_data
fd, self.correct_mapping_fp = mkstemp(
prefix='correct_mapping_',
suffix='.txt')
close(fd)
map_file = open(self.correct_mapping_fp, 'w')
map_file.write(self.sample_correct_mapping_data)
map_file.close()
fd, self.errors_mapping_fp = mkstemp(
prefix='errors_mapping_',
suffix='.txt')
close(fd)
map_file = open(self.errors_mapping_fp, 'w')
map_file.write(self.sample_errors_mapping_data)
map_file.close()
fd, self.empty_fields_fp = mkstemp(
prefix='empty_fields_',
suffix='.txt')
close(fd)
map_file = open(self.empty_fields_fp, 'w')
map_file.write(self.empty_fields_mapping_data)
map_file.close()
fd, self.warnings_mapping_fp = mkstemp(
prefix='warnings_mapping_',
suffix='.txt')
close(fd)
map_file = open(self.warnings_mapping_fp, 'w')
map_file.write(self.sample_warnings_mapping_data)
map_file.close()
fd, self.errors_warnings_mapping_fp = mkstemp(
prefix='errors_warnings_mapping_',
suffix='.txt')
close(fd)
map_file = open(self.errors_warnings_mapping_fp, 'w')
map_file.write(self.sample_errors_warnings_mapping_data)
map_file.close()
# Output data
self.expected_html_data_correct_input = expected_html_data_correct_input
self.expected_corrected_data_correct_input =\
expected_corrected_data_correct_input
self.expected_log_data_correct_input = expected_log_data_correct_input
self.expected_html_errors_output = expected_html_errors_output
self.expected_data_errors_corrected_output =\
expected_data_errors_corrected_output
self.expected_data_log_errors_output = expected_data_log_errors_output
self.expected_html_errors_suppressed_bcs =\
expected_html_errors_suppressed_bcs
self.expected_output_log_errors_bcs_suppressed =\
expected_output_log_errors_bcs_suppressed
self.expected_html_output_warnings = expected_html_output_warnings
self.expected_corrected_warnings_output =\
expected_corrected_warnings_output
self.expected_log_warnings_output =\
expected_log_warnings_output
self.expected_html_errors_warnings_output =\
expected_html_errors_warnings_output
self.expected_corrected_data_errors_warnings =\
expected_corrected_data_errors_warnings
self.expected_log_errors_warnings_output =\
expected_log_errors_warnings_output
self.output_dir = mkdtemp()
self.output_dir += '/'
create_dir(self.output_dir)
self._files_to_remove =\
[self.correct_mapping_fp, self.errors_mapping_fp,
self.warnings_mapping_fp, self.errors_warnings_mapping_fp,
self.empty_fields_fp]
def tearDown(self):
if self._files_to_remove:
remove_files(self._files_to_remove)
if exists(self.output_dir):
rmtree(self.output_dir)
def test_check_mapping_file_correct_file(self):
""" Gives proper files for valid mapping files """
# Use valid data, default parameters
check_mapping_file(mapping_fp=self.correct_mapping_fp,
output_dir=self.output_dir,
verbose=False)
# Check existence of expected output files
output_html_fp = join(self.output_dir,
basename(self.correct_mapping_fp).replace('.txt', '.html'))
output_corrected_fp =\
join(self.output_dir,
basename(self.correct_mapping_fp).replace('.txt', '_corrected.txt'))
output_log_fp =\
join(self.output_dir,
basename(self.correct_mapping_fp).replace('.txt', '.log'))
overlib_js_fp = join(self.output_dir, 'overlib.js')
self.assertTrue(exists(output_html_fp))
self.assertTrue(exists(output_corrected_fp))
self.assertTrue(exists(output_log_fp))
self.assertTrue(exists(overlib_js_fp))
# Check output data for expected results
html_data = "".join([line for line in open(output_html_fp, "U")])
corrected_data =\
"".join([line for line in open(output_corrected_fp, "U")])
log_data = "".join([line for line in open(output_log_fp, "U")])
self.assertEqual(html_data, self.expected_html_data_correct_input)
self.assertEqual(corrected_data,
self.expected_corrected_data_correct_input)
self.assertEqual(log_data, self.expected_log_data_correct_input)
# With additional parameters added should not change results using
# same valid input data
check_mapping_file(mapping_fp=self.correct_mapping_fp,
output_dir=self.output_dir,
has_barcodes=True,
char_replace="A",
verbose=False,
variable_len_barcodes=True,
disable_primer_check=True,
added_demultiplex_field=None)
# Check existence of expected output files
output_html_fp = join(self.output_dir,
basename(self.correct_mapping_fp).replace('.txt', '.html'))
output_corrected_fp =\
join(self.output_dir,
basename(self.correct_mapping_fp).replace('.txt', '_corrected.txt'))
output_log_fp =\
join(self.output_dir,
basename(self.correct_mapping_fp).replace('.txt', '.log'))
overlib_js_fp = join(self.output_dir, 'overlib.js')
self.assertTrue(exists(output_html_fp))
self.assertTrue(exists(output_corrected_fp))
self.assertTrue(exists(output_log_fp))
self.assertTrue(exists(overlib_js_fp))
# Check output data for expected results
html_data = "".join([line for line in open(output_html_fp, "U")])
corrected_data =\
"".join([line for line in open(output_corrected_fp, "U")])
log_data = "".join([line for line in open(output_log_fp, "U")])
self.assertEqual(html_data, self.expected_html_data_correct_input)
self.assertEqual(corrected_data,
self.expected_corrected_data_correct_input)
self.assertEqual(log_data, self.expected_log_data_correct_input)
def test_check_mapping_file_errors(self):
""" Gives proper files for errors in mapping files """
# Use data with errors, default parameters
check_mapping_file(mapping_fp=self.errors_mapping_fp,
output_dir=self.output_dir,
verbose=False)
# Check existence of expected output files
output_html_fp = join(self.output_dir,
basename(self.errors_mapping_fp).replace('.txt', '.html'))
output_corrected_fp =\
join(self.output_dir,
basename(self.errors_mapping_fp).replace('.txt', '_corrected.txt'))
output_log_fp =\
join(self.output_dir,
basename(self.errors_mapping_fp).replace('.txt', '.log'))
overlib_js_fp = join(self.output_dir, 'overlib.js')
self.assertTrue(exists(output_html_fp))
self.assertTrue(exists(output_corrected_fp))
self.assertTrue(exists(output_log_fp))
self.assertTrue(exists(overlib_js_fp))
# Check output data for expected results
html_data = "".join([line for line in open(output_html_fp, "U")])
corrected_data =\
"".join([line for line in open(output_corrected_fp, "U")])
log_data = "".join([line for line in open(output_log_fp, "U")])
self.assertEqual(html_data, self.expected_html_errors_output)
self.assertEqual(corrected_data,
self.expected_data_errors_corrected_output)
self.assertEqual(log_data, self.expected_data_log_errors_output)
def test_check_mapping_file_errors_suppressed_bcs(self):
""" Should suppress errors about barcodes if bcs turned off """
# Should not flag bcs for errors with invalid characters
check_mapping_file(mapping_fp=self.errors_mapping_fp,
output_dir=self.output_dir,
has_barcodes=False,
char_replace="A",
verbose=False,
variable_len_barcodes=True,
disable_primer_check=True,
added_demultiplex_field=None)
# Check existence of expected output files
output_html_fp = join(self.output_dir,
basename(self.errors_mapping_fp).replace('.txt', '.html'))
output_corrected_fp =\
join(self.output_dir,
basename(self.errors_mapping_fp).replace('.txt', '_corrected.txt'))
output_log_fp =\
join(self.output_dir,
basename(self.errors_mapping_fp).replace('.txt', '.log'))
overlib_js_fp = join(self.output_dir, 'overlib.js')
self.assertTrue(exists(output_html_fp))
self.assertTrue(exists(output_corrected_fp))
self.assertTrue(exists(output_log_fp))
self.assertTrue(exists(overlib_js_fp))
# Check output data for expected results
html_data = "".join([line for line in open(output_html_fp, "U")])
corrected_data =\
"".join([line for line in open(output_corrected_fp, "U")])
log_data = "".join([line for line in open(output_log_fp, "U")])
self.assertEqual(html_data, self.expected_html_errors_suppressed_bcs)
self.assertEqual(corrected_data,
self.expected_data_errors_corrected_output)
self.assertEqual(log_data,
self.expected_output_log_errors_bcs_suppressed)
def test_check_mapping_file_warnings(self):
""" Gives proper files for warnings in mapping files """
check_mapping_file(mapping_fp=self.warnings_mapping_fp,
output_dir=self.output_dir,
verbose=False)
# Check existence of expected output files
output_html_fp = join(self.output_dir,
basename(self.warnings_mapping_fp).replace('.txt', '.html'))
output_corrected_fp =\
join(self.output_dir,
basename(self.warnings_mapping_fp).replace('.txt', '_corrected.txt'))
output_log_fp =\
join(self.output_dir,
basename(self.warnings_mapping_fp).replace('.txt', '.log'))
overlib_js_fp = join(self.output_dir, 'overlib.js')
self.assertTrue(exists(output_html_fp))
self.assertTrue(exists(output_corrected_fp))
self.assertTrue(exists(output_log_fp))
self.assertTrue(exists(overlib_js_fp))
# Check output data for expected results
html_data = "".join([line for line in open(output_html_fp, "U")])
corrected_data =\
"".join([line for line in open(output_corrected_fp, "U")])
log_data = "".join([line for line in open(output_log_fp, "U")])
self.assertEqual(html_data, self.expected_html_output_warnings)
self.assertEqual(corrected_data,
self.expected_corrected_warnings_output)
self.assertEqual(log_data, self.expected_log_warnings_output)
def test_check_mapping_file_multiple_problems(self):
""" Gives proper files for combinations of errors/warnings """
check_mapping_file(mapping_fp=self.errors_warnings_mapping_fp,
output_dir=self.output_dir,
added_demultiplex_field="DoesNotExist",
verbose=False)
# Check existence of expected output files
output_html_fp = join(self.output_dir,
basename(self.errors_warnings_mapping_fp).replace('.txt', '.html'))
output_corrected_fp =\
join(self.output_dir,
basename(self.errors_warnings_mapping_fp).replace('.txt',
'_corrected.txt'))
output_log_fp =\
join(self.output_dir,
basename(self.errors_warnings_mapping_fp).replace('.txt', '.log'))
overlib_js_fp = join(self.output_dir, 'overlib.js')
self.assertTrue(exists(output_html_fp))
self.assertTrue(exists(output_corrected_fp))
self.assertTrue(exists(output_log_fp))
self.assertTrue(exists(overlib_js_fp))
# Check output data for expected results
html_data = "".join([line for line in open(output_html_fp, "U")])
corrected_data =\
"".join([line for line in open(output_corrected_fp, "U")])
log_data = "".join([line for line in open(output_log_fp, "U")])
self.assertEqual(html_data, self.expected_html_errors_warnings_output)
self.assertEqual(corrected_data,
self.expected_corrected_data_errors_warnings)
self.assertEqual(log_data, self.expected_log_errors_warnings_output)
def test_process_id_map_correct_data(self):
""" Returns expected results for correct mapping data """
header, mapping_data, comments, errors, warnings =\
process_id_map(self.correct_mapping_fp)
expected_header = [
'SampleID',
'BarcodeSequence',
'LinkerPrimerSequence',
'Treatment',
'ReversePrimer',
'Description']
expected_mapping_data = [['PC.354',
'AGCACGAGCCTA',
'YATGCTGCCTCCCGTAGGAGT',
'Control',
'ATGACCGATTRGACCAG',
'Control_mouse_I.D._354'],
['PC.355',
'AACTCGTCGATG',
'YATGCTGCCTCCCGTAGGAGT',
'Control',
'ATGACCGATTRGACCAG',
'Control_mouse_I.D._355'],
['PC.356',
'ACAGACCACTCA',
'YATGCTGCCTCCCGTAGGAGT',
'Control',
'ATGACCGATTRGACCAG',
'Control_mouse_I.D._356']]
expected_comments = [
'Example mapping file for the QIIME analysis package. These 9 samples are from a study of the effects of exercise and diet on mouse cardiac physiology (Crawford, et al, PNAS, 2009).']
expected_errors = []
expected_warnings = []
self.assertEqual(header, expected_header)
self.assertEqual(mapping_data, expected_mapping_data)
self.assertEqual(comments, expected_comments)
self.assertEqual(errors, expected_errors)
self.assertEqual(warnings, expected_warnings)
def test_process_id_map_errors(self):
""" Returns expected results for mapping data with errors """
header, mapping_data, comments, errors, warnings =\
process_id_map(self.errors_mapping_fp)
expected_header = [
'SampleID',
'BarcodeSequence',
'LinkerPrimerSequence',
'Treatment',
'ReversePrimer',
'NotDescription']
expected_mapping_data = [['PC.355',
'AGCACGAGCCxTA',
'YATGCTGCCTCCCGTAGGAGT',
'Control',
'ATGACCGATTRGACCAG',
'Control_mouse_I.D._354'],
['PC.355',
'AACTCGTCGATG',
'YATGCTGCCTCCCGTAGGAGT',
'Control',
'ATGACCGATTRGACCAG',
'Control_mouse_I.D._355'],
['PC.356',
'ACAGACCACTCA',
'YATGCTGCCTCCCGTAGGAGT',
'Control',
'ATGACCGATTRGACCAG',
'Control_mouse_I.D._356']]
expected_comments = [
'Example mapping file for the QIIME analysis package. These 9 samples are from a study of the effects of exercise and diet on mouse cardiac physiology (Crawford, et al, PNAS, 2009).']
expected_errors = [
'Found header field NotDescription, last field should be Description\t0,5',
'Invalid DNA sequence detected: AGCACGAGCCxTA\t1,1',
'Duplicate SampleID PC.355 found.\t1,0',
'Duplicate SampleID PC.355 found.\t2,0']
expected_warnings = [
'Barcode AGCACGAGCCxTA differs than length 12\t1,1']
self.assertEqual(header, expected_header)
self.assertEqual(mapping_data, expected_mapping_data)
self.assertEqual(comments, expected_comments)
self.assertEqual(errors, expected_errors)
self.assertEqual(warnings, expected_warnings)
def test_process_id_map_empty_data_fields(self):
""" Returns expected results for mapping data with missing fields """
header, mapping_data, comments, errors, warnings =\
process_id_map(self.empty_fields_fp)
expected_header = [
'SampleID',
'BarcodeSequence',
'LinkerPrimerSequence',
'Treatment',
'ReversePrimer',
'Description']
expected_mapping_data = [['PC.354',
'AGCACGAGCCTA',
'YATGCTGCCTCCCGTAGGAGT',
'Control',
'ATGACCGATTRGACCAG',
'Control_mouse_I.D._354'],
['PC.355',
'AACTCGTCGATG',
'YATGCTGCCTCCCGTAGGAGT',
'Control',
'',
''],
['PC.356',
'ACAGACCACTCA',
'YATGCTGCCTCCCGTAGGAGT',
'Control',
'ATGACCGATTRGACCAG',
'Control_mouse_I.D._356']]
expected_comments = [
'Example mapping file for the QIIME analysis package. These 9 samples are from a study of the effects of exercise and diet on mouse cardiac physiology (Crawford, et al, PNAS, 2009).']
expected_errors = ['Missing expected DNA sequence\t2,4']
expected_warnings = [
'Empty data field found\t2,4',
'Empty data field found\t2,5']
self.assertEqual(header, expected_header)
self.assertEqual(mapping_data, expected_mapping_data)
self.assertEqual(comments, expected_comments)
self.assertEqual(errors, expected_errors)
self.assertEqual(warnings, expected_warnings)
def test_process_id_map_warnings(self):
""" Returns expected results for mapping data with warnings """
header, mapping_data, comments, errors, warnings =\
process_id_map(self.warnings_mapping_fp)
expected_header = [
'SampleID',
'BarcodeSequence',
'LinkerPrimerSequence',
'Treatm-ent',
'ReversePrimer',
'Description']
expected_mapping_data = [['PC.354',
'AGCACGAGCCTA',
'YATGCTGCCTCCCGTAGGAGT',
'Control',
'ATGACCGATTRGACCAG',
'Control_mouse_I.D._354'],
['PC_355',
'AACTCGTCGATG',
'YATGCTGCCTCCCGTAGGAGT',
'Co&ntrol',
'ATGACCGATTRGACCAG',
'Control_mouse_I.D._355',
'OutOfBounds'],
['PC.356',
'ACAGACCACTCA',
'YATGCTGCCTCCCGTAGGAGT',
'Control',
'ATGACCGATTRGACCAG',
'Control_mouse_I.D._356']]
expected_comments = [
'Example mapping file for the QIIME analysis package. These 9 samples are from a study of the effects of exercise and diet on mouse cardiac physiology (Crawford, et al, PNAS, 2009).']
expected_errors = []
expected_warnings = [
'Found invalid character in Treatm-ent header field.\t0,3',
'Invalid characters found in PC_355\t2,0',
'Invalid characters found in Co&ntrol\t2,3',
'Data field OutOfBounds found after Description column\t2,6']
self.assertEqual(header, expected_header)
self.assertEqual(mapping_data, expected_mapping_data)
self.assertEqual(comments, expected_comments)
self.assertEqual(errors, expected_errors)
self.assertEqual(warnings, expected_warnings)
def test_process_id_map_multiple_problems(self):
""" Returns expected results for combinations of problems """
header, mapping_data, comments, errors, warnings =\
process_id_map(self.errors_warnings_mapping_fp)
expected_header = [
'SampleID',
'BarcodeSequence',
'LinkerPrimerSequence',
'Treatment',
'Treatment',
'Description']
expected_mapping_data = [['PC.354',
'AGCACGAGCCTA',
'YATGCTGCCTCCCGTAGGAGT',
'Cont^^rol',
'ATGACCGATTRGACCAG',
'Control_mouse_I.D._354'],
['PC-355',
'AACTCGTCGATGN',
'YATGCTGCCTCCCGTAGGAGT',
'Control',
'ATGACCGATTRGACCAG',
'Control_mouse_I.D._355',
'outofbounds'],
['PC.356',
'ACAGACCACTCA',
'YATGCTGCCTCxCCGTAGGAGT',
'Control',
'ATGACCGATTRGACCAG',
'Control_mouse_I.D._356']]
expected_comments = [
'Example mapping file for the QIIME analysis package. These 9 samples are from a study of the effects of exercise and diet on mouse cardiac physiology (Crawford, et al, PNAS, 2009).']
expected_errors = [
'Treatment found in header 2 times. Header fields must be unique.\t0,3',
'Treatment found in header 2 times. Header fields must be unique.\t0,4',
'Invalid DNA sequence detected: YATGCTGCCTCxCCGTAGGAGT\t3,2',
'Invalid DNA sequence detected: AACTCGTCGATGN\t2,1']
expected_warnings = [
'Barcode AACTCGTCGATGN differs than length 12\t2,1',
'Invalid characters found in PC-355\t2,0',
'Invalid characters found in Cont^^rol\t1,3',
'Data field outofbounds found after Description column\t2,6']
self.assertEqual(header, expected_header)
self.assertEqual(mapping_data, expected_mapping_data)
self.assertEqual(comments, expected_comments)
self.assertEqual(errors, expected_errors)
self.assertEqual(warnings, expected_warnings)
def test_check_data_fields(self):
""" Overall data fields check returns expected results """
header =\
['SampleID',
'BarcodeSequence',
'LinkerPrimerSequence',
'Description']
mapping_data = [['s1', 'ACGT', 'AAAA', 's1_data'],
['s2', 'CGTA', 'AAAA', 's2_data']]
errors = []
warnings = []
errors, warnings = check_data_fields(header,
mapping_data, errors, warnings)
expected_errors = []
expected_warnings = []
self.assertEqual(errors, expected_errors)
self.assertEqual(warnings, expected_warnings)
def test_check_data_fields_errors_warnings(self):
""" Overall data fields check returns expected results """
header =\
['SampleID',
'BarcodeSequence',
'LinkerPrimerSequence',
'Description']
mapping_data = [['s1', 'ACGT', 'AAAxA', 's1_data'],
['s_2', 'CGTA', 'AAAA', 's2_data']]
errors = []
warnings = []
errors, warnings = check_data_fields(header,
mapping_data, errors, warnings)
expected_errors = ['Invalid DNA sequence detected: AAAxA\t1,2']
expected_warnings = ['Invalid characters found in s_2\t2,0']
self.assertEqual(errors, expected_errors)
self.assertEqual(warnings, expected_warnings)
def test_check_fields_past_bounds(self):
""" Flags fields found past the Description column as warnings """
header =\
['SampleID',
'BarcodeSequence',
'LinkerPrimerSequence',
'Description']
mapping_data = [['s1', 'ACGT', 'AAAA', 's1_data'],
['s2', 'CGTA', 'AAAA', 's2_data', 'out_of_bounds']]
warnings = []
warnings = check_fields_past_bounds(header, mapping_data, warnings)
expected_warnings =\
['Data field out_of_bounds found after Description column\t2,4']
self.assertEqual(warnings, expected_warnings)
def test_check_chars_data_fields(self):
""" Flags fields with invalid characters as warnings """
header =\
['SampleID',
'BarcodeSequence',
'LinkerPrimerSequence',
'Description']
mapping_data = [['s-1', 'ACGT', 'AAAA', 's1&data'],
['s2', 'CGTA', 'AAAA', 's2_data']]
warnings = []
warnings = check_chars_data_fields(header, mapping_data, warnings)
expected_warnings = ['Invalid characters found in s-1\t1,0',
'Invalid characters found in s1&data\t1,3']
self.assertEqual(warnings, expected_warnings)
def test_check_dna_chars_primers(self):
""" Flags primer fields with invalid characters as errors """
header =\
['SampleID',
'BarcodeSequence',
'LinkerPrimerSequence',
'Description']
mapping_data = [['s-1', 'ACGT', 'AARNCWSVDAA', 's1&data'],
['s2', 'CGTA', 'AAA1A', 's2_data']]
errors = []
errors = check_dna_chars_primers(header, mapping_data, errors)
expected_errors = ['Invalid DNA sequence detected: AAA1A\t2,2']
self.assertEqual(errors, expected_errors)
# Should be able to suppress LinkerPrimerSequence check, won't
# suppress ReversePrimer check
header =\
['SampleID', 'BarcodeSequence', 'LinkerPrimerSequence',
'ReversePrimer', 'Description']
mapping_data = [['s-1', 'ACGT', 'AARNCWSVDAA', 'ACGT', 's1&data'],
['s2', 'CGTA', 'AAA1A', 'ACGTF', 's2_data']]
errors = []
errors = check_dna_chars_primers(header, mapping_data, errors,
disable_primer_check=True)
expected_errors = ['Invalid DNA sequence detected: ACGTF\t2,3']
self.assertEqual(errors, expected_errors)
def test_check_dna_chars_bcs(self):
""" Flags barcode fields with invalid characters as errors """
header =\
['SampleID',
'BarcodeSequence',
'LinkerPrimerSequence',
'Description']
mapping_data = [['s-1', 'ACGT', 'AARNCWSVDAA', 's1&data'],
['s2', 'CGTA', 'AAA1A', 's2_data']]
errors = []
errors = check_dna_chars_bcs(header, mapping_data, errors)
expected_errors = []
self.assertEqual(errors, expected_errors)
# Should find no errors
header =\
['SampleID', 'BarcodeSequence', 'LinkerPrimerSequence',
'ReversePrimer', 'Description']
mapping_data = [['s-1', 'ACGT', 'AARNCWSVDAA', 'ACGT', 's1&data'],
['s2', 'C1GTA', 'AAA1A', 'ACGTF', 's2_data']]
errors = []
errors = check_dna_chars_bcs(header, mapping_data, errors,
has_barcodes=False)
expected_errors = []
self.assertEqual(errors, expected_errors)
# Should find errors with has_barcodes=True
header =\
['SampleID', 'BarcodeSequence', 'LinkerPrimerSequence',
'ReversePrimer', 'Description']
mapping_data = [['s-1', 'ACGT', 'AARNCWSVDAA', 'ACGT', 's1&data'],
['s2', 'CNGTA', 'AAA1A', 'ACGTF', 's2_data']]
errors = []
errors = check_dna_chars_bcs(header, mapping_data, errors,
has_barcodes=True)
expected_errors = ['Invalid DNA sequence detected: CNGTA\t2,1']
self.assertEqual(errors, expected_errors)
def test_check_bcs_lengths(self):
""" Flags barcodes of different lengths as warnings """
# should not give any warnings for equal lengths
header =\
['SampleID',
'BarcodeSequence',
'LinkerPrimerSequence',
'Description']
mapping_data = [['s-1', 'ACGT', 'AARNCWSVDAA', 's1&data'],
['s2', 'CGTA', 'AAA1A', 's2_data']]
warnings = []
warnings = check_bcs_lengths(header, mapping_data, warnings)
expected_warnings = []
self.assertEqual(warnings, expected_warnings)
# Should give warning for different lengths
header =\
['SampleID', 'BarcodeSequence', 'LinkerPrimerSequence',
'ReversePrimer', 'Description']
mapping_data = [['s-1', 'ACGT', 'AARNCWSVDAA', 'ACGT', 's1&data'],
['s2', 'C1GTA', 'AAA1A', 'ACGTF', 's2_data'],
['s3', 'CGTA', 'AAAAA', 'ACGTF', 's3_data']]
warnings = []
warnings = check_bcs_lengths(header, mapping_data, warnings)
expected_warnings = ['Barcode C1GTA differs than length 4\t2,1']
self.assertEqual(warnings, expected_warnings)
def test_check_bc_duplicates_default_correct(self):
""" Handles duplicate checks of barcodes and added demultiplex data """
header =\
['SampleID', 'BarcodeSequence', 'LinkerPrimerSequence', 'run_prefix',
'Description']
mapping_data = [['s-1', 'ACGT', 'AAAA', '1', 's1&data'],
['s2', 'CGTA', 'AAAA', '2', 's2_data']]
errors = []
errors = check_bc_duplicates(header,
mapping_data,
errors,
has_barcodes=True,
variable_len_barcodes=False,
added_demultiplex_field=None)
expected_errors = []
self.assertEqual(errors, expected_errors)
def test_check_bc_duplicates_default_dups(self):
""" Handles duplicate checks of barcodes and added demultiplex data """
header =\
['SampleID', 'BarcodeSequence', 'LinkerPrimerSequence', 'run_prefix',
'Description']
mapping_data = [['s-1', 'ACGT', 'AAAA', '1', 's1&data'],
['s2', 'ACGT', 'AAAA', '2', 's2_data']]
errors = []
errors = check_bc_duplicates(header,
mapping_data,
errors,
has_barcodes=True,
variable_len_barcodes=False,
added_demultiplex_field=None)
expected_errors = ['Duplicate barcode ACGT found.\t1,1',
'Duplicate barcode ACGT found.\t2,1']
self.assertEqual(errors, expected_errors)
def test_check_bc_duplicates_disable_bcs_dups(self):
""" Handles duplicate checks of no barcodes or added demultiplex data """
header =\
['SampleID', 'BarcodeSequence', 'LinkerPrimerSequence', 'run_prefix',
'Description']
mapping_data = [['s-1', 'ACGT', 'AAAA', '1', 's1&data'],
['s2', 'ACGT', 'AAAA', '2', 's2_data']]
errors = []
errors = check_bc_duplicates(header,
mapping_data,
errors,
has_barcodes=False,
variable_len_barcodes=False,
added_demultiplex_field=None)
# Should return an error if more than one SampleID present and
# barcodes disabled and no added_demultiplex_field
expected_errors = [
"If no barcodes are present, and the added_demultiplex_field option isn't used, only a single SampleID can be present.\t-1,-1"]
self.assertEqual(errors, expected_errors)
# If a single sample present, should not raise any errors
header =\
['SampleID', 'BarcodeSequence', 'LinkerPrimerSequence', 'run_prefix',
'Description']
mapping_data = [['s-1', 'ACGT', 'AAAA', '1', 's1&data']]
errors = []
errors = check_bc_duplicates(header,
mapping_data,
errors,
has_barcodes=False,
variable_len_barcodes=False,
added_demultiplex_field=None)
expected_errors = []
self.assertEqual(errors, expected_errors)
def test_check_bc_duplicates_var_len_no_dupes(self):
""" Handles duplicate checks of barcodes and added demultiplex data """
header =\
['SampleID', 'BarcodeSequence', 'LinkerPrimerSequence', 'run_prefix',
'Description']
mapping_data = [['s-1', 'ACGTA', 'AAAA', '1', 's1&data'],
['s2', 'ACGT', 'TAAA', '2', 's2_data']]
errors = []
errors = check_bc_duplicates(header,
mapping_data,
errors,
has_barcodes=True,
variable_len_barcodes=True,
added_demultiplex_field=None)
# combination of primer seq and barcodes to match largest barcode
# present is ACGTA and ACGTT, so should not get a duplicate hit.
expected_errors = []
self.assertEqual(errors, expected_errors)
def test_check_bc_duplicates_var_len_dupes(self):
""" Handles duplicate checks of barcodes and added demultiplex data """
header =\
['SampleID', 'BarcodeSequence', 'LinkerPrimerSequence', 'run_prefix',
'Description']
mapping_data = [['s-1', 'ACGTA', 'AAAA', '1', 's1&data'],
['s2', 'ACGT', 'AAAA', '2', 's2_data']]
errors = []
errors = check_bc_duplicates(header,
mapping_data,
errors,
has_barcodes=True,
variable_len_barcodes=True,
added_demultiplex_field=None)
# Barcode 1 is the largest, with 5 nts, is sequence ACGTA. When the
# last base at 5' end of primer is added to barcode 2, there is a
# duplicate, as this is also ACGTA.
expected_errors = [
'Duplicate barcode and primer fragment sequence ACGTA found.\t1,1',
'Duplicate barcode and primer fragment sequence ACGTA found.\t2,1']
self.assertEqual(errors, expected_errors)
def test_check_bc_duplicates_added_demultiplex(self):
""" Handles duplicate checks of barcodes and added demultiplex data """
# Should not find any duplicates
header =\
['SampleID', 'BarcodeSequence', 'LinkerPrimerSequence', 'run_prefix',
'Description']
mapping_data = [['s-1', 'ACGT', 'AAAA', '1', 's1&data'],
['s2', 'CGTA', 'AAAA', '2', 's2_data']]
errors = []
errors = check_bc_duplicates(header,
mapping_data,
errors,
has_barcodes=True,
variable_len_barcodes=False,
added_demultiplex_field='run_prefix')
expected_errors = []
self.assertEqual(errors, expected_errors)
# Should not find any duplicates with var length turned on.
header =\
['SampleID', 'BarcodeSequence', 'LinkerPrimerSequence', 'run_prefix',
'Description']
mapping_data = [['s-1', 'ACGT', 'AAAA', '1', 's1&data'],
['s2', 'CGTA', 'AAAA', '2', 's2_data']]
errors = []
errors = check_bc_duplicates(header,
mapping_data,
errors,
has_barcodes=True,
variable_len_barcodes=True,
added_demultiplex_field='run_prefix')
expected_errors = []
self.assertEqual(errors, expected_errors)
# Should not find errors when only looking at added field
header =\
['SampleID', 'BarcodeSequence', 'LinkerPrimerSequence', 'run_prefix',
'Description']
mapping_data = [['s-1', 'ACGT', 'AAAA', '1', 's1&data'],
['s2', 'CGTA', 'AAAA', '2', 's2_data']]
errors = []
errors = check_bc_duplicates(header,
mapping_data,
errors,
has_barcodes=False,
variable_len_barcodes=False,
added_demultiplex_field='run_prefix')
expected_errors = []
self.assertEqual(errors, expected_errors)
def test_check_bc_duplicates_added_demultiplex_finds_dups(self):
""" Handles duplicate checks of barcodes and added demultiplex data """
# Should find duplicates
header =\
['SampleID', 'BarcodeSequence', 'LinkerPrimerSequence', 'run_prefix',
'Description']
mapping_data = [['s-1', 'CGTA', 'AAAA', '1', 's1&data'],
['s2', 'CGTA', 'AAAA', '1', 's2_data']]
errors = []
errors = check_bc_duplicates(header,
mapping_data,
errors,
has_barcodes=True,
variable_len_barcodes=False,
added_demultiplex_field='run_prefix')