Answers.Body: <p>That's a complicated output.</p>
  <p>Two solutions, the easiest is simply to use Andrew Rambaut's <code>figtree</code> <a href="http://tree.bio.ed.ac.uk/software/figtree/" rel="nofollow noreferrer">http://tree.bio.ed.ac.uk/software/figtree/</a> . Figtree is a very, very good tree drawing program and it will work; it works on Mac, Windows and Linux.</p>
  <p>You can then export the file from Figure - from memory in numerous formats, e.g. phylip format, and import it into Biopython if that is where it is needed. If you are parsing Beast output, Beast has A LOT of internal tools and you might be attempting to replace those via Biopython. I would avoid it. Also there is the package for processing Beast output (which I forget), I think its called Tracer.</p>
  <p>With Biopython try parse, it might help if there are multiple trees:</p>
  <pre><code>from Bio import Phylo
  trees = Phylo.parse(&quot;examples.nex&quot;, &quot;nexus&quot;)
  for tree in trees:
      print(tree.name)
  </code></pre>
  <p>I would avoid the 'r'. I agree it makes no difference, but really you'd need to be using unusual file names for 'r' to be needed. Its possible you are using it to get around \s, i.e. spaces in the file name. You could just think about simply not including \ and space type characters in file names. In regex it is used for certain but that's because its 'inescapable' (no pun intended, but I thought it was funny).</p>
  <hr />
  <p>Just to wrap up and clarify, I recommended:</p>
  <ul>
  <li>To open the tree in Figtree;</li>
  <li>Find the export menu and output as either 'newick' or preferably 'nexus' or both;</li>
  <li>Re-run Biopython importing as 'nexus' or 'newick' (if nexus fails).</li>
  </ul>
  <p>The OP reported ...</p>
  <blockquote>
  <p>I have just compared them (nexus-&gt;FigTree-&gt;nexus).</p>
  </blockquote>
  <p>Good so it opened up in FigTree and exported, this was expected.</p>
  <blockquote>
  <p>Fig Tree compose all this actually in newick &amp; stash it in nexus block decor. While in the previous case newick tree contains IDs, &amp; there is a seclude block with ID-name pairs</p>
  </blockquote>
  <p>I missed this bit, &quot;newick stashed in a nexus block&quot;. That is potentially really cool because I suspect that is within the TREES block. If that's correct then that is nexus format. This should now open Biopython.</p>
  <p><strong>Why the bug?</strong></p>
  <p>One of the issues with Nexus is there are two variants regarding how the TAXA block relates to the TREES block. In one format they recode the taxa - I definitely understand why. Recoding is possibly (very likely) be being used in Beast to increase algorithm efficiency - it makes no sense slushing a load of taxa names around a convergence algorithm. Writing all that stuff out is not cool either.</p>
  <p>However, when you are looking at a tree that's not what you want at all - efficiency is an irrelevance and readability is important. Thus recoding the taxa labels is definitely not wanted. This is a possible explanation for the read error.</p>
  <p>Whatever the reason for the bug I am certain Biopython has a work around because in phylip alignment format this is certainly true. There is a custom module that handles the specific issues the format generates.</p>
  <p>It was fun but time to move on.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-22T14:57:13.697Z
