This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Split fasta string into variables by using R

Hi All,

How do you split fasta string into variables by using R?

For an example:

Input fasta string:

">VFG000871(gb|NP_757239) (fimB) Type 1 fimbriae Regulatory protein fimB [Type 1 fimbriae (VF0221)] [Escherichia coli CFT073]"

Goal to get this:

[1] ">VFG000871"   "(gb|N_757239)"   "(fimB)"   "[Type 1 fimbriae (VF0221)]"   "[Escherichia coli CFT073]"

I'm struggling to split the string by using sub() because of '(' ... It would show the error that ')' is missing. I have only to make ">VFG000871" as variable. Do you have any better suggestion for me?

r fasta

1 answer

The problem is that you have no common delimiter, therefore one has to address every delimiter itself:

Split_String <- function(my.string){
  first  <- strsplit(my.string, split="\\(")[[1]][1]
  second <- paste("(", strsplit(my.string, split="\\(")[[1]][2], sep="")
  third  <- strsplit(my.string, split=" ")[[1]][2]
  fourth <- paste("[", trimws(strsplit(my.string, split = "\\[")[[1]][2], "right"), sep="")
  fifth  <- paste("[", strsplit(my.string, split="\\[")[[1]][3], sep="")

  return(c(first, second, third, fourth, fifth))
}

Given that you have multiple strings like this, you can use the function to loop over a data frame or whatever container you have.

Thank you for your input

Log in to answer this question.