# How to obtain amino acid context at a given position

I'm trying to find the amino acid context around a particular position in a protein (5 aa amino acid window on each side).

I used to have some code that works and then biomaRt stopped working. I think some of these issues would be fixed in later versions of biomaRt, but I would have to install a new version of R, which would require me to install a new version of my OS.

require('biomaRt')
require('Biostrings')
hs_ensembl_mart =
useMart(biomart="ENSEMBL_MART_ENSEMBL",dataset="hsapiens_gene_ensembl")

gene_names=c('KRAS')
aa_affected_loc=12

biomart_query_results=getBM(attributes=c('hgnc_symbol', 'entrezgene','coding'),
filters = 'hgnc_symbol',
values = gene_names,
mart = hs_ensembl_mart)

preceding_peptide= substr(as.character(translate(DNAString(biomart_query_results[1,'coding']))),aa_affected_loc-5,aa_affected_loc-1)

trailing_peptide= substr(as.character(translate(DNAString(biomart_query_results[1,'coding']))),aa_affected_loc+1,aa_affected_loc+5)


Does anyone have a workaround for this?

• You can install a new version of R without a new OS. Which problem are you having? When I try your code the problem is that j is not defined, if I define it with a number I get unable to find an inherited method for function ‘XString’ for signature ‘"integer"’ (and I'm using the latest versions of Bioconductor and R)
– llrs
Dec 25 '18 at 16:19
• Sorry, I had extracted that line from a loop and forgot to change the j. Should work now. I have a 5 year old computer and the OSX version I was using wouldn't allow upgrading R. I ended up upgrading my OSX version and reinstalling R. I'll see if it works now. Dec 25 '18 at 18:52
• It doesn't work, I said so in my previous comment, see the error, and could you explain what are you doing in this long line?
– llrs
Dec 25 '18 at 19:35
• You are simply using a substr command, which is present in Perl and there is a very cool way for doing this in python. It is honestly easier to code this directly.
– M__
Dec 26 '18 at 13:25
• From what I heard even learning R is kinda waste of time, much less using it for simple scripts. Jan 3 '19 at 23:24

It turned out that I needed to reinstall OSX to upgrade my R version and biomaRt install.

You could also save your "biomart_query_result" object after the first time it is generated. Then you can re-use it without having to depend on the biomaRt API functionality.

Would this be useful to you?


library(EnsDb.Hsapiens.v86)
library(ensembldb)
mygene <- genes(EnsDb.Hsapiens.v86, filter= GenenameFilter("KRAS"))


# to get the protein info and protein sequence for each transcript

proteins(EnsDb.Hsapiens.v86, filter= GenenameFilter("KRAS"))

Also have a look at functions proteinToTranscript proteinToGenome

> x<- proteins(EnsDb.Hsapiens.v86, filter= GenenameFilter("KRAS"), return.type = "AAStringSet")
> x
A AAStringSet instance of length 4
width seq                                    names
[1]   188 MTEYKLVVVGAGGVGKSA...KDGKKKKKKSKTKCVIM ENSP00000308495
[2]    75 MTEYKLVVVGAGGVGKSA...KDGKKKKKKSKTKCVIM ENSP00000452512
[3]   189 MTEYKLVVVGAGGVGKSA...KEEKTPGCVKIKKCIIM ENSP00000256078
[4]    43 MTEYKLVVVGAGGVGKSA...HFVDEYDPTIEVLLSSM ENSP00000451856



There are also functions exons, exonsBy, transcriptsBy, cdsBy, fiveUTRsByTranscript and threeUTRsByTranscript. These functions return a GenomicRanges object and inherit all of the methods in that package too.

x<-  genes(EnsDb.Hsapiens.v86, filter= GenenameFilter("KRAS"))
start(x)
end(x)
strand(x)
length(x)
flank(x)


You can manipulate the sequence selected as an IRanges object

ranges(x)   # gives an IRanges object
narrow(ranges(x), start=10, end=20)  # select the 10th position

> ranges(x)
IRanges object with 1 range and 0 metadata columns:
start       end     width
<integer> <integer> <integer>
ENSG00000133703  25204789  25250936     46148
> narrow(ranges(x), start=10, end=19)
IRanges object with 1 range and 0 metadata columns:
start       end     width
<integer> <integer> <integer>
ENSG00000133703  25204798  25204807        10


It is worth having a look at these vignettes. Let me know if you've questions or post to the Bioconductor support forum ;-)