File: generate_conversion_tables.py

package info (click to toggle)
python-dnaio 1.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 524 kB
  • sloc: python: 2,726; ansic: 164; sh: 28; makefile: 15
file content (79 lines) | stat: -rw-r--r-- 1,809 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
import io


def nucleotide_complements_table():
    # A nice list of complements can be found at:
    # http://www.reverse-complement.com/ambiguity.html
    complements = dict(
        A="T",
        C="G",
        G="C",
        T="A",
        a="t",
        c="g",
        g="c",
        t="a",
        U="A",
        u="a",
        # R, purine (A, G) vs Y, pyrimidine (C, T)
        R="Y",
        Y="R",
        r="y",
        y="r",
        # K, keto (G, T) vs A, amino (A, C)
        K="M",
        M="K",
        k="m",
        m="k",
        # B, not A, vs V, not T
        B="V",
        V="B",
        b="v",
        v="b",
        # D, not C vs H, not G
        D="H",
        H="D",
        d="h",
        h="d",
        # S, W and N's complements are the same. So they are not explicitly
        # included above
    )

    table = []
    for i in range(256):
        c = chr(i)
        if c in complements:
            table.append(f"'{complements[c]}'")
        else:
            table.append(i)
    return table


def make_table(variable_name, table, columns=16):
    out = io.StringIO()
    out.write(variable_name + " = {")
    for i, literal in enumerate(table):
        if i % columns == 0:
            out.write("\n    ")
        out.write(f"{literal:3}, ")
    out.write("\n")
    out.write("};\n")
    return out.getvalue()


def main():
    with open("src/dnaio/_conversions.h", "wt", encoding="utf-8") as out:
        out.write(
            "// This file is generated by generate_conversion_tables.py\n"
            "// Please do not edit manually.\n\n"
        )
        out.write(
            make_table(
                "static const char NUCLEOTIDE_COMPLEMENTS[256]",
                nucleotide_complements_table(),
            )
        )


if __name__ == "__main__":
    main()