Coder Social home page Coder Social logo

Comments (4)

jeetsukumaran avatar jeetsukumaran commented on August 16, 2024

Hmmm. I don't think prune_taxa returns a tree. Did you mean some other function in the first line?

from dendropy.

wrightaprilm avatar wrightaprilm commented on August 16, 2024

'Returns' was not the right way to phrase that. Looking at the source, it looks like prune_taxa just prunes the listed taxa on the tree object that's in memory. So, if I'm understanding this right, prune_taxa won't return anything, so trying to write the results of the command to a new object will give me nothing? So if I want to maintain a copy of the unpruned tree in memory, I should make a copy of the tree before pruning, and perform the pruning on the copy. Is that right?

from dendropy.

jeetsukumaran avatar jeetsukumaran commented on August 16, 2024

Python functions return None by default. So, your assignment state does bind tree to the implicit return value of the function, which is None.

For what you want to do, yes, making a copy of the unpruned tree before pruning will work. But note that Python has name binding semantics, and not memory-binding semantics like C++.

So if you say:

tree_copy = tree1 # NOT actually a copy!!
tree1.prune_taxa()
assert tree_copy is tree1 # True

you will not get anywhere, because tree_copy is a name bound to the same object as the tree1 name. Unlike C/C++, a value is NOT assigned to a memory slot, but one name is bound to the same object/reference to which the other name is bound. You will need to explicitly clone or deep-copy the tree:

tree_copy = tree1.clone()

and then go ahead and make changes to tree1.

Apologies if all of this is already familiar to you! But sometimes, especially if you switch back-and-forth between C/C++ and Python, it is easy to get tripped up by Python name-binding semantics vs. C/C++ memory-value assignment semantics.

Note that if you do not need/use any metadata annotations, you should consider using the extract_tree family of methods (https://pythonhosted.org/DendroPy/primer/treemanips.html#extracting-trees-and-subtrees-from-an-existing-tree):

  • Tree.extract_tree
  • Tree.extract_tree_with_taxa
  • Tree.extract_tree_without_taxa
  • Tree.extract_tree_with_taxa_labels
  • Tree.extract_tree_without_taxa_labels

Much faster and more efficient than cloning and then pruning.

from dendropy.

wrightaprilm avatar wrightaprilm commented on August 16, 2024

OK, thanks! That answers my question.

I'll give the extract methods a try, since I'm not using metadata.

from dendropy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.