将FASTA读入数据帧并提取FASTA文件的子序列

时间:2014-01-21 16:23:33

标签: r subset bioinformatics fasta

我有一个小的 fasta DNA序列文件,如下所示:

>NM_000016 700 200 234
ACATATTGGAGGCCGAAACAATGAGGCGTGATCAACTCAGTATATCAC

>NM_000775 700 124 236
CTAACCTCTCCCAGTGTGGAACCTCTATCTCATGAGAAAGCTGGGATGAG

>NM_003820 700 111 222
ATTTCCTCCTGCTGCCCGGGAGGTAACACCCTGGACCCCTGGAGTCTGCA

问题:

1)如何将这个fasta文件读入R作为数据帧,其中每一行是一个序列记录,第一列是refseqID,第二列是序列。

2)如何在(开始,结束)位置提取子序列?

NM_000016 1  3 #"ACA"
NM_000775 2  6 #"TAACC"
NM_003820 3  5 #"TTC"

3 个答案:

答案 0 :(得分:13)

您应该查看Biostrings包。

library("Biostrings")

s = readDNAStringSet("nm.fasta")
subseq(s, start=c(1, 2, 3), end=c(3, 6, 5))

答案 1 :(得分:7)

library("Biostrings")

fastaFile <- readDNAStringSet("my.fasta")
seq_name = names(fastaFile)
sequence = paste(fastaFile)
df <- data.frame(seq_name, sequence)

答案 2 :(得分:2)

受上面sgibb的回答启发,我回答第一个问题如下:

#read fasta file into R as a dataframe: 1st column as "RefSeqID", 2nd column as "seq"

library("Biostrings")
fasta2dataframe=function(fastaFile){
s = readDNAStringSet(fastaFile)
RefSeqID = names(s)
RefSeqID = sub(" .*", "", RefSeqID) 
#erase all characters after the first space: regular expression matches a space followed by any sequence of characters and sub replaces that with a string having zero  characters 

for (i in 1:length(s)){
seq[i]=toString(s[i])
}

RefSeqID_seq=data.frame(RefSeqID,seq)
return(RefSeqID_seq)
}

示例:

mydf = fasta2dataframe(myFastaFile.fasta)