aboutsummaryrefslogtreecommitdiff
path: root/gn2/wqflask/static/new/javascript/network_graph.js
blob: 480443eed44ef8acfd284f0febe440d4714aed1e (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
var default_style = [ // the stylesheet for the graph
          {    
            selector: 'node',
            style: {
              'background-color': '#666',
              'label': 'data(label)',
              'font-size': 10
            }
          },

          {
            selector: 'edge',
            style: {
              'width': 'data(width)',
              'line-color': 'data(color)',
              'target-arrow-color': '#ccc',
              'target-arrow-shape': 'triangle',
              'font-size': 8
            }
          }
      ]

var default_layout = { name: 'circle',
                fit: true, // whether to fit the viewport to the graph
                padding: 30 // the padding on fit
                //idealEdgeLength: function( edge ){ return edge.data['correlation']*10; },                
              }

window.onload=function() {
    // id of Cytoscape Web container div
    //var div_id = "cytoscapeweb";

    var cy = cytoscape({
      container: $('#cytoscapeweb'), // container to render in

      elements: elements_list,
      
      style: default_style,

      zoom: 12,
      layout: default_layout,

      zoomingEnabled: true,
      userZoomingEnabled: true,
      panningEnabled: true,
      userPanningEnabled: true,
      boxSelectionEnabled: false,
      selectionType: 'single',

      // rendering options:
      styleEnabled: true
    });

    var eles = cy.$() // var containing all elements, so elements can be restored after being removed
    
    var defaults = {
      zoomFactor: 0.05, // zoom factor per zoom tick
      zoomDelay: 45, // how many ms between zoom ticks
      minZoom: 0.1, // min zoom level
      maxZoom: 10, // max zoom level
      fitPadding: 30, // padding when fitting
      panSpeed: 10, // how many ms in between pan ticks
      panDistance: 10, // max pan distance per tick
      panDragAreaSize: 75, // the length of the pan drag box in which the vector for panning is calculated (bigger = finer control of pan speed and direction)
      panMinPercentSpeed: 0.25, // the slowest speed we can pan by (as a percent of panSpeed)
      panInactiveArea: 8, // radius of inactive area in pan drag box
      panIndicatorMinOpacity: 0.5, // min opacity of pan indicator (the draggable nib); scales from this to 1.0
      zoomOnly: false, // a minimal version of the ui only with zooming (useful on systems with bad mousewheel resolution)
      fitSelector: undefined, // selector of elements to fit
      animateOnFit: function(){ // whether to animate on fit
        return false;
      },
      fitAnimationDuration: 1000, // duration of animation on fit

      // icon class names
      sliderHandleIcon: 'fa fa-minus',
      zoomInIcon: 'fa fa-plus',
      zoomOutIcon: 'fa fa-minus',
      resetIcon: 'fa fa-expand'
    };

    cy.panzoom( defaults );
    
    function create_qtips(cy){
        cy.nodes().qtip({
                            content: function(){
                                qtip_content = ''
                                gn_link = '<b>'+'<a href="' + gn2_url + '/show_trait?trait_id=' + this.data().id.split(":")[0] + '&dataset=' + this.data().id.split(":")[1] + '" >'+this.data().id +'</a>'+'</b><br>'
                                qtip_content += gn_link
                                if (typeof(this.data().geneid) !== 'undefined'){
                                    ncbi_link = '<a href="http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=gene&cmd=Retrieve&dopt=Graphics&list_uids=' + this.data().geneid + '" >NCBI<a>'+'<br>'
                                    qtip_content += ncbi_link
                                }
                                if (typeof(this.data().omim) !== 'undefined'){
                                    omim_link = '<a href="http://www.ncbi.nlm.nih.gov/omim/' + this.data().omim + '" >OMIM<a>'+'<br>'
                                    qtip_content += omim_link
                                }
                                return qtip_content
                            },
                            position: {
                                my: 'top center',
                                at: 'bottom center'
                            },
                            style: {
                                classes: 'qtip-bootstrap',
                                tip: {
                                    width: 16,
                                    height: 8
                                }
                            }
                        });
                        
        cy.edges().qtip({
                            content: function(){
                                correlation_line = '<b>Sample r: ' + this.data().correlation + '</b><br>'
                                p_value_line = 'Sample p(r): ' + this.data().p_value + '<br>'
                                overlap_line = 'Overlap: ' + this.data().overlap + '<br>'
                                scatter_plot = '<a href="' + gn2_url + '/corr_scatter_plot?method=pearson&dataset_1=' + this.data().source.split(":")[1] + '&dataset_2=' + this.data().target.split(":")[1] + '&trait_1=' + this.data().source.split(":")[0] + '&trait_2=' + this.data().target.split(":")[0] + '" >View Scatterplot</a>'
                                return correlation_line + p_value_line + overlap_line + scatter_plot
                            },
                            position: {
                                my: 'top center',
                                at: 'bottom center'
                            },
                            style: {
                                classes: 'qtip-bootstrap',
                                tip: {
                                    width: 16,
                                    height: 8
                                }
                            }
                        });    
    }
    
    create_qtips(cy)
    
    $('#neg_slide').change(function() {
        eles.restore()

        pos_slide_val = $('#pos_slide').val();
        cy.$("node[max_corr > " + $(this).val() + "][max_corr < " + pos_slide_val + "]").remove(); 
        cy.$("edge[correlation > " + $(this).val() + "][correlation < " + pos_slide_val + "]").remove();

        cy.layout({ name: $('select[name=layout_select]').val(),
                    fit: true, // whether to fit the viewport to the graph
                    padding: 25 // the padding on fit              
                  }).run();
        
    });
    $('#pos_slide').change(function() {
        eles.restore()

        neg_slide_val = $('#neg_slide').val();
        cy.$("node[max_corr > " + neg_slide_val +"][max_corr < " + $(this).val() + "]").remove(); 
        cy.$("edge[correlation > " + neg_slide_val +"][correlation < " + $(this).val() + "]").remove();

        cy.layout({ name: $('select[name=layout_select]').val(),
                    fit: true, // whether to fit the viewport to the graph
                    padding: 25 // the padding on fit              
                  }).run();
        
    });
    
    $('#reset_graph').click(function() {
        eles.restore() 
        $('#pos_slide').val(0)
        $('#neg_slide').val(0)
        cy.layout({ name: $('select[name=layout_select]').val(),
                    fit: true, // whether to fit the viewport to the graph
                    padding: 25 // the padding on fit              
                  }).run();
    });
    
    $('select[name=focus_select]').change(function() {
        focus_trait = $(this).val()

        eles.restore()
        cy.$('edge[source != "' + focus_trait + '"][target != "' + focus_trait + '"]').remove()

        cy.layout({ name: $('select[name=layout_select]').val(),
                    fit: true, // whether to fit the viewport to the graph
                    padding: 25 // the padding on fit              
                  }).run();
    });
    
    $('select[name=layout_select]').change(function() {
        layout_type = $(this).val()
        cy.layout({ name: layout_type,
                    fit: true, // whether to fit the viewport to the graph
                    padding: 25 // the padding on fit              
                  }).run();
    });
    
    $('select[name=font_size]').change(function() {
        font_size = $(this).val()

        new_style = default_style
        new_style[0]['style']['font-size'] = parseInt(font_size)
        cy.style().fromJson(new_style).update()
    });
    $('select[name=edge_width]').change(function() {
        //eles.restore()

        //ZS: This is needed, or else it alters the original object
        orig_elements = JSON.parse(JSON.stringify(elements_list));

        width_multiplier = $(this).val()
        updated_elements = []
        for (i=0; i < orig_elements.length; i++){
            this_element = orig_elements[i]
            if ('source' in this_element['data']) {
                orig_width = this_element['data']['width']
                this_element['data']['width'] = orig_width * width_multiplier
            }
            updated_elements.push(this_element)
        }
        cy.remove(eles)
        cy.add(updated_elements)
        cy.layout({ name: $('select[name=layout_select]').val(),
                    fit: true, // whether to fit the viewport to the graph
                    padding: 25 // the padding on fit              
                  }).run();
    });

    $('select[name=edge_width]').change(function() {
        //eles.restore()

        //ZS: This is needed, or else it alters the original object
        orig_elements = JSON.parse(JSON.stringify(elements_list));

        width_multiplier = $(this).val()
        updated_elements = []
        for (i=0; i < orig_elements.length; i++){
            this_element = orig_elements[i]
            if ('source' in this_element['data']) {
                orig_width = this_element['data']['width']
                this_element['data']['width'] = orig_width * width_multiplier
            }
            updated_elements.push(this_element)
        }
        cy.remove(eles)
        cy.add(updated_elements)
        cy.layout({ name: $('select[name=layout_select]').val(),
                    fit: true, // whether to fit the viewport to the graph
                    padding: 25 // the padding on fit              
                  }).run();
    });

    $("a#image_link").click(function(e) {
      var pngData = cy.png();

      $(this).attr('href', pngData);
      $(this).attr('download', 'network_graph.png');
    });

    
};