# 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 pysam
import pyfastaq
from circlator import common, external_progs
    common.syscall(cmd, verbose=verbose)
    bwa_index_clean(tmp_index)
    threads = min(4, threads)
    thread_mem = int(500 / threads)

    # here we have to check for the version of samtools, starting from 1.3 the
    # -o flag is used for specifying the samtools sort output-file.
    # Starting from 1.2 you can use the -o flag, but can't have
    # -o out.bam at the end of the call, so use new style from 1.3 onwards.

    outparam = ''

    if samtools.version_at_least('1.3'):
        outparam = '-o'
        samout = outfile
    else:
        samout = outfile[:-4]

    cmd = ' '.join([
        samtools.exe(), 'sort',
        '-@', str(threads),
        '-m', str(thread_mem) + 'M',
        unsorted_bam,
        outparam,samout
    ])

    common.syscall(cmd, verbose=verbose)
    os.unlink(unsorted_bam)

    cmd = samtools.exe() + ' index ' + outfile

# 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/mapping.py
import os
import pysam
import pyfastaq
from circlator import common, external_progs

    common.syscall(cmd, verbose=verbose)
    bwa_index_clean(tmp_index)
    threads = min(4, threads)
    thread_mem = int(500 / threads)

    # here we have to check for the version of samtools, starting from 1.3 the
    # -o flag is used for specifying the samtools sort output-file.
    # Starting from 1.2 you can use the -o flag, but can't have
    # -o out.bam at the end of the call, so use new style from 1.3 onwards.

    outparam = ''

    if samtools.version_at_least('1.3'):
        outparam = '-o'
        samout = outfile
    else:
        samout = outfile[:-4]

    cmd = ' '.join([
        samtools.exe(), 'sort',
        '-@', str(threads),
        '-m', str(thread_mem) + 'M',
        unsorted_bam,
        outparam,samout
    ])

    common.syscall(cmd, verbose=verbose)
    os.unlink(unsorted_bam)

    cmd = samtools.exe() + ' index ' + outfile

    common.syscall(cmd, verbose=verbose)