<p>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. </p>

<hr>

<pre><code>require(CNTools)
segData &lt;- read.csv("result_cnv.csv", stringsAsFactors = FALSE)
head(segData)

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

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

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

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

# Initalize reduced segment
reducedseg &lt;- rs(rdseg)

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

# Create a filter based on f1
ffun &lt;- filterfun(f1)

# Use the CNTools genefilter 
filteredrs &lt;- genefilter(rdseg, ffun)

# Use the CNTools madFilter
filteredrs &lt;- madFilter(rdseg, 0.8)

filteredrs

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

# Reading the recently-written file helps with formatting
CNseg &lt;- read.csv("CNseg_CNOut.csv")

# Keep only the columns with actual sample values
CNseg &lt;- CNseg[5:ncol(CNseg)]

# Calculate eluclidian distance between samples
d &lt;- dist(t(CNseg), method = "euclidean")

# Calculate clustering
hc1 &lt;- hclust(d, method = "ward.D")
plot(hc1, cex = 0.6, hang = -1, main = "Clusters of Copy Number Alterations", xlab = "Euclidean Distance")
</code></pre>

CC BY-SA 4.0
2018-06-18T16:20:31.053Z
2018-06-20T07:09:47.733Z
2018-06-20T07:09:47.733Z
julianstanley
<p>If you are using the latest version of Bioconductor you should always look at the latest <a href="https://bioconductor.org/packages/release/bioc/vignettes/CNTools/inst/doc/HowTo.pdf" rel="nofollow noreferrer">documentation</a> 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).</p>

<p>The problem is the package doesn't define how <code>dist</code> should behave with that object class (filteredrs is a RS object if I read the source code well). </p>

<p>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 <a href="http://support.bioconductor.org" rel="nofollow noreferrer">support site</a> of Bioconductor. </p>

CC BY-SA 4.0
2018-06-15T16:44:24.540Z
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.
julianstanley
CC BY-SA 4.0
2018-06-15T16:53:20.120Z
EDIT: Solution is `attributes(filteredrs)[1]`, since "$" doesn't work on the object.
julianstanley
CC BY-SA 4.0
2018-06-18T16:15:37.480Z
@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
CC BY-SA 4.0
2018-06-15T14:56:43.087Z
2018-06-15T14:56:43.087Z
llrs
<p>I'm attempting to analyze some genomic data with the help of a package called CNSeg. The package <a href="https://bioconductor.org/packages/release/bioc/html/CNTools.html" rel="nofollow noreferrer">is located here</a> and <a href="https://confluence.broadinstitute.org/download/attachments/29790363/CNTools_How_To.pdf?version=1&amp;modificationDate=1312551505000" rel="nofollow noreferrer">the documentation is here.</a> </p>

<p>My input data looks like:</p>

<pre><code>&gt; 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 ..
</code></pre>

<p>(Note: I did import the segments data with FileName as a vector instead of a factor as well. No difference in the error below).</p>

<p>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: <strong>Error in as.vector(data) : 
  no method for coercing this S4 class to a vector</strong>.</p>

<p>Here is my session information:</p>

<pre><code>&gt; segments &lt;- read.csv("Probe_Segments_CN.csv")
&gt; cnseg &lt;- CNSeg(segList = segments, chromosome = "Chromosome", end = "StopPosition", start = "StartPosition", segMean = "Median.Log2.Ratio", id = "FileName")
&gt; rdseg &lt;- getRS(cnseg, by = "region", imput = FALSE, XY = FALSE, what = "mean")
Processing samples ... Done
&gt; data("geneInfo")
&gt; geneInfo &lt;- geneInfo[sample(1:nrow(geneInfo), 2000), ]
&gt; 
&gt; rdByGene &lt;- getRS(cnseg, by = "gene", imput = FALSE, XY = FALSE, geneMap = geneInfo, what = "median")
&gt; 
&gt; reducedseg &lt;- rs(rdseg)
&gt; f1 &lt;- kOverA(5, 1)
&gt; 
&gt; ffun &lt;- filterfun(f1)
&gt; 
&gt; filteredrs &lt;- genefilter(rdseg, ffun)
&gt; filteredrs &lt;- madFilter(rdseg, 0.8)
&gt; dist(filteredrs)
Error in as.vector(data) : 
  no method for coercing this S4 class to a vector
&gt; sessionInfo()
R version 3.4.0 (2017-04-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows &gt;= 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   
</code></pre>

<r><bioconductor>
CNSeg no method coercing S4 class to vector
# CNSeg no method coercing S4 class to vector

(Asked by: username_0 on 2018-06-15)

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

**Tags:** r, bioconductor

---

## 2 answers

### Answer by username_0 on 2018-06-18. Score: 1 | Accepted answer

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)
head(segData)

# 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)

# Use the CNTools madFilter
filteredrs <- madFilter(rdseg, 0.8)

filteredrs

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

# Reading the recently-written file helps with formatting
CNseg <- read.csv("CNseg_CNOut.csv")

# Keep only the columns with actual sample values
CNseg <- CNseg[5:ncol(CNseg)]

# Calculate eluclidian distance between samples
d <- dist(t(CNseg), method = "euclidean")

# Calculate clustering
hc1 <- hclust(d, method = "ward.D")
plot(hc1, cex = 0.6, hang = -1, main = "Clusters of Copy Number Alterations", xlab = "Euclidean Distance")


---

### Answer by username_1 on 2018-06-15. Score: 2

If you are using the latest version of Bioconductor you should always look at the latest [documentation](https://bioconductor.org/packages/release/bioc/vignettes/CNTools/inst/doc/HowTo.pdf) 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](http://support.bioconductor.org) of Bioconductor.

> **Comments:**
> **username_0**: 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.  
> **username_0**: EDIT: Solution is `attributes(filteredrs)[1]`, since "$" doesn't work on the object.  
> **username_1**: @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).  

---

