1from ete3 import PhyloTree 2# Loads an example tree 3nw = """ 4((Dme_001,Dme_002),(((Cfa_001,Mms_001),((Hsa_001,Ptr_001),Mmu_001)), 5(Ptr_002,(Hsa_002,Mmu_002)))); 6""" 7t = PhyloTree(nw) 8print t 9# /-Dme_001 10# /--------| 11# | \-Dme_002 12# | 13# | /-Cfa_001 14# | /--------| 15#---------| | \-Mms_001 16# | /--------| 17# | | | /-Hsa_001 18# | | | /--------| 19# | | \--------| \-Ptr_001 20# \--------| | 21# | \-Mmu_001 22# | 23# | /-Ptr_002 24# \--------| 25# | /-Hsa_002 26# \--------| 27# \-Mmu_002 28# 29# To obtain all the evolutionary events involving a given leaf node we 30# use get_my_evol_events method 31matches = t.search_nodes(name="Hsa_001") 32human_seq = matches[0] 33# Obtains its evolutionary events 34events = human_seq.get_my_evol_events() 35# Print its orthology and paralogy relationships 36print "Events detected that involve Hsa_001:" 37for ev in events: 38 if ev.etype == "S": 39 print ' ORTHOLOGY RELATIONSHIP:', ','.join(ev.in_seqs), "<====>", ','.join(ev.out_seqs) 40 elif ev.etype == "D": 41 print ' PARALOGY RELATIONSHIP:', ','.join(ev.in_seqs), "<====>", ','.join(ev.out_seqs) 42 43# Alternatively, you can scan the whole tree topology 44events = t.get_descendant_evol_events() 45# Print its orthology and paralogy relationships 46print "Events detected from the root of the tree" 47for ev in events: 48 if ev.etype == "S": 49 print ' ORTHOLOGY RELATIONSHIP:', ','.join(ev.in_seqs), "<====>", ','.join(ev.out_seqs) 50 elif ev.etype == "D": 51 print ' PARALOGY RELATIONSHIP:', ','.join(ev.in_seqs), "<====>", ','.join(ev.out_seqs) 52 53# If we are only interested in the orthology and paralogy relationship 54# among a given set of species, we can filter the list of sequences 55# 56# fseqs is a function that, given a list of sequences, returns only 57# those from human and mouse 58fseqs = lambda slist: [s for s in slist if s.startswith("Hsa") or s.startswith("Mms")] 59print "Paralogy relationships among human and mouse" 60for ev in events: 61 if ev.etype == "D": 62 # Prints paralogy relationships considering only human and 63 # mouse. Some duplication event may not involve such species, 64 # so they will be empty 65 print ' PARALOGY RELATIONSHIP:', \ 66 ','.join(fseqs(ev.in_seqs)), \ 67 "<====>",\ 68 ','.join(fseqs(ev.out_seqs)) 69 70# Note that besides the list of events returned, the detection 71# algorithm has labeled the tree nodes according with the 72# predictions. We can use such lables as normal node features. 73dups = t.search_nodes(evoltype="D") # Return all duplication nodes 74