# 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

script_info['version'] = __version__
       

def main():
    option_parser, opts, args =\
                   parse_command_line_parameters(**script_info)

    #set some defaults for the options
    input_dir=opts.input_dir
    output_dir=opts.output_dir or input_dir
    tmp_dir=opts.tmp_dir or output_dir
    parallel_method=opts.parallel_method
    asr_method = opts.asr_method
    predict_traits_method = opts.prediction_method
    
    if opts.num_jobs > 80 and parallel_method == 'multithreaded':
        raise ValueError('You probably dont want to run multithreaded evaluations with a large num_jobs. Please adjust options num_jobs and or parallel_method')
        
    if opts.with_confidence and asr_method not in ['ace_ml','ace_reml']:
        raise ValueError("PICRUST currently only supports confidence intervals with the ace_ml and ace_reml ASR methods")

    if opts.verbose:
        print "Reconstruction method:",asr_method
        print "Prediction method:",predict_traits_method
        print "Parallel method:",parallel_method
        print "num_jobs:",opts.num_jobs
        print "\nOutput will be saved here:'%s'" %output_dir 
    
    #create the output directory unless it already exists

# 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


script_info['version'] = __version__
       

def main():
    option_parser, opts, args =\
                   parse_command_line_parameters(**script_info)

    #set some defaults for the options
    input_dir=opts.input_dir
    output_dir=opts.output_dir or input_dir
    tmp_dir=opts.tmp_dir or output_dir
    parallel_method=opts.parallel_method
    asr_method = opts.asr_method
    predict_traits_method = opts.prediction_method
    
    if opts.num_jobs > 80 and parallel_method == 'multithreaded':
        raise ValueError('You probably dont want to run multithreaded evaluations with a large num_jobs. Please adjust options num_jobs and or parallel_method')
        
    if opts.with_confidence and asr_method not in ['ace_ml','ace_reml']:
        raise ValueError("PICRUST currently only supports confidence intervals with the ace_ml and ace_reml ASR methods")

    if opts.verbose:
        print "Reconstruction method:",asr_method
        print "Prediction method:",predict_traits_method
        print "Parallel method:",parallel_method
        print "num_jobs:",opts.num_jobs
        print "\nOutput will be saved here:'%s'" %output_dir 
    
    #create the output directory unless it already exists

    make_output_dir(output_dir)