File: test_seq.py

package info (click to toggle)
python-biopython 1.68%2Bdfsg-3~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 46,856 kB
  • sloc: python: 160,306; xml: 93,216; ansic: 9,118; sql: 1,208; makefile: 155; sh: 63
file content (1186 lines) | stat: -rw-r--r-- 51,623 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
# This code is part of the Biopython distribution and governed by its
# license.  Please see the LICENSE file that should have been included
# as part of this package.

from __future__ import print_function
import array
import copy
import sys
import warnings

# Remove unittest2 import after dropping support for Python2.6
if sys.version_info < (2, 7):
    try:
        import unittest2 as unittest
    except ImportError:
        from Bio import MissingPythonDependencyError
        raise MissingPythonDependencyError("Under Python 2.6 this test needs the unittest2 library")
else:
    import unittest

from Bio import Alphabet
from Bio import Seq
from Bio.Alphabet import IUPAC, Gapped
from Bio.Data.IUPACData import ambiguous_dna_complement, ambiguous_rna_complement
from Bio.Data.IUPACData import ambiguous_dna_values, ambiguous_rna_values
from Bio.Data.CodonTable import TranslationError
from Bio.Data.CodonTable import standard_dna_table
from Bio.Seq import MutableSeq


if sys.version_info[0] == 3:
    array_indicator = "u"
else:
    array_indicator = "c"

test_seqs = [
    Seq.Seq("TCAAAAGGATGCATCATG", IUPAC.unambiguous_dna),
    Seq.Seq("T", IUPAC.ambiguous_dna),
    Seq.Seq("ATGAAACTG"),
    Seq.Seq("ATGAARCTG"),
    Seq.Seq("AWGAARCKG"),  # Note no U or T
    Seq.Seq("".join(ambiguous_rna_values)),
    Seq.Seq("".join(ambiguous_dna_values)),
    Seq.Seq("".join(ambiguous_rna_values), Alphabet.generic_rna),
    Seq.Seq("".join(ambiguous_dna_values), Alphabet.generic_dna),
    Seq.Seq("".join(ambiguous_rna_values), IUPAC.IUPACAmbiguousRNA()),
    Seq.Seq("".join(ambiguous_dna_values), IUPAC.IUPACAmbiguousDNA()),
    Seq.Seq("AWGAARCKG", Alphabet.generic_dna),
    Seq.Seq("AUGAAACUG", Alphabet.generic_rna),
    Seq.Seq("ATGAAACTG", IUPAC.unambiguous_dna),
    Seq.Seq("ATGAAA-CTG", Alphabet.Gapped(IUPAC.unambiguous_dna)),
    Seq.Seq("ATGAAACTGWN", IUPAC.ambiguous_dna),
    Seq.Seq("AUGAAACUG", Alphabet.generic_rna),
    Seq.Seq("AUGAAA==CUG", Alphabet.Gapped(Alphabet.generic_rna, "=")),
    Seq.Seq("AUGAAACUG", IUPAC.unambiguous_rna),
    Seq.Seq("AUGAAACUGWN", IUPAC.ambiguous_rna),
    Seq.Seq("ATGAAACTG", Alphabet.generic_nucleotide),
    Seq.Seq("AUGAAACTG", Alphabet.generic_nucleotide),  # U and T
    Seq.MutableSeq("ATGAAACTG", Alphabet.generic_dna),
    Seq.MutableSeq("AUGaaaCUG", IUPAC.unambiguous_rna),
    Seq.Seq("ACTGTCGTCT", Alphabet.generic_protein),
]
protein_seqs = [
    Seq.Seq("ATCGPK", IUPAC.protein),
    Seq.Seq("T.CGPK", Alphabet.Gapped(IUPAC.protein, ".")),
    Seq.Seq("T-CGPK", Alphabet.Gapped(IUPAC.protein, "-")),
    Seq.Seq("MEDG-KRXR*", Alphabet.Gapped(Alphabet.HasStopCodon(IUPAC.extended_protein, "*"), "-")),
    Seq.MutableSeq("ME-K-DRXR*XU", Alphabet.Gapped(Alphabet.HasStopCodon(IUPAC.extended_protein, "*"), "-")),
    Seq.Seq("MEDG-KRXR@", Alphabet.HasStopCodon(Alphabet.Gapped(IUPAC.extended_protein, "-"), "@")),
    Seq.Seq("ME-KR@", Alphabet.HasStopCodon(Alphabet.Gapped(IUPAC.protein, "-"), "@")),
    Seq.Seq("MEDG.KRXR@", Alphabet.Gapped(Alphabet.HasStopCodon(IUPAC.extended_protein, "@"), ".")),
]


class TestSeq(unittest.TestCase):
    def setUp(self):
        self.s = Seq.Seq("TCAAAAGGATGCATCATG", IUPAC.unambiguous_dna)

    def test_as_string(self):
        """Test converting Seq to string"""
        self.assertEqual("TCAAAAGGATGCATCATG", str(self.s))

    def test_construction_using_a_seq_object(self):
        """Test using a Seq object to initialize another Seq object"""
        with self.assertRaises(TypeError):
            Seq.Seq(self.s)

    def test_repr(self):
        """Test representation of Seq object"""
        self.assertEqual("Seq('TCAAAAGGATGCATCATG', IUPACUnambiguousDNA())",
                         repr(self.s))

    def test_truncated_repr(self):
        seq = "TCAAAAGGATGCATCATGTCAAAAGGATGCATCATGTCAAAAGGATGCATCATGTCAAAAGGA"
        expected = "Seq('TCAAAAGGATGCATCATGTCAAAAGGATGCATCATGTCAAAAGGATGCATCATG...GGA', IUPACAmbiguousDNA())"
        self.assertEqual(expected, repr(Seq.Seq(seq, IUPAC.ambiguous_dna)))

    def test_length(self):
        """Test len method on Seq object"""
        self.assertEqual(18, len(self.s))

    def test_first_nucleotide(self):
        """Test getting first nucleotide of Seq"""
        self.assertEqual("T", self.s[0])

    def test_last_nucleotide(self):
        """Test getting last nucleotide of Seq"""
        self.assertEqual("G", self.s[-1])

    def test_slicing(self):
        """Test slicing of Seq"""
        self.assertEqual("AA", str(self.s[3:5]))

    def test_reverse(self):
        """Test reverse using -1 stride"""
        self.assertEqual("GTACTACGTAGGAAAACT", self.s[::-1])

    def test_extract_third_nucleotide(self):
        """Test extracting every third nucleotide (slicing with stride 3)"""
        self.assertEqual("TAGTAA", str(self.s[0::3]))
        self.assertEqual("CAGGTT", str(self.s[1::3]))
        self.assertEqual("AAACCG", str(self.s[2::3]))

    def test_alphabet_letters(self):
        """Test nucleotides in DNA Seq"""
        self.assertEqual("GATC", self.s.alphabet.letters)

    def test_alphabet(self):
        """Test alphabet of derived Seq object"""
        t = Seq.Seq("T", IUPAC.unambiguous_dna)
        u = self.s + t
        self.assertEqual("IUPACUnambiguousDNA()", str(u.alphabet))

    def test_length_concatenated_unambiguous_seq(self):
        """Test length of concatenated Seq object with unambiguous DNA"""
        t = Seq.Seq("T", IUPAC.unambiguous_dna)
        u = self.s + t
        self.assertEqual(19, len(u))

    def test_concatenation_of_seq(self):
        t = Seq.Seq("T", IUPAC.unambiguous_dna)
        u = self.s + t
        self.assertEqual(str(self.s) + "T", str(u))

    def test_concatenation_error(self):
        """Test DNA Seq objects cannot be concatenated with Protein Seq
        objects"""
        with self.assertRaises(TypeError):
            self.s + Seq.Seq("T", IUPAC.protein)

    def test_concatenation_of_ambiguous_and_unambiguous_dna(self):
        """Test concatenated Seq object with ambiguous and unambiguous DNA
        returns ambiguous Seq"""
        t = Seq.Seq("T", IUPAC.ambiguous_dna)
        u = self.s + t
        self.assertEqual("IUPACAmbiguousDNA()", str(u.alphabet))

    def test_ungap(self):
        self.assertEqual("ATCCCA", str(Seq.Seq("ATC-CCA").ungap("-")))

        with self.assertRaises(ValueError):
            Seq.Seq("ATC-CCA").ungap("--")

        with self.assertRaises(ValueError):
            Seq.Seq("ATC-CCA").ungap()


class TestSeqStringMethods(unittest.TestCase):
    def setUp(self):
        self.s = Seq.Seq("TCAAAAGGATGCATCATG", IUPAC.unambiguous_dna)
        self.dna = [
            Seq.Seq("ATCG", IUPAC.ambiguous_dna),
            Seq.Seq("gtca", Alphabet.generic_dna),
            Seq.MutableSeq("GGTCA", Alphabet.generic_dna),
            Seq.Seq("CTG-CA", Alphabet.Gapped(IUPAC.unambiguous_dna, "-")),
        ]
        self.rna = [
            Seq.Seq("AUUUCG", IUPAC.ambiguous_rna),
            Seq.MutableSeq("AUUCG", IUPAC.ambiguous_rna),
            Seq.Seq("uCAg", Alphabet.generic_rna),
            Seq.MutableSeq("UC-AG", Alphabet.Gapped(Alphabet.generic_rna, "-")),
            Seq.Seq("U.CAG", Alphabet.Gapped(Alphabet.generic_rna, ".")),
        ]
        self.nuc = [Seq.Seq("ATCG", Alphabet.generic_nucleotide)]
        self.protein = [
            Seq.Seq("ATCGPK", IUPAC.protein),
            Seq.Seq("atcGPK", Alphabet.generic_protein),
            Seq.Seq("T.CGPK", Alphabet.Gapped(IUPAC.protein, ".")),
            Seq.Seq("T-CGPK", Alphabet.Gapped(IUPAC.protein, "-")),
            Seq.Seq("MEDG-KRXR*", Alphabet.Gapped(Alphabet.HasStopCodon(IUPAC.extended_protein, "*"), "-")),
            Seq.MutableSeq("ME-K-DRXR*XU", Alphabet.Gapped(Alphabet.HasStopCodon(IUPAC.extended_protein, "*"), "-")),
            Seq.Seq("MEDG-KRXR@", Alphabet.HasStopCodon(Alphabet.Gapped(IUPAC.extended_protein, "-"), "@")),
            Seq.Seq("ME-KR@", Alphabet.HasStopCodon(Alphabet.Gapped(IUPAC.protein, "-"), "@")),
            Seq.Seq("MEDG.KRXR@", Alphabet.Gapped(Alphabet.HasStopCodon(IUPAC.extended_protein, "@"), ".")),
        ]
        self.test_chars = ["-", Seq.Seq("-"), Seq.Seq("*"), "-X@"]

    def test_string_methods(self):
        for a in self.dna + self.rna + self.nuc + self.protein:
            if isinstance(a, Seq.Seq):
                self.assertEqual(str(a.strip()), str(a).strip())
                self.assertEqual(str(a.lstrip()), str(a).lstrip())
                self.assertEqual(str(a.rstrip()), str(a).rstrip())
                self.assertEqual(str(a.lower()), str(a).lower())
                self.assertEqual(str(a.upper()), str(a).upper())

    def test_hash(self):
        with warnings.catch_warnings(record=True):
            hash(self.s)

    def test_equal_comparison_of_incompatible_alphabets(self):
        """Test __eq__ comparison method"""
        with warnings.catch_warnings(record=True):
            Seq.Seq("TCAAAA", IUPAC.ambiguous_dna) == Seq.Seq("TCAAAA", IUPAC.ambiguous_rna)

    def test_not_equal_comparsion(self):
        """Test __ne__ comparison method"""
        self.assertNotEqual(Seq.Seq("TCAAA", IUPAC.ambiguous_dna),
                            Seq.Seq("TCAAAA", IUPAC.ambiguous_dna))

    def test_less_than_comparison_of_incompatible_alphabets(self):
        """Test __lt__ comparison method"""
        seq1 = Seq.Seq("TCAAA", IUPAC.ambiguous_dna)
        seq2 = Seq.Seq("UCAAAA", IUPAC.ambiguous_rna)
        with warnings.catch_warnings(record=True):
            self.assertTrue(seq1 < seq2)

    def test_less_than_or_equal_comparison_of_incompatible_alphabets(self):
        """Test __lt__ comparison method"""
        seq1 = Seq.Seq("TCAAA", IUPAC.ambiguous_dna)
        seq2 = Seq.Seq("UCAAAA", IUPAC.ambiguous_rna)
        with warnings.catch_warnings(record=True):
            self.assertTrue(seq1 <= seq2)

    def test_add_method_using_wrong_object(self):
        with self.assertRaises(TypeError):
            self.s + dict()

    def test_radd_method(self):
        self.assertEqual("TCAAAAGGATGCATCATGTCAAAAGGATGCATCATG", str(self.s.__radd__(self.s)))

    def test_radd_method_using_incompatible_alphabets(self):
        rna_seq = Seq.Seq("UCAAAA", IUPAC.ambiguous_rna)
        with self.assertRaises(TypeError):
            self.s.__radd__(rna_seq)

    def test_radd_method_using_wrong_object(self):
        with self.assertRaises(TypeError):
            self.s.__radd__(dict())

    def test_to_string_deprecated_method(self):
        with warnings.catch_warnings(record=True):
            self.s.tostring()

    def test_contains_method(self):
        self.assertTrue("AAAA" in self.s)

    def test_startswith(self):
        self.assertTrue(self.s.startswith("TCA"))
        self.assertTrue(self.s.startswith(("CAA", "CTA"), 1))

    def test_endswith(self):
        self.assertTrue(self.s.endswith("ATG"))
        self.assertTrue(self.s.endswith(("ATG", "CTA")))

    def test_append_nucleotides(self):
        self.test_chars.append(Seq.Seq("A", IUPAC.ambiguous_dna))
        self.test_chars.append(Seq.Seq("A", IUPAC.ambiguous_rna))
        self.test_chars.append(Seq.Seq("A", Alphabet.generic_nucleotide))

        self.assertEqual(7, len(self.test_chars))

    def test_append_proteins(self):
        self.test_chars.append(Seq.Seq("K", Alphabet.generic_protein))
        self.test_chars.append(Seq.Seq("K-", Alphabet.Gapped(Alphabet.generic_protein, "-")))
        self.test_chars.append(Seq.Seq("K@", Alphabet.Gapped(IUPAC.protein, "@")))

        self.assertEqual(7, len(self.test_chars))

    def test_exception_when_clashing_alphabets(self):
        """Test by setting up clashing alphabet sequences"""
        b = Seq.Seq("-", Alphabet.generic_nucleotide)
        self.assertRaises(TypeError, self.protein[0].strip, b)

        b = Seq.Seq("-", Alphabet.generic_protein)
        self.assertRaises(TypeError, self.dna[0].strip, b)

    def test_stripping_characters(self):
        for a in self.dna + self.rna + self.nuc + self.protein:
            for char in self.test_chars:
                str_char = str(char)
                if isinstance(a, Seq.Seq):
                    self.assertEqual(str(a.strip(char)), str(a).strip(str_char))
                    self.assertEqual(str(a.lstrip(char)), str(a).lstrip(str_char))
                    self.assertEqual(str(a.rstrip(char)), str(a).rstrip(str_char))

    def test_finding_characters(self):
        for a in self.dna + self.rna + self.nuc + self.protein:
            for char in self.test_chars:
                str_char = str(char)
                if isinstance(a, Seq.Seq):
                    self.assertEqual(a.find(char), str(a).find(str_char))
                    self.assertEqual(a.find(char, 2, -2), str(a).find(str_char, 2, -2))
                    self.assertEqual(a.rfind(char), str(a).rfind(str_char))
                    self.assertEqual(a.rfind(char, 2, -2), str(a).rfind(str_char, 2, -2))

    def test_counting_characters(self):
        for a in self.dna + self.rna + self.nuc + self.protein:
            for char in self.test_chars:
                str_char = str(char)
                if isinstance(a, Seq.Seq):
                    self.assertEqual(a.count(char), str(a).count(str_char))
                    self.assertEqual(a.count(char, 2, -2), str(a).count(str_char, 2, -2))

    def test_splits(self):
        for a in self.dna + self.rna + self.nuc + self.protein:
            for char in self.test_chars:
                str_char = str(char)
                if isinstance(a, Seq.Seq):
                    self.assertEqual([str(x) for x in a.split(char)],
                                     str(a).split(str_char))
                    self.assertEqual([str(x) for x in a.rsplit(char)],
                                     str(a).rsplit(str_char))

                    for max_sep in [0, 1, 2, 999]:
                        self.assertEqual([str(x) for x in a.split(char, max_sep)],
                                         str(a).split(str_char, max_sep))


class TestSeqAddition(unittest.TestCase):
    def setUp(self):
        self.dna = [
            Seq.Seq("ATCG", IUPAC.ambiguous_dna),
            Seq.Seq("gtca", Alphabet.generic_dna),
            Seq.MutableSeq("GGTCA", Alphabet.generic_dna),
            Seq.Seq("CTG-CA", Alphabet.Gapped(IUPAC.unambiguous_dna, "-")),
            "TGGTCA",
        ]
        self.rna = [
            Seq.Seq("AUUUCG", IUPAC.ambiguous_rna),
            Seq.MutableSeq("AUUCG", IUPAC.ambiguous_rna),
            Seq.Seq("uCAg", Alphabet.generic_rna),
            Seq.MutableSeq("UC-AG", Alphabet.Gapped(Alphabet.generic_rna, "-")),
            Seq.Seq("U.CAG", Alphabet.Gapped(Alphabet.generic_rna, ".")),
            "UGCAU",
        ]
        self.nuc = [
            Seq.Seq("ATCG", Alphabet.generic_nucleotide),
            "UUUTTTACG",
        ]
        self.protein = [
            Seq.Seq("ATCGPK", IUPAC.protein),
            Seq.Seq("atcGPK", Alphabet.generic_protein),
            Seq.Seq("T.CGPK", Alphabet.Gapped(IUPAC.protein, ".")),
            Seq.Seq("T-CGPK", Alphabet.Gapped(IUPAC.protein, "-")),
            Seq.Seq("MEDG-KRXR*", Alphabet.Gapped(Alphabet.HasStopCodon(IUPAC.extended_protein, "*"), "-")),
            Seq.MutableSeq("ME-K-DRXR*XU", Alphabet.Gapped(Alphabet.HasStopCodon(IUPAC.extended_protein, "*"), "-")),
            "TEDDF",
        ]

    def test_addition_dna_rna_with_generic_nucleotides(self):
        for a in self.dna + self.rna:
            for b in self.nuc:
                c = a + b
                self.assertEqual(str(c), str(a) + str(b))

    def test_addition_rna_with_rna(self):
        self.rna.pop(3)
        for a in self.rna:
            for b in self.rna:
                c = a + b
                self.assertEqual(str(c), str(a) + str(b))

    def test_exception_when_added_rna_has_more_than_one_gap_type(self):
        """Test resulting sequence has gap types '-' and '.'"""
        with self.assertRaises(ValueError):
            self.rna[3] + self.rna[4]

    def test_addition_dna_with_dna(self):
        for a in self.dna:
            for b in self.dna:
                c = a + b
                self.assertEqual(str(c), str(a) + str(b))

    def test_addition_dna_with_rna(self):
        self.dna.pop(4)
        self.rna.pop(5)
        for a in self.dna:
            for b in self.rna:
                with self.assertRaises(TypeError):
                    a + b
                with self.assertRaises(TypeError):
                    b + a

    def test_addition_proteins(self):
        self.protein.pop(2)
        for a in self.protein:
            for b in self.protein:
                c = a + b
                self.assertEqual(str(c), str(a) + str(b))

    def test_exception_when_added_protein_has_more_than_one_gap_type(self):
        """Test resulting protein has gap types '-' and '.'"""
        a = Seq.Seq("T.CGPK", Alphabet.Gapped(IUPAC.protein, "."))
        b = Seq.Seq("T-CGPK", Alphabet.Gapped(IUPAC.protein, "-"))
        with self.assertRaises(ValueError):
            a + b

    def test_exception_when_added_protein_has_more_than_one_stop_codon_type(self):
        """Test resulting protein has stop codon types '*' and '@'"""
        a = Seq.Seq("MEDG-KRXR@", Alphabet.HasStopCodon(Alphabet.Gapped(IUPAC.extended_protein, "-"), "@"))
        b = Seq.Seq("MEDG-KRXR*", Alphabet.Gapped(Alphabet.HasStopCodon(IUPAC.extended_protein, "*"), "-"))
        with self.assertRaises(ValueError):
            a + b

    def test_exception_when_adding_protein_with_nucletides(self):
        for a in self.protein[0:5]:
            for b in self.dna[0:3] + self.rna[0:4]:
                with self.assertRaises(TypeError):
                    a + b

    def test_adding_generic_nucleotide_with_other_nucleotides(self):
        for a in self.nuc:
            for b in self.dna + self.rna + self.nuc:
                c = a + b
                self.assertEqual(str(c), str(a) + str(b))


