#! /usr/bin/env python 
###################################
#    Davi Ortega 9/4/2013 
###################################
import ete2
import json
import sys
import os

if '-h' in sys.argv:
    print 'Make tree from nwk file using ete2 python module. Input yourtree.nwk \n \
    	   Options: -pdf    Generate PDF\n \
	   	    -svg    Outputs SVG\n \
		    -show   Show tree\n \
		    -bal    Balance tree\n \
		    -aln    Align leaf names\n \
		    -c      Circular layout\n \
		    -c180   Semi-circular layout\n \
		    --image N1,N2 Insert image with name in bmp. Ni are index of features that should be deleted to match image name. N1 = -1 for name in tree == name in image '
    sys.exit()


filename = sys.argv[1]

colors_array = [ '#E41A1C' , '#377EB8' , '#4DAF4A' , '#984EA3' , '#FF7F00' , '#999999' , '#A65628' , '#F781BF', '#17BECF']

T = ete2.Tree(sys.argv[1])

ts = ete2.TreeStyle()


def layout(node):
	if node.is_leaf():
		N = ete2.AttrFace("name")
		ete2.faces.add_face_to_node(N, node, 0, position = "aligned")

if '--unroot' in sys.argv:
    mp = T.get_midpoint_outgroup()
    T.set_outgroup(mp)

node_id = 0
for n in T.traverse():
        if not n.is_leaf():
                n.name = str(node_id)
        node_id += 1

if '--root' in sys.argv:
        root_id = sys.argv[sys.argv.index('--root') + 1]
        for n in T.traverse():
                if n.name == root_id:
                        D = n
                        break
        T.set_outgroup(D)

if '--balance' in sys.argv:
        T.ladderize()

if '-fbal' in sys.argv:
	ts.force_topology = True


if '-no_top' in sys.argv:
	T.convert_to_ultrametric(1)




#ts.show_leaf_name = False
ts.branch_vertical_margin = 5


if "--image" in sys.argv:
	ts.show_leaf_name = False
	if sys.argv[sys.argv.index("--image") + 1] == '-1':
		N = []
	else:
		N = [ int(i) for i in sys.argv[sys.argv.index("--image") + 1].split(',') ]
	N.sort()
	N.reverse()
	def mylayout(node):
		if node.is_leaf():
			ete2.faces.add_face_to_node(ete2.faces.TextFace(node.name), node, column = 0, aligned = True )
			img_name_l = node.name.split('-')
			for n in N:
				img_name_l.pop(n+1)
#			print '-'.join(img_name_l) + '.png'
			ete2.faces.add_face_to_node(ete2.faces.ImgFace( '-'.join(img_name_l) + '.png'), node, column = 1, aligned = True)
	ts.layout_fn = mylayout

for node in T.traverse():
	node.img_style["size"] = 0


if '-c' in sys.argv:
    filename += '.c'
    ts.mode = 'c'

if '-c180' in sys.argv:
    filename += '.c180'
    ts.mode = 'c'
    ts.arc_start = 180 # 0 degrees = 3 o'clock
    ts.arc_span = 181		
    


if '-aln' in sys.argv:
	filename += '.aln'
	ts.layout_fn = layout
	ts.show_leaf_name = False

if '-show' in sys.argv:
    T.show(tree_style = ts)
    
if '-pdf' in sys.argv:
    print "Making PDF"
    T.render(filename + '.pdf',w=600, tree_style = ts)

if '-svg' in sys.argv:
    print "Making SVG"
    T.render(filename + '.svg', tree_style = ts)

if '-nwk' in sys.argv:
    T.write(format=1, outfile = filename + '.nwk')

if '-xml' in sys.argv:
    T.write(format=1, outfile = filename + '.nwk')
    with open(filename + '.nwk', 'r') as f:
    	data = f.read()
	data.replace(' ', '_')
    os.system('java -cp ~/Downloads/forester_1034.jar org.forester.application.phyloxml_converter -f=sn -ru ' + filename + '.nwk ' + filename + '.xml')

    


#for node in T.iter_leaves():
#    for che in cla['allcla']:
#        ete2.faces.add_face_to_node(node.
#        if che in cla[node.name.split('|')[0]]:
           

#   nstyle = NodeStyle()
#   nstyle["fgcolor"] = "red"
#   nstyle["size"] = 15
#   n.set_style(nstyle)






