Idle rotation seurat spatial rotate image flip data structure object seurat’s idle data storage

1seurat subset operation

3. Object operations
① Use the @ and $ symbols on the structure diagram to get the
② Two bracket operations, pbmc[[ ]].
In the tutorial, pbmc[['percent.MT']] adds the percent.MT column to meta.data.
pbmc[[]], the square brackets refer to the secondary data name in the above structure diagram

What is the difference between the above two methods?
If you take the subordinate content of the primary structure Assays:

no difference

 > class(pbmc[['RNA']])
 [1] "Assay"
 attr(,"package")
 [1] "Seurat"
 > class(pbmc@assays$RNA)
 [1] "Assay"
 attr(,"package")
 [1] "Seurat"
If it is the subordinate content in the first-level structure meta.data:

The data type returned is different

> class(pbmc[['nCount_RNA']])
[1] "data.frame"
> class([email protected]$nCount_RNA)
[1] "numeric"
Take out pbmc[['nCount_RNA']], it is nCount_RNA of all cells, including cell information, data frame
image.png

And [email protected]$nCount_RNA takes out a single nCount_RNA column, which is a vector
image.png
If you take the subordinate content of reductions in the first-level structure:

no difference


> class(pbmc[['pca']])
 [1] "DimReduc"
 attr(,"package")
 [1] "Seurat"
> class(pbmc@reductions$pca)
 [1] "DimReduc"
 attr(,"package")
 [1] "Seurat"

Author: Sunny
Link: https://www.jianshu.com/p/0c4bc6a932b2
Source: Jianshu
Copyright belongs to the author. For commercial reprinting, please contact the author for authorization. For non-commercial reprinting, please indicate the source. 

1.4 The first step in the Seurat process is to create a Seurat object. First, you must understand the composition of the Seurat object. Seurat objects are further subdivided into: Assay Object and DimReduc Object.

The Assay Object object stores multi-omics expression data, and the DimReduc Object object stores the results of dimensionality reduction analysis on the Assay Object object.

Seurat object data structure – short book

5.2 assay
A Seurat object can include multiple assay objects, but at a certain time, only one assay object is activated by default. You can query which assay object is currently activated by default through the function active.assay. You can also use DefaultAssay to set the default assay.

