#!/usr/bin/env python

import birchenv
import birchscript
import os
import os.path
import shutil
import subprocess
import sys

#Version   July 14, 2015
# Launch viewers for phylogenetic tree data or send output to files
# For use by programs that generate tree output eg. bldna, blprotein, blmarker etc.
#
#Synopsis: bltreeoutput.py [-e|-d|-f] outtree outfile outname 

#Convert arguments to variables
WHATTODO = str(sys.argv[1])
OUTTREE  = str(sys.argv[2])
OUTFILE  = str(sys.argv[3])
OUTNAME  = str(sys.argv[4])
JOBID    = str(os.getpid())

# Note that the second argument to birchscript.Cleanrun is a list of files to delete after the 
# command is executed. For the -e and -d options, OUTTREE is read by more than one command. 
# Therefore, the delete list is empty until the last command that reads OUTTREE. 

if WHATTODO == '-e': # output to tree editor
    birchscript.Cleanrun([["chooseviewer.py", OUTFILE]], [OUTFILE], True)
    # The current archaeopteryx version doesn't seem to read in the treefiles. Maybe it needs a different format?
    #birchscript.Cleanrun([['archaeopteryx', OUTTREE]], [], True)
    birchscript.Cleanrun([["chooseviewer.py", OUTTREE]], [], True)
    birchscript.Cleanrun([['python', os.path.join(birchenv.BIRCH, 'script', 'tree2flat.py'), OUTTREE, JOBID + '.flat'],
        ['bltree', JOBID + '.flat']], [JOBID + '.flat'], True)
    if os.path.exists(OUTTREE + '.alltrees'):
        birchscript.Cleanrun([['python', os.path.join(birchenv.BIRCH, 'script', 'tree2flat.py'), OUTTREE + '.alltrees', JOBID + '.alltrees.flat'],
            ['bltree', JOBID + '.alltrees.flat']], [JOBID + '.alltrees.flat'], True)
        birchscript.Cleanrun([["chooseviewer.py", OUTTREE + '.alltrees']], [OUTTREE + '.alltrees'], True)
    birchscript.Cleanrun([['atv', OUTTREE]], [OUTTREE], True)
elif WHATTODO == '-d': # output to the Phylip drawtree program
    birchscript.Cleanrun([["chooseviewer.py", OUTFILE]], [OUTFILE], True)
    p= subprocess.Popen(['python', os.path.join(birchenv.BIRCH, 'script', 'drawtree.py'), OUTTREE, 'yes', 'f', '0', '360', 'IE', '360', 'reg', os.path.join(birchenv.BIRCH, 'dat', 'Phylip', 'font1'), '0.333', 'l', JOBID + '.ps'])
    p.wait()
    birchscript.Cleanrun([["chooseviewer.py", JOBID + '.ps']], [JOBID + '.ps'], True)
    birchscript.Cleanrun([['python', os.path.join(birchenv.BIRCH, 'script', 'tree2flat.py'), OUTTREE, JOBID + '.flat'],
        ['bltree', JOBID + '.flat']], [JOBID + '.flat'], True)
    birchscript.Cleanrun([["chooseviewer.py", OUTTREE]], [OUTTREE], True)
    if os.path.exists(OUTTREE + '.alltrees'):
        birchscript.Cleanrun([['python', os.path.join(birchenv.BIRCH, 'script', 'tree2flat.py'), OUTTREE + '.alltrees', JOBID + '.alltrees.flat'],
            ['bltree', JOBID + '.alltrees.flat']], [JOBID + '.alltrees.flat'], True)
        birchscript.Cleanrun([["chooseviewer.py", OUTTREE + '.alltrees']], [OUTTREE + '.alltrees'], True)
elif WHATTODO == '-f': #output to files
    shutil.move(OUTFILE, OUTNAME + '.outfile')
    shutil.move(OUTTREE, OUTNAME + '.treefile')
    if os.path.exists(OUTTREE + '.alltrees'):
        shutil.move(OUTTREE + '.alltrees', OUTNAME + '.alltrees.treefile')
