# Path: circlator/common.py
# class Error (Exception): pass
# def syscall(cmd, allow_fail=False, verbose=False):
# def syscall_get_stdout(cmd):
# def decode(x):
# def check_files_exist(filenames):
# 
# Path: circlator/external_progs.py
# class Error (Exception): pass
# def handle_error(message, raise_error=True):
# def make_and_check_prog(name, verbose=False, raise_error=True, filehandle=None, debug=False, required=False):
# def check_all_progs(verbose=False, raise_error=False, filehandle=None, debug=False, assembler=None):


import os
import tempfile
import sys
import shutil
import pyfastaq
from circlator import common, external_progs
                if best_k is None or n50[k] >= n50[best_k]:
                    best_k = k

            assert best_k is not None

            for k, directory in kmer_to_dir.items():
                if k == best_k:
                    if self.verbose:
                        print('[assemble] using assembly with kmer', k)
                    os.rename(directory, self.outdir)
                else:
                    shutil.rmtree(directory)
        else:
            raise Error('Error running SPAdes. Output directories are:\n  ' + '\n  '.join(kmer_to_dir.values()) + '\nThe reason why should be in the spades.log file in each directory.')


    @classmethod
    def _rename_canu_contigs(cls, infile, outfile):
        with open (infile) as f_in:
            with open(outfile, 'w') as f_out:
                for line in f_in:
                    if line.startswith('>'):
                        print(line.split()[0], file=f_out)
                    else:
                        print(line, end='', file=f_out)


    def run_canu(self):
        '''Runs canu instead of spades'''
        cmd = self._make_canu_command(self.outdir,'canu')

# Path: circlator/common.py
# class Error (Exception): pass
# def syscall(cmd, allow_fail=False, verbose=False):
# def syscall_get_stdout(cmd):
# def decode(x):
# def check_files_exist(filenames):
# 
# Path: circlator/external_progs.py
# class Error (Exception): pass
# def handle_error(message, raise_error=True):
# def make_and_check_prog(name, verbose=False, raise_error=True, filehandle=None, debug=False, required=False):
# def check_all_progs(verbose=False, raise_error=False, filehandle=None, debug=False, assembler=None):


# Path: circlator/assemble.py
import os
import tempfile
import sys
import shutil
import pyfastaq
from circlator import common, external_progs

                if best_k is None or n50[k] >= n50[best_k]:
                    best_k = k

            assert best_k is not None

            for k, directory in kmer_to_dir.items():
                if k == best_k:
                    if self.verbose:
                        print('[assemble] using assembly with kmer', k)
                    os.rename(directory, self.outdir)
                else:
                    shutil.rmtree(directory)
        else:
            raise Error('Error running SPAdes. Output directories are:\n  ' + '\n  '.join(kmer_to_dir.values()) + '\nThe reason why should be in the spades.log file in each directory.')


    @classmethod
    def _rename_canu_contigs(cls, infile, outfile):
        with open (infile) as f_in:
            with open(outfile, 'w') as f_out:
                for line in f_in:
                    if line.startswith('>'):
                        print(line.split()[0], file=f_out)
                    else:
                        print(line, end='', file=f_out)


    def run_canu(self):
        '''Runs canu instead of spades'''
        cmd = self._make_canu_command(self.outdir,'canu')

        ok, errs = common.syscall(cmd, verbose=self.verbose, allow_fail=False)