I have a small fasta file of DNA sequences which looks like this:
>NM_000016 700 200 234
ACATATTGGAGGCCGAAACAATGAGGCGTGATCAACTCAGTATATCAC
>NM
inspired by sgibb's answer above, I answer the first question as follow:
#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)
}
Example:
mydf = fasta2dataframe(myFastaFile.fasta)