#!/usr/bin/env python3
"""Write the curated data tables transcribed from Mendel (1866).

Every count below was keyed from the German text of
  Mendel, G. (1866). Versuche ueber Pflanzen-Hybriden.
  Verhandlungen des naturforschenden Vereines in Bruenn, Bd. IV (1865), Abhandlungen, 3-47.
and is checked against three independent transcriptions by code/02_verify_sources.py.
No count is altered, rounded, pooled or dropped here.
"""
import csv, pathlib

ROOT = pathlib.Path(__file__).resolve().parents[1]
CUR = ROOT / "data/curated"
PUB = ROOT / "data/published_benchmarks"
CUR.mkdir(parents=True, exist_ok=True)
PUB.mkdir(parents=True, exist_ok=True)

def wcsv(path, header, rows):
    with open(path, "w", encoding="utf-8", newline="") as f:
        w = csv.writer(f, lineterminator="\n")
        w.writerow(header)
        w.writerows(rows)

# 1. Seven single-character experiments, first generation bred from the hybrids (modern F2).
wcsv(CUR / "mendel_f2_monohybrid.csv",
     ["experiment", "trait_en", "trait_de", "unit_counted", "dominant_form", "recessive_form",
      "n_hybrid_parents", "n_total_printed", "dominant", "recessive", "printed_ratio"],
     [[1, "Seed shape", "Gestalt der Samen", "seeds", "round (rund oder rundlich)", "wrinkled (kantig runzlig)", 253, 7324, 5474, 1850, 2.96],
      [2, "Seed colour (albumen)", "Faerbung des Albumens", "seeds", "yellow (gelb)", "green (gruen)", 258, 8023, 6022, 2001, 3.01],
      [3, "Seed-coat colour", "Farbe der Samenschale", "plants", "grey-brown coat with violet-red flowers", "white coat with white flowers", "", 929, 705, 224, 3.15],
      [4, "Pod shape", "Gestalt der Huelsen", "plants", "inflated (einfach gewoelbt)", "constricted (eingeschnuert)", "", 1181, 882, 299, 2.95],
      [5, "Unripe pod colour", "Faerbung der unreifen Huelse", "plants", "green (gruen)", "yellow (gelb)", "", 580, 428, 152, 2.82],
      [6, "Flower position", "Stellung der Bluethen", "plants", "axial (axenstaendig)", "terminal (endstaendig)", "", 858, 651, 207, 3.14],
      [7, "Stem length", "Laenge der Axe", "plants", "long (lang)", "short (kurz)", "", 1064, 787, 277, 2.84]])

# 2. Progeny tests of dominant-phenotype plants, second generation bred from the hybrids (modern F3).
wcsv(CUR / "mendel_f3_progeny_tests.csv",
     ["experiment", "trait_en", "n_plants_tested", "constant_dominant", "segregating", "progeny_scored_per_plant", "note"],
     [["1", "Seed shape", 565, 193, 372, "all seeds borne by the plant", "seed character: scored on the seeds of each plant"],
      ["2", "Seed colour (albumen)", 519, 166, 353, "all seeds borne by the plant", "seed character: scored on the seeds of each plant"],
      ["3", "Seed-coat colour", 100, 36, 64, "10", "10 seeds sown per plant (von jeder 10 Samen angebaut)"],
      ["4", "Pod shape", 100, 29, 71, "10", "10 seeds sown per plant"],
      ["5", "Unripe pod colour", 100, 40, 60, "10", "10 seeds sown per plant; largest deviation, repeated by Mendel"],
      ["6", "Flower position", 100, 33, 67, "10", "10 seeds sown per plant"],
      ["7", "Stem length", 100, 28, 72, "10", "10 seeds sown per plant"],
      ["5-repeat", "Unripe pod colour (repeat)", 100, 35, 65, "10", "repeat of experiment 5: 65:35 instead of 60:40"]])

# 3. First ten plants of experiments 1 and 2 (Mendel's own illustration of plant-to-plant variation).
T10 = [(45, 12, 25, 11), (27, 8, 32, 7), (24, 7, 14, 5), (19, 10, 70, 27), (32, 11, 24, 13),
       (26, 6, 20, 6), (88, 24, 32, 13), (22, 10, 44, 9), (28, 6, 50, 14), (25, 7, 44, 18)]
wcsv(CUR / "mendel_first10_plants.csv", ["plant", "round", "wrinkled", "yellow", "green"],
     [[i + 1, *r] for i, r in enumerate(T10)])

# 4. Two-character experiment (A/a seed shape, B/b seed colour): seeds, then plants classified by progeny.
wcsv(CUR / "mendel_bifactorial_f2_seeds.csv", ["seed_shape", "seed_colour", "count"],
     [["round", "yellow", 315], ["wrinkled", "yellow", 101], ["round", "green", 108], ["wrinkled", "green", 32]])
