genome-browser.ts 1.82 KB
Newer Older
1
import { GenomeBrowserState, GeneData, GenomeBrowserData } from "../types";
2
3
4
5
6
7
8
9
10
11
12
import { format as d3Format } from "d3-format";
import { scaleOrdinal } from "d3-scale";
import { color } from "d3";
import { schemeSet1 } from "d3-scale-chromatic";

const geneColor = scaleOrdinal(
  // schemeDark2
  // schemeCategory10
  schemeSet1
);
export default function genomeBrowserData(
13
  state: GenomeBrowserState,
14
15
  brushHandler: any,
  clickHandler: any
16
): GenomeBrowserData {
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
  const {
    window,
    width,
    chromosomeSize,
    genes,
    chromosome: { title: chromosomeTitle },
    selectedChromosome: { title: selectedChromosomeTitle }
  } = state;
  const intervalFormatter = d3Format(".3s");
  const chromInterval: [number, number] = [0, chromosomeSize]
  return {
    width,
    chromosome: {
      size: chromosomeSize,
      genes,
      ruler: {
        title: chromosomeTitle + " (" + chromosomeSize + " bp)",
        interval: chromInterval,
        window,
        maxWindowSize: chromosomeSize,
        eventHandler: {
          brushed: (scale: any) => brushHandler(scale, state)
        }
      }
    },
    selectedChromosome: {
      window,
      genes: state.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: (geneLocation: [number, number]) => clickHandler(geneLocation, state)
          },
          fill: (fill) ? fill.toString() : "lightgray",
          stroke
        }
      }),
      ruler: {
        title: selectedChromosomeTitle
          + " [" + intervalFormatter(window[0]) + ", " + intervalFormatter(window[1]) + "]",
        interval: window
      }
    },
  }
}