Expression using greek letters in R
3
0
Entering edit mode
6.4 years ago

Hello,

I'm trying to detect greek letters in a string in R and convert my string to an expression. I know there are multiple threads about this (https://stackoverflow.com/questions/6044800/adding-greek-character-to-axis-title). But I can't find a way to paste multiple greek letters at once...

My data :

str <- "myrangammadomsequenmuce15874withsomephigreek2874lettergamma"

I want to get something like this :

expression(paste("myran", gamma, "domsequen", mu, "ce15874withsome", phi, "greek2874letter", gamma)

I tried to split my string using multiple delimiters but when I want to collapse I don't know which was the delimiter used.

Thanks !

R • 11k views
ADD COMMENT
0
Entering edit mode

@Bastien: Pure R questions like this are best asked elsewhere.

Edit: Perhaps there is a bioinformatics angle. Are you trying to label an axis with a greek character?

ADD REPLY
0
Entering edit mode

I'm trying to display labels for IgH locus of mm10 (gamma3, gamma1, mu etc...)

I have GRanges with these locus as name and I want to create an expression vector with it

ADD REPLY
0
Entering edit mode

Probably would be better if you could share example GRanges object, and expected output.

ADD REPLY
0
Entering edit mode

Why do you need the entire GRanges ?

In my GRanges, first line I have as name : myrangammadomsequenmuce15874withsomephigreek2874lettergamma

I want to create an expression with that and append this expression to a vector of expression...etc

Then second line, I take the name, then I append the vector of expression.

I know how to loop over my GRanges and append a vector. I'm just stuck at expression creation

ADD REPLY
0
Entering edit mode

OK, still wondering how did you end up with that string.

ADD REPLY
0
Entering edit mode

I'll ask users to input their own string. As everyone know here, user are unpredictable. I expect this string will be the most wtf string I will got from my users :)

ADD REPLY
0
Entering edit mode

How did you come up with that string, where is it from?

ADD REPLY
0
Entering edit mode

my str string is pure training but covers all cases.

ADD REPLY
2
Entering edit mode
6.4 years ago

After 1 day I've found a really dirty way !

library(stringr)
str <- "myrangammadomsequenmuce15874withsomephigreek2874lettergamma"
str <- gsub("gamma", "'~gamma~'", str)
str <- gsub("phi", "'~phi~'", str)
str <- gsub("mu", "'~mu~'", str)

str
#[1] "myran'~gamma~'domsequen'~mu~'ce15874withsome'~phi~'greek2874letter'~gamma~'"

if (substr(str,1,1) == "'"){
    str <- paste0("expression(",substr(str,3,nchar(str)))
} else{
    str <- paste0("expression('",str)
}
if (str_sub(str,-1,-1) == "'"){
    str <- paste0(substr(str,1,nchar(str)-2),")")
} else{
    str <- paste0(str,"')")
}
result <- eval(parse(text=str))

result
#expression("myran" ~ gamma ~ "domsequen" ~ mu ~ "ce15874withsome" ~ phi ~ "greek2874letter" ~ gamma)
ADD COMMENT
1
Entering edit mode
6.4 years ago
zx8754 12k

I don't have the full answer, but below should get you started:

library(stringr)

x <- "myrangammadomsequenmuce15874withsomephigreek2874lettergamma"
greek <- "gamma|mu|phi"

str_split(x, greek)
# [[1]]
# [1] "myran"           "domsequen"       "ce15874withsome" "greek2874letter" ""  
str_extract_all(x, greek)
# [[1]]
# [1] "gamma" "mu"    "phi"   "gamma"
str_locate_all(x, greek)
# [[1]]
#      start end
# [1,]     6  10
# [2,]    20  21
# [3,]    37  39
# [4,]    55  59
ADD COMMENT
0
Entering edit mode

I got something like this for each greek letters but I can't figure it out how to create my expression in once. Because I can't iterate on my paste for each greek letter, then, at the end create the expression. Or greek letters will be display as normal string.

ADD REPLY
1
Entering edit mode
6.4 years ago

can you get greek letters converted to uTF(8) in R (from english words to greek to alphabets)? If so, you can use lookaround in R.

>gamma=intToUtf8(0x03B3)
>mu=intToUtf8(0x03BC)
>phi=intToUtf8(0x03D5)
> test1=paste0("myran", gamma, "domsequen", mu, "ce15874withsome", phi, "greek2874letter", gamma)
> test1
[1] "myranγdomsequenμce15874withsomeϕgreek2874letterγ"
> strsplit(test1,"(?<=\\W)(?=\\w)|(?<=\\w)(?=\\W)|(?<=\\W)(?=\\W)", perl = T)
[[1]]
[1] "myran"           "γ"               "domsequen"       "μ"               "ce15874withsome"
[6] "ϕ"               "greek2874letter" "γ"
ADD COMMENT
0
Entering edit mode

Nice one, problem is that I don't have test1 as you wrote it :

test1=paste0("myran", gamma, "domsequen", mu, "ce15874withsome", phi, "greek2874letter", gamma)

I just have my string :

test1 <- "myrangammadomsequenmuce15874withsomephigreek2874lettergamma"

I need to detect my greek letters word in my string (maybe as zx8754 suggest above), to transform them into "real" greek letters then after to paste the result

ADD REPLY

Login before adding your answer.

Traffic: 1816 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6