def draw(self):
    dwg = self.drawing
    node_x_coord_map = self.node_x_coord_map
    node_y_coord_map = self.node_y_coord_map
    tree = self.tree
    node_mutations = collections.defaultdict(list)
    for site in tree.sites():
        for mutation in site.mutations:
            node_mutations[mutation.node].append(mutation)
    for u in tree.nodes():
        pu = (node_x_coord_map[u], node_y_coord_map[u])
        node_id = 'node_{}_{}'.format(tree.index, u)
        self.nodes.add(dwg.circle(id=node_id, center=pu, **self.node_attrs[u]))
        dx = 0
        dy = (- 5)
        labels = self.mid_labels
        if tree.is_leaf(u):
            dy = 20
        elif (tree.parent(u) != NULL):
            dx = 5
            if (tree.left_sib(u) == NULL):
                dx *= (- 1)
                labels = self.right_labels
            else:
                labels = self.left_labels
        labels.add(dwg.text(insert=((pu[0] + dx), (pu[1] + dy)), **self.node_label_attrs[u]))
        v = tree.parent(u)
        if (v != NULL):
            edge_id = 'edge_{}_{}'.format(tree.index, u)
            pv = (node_x_coord_map[v], node_y_coord_map[v])
            path = dwg.path([('M', pu), ('V', pv[1]), ('H', pv[0])], id=edge_id, **self.edge_attrs[u])
            self.edges.add(path)
        else:
            pv = (pu[0], (pu[1] - 20))
        num_mutations = len(node_mutations[u])
        delta = ((pv[1] - pu[1]) / (num_mutations + 1))
        x = pu[0]
        y = (pv[1] - delta)
        for mutation in reversed(node_mutations[u]):
            self.mutations.add(dwg.rect(insert=(x, y), **self.mutation_attrs[mutation.id]))
            dx = 5
            if (tree.left_sib(mutation.node) == NULL):
                dx *= (- 1)
                labels = self.mutation_right_labels
            else:
                labels = self.mutation_left_labels
            dy = 4
            labels.add(dwg.text(insert=((x + dx), (y + dy)), **self.mutation_label_attrs[mutation.id]))
            y -= delta
def draw(self):
    dwg = self.drawing
    node_x_coord_map = self.node_x_coord_map
    node_y_coord_map = self.node_y_coord_map
    tree = self.tree
    node_mutations = collections.defaultdict(list)
    for site in tree.sites():
        for mutation in site.mutations:
            node_mutations[mutation.node].append(mutation)
    for u in tree.nodes():
        pu = (node_x_coord_map[u], node_y_coord_map[u])
        node_id = 'node_{}_{}'.format(tree.index, u)
        self.nodes.add(dwg.circle(id=node_id, center=pu, **self.node_attrs[u]))
        dx = 0
        dy = (- 5)
        labels = self.mid_labels
        if tree.is_leaf(u):
            dy = 20
        elif (tree.parent(u) != NULL):
            dx = 5
            if (tree.left_sib(u) == NULL):
                dx *= (- 1)
                labels = self.right_labels
            else:
                labels = self.left_labels
        labels.add(dwg.text(insert=((pu[0] + dx), (pu[1] + dy)), **self.node_label_attrs[u]))
        v = tree.parent(u)
        if (v != NULL):
            edge_id = 'edge_{}_{}'.format(tree.index, u)
            pv = (node_x_coord_map[v], node_y_coord_map[v])
            path = dwg.path([('M', pu), ('V', pv[1]), ('H', pv[0])], id=edge_id, **self.edge_attrs[u])
            self.edges.add(path)
        else:
            pv = (pu[0], (pu[1] - 20))
        num_mutations = len(node_mutations[u])
        delta = ((pv[1] - pu[1]) / (num_mutations + 1))
        x = pu[0]
        y = (pv[1] - delta)
        for mutation in reversed(node_mutations[u]):
            self.mutations.add(dwg.rect(insert=(x, y), **self.mutation_attrs[mutation.id]))
            dx = 5
            if (tree.left_sib(mutation.node) == NULL):
                dx *= (- 1)
                labels = self.mutation_right_labels
            else:
                labels = self.mutation_left_labels
            dy = 4
            labels.add(dwg.text(insert=((x + dx), (y + dy)), **self.mutation_label_attrs[mutation.id]))
            y -= delta