# 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
            self.metadata_tsv_files,
            min_gene_length=self.min_gene_length,
            max_gene_length=self.max_gene_length,
            min_noncoding_length = self.min_noncoding_length,
            max_noncoding_length = self.max_noncoding_length,
            genetic_code=self.genetic_code,
        )

        if self.verbose:
            print('\nLoading and checking input data', flush=True)

        self.refdata.rename_sequences(os.path.join(outdir, '00.rename_info'))
        number_of_removed_seqs, number_of_bad_variants_logged = self.refdata.sanity_check(os.path.join(outdir, '01.filter'))

        if self.verbose:
            print('\nRunning cdhit', flush=True)
        cdhit_outprefix = os.path.join(outdir, '02.cdhit')

        clusters = self.refdata.cluster_with_cdhit(
            cdhit_outprefix,
            seq_identity_threshold=self.cdhit_min_id,
            threads=self.threads,
            length_diff_cutoff=self.cdhit_min_length,
            memory_limit=self.cdhit_max_memory,
            nocluster=not self.run_cdhit,
            verbose=self.verbose,
            clusters_file=self.clusters_file,
        )

        clusters = self._rename_clusters(clusters)

# 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

            self.metadata_tsv_files,
            min_gene_length=self.min_gene_length,
            max_gene_length=self.max_gene_length,
            min_noncoding_length = self.min_noncoding_length,
            max_noncoding_length = self.max_noncoding_length,
            genetic_code=self.genetic_code,
        )

        if self.verbose:
            print('\nLoading and checking input data', flush=True)

        self.refdata.rename_sequences(os.path.join(outdir, '00.rename_info'))
        number_of_removed_seqs, number_of_bad_variants_logged = self.refdata.sanity_check(os.path.join(outdir, '01.filter'))

        if self.verbose:
            print('\nRunning cdhit', flush=True)
        cdhit_outprefix = os.path.join(outdir, '02.cdhit')

        clusters = self.refdata.cluster_with_cdhit(
            cdhit_outprefix,
            seq_identity_threshold=self.cdhit_min_id,
            threads=self.threads,
            length_diff_cutoff=self.cdhit_min_length,
            memory_limit=self.cdhit_max_memory,
            nocluster=not self.run_cdhit,
            verbose=self.verbose,
            clusters_file=self.clusters_file,
        )

        clusters = self._rename_clusters(clusters)

        reference_data.ReferenceData.write_cluster_allocation_file(clusters, cdhit_outprefix + '.clusters.tsv')