Volcanoplot.vue 5.06 KB
Newer Older
Sascha Herzinger's avatar
Sascha Herzinger 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
<template>
    <chart v-on:resize="resize">
        <control-panel class="fjs-control-panel" name="Volcanoplot Panel">
            <data-box class="fjs-data-box"
                      header="Numerical Array Variables"
                      :data-types="['numerical_array']"
                      v-on:update="update_arrays">
            </data-box>
            <hr class="fjs-seperator"/>
            <fieldset class="fjs-fieldset">
                <legend>Differential Expression Analysis</legend>
                <div>
                    <label>
                        <input type="radio" value="limma" v-model="rankingMethod">
                        Limma
                    </label>
                </div>
                <div>
                    <label>
                        <input type="radio" value="DESeq2" v-model="rankingMethod">
                        DESeq2
                    </label>
                </div>
            </fieldset>
        </control-panel>
        <svg :height="height" :width="width">
            <g :transform="`translate(${margin.left}, ${margin.top})`">
                <crosshair :width="padded.width" :height="padded.height"/>
                <image :xlink:href="dataUrl" :width="padded.width" :height="padded.height"></image>
            </g>
        </svg>
    </chart>
</template>

<script>
  import Chart from '../components/Chart.vue'
  import ControlPanel from '../components/ControlPanel.vue'
  import DataBox from '../components/DataBox.vue'
  import RunAnalysis from '../mixins/run-analysis'
  import store from '../../store/store'
  import deepFreeze from 'deep-freeze-strict'
  import getHDPICanvas from '../../utils/high-dpi-canvas'
  import * as d3 from 'd3'
  import Crosshair from '../components/Crosshair.vue'
  export default {
    name: 'volcanoplot',
    components: {Crosshair, DataBox, ControlPanel, Chart},
    mixins: [RunAnalysis],
    data () {
      return {
        height: 0,
        width: 0,
        arrays: [],
        rankingMethod: 'limma',
        results: {
          pValue: [],
          logFC: [],
          baseMean: []
        },
        dataUrl: ''
      }
    },
    computed: {
      args () {
        return {
          numerical_arrays: this.arrays,
          id_filter: [],
          ranking_method: this.rankingMethod,
          subsets: store.getters.subsets
        }
      },
      validArgs () {
        return this.args.numerical_arrays.length > 0
      },
      margin () {
        const left = this.width / 20
        const top = this.height / 20
        const right = this.width / 20
        const bottom = this.height / 20
        return {left, top, right, bottom}
      },
      padded () {
        const width = this.width - this.margin.left - this.margin.right
        const height = this.height - this.margin.top - this.margin.bottom
        return {width, height}
      },
      pointSize () {
        return this.padded.width / 125
      },
      canvas () {
        return getHDPICanvas(this.padded.width, this.padded.height)
      },
      rawPoints () {
        // TODO: make configurable
        const x = this.results.pValue
        const y = this.results.logFC
        return { x, y }
      },
      scales () {
        const x = d3.scaleLinear()
          .domain(d3.extent(this.rawPoints.x))
          .range([0, this.padded.width])
        const y = d3.scaleLinear()
          .domain(d3.extent(this.rawPoints.y))
          .range([this.padded.height, 0])
        return { x, y }
      },
      scaledPoints () {
        return this.rawPoints.x.map(_, i => {
          const x = this.scales.x(this.rawPoints.x[i])
          const y = this.scales.y(this.rawPoints.y[i])
          return { x, y }
        })
      },
      axis () {
        return {}
      }
    },
    methods: {
      resize (width, height) {
        this.width = width
        this.height = height
      },
      update_arrays (ids) {
        this.arrays = ids
      },
      runAnalysisWrapper (args) {
        this.runAnalysis('compute-volcanoplot', args)
          .then(response => {
            const results = JSON.parse(response)
            deepFreeze(results) // massively improve performance by telling Vue that the objects properties won't change
          })
          .catch(error => console.error(error))
      },
      drawPoints (points) {
        const ctx = this.canvas.getContext('2d')
        ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
        points.forEach(d => {
          ctx.beginPath()
          ctx.fillStyle = '#000'
          ctx.fillRect(d.x - this.pointSize / 2, d.y - this.pointSize / 2, this.pointSize, this.pointSize)
        })
        this.dataUrl = this.canvas.toDataURL('image/png')
      }
    },
    watch: {
      'args': {
        handler: function (newArgs, oldArgs) {
          if (this.validArgs) {
            this.runAnalysisWrapper(newArgs)
          }
        }
      },
      'scaledPoints': {
        handler: function (newPoints) {
          this.$nextTick(() => {
            this.drawPoints(newPoints)
          })
        }
      }
    }
  }
</script>

<style lang="sass" scoped>
    @import '~assets/base.sass'
</style>