Utilities¶
This module contains certain utility functions, that can be useful, but didn't fit into any other module.
CELLxGENE download¶
hide_deconv.download.fetch_cellxgene_catalog()
¶
Fetch the list of all public datasets from CELLxGENE Discover.
The catalog is requested from the CELLxGENE Discover API on every call. Only datasets providing an H5AD file are returned.
Returns:
| Type | Description |
|---|---|
list[dict]
|
List of datasets. Each dataset is a dictionary with the keys "dataset_id", "title", "collection_name", "organism", "tissue", "disease", "assay", "suspension_type" (lists of labels), "cell_count", "published_at", "url" and "filesize". Additionally the citation information "collection_id", "collection_doi", "collection_doi_label", "citation" and "explorer_url" is stored. |
Raises:
| Type | Description |
|---|---|
ConnectionError
|
If the CELLxGENE Discover API cannot be reached. |
Timeout
|
If the CELLxGENE Discover API does not respond in time. |
HTTPError
|
If the CELLxGENE Discover API returns an error status. |
Source code in src/hide_deconv/download/download_file.py
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 | |
hide_deconv.download.filter_datasets(datasets, organism=None, tissues=None, diseases=None)
¶
Filter datasets by organism, tissues and diseases.
A dataset matches, if it contains the organism and at least one of the given tissues and at least one of the given diseases. Note that tissues and diseases are annotated per dataset, a matching dataset can therefore also contain cells of other tissues and diseases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
datasets
|
list[dict]
|
Datasets as returned by fetch_cellxgene_catalog. |
required |
organism
|
str | None = None
|
Organism label, e.g. "Homo sapiens". None matches all organisms. |
None
|
tissues
|
list[str] | None = None
|
Tissue labels. None or an empty list matches all tissues. |
None
|
diseases
|
list[str] | None = None
|
Disease labels. None or an empty list matches all diseases. |
None
|
Returns:
| Type | Description |
|---|---|
list[dict]
|
Datasets matching all given criteria. |
Source code in src/hide_deconv/download/download_file.py
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 | |
hide_deconv.download.prepare_cellxgene_adata(path, tissues=None, diseases=None, gene_id='symbol', protein_coding_only=True)
¶
Prepare a downloaded CELLxGENE AnnData file for HIDE-Deconv.
Optionally subsets the cells to the given tissues and diseases. If raw counts are stored in adata.raw, these are moved to adata.X and adata.raw is removed. Spike-ins are always removed and optionally only protein coding genes are kept.
CELLxGENE stores Ensembl IDs as var_names and gene symbols in adata.var["feature_name"]. If gene symbols are selected, these are used as var_names and the Ensembl IDs are stored in adata.var["feature_id"]. Genes with the same gene symbol are summed up. If Ensembl IDs are selected, the index of adata.var is named "feature_id". The file is overwritten with the prepared AnnData object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path of the CELLxGENE AnnData file. |
required |
tissues
|
list[str] | None = None
|
Keep only cells with these labels in obs["tissue"]. None keeps all tissues. |
None
|
diseases
|
list[str] | None = None
|
Keep only cells with these labels in obs["disease"]. None keeps all diseases. |
None
|
gene_id
|
str = "symbol"
|
Gene identifier used as var_names. Either "symbol" or "ensembl". |
'symbol'
|
protein_coding_only
|
bool = True
|
Keep only genes with var["feature_type"] == "protein_coding". |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
Path of the prepared AnnData file. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a required obs column is missing, no cells match the selection or gene_id is invalid. |
Source code in src/hide_deconv/download/download_file.py
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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | |
hide_deconv.download.write_cellxgene_citation(dataset, ad_path)
¶
Write the citation information of a CELLxGENE dataset to a text file.
The file is stored next to the AnnData file as "
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
dict
|
Dataset entry as returned by fetch_cellxgene_catalog. |
required |
ad_path
|
str | Path
|
Path of the downloaded AnnData file. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path of the citation file. |
Source code in src/hide_deconv/download/download_file.py
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | |
AnnData utilities¶
hide_deconv.utils.subset_adata_obs(adata, obs_col, values)
¶
Subset a AnnData dataframe, such that a specified observation column only contains certain values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adata
|
AnnData
|
AnnData dataframe, that should be subsetted. |
required |
obs_col
|
str
|
Name of observation that should be subsetted. |
required |
values
|
str
|
List of values, that should be kept in observation. |
required |
Returns:
| Type | Description |
|---|---|
AnnData
|
Subsetted AnnData dataframe. |
Source code in src/hide_deconv/utils/anndata_utils.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
hide_deconv.utils.create_annotation_template(adata, obs_col, example_col_name='new_cell_layer_example')
¶
Create a template for adding higher annotation layers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adata
|
AnnData
|
AnnData dataframe. |
required |
obs_col
|
str
|
Observation column with the sub cell types layer. |
required |
example_col_name
|
str
|
Name of the example column that is initialized with the same values. |
"new_cell_layer_example"
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Template dataframe with the selected annotation layer and an example column. |
Source code in src/hide_deconv/utils/anndata_utils.py
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 | |
hide_deconv.utils.add_annotation_columns_from_template(adata, annotation_df, sub_ct_col)
¶
Add higher annotation layers from an edited template dataframe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adata
|
AnnData
|
AnnData dataframe that should receive new observation columns. |
required |
annotation_df
|
DataFrame
|
Edited annotation template. |
required |
sub_ct_col
|
str
|
Observation column that matches the original cell type layer. |
required |
Returns:
| Type | Description |
|---|---|
tuple[AnnData, list[str]]
|
Updated AnnData object and a list with the newly added observation columns. |
Source code in src/hide_deconv/utils/anndata_utils.py
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 | |
hide_deconv.utils.switch_adata_var_names(adata, var_col, old_var_col)
¶
Switch the gene annotation used as adata.var_names, e.g. from Ensembl IDs to gene symbols.
The current var_names are stored in adata.var[old_var_col], the column var_col is used as new var_names. Genes that share the same new identifier are combined by summing their expression (as done for duplicate gene symbols in bulk data). For combined genes, adata.var[old_var_col] contains all previous identifiers separated by ";" and all other columns of adata.var contain the value of the first gene.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adata
|
AnnData
|
AnnData dataframe, whose var_names should be switched. |
required |
var_col
|
str
|
Column of adata.var, that should be used as new var_names. |
required |
old_var_col
|
str
|
Name of the column in adata.var, where the current var_names are stored. |
required |
Returns:
| Type | Description |
|---|---|
AnnData
|
AnnData dataframe with switched var_names. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If var_col is not a column of adata.var or old_var_col is already used by another column. |
Source code in src/hide_deconv/utils/anndata_utils.py
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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | |
hide_deconv.utils.get_var_names_from_column(adata, var_col)
¶
Get the gene identifiers that result from using a column of adata.var as var_names.
Missing or empty values are replaced by the current var_names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adata
|
AnnData
|
AnnData dataframe. |
required |
var_col
|
str
|
Column of adata.var, that should be used as var_names. |
required |
Returns:
| Type | Description |
|---|---|
Index
|
New gene identifiers in the order of adata.var_names. Can contain duplicates. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If var_col is not a column of adata.var. |
Source code in src/hide_deconv/utils/anndata_utils.py
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 | |
MTX conversion¶
hide_deconv.utils.mtx_to_adata(mtx_path, barcodes_path, genes_path)
¶
Read a mtx file and convert it to a anndata
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mtx_path
|
str
|
Path to mtx file |
required |
barcodes_path
|
str
|
Path to mtx barcodes |
required |
genes_path
|
str
|
Path to mtx genes |
required |
Returns:
| Type | Description |
|---|---|
AnnData
|
AnnData object containing the converted mtx file |
Source code in src/hide_deconv/utils/mtx_utils.py
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 | |
hide_deconv.utils.mtx_to_csv(mtx_path, barcodes_path, genes_path)
¶
Read a mtx file and convert it to a pandas DataFrame
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mtx_path
|
str
|
Path to mtx file |
required |
barcodes_path
|
str
|
Path to mtx barcodes |
required |
genes_path
|
str
|
Path to mtx genes |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Dataframe containing the converted mtx file |
Source code in src/hide_deconv/utils/mtx_utils.py
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 | |
Sample sheets and cohorts¶
hide_deconv.utils.filter_sample_sheet(sample_sheet, sample_id_col)
¶
Filter a sample sheet, such that only non-NaN sample id's are kept.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample_sheet
|
DataFrame
|
Sample sheet. |
required |
sample_id_col
|
str
|
Name of column that holds the sample ids. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Series, DataFrame]
|
Valid Sample Ids, Sample Sheet only with non-NaN Sample Id Samples |
Source code in src/hide_deconv/utils/sample_sheet_utils.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
hide_deconv.utils.combine_categorical_cohorts(sample_sheet, cohort_col, new_col_name, n_groups)
¶
Combines various categorical cohorts into new ones.
Source code in src/hide_deconv/utils/cohort_utils.py
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 | |
hide_deconv.utils.combine_numerical_cohorts(sample_sheet, cohort_col, new_col_name, method, threshold=None)
¶
Combines numerical values in a high / low group split by either mean, median or greater equal
Source code in src/hide_deconv/utils/cohort_utils.py
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 | |