File: maf_tile_2.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 (278 lines) | stat: -rwxr-xr-x 11,428 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
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
#!/usr/bin/python3

"""
'Tile' the blocks of a maf file over each of a set of intervals. The
highest scoring block that covers any part of a region will be used, and
pieces not covered by any block filled with "-" or optionally "*".

This version uses synteny annotation if found on the alignment blocks, and
will attempt to fill gaps with special characters depending on the type of
gap, similar to the projected alignment display of the UCSC genome browser:
'*' for new, '=' for inverse/inset, '#' for contig, 'X' for missing.

- The list of species to tile is specified by the first argument (either a
  newick tree or just a comma separated list).

- The `seq_db` is a lookup table mapping species and chromosome names
  to nib file for filling in the reference species sequence. In this file
  column 1 contains the species, column 2 the chromomsome or contig, and
  column 4 the directory containing the sequences in nib format.

- The remaining arguments are a list of maf files which must have
  corresponding ".index" files.

TODO: The seq_db format is specific to something old and obsure at PSU,
      need to standardize.

usage: %prog list,of,species,to,keep seq_db_file indexed_maf_files ...
    -m, --missingData: Inserts wildcards for missing block rows instead of '-'
    -s, --strand:      Use strand information for intervals, reveres complement if '-'
"""

import sys

import bx.align as align
import bx.align.maf as maf
import bx.seq.nib
from bx.cookbook import doc_optparse

tree_tx = str.maketrans("(),", "   ")


def main():
    options, args = doc_optparse.parse(__doc__)
    try:
        sources = args[0].translate(tree_tx).split()
        seq_db = load_seq_db(args[1])
        index = maf.MultiIndexed(args[2:])

        out = maf.Writer(sys.stdout)
        missing_data = bool(options.missingData)
        use_strand = bool(options.strand)
    except Exception:
        doc_optparse.exception()

    for line in sys.stdin:
        fields = line.split()
        ref_src, start, end = fields[0:3]
        if use_strand and len(fields) > 5:
            strand = fields[5]
        else:
            strand = "+"
        do_interval(sources, index, out, ref_src, int(start), int(end), seq_db, missing_data, strand)

    out.close()


def load_seq_db(fname):
    db = {}
    for line in open(fname):
        fields = line.split(",")
        src = fields[1] + "." + fields[2]
        seq = fields[4]
        db[src] = seq.strip()
    return db


def get_fill_char(maf_status):
    """
    Return the character that should be used to fill between blocks
    having a given status
    """
    # assert maf_status not in (maf.MAF_CONTIG_NESTED_STATUS, maf.MAF_NEW_NESTED_STATUS,
    #                           maf.MAF_MAYBE_NEW_NESTED_STATUS ), \
    #     "Nested rows do not make sense in a single coverage MAF (or do they?)"
    if maf_status in (
        maf.MAF_NEW_STATUS,
        maf.MAF_MAYBE_NEW_STATUS,
        maf.MAF_NEW_NESTED_STATUS,
        maf.MAF_MAYBE_NEW_NESTED_STATUS,
    ):
        return "*"
    elif maf_status in (maf.MAF_INVERSE_STATUS, maf.MAF_INSERT_STATUS):
        return "="
    elif maf_status in (maf.MAF_CONTIG_STATUS, maf.MAF_CONTIG_NESTED_STATUS):
        return "#"
    elif maf_status == maf.MAF_MISSING_STATUS:
        return "X"
    else:
        raise ValueError("Unknwon maf status")


def guess_fill_char(left_comp, right_comp):
    """
    For the case where there is no annotated synteny we will try to guess it
    """
    # No left component, obiously new
    return "*"
    # First check that the blocks have the same src (not just species) and
    # orientation
    if left_comp.src == right_comp.src and left_comp.strand != right_comp.strand:
        # Are they completely contiguous? Easy to call that a gap
        if left_comp.end == right_comp.start:
            return "-"
        # TODO: should be able to make some guesses about short insertions
        # here
    # All other cases we have no clue about
    return "*"


def remove_all_gap_columns(texts):
    """
    Remove any columns containing only gaps from alignment texts
    """
    seqs = [list(t) for t in texts]
    i = 0
    text_size = len(texts[0])
    while i < text_size:
        all_gap = True
        for seq in seqs:
            if seq[i] not in ("-", "#", "*", "=", "X", "@"):
                all_gap = False
        if all_gap:
            for seq in seqs:
                del seq[i]
            text_size -= 1
        else:
            i += 1
    return ["".join(s) for s in seqs]


def do_interval(sources, index, out, ref_src, start, end, seq_db, missing_data, strand):
    """
    Join together alignment blocks to create a semi human projected local
    alignment (small reference sequence deletions are kept as supported by
    the local alignment).
    """
    ref_src_size = None
    # Make sure the reference component is also the first in the source list
    assert sources[0].split(".")[0] == ref_src.split(".")[0], "{} != {}".format(
        sources[0].split(".")[0],
        ref_src.split(".")[0],
    )
    # Counter for the last reference species base we have processed
    last_stop = start
    # Rows in maf blocks come in in arbitrary order, we'll convert things
    # to the destred order of the tiled block
    source_to_index = {name: i for (i, name) in enumerate(sources)}
    # This gets all the maf blocks overlapping our interval of interest
    # NOTE: Unlike maf_tile we're expecting
    # things to be single coverage in the reference species, so we won't
    # sort by score and lay down.
    blocks = index.get(ref_src, start, end)
    # The last component seen for each species onto which we are tiling
    last_components = [None] * len(sources)
    last_status = [None] * len(sources)
    cols_needing_fill = [0] * len(sources)
    # The list of strings in which we build up the tiled alignment
    tiled_rows = ["" for i in range(len(sources))]
    # Enumerate the (ordered) list of blocks
    for i, block in enumerate(blocks):
        # Check for overlap in reference species
        ref = block.get_component_by_src_start(ref_src)
        if ref.start < last_stop:
            if ref.end < last_stop:
                continue
            block = block.slice_by_component(ref, last_stop, min(end, ref.end))
            ref = block.get_component_by_src_start(ref_src)
        block = block.slice_by_component(ref, max(start, ref.start), min(end, ref.end))
        ref = block.get_component_by_src_start(ref_src)
        # print block
        assert (
            last_components[0] is None or ref.start >= last_components[0].end
        ), "MAF must be sorted and single coverage in reference species!"
        assert ref.strand == "+", "MAF must have all reference species blocks on the plus strand"
        # Store the size of the reference sequence for building fake block
        if ref_src_size is None:
            ref_src_size = ref.src_size
        # Handle the reference component separately, it has no synteny status
        # but we will try to fill in missing sequence
        if ref.start > last_stop:
            # Need to fill in some reference sequence
            chunk_len = ref.start - last_stop
            text = bx.seq.nib.NibFile(open(seq_db[ref_src])).get(last_stop, chunk_len)
            tiled_rows[0] += text
            for source in sources[1:]:
                cols_needing_fill[source_to_index[source]] += chunk_len
        # Do reference component
        chunk_len = len(ref.text)
        tiled_rows[0] += ref.text
        # Do each other component
        for source in sources[1:]:
            source_index = source_to_index[source]
            comp = block.get_component_by_src_start(source)
            if comp:
                if comp.synteny_left is None:
                    left_status, left_length = None, -1
                else:
                    left_status, left_length = comp.synteny_left
                if comp.synteny_right is None:
                    right_status, right_length = None, -1
                else:
                    right_status, right_length = comp.synteny_right
                # We have a component, do we need to do some filling?
                cols_to_fill = cols_needing_fill[source_index]
                if cols_to_fill > 0:
                    # Adjacent components should have matching status
                    # assert last_status[ source_index ] is None or last_status[ source_index ] == left_status, \
                    #     "left status (%s) does not match right status (%s) of last component for %s" \
                    #     % ( left_status, last_status[ source_index ], source )
                    if left_status is None:
                        fill_char = guess_fill_char(last_components[source_index], comp)
                    else:
                        fill_char = get_fill_char(left_status)
                    tiled_rows[source_index] += fill_char * cols_to_fill
                    cols_needing_fill[source_index] = 0
                # Okay, filled up to current position, now append the text
                tiled_rows[source_index] += comp.text
                assert len(tiled_rows[source_index]) == len(
                    tiled_rows[0]
                ), "length of tiled row should match reference row"
                last_components[source_index] = comp
                last_status[source_index] = right_status
            else:
                # No component, we'll have to fill this region when we know
                # the status
                cols_needing_fill[source_index] += chunk_len
        last_stop = ref.end
    # No more components, clean up the ends
    if last_stop < end:
        # Need to fill in some reference sequence
        chunk_len = end - last_stop
        tiled_rows[0] += bx.seq.nib.NibFile(open(seq_db[ref_src])).get(last_stop, chunk_len)
        for source in sources[1:]:
            cols_needing_fill[source_to_index[source]] += chunk_len
    # Any final filling that needs to be done?
    for source in sources[1:]:
        source_index = source_to_index[source]
        fill_needed = cols_needing_fill[source_index]
        if fill_needed > 0:
            if last_components[source_index] is None:
                # print >>sys.stderr, "Never saw any components for %s, filling with @" % source
                fill_char = "@"
            else:
                if last_status[source_index] is None:
                    fill_char = "*"
                else:
                    fill_char = get_fill_char(last_status[source_index])
            tiled_rows[source_index] += fill_char * fill_needed
        assert len(tiled_rows[source_index]) == len(tiled_rows[0]), "length of tiled row should match reference row"
    # Okay, now make up the fake alignment from the tiled rows.
    tiled_rows = remove_all_gap_columns(tiled_rows)
    a = align.Alignment()
    for i, name in enumerate(sources):
        text = "".join(tiled_rows[i])
        size = len(text) - text.count("-")
        if i == 0:
            if ref_src_size is None:
                ref_src_size = bx.seq.nib.NibFile(open(seq_db[ref_src])).length
            c = align.Component(ref_src, start, end - start, "+", ref_src_size, text)
        else:
            c = align.Component(name + ".fake", 0, size, "?", size, text)
        a.add_component(c)
    if strand == "-":
        a = a.reverse_complement()
    out.write(a)


main()