I have multiple fasta sequences within a text file, looking like this:
>header1
ACTGACTG
>header2
ATGCATGC
...
I would like to apply a function all of the sequences at once. Is there a function achieving this?
Every answer will be appreciated.
I have multiple fasta sequences within a text file, looking like this:
>header1
ACTGACTG
>header2
ATGCATGC
...
I would like to apply a function all of the sequences at once. Is there a function achieving this?
Every answer will be appreciated.
 
    
    The answer is simple =  sapply(). If you want to apply function e.g. to a list of some objects, you use sapply() method, which is a map() function (you may know this from python). Here is an example:
v <- sample(1:100, 10)
> v
 [1] 92 69 87 42  7 33 51 62 26 80
f <- function(x){
+     # T if even else F
+     return(!x %% 2)
+ }
> sapply(v, FUN = f)
 [1]  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE
Example with DNA:
> library('dplyr')
> v <- c('ATGCTAGCT', 'GTGTACGTAC')
> sapply(v, FUN = function(dna){
+     return(dna %>% tolower)
+ })
   ATGCTAGCT   GTGTACGTAC 
 "atgctagct" "gtgtacgtac" 
