This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to extract specific samples (by ID) from Fasta file to new fasta file in R

I have a question concerning the extraction of sequences from a multy fasta file with sequence headers. I have been playing around and been looking all over the internet to find a solution for this problem, but surprisingly, nothing really matches what I want to do.

code r

1 answer

#/ In R using Biostrings:
library(Biostrings)

fa <- readDNAStringSet("~/foo.fa")
> fa
DNAStringSet object of length 3:
    width seq                names               
[1]     4 ATCG               chr1
[2]    12 GGATGTGTGTCA       chr2
[3]     6 GTAGCT             chr3

#/ Say we want chr2 and chr3:
fa_new <- fa[c("chr2", "chr3")]
> fa_new
DNAStringSet object of length 2:
    width seq                names               
[1]    12 GGATGTGTGTCA       chr2
[2]     6 GTAGCT             chr3

#/ write back to a file:
writeXStringSet(fa_new, "~/out.fa")

Log in to answer this question.