File size: 7,465 Bytes
93818e9
05fdb87
 
 
 
93818e9
05fdb87
93818e9
 
 
 
 
05fdb87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: Spatial Transcriptomics Viewer
emoji: 🧬
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 4.0.0
app_file: app.py
pinned: false
license: mit
---

# Spatial Transcriptomics Viewer

A web-based tool for visualizing spatial gene expression from AnnData (.h5ad) files.

## Features

- **Interactive Visualization**: Explore spatial gene expression with interactive Plotly plots
- **Memory Efficient**: Uses AnnData backed mode for handling large datasets
- **Flexible Input**: Load data from URLs (HuggingFace, Zenodo) or upload files
- **Single-Gene Queries**: Visualize expression of individual genes across spatial coordinates
- **Expression Statistics**: Get detailed statistics for each gene
- **Customizable**: Adjust point size, color scale, and transformations

## Quick Start

### Using the Public Demo

1. Visit the Space URL
2. Load your data:
   - **URL**: Paste a link to your h5ad file
   - **Upload**: Upload your h5ad file directly (< 2GB recommended)
3. Enter a gene name and visualize!

### For Heavy Usage: Duplicate This Space

For large files or frequent use, we recommend duplicating this Space to your account:

1. Click the **⋮** menu at the top right
2. Select **"Duplicate this Space"**
3. Choose your HuggingFace account
4. (Optional) Upgrade to persistent storage for better performance

**Benefits of Duplicating:**
- Independent computing resources
- No queueing with other users
- Private data processing
- Customizable settings
- Optional paid upgrades for more resources

## Data Requirements

Your h5ad file must contain:
- `adata.obsm['spatial']`: 2D spatial coordinates (N × 2 array)
- Gene expression data in `adata.X`
- Gene names in `adata.var_names`

**Supported formats:**
- Visium (10x Genomics)
- MERFISH
- seqFISH
- Any spatial transcriptomics data in AnnData format

## How It Works

### Architecture

```
User Input (URL/Upload)

Load h5ad with backed='r' (memory efficient)

Validate spatial coordinates

Query single gene expression

Plotly interactive visualization
```

### Memory Efficiency

This tool uses AnnData's **backed mode** (`backed='r'`), which means:
- Files are read from disk on-demand
- Only requested data is loaded into memory
- Can handle files much larger than available RAM
- Suitable for large-scale spatial transcriptomics datasets

## Technical Details

### Stack
- **Frontend**: Gradio 4.0+
- **Backend**: Python 3.9+
- **Data**: AnnData, scanpy
- **Visualization**: Plotly
- **Platform**: Hugging Face Spaces

### File Size Limits

**Public Space:**
- Recommended: < 2GB
- Maximum: ~10GB (may be slow)

**Duplicated Space (Free):**
- Recommended: < 5GB
- With persistent storage upgrade: 50GB+

### URL Sources

Supported domains for URL input:
- `huggingface.co` - HuggingFace Datasets
- `zenodo.org` - Zenodo repositories
- `s3.amazonaws.com` - S3 buckets

## Usage Examples

### Example 1: Visualize from HuggingFace Dataset

```python
# If you have a h5ad file in a HuggingFace dataset:
URL = "https://huggingface.co/datasets/{username}/{dataset}/resolve/main/data.h5ad"

# Paste this URL in the tool and load
# Then enter gene names like: "GAPDH", "ACTB", "MYC"
```

### Example 2: Prepare Your Own Data

```python
import scanpy as sc
import numpy as np

# Load your data
adata = sc.read_10x_h5("your_data.h5")

# Add spatial coordinates (if not already present)
# Example: load from spatial folder
spatial = sc.read_visium("path/to/spatial_folder")
adata.obsm['spatial'] = spatial.obsm['spatial']

# Save as h5ad
adata.write("your_spatial_data.h5ad")

# Upload to HuggingFace Dataset or use directly
```

## Privacy & Data Security

### Public Space
- Files are processed in **temporary storage**
- No permanent data retention
- Cleared after session ends
- Not suitable for sensitive data

### Duplicated Private Space
- Data stays in your account
- Full control over access
- Suitable for private research data
- Can delete anytime

## Limitations

- **No preprocessing**: Tool does not normalize, scale, or transform data
- **Read-only**: Cannot modify or save h5ad files
- **Single gene**: Visualize one gene at a time
- **2D spatial only**: Requires 2D coordinates in `obsm['spatial']`

## Troubleshooting

### "Spatial coordinates not found"
- Check that your h5ad contains `adata.obsm['spatial']`
- Ensure it's a 2D array (N × 2)

### "Gene not found"
- Check gene name spelling
- Use exact gene names from `adata.var_names`
- Tool will suggest similar gene names

### "File too large" or slow loading
- Try duplicating the Space for more resources
- Consider subsetting your data
- Use URL input instead of upload

### Memory errors
- Ensure backed mode is working (check file size limits)
- Duplicate Space for more RAM
- Consider downsampling your dataset

## Development

### Local Setup

```bash
# Clone the repository
git clone <repo_url>
cd spatial-viewer

# Install dependencies
pip install -r requirements.txt

# Run locally
python app.py
```

### Project Structure

```
spatial-viewer/
├── app.py                  # Main Gradio application
├── utils/
│   ├── __init__.py
│   ├── loader.py           # H5ad loading with backed mode
│   ├── validator.py        # AnnData validation
│   └── plot.py             # Plotly visualization
├── data/
│   └── demo.h5ad           # (Optional) Demo dataset
├── requirements.txt        # Python dependencies
├── README.md               # This file
└── .huggingface/
    └── space_config.yaml   # HF Space configuration
```

## Contributing

Contributions welcome! Areas for improvement:
- Multi-gene visualization
- Additional plot types
- Performance optimizations
- UI enhancements
- Documentation

## Citation

If you use this tool in your research, please cite:

```bibtex
@software{spatial_viewer,
  title = {Spatial Transcriptomics Viewer},
  author = {Your Name},
  year = {2025},
  url = {https://huggingface.co/spaces/...}
}
```

## License

MIT License - see LICENSE file for details

## Acknowledgments

- Built with [Gradio](https://gradio.app/)
- Uses [AnnData](https://anndata.readthedocs.io/) and [Scanpy](https://scanpy.readthedocs.io/)
- Hosted on [Hugging Face Spaces](https://huggingface.co/spaces)

---

## 中文说明

### 功能特点

这是一个基于网页的空间转录组基因表达可视化工具,支持 AnnData (.h5ad) 格式。

**主要特性:**
- 交互式可视化
- 内存高效(支持大文件)
- 灵活的输入方式(URL 或上传)
- 单基因表达查询
- 表达量统计分析

### 使用方法

1. **加载数据**:通过 URL 或上传 h5ad 文件
2. **输入基因名**:输入您想查看的基因
3. **可视化**:查看空间表达图和统计信息

### 大文件或高频使用

对于大型 h5ad 文件(>2GB)或频繁使用,建议 **复制此 Space** 到您的账户:
- 独立计算资源
- 无需排队
- 数据隐私保护
- 可选付费升级

### 数据要求

您的 h5ad 文件必须包含:
- `adata.obsm['spatial']`:空间坐标(N × 2)
- `adata.X`:基因表达数据
- `adata.var_names`:基因名称

支持 Visium、MERFISH、seqFISH 等格式。

### 技术原理

使用 AnnData 的 **backed 模式**`backed='r'`):
- 按需从磁盘读取数据
- 内存占用最小化
- 可处理大于内存的文件
- 适合大规模空间转录组数据

---

**为空间转录组研究社区构建** 🧬