def main(args):
    
    dataDir = args[0]
    outDir = args[1]
    DEFAULT_COUNT = 300
    dataCount = DEFAULT_COUNT
    if (len(args) >= 2):
        dataCount = int(args[2])

    print 'Processing ' + dataDir
    print 'Progress: '
    for i in xrange(1, dataCount+1):
        print '\t' + str(i) + '/' + str(dataCount)

        filenamePrefix = 's' + '{:03d}'.format(i)
        filename = filenamePrefix + '.align.1.msl'
        filePath = os.path.join(dataDir, filename)

        treeNoisyName = filenamePrefix + 'Noisy.tree'
        treeNoisyPath = os.path.join(outDir, treeNoisyName)
        treeNoisyOut = open(treeNoisyPath, 'w')
        
        treeDenoisedName = filenamePrefix + 'Denoised.tree'
        treeDenoisedPath = os.path.join(outDir, treeDenoisedName)
        treeDenoisedOut = open(treeDenoisedPath, 'w')
        try:
            treeCalc.calcTrees(filePath, treeNoisyOut, treeDenoisedOut)
        except RuntimeError:
            treeNoisyOut.close()
            subprocess.call(['rm', treeNoisyPath])
            treeDenoisedOut.close()
            subprocess.call(['rm', treeDenoisedPath])
            continue
        
        treeNoisyOut.close()
        treeDenoisedOut.close()
'''

import os
import sys

#path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../src'))
path = os.path.abspath('src')
if not path in sys.path:
    sys.path.append(path)
del path

import treeCalc

treeNoisyOut = open('./results/2012-12-23/treecalc_control/indelTreeNoisy', 'w')
treeDenoisedOut = open('./results/2012-12-23/treecalc_control/indelTreeDenoised', 'w')
treeCalc.calcTrees('data/2012-12-23/treecalc_control/indels.fa', treeNoisyOut, treeDenoisedOut)
treeNoisyOut.close()
treeDenoisedOut.close()

treeNoisyOut = open('results/2012-12-23/treecalc_control/s001TreeNoisy', 'w')
treeDenoisedOut = open('results/2012-12-23/treecalc_control/s001TreeDenoised', 'w')
treeCalc.calcTrees('data/2012-12-23/treecalc_control/s001.align.1.msl', treeNoisyOut, treeDenoisedOut)
treeNoisyOut.close()
treeDenoisedOut.close()

treeNoisyOut = open('results/2012-12-23/treecalc_control/onlynoiseTreeNoisy', 'w')
treeDenoisedOut = open('results/2012-12-23/treecalc_control/onlynoiseTreeDenoised', 'w')
try:
    treeCalc.calcTrees('data/2012-12-23/treecalc_control/only_noise.fa', treeNoisyOut, treeDenoisedOut)
except RuntimeError as e:
    print >> sys.stderr, e.message