index.ts 3.4 KB
Newer Older
Remi  PLANEL's avatar
Remi PLANEL committed
1
2
3
4
import { GeneData } from "./component/sequence/gene";
import { select, event } from "d3-selection";
import { scaleLinear } from "d3-scale";
import GenomeBrowser, { GenomeBrowserData } from "./component/genome-browser";
5

Remi  PLANEL's avatar
Remi PLANEL committed
6
const width = 1500;
Remi  PLANEL's avatar
Remi PLANEL committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const height = 300;
const genomeBrowserComponent = GenomeBrowser();
const geneData: GeneData[] = [
  {
    name: "gene 1",
    strand: "-",
    begin: 20815,
    end: 21078,
    gene: "insA",
  },
  {
    name: "gene 2",
    strand: "+",
    begin: 21181,
    end: 21399,
    gene: "yaaY",
  },
  {
    name: "gene 3",
    strand: "+",
    begin: 21407,
    end: 22348,
    gene: "ribF"
  },
  {
    name: "gene 4",
    strand: "+",
    begin: 22391,
    end: 25207,
    gene: "ileS"
  }
];
Remi  PLANEL's avatar
Remi PLANEL committed
39

Remi  PLANEL's avatar
Remi PLANEL committed
40
41
42
43
44
45
46
47
48
49
50
const genomeBrowserData: GenomeBrowserData[] = [{
  width: 1500,
  genomeWindow: {
    center: 23000,
    size: 6000
  },
  currentMousePosition: 0,
  genes: geneData,
  x: 0,
  y: 0
}];
Remi  PLANEL's avatar
Remi PLANEL committed
51
52
53
54
55

draw();



56
select("#zoom-in").on("click", function () {
Remi  PLANEL's avatar
Remi PLANEL committed
57
58
  genomeBrowserData[0].genomeWindow.size -= 1000;
  draw()
59
60
});
select("#zoom-out").on("click", function () {
Remi  PLANEL's avatar
Remi PLANEL committed
61
62
  genomeBrowserData[0].genomeWindow.size += 1000;
  draw()
63
});
Remi  PLANEL's avatar
Remi PLANEL committed
64

65
66
67
68
/*
    FUNCTIONS
*/
function draw() {
Remi  PLANEL's avatar
Remi PLANEL committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  // 
  const computedGenomeBrowserData: GenomeBrowserData[] = genomeBrowserData
    .map(function (genomesBrowser, i) {
      const { width, genomeWindow: { center, size }, genes } = genomesBrowser;
      const genomeWindowBoundaries = getGenomeWindow(center, size);
      const xScale = scaleLinear()
        .domain(genomeWindowBoundaries)
        .range([0, width]);
      // Construct clickHandler function that depends of the current window.
      const clickHandler = function ([begin, end]: [number, number]) {
        genomesBrowser.genomeWindow.center = (end + begin) / 2;
        draw();
      };

      // Filter genes in order to display those visible
      const visibleGenes = genes.filter(
        gene => gene.end > genomeWindowBoundaries[0] || gene.begin < genomeWindowBoundaries[1]
      );

Remi  PLANEL's avatar
Remi PLANEL committed
88

Remi  PLANEL's avatar
Remi PLANEL committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
      const dragStartCallback = function (elem: SVGElement) {
        select(elem).classed("active", true);
        genomesBrowser.currentMousePosition = xScale
          .invert(event.x);
      }

      const draggedCallback = function () {
        const mousePosition = xScale
          .invert(event.x);

        const diff = genomesBrowser.currentMousePosition - mousePosition;
        genomesBrowser.currentMousePosition = mousePosition;
        genomeBrowserData[i].genomeWindow.center += diff;
        draw();
      };
      const dragendedCallback = function (elem: SVGElement) {
        select(elem).classed("active", false);
      }

      const newGenomeBrowser = {
        ...genomesBrowser,
        genes: visibleGenes.map(function (gene) {
          gene.eventHandler = {
            click: clickHandler
          };
          return gene;
        }),
        scale: xScale,
        eventHandler: {
          dragged: draggedCallback,
          dragstarted: dragStartCallback,
          dragended: dragendedCallback
        }
      };

      return newGenomeBrowser;

    });
  const svg = select<SVGElement, any>("svg")
    .attr("width", width + 1)
    .attr("height", height);

  svg
    .datum(computedGenomeBrowserData)
    .call(genomeBrowserComponent, width, height);
Remi  PLANEL's avatar
Remi PLANEL committed
134

135
}
Remi  PLANEL's avatar
Remi PLANEL committed
136
137
138
139
140
141
function getGenomeWindow(center: number, genomeWindowSize: number): [number, number] {
  const halfWindow = genomeWindowSize / 2;
  return [center - halfWindow, center + halfWindow]

}

142