#!/usr/bin/python

# driver script to run through standard glint analysis

import sys
import ConfigParser
import subprocess
import os.path

if len(sys.argv) < 2:
  print "Usage: glint.py config_file"
  print "config_file contains all needed information -- see documentation for details"
  sys.exit(1)

conffile= sys.argv[1]

# read needed config information

config=ConfigParser.RawConfigParser()  
config.read(conffile)                  

name1 = config.get('Genome1', 'name')
genome1 = config.get('Genome1', 'seqfile')
name2 = config.get('Genome2', 'name')
genome2 = config.get('Genome2', 'seqfile')

aln = config.get('Alignment', 'xmfa_file')

bindir = config.get('Environment', 'glint_bin_dir')
tmp_prefix = config.get('Environment', 'temp_prefix')
o_prefix = config.get('Environment', 'output_prefix')
if config.has_option('Environment', 'python_cmd'):
  python_cmd = config.get('Environment', 'python_cmd')
else:
  python_cmd = "python"

probefile = config.get('Readout', 'ori_file')
datfile = config.get('Readout', 'intensity_file')

skip_steps = config.get('protocol','skip_steps')

# peek at the sequence files to get needed sequence lengths
sys.path.append(bindir)
from glint_misc import get_genome_length
genome1_length = get_genome_length(genome1)


# names of intermediate files to be produced

full_weight_file = "%s_%s_vs_%s_allweights.txt" % (tmp_prefix, name1, name2)
sparsed_weight_file = "%s_allweights_sparsed.txt" % tmp_prefix
gapped_datfile = "%s_ungapped_logratios.txt" % tmp_prefix
aug_gapscore_file = "%s_aug_gapscores.txt" % tmp_prefix

# preprocess the alignment obtained from mauve
if skip_steps.count('MAUVE_PP') == 0:
  cmdline = "%s %s %s %s %s %s %s %s" % (python_cmd, os.path.join( bindir, 'calc_seq_mapping_from_xmfa.py'), aln, genome1, genome2, name1, name2, tmp_prefix)
  print "******** Identifying major gaps in strain-strain alignment ********"
  subprocess.call(cmdline, shell=True)

# remove un-needed entries from the homology file
if skip_steps.count('HOMOLOGY_PP') == 0:
  cmdline = "%s %s %s %s %s" % (python_cmd, os.path.join( bindir, 'sparse_table_for_scores.py'), full_weight_file, datfile, sparsed_weight_file)
  print "******** Removing un-needed entries for faster access ********"
  subprocess.call(cmdline, shell=True)

# collapse probes that are near a strain1-specific insertion
if skip_steps.count('PROBE_MERGE') == 0:
  cmdline = "%s %s %s %s %s" % (python_cmd,  os.path.join( bindir, "remove_gap_probes.py"), sparsed_weight_file, datfile, gapped_datfile)
  print "******** Collapsing strain1-specific regions ********"
  subprocess.call(cmdline, shell=True)

# combining normalized scores with alignment data
if skip_steps.count('SCORES_MERGE') == 0:
  cmdline = "%s %s %s %s %s %s" % (python_cmd,  os.path.join( bindir, "combine_probeinfo_with_gapdist.py"), gapped_datfile, aug_gapscore_file, probefile, genome1_length )
  print "******** Combining normalized scores with alignment data ********"
  subprocess.call(cmdline, shell=True)

# run peak calling
if skip_steps.count('CALL_PEAKS') == 0:
  cmdline = "%s %s -c %s -o %s -g %s %s" % (python_cmd,  os.path.join( bindir, "tlpeakfind.py"), conffile, o_prefix, genome1_length, aug_gapscore_file)
  print "******** Running peak calling ********"
  subprocess.call(cmdline, shell=True)

