# Path: picrust/parallel.py
# def grouper(iterable, n, fillvalue=None):
#         args = [iter(iterable)] * n
#         return izip_longest(*args, fillvalue=fillvalue)


from os.path import exists
from os import remove, rename, rmdir, makedirs
from subprocess import Popen, PIPE, STDOUT
from cogent.util.misc import app_path, create_dir
from cogent.app.util import ApplicationNotFoundError
from cogent.app.util import get_tmp_filename
from picrust.parallel import grouper
from math import ceil
from time import sleep
    """prepare qsub text files.

    command: list of commands

    job_prefix: a short, descriptive name for the job.

    queue: name of the queue to submit to

    jobs_dir: path to directory where job submision scripts are written

    max_hours_per_job: the maximum expected time for each command (this will be multiplied by number of commands per job to get a 'walltime'

    ncpus: number of cpus

    nodes: number of nodes

    keep_output: keep standard error, standard out, both, or neither
                 o=std out, e=std err, oe=both, n=neither
    """

    filenames=[]
    create_dir(jobs_dir)

    #calculate the number of commands to put in each job
    num_commands_per_job=int(ceil(len(commands)/float(num_jobs)))

    #calculate the walltime (time before job will be killed by scheduler if still running)
    total_time = max_hours_per_job*num_commands_per_job
    walltime= "{0}:00:00".format(total_time)


# Path: picrust/parallel.py
# def grouper(iterable, n, fillvalue=None):
#         args = [iter(iterable)] * n
#         return izip_longest(*args, fillvalue=fillvalue)


# Path: picrust/make_cluster_jobs.py
from os.path import exists
from os import remove, rename, rmdir, makedirs
from subprocess import Popen, PIPE, STDOUT
from cogent.util.misc import app_path, create_dir
from cogent.app.util import ApplicationNotFoundError
from cogent.app.util import get_tmp_filename
from picrust.parallel import grouper
from math import ceil
from time import sleep

    """prepare qsub text files.

    command: list of commands

    job_prefix: a short, descriptive name for the job.

    queue: name of the queue to submit to

    jobs_dir: path to directory where job submision scripts are written

    max_hours_per_job: the maximum expected time for each command (this will be multiplied by number of commands per job to get a 'walltime'

    ncpus: number of cpus

    nodes: number of nodes

    keep_output: keep standard error, standard out, both, or neither
                 o=std out, e=std err, oe=both, n=neither
    """

    filenames=[]
    create_dir(jobs_dir)

    #calculate the number of commands to put in each job
    num_commands_per_job=int(ceil(len(commands)/float(num_jobs)))

    #calculate the walltime (time before job will be killed by scheduler if still running)
    total_time = max_hours_per_job*num_commands_per_job
    walltime= "{0}:00:00".format(total_time)


    for command_group in grouper(commands,num_commands_per_job,''):