<p>The obvious single answer is R "ape". This will give you access to PhylML for tree building and Clustal/Muscle for alignment building. The paths to the binarys are important. There are several distance methods in there such as NJ and BIONJ. Its distance approaches however don't look mainstream, but I could be wrong.</p>

<p>There are functions within ape which are cool, the tree sorting is very cool and I need to read through this with much greater care. Personally I wouldn't perform a core phylogenetic analysis within R, because the standalones are sufficient and the analysis is intensive. </p>

<p><a href="https://cran.r-project.org/web/packages/ape/ape.pdf" rel="nofollow noreferrer">https://cran.r-project.org/web/packages/ape/ape.pdf</a></p>

CC BY-SA 4.0
2019-02-13T18:41:17.643Z
2019-02-13T18:41:17.643Z
M__
<p>I agree with Chris Rands that a reasonable approach would be to call external tools.</p>

<p>However, if you really want to do the phylogeny from within Python, you could use the P4 package, which is a bit complicated to handle but gives you lots of options in the way to build MCMC-based bayesian phylogenies:</p>

<p><a href="https://github.com/pgfoster/p4-phylogenetics" rel="nofollow noreferrer">https://github.com/pgfoster/p4-phylogenetics</a></p>

<p>You would still need something else to align the sequences before.</p>

<p>To visualize the tree using python, you could use the ete toolkit, which is likely more powerful than what you can find in Biopython: <a href="http://etetoolkit.org/" rel="nofollow noreferrer">http://etetoolkit.org/</a></p>

CC BY-SA 4.0
2019-02-25T15:55:00.997Z
2019-02-25T15:55:00.997Z
bli
<p>I would not look for a package for this, but instead build a small pipeline calling external tools with something like the following workflow:</p>
<ul>
<li>Cluster the ~100 sequences with <a href="https://sites.google.com/view/cd-hit" rel="nofollow noreferrer">CD-HIT-EST/PSI-CD-HIT</a> or many other options</li>
<li>Take all the sequences that form one individual cluster and build a multiple sequence alignment (MSA) with <a href="https://mafft.cbrc.jp/alignment/software/" rel="nofollow noreferrer">MAFFT</a>/<a href="http://www.clustal.org/omega/" rel="nofollow noreferrer">ClustalOmega</a> or similar</li>
<li>Take the MSA and build a phylogenetic tree with a Maximum-Likelihood approach like <a href="http://www.iqtree.org/" rel="nofollow noreferrer">IQ-TREE</a> or similar</li>
<li>Visualize the tree file with <a href="https://www.jalview.org/" rel="nofollow noreferrer">Jalview</a> or similar</li>
</ul>
<p>Of course this is rather general and depending on exactly what you're doing you may want a different workflow and/or different tools. You should also explore the parameter space, <em>do not</em> assume the defaults are necessarily good choices</p>

CC BY-SA 4.0
2019-02-13T22:03:47.660Z
Yes, I agree with this and thats what I do. R is has its uses however in phylogeny
M__
CC BY-SA 4.0
2019-02-13T22:57:26.933Z
Jalview can also make the tree for you. Just load the alignment into jalview and tell it to build a tree from it.
terdon
CC BY-SA 4.0
2019-02-14T07:43:23.437Z
@terdon not recommended though, I think jalview only implements NJ
Chris_Rands
CC BY-SA 4.0
2019-02-14T09:12:40.840Z
Oh sure, I am not saying it's the best tool. I just know that ~10 years ago when I last used it, it was perfectly capable of producing a decent tree. But then I was using about a dozen sequences, all of which were homologous. So not a very hard tree to build.
terdon
CC BY-SA 4.0
2019-02-14T17:15:44.707Z
@terdon 10 years ago is about right - pre-history and even Perl programmers existed then ... I think you critiqued me for 'samtools' so touche!
M__
CC BY-SA 4.0
2019-02-16T20:53:07.550Z
I agree with the first two steps. For the tree building I might consider RAxML. I might then load the finished tree into R with the ape/phangorn package for manipulation/visualisation.  For stand-along visualisation figtree is great.
Jay Moore
CC BY-SA 4.0
2019-02-13T21:14:56.727Z
2023-10-05T20:47:57.320Z
2023-10-05T20:47:57.320Z
Chris_Rands
<p>(if I understand your situation correctly) <code>https://www.rdocumentation.org/packages/seqinr/versions/3.6-1/topics/read.alignment</code> shows how to use the function <strong>read.alignment</strong> which can take fasta msf etc. The docs provide the example' <code>read.alignment(file = system.file(&quot;sequences/LTPs128_SSU_aligned_First_Two.fasta&quot;,  package = &quot;seqinr&quot;), format = &quot;fasta&quot;, whole.header = TRUE)</code> but you can use this code below (assumes those files are aligned) to go from reading the tree to getting the distances, producing the neighbor joining phylogenetic tree, and then plotting the tree.</p>
<pre><code>library(&quot;Biostrings&quot;)
library(&quot;seqinr&quot;)
library(&quot;ape&quot;)
library(phylogram)
library(&quot;dendextend&quot;)

