theMILOlab / SPATA2

A Toolbox for Spatial Gene Expression Analysis
77 stars 15 forks source link

Lost some info after converting to spata2 object #87

Open phoebee-h opened 10 months ago

phoebee-h commented 10 months ago

Hi, Thanks for maintaining SPATA2! I have a seurat object after QC and SCT normalized, which was used for spata trajectory analysis. I am confused by the structure of spata2 object. Q1. What information should be included in spata2 object for trajectory analysis? What should be checked first? Q2. If I use the "addSpatialTrajectory", how do I check the limitation (or borders) of the parameter "start, end"? Q3. Why the genes cannot be retrieved by "getGenes()", which causes the problem in generating figures? Q4. I lost something after converting the object from seurat, how do I make the information as in demonstration "image_annotations[["269_T"]]"? Would it be necessary for trajectory analysis? Q5. In which step expand a column "outline" in the coordinate?

library(Seurat)
library(SPATA2)
library(dplyr)

# Seurat object handling
Seurat::DefaultAssay(cortex) <- "SCT"
testedgenes <- "Mrps31"

# Convert to Spata2 object
spata2_object <- 
  SPATA2::asSPATA2(
    object = cortex,
    sample_name = "mouse_brain",
    image_name = "slice1", 
    spatial_method = "Visium"
    )

# Create a new "histology" segmentation group
spata2_object <- createSpatialSegmentation(spata2_object)
# The "histology" shows
getGroupingOptions(object = spata2_object)
plotSurface(object = spata2_object, color_by = "histology", pt_clrp = "npg")

# NULL ?
getGenes(object = spata2_object) 

# Failed, Error in check_variables: Could not find any of the specified input of 'variables' among genes, gene-sets and features...
plotSurfaceComparison(
  object = spata2_object,
  color_by = c("Pcp4", "Mbp", "Lamp5"), 
  nrow = 1
)

# Follow the steps: https://themilolab.github.io/SPATA2/articles/spata-v2-spatial-trajectories.html
# No column called "outline" so the function "ggpLayerTissueOutline()" failed?
getCoordsDf(spata2_object)

Thank you. Phoebe.

kueckelj commented 10 months ago

Hello Phoebe, thanks for using SPATA2. As for your questions:

  1. The idea of spatial trajectory screening is to predefine a spatial axis which connects to areas and to screen for genes with biologicaly interesting gradients between (!) them. This overcomes the limitations of the "black-and-white" approach by clustering with subsequent DEA and might find genes that are not identified by DEA. However, what should be "checked first" highly depends on your sample and your hypothesize. Can you elaborate a little bit on your goal? Generally speaking, I suggest that you first check for genes that are identified as spatially variable. For instance, SPATA2 implements SPARKX via runSparkx() and getSparkxGenes() but you can do your own testing as well. The function spatialTrajectoryScreening() contains an argument named variables where you can specify exactly which numeric variables you want to include in the screening, genes, gene sets other numeric features etc.
  2. What exactly do you mean with "check"? What is it you want to check? The course of the spatial trajectory? If you are unsure about the input, you can draw the trajectory directly on the image using createSpatialTrajectories().
  3. The function asSPATA2() has an argument called assay_name. It defaults to an assay named Spatial. I recognize that this is suboptimal and should default to the default assay of the Seurat object. Can you try and set assay_name = "SCT" and run the function again? Can you extract the genes now?
  4. What did you loose? Does that refer to the lost genes you mentioned in Q3? You can create image annotations with the function createImageAnnotations(). You do not need these for spatial trajectory screening. Spatial trajectory screening and image annotation screening are related in so far as that they screen for genes with spatially interesting gradients. Spatial trajectory screening can be used in samples with a "linear architectur" such as our example sample UKF269 with a left-to-right architecture. Image annotation screening can be used for circular architectures such as the necrotic center in UKF313.
  5. You can use object <- identifyTissueOutline(object) at any time. This should identify the spatial outline that can be visualized via ggpLayerTissueOutline().

I hope this helps a bit. Please feel free to pose follow up questions if I could not adress your questions sufficiently.

Best regards

Jan

phoebee-h commented 10 months ago

Hi Jan,

Thank you for your kind response. Indeed, genes can be viewed followed by your instructions.

spata2_object <- 
  SPATA2::asSPATA2(
    object = cortex,
    sample_name = "mouse_brain",
    image_name = "slice1", 
    spatial_method = "SCT"    # Do not write "Visium" or "Spatial"
    )

I have some follow-up questions.

  1. Difference of transformSeuratToSpata() and asSPATA2() ?

  2. The createSpatialTrajectories() helps a lot, I think the arrow is way more straightforward. What does 'guiding comment' mean, would it be necessary?

  3. According to functions in createSpatialTrajectories(), does the number of arrows matter in a line? For example, when drawing a trajectory, is a single arrow different from one composed of three arrows? (Please ignore the spot not chosen here due to my shaky fingers. Please assume that all spots are chosen the same.) image

  4. For the demo dataset, what is image_annotation, such as "image_annotations[["269_T"]]" ? Is it necessary for spata2 object? If so, how do we manually create it?

  5. Where can I check the info on segmentation annotation? Is there a way to import segmentation results in the spata2 object, instead of using createSpatialSegmentation()? In which part is it stored? image

  6. I created a trajectory called "test2", but the following code does not work. It shows the error:

    
    # > genes
    # [1] "Trbc2"   "Ighm"    "Lamp5"   "Slc30a3"
    # > gene_colors
    #      Trbc2        Ighm       Lamp5     Slc30a3
    # "#E64B35FF" "#4DBBD5FF" "#00A087FF" "#3C5488FF"

plotTrajectoryLineplot( object = spata2_object, id = "test2", variables = genes, smooth_se = TRUE, clrp_adjust = gene_colors )

Error in base::as.numeric(tobj@segment[, c("x", "y")]) : 'list' object cannot be coerced to type 'double'



Thank you again!
Phoebe.
kueckelj commented 9 months ago

Hello Phoebe, sorry for the late reply. I've been facing some busy days lately.

  1. transformSeuratToSpata() is an old version of asSPATA2(). Use asSPATA2() please.
  2. Guiding comment just provides an option to take a note on why you drew the trajectory. Like the reasoning behind it in case you are drawing a lot of them.
  3. No it does not matter. This feature can be used to mark specific "points" along the trajectory where you either want to change the direction or there is somehting spatially interesting that you'd like to display in the gradient line plots. The argument display_trajectory_parts should allow this.
  4. An image annotation primarily is a polygon that encircles specific areas on the image. This vignette goes into detail: https://themilolab.github.io/SPATA2/articles/cc-spatial-segmentation-vs-image-annotations.html
  5. Segmentations are stored in the objects feature data.frame as variables of class factor. You can use the groups of spots created this way to conduct group based statistical analysis such as DEA or GSEA or t.tests. The vignette I mentioned in 4. elaborates on this, too. What kind of segmentation results do you want to import? Grouping of spots? addFeatures() should help.
  6. I will look into this. Can you visualize the trajectory with plotSpatialTrajectories()?
phoebee-h commented 9 months ago

Hi Jan, Thanks for your kind response anyway.

plotSpatialTrajectories() works for me. So, I am not sure why plotTrajectoryLineplot() failed. These are the ids that were stored in my object.

> names(spata2_object@trajectories$mouse_brain)
[1] "test1" "test2" "test3"

image

Thank you. Phoebe

kueckelj commented 9 months ago

Hey, I found the problem and fixed it. Plotting trajectory lineplots should work now for trajectories with multiple parts, too. I pushed the fixed code to the current master branch. Can you install again and see if it works for you, too?

phoebee-h commented 8 months ago

@kueckelj

Thank you for dealing with my question. Does the deployment failure affect the version of what I can download? remotes::install_github(repo = "theMILOlab/SPATA2@87c7f5cfd24caaf5a3c2001653b61d74fc04bc5f")

image