# 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/reference_data.py
# class Error (Exception): pass
# class ReferenceData:
#     def __init__(self,
#         fasta_files,
#         metadata_tsv_files,
#         rename_file=None,
#         min_gene_length=6,
#         max_gene_length=10000,
#         min_noncoding_length=6,
#         max_noncoding_length=20000,
#         genetic_code=11,
#         parameters_file=None,
#     ):
#     def _load_rename_file(cls, filename):
#     def _load_metadata_tsv(cls, filename, metadata_dict):
#     def _load_all_metadata_tsvs(cls, filenames):
#     def _load_fasta_file(cls, filename, seq_dict):
#     def _load_all_fasta_files(cls, filenames):
#     def _load_input_files_and_check_seq_names(fasta_files, metadata_files):
#     def _write_metadata_tsv(cls, metadata, filename):
#     def _write_sequences_to_files(cls, sequences, metadata, outprefix):
#     def _filter_bad_variant_data(cls, sequences, all_metadata, out_prefix, removed_sequences):
#     def _try_to_get_gene_seq(cls, seq, min_length, max_length):
#     def _check_noncoding_seq(cls, seq, min_length, max_length):
#     def _remove_bad_genes(cls, sequences, metadata, log_file, min_gene_length, max_gene_length):
#     def _remove_bad_noncoding_seqs(cls, sequences, metadata, log_file, min_noncoding_length, max_noncoding_length):
#     def sanity_check(self, outprefix):
#     def _new_seq_name(cls, name):
#     def _seq_names_to_rename_dict(cls, names):
#     def _rename_names_in_seq_dict(cls, seq_dict, rename_dict):
#     def _rename_metadata_set(cls, metadata_set, new_name):
#     def _rename_names_in_metadata(cls, meta_dict, rename_dict):
#     def rename_sequences(self, outfile):
#     def sequence_type(self, sequence_name):
#     def sequence(self, sequence_name):
#     def all_non_wild_type_variants(self, ref_name):
#     def write_cluster_allocation_file(clusters, outfile):
#     def cluster_with_cdhit(self, outprefix, seq_identity_threshold=0.9, threads=1, length_diff_cutoff=0.0, memory_limit=None, nocluster=False, verbose=False, clusters_file=None):
#     def write_seqs_to_fasta(self, outfile, names):


import sys
import os
import pickle
import pyfastaq
from ariba import common, reference_data
            for name in name_set:
                if '.' in name:
                    prefix = name.split('.')[0]
                    if len(prefix) >= min_prefix_length:
                        names_before_dots[prefix] = names_before_dots.get(prefix, 0) + 1

            if len(names_before_dots) == 0:
                new_key = 'cluster'
            elif len(names_before_dots) == 1:
                new_key = list(names_before_dots.keys())[0]
                if sum(list(names_before_dots.values())) < len(name_set):
                    new_key += '+'
            else:
                common_prefix = os.path.commonprefix(list(names_before_dots.keys()))
                if common_prefix == '' or len(common_prefix) < min_prefix_length:
                    max_value = max(list(names_before_dots.values()))
                    possible_keys = [x for x in names_before_dots if names_before_dots[x] == max_value]
                    possible_keys.sort()
                    new_key = possible_keys[0] + '+'
                else:
                    new_key = common_prefix + '-'

            i = 1
            new_new_key = new_key
            while new_new_key in new_clusters:
                new_new_key = new_key + '_' + str(i)
                i += 1
            new_key = new_new_key

            if new_key in key_count:

# 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/reference_data.py
# class Error (Exception): pass
# class ReferenceData:
#     def __init__(self,
#         fasta_files,
#         metadata_tsv_files,
#         rename_file=None,
#         min_gene_length=6,
#         max_gene_length=10000,
#         min_noncoding_length=6,
#         max_noncoding_length=20000,
#         genetic_code=11,
#         parameters_file=None,
#     ):
#     def _load_rename_file(cls, filename):
#     def _load_metadata_tsv(cls, filename, metadata_dict):
#     def _load_all_metadata_tsvs(cls, filenames):
#     def _load_fasta_file(cls, filename, seq_dict):
#     def _load_all_fasta_files(cls, filenames):
#     def _load_input_files_and_check_seq_names(fasta_files, metadata_files):
#     def _write_metadata_tsv(cls, metadata, filename):
#     def _write_sequences_to_files(cls, sequences, metadata, outprefix):
#     def _filter_bad_variant_data(cls, sequences, all_metadata, out_prefix, removed_sequences):
#     def _try_to_get_gene_seq(cls, seq, min_length, max_length):
#     def _check_noncoding_seq(cls, seq, min_length, max_length):
#     def _remove_bad_genes(cls, sequences, metadata, log_file, min_gene_length, max_gene_length):
#     def _remove_bad_noncoding_seqs(cls, sequences, metadata, log_file, min_noncoding_length, max_noncoding_length):
#     def sanity_check(self, outprefix):
#     def _new_seq_name(cls, name):
#     def _seq_names_to_rename_dict(cls, names):
#     def _rename_names_in_seq_dict(cls, seq_dict, rename_dict):
#     def _rename_metadata_set(cls, metadata_set, new_name):
#     def _rename_names_in_metadata(cls, meta_dict, rename_dict):
#     def rename_sequences(self, outfile):
#     def sequence_type(self, sequence_name):
#     def sequence(self, sequence_name):
#     def all_non_wild_type_variants(self, ref_name):
#     def write_cluster_allocation_file(clusters, outfile):
#     def cluster_with_cdhit(self, outprefix, seq_identity_threshold=0.9, threads=1, length_diff_cutoff=0.0, memory_limit=None, nocluster=False, verbose=False, clusters_file=None):
#     def write_seqs_to_fasta(self, outfile, names):


# Path: ariba/ref_preparer.py
import sys
import os
import pickle
import pyfastaq
from ariba import common, reference_data

            for name in name_set:
                if '.' in name:
                    prefix = name.split('.')[0]
                    if len(prefix) >= min_prefix_length:
                        names_before_dots[prefix] = names_before_dots.get(prefix, 0) + 1

            if len(names_before_dots) == 0:
                new_key = 'cluster'
            elif len(names_before_dots) == 1:
                new_key = list(names_before_dots.keys())[0]
                if sum(list(names_before_dots.values())) < len(name_set):
                    new_key += '+'
            else:
                common_prefix = os.path.commonprefix(list(names_before_dots.keys()))
                if common_prefix == '' or len(common_prefix) < min_prefix_length:
                    max_value = max(list(names_before_dots.values()))
                    possible_keys = [x for x in names_before_dots if names_before_dots[x] == max_value]
                    possible_keys.sort()
                    new_key = possible_keys[0] + '+'
                else:
                    new_key = common_prefix + '-'

            i = 1
            new_new_key = new_key
            while new_new_key in new_clusters:
                new_new_key = new_key + '_' + str(i)
                i += 1
            new_key = new_new_key

            if new_key in key_count:

                if new_key in new_clusters: