This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Iterate over Rangeddata in R with a for loop

Hey I have a dataframe (sample.annotations) that I get some paths for my datasets which are: peaks.data ( GRange object with peaks coordinations from Chip-seq experiments) and annotation.data (GRanges with gene annotations from ensembl). I can successful use the annotatePeakInBatch() function in R through 'ChIPpeakAnno' package.

As I have many datasets I want to run it iteratively (trying to do it with a for loop) and fill in an empty list with the RangedData objects that come out of the annotatePeakInBatch() function.I cant do that in R as a relatively new programmer, but the code is correct because I get annotations if I run it for specific datasets. My problem is somewhere on the iteration path!

Anyone can pin point any mistake on the following code? I keep on getting the following mistake:

Error in peaks2genes[[i]] <- annotatePeakInBatch(peaksRD, AnnotationData = annoRD) : 
  invalid type/length (S4/0) in vector allocation

My code:

library(ChIPpeakAnno)

peaks2genes<- NULL
for(i in 1:nrow(samples.annotations)) {
  peaks.data<- readRDS(samples.annotations$path[i])
  chr<- peaks.data[,samples.annotations$peak.coord.chr.col[i]]
  chr<- gsub("chr","",chr, perl=T,ignore.case=T)
  start<-peaks.data[,samples.annotations$peak.coord.start.col[i]]
  end<- peaks.data[,samples.annotations$peak.coord.end.col[i]]
  names <- paste("Site",1:nrow(peaks.data),sep="")
  peaksRD <- RangedData(space = chr,ranges=IRanges(start = start, end = end,names= names))
  annotation.data <- readRDS(samples.annotations$GeneAnnotationsPath[i])
  annotation.dataframe<-as.data.frame(annotation.data)
  annoRD <- RangedData(space = annotation.dataframe$seqnames,ranges = IRanges(start = annotation.dataframe$start, end = annotation.dataframe$end, names = annotation.dataframe$gene_id),strand = annotation.dataframe$strand)
  peaks2genes[[i]] <- annotatePeakInBatch(peaksRD,AnnotationData = annoRD)
}
chip-seq

1 answer

I imagine that something like:

peaks2genes <- list()

at the beginning would be helpful

Thanks, that was it (I feel kind of dumb but thanks again)

Log in to answer this question.