HelenaLC / CATALYST

Cytometry dATa anALYsis Tools
67 stars 30 forks source link

Updating colData #337

Closed shanh1 closed 1 year ago

shanh1 commented 1 year ago

Hi there,

If I want to add a completely new column to colData, how can I do this? I have managed to do it for experiment_info, but I am struggling to do this for colData.

I am trying to add it through a table I have uploaded into R, using colbind but I am not sure of the exact syntax.

Thank you!

Shan

HelenaLC commented 1 year ago

The colData in a SCE is nothing more than a DataFrame, which is a "sophisticated" data.frame that can store arbitrary types of data and has a neater show method. Hence, you can add data to it the same way you would to a data.frame, or using the special $ replacement method, i.e.,

# these work directly on a SCE
sce$new <- ...
sce[["new"]] <- ...
# alternatively, any of the following syntaxes works
cd <- colData(sce)
cd <- cbind(cd, ...) 
cd$new <- ...
cd[, "new"] <- ...
cd[["new"]] <- ...
colData(sce) <- cd

Now, I don't know what format your table has that provides supplementary cell metadata. Let us assume it's called tbl and it has an entry sample_id that specifies the sample identifier per row, or something similar, then you can incorporate it as follows:

# get row indices according to what's in the SCE already
i <- match(tbl$sample_id, sce$sample_id)
# specify which columns to keep, e.g., 
# don't want to duplicate the 'sample_id's
j <- c("column1", "column2", ...) # or
j <- setdiff(names(tbl), names(colData(sce)))
cd <- cbind(colData(sce), tbl[i, j])
colData(sce) <- cd