aboutsummaryrefslogtreecommitdiff
path: root/pangraph/tree.py
blob: d36b77d13b4bf7fbc9fc172e489ffc5e77ea71eb (plain)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import os, sys
import json

from math import inf
from copy import deepcopy

import numpy as np
import matplotlib.pylab as plt

from Bio.Seq import Seq

from .utils import Strand, log, flatten, panic, breakpoint, rev_cmpl
from .graph import Graph

# ------------------------------------------------------------------------
# Global variables

MAXSELFMAPS = 25

# ------------------------------------------------------------------------
# Helper functions

def nodiag(mtx):
    return mtx-np.diag(np.diag(mtx))

def parse(mtx):
    with open(mtx) as fh:
        nrows = int(fh.readline().strip())
        M, r  = np.zeros((nrows, nrows), dtype=float), 0

        del_idxs  = []
        seq_names = []
        for li, line in enumerate(fh):
            e = line.strip().split()
            n = e[0].split('/')[-1][:-3]
            if n not in seq_names:
                seq_names.append(n)
                M[li,:(li+1)] = [float(x) for x in e[1:]]
            else:
                del_idxs.append(li)

    M = np.delete(M, del_idxs, axis=0)
    M = np.delete(M, del_idxs, axis=1)

    # Symmetrize
    M = 1 - M
    M = nodiag(M + M.T)/2

    return M, seq_names

def to_list(dmtx):
    assert len(dmtx.shape) == 2 and dmtx.shape[0] == dmtx.shape[1], "expected a square matrix"

    dlst = []
    for n in range(dmtx.shape[0]):
        dlst.append(list(dmtx[n,:(n+1)]))

    return dlst

# ------------------------------------------------------------------------
# Clade and Tree classes

class Clade(object):
    # ---------------------------------
    # Internal functions

    def __init__(self, name, parent, dist, children=[]):
        self.name   = name
        self.dist   = dist
        self.parent = parent
        self.child  = children
        self.fapath = ""
        self.graph  = None

    def __str__(self):
        if self.dist is None:
            return f"{self.name} :: Unknown"
        else:
            return f"{self.name} :: {self.dist:.4f}"

    def __repr__(self):
        return self.__str__()

    # ---------------------------------
    # Static functions

    @classmethod
    def from_dict(cls, d, parent):
        N = Clade(d['name'], parent, d['dist'])
        N.child = [Clade.from_dict(child, N) for child in d['child']]
        N.fapath = d['fapath']
        N.graph  = Graph.from_dict(d['graph']) if d['graph'] is not None else None

        return N

    # ---------------------------------
    # Class methods

    def is_leaf(self):
        return len(self.child) == 0

    def postorder(self):
        for child in self.child:
            for it in child.postorder():
                yield it
        yield self

    def children_graphs(self, func=lambda n: n.graph):
        gs = []
        for child in self.child:
            if child.graph:
                gs.append(func(child))
            else:
                gs.extend(child.children_graphs(func))
        return gs

    def new_parent(self, parent, dist):
        self.parent = parent
        self.dist   = dist if dist > 0 else 0

    def to_nwk(self, wtr):
        if not self.is_leaf():
            wtr.write("(")
            for i, child in enumerate(self.child):
                if i > 0:
                    wtr.write(",")
                child.to_nwk(wtr)
            wtr.write(")")

        wtr.write(self.name)
        wtr.write(":")
        wtr.write(f"{self.dist:.6f}")

    def to_json(self):
        serialize = lambda gs: [g.to_dict() for g in gs] if isinstance(gs, list) else [gs.to_dict()]
        return {'name'     : self.name,
                'dist'     : self.dist,
                'child'    : [ child.to_json() for child in self.child ],
                'fapath'   : self.fapath,
                'graph'    : serialize(self.graph) if self.graph is not None else None }

    def set_level(self, level):
        for c in self.child:
            c.set_level(level+1)
        self.level = level

class Tree(object):
    # ------------------- 
    # Class constructor
    def __init__(self, bare=False):
        self.root   = Clade("ROOT", None, 0) if not bare else None
        self.seqs   = None
        self.leaves = None

    # ------------------- 
    # Static methods

    # Loading from json
    @classmethod
    def from_json(cls, rdr):
        data   = json.load(rdr)
        T      = Tree(bare=True)
        T.root = Clade.from_dict(data['tree'], None)

        leafs  = {n.name: n for n in T.get_leafs()}
        T.seqs = {leafs[k]:Seq(v) for k,v in data['seqs'].items()}

        return T

    # our own neighbor joining
    # Biopython implementation is WAY too slow.
    @classmethod
    def nj(cls, mtx, names, verbose=False):
        # -----------------------------
        # internal functions

        def q(D):
            n = D.shape[0]
            Q = (n-2)*D - (np.sum(D,axis=0,keepdims=True) + np.sum(D,axis=1,keepdims=True))
            np.fill_diagonal(Q, np.inf)
            return Q

        def minpair(q):
            i, j = np.unravel_index(np.argmin(q), q.shape)
            qmin = q[i, j]
            if i > j:
                i, j = j, i
            return (i, j), qmin

        def pairdists(D, i, j):
            n  = D.shape[0]
            d1 = .5*D[i,j] + 1/(2*(n-2)) * (np.sum(D[i,:], axis=0) - np.sum(D[j,:], axis=0))
            d2 = D[i,j] - d1

            # remove negative branches while keeping total fixed
            if d1 < 0:
                d2 -= d1
                d1  = 0
            if d2 < 0:
                d1 -= d2
                d2  = 0

            dnew = .5*(D[i,:] + D[j,:] - D[i, j])
            return d1, d2, dnew

        def join(D, debug=False):
            nonlocal idx
            Q = q(D)
            (i, j), qmin = minpair(Q)
            if debug:
                q0min = min(flatten(Q[:]))
                assert abs(qmin-q0min) < 1e-2, f"minimum not found correctly. returned {qmin}, expected {q0min}"
                print(f"{D}\n--> Joining {i} and {j}. d={D[i,j]}")

            node   = Clade(f"NODE_{idx:05d}", T.root, None, [T.root.child[i], T.root.child[j]])

            d1, d2, dnew = pairdists(D, i, j)
            node.child[0].new_parent(node, d1)
            node.child[1].new_parent(node, d2)

            D[i, :] = dnew
            D[:, i] = dnew
            D[i, i] = 0
            D = np.delete(D, j, axis=0)
            D = np.delete(D, j, axis=1)
            T.root.child[i] = node
            T.root.child.pop(j)

            idx = idx + 1

            return D

        # -----------------------------
        # body
        assert len(names) == len(set(names)), "non-unique names found"

        T = Tree()
        for name in names:
            T.root.child.append(Clade(name, T.root, None, children=[]))
        idx = 0

        while mtx.shape[0] > 2:
            if verbose:
                print(f"--> Matrix size={mtx.shape[0]}. Number of root child={len(T.root.child)}")
            mtx = join(mtx)

        assert mtx.shape[0] == 2
        d = mtx[0, 1]
        T.root.child[0].dist = d/2
        T.root.child[1].dist = d/2

        return T

    # ------------------- 
    # methods 

    def postorder(self):
        return self.root.postorder()

    def preterminals(self):
        for n in self.postorder():
            if n.is_leaf():
                continue

            pre_terminal = True
            for c in n.child:
                if not c.is_leaf():
                    pre_terminal = False
                    break
            if pre_terminal:
                yield n

    def node(self, name):
        for n in self.postorder():
            if n.name == name:
                return n
        return None

    def get_leafs(self):
        if self.leaves is None:
            self.leaves = [node for node in self.postorder() if node.is_leaf()]

        return self.leaves

    def num_leafs(self):
        if self.leaves is None:
            self.leaves = [node for node in self.postorder() if node.is_leaf()]

        return len(self.leaves)

    def attach(self, seqs):
        leafs = {n.name: n for n in self.get_leafs()}
        self.seqs = {leafs[name]:seq for name,seq in seqs.items()}

    def align(self, tmpdir, min_blk_len, mu, beta, extensive, edge_window, edge_extend, log_stats=False, verbose=False):
        self.root.set_level(0) # NOTE: for debug logging
        stats = {}
        # ---------------------------------------------
        # internal functions
        # debugging function that will check reconstructed sequence against known real one.
        def check(seqs, G, verbose=False):
            nerror = 0
            uncompressed_length = 0
            for n in self.get_leafs():
                if n.name not in G.seqs:
                    continue

                seq  = seqs[n]
                orig = str(seq[:]).upper()
                rec  = G.extract(n.name)
                uncompressed_length += len(orig)
                if orig != rec:
                    breakpoint("inconsistency")
                    nerror += 1

                    with open("test.fa", "w+") as out:
                        out.write(f">original\n{orig}\n")
                        out.write(f">reconstructed\n{rec}")

                    for i in range(len(orig)//100):
                        if (orig[i*100:(i+1)*100] != rec[i*100:(i+1)*100]):
                            log("-----------------")
                            log(f"O: {i} {orig[i*100:(i+1)*100]}")
                            log(f"G: {i} {rec[i*100:(i+1)*100]}")

                            diffs = [i for i in range(len(rec)) if rec[i] != orig[i]]
                            pos   = [0]
                            seq   = G.seqs[n.name]
                            for nn in seq.nodes:
                                pos.append(pos[-1] + len(G.blks[nn.blk.id].extract(n.name, nn.num)))
                            pos = pos[1:]

                            testseqs = []
                            for nn in G.seqs[n.name].nodes:
                                if nn.strand == Strand.Plus:
                                    testseqs.append("".join(G.blks[nn.blk.id].extract(n.name, nn.num)))
                                else:
                                    testseqs.append("".join(rev_cmpl(G.blks[nn.blk.id].extract(n.name, nn.num))))

            if nerror == 0:
                log("all sequences correctly reconstructed")
                tlen = np.sum([len(x) for x in G.blks.values()])
                log(f"--- total graph length: {tlen}")
                log(f"--- total input sequence: {uncompressed_length}")
                log(f"--- compression: {uncompressed_length/tlen:1.2f}")
            else:
                raise ValueError("bad sequence reconstruction")

        def merge(node1, node2):
            if node1 != node2:
                graph1, fapath1 = node1.graph, node1.fapath
                graph2, fapath2 = node2.graph, node2.fapath
                graph    = Graph.fuse(graph1, graph2)
                graph, _ = graph.union(fapath1, fapath2, f"{tmpdir}/{n.name}", min_blk_len, mu, beta, extensive, edge_window, edge_extend)
            else:
                graph = node1.graph

            for i in range(MAXSELFMAPS):
                log(f"----> merge round {i}")
                check(self.seqs, graph)
                itr = f"{tmpdir}/{n.name}_iter_{i}"
                with open(f"{itr}.fa", 'w') as fd:
                    graph.write_fasta(fd)
                graph, contin = graph.union(itr, itr, f"{tmpdir}/{n.name}_iter_{i}", min_blk_len, mu, beta, extensive, edge_window, edge_extend)
                if not contin:
                    return graph
            return graph

        # --------------------------------------------
        # body

        if self.num_leafs() == 1:
            return Graph()

        # fix trivial graphs onto the leafs
        for i, n in enumerate(self.get_leafs()):
            seq      = self.seqs[n]
            n.graph  = Graph.from_seq(n.name, str(seq).upper())
            n.fapath = f"{tmpdir}/{n.name}"
            with open(f"{n.fapath}.fa", 'w') as fd:
                n.graph.write_fasta(fd)

        for n in self.postorder():
            if n.is_leaf():
                continue
            print(f"+++LEVEL={n.level}+++")
            n.fapath = f"{tmpdir}/{n.name}"
            log(f"fusing {n.child[0].name} with {n.child[1].name} @ {n.name}")
            n.graph = merge(*n.child)
            # delete references to children graphs for cleanup
            for c in n.child:
                if log_stats:
                    stats[c.name] = {
                        'length' : [b.length for b in c.graph.blks.values()],
                        'depth'  : [b.depth for b in c.graph.blks.values()],
                    }
                c.graph = None

            check(self.seqs, n.graph)
            with open(f"{n.fapath}.fa", 'w') as fd:
                n.graph.write_fasta(fd)

                log((f"--> compression ratio: "
                       f"{n.graph.compress_ratio()}"))
                log((f"--> number of blocks: "
                       f"{len(n.graph.blks)}"))
                log((f"--> number of members: "
                       f"{len(n.graph.seqs)}"))

    def collect(self):
        if not self.root.graph:
            return None
        self.root.graph = Graph.connected_components(self.root.graph)
        return self.root.graph

    def write_nwk(self, wtr):
        self.root.to_nwk(wtr)
        wtr.write(";")

    def write_json(self, wtr, no_seqs=False):
        data = {'tree' : self.root.to_json(),
                'seqs' : None if no_seqs else {k.name:str(v) for k,v in self.seqs.items()}}
        wtr.write(json.dumps(data))