# Path: pham/conserveddomain.py
# INSERT_INTO_DOMAIN = (
#     """INSERT IGNORE INTO domain (HitID, DomainID, Name, Description) """
#     """Values ("{}", "{}", "{}", "{}")""")
# INSERT_INTO_GENE_DOMAIN = (
#     """INSERT IGNORE INTO gene_domain (GeneID, HitID, Expect, QueryStart, """
#     """QueryEnd) VALUES ("{}", "{}", {}, {}, {})""")
# _OUTPUT_FORMAT_XML = 5  # constant used by rpsblast
# _DATA_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data')
# def find_domains(alchemist, gene_ids, sequences, num_threads=1):
# def read_domains_from_xml(alchemist, xml_filename):
# def _read_hit(hit):
# def _upload_domain(engine, hit_id, domain_id, name, description):
# def _upload_hit(engine, gene_id, hit_id, expect, query_start, query_end):
# 
# Path: pham/genbank.py
# def read_file(filepath):
# def write_file(gnm, filehandle):
#     def __init__(self, filename, translation_table):
#     def to_db_object(self):
#     def _add_error(self, error, *args, **kwargs):
#     def _validate_record(self):
#     def _read_legacy_source_record(self, feature):
#     def _read_value(self, feature, qualifiers):
#     def _read_gene_record(self, feature):
#     def _ensure_unique_gene_ids(self):
#     def _set_gene_neighbor_ids(self):
#     def __init__(self, genbank_file=None):
#     def read_file(self, genbank_file):
#     def line_for(self, feature, index=0):
#     def __init__(self, feature, sequence, translation_table, filename,
#                  line_number=None, validate_gene_sequence=True):
#     def to_db_object(self):
#     def _read_gene_id(self):
#     def _add_error(self, error_code, *args):
#     def _read_gene_name(self):
#     def _read_gene_notes(self):
#     def _read_orientation(self):
#     def _read_sequence(self, sequence):
#     def _read_translation(self):
#     def __init__(self, error_code, line_number, filename, *args):
#     def is_warning(self):
#     def __repr__(self):
#     def message(self):
#     def __str__(self):
#     def __eq__(self, other):
# class _PhageReader(object):
# class GenbankLineNumbers(object):
# class GeneReader(object):
# class PhageError(object):
# 
# Path: pham/mmseqs.py
# _DATA_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data')
# def _default_callback(*args, **kwargs):
# def cluster(gene_sequences, gene_ids,
#             on_first_iteration_done=_default_callback):
#     def __init__(self, on_first_iteration_done):
#     def cluster(self, gene_sequences, gene_ids):
#     def _first_iteration(self, gene_ids):
#     def _second_iteration(self):
#     def _read_mmseqs_result(filename):
#     def _combine_iterations(first_iteration, second_iteration):
#     def __init__(self):
#     def insert(self, gene_id, pham_id):
# def _refresh_dir(directory):
# def _write_fasta_record(fasta_file, sequence, gene_id):
# def _call(command):
# class _MMseqs(object):
# class _Phams(object):
# 
# Path: pham/query.py
# EXISTS_QUERY_PREFIX = ("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA "
#                        "WHERE SCHEMA_NAME =")
# def database_exists(alchemist, database):
# def count_phages(alchemist):
# def count_phams(alchemist):
# def count_orphan_genes(alchemist):
# def list_organisms(alchemist):
# def count_domains(alchemist):
# def delete_phage(metadata, engine, phage_id):
# def list_genes(alchemist, phage_id):
# def phage_exists(alchemist, phage_id):
# def retrieve_gene_sequences_and_geneids(metadata, engine):
# def get_pham_geneids(metadata, engine):
# def get_pham_colors(metadata, engine):
# def list_phams(server, id):
# def version_number(alchemist):


import colorsys
import hashlib
import os
import random
import shlex
import subprocess
from enum import Enum
from pathlib import Path
from pdm_utils.classes.alchemyhandler import AlchemyHandler
from pdm_utils.functions import (fileio, mysqldb, mysqldb_basic, querying)
from pdm_utils.pipelines import export_db
from sqlalchemy.sql import func
from pham import conserveddomain
from pham import genbank
from pham import mmseqs
from pham import query
        # an original pham was modified by adding or removing genes
        # use the old pham id
        return original_pham_id


def _execute_sql_file(alchemist, filepath):
    file_handle = open(filepath, "r")
    command_string = f"mysql -u {alchemist.username} -p{alchemist.password}"
    if alchemist.database:
        command_string = " ".join([command_string, alchemist.database])

    command_list = shlex.split(command_string)
    subprocess.check_call(command_list, stdin=file_handle)
    file_handle.close()


# REBUILD HELPER FUNCTIONS
# -----------------------------------------------------------------------------
def validate_genbank_files(db_alchemist, genbank_files_to_add,
                           organism_ids_to_delete, callback):
    valid = True

    phage_id_to_filenames = {}
    duplicate_phage_ids = set()
    duplicate_phage_ids_on_server = set()
    if genbank_files_to_add is not None:
        for index, path in enumerate(genbank_files_to_add):
            callback(CallbackCode.status, 'validating genbank files',
                     index, len(genbank_files_to_add))
            try:

