# Path: util2.py
# def logmsg(msg, fd=sys.stdout):
#     print >> fd, '[%s] %s' % (datetime.now().strftime('%Y-%m-%d %H:%m:%S'), msg)


import argparse
import os
import subprocess
import zipfile
import numpy as np
import sys
import re
import Queue
import threading
import time
import scipy.misc
import hashlib
from util2 import logmsg
from collections import defaultdict
    app_dir = os.path.dirname(os.path.realpath(__file__))
    processes = []
    out_dirs = []
    for chain_index in range(args['num_chains']):
        output_dir = os.path.join(args['output_dir'],"chain_"+str(chain_index))
        out_dirs.append(output_dir)
        create_directory(output_dir)
        process = run_chain(chain_index, args['ssm_file'], args['cnv_file'], app_dir, working_dir, output_dir, args['random_seeds'], other_args)
        processes.append(process)
    watch_chains(processes)
    return out_dirs

def run_chain(chain_index, ssm_fn, cna_fn, app_dir, working_dir, output_dir, seeds, other_args):
    '''
    Start a new subprocess for every call to evolve. Return the subprocess
    so that we can capture its outputs and see if it is complete.
    '''
    cmd = [
        sys.executable,
        os.path.join(app_dir, "evolve.py"),
        '--output-dir', output_dir,
    ]
    if seeds is not None:
        cmd += ['--random-seed', str(seeds[chain_index])]
    cmd += [
        ssm_fn,
        cna_fn,
    ]
    cmd = cmd + list(other_args)


# Path: util2.py
# def logmsg(msg, fd=sys.stdout):
#     print >> fd, '[%s] %s' % (datetime.now().strftime('%Y-%m-%d %H:%m:%S'), msg)


# Path: multievolve.py
import argparse
import os
import subprocess
import zipfile
import numpy as np
import sys
import re
import Queue
import threading
import time
import scipy.misc
import hashlib
from util2 import logmsg
from collections import defaultdict

    app_dir = os.path.dirname(os.path.realpath(__file__))
    processes = []
    out_dirs = []
    for chain_index in range(args['num_chains']):
        output_dir = os.path.join(args['output_dir'],"chain_"+str(chain_index))
        out_dirs.append(output_dir)
        create_directory(output_dir)
        process = run_chain(chain_index, args['ssm_file'], args['cnv_file'], app_dir, working_dir, output_dir, args['random_seeds'], other_args)
        processes.append(process)
    watch_chains(processes)
    return out_dirs

def run_chain(chain_index, ssm_fn, cna_fn, app_dir, working_dir, output_dir, seeds, other_args):
    '''
    Start a new subprocess for every call to evolve. Return the subprocess
    so that we can capture its outputs and see if it is complete.
    '''
    cmd = [
        sys.executable,
        os.path.join(app_dir, "evolve.py"),
        '--output-dir', output_dir,
    ]
    if seeds is not None:
        cmd += ['--random-seed', str(seeds[chain_index])]
    cmd += [
        ssm_fn,
        cna_fn,
    ]
    cmd = cmd + list(other_args)


    logmsg("Starting chain %s" % chain_index)