import sys
import re
from setuptools import Command, find_packages, setup

_deps = [
    "scanpy==1.8.2",
    "scikit-learn==1.0.2",
    "scikit-image==0.18.3",
    "scipy==1.7.3,",
    "pandas==1.4.1",
    "torch==1.13.1",
    "torch-geometric==2.3.1",
    "tqdm==4.63.0",
    "rpy2==3.1.0",
    "umap-learn==0.5.2",
    "matplotlib==3.5.1",
    "seaborn==0.12.2",
    "h5py==3.6.0"
]


deps = {b: a for a, b in (re.findall(r"^(([^!=<>~]+)(?:[!=<>~].*)?$)", x)[0] for x in _deps)}


def deps_list(*pkgs):
    return [deps[pkg] for pkg in pkgs]


class DepsTableUpdateCommand(Command):
    """
    A custom command that updates the dependency table.
    usage: python setup.py deps_table_update
    """

    description = "build runtime dependency table"
    user_options = [
        # format: (long option, short option, description).
        (
            "dep-table-update",
            None,
            "updates TADGATE/dependency_versions_table.py",
        ),
    ]

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        entries = "\n".join([f'    "{k}": "{v}",' for k, v in deps.items()])
        content = [
            "# THIS FILE HAS BEEN AUTOGENERATED. To update:",
            "# 1. modify the `_deps` dict in setup.py",
            "# 2. run `make deps_table_update`",
            "deps = {",
            entries,
            "}",
            "",
        ]
        target = "TADGATE/dependency_versions_table.py"
        print(f"updating {target}")
        with open(target, "w", encoding="utf-8", newline="\n") as f:
            f.write("\n".join(content))


extras = {}
#extras["rpy2"] = deps_list("rpy2")


version_range_max = max(sys.version_info[1], 10) + 1
install_requires = [
    deps["scanpy"],
    deps["scikit-learn"],
    deps["scikit-image"],
    deps["scipy"],
    deps["pandas"],
    deps["torch"],
    deps["torch-geometric"],
    deps["tqdm"],
    deps["rpy2"],
    deps["umap-learn"],
    deps["matplotlib"],
    deps["seaborn"],
    deps["h5py"],
]


setup(
    name = "TADGATE",
    version = "1.0.1",
    description = "A versatile frame to uncover topologically associating domains from three-dimensional genome maps",
    url = "https://github.com/zhanglabtools/TADGATE",
    author = "Dachang Dang",
    author_email = "dangdachang@163.com",
    python_requires=">=3.8.0",
    license = "MIT",
    install_requires=list(list(install_requires)),
    extras_require=extras,
    packages=find_packages(),
)