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.
• 1,455 views
•
link
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")
• 0 views
•
link
Log in to answer this question.
Also for non-R solutions: How To Extract A Sequence From A Big (6Gb) Multifasta File ?