marsilea 0.4.2__py3-none-any.whl → 0.4.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- marsilea/__init__.py +1 -1
- marsilea/_deform.py +17 -3
- marsilea/base.py +30 -6
- marsilea/dendrogram.py +37 -5
- marsilea/exceptions.py +5 -4
- marsilea/heatmap.py +2 -0
- marsilea/plotter/__init__.py +3 -1
- marsilea/plotter/_seaborn.py +8 -6
- marsilea/plotter/arc.py +1 -2
- marsilea/plotter/bar.py +8 -7
- marsilea/plotter/base.py +12 -2
- marsilea/plotter/bio.py +3 -2
- marsilea/plotter/images.py +293 -0
- marsilea/plotter/mesh.py +25 -13
- marsilea/plotter/text.py +69 -32
- marsilea/utils.py +1 -2
- marsilea-0.4.4.dist-info/METADATA +168 -0
- marsilea-0.4.4.dist-info/RECORD +30 -0
- {marsilea-0.4.2.dist-info → marsilea-0.4.4.dist-info}/WHEEL +1 -1
- oncoprinter/__init__.py +4 -0
- oncoprinter/core.py +360 -0
- oncoprinter/preset.py +258 -0
- marsilea/plotter/_images.py +0 -99
- marsilea-0.4.2.dist-info/METADATA +0 -76
- marsilea-0.4.2.dist-info/RECORD +0 -27
- {marsilea-0.4.2.dist-info → marsilea-0.4.4.dist-info/licenses}/LICENSE +0 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: marsilea
|
|
3
|
+
Version: 0.4.4
|
|
4
|
+
Dynamic: Summary
|
|
5
|
+
Project-URL: Home, https://github.com/Marsilea-viz/marsilea
|
|
6
|
+
Author: Zhihang Zheng
|
|
7
|
+
Author-email: Mr-Milk <yzheng@cemm.at>
|
|
8
|
+
License: The MIT License (MIT)
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2024 Mr-Milk
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in
|
|
20
|
+
all copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
28
|
+
THE SOFTWARE.
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Classifier: Framework :: Matplotlib
|
|
31
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
32
|
+
Classifier: Programming Language :: Python :: 3
|
|
33
|
+
Requires-Python: >=3.8
|
|
34
|
+
Requires-Dist: legendkit
|
|
35
|
+
Requires-Dist: matplotlib>=3.6
|
|
36
|
+
Requires-Dist: numpy
|
|
37
|
+
Requires-Dist: pandas
|
|
38
|
+
Requires-Dist: platformdirs
|
|
39
|
+
Requires-Dist: scipy
|
|
40
|
+
Requires-Dist: seaborn
|
|
41
|
+
Provides-Extra: dev
|
|
42
|
+
Requires-Dist: icecream; extra == 'dev'
|
|
43
|
+
Requires-Dist: mpl-fontkit; extra == 'dev'
|
|
44
|
+
Requires-Dist: numpydoc; extra == 'dev'
|
|
45
|
+
Requires-Dist: pydata-sphinx-theme; extra == 'dev'
|
|
46
|
+
Requires-Dist: pytest; extra == 'dev'
|
|
47
|
+
Requires-Dist: python-hmr; extra == 'dev'
|
|
48
|
+
Requires-Dist: ruff; extra == 'dev'
|
|
49
|
+
Requires-Dist: scikit-learn; extra == 'dev'
|
|
50
|
+
Requires-Dist: sphinx; extra == 'dev'
|
|
51
|
+
Requires-Dist: sphinx-copybutton; extra == 'dev'
|
|
52
|
+
Requires-Dist: sphinx-design; extra == 'dev'
|
|
53
|
+
Requires-Dist: sphinx-gallery; extra == 'dev'
|
|
54
|
+
Description-Content-Type: text/markdown
|
|
55
|
+
|
|
56
|
+
<p align="center">
|
|
57
|
+
<picture align="center">
|
|
58
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/Marsilea-viz/marsilea/raw/main/img/banner-dark.jpg">
|
|
59
|
+
<source media="(prefers-color-scheme: light)" srcset="https://github.com/Marsilea-viz/marsilea/raw/main/img/banner-blue.jpg">
|
|
60
|
+
<img alt="Shows a bar chart with benchmark results." src="https://github.com/Marsilea-viz/marsilea/raw/main/img/banner-dark.jpg" width="400">
|
|
61
|
+
</picture>
|
|
62
|
+
</p>
|
|
63
|
+
|
|
64
|
+
[](https://marsilea.readthedocs.io/en/stable)
|
|
65
|
+

|
|
66
|
+

|
|
67
|
+
|
|
68
|
+
# Marsilea: Declarative creation of composable visualization!
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## Documentation
|
|
73
|
+
|
|
74
|
+
You can read the documentation on Read the Docs.
|
|
75
|
+
|
|
76
|
+
[Read Documentation](https://marsilea.readthedocs.io/)
|
|
77
|
+
|
|
78
|
+
## Installation
|
|
79
|
+
|
|
80
|
+
```shell
|
|
81
|
+
pip install marsilea
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## What is Composable Visualization?
|
|
85
|
+
|
|
86
|
+
<p align="center">
|
|
87
|
+
<picture align="center">
|
|
88
|
+
<img alt="Shows a bar chart with benchmark results." src="https://github.com/Marsilea-viz/marsilea/raw/main/img/showcase.gif" width="300">
|
|
89
|
+
</picture>
|
|
90
|
+
</p>
|
|
91
|
+
|
|
92
|
+
When we do visualization, we often need to combine multiple plots to show different aspects of the data.
|
|
93
|
+
For example, we may need to create a heatmap to show the expression of genes in different cells,
|
|
94
|
+
and then create a bar chart to show the expression of genes in different cell types.
|
|
95
|
+
A visualization contains multiple plots is called a composable visualization.
|
|
96
|
+
In Marsilea, we employ a declarative approach for user to create composable visualization incrementally.
|
|
97
|
+
|
|
98
|
+
## Examples
|
|
99
|
+
|
|
100
|
+
<table>
|
|
101
|
+
<thead>
|
|
102
|
+
<tr>
|
|
103
|
+
<th>
|
|
104
|
+
<a href="https://marsilea.readthedocs.io/en/latest/examples/Gallery/plot_tiobe_index.html">
|
|
105
|
+
Bar Chart With Image
|
|
106
|
+
</a>
|
|
107
|
+
</th>
|
|
108
|
+
<th>
|
|
109
|
+
<a href="https://marsilea.readthedocs.io/en/latest/examples/Gallery/plot_oil_well.html">
|
|
110
|
+
Stacked Bar
|
|
111
|
+
</a>
|
|
112
|
+
</th>
|
|
113
|
+
<th>
|
|
114
|
+
<a href="https://marsilea.readthedocs.io/en/latest/examples/Gallery/plot_arc_diagram.html">
|
|
115
|
+
Arc Diagram
|
|
116
|
+
</a>
|
|
117
|
+
</th>
|
|
118
|
+
</tr>
|
|
119
|
+
</thead>
|
|
120
|
+
<tbody>
|
|
121
|
+
<tr>
|
|
122
|
+
<td>
|
|
123
|
+
<img src="https://marsilea.readthedocs.io/en/latest/_images/sphx_glr_plot_tiobe_index_001_2_00x.png" height="300px">
|
|
124
|
+
</td>
|
|
125
|
+
<td>
|
|
126
|
+
<img src="https://marsilea.readthedocs.io/en/latest/_images/sphx_glr_plot_oil_well_001_2_00x.png" height="300px">
|
|
127
|
+
</td>
|
|
128
|
+
<td>
|
|
129
|
+
<img src="https://marsilea.readthedocs.io/en/latest/_images/sphx_glr_plot_arc_diagram_001_2_00x.png" width="300px">
|
|
130
|
+
</td>
|
|
131
|
+
</tr>
|
|
132
|
+
</tbody>
|
|
133
|
+
</table>
|
|
134
|
+
|
|
135
|
+
<table>
|
|
136
|
+
<thead>
|
|
137
|
+
<tr>
|
|
138
|
+
<th>
|
|
139
|
+
<a href="https://marsilea.readthedocs.io/en/latest/examples/Gallery/plot_pbmc3k.html">
|
|
140
|
+
Heatmap
|
|
141
|
+
</a>
|
|
142
|
+
</th>
|
|
143
|
+
<th>
|
|
144
|
+
<a href="https://marsilea.readthedocs.io/en/latest/examples/Gallery/plot_oncoprint.html">
|
|
145
|
+
Oncoprint
|
|
146
|
+
</a>
|
|
147
|
+
</th>
|
|
148
|
+
<th>
|
|
149
|
+
<a href="https://marsilea.readthedocs.io/en/latest/examples/Gallery/plot_upset.html">
|
|
150
|
+
Upsetplot
|
|
151
|
+
</a>
|
|
152
|
+
</th>
|
|
153
|
+
</tr>
|
|
154
|
+
</thead>
|
|
155
|
+
<tbody>
|
|
156
|
+
<tr>
|
|
157
|
+
<td>
|
|
158
|
+
<img src="https://marsilea.readthedocs.io/en/latest/_images/sphx_glr_plot_pbmc3k_001_2_00x.png" width="300px">
|
|
159
|
+
</td>
|
|
160
|
+
<td>
|
|
161
|
+
<img src="https://marsilea.readthedocs.io/en/latest/_images/sphx_glr_plot_oncoprint_005_2_00x.png" width="300px">
|
|
162
|
+
</td>
|
|
163
|
+
<td>
|
|
164
|
+
<img src="https://marsilea.readthedocs.io/en/latest/_images/sphx_glr_plot_upset_001_2_00x.png" width="300px">
|
|
165
|
+
</td>
|
|
166
|
+
</tr>
|
|
167
|
+
</tbody>
|
|
168
|
+
</table>
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
marsilea/__init__.py,sha256=N_-2Ce1mWmyFqjfe6217db2yfJ5eZfGWxJjEP8krBX0,541
|
|
2
|
+
marsilea/_api.py,sha256=tymWZHfjhx8-0NNd9762znfdIu36NrARRweEIr5L1mA,283
|
|
3
|
+
marsilea/_deform.py,sha256=QRz4OGXMsQzbiIkC3ASzZayMPhHhoFsEK38oBzSeQG8,14440
|
|
4
|
+
marsilea/base.py,sha256=aj15043lJURGR83T3q8Y8UUpeVHHLsi8T9VLol6KB1I,47552
|
|
5
|
+
marsilea/dataset.py,sha256=a0mXjPu9_tRGHofnnQaTryFpxftkfqldq_ZLXMSBf7A,4410
|
|
6
|
+
marsilea/dendrogram.py,sha256=Ung43zseybZKzTEvH5P_ge3WGfsr7i7qsX7YEVDlC74,15590
|
|
7
|
+
marsilea/exceptions.py,sha256=wN5ElUZxuaJKSnnwWdkNx6P-Oc16dzSuaRPbRKWIBEM,1046
|
|
8
|
+
marsilea/heatmap.py,sha256=UhRcBFhx1JKrtndZPvQCDaiMt8Pb314PT1BtL-JndCY,4208
|
|
9
|
+
marsilea/layers.py,sha256=puXLlGGpEqAzaTqadpgpsYmIDPH33WyyHIuysRSqFZQ,12163
|
|
10
|
+
marsilea/layout.py,sha256=X8MGPlAbbr7dcZiqW4pI7sEb8U3jVaiS7t1DKOqMYLI,27758
|
|
11
|
+
marsilea/upset.py,sha256=U1Rsmo1WpCAV9z3LBlE2L4T0nAW9ols8Z36fXzmXycw,30388
|
|
12
|
+
marsilea/utils.py,sha256=y_KYs4ToiuKEsiBdmcIVtmxMXFpD4wKiJ0k7iBa11z8,2854
|
|
13
|
+
marsilea/plotter/__init__.py,sha256=vtusGk_3B5QKw0k3EMV219FT1U8ZeJse6nMOB_wmQHA,768
|
|
14
|
+
marsilea/plotter/_seaborn.py,sha256=svo1YNenW_EBOKpR-w5suYhCJoL99VrEGzL7Q8-7sX0,8330
|
|
15
|
+
marsilea/plotter/_utils.py,sha256=Efhdk-TrrAanhbXRiEVWThMYvZ4vVHZMVYMs5X3JvIM,710
|
|
16
|
+
marsilea/plotter/arc.py,sha256=44BKVGvDc_OpghfgEvaiVCovZe7_OwZiM20iepaRMFw,8139
|
|
17
|
+
marsilea/plotter/area.py,sha256=zjjAhvgKHYe9rqzcseqZqhwfpgvzm0w2FRJ_vr9Fxm4,2650
|
|
18
|
+
marsilea/plotter/bar.py,sha256=tXM5iR4cTkTrt55ff-aWoh7lxupKtA5vJ9Yn9hr7RkA,12067
|
|
19
|
+
marsilea/plotter/base.py,sha256=TbnZDjm161RF5OlK4TCfhCFR9lM96MJUrvAk_xTk_kQ,20657
|
|
20
|
+
marsilea/plotter/bio.py,sha256=34tucmxs4LM3TFZoGsrjnXTolyrzYaHVEiRe4dzDH68,5040
|
|
21
|
+
marsilea/plotter/images.py,sha256=gb0xIQhUch3rNAt3FfvuUoamSGEynoBBBky2eE754ec,9560
|
|
22
|
+
marsilea/plotter/mesh.py,sha256=eUMXX9PNHJXf9O4wpcRgO6uEFKlpft1LwshHgMuojp8,24176
|
|
23
|
+
marsilea/plotter/text.py,sha256=6S4mnAxLJLMkduKiyor03lPd86oTOJ5TojVREA9oU6s,37466
|
|
24
|
+
oncoprinter/__init__.py,sha256=efshcAD1h9s-NVJj4HLU9-hXc_LtTeIrNYqLHl-sm_g,106
|
|
25
|
+
oncoprinter/core.py,sha256=5KPnKW5ivlxPp14uJd0OtfTv-pXV2UEym8EbII2VCcw,11846
|
|
26
|
+
oncoprinter/preset.py,sha256=mBk2tFCqoTj_1ZZKRYuv4j2I3NTBa6Swc9wjzbmxRVw,8238
|
|
27
|
+
marsilea-0.4.4.dist-info/METADATA,sha256=jA6MqUk97yh7Gapgbgij-DGr_W7260WAY8l_IZJrE6c,6576
|
|
28
|
+
marsilea-0.4.4.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
|
|
29
|
+
marsilea-0.4.4.dist-info/licenses/LICENSE,sha256=2TLD8FnLJqXzg8YBRs7W3VZBwfWfp4ArDfBl-rn96Qc,1074
|
|
30
|
+
marsilea-0.4.4.dist-info/RECORD,,
|
oncoprinter/__init__.py
ADDED
oncoprinter/core.py
ADDED
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import warnings
|
|
2
|
+
from collections import Counter
|
|
3
|
+
from copy import deepcopy
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from itertools import count
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
import pandas as pd
|
|
10
|
+
from marsilea import ClusterBoard
|
|
11
|
+
from marsilea.layers import LayersMesh, FrameRect, Piece
|
|
12
|
+
from marsilea.plotter import Labels, StackBar, Numbers, ColorMesh
|
|
13
|
+
from marsilea.utils import get_canvas_size_by_data
|
|
14
|
+
|
|
15
|
+
from .preset import SHAPE_BANK, MATCH_POOL, Alteration
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def guess_alteration(event: str):
|
|
19
|
+
for alt, rule in MATCH_POOL.items():
|
|
20
|
+
if rule.is_match(event):
|
|
21
|
+
return alt
|
|
22
|
+
return Alteration.OTHER
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass(repr=False)
|
|
26
|
+
class LayerData:
|
|
27
|
+
matrix: np.ndarray
|
|
28
|
+
piece: Piece
|
|
29
|
+
color: Any
|
|
30
|
+
event: Any
|
|
31
|
+
|
|
32
|
+
def __repr__(self):
|
|
33
|
+
return f"{self.piece.label} ({self.color})"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class GenomicData:
|
|
37
|
+
"""Handle class for genomics data
|
|
38
|
+
|
|
39
|
+
Parameters
|
|
40
|
+
----------
|
|
41
|
+
data : pd.DataFrame
|
|
42
|
+
Each column is:
|
|
43
|
+
1) Sample ID
|
|
44
|
+
2) Track name
|
|
45
|
+
3) Alteration
|
|
46
|
+
samples_order : list, optional
|
|
47
|
+
The order of samples, by default None
|
|
48
|
+
tracks_order : list, optional
|
|
49
|
+
The order of tracks, by default None
|
|
50
|
+
custom_pieces : dict, optional
|
|
51
|
+
Custom pieces for each alteration, by default None
|
|
52
|
+
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
def __repr__(self):
|
|
56
|
+
ntrack, nsample = self.shape
|
|
57
|
+
return (
|
|
58
|
+
f"{ntrack} Tracks, {nsample} Samples with "
|
|
59
|
+
f"{len(self.events)} Alterations"
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
def __init__(
|
|
63
|
+
self,
|
|
64
|
+
data,
|
|
65
|
+
samples_order=None,
|
|
66
|
+
tracks_order=None,
|
|
67
|
+
custom_pieces=None,
|
|
68
|
+
):
|
|
69
|
+
self.data = data.copy()
|
|
70
|
+
self.data.columns = ["sample", "track", "event"]
|
|
71
|
+
|
|
72
|
+
if samples_order is None:
|
|
73
|
+
samples_order = self.data["sample"].unique()
|
|
74
|
+
|
|
75
|
+
self.samples = samples_order
|
|
76
|
+
self._patients_ix = dict(zip(samples_order, count(0, 1)))
|
|
77
|
+
|
|
78
|
+
if tracks_order is None:
|
|
79
|
+
tracks_order = self.data["track"].unique()
|
|
80
|
+
self.tracks = tracks_order
|
|
81
|
+
self._tracks_ix = dict(zip(tracks_order, count(0, 1)))
|
|
82
|
+
|
|
83
|
+
self._shape = (len(self.tracks), len(self.samples))
|
|
84
|
+
|
|
85
|
+
if custom_pieces is None:
|
|
86
|
+
custom_pieces = {}
|
|
87
|
+
self.custom_pieces = custom_pieces
|
|
88
|
+
|
|
89
|
+
self._process_alterations()
|
|
90
|
+
|
|
91
|
+
layers = {}
|
|
92
|
+
for e in self.events:
|
|
93
|
+
layers[e] = np.zeros(self._shape, dtype=bool)
|
|
94
|
+
|
|
95
|
+
for _, row in self.data.iterrows():
|
|
96
|
+
patient, track, event = row
|
|
97
|
+
row_ix = self._tracks_ix[track]
|
|
98
|
+
col_ix = self._patients_ix[patient]
|
|
99
|
+
layers[event][row_ix, col_ix] = True
|
|
100
|
+
|
|
101
|
+
self.layers = layers
|
|
102
|
+
|
|
103
|
+
def _process_alterations(self):
|
|
104
|
+
events_alt = dict()
|
|
105
|
+
custom_events = list(self.custom_pieces.keys())
|
|
106
|
+
raw_events = self.data["event"].unique()
|
|
107
|
+
|
|
108
|
+
unknown_alterations = []
|
|
109
|
+
for e in raw_events:
|
|
110
|
+
alt = guess_alteration(e)
|
|
111
|
+
if alt == Alteration.OTHER:
|
|
112
|
+
alt = e
|
|
113
|
+
if e not in custom_events:
|
|
114
|
+
unknown_alterations.append(e)
|
|
115
|
+
events_alt[e] = alt
|
|
116
|
+
self.data["event"] = [events_alt[e] for e in self.data["event"]]
|
|
117
|
+
self.events = self.data["event"].unique()
|
|
118
|
+
if len(unknown_alterations) > 0:
|
|
119
|
+
msg = (
|
|
120
|
+
f"Found unknown alterations: {unknown_alterations}, "
|
|
121
|
+
f"please specify a piece for this alteration."
|
|
122
|
+
)
|
|
123
|
+
warnings.warn(msg)
|
|
124
|
+
|
|
125
|
+
@property
|
|
126
|
+
def shape(self):
|
|
127
|
+
return self._shape
|
|
128
|
+
|
|
129
|
+
def get_layers_data(self, background_color="#BEBEBE"):
|
|
130
|
+
# explicitly make copy
|
|
131
|
+
bg_pieces = deepcopy(SHAPE_BANK[Alteration.BACKGROUND])
|
|
132
|
+
bg_pieces.background_color = background_color
|
|
133
|
+
|
|
134
|
+
# Add background layer
|
|
135
|
+
layers_data = [
|
|
136
|
+
LayerData(
|
|
137
|
+
np.ones(self._shape), bg_pieces, background_color, Alteration.BACKGROUND
|
|
138
|
+
)
|
|
139
|
+
]
|
|
140
|
+
for alt in self.events:
|
|
141
|
+
layer = self.layers[alt]
|
|
142
|
+
if not isinstance(alt, Alteration):
|
|
143
|
+
piece = self.custom_pieces.get(alt)
|
|
144
|
+
if piece is None:
|
|
145
|
+
# The default style for OTHER
|
|
146
|
+
piece = FrameRect(color="pink", label=alt)
|
|
147
|
+
else:
|
|
148
|
+
piece = deepcopy(piece)
|
|
149
|
+
else:
|
|
150
|
+
piece = deepcopy(SHAPE_BANK[alt])
|
|
151
|
+
color = piece.color
|
|
152
|
+
piece.background_color = background_color
|
|
153
|
+
|
|
154
|
+
layers_data.append(LayerData(layer, piece, color, alt))
|
|
155
|
+
|
|
156
|
+
return layers_data
|
|
157
|
+
|
|
158
|
+
def get_track_mutation_rate(self):
|
|
159
|
+
gb = self.data.groupby("track", sort=False, observed=True)
|
|
160
|
+
ts = {}
|
|
161
|
+
for track, df in gb:
|
|
162
|
+
ts[track] = len(df["sample"].unique())
|
|
163
|
+
counts = np.array([ts[t] for t in self.tracks])
|
|
164
|
+
return counts / len(self.samples)
|
|
165
|
+
|
|
166
|
+
def get_track_mutation_types(self):
|
|
167
|
+
gb = self.data.groupby("track", sort=False, observed=True)
|
|
168
|
+
cs = {}
|
|
169
|
+
for track, df in gb:
|
|
170
|
+
cs[track] = Counter(df["event"])
|
|
171
|
+
return pd.DataFrame(cs).fillna(0.0)
|
|
172
|
+
|
|
173
|
+
def get_sample_mutation_types(self):
|
|
174
|
+
gb = self.data.groupby("sample", sort=False, observed=True)
|
|
175
|
+
cs = {}
|
|
176
|
+
for track, df in gb:
|
|
177
|
+
cs[track] = Counter(df["event"])
|
|
178
|
+
return pd.DataFrame(cs).fillna(0.0)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
class OncoPrint(ClusterBoard):
|
|
182
|
+
"""OncoPrint plot
|
|
183
|
+
|
|
184
|
+
The oncoprint plot is a visualization for genomics data in cancer research.
|
|
185
|
+
It's first introduced by the cBioPortal project.
|
|
186
|
+
See https://www.cbioportal.org/oncoprinter for more details.
|
|
187
|
+
|
|
188
|
+
To use this class, import from oncoprinter
|
|
189
|
+
|
|
190
|
+
>>> from oncoprinter import OncoPrint
|
|
191
|
+
|
|
192
|
+
Parameters
|
|
193
|
+
----------
|
|
194
|
+
genomic_data : pd.DataFrame
|
|
195
|
+
Genomics data, each column is:
|
|
196
|
+
1) Sample ID
|
|
197
|
+
2) Track name
|
|
198
|
+
3) Alteration
|
|
199
|
+
|
|
200
|
+
patients_order : list, optional
|
|
201
|
+
The order of samples, by default None
|
|
202
|
+
tracks_order : list, optional
|
|
203
|
+
The order of tracks, by default None
|
|
204
|
+
pieces : dict, optional
|
|
205
|
+
Custom pieces for each alteration, by default None
|
|
206
|
+
See :class:`Piece <marsilea.layers.Piece>` for details
|
|
207
|
+
background_color : str, optional, default: "#BEBEBE"
|
|
208
|
+
The background color
|
|
209
|
+
shrink : tuple, optional, default: (0.8, 0.8)
|
|
210
|
+
The shrink ratio for each layer
|
|
211
|
+
width, height : float, optional
|
|
212
|
+
The size in inches to define the size of main canvas
|
|
213
|
+
aspect : float, optional, default: 2.5
|
|
214
|
+
The aspect ratio of the main canvas
|
|
215
|
+
legend_kws : dict, optional
|
|
216
|
+
The options for legend, by default None
|
|
217
|
+
See :class:`cat_legend <legendkit.cat_legend>` for details
|
|
218
|
+
name : str, optional
|
|
219
|
+
The name of this OncoPrint
|
|
220
|
+
add_tracks_names : str, optional, default: "left"
|
|
221
|
+
The position to add tracks names
|
|
222
|
+
If None, will not add tracks names
|
|
223
|
+
add_samples_names : str, optional, default: "bottom"
|
|
224
|
+
The position to add samples names
|
|
225
|
+
If None, will not add samples names
|
|
226
|
+
add_mut_perc : str, optional, default: "right"
|
|
227
|
+
The position to add mutation percentage
|
|
228
|
+
If None, will not add mutation percentage
|
|
229
|
+
add_tracks_counts : str, optional, default: "right"
|
|
230
|
+
The position to add tracks mutation counts
|
|
231
|
+
If None, will not add tracks mutation counts
|
|
232
|
+
add_mut_counts : str, optional, default: "top"
|
|
233
|
+
The position to add mutation counts
|
|
234
|
+
If None, will not add mutation counts
|
|
235
|
+
add_tracks_counts_size : float, optional, default: 0.2
|
|
236
|
+
The size of tracks mutation counts
|
|
237
|
+
add_tracks_counts_pad : float, optional, default: 0
|
|
238
|
+
The padding of tracks mutation counts
|
|
239
|
+
add_mut_counts_size : float, optional, default: 0.2
|
|
240
|
+
The size of mutation counts
|
|
241
|
+
add_mut_counts_pad : float, optional, default: 0.1
|
|
242
|
+
The padding of mutation counts
|
|
243
|
+
|
|
244
|
+
"""
|
|
245
|
+
|
|
246
|
+
def __init__(
|
|
247
|
+
self,
|
|
248
|
+
genomic_data=None,
|
|
249
|
+
patients_order=None,
|
|
250
|
+
tracks_order=None,
|
|
251
|
+
pieces=None,
|
|
252
|
+
background_color="#BEBEBE",
|
|
253
|
+
shrink=(0.8, 0.8),
|
|
254
|
+
width=None,
|
|
255
|
+
height=None,
|
|
256
|
+
aspect=2.5,
|
|
257
|
+
legend_kws=None,
|
|
258
|
+
name=None,
|
|
259
|
+
add_tracks_names="left",
|
|
260
|
+
add_samples_names="bottom",
|
|
261
|
+
add_mut_perc="right",
|
|
262
|
+
add_tracks_counts="right",
|
|
263
|
+
add_mut_counts="top",
|
|
264
|
+
add_tracks_counts_size=0.2,
|
|
265
|
+
add_tracks_counts_pad=0,
|
|
266
|
+
add_mut_counts_size=0.2,
|
|
267
|
+
add_mut_counts_pad=0.1,
|
|
268
|
+
):
|
|
269
|
+
data = GenomicData(
|
|
270
|
+
genomic_data,
|
|
271
|
+
samples_order=patients_order,
|
|
272
|
+
tracks_order=tracks_order,
|
|
273
|
+
custom_pieces=pieces,
|
|
274
|
+
)
|
|
275
|
+
self.genomic_data = data
|
|
276
|
+
width, height = get_canvas_size_by_data(
|
|
277
|
+
data.shape, width=width, height=height, scale=0.2, aspect=aspect
|
|
278
|
+
)
|
|
279
|
+
|
|
280
|
+
self.canvas = super().__init__(
|
|
281
|
+
name=name, cluster_data=np.zeros(data.shape), width=width, height=height
|
|
282
|
+
)
|
|
283
|
+
|
|
284
|
+
legend_options = dict(title="Alterations", handleheight=aspect, handlelength=1)
|
|
285
|
+
legend_kws = {} if legend_kws is None else legend_kws
|
|
286
|
+
legend_options.update(legend_kws)
|
|
287
|
+
|
|
288
|
+
layers, pieces, colors_mapper = [], [], {}
|
|
289
|
+
for layer in data.get_layers_data(background_color):
|
|
290
|
+
layers.append(layer.matrix)
|
|
291
|
+
pieces.append(layer.piece)
|
|
292
|
+
colors_mapper[layer.event] = layer.color
|
|
293
|
+
mesh = LayersMesh(
|
|
294
|
+
layers=layers, pieces=pieces, shrink=shrink, legend_kws=legend_options
|
|
295
|
+
)
|
|
296
|
+
self.add_layer(mesh)
|
|
297
|
+
|
|
298
|
+
if add_tracks_names:
|
|
299
|
+
self.add_plot(add_tracks_names, Labels(data.tracks))
|
|
300
|
+
|
|
301
|
+
# Add other statistics
|
|
302
|
+
track_mut_rate = data.get_track_mutation_rate()
|
|
303
|
+
# Convert to percentage string
|
|
304
|
+
if add_mut_perc:
|
|
305
|
+
rates = [_format_percentage(t) for t in track_mut_rate]
|
|
306
|
+
self.add_plot(add_mut_perc, Labels(rates))
|
|
307
|
+
if add_samples_names:
|
|
308
|
+
self.add_plot(add_samples_names, Labels(data.samples))
|
|
309
|
+
|
|
310
|
+
if add_tracks_counts:
|
|
311
|
+
track_counter = data.get_track_mutation_types()
|
|
312
|
+
colors = [colors_mapper[e] for e in track_counter.index]
|
|
313
|
+
track_bar = StackBar(track_counter, colors=colors, show_value=False)
|
|
314
|
+
self.add_plot(
|
|
315
|
+
add_tracks_counts,
|
|
316
|
+
track_bar,
|
|
317
|
+
legend=False,
|
|
318
|
+
size=add_tracks_counts_size,
|
|
319
|
+
pad=add_tracks_counts_pad,
|
|
320
|
+
)
|
|
321
|
+
|
|
322
|
+
if add_mut_counts:
|
|
323
|
+
patients_counter = data.get_sample_mutation_types()
|
|
324
|
+
colors = [colors_mapper[e] for e in patients_counter.index]
|
|
325
|
+
patients_bar = StackBar(patients_counter, colors=colors, show_value=False)
|
|
326
|
+
self.add_plot(
|
|
327
|
+
add_mut_counts,
|
|
328
|
+
patients_bar,
|
|
329
|
+
legend=False,
|
|
330
|
+
size=add_mut_counts_size,
|
|
331
|
+
pad=add_mut_counts_pad,
|
|
332
|
+
)
|
|
333
|
+
self.add_legends()
|
|
334
|
+
|
|
335
|
+
clinical_plots = {
|
|
336
|
+
"bar": Numbers,
|
|
337
|
+
"stack_bar": StackBar,
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
def add_clinical_data(self, data, plot="bar", size=None, pad=0.1, **kwargs):
|
|
341
|
+
data = data.loc[self.samples_order]
|
|
342
|
+
plotter = self.clinical_plots[plot]
|
|
343
|
+
self.add_bottom(plotter(data, **kwargs), size=size, pad=pad)
|
|
344
|
+
|
|
345
|
+
def add_heatmap_data(self, data, size=0.2, pad=0.1, **kwargs):
|
|
346
|
+
data = data.loc[self.samples_order]
|
|
347
|
+
options = {"cmap": "Blues", "label_loc": "left", **kwargs}
|
|
348
|
+
self.add_bottom(ColorMesh(data, **options), size=size, pad=pad)
|
|
349
|
+
|
|
350
|
+
@property
|
|
351
|
+
def samples_order(self):
|
|
352
|
+
return self.genomic_data.samples
|
|
353
|
+
|
|
354
|
+
@property
|
|
355
|
+
def tracks_order(self):
|
|
356
|
+
return self.genomic_data.tracks
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
def _format_percentage(t):
|
|
360
|
+
return f"{float(t) * 100:.2f}".rstrip("0").rstrip(".") + "%"
|