main-genome-browser.ts 4.1 KB
Newer Older
Remi  PLANEL's avatar
Remi PLANEL committed
1
2
3
4
5
6
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { GeneData, GenomeBrowserData, State } from "./types";
import { select, event } from "d3-selection";
import { scaleOrdinal } from "d3-scale";
import GenomeBrowser from "./component/genome-browser";
import { color } from "d3";
import { format as d3Format } from "d3-format";
import { schemeSet1, schemeDark2, schemeCategory10 } from "d3-scale-chromatic";

const width = 1500;
const height = 300;
const genomeBrowserComponent = GenomeBrowser();
const geneColor = scaleOrdinal(
  // schemeDark2
  // schemeCategory10
  schemeSet1
);

const geneData: GeneData[] = [
  {
    name: "gene 1",
    strand: "-",
    begin: 20815,
    end: 21078,
    gene: "insA",
  },
  {
    name: "gene 2",
    strand: "+",
    begin: 21181,
    end: 21400,
    gene: "yaaY",
  },
  {
    name: "gene 3",
    strand: "+",
    begin: 21407,
    end: 22348,
    gene: "ribF"
  },
  {
    name: "gene 4",
    strand: "+",
    begin: 22391,
    end: 23207,
    gene: "ileS"
  },
  {
    name: "gene 4",
    strand: "+",
    begin: 23391,
    end: 24207,
    gene: "ileS"
  },
  {
    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"
  },

];


const state: State = {
  width: 1500,
  chromosomeSize: 75000,
  window: [20000, 26000],
  genes: geneData,
  chromosome: {
    title: "Genome XXXX",
  },
  selectedChromosome: {
    title: "Chromosome X"
  }
}


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

draw();

select("#zoom-in").on("click", function () {
  const [start, end] = state.window;
  state.window = [start + 500, end - 500];
  draw()
});
select("#zoom-out").on("click", function () {
  const [start, end] = state.window;
  state.window = [start - 500, end + 500];
  draw()
});

/*
    FUNCTIONS
*/
function draw() {
  // 
  const computedGenomeBrowserData: GenomeBrowserData =
    getGenomeBrowserData(state);
  svg
    .datum([computedGenomeBrowserData])
    .call(genomeBrowserComponent, width, height);

}

function getGenomeBrowserData(state: State) {
  const {
    window,
    width,
    chromosomeSize,
    genes,
    chromosome: { title: chromosomeTitle },
    selectedChromosome: { title: selectedChromosomeTitle }
  } = state;
  const intervalFormatter = d3Format(".3s");
  // Callback when brushed => modify data and redraw the genome + axis
  const brushedCallback = function (scale: any) {
    if (!event.sourceEvent) return;
    if (event.selection) {
      const { selection: [x1, x2] } = event;
      const newwindow: [number, number] = [scale.invert(x1), scale.invert(x2)];
      state.window = newwindow;
      genomeBrowserComponent.updateSelectedChromosome(
        getGenomeBrowserData(state)
      );
    }
  };
  const clickHandler = function ([begin, end]: [number, number]) {
    const centerGene = (end + begin) / 2;
    const sizeWindow = window[1] - window[0];
    state.window = [centerGene - sizeWindow / 2, centerGene + sizeWindow / 2];
    draw();
  };

  const chromInterval: [number, number] = [0, chromosomeSize]
  return {
    width,
    chromosome: {
      size: chromosomeSize,
      genes,
      rule: {
        title: chromosomeTitle + " (" + chromosomeSize + " bp)",
        interval: chromInterval,
        window,
        eventHandler: {
          brushed: brushedCallback
        }
      }
    },
    selectedChromosome: {
      window,
      genes: genes.filter(
        (gene: GeneData) => gene.end > window[0] && gene.begin < window[1]
      ).map(function (gene) {
        const fill = color(geneColor(gene.strand));
        const stroke = (fill) ? fill.darker(1).toString() : "lighgray";
        return {
          ...gene,
          eventHandler: {
            click: clickHandler
          },
          fill: (fill) ? fill.toString() : "lightgray",
          stroke
        }
      }),
      rule: {
        title: selectedChromosomeTitle
          + " [" + intervalFormatter(window[0]) + ", " + intervalFormatter(window[1]) + "]",
        interval: window
      }
    },
  }
}