Skip to content

TreeFit

Solution class to hold the solution for each tree fit by Arborist

Attributes:

Name Type Description
tree list of tuples of int

Edge list of tree including normal clone.

tree_idx int

Index identifier of tree in the original input

elbo float

The evidence lower bound (ELBO) computed for the tree.

q_z ndarray

The inferred approximation to the posterior cell-to-clone label.

q_y ndarray

The inferred approximation to the posterior SNV-to-cluster label.

cell_to_idx. dict[str, int]

The internal mapping of cell label to index.

snv_to_idx dict[str, int]

The internal mapping of SNV label to index.

clone_to_idx dict[str, int]

The internal mapping of clone label to index.

cluster_to_idx dict[str, int]

The internal mapping of SNV cluster label to index.

Source code in src/arborist/treefit.py
  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
@dataclass
class TreeFit:
    """
    Solution class to hold the solution for each tree fit by Arborist

    Attributes
    -----------
    tree : list of tuples of int
        Edge list of tree including normal clone.
    tree_idx : int 
        Index identifier of tree in the original input
    elbo : float
        The evidence lower bound (ELBO) computed for the tree.
    q_z : numpy.ndarray
        The inferred approximation to the posterior cell-to-clone label.
    q_y : numpy.ndarray
        The inferred approximation to the posterior SNV-to-cluster label.
    cell_to_idx. : dict[str,int]
        The internal mapping of cell label to index.
    snv_to_idx : dict[str,int]
        The internal mapping of SNV label to index.
    clone_to_idx : dict[str,int]
        The internal mapping of clone label to index.
    cluster_to_idx : dict[str,int]
        The internal mapping of SNV cluster label to index.

    """

    tree: list   
    tree_idx: int 
    elbo: float 
    q_z: np.ndarray 
    q_y: np.ndarray #the inferred approximation to the posterior SNV-to-cluster label
    cell_to_idx: dict #internal cell label to index 
    snv_to_idx: dict #intenal SNV label to index
    clone_to_idx: dict #internal clone label to index
    cluster_to_idx: dict #interal SNV cluster label to index 

    def __post_init__(self):
        self.idx_to_clone = {v: k for k, v in self.clone_to_idx.items()}
        self.idx_to_cluster = {v: k for k, v in self.cluster_to_idx.items()}

    def __str__(self):
        mystr = f"Tree {self.tree_idx}\nELBO: {self.elbo:.2f}\n"
        for u, v in self.tree:
            mystr += f" {u}->{v}\n"
        return mystr

    def _convert_q_to_dataframe(self, q, row_dict, col_dict, prefix="clone"):
        """
        converts q to a dataframe
        """
        df = pd.DataFrame(q)
        df.columns = [f"{prefix}_{col_dict[i]}" for i in range(q.shape[1])]
        df.index.name = "id"
        df.reset_index(inplace=True)
        label_dict = {val: key for key, val in row_dict.items()}
        df["id"] = df["id"].map(label_dict)

        return df

    def q_z_df(self):
        """
        Converts q_z numpy array to pandas.DataFrame

        Returns
        -------
        pandas.DataFrame
            A dataframe containing the inferred cell-to-clone label posterior distribution. 

        """
        df = self._convert_q_to_dataframe(self.q_z, self.cell_to_idx, self.idx_to_clone, prefix="clone")

        return df

    def q_y_df(self):
        """
        Converts q_z numpy array to pandas.DataFrame

        Returns
        -------
        pandas.DataFrame
            A dataframe containing the inferred SNV-to-cluster label posterior distribution. 

        """
        df = self._convert_q_to_dataframe(self.q_y, self.snv_to_idx, self.idx_to_cluster, prefix="cluster")

        return df

    def __repr__(self):
        return f"TreeFit(tree={self.tree}, tree_idx={self.tree_idx}, expected_log_likelihood={self.elbo})"

    @staticmethod
    def _map_assign(q, mydict, assign_dict):
        """
        assigns cell to the maximum a posteriori (MAP) clone/cluster
        """
        q_assign = q.argmax(axis=1)
        q_assign = [assign_dict[val] for val in q_assign]

        q_df = pd.DataFrame(q_assign, columns=["assignment"])
        q_df.index.name = "id"
        q_df.reset_index(inplace=True)
        label_dict = {val: key for key, val in mydict.items()}
        q_df["id"] = q_df["id"].map(label_dict)

        return q_df

    def map_assign_z(self):
        """
        Assigns cell to the maximum a posteriori (MAP) clone

        Returns
        -------
        pandas.DataFrame
            A dataframe with columns ['id', 'assignment'] providing the cell-to-clone MAP assignment.

        """
        return self._map_assign(self.q_z, self.cell_to_idx, self.idx_to_clone)

    def map_assign_y(self):
        """
        Assigns SNVs to the maximum a posteriori (MAP) cluster

        Returns
        -------
        pandas.DataFrame
            A dataframe with columns ['id', 'assignment'] providing the SNV-to-cluster MAP assignment
        """
        return self._map_assign(self.q_y, self.snv_to_idx, self.idx_to_cluster)

    def save_tree(self, fname, sep=" "):
        """
        Write the tree as a flat file in the same format as in the input style.

        Parameters
        ----------
        fname : str
            The name of the output file to write the tree.
        sep : str
            The delimiter to use to separate parent and child (default is " ").
        """
        with open(fname, "w+") as file:
            file.write(f"{len(self.tree)} #edges tree 0\n")
            for u, v in self.tree:
                file.write(f"{u}{sep}{v}\n")

    def visualize_tree(self,  output_file):
        """
        Visualizes a tree using Graphviz.

        Parameters
        ----------
        output_file : str
            The path to save the visualization. 


        Notes
        -----
        If a '.dot' extension is passed, the file will be saved as a dot file. Otherwise,
        the format will be autodetected by extension, i.e., png or pdf. 


        """     

        labels = defaultdict(str)


        for parent, child in self.tree:
            labels[parent] = f"{parent}"
            labels[child] = f"{child}"



        graph = pgv.AGraph(directed=True)

        # Add nodes with labels and shapes
        for parent, child in self.tree:
            graph.add_node(parent, shape="circle", label=labels[parent])
            graph.add_node(child, shape="circle", label=labels[child])
            graph.add_edge(parent, child)

        if output_file.endswith(".dot"):
            graph.write(output_file)  # Save as a DOT file

        else:
            graph.draw(output_file, prog="dot")  # Save as a PNG

    def _save_genotypes(self, fname, x=1, y=1):
        """
        Write the genotypes of each clone to a file. Default copy numbers (x,y) are (1,1)
        """
        snv_to_cluster_df = self.map_assign_y()
        snv_to_cluster = dict(
            zip(snv_to_cluster_df["id"], snv_to_cluster_df["assignment"])
        )
        tree = nx.DiGraph(self.tree)
        root = [n for n in tree if len(list(tree.predecessors(n))) == 0][0]
        geno_dict = {n: {} for n in tree}

        for node in tree:
            for cluster in tree:
                if cluster != root:
                    if node == cluster or tree.has_predecessor(node, cluster):
                        geno_dict[node][cluster] = 1
                    else:
                        geno_dict[node][cluster] = 0
                else:
                    geno_dict[node][cluster] = 0
        geno_list = []

        for n in tree:
            for j, clust in snv_to_cluster.items():
                geno_list.append([n, j, x, y, geno_dict[n][clust], 0, 1])
        geno_df = pd.DataFrame(
            geno_list, columns=["node", "snv", "x", "y", "xbar", "ybar", "segment"]
        )
        geno_df.to_csv(fname, index=False)

    def cell_entropy(self, eps=1e-12):
        """
        Computes the entropy of each cell assignment from the approximate variational posterior q_z

        Returns
        -------
        pandas.DataFrame
            A dataframe with columns ['id', 'entropy'] containing the 
            entropy of the inferred posterior distribution for each cell.

        """

        row_dict = self.cell_to_idx
        h_z = self._compute_hz(eps)
        df = pd.DataFrame(h_z)
        df.columns = ["entropy"]
        df.index.name = "id"
        df.reset_index(inplace=True)
        label_dict = {val: key for key, val in row_dict.items()}
        df["id"] = df["id"].map(label_dict)

        return df

    def snv_cluster_entropy(self, eps=1e-12):
        """
        Computes the entropy of each SNV assignment from the approximate variational posterior q_y

        Returns
        -------
        pandas.DataFrame
            A dataframe with columns ['id', 'entropy'] containing the 
            entropy of the inferred posterior distribution for each SNV.
        """
        row_dict = self.snv_to_idx
        h_y = self._compute_hy(eps)
        assert h_y.shape[0] == len(row_dict)
        df = pd.DataFrame(h_y)
        df.columns = ["entropy"]
        df.index.name = "id"
        df.reset_index(inplace=True)
        label_dict = {val: key for key, val in row_dict.items()}
        df["id"] = df["id"].map(label_dict)

        return df

    def _compute_hz(self, eps=1e-12):
        """
        Helper function to compute cell entropy
        """
        h_z = -np.sum(self.q_z * np.log(self.q_z + eps), axis=1) / self.q_z.shape[1]
        return h_z

    def _compute_hy(self, eps=1e-12):
        """
        Helper function to compute SNV entropy
        """
        h_y = -np.sum(self.q_y * np.log(self.q_y + eps), axis=1) / self.q_y.shape[1]
        return h_y

