16

I have a small fasta file of DNA sequences which looks like this:

>NM_000016 700 200 234
ACATATTGGAGGCCGAAACAATGAGGCGTGATCAACTCAGTATATCAC

>NM_000775 700 124 236
CTAACCTCTCCCAGTGTGGAACCTCTATCTCATGAGAAAGCTGGGATGAG

>NM_003820 700 111 222
ATTTCCTCCTGCTGCCCGGGAGGTAACACCCTGGACCCCTGGAGTCTGCA

Questions:

1) How can I read this fasta file into R as a dataframe where each row is a sequence record, the 1st column is the refseqID and the 2nd column is the sequence.

2) How to extract subsequence at (start, end) location?

NM_000016 1  3 #"ACA"
NM_000775 2  6 #"TAACC"
NM_003820 3  5 #"TTC"
zx8754
  • 52,746
  • 12
  • 114
  • 209
Paul.j
  • 794
  • 2
  • 8
  • 17

4 Answers4

21

You should have a look at the Biostrings package.

library("Biostrings")

s = readDNAStringSet("nm.fasta")
subseq(s, start=c(1, 2, 3), end=c(3, 6, 5))
sgibb
  • 25,396
  • 3
  • 68
  • 74
  • 1
    This function is masking the repeats. How do we prevent that? – DarkRose Jul 12 '16 at 17:31
  • Can you give a little example? Maybe ask on https://support.bioconductor.org/ for specific use cases. – sgibb Jul 15 '16 at 09:26
  • Oh, thank you :) I solved the problem using Python. Just treated them as strings. I wanted to delete those sequences which had repeats. So, I used string.isupper() to check that. – DarkRose Jul 18 '16 at 07:58
  • I kind of have the same question regarding the masking: `DNAString("TATCAAATACTCAAGCACtaaggaaacaggaaaatct")` will return `37-letter "DNAString" instance seq: TATCAAATACTCAAGCACTAAGGAAACAGGAAAATCT` Why is `aaggaaacaggaaaatct` not staying in lowercase? – M. Beausoleil Nov 10 '17 at 18:18
18
library("Biostrings")

fastaFile <- readDNAStringSet("my.fasta")
seq_name = names(fastaFile)
sequence = paste(fastaFile)
df <- data.frame(seq_name, sequence)
lblum
  • 203
  • 2
  • 5
  • 2
    could you please explain your answer – Prudhvi Sep 24 '15 at 21:18
  • @Prudhvi not OP but that looks like a function that returns a name vector. So the first step was to the name which is >xxx part of the fasta , 2nd step was get sequence and then last was to put that all into a dataframe. Could had done it in one step though but this is much easier to read. ```data.frame(name = names(fastaFile), seq =paste(fastaFile))``` – Ahdee Sep 03 '22 at 04:21
5

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)
user1981275
  • 13,002
  • 8
  • 72
  • 101
Paul.j
  • 794
  • 2
  • 8
  • 17
2

you can use phylotools to read your FASTA file into dataframe

library(phylotools)
fasta.df = read.fasta("file.fasta")

for subseting the sequences, I like @sgibb answer.

Tal Zaquin
  • 63
  • 5