# CNSeg no method coercing S4 class to vector

I'm attempting to analyze some genomic data with the help of a package called CNSeg. The package is located here and the documentation is here.

My input data looks like:

> str(segments)
'data.frame':   11897 obs. of  7 variables:
$X : int 0 1 2 3 4 5 6 7 8 9 ...$ SegmentID        : int  72 73 74 75 76 77 78 79 80 81 ...
$Chromosome : int 1 1 2 2 2 3 3 3 3 3 ...$ StartPosition    : int  754192 145260908 21494 141215321 141275624 63411 69812903 69884262 126473310 126790130 ...
$StopPosition : int 145258178 249212878 141214996 141275051 243052331 69811900 69884106 126457276 126772699 197852564 ...$ Median.Log2.Ratio: num  -0.014 0.311 -0.003 0.059 -0.012 -0.018 -0.106 0.007 -0.171 0.001 ...
$FileName : Factor w/ 95 levels "TSB02183","TSB02184",..: 1 1 1 1 1 1 1 1 1 1 ..  (Note: I did import the segments data with FileName as a vector instead of a factor as well. No difference in the error below). I followed the vignette to the letter and, at the end of the vignette, the creator tells me that I can use the dist() method. When I try to use it, I get an error: Error in as.vector(data) : no method for coercing this S4 class to a vector. Here is my session information: > segments <- read.csv("Probe_Segments_CN.csv") > cnseg <- CNSeg(segList = segments, chromosome = "Chromosome", end = "StopPosition", start = "StartPosition", segMean = "Median.Log2.Ratio", id = "FileName") > rdseg <- getRS(cnseg, by = "region", imput = FALSE, XY = FALSE, what = "mean") Processing samples ... Done > data("geneInfo") > geneInfo <- geneInfo[sample(1:nrow(geneInfo), 2000), ] > > rdByGene <- getRS(cnseg, by = "gene", imput = FALSE, XY = FALSE, geneMap = geneInfo, what = "median") > > reducedseg <- rs(rdseg) > f1 <- kOverA(5, 1) > > ffun <- filterfun(f1) > > filteredrs <- genefilter(rdseg, ffun) > filteredrs <- madFilter(rdseg, 0.8) > dist(filteredrs) Error in as.vector(data) : no method for coercing this S4 class to a vector > sessionInfo() R version 3.4.0 (2017-04-21) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows >= 8 x64 (build 9200) Matrix products: default locale: [1] LC_COLLATE=English_United States.1252 [2] LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] tools stats graphics grDevices [5] utils datasets methods base other attached packages: [1] CNTools_1.34.0 genefilter_1.60.0 loaded via a namespace (and not attached): [1] Rcpp_0.12.17 AnnotationDbi_1.40.0 [3] BiocGenerics_0.24.0 splines_3.4.0 [5] IRanges_2.12.0 bit_1.1-14 [7] lattice_0.20-35 xtable_1.8-2 [9] blob_1.1.1 parallel_3.4.0 [11] grid_3.4.0 Biobase_2.38.0 [13] DBI_1.0.0 survival_2.41-3 [15] bit64_0.9-7 digest_0.6.15 [17] Matrix_1.2-9 S4Vectors_0.16.0 [19] bitops_1.0-6 RCurl_1.95-4.10 [21] memoise_1.1.0 RSQLite_2.1.1 [23] compiler_3.4.0 stats4_3.4.0 [25] XML_3.98-1.11 annotate_1.56.2  ## 2 Answers If you are using the latest version of Bioconductor you should always look at the latest documentation of the package in Bioconductor. There could be changes from 2011 to 2018 (In this case there aren't any changes in the section you mention). The problem is the package doesn't define how dist should behave with that object class (filteredrs is a RS object if I read the source code well). You can extract the metrics to calculate the distance yourself or ask the maintainer to add this method to the class. However it could be that the maintainer answer you if you post the question in the support site of Bioconductor. • Good catch--I'll look at more updated documentation in the future and I'll ask the question on the Bioconductor support site. And yes, you read the code correctly I believe. How would you extract the metrics? It's in the values pane as a "Large RS" value with a dataframe inside, I just don't know how to get to the dataframe. Jun 15 '18 at 16:44 • EDIT: Solution is attributes(filteredrs)[1], since "$" doesn't work on the object. Jun 15 '18 at 16:53
• @CalendarJ You can post (and accept) your own solution, that way you can help future readers (comments are harder to find [not indexed by google] and can be deleted).
– llrs
Jun 18 '18 at 16:15

I was not able to get the CNSeg clustering method to work, but the work-around was to export the data and cluster it independently of the CNTools package (thanks to Llopis's answer below). I'll post the analysis below.

require(CNTools)
segData <- read.csv("result_cnv.csv", stringsAsFactors = FALSE)

# Create inital CN object
cnseg <- CNSeg(segList = segData, chromosome = "Chromosome", end = "StopPosition", start = "StartPosition", segMean = "Median.Log2.Ratio", id = "FileName")
cnseg

# Create inital RD object
rdseg <- getRS(cnseg, by = "region", imput = FALSE, XY = FALSE, what = "mean")
rdseg

# Collect gene information
data("geneInfo")
geneInfo <- geneInfo[sample(1:nrow(geneInfo), 2000), ]

# Create an RD based on gene information
rdByGene <- getRS(cnseg, by = "gene", imput = FALSE, XY = FALSE, geneMap = geneInfo, what = "median")

# Initalize reduced segment
reducedseg <- rs(rdseg)

# Create a function that evaluates to TRUE if at least 5 of the argument elements are larger than 1
f1 <- kOverA(5, 1)

# Create a filter based on f1
ffun <- filterfun(f1)

# Use the CNTools genefilter
filteredrs <- genefilter(rdseg, ffun)

filteredrs

# Write the filtered data to file
write.csv(attributes(filteredrs)[1], "CNseg_CNOut.csv")

# Reading the recently-written file helps with formatting