cell_entropy(eps=1e-12)

Computes the entropy of each cell assignment from the approximate variational posterior q_z

Returns:

Type Description
DataFrame

A dataframe with columns ['id', 'entropy'] containing the entropy of the inferred posterior distribution for each cell.

Source code in src/arborist/treefit.py
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
def cell_entropy(self, eps=1e-12):
    """
    Computes the entropy of each cell assignment from the approximate variational posterior q_z

    Returns
    -------
    pandas.DataFrame
        A dataframe with columns ['id', 'entropy'] containing the 
        entropy of the inferred posterior distribution for each cell.

    """

    row_dict = self.cell_to_idx
    h_z = self._compute_hz(eps)
    df = pd.DataFrame(h_z)
    df.columns = ["entropy"]
    df.index.name = "id"
    df.reset_index(inplace=True)
    label_dict = {val: key for key, val in row_dict.items()}
    df["id"] = df["id"].map(label_dict)

    return df

map_assign_y()

Assigns SNVs to the maximum a posteriori (MAP) cluster

Returns:

Type Description
DataFrame

A dataframe with columns ['id', 'assignment'] providing the SNV-to-cluster MAP assignment

Source code in src/arborist/treefit.py
129
130
131
132
133
134
135
136
137
138
def map_assign_y(self):
    """
    Assigns SNVs to the maximum a posteriori (MAP) cluster

    Returns
    -------
    pandas.DataFrame
        A dataframe with columns ['id', 'assignment'] providing the SNV-to-cluster MAP assignment
    """
    return self._map_assign(self.q_y, self.snv_to_idx, self.idx_to_cluster)

map_assign_z()

Assigns cell to the maximum a posteriori (MAP) clone

Returns:

Type Description
DataFrame

A dataframe with columns ['id', 'assignment'] providing the cell-to-clone MAP assignment.

Source code in src/arborist/treefit.py
117
118
119
120
121
122
123
124
125
126
127
def map_assign_z(self):
    """
    Assigns cell to the maximum a posteriori (MAP) clone

    Returns
    -------
    pandas.DataFrame
        A dataframe with columns ['id', 'assignment'] providing the cell-to-clone MAP assignment.

    """
    return self._map_assign(self.q_z, self.cell_to_idx, self.idx_to_clone)

q_y_df()

Converts q_z numpy array to pandas.DataFrame

Returns:

Type Description
DataFrame

A dataframe containing the inferred SNV-to-cluster label posterior distribution.

Source code in src/arborist/treefit.py
84
85
86
87
88
89
90
91
92
93
94
95
96
def q_y_df(self):
    """
    Converts q_z numpy array to pandas.DataFrame

    Returns
    -------
    pandas.DataFrame
        A dataframe containing the inferred SNV-to-cluster label posterior distribution. 

    """
    df = self._convert_q_to_dataframe(self.q_y, self.snv_to_idx, self.idx_to_cluster, prefix="cluster")

    return df

