# 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
    m = hashlib.md5()
    with open(filepath, 'rb') as sql_file:
        while True:
            data = sql_file.read(8192)
            if not data:
                break
            m.update(data)

    # write .md5sum file
    checksum = m.hexdigest()
    with checksum_filename.open(mode="w") as out_file:
        out_file.write(f"{checksum}  {filepath}\n")


# EXPORT gb pipeline
def export_to_genbank(alchemist, identifier, organism_id, filehandle):
    """Download a phage from the database to the given file or file handle.

    Returns an instance of `db_object.Phage`.

    Raises: PhageNotFoundError, DatabaseDoesNotExistError
    """
    if not query.database_exists(alchemist, identifier):
        raise DatabaseDoesNotExistError("No such database: {}".format(
                                                            identifier))

    temp_alchemist = AlchemyHandler()
    temp_alchemist.engine = alchemist.engine
    temp_alchemist.database = identifier


# 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

    m = hashlib.md5()
    with open(filepath, 'rb') as sql_file:
        while True:
            data = sql_file.read(8192)
            if not data:
                break
            m.update(data)

    # write .md5sum file
    checksum = m.hexdigest()
    with checksum_filename.open(mode="w") as out_file:
        out_file.write(f"{checksum}  {filepath}\n")


# EXPORT gb pipeline
def export_to_genbank(alchemist, identifier, organism_id, filehandle):
    """Download a phage from the database to the given file or file handle.

    Returns an instance of `db_object.Phage`.

    Raises: PhageNotFoundError, DatabaseDoesNotExistError
    """
    if not query.database_exists(alchemist, identifier):
        raise DatabaseDoesNotExistError("No such database: {}".format(
                                                            identifier))

    temp_alchemist = AlchemyHandler()
    temp_alchemist.engine = alchemist.engine
    temp_alchemist.database = identifier


    if not query.phage_exists(temp_alchemist, organism_id):