This is a test version of Biostars. For the public version, visit https://www.biostars.org.
GRanges R

Hi, I have a small question

When I try to run this code, everything seems to be good :

# load refrence fasta file
dna <- readDNAStringSet('hg19.fasta')
# Find chromosome
dna.chr <- dna[grep('11',names(dna))]
names(dna.chr) <- '11'
# set chromosome poriostion
gr <- GRanges('11: 108121429')
print(gr)

output :

> > GRanges object with 1 range and 0 metadata columns:
> >       seqnames    ranges strand
> >          <Rle> <IRanges>  <Rle>   [1]       11 108121429      *
> 
>   -------   seqinfo: 1 sequence from an unspecified genome; no
> seqlengths

But when I try to change the code to this by adding a variable:

# load refrence fasta file
dna <- readDNAStringSet('hg19.fasta')
# Find chromosome
dna.chr <- dna[grep('11',names(dna))]
names(dna.chr) <- '11'
# set chromosome poriostion
i <- 108121429
gr <- GRanges('11:i')
print(gr)

output:

I get this error

> Error in asMethod(object) :    The character vector to convert to a
> GRanges object must contain strings of the form   "chr:start-end" or
> "chr:start-end:strand", with end >= start - 1, or "chr:pos" or  
> "chr:pos:strand". For example: "chr1:2501-2900", "chr1:2501-2900:+",
> or "chr1:740". Note   that ".." is a valid alternate start/end
> separator. Strand can be "+", "-", "*", or   missing.

The problem is that I need to have a variable inside Granges to make a boucle. Anyone have a solution? Thank you very much

chromosome granges position r

1 answer

'11:i' is a literal string, meaning that i won't be substituted with 108121429 and will be just the letter 'i'. Instead you need to programmatically construct the string.

GRanges(paste0("11:", i))

Thank's a lot !! That solved the problem

Log in to answer this question.