ImmuneDynamics / Spectre

A computational toolkit in R for the integration, exploration, and analysis of high-dimensional single-cell cytometry and imaging data.
https://immunedynamics.github.io/spectre/
MIT License
57 stars 22 forks source link

Area analysis on Data.table not working in Spectre 1.2.0 #195

Open RoryCostell opened 2 months ago

RoryCostell commented 2 months ago

Hi, I have updated my Spectre to 1.2.0, and also updated R and R studio to the latest build.

When I run the new advanced spatial workflow, everything works great and the addition of the regions and cell types is great.

However, when I run the third script - advanced quantitative analysis, and I run the area analysis, it seems unable to run the area analysis by region for all the ROIs, giving the error:

Smooth muscle cells to Podoplanin+ fibroblasts
Smooth muscle cells to Podoplanin+aSMA+ fibroblasts
Smooth muscle cells to Podoplanin+aSMA+Factor 13a+ fibroblasts
---- Smooth muscle cells to Smooth muscle cells
Region: Tumour
Error in gtools::permutations(n = length(reg.pops), r = 2, V = reg. pops, :
bad value of n

It this because some of the cell types are not present in the region: Tumour?

The area analysis is working fine with the old code, setting the region to total and ignoring the individual regions for the area analysis.

Please let me know if you need more information and I will reproduce the error.

Thank you very much!

Screenshot 2024-08-30 at 11 42 59 AM
tomashhurst commented 2 months ago

@RoryCostell hey mate, we will have a look! This most often comes up if there are some ROIs that don't have all the region types present. Was an oversight on our part when writing the code (we had larger ROIs where there was always representation across them). But if it was working before the update then perhaps something went wrong when we moved to v1.2.

RoryCostell commented 2 months ago

@RoryCostell hey mate, we will have a look! This most often comes up if there are some ROIs that don't have all the region types present. Was an oversight on our part when writing the code (we had larger ROIs where there was always representation across them). But if it was working before the update then perhaps something went wrong when we moved to v1.2.

Thanks Tom!

That must be it, as the ROIs are based on a TMA of separate regions. The workaround I've used for now is to redefine area.table to area.totals, with the previous checks above.

` ### Some checks

    as.matrix(unique(cell.dat[[roi.col]]))
    as.matrix(unique(cell.dat[[group.col]]))
    as.matrix(unique(cell.dat[[region.col]]))
    as.matrix(unique(cell.dat[[batch.col]]))
    area.table

    as.matrix(unique(cell.dat[[pop.col]]))

    ## Temporary until region fix
    **area.totals
    cell.dat$Region <- 'Total'
    region.col <- 'Region'**

### Run area analysis on the DATA.TABLE

    reg.dat <- run.spatial.analysis(dat = cell.dat, 
                                    sample.col = roi.col, 
                                    pop.col = pop.col, 
                                    annot.cols = batch.col, group.col,
                                    region.col = region.col, 
                                    **area.table = area.totals**) ## Also calculate on total by default

    reg.dat[,c(1:10)]
    `