{
  "fdstools_visversion": "2.0.1",
  "width": 600,
  "height": 400,
  "signals": [
    {
      "name": "hovered",
      "init": false,
      "streams": [
        {"type": "symbol:mouseover", "expr": "datum"},
        {"type": "symbol:mouseout", "expr": "false"},
        {"type": "path:mouseover", "expr": "datum"},
        {"type": "path:mouseout", "expr": "false"}
      ]
    }
  ],
  "data": [
    {
      "name": "raw",
      "values": "VALUES HERE",
      "format": {"type": "tsv"},
      "transform": [
        {
          "type": "formula",
          "field": "samplemarker",
          "expr": "datum.sample + '\t' + datum.marker"
        },
        {
          "type": "formula",
          "field": "markerallele",
          "expr": "datum.marker + '\t' + datum.allele"
        },
        {
          "type": "formula",
          "field": "samplemarkerallele",
          "expr": "datum.samplemarker + '\t' + datum.allele"
        }
      ]
    },
    {
      "name": "homozygotes",
      "source": "raw",
      "transform": [
        {
          "type": "aggregate",
          "groupby": ["samplemarker"],
          "summarize": {"allele": "distinct"}
        },
        {
          "type": "filter",
          "test": "datum.distinct_allele == 1"
        },
        {
          "type": "lookup",
          "on": "raw",
          "onKey": "samplemarker",
          "keys": ["samplemarker"],
          "as": ["alleleobj"]
        },
        {
          "type": "formula",
          "field": "markerallele",
          "expr": "datum.alleleobj.markerallele"
        },
        {
          "type": "aggregate",
          "groupby": ["markerallele"],
          "summarize": {"*": "count"}
        }
      ]
    },
    {
      "name": "nodes",
      "source": "raw",
      "transform": [
        {
          "type": "aggregate",
          "groupby": ["marker", "allele"],
          "summarize": {"*": "count"}
        },
        {
          "type": "rank"
        },
        {
          "type": "formula",
          "field": "markerallele",
          "expr": "datum.marker + '\t' + datum.allele"
        },
        {
          "type": "lookup",
          "on": "homozygotes",
          "onKey": "markerallele",
          "keys": ["markerallele"],
          "as": ["homcount"],
          "default": {"count": 0}
        }
      ]
    },
    {
      "name": "edges",
      "source": "raw",
      "transform": [
        {
          "type": "cross",
          "with": "nodes",
          "output": {"left": "samples", "right": "source"},
          "filter": "datum.source.markerallele == datum.samples.markerallele"
        },
        {
          "type": "cross",
          "with": "nodes",
          "output": {"left": "a", "right": "target"},
          "filter": "datum.a.source.marker == datum.target.marker && datum.a.source.allele < datum.target.allele"
        },
        {
          "type": "formula",
          "field": "samplemarkerallele",
          "expr": "datum.a.samples.sample + '\t' + datum.target.marker + '\t' + datum.target.allele"
        },
        {
          "type": "lookup",
          "on": "raw",
          "onKey": "samplemarkerallele",
          "keys": ["samplemarkerallele"],
          "as": ["targetsample"],
          "default": {"sample": "NOTFOUND"}
        },
        {
          "type": "filter",
          "test": "datum.targetsample.sample != 'NOTFOUND'"
        },
        {
          "type": "formula",
          "field": "source",
          "expr": "datum.a.source.rank - 1"
        },
        {
          "type": "formula",
          "field": "target",
          "expr": "datum.target.rank - 1"
        },
        {
          "type": "aggregate",
          "groupby": ["source", "target"],
          "summarize": {"*": "count"}
        }
      ]
    },
    {
      "name": "layout",
      "source": "nodes",
      "transform": [
        {
          "type": "force",
          "links": "edges",
          "linkDistance": 30,
          "linkStrength": 0.1,
          "charge": -20,
          "iterations": 1000
        }
      ]
    }
  ],
  "scales": [
    {
      "name": "c",
      "type": "ordinal",
      "range": [
        "#1f77b4", "#aec7e8", "#ff7f0e", "#ffbb78", "#4E843C", "#2ca02c",
        "#98df8a", "#F73939", "#e05f5f", "#ff9896", "#9467bd", "#c5b0d5",
        "#8c564b", "#c49c94", "#e377c2", "#f7b6d2", "#7f7f7f", "#c7c7c7",
        "#bcbd22", "#dbdb8d", "#17becf", "#9edae5"],
      "domain": {"data": "raw", "field": "marker", "sort": true}
    },
    {
      "name": "legend_sizes",
      "type": "ordinal",
      "range": [1, 5, 10, 20, 50],
      "domain": [1, 5, 10, 20, 50]
    }
  ],
  "legends": [
    {
      "fill": "c",
      "properties": {
        "symbols": {
          "size": {"value": 100},
          "stroke": {"value": "transparent"},
          "fillOpacity": {"value": 0.8}
        }
      }
    },
    {"size": "legend_sizes"}
  ],
  "marks": [
    {
      "type": "path",
      "from": {
        "data": "edges",
        "transform": [
          {
            "type": "lookup",
            "on": "layout",
            "keys": ["source", "target"],
            "as": ["_source", "_target"]
          },
          {
            "type": "sort",
            "by": "_source.marker"
          },
          {"type": "linkpath"}
        ]
      },
      "properties": {
        "update": {
          "path": {"field": "layout_path"},
          "stroke": {"scale": "c", "field": "_source.marker"},
          "strokeWidth": {"field": "count", "mult": 0.5},
          "strokeOpacity": [
            {"test": "hovered && (hovered == datum || hovered == datum._source || hovered == datum._target)", "value": 1},
            {"value": 0.3}
          ]
        }
      }
    },
    {
      "type": "symbol",
      "from": {"data": "layout"},
      "properties": {
        "enter": {
          "size": {"field": "count"}
        },
        "update": {
          "x": {"field": "layout_x"},
          "y": {"field": "layout_y"},
          "fill": {"scale": "c", "field": "marker"},
          "stroke": {"scale": "c", "field": "marker"}
        }
      }
    },
    {
      "type": "symbol",
      "from": {"data": "layout"},
      "properties": {
        "enter": {
          "size": {"field": "homcount.count"}
        },
        "update": {
          "x": {"field": "layout_x"},
          "y": {"field": "layout_y"},
          "fill": {"value": "black"},
          "fillOpacity": {"value": 0.7},
          "stroke": {"value": "transparent"}
        }
      }
    }
  ]
}