# Path: anoisetools/run.py
# def run(self, command, mpi=True, suppress_stdout=True,
#         suppress_stderr=True, **kwargs):
#     """
#     Run the specified command
#     """
# 
#     if not (self.temp_dir and os.path.isdir(self.temp_dir)):
#         raise ValueError("Missing temporary directory: "
#                 "{0}".format(self.temp_dir))
# 
#     if mpi:
#         command = self._mpi_command(command)
# 
#     outputs = [i for i in ('stderr', 'stdout') if i not in kwargs]
#     for i in ('stderr', 'stdout'):
#         if not i in kwargs:
#             kwargs[i] = tempfile.SpooledTemporaryFile()
# 
#     # Convert args to strings
#     command = map(str, command)
# 
#     logging.info("Running: %s", " ".join(command))
#     try:
#         p = subprocess.Popen(command, cwd=self.temp_dir, **kwargs)
#         p.wait()
#         if p.returncode:
#             raise subprocess.CalledProcessError("Error", p.returncode)
#     except subprocess.CalledProcessError, e:
#         if 'stderr' in outputs:
#             kwargs['stderr'].seek(0)
#             stderr = kwargs['stderr'].read()
#         else:
#             stderr = ''
#         if 'stdout' in outputs:
#             kwargs['stdout'].seek(0)
#             stdout = kwargs['stdout'].read()
#         else:
#             stdout = ''
# 
#         logging.error("Error running '%s': returned with %s.\nstdout:\n%s\nstderr:%s",
#                 ' '.join(command), p.returncode, stdout, stderr)
#         raise SystemExit(1)


import logging
import os
import os.path
from anoisetools import run

    return parser

def main(arguments):
    if not arguments.stub:
        arguments.stub = os.path.basename(
                os.path.splitext(arguments.fasta_file)[0])
    if arguments.stub.endswith('-pnoise_cd'):
        arguments.stub = arguments.stub[:-10]

    snoise_stub = arguments.stub + '-snoise'
    targets = [snoise_stub + '_cd.fa', snoise_stub + '.mapping']

    runner = run.NoiseRunner(targets, temp_base=arguments.temp_dir,
        mpi_flags=arguments.mpi_args)

    with runner:
        logging.info("Running SeqNoise")
        run_seqnoise(runner, arguments.fasta_file, arguments.mapping_file,
            arguments.sigma, arguments.cutoff, arguments.stub + '-snoise',
            use_m=arguments.use_m, lookup_file=arguments.lookup_file)

def run_seqnoise(runner, fasta_file, pnoise_mapping, sigma, cutoff, stub,
        use_m=False, lookup_file=None):
    m = run.executable_transformer(use_m)
    # SeqDist
    seqdist_out = runner.path_join(stub + '.seqdist')
    with open(seqdist_out, 'w') as fp:
        runner.run(['SeqDist', '-in', os.path.abspath(fasta_file)], stdout=fp)


# Path: anoisetools/run.py
# def run(self, command, mpi=True, suppress_stdout=True,
#         suppress_stderr=True, **kwargs):
#     """
#     Run the specified command
#     """
# 
#     if not (self.temp_dir and os.path.isdir(self.temp_dir)):
#         raise ValueError("Missing temporary directory: "
#                 "{0}".format(self.temp_dir))
# 
#     if mpi:
#         command = self._mpi_command(command)
# 
#     outputs = [i for i in ('stderr', 'stdout') if i not in kwargs]
#     for i in ('stderr', 'stdout'):
#         if not i in kwargs:
#             kwargs[i] = tempfile.SpooledTemporaryFile()
# 
#     # Convert args to strings
#     command = map(str, command)
# 
#     logging.info("Running: %s", " ".join(command))
#     try:
#         p = subprocess.Popen(command, cwd=self.temp_dir, **kwargs)
#         p.wait()
#         if p.returncode:
#             raise subprocess.CalledProcessError("Error", p.returncode)
#     except subprocess.CalledProcessError, e:
#         if 'stderr' in outputs:
#             kwargs['stderr'].seek(0)
#             stderr = kwargs['stderr'].read()
#         else:
#             stderr = ''
#         if 'stdout' in outputs:
#             kwargs['stdout'].seek(0)
#             stdout = kwargs['stdout'].read()
#         else:
#             stdout = ''
# 
#         logging.error("Error running '%s': returned with %s.\nstdout:\n%s\nstderr:%s",
#                 ' '.join(command), p.returncode, stdout, stderr)
#         raise SystemExit(1)


# Path: anoisetools/scripts/seqnoise.py
import logging
import os
import os.path
from anoisetools import run


    return parser

def main(arguments):
    if not arguments.stub:
        arguments.stub = os.path.basename(
                os.path.splitext(arguments.fasta_file)[0])
    if arguments.stub.endswith('-pnoise_cd'):
        arguments.stub = arguments.stub[:-10]

    snoise_stub = arguments.stub + '-snoise'
    targets = [snoise_stub + '_cd.fa', snoise_stub + '.mapping']

    runner = run.NoiseRunner(targets, temp_base=arguments.temp_dir,
        mpi_flags=arguments.mpi_args)

    with runner:
        logging.info("Running SeqNoise")
        run_seqnoise(runner, arguments.fasta_file, arguments.mapping_file,
            arguments.sigma, arguments.cutoff, arguments.stub + '-snoise',
            use_m=arguments.use_m, lookup_file=arguments.lookup_file)

def run_seqnoise(runner, fasta_file, pnoise_mapping, sigma, cutoff, stub,
        use_m=False, lookup_file=None):
    m = run.executable_transformer(use_m)
    # SeqDist
    seqdist_out = runner.path_join(stub + '.seqdist')
    with open(seqdist_out, 'w') as fp:
        runner.run(['SeqDist', '-in', os.path.abspath(fasta_file)], stdout=fp)


    runner.run([m('FCluster'), '-in', seqdist_out, '-out', stub])