#! /usr/bin/env python 
###################################
#    Davi Ortega 7/15/2014 
###################################
import bitkTOL
import sys
import json
if '-h' in sys.argv:
	print 'Build concatenated alignment based on concatenated alignment of 30 cogs recognized as present in all organisms.\n  \
		Options: --rps    			Runs RPS-BLAST with default paramenters on your dataset\n \
			 --buildmongo mongo_db_name	Build new mongodb with RPS results from scratch. If mongo_db_name exists, the program will drop it.\n \
			 --updatemongo mongo_db_name	Update mongo_db_name if necessary (recommended if you are inputing some new sequences\n \
			 --frommongo mongo_db_name	produce the alignment from current data in the mongo_db_name\n \
			 --maketree			Make the tree with the info calculated before\n \
			 --bydomain			perform alignments separately per domain\n \
			 --algo (linsi,einsi,muscle)	choose the alignment algorithm ( default = linsi)\n'
	sys.exit()


if '--algo' in sys.argv:
	algo = sys.argv[sys.argv.index('--algo')+1]
	if algo not in ['linsi', 'einsi', 'muscle']:
		print "The " + algo + " alignment algorithm is not available. Try: linsi, einsi or muscle"
		sys.exit()
else:
	algo = 'linsi'


genomes = []
with open(sys.argv[1], 'r') as f:
        for line in f:
                genomes.append(line.replace('\n',''))

notindb = []
for genome in genomes:
        if bitkTOL.isinmongoTOL(genome, 'mytol') == False:
                notindb.append(genome)

if '--rps' in sys.argv:
	if '--autofix' not in sys.argv:
		data, genomes_new = bitkTOL.run_rps(notindb, )
	else:
		data, genomes_new = bitkTOL.run_rps(notindb, autofix = True)
		if list(set(genomes) - set(genomes_new)) != []:
			print "genomes ignored:"
			print list(set(genomes) - set(genomes_new))
			genomes = genomes_new
	if list(set(genomes) - set(genomes_new)) != []:
		print "something is wrong with run_rps. It is letting problems pass by"
		sys.exit()
	genomes = genomes_new
	data = bitkTOL.rpsdata2mongocards(data)
if '--buildmongo' in sys.argv:
	bitkTOL.new_mongoTOL(data, sys.argv[sys.argv.index('--buildmongo')+1])
elif '--updatemongo' in sys.argv:
	bitkTOL.update_mongoTOL(data, sys.argv[sys.argv.index('--updatemongo')+1])

#bitkTOL.check_mongoTOL('mytol')
if '--frommongo' in sys.argv:
	data, errors = bitkTOL.load_from_mongoTOL(genomes, sys.argv[sys.argv.index('--frommongo')+1])
	if errors != []:
		print "Found errors due to some genomes in the original list was not present in the database. I will try to update the database automatically for you"
		new_data, updatedtodb = bitkTOL.run_rps(errors)
		new_data = bitkTOL.rpsdata2mongocards(new_data)
		bitkTOL.update_mongoTOL(new_data, sys.argv[sys.argv.index('--frommongo')+1])
	data, errors = bitkTOL.load_from_mongoTOL(genomes, sys.argv[sys.argv.index('--frommongo')+1])
	if errors != []:
		print "Something is really wrong... There are still errors in these genomes:"
		print errors
		sys.exit()

if '--maketree' in sys.argv or "--alignonly" in sys.argv:
	cog_list = bitkTOL.pre_aligncog(genomes, data)
	if '--bydomain' in sys.argv:
		domainlist = bitkTOL.aligncogbykingdom(cog_list, algo = algo)
		name = bitkTOL.concatbykingdom(cog_list, domainlist, algo = algo)
                if "--nogb" not in sys.argv:
                    name = bitkTOL.gbbykingdom(name)
	else:
		bitkTOL.aligncog(cog_list)
		name = bitkTOL.concat(cog_list)
                if "--nogb" not in sys.argv:
                    name = bitkTOL.gb(name)
	name = bitkTOL.preptree(name)
        print "This is the name: " + name
	if '--supertree' in sys.argv:
		if '--alignonly' in sys.argv:
			name = bitkTOL.maketree(name, notree = True)
		else:
			name = bitkTOL.maketree(name, supertree = 'Y')
			bitkTOL.posttree(name, sys.argv[1] + '.conservedcog.supertree.nwk', supertree = 'Y')
	else:
		if '--alignonly' in sys.argv:
			name = bitkTOL.maketree(name, notree = True)
		else:
			name = bitkTOL.maketree(name, notree = False)
			bitkTOL.posttree(name, sys.argv[1] + '.conservedcog.nwk', supertree = 'N')
#print data