class TestMutableSeq(unittest.TestCase):
    def setUp(self):
        self.s = Seq.Seq("TCAAAAGGATGCATCATG", IUPAC.unambiguous_dna)
        self.mutable_s = MutableSeq("TCAAAAGGATGCATCATG", IUPAC.ambiguous_dna)

    def test_mutableseq_creation(self):
        """Test creating MutableSeqs in multiple ways"""
        mutable_s = MutableSeq("TCAAAAGGATGCATCATG", IUPAC.ambiguous_dna)
        self.assertIsInstance(mutable_s, MutableSeq, "Creating MutableSeq")

        mutable_s = self.s.tomutable()
        self.assertIsInstance(mutable_s, MutableSeq, "Converting Seq to mutable")

        array_seq = MutableSeq(array.array(array_indicator, "TCAAAAGGATGCATCATG"),
                               IUPAC.ambiguous_dna)
        self.assertIsInstance(array_seq, MutableSeq, "Creating MutableSeq using array")

    def test_repr(self):
        self.assertEqual("MutableSeq('TCAAAAGGATGCATCATG', IUPACAmbiguousDNA())",
                         repr(self.mutable_s))

    def test_truncated_repr(self):
        seq = "TCAAAAGGATGCATCATGTCAAAAGGATGCATCATGTCAAAAGGATGCATCATGTCAAAAGGA"
        expected = "MutableSeq('TCAAAAGGATGCATCATGTCAAAAGGATGCATCATGTCAAAAGGATGCATCATG...GGA', IUPACAmbiguousDNA())"
        self.assertEqual(expected, repr(MutableSeq(seq, IUPAC.ambiguous_dna)))

    def test_equal_comparison(self):
        """Test __eq__ comparison method"""
        self.assertEqual(self.mutable_s, "TCAAAAGGATGCATCATG")

    def test_equal_comparison_of_incompatible_alphabets(self):
        with warnings.catch_warnings(record=True):
            self.mutable_s == MutableSeq('UCAAAAGGA', IUPAC.ambiguous_rna)

    def test_not_equal_comparison(self):
        """Test __ne__ comparison method"""
        self.assertNotEqual(self.mutable_s, "other thing")

    def test_less_than_comparison(self):
        """Test __lt__ comparison method"""
        self.assertTrue(self.mutable_s[:-1] < self.mutable_s)

    def test_less_than_comparison_of_incompatible_alphabets(self):
        with warnings.catch_warnings(record=True):
            self.mutable_s[:-1] < MutableSeq("UCAAAAGGAUGCAUCAUG", IUPAC.ambiguous_rna)

    def test_less_than_comparison_without_alphabet(self):
        self.assertTrue(self.mutable_s[:-1] < "TCAAAAGGATGCATCATG")

    def test_less_than_or_equal_comparison(self):
        """Test __le__ comparison method"""
        self.assertTrue(self.mutable_s[:-1] <= self.mutable_s)

    def test_less_than_or_equal_comparison_of_incompatible_alphabets(self):
        with warnings.catch_warnings(record=True):
            self.mutable_s[:-1] <= MutableSeq("UCAAAAGGAUGCAUCAUG", IUPAC.ambiguous_rna)

    def test_less_than_or_equal_comparison_without_alphabet(self):
        self.assertTrue(self.mutable_s[:-1] <= "TCAAAAGGATGCATCATG")

    def test_add_method(self):
        """Test adding wrong type to MutableSeq"""
        with self.assertRaises(TypeError):
            self.mutable_s + 1234

    def test_radd_method(self):
        self.assertEqual("TCAAAAGGATGCATCATGTCAAAAGGATGCATCATG",
                         self.mutable_s.__radd__(self.mutable_s))

    def test_radd_method_incompatible_alphabets(self):
        with self.assertRaises(TypeError):
            self.mutable_s.__radd__(MutableSeq("UCAAAAGGA", IUPAC.ambiguous_rna))

    def test_radd_method_using_seq_object(self):
        self.assertEqual("TCAAAAGGATGCATCATGTCAAAAGGATGCATCATG",
                         self.mutable_s.__radd__(self.s))

    def test_radd_method_wrong_type(self):
        with self.assertRaises(TypeError):
            self.mutable_s.__radd__(1234)

    def test_as_string(self):
        self.assertEqual("TCAAAAGGATGCATCATG", str(self.mutable_s))

    def test_length(self):
        self.assertEqual(18, len(self.mutable_s))

    def test_converting_to_immutable(self):
        self.assertIsInstance(self.mutable_s.toseq(), Seq.Seq)

    def test_first_nucleotide(self):
        self.assertEqual('T', self.mutable_s[0])

    def test_setting_slices(self):
        self.assertEqual(MutableSeq('CAAA', IUPAC.ambiguous_dna),
                         self.mutable_s[1:5], "Slice mutable seq")

        self.mutable_s[1:3] = "GAT"
        self.assertEqual(MutableSeq("TGATAAAGGATGCATCATG", IUPAC.ambiguous_dna),
                         self.mutable_s,
                         "Set slice with string and adding extra nucleotide")

        self.mutable_s[1:3] = self.mutable_s[5:7]
        self.assertEqual(MutableSeq("TAATAAAGGATGCATCATG", IUPAC.ambiguous_dna),
                         self.mutable_s, "Set slice with MutableSeq")

        self.mutable_s[1:3] = array.array(array_indicator, "GAT")
        self.assertEqual(MutableSeq("TGATTAAAGGATGCATCATG", IUPAC.ambiguous_dna),
                         self.mutable_s, "Set slice with array")

    def test_setting_item(self):
        self.mutable_s[3] = "G"
        self.assertEqual(MutableSeq("TCAGAAGGATGCATCATG", IUPAC.ambiguous_dna),
                         self.mutable_s)

    def test_deleting_slice(self):
        del self.mutable_s[4:5]
        self.assertEqual(MutableSeq("TCAAAGGATGCATCATG", IUPAC.ambiguous_dna),
                         self.mutable_s)

    def test_deleting_item(self):
        del self.mutable_s[3]
        self.assertEqual(MutableSeq("TCAAAGGATGCATCATG", IUPAC.ambiguous_dna),
                         self.mutable_s)

    def test_appending(self):
        self.mutable_s.append("C")
        self.assertEqual(MutableSeq("TCAAAAGGATGCATCATGC", IUPAC.ambiguous_dna),
                         self.mutable_s)

    def test_inserting(self):
        self.mutable_s.insert(4, "G")
        self.assertEqual(MutableSeq("TCAAGAAGGATGCATCATG", IUPAC.ambiguous_dna),
                         self.mutable_s)

    def test_popping_last_item(self):
        self.assertEqual("G", self.mutable_s.pop())

    def test_remove_items(self):
        self.mutable_s.remove("G")
        self.assertEqual(MutableSeq("TCAAAAGATGCATCATG", IUPAC.ambiguous_dna),
                         self.mutable_s, "Remove first G")

        self.assertRaises(ValueError, self.mutable_s.remove, 'Z')

    def test_count(self):
        self.assertEqual(7, self.mutable_s.count("A"))
        self.assertEqual(2, self.mutable_s.count("AA"))

    def test_index(self):
        self.assertEqual(2, self.mutable_s.index("A"))
        self.assertRaises(ValueError, self.mutable_s.index, "8888")

    def test_reverse(self):
        """Test using reverse method"""
        self.mutable_s.reverse()
        self.assertEqual(MutableSeq("GTACTACGTAGGAAAACT", IUPAC.ambiguous_dna),
                         self.mutable_s)

    def test_reverse_with_stride(self):
        """Test reverse using -1 stride"""
        self.assertEqual(MutableSeq("GTACTACGTAGGAAAACT", IUPAC.ambiguous_dna),
                         self.mutable_s[::-1])

    def test_complement(self):
        self.mutable_s.complement()
        self.assertEqual(str("AGTTTTCCTACGTAGTAC"), str(self.mutable_s))

    def test_complement_rna(self):
        seq = Seq.MutableSeq("AUGaaaCUG", IUPAC.unambiguous_rna)
        seq.complement()
        self.assertEqual(str("UACuuuGAC"), str(seq))

    def test_complement_mixed_aphabets(self):
        seq = Seq.MutableSeq("AUGaaaCTG")
        with self.assertRaises(ValueError):
            seq.complement()

    def test_complement_rna_string(self):
        seq = Seq.MutableSeq("AUGaaaCUG")
        seq.complement()
        self.assertEqual('UACuuuGAC', str(seq))

    def test_complement_dna_string(self):
        seq = Seq.MutableSeq("ATGaaaCTG")
        seq.complement()
        self.assertEqual('TACtttGAC', str(seq))

    def test_reverse_complement(self):
        self.mutable_s.reverse_complement()
        self.assertEqual("CATGATGCATCCTTTTGA", str(self.mutable_s))

    def test_reverse_complement_of_protein(self):
        seq = Seq.MutableSeq("ACTGTCGTCT", Alphabet.generic_protein)
        with self.assertRaises(ValueError):
            seq.reverse_complement()

    def test_to_string_method(self):
        """This method is currently deprecated, probably will need to remove this test soon"""
        with warnings.catch_warnings(record=True):
            self.mutable_s.tostring()

    def test_extend_method(self):
        self.mutable_s.extend("GAT")
        self.assertEqual(MutableSeq("TCAAAAGGATGCATCATGGAT", IUPAC.ambiguous_dna),
                         self.mutable_s)

    def test_extend_with_mutable_seq(self):
        self.mutable_s.extend(MutableSeq("TTT", IUPAC.ambiguous_dna))
        self.assertEqual(MutableSeq("TCAAAAGGATGCATCATGTTT", IUPAC.ambiguous_dna),
                         self.mutable_s)

    def test_delete_stride_slice(self):
        del self.mutable_s[4:6 - 1]
        self.assertEqual(MutableSeq("TCAAAGGATGCATCATG", IUPAC.ambiguous_dna),
                         self.mutable_s)

    def test_extract_third_nucleotide(self):
        """Test extracting every third nucleotide (slicing with stride 3)"""
        self.assertEqual(MutableSeq("TAGTAA", IUPAC.ambiguous_dna), self.mutable_s[0::3])
        self.assertEqual(MutableSeq("CAGGTT", IUPAC.ambiguous_dna), self.mutable_s[1::3])
        self.assertEqual(MutableSeq("AAACCG", IUPAC.ambiguous_dna), self.mutable_s[2::3])

    def test_set_wobble_codon_to_n(self):
        """Test setting wobble codon to N (set slice with stride 3)"""
        self.mutable_s[2::3] = "N" * len(self.mutable_s[2::3])
        self.assertEqual(MutableSeq("TCNAANGGNTGNATNATN", IUPAC.ambiguous_dna),
                         self.mutable_s)


class TestUnknownSeq(unittest.TestCase):
    def setUp(self):
        self.s = Seq.UnknownSeq(6)

    def test_construction(self):
        self.assertEqual("??????", str(Seq.UnknownSeq(6)))
        self.assertEqual("NNNNNN", str(Seq.UnknownSeq(6, Alphabet.generic_dna)))
        self.assertEqual("XXXXXX", str(Seq.UnknownSeq(6, Alphabet.generic_protein)))
        self.assertEqual("??????", str(Seq.UnknownSeq(6, character="?")))

        with self.assertRaises(ValueError):
            Seq.UnknownSeq(-10)

        with self.assertRaises(ValueError):
            Seq.UnknownSeq(6, character='??')

    def test_length(self):
        self.assertEqual(6, len(self.s))

    def test_repr(self):
        self.assertEqual("UnknownSeq(6, alphabet = Alphabet(), character = '?')",
                         repr(self.s))

    def test_add_method(self):
        seq1 = Seq.UnknownSeq(3, Alphabet.generic_dna)
        self.assertEqual("??????NNN", str(self.s + seq1))

        seq2 = Seq.UnknownSeq(3, Alphabet.generic_dna)
        self.assertEqual("NNNNNN", str(seq1 + seq2))

    def test_getitem_method(self):
        self.assertEqual("", self.s[-1:-1])
        self.assertEqual("?", self.s[1])
        self.assertEqual("?", self.s[5:])
        self.assertEqual("?", self.s[:1])
        self.assertEqual("??", self.s[1:3])
        self.assertEqual("???", self.s[1:6:2])
        self.assertEqual("????", self.s[1:-1])
        with self.assertRaises(ValueError):
            self.s[1:6:0]

    def test_count(self):
        self.assertEqual(6, self.s.count("?"))
        self.assertEqual(3, self.s.count("??"))
        self.assertEqual(0, Seq.UnknownSeq(6, character="N").count("?"))
        self.assertEqual(0, Seq.UnknownSeq(6, character="N").count("??"))
        self.assertEqual(4, Seq.UnknownSeq(6, character="?").count("?", start=2))
        self.assertEqual(2, Seq.UnknownSeq(6, character="?").count("??", start=2))

    def test_complement(self):
        self.s.complement()
        self.assertEqual(str("??????"), str(self.s))

    def test_complement_of_protein(self):
        """Test reverse complement shouldn't work on a protein!"""
        seq = Seq.UnknownSeq(6, Alphabet.generic_protein)
        with self.assertRaises(ValueError):
            seq.complement()

    def test_reverse_complement(self):
        self.s.reverse_complement()
        self.assertEqual("??????", str(self.s))

    def test_reverse_complement_of_protein(self):
        seq = Seq.UnknownSeq(6, Alphabet.generic_protein)
        self.assertRaises(ValueError, seq.reverse_complement)

    def test_transcribe(self):
        self.assertEqual("??????", self.s.transcribe())

    def test_back_transcribe(self):
        self.assertEqual("??????", self.s.back_transcribe())

    def test_upper(self):
        seq = Seq.UnknownSeq(6, Alphabet.generic_dna)
        self.assertEqual("NNNNNN", str(seq.upper()))

    def test_lower(self):
        seq = Seq.UnknownSeq(6, Alphabet.generic_dna)
        self.assertEqual("nnnnnn", str(seq.lower()))

    def test_translation(self):
        self.assertEqual("XX", str(self.s.translate()))

    def test_translation_of_proteins(self):
        seq = Seq.UnknownSeq(6, IUPAC.protein)
        self.assertRaises(ValueError, seq.translate)

    def test_ungap(self):
        seq = Seq.UnknownSeq(7, alphabet=Alphabet.Gapped(Alphabet.DNAAlphabet(), "-"))
        self.assertEqual("NNNNNNN", str(seq.ungap("-")))

        seq = Seq.UnknownSeq(20, alphabet=Alphabet.Gapped(Alphabet.DNAAlphabet(), "-"), character='-')
        self.assertEqual("", seq.ungap("-"))


class TestAmbiguousComplements(unittest.TestCase):
    def test_ambiguous_values(self):
        """Test that other tests do not introduce characters to our values"""
        self.assertFalse("-" in ambiguous_dna_values)
        self.assertFalse("?" in ambiguous_dna_values)


