# Path: picrust/util.py
# def make_output_dir_for_file(filepath):
#     """Create sub-directories for a new file if they don't already exist"""
#     dirpath = dirname(filepath)
#     if not isdir(dirpath) and not dirpath == '':
#         makedirs(dirpath)
# 
# def file_contains_nulls(file):
#     """Checks given file for null characters. These are sometimes created on SGE clusters when system IO is overloaded."""
# 
#     return '\x00' in open(file,'rb').read()
# 
# Path: picrust/util.py
# def get_picrust_project_dir():
#     """ Returns the top-level PICRUST directory
#     """
#     # Get the full path of util.py
#     current_file_path = abspath(__file__)
#     # Get the directory containing util.py
#     current_dir_path = dirname(current_file_path)
#     # Return the directory containing the directory containing util.py
#     return dirname(current_dir_path)
# 
# def make_output_dir(dirpath, strict=False):
#     """Make an output directory if it doesn't exist
# 
#     Returns the path to the directory
#     dirpath -- a string describing the path to the directory
#     strict -- if True, raise an exception if dir already
#     exists
#     """
#     dirpath = abspath(dirpath)
# 
#     #Check if directory already exists
#     if isdir(dirpath):
#         if strict == True:
#             err_str = "Directory '%s' already exists" % dirpath
#             raise IOError(err_str)
# 
#         return dirpath
#     try:
#         makedirs(dirpath)
#     except IOError,e:
#         err_str = "Could not create directory '%s'. Are permissions set correctly? Got error: '%s'" %e
#         raise IOError(err_str)
# 
#     return dirpath
# 
# Path: picrust/parallel.py
# def submit_jobs(path_to_cluster_jobs, jobs_fp, job_prefix,num_jobs=100,delay=0):
#     """ Submit the jobs to the queue using cluster_jobs.py
#     """
#     cmd = '%s -d %s -n %s -ms %s %s' % (path_to_cluster_jobs, delay, num_jobs, jobs_fp, job_prefix)
#     stdout, stderr, return_value = system_call(cmd)
#     if return_value != 0:
#         msg = "\n\n*** Could not start parallel jobs. \n" +\
#          "Command run was:\n %s\n" % cmd +\
#          "Command returned exit status: %d\n" % return_value +\
#          "Stdout:\n%s\nStderr\n%s\n" % (stdout,stderr)
#         raise RuntimeError, msg
# 
#     # Leave this comments in as they're useful for debugging.
#     # print 'Return value: %d\n' % return_value
#     # print 'STDOUT: %s\n' % stdout
#     # print 'STDERR: %s\n' % stderr
# 
# def wait_for_output_files(files):
#     ''' Function waits until all files exist in the filesystem'''
#     #make a copy of the list
#     waiting_files=list(files)
# 
#     #wait until nothing left in the list
#     while(waiting_files):
#         #wait 30 seconds between each check
#         sleep(30)
#         #check each file and keep ones that don't yet exist
#         waiting_files=filter(lambda x: not exists(x),waiting_files)


from cogent.util.option_parsing import parse_command_line_parameters, make_option
from glob import glob
from picrust.util import make_output_dir_for_file,file_contains_nulls
from cogent.app.util import get_tmp_filename
from picrust.util import get_picrust_project_dir,make_output_dir
from picrust.parallel import submit_jobs, wait_for_output_files
from os.path import join,exists
from os import remove
from re import split
            predict_traits_cmd= """python {0} -i "{1}" -t "{2}" -r "{3}" -g "{4}" -o "{5}" -m "{6}" -w {7} """.format(predict_traits_script_fp,\
            test_datasets[test_id][0], opts.ref_tree, asr_out_fp,genome_id, predict_traits_out_fp,predict_traits_method,opts.weighting_method)

            #Instruct predict_traits to use confidence intervals output by ASR
            if opts.with_confidence:
                confidence_param = ' -c "%s"' %(asr_params_out_fp)
                predict_traits_cmd = predict_traits_cmd + confidence_param
        
            #Instruct predict traits to output the NTSI measure of distance to
            #nearby sequences.

            if opts.with_accuracy:
                accuracy_param = ' -a "%s"' %(predict_traits_accuracy_out_fp)
                predict_traits_cmd = predict_traits_cmd + accuracy_param

        

 
            #add job command to the the jobs file
            jobs.write(asr_cmd+';'+predict_traits_cmd+"\n")

    jobs.close()

    #created_tmp_files.extend(output_files)

    #submit the jobs
    job_prefix='eval_'
    
    if opts.verbose:
        print "Submitting jobs:",cluster_jobs_fp,jobs_fp,job_prefix,opts.num_jobs

# Path: picrust/util.py
# def make_output_dir_for_file(filepath):
#     """Create sub-directories for a new file if they don't already exist"""
#     dirpath = dirname(filepath)
#     if not isdir(dirpath) and not dirpath == '':
#         makedirs(dirpath)
# 
# def file_contains_nulls(file):
#     """Checks given file for null characters. These are sometimes created on SGE clusters when system IO is overloaded."""
# 
#     return '\x00' in open(file,'rb').read()
# 
# Path: picrust/util.py
# def get_picrust_project_dir():
#     """ Returns the top-level PICRUST directory
#     """
#     # Get the full path of util.py
#     current_file_path = abspath(__file__)
#     # Get the directory containing util.py
#     current_dir_path = dirname(current_file_path)
#     # Return the directory containing the directory containing util.py
#     return dirname(current_dir_path)
# 
# def make_output_dir(dirpath, strict=False):
#     """Make an output directory if it doesn't exist
# 
#     Returns the path to the directory
#     dirpath -- a string describing the path to the directory
#     strict -- if True, raise an exception if dir already
#     exists
#     """
#     dirpath = abspath(dirpath)
# 
#     #Check if directory already exists
#     if isdir(dirpath):
#         if strict == True:
#             err_str = "Directory '%s' already exists" % dirpath
#             raise IOError(err_str)
# 
#         return dirpath
#     try:
#         makedirs(dirpath)
#     except IOError,e:
#         err_str = "Could not create directory '%s'. Are permissions set correctly? Got error: '%s'" %e
#         raise IOError(err_str)
# 
#     return dirpath
# 
# Path: picrust/parallel.py
# def submit_jobs(path_to_cluster_jobs, jobs_fp, job_prefix,num_jobs=100,delay=0):
#     """ Submit the jobs to the queue using cluster_jobs.py
#     """
#     cmd = '%s -d %s -n %s -ms %s %s' % (path_to_cluster_jobs, delay, num_jobs, jobs_fp, job_prefix)
#     stdout, stderr, return_value = system_call(cmd)
#     if return_value != 0:
#         msg = "\n\n*** Could not start parallel jobs. \n" +\
#          "Command run was:\n %s\n" % cmd +\
#          "Command returned exit status: %d\n" % return_value +\
#          "Stdout:\n%s\nStderr\n%s\n" % (stdout,stderr)
#         raise RuntimeError, msg
# 
#     # Leave this comments in as they're useful for debugging.
#     # print 'Return value: %d\n' % return_value
#     # print 'STDOUT: %s\n' % stdout
#     # print 'STDERR: %s\n' % stderr
# 
# def wait_for_output_files(files):
#     ''' Function waits until all files exist in the filesystem'''
#     #make a copy of the list
#     waiting_files=list(files)
# 
#     #wait until nothing left in the list
#     while(waiting_files):
#         #wait 30 seconds between each check
#         sleep(30)
#         #check each file and keep ones that don't yet exist
#         waiting_files=filter(lambda x: not exists(x),waiting_files)


# Path: scripts/run_genome_evaluations.py
from cogent.util.option_parsing import parse_command_line_parameters, make_option
from glob import glob
from picrust.util import make_output_dir_for_file,file_contains_nulls
from cogent.app.util import get_tmp_filename
from picrust.util import get_picrust_project_dir,make_output_dir
from picrust.parallel import submit_jobs, wait_for_output_files
from os.path import join,exists
from os import remove
from re import split

            predict_traits_cmd= """python {0} -i "{1}" -t "{2}" -r "{3}" -g "{4}" -o "{5}" -m "{6}" -w {7} """.format(predict_traits_script_fp,\
            test_datasets[test_id][0], opts.ref_tree, asr_out_fp,genome_id, predict_traits_out_fp,predict_traits_method,opts.weighting_method)

            #Instruct predict_traits to use confidence intervals output by ASR
            if opts.with_confidence:
                confidence_param = ' -c "%s"' %(asr_params_out_fp)
                predict_traits_cmd = predict_traits_cmd + confidence_param
        
            #Instruct predict traits to output the NTSI measure of distance to
            #nearby sequences.

            if opts.with_accuracy:
                accuracy_param = ' -a "%s"' %(predict_traits_accuracy_out_fp)
                predict_traits_cmd = predict_traits_cmd + accuracy_param

        

 
            #add job command to the the jobs file
            jobs.write(asr_cmd+';'+predict_traits_cmd+"\n")

    jobs.close()

    #created_tmp_files.extend(output_files)

    #submit the jobs
    job_prefix='eval_'
    
    if opts.verbose:
        print "Submitting jobs:",cluster_jobs_fp,jobs_fp,job_prefix,opts.num_jobs

    submit_jobs(cluster_jobs_fp ,jobs_fp,job_prefix,num_jobs=opts.num_jobs)