index.ts 4.79 KB
Newer Older
Remi  PLANEL's avatar
Remi PLANEL committed
1
import { GeneData, GenomeBrowserData } from "./types";
Remi  PLANEL's avatar
Remi PLANEL committed
2
3
import { select, event } from "d3-selection";
import { scaleLinear } from "d3-scale";
Remi  PLANEL's avatar
Remi PLANEL committed
4
import GenomeBrowser from "./component/genome-browser";
Remi  PLANEL's avatar
Remi PLANEL committed
5
import { color } from "d3";
Remi  PLANEL's avatar
Remi PLANEL committed
6
7
import genomeBrowser from "./component/genome-browser";
import { filter } from "rxjs/operators";
8

Remi  PLANEL's avatar
Remi PLANEL committed
9
const width = 1500;
Remi  PLANEL's avatar
Remi PLANEL committed
10
11
const height = 300;
const genomeBrowserComponent = GenomeBrowser();
Remi  PLANEL's avatar
Remi PLANEL committed
12

Remi  PLANEL's avatar
Remi PLANEL committed
13
14
15
16
17
18
19
20
21
22
23
24
const geneData: GeneData[] = [
  {
    name: "gene 1",
    strand: "-",
    begin: 20815,
    end: 21078,
    gene: "insA",
  },
  {
    name: "gene 2",
    strand: "+",
    begin: 21181,
Remi  PLANEL's avatar
Remi PLANEL committed
25
    end: 21400,
Remi  PLANEL's avatar
Remi PLANEL committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  },
  {
    name: "gene 4",
    strand: "-",
    begin: 25304,
    end: 25956,
    gene: "foo"
  },
  {
    name: "gene 4",
    strand: "+",
    begin: 26004,
    end: 26329,
    gene: "foo"
  },
  {
    name: "gene 4",
    strand: "+",
    begin: 29004,
    end: 29429,
    gene: "foo"
  },

Remi  PLANEL's avatar
Remi PLANEL committed
64
];
Remi  PLANEL's avatar
Remi PLANEL committed
65
const chromosomeSize = 75000;
Remi  PLANEL's avatar
Remi PLANEL committed
66
67
68
69
70
71
72
const genomeBrowserData: GenomeBrowserData[] = [{
  width: 1500,
  genomeWindow: {
    center: 23000,
    size: 6000
  },
  currentMousePosition: 0,
Remi  PLANEL's avatar
Remi PLANEL committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  chromosome: {
    size: chromosomeSize,
    genes: geneData
  },
  axis: {
    global: {
      title: "Genome XXXX (" + chromosomeSize + " bp)",
      interval: [0, chromosomeSize],
      window: [0, 0]
    },
    chromosome: {
      title: "Chromosome X ",
      interval: [0, 0]
    }
  }
Remi  PLANEL's avatar
Remi PLANEL committed
88
}];
Remi  PLANEL's avatar
Remi PLANEL committed
89
90
91
92
const svg = select<SVGElement, any>("svg")
  .attr("width", width + 1)
  .attr("height", height);

Remi  PLANEL's avatar
Remi PLANEL committed
93
94
95
96
97

draw();



98
select("#zoom-in").on("click", function () {
Remi  PLANEL's avatar
Remi PLANEL committed
99
100
  genomeBrowserData[0].genomeWindow.size -= 1000;
  draw()
101
102
});
select("#zoom-out").on("click", function () {
Remi  PLANEL's avatar
Remi PLANEL committed
103
104
  genomeBrowserData[0].genomeWindow.size += 1000;
  draw()
105
});
Remi  PLANEL's avatar
Remi PLANEL committed
106

107
108
109
110
/*
    FUNCTIONS
*/
function draw() {
Remi  PLANEL's avatar
Remi PLANEL committed
111
112
  // 
  const computedGenomeBrowserData: GenomeBrowserData[] = genomeBrowserData
Remi  PLANEL's avatar
Remi PLANEL committed
113
114
    .map(function (genomesBrowser: GenomeBrowserData, i) {
      const { width, genomeWindow: { center, size }, chromosome: { genes } } = genomesBrowser;
Remi  PLANEL's avatar
Remi PLANEL committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
      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]
      );

      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);
      }

Remi  PLANEL's avatar
Remi PLANEL committed
149
150
      const globalAxisWindow = getGenomeWindow(center, size);

Remi  PLANEL's avatar
Remi PLANEL committed
151
152
      const newGenomeBrowser = {
        ...genomesBrowser,
Remi  PLANEL's avatar
Remi PLANEL committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
        chromosome: {
          ...genomesBrowser.chromosome,
          genes: visibleGenes.map(function (gene) {
            const fill = gene.strand === "+" ? color("darkred") : color("darkblue");
            const stroke = (fill) ? fill.darker(1).toString() : "lighgray"
            return {
              ...gene,
              eventHandler: {
                click: clickHandler
              },
              fill: (fill) ? fill.toString() : "lightgray",
              stroke
            }
          })
        },
Remi  PLANEL's avatar
Remi PLANEL committed
168
        scale: xScale,
Remi  PLANEL's avatar
Remi PLANEL committed
169
170
171
172
173
174
175
176
177
178
179
180
181
        axis: {
          ...genomesBrowser.axis,
          global: {
            ...genomesBrowser.axis.global,
            window: globalAxisWindow
          },
          chromosome: {
            ...genomesBrowser.axis.chromosome,
            interval: globalAxisWindow

          }

        },
Remi  PLANEL's avatar
Remi PLANEL committed
182
183
184
185
186
187
188
189
190
191
        eventHandler: {
          dragged: draggedCallback,
          dragstarted: dragStartCallback,
          dragended: dragendedCallback
        }
      };

      return newGenomeBrowser;

    });
Remi  PLANEL's avatar
Remi PLANEL committed
192

Remi  PLANEL's avatar
Remi PLANEL committed
193
194
195
196

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

198
}
Remi  PLANEL's avatar
Remi PLANEL committed
199
200
201
202
203
204
function getGenomeWindow(center: number, genomeWindowSize: number): [number, number] {
  const halfWindow = genomeWindowSize / 2;
  return [center - halfWindow, center + halfWindow]

}

205