Entering edit mode
Hi there, I have a problem with change IRanges object to GRanges.
> test_bam_Iranges IRangesList of length 1 [[1]] IRanges object with 8 ranges and 0 metadata columns: start end width <integer> <integer> <integer> [1] 14404 14829 426 [2] 14970 15038 69 [3] 15796 15947 152 [4] 16607 16765 159 [5] 16858 17742 885 [6] 17915 18061 147 [7] 18268 18366 99 [8] 195263 195426 164
I got an IRanges object from BAM file (from CIGAR). And I want to make it GRanges and it causes error. test_bam_Granges <- GRanges(seqnames = test_bam_seqname, ranges = test_bam_Iranges2, strand = test_bam_strand) Error in as(ranges, "IRanges") : no method or default for coercing “CompressedIRangesList” to “IRanges”
Can anyone had same problem or can anyone give me advice for this error?
Thanks in advance
You actually have a RangesList, not a Ranges. How did you end up with one of those? Maybe you could show the code for importing the object from the BAM.
I solved the problem by changing into data frame and making Granges object.
bam <- readGAlignments("file.bam")
bam2 <- as.data.frame(bam)
# data frame to GRanges (Test)
bam2.GR <- GRanges(seqnames = bam2$seqnames, ranges=IRanges(start = bam2$start, end = bam2$end),
strand = bam2$strand, cigar = bam2$cigar, qwidth = bam2$qwidth )
head(bam2.GR)
head(cigarOpTable(bam2.GR$cigar))
test_bam <- bam2.GR[39258]
test_bam2 <- bam2.GR[39258]
test_bam3 <- bam2.GR[39268]
test_bam4 <- bam2.GR[39268]
test_bam_pos <- start(test_bam)
test_bam_cigar <- test_bam$cigar
test_bam_seqname <- seqnames(test_bam)
test_bam_strand <- strand(test_bam)
test_bam_Iranges <- extractAlignmentRangesOnReference(test_bam_cigar, pos = test_bam_pos)
test_bam_Iranges2 <- as.data.frame(test_bam_Iranges)
test_bam_Granges <- GRanges(seqnames = test_bam_seqname, ranges = IRanges(start = test_bam_Iranges2$start, end = test_bam_Iranges2$end), strand = test_bam_strand)
Perhaps all you needed to do was extract the first (and only) element with
test_bam_Iranges[[1]]
. I also wonder whether you could have got what you wanted by replacing all of that withgrglist(bam)
.