Partitioning taxon sets, missing partition attribute

33 views
Skip to first unread message

Maria Izabel Cavassim Alves

unread,
Mar 27, 2018, 5:45:53 AM3/27/18
to DendroPy Users
I have just been trying to follow the tutorial to set the different subpopulations:


import dendropy
seqstr = """\
    #NEXUS

     BEGIN TAXA;
         DIMENSIONS NTAX=4;
         TAXLABELS a1 a2 b1 b2;
     END;
     BEGIN CHARACTERS;
        DIMENSIONS NCHAR=7;
        FORMAT DATATYPE=DNA MISSING=? GAP=- MATCHCHAR=.;
        MATRIX
            a1 ACCTTTG
            a2 ACCTTTG
            b1 ACCTTTG
            b2 ATCTTTG
        ;
    END
    """
seqs = dendropy.DnaCharacterMatrix.get_from_string(seqstr, 'nexus')
taxon_namespace = seqs.taxon_namespace
print(dir(taxon_namespace))

def mf(t):
    return t.label[0]
tax_parts = taxon_namespace.partition(membership_func=mf)

However, by just using the example given by the website the following error is given:

AttributeError: 'TaxonNamespace' object has no attribute 'partition'


I have printed the attributes of the variable taxon_namespace. And it follows:


['__class__', '__contains__', '__copy__', '__deepcopy__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__iter__', '__len__', '__lt__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_accession_index_taxon_map', '_current_accession_count', '_format_and_write_to_stream', '_get_annotations', '_get_from', '_get_label', '_has_annotations', '_label', '_lookup_label', '_parse_and_add_from_stream', '_parse_and_create_from_stream', '_read_from', '_set_annotations', '_set_label', '_taxa', '_taxon_accession_index_map', '_taxon_bitmask_map', '_write_to', 'accession_index', 'add_taxa', 'add_taxon', 'all_taxa_bitmask', 'annotations', 'append', 'as_string', 'bitmask_as_bitstring', 'bitmask_as_newick_string', 'bitmask_taxa_list', 'clear', 'clone', 'comments', 'copy_annotations_from', 'deep_copy_annotations_from', 'description', 'discard_taxon_label', 'findall', 'get_from_path', 'get_from_stream', 'get_from_string', 'get_from_url', 'get_taxa', 'get_taxa_bitmask', 'get_taxon', 'has_annotations', 'has_taxa_labels', 'has_taxon_label', 'is_case_sensitive', 'is_mutable', 'label', 'label_taxon_map', 'labels', 'new_taxa', 'new_taxon', 'populate_memo_for_taxon_namespace_scoped_copy', 'read_from_path', 'read_from_stream', 'read_from_string', 'read_from_url', 'remove', 'remove_taxon', 'remove_taxon_label', 'require_taxon', 'reverse', 'sort', 'split_as_newick_string', 'split_as_string', 'taxa_bipartition', 'taxa_bitmask', 'taxon_bitmask', 'taxon_namespace_scoped_copy', 'write', 'write_to_path', 'write_to_stream']


There is no 'partition' attribute there truly. I was wondering if I have been doing something wrong or if the names have changed. 

Cheers.


Jeet Sukumaran

unread,
Apr 10, 2018, 11:09:58 AM4/10/18
to DendroPy Users
Unfortunately, the documentation is out of date here, reflecting a feature of DendroPy 3 that was never fully migrated to DendroPy 4. We could work on bringing it in if needed. What is your use case?

Paul Maier

unread,
May 25, 2018, 1:31:04 AM5/25/18
to DendroPy Users
This feature appears in recent versions (e.g. DendroPy 4.3) user manuals:

I am trying to use this feature, because it seems to be a requirement for using your tool monophyletic_partition_discordance:

How can I use this tool, if taxon_namespace.partition() no longer works? Which DendroPy version should I install? 3.0? 4.3? Working example please?

Much appreciated.

Paul Maier

unread,
May 25, 2018, 4:30:34 AM5/25/18
to dendrop...@googlegroups.com
Nevermind, figured it out. The function works with DendroPy v3.9. Code is pasted below.

Next question: Does anyone know how to create a null distribution of 's', by permuting taxa?


from collections import defaultdict
from StringIO import StringIO
import dendropy
from os import listdir
from os.path import isfile, join
import glob, os
import re

mypath = '/Users/maierpa/Desktop/CH2/bgc/infiles/haplo.EN.100_raxml/'
files = []
os.chdir(mypath)
for file in glob.glob("*.tre"):
    files.append(file)

trees = dendropy.TreeList()
for i in range(0, len(files)):
trees.read_from_path(files[i], 'newick')

for tree in range(0, len(trees)):
species_partitions = [] #list of lists to hold taxon partitions
#make a dictionary with species as keys and lists of individuals as values
tax_pop_label_map = defaultdict(list)
for i in trees[tree].taxon_set:
    newtaxa = str(i).split(' ') #Somehow the '_' is lost, and replaced with a ' '
    tax_pop_label_map[newtaxa[0]].append(i)
#convert default dict list into a list of lists
for key, value in tax_pop_label_map.iteritems():
species_partitions.append(value)
#partition the taxa into species
tps = trees[tree].taxon_set.partition(membership_lists=species_partitions)
s = dendropy.reconcile.monophyletic_partition_discordance(trees[tree], tps)
locus = re.sub("RAxML_bestTree\\.([CI]\\d+)\\.tre", "\\1", files[tree])
print(locus)
print(s)
print(" ")

---------------------------------------
Paul Maier, Ph.D. Candidate

JDP in Evolutionary Biology
San Diego State University
5500 Campanile Drive
San Diego, CA 92182
Cell: (408) 691-2169
Lab: (619) 594-0457
Email: mai...@gmail.com
www.mountainmanmaier.com
---------------------------------------

--
You received this message because you are subscribed to the Google Groups "DendroPy Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dendropy-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paul Maier

unread,
May 25, 2018, 5:47:15 AM5/25/18
to dendrop...@googlegroups.com
Nevermind! Figured that one out too:

from collections import defaultdict
from StringIO import StringIO
import dendropy
from os import listdir
from os.path import isfile, join
import glob, os
import re
import random

# Get names of all tree files in directory first
mypath = '/Users/maierpa/Desktop/CH2/bgc/infiles/haplo.EN.100_raxml/'
files = []
os.chdir(mypath)
for file in glob.glob("*.tre"):
    files.append(file)

# Read in all RAxML trees
trees = dendropy.TreeList()
for i in range(0, len(files)):
trees.read_from_path(files[i], 'newick')

f = open("SlatkinMaddisonS.txt","w+")
for tree in range(0, len(trees)):
# List of lists to hold taxon partitions
species_partitions = []
# Make a dictionary with species as keys and lists of individuals as values
tax_pop_label_map = defaultdict(list)
for i in trees[tree].taxon_set:
# Somehow the '_' is lost, and replaced with a ' '
    newtaxa = str(i).split(' ')
    tax_pop_label_map[newtaxa[0]].append(i)
# Convert default dict list into a list of lists
for key, value in tax_pop_label_map.iteritems():
species_partitions.append(value)
# Partition the taxa into species
tps = trees[tree].taxon_set.partition(membership_lists=species_partitions)
# Calculate Slatkin and Maddison's 's'
s = dendropy.reconcile.monophyletic_partition_discordance(trees[tree], tps)
# Locus name in tree file name
locus = re.sub("RAxML_bestTree\\.([CI]\\d+)\\.tre", "\\1", files[tree])
# Get all pop names so they can be randomly reassigned for permutations
pops = tax_pop_label_map.keys()
sperms = []
# Randomly permute pop assignment on tree to create null distribution
permutations = 100000
for perm in range(0,permutations):
species_partitions = []
tax_pop_label_map = defaultdict(list)
for i in trees[tree].taxon_set:
    pop_rand = random.choice(pops)
    tax_pop_label_map[pop_rand].append(i)
for key, value in tax_pop_label_map.iteritems():
species_partitions.append(value)
tps = trees[tree].taxon_set.partition(membership_lists=species_partitions)
s = dendropy.reconcile.monophyletic_partition_discordance(trees[tree], tps)
sperms.append(s)
# Calculate p-value and write locus, s, and p-value to file
pval = sum(i < s for i in sperms)/float(len(sperms))
f.write(locus + "\t" + str(s) + "\t" + str(pval) + "\n")

f.close()

---------------------------------------
Paul Maier, Ph.D. Candidate

JDP in Evolutionary Biology
San Diego State University
5500 Campanile Drive
San Diego, CA 92182
Cell: (408) 691-2169
Lab: (619) 594-0457
Email: mai...@gmail.com
www.mountainmanmaier.com
---------------------------------------

Reply all
Reply to author
Forward
0 new messages