evogytis/baltic

Unable to extract assigned color, clade id, and leaf id into tsv/dataframe

Closed this issue · 1 comments

I successfully used the documentation to make this plot:
AZ20_panaroo-datetree_muller

However, now what I want to do is assign a clade/grouping-id to each color, so that I can match the id to the color for labeling and further experimental study. I also want all of the leaf tips (isolate ids) for all the groupings. That way, I can randomly select isolates from each of the grouping for additional follow-up analysis and experiments.

My guess on how to do that was:

collapse_target=lambda k: k.is_node() and 'type' in k.parent.traits and k.traits['type']!=k.parent.traits['type']

for idx,k in enumerate(ll.getBranches(collapse_target)): 
    clade_id = f'Clade {idx+1}'
    for elem in K:
        if element.is_leaf():
            print(f'{elem.id}\t{clade_id}')

But I get error: Exception: No branches satisfying function were found amongst branches

How do I extract that information into a tsv or other dataframe?

Hi Celeste,

The error is saying that your tree does not contain any internal nodes (k.is_node()) whose parent node has a trait called type ('type' in k.parent.traits) and whose state is different from its parent's (k.traits['type']!=k.parent.traits['type']). Did you collapse some branches of the tree using collapse_target by any chance? If so, then all the nodes matching that condition will have been removed and so won't exist and won't be found in the modified tree.

You'd also have issues assigning clade ID to the tips since for elem in K: is trying to iterate over a variable that wan't defined (k would be a node if the condition above succeeded but K has not been mentioned previously). Likewise for element - only elem is defined.

The way you would achieve what you want is something like

for elem in k.leaves: ## this iterates over a list of node's descendant tip names
    print(f'{elem}\t{clade_id}')

Since this is not a bug but a use case I'll close this issue. If you have further questions about using baltic please direct them to my email.