girke-lab / ChemmineR

Cheminformatics Toolkit for R
13 stars 7 forks source link

Output type of groups() is inconsistent #15

Closed Aariq closed 1 year ago

Aariq commented 1 year ago

With an SDFset object with just 1 molecule, the output is a named numeric vector, otherwise it is a matrix. This makes it difficult to work with because the column names when converting the output to a data frame are different depending on the number of molecules in the SDFset.

library(ChemmineR)
data(sdfsample); sdfset <- sdfsample
write.SDF(sdfset[1:4], file="sub.sdf")
sdf <- read.SDFset("sub.sdf")

class(ChemmineR::groups(sdf[1]))
#> [1] "numeric"
class(ChemmineR::groups(sdf))
#> [1] "matrix" "array"

Created on 2023-06-29 with reprex v2.0.2

tgirke commented 1 year ago

Thanks. Seems like an oversight. Should be an easy fix, possibly just adding drop=FALSE, but will need to do some testing before committing an update. For now, you could do something like this:

## Define tmp function
groupsTmp <- function(x, groups="fctgroup") {
    if(class(x) != "SDFset") { stop("Input is not an 'SDFset' object") }
    if(length(x) > 1) {
        groups(x, groups) 
    } else if(length(x) == 1) {
        matrix(groups(x, groups), nrow=1, dimnames=list(cid(x), names(groups(x, groups))))
    } else {
        stop("something unexpected")
    }
}

## Test tmp function
library(ChemmineR)
data(sdfsample)

## More than 1 SDF
sdfset <- sdfsample
groupsTmp(x=sdfset)
## One SDF
sdfset <- sdfsample[1]
groupsTmp(x=sdfset)
groupsTmp(x=sdfset, groups="fctgroup")
groupsTmp(x=sdfset, groups="neighbors")
## Something unintended
sdfset <- iris
khoran commented 1 year ago

This has been fixed in version 3.53.1. Just needed a drop=FALSE, as @tgirke suggested.