antclient 0.2.0__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.
- antclient-0.2.0/LICENSE +21 -0
- antclient-0.2.0/MANIFEST.in +28 -0
- antclient-0.2.0/PKG-INFO +351 -0
- antclient-0.2.0/README.md +303 -0
- antclient-0.2.0/antclient/__init__.py +23 -0
- antclient-0.2.0/antclient/client.py +430 -0
- antclient-0.2.0/antclient/sql.py +271 -0
- antclient-0.2.0/antclient.egg-info/PKG-INFO +351 -0
- antclient-0.2.0/antclient.egg-info/SOURCES.txt +14 -0
- antclient-0.2.0/antclient.egg-info/dependency_links.txt +1 -0
- antclient-0.2.0/antclient.egg-info/requires.txt +13 -0
- antclient-0.2.0/antclient.egg-info/top_level.txt +1 -0
- antclient-0.2.0/pyproject.toml +73 -0
- antclient-0.2.0/requirements.txt +2 -0
- antclient-0.2.0/setup.cfg +4 -0
- antclient-0.2.0/setup.py +50 -0
antclient-0.2.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Anthive Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Include documentation
|
|
2
|
+
include README.md
|
|
3
|
+
include LICENSE
|
|
4
|
+
include CHANGELOG.md
|
|
5
|
+
|
|
6
|
+
# Include requirements
|
|
7
|
+
include requirements.txt
|
|
8
|
+
|
|
9
|
+
# Exclude unnecessary files
|
|
10
|
+
exclude *.md
|
|
11
|
+
exclude test_client.py
|
|
12
|
+
|
|
13
|
+
# Exclude development/documentation files
|
|
14
|
+
exclude INSTALL.md
|
|
15
|
+
exclude MIGRATION.md
|
|
16
|
+
exclude RENAMED.md
|
|
17
|
+
exclude SUMMARY.md
|
|
18
|
+
exclude QUICKSTART.md
|
|
19
|
+
exclude SQL_FUNCTIONS.md
|
|
20
|
+
exclude WIDGET*.md
|
|
21
|
+
|
|
22
|
+
# Exclude examples (not needed in distribution)
|
|
23
|
+
recursive-exclude examples *
|
|
24
|
+
|
|
25
|
+
# Exclude Python cache and builds
|
|
26
|
+
global-exclude __pycache__
|
|
27
|
+
global-exclude *.py[co]
|
|
28
|
+
global-exclude *.egg-info
|
antclient-0.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: antclient
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Python client for Anthive REST API - query single-cell expression databases
|
|
5
|
+
Home-page: https://github.com/yourusername/anthive
|
|
6
|
+
Author: Anthive Team
|
|
7
|
+
Author-email: Anthive Team <anthive@example.com>
|
|
8
|
+
Maintainer-email: Anthive Team <anthive@example.com>
|
|
9
|
+
License: MIT
|
|
10
|
+
Project-URL: Homepage, https://github.com/yourusername/anthive
|
|
11
|
+
Project-URL: Documentation, https://github.com/yourusername/anthive
|
|
12
|
+
Project-URL: Repository, https://github.com/yourusername/anthive
|
|
13
|
+
Project-URL: Bug Tracker, https://github.com/yourusername/anthive/issues
|
|
14
|
+
Project-URL: Changelog, https://github.com/yourusername/anthive/blob/main/antclient/CHANGELOG.md
|
|
15
|
+
Keywords: single-cell,genomics,bioinformatics,expression,scRNA-seq,database,API,client
|
|
16
|
+
Classifier: Development Status :: 4 - Beta
|
|
17
|
+
Classifier: Intended Audience :: Science/Research
|
|
18
|
+
Classifier: Intended Audience :: Developers
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
20
|
+
Classifier: Topic :: Database
|
|
21
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
22
|
+
Classifier: Programming Language :: Python :: 3
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
27
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
28
|
+
Classifier: Operating System :: OS Independent
|
|
29
|
+
Classifier: Typing :: Typed
|
|
30
|
+
Requires-Python: >=3.8
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
License-File: LICENSE
|
|
33
|
+
Requires-Dist: requests>=2.25.0
|
|
34
|
+
Requires-Dist: pandas>=1.3.0
|
|
35
|
+
Provides-Extra: widgets
|
|
36
|
+
Requires-Dist: ipywidgets>=7.6.0; extra == "widgets"
|
|
37
|
+
Provides-Extra: dev
|
|
38
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
39
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
40
|
+
Requires-Dist: black>=22.0.0; extra == "dev"
|
|
41
|
+
Requires-Dist: flake8>=4.0.0; extra == "dev"
|
|
42
|
+
Requires-Dist: build>=0.10.0; extra == "dev"
|
|
43
|
+
Requires-Dist: twine>=4.0.0; extra == "dev"
|
|
44
|
+
Dynamic: author
|
|
45
|
+
Dynamic: home-page
|
|
46
|
+
Dynamic: license-file
|
|
47
|
+
Dynamic: requires-python
|
|
48
|
+
|
|
49
|
+
# AntClient
|
|
50
|
+
|
|
51
|
+
[](https://badge.fury.io/py/antclient)
|
|
52
|
+
[](https://pypi.org/project/antclient/)
|
|
53
|
+
[](https://opensource.org/licenses/MIT)
|
|
54
|
+
|
|
55
|
+
Python client library for Anthive REST API - Simple, secure, and fast querying of single-cell expression databases.
|
|
56
|
+
|
|
57
|
+
## Features
|
|
58
|
+
|
|
59
|
+
- **Simple API**: Intuitive methods for common operations
|
|
60
|
+
- **Primary Data Retrieval**: `get_cell_data()` for flexible gene and metadata queries
|
|
61
|
+
- **SQL Templates**: Pre-built queries for complex aggregations
|
|
62
|
+
- **Secure**: Bearer token authentication support
|
|
63
|
+
- **Fast**: Built on requests with connection pooling
|
|
64
|
+
- **Type-safe**: Full type hints for IDE support
|
|
65
|
+
- **URL Encoding**: Automatic handling of database names with special characters
|
|
66
|
+
|
|
67
|
+
## Installation
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
# From PyPI
|
|
71
|
+
pip install antclient
|
|
72
|
+
|
|
73
|
+
# From source
|
|
74
|
+
pip install .
|
|
75
|
+
|
|
76
|
+
# Development mode
|
|
77
|
+
pip install -e .
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Quick Start
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from antclient import AntClient
|
|
84
|
+
|
|
85
|
+
# Connect to Anthive server
|
|
86
|
+
client = AntClient("http://localhost:8000", token="your-secret-token")
|
|
87
|
+
|
|
88
|
+
# List available databases
|
|
89
|
+
databases = client.get_databases()
|
|
90
|
+
print(f"Found {len(databases)} databases")
|
|
91
|
+
|
|
92
|
+
# Select first database
|
|
93
|
+
db = databases[0]['id']
|
|
94
|
+
|
|
95
|
+
# Get database info
|
|
96
|
+
info = client.get_database_info(db)
|
|
97
|
+
print(f"Database: {info['n_cells']} cells, {info['n_genes']} genes")
|
|
98
|
+
|
|
99
|
+
# Get cell data with genes and metadata
|
|
100
|
+
df = client.get_cell_data(
|
|
101
|
+
db,
|
|
102
|
+
genes=["CD3D", "CD4", "CD8A"],
|
|
103
|
+
obs=["cell_type", "donor"]
|
|
104
|
+
)
|
|
105
|
+
print(df.head())
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Primary Data Retrieval
|
|
109
|
+
|
|
110
|
+
The `get_cell_data()` method is the primary way to retrieve expression data and metadata:
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
# Get specific genes with metadata
|
|
114
|
+
df = client.get_cell_data(
|
|
115
|
+
db,
|
|
116
|
+
genes=["CD3D", "CD4", "CD8A"],
|
|
117
|
+
obs=["cell_type", "n_counts", "donor"],
|
|
118
|
+
limit=1000
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
# Get only metadata (no genes)
|
|
122
|
+
df = client.get_cell_data(
|
|
123
|
+
db,
|
|
124
|
+
obs=["cell_type", "tissue"]
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
# Get only gene expression (no metadata)
|
|
128
|
+
df = client.get_cell_data(
|
|
129
|
+
db,
|
|
130
|
+
genes=["CD3D", "CD4"],
|
|
131
|
+
obs=[] # Empty list = no metadata
|
|
132
|
+
)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Returns a DataFrame with cells as rows and genes + metadata as columns.
|
|
136
|
+
|
|
137
|
+
## SQL Templates
|
|
138
|
+
|
|
139
|
+
SQL templates provide server-side aggregations for complex queries:
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
import antclient.sql as sql
|
|
143
|
+
|
|
144
|
+
# Top expressed genes (aggregates ALL genes)
|
|
145
|
+
df = sql.top_genes_by_expression(client, db, "X", limit=20)
|
|
146
|
+
|
|
147
|
+
# Gene expression grouped by metadata
|
|
148
|
+
df = sql.gene_expression_by_metadata(
|
|
149
|
+
client, db, "X", "CD4", "cell_type", limit=20
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
# Top genes in specific cell type
|
|
153
|
+
df = sql.genes_in_cell_type(
|
|
154
|
+
client, db, "X", "cell_type", "T cell", limit=10
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
# Count cells by metadata category
|
|
158
|
+
df = sql.cells_by_metadata(client, db, "cell_type", limit=10)
|
|
159
|
+
|
|
160
|
+
# Numerical metadata statistics
|
|
161
|
+
df = sql.metadata_distribution(client, db, "n_counts")
|
|
162
|
+
|
|
163
|
+
# Correlation between numerical fields
|
|
164
|
+
df = sql.correlation_two_numeric(
|
|
165
|
+
client, db, "n_genes", "n_counts", "cell_type"
|
|
166
|
+
)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Gene Search
|
|
170
|
+
|
|
171
|
+
```python
|
|
172
|
+
# Search for genes (case-insensitive by default)
|
|
173
|
+
genes = client.search_genes(db, query="CD", limit=10)
|
|
174
|
+
|
|
175
|
+
# Case-sensitive search
|
|
176
|
+
genes = client.search_genes(db, query="CD", case_sensitive=True, limit=10)
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## Custom SQL Queries
|
|
180
|
+
|
|
181
|
+
Execute custom SQL queries:
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
# Custom SQL
|
|
185
|
+
df = client.execute_sql(
|
|
186
|
+
db,
|
|
187
|
+
query="""
|
|
188
|
+
SELECT
|
|
189
|
+
o.cell_id,
|
|
190
|
+
c.count as expression,
|
|
191
|
+
o.cell_type
|
|
192
|
+
FROM layer_X c
|
|
193
|
+
JOIN obs o ON c.cell_id = o.cell_id
|
|
194
|
+
WHERE c.gene_id = 'CD4'
|
|
195
|
+
ORDER BY c.count DESC
|
|
196
|
+
""",
|
|
197
|
+
limit=50
|
|
198
|
+
)
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## Metadata Exploration
|
|
202
|
+
|
|
203
|
+
```python
|
|
204
|
+
# Get metadata fields
|
|
205
|
+
fields = client.get_metadata_fields(db)
|
|
206
|
+
print("Numerical fields:", fields['numerical'])
|
|
207
|
+
print("Categorical fields:", fields['categorical'])
|
|
208
|
+
|
|
209
|
+
# Search categorical metadata
|
|
210
|
+
cell_types = client.search_categorical_metadata(
|
|
211
|
+
db=db,
|
|
212
|
+
field='cell_type',
|
|
213
|
+
query='T',
|
|
214
|
+
limit=20
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
# Get numerical metadata statistics
|
|
218
|
+
stats = client.get_numerical_metadata_stats(
|
|
219
|
+
db=db,
|
|
220
|
+
field='n_counts'
|
|
221
|
+
)
|
|
222
|
+
print(f"Count range: {stats['min']} - {stats['max']}")
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## Authentication
|
|
226
|
+
|
|
227
|
+
```python
|
|
228
|
+
# With authentication
|
|
229
|
+
client = AntClient(
|
|
230
|
+
"https://anthive.example.com",
|
|
231
|
+
token="your-secret-token"
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
# Without authentication (if server doesn't require it)
|
|
235
|
+
client = AntClient("http://localhost:8000")
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
## Remote Server Connection
|
|
239
|
+
|
|
240
|
+
```python
|
|
241
|
+
# Connect to remote server
|
|
242
|
+
client = AntClient(
|
|
243
|
+
"https://anthive.example.com:8000",
|
|
244
|
+
token="your-token",
|
|
245
|
+
verify_ssl=True # Set to False to disable SSL verification (not recommended)
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
# All operations work the same
|
|
249
|
+
databases = client.get_databases()
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## Database Names with Special Characters
|
|
253
|
+
|
|
254
|
+
Database names containing forward slashes or other special characters are automatically URL-encoded:
|
|
255
|
+
|
|
256
|
+
```python
|
|
257
|
+
# This works automatically (v0.2.0+)
|
|
258
|
+
db = "Project/Experiment/Dataset"
|
|
259
|
+
info = client.get_database_info(db) # Forward slash encoded as %2F
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
## Error Handling
|
|
263
|
+
|
|
264
|
+
```python
|
|
265
|
+
from requests.exceptions import HTTPError
|
|
266
|
+
|
|
267
|
+
try:
|
|
268
|
+
df = client.get_cell_data(db, genes=["CD4"])
|
|
269
|
+
except HTTPError as e:
|
|
270
|
+
if "404" in str(e):
|
|
271
|
+
print("Database or gene not found")
|
|
272
|
+
elif "500" in str(e):
|
|
273
|
+
print("Server error")
|
|
274
|
+
else:
|
|
275
|
+
print(f"API error: {e}")
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
## API Reference
|
|
279
|
+
|
|
280
|
+
### Core Methods
|
|
281
|
+
|
|
282
|
+
- `get_databases()` - List all databases
|
|
283
|
+
- `get_database_info(db)` - Get database metadata
|
|
284
|
+
- `get_layers(db)` - Get available data layers
|
|
285
|
+
- `search_genes(db, query, ...)` - Search for genes
|
|
286
|
+
- `get_metadata_fields(db)` - Get metadata field names
|
|
287
|
+
- `get_cell_data(db, genes, obs, ...)` - **Primary data retrieval method**
|
|
288
|
+
- `search_categorical_metadata(db, field, query, ...)` - Search metadata values
|
|
289
|
+
- `get_numerical_metadata_stats(db, field, ...)` - Get metadata statistics
|
|
290
|
+
- `execute_sql(db, query, limit)` - Execute custom SQL
|
|
291
|
+
- `execute_template(db, template_id, parameters, limit)` - Execute template
|
|
292
|
+
- `get_gene(db, gene_id)` - Get gene information
|
|
293
|
+
- `list_templates()` - List available templates
|
|
294
|
+
|
|
295
|
+
### SQL Template Functions
|
|
296
|
+
|
|
297
|
+
Available in `antclient.sql` module:
|
|
298
|
+
|
|
299
|
+
- `top_genes_by_expression(client, db, layer, limit)` - Most expressed genes
|
|
300
|
+
- `gene_expression_by_metadata(client, db, layer, gene, field, limit)` - Group by metadata
|
|
301
|
+
- `genes_in_cell_type(client, db, layer, field, value, limit)` - Top genes in type
|
|
302
|
+
- `cells_by_metadata(client, db, field, limit)` - Count by category
|
|
303
|
+
- `metadata_distribution(client, db, field, limit)` - Numerical statistics
|
|
304
|
+
- `correlation_two_numeric(client, db, field1, field2, cat_field, limit)` - Correlation
|
|
305
|
+
|
|
306
|
+
## Development
|
|
307
|
+
|
|
308
|
+
```bash
|
|
309
|
+
# Install with development dependencies
|
|
310
|
+
pip install -e ".[dev]"
|
|
311
|
+
|
|
312
|
+
# Run tests
|
|
313
|
+
pytest
|
|
314
|
+
|
|
315
|
+
# Format code
|
|
316
|
+
black antclient/
|
|
317
|
+
|
|
318
|
+
# Lint
|
|
319
|
+
flake8 antclient/
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
## Requirements
|
|
323
|
+
|
|
324
|
+
- Python >= 3.8
|
|
325
|
+
- requests >= 2.25.0
|
|
326
|
+
- pandas >= 1.3.0
|
|
327
|
+
|
|
328
|
+
## Changelog
|
|
329
|
+
|
|
330
|
+
See [CHANGELOG.md](https://github.com/yourusername/anthive/blob/main/antclient/CHANGELOG.md) for version history.
|
|
331
|
+
|
|
332
|
+
## License
|
|
333
|
+
|
|
334
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
|
335
|
+
|
|
336
|
+
## Contributing
|
|
337
|
+
|
|
338
|
+
Contributions welcome! Please submit issues and pull requests on GitHub.
|
|
339
|
+
|
|
340
|
+
## Links
|
|
341
|
+
|
|
342
|
+
- **Documentation**: [Full documentation](https://github.com/yourusername/anthive)
|
|
343
|
+
- **GitHub**: [Source code](https://github.com/yourusername/anthive)
|
|
344
|
+
- **PyPI**: [Package](https://pypi.org/project/antclient/)
|
|
345
|
+
- **Issues**: [Bug reports](https://github.com/yourusername/anthive/issues)
|
|
346
|
+
|
|
347
|
+
## Examples
|
|
348
|
+
|
|
349
|
+
Check out the [demo notebooks](https://github.com/yourusername/anthive/tree/main/) for complete examples:
|
|
350
|
+
- `demo_antclient.ipynb` - Complete API walkthrough
|
|
351
|
+
- `demo_gene_distribution.ipynb` - Gene expression distribution visualization
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
# AntClient
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/py/antclient)
|
|
4
|
+
[](https://pypi.org/project/antclient/)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
Python client library for Anthive REST API - Simple, secure, and fast querying of single-cell expression databases.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **Simple API**: Intuitive methods for common operations
|
|
12
|
+
- **Primary Data Retrieval**: `get_cell_data()` for flexible gene and metadata queries
|
|
13
|
+
- **SQL Templates**: Pre-built queries for complex aggregations
|
|
14
|
+
- **Secure**: Bearer token authentication support
|
|
15
|
+
- **Fast**: Built on requests with connection pooling
|
|
16
|
+
- **Type-safe**: Full type hints for IDE support
|
|
17
|
+
- **URL Encoding**: Automatic handling of database names with special characters
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# From PyPI
|
|
23
|
+
pip install antclient
|
|
24
|
+
|
|
25
|
+
# From source
|
|
26
|
+
pip install .
|
|
27
|
+
|
|
28
|
+
# Development mode
|
|
29
|
+
pip install -e .
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Quick Start
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
from antclient import AntClient
|
|
36
|
+
|
|
37
|
+
# Connect to Anthive server
|
|
38
|
+
client = AntClient("http://localhost:8000", token="your-secret-token")
|
|
39
|
+
|
|
40
|
+
# List available databases
|
|
41
|
+
databases = client.get_databases()
|
|
42
|
+
print(f"Found {len(databases)} databases")
|
|
43
|
+
|
|
44
|
+
# Select first database
|
|
45
|
+
db = databases[0]['id']
|
|
46
|
+
|
|
47
|
+
# Get database info
|
|
48
|
+
info = client.get_database_info(db)
|
|
49
|
+
print(f"Database: {info['n_cells']} cells, {info['n_genes']} genes")
|
|
50
|
+
|
|
51
|
+
# Get cell data with genes and metadata
|
|
52
|
+
df = client.get_cell_data(
|
|
53
|
+
db,
|
|
54
|
+
genes=["CD3D", "CD4", "CD8A"],
|
|
55
|
+
obs=["cell_type", "donor"]
|
|
56
|
+
)
|
|
57
|
+
print(df.head())
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Primary Data Retrieval
|
|
61
|
+
|
|
62
|
+
The `get_cell_data()` method is the primary way to retrieve expression data and metadata:
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
# Get specific genes with metadata
|
|
66
|
+
df = client.get_cell_data(
|
|
67
|
+
db,
|
|
68
|
+
genes=["CD3D", "CD4", "CD8A"],
|
|
69
|
+
obs=["cell_type", "n_counts", "donor"],
|
|
70
|
+
limit=1000
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
# Get only metadata (no genes)
|
|
74
|
+
df = client.get_cell_data(
|
|
75
|
+
db,
|
|
76
|
+
obs=["cell_type", "tissue"]
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
# Get only gene expression (no metadata)
|
|
80
|
+
df = client.get_cell_data(
|
|
81
|
+
db,
|
|
82
|
+
genes=["CD3D", "CD4"],
|
|
83
|
+
obs=[] # Empty list = no metadata
|
|
84
|
+
)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Returns a DataFrame with cells as rows and genes + metadata as columns.
|
|
88
|
+
|
|
89
|
+
## SQL Templates
|
|
90
|
+
|
|
91
|
+
SQL templates provide server-side aggregations for complex queries:
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
import antclient.sql as sql
|
|
95
|
+
|
|
96
|
+
# Top expressed genes (aggregates ALL genes)
|
|
97
|
+
df = sql.top_genes_by_expression(client, db, "X", limit=20)
|
|
98
|
+
|
|
99
|
+
# Gene expression grouped by metadata
|
|
100
|
+
df = sql.gene_expression_by_metadata(
|
|
101
|
+
client, db, "X", "CD4", "cell_type", limit=20
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
# Top genes in specific cell type
|
|
105
|
+
df = sql.genes_in_cell_type(
|
|
106
|
+
client, db, "X", "cell_type", "T cell", limit=10
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
# Count cells by metadata category
|
|
110
|
+
df = sql.cells_by_metadata(client, db, "cell_type", limit=10)
|
|
111
|
+
|
|
112
|
+
# Numerical metadata statistics
|
|
113
|
+
df = sql.metadata_distribution(client, db, "n_counts")
|
|
114
|
+
|
|
115
|
+
# Correlation between numerical fields
|
|
116
|
+
df = sql.correlation_two_numeric(
|
|
117
|
+
client, db, "n_genes", "n_counts", "cell_type"
|
|
118
|
+
)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Gene Search
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
# Search for genes (case-insensitive by default)
|
|
125
|
+
genes = client.search_genes(db, query="CD", limit=10)
|
|
126
|
+
|
|
127
|
+
# Case-sensitive search
|
|
128
|
+
genes = client.search_genes(db, query="CD", case_sensitive=True, limit=10)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Custom SQL Queries
|
|
132
|
+
|
|
133
|
+
Execute custom SQL queries:
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
# Custom SQL
|
|
137
|
+
df = client.execute_sql(
|
|
138
|
+
db,
|
|
139
|
+
query="""
|
|
140
|
+
SELECT
|
|
141
|
+
o.cell_id,
|
|
142
|
+
c.count as expression,
|
|
143
|
+
o.cell_type
|
|
144
|
+
FROM layer_X c
|
|
145
|
+
JOIN obs o ON c.cell_id = o.cell_id
|
|
146
|
+
WHERE c.gene_id = 'CD4'
|
|
147
|
+
ORDER BY c.count DESC
|
|
148
|
+
""",
|
|
149
|
+
limit=50
|
|
150
|
+
)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Metadata Exploration
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
# Get metadata fields
|
|
157
|
+
fields = client.get_metadata_fields(db)
|
|
158
|
+
print("Numerical fields:", fields['numerical'])
|
|
159
|
+
print("Categorical fields:", fields['categorical'])
|
|
160
|
+
|
|
161
|
+
# Search categorical metadata
|
|
162
|
+
cell_types = client.search_categorical_metadata(
|
|
163
|
+
db=db,
|
|
164
|
+
field='cell_type',
|
|
165
|
+
query='T',
|
|
166
|
+
limit=20
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
# Get numerical metadata statistics
|
|
170
|
+
stats = client.get_numerical_metadata_stats(
|
|
171
|
+
db=db,
|
|
172
|
+
field='n_counts'
|
|
173
|
+
)
|
|
174
|
+
print(f"Count range: {stats['min']} - {stats['max']}")
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Authentication
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
# With authentication
|
|
181
|
+
client = AntClient(
|
|
182
|
+
"https://anthive.example.com",
|
|
183
|
+
token="your-secret-token"
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
# Without authentication (if server doesn't require it)
|
|
187
|
+
client = AntClient("http://localhost:8000")
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## Remote Server Connection
|
|
191
|
+
|
|
192
|
+
```python
|
|
193
|
+
# Connect to remote server
|
|
194
|
+
client = AntClient(
|
|
195
|
+
"https://anthive.example.com:8000",
|
|
196
|
+
token="your-token",
|
|
197
|
+
verify_ssl=True # Set to False to disable SSL verification (not recommended)
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
# All operations work the same
|
|
201
|
+
databases = client.get_databases()
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## Database Names with Special Characters
|
|
205
|
+
|
|
206
|
+
Database names containing forward slashes or other special characters are automatically URL-encoded:
|
|
207
|
+
|
|
208
|
+
```python
|
|
209
|
+
# This works automatically (v0.2.0+)
|
|
210
|
+
db = "Project/Experiment/Dataset"
|
|
211
|
+
info = client.get_database_info(db) # Forward slash encoded as %2F
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Error Handling
|
|
215
|
+
|
|
216
|
+
```python
|
|
217
|
+
from requests.exceptions import HTTPError
|
|
218
|
+
|
|
219
|
+
try:
|
|
220
|
+
df = client.get_cell_data(db, genes=["CD4"])
|
|
221
|
+
except HTTPError as e:
|
|
222
|
+
if "404" in str(e):
|
|
223
|
+
print("Database or gene not found")
|
|
224
|
+
elif "500" in str(e):
|
|
225
|
+
print("Server error")
|
|
226
|
+
else:
|
|
227
|
+
print(f"API error: {e}")
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## API Reference
|
|
231
|
+
|
|
232
|
+
### Core Methods
|
|
233
|
+
|
|
234
|
+
- `get_databases()` - List all databases
|
|
235
|
+
- `get_database_info(db)` - Get database metadata
|
|
236
|
+
- `get_layers(db)` - Get available data layers
|
|
237
|
+
- `search_genes(db, query, ...)` - Search for genes
|
|
238
|
+
- `get_metadata_fields(db)` - Get metadata field names
|
|
239
|
+
- `get_cell_data(db, genes, obs, ...)` - **Primary data retrieval method**
|
|
240
|
+
- `search_categorical_metadata(db, field, query, ...)` - Search metadata values
|
|
241
|
+
- `get_numerical_metadata_stats(db, field, ...)` - Get metadata statistics
|
|
242
|
+
- `execute_sql(db, query, limit)` - Execute custom SQL
|
|
243
|
+
- `execute_template(db, template_id, parameters, limit)` - Execute template
|
|
244
|
+
- `get_gene(db, gene_id)` - Get gene information
|
|
245
|
+
- `list_templates()` - List available templates
|
|
246
|
+
|
|
247
|
+
### SQL Template Functions
|
|
248
|
+
|
|
249
|
+
Available in `antclient.sql` module:
|
|
250
|
+
|
|
251
|
+
- `top_genes_by_expression(client, db, layer, limit)` - Most expressed genes
|
|
252
|
+
- `gene_expression_by_metadata(client, db, layer, gene, field, limit)` - Group by metadata
|
|
253
|
+
- `genes_in_cell_type(client, db, layer, field, value, limit)` - Top genes in type
|
|
254
|
+
- `cells_by_metadata(client, db, field, limit)` - Count by category
|
|
255
|
+
- `metadata_distribution(client, db, field, limit)` - Numerical statistics
|
|
256
|
+
- `correlation_two_numeric(client, db, field1, field2, cat_field, limit)` - Correlation
|
|
257
|
+
|
|
258
|
+
## Development
|
|
259
|
+
|
|
260
|
+
```bash
|
|
261
|
+
# Install with development dependencies
|
|
262
|
+
pip install -e ".[dev]"
|
|
263
|
+
|
|
264
|
+
# Run tests
|
|
265
|
+
pytest
|
|
266
|
+
|
|
267
|
+
# Format code
|
|
268
|
+
black antclient/
|
|
269
|
+
|
|
270
|
+
# Lint
|
|
271
|
+
flake8 antclient/
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
## Requirements
|
|
275
|
+
|
|
276
|
+
- Python >= 3.8
|
|
277
|
+
- requests >= 2.25.0
|
|
278
|
+
- pandas >= 1.3.0
|
|
279
|
+
|
|
280
|
+
## Changelog
|
|
281
|
+
|
|
282
|
+
See [CHANGELOG.md](https://github.com/yourusername/anthive/blob/main/antclient/CHANGELOG.md) for version history.
|
|
283
|
+
|
|
284
|
+
## License
|
|
285
|
+
|
|
286
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
|
287
|
+
|
|
288
|
+
## Contributing
|
|
289
|
+
|
|
290
|
+
Contributions welcome! Please submit issues and pull requests on GitHub.
|
|
291
|
+
|
|
292
|
+
## Links
|
|
293
|
+
|
|
294
|
+
- **Documentation**: [Full documentation](https://github.com/yourusername/anthive)
|
|
295
|
+
- **GitHub**: [Source code](https://github.com/yourusername/anthive)
|
|
296
|
+
- **PyPI**: [Package](https://pypi.org/project/antclient/)
|
|
297
|
+
- **Issues**: [Bug reports](https://github.com/yourusername/anthive/issues)
|
|
298
|
+
|
|
299
|
+
## Examples
|
|
300
|
+
|
|
301
|
+
Check out the [demo notebooks](https://github.com/yourusername/anthive/tree/main/) for complete examples:
|
|
302
|
+
- `demo_antclient.ipynb` - Complete API walkthrough
|
|
303
|
+
- `demo_gene_distribution.ipynb` - Gene expression distribution visualization
|