5.3 ident Seurat object data structure – short book 1. For the three steps of filtering in the standard process, SCTransform can also be used instead of Seurat basic tutorial [Seurat basic tutorial…icon-default .png?t=N7T8https://www.jianshu.com/p/238976158dcc
It can be understood as the type of cell. In the Seurat object, cells may have several types annotated by different methods, but at a certain moment, only one cell type is activated by default. You can use active.ident to query what the current default cell type is.

5.4 reduction
Like assay, reduction returns a list. Contained inside is one or more DimReduc object objects. Each DimReduc object object corresponds to the result obtained after performing some dimensionality reduction analysis on the assay object. There are three types of dimensionality reduction: PCA, tsen, and umap. In the list in the example below, there are two DimReduc objects, namely PCA and umap.

1.5 key: Each active object has a key value, which can be obtained using the fetch function

2 anndata data structure

anndata

The core of the single-cell transcriptome is a two-dimensional table of cell Therefore, the information related to the object cell of this table.

I remember when we were learning solid geometry in junior high school, the teacher asked us to have spatial imagination and raise our thinking to a new dimension. In the process of single cell data analysis, we must also stimulate our imagination. For example, in the analysis of RNA rate, the content stored in andata is as follows:

adata

AnnData object with n_obs × n_vars = 7292 × 1999
    obs: 'initial_size_unspliced', 'initial_size_spliced', 'initial_size', 'n_counts', 'velocity_self_transition', 'leiden', 'velocity_clusters'
    var: 'Accession', 'Chromosome', 'End', 'Start', 'Strand', 'means', 'dispersions', 'dispersions_norm', 'velocity_gamma', 'velocity_r2', 'velocity_genes', 'velocity_score', 'fit_alpha', 'fit_beta', 'fit_gamma', 'fit_t_', ' fit_scaling', 'fit_std_u', 'fit_std_s', 'fit_likelihood', 'fit_u0', 'fit_s0', 'fit_pval_steady', 'fit_steady_u', 'fit_steady_s\ ', 'fit_alignment_scaling', 'fit_r2'
    uns: 'pca', 'neighbors', 'connectivities_key', 'distances_key', 'velocity_settings', 'velocity_graph', 'velocity_graph_neg', 'leiden', 'umap', 'leiden_colors', 'rank_velocity_genes', 'recover_dynamics'
    obsm: 'X_pca', 'X_umap', 'velocity_umap'
    varm: 'PCs', 'loss'
    layers: 'matrix', 'ambiguous', 'spliced', 'unspliced', 'Ms', 'Mu', 'velocity', 'variance_velocity', 'fit_t', 'fit_tau', 'fit_tau_', 'velocity_u'
    obsp: 'distances', 'connectivities'

We not only need to know what is stored in each part, but also the relationship between the parts.

Link: https://www.jianshu.com/p/13142bf51e81

3

3 Idle data flip Seurat’s idle data storage

#Self-built function flip ----------
#https://github.com/satijalab/seurat/issues/2702
{
  
  # flip_angle %in% c(180, "R90", "L90", "Hf", "Vf")
  
  rotimat=function(foo,rotation){
    if(!is.matrix(foo)){
      cat("Input is not a matrix")
      return(foo)
    }
    if(!(rotation %in% c("180","Hf","Vf", "R90", "L90"))){
      cat("Rotation should be either L90, R90, 180, Hf or Vf\\
")
      return(foo)
    }
    if(rotation == "180"){
      foo <- foo %>%
        .[, dim(.)[2]:1] %>%
        .[dim(.)[1]:1, ]
    }
    if(rotation == "Hf"){
      foo <- foo %>%
        .[, dim(.)[2]:1]
    }
    
    if(rotation == "Vf"){
      foo <- foo %>%
        .[dim(.)[1]:1, ]
    }
    if(rotation == "L90"){
      foo = t(foo)
      foo <- foo %>%
        .[dim(.)[1]:1, ]
    }
    if(rotation == "R90"){
      foo = t(foo)
      foo <- foo %>%
        .[, dim(.)[2]:1]
    }
    return(foo)
  }
  
  rotateSeuratImage = function(seuratVisumObject, slide = "slice1", rotation="Vf"){
    if(!(rotation %in% c("180","Hf","Vf", "L90", "R90"))){
      cat("Rotation should be either 180, L90, R90, Hf or Vf\\
")
      return(NULL)
    }else{
      seurat.visium = seuratVisumObject
      ori.array = (seurat.visium@images)[[slide]]@image
      img.dim = dim(ori.array)[1:2]/(seurat.visium@images)[[slide]]@scale.factors$lowres
      new.mx <- c()
      # transform the image array
      for (rgb_idx in 1:3){
        each.mx <- ori.array[,,rgb_idx]
        each.mx.trans <- rotimat(each.mx, rotation)
        new.mx <- c(new.mx, list(each.mx.trans))
      }
      
      # construct new rgb image array
      new.X.dim <- dim(each.mx.trans)[1]
      new.Y.dim <- dim(each.mx.trans)[2]
      new.array <- array(c(new.mx[[1]],
                           new.mx[[2]],
                           new.mx[[3]]),
                         dim = c(new.X.dim, new.Y.dim, 3))
      
      #swap old image with new image
      seurat.visium@images[[slide]]@image <- new.array
      
      ## step4: change the tissue pixel-spot index
      img.index <- (seurat.visium@images)[[slide]]@coordinates
      
      #swap index
      if(rotation == "Hf"){
        seurat.visium@images[[slide]]@coordinates$imagecol <- img.dim[2]-img.index$imagecol
      }
      
      if(rotation == "Vf"){
        seurat.visium@images[[slide]]@coordinates$imagerow <- img.dim[1]-img.index$imagerow
      }
      
      if(rotation == "180"){
        seurat.visium@images[[slide]]@coordinates$imagerow <- img.dim[1]-img.index$imagerow
        seurat.visium@images[[slide]]@coordinates$imagecol <- img.dim[2]-img.index$imagecol
      }
      
      if(rotation == "L90"){
        seurat.visium@images[[slide]]@coordinates$imagerow <- img.dim[2]-img.index$imagecol
        seurat.visium@images[[slide]]@coordinates$imagecol <- img.index$imagerow
      }
      
      if(rotation == "R90"){
        seurat.visium@images[[slide]]@coordinates$imagerow <- img.index$imagecol
        seurat.visium@images[[slide]]@coordinates$imagecol <- img.dim[1]-img.index$imagerow
      }
      
      return(seurat.visium)
    }
  }
  
  
}
{
  d.all=rotateSeuratImage(d.all,rotation = "L90", slide = "SiO2_56")
  SpatialDimPlot(d.all, images = "SiO2_56",pt.size.factor = 1,label = TRUE)
  
  d.all=rotateSeuratImage(d.all,rotation = "L90", slide = "NS_7")
  SpatialDimPlot(d.all, images = "NS_7",pt.size.factor = 1,label = TRUE)
  
  
  d.all=rotateSeuratImage(d.all,rotation = "L90", slide = "SiO2_7")
  SpatialDimPlot(d.all, images = "SiO2_7",pt.size.factor = 1,label = TRUE,repel = TRUE)
  
  
  SpatialDimPlot(d.all, repel = TRUE,
                 ncol = 2,images = c("NS_7" ,"SiO2_7","NS_56","SiO2_56"),
                 pt.size.factor = 1,label = TRUE,
                 label.size = 2)
  
  SpatialDimPlot(d.all, # repel = TRUE,
                 ncol = 2,images = c("NS_7" ,"SiO2_7","NS_56","SiO2_56"),
                 pt.size.factor = 1,label = TRUE,
                 label.size = 2)
  
}