Download code/read_data.R from kmmuleelab/P2Mdb: direct link, hf CLI and curl.
- Browser
- Download file 3.59 kB
-
https://huggingface.co/datasets/kmmuleelab/P2Mdb/resolve/main/code/read_data.R
- Command line
-
hf download hf://datasets/kmmuleelab/P2Mdb/code/read_data.R
-
curl -L -o read_data.R https://huggingface.co/datasets/kmmuleelab/P2Mdb/resolve/main/code/read_data.R
3.59 kB
| # Rscript read_release.R PATH_TO_RDS [MATRIX_NAME] [PLATFORM] | |
| # Read metadata and select confirmed specimen groups without rerunning analysis. | |
| args <- commandArgs(trailingOnly = TRUE) | |
| if (!length(args)) stop('Provide the path to a released RDS') | |
| if (requireNamespace('SeuratObject', quietly = TRUE)) | |
| suppressPackageStartupMessages(library(SeuratObject)) | |
| x <- readRDS(args[1]) | |
| if (inherits(x, 'Seurat')) { | |
| metadata <- x@meta.data | |
| ids <- colnames(x) | |
| stopifnot(identical(ids, rownames(metadata)), !anyDuplicated(ids)) | |
| cat('Single-cell object:', nrow(x), 'features;', length(ids), 'cells\n') | |
| fields <- intersect(c('sample_id', 'patient_id', 'sample_class', 'specimen_role', | |
| 'sampled_tissue', 'celltype_article', 'celltype_singler', | |
| 'celltype_sctype'), names(metadata)) | |
| print(head(metadata[, fields, drop = FALSE])) | |
| } else if (is.list(x) && all(c('expression', 'samples') %in% names(x))) { | |
| expression <- x[['expression']] | |
| metadata <- x[['samples']] | |
| if (is.list(expression) && !is.data.frame(expression)) { | |
| if (length(args) < 2L || !args[2] %in% names(expression)) { | |
| print(names(expression)) | |
| stop('Multiple matrices: provide one exact MATRIX_NAME as argument 2') | |
| } | |
| expression <- expression[[args[2]]] | |
| } | |
| if (length(args) >= 3L) { | |
| if (!'platform' %in% names(metadata)) stop('Platform metadata unavailable') | |
| metadata <- metadata[!is.na(metadata$platform) & metadata$platform == args[3], , drop = FALSE] | |
| } | |
| ids <- colnames(expression) | |
| if (anyDuplicated(metadata$sample_id)) | |
| stop('Sample IDs recur across platforms; provide the exact PLATFORM as argument 3') | |
| idx <- match(ids, metadata$sample_id) | |
| stopifnot(!anyNA(idx), !anyDuplicated(ids)) | |
| metadata <- metadata[idx, , drop = FALSE] | |
| stopifnot(identical(ids, as.character(metadata$sample_id))) | |
| cat('Bulk matrix:', nrow(expression), 'features;', length(ids), 'expression columns\n') | |
| fields <- intersect(c('sample_id', 'platform', 'patient_id', 'sample_class', | |
| 'sampled_tissue', 'classification_source', 'sample_type_code', | |
| 'clinical_m_stage'), names(metadata)) | |
| print(head(metadata[, fields, drop = FALSE])) | |
| print(x[['information']]) | |
| } else stop('Unsupported RDS structure') | |
| if (!'sample_class' %in% names(metadata)) stop('Specimen class is not recorded') | |
| print(table(metadata$sample_class, useNA = 'ifany')) | |
| if (inherits(x, 'Seurat')) { | |
| if (!'specimen_role' %in% names(metadata)) | |
| stop('Detailed specimen roles are unavailable; inspect samples.csv before selecting tissue groups') | |
| primary_ids <- ids[which(metadata$specimen_role == 'primary_tumor')] | |
| metastasis_ids <- ids[which(metadata$specimen_role == 'metastatic_tumor')] | |
| } else { | |
| primary_ids <- ids[which(metadata$sample_class == 'Primary')] | |
| metastasis_ids <- ids[which(metadata$sample_class == 'Metastasis')] | |
| } | |
| cat('Tissue-group selections from recorded metadata:', length(primary_ids), 'Primary;', | |
| length(metastasis_ids), 'Metastasis\n') | |
| # Fluid specimens (including malignant ascites), normal and unresolved roles | |
| # are excluded from single-cell tissue selections, even if sample_class is Metastasis. | |
| # TCGA clinical M0/M1 is not substituted for specimen class. | |
| # Available author, SingleR and scType annotations remain independent fields. | |
| # Inspect expression scale, patient dependence and analysis/comparisons.csv | |
| # before constructing any new contrast. This example performs no DEG analysis. | |