From 446d631c35f7ca63b2803505b4c6c6b9ef7431db Mon Sep 17 00:00:00 2001 From: ammichalowski Date: Thu, 1 Dec 2022 05:17:32 -0800 Subject: [PATCH] add SampleNames .R and .Rd files --- R/Sample_Names.R | 63 ++++++++++++++++++++++++++++++++++++++++++++++ man/SampleNames.Rd | 20 +++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 R/Sample_Names.R create mode 100644 man/SampleNames.Rd diff --git a/R/Sample_Names.R b/R/Sample_Names.R new file mode 100644 index 0000000..cfa4ef5 --- /dev/null +++ b/R/Sample_Names.R @@ -0,0 +1,63 @@ +# This code comes from NIDAP 'Metadata Table [scRNA-seq][CCBR]' code template https://nidap.nih.gov/workspace/vector/templates/ri.vector.main.template.51ea15ce-2be0-415f-902b-3c86175eb6cd +# Documentation https://nidap.nih.gov/workspace/notepad/view/ri.notepad.main.notepad.53192624-dba0-4e63-9cbb-9bec776ede47 +# Example https://nidap.nih.gov/workspace/vector/view/ri.vector.main.workbook.00e7a393-4cd7-4f20-bdd2-1c1c0cfd1ea2?branch=master + + +#' Extracting cell identities from the meta.data slot of Seurat-class object +#' +#' Returns tabulation of cell identities present in the orig_ident column +#' +#' It is recommended to run this function after doing SingleR annotations and before doing any visualizations/downstream analysis that requires inputting sample names. +#' +#' @param SO Seurat-class object +#' +#' +#' @export +#' @return A table (data.frame) with the number of cells per each sample name (column names) + + +SampleNames <- function(SO) { + # extract metadata table and sample names + + cat("\n Extracting Sample Names from metadata table: ") + if ("meta.data" %in% slotNames(SO)) { + if ("orig.ident" %in% colnames(SO@meta.data)) { + dim.df <- + as.data.frame(t(as.matrix(table( + SO@meta.data$orig.ident + )))) + } else { + dim.df <- + as.data.frame(t(as.matrix(table( + SO@meta.data$orig_ident + )))) + } + colnames(dim.df) <- + lapply(colnames(dim.df), function(x) + gsub(".h5", "", x)) + } else { + if ("orig.ident" %in% colnames(SO$RNA@meta.data)) { + dim.df <- + as.data.frame(t(as.matrix( + table(SO$RNA@meta.data$orig.ident) + ))) + } else { + dim.df <- + as.data.frame(t(as.matrix( + table(SO$RNA@meta.data$orig_ident) + ))) + } + dim.df <- + as.data.frame(t(as.matrix(table( + SO$RNA@meta.data$orig.ident + )))) + colnames(dim.df) <- + lapply(colnames(dim.df), function(x) + gsub(".h5", "", x)) + } + + # output data.frame + cat(sprintf("%g sample names returned", ncol(dim.df))) + return(dim.df) + +} \ No newline at end of file diff --git a/man/SampleNames.Rd b/man/SampleNames.Rd new file mode 100644 index 0000000..e740bb5 --- /dev/null +++ b/man/SampleNames.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/Sample_Names.R +\name{SampleNames} +\alias{SampleNames} +\title{Extracting cell identities from the meta.data slot of Seurat-class object} +\usage{ +SampleNames(SO) +} +\arguments{ +\item{SO}{Seurat-class object} +} +\value{ +A table (data.frame) with the number of cells per each sample name (column names) +} +\description{ +Returns tabulation of cell identities present in the orig_ident column +} +\details{ +It is recommended to run this function after doing SingleR annotations and before doing any visualizations/downstream analysis that requires inputting sample names. +}