File: axt_to_fasta.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 (58 lines) | stat: -rwxr-xr-x 1,207 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
#!/usr/bin/python3
"""
Application to convert AXT file to FASTA file. Reads an AXT file from standard
input and writes a FASTA file to standard out.

usage: %prog < axt_file > fasta_file
"""

__author__ = "Bob Harris (rsharris@bx.psu.edu)"

import sys

import bx.align.axt


def usage(s=None):
    message = """
axt_to_fasta < axt_file > fasta_file
"""
    if s is None:
        sys.exit(message)
    else:
        sys.exit(f"{s}\n{message}")


def main():
    # check the command line

    if len(sys.argv) > 1:
        usage("give me no arguments")

    # convert the alignment blocks

    reader = bx.align.axt.Reader(sys.stdin, support_ids=True, species1="", species2="")

    for a in reader:
        if "id" in a.attributes:
            id = a.attributes["id"]
        else:
            id = None
        print_component_as_fasta(a.components[0], id)
        print_component_as_fasta(a.components[1], id)
        print()


# $$$ this should be moved to a bx.align.fasta module


def print_component_as_fasta(c, id=None):
    header = f">{c.src}_{c.start}_{c.start + c.size}"
    if id is not None:
        header += " " + id
    print(header)
    print(c.text)


if __name__ == "__main__":
    main()