# 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):


import os
import sys
import shutil
import tempfile
import pyfastaq
import logging
from circlator import common
        for program in ['toAmos', 'minimus2']:
            self.check_path(program)

        self.log_file = self.outprefix + '.log'


    def check_path(self, program):
        if shutil.which(program) is None:
            print(program + ' not found in path! Cannot continue')
            raise Error(program + ' not found in path! Cannot continue')


    def _write_split_contig(self, contig, outfile):
        half_coord = int(len(contig) / 2)
        seq1 = contig.subseq(0, half_coord)
        seq2 = contig.subseq(half_coord, len(contig))
        seq1.id += '.1'
        seq2.id += '.2'
        f = pyfastaq.utils.open_file_write(outfile)
        print(seq1, file=f)
        print(seq2, file=f)
        pyfastaq.utils.close(f)


    def _run_minimus2(self, infile, outdir):
        amos_afg_prefix = os.path.join(outdir, 'minimus2')
        amos_afg = amos_afg_prefix + '.afg'
        cmd = 'toAmos -s ' + infile + ' -o ' + amos_afg
        common.syscall(cmd)
        cmd = 'minimus2 ' + amos_afg_prefix

# 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/minimus2.py
import os
import sys
import shutil
import tempfile
import pyfastaq
import logging
from circlator import common

        for program in ['toAmos', 'minimus2']:
            self.check_path(program)

        self.log_file = self.outprefix + '.log'


    def check_path(self, program):
        if shutil.which(program) is None:
            print(program + ' not found in path! Cannot continue')
            raise Error(program + ' not found in path! Cannot continue')


    def _write_split_contig(self, contig, outfile):
        half_coord = int(len(contig) / 2)
        seq1 = contig.subseq(0, half_coord)
        seq2 = contig.subseq(half_coord, len(contig))
        seq1.id += '.1'
        seq2.id += '.2'
        f = pyfastaq.utils.open_file_write(outfile)
        print(seq1, file=f)
        print(seq2, file=f)
        pyfastaq.utils.close(f)


    def _run_minimus2(self, infile, outdir):
        amos_afg_prefix = os.path.join(outdir, 'minimus2')
        amos_afg = amos_afg_prefix + '.afg'
        cmd = 'toAmos -s ' + infile + ' -o ' + amos_afg
        common.syscall(cmd)
        cmd = 'minimus2 ' + amos_afg_prefix

        return common.syscall(cmd, allow_fail=True)