# Path: pham/conserveddomain.py
# INSERT_INTO_DOMAIN = (
#     """INSERT IGNORE INTO domain (HitID, DomainID, Name, Description) """
#     """Values ("{}", "{}", "{}", "{}")""")
# INSERT_INTO_GENE_DOMAIN = (
#     """INSERT IGNORE INTO gene_domain (GeneID, HitID, Expect, QueryStart, """
#     """QueryEnd) VALUES ("{}", "{}", {}, {}, {})""")
# _OUTPUT_FORMAT_XML = 5  # constant used by rpsblast
# _DATA_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data')
# def find_domains(alchemist, gene_ids, sequences, num_threads=1):
# def read_domains_from_xml(alchemist, xml_filename):
# def _read_hit(hit):
# def _upload_domain(engine, hit_id, domain_id, name, description):
# def _upload_hit(engine, gene_id, hit_id, expect, query_start, query_end):
# 
# Path: pham/genbank.py
# def read_file(filepath):
# def write_file(gnm, filehandle):
#     def __init__(self, filename, translation_table):
#     def to_db_object(self):
#     def _add_error(self, error, *args, **kwargs):
#     def _validate_record(self):
#     def _read_legacy_source_record(self, feature):
#     def _read_value(self, feature, qualifiers):
#     def _read_gene_record(self, feature):
#     def _ensure_unique_gene_ids(self):
#     def _set_gene_neighbor_ids(self):
#     def __init__(self, genbank_file=None):
#     def read_file(self, genbank_file):
#     def line_for(self, feature, index=0):
#     def __init__(self, feature, sequence, translation_table, filename,
#                  line_number=None, validate_gene_sequence=True):
#     def to_db_object(self):
#     def _read_gene_id(self):
#     def _add_error(self, error_code, *args):
#     def _read_gene_name(self):
#     def _read_gene_notes(self):
#     def _read_orientation(self):
#     def _read_sequence(self, sequence):
#     def _read_translation(self):
#     def __init__(self, error_code, line_number, filename, *args):
#     def is_warning(self):
#     def __repr__(self):
#     def message(self):
#     def __str__(self):
#     def __eq__(self, other):
# class _PhageReader(object):
# class GenbankLineNumbers(object):
# class GeneReader(object):
# class PhageError(object):
# 
# Path: pham/mmseqs.py
# _DATA_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data')
# def _default_callback(*args, **kwargs):
# def cluster(gene_sequences, gene_ids,
#             on_first_iteration_done=_default_callback):
#     def __init__(self, on_first_iteration_done):
#     def cluster(self, gene_sequences, gene_ids):
#     def _first_iteration(self, gene_ids):
#     def _second_iteration(self):
#     def _read_mmseqs_result(filename):
#     def _combine_iterations(first_iteration, second_iteration):
#     def __init__(self):
#     def insert(self, gene_id, pham_id):
# def _refresh_dir(directory):
# def _write_fasta_record(fasta_file, sequence, gene_id):
# def _call(command):
# class _MMseqs(object):
# class _Phams(object):
# 
# Path: pham/query.py
# EXISTS_QUERY_PREFIX = ("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA "
#                        "WHERE SCHEMA_NAME =")
# def database_exists(alchemist, database):
# def count_phages(alchemist):
# def count_phams(alchemist):
# def count_orphan_genes(alchemist):
# def list_organisms(alchemist):
# def count_domains(alchemist):
# def delete_phage(metadata, engine, phage_id):
# def list_genes(alchemist, phage_id):
# def phage_exists(alchemist, phage_id):
# def retrieve_gene_sequences_and_geneids(metadata, engine):
# def get_pham_geneids(metadata, engine):
# def get_pham_colors(metadata, engine):
# def list_phams(server, id):
# def version_number(alchemist):


# Path: pham/db.py
import colorsys
import hashlib
import os
import random
import shlex
import subprocess
from enum import Enum
from pathlib import Path
from pdm_utils.classes.alchemyhandler import AlchemyHandler
from pdm_utils.functions import (fileio, mysqldb, mysqldb_basic, querying)
from pdm_utils.pipelines import export_db
from sqlalchemy.sql import func
from pham import conserveddomain
from pham import genbank
from pham import mmseqs
from pham import query

        # an original pham was modified by adding or removing genes
        # use the old pham id
        return original_pham_id


def _execute_sql_file(alchemist, filepath):
    file_handle = open(filepath, "r")
    command_string = f"mysql -u {alchemist.username} -p{alchemist.password}"
    if alchemist.database:
        command_string = " ".join([command_string, alchemist.database])

    command_list = shlex.split(command_string)
    subprocess.check_call(command_list, stdin=file_handle)
    file_handle.close()


# REBUILD HELPER FUNCTIONS
# -----------------------------------------------------------------------------
def validate_genbank_files(db_alchemist, genbank_files_to_add,
                           organism_ids_to_delete, callback):
    valid = True

    phage_id_to_filenames = {}
    duplicate_phage_ids = set()
    duplicate_phage_ids_on_server = set()
    if genbank_files_to_add is not None:
        for index, path in enumerate(genbank_files_to_add):
            callback(CallbackCode.status, 'validating genbank files',
                     index, len(genbank_files_to_add))
            try:

                phage = genbank.read_file(path)