class TestComplement(unittest.TestCase):
    def test_complement_ambiguous_dna_values(self):
        for ambig_char, values in sorted(ambiguous_dna_values.items()):
            compl_values = str(Seq.Seq(values, alphabet=IUPAC.ambiguous_dna).complement())
            self.assertEqual(set(compl_values),
                             set(ambiguous_dna_values[ambiguous_dna_complement[ambig_char]]))

    def test_complement_ambiguous_rna_values(self):
        for ambig_char, values in sorted(ambiguous_rna_values.items()):
            compl_values = str(Seq.Seq(values, alphabet=IUPAC.ambiguous_rna).complement())
            self.assertEqual(set(compl_values),
                             set(ambiguous_rna_values[ambiguous_rna_complement[ambig_char]]))

    def test_complement_incompatible_alphabets(self):
        seq = Seq.Seq("CAGGTU")
        with self.assertRaises(ValueError):
            seq.complement()


class TestReverseComplement(unittest.TestCase):
    def test_reverse_complement(self):
        test_seqs_copy = copy.copy(test_seqs)
        test_seqs_copy.pop(21)

        for nucleotide_seq in test_seqs_copy:
            if not isinstance(nucleotide_seq.alphabet, Alphabet.ProteinAlphabet) and \
                    isinstance(nucleotide_seq, Seq.Seq):
                expected = Seq.reverse_complement(nucleotide_seq)
                self.assertEqual(repr(expected), repr(nucleotide_seq.reverse_complement()))
                self.assertEqual(repr(expected[::-1]), repr(nucleotide_seq.complement()))
                self.assertEqual(str(nucleotide_seq.complement()),
                                 str(Seq.reverse_complement(nucleotide_seq))[::-1])
                self.assertEqual(str(nucleotide_seq.reverse_complement()),
                                 str(Seq.reverse_complement(nucleotide_seq)))

    def test_reverse_complement_of_mixed_dna_rna(self):
        seq = "AUGAAACTG"  # U and T
        self.assertRaises(ValueError, Seq.reverse_complement, seq)

    def test_reverse_complement_of_rna(self):
        seq = "AUGAAACUG"
        self.assertEqual("CAGUUUCAU", Seq.reverse_complement(seq))

    def test_reverse_complement_of_dna(self):
        seq = "ATGAAACTG"
        self.assertEqual("CAGTTTCAT", Seq.reverse_complement(seq))

    def test_reverse_complement_on_proteins(self):
        """Test reverse complement shouldn't work on a protein!"""
        for s in protein_seqs:
            with self.assertRaises(ValueError):
                Seq.reverse_complement(s)

            with self.assertRaises(ValueError):
                s.reverse_complement()

    def test_complement_on_proteins(self):
        """Test complement shouldn't work on a protein!"""
        for s in protein_seqs:
            with self.assertRaises(ValueError):
                s.complement()


class TestDoubleReverseComplement(unittest.TestCase):
    def test_reverse_complements(self):
        """Test double reverse complement preserves the sequence"""
        for sequence in [Seq.Seq("".join(sorted(ambiguous_rna_values))),
                         Seq.Seq("".join(sorted(ambiguous_dna_values))),
                         Seq.Seq("".join(sorted(ambiguous_rna_values)), Alphabet.generic_rna),
                         Seq.Seq("".join(sorted(ambiguous_dna_values)), Alphabet.generic_dna),
                         Seq.Seq("".join(sorted(ambiguous_rna_values)).replace("X", ""), IUPAC.IUPACAmbiguousRNA()),
                         Seq.Seq("".join(sorted(ambiguous_dna_values)).replace("X", ""), IUPAC.IUPACAmbiguousDNA()),
                         Seq.Seq("AWGAARCKG")]:  # Note no U or T
            reversed_sequence = sequence.reverse_complement()
            self.assertEqual(str(sequence),
                             str(reversed_sequence.reverse_complement()))


class TestSequenceAlphabets(unittest.TestCase):
    def test_sequence_alphabets(self):
        """Sanity test on the test sequence alphabets (see also enhancement
        bug 2597)"""
        for nucleotide_seq in test_seqs:
            if "U" in str(nucleotide_seq).upper():
                self.assertNotIsInstance(nucleotide_seq.alphabet, Alphabet.DNAAlphabet)
            if "T" in str(nucleotide_seq).upper():
                self.assertNotIsInstance(nucleotide_seq.alphabet, Alphabet.RNAAlphabet)


class TestTranscription(unittest.TestCase):
    def test_transcription_dna_into_rna(self):
        for nucleotide_seq in test_seqs:
            if isinstance(nucleotide_seq.alphabet, Alphabet.DNAAlphabet):
                expected = Seq.transcribe(nucleotide_seq)
                self.assertEqual(str(nucleotide_seq).replace("t", "u").replace("T", "U"),
                                 str(expected))

    def test_transcription_dna_string_into_rna(self):
        seq = "ATGAAACTG"
        self.assertEqual("AUGAAACUG", Seq.transcribe(seq))

    def test_seq_object_transcription_method(self):
        for nucleotide_seq in test_seqs:
            if isinstance(nucleotide_seq.alphabet, Alphabet.DNAAlphabet) and \
                    isinstance(nucleotide_seq, Seq.Seq):
                self.assertEqual(repr(Seq.transcribe(nucleotide_seq)),
                                 repr(nucleotide_seq.transcribe()))

    def test_transcription_of_rna(self):
        """Test transcription shouldn't work on RNA!"""
        seq = Seq.Seq("AUGAAACUG", IUPAC.ambiguous_rna)
        with self.assertRaises(ValueError):
            seq.transcribe()

    def test_transcription_of_proteins(self):
        """Test transcription shouldn't work on a protein!"""
        for s in protein_seqs:
            with self.assertRaises(ValueError):
                Seq.transcribe(s)

            if isinstance(s, Seq.Seq):
                with self.assertRaises(ValueError):
                    s.transcribe()

    def test_back_transcribe_rna_into_dna(self):
        for nucleotide_seq in test_seqs:
            if isinstance(nucleotide_seq.alphabet, Alphabet.RNAAlphabet):
                expected = Seq.back_transcribe(nucleotide_seq)
                self.assertEqual(str(nucleotide_seq).replace("u", "t").replace("U", "T"),
                                 str(expected))

    def test_back_transcribe_rna_string_into_dna(self):
        seq = "AUGAAACUG"
        self.assertEqual("ATGAAACTG", Seq.back_transcribe(seq))

    def test_seq_object_back_transcription_method(self):
        for nucleotide_seq in test_seqs:
            if isinstance(nucleotide_seq.alphabet, Alphabet.RNAAlphabet) and \
                    isinstance(nucleotide_seq, Seq.Seq):
                expected = Seq.back_transcribe(nucleotide_seq)
                self.assertEqual(repr(nucleotide_seq.back_transcribe()), repr(expected))

    def test_back_transcription_of_proteins(self):
        """Test back-transcription shouldn't work on a protein!"""
        for s in protein_seqs:
            with self.assertRaises(ValueError):
                Seq.back_transcribe(s)

            if isinstance(s, Seq.Seq):
                with self.assertRaises(ValueError):
                    s.back_transcribe()

    def test_back_transcription_of_dna(self):
        """Test back-transcription shouldn't work on DNA!"""
        seq = Seq.Seq("ATGAAACTG", IUPAC.ambiguous_dna)
        with self.assertRaises(ValueError):
            seq.back_transcribe()


