Skip to content

Commit 5efde63

Browse files
authored
Merge pull request #504 from natverse/fix/matching-by-col-numeric-id
Fix/matching by col numeric
2 parents 3741c70 + 2b36409 commit 5efde63

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

R/neuronlist.R

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,13 @@ as.data.frame.neuronlist<-function(x, row.names = names(x), optional = FALSE, ..
298298
nn=names(x)
299299
matching_rows=intersect(nn, rownames(value))
300300
if (length(matching_rows)!=length(nn) && any(nn %in% value[,1])) {
301-
matching_rows_fc=intersect(nn, value[,1])
301+
# need to turn them into string
302+
candids <- id2char(value[,1])
303+
matching_rows_fc=intersect(nn, candids)
302304
if (length(matching_rows)<length(matching_rows_fc)){
303305
warning("Matching neurons by first column.")
304306
matching_rows=matching_rows_fc
305-
rownames(value) <- value[,1]
307+
rownames(value) <- candids
306308
}
307309
}
308310
if(length(matching_rows)){
@@ -1140,3 +1142,19 @@ prune_twigs.neuronlist <- function(x, twig_length, OmitFailures=NA, ...) {
11401142
if(missing(twig_length)) stop("Missing twig_length argument")
11411143
nlapply(x, prune_twigs, twig_length=twig_length, OmitFailures=OmitFailures, ...)
11421144
}
1145+
1146+
# helper function to convert numeric ids to character
1147+
id2char <- function(x) {
1148+
if(is.character(x)) return(x)
1149+
if(is.integer(x) || inherits(x, "integer64")) return(as.character(x))
1150+
# if(is.integer(x)) return(as.character(x))
1151+
if(is.numeric(x)) {
1152+
# this is the tricky one
1153+
# unfortunately it is slower than bit64::as.integer64
1154+
return(format(x, scientific=F))
1155+
}
1156+
warning("unrecognised id class:", paste(class(x), collapse = ','))
1157+
# we don't want to turn logical values into character
1158+
if(is.logical(x)) return(x)
1159+
as.character(x)
1160+
}

tests/testthat/test-neuronlist.R

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@ test_that("as.data.frame.neuronlist behaves", {
282282
expect_equal(kcs20nodf[,], kcs20[,])
283283
expect_equal(rownames(kcs20[,]), rownames(kcs20nodf[,]))
284284

285+
# numeric id column
286+
df$gene_name=1e3+seq_along(kcs20)-1
287+
kcs20nodf=kcs20
288+
data.frame(kcs20nodf)=NULL
289+
names(kcs20nodf)=df$gene_name
290+
expect_warning(kcs20nodf[,] <- df, 'Matching neurons by first column')
291+
expect_equal(rownames(kcs20nodf[,]), as.character(df$gene_name))
285292
})
286293

287294
context("neuronlist: [")
@@ -336,6 +343,11 @@ test_that("prune twigs of a neuronlist", {
336343
expect_lt(pruned_nl[[1]]$NumSegs,n[[1]]$NumSegs)
337344
expect_lt(pruned_nl[[2]]$NumSegs,n[[2]]$NumSegs)
338345
expect_lt(pruned_nl[[3]]$NumSegs,n[[3]]$NumSegs)
339-
340-
346+
})
347+
348+
349+
test_that("id2char works", {
350+
expect_equal(id2char(1e5), id2char("100000"))
351+
expect_equal(id2char(100000L), "100000")
352+
expect_equal(id2char(c(100000L, NA)), c("100000", NA))
341353
})

0 commit comments

Comments
 (0)