Calculate gene-level (variable) quality control metrics. Computes QC statistics for each gene including detection rates across cells, mean expression levels, and dropout percentages. ## Parameters `adata` - AnnData object containing the dataset `x` - Expression matrix to analyze `expr_type` - Label for expression type (e.g., "counts", "UMI") `_var_type` - Label for variable type (unused, kept fo
(
adata: &IMAnnData,
x: &anndata_memory::IMArrayElement,
expr_type: &str,
_var_type: &str,
log1p: bool,
)
| 238 | /// - Assess gene expression distributions |
| 239 | /// - Quality control before feature selection |
| 240 | fn describe_var( |
| 241 | adata: &IMAnnData, |
| 242 | x: &anndata_memory::IMArrayElement, |
| 243 | expr_type: &str, |
| 244 | _var_type: &str, |
| 245 | log1p: bool, |
| 246 | ) -> anyhow::Result<DataFrame> { |
| 247 | let n_obs = adata.n_obs(); |
| 248 | |
| 249 | let n_cells_by_counts: Vec<u32> = x.nonzero_whole(&Direction::COLUMN)?; |
| 250 | let total_counts: Vec<f64> = x.sum_whole(&Direction::COLUMN)?; |
| 251 | |
| 252 | let mean_counts: Vec<f64> = total_counts |
| 253 | .iter() |
| 254 | .map(|&total| total / n_obs as f64) |
| 255 | .collect(); |
| 256 | |
| 257 | let mut columns = vec![]; |
| 258 | |
| 259 | let col_name = format!("n_cells_by_{}", expr_type); |
| 260 | columns.push(Column::new(col_name.into(), n_cells_by_counts.clone())); |
| 261 | |
| 262 | let col_name = format!("mean_{}", expr_type); |
| 263 | columns.push(Column::new(col_name.into(), mean_counts.clone())); |
| 264 | |
| 265 | if log1p { |
| 266 | let log_values: Vec<f64> = mean_counts.iter().map(|&x| (x + 1.0).ln()).collect(); |
| 267 | let col_name = format!("log1p_mean_{}", expr_type); |
| 268 | columns.push(Column::new(col_name.into(), log_values)); |
| 269 | } |
| 270 | |
| 271 | let pct_dropout: Vec<f64> = n_cells_by_counts |
| 272 | .iter() |
| 273 | .map(|&n| (1.0 - n as f64 / n_obs as f64) * 100.0) |
| 274 | .collect(); |
| 275 | let col_name = format!("pct_dropout_by_{}", expr_type); |
| 276 | columns.push(Column::new(col_name.into(), pct_dropout)); |
| 277 | |
| 278 | let col_name = format!("total_{}", expr_type); |
| 279 | columns.push(Column::new(col_name.into(), total_counts.clone())); |
| 280 | |
| 281 | if log1p { |
| 282 | let log_values: Vec<f64> = total_counts.iter().map(|&x| (x + 1.0).ln()).collect(); |
| 283 | let col_name = format!("log1p_total_{}", expr_type); |
| 284 | columns.push(Column::new(col_name.into(), log_values)); |
| 285 | } |
| 286 | |
| 287 | DataFrame::new(columns).map_err(Into::into) |
| 288 | } |
| 289 | |
| 290 | /// Calculate comprehensive quality control metrics for single-cell data. |
| 291 | /// |
no test coverage detected