Can't get end value of an IRange in R/Bioconductor - r

I am new to the IRanges package and am having trouble getting the end value of an IRange. I am able to get the start and width values with no problem, which has me a bit baffled, and my case/spelling of end match the header line. Has anyone else run into this or can please spot what I am doing wrong? Thanks and it is much appreciated!
library(IRanges)
> test=IRanges(100645,100664)
> test
IRanges of length 1
start end width
[1] 100645 100664 20
> test#start
[1] 100645
> test#width
[1] 20
> test#end
Error: no slot of name "end" for this object of class "IRanges"

The easiest manner to access the fields of an IRange object is using the helper functions: start(),end() and width(). These will return a vector with all the elements of the corresponding column.

No experience with the package, but based on ?"class:Ranges":
end(test$ranges[1])
It would also help in the future to provide reproducible sample data.

Related

Reading an attribute value from a NetCDF4 file with nested groups

This should be trivial but I can't for the life of me figure out how to do this: I am trying to read an attribute value from a NetCDF4 file in R. Now, my NetCDF4 file (uploaded here) is fairly complex, i.e. it contains nested groups.
I would like to extract the value of the attribute called gml:posList from the group METADATA/EOP_METADATA/om:featureOfInterest/eop:multiExtentOf/gml:surfaceMembers/gml:exterior using R. I am not sure if it matters in this context, but this group does not contain any variables, only metadata attributes.
I have tried the following
library(ncdf4)
fid = nc_open('S5P_NRTI_L2__NO2____20180728T130136_20180728T130636_04089_01_010100_20180728T140302.nc')
ncatt_get(fid, varid=0, attname='METADATA/EOP_METADATA/om:featureOfInterest/eop:multiExtentOf/gml:surfaceMembers/gml:exterior/gml:posList', verbose=TRUE)
but this returns
[1] "ncatt_get: entering"
[1] "ncatt_get: is a global att"
[1] "ncatt_get: calling ncatt_get_inner for a global att"
[1] "ncatt_get_inner: entering with ncid= 65536 varid= -1 attname= METADATA/EOP_METADATA/om:featureOfInterest/eop:multiExtentOf/gml:surfaceMembers/gml:exterior/gml:posList"
[1] "ncatt_get_inner: about to call R_nc4_inq_att"
[1] "ncatt_get_inner: R_nc4_inq_att returned with error= -43 type= -1"
$`hasatt`
[1] FALSE
$value
[1] 0
presumably indicating that it cannot find the attribute and I assume that I got the path wrong somehow.
So my question is, how do I need to specify the path to an attribute that is a) in a nested group and b) not linked to a specific variable, such that ncatt_get() can find the attribute and return its value?
By the way, just for reference, in Matlab the command
test = ncreadatt(file, 'METADATA/EOP_METADATA/om:featureOfInterest/eop:multiExtentOf/gml:surfaceMembers/gml:exterior', 'gml:posList')
works fine, so I know it's not an issue with the file.
Any hints would be highly appreciated!

In R, how do I access information in a data set using a variable after the $?

I'm using Bioconductor to look at GO terms. I can use for instance GOBPANCESTOR$"GO:0060412" to get all the ancestral terms to 0060412. However, I need to loop through many possible terms. However, I can't seem to get GOBPANCESTOR$ to accept a variable after the $.
> GOBPANCESTOR$"GO:0060412"
[1] "GO:0003007" "GO:0003205" "GO:0003206" "GO:0003231" "GO:0003279" "GO:0003281" "GO:0007275" "GO:0009653" "GO:0009887" "GO:0007507" "GO:0008150"
[12] "GO:0032501" "GO:0032502" "GO:0044699" "GO:0044707" "GO:0044767" "GO:0048513" "GO:0048731" "GO:0048856" "GO:0060411" "GO:0072358" "GO:0072359"
[23] "all"
But...
> mygoterm <- "GO:0060412"
> GOBPANCESTOR$mygoterm
NULL
Also tried using paste to no avail. I feel like I must be misunderstanding something integral about the way R works...
Thanks for your help!

window function R code

fine people of stackoverflow. I have become trapped on a rather simple part of my program and was wondering if you guys could help me.
library(nonlinearTseries)
tt<-c(0,500,1000)
mm<-rep(0,2)
for (j in 1:2){mm[j]=estimateEmbeddingDim(window(rnorm(1000), start=tt[j],end=tt[j+1]), number.points=(tt[j+1]-tt[j]),do.plot=FALSE)}
Warning message:
In window.default(rnorm(1000), start = tt[j], end = tt[j + 1]) :
'start' value not changed
If I plug in the values directly (tt[1], tt[2], tt[3]), it works but I also get a warning
estimateEmbeddingDim(window(rnorm(1000), start=tt[1],end=tt[2]), number.points=(tt[2]-tt[1]),do.plot=FALSE)
[1] 9
Warning message:
In window.default(rnorm(1000), start = tt[1], end = tt[2]) :
'start' value not changed
Thanks, Matt.
The problem seems to be with the
window(rnorm(1000), start=tt[j],end=tt[j+1])
lines. First of all, window is only meant to be used with a time series object (class=="ts"). In this case, rnorm(1000) simply returns a numeric vector, there are no dates associated with this object. So i'm not sure what you think this function does. Did you only want to extract the values that were between 0-500 and 500-1000? If so that seems a bit because with a standard normal variable, the max of 1000 samples isn't likely to be much over 4 let alone 500.
So be sure to use a proper "ts" object with dates and everything to get this to work.

Strange behaviour while accessing elements in an XTS

I have an XTS that is part of a list returns$sig and from that XTS, I pull out a set of elements based on some conditions and store the Index in a variable tstart.
> tstart <- index(returns$sig[which(returns$sig != lag(returns$sig,1) & returns$sig != 0)])
> length(tstart)
[1] 599
When I try and access the returns$sig XTS again with the dates in tstart, I get a XTS with a different length:
> length(returns$sig[tstart])
[1] 478
It should return something with length 599. If I try and access the XTS in a different way, I do get something of the same length:
> length(returns$sig[match(tstart,index(returns$sig))])
[1] 599
Spent hours on this and haven't found a resolution. Is there something obvious that I am doing wrong? And to make matters worse, I swear that length(returns$sig[tstart]) returned 599 yesterday and everything was working fine.
Sorry for the poorly formed question. I couldn't reproduce the error with a short example and didn't want to post all my code. I have finally figured out the issue. It seems to be related to a bug some ppl (myself included) have been reporting with XTS. All I needed to do was to specify a timezone for my system with Sys.setenv(TZ = "GMT").
For those that are interested, I am using xts_0.8-8.

In R, how do I set an S4 class based on another object's class

I need to create an object of type ShortReadQ from Bioconductor's ShortRead library.
ShortReadQ 'signature(sread = "DNAStringSet", quality =
"QualityScore", id = "BStringSet")'
The quality slot needs to be an object inheriting from QualityScore, of which I can easily determine from another ShortReadQ object that I wish to emulate.
> class(quality(anotherObject))
[1] "SFastqQuality"
attr(,"package")
[1] "ShortRead"
What is the best way to use that information ("SFastqQuality") in the contructor argument?
newObject<-ShortReadQ(sread=...,
quality=SFastqQuality(...),
id=...)
Does this do what you want?
quality = new(class(old.quality.obj)[[1]]))
You might want the get function:
a <- get(class(object))
a(...)
thanks for your responses. they lead me to a solution that works
newObject<-ShortReadQ(sread=...,
quality=new(Class=class(quality(anotherObject)),theFirstParameter=...),
id=...)

Resources