segger.data.utils¶
SpatialTranscriptomicsDataset ¶
SpatialTranscriptomicsDataset(root, transform=None, pre_transform=None, pre_filter=None)
Bases: InMemoryDataset
A dataset class for handling SpatialTranscriptomics spatial transcriptomics data.
Attributes:
Name | Type | Description |
---|---|---|
root |
str
|
The root directory where the dataset is stored. |
transform |
callable
|
A function/transform that takes in a Data object and returns a transformed version. |
pre_transform |
callable
|
A function/transform that takes in a Data object and returns a transformed version. |
pre_filter |
callable
|
A function that takes in a Data object and returns a boolean indicating whether to keep it. |
Initialize the SpatialTranscriptomicsDataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root
|
str
|
Root directory where the dataset is stored. |
required |
transform
|
callable
|
A function/transform that takes in a Data object and returns a transformed version. Defaults to None. |
None
|
pre_transform
|
callable
|
A function/transform that takes in a Data object and returns a transformed version. Defaults to None. |
None
|
pre_filter
|
callable
|
A function that takes in a Data object and returns a boolean indicating whether to keep it. Defaults to None. |
None
|
Source code in src/segger/data/utils.py
399 400 401 402 403 404 405 406 407 408 409 410 |
|
processed_file_names
property
¶
processed_file_names
Return a list of processed file names in the processed directory.
Returns:
Type | Description |
---|---|
List[str]
|
List[str]: List of processed file names. |
raw_file_names
property
¶
raw_file_names
Return a list of raw file names in the raw directory.
Returns:
Type | Description |
---|---|
List[str]
|
List[str]: List of raw file names. |
download ¶
download()
Download the raw data. This method should be overridden if you need to download the data.
Source code in src/segger/data/utils.py
430 431 432 |
|
get ¶
get(idx)
Get a processed data object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
idx
|
int
|
Index of the data object to retrieve. |
required |
Returns:
Name | Type | Description |
---|---|---|
Data |
Data
|
The processed data object. |
Source code in src/segger/data/utils.py
446 447 448 449 450 451 452 453 454 455 456 457 |
|
len ¶
len()
Return the number of processed files.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
Number of processed files. |
Source code in src/segger/data/utils.py
438 439 440 441 442 443 444 |
|
process ¶
process()
Process the raw data and save it to the processed directory. This method should be overridden if you need to process the data.
Source code in src/segger/data/utils.py
434 435 436 |
|
calculate_gene_celltype_abundance_embedding ¶
calculate_gene_celltype_abundance_embedding(adata, celltype_column)
Calculate the cell type abundance embedding for each gene based on the fraction of cells in each cell type that express the gene (non-zero expression).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
adata
|
AnnData
|
An AnnData object containing gene expression data and cell type information. |
required |
celltype_column
|
str
|
The column name in |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: A DataFrame where rows are genes and columns are cell types, with each value representing the fraction of cells in that cell type expressing the gene. |
Example
adata = AnnData(...) # Load your scRNA-seq AnnData object celltype_column = 'celltype_major' abundance_df = calculate_gene_celltype_abundance_embedding(adata, celltype_column) abundance_df.head()
Source code in src/segger/data/utils.py
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 |
|
compute_transcript_metrics ¶
compute_transcript_metrics(df, qv_threshold=30, cell_id_col='cell_id')
Computes various metrics for a given dataframe of transcript data filtered by quality value threshold.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
The dataframe containing transcript data. |
required |
qv_threshold
|
float
|
The quality value threshold for filtering transcripts. |
30
|
cell_id_col
|
str
|
The name of the column representing the cell ID. |
'cell_id'
|
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: A dictionary containing various transcript metrics: - 'percent_assigned' (float): The percentage of assigned transcripts. - 'percent_cytoplasmic' (float): The percentage of cytoplasmic transcripts among assigned transcripts. - 'percent_nucleus' (float): The percentage of nucleus transcripts among assigned transcripts. - 'percent_non_assigned_cytoplasmic' (float): The percentage of non-assigned cytoplasmic transcripts. - 'gene_metrics' (pd.DataFrame): A dataframe containing gene-level metrics. |
Source code in src/segger/data/utils.py
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 |
|
create_anndata ¶
create_anndata(df, panel_df=None, min_transcripts=5, cell_id_col='cell_id', qv_threshold=30, min_cell_area=10.0, max_cell_area=1000.0)
Generates an AnnData object from a dataframe of segmented transcriptomics data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
The dataframe containing segmented transcriptomics data. |
required |
panel_df
|
Optional[DataFrame]
|
The dataframe containing panel information. |
None
|
min_transcripts
|
int
|
The minimum number of transcripts required for a cell to be included. |
5
|
cell_id_col
|
str
|
The column name representing the cell ID in the input dataframe. |
'cell_id'
|
qv_threshold
|
float
|
The quality value threshold for filtering transcripts. |
30
|
min_cell_area
|
float
|
The minimum cell area to include a cell. |
10.0
|
max_cell_area
|
float
|
The maximum cell area to include a cell. |
1000.0
|
Returns:
Type | Description |
---|---|
AnnData
|
ad.AnnData: The generated AnnData object containing the transcriptomics data and metadata. |
Source code in src/segger/data/utils.py
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 |
|
filter_transcripts ¶
filter_transcripts(transcripts_df, min_qv=20.0)
Filters transcripts based on quality value and removes unwanted transcripts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
transcripts_df
|
DataFrame
|
The dataframe containing transcript data. |
required |
min_qv
|
float
|
The minimum quality value threshold for filtering transcripts. |
20.0
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: The filtered dataframe. |
Source code in src/segger/data/utils.py
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 |
|
format_time ¶
format_time(elapsed)
Format elapsed time to hs.
Parameters:¶
elapsed : float Elapsed time in seconds.
Returns:¶
str Formatted time in hs.
Source code in src/segger/data/utils.py
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 |
|
get_edge_index ¶
get_edge_index(coords_1, coords_2, k=5, dist=10, method='kd_tree', workers=1)
Computes edge indices using KD-Tree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coords_1
|
ndarray
|
First set of coordinates. |
required |
coords_2
|
ndarray
|
Second set of coordinates. |
required |
k
|
int
|
Number of nearest neighbors. |
5
|
dist
|
int
|
Distance threshold. |
10
|
method
|
str
|
The method to use. Only 'kd_tree' is supported now. |
'kd_tree'
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Edge indices. |
Source code in src/segger/data/utils.py
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 |
|
get_edge_index_kdtree ¶
get_edge_index_kdtree(coords_1, coords_2, k=5, dist=10, workers=1)
Computes edge indices using KDTree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coords_1
|
ndarray
|
First set of coordinates. |
required |
coords_2
|
ndarray
|
Second set of coordinates. |
required |
k
|
int
|
Number of nearest neighbors. |
5
|
dist
|
int
|
Distance threshold. |
10
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Edge indices. |
Source code in src/segger/data/utils.py
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 |
|
get_xy_extents ¶
get_xy_extents(filepath, x, y)
Get the bounding box of the x and y coordinates from a Parquet file.
Parameters¶
filepath : str The path to the Parquet file. x : str The name of the column representing the x-coordinate. y : str The name of the column representing the y-coordinate.
Returns¶
shapely.Polygon A polygon representing the bounding box of the x and y coordinates.
Source code in src/segger/data/utils.py
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
|