class TestTranslating(unittest.TestCase):
    def setUp(self):
        self.test_seqs = [
            Seq.Seq("TCAAAAGGATGCATCATG", IUPAC.unambiguous_dna),
            Seq.Seq("ATGAAACTG"),
            Seq.Seq("ATGAARCTG"),
            Seq.Seq("AWGAARCKG"),  # Note no U or T
            Seq.Seq("".join(ambiguous_rna_values)),
            Seq.Seq("".join(ambiguous_dna_values)),
            Seq.Seq("".join(ambiguous_rna_values), Alphabet.generic_rna),
            Seq.Seq("".join(ambiguous_dna_values), Alphabet.generic_dna),
            Seq.Seq("".join(ambiguous_rna_values), IUPAC.IUPACAmbiguousRNA()),
            Seq.Seq("".join(ambiguous_dna_values), IUPAC.IUPACAmbiguousDNA()),
            Seq.Seq("AWGAARCKG", Alphabet.generic_dna),
            Seq.Seq("AUGAAACUG", Alphabet.generic_rna),
            Seq.Seq("ATGAAACTG", IUPAC.unambiguous_dna),
            Seq.Seq("ATGAAACTGWN", IUPAC.ambiguous_dna),
            Seq.Seq("AUGAAACUG", Alphabet.generic_rna),
            Seq.Seq("AUGAAACUG", IUPAC.unambiguous_rna),
            Seq.Seq("AUGAAACUGWN", IUPAC.ambiguous_rna),
            Seq.Seq("ATGAAACTG", Alphabet.generic_nucleotide),
            Seq.MutableSeq("ATGAAACTG", Alphabet.generic_dna),
            Seq.MutableSeq("AUGaaaCUG", IUPAC.unambiguous_rna),
        ]

    def test_translation(self):
        for nucleotide_seq in self.test_seqs:
            nucleotide_seq = nucleotide_seq[:3 * (len(nucleotide_seq) // 3)]
            if isinstance(nucleotide_seq, Seq.Seq) and 'X' not in str(nucleotide_seq):
                expected = Seq.translate(nucleotide_seq)
                self.assertEqual(repr(expected), repr(nucleotide_seq.translate()))

    def test_alphabets_of_translated_seqs(self):

        def triple_pad(s):
            """Add N to ensure length is a multiple of three (whole codons)."""
            while len(s) % 3:
                s += "N"
            return s

        self.assertEqual("IUPACProtein()", repr(self.test_seqs[0].translate().alphabet))
        self.assertEqual("ExtendedIUPACProtein()", repr(self.test_seqs[1].translate().alphabet))
        self.assertEqual("ExtendedIUPACProtein()", repr(self.test_seqs[2].translate().alphabet))
        self.assertEqual("ExtendedIUPACProtein()", repr(self.test_seqs[3].translate().alphabet))
        self.assertEqual("ExtendedIUPACProtein()", repr(self.test_seqs[10].translate().alphabet))
        self.assertEqual("ExtendedIUPACProtein()", repr(self.test_seqs[11].translate().alphabet))
        self.assertEqual("IUPACProtein()", repr(self.test_seqs[12].translate().alphabet))
        self.assertEqual("ExtendedIUPACProtein()",
                         repr(triple_pad(self.test_seqs[13]).translate().alphabet))
        self.assertEqual("ExtendedIUPACProtein()", repr(self.test_seqs[14].translate().alphabet))
        self.assertEqual("IUPACProtein()", repr(self.test_seqs[15].translate().alphabet))
        self.assertEqual("ExtendedIUPACProtein()",
                         repr(triple_pad(self.test_seqs[16]).translate().alphabet))
        self.assertEqual("ExtendedIUPACProtein()",
                         repr(triple_pad(self.test_seqs[17]).translate().alphabet))

    def test_translation_of_gapped_seq_with_gap_char_given(self):
        seq = Seq.Seq("ATG---AAACTG")
        self.assertEqual("M-KL", seq.translate(gap="-"))
        self.assertRaises(TranslationError, seq.translate, gap="~")

    def test_translation_of_gapped_seq_with_stop_codon_and_gap_char_given(self):
        seq = Seq.Seq("GTG---GCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG")
        self.assertEqual("V-AIVMGR*KGAR*", seq.translate(gap="-"))
        self.assertRaises(TranslationError, seq.translate)

    def test_translation_of_gapped_seq_with_gap_char_given_and_inferred_from_alphabet(self):
        seq = Seq.Seq("ATG---AAACTG", Gapped(IUPAC.unambiguous_dna))
        self.assertEqual("M-KL", seq.translate(gap="-"))
        self.assertRaises(ValueError, seq.translate, gap="~")

        seq = Seq.Seq("ATG~~~AAACTG", Gapped(IUPAC.unambiguous_dna))
        self.assertRaises(ValueError, seq.translate, gap="~")
        self.assertRaises(TranslationError, seq.translate, gap="-")

    def test_translation_of_gapped_seq_with_gap_char_given_and_inferred_from_alphabet2(self):
        """Test using stop codon in sequence"""
        seq = Seq.Seq("ATG---AAACTGTAG", Gapped(IUPAC.unambiguous_dna))
        self.assertEqual("M-KL*", seq.translate(gap="-"))
        self.assertRaises(ValueError, seq.translate, gap="~")

        seq = Seq.Seq("ATG---AAACTGTAG", Gapped(IUPAC.unambiguous_dna))
        self.assertEqual("M-KL@", seq.translate(gap="-", stop_symbol="@"))
        self.assertRaises(ValueError, seq.translate, gap="~")

        seq = Seq.Seq("ATG~~~AAACTGTAG", Gapped(IUPAC.unambiguous_dna))
        self.assertRaises(ValueError, seq.translate, gap="~")
        self.assertRaises(TranslationError, seq.translate, gap="-")

    def test_translation_of_gapped_seq_no_gap_char_given(self):
        seq = Seq.Seq("ATG---AAACTG")
        self.assertRaises(TranslationError, seq.translate)

    def test_translation_of_gapped_seq_no_gap_char_given_and_inferred_from_alphabet(self):
        seq = Seq.Seq("ATG---AAACTG", Gapped(IUPAC.unambiguous_dna))
        self.assertEqual("M-KL", seq.translate())

        seq = Seq.Seq("ATG~~~AAACTG", Gapped(IUPAC.unambiguous_dna))
        self.assertRaises(TranslationError, seq.translate)

        seq = Seq.Seq("ATG~~~AAACTG", Gapped(IUPAC.unambiguous_dna, "~"))
        self.assertEqual("M~KL", seq.translate())

    def test_alphabet_of_translated_gapped_seq(self):
        seq = Seq.Seq("ATG---AAACTG", Gapped(IUPAC.unambiguous_dna))
        self.assertEqual("Gapped(ExtendedIUPACProtein(), '-')", repr(seq.translate().alphabet))

        seq = Seq.Seq("ATG---AAACTG", Gapped(IUPAC.unambiguous_dna, "-"))
        self.assertEqual("Gapped(ExtendedIUPACProtein(), '-')", repr(seq.translate().alphabet))

        seq = Seq.Seq("ATG~~~AAACTG", Gapped(IUPAC.unambiguous_dna, "~"))
        self.assertEqual("Gapped(ExtendedIUPACProtein(), '~')", repr(seq.translate().alphabet))

        seq = Seq.Seq("ATG---AAACTG")
        self.assertEqual("Gapped(ExtendedIUPACProtein(), '-')", repr(seq.translate(gap="-").alphabet))

        seq = Seq.Seq("ATG~~~AAACTG")
        self.assertEqual("Gapped(ExtendedIUPACProtein(), '~')", repr(seq.translate(gap="~").alphabet))

        seq = Seq.Seq("ATG~~~AAACTGTAG")
        self.assertEqual("HasStopCodon(Gapped(ExtendedIUPACProtein(), '~'), '*')",
                         repr(seq.translate(gap="~").alphabet))

        seq = Seq.Seq("ATG---AAACTGTGA")
        self.assertEqual("HasStopCodon(Gapped(ExtendedIUPACProtein(), '-'), '*')",
                         repr(seq.translate(gap="-").alphabet))

        seq = Seq.Seq("ATG---AAACTGTGA")
        self.assertEqual("HasStopCodon(Gapped(ExtendedIUPACProtein(), '-'), '@')",
                         repr(seq.translate(gap="-", stop_symbol="@").alphabet))

    def test_translation_wrong_type(self):
        """Test translation table cannot be CodonTable"""
        seq = Seq.Seq("ATCGTA")
        with self.assertRaises(ValueError):
            seq.translate(table=ambiguous_dna_complement)

    def test_translation_of_string(self):
        seq = "GTGGCCATTGTAATGGGCCGC"
        self.assertEqual("VAIVMGR", Seq.translate(seq))

    def test_translation_of_gapped_string_with_gap_char_given(self):
        seq = "GTG---GCCATTGTAATGGGCCGC"
        expected = "V-AIVMGR"
        self.assertEqual(expected, Seq.translate(seq, gap="-"))
        self.assertRaises(TypeError, Seq.translate, seq, gap=[])
        self.assertRaises(ValueError, Seq.translate, seq, gap="-*")

    def test_translation_of_gapped_string_no_gap_char_given(self):
        seq = "GTG---GCCATTGTAATGGGCCGC"
        self.assertRaises(TranslationError, Seq.translate, seq)

    def test_translation_to_stop(self):
        for nucleotide_seq in self.test_seqs:
            nucleotide_seq = nucleotide_seq[:3 * (len(nucleotide_seq) // 3)]
            if isinstance(nucleotide_seq, Seq.Seq) and 'X' not in str(nucleotide_seq):
                short = Seq.translate(nucleotide_seq, to_stop=True)
                self.assertEqual(str(short), str(Seq.translate(nucleotide_seq).split('*')[0]))

        seq = "GTGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG"
        self.assertEqual("VAIVMGRWKGAR", Seq.translate(seq, table=2, to_stop=True))

    def test_translation_on_proteins(self):
        """Test translation shouldn't work on a protein!"""
        for s in protein_seqs:
            with self.assertRaises(ValueError):
                Seq.translate(s)

            if isinstance(s, Seq.Seq):
                with self.assertRaises(ValueError):
                    s.translate()

    def test_translation_of_invalid_codon(self):
        for codon in ["TA?", "N-N", "AC_", "Ac_"]:
            with self.assertRaises(TranslationError):
                Seq.translate(codon)

    def test_translation_of_glutamine(self):
        for codon in ['SAR', 'SAG', 'SAA']:
            self.assertEqual('Z', Seq.translate(codon))

    def test_translation_of_asparagine(self):
        for codon in ['RAY', 'RAT', 'RAC']:
            self.assertEqual('B', Seq.translate(codon))

    def test_translation_of_leucine(self):
        for codon in ['WTA', 'MTY', 'MTT', 'MTW', 'MTM', 'MTH', 'MTA', 'MTC', 'HTA']:
            self.assertEqual('J', Seq.translate(codon))

    def test_translation_with_bad_table_argument(self):
        table = dict()
        with self.assertRaises(ValueError):
            Seq.translate("GTGGCCATTGTAATGGGCCGC", table=table)

    def test_translation_with_codon_table_as_table_argument(self):
        table = standard_dna_table
        self.assertEqual("VAIVMGR", Seq.translate("GTGGCCATTGTAATGGGCCGC", table=table))

    def test_translation_incomplete_codon(self):
        with warnings.catch_warnings(record=True):
            Seq.translate("GTGGCCATTGTAATGGGCCG")

    def test_translation_extra_stop_codon(self):
        seq = "GTGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAGTAG"
        with self.assertRaises(TranslationError):
            Seq.translate(seq, table=2, cds=True)

    def test_translation_using_cds(self):
        seq = "GTGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG"
        self.assertEqual("MAIVMGRWKGAR", Seq.translate(seq, table=2, cds=True))

        seq = "GTGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCG"  # not multiple of three
        with self.assertRaises(TranslationError):
            Seq.translate(seq, table=2, cds=True)

        seq = "GTGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGA"  # no stop codon
        with self.assertRaises(TranslationError):
            Seq.translate(seq, table=2, cds=True)

        seq = "GCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG"  # no start codon
        with self.assertRaises(TranslationError):
            Seq.translate(seq, table=2, cds=True)


class TestStopCodons(unittest.TestCase):
    def setUp(self):
        self.misc_stops = "TAATAGTGAAGAAGG"

    def test_stops(self):
        for nucleotide_seq in [self.misc_stops, Seq.Seq(self.misc_stops),
                               Seq.Seq(self.misc_stops, Alphabet.generic_nucleotide),
                               Seq.Seq(self.misc_stops, Alphabet.DNAAlphabet()),
                               Seq.Seq(self.misc_stops, IUPAC.unambiguous_dna)]:
            self.assertEqual("***RR", str(Seq.translate(nucleotide_seq)))
            self.assertEqual("***RR", str(Seq.translate(nucleotide_seq, table=1)))
            self.assertEqual("***RR", str(Seq.translate(nucleotide_seq, table="SGC0")))
            self.assertEqual("**W**", str(Seq.translate(nucleotide_seq, table=2)))
            self.assertEqual("**WRR", str(Seq.translate(nucleotide_seq,
                                          table='Yeast Mitochondrial')))
            self.assertEqual("**WSS", str(Seq.translate(nucleotide_seq, table=5)))
            self.assertEqual("**WSS", str(Seq.translate(nucleotide_seq, table=9)))
            self.assertEqual("**CRR", str(Seq.translate(nucleotide_seq,
                                          table='Euplotid Nuclear')))
            self.assertEqual("***RR", str(Seq.translate(nucleotide_seq, table=11)))
            self.assertEqual("***RR", str(Seq.translate(nucleotide_seq, table='Bacterial')))

    def test_translation_of_stops(self):
        self.assertEqual(Seq.translate("TAT"), "Y")
        self.assertEqual(Seq.translate("TAR"), "*")
        self.assertEqual(Seq.translate("TAN"), "X")
        self.assertEqual(Seq.translate("NNN"), "X")

        self.assertEqual(Seq.translate("TAt"), "Y")
        self.assertEqual(Seq.translate("TaR"), "*")
        self.assertEqual(Seq.translate("TaN"), "X")
        self.assertEqual(Seq.translate("nnN"), "X")

        self.assertEqual(Seq.translate("tat"), "Y")
        self.assertEqual(Seq.translate("tar"), "*")
        self.assertEqual(Seq.translate("tan"), "X")
        self.assertEqual(Seq.translate("nnn"), "X")


if __name__ == "__main__":
    runner = unittest.TextTestRunner(verbosity=2)
    unittest.main(testRunner=runner)