Extract S4 object from list vector monitoR - r

I have a list vector containing S4 correlation templates from the monitoR::makeCorTemplate function.
temps_0 <- vector(mode = 'list',length=length(tru_files_info_0$species_id))
for (j in 1: length(tru_files_info_0$species_id)) {
temps_0[j] <- MonitoR::makeCorTemplate(paste0('tru_tp_files','/',paste0(tru_files_info_0$recording_id[j],'',
'.wav')), t.lim =c(tru_files_info_0$t_min[j], tru_files_info_0$t_max[j]),
frq.lim = c(tru_files_info_0$f_min[j]/1000, tru_files_info_0$f_max[j]/1000),
select = 'auto', dens =1, score.cutoff = 0.2, name = tru_files_info_0$new_name[j])
+ }```
Resulting object
Formal class 'corTemplateList' [package "monitoR"] with 1 slot
..# templates:List of 1
.. ..$ 00d442df7_0:Formal class 'corTemplate' [package "monitoR"] with 15 slots
.. .. .. ..# clip.path : chr "tru_tp_files/00d442df7.wav"
.. .. .. ..# samp.rate : int 48000
.. .. .. ..# pts : num [1:1924, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
.. .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. .. ..$ : NULL
.. .. .. .. .. ..$ : chr [1:3] "t" "frq" "amp"
.. .. .. ..# t.step : num 0.0107
.. .. .. ..# frq.step : num 0.0938
.. .. .. ..# n.t.bins : int 73
.. .. .. ..# first.t.bin : num 19.4
.. .. .. ..# n.frq.bins : int 25
.. .. .. ..# duration : num 0.779
.. .. .. ..# frq.lim : num [1:2] 5.91 8.25
.. .. .. ..# wl : int 512
.. .. .. ..# ovlp : int 0
.. .. .. ..# wn : chr "hanning"
.. .. .. ..# score.cutoff: num 0.2
.. .. .. ..# comment : chr ""
The next processing step is to combine these 10 templates via combineCorTemplates:
> ctemps_0 <- monitoR::combineCorTemplates(temps_0[[1]], temps_0[[2]], temps_0[[3]], temps_0[[4]], temps_0[[5]], temps_0[[6]], temps_0[[7]], temps_0[[8]], temps_0[[9]], temps_0[[10]])
> ctemps_0
Object of class "corTemplateList"
containing 10 templates
original.recording sample.rate lower.frequency
00d442df7_0 tru_tp_files/00d442df7.wav 48000 5.906
0ea8ea68a_0 tru_tp_files/0ea8ea68a.wav 48000 5.906
2e40b2294_0 tru_tp_files/2e40b2294.wav 48000 5.906
45c356538_0 tru_tp_files/45c356538.wav 48000 5.906
My question, how to extract the S4 from the list vector without writing out
each list element as combineCorTemplates(temps_0[[1]], temps_0[[2]], & etc
as this is error prone.

We could use do.call
c_temps_0 <- do.call(monitoR::combineCorTemplates, temps_0)
-testing
c_temps_1 <- combineCorTemplates(temps_0[[1]], temps_0[[2]],
temps_0[[3]], temps_0[[4]])
identical(c_temps_0, c_temps_1)
#[1] TRUE
NOTE: The reproducible example is created from ?combineCorTemplates

Related

Extract shape file coordinates from a leaflet map in shiny app

I have a shape file to plot on the map in shiny app. The file structure is as follow :
user#servr:~/home/shp$ ls -la
total 48
drwxr-xr-x 2 user user Jan 17 13:17 .
drwxr-xr-x 7 user user 4096 Jan 17 13:13 ..
-rw-r--r-- 1 user user 5 Jan 17 13:13 myshape.cpg
-rw-r--r-- 1 user user 423 Jan 17 13:13 myshape.dbf
-rw-r--r-- 1 user user 404 Jan 17 13:13 myshape.prj
-rw-r--r-- 1 user user 132 Jan 17 13:13 myshape.sbn
-rw-r--r-- 1 user user 116 Jan 17 13:13 myshape.sbx
-rw-r--r-- 1 user user 15044 Jan 17 13:13 myshape.shp
-rw-r--r-- 1 user user 108 Jan 17 13:13 myshape.shx
I could read those file in R as well :
myspdf <- readOGR(dsn = "/home/shp/",layer = "myshape")
myspdf <- spTransform(myspdf, CRS("+init=epsg:4326"))
Now the str of myspdf is :
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
..# data :'data.frame': 1 obs. of 3 variables:
.. ..$ Shape_Leng: num 80111
.. ..$ Shape_Area: num 59127699
.. ..$ GISFILE : chr "O22_EVAR_ArchWreckFound_v01_221216dehm25833_Buff400m"
..# polygons :List of 1
.. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
.. .. .. ..# Polygons :List of 3
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..# labpt : num [1:2] 420838 6079834
.. .. .. .. .. .. ..# area : num 50391583
.. .. .. .. .. .. ..# hole : logi FALSE
.. .. .. .. .. .. ..# ringDir: int 1
.. .. .. .. .. .. ..# coords : num [1:586, 1:2] 429230 429217 429183 429148 429114 ...
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..# labpt : num [1:2] 424698 6072349
.. .. .. .. .. .. ..# area : num 6408830
.. .. .. .. .. .. ..# hole : logi FALSE
.. .. .. .. .. .. ..# ringDir: int 1
.. .. .. .. .. .. ..# coords : num [1:312, 1:2] 421726 421745 421765 421785 421806 ...
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..# labpt : num [1:2] 422217 6071588
.. .. .. .. .. .. ..# area : num 2329246
.. .. .. .. .. .. ..# hole : logi FALSE
.. .. .. .. .. .. ..# ringDir: int 1
.. .. .. .. .. .. ..# coords : num [1:32, 1:2] 425110 421328 420681 420682 420683 ...
.. .. .. ..# plotOrder: int [1:3] 1 2 3
.. .. .. ..# labpt : num [1:2] 420838 6079834
.. .. .. ..# ID : chr "0"
.. .. .. ..# area : num 59129659
.. .. .. ..$ comment: chr "0 0 0"
..# plotOrder : int 1
..# bbox : num [1:2, 1:2] 413125 6069810 430341 6084226
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "x" "y"
.. .. ..$ : chr [1:2] "min" "max"
..# proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..# projargs: chr "+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"
.. .. ..$ comment: chr "PROJCRS[\"ETRS89 / UTM zone 33N\",\n BASEGEOGCRS[\"ETRS89\",\n DATUM[\"European Terrestrial Reference"| __truncated__
..$ comment: chr "TRUE"
This shows that I have 3 separated area with coordinates array of length 586,312 and 32. I'm able to get the coordinate of the polygons as well :
coor01 <- myspdf#polygons[[1]]#Polygons[[1]]#coords
coor02 <- myspdf#polygons[[1]]#Polygons[[2]]#coords
coor03 <- myspdf#polygons[[1]]#Polygons[[3]]#coords
What I am looking for is the option on shiny app, which by click on the shape file I get the coordinate (coor01,coor02 or coor03) !
Right now if I click on shape file from the app, I get always one single lat and lng value not the complete list !
library(shiny)
library(leaflet)
library(rgdal)
ui <- fluidPage(
titlePanel(p("Spatial app", style = "color:#3474A7")),
sidebarLayout(
sidebarPanel(
),
mainPanel(
leafletOutput("map")
)
)
)
# server()
server <- function(input, output, session) {
data <- reactiveValues(clickedShape=NULL)
myspdf <- readOGR(dsn = "/home/shp/",layer = "myshape")
myspdf <- spTransform(myspdf, CRS("+init=epsg:4326"))
output$map<-renderLeaflet({
leaflet() %>% addTiles()%>% addPolygons(data = myspdf,highlight = highlightOptions(
weight = 5,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE))
})
observeEvent(input$map_shape_click,{
print("observed map_marker_click")
data$clickedShape <- input$map_shape_click
print(data$clickedShape)
})

Convert from spatialpixeldataframe to estUD

In short: I have a spatialpixeldataframe of a utilization distribution of an animal across a grid (udspdf) which I need to convert to estUD-class.
Background to how I got there: After calculating the KDE of a single animal, I then need to convert the estUD-class object to a spatialpixeldataframe in order to remove non-habitat cells, and rescale the utilisation distribution of the habitat-cells to 1. I then need to convert the spatialpixeldataframe back to an estUD class file so that I can run kerneloverlaphr() on it.
I have the following code, which converts it to an estUDm-class. But I need it in estUD-class, as there is only one animal.
re <- lapply(1:ncol(udspdf), function(i) {
so <- new("estUD", udspdf[,i])
so#h <- list(h=0, meth="specified") # specify dummy h values: they are only required to recreate the estUDm
so#vol <- FALSE
return(so)
})
names(re) <- names(udspdf)
class(re) <- "estUDm"
image(re)
If I just change
class(re) <- "estUD"
This seems to work, but then I can see there is an issue because
image(re)
renders the following error: Error in is(x, "GridTopology") :
trying to get slot "grid" from an object (class "estUD") that is not an S4 object
I am sorry I do not know how to provide a reproducible example for such an example as the data is quite complex. I hope that a general code exists.
Any pointers appreciated!
> str(re)
List of 1
$ ud:Formal class 'estUD' [package "adehabitatHR"] with 9 slots
.. ..# h :List of 2
.. .. ..$ h : num 0
.. .. ..$ meth: chr "specified"
.. ..# vol : logi FALSE
.. ..# data :'data.frame': 4400000 obs. of 1 variable:
.. .. ..$ ud: num [1:4400000] 0 0 0 0 0 0 0 0 0 0 ...
.. ..# coords.nrs : num(0)
.. ..# grid :Formal class 'GridTopology' [package "sp"] with 3 slots
.. .. .. ..# cellcentre.offset: Named num [1:2] -70 -60
.. .. .. .. ..- attr(*, "names")= chr [1:2] "Var2" "Var1"
.. .. .. ..# cellsize : Named num [1:2] 0.01 0.01
.. .. .. .. ..- attr(*, "names")= chr [1:2] "Var2" "Var1"
.. .. .. ..# cells.dim : Named int [1:2] 2000 2200
.. .. .. .. ..- attr(*, "names")= chr [1:2] "Var2" "Var1"
.. ..# grid.index : int [1:4400000] 1 2 3 4 5 6 7 8 9 10 ...
.. ..# coords : num [1:4400000, 1:2] -70 -70 -70 -70 -70 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : NULL
.. .. .. ..$ : chr [1:2] "Var2" "Var1"
.. ..# bbox : num [1:2, 1:2] -70 -60 -50 -38
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:2] "Var2" "Var1"
.. .. .. ..$ : chr [1:2] "min" "max"
.. ..# proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. .. ..# projargs: chr "+proj=longlat +datum=WGS84 +no_defs"
- attr(*, "class")= chr "estUDm"
my code for kerneloverlaphr(), which works with the outputs stright from kernelUD() (estUD-class) is as follows:
NWI15b, BCI15b and BCI15i are my individual animals
library(adehabitatHR)
# convert list of KDEs (estUDs) to class estUDm
tot <- list(NWI15b=NWI15b, BCI15b=BCI15b, BCI15i=BCI15i)
class(tot) <- "estUDm"
#calculate overlap using kerneloverlaphr
kerneloverlaphr(tot, method = c("BA"), percent = 95, conditional = FALSE)
dput(head(as.data.frame.estUD(re), n = 10))
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'x' in selecting a method for function 'head': no method or default for coercing “estUDm” to “SpatialPixelsDataFrame”
> unique(re#data$ud)
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'x' in selecting a method for function 'unique': trying to get slot "data" from an object (class "estUDm") that is not an S4 object
structure of object fed to kernelUD
str(track_sp)
Formal class 'SpatialPoints' [package "sp"] with 3 slots
..# coords : num [1:9790, 1:2] -59.2 -59.2 -59.2 -59.2 -59.2 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:9790] "1" "2" "3" "4" ...
.. .. ..$ : chr [1:2] "x" "y"
..# bbox : num [1:2, 1:2] -65.8 -55.6 -56.1 -52.2
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "x" "y"
.. .. ..$ : chr [1:2] "min" "max"
..# proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..# projargs: chr "+proj=longlat +datum=WGS84 +no_defs"
Where we have
re <- lapply(1:ncol(udspdf), function(i) {
so <- new("estUD", udspdf[,i])
so#h <- list(h=0, meth="specified") # specify dummy h values: they are only required to recreate the estUDm
so#vol <- FALSE
return(so)
})
> names(re) <- names(udspdf)
> class(re) <- "estUDm"
> image(re)
We then need to simply add this:
colonyA <- re[[1]]
#and then do the same after running the code for the next colony
colonyB<- re[[1]]

Significantly different speeds on raster (netCDF) calculations in R

I have some WRF output data that was subsetted and masked using pythons xarray module.
I'm now performing calculations on raster bricks using R's raster package and finding very different speeds for very similar files.
Knowns:
There are 3 netCDF files, all the exact same size - 9.47 GB, that contain 9 variables
They all have the exact same dimensions (nrow 327, ncol 348, nlayer 365)
All calculations are on individual files (layer calculations)
All calculations are on the same variable with the same values (except for the second which is masked)
system.time(sum(d97[[1:365]]))
user system elapsed
5.428 2.771 8.840
The second file is the exact same file but a masked portion, with all the masked values converted to NaN.
system.time(sum(masked_d97[[1:365]]))
user system elapsed
10.784 2.157 13.052
The last file is a slightly modified version (daily values rather than cummulative values) of the first file. It was modified using Xarray in Python.
system.time(sum(mod_d97[[1:365]]))
user system elapsed
22.015 1.773 24.474
What on earth is happening here? I'm happy to provide more details (code, ncdumps, etc) as requested.
EDIT: added str() of files
d97 <- brick(files[8], varname = "TMIN")
masked_97 <- brick(files[3], varname = "TMIN")
d03 <- brick(files[11], varname = "TMIN")
str(d97)
Formal class 'RasterBrick' [package "raster"] with 12 slots
..# file :Formal class '.RasterFile' [package "raster"] with 13 slots
.. .. ..# name : chr "/Users/charlesbecker/Desktop/Data/Project Data/Shiny/WY1997_yearly_stats.nc"
.. .. ..# datanotation: chr "FLT4S"
.. .. ..# byteorder : chr "little"
.. .. ..# nodatavalue : num NaN
.. .. ..# NAchanged : logi FALSE
.. .. ..# nbands : int 365
.. .. ..# bandorder : chr "BIL"
.. .. ..# offset : int 0
.. .. ..# toptobottom : logi TRUE
.. .. ..# blockrows : int 0
.. .. ..# blockcols : int 0
.. .. ..# driver : chr "netcdf"
.. .. ..# open : logi FALSE
..# data :Formal class '.MultipleRasterData' [package "raster"] with 14 slots
.. .. ..# values : logi[0 , 0 ]
.. .. ..# offset : num 0
.. .. ..# gain : num 1
.. .. ..# inmemory : logi FALSE
.. .. ..# fromdisk : logi TRUE
.. .. ..# nlayers : int 365
.. .. ..# dropped : NULL
.. .. ..# isfactor : logi FALSE
.. .. ..# attributes: list()
.. .. ..# haveminmax: logi FALSE
.. .. ..# min : num [1:365] Inf Inf Inf Inf Inf ...
.. .. ..# max : num [1:365] -Inf -Inf -Inf -Inf -Inf ...
.. .. ..# unit : chr "K"
.. .. ..# names : chr [1:365] "X1" "X2" "X3" "X4" ...
..# legend :Formal class '.RasterLegend' [package "raster"] with 5 slots
.. .. ..# type : chr(0)
.. .. ..# values : logi(0)
.. .. ..# color : logi(0)
.. .. ..# names : logi(0)
.. .. ..# colortable: logi(0)
..# title : chr "TMIN"
..# extent :Formal class 'Extent' [package "raster"] with 4 slots
.. .. ..# xmin: num 0.5
.. .. ..# xmax: num 348
.. .. ..# ymin: num 0.5
.. .. ..# ymax: num 328
..# rotated : logi FALSE
..# rotation:Formal class '.Rotation' [package "raster"] with 2 slots
.. .. ..# geotrans: num(0)
.. .. ..# transfun:function ()
..# ncols : int 348
..# nrows : int 327
..# crs :Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..# projargs: chr NA
..# history : list()
..# z :List of 1
.. ..$ : int [1:365] 1 2 3 4 5 6 7 8 9 10 ...
str(masked_d97)
Formal class 'RasterBrick' [package "raster"] with 12 slots
..# file :Formal class '.RasterFile' [package "raster"] with 13 slots
.. .. ..# name : chr "/Users/charlesbecker/Desktop/Data/Project Data/Shiny/AVA_WY1997_yearly_stats.nc"
.. .. ..# datanotation: chr "FLT4S"
.. .. ..# byteorder : chr "little"
.. .. ..# nodatavalue : num NaN
.. .. ..# NAchanged : logi FALSE
.. .. ..# nbands : int 365
.. .. ..# bandorder : chr "BIL"
.. .. ..# offset : int 0
.. .. ..# toptobottom : logi TRUE
.. .. ..# blockrows : int 0
.. .. ..# blockcols : int 0
.. .. ..# driver : chr "netcdf"
.. .. ..# open : logi FALSE
..# data :Formal class '.MultipleRasterData' [package "raster"] with 14 slots
.. .. ..# values : logi[0 , 0 ]
.. .. ..# offset : num 0
.. .. ..# gain : num 1
.. .. ..# inmemory : logi FALSE
.. .. ..# fromdisk : logi TRUE
.. .. ..# nlayers : int 365
.. .. ..# dropped : NULL
.. .. ..# isfactor : logi FALSE
.. .. ..# attributes: list()
.. .. ..# haveminmax: logi FALSE
.. .. ..# min : num [1:365] Inf Inf Inf Inf Inf ...
.. .. ..# max : num [1:365] -Inf -Inf -Inf -Inf -Inf ...
.. .. ..# unit : chr ""
.. .. ..# names : chr [1:365] "X1" "X2" "X3" "X4" ...
..# legend :Formal class '.RasterLegend' [package "raster"] with 5 slots
.. .. ..# type : chr(0)
.. .. ..# values : logi(0)
.. .. ..# color : logi(0)
.. .. ..# names : logi(0)
.. .. ..# colortable: logi(0)
..# title : chr "TMIN"
..# extent :Formal class 'Extent' [package "raster"] with 4 slots
.. .. ..# xmin: num 0.5
.. .. ..# xmax: num 348
.. .. ..# ymin: num 0.5
.. .. ..# ymax: num 328
..# rotated : logi FALSE
..# rotation:Formal class '.Rotation' [package "raster"] with 2 slots
.. .. ..# geotrans: num(0)
.. .. ..# transfun:function ()
..# ncols : int 348
..# nrows : int 327
..# crs :Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..# projargs: chr NA
..# history : list()
..# z :List of 1
.. ..$ : int [1:365] 1 2 3 4 5 6 7 8 9 10 ...
str(d03)
Formal class 'RasterBrick' [package "raster"] with 12 slots
..# file :Formal class '.RasterFile' [package "raster"] with 13 slots
.. .. ..# name : chr "/Users/charlesbecker/Desktop/Data/Project Data/Shiny/WY2003_yearly_stats.nc"
.. .. ..# datanotation: chr "FLT4S"
.. .. ..# byteorder : chr "little"
.. .. ..# nodatavalue : num NaN
.. .. ..# NAchanged : logi FALSE
.. .. ..# nbands : int 365
.. .. ..# bandorder : chr "BIL"
.. .. ..# offset : int 0
.. .. ..# toptobottom : logi TRUE
.. .. ..# blockrows : int 0
.. .. ..# blockcols : int 0
.. .. ..# driver : chr "netcdf"
.. .. ..# open : logi FALSE
..# data :Formal class '.MultipleRasterData' [package "raster"] with 14 slots
.. .. ..# values : logi[0 , 0 ]
.. .. ..# offset : num 0
.. .. ..# gain : num 1
.. .. ..# inmemory : logi FALSE
.. .. ..# fromdisk : logi TRUE
.. .. ..# nlayers : int 365
.. .. ..# dropped : NULL
.. .. ..# isfactor : logi FALSE
.. .. ..# attributes: list()
.. .. ..# haveminmax: logi FALSE
.. .. ..# min : num [1:365] Inf Inf Inf Inf Inf ...
.. .. ..# max : num [1:365] -Inf -Inf -Inf -Inf -Inf ...
.. .. ..# unit : chr "K"
.. .. ..# names : chr [1:365] "X1" "X2" "X3" "X4" ...
..# legend :Formal class '.RasterLegend' [package "raster"] with 5 slots
.. .. ..# type : chr(0)
.. .. ..# values : logi(0)
.. .. ..# color : logi(0)
.. .. ..# names : logi(0)
.. .. ..# colortable: logi(0)
..# title : chr "TMIN"
..# extent :Formal class 'Extent' [package "raster"] with 4 slots
.. .. ..# xmin: num 0.5
.. .. ..# xmax: num 348
.. .. ..# ymin: num 0.5
.. .. ..# ymax: num 328
..# rotated : logi FALSE
..# rotation:Formal class '.Rotation' [package "raster"] with 2 slots
.. .. ..# geotrans: num(0)
.. .. ..# transfun:function ()
..# ncols : int 348
..# nrows : int 327
..# crs :Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..# projargs: chr NA
..# history : list()
..# z :List of 1
.. ..$ : int [1:365] 1 2 3 4 5 6 7 8 9 10 ...
system.time(sum(d97[[1:365]]))
user system elapsed
5.569 2.219 8.048
system.time(sum(masked_97[[1:365]]))
user system elapsed
11.887 2.342 14.569
system.time(sum(d03[[1:365]]))
user system elapsed
22.253 1.772 24.879
The most likely difference is that data in your new netCDF file is now compressed differently. Two forms of compression are common with netCDF files:
scale/offset encoding, e.g., to decode from int16 via a formula like scale_factor * values + add_offset.
zlib compression on individual chunks of the array (only supported with netCDF4 files).
If you don't slice or manipulate your variables, xarray will preserve compression setting via the encoding attribute, but this is generally dropped by xarray operations. See the xarray docs on reading/writing encoded data for more details.

How to find out which index is out of bounds in object in R

Although I understand OOP, I've only just encountered them in R
I am using a package from Bioconductor to churn through some genomic data.
The object it creates is called readCounts and typing this into the command gives the following.
QDNAseqReadCounts (storageMode: lockedEnvironment)
assayData: 206391 features, 1 samples
element names: counts
protocolData: none
phenoData
sampleNames: SLX-10457.FastSeqA.BloodDMets_11AF_-AHMMH.s_1.r_1.fq.gz
varLabels: name total.reads used.reads expected.variance
varMetadata: labelDescription
featureData
featureNames: 1:825001-840000 1:840001-855000 ... 22:51165001-51180000 (168063 total)
fvarLabels: chromosome start ... use (9 total)
fvarMetadata: labelDescription
experimentData: use 'experimentData(object)'
Annotation:
I am trying to plot readcounts on a simple xy graph as follows:
plot(readCounts, logTransform=TRUE, ylim=c(-1000, binSize * 15))
However when I do so I get the following error:
Error in sort.int(x, partial = unique(c(lo, hi))) :
index 180 outside bounds
with the traceback() showing:
6: sort.int(x, partial = unique(c(lo, hi)))
5: FUN(newX[, i], ...)
4: apply(copynumber, 2, sdFUN, na.rm = TRUE)
3: .local(x, y, ...)
2: plot(readCounts, logTransform = TRUE, ylim = c(-1000, binSize *
15))
1: plot(readCounts, logTransform = TRUE, ylim = c(-1000, binSize *
15))
so having googled I thought it might be a missing values problem so I tried na.omit(readCounts) but got the same error again but this time setting the out of bounds index as being 207.
I have tried to inspect the data but I can't find anything wrong at row 207 although I'm not really sure which slot this refers to. I really don't know how to debug this. I'm happy to give more info regarding what I'm trying to do but I don't really know how to determine what the problem is with this error in a R object.
When I do str(readCounts) I get:
Formal class 'QDNAseqReadCounts' [package "QDNAseq"] with 7 slots
..# assayData :<environment: 0x13a99ed90>
..# phenoData :Formal class 'AnnotatedDataFrame' [package "Biobase"] with 4 slots
.. .. ..# varMetadata :'data.frame': 4 obs. of 1 variable:
.. .. .. ..$ labelDescription: chr [1:4] NA NA NA NA
.. .. ..# data :'data.frame': 1 obs. of 4 variables:
.. .. .. ..$ name : chr "SLX-10457.FastSeqA.BloodDMets_11AF_-AHMMH.s_1.r_1.fq.gz"
.. .. .. ..$ total.reads : num 0
.. .. .. ..$ used.reads : num 0
.. .. .. ..$ expected.variance: num Inf
.. .. ..# dimLabels : chr [1:2] "sampleNames" "sampleColumns"
.. .. ..# .__classVersion__:Formal class 'Versions' [package "Biobase"] with 1 slot
.. .. .. .. ..# .Data:List of 1
.. .. .. .. .. ..$ : int [1:3] 1 1 0
..# featureData :Formal class 'AnnotatedDataFrame' [package "Biobase"] with 4 slots
.. .. ..# varMetadata :'data.frame': 9 obs. of 1 variable:
.. .. .. ..$ labelDescription: chr [1:9] "Chromosome name" "Base pair start position" "Base pair end position" "Percentage of non-N nucleotides (of full bin size)" ...
.. .. ..# data :'data.frame': 168063 obs. of 9 variables:
.. .. .. ..$ chromosome : chr [1:168063] "1" "1" "1" "1" ...
.. .. .. ..$ start : num [1:168063] 825001 840001 855001 870001 885001 ...
.. .. .. ..$ end : num [1:168063] 840000 855000 870000 885000 900000 915000 930000 945000 960000 975000 ...
.. .. .. ..$ bases : num [1:168063] 100 100 100 100 100 100 100 100 100 100 ...
.. .. .. ..$ gc : num [1:168063] 48 61.8 65.1 65.5 62.6 ...
.. .. .. ..$ mappability: num [1:168063] 58.6 91.5 94.1 93.2 93.9 ...
.. .. .. ..$ blacklist : num [1:168063] 0.727 0 0 0 0 ...
.. .. .. ..$ residual : num [1:168063] -0.0627 0.05036 0.09384 0.00541 -0.00588 ...
.. .. .. ..$ use : logi [1:168063] TRUE TRUE TRUE TRUE TRUE TRUE ...
.. .. .. ..- attr(*, "na.action")=Class 'omit' Named int [1:38328] 1 2 3 4 5 6 7 8 9 10 ...
.. .. .. .. .. ..- attr(*, "names")= chr [1:38328] "1:1-15000" "1:15001-30000" "1:30001-45000" "1:45001-60000" ...
.. .. ..# dimLabels : chr [1:2] "featureNames" "featureColumns"
.. .. ..# .__classVersion__:Formal class 'Versions' [package "Biobase"] with 1 slot
.. .. .. .. ..# .Data:List of 1
.. .. .. .. .. ..$ : int [1:3] 1 1 0
..# experimentData :Formal class 'MIAME' [package "Biobase"] with 13 slots
.. .. ..# name : chr ""
.. .. ..# lab : chr ""
.. .. ..# contact : chr ""
.. .. ..# title : chr ""
.. .. ..# abstract : chr ""
.. .. ..# url : chr ""
.. .. ..# pubMedIds : chr ""
.. .. ..# samples : list()
.. .. ..# hybridizations : list()
.. .. ..# normControls : list()
.. .. ..# preprocessing : list()
.. .. ..# other : list()
.. .. ..# .__classVersion__:Formal class 'Versions' [package "Biobase"] with 1 slot
.. .. .. .. ..# .Data:List of 2
.. .. .. .. .. ..$ : int [1:3] 1 0 0
.. .. .. .. .. ..$ : int [1:3] 1 1 0
..# annotation : chr(0)
..# protocolData :Formal class 'AnnotatedDataFrame' [package "Biobase"] with 4 slots
.. .. ..# varMetadata :'data.frame': 0 obs. of 1 variable:
.. .. .. ..$ labelDescription: chr(0)
.. .. ..# data :'data.frame': 1 obs. of 0 variables
.. .. ..# dimLabels : chr [1:2] "sampleNames" "sampleColumns"
.. .. ..# .__classVersion__:Formal class 'Versions' [package "Biobase"] with 1 slot
.. .. .. .. ..# .Data:List of 1
.. .. .. .. .. ..$ : int [1:3] 1 1 0
..# .__classVersion__:Formal class 'Versions' [package "Biobase"] with 1 slot
.. .. ..# .Data:List of 4
.. .. .. ..$ : int [1:3] 3 1 2
.. .. .. ..$ : int [1:3] 2 26 0
.. .. .. ..$ : int [1:3] 1 3 0
.. .. .. ..$ : int [1:3] 1 2 4

R error while using cbind

I trying to combine 2 vectors using cbind, both vectors are the same size, and I am having an error while i run the code, the vectors are quite big, length = 57605.
final=cbind (counts1,tx_by_gene)
> > Error: cannot allocate vector of size 225 Kb R(473,0xa0cb8540) malloc: *** mmap(size=233472) failed (error code=12)
> *** error: can't allocate region
> *** set a breakpoint in malloc_error_break to debug R(473,0xa0cb8540) malloc: *** mmap(size=233472) failed (error code=12)
> *** error: can't allocate region
> *** set a breakpoint in malloc_error_break to debug
Can anyone help me why am I having this error? or some other way of combining the 2 vectors?
thank you
> str(counts1) = int [1:57605] 0 0 0 0 0 0 0 0 0 0 ...
>str(tx_by_gene)
> Formal class 'GRangesList' [package "GenomicRanges"] with 5 slots ..# partitioning :Formal class 'PartitioningByEnd' [package
> "IRanges"] with 5 slots .. .. ..# end : int [1:57605] 3 5
> 12 17 27 36 42 46 58 60 ... .. .. ..# NAMES : chr [1:57605]
> "ENSG00000000003" "ENSG00000000005" "ENSG00000000419"
> "ENSG00000000457" ... .. .. ..# elementMetadata: NULL .. .. ..#
> elementType : chr "integer" .. .. ..# metadata : list()
> ..# unlistData :Formal class 'GRanges' [package "GenomicRanges"]
> with 7 slots .. .. ..# seqnames :Formal class 'Rle' [package
> "IRanges"] with 5 slots .. .. .. .. ..# values : Factor w/
> 93 levels "chr1","chr2",..: 8 20 1 6 1 8 6 3 7 13 ... .. .. .. ..
> ..# lengths : int [1:41694] 5 7 30 18 21 6 2 9 43 23 ... ..
> .. .. .. ..# elementMetadata: NULL .. .. .. .. ..# elementType :
> chr "ANY" .. .. .. .. ..# metadata : list() .. .. ..# ranges
> :Formal class 'IRanges' [package "IRanges"] with 6 slots .. .. .. ..
> ..# start : int [1:191891] 99883667 99887538 99888439
> 99839799 99848621 49551404 49551404 49551404 49551433 49551482 ...
> .. .. .. .. ..# width : int [1:191891] 8137 4149 6550 15084
> 3908 23684 23684 23689 10966 23577 ... .. .. .. .. ..# NAMES
> : NULL .. .. .. .. ..# elementMetadata: NULL .. .. .. .. ..#
> elementType : chr "integer" .. .. .. .. ..# metadata :
> list() .. .. ..# strand :Formal class 'Rle' [package
> "IRanges"] with 5 slots .. .. .. .. ..# values : Factor w/ 3
> levels "+","-","*": 2 1 2 1 2 1 2 1 2 1 ... .. .. .. .. ..# lengths
> : int [1:28670] 3 2 12 10 9 6 16 2 13 8 ... .. .. .. .. ..#
> elementMetadata: NULL .. .. .. .. ..# elementType : chr "ANY"
> .. .. .. .. ..# metadata : list() .. .. ..# seqlengths :
> Named int [1:93] 249250621 243199373 198022430 191154276 180915260
> 171115067 159138663 155270560 146364022 141213431 ... .. .. .. ..-
> attr(*, "names")= chr [1:93] "chr1" "chr2" "chr3" "chr4" ... .. ..
> ..# elementMetadata:Formal class 'DataFrame' [package "IRanges"] with
> 6 slots .. .. .. .. ..# rownames : NULL .. .. .. .. ..#
> nrows : int 191891 .. .. .. .. ..# elementMetadata: NULL
> .. .. .. .. ..# elementType : chr "ANY" .. .. .. .. ..# metadata
> : list() .. .. .. .. ..# listData :List of 2 .. .. .. .. ..
> ..$ tx_id : int [1:191891] 93738 93739 93740 93736 93737 175481
> 175482 175480 175483 175484 ... .. .. .. .. .. ..$ tx_name: chr
> [1:191891] "ENST00000373020" "ENST00000496771" "ENST00000494424"
> "ENST00000373031" ... .. .. ..# elementType : chr "ANY" .. ..
> ..# metadata : list() ..# elementMetadata:Formal class
> 'DataFrame' [package "IRanges"] with 6 slots .. .. ..# rownames
> : NULL .. .. ..# nrows : int 57605 .. .. ..#
> elementMetadata: NULL .. .. ..# elementType : chr "ANY" .. ..
> ..# metadata : list() .. .. ..# listData : list() ..#
> elementType : chr "GRanges" ..# metadata : list()
The object tx_by_gene isn't a vector. You can check using the is.vector function
is.vector(counts1)
is.vector(tx_by_gene)
Of course, there could be method defined so that the two objects can be combined
Those vectors should not be too big for R. You probably used up a lot of memory before the cbind() operation. Look at what objects you currently have with ls() and delete those you don't need any more with rm().

Resources