q_z_df()

Converts q_z numpy array to pandas.DataFrame

Returns:

Type Description
DataFrame

A dataframe containing the inferred cell-to-clone label posterior distribution.

Source code in src/arborist/treefit.py
70
71
72
73
74
75
76
77
78
79
80
81
82
def q_z_df(self):
    """
    Converts q_z numpy array to pandas.DataFrame

    Returns
    -------
    pandas.DataFrame
        A dataframe containing the inferred cell-to-clone label posterior distribution. 

    """
    df = self._convert_q_to_dataframe(self.q_z, self.cell_to_idx, self.idx_to_clone, prefix="clone")

    return df

save_tree(fname, sep=' ')

Write the tree as a flat file in the same format as in the input style.

Parameters:

Name Type Description Default
fname str

The name of the output file to write the tree.

required
sep str

The delimiter to use to separate parent and child (default is " ").

' '
Source code in src/arborist/treefit.py
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
def save_tree(self, fname, sep=" "):
    """
    Write the tree as a flat file in the same format as in the input style.

    Parameters
    ----------
    fname : str
        The name of the output file to write the tree.
    sep : str
        The delimiter to use to separate parent and child (default is " ").
    """
    with open(fname, "w+") as file:
        file.write(f"{len(self.tree)} #edges tree 0\n")
        for u, v in self.tree:
            file.write(f"{u}{sep}{v}\n")

snv_cluster_entropy(eps=1e-12)

Computes the entropy of each SNV assignment from the approximate variational posterior q_y

Returns:

Type Description
DataFrame

A dataframe with columns ['id', 'entropy'] containing the entropy of the inferred posterior distribution for each SNV.

Source code in src/arborist/treefit.py
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
def snv_cluster_entropy(self, eps=1e-12):
    """
    Computes the entropy of each SNV assignment from the approximate variational posterior q_y

    Returns
    -------
    pandas.DataFrame
        A dataframe with columns ['id', 'entropy'] containing the 
        entropy of the inferred posterior distribution for each SNV.
    """
    row_dict = self.snv_to_idx
    h_y = self._compute_hy(eps)
    assert h_y.shape[0] == len(row_dict)
    df = pd.DataFrame(h_y)
    df.columns = ["entropy"]
    df.index.name = "id"
    df.reset_index(inplace=True)
    label_dict = {val: key for key, val in row_dict.items()}
    df["id"] = df["id"].map(label_dict)

    return df

visualize_tree(output_file)

Visualizes a tree using Graphviz.

Parameters:

Name Type Description Default
output_file str

The path to save the visualization.

required
Notes

If a '.dot' extension is passed, the file will be saved as a dot file. Otherwise, the format will be autodetected by extension, i.e., png or pdf.

Source code in src/arborist/treefit.py
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
def visualize_tree(self,  output_file):
    """
    Visualizes a tree using Graphviz.

    Parameters
    ----------
    output_file : str
        The path to save the visualization. 


    Notes
    -----
    If a '.dot' extension is passed, the file will be saved as a dot file. Otherwise,
    the format will be autodetected by extension, i.e., png or pdf. 


    """     

    labels = defaultdict(str)


    for parent, child in self.tree:
        labels[parent] = f"{parent}"
        labels[child] = f"{child}"



    graph = pgv.AGraph(directed=True)

    # Add nodes with labels and shapes
    for parent, child in self.tree:
        graph.add_node(parent, shape="circle", label=labels[parent])
        graph.add_node(child, shape="circle", label=labels[child])
        graph.add_edge(parent, child)

    if output_file.endswith(".dot"):
        graph.write(output_file)  # Save as a DOT file

    else:
        graph.draw(output_file, prog="dot")  # Save as a PNG