Ejemplo n.º 1
0
    def test_to_html(self):
        """produce correct html formatted text"""
        seq = DnaSequence("ACGGTGGGGGGGGG")
        got = seq.to_html(wrap=50)
        # ensure balanced tags are in the txt
        for tag in [
                "<style>", "</style>", "<div", "</div>", "<table>", "</table>"
        ]:
            self.assertTrue(tag in got)

        seq_row = ('<tr><td class="label">None</td>'
                   '<td><span class="A_dna">A</span>'
                   '<span class="C_dna">C</span>'
                   '<span class="G_dna">G</span>'
                   '<span class="G_dna">G</span>'
                   '<span class="T_dna">T</span>'
                   '<span class="G_dna">G</span>'
                   '<span class="G_dna">G</span>'
                   '<span class="G_dna">G</span>'
                   '<span class="G_dna">G</span>'
                   '<span class="G_dna">G</span>'
                   '<span class="G_dna">G</span>'
                   '<span class="G_dna">G</span>'
                   '<span class="G_dna">G</span>'
                   '<span class="G_dna">G</span></td></tr>')

        self.assertTrue(seq_row in got)
Ejemplo n.º 2
0
    def test_DnaSequence(self):
        """DnaSequence should behave as expected"""
        x = DnaSequence("tcag")
        # note: no longer preserves case
        self.assertEqual(x, "TCAG")

        x = DnaSequence("aaa") + DnaSequence("ccc")
        # note: doesn't preserve case
        self.assertEqual(x, "AAACCC")
        assert x.moltype is DNA
        self.assertRaises(AlphabetError, x.__add__, "z")
        self.assertEqual(DnaSequence("TTTAc").rc(), "GTAAA")
Ejemplo n.º 3
0
 def test_feature_copy_annotations_to(self):
     """test correct copy of annotations"""
     orig = DnaSequence("TTTTTTTTTTAAAA", name="Orig")
     annot = orig.add_annotation(Feature, "exon", "fred", [(0, 14)])
     seq = RnaSequence("UUUUUUUUUUAAAA", name="Test")
     got = annot.copy_annotations_to(seq)
     self.assertEqual(len(orig.annotations), len(got.annotations))
     for src, dest in zip(orig.annotations, got.annotations):
         self.assertEqual(src.get_coordinates(), dest.get_coordinates())
         self.assertIsInstance(src, dest.__class__)
         self.assertIs(dest.parent, seq)
     with self.assertRaises(AssertionError):
         _ = annot.copy_annotations_to(seq[:-2])
Ejemplo n.º 4
0
    def test_single_constructor(self):
        """RdbParser should use constructors if supplied"""
        to_dna = lambda x, info: DnaSequence(str(x).replace("U", "T"), info=info)
        f = list(RdbParser(self.oneseq, to_dna))
        self.assertEqual(len(f), 1)
        a = f[0]
        self.assertEqual(a, "AGTCATCTAGATHCATHC")
        self.assertEqual(
            a.info, Info({"Species": "H.Sapiens", "OriginalSeq": "AGUCAUCUAGAUHCAUHC"})
        )

        def alternativeConstr(header_lines):
            info = Info()
            for line in header_lines:
                all = line.strip().split(":", 1)
                # strip out empty lines, lines without name, lines without
                # colon
                if not all[0] or len(all) != 2:
                    continue
                name = all[0].upper()
                value = all[1].strip().upper()
                info[name] = value
            return info

        f = list(RdbParser(self.oneseq, to_dna, alternativeConstr))
        self.assertEqual(len(f), 1)
        a = f[0]
        self.assertEqual(a, "AGTCATCTAGATHCATHC")
        exp_info = Info(
            {"OriginalSeq": "AGUCAUCUAGAUHCAUHC", "Refs": {}, "SEQ": "H.SAPIENS"}
        )
        self.assertEqual(
            a.info,
            Info({"OriginalSeq": "AGUCAUCUAGAUHCAUHC", "Refs": {}, "SEQ": "H.SAPIENS"}),
        )
Ejemplo n.º 5
0
def Dna(seq, *args, **kwargs):
    seq = seq.replace("u", "t")
    seq = seq.replace("U", "T")
    d = DnaSequence(seq, *args, **kwargs)
    return d
Ejemplo n.º 6
0
 def dnastrict(x, **kwargs):
     try:
         return DnaSequence(x, **kwargs)
     except Exception:
         raise RecordError("Could not convert sequence")
Ejemplo n.º 7
0
    def test_strand_symmetry(self):
        """correctly compute test of strand symmetry"""
        from cogent3 import get_moltype
        from cogent3.core.alignment import Aligned

        seq = DnaSequence("ACGGCTGAAGCGCTCCGGGTTTAAAACG")
        ssym = seq.strand_symmetry(motif_length=1)
        assert_allclose(ssym.observed.array, [[7, 5], [7, 9]])
        assert_allclose(ssym.expected.array, [[6, 6], [8, 8]])

        # RNA too
        seq = seq.to_rna()
        ssym = seq.strand_symmetry(motif_length=1)
        assert_allclose(ssym.observed.array, [[7, 5], [7, 9]])

        # Aligned
        seq = DnaSequence("ACGGCTGAAGCGCTCCGGGTTTAAAACG")
        m, s = seq.parse_out_gaps()
        seq = Aligned(m, s)
        ssym = seq.strand_symmetry(motif_length=1)
        assert_allclose(ssym.observed.array, [[7, 5], [7, 9]])

        with self.assertRaises(TypeError):
            text = get_moltype("text")
            m, s = text.make_seq(
                "ACGGCTGAAGCGCTCCGGGTTTAAAACG").parse_out_gaps()
            s.strand_symmetry(motif_length=1)

        # with motif_length=2
        seq = DnaSequence("AC GG CT GA AG CG CT CC GG GT TT AA AA CG".replace(
            " ", ""))
        ssym = seq.strand_symmetry(motif_length=2)
        self.assertLessEqual(len(ssym.observed.keys()), 8)
        assert_allclose(ssym.observed["AA"].to_array(), [2, 1])
        assert_allclose(ssym.observed["CC"].to_array(), [1, 2])
Ejemplo n.º 8
0
 def test_to_html_deprecation_warning(self):
     """produce correct html formatted text"""
     seq = DnaSequence("ACGGTGGGGGGGGG")
     with self.assertWarns(DeprecationWarning):
         seq.to_html(interleave_len=50)