wcsv(CUR / "mendel_bifactorial_f2_plant_genotypes.csv", ["mendel_symbol", "count"],
     [["AB", 38], ["Ab", 35], ["aB", 28], ["ab", 30], ["ABb", 65], ["aBb", 68], ["AaB", 60], ["Aab", 67], ["AaBb", 138]])

# 5. Three-character experiment (C/c seed-coat colour added): 639 plants in 27 classes.
wcsv(CUR / "mendel_trifactorial_f2_plant_genotypes.csv", ["mendel_symbol", "count"],
     [("ABC", 8), ("ABc", 14), ("AbC", 9), ("Abc", 11), ("aBC", 8), ("aBc", 10), ("abC", 10), ("abc", 7),
      ("ABCc", 22), ("AbCc", 17), ("aBCc", 25), ("abCc", 20), ("ABbC", 15), ("ABbc", 18), ("aBbC", 19), ("aBbc", 24),
      ("AaBC", 14), ("AaBc", 18), ("AabC", 20), ("Aabc", 16),
      ("ABbCc", 45), ("aBbCc", 36), ("AaBCc", 38), ("AabCc", 40), ("AaBbC", 49), ("AaBbc", 48), ("AaBbCc", 78)])

# 6. Experiments on the composition of egg and pollen cells (expected 1:1:1:1).
#    c11,c12 share the first level of the first character; c11,c21 share the first level of the second.
wcsv(CUR / "mendel_gametic_ratio_experiments.csv",
     ["experiment", "n", "first_character_levels", "second_character_levels", "c11", "c12", "c21", "c22", "mendel_classes"],
     [[1, 90, "AA|Aa", "BB|Bb", 20, 23, 25, 22, "AB|ABb|AaB|AaBb"],
      [2, 110, "Aa|aa", "Bb|bb", 31, 26, 27, 26, "AaBb|Aab|aBb|ab"],
      [3, 87, "AA|Aa", "BB|Bb", 25, 19, 22, 21, "AB|ABb|AaB|AaBb"],
      [4, 98, "Aa|aa", "Bb|bb", 24, 25, 22, 27, "AaBb|Aab|aBb|ab"],
      [5, 166, "long stem|short stem", "violet-red flower|white flower", 47, 40, 38, 41, "violet long|white long|violet short|white short"]])

# 7. Published benchmarks used only for verification (numbers as printed in the cited sources).
wcsv(PUB / "fisher_1936_tableV_as_reproduced_in_pires_branco_2010_table5.csv",
     ["group", "df", "chi_square", "probability_of_exceeding"],
     [["3:1 ratios", 7, 2.1389, 0.95], ["2:1 ratios", 8, 5.1733, 0.74], ["Bifactorial", 8, 2.8110, 0.94],
      ["Gametic ratios", 15, 3.6730, 0.9987], ["Trifactorial", 26, 15.3224, 0.95],
      ["Total (excluding illustrations)", 64, 29.1186, 0.99987],
      ["Illustrations of plant variation", 20, 12.4870, 0.90], ["Total", 84, 41.6056, 0.99993]])
wcsv(PUB / "pires_branco_2010_table6_binomial_organisation.csv",
     ["group", "df", "chi_square", "p_value_chi2"],
     [["3:1 ratios", 7, 2.1389, 0.9518], ["2:1 ratios", 8, 5.1733, 0.7389], ["Bifactorial", 8, 2.7778, 0.9475],
      ["Gametic ratios", 15, 3.6277, 0.9987], ["Trifactorial", 26, 15.1329, 0.9549],
      ["Total (excluding illustrations)", 64, 28.8506, 0.99995],
      ["Illustrations of plant variation", 20, 12.4870, 0.8983], ["Total", 84, 41.3376, 0.99998]])
wcsv(PUB / "pires_branco_2010_reported_statistics.csv", ["statistic", "value", "where_reported"],
     [["KS D, 84 p-values vs uniform", 0.1913, "Appendix B.2"], ["KS p-value, 84 p-values vs uniform", 0.0036, "Appendix B.2"],
      ["alpha-hat, selective-repeat model A", 0.201, "Section 5.1"], ["KS D at alpha-hat", 0.0623, "Section 5.1"],
      ["KS p-value at alpha-hat", 0.8804, "Section 5.1"],
      ["Monte Carlo P(total chi-square larger than observed), binomial model", 0.99998, "Table 6"]])
wcsv(PUB / "huskey_1999_webpage_chi_square.csv",
     ["experiment", "expected_dominant_integer", "expected_recessive_integer", "chi_square_2dp"],
     [[1, 5493, 1831, 0.26], [2, 6017, 2006, 0.02], [3, 697, 232, 0.37], [4, 886, 295, 0.07],
      [5, 435, 145, 0.45], [6, 643, 215, 0.40], [7, 798, 266, 0.61]])
print("curated tables written to", CUR)
