import phylodata
from phylodata.data_types import ClassificationEntryType
from phylodata.maintenance.create_new_version import create_new_version
from phylodata.process.samples.add_nucleotide_metadata import add_nucleotide_metadata

LOG = "scripts/blast_migration_log.txt"
EXPERIMENT_IDS = [
    "savelyev-2020-bayesian-88zc",
    "nen-2019-postglacial-qh0e",
    "nen-2019-postglacial-n1bf",
    "kornilios-2019-genome-3ijw",
    "hancock-2019-phylogeography-4z1v",
    "vianna-2020-genome-mze8",
    "vianna-2020-genome-okrw",
    "burridge-2020-migration-nbmn",
    "corrales-2020-allopatric-v6pl",
    "corrales-2020-allopatric-w7lu",
    "stervander-2019-origin-1z13",
    "stervander-2019-origin-avfu",
    "stervander-2019-origin-702n",
    "hodges-2014-phylogeography-573d",
    "varela-2018-phylogeny-uvh2",
    "leach-2019-exploring-rb8o",
    "leach-2019-exploring-1px9",
    "hammer-2014-multigene-sfjz",
    "linck-2020-speciation-ncxu",
    "tsang-2020-dispersal-9tzw",
    "johnson-2021-systematics-dbeu",
    "johnson-2021-systematics-3ch0",
    "johnson-2021-systematics-ex86",
    "tornabene-2016-repeated-k72b",
    "stervander-2020-molecular-0h8r",
    "chaves-2020-evolutionary-odm3",
    "chaves-2020-evolutionary-jjat",
    "chaves-2020-evolutionary-slrw",
    "chaves-2020-evolutionary-0vrl",
    "chaves-2020-evolutionary-w9n2",
    "chaves-2020-evolutionary-vubq",
    "chaves-2020-evolutionary-5dgl",
    "chaves-2020-evolutionary-8byo",
    "chaves-2020-evolutionary-9cs6",
    "chaves-2020-evolutionary-fmyh",
    "foote-2016-genome-10ig",
    "foote-2016-genome-hcha",
    "foote-2016-genome-8xm0",
    "foote-2016-genome-ydzw",
    "foote-2016-genome-1k2i",
    "carnicero-2018-phylogeography-zpjw",
    "carnicero-2018-phylogeography-riax",
    "vuataz-2016-molecular-9dq5",
    "bagley-2016-phylogeography-18cm",
    "bagley-2016-phylogeography-tsuj",
    "singhal-2018-population-v2of",
    "presslee-2019-palaeoproteomics-btvn",
    "presslee-2019-palaeoproteomics-mjp2",
    "close-2015-evidence-mmit",
    "anest-2021-evolving-n92s",
    "mil-2021-new-786l",
    "mil-2021-new-j2s4",
    "buckley-2020-multiple-mpex",
    "sklen-2017-phylogeny-l1mi",
    "sklen-2017-phylogeny-qwwe",
    "sklen-2017-phylogeny-nda2",
    "magalhaes-2022-phylogeny-iwk9",
    "imfeld-2020-mitochondrial-stuu",
    "near-2021-phylogeny-hiv3",
    "near-2021-phylogeny-dfe7",
    "blow-2021-molecular-l62k",
    "v-2016-new-g4qu",
    "hara-2021-relict-bi5r",
    "deli-2020-subterranean-9yvn",
    "pela-2021-subterranean-iao8",
    "pela-2021-subterranean-zego",
    "catanach-2021-systematics-6ujn",
    "catanach-2021-systematics-yiig",
    "sidlauskas-2021-total-gcfr",
    "winger-2015-inferring-g4kz",
    "georges-2013-contemporary-sr8q",
    "economo-2019-evolution-vn9j",
    "salariato-2016-diversification-059x",
    "salariato-2016-diversification-hhr5",
    "salariato-2016-diversification-6296",
    "jr-2015-multilocus-qbd4",
    "jr-2015-multilocus-t1nb",
    "blanchet-2018-related-q4yw",
    "salariato-2018-reinstatement-b34a",
    "brandrud-2019-phylogenomic-73ad",
    "brandrud-2019-phylogenomic-iyvq",
    "brandrud-2019-phylogenomic-q6h8",
    "bapst-2016-topology-7lg1",
    "bapst-2016-topology-d1nj",
    "salariato-2017-climatic-5lpn",
    "salariato-2017-climatic-4as2",
    "marburger-2018-whole-8zqj",
    "zarza-2016-hidden-0uci",
    "j-2018-allegory-iwn6",
    "morin-2015-geographic-orf9",
    "piatkowski-2019-functional-7jsc",
    "barrett-2024-mosaic-epo4",
    "slater-2017-independent-xt7u",
    "colombo-2015-diversity-4yr1",
    "colombo-2015-diversity-5jap",
    "colombo-2015-diversity-r5ri",
    "colombo-2015-diversity-cf8w",
    "colombo-2015-diversity-i28o",
    "colombo-2015-diversity-gwrh",
    "colombo-2015-diversity-inb1",
    "colombo-2015-diversity-xdg9",
    "colombo-2015-diversity-8kcc",
    "colombo-2015-diversity-ne95",
    "colombo-2015-diversity-4xoe",
    "colombo-2015-diversity-xvrj",
    "stange-2018-bayesian-q044",
    "stange-2018-bayesian-krv3",
    "stange-2018-bayesian-2uhx",
    "toljagic-2018-millions-wb2s",
    "shu-2017-multi-7trv",
    "gohli-2014-evolutionary-gtlx",
    "gohli-2014-evolutionary-scpc",
    "paun-2015-processes-sqks",
    "trotta-2018-community-a38u",
    "sessa-2018-evolution-l9k2",
    "sessa-2018-evolution-8p92",
    "looney-2016-tropics-5f11",
    "lajeunesse-2018-systematic-307m",
    "close-2016-mosaicism-kq3y",
    "close-2016-mosaicism-6hm9",
    "close-2016-mosaicism-d9zx",
    "close-2016-mosaicism-zkjh",
    "munro-2019-climate-6tvf",
    "rawlence-2016-human-852c",
    "rawlence-2016-human-ojb6",
    "saladin-2017-fossils-q2ha",
    "saladin-2017-fossils-kgu9",
    "saladin-2017-fossils-jfg5",
    "saladin-2017-fossils-008y",
    "saladin-2017-fossils-s3c2",
    "saladin-2017-fossils-ju2l",
    "saladin-2017-fossils-yv8r",
    "saladin-2017-fossils-ao96",
    "saladin-2017-fossils-wtv8",
    "saladin-2017-fossils-lfr1",
    "slater-2019-hierarchy-xv5v",
    "clucas-2018-comparative-sqdr",
    "clucas-2018-comparative-ic87",
    "oliver-2020-oligocene-cv1m",
    "cole-2016-nearctic-fgy6",
    "zaher-2018-origin-11vu",
    "grear-2018-inferring-vv5k",
    "grear-2018-inferring-ltdj",
    "grear-2018-inferring-86ph",
    "grear-2018-inferring-plz2",
    "grear-2018-inferring-p7zg",
    "jorge-2018-getting-7s4n",
    "leach-2021-phylogenomic-aphe",
    "leach-2021-phylogenomic-ahe1",
    "davis-2022-population-f3j9",
    "bler-2022-phylogenetic-k54f",
    "brock-2021-color-a2u8",
    "liu-2021-testing-me58",
    "anzas-2022-bayesian-p68i",
    "arbour-2021-little-vfxn",
    "arbour-2021-little-33ff",
    "obiol-2022-palaeoceanographic-u1tx",
    "obiol-2022-palaeoceanographic-7e7s",
    "obiol-2022-palaeoceanographic-cf5c",
    "obiol-2022-palaeoceanographic-l2ba",
    "obiol-2022-palaeoceanographic-fpjv",
    "obiol-2022-palaeoceanographic-rmgd",
    "imfeld-2024-diversification-xtpf",
    "imfeld-2024-diversification-wu1h",
    "imfeld-2024-diversification-i5i3",
    "imfeld-2024-diversification-slkl",
    "gable-2022-genomic-fm05",
    "gable-2022-genomic-n5f4",
    "gable-2022-genomic-nugh",
    "wutke-2024-phylogenomics-oqvj",
    "wutke-2024-phylogenomics-b462",
    "wutke-2024-phylogenomics-a4w7",
    "wutke-2024-phylogenomics-0wb6",
    "wutke-2024-phylogenomics-05x2",
    "wutke-2024-phylogenomics-5qh4",
    "wutke-2024-phylogenomics-6eyi",
    "wutke-2024-phylogenomics-1p3a",
    "wutke-2024-phylogenomics-jeno",
    "wutke-2024-phylogenomics-ck2k",
    "sancho-2022-tracking-8zg6",
    "takahashi-2022-stable-rt26",
    "darlim-2022-impact-umzp",
    "darlim-2022-impact-em28",
    "johnston-2022-cophylogeny-2u2d",
    "mathers-2023-hybridisation-vwt0",
    "forni-2022-macroevolutionary-r34r",
    "kim-2022-phylogenomics-w5kg",
    "brownstein-2022-hidden-zhs4",
    "brownstein-2022-hidden-nk9h",
    "buckingham-2022-population-1mid",
    "buckingham-2022-population-wksf",
    "thomas-2023-multiple-elyn",
    "auderset-2023-subgrouping-eb04",
    "auderset-2023-subgrouping-nq9a",
    "auderset-2023-subgrouping-l8go",
    "auderset-2023-subgrouping-uj71",
    "pyrcz-2023-phylogeny-53vj",
    "puentes-2023-pre-y0hv",
    "mcguire-2023-species-cddh",
    "mcguire-2023-species-iyhy",
    "mcguire-2023-species-y2km",
    "letsch-2023-jumping-rg34",
    "leach-2023-repeated-zn4e",
    "meyer-2023-morrison-zdih",
    "rez-2023-species-ejki",
    "rez-2023-species-3b6y",
    "miri-2024-first-g6o6",
    "harrington-2024-dispersal-xw9b",
    "harrington-2024-dispersal-we1f",
    "harrington-2024-dispersal-wn6u",
    "rozo-2024-polarization-9ofb",
    "tanoyo-2024-systematics-5flw",
    "tanoyo-2024-systematics-frn3",
    "tanoyo-2024-systematics-cskv",
    "tanoyo-2024-systematics-3ytw",
    "vieira-2024-mitochondrial-cg9a",
    "serrano-2024-targeted-zc3e",
    "serrano-2024-targeted-yd14",
    "titus-2024-topology-qk0s",
    "magalhaes-2024-complete-njnd",
    "wilinski-2024-congruity-6pcr",
    "wilinski-2024-congruity-fcth",
]

experiments_to_rerun = []

for experiment_id in EXPERIMENT_IDS:
    experiment = phylodata.load_experiment(
        experiment_id, files_to_download=[], directory="data"
    )

    for sample in experiment.samples:
        if not sample.classification:
            continue

        if sample.classification[0].id_type == ClassificationEntryType.NCBI_TAXONOMY_ID:
            experiments_to_rerun.append(experiment)
            break

with open(LOG, "r") as log_file:
    processed_experiments = set(log_file.read().splitlines())

for experiment in experiments_to_rerun:
    if experiment.experiment.human_readable_id in processed_experiments:
        continue

    print(f"Processing experiment {experiment.experiment.human_readable_id}...")

    for sample in experiment.samples:
        sample.classification = []

    add_nucleotide_metadata(experiment.samples)
    create_new_version(experiment)

    with open(LOG, "a") as log_file:
        log_file.write(f"{experiment.experiment.human_readable_id}\n")
