### Loading CARD Library (R) Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md Loads the CARD package library into the R session, making its functions available for use in cell type deconvolution. ```R library(CARD) ``` -------------------------------- ### Loading Single-Cell RNA-seq Meta Data (R) Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md Loads example single-cell RNA-seq meta data from an RData file. The data is expected to be a data frame with cells as rows and columns for cell ID, cell type, and sample info. Rownames should match scRNA-seq count column names. Displays the first few rows. ```R #### load the example scRNA-seq meta data, load("./sc_meta.RData") sc_meta[1:4,] ``` -------------------------------- ### Loading Single-Cell RNA-seq Count Data (R) Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md Loads example single-cell RNA-seq count data from an RData file. The data is expected to be a matrix or sparse matrix with genes as rows and cells as columns. Displays the top-left corner of the loaded matrix. ```R #### load the example scRNA-seq count data, load("./sc_count.RData") sc_count[1:4,1:4] ``` -------------------------------- ### Loading Spatial Transcriptomics Location Data (R) Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md Loads example spatial location data from an RData file. The data is expected to be a data frame with spatial locations as rows and 'x' and 'y' coordinates as columns. Rownames should match spatial count column names. Displays the first few rows. ```R #### load the example spatial location data, load("./spatial_location.RData") spatial_location[1:4,] ``` -------------------------------- ### Installing devtools R package Source: https://github.com/yma-lab/card/blob/master/docs/documentation/02_installation.md Installs the 'devtools' package from CRAN, which is required to install packages directly from GitHub. This is a prerequisite for installing the CARD package. ```R install.packages('devtools') ``` -------------------------------- ### Loading CARD R package Source: https://github.com/yma-lab/card/blob/master/docs/documentation/02_installation.md Loads the installed CARD package into the current R session, making its functions and data available for use. This is typically done after successful installation. ```R library(CARD) ``` -------------------------------- ### Loading Spatial Transcriptomics Count Data (R) Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md Loads example spatial transcriptomics count data from an RData file. The data is expected to be a matrix or sparse matrix with genes as rows and spatial locations as columns. Displays the top-left corner of the loaded matrix. ```R #### load the example spatial transcriptomics count data, load("./spatial_count.RData") spatial_count[1:4,1:4] ``` -------------------------------- ### Extracting Spatial Data and Counts from SingleCellExperiment (R) Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md This snippet demonstrates how to load the SingleCellExperiment library and extract spatial location information from the colData slot and expression counts from the assays slot of a scMapping object. ```R library(SingleCellExperiment) MapCellCords = as.data.frame(colData(scMapping)) count_SC = assays(scMapping)$counts ``` -------------------------------- ### Install and Load CARD R Package Source: https://github.com/yma-lab/card/blob/master/README.md This snippet provides the R code to install the CARD package directly from GitHub using the `devtools` package. It first checks if `devtools` is installed and installs it if necessary, then uses `install_github` to get the CARD package, and finally loads it into the R session using `library()`. Requires the `devtools` package. ```R # install devtools if necessary install.packages('devtools') # install the CARD package devtools::install_github('YingMa0107/CARD') # load package library(CARD) ``` -------------------------------- ### GNU GPL Interactive Startup Notice Template Source: https://github.com/yma-lab/card/blob/master/LICENSE.md This template provides a short notice to display when a program starts in interactive mode. It informs the user about the program's copyright, explicitly states the lack of warranty, and mentions the freedom to redistribute under certain conditions, suggesting commands (like 'show w' and 'show c') to view license details. ```text Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type 'show w'. This is free software, and you are welcome to redistribute it under certain conditions; type 'show c' for details. ``` -------------------------------- ### Performing Deconvolution with CARDfree (R) Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md Executes the deconvolution process using the `CARD_refFree` function on the previously created `CARDfree` object to obtain cell type proportions without requiring a single-cell reference. ```R ## deconvolution using CARDfree CARDfree_obj = CARD_refFree(CARDfree_obj) ``` -------------------------------- ### Visualizing Cell Type Proportion Pie Chart - CARD - R Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md Sets custom colors and uses the `CARD.visualize.pie` function to generate a pie chart visualization of cell type proportions at each spatial location. Requires the CARD object containing estimated proportions and spatial coordinates. Prints the generated plot. ```R ## set the colors. Here, I just use the colors in the manuscript, if the color is not provided, the function will use default color in the package. colors = c("#FFD92F","#4DAF4A","#FCCDE5","#D9D9D9","#377EB8","#7FC97F","#BEAED4", "#FDC086","#FFFF99","#386CB0","#F0027F","#BF5B17","#666666","#1B9E77","#D95F02", "#7570B3","#E7298A","#66A61E","#E6AB02","#A6761D") p1 <- CARD.visualize.pie( proportion = CARD_obj@Proportion_CARD, spatial_location = CARD_obj@spatial_location, colors = colors, radius = 0.52) ### You can choose radius = NULL or your own radius number print(p1) ``` -------------------------------- ### Setting R build tools check option Source: https://github.com/yma-lab/card/blob/master/docs/documentation/02_installation.md Provides a potential solution for the "Could not find tools necessary to compile a package" error during installation, particularly on MacOS. It disables the build tools check within R. ```R options(buildtools.check = function(action) TRUE ) ``` -------------------------------- ### Initialize CARD Object in R Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md Creates a CARD object required for deconvolution. It takes scRNA-seq and spatial transcriptomics count data, metadata, spatial locations, and parameters for cell type and sample identification, as well as filtering thresholds. ```R CARD_obj = createCARDObject( sc_count = sc_count, sc_meta = sc_meta, spatial_count = spatial_count, spatial_location = spatial_location, ct.varname = "cellType", ct.select = unique(sc_meta$cellType), sample.varname = "sampleInfo", minCountGene = 100, minCountSpot = 5) ``` -------------------------------- ### Creating CARDfree Object (R) Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md Loads a marker gene list from an RData file and creates a `CARDfree` object using the `createCARDfreeObject` function, providing spatial count data, spatial location, and minimum count thresholds. ```R ## load the marker gene list load("./markerList.RData") CARDfree_obj = createCARDfreeObject( markerList = markerList, spatial_count = spatial_count, spatial_location = spatial_location, minCountGene = 100, minCountSpot =5) ``` -------------------------------- ### Visualizing CARDfree Cell Type Proportions as Scatterpie (R) Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md This R code block prepares and generates a scatterpie plot to visualize cell type proportions from CARDfree. It defines a color palette, reorders the proportion matrix columns to match a reference, renames them, and then uses the `CARD.visualize.pie` function with spatial locations and colors. This visualization is suitable for datasets with a small number of spots. ```R colors = c("#FFD92F","#4DAF4A","#FCCDE5","#D9D9D9","#377EB8","#7FC97F","#BEAED4","#FDC086","#FFFF99","#386CB0","#F0027F","#BF5B17","#666666","#1B9E77","#D95F02","#7570B3","#E7298A","#66A61E","#E6AB02","#A6761D") ### In order to maximumply match with the original results of CARD, we order the colors to generally match with the results infered by CARD CARDfree_obj@Proportion_CARD = CARDfree_obj@Proportion_CARD[,c(8,10,14,2,1,6,12,18,7,13,20,19,16,17,11,15,4,9,3,5)] colnames(CARDfree_obj@Proportion_CARD) = paste0("CT",1:20) p9 <- CARD.visualize.pie(CARDfree_obj@Proportion_CARD,CARDfree_obj@spatial_location,colors = colors) print(p9) ``` -------------------------------- ### Visualizing Enhanced Resolution Cell Type Proportion (R) Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md Uses the `CARD.visualize.prop` function to plot cell type proportions at the newly grided spatial locations obtained after imputation, visualizing the results at an enhanced resolution. ```R p6 <- CARD.visualize.prop( proportion = CARD_obj@refined_prop, spatial_location = location_imputation, ct.visualize = ct.visualize, colors = c("lightblue","lightyellow","red"), NumCols = 4) print(p6) ``` -------------------------------- ### Visualizing Selected Cell Type Proportion Spatial Distribution - CARD - R Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md Defines a vector of specific cell types to visualize. Uses the `CARD.visualize.prop` function to create a spatial plot showing the proportion of these selected cell types across locations. Allows customization of colors, number of columns in the panel, and point size. Prints the resulting plot. ```R ## select the cell type that we are interested ct.visualize = c("Acinar_cells","Cancer_clone_A","Cancer_clone_B","Ductal_terminal_ductal_like","Ductal_CRISP3_high-centroacinar_like","Ductal_MHC_Class_II","Ductal_APOL1_high-hypoxic","Fibroblasts") ## visualize the spatial distribution of the cell type proportion p2 <- CARD.visualize.prop( proportion = CARD_obj@Proportion_CARD, spatial_location = CARD_obj@spatial_location, ct.visualize = ct.visualize, ### selected cell types to visualize colors = c("lightblue","lightyellow","red"), ### if not provide, we will use the default colors NumCols = 4, ### number of columns in the figure panel pointSize = 3.0) ### point size in ggplot2 scatterplot print(p2) ``` -------------------------------- ### Visualizing Enhanced Resolution Marker Gene Expression (R) Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md Uses the `CARD.visualize.gene` function to visualize predicted spatial gene expression for specified marker genes at the enhanced resolution spatial locations. ```R p7 <- CARD.visualize.gene( spatial_expression = CARD_obj@refined_expression, spatial_location = location_imputation, gene.visualize = c("Tm4sf1","S100a4","Tff3","Apol1","Crisp3","CD248"), colors = NULL, NumCols = 6) print(p7) ``` -------------------------------- ### Visualizing Cell Type Proportion Correlation - CARD - R Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md Uses the `CARD.visualize.Cor` function to generate a visualization showing the correlation matrix of cell type proportions estimated by CARD. Takes the proportion data from the CARD object as input. Allows using default or custom colors. Prints the correlation plot. ```R p4 <- CARD.visualize.Cor(CARD_obj@Proportion_CARD,colors = NULL) # if not provide, we will use the default colors print(p4) ``` -------------------------------- ### View Deconvolution Results in R Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md Accesses and prints the estimated cell type proportions stored in the @Proportion_CARD slot of the CARD object. This snippet shows how to view the proportions for the first two spatial locations. ```R print(CARD_obj@Proportion_CARD[1:2,]) ``` -------------------------------- ### Visualizing Original Resolution Marker Gene Expression (R) Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md Uses the `CARD.visualize.gene` function to visualize spatial gene expression for the same marker genes but using the original spatial count matrix and locations for comparison with the enhanced resolution. ```R p8 <- CARD.visualize.gene( spatial_expression = CARD_obj@spatial_countMat, spatial_location = CARD_obj@spatial_location, gene.visualize = c("Tm4sf1","S100a4","Tff3","Apol1","Crisp3","CD248"), colors = NULL, NumCols = 6) print(p8) ``` -------------------------------- ### Visualizing Imputed Spatial Locations (R) Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md Creates a data frame for imputed spatial locations from CARD object rownames, then uses ggplot2 to visualize these locations as points to check if the detected shape is correct. ```R location_imputation = cbind.data.frame(x=as.numeric(sapply(strsplit(rownames(CARD_obj@refined_prop),split="x"),"[",1)), y=as.numeric(sapply(strsplit(rownames(CARD_obj@refined_prop),split="x"),"[",2))) rownames(location_imputation) = rownames(CARD_obj@refined_prop) library(ggplot2) p5 <- ggplot(location_imputation, aes(x = x, y = y)) + geom_point(shape=22,color = "#7dc7f5")+ theme(plot.margin = margin(0.1, 0.1, 0.1, 0.1, "cm"), legend.position="bottom", panel.background = element_blank(), plot.background = element_blank(), panel.border = element_rect(colour = "grey89", fill=NA, size=0.5)) print(p5) ``` -------------------------------- ### Displaying CARDfree Cell Type Proportions (R) Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md This R snippet prints the first two rows of the cell type proportion matrix stored in the `@Proportion_CARD` slot of a CARDfree object. It allows inspection of the estimated proportions for each inferred cell type across initial spatial spots. ```R print(CARDfree_obj@Proportion_CARD[1:2,]) ``` -------------------------------- ### Perform Spatial Deconvolution with CARD in R Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md Runs the CARD deconvolution algorithm on the initialized CARD object. This function performs reference matrix creation, informative gene selection, and the core deconvolution process to estimate cell type proportions in spatial locations. ```R CARD_obj = CARD_deconvolution(CARD_object = CARD_obj) ``` -------------------------------- ### Visualizing Two Cell Type Proportion Spatial Distribution - CARD - R Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md Uses the `CARD.visualize.prop.2CT` function to plot the spatial distribution of proportions for two specified cell types on a single plot. Requires the CARD object with proportion and spatial location data. Allows providing separate color scales for each cell type. Prints the combined plot. ```R ## visualize the spatial distribution of two cell types on the same plot p3 = CARD.visualize.prop.2CT( proportion = CARD_obj@Proportion_CARD, ### Cell type proportion estimated by CARD spatial_location = CARD_obj@spatial_location, ### spatial location information ct2.visualize = c("Cancer_clone_A","Cancer_clone_B"), ### two cell types you want to visualize colors = list(c("lightblue","lightyellow","red"),c("lightblue","lightyellow","black"))) ### two color scales print(p3) ``` -------------------------------- ### Visualizing Cell Type Spatial Distribution with ggplot2 (R) Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md This code snippet uses the ggplot2 library to create a scatter plot visualizing the spatial distribution of single cells. It maps cell coordinates (x, y) to the plot axes and colors points based on cell type (CT), applying custom colors and theme settings for clarity. ```R library(ggplot2) df = MapCellCords colors = c("#8DD3C7","#CFECBB","#F4F4B9","#CFCCCF","#D1A7B9","#E9D3DE","#F4867C","#C0979F", "#D5CFD6","#86B1CD","#CEB28B","#EDBC63","#C59CC5","#C09CBF","#C2D567","#C9DAC3","#E1EBA0", "#FFED6F","#CDD796","#F8CDDE") p10 = ggplot(df, aes(x = x, y = y, colour = CT)) + geom_point(size = 3.0) + scale_colour_manual(values = colors) + #facet_wrap(~Method,ncol = 2,nrow = 3) + theme(plot.margin = margin(0.1, 0.1, 0.1, 0.1, "cm"), panel.background = element_rect(colour = "white", fill="white"), plot.background = element_rect(colour = "white", fill="white"), legend.position="bottom", panel.border = element_rect(colour = "grey89", fill=NA, size=0.5), axis.text =element_blank(), axis.ticks =element_blank(), axis.title =element_blank(), legend.title=element_text(size = 13,face="bold"), legend.text=element_text(size = 12), legend.key = element_rect(colour = "transparent", fill = "white"), legend.key.size = unit(0.45, 'cm'), strip.text = element_text(size = 15,face="bold"))+ guides(color=guide_legend(title="Cell Type")) print(p10) ``` -------------------------------- ### Performing Spatial Imputation - CARD - R Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md Applies the `CARD.imputation` function to the CARD object to refine the spatial map. This process imputes cell type compositions and gene expression on newly gridded spatial locations by modeling spatial correlation. Requires specifying the desired number of grids and neighbors for imputation. The results are stored in the `CARD_obj@refined_prop` and `CARD_obj@refined_expression` slots. ```R CARD_obj = CARD.imputation(CARD_obj,NumGrids = 2000,ineibor = 10,exclude = NULL) ## The rownames of locations are matched ... ## Make grids on new spatial locations ... ``` -------------------------------- ### Performing CARD Single-Cell Resolution Mapping (R) Source: https://github.com/yma-lab/card/blob/master/docs/documentation/04_CARD_Example.md This R snippet demonstrates the use of the `CARD_SCMapping` function to infer single-cell resolution gene expression from non-single-cell spatial transcriptomics data using a deconvoluted CARD object. It requires specifying the assumed shape of the single-cell capture area (`shapeSpot`) and parameters for parallel processing (`numCell`, `ncore`). The function returns a `SingleCellExperiment` object containing the inferred single-cell data. ```R #### Note that here the shapeSpot is the user defined variable which indicates the capturing area of single cells. Details see above. scMapping = CARD_SCMapping(CARD_obj,shapeSpot="Square",numCell=20,ncore=10) print(scMapping) ### the output class: SingleCellExperiment dim: 16381 8560 metadata(0): assays(1): counts rownames(16381): A1BG A1CF ... ZZEF1 ZZZ3 rowData names(1): rownames(count_CT) colnames(8560): Cell686:10x10:9.97518192091957x9.83765210071579 Cell734:10x10:10.1896061601583x9.94081321195699 ... Cell443:9x33:9.4747460691724x32.5644472888671 Cell1488:9x33:9.43348842463456x33.4998327996582 colData names(7): x y ... CT Cell reducedDimNames(0): mainExpName: NULL altExpNames(0): ``` -------------------------------- ### GNU GPL Source File License Notice Template Source: https://github.com/yma-lab/card/blob/master/LICENSE.md This template provides the standard text recommended by the Free Software Foundation to include at the beginning of each source file when licensing a program under the GNU GPL. It includes copyright information, a statement of licensing under GPLv3 or later, a disclaimer of warranty, and a pointer to the full license text. ```text Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.