#!python

from spada.methods import *
from spada.io import io

import argparse
import logging

parser = argparse.ArgumentParser(prog = "spada",
				description = "Find significant alternative splicing switches. \
							   Analyze their functional impact.")

subparsers = parser.add_subparsers(help='sub-command help')

################################################
###   INITIALIZE THE NETWORKS               ####
################################################
def createNetwork(o):
	c = create_network.CreateNetwork(o.tumor, o.annotation, o.newNetwork)
	c.run(o.gtf, o.normalExpression, o.tumorExpression, o.minExpression, o.seq,
		  o.ppi, o.ddi, o.drivers, o.features, o.aberrant)

subparser_init = subparsers.add_parser('init', help='Create network.')

subparser_init.add_argument('-T', '--tumor', dest='tumor', action='store',
							help='Identifier of the analysis e.g. the tumor type.')
subparser_init.add_argument('-N', '--new-net', dest='newNetwork', action='store_true',
							help='Use previous networks.', default=False)
subparser_init.add_argument('-a', '--annotation', dest='annotation', action='store',
							choices=['ucsc', 'gencode'], default=None,
							help='Used annotation. Required for newly generated networks.')
subparser_init.add_argument('-g', '--gtf', dest='gtf', action='store', default=None,
							help='GTF with the gene, transcript, exon and CDS annotation.')
subparser_init.add_argument('-n', '--expression-normal', dest='normalExpression',
							action='store', default=None,
							help='Tab-separated table with transcript-level expression data of the normal samples \
							in TPM; a row per annotated transcript, indicated in the first column. Required \
							for newly generated networks.')
subparser_init.add_argument('-t', '--expression-tumor', dest='tumorExpression',
							action='store', default=None,
							help='Equivalent to --expression-normal for the tumor samples. Required for newly \
							generated networks.')
subparser_init.add_argument('-m', '--minimum-expression', dest='minExpression',
							action='store', default=None, type=float,
					 		help='Minimum expression value, in TPM, to consider a transcript expressed. Required \
							for newly generated networks.')
subparser_init.add_argument('-p', '--ppi', dest='ppi', action='store', default=None,
							help='File with protein-protein interactions, in PSI-MI TAB format >= 2.5. Required \
							for newly generated networks.')
subparser_init.add_argument('-d', '--ddi', dest='ddi', action='store', default=None,
							help='Pairs of interacting domains, in TSV format. Required for newly generated networks.')
subparser_init.add_argument('-s', '--seq', dest='seq', action='store', default=None,
							help='Fasta file with the protein sequences of each transcript. Required for newly \
							generated networks.')
subparser_init.add_argument('-f', '--features', dest='features', action='store', default=None,
							help='Tab-separated table with transcript-level features. Required for newly \
							generated networks.')
subparser_init.add_argument('-D', '--drivers', dest='drivers', action='store', default=None,
							help='Tab-separated table containing the genes to be considered tumor drivers. The first column \
							contains the gene symbol, and the second the tumor type where it was detected (which must match \
							--tumor when required). Required for newly generated networks.')
subparser_init.add_argument('-A', '--aberrant', dest='aberrant', action='store', default=None,
							help='Tab-separated table containing gene-aberrant transcript pairs. The genes must be in the \
							annotation, but not the transcripts. Hence, they do not have known genomic coordinates, \
							exon structure or CDS. They can, however, present other features (protein sequence, \
							expression, etc.).')

subparser_init.set_defaults(task="Create network")
subparser_init.set_defaults(func=createNetwork)

################################################
###   COMPUTE SWITCHES                      ####
################################################
def computeSwitches(o):
	c = compute_switches.ComputeSwitches(True, True, o.normalExpression, o.tumorExpression)
	c.run(o.minExpression)

subparser_switches = subparsers.add_parser('switches', help='Detect switches.')
subparser_switches.add_argument('-n', '--expression-normal', dest='normalExpression',
								action='store', default=None,
								help='Tab-separated table with transcript-level expression data of the normal samples \
								in TPM; a row per annotated transcript, indicated in the first column. Sorting \
								by gene (i.e. transcripts from the same gene are consecutive) will drastically \
								optimize memory usage.')
subparser_switches.add_argument('-t', '--expression-tumor', dest='tumorExpression',
								action='store', default=None,
								help='Equivalent to --expression-normal for the tumor samples. Required for newly \
								generated networks.')
subparser_switches.add_argument('-m', '--minimum-expression', dest='minExpression',
								action='store', default=None, type=float,
					 			help='Minimum expression value, in TPM, to consider a transcript expressed. Required \
								for newly generated networks.')
subparser_switches.set_defaults(task="Compute switches")
subparser_switches.set_defaults(func=computeSwitches)

################################################
###   STRUCTURAL ANALYSIS                   ####
################################################
def functionalAnalysis(o):
	g = get_switches.GetSwitches(True, True)
	g.run(o.switchesFile)
	s = structural_analysis.StructuralAnalysis(g._genes, g._txs)
	s.run()

subparser_functional = subparsers.add_parser('function', help='Functional analysis of the switches.')
subparser_functional.add_argument('-s', '--switches', dest='switchesFile', action='store', required=True,
								  default=None, type=str, help='File containing switches as TSV.')
subparser_functional.set_defaults(task="Functional analysis")
subparser_functional.set_defaults(func=functionalAnalysis)

################################################
###   GET RANDOM SWITCHES                   ####
################################################
def simulateSwitches():
	r = simulate_switches.SimulateSwitches(True,True)
	r.run()

subparser_sim = subparsers.add_parser('simulate', help='Simulate random switches.')
subparser_sim.set_defaults(task="Simulate switches")
subparser_sim.set_defaults(func=simulateSwitches)

################################################
###   SWITCH SUMMARY	                    ####
################################################
def summarize(o):
	g = get_switches.GetSwitches(True, True)
	g.run(o.switchesFile)
	s = summary.Summary(g._genes, g._txs)
	s.run()

subparser_summary = subparsers.add_parser('summary', help='Summary statistics of the switches.')
subparser_summary.set_defaults(task="Summarize results")
subparser_summary.add_argument('-s', '--switches', dest='switchesFile', action='store', required=True,
								  default=None, type=str, help='File containing switches as TSV.')
subparser_summary.set_defaults(func=summarize)

################################################
###   MAIN                                  ####
################################################
params = parser.parse_args()

if "task" not in params:
	print("No sub-command selected.")
	parser.print_help()
	exit()

logging.basicConfig(level 	 = logging.DEBUG,
					format 	 = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
				   	filename = 'spada_{}.log'.format(params.task), filemode='w')

console = logging.StreamHandler()
console.setLevel(logging.INFO)

formatter = logging.Formatter('%(asctime)s: %(levelname)-8s %(message)s',
							  datefmt = '%X',)
console.setFormatter(formatter)
logging.getLogger().addHandler(console)

logger = logging.getLogger()
logger.info("SPADA " + params.task)
params.func(params)
logger.info("All operations completed.")
