title: Dendropy: Add inner node midway between two nodes
body: <p>I'm very new to DendroPy. What I want to do seems simple but I can't figure how to do it correctly and I didn't find anything on the internet.</p>
  <p>I want to add a node midway between two nodes in a existing rooted dendropy tree.</p>
  <pre class="lang-py prettyprint-override"><code>from dendropy import Tree, Taxon, Node
  
  t1 = Tree.get_from_string(
      &quot;(((sp1: 0.35, sp2: 0.15):0.75, sp3:1): 0.5, (sp4: 0.5, sp5: 0.05)MRCA_sp4&amp;sp5: 1)root;&quot;,
      &quot;newick&quot;, rooting='force-rooted'
  )
  t1.print_plot()
  mrca = t1.mrca(taxon_labels=[&quot;sp4&quot;, &quot;sp5&quot;])
  print(mrca.description())
  </code></pre>
  <p><a href="https://i.stack.imgur.com/9AjpF.png" rel="nofollow noreferrer">Tree and MRCA node description</a></p>
  <p>The MRCA of sp4 and sp5 is found correctly. Now I'm trying to add a node midway between MRCA and root, using the code below:</p>
  <pre class="lang-py prettyprint-override"><code>def add_node_midway_between_2_nodes(lowernode, taxon_label=None, node_label=None):
      newtaxon = Taxon(label=taxon_label)
      newnode = Node(taxon=newtaxon, label=node_label)
      newnode.parent_node = lowernode.parent_node
      newnode.edge_length = lowernode.edge_length/2
      lowernode.parent_node = newnode
      lowernode.edge_length = newnode.edge_length
      return newnode
  
  node = add_node_midway_between_2_nodes(mrca, node_label=&quot;midway between root and MRCA sp4&amp;sp5&quot;)
  t1.print_plot()
  str_t1 = t1.as_string(schema='newick')
  print(str_t1)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/2VlwW.png" rel="nofollow noreferrer">Tree with a node between root and MRCA sp4&amp;sp5</a></p>
  <p>[&amp;R] (((sp1:0.35,sp2:0.15):0.75,sp3:1.0):0.5,((sp4:0.5,sp5:0.05)MRCA_sp4&amp;sp5:0.5)midway_between_root_and_MRCA_sp4&amp;sp5:0.5)root;</p>
  <p>Looking at the plot and at the string it seems to have worked. But then when I try to compute the MRCA of sp4 et sp5 again, it doesn't find &quot;MRCA sp4&amp;sp5&quot; anymore but the root node.</p>
  <pre class="lang-py prettyprint-override"><code>mrca = t1.mrca(taxon_labels=[&quot;sp4&quot;, &quot;sp5&quot;])
  print(mrca.description())
  </code></pre>
  <p><a href="https://i.stack.imgur.com/9NYOA.png" rel="nofollow noreferrer">Output = Description of root node</a></p>
  <p>Going through parent_node from sp5, I do still find &quot;MRCA sp4&amp;sp5&quot;.</p>
  <p>Out of desperation, I tried to redo the tree using the string str_t1, but it doesn't work either and even gives me another result (just as incorrect): the node &quot;midway between root and MRCA sp4&amp;sp5&quot;</p>
  <pre class="lang-py prettyprint-override"><code>t1 = Tree.get_from_string(
      str_t1,
      &quot;newick&quot;, rooting='force-rooted'
  )
  mrca = t1.mrca(taxon_labels=[&quot;sp4&quot;, &quot;sp5&quot;])
  print(mrca.description())
  </code></pre>
  <p><a href="https://i.stack.imgur.com/KQJ6X.png" rel="nofollow noreferrer">Output = description of node &quot;midway between root and MRCA sp4&amp;sp5&quot;</a></p>
  <p>So what is a clean way to add a node midway between two nodes, that doesn't create weird events afterward?</p>
  <p>Thank you very much</p>