93e2b4c9d8a029cd984f185d39cce13ebcd11b4b
#! /usr/bin/env python 
###################################
#    Davi Ortega 9/4/2013 
###################################
import ete2
import json
import sys
import os

if '-h' in sys.argv:
    print 'Make tree from nwk file using ete2 python module. Input yourtree.nwk \n \
    	   Options: -pdf    Generate PDF\n \
	   	    -svg    Outputs SVG\n \
		    -show   Show tree\n \
		    -bal    Balance tree\n \
		    -aln    Align leaf names\n \
		    -c      Circular layout\n \
		    -c180   Semi-circular layout\n \
		    --image N1,N2 Insert image with name in bmp. Ni are index of features that should be deleted to match image name. N1 = -1 for name in tree == name in image '
    sys.exit()


filename = sys.argv[1]

colors_array = [ '#E41A1C' , '#377EB8' , '#4DAF4A' , '#984EA3' , '#FF7F00' , '#999999' , '#A65628' , '#F781BF', '#17BECF']

T = ete2.Tree(sys.argv[1])

ts = ete2.TreeStyle()


def layout(node):
	if node.is_leaf():
		N = ete2.AttrFace("name")
		ete2.faces.add_face_to_node(N, node, 0, position = "aligned")

if '--unroot' in sys.argv:
    mp = T.get_midpoint_outgroup()
    T.set_outgroup(mp)

node_id = 0
for n in T.traverse():
        if not n.is_leaf():
                n.name = str(node_id)
        node_id += 1

if '--root' in sys.argv:
        root_id = sys.argv[sys.argv.index('--root') + 1]
        for n in T.traverse():
                if n.name == root_id:
                        D = n
                        break
        T.set_outgroup(D)

if '--balance' in sys.argv:
        T.ladderize()

if '-fbal' in sys.argv:
	ts.force_topology = True


if '-no_top' in sys.argv:
	T.convert_to_ultrametric(1)




#ts.show_leaf_name = False
ts.branch_vertical_margin = 5


if "--image" in sys.argv:
	ts.show_leaf_name = False
	if sys.argv[sys.argv.index("--image") + 1] == '-1':
		N = []
	else:
		N = [ int(i) for i in sys.argv[sys.argv.index("--image") + 1].split(',') ]
	N.sort()
	N.reverse()
	def mylayout(node):
		if node.is_leaf():
			ete2.faces.add_face_to_node(ete2.faces.TextFace(node.name), node, column = 0, aligned = True )
			img_name_l = node.name.split('-')
			for n in N:
				img_name_l.pop(n+1)
#			print '-'.join(img_name_l) + '.png'
			ete2.faces.add_face_to_node(ete2.faces.ImgFace( '-'.join(img_name_l) + '.png'), node, column = 1, aligned = True)
	ts.layout_fn = mylayout

for node in T.traverse():
	node.img_style["size"] = 0


if '-c' in sys.argv:
    filename += '.c'
    ts.mode = 'c'

if '-c180' in sys.argv:
    filename += '.c180'
    ts.mode = 'c'
    ts.arc_start = 180 # 0 degrees = 3 o'clock
    ts.arc_span = 181		
    


if '-aln' in sys.argv:
	filename += '.aln'
	ts.layout_fn = layout
	ts.show_leaf_name = False

if '-show' in sys.argv:
    T.show(tree_style = ts)
    
if '-pdf' in sys.argv:
    print "Making PDF"
    T.render(filename + '.pdf',w=600, tree_style = ts)

if '-svg' in sys.argv:
    print "Making SVG"
    T.render(filename + '.svg', tree_style = ts)

if '-nwk' in sys.argv:
    T.write(format=1, outfile = filename + '.nwk')

if '-xml' in sys.argv:
    T.write(format=1, outfile = filename + '.nwk')
    with open(filename + '.nwk', 'r') as f:
    	data = f.read()
	data.replace(' ', '_')
    os.system('java -cp ~/Downloads/forester_1034.jar org.forester.application.phyloxml_converter -f=sn -ru ' + filename + '.nwk ' + filename + '.xml')

    


#for node in T.iter_leaves():
#    for che in cla['allcla']:
#        ete2.faces.add_face_to_node(node.
#        if che in cla[node.name.split('|')[0]]:
           

#   nstyle = NodeStyle()
#   nstyle["fgcolor"] = "red"
#   nstyle["size"] = 15
#   n.set_style(nstyle)





