Skip to content

Instantly share code, notes, and snippets.

@radaniba
Created November 29, 2012 16:59
Show Gist options
  • Save radaniba/4170378 to your computer and use it in GitHub Desktop.
Save radaniba/4170378 to your computer and use it in GitHub Desktop.
A sample script using the ETE package to detect shift in internal nodes where a significant change in enrichment values (could be anything) happens
#A sample script using the ETE package to detect shift in internal nodes where a significant change in enrichment values (could be anything) happens
#An example (using the scipy python module to perform a K-S test):
from scipy import stats
from ete2 import Tree
newick = "((((A, B)edge1, C)edge2, ((D, E)edge3, F)edge4)edge5, (((G, H)edge6, I)edge7, ((J, K)edge8, L)edge9)edge10)RootEdge;"
enrichment = {
"A": 10,
"B": 11,
"C": 12,
"D": 40,
"E": 41,
"F": 42,
"G": 91,
"H": 92,
"I": 90,
"J": 92,
"K": 91,
"L": 95 }
# load in format 1 to keep edge names in newick
t = Tree(newick, format=1)
print "Tree loaded"
print t.get_ascii()
# load enrichment info into the tree
for n in t.traverse():
if n.is_leaf():
n.add_feature("enrich", enrichment[n.name])
print "Test starting..."
# Analyze the tree
for n in t.traverse():
if not n.is_leaf():
# list of all enrichment values in one branch
e1 = [leaf.enrich for leaf in n.children[0].get_leaves()]
# list of all enrichment values in the sister branch
e2 = [leaf.enrich for leaf in n.children[1].get_leaves()]
# I use scipy.stats to perform ks two sample test. Change this
# to the most appropriate method for your data.
D, pvalue = stats.ks_2samp(e1, e2)
#print pvalue, D
if pvalue < 0.05:
print "significant shift found in ", n.name
print "pvalue =", pvalue
print "enrichment one side", e1
print "enrichment sister branch", e2
print n
Produces:
Tree loaded
/-A
/edge1
/edge2 \-B
| |
| \-C
/edge5
| | /-D
| | /edge3
| \edge4 \-E
| |
| \-F
-RootEdge
| /-G
| /edge6
| /edge7 \-H
| | |
| | \-I
\edge10
| /-J
| /edge8
\edge9 \-K
|
\-L
Test starting...
significant shift found in RootEdge
pvalue = 0.00129974398539
enrichment one side [12, 42, 10, 11, 40, 41]
enrichment sister branch [90, 95, 91, 92, 92, 91]
/-A
/---|
/---| \-B
| |
| \-C
/---|
| | /-D
| | /---|
| \---| \-E
| |
| \-F
----|
| /-G
| /---|
| /---| \-H
| | |
| | \-I
\---|
| /-J
| /---|
\---| \-K
|
\-L
significant shift found in edge5
pvalue = 0.032621651652
enrichment one side [12, 10, 11]
enrichment sister branch [42, 40, 41]
/-A
/---|
/---| \-B
| |
| \-C
----|
| /-D
| /---|
\---| \-E
|
\-F
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment