How to convert fasta files to nexus files in R

Both .fasta and .nexus files are used to save the sequences information in phylogenetics and they are both widely used. However, for some software like MrBayes, it only reads the .nexus file. As a result, if you have a .fasta file at hand, you will need to be able to convert between different formats for files. R provides an easy way to achieve this.  We can use read.fasta() function in “seqinr” package to read the data first and then use write.nexus.data() function to write our new .nexus data. One thing worth mentioning is that the argument “format” in write.nexus.data()has two options: one is “dna” and the other is “protein” so that we can write both DNA and amino acid sequence data.  Below is the example code to implement this in R:

library(seqinr)

data = read.fasta("~/5000alignsimutree.txt")

library(ape)
write.nexus.data(data, file="~/5000alignsimutree.nexus",
format="protein")

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s