# Path: ariba/common.py
# class Error (Exception): pass
# def syscall(cmd, allow_fail=False, verbose=False, verbose_filehandle=sys.stdout, print_errors=True, shell=True):
# def decode(x):
# def cat_files(infiles, outfile):
# def download_file(url, outfile, max_attempts=3, sleep_time=2, verbose=False):
# def rmtree(input_dir):


import tempfile
import copy
import os
import pymummer
import pyfastaq
from ariba import common
            min_length=min_length,
            breaklen=breaklen,
            maxmatch=True,
        ).run()
        nucmer_matches = RefSeqChooser._load_nucmer_coords_file(coords_file, log_fh=log_fh)
        common.rmtree(tmpdir)

        if len(nucmer_matches) == 0:
            return None, {}
        else:
            best_hit = RefSeqChooser._choose_best_nucmer_match(nucmer_matches, use_qry_length=use_qry_length, check_flanking=check_flanking)
            return best_hit, nucmer_matches


    def run(self):
        print('Looking for closest match from sequences within cluster', file=self.log_fh)
        best_hit_from_cluster, nucmer_matches = RefSeqChooser._closest_nucmer_match_between_fastas(self.cluster_fasta, self.assembly_fasta_in, self.log_fh, self.nucmer_min_id, self.nucmer_min_len, self.nucmer_breaklen, False, True)
        if best_hit_from_cluster is None:
            return

        self.closest_ref_within_cluster = best_hit_from_cluster.ref_name
        contigs_prefix = best_hit_from_cluster.qry_name.rsplit('.', maxsplit=2)[0]
        print('Closest cluster ref sequence is', self.closest_ref_within_cluster, 'to assembly', contigs_prefix, file=self.log_fh)
        tmpdir = tempfile.mkdtemp(prefix='tmp.choose_ref.', dir=os.getcwd())
        pieces_fasta_file = os.path.join(tmpdir, 'nucmer_vs_cluster_refs.pieces.fa')
        pieces_coords = [hit for hit in nucmer_matches[best_hit_from_cluster.ref_name] if hit.qry_name.startswith(contigs_prefix + '.')]
        RefSeqChooser._make_matching_contig_pieces_fasta(self.assembly_fasta_in, pieces_coords, pieces_fasta_file)

        print('Checking for a better match to a ref sequence outside the cluster', file=self.log_fh)
        best_hit_from_all_seqs, not_needed = RefSeqChooser._closest_nucmer_match_between_fastas(self.all_refs_fasta, pieces_fasta_file, self.log_fh, self.nucmer_min_id, self.nucmer_min_len, self.nucmer_breaklen, True, False)

# Path: ariba/common.py
# class Error (Exception): pass
# def syscall(cmd, allow_fail=False, verbose=False, verbose_filehandle=sys.stdout, print_errors=True, shell=True):
# def decode(x):
# def cat_files(infiles, outfile):
# def download_file(url, outfile, max_attempts=3, sleep_time=2, verbose=False):
# def rmtree(input_dir):


# Path: ariba/ref_seq_chooser.py
import tempfile
import copy
import os
import pymummer
import pyfastaq
from ariba import common

            min_length=min_length,
            breaklen=breaklen,
            maxmatch=True,
        ).run()
        nucmer_matches = RefSeqChooser._load_nucmer_coords_file(coords_file, log_fh=log_fh)
        common.rmtree(tmpdir)

        if len(nucmer_matches) == 0:
            return None, {}
        else:
            best_hit = RefSeqChooser._choose_best_nucmer_match(nucmer_matches, use_qry_length=use_qry_length, check_flanking=check_flanking)
            return best_hit, nucmer_matches


    def run(self):
        print('Looking for closest match from sequences within cluster', file=self.log_fh)
        best_hit_from_cluster, nucmer_matches = RefSeqChooser._closest_nucmer_match_between_fastas(self.cluster_fasta, self.assembly_fasta_in, self.log_fh, self.nucmer_min_id, self.nucmer_min_len, self.nucmer_breaklen, False, True)
        if best_hit_from_cluster is None:
            return

        self.closest_ref_within_cluster = best_hit_from_cluster.ref_name
        contigs_prefix = best_hit_from_cluster.qry_name.rsplit('.', maxsplit=2)[0]
        print('Closest cluster ref sequence is', self.closest_ref_within_cluster, 'to assembly', contigs_prefix, file=self.log_fh)
        tmpdir = tempfile.mkdtemp(prefix='tmp.choose_ref.', dir=os.getcwd())
        pieces_fasta_file = os.path.join(tmpdir, 'nucmer_vs_cluster_refs.pieces.fa')
        pieces_coords = [hit for hit in nucmer_matches[best_hit_from_cluster.ref_name] if hit.qry_name.startswith(contigs_prefix + '.')]
        RefSeqChooser._make_matching_contig_pieces_fasta(self.assembly_fasta_in, pieces_coords, pieces_fasta_file)

        print('Checking for a better match to a ref sequence outside the cluster', file=self.log_fh)
        best_hit_from_all_seqs, not_needed = RefSeqChooser._closest_nucmer_match_between_fastas(self.all_refs_fasta, pieces_fasta_file, self.log_fh, self.nucmer_min_id, self.nucmer_min_len, self.nucmer_breaklen, True, False)

        common.rmtree(tmpdir)