Answers.Text: Could I use Figtree algorithms through command line? If it has to be converted to another format, I would like to do this automatically.
Answers.UserDisplayName: Vovin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-22T15:08:40.657Z
Answers.Text: My advice is to get it working, even if point and click, - it will work then compare the input formats to understand what the issue is. You can then script the parsing.
Answers.UserDisplayName: M__
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-22T15:14:53.960Z
Answers.Text: So do you suggest to convert nexus to nexus & compare the difference?
Answers.UserDisplayName: Vovin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-22T15:23:22.417Z
Answers.Text: I have just compared them (nexus->FigTree->nexus). Fig Tree compose all this actually in _newick_ & stash it in nexus block decor. While in the previous case _newick_ tree contains IDs, & there is a seclude block with ID-name pairs.
Answers.UserDisplayName: Vovin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-22T19:44:23.513Z
Answers.Text: Good, should work. I've rewritten the answer to accommodate this.
Answers.UserDisplayName: M__
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-22T14:31:31.763Z
Answers.LastActivityDate: 2022-03-22T23:04:01.123Z
Answers.LastEditDate: 2022-03-22T23:04:01.123Z
Answers.UserDisplayName: M__
Body: <p>I want to edit a tree that I got from BEAST2 treeannotator in nexus-format.</p>
  <p>Usually I use the module Phylo from Biopython for such work but <code>Phylo.read(r&quot;filename.tree&quot;, &quot;nexus&quot;)</code> gave me the next exception:</p>
  <pre><code>---------------------------------------------------------------------------
  NexusError                                Traceback (most recent call last)
  Input In [29], in &lt;cell line: 1&gt;()
  ----&gt; 1 Phylo.read(r&quot;filename.tree&quot;, &quot;nexus&quot;)
  
  File ~\miniconda3\lib\site-packages\Bio\Phylo\_io.py:60, in read(file, format, **kwargs)
       58 try:
       59     tree_gen = parse(file, format, **kwargs)
  ---&gt; 60     tree = next(tree_gen)
       61 except StopIteration:
       62     raise ValueError(&quot;There are no trees in this file.&quot;) from None
  
  File ~\miniconda3\lib\site-packages\Bio\Phylo\_io.py:49, in parse(file, format, **kwargs)
       34 &quot;&quot;&quot;Parse a file iteratively, and yield each of the trees it contains.
       35 
       36 If a file only contains one tree, this still returns an iterable object that
     (...)
       46 
       47 &quot;&quot;&quot;
       48 with File.as_handle(file) as fp:
  ---&gt; 49     yield from getattr(supported_formats[format], &quot;parse&quot;)(fp, **kwargs)
  
  File ~\miniconda3\lib\site-packages\Bio\Phylo\NexusIO.py:40, in parse(handle)
       32 def parse(handle):
       33     &quot;&quot;&quot;Parse the trees in a Nexus file.
       34 
       35     Uses the old Nexus.Trees parser to extract the trees, converts them back to
     (...)
       38     eventually change Nexus to use the new NewickIO parser directly.)
       39     &quot;&quot;&quot;
  ---&gt; 40     nex = Nexus.Nexus(handle)
       42     # NB: Once Nexus.Trees is modified to use Tree.Newick objects, do this:
       43     # return iter(nex.trees)
       44     # Until then, convert the Nexus.Trees.Tree object hierarchy:
       45     def node2clade(nxtree, node):
  
  File ~\miniconda3\lib\site-packages\Bio\Nexus\Nexus.py:668, in Nexus.__init__(self, input)
      665 self.options[&quot;gapmode&quot;] = &quot;missing&quot;
      667 if input:
  --&gt; 668     self.read(input)
      669 else:
      670     self.read(DEFAULTNEXUS)
  
  File ~\miniconda3\lib\site-packages\Bio\Nexus\Nexus.py:718, in Nexus.read(self, input)
      716     break
      717 if title in KNOWN_NEXUS_BLOCKS:
  --&gt; 718     self._parse_nexus_block(title, contents)
      719 else:
      720     self._unknown_nexus_block(title, contents)
  
  File ~\miniconda3\lib\site-packages\Bio\Nexus\Nexus.py:759, in Nexus._parse_nexus_block(self, title, contents)
      757 for line in block.commandlines:
      758     try:
  --&gt; 759         getattr(self, &quot;_&quot; + line.command)(line.options)
      760     except AttributeError:
      761         raise NexusError(&quot;Unknown command: %s &quot; % line.command) from None
  
  File ~\miniconda3\lib\site-packages\Bio\Nexus\Nexus.py:1144, in Nexus._translate(self, options)
     1142         break
     1143     elif c != &quot;,&quot;:
  -&gt; 1144         raise NexusError(&quot;Missing ',' in line %s.&quot; % options)
     1145 except NexusError:
     1146     raise
  
  NexusError: Missing ',' in line 1 AB298157.1_2015_-7.9133750332192605_114.8086828279248, 2 AB298158.1_2007_-8.41698974207…
  </code></pre>
  <p>Using <code>Nexus.read(Nexus(), input=r&quot;filename.tree&quot;)</code> gave the same result. Please could anyone help with this? I cannot understand the reason of this error because <a href="https://drive.google.com/file/d/1Ex_dbdA6IdkRBU33qLuo0sOXty2k3JHu/view?usp=sharing" rel="nofollow noreferrer">nexus file</a> looks correct.</p>
  <hr />
  <p><strong>Nexus format</strong></p>
  <p>A NEXUS tree file is a rigid but very useful format which can be used to store a phylogenetic tree, along with associated metadata, for example, the alignment.</p>
  <p>It will always comprise a:</p>
  <blockquote>
  <p>TAXA block</p>
  </blockquote>
  <p>The TAXA block contains information about taxa.
  ... can comprise a,</p>
  <blockquote>
  <p>DATA block</p>
  </blockquote>
  <p>The DATA block contains the data matrix (e.g. sequence alignment).
  ... in this context importantly will have a,</p>
  <blockquote>
  <p>TREES block</p>
  </blockquote>
  <p>The TREES block contains trees described via the format, e.g. ((A,B),C);:</p>
  <p>Each block will open with a BEGIN statement and terminate with an END statement. Again its versatility to store specific information within a block is valuable property of the format.</p>
  <hr />
  <p><strong>NB!</strong> I encountered some problems with my following work. So I would like to warn readers about them.
  It is allowed in the <strong>newick</strong> format to surround the label with <em>quotes</em>, &amp; some programmes or scripts add them to those names that have ambiguous characters. But it can lead to exceptions during the following phylogenetic analysis, for instance, in <strong>BEAST</strong>. I wish you would be careful with this.</p>
  
