sclab 0.1.7__tar.gz → 0.2.2__tar.gz

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.
Files changed (44) hide show
  1. sclab-0.2.2/LICENSE +29 -0
  2. {sclab-0.1.7 → sclab-0.2.2}/PKG-INFO +22 -11
  3. {sclab-0.1.7 → sclab-0.2.2}/README.md +12 -6
  4. {sclab-0.1.7 → sclab-0.2.2}/pyproject.toml +11 -4
  5. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/__init__.py +3 -1
  6. sclab-0.2.2/src/sclab/_io.py +109 -0
  7. sclab-0.2.2/src/sclab/_methods_registry.py +65 -0
  8. sclab-0.2.2/src/sclab/_sclab.py +288 -0
  9. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/dataset/_dataset.py +3 -5
  10. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/dataset/processor/_processor.py +22 -15
  11. sclab-0.2.2/src/sclab/dataset/processor/_results_panel.py +80 -0
  12. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/dataset/processor/step/_processor_step_base.py +12 -6
  13. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/examples/processor_steps/__init__.py +2 -0
  14. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/examples/processor_steps/_cluster.py +2 -2
  15. sclab-0.2.2/src/sclab/examples/processor_steps/_differential_expression.py +328 -0
  16. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/examples/processor_steps/_neighbors.py +2 -2
  17. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/examples/processor_steps/_pca.py +2 -2
  18. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/examples/processor_steps/_preprocess.py +2 -2
  19. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/examples/processor_steps/_qc.py +2 -2
  20. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/examples/processor_steps/_umap.py +2 -2
  21. sclab-0.2.2/src/sclab/methods/__init__.py +28 -0
  22. sclab-0.2.2/src/sclab/scanpy/_compat.py +92 -0
  23. sclab-0.2.2/src/sclab/scanpy/_settings.py +526 -0
  24. sclab-0.2.2/src/sclab/scanpy/logging.py +290 -0
  25. sclab-0.2.2/src/sclab/scanpy/plotting/__init__.py +0 -0
  26. sclab-0.2.2/src/sclab/scanpy/plotting/_rcmod.py +73 -0
  27. sclab-0.2.2/src/sclab/scanpy/plotting/palettes.py +221 -0
  28. sclab-0.2.2/src/sclab/scanpy/readwrite.py +1108 -0
  29. sclab-0.1.7/src/sclab/_io.py +0 -32
  30. sclab-0.1.7/src/sclab/_sclab.py +0 -80
  31. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/dataset/__init__.py +0 -0
  32. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/dataset/_exceptions.py +0 -0
  33. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/dataset/plotter/__init__.py +0 -0
  34. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/dataset/plotter/_controls.py +0 -0
  35. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/dataset/plotter/_plotter.py +0 -0
  36. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/dataset/plotter/_utils.py +0 -0
  37. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/dataset/processor/__init__.py +0 -0
  38. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/dataset/processor/step/__init__.py +0 -0
  39. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/dataset/processor/step/_basic_processor_step.py +0 -0
  40. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/event/__init__.py +0 -0
  41. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/event/_broker.py +0 -0
  42. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/event/_client.py +0 -0
  43. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/event/_utils.py +0 -0
  44. {sclab-0.1.7 → sclab-0.2.2}/src/sclab/examples/__init__.py +0 -0
sclab-0.2.2/LICENSE ADDED
@@ -0,0 +1,29 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2025 Argenis Arriojas
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.3
1
+ Metadata-Version: 2.4
2
2
  Name: sclab
3
- Version: 0.1.7
3
+ Version: 0.2.2
4
4
  Summary: sclab
5
5
  Author-email: Argenis Arriojas <ArriojasMaldonado001@umb.edu>
6
6
  Requires-Python: >=3.10,<3.13
@@ -10,23 +10,28 @@ Classifier: Programming Language :: Python :: 3
10
10
  Classifier: Programming Language :: Python :: 3.10
11
11
  Classifier: Programming Language :: Python :: 3.11
12
12
  Classifier: Programming Language :: Python :: 3.12
13
+ License-File: LICENSE
13
14
  Requires-Dist: anndata
14
15
  Requires-Dist: anywidget
15
16
  Requires-Dist: ipywidgets
16
17
  Requires-Dist: itables
17
18
  Requires-Dist: numpy<2.2
18
19
  Requires-Dist: pandas
19
- Requires-Dist: plotly
20
- Requires-Dist: scanpy
20
+ Requires-Dist: plotly<6.0
21
+ Requires-Dist: requests>=2.32.3
21
22
  Requires-Dist: scikit-learn
22
- Requires-Dist: scikit-misc
23
23
  Requires-Dist: svgpathtools
24
+ Requires-Dist: tqdm>=4.67.1
25
+ Requires-Dist: jupyterlab>=4.3.6 ; extra == "jupyter"
26
+ Requires-Dist: scanpy[leiden, skmisc]>=1.10 ; extra == "scanpy"
24
27
  Requires-Dist: pytest>=8.3.4 ; extra == "test"
25
28
  Project-URL: Bug Tracker, https://github.com/umbibio/sclab/issues
26
29
  Project-URL: Changelog, https://github.com/umbibio/sclab/blob/main/CHANGELOG.md
27
30
  Project-URL: Documentation, https://github.com/umbibio/sclab/docs
28
31
  Project-URL: Homepage, https://github.com/umbibio/sclab
29
32
  Project-URL: Repository, https://github.com/umbibio/sclab.git
33
+ Provides-Extra: jupyter
34
+ Provides-Extra: scanpy
30
35
  Provides-Extra: test
31
36
 
32
37
  # SCLab
@@ -53,23 +58,29 @@ pip install sclab
53
58
  Open a Jupyter Notebook and run the following:
54
59
 
55
60
  ```python
61
+ from IPython.display import display
56
62
  from sclab import SCLabDashboard
63
+ from sclab.examples.processor_steps import QC, Preprocess, PCA, Neighbors, UMAP, Cluster
57
64
  import scanpy as sc
58
- from IPython.display import display
59
65
 
60
66
  # Load your data
61
67
  adata = sc.read_10x_h5("your_data.h5")
62
68
 
63
69
  # Create dashboard
64
70
  dashboard = SCLabDashboard(adata, name="My Analysis")
71
+ # Add desired processing steps to the interface
72
+ dashboard.pr.add_steps({"Processing": [QC, Preprocess, PCA, Neighbors, UMAP, Cluster]})
65
73
 
66
74
  # Display dashboard
67
75
  display(dashboard)
68
76
 
69
77
  # The dashboard provides easy access to components:
70
- dashboard.ds # Dataset (wrapper for AnnData)
71
- dashboard.pl # Plotter
72
- dashboard.pr # Processor
78
+ # dashboard.ds # Dataset (wrapper for AnnData)
79
+ # dashboard.pl # Plotter
80
+ # dashboard.pr # Processor
81
+
82
+ # the resulting AnnData object is found within the dataset object:
83
+ # dashboard.ds.adata
73
84
  ```
74
85
 
75
86
  ## Components