fasta.res &lt;- read.alignment(file = &quot;geneticAlignment.msf&quot;, format = &quot;fasta&quot;)
fasta.res.dist.alignment = dist.alignment(msf.res, matrix = &quot;identity&quot;)
fasta.res.dist.alignment.nj = nj(fasta.res.dist.alignment)
plot(fasta.res.dist.alignment.nj, main = &quot;from fasta files&quot;)
</code></pre>

CC BY-SA 4.0
2020-09-06T01:54:32.970Z
2020-09-06T01:54:32.970Z
Vass
<p>Within the R/Bioconductor ecosystem you could use the <code>DECIPHER</code> package (<a href="https://bioconductor.org/packages/release/bioc/html/DECIPHER.html" rel="nofollow noreferrer">https://bioconductor.org/packages/release/bioc/html/DECIPHER.html</a>) to align your sequences and then continue with either <code>ape</code> or <code>phangorn</code>  (<a href="https://klausvigo.github.io/phangorn/" rel="nofollow noreferrer">https://klausvigo.github.io/phangorn/</a>) packages to compute a tree. You can then visualize your tree using for example the <code>ggtree</code> package (<a href="https://bioconductor.org/packages/release/bioc/html/ggtree.html" rel="nofollow noreferrer">https://bioconductor.org/packages/release/bioc/html/ggtree.html</a>).</p>

CC BY-SA 4.0
2023-10-06T15:59:59.727Z
2023-10-06T15:59:59.727Z
Niklas
<p>I have around a hundred Fasta files (and will collect several thousand) with DNA sequences and +50x coverage. What is a recommended method to construct a phylogenetic tree? Solutions in Python or R are sought.</p>
<p>I found <code>Phylo</code> from <code>Biopython</code> only handles already calculated trees.</p>

CC BY-SA 4.0
2019-02-13T17:35:57.033Z
Would you be open to using a program designed to do this or do you need to roll your own in a programming language?
terdon
<phylogenetics><python><phylogeny>
How to create Phylogenetic Trees from fasta files in Python or R?
# How to create Phylogenetic Trees from fasta files in Python or R?

(Asked by: username_0 on 2019-02-13)

I have around a hundred Fasta files (and will collect several thousand) with DNA sequences and +50x coverage. What is a recommended method to construct a phylogenetic tree? Solutions in Python or R are sought.

I found `Phylo` from `Biopython` only handles already calculated trees.

**Tags:** phylogenetics, python, phylogeny
> **Comments:**
> **username_1**: Would you be open to using a program designed to do this or do you need to roll your own in a programming language?  

---

## 5 answers

### Answer by username_4 on 2019-02-13. Score: 7

I would not look for a package for this, but instead build a small pipeline calling external tools with something like the following workflow:

* Cluster the ~100 sequences with [CD-HIT-EST/PSI-CD-HIT](https://sites.google.com/view/cd-hit) or many other options
* Take all the sequences that form one individual cluster and build a multiple sequence alignment (MSA) with [MAFFT](https://mafft.cbrc.jp/alignment/software/)/[ClustalOmega](http://www.clustal.org/omega/) or similar
* Take the MSA and build a phylogenetic tree with a Maximum-Likelihood approach like [IQ-TREE](http://www.iqtree.org/) or similar
* Visualize the tree file with [Jalview](https://www.jalview.org/) or similar

Of course this is rather general and depending on exactly what you're doing you may want a different workflow and/or different tools. You should also explore the parameter space, *do not* assume the defaults are necessarily good choices

> **Comments:**
> **username_2**: Yes, I agree with this and thats what I do. R is has its uses however in phylogeny  
> **username_1**: Jalview can also make the tree for you. Just load the alignment into jalview and tell it to build a tree from it.  
> **username_4**: @terdon not recommended though, I think jalview only implements NJ  
> **username_1**: Oh sure, I am not saying it's the best tool. I just know that ~10 years ago when I last used it, it was perfectly capable of producing a decent tree. But then I was using about a dozen sequences, all of which were homologous. So not a very hard tree to build.  
> **username_2**: @terdon 10 years ago is about right - pre-history and even Perl programmers existed then ... I think you critiqued me for 'samtools' so touche!  
> **username_5**: I agree with the first two steps. For the tree building I might consider RAxML. I might then load the finished tree into R with the ape/phangorn package for manipulation/visualisation.  For stand-along visualisation figtree is great.  

---

### Answer by username_2 on 2019-02-13. Score: 3

The obvious single answer is R "ape". This will give you access to PhylML for tree building and Clustal/Muscle for alignment building. The paths to the binarys are important. There are several distance methods in there such as NJ and BIONJ. Its distance approaches however don't look mainstream, but I could be wrong.

There are functions within ape which are cool, the tree sorting is very cool and I need to read through this with much greater care. Personally I wouldn't perform a core phylogenetic analysis within R, because the standalones are sufficient and the analysis is intensive.

<https://cran.r-project.org/web/packages/ape/ape.pdf>


---

### Answer by username_3 on 2019-02-25. Score: 3

I agree with Chris Rands that a reasonable approach would be to call external tools.

However, if you really want to do the phylogeny from within Python, you could use the P4 package, which is a bit complicated to handle but gives you lots of options in the way to build MCMC-based bayesian phylogenies:

<https://github.com/pgfoster/p4-phylogenetics>

You would still need something else to align the sequences before.

To visualize the tree using python, you could use the ete toolkit, which is likely more powerful than what you can find in Biopython: <http://etetoolkit.org/>


---

### Answer by username_6 on 2020-09-06. Score: 2

(if I understand your situation correctly) https://www.rdocumentation.org/packages/seqinr/versions/3.6-1/topics/read.alignment shows how to use the function read.alignment which can take fasta msf etc. The docs provide the example' read.alignment(file = system.file("sequences/LTPs128_SSU_aligned_First_Two.fasta",  package = "seqinr"), format = "fasta", whole.header = TRUE) but you can use this code below (assumes those files are aligned) to go from reading the tree to getting the distances, producing the neighbor joining phylogenetic tree, and then plotting the tree.
library("Biostrings")
library("seqinr")
library("ape")
library(phylogram)
library("dendextend")

fasta.res <- read.alignment(file = "geneticAlignment.msf", format = "fasta")
fasta.res.dist.alignment = dist.alignment(msf.res, matrix = "identity")
fasta.res.dist.alignment.nj = nj(fasta.res.dist.alignment)
plot(fasta.res.dist.alignment.nj, main = "from fasta files")


---

### Answer by username_7 on 2023-10-06. Score: 1

Within the R/Bioconductor ecosystem you could use the `DECIPHER` package (<https://bioconductor.org/packages/release/bioc/html/DECIPHER.html>) to align your sequences and then continue with either `ape` or `phangorn` (<https://klausvigo.github.io/phangorn/>) packages to compute a tree. You can then visualize your tree using for example the `ggtree` package (<https://bioconductor.org/packages/release/bioc/html/ggtree.html>).


---

