genome-browser.ts 2.5 KB
Newer Older
1
import SelectedChromosome from "./selected-chromosome";
Remi  PLANEL's avatar
Remi PLANEL committed
2
import BrushableChromosomeRule from "./ruler/brushable-chromosome";
3
4
// D3
import { select, Selection } from "d3-selection";
5

6
//types
Remi  PLANEL's avatar
Remi PLANEL committed
7
import { GenomeBrowserData, BrushableAxisData, SelectedChromosomeData } from "../types";
8
9
10
11



export default function () {
12
13
14
15
  const classes = {
    chromosomeRule: "chromosome-rule",
    selectedChromosome: "selected-chromosomes"
  };
16
  const wholeChromosomeRule = BrushableChromosomeRule();
17
18
  const selectedChromosomeComponent = SelectedChromosome();

19
20
  let width = 900;
  let genomesBrowserU: Selection<SVGElement, GenomeBrowserData, SVGElement, any> | null = null;
21
22
23

  function genomeBrowser(
    _selection: Selection<SVGElement, Array<GenomeBrowserData>, HTMLElement, any>,
24
    w: number,
25
26
    height: number
  ) {
27
    width = w;
28
29
30
    _selection.each(function (_data: Array<GenomeBrowserData>) {
      const container = select(this);
      const genomeBrowser = container
Remi  PLANEL's avatar
Remi PLANEL committed
31
        .selectAll<SVGElement, GenomeBrowserData>(".genome-browser")
32
33
34
        .data(_data);

      //ENTER
Remi  PLANEL's avatar
Remi PLANEL committed
35
      const genomeBrowserE = genomeBrowser
36
        .enter()
Remi  PLANEL's avatar
Remi PLANEL committed
37
        .append<SVGElement>("g")
38
39
        .classed("genome-browser", true);

40
41
      genomeBrowserE.append("g").classed(classes.chromosomeRule, true);
      genomeBrowserE.append("g")
Remi  PLANEL's avatar
Remi PLANEL committed
42
43
44
        .classed(classes.selectedChromosome, true)
        .attr("transform", "translate(0,70)");

45
46
47
48
      //EXIT
      genomeBrowser.exit().remove();

      //UPDATE
49
      genomesBrowserU = genomeBrowser.merge(genomeBrowserE);
Remi  PLANEL's avatar
Remi PLANEL committed
50

51

52
      genomesBrowserU.each(function (data) {
Remi  PLANEL's avatar
Remi PLANEL committed
53
54
55
56
57
58
        const {
          chromosome: { rule: chromosomeRule },
          selectedChromosome: { genes: selectedGenes, rule: selectedChromosomeRule },
          // axis: { chromosome, global }
        } = data;
        updateWholeChromosomeAxis(chromosomeRule);
59
        updateSelectedChromosome(data);
60
61
62
      });
    });
  }
63

64
65
  function updateSelectedChromosome(data: GenomeBrowserData) {
    if (genomesBrowserU !== null) {
66
      genomesBrowserU
67
68
69
        .select<SVGElement>("." + classes.selectedChromosome)
        .datum<SelectedChromosomeData[]>([data.selectedChromosome])
        .call(selectedChromosomeComponent, width);
70
    }
71
  }
72

73
74
75
  function updateWholeChromosomeAxis(axis: BrushableAxisData) {
    if (genomesBrowserU) {
      genomesBrowserU
Remi  PLANEL's avatar
Remi PLANEL committed
76
        .select<SVGGElement>("." + classes.chromosomeRule)
Remi  PLANEL's avatar
Remi PLANEL committed
77
        .datum(axis)
78
        .call(wholeChromosomeRule, width, 0);
79
    }
80
  }
81
  genomeBrowser.updateSelectedChromosome = updateSelectedChromosome;
82
83
84
  return genomeBrowser;
}

85
86