@@ -129,10 +140,10 @@ This project is licensed under the BSD 3-Clause License - see the [LICENSE](LICE
129
140
  If you use SCLab in your research, please cite:
130
141
 
131
142
  ```bibtex
132
- @software{sclab2024,
143
+ @software{sclab2025,
133
144
  author = {Arriojas, Argenis},
134
145
  title = {SCLab: Interactive Single-Cell Analysis Toolkit},
135
- year = {2024},
146
+ year = {2025},
136
147
  publisher = {GitHub},
137
148
  url = {https://github.com/umbibio/sclab}
138
149
  }
@@ -22,23 +22,29 @@ pip install sclab
22
22
  Open a Jupyter Notebook and run the following:
23
23
 
24
24
  ```python
25
+ from IPython.display import display
25
26
  from sclab import SCLabDashboard
27
+ from sclab.examples.processor_steps import QC, Preprocess, PCA, Neighbors, UMAP, Cluster
26
28
  import scanpy as sc
27
- from IPython.display import display
28
29
 
29
30
  # Load your data
30
31
  adata = sc.read_10x_h5("your_data.h5")
31
32
 
32
33
  # Create dashboard
33
34
  dashboard = SCLabDashboard(adata, name="My Analysis")
35
+ # Add desired processing steps to the interface
36
+ dashboard.pr.add_steps({"Processing": [QC, Preprocess, PCA, Neighbors, UMAP, Cluster]})
34
37
 
35
38
  # Display dashboard
36
39
  display(dashboard)
37
40
 
38
41
  # The dashboard provides easy access to components:
39
- dashboard.ds # Dataset (wrapper for AnnData)
40
- dashboard.pl # Plotter
41
- dashboard.pr # Processor
42
+ # dashboard.ds # Dataset (wrapper for AnnData)
43
+ # dashboard.pl # Plotter
44
+ # dashboard.pr # Processor
45
+
46
+ # the resulting AnnData object is found within the dataset object:
47
+ # dashboard.ds.adata
42
48
  ```
43
49
 
44
50
  ## Components
@@ -98,10 +104,10 @@ This project is licensed under the BSD 3-Clause License - see the [LICENSE](LICE
98
104
  If you use SCLab in your research, please cite:
99
105
 
100
106
  ```bibtex
101
- @software{sclab2024,
107
+ @software{sclab2025,
102
108
  author = {Arriojas, Argenis},
103
109
  title = {SCLab: Interactive Single-Cell Analysis Toolkit},
104
- year = {2024},
110
+ year = {2025},
105
111
  publisher = {GitHub},
106
112
  url = {https://github.com/umbibio/sclab}
107
113
  }
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sclab"
3
- version = "0.1.7"
3
+ version = "0.2.2"
4
4
  description = "sclab"
5
5
  readme = "README.md"
6
6
  authors = [
@@ -21,11 +21,11 @@ dependencies = [
21
21
  "itables",
22
22
  "numpy<2.2",
23
23
  "pandas",
24
- "plotly",
25
- "scanpy",
24
+ "plotly<6.0",
25
+ "requests>=2.32.3",
26
26
  "scikit-learn",
27
- "scikit-misc",
28
27
  "svgpathtools",
28
+ "tqdm>=4.67.1",
29
29
  ]
30
30
 
31
31
  [project.urls]
@@ -37,6 +37,8 @@ dependencies = [
37
37
 
38
38
  [project.optional-dependencies]
39
39
  test = ["pytest>=8.3.4"]
40
+ scanpy = ["scanpy[leiden,skmisc]>=1.10"]
41
+ jupyter = ["jupyterlab>=4.3.6"]
40
42
 
41
43
  [build-system]
42
44
  requires = ["flit_core>=3.2,<4"]
@@ -53,4 +55,9 @@ dev = [
53
55
  "ruff>=0.9.4",
54
56
  "nox>=2024.1.29",
55
57
  "pytest>=8.3.4",
58
+ "mkdocs>=1.6.1",
59
+ "mkdocs-material>=9.6.3",
60
+ "mkdocs-jupyter>=0.25.1",
61
+ "scanpy[leiden]>=1.10.4",
62
+ "jupyterlab>=4.3.6",
56
63
  ]
@@ -1,7 +1,9 @@
1
+ from . import methods
1
2
  from ._sclab import SCLabDashboard
2
3
 
3
4
  __all__ = [
5
+ "methods",
4
6
  "SCLabDashboard",
5
7
  ]
6
8
 
7
- __version__ = "0.1.7"
9
+ __version__ = "0.2.2"
@@ -0,0 +1,109 @@
1
+ from io import BytesIO
2
+ from pathlib import Path
3
+ from urllib.parse import urlparse
4
+
5
+ import requests
6
+ from anndata import AnnData, read_h5ad
7
+ from tqdm.auto import tqdm
8
+
9
+
10
+ def read_adata(path: str | Path, var_names: str = "gene_ids") -> AnnData:
11
+ from .scanpy.readwrite import read_10x_h5, read_10x_mtx
12
+
13
+ path = Path(path)
14
+
15
+ match path.suffix:
16
+ case ".h5":
17
+ adata = read_10x_h5(path)
18
+ case ".h5ad":
19
+ adata = read_h5ad(path)
20
+ case "":
21
+ assert path.is_dir()
22
+ adata = read_10x_mtx(path)
23
+ case _:
24
+ raise ValueError(
25
+ "Input file must be a 10x h5, h5ad or a folder of 10x mtx files"
26
+ )
27
+
28
+ if var_names in adata.var:
29
+ adata.var = adata.var.set_index(var_names)
30
+
31
+ return adata
32
+
33
+
34
+ def load_adata_from_url(
35
+ url: str,
36
+ var_names: str = "gene_ids",
37
+ progress: bool = True,
38
+ ) -> AnnData:
39
+ """
40
+ Load an AnnData object from a URL to an .h5ad file.
41
+
42
+ Parameters:
43
+ -----------
44
+ url : str
45
+ URL to the .h5ad file
46
+ var_names : str
47
+ Name of the variable column in the .h5ad file
48
+ progress : bool
49
+ Whether to show a progress bar
50
+
51
+ Returns:
52
+ --------
53
+ anndata.AnnData
54
+ Loaded AnnData object
55
+ """
56
+ from .scanpy.readwrite import read_10x_h5
57
+
58
+ assert is_valid_url(url), "URL is not valid"
59
+ url_path = Path(urlparse(url).path)
60
+
61
+ if url_path.suffix == ".h5":
62
+ try:
63
+ import scanpy as sc
64
+ except ImportError:
65
+ raise ImportError("Please install scanpy: `pip install scanpy`")
66
+
67
+ file_content = fetch_file(url, progress=progress)
68
+ match url_path.suffix:
69
+ case ".h5":
70
+ adata = read_10x_h5(file_content)
71
+ case ".h5ad":
72
+ adata = read_h5ad(file_content)
73
+ case _:
74
+ raise ValueError("Input file must be a 10x h5 or h5ad file")
75
+
76
+ if var_names in adata.var:
77
+ adata.var = adata.var.set_index(var_names)
78
+
79
+ return adata
80
+
81
+
82
+ def fetch_file(url: str, progress: bool = True) -> BytesIO:
83
+ response = requests.get(url, stream=True)
84
+ response.raise_for_status()
85
+
86
+ total_size_in_bytes = int(response.headers.get("content-length", 0))
87
+ block_size = 1024 # 1 Kibibyte
88
+
89
+ if progress:
90
+ progress_bar = tqdm(total=total_size_in_bytes, unit="iB", unit_scale=True)
91
+
92
+ result = BytesIO()
93
+ for data in response.iter_content(block_size):
94
+ result.write(data)
95
+ if progress:
96
+ progress_bar.update(len(data))
97
+
98
+ if progress:
99
+ progress_bar.close()
100
+
101
+ return result
102
+
103
+
104
+ def is_valid_url(url: str) -> bool:
105
+ if not isinstance(url, str):
106
+ return False
107
+
108
+ result = urlparse(url)
109
+ return all([result.scheme, result.netloc])
@@ -0,0 +1,65 @@
1
+ from typing import Callable, Type
2
+
3
+
4
+ # full class definition is in .dataset/processor/step/_processor_step_base.py
5
+ class ProcessorStepBase:
6
+ name: str
7
+ description: str
8
+
9
+
10
+ methods_registry: dict[str, list[ProcessorStepBase]] = {}
11
+
12
+
13
+ def register_sclab_method(
14
+ category: str,
15
+ name: str | None = None,
16
+ description: str | None = None,
17
+ order: int | None = None,
18
+ ) -> Callable:
19
+ """
20
+ Decorator to register a class as a sclab method.
21
+
22
+ Args:
23
+ category: The category to register the method under (e.g., "Processing")
24
+ name: Optional display name for the method. If None, uses the class name.
25
+ description: Optional description of the method.
26
+ order: Optional ordering within the category. Lower numbers appear first.
27
+
28
+ Returns:
29
+ Decorated class
30
+ """
31
+
32
+ def decorator(cls: Type[ProcessorStepBase]) -> Type[ProcessorStepBase]:
33
+ if name:
34
+ cls.name = name
35
+
36
+ if description:
37
+ cls.description = description
38
+
39
+ if order is not None:
40
+ cls.order = order
41
+
42
+ # Initialize the category in the registry if it doesn't exist
43
+ if category not in methods_registry:
44
+ methods_registry[category] = []
45
+
46
+ methods_list = methods_registry[category]
47
+
48
+ # Add the class to the registry
49
+ methods_list.append(cls)
50
+
51
+ # Sort the methods by order
52
+ methods_registry[category] = sorted(methods_list, key=lambda x: x.order)
53
+
54
+ return cls
55
+
56
+ return decorator
57
+
58
+
59
+ def get_sclab_methods():
60
+ methods = {}
61
+
62
+ for category, methods_list in methods_registry.items():
63
+ methods[category] = sorted(methods_list, key=lambda x: x.order)
64
+
65
+ return methods
@@ -0,0 +1,288 @@
1
+ import inspect
2
+ from io import BytesIO
3
+ from pathlib import Path
4
+
5
+ from anndata import AnnData
6
+ from IPython.display import display
7
+ from ipywidgets.widgets import (
8
+ Button,
9
+ FileUpload,
10
+ GridBox,
11
+ HBox,
12
+ Label,
13
+ Layout,
14
+ Output,
15
+ Tab,
16
+ Text,
17
+ ToggleButtons,
18
+ VBox,
19
+ )
20
+
21
+ from ._io import is_valid_url, load_adata_from_url, read_adata
22
+ from .dataset import SCLabDataset
23
+ from .dataset.plotter import Plotter
24
+ from .dataset.processor import Processor
25
+ from .event import EventBroker
26
+
27
+
28
+ class SCLabDashboard(GridBox):
29
+ broker: EventBroker
30
+ dataset: SCLabDataset
31
+ plotter: Plotter
32
+ processor: Processor
33
+ main_content: Tab
34
+
35
+ def __init__(
36
+ self,
37
+ adata_or_filepath_or_url: AnnData | str | None = None,
38
+ name: str = "SCLab Dashboard",
39
+ counts_layer: str = "counts",
40
+ batch_key: str | None = None,
41
+ copy: bool = False,
42
+ ):
43
+ if adata_or_filepath_or_url is None:
44
+ adata = None
45
+
46
+ elif isinstance(adata_or_filepath_or_url, AnnData):
47
+ adata = adata_or_filepath_or_url
48
+
49
+ elif is_valid_url(adata_or_filepath_or_url):
50
+ url = adata_or_filepath_or_url
51
+ adata = load_adata_from_url(url)
52
+
53
+ elif isinstance(adata_or_filepath_or_url, str):
54
+ filepath = adata_or_filepath_or_url
55
+ adata = read_adata(filepath)
56
+
57
+ self.name = name
58
+ self.counts_layer = counts_layer
59
+ self.batch_key = batch_key
60
+
61
+ self.broker = EventBroker()
62
+
63
+ self.dataset = None
64
+ self.plotter = None
65
+ self.processor = None
66
+ self.main_content = None
67
+
68
+ self.data_loader_layout = Layout(
69
+ width="100%",
70
+ height="500px",
71
+ grid_template_columns="auto",
72
+ grid_template_areas=""" "data_loader" """,
73
+ border="0px solid black",
74
+ )
75
+
76
+ self.dashboard_layout = Layout(
77
+ width="100%",
78
+ height="100%",
79
+ grid_template_columns="350px auto",
80
+ grid_template_areas=""" "processor plotter" """,
81
+ border="0px solid black",
82
+ )
83
+
84
+ self.data_loader = DataLoader(self)
85
+
86
+ GridBox.__init__(self)
87
+ if adata is not None:
88
+ self._load(adata, copy=copy)
89
+ else:
90
+ self.children = (self.data_loader,)
91
+ self.layout = self.data_loader_layout
92
+
93
+ def _load(self, adata: AnnData, copy: bool = False):
94
+ self.dataset = SCLabDataset(
95
+ adata,
96
+ name=self.name,
97
+ counts_layer=self.counts_layer,
98
+ copy=copy,
99
+ broker=self.broker,
100
+ )
101
+ self.plotter = Plotter(self.dataset)
102
+ self.processor = Processor(
103
+ self.dataset,
104
+ self.plotter,
105
+ batch_key=self.batch_key,
106
+ )
107
+
108
+ self.main_content = Tab(
109
+ children=[
110
+ self.plotter,
111
+ self.processor.results_panel,
112
+ self.dataset.obs_table,
113
+ self.dataset.var_table,
114
+ self.broker.logs_tab,
115
+ ],
116
+ titles=[
117
+ "Main graph",
118
+ "Results",
119
+ "Observations",
120
+ "Genes",
121
+ "Logs",
122
+ ],
123
+ )
124
+
125
+ self.children = (
126
+ self.processor.main_accordion,
127
+ self.main_content,
128
+ )
129
+ self.layout = self.dashboard_layout
130
+
131
+ @property
132
+ def ds(self):
133
+ return self.dataset
134
+
135
+ @property
136
+ def pr(self):
137
+ return self.processor
138
+
139
+ @property
140
+ def pl(self):
141
+ return self.plotter
142
+
143
+
144
+ class DataLoader(VBox):
145
+ dashboard: SCLabDashboard
146
+ adata: AnnData
147
+
148
+ upload: FileUpload
149
+ upload_info: Output
150
+ upload_row: HBox
151
+ upload_row_label: Label
152
+
153
+ url: Text
154
+ load_button: Button
155
+ url_row: HBox
156
+ url_row_label: Label
157
+
158
+ defined_adatas_dict: dict[str, AnnData]
159
+ defined_adatas: ToggleButtons
160
+ defined_adatas_row: HBox
161
+ defined_adatas_label: Label
162
+
163
+ progress_output: Output
164
+ continue_button: Button
165
+
166
+ def __init__(self, dashboard: SCLabDashboard):
167
+ self.dashboard = dashboard
168
+
169
+ self.upload_row_label = Label("Load from file:", layout=Layout(width="120px"))
170
+ self.upload = FileUpload(layout=Layout(width="200px"))
171
+ self.upload_info = Output(layout=Layout(width="95%"))
172
+ self.upload_row = HBox(
173
+ [self.upload_row_label, self.upload, self.upload_info],
174
+ layout=Layout(width="100%"),
175
+ )
176
+ self.upload.observe(self.on_upload, "value")
177
+
178
+ self.url_row_label = Label("Load from URL:", layout=Layout(width="120px"))
179
+ self.url = Text(placeholder="https://...", layout=Layout(width="100%"))
180
+ self.load_button = Button(description="Load", layout=Layout(width="200px"))
181
+ self.url_row = HBox(
182
+ [self.url_row_label, self.url, self.load_button],
183
+ layout=Layout(width="100%"),
184
+ )
185
+ self.load_button.on_click(self.on_load_url)
186
+
187
+ user_f_locals = inspect.stack()[2].frame.f_locals
188
+ self.defined_adatas_dict = {}
189
+ for name, variable_type in [(k, type(v)) for k, v in user_f_locals.items()]:
190
+ if variable_type is AnnData:
191
+ self.defined_adatas_dict[name] = user_f_locals[name]
192
+
193
+ self.defined_adatas_label = Label(
194
+ "Defined datasets:", layout=Layout(width="120px")
195
+ )
196
+ self.defined_adatas = ToggleButtons(
197
+ options=list(self.defined_adatas_dict.keys()),
198
+ value=None,
199
+ layout=Layout(width="100%"),
200
+ )
201
+ self.defined_adatas_row = HBox(
202
+ [self.defined_adatas_label, self.defined_adatas],
203
+ layout=Layout(width="100%"),
204
+ )
205
+ self.defined_adatas.observe(self.on_defined_adatas_toggle, "value")
206
+
207
+ self.progress_output = Output(layout=Layout(width="95%"))
208
+ self.continue_button = Button(
209
+ description="Continue", layout=Layout(width="100%"), button_style="success"
210
+ )
211
+ self.continue_button.on_click(self.on_continue)
212
+
213
+ VBox.__init__(
214
+ self,
215
+ [
216
+ self.url_row,
217
+ self.upload_row,
218
+ self.defined_adatas_row,
219
+ self.progress_output,
220
+ ],
221
+ layout=Layout(width="100%"),
222
+ )
223
+
224
+ def on_defined_adatas_toggle(self, *args, **kwargs):
225
+ adata = self.defined_adatas_dict[self.defined_adatas.value]
226
+
227
+ self.progress_output.clear_output()
228
+ with self.progress_output:
229
+ print(f"Loaded {adata.shape[0]} observations and {adata.shape[1]} genes\n")
230
+ print(adata)
231
+ display(self.continue_button)
232
+
233
+ self.adata = adata
234
+
235
+ def on_upload(self, *args, **kwargs):
236
+ from .scanpy.readwrite import read_10x_h5, read_h5ad
237
+
238
+ files = self.upload.value
239
+ if len(files) == 0:
240
+ return
241
+
242
+ file = files[0]
243
+
244
+ self.upload_info.clear_output()
245
+ with self.upload_info:
246
+ for k, v in file.items():
247
+ if k == "content":
248
+ continue
249
+ print(f"{k}: {v}")
250
+
251
+ filename = file["name"]
252
+ contents = BytesIO(file["content"].tobytes())
253
+ var_names = "gene_ids"
254
+
255
+ path = Path(filename)
256
+
257
+ match path.suffix:
258
+ case ".h5":
259
+ adata = read_10x_h5(contents)
260
+ case ".h5ad":
261
+ adata = read_h5ad(contents)
262
+ case _:
263
+ self.upload_info.clear_output()
264
+ with self.upload_info:
265
+ print(f"`{filename}` is not valid")
266
+ print("Please upload a 10x h5 or h5ad file")
267
+ return
268
+
269
+ if var_names in adata.var:
270
+ adata.var = adata.var.set_index(var_names)
271
+
272
+ with self.progress_output:
273
+ print(f"Loaded {adata.shape[0]} observations and {adata.shape[1]} genes\n")
274
+ print(adata)
275
+ display(self.continue_button)
276
+
277
+ self.adata = adata
278
+
279
+ def on_load_url(self, *args, **kwargs):
280
+ self.progress_output.clear_output()
281
+ with self.progress_output:
282
+ self.adata = load_adata_from_url(self.url.value)
283
+ display(self.continue_button)
284
+
285
+ def on_continue(self, *args, **kwargs):
286
+ self.dashboard._load(self.adata)
287
+ self.adata = None
288
+ self.defined_adatas_dict = {}