import os
import glob
from collections import OrderedDict
from SCons.Script import Command, Depends

tests = OrderedDict()
tests['casino'] = ('casino', '666655666613423414513666666666666')
tests['cpg'] = ('cpg', 'ACTTTTACCGTCAGTGCAGTGCGCGCGCGCGCGCGCCGTTTTAAAAAACCAATT')
tests['multi-cpg'] = ('cpg', 'CGCCGCACTTTTACCGTCAGTGCAGTGCGCGCGCGCGCGCGCCGTTTTAAAAAACCAATT:GCGGCGCCTTCGACCGTCAGTGCAGTGCTTGCGCGCGCGAGCCGTTTGCATTAACGCATT:GCGGAAACTTCGACCGTTTTTGCAGTGCTTGCGCGCGCGAGTTTTTTGCAAAAACGCATT')

testdir = 'test/data/regression/bcrham'
bcrham_args = ' --debug 1 --chain h --hmmdir ' + testdir + ' --datadir ' + testdir + '/germlines --dont-rescale-emissions'
# tests['bcrham-single-viterbi'] = ' --algorithm viterbi' + bcrham_args + ' --infile '+testdir+'/single-input.csv'
# tests['bcrham-pair-viterbi'] = ' --algorithm viterbi' + bcrham_args + ' --infile '+testdir+'/pair-input.csv'
# tests['bcrham-forward'] = ' --algorithm forward' + bcrham_args + ' --infile '+testdir+'/pair-input.csv'
# tests['bcrham-k'] = ' --algorithm forward' + bcrham_args + ' --infile '+testdir+'/k-input.csv'

all_passed = '_results/ALL.passed'
individual_passed = ['_results/%s.passed' % test for test in tests]

for path in individual_passed + [all_passed]:
    if os.path.exists(path):
        os.remove(path)

for test, args in tests.items():
    out = '_results/{0}.out'.format(test)
    if 'bcrham' in test:
        Command(out,
                ['../bcrham',] + glob.glob('data/regression/bcrham/*'),
                './${SOURCES[0]} ' + args + ' --outfile $TARGET')
        Depends(out, '../bcrham')
    else:
        # Run hample with specified conditions.
        Command(out,
                ['../hample', '../examples/%s.yaml' % args[0]],
                './${SOURCES[0]} --hmmfname ${SOURCES[1]} --seqs ' + args[1] + ' -o $TARGET')
        Depends(out, '../hample')

    # Touch a sentinel `passed` file if we get what we expect.
    Command('_results/%s.passed' % test,
            [out, 'data/regression/%s.out' % test],
            'diff ${SOURCES[0]} ${SOURCES[1]} && touch $TARGET')

# Set up sentinel dependency of all passed on the individual_passed sentinels.
Command(all_passed,
        individual_passed,
        'cat $SOURCES > $TARGET')
