Skip to content

Adding data to row/colData #92

Description

@RiboRings

Hi!

Recently I came across a potential utility for the SE package. If you see utility in this usage, I would be happy to develop it further and contribute to the SE package.

Packages such as ALDEx2 and Maaslin3 for differential abundance analysis do not return SE objects but rather a data.frame of statistical results. Since I wanted to integrate these results with rowTree and other information contained in the SE to make comprehensive plots, I repeatedly found myself adding the results to the rowData:

### After running differential abundance analysis

# Add feature IDs to results rownames
rownames(maaslin3_res) <- maaslin3_res$feature
# Match results rows to tse rows
tax.order <- match(rownames(tse), rownames(maaslin3_res))
# Add results to rowData
rowData(tse) <- cbind(rowData(tse), maaslin3_abund[tax.order, ])

I ended up writing a function for this as I needed it multiple times in the analysis:

# Add feature IDs to results rownames
rownames(maaslin3_res) <- maaslin3_res$feature
# Add results to rowData
tse <- addToRowData(tse, maaslin3_res)

This is a simple draft of the function:

addToRowData <- function(x, y, suffix = ""){
  rowData(x) <- .add_to_side(x, y, 1L, suffix)
  return(x)
}

addToColData <- function(x, y, suffix = ""){
  colData(x) <- .add_to_side(x, y, 2L, suffix)
  return(x)
}

.add_to_side <- function(x, y, by, suffix){
    FUN <- switch(by, rowData, colData)
    df <- as.data.frame(FUN(x))
    names(y) <- paste(names(y), suffix, sep = ".")
    row.order <- match(rownames(df), rownames(y))
    new.y <- cbind(df, y[row.order, , drop = FALSE])
    return(new.y)
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions