Operations on TglowDataset
The following page shows examples of common operations that can be performed on TglowDatasets and its derived objects.
Show
Show will show some useful info on the object
You can also show tglow assays
Slicing
TglowDatasets and assays can be sliced by row
They can also be sliced by column, but this is not recommended unless using column names, as assays can have different number of columns. A warning is raised when you try to slice columns with a non-character.
Individual TglowAssays can also be sliced, in this case it is safe to use integers to select features.
Accessing assays
You can access TglowAssays from the @assays slot by using $ or by [[]]
Accessing feature data
You can access the data using slicing [] or by using $ on a TglowAssay or a TglowMatrix. Slicing a TglowAssay returns a new assay. $ on a TglowAssay returns a list with items data and scale.data, $ on a TglowMatrix returns a numeric vector.
Setting and manipulating assays
You can set TglowAssays using the @assays slot. The assays slot is just a list, so you can put anything in it but if you want it to work properly new.assay must be a TglowAssay. Note at the moment you can only add assays using tglow@assays[["new.assay"]] <- new.assay and not the other operators.
You can manipulate specific slots in existing assays as well. For example, replacing the data slot with the scale.data slot. The scale.data slot is NULL until the assay is scaled, so run scale_assay() first.
Adding features to an existing assay
The function add_features_to_assay() can be used to add new features to an existing assay. The features are added to the slot given by slot, and by default matching columns of NA's are added to the other slot (if it is set). With preserve.other=TRUE the other slot is left untouched, so it no longer has the same columns as slot.
Accessing data
To get a mix of meta data of feature information per cell object there is a convenience function getDataByObject() which grabs data from any assay, slot or metadata item as long as its colnames are uniquely findable. The function returns a object x feature data frame. The function getDataByObject() only works with one assay at the time, if you grab features from two different assays, you will need to run getDataByObject twice and call cbind on the result.
Getting and setting object IDs
Object ID's can be viewed in two ways
The method objectIds is implemented for TglowDataset, TglowAssay and TglowReduction.
To set object ID's you can use the <- operator.
NOTE: You can also do it manually through the slots, but this is not recommended, as it can lead to issues when not all slots are set properly, as it assumed all slots have the rownames set to enable easy slicing by object ID. Similarly, you could call
objectIds(tglow@assays[[1]]) <- 1:nrow(tglow)but this will likely break downstream functionality.
Aggregating datasets
TglowDatasets can be aggregated using aggregate_by_imagecol() which will take a dataset, apply the supplied aggregation function, and return a new TglowDataset with the aggregated data. Supported aggregations are mean, median and sum. Metadata in @meta and @image.meta is aggregated as well:
- Numeric columns are aggregated with
method. - Character and logical columns are kept if there is one unique value per group, otherwise the result is NA. With
drop.multival.string=FALSE, multiple character values are pasted together withsep(default"; "). - Factors with several values are always pasted together with
sep. - Columns that have only NA's are dropped unless
drop.na.col=FALSE.
The result has no feature map and no reductions, and @image.data.trans and @image.data.norm are not carried over.
The output object is a valid TglowDataset, so all functions should work as usual
Matching two TglowDatasets
Matching on nearest neighbour position
There are often cases when features have been extracted for the same set of images and cells, but perhaps using slightly different masks, or different softwares. This can make it harder to match due to rounding errors or implementation differences yielding slightly different xyz positions for cells. Tglow-r contains a function that matches two TglowDatasets based on the nearest neighbors in xy space, within the same plate, well and field. It uses the @feature.map of both objects to find these columns. Both read_cellprofiler_parquet() and read_pipeline_parquet() set the feature map automatically, but aggregated or manually constructed objects will not have one.
tol: the maximum xy distance for a nearest neighbour to count as a match (default 2). Objects inawithout a match get NA's in the data taken fromb.mode="add"(default): all assays frombare added toaas new assays. If an assay name already exists ina, it is prefixed withassay.prefix(default"b_").mode="merge": assays with the same name are merged, adding only the features frombthat are not yet ina.
Columns in b@meta that are not in a@meta are added to a@meta. Image level data is not merged.
This is the recommended way to match pipeline intensity output (read_pipeline_parquet()) to CellProfiler output (read_cellprofiler_parquet()). Their object IDs differ, as the pipeline uses the Cellpose label (P1_B08_I1_L1) and CellProfiler the ObjectNumber (P1_B08_I1_O1).
Matching manually on ID
You can align two datasets on a matching ID. Object IDs look like P1_B08_I1_O1, where the short plate ID P1 is assigned per run, so IDs are not guaranteed to match between datasets from different runs. Given you use the same cellpose masks and have configured CellProfiler to NOT relabel cells, you can use the plate, well, field and cell_ObjectNumber to match CellProfiler datasets.