File: score_tests.py

package info (click to toggle)
python-bx 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,000 kB
  • sloc: python: 17,136; ansic: 2,326; makefile: 24; sh: 8
file content (106 lines) | stat: -rw-r--r-- 3,605 bytes parent folder | download
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
"""
Tests for `bx.align.score`.
"""

import unittest
from io import StringIO

from numpy import (
    allclose,
    array,
    cumsum,
)

import bx.align.maf
import bx.align.score

aligns = [
    (
        "CCACTAGTTTTTAAATAATCTACTATCAAATAAAAGATTTGTTAATAATAAATTTTAAATCATTAACACTT",
        "CCATTTGGGTTCAAAAATTGATCTATCA----------TGGTGGATTATTATTTAGCCATTAAGGACAAAT",
        -111,
    ),
    (
        "CCACTAGTTTTTAAATAATCTAC-----AATAAAAGATTTGTTAATAAT---AAATTTTAAATCATTAA-----CACTT",
        "CCATTTGGGTTCAAAAATTGATCTATCA----------TGGTGGAT---TATTATTT-----AGCCATTAAGGACAAAT",
        -3626,
    ),
    ("CCACTAGTTTTTGATTC", "CCATTTGGGTTC-----", -299),
    ("CTTAGTTTTTGATCACC", "-----CTTGGGTTTACC", -299),
    (
        "gggaattgaacaatgagaacacatggacacaggaaggggaacatcacacacc----------ggggcctgttgtggggtggggggaag",
        "ggaactagaacaagggagacacatacaaacaacaacaacaacaacacagcccttcccttcaaagagcttatagtctgatggaggagag",
        1690,
    ),
]

mafs = """##maf
a score=2883.0
s hg17.chr1             6734 30 + 245522847 CTACCTCAGTGTGGAAGGTGGGCAGTTCTG
s rheMac1.SCAFFOLD71394 9319 30 -     13789 CTACCTCAGTGTGGAAGGTGGGCAGTTCTG

a score=8167.0
s hg17.chr1             41401 40 + 245522847 TGTGTGATTAATGCCTGAGACTGTGTGAAGTAAGAGATGG
s panTro1.chr1          49673 40 + 229575298 TGCGTGATTAATGCCTGAGATTGTGTGAAGTAAAAGATGG
s rheMac1.SCAFFOLD45837 26063 33 -     31516 TGTGTGATTAATGCCTGAGATTGTGTGAAGTAA-------
"""

nonsymm_scheme = bx.align.score.build_scoring_scheme(
    """  A    C    G    T
                                                           91    0  -31 -123
                                                         -114  100 -125  -31
                                                          -31 -125  100 -114
                                                         -123  -31 -114   91 """,
    400,
    30,
)

aligns_for_nonsymm_scheme = [("AAAACCCCGGGGTTTT", "ACGTACGTACGTACGT", -580)]

asymm_scheme = bx.align.score.build_scoring_scheme(
    """    01   02    A    C    G    T
                                                       01  200 -200  -50  100  -50  100
                                                       02 -200  200  100  -50  100  -50 """,
    0,
    0,
    gap1="\x00",
)

aligns_for_asymm_scheme = [("\x01\x01\x01\x01\x01\x01", "ACGT\x01\x02", 100)]


class BasicTests(unittest.TestCase):
    def test_scoring_text(self):
        ss = bx.align.score.hox70
        for t1, t2, score in aligns:
            self.assertEqual(bx.align.score.score_texts(ss, t1, t2), score)

    def test_align(self):
        ss = bx.align.score.hox70
        for block in bx.align.maf.Reader(StringIO(mafs)):
            self.assertEqual(bx.align.score.score_alignment(ss, block), float(block.score))

    def test_accumulate(self):
        ss = bx.align.score.hox70
        self.assertTrue(
            allclose(
                bx.align.score.accumulate_scores(ss, "-----CTTT", "CTTAGTTTA"),
                cumsum(array([-430, -30, -30, -30, -30, -31, 91, 91, -123])),
            )
        )
        self.assertTrue(
            allclose(
                bx.align.score.accumulate_scores(ss, "-----CTTT", "CTTAGTTTA", skip_ref_gaps=True),
                cumsum(array([-581, 91, 91, -123])),
            )
        )

    def test_nonsymm_scoring(self):
        ss = nonsymm_scheme
        for t1, t2, score in aligns_for_nonsymm_scheme:
            self.assertEqual(bx.align.score.score_texts(ss, t1, t2), score)

    def test_asymm_scoring(self):
        ss = asymm_scheme
        for t1, t2, score in aligns_for_asymm_scheme:
            self.assertEqual(bx.align.score.score_texts(ss, t1, t2), score)