Tags: <phylogenetics><python><phylogeny><beast>
Title: Missing ',' in line when Biopython reads a nexus tree
ThreadText: # Missing ',' in line when Biopython reads a nexus tree
  
  (Asked by: username_0 on 2022-03-22)
  
  I want to edit a tree that I got from BEAST2 treeannotator in nexus-format.
  
  Usually I use the module Phylo from Biopython for such work but `Phylo.read(r"filename.tree", "nexus")` gave me the next exception:
  
  ```
  ---------------------------------------------------------------------------
  NexusError                                Traceback (most recent call last)
  Input In [29], in <cell line: 1>()
  ----> 1 Phylo.read(r"filename.tree", "nexus")
  
  File ~\miniconda3\lib\site-packages\Bio\Phylo\_io.py:60, in read(file, format, **kwargs)
       58 try:
       59     tree_gen = parse(file, format, **kwargs)
  ---> 60     tree = next(tree_gen)
       61 except StopIteration:
       62     raise ValueError("There are no trees in this file.") from None
  
  File ~\miniconda3\lib\site-packages\Bio\Phylo\_io.py:49, in parse(file, format, **kwargs)
       34 """Parse a file iteratively, and yield each of the trees it contains.
       35 
       36 If a file only contains one tree, this still returns an iterable object that
     (...)
       46 
       47 """
       48 with File.as_handle(file) as fp:
  ---> 49     yield from getattr(supported_formats[format], "parse")(fp, **kwargs)
  
  File ~\miniconda3\lib\site-packages\Bio\Phylo\NexusIO.py:40, in parse(handle)
       32 def parse(handle):
       33     """Parse the trees in a Nexus file.
       34 
       35     Uses the old Nexus.Trees parser to extract the trees, converts them back to
     (...)
       38     eventually change Nexus to use the new NewickIO parser directly.)
       39     """
  ---> 40     nex = Nexus.Nexus(handle)
       42     # NB: Once Nexus.Trees is modified to use Tree.Newick objects, do this:
       43     # return iter(nex.trees)
       44     # Until then, convert the Nexus.Trees.Tree object hierarchy:
       45     def node2clade(nxtree, node):
  
  File ~\miniconda3\lib\site-packages\Bio\Nexus\Nexus.py:668, in Nexus.__init__(self, input)
      665 self.options["gapmode"] = "missing"
      667 if input:
  --> 668     self.read(input)
      669 else:
      670     self.read(DEFAULTNEXUS)
  
  File ~\miniconda3\lib\site-packages\Bio\Nexus\Nexus.py:718, in Nexus.read(self, input)
      716     break
      717 if title in KNOWN_NEXUS_BLOCKS:
  --> 718     self._parse_nexus_block(title, contents)
      719 else:
      720     self._unknown_nexus_block(title, contents)
  
  File ~\miniconda3\lib\site-packages\Bio\Nexus\Nexus.py:759, in Nexus._parse_nexus_block(self, title, contents)
      757 for line in block.commandlines:
      758     try:
  --> 759         getattr(self, "_" + line.command)(line.options)
      760     except AttributeError:
      761         raise NexusError("Unknown command: %s " % line.command) from None
  
  File ~\miniconda3\lib\site-packages\Bio\Nexus\Nexus.py:1144, in Nexus._translate(self, options)
     1142         break
     1143     elif c != ",":
  -> 1144         raise NexusError("Missing ',' in line %s." % options)
     1145 except NexusError:
     1146     raise
  
  NexusError: Missing ',' in line 1 AB298157.1_2015_-7.9133750332192605_114.8086828279248, 2 AB298158.1_2007_-8.41698974207…
  
  ```
  
  Using `Nexus.read(Nexus(), input=r"filename.tree")` gave the same result. Please could anyone help with this? I cannot understand the reason of this error because [nexus file](https://drive.google.com/file/d/1Ex_dbdA6IdkRBU33qLuo0sOXty2k3JHu/view?usp=sharing) looks correct.
  
  ---
  
  **Nexus format**
  
  A NEXUS tree file is a rigid but very useful format which can be used to store a phylogenetic tree, along with associated metadata, for example, the alignment.
  
  It will always comprise a:
  
  > TAXA block
  
  The TAXA block contains information about taxa.
  ... can comprise a,
  
  > DATA block
  
  The DATA block contains the data matrix (e.g. sequence alignment).
  ... in this context importantly will have a,
  
  > TREES block
  
  The TREES block contains trees described via the format, e.g. ((A,B),C);:
  
  Each block will open with a BEGIN statement and terminate with an END statement. Again its versatility to store specific information within a block is valuable property of the format.
  
  ---
  
  **NB!** I encountered some problems with my following work. So I would like to warn readers about them.
  It is allowed in the **newick** format to surround the label with *quotes*, & some programmes or scripts add them to those names that have ambiguous characters. But it can lead to exceptions during the following phylogenetic analysis, for instance, in **BEAST**. I wish you would be careful with this.
  
  **Tags:** phylogenetics, python, phylogeny, beast
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-03-22. Score: 1 | Accepted answer
  
  That's a complicated output.
  
  Two solutions, the easiest is simply to use Andrew Rambaut's `figtree` <http://tree.bio.ed.ac.uk/software/figtree/> . Figtree is a very, very good tree drawing program and it will work; it works on Mac, Windows and Linux.
  
  You can then export the file from Figure - from memory in numerous formats, e.g. phylip format, and import it into Biopython if that is where it is needed. If you are parsing Beast output, Beast has A LOT of internal tools and you might be attempting to replace those via Biopython. I would avoid it. Also there is the package for processing Beast output (which I forget), I think its called Tracer.
  
  With Biopython try parse, it might help if there are multiple trees:
  
  ```
  from Bio import Phylo
  trees = Phylo.parse("examples.nex", "nexus")
  for tree in trees:
      print(tree.name)
  
  ```
  
  I would avoid the 'r'. I agree it makes no difference, but really you'd need to be using unusual file names for 'r' to be needed. Its possible you are using it to get around \s, i.e. spaces in the file name. You could just think about simply not including \ and space type characters in file names. In regex it is used for certain but that's because its 'inescapable' (no pun intended, but I thought it was funny).
  
  ---
  
  Just to wrap up and clarify, I recommended:
  
  * To open the tree in Figtree;
  * Find the export menu and output as either 'newick' or preferably 'nexus' or both;
  * Re-run Biopython importing as 'nexus' or 'newick' (if nexus fails).
  
  The OP reported ...
  
  > I have just compared them (nexus->FigTree->nexus).
  
  Good so it opened up in FigTree and exported, this was expected.
  
  > Fig Tree compose all this actually in newick & stash it in nexus block decor. While in the previous case newick tree contains IDs, & there is a seclude block with ID-name pairs
  
  I missed this bit, "newick stashed in a nexus block". That is potentially really cool because I suspect that is within the TREES block. If that's correct then that is nexus format. This should now open Biopython.
  
  **Why the bug?**
  
  One of the issues with Nexus is there are two variants regarding how the TAXA block relates to the TREES block. In one format they recode the taxa - I definitely understand why. Recoding is possibly (very likely) be being used in Beast to increase algorithm efficiency - it makes no sense slushing a load of taxa names around a convergence algorithm. Writing all that stuff out is not cool either.
  
  However, when you are looking at a tree that's not what you want at all - efficiency is an irrelevance and readability is important. Thus recoding the taxa labels is definitely not wanted. This is a possible explanation for the read error.
  
  Whatever the reason for the bug I am certain Biopython has a work around because in phylip alignment format this is certainly true. There is a custom module that handles the specific issues the format generates.
  
  It was fun but time to move on.
  
  > **Comments:**
  > **username_0**: Could I use Figtree algorithms through command line? If it has to be converted to another format, I would like to do this automatically.  
  > **username_1**: My advice is to get it working, even if point and click, - it will work then compare the input formats to understand what the issue is. You can then script the parsing.  
  > **username_0**: So do you suggest to convert nexus to nexus & compare the difference?  
  > **username_0**: I have just compared them (nexus->FigTree->nexus). Fig Tree compose all this actually in _newick_ & stash it in nexus block decor. While in the previous case _newick_ tree contains IDs, & there is a seclude block with ID-name pairs.  
  > **username_1**: Good, should work. I've rewritten the answer to accommodate this.  
  
  ---
  
  