Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sc_geom_annot to add the annotation layer for external data #23

Merged
merged 8 commits into from
May 28, 2024

Conversation

xiangpin
Copy link
Member

This pr provides two methods like the methods of ggtree linking to external data

  • using data of sc_geom_annot (recommend)
  • using %<<+% method to add the external data to the plot data.

Annotation for single feature

{library(SingleCellExperiment)
library(SpatialExperiment)
library(SVP)
library(ggsc)
library(ggplot2)} |> suppressPackageStartupMessages()

data(hpda_spe_cell_dec)
hpda_spe_cell_dec

# identifing the spatial variable function
hpda_spe_cell_dec <- hpda_spe_cell_dec |>
                     runDetectSVG(assay.type="affi.score", method='moran', weight.method='voronoi', action='add')

# extrating the result (action = 'add')
svg.res <- hpda_spe_cell_dec |> svDf()
svg.res |> dplyr::arrange(rank) |> head()


# identifing the spatial functional domain 
lisa.res <- hpda_spe_cell_dec |>
            runLISA(assay.type = 'affi.score', features='Cancer clone A', method='localG', weight.method='voronoi')

# this is the result of local spatial stastics using localG.
lisa.res
lisa.res[["Cancer clone A"]] |> head()
dt <- lisa.res[['Cancer clone A']] |> tibble::rownames_to_column(var='.BarcodeID')

p1 <- sc_spatial(hpda_spe_cell_dec, features='Cancer clone A', mapping=aes(x=x, y=y), pointsize=8)
p1

image

The first method:

the dt (external data) which must contain .Barcode and features or (only .Barcode for single feature), will be added to the f1$data using left_join internally.

p2 <- p1 +
      sc_geom_annot(
        data = dt,
        mapping = aes(bg_colour=cluster.test, subset=cluster.test %in% c("High", "Low")),
        pointsize = 7,
        bg_line_width = .14,
        gap_line_width = .12
      ) +
      scale_bg_color_manual(values=c('black', 'grey'))

image

The second method:

the dt (external data) will be added to the f1$data using %<<+% (also using left_join), but change the plot data. (not recommend)

p1 <- p1 %<<+% dt

p3 <- p1 +
      sc_geom_annot(
        mapping = aes(bg_colour=cluster.test, subset=cluster.test %in% c("High", "Low")),
        pointsize = 7,
        bg_line_width = .14,
        gap_line_width = .12
      ) +
      scale_bg_color_manual(values = c("black", "grey"))
p3

Annotation for multiple features

we can also perform the local spatial statistics to identify the functional spatial domain using runLISA of SVP

lisa.res2 <- runLISA(hpda_spe_cell_dec,
                     features=rownames(hpda_spe_cell_dec)[seq(6)],
                     assay.type='affi.score')
lisa.res2
df <- lisa.res2 |> lapply(function(x)x|>tibble::rownames_to_column(var='.BarcodeID')) |> dplyr::bind_rows(.id='features')
df |> head()
f1 <- sc_spatial(hpda_spe_cell_dec, features = names(lisa.res2), mapping=aes(x=x, y=y), pointsize = 9, ncol=3)
f1

image

The first method:

using sc_geom_annot(data = df, ...)

f2 <- f1 +
      sc_geom_annot(
        data = df,
        mapping = aes(bg_colour = cluster.test, subset = cluster.test %in% c("High", "Low")),
        pointsize = 7,
        bg_line_width = .15,
        gap_line_width = .12
      ) +
      scale_bg_color_manual(values = c("black", "grey"))
f2

image

The second method:

using %<<+% and sc_geom_annot(...)

f1 <- f1 %<<+% df

f3 <- f1 +
    sc_geom_annot(
        mapping = aes(bg_colour = cluster.test, subset = cluster.test %in% c("High", "Low")),
        pointsize = 7,
        bg_line_width = .15,
        gap_line_width = .12
    ) +
    scale_bg_color_manual(values = c("black", "grey"))
f3

image

When the common.legend=FALSE

the fig1 is a patchwork object when common.legend=FALSE.

fig1 <- sc_spatial(hpda_spe_cell_dec,
                   features = names(lisa.res2),
                   mapping = aes(x=x, y=y),
                   pointsize = 9,
                   common.legend=FALSE,
                   ncol = 3
        )

image

the first method:

the annotation layer should use & to add the original layer instead of +.

fig2 <- fig1 &
        sc_geom_annot(
          data = df,
          mapping = aes(bg_colour = cluster.test, subset = cluster.test %in% c("High", "Low")),
          pointsize = 7,
          bg_line_width = .14,
          gap_line_width = .12
        ) &
        scale_bg_color_manual(values = c('black', 'grey'))

image

the second method:

fig1 <- fig1 %<<+% df
fig3 <- fig1 &
      sc_geom_annot(
        mapping = aes(bg_colour = cluster.test, subset = cluster.test %in% c("High", "Low")),
        pointsize = 7,
        bg_line_width = .14,
        gap_line_width = .12
      )
     
fig3

image

@GuangchuangYu
Copy link
Member

这个%<<+%能和ggtree::%<+%统一?

可以统一的话,我们可以放到ggfun包里。

@xiangpin
Copy link
Member Author

我看下,应该也可以用S3方法写成统一的

@xiangpin
Copy link
Member Author

已经用S3方法统一到ggfun

@GuangchuangYu
Copy link
Member

那么ggtree和ggsc就做相应的修改吧。

@xiangpin
Copy link
Member Author

已经做好相应的修改了,现在统一成 %<+% , 所以上面ggsc 中有用到%<<+%改成%<+%即可。然后再DESCRIPTION里已经声明ggfun (>0.1.4).

update ggfun version
@GuangchuangYu GuangchuangYu merged commit 8682681 into YuLab-SMU:devel May 28, 2024
0 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants