Last active
October 10, 2015 01:28
-
-
Save florin-chelaru/07973ea9bc10070078f7 to your computer and use it in GitHub Desktop.
Mihai Python Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Florin Chelaru ( florinc [at] umd [dot] edu ) | |
* Date: 10/9/13 | |
* Time: 11:13 AM | |
*/ | |
/** | |
* Overrides the default configuration settings for EpiViz | |
*/ | |
epiviz.Config.SETTINGS = { | |
// PHP/MySQL Data | |
dataServerLocation: 'http://epiviz.cbcb.umd.edu/data/', // TODO: Fill in | |
chartSaverLocation: 'src/chart_saving/save_svg.php', | |
// Navigation settings | |
zoominRatio: 0.8, | |
zoomoutRatio: 1.2, | |
navigationStepRatio: 0.2, | |
navigationDelay: 0, | |
// Plug-ins | |
dataProviders: [], | |
workspacesDataProvider: sprintf('epiviz.data.WebServerDataProvider,%s,%s', | |
'workspaces_provider', | |
'http://epiviz.cbcb.umd.edu/data/main.php'), // TODO: Fill in | |
cacheUpdateIntervalMilliseconds: 30000, | |
maxSearchResults: 12, | |
chartTypes: [ | |
'epiviz.plugins.charts.BlocksTrackType', | |
'epiviz.plugins.charts.LineTrackType', | |
'epiviz.plugins.charts.StackedLineTrackType', | |
'epiviz.plugins.charts.ScatterPlotType', | |
'epiviz.plugins.charts.GenesTrackType', | |
'epiviz.plugins.charts.HeatmapPlotType', | |
'epiviz.plugins.charts.LinePlotType', | |
'epiviz.plugins.charts.StackedLinePlotType' | |
], | |
// Chart default settings | |
chartSettings: { | |
default: { | |
colors: 'd3-category10', | |
decorations: [ | |
'epiviz.ui.charts.decoration.RemoveChartButton', | |
'epiviz.ui.charts.decoration.SaveChartButton', | |
'epiviz.ui.charts.decoration.CustomSettingsButton', | |
'epiviz.ui.charts.decoration.EditCodeButton', | |
'epiviz.ui.charts.decoration.ChartColorsButton', | |
'epiviz.ui.charts.decoration.ChartLoaderAnimation', | |
'epiviz.ui.charts.decoration.ChartResize' | |
] | |
}, | |
plot: { | |
width: 400, | |
height: 400, | |
margins: new epiviz.ui.charts.Margins(15, 30, 30, 15), | |
decorations: [ | |
'epiviz.ui.charts.decoration.ToggleTooltipButton', | |
'epiviz.ui.charts.decoration.ChartTooltip', | |
'epiviz.ui.charts.decoration.ChartFilterCodeButton' | |
] | |
}, | |
track: { | |
width: '100%', | |
height: 90, | |
margins: new epiviz.ui.charts.Margins(25, 20, 23, 10), | |
decorations: [ | |
'epiviz.ui.charts.decoration.ToggleTooltipButton', | |
'epiviz.ui.charts.decoration.ChartTooltip', | |
'epiviz.ui.charts.decoration.ChartFilterCodeButton' | |
] | |
}, | |
'epiviz.plugins.charts.GenesTrack': { | |
height: 120, | |
colors: 'genes-default' | |
}, | |
'epiviz.plugins.charts.LineTrack': { | |
decorations: [ | |
'epiviz.ui.charts.decoration.ChartGroupByMeasurementsCodeButton', | |
'epiviz.ui.charts.decoration.ChartColorByMeasurementsCodeButton' | |
] | |
}, | |
'epiviz.plugins.charts.StackedLineTrack': { | |
height: 300 | |
}, | |
'epiviz.plugins.charts.ScatterPlot': { | |
margins: new epiviz.ui.charts.Margins(15, 50, 50, 15) | |
}, | |
'epiviz.plugins.charts.HeatmapPlot': { | |
width: 800, | |
height: 400, | |
margins: new epiviz.ui.charts.Margins(80, 120, 40, 40), | |
decorations: [ | |
'epiviz.ui.charts.decoration.ChartGroupByMeasurementsCodeButton', | |
'epiviz.ui.charts.decoration.ChartOrderByMeasurementsCodeButton', | |
'epiviz.ui.charts.decoration.ChartColorByRowCodeButton' | |
], | |
colors: 'heatmap-default' | |
}, | |
'epiviz.plugins.charts.LinePlot': { | |
width: 800, | |
height: 400, | |
margins: new epiviz.ui.charts.Margins(30, 30, 50, 15), | |
decorations: [ | |
'epiviz.ui.charts.decoration.ChartGroupByMeasurementsCodeButton', | |
'epiviz.ui.charts.decoration.ChartColorByRowCodeButton', | |
'epiviz.ui.charts.decoration.ChartOrderByMeasurementsCodeButton' | |
], | |
colors: 'd3-category20b' | |
}, | |
'epiviz.plugins.charts.StackedLinePlot': { | |
width: 800, | |
height: 400, | |
margins: new epiviz.ui.charts.Margins(30, 30, 50, 15), | |
decorations: [ | |
'epiviz.ui.charts.decoration.ChartGroupByMeasurementsCodeButton', | |
'epiviz.ui.charts.decoration.ChartColorByRowCodeButton', | |
'epiviz.ui.charts.decoration.ChartOrderByMeasurementsCodeButton' | |
], | |
colors: 'd3-category20b' | |
} | |
}, | |
chartCustomSettings: { | |
'epiviz.plugins.charts.BlocksTrack': { | |
minBlockDistance: 3 | |
}, | |
'epiviz.plugins.charts.GenesTrack': { | |
}, | |
'epiviz.plugins.charts.LineTrack': { | |
step: 150, | |
showPoints: true, | |
showLines: true, | |
pointRadius: 3, | |
lineThickness: 2 | |
}, | |
'epiviz.plugins.charts.ScatterPlot': { | |
circleRadiusRatio: 0.01 | |
}, | |
'epiviz.plugins.charts.HeatmapPlot': { | |
maxColumns: 120 | |
} | |
}, | |
defaultWorkspaceSettings: { | |
name: epiviz.Config.DEFAULT_WORKSPACE_NAME, | |
content: { | |
range: { | |
seqName: 'chr11', | |
start: 99800000, | |
width: 3583180 | |
}, | |
measurements: [ | |
{ | |
id: 'genes', | |
name: 'Genes', | |
type: 'range', | |
datasourceId: 'genes', | |
datasourceGroup: 'genes', | |
dataprovider: epiviz.Config.DEFAULT_DATA_PROVIDER_ID, | |
formula: null, | |
defaultChartType: 'Genes Track', | |
annotation: null, | |
minValue: null, | |
maxValue: null, | |
metadata: ['gene', 'entrez', 'exon_starts', 'exon_ends'] | |
} | |
], | |
charts: { | |
track: [ | |
{ | |
id: 'track-genes-initial', | |
type: 'epiviz.plugins.charts.GenesTrack', | |
properties: { width: 837, height: 120, | |
margins: { top: 25, left: 20, bottom: 23, right: 10 }, | |
measurements: [0], | |
colors: { id: 'genes-default' }, | |
customSettings: {} | |
} | |
} | |
], | |
plot: [] | |
} | |
} | |
}, | |
clustering: { | |
algorithms: [ | |
'epiviz.ui.charts.transform.clustering.NoneClustering', | |
'epiviz.ui.charts.transform.clustering.AgglomerativeClustering' | |
], | |
metrics: ['epiviz.ui.charts.transform.clustering.EuclideanMetric'], | |
linkages: ['epiviz.ui.charts.transform.clustering.CompleteLinkage'] | |
}, | |
colorPalettes: [ | |
new epiviz.ui.charts.ColorPalette( | |
['#025167', '#e7003e', '#ffcd00', '#057d9f', '#970026', '#ffe373', '#ff8100'], | |
'Epiviz v1.0 Colors', 'epiviz-v1'), | |
new epiviz.ui.charts.ColorPalette( | |
['#1859a9', '#ed2d2e', '#008c47', '#010101', '#f37d22', '#662c91', '#a11d20', '#b33893'], | |
'Epiviz v2.0 Bright', 'epiviz-v2-bright'), | |
new epiviz.ui.charts.ColorPalette( | |
['#b8d2eb', '#f2aeac', '#d8e4aa', '#cccccc', '#f2d1b0', '#d4b2d3', '#ddb8a9', '#ebbfd9'], | |
'Epiviz v2.0 Light', 'epiviz-v2-light'), | |
new epiviz.ui.charts.ColorPalette( | |
['#599ad3', '#f1595f', '#79c36a', '#727272', '#f9a65a', '#9e66ab', '#cd7058', '#d77fb3'], | |
'Epiviz v2.0 Medium', 'epiviz-v2-medium'), | |
new epiviz.ui.charts.ColorPalette( | |
["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"], | |
'D3 Category 10', 'd3-category10'), | |
new epiviz.ui.charts.ColorPalette( | |
["#1f77b4", "#aec7e8", "#ff7f0e", "#ffbb78", "#2ca02c", "#98df8a", "#d62728", "#ff9896", "#9467bd", "#c5b0d5", "#8c564b", "#c49c94", "#e377c2", "#f7b6d2", "#7f7f7f", "#c7c7c7", "#bcbd22", "#dbdb8d", "#17becf", "#9edae5"], | |
'D3 Category 20', 'd3-category20'), | |
new epiviz.ui.charts.ColorPalette( | |
["#393b79", "#5254a3", "#6b6ecf", "#9c9ede", "#637939", "#8ca252", "#b5cf6b", "#cedb9c", "#8c6d31", "#bd9e39", "#e7ba52", "#e7cb94", "#843c39", "#ad494a", "#d6616b", "#e7969c", "#7b4173", "#a55194", "#ce6dbd", "#de9ed6"], | |
'D3 Category 20b', 'd3-category20b'), | |
new epiviz.ui.charts.ColorPalette( | |
["#3182bd", "#6baed6", "#9ecae1", "#c6dbef", "#e6550d", "#fd8d3c", "#fdae6b", "#fdd0a2", "#31a354", "#74c476", "#a1d99b", "#c7e9c0", "#756bb1", "#9e9ac8", "#bcbddc", "#dadaeb", "#636363", "#969696", "#bdbdbd", "#d9d9d9"], | |
'D3 Category 20c', 'd3-category20c'), | |
new epiviz.ui.charts.ColorPalette( | |
['#f9a65a', '#599ad3', '#79c36a', '#f1595f', '#727272', '#cd7058', '#d77fb3'], | |
'Genes Default', 'genes-default'), | |
new epiviz.ui.charts.ColorPalette( | |
['#1859a9', '#ed2d2e', '#008c47', '#010101', '#f37d22', '#662c91', '#a11d20', '#b33893'], | |
'Heatmap Default', 'heatmap-default') | |
] | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by: Florin Chelaru | |
* Date: 9/30/13 | |
* Time: 8:35 PM | |
*/ | |
/** | |
* @param {number} id | |
* @param {Object.<string, string>} args | |
* @param {epiviz.data.Request.Method} [method] | |
* @constructor | |
*/ | |
epiviz.data.Request = function(id, args, method) { | |
/** | |
* @type {number} | |
* @private | |
*/ | |
this._id = id; | |
/** | |
* @type {Object.<string, string>} | |
* @private | |
*/ | |
this._args = args; | |
/** | |
* @type {epiviz.data.Request.Method} | |
* @private | |
*/ | |
this._method = method; | |
}; | |
/** | |
* @enum {string} | |
*/ | |
epiviz.data.Request.Method = { | |
GET: 'get', | |
POST: 'post' | |
}; | |
/** | |
* @enum {string} | |
*/ | |
epiviz.data.Request.Action = { | |
// Server actions | |
GET_ROWS: 'getRows', | |
GET_VALUES: 'getValues', | |
GET_COMBINED: 'getCombined', | |
GET_MEASUREMENTS: 'getMeasurements', | |
SEARCH: 'search', | |
GET_SEQINFOS: 'getSeqInfos', | |
SAVE_WORKSPACE: 'saveWorkspace', | |
DELETE_WORKSPACE: 'deleteWorkspace', | |
GET_WORKSPACES: 'getWorkspaces', | |
GET_HIERARCHY: 'getHierarchy', | |
PROPAGATE_HIERARCHY_CHANGES: 'propagateHierarchyChanges', | |
// UI actions | |
ADD_MEASUREMENTS: 'addMeasurements', | |
REMOVE_MEASUREMENTS: 'removeMeasurements', | |
ADD_SEQINFOS: 'addSeqInfos', | |
REMOVE_SEQNAMES: 'removeSeqNames', | |
ADD_CHART: 'addChart', | |
REMOVE_CHART: 'removeChart', | |
CLEAR_DATASOURCE_GROUP_CACHE: 'clearDatasourceGroupCache', | |
FLUSH_CACHE: 'flushCache', | |
NAVIGATE: 'navigate', | |
REDRAW: 'redraw', | |
GET_CURRENT_LOCATION: 'getCurrentLocation', | |
WRITE_DEBUG_MSG: 'writeMsg', | |
DEMO: 'demo' | |
}; | |
/** | |
* @param {Object.<string, string>} args A map with the arguments for the request | |
* @param {epiviz.data.Request.Method} [method] | |
* @returns {epiviz.data.Request} | |
*/ | |
epiviz.data.Request.createRequest = function(args, method) { | |
return new epiviz.data.Request( | |
epiviz.data.Request._nextId++, | |
args, | |
method || epiviz.data.Request.Method.GET | |
); | |
}; | |
/** | |
* @param {{requestId: number, type: string, data: Object.<string, string>}} o | |
* @returns {epiviz.data.Request} | |
*/ | |
epiviz.data.Request.fromRawObject = function(o) { | |
return new epiviz.data.Request(o.requestId, o.data); | |
}; | |
/** | |
* @type {number} | |
* @private | |
*/ | |
epiviz.data.Request._nextId = 0; | |
/** | |
* @returns {number} | |
*/ | |
epiviz.data.Request.prototype.id = function() { return this._id; }; | |
/** | |
* @returns {epiviz.data.MessageType} | |
*/ | |
epiviz.data.Request.prototype.type = function() { return epiviz.data.MessageType.REQUEST; }; | |
/** | |
* @returns {epiviz.data.Request.Method} | |
*/ | |
epiviz.data.Request.prototype.method = function() { return this._method; }; | |
/** | |
* Concatenates all arguments in the request into one string. By default, the result will have the following format: | |
* <key1>=<val1>&<key2>=<val2>... | |
* @param {string} [keyValGlue] The token used to join keys and values; by default, this is '=' | |
* @param {string} [argGlue] The token used to join different arguments together; by default, this is '&' | |
* @returns {string} | |
*/ | |
epiviz.data.Request.prototype.joinArgs = function(keyValGlue, argGlue) { | |
keyValGlue = keyValGlue || '='; | |
argGlue = argGlue || '&'; | |
var result = sprintf('requestId%s%s', keyValGlue, this._id); | |
for (var arg in this._args) { | |
if (!this._args.hasOwnProperty(arg)) { continue; } | |
if (!Array.isArray(this._args[arg])) { | |
result += sprintf('%s%s%s%s', argGlue, arg, keyValGlue, this._args[arg] || ''); | |
} else { | |
for (var i = 0; i < this._args[arg].length; ++i) { | |
result += sprintf('%s%s[]%s%s', argGlue, arg, keyValGlue, this._args[arg][i]); | |
} | |
} | |
} | |
return result; | |
}; | |
/** | |
* @returns {boolean} | |
*/ | |
epiviz.data.Request.prototype.isEmpty = function() { | |
for (var arg in this._args) { | |
if (!this._args.hasOwnProperty(arg)) { continue; } | |
return false; | |
} | |
return true; | |
}; | |
/** | |
* @param arg | |
* @returns {?string} | |
*/ | |
epiviz.data.Request.prototype.get = function(arg) { | |
return (arg in this._args) ? this._args[arg] : null; | |
}; | |
/** | |
* @returns {{requestId: number, type: string, data: Object.<string, string>}} | |
*/ | |
epiviz.data.Request.prototype.raw = function() { | |
return { | |
requestId: this._id, | |
type: this.type(), | |
data: epiviz.utils.mapCopy(this._args) | |
}; | |
}; | |
/** | |
* @returns {epiviz.data.Request} | |
*/ | |
epiviz.data.Request.emptyRequest = function() { | |
return epiviz.data.Request.createRequest({}); | |
}; | |
/** | |
* @param {epiviz.measurements.Measurement} datasource | |
* @param {epiviz.datatypes.GenomicRange} [range] | |
* @returns {epiviz.data.Request} | |
*/ | |
epiviz.data.Request.getRows = function(datasource, range) { | |
return epiviz.data.Request.createRequest({ | |
version: epiviz.EpiViz.VERSION, | |
action: epiviz.data.Request.Action.GET_ROWS, | |
datasource: datasource.id(), | |
seqName: range ? range.seqName() : undefined, | |
start: range ? range.start() : undefined, | |
end: range ? range.end() : undefined, | |
metadata: datasource.metadata() | |
}); | |
}; | |
/** | |
* @param {epiviz.measurements.Measurement} measurement | |
* @param {epiviz.datatypes.GenomicRange} [range] | |
* @returns {epiviz.data.Request} | |
*/ | |
epiviz.data.Request.getValues = function(measurement, range) { | |
return epiviz.data.Request.createRequest({ | |
version: epiviz.EpiViz.VERSION, | |
action: epiviz.data.Request.Action.GET_VALUES, | |
datasource: measurement.datasource().id(), | |
measurement: measurement.id(), | |
seqName: range ? range.seqName() : undefined, | |
start: range ? range.start() : undefined, | |
end: range ? range.end() : undefined | |
}); | |
}; | |
/** | |
* @param {Object.<string, epiviz.measurements.MeasurementSet>} measurementsByDatasource | |
* @param {epiviz.datatypes.GenomicRange} range | |
* @returns {epiviz.data.Request} | |
*/ | |
epiviz.data.Request.getCombined = function(measurementsByDatasource, range) { | |
var rawMsByDs = {}; | |
for (var ds in measurementsByDatasource) { | |
if (!measurementsByDatasource.hasOwnProperty(ds)) { continue; } | |
rawMsByDs[ds] = (function() { | |
var ms = []; | |
measurementsByDatasource[ds].foreach(function(m) { | |
ms.push(m.id()); | |
}); | |
return ms; | |
})(); | |
} | |
return epiviz.data.Request.createRequest({ | |
version: epiviz.EpiViz.VERSION, | |
action: epiviz.data.Request.Action.GET_COMBINED, | |
seqName: range ? range.seqName() : undefined, | |
start: range ? range.start() : undefined, | |
end: range ? range.end() : undefined, | |
measurements: rawMsByDs | |
}); | |
}; | |
/** | |
* @returns {epiviz.data.Request} | |
*/ | |
epiviz.data.Request.getMeasurements = function() { | |
return epiviz.data.Request.createRequest({ | |
version: epiviz.EpiViz.VERSION, | |
action: epiviz.data.Request.Action.GET_MEASUREMENTS | |
}); | |
}; | |
/** | |
* @param {string} query | |
* @param {number} maxResults | |
* @returns {epiviz.data.Request} | |
*/ | |
epiviz.data.Request.search = function(query, maxResults) { | |
return epiviz.data.Request.createRequest({ | |
version: epiviz.EpiViz.VERSION, | |
action: epiviz.data.Request.Action.SEARCH, | |
q: query || '', | |
maxResults: maxResults | |
}); | |
}; | |
/** | |
* @returns {epiviz.data.Request} | |
*/ | |
epiviz.data.Request.getSeqInfos = function() { | |
return epiviz.data.Request.createRequest({ | |
version: epiviz.EpiViz.VERSION, | |
action: epiviz.data.Request.Action.GET_SEQINFOS | |
}); | |
}; | |
/** | |
* @param {epiviz.workspaces.Workspace} workspace | |
* @param {epiviz.Config} config | |
* @returns {epiviz.data.Request} | |
*/ | |
epiviz.data.Request.saveWorkspace = function(workspace, config) { | |
return epiviz.data.Request.createRequest({ | |
version: epiviz.EpiViz.VERSION, | |
action: epiviz.data.Request.Action.SAVE_WORKSPACE, | |
id: workspace.id(), | |
name: workspace.name(), | |
content: encodeURIComponent(JSON.stringify(workspace.raw(config).content)) | |
}, | |
epiviz.data.Request.Method.POST); | |
}; | |
/** | |
* @param {epiviz.workspaces.Workspace} workspace | |
* @returns {epiviz.data.Request} | |
*/ | |
epiviz.data.Request.deleteWorkspace = function(workspace) { | |
return epiviz.data.Request.createRequest({ | |
version: epiviz.EpiViz.VERSION, | |
action: epiviz.data.Request.Action.DELETE_WORKSPACE, | |
id: workspace.id() | |
}, | |
epiviz.data.Request.Method.POST); | |
}; | |
/** | |
* @param filter | |
* @param requestWorkspaceId | |
* @returns {epiviz.data.Request} | |
*/ | |
epiviz.data.Request.getWorkspaces = function(filter, requestWorkspaceId) { | |
return epiviz.data.Request.createRequest({ | |
version: epiviz.EpiViz.VERSION, | |
action: epiviz.data.Request.Action.GET_WORKSPACES, | |
q: filter || '', | |
ws: requestWorkspaceId | |
}); | |
}; | |
/** | |
* @param {string} datasourceGroup | |
* @param {string} [nodeId] | |
* @returns {epiviz.data.Request} | |
*/ | |
epiviz.data.Request.getHierarchy = function(datasourceGroup, nodeId) { | |
return epiviz.data.Request.createRequest({ | |
version: epiviz.EpiViz.VERSION, | |
action: epiviz.data.Request.Action.GET_HIERARCHY, | |
datasourceGroup: datasourceGroup, | |
nodeId: nodeId | |
}); | |
}; | |
/** | |
* @param {string} datasourceGroup | |
* @param {Object.<string, epiviz.ui.charts.tree.NodeSelectionType>} [selection] | |
* @param {Object.<string, number>} [order] | |
* @param {Object.<number, number>} [selectedLevels] | |
* @returns {epiviz.data.Request} | |
*/ | |
epiviz.data.Request.propagateHierarchyChanges = function(datasourceGroup, selection, order, selectedLevels) { | |
return epiviz.data.Request.createRequest({ | |
version: epiviz.EpiViz.VERSION, | |
action: epiviz.data.Request.Action.PROPAGATE_HIERARCHY_CHANGES, | |
datasourceGroup: datasourceGroup, | |
selection: selection, | |
order: order, | |
selectedLevels: selectedLevels | |
}); | |
}; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by: Florin Chelaru | |
* Date: 10/1/13 | |
* Time: 1:22 PM | |
*/ | |
/** | |
* @param {?string} [id] | |
* @param {string} websocketHost | |
* @constructor | |
* @extends {epiviz.data.DataProvider} | |
*/ | |
epiviz.data.WebsocketDataProvider = function (id, websocketHost) { | |
epiviz.data.DataProvider.call(this, id || epiviz.data.WebsocketDataProvider.DEFAULT_ID); | |
/** | |
* @type {string} | |
* @private | |
*/ | |
this._websocketHost = websocketHost; | |
/** | |
* @type {?WebSocket} | |
* @private | |
*/ | |
this._socket = null; | |
/** | |
* Variable used for testing. If this is set to false, then | |
* events triggered by instances of this class should have no | |
* UI effect | |
* @type {boolean} | |
* @private | |
*/ | |
this._useUI = (epiviz.ui.WebArgsManager.WEB_ARGS['websocketNoUI'] != 'true'); | |
/** | |
* Used for testing | |
* @type {boolean} | |
* @private | |
*/ | |
this._debug = (epiviz.ui.WebArgsManager.WEB_ARGS['debug'] == 'true'); | |
/** | |
* Callbacks hashtable, mapping request ids to their corresponding callbacks | |
* @type {Object.<string, function>} | |
* @private | |
*/ | |
this._callbacks = {}; | |
/** | |
* Stores messages as a stack until the socket is actually open | |
* @type {Array.<string>} | |
* @private | |
*/ | |
this._requestsStack = []; | |
this._initialize(); | |
}; | |
/** | |
* Copy methods from upper class | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype = epiviz.utils.mapCopy(epiviz.data.DataProvider.prototype); | |
epiviz.data.WebsocketDataProvider.constructor = epiviz.data.WebsocketDataProvider; | |
epiviz.data.WebsocketDataProvider.DEFAULT_ID = 'websocket'; | |
/** | |
* @private | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype._initialize = function () { | |
if (!this._websocketHost || this._websocketHost == 'None') { return; } | |
try { | |
this._socket = new WebSocket(this._websocketHost); | |
this._log('WebSocket - status ' + this._socket.readyState); | |
var self = this; | |
this._socket.onopen = function () { self._onSocketOpen(); }; | |
this._socket.onmessage = function (msg) { self._onSocketMessage(msg); }; | |
this._socket.onclose = function () { self._onSocketClose(); }; | |
} catch (error) { | |
this._log(error.toString()); | |
// TODO: Throw some error to be caught up in epiviz.js | |
} | |
}; | |
/** | |
* @private | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype._onSocketOpen = function () { | |
// Send the requests that were made before the socked was fully open. | |
// Those are stored in this._requestStack | |
for (var i = 0; i < this._requestsStack.length; ++i) { | |
this._socket.send(this._requestsStack[i]); | |
} | |
this._requestsStack = []; | |
}; | |
/** | |
* @private | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype._onSocketClose = function () { | |
this._socket = null; | |
}; | |
/** | |
* @param {string} message | |
* @private | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype._sendMessage = function (message) { | |
if (this.connected() && this._socket.readyState) { | |
this._socket.send(message); | |
} else { | |
this._requestsStack.push(message); | |
} | |
}; | |
/** | |
* @param {{data: string}} msg | |
* @private | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype._onSocketMessage = function (msg) { | |
this._log('Local Controller Received: ' + msg.data); | |
/** | |
* @type {{requestId: number, type: string, data: *}} | |
*/ | |
var message = JSON.parse(msg.data); | |
if (message['type'] == epiviz.data.MessageType.RESPONSE) { | |
var response = epiviz.data.Response.fromRawObject(message); | |
var callback = this._callbacks[response.id()]; | |
delete this._callbacks[response.id()]; | |
callback(response); | |
} else if (message['type'] == epiviz.data.MessageType.REQUEST) { | |
var Action = epiviz.data.Request.Action; | |
var request = epiviz.data.Request.fromRawObject(message); | |
switch (request.get('action')) { | |
case Action.ADD_MEASUREMENTS: | |
this._addMeasurements(request); | |
break; | |
case Action.REMOVE_MEASUREMENTS: | |
this._removeMeasurements(request); | |
break; | |
case Action.ADD_SEQINFOS: | |
this._addSeqInfos(request); | |
break; | |
case Action.REMOVE_SEQNAMES: | |
this._removeSeqNames(request); | |
break; | |
case Action.ADD_CHART: | |
this._addChart(request); | |
break; | |
case Action.REMOVE_CHART: | |
this._removeChart(request); | |
break; | |
case Action.CLEAR_DATASOURCE_GROUP_CACHE: | |
this._clearDatasourceGroupCache(request); | |
break; | |
case Action.FLUSH_CACHE: | |
this._flushCache(request); | |
break; | |
case Action.NAVIGATE: | |
this._navigate(request); | |
break; | |
case Action.REDRAW: | |
this._redraw(request); | |
break; | |
case Action.GET_CURRENT_LOCATION: | |
this._getCurrentLocation(request); | |
break; | |
case Action.WRITE_DEBUG_MSG: | |
this._writeDebugMsg(request); | |
break; | |
case Action.DEMO: | |
alert(JSON.stringify(message)); | |
break; | |
} | |
} | |
}; | |
/** | |
* @param {string} message | |
* @private | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype._log = function(message) { | |
if (this._debug) { console.log(message); } | |
}; | |
/** | |
* @param {epiviz.events.Event.<{result: epiviz.events.EventResult<*>}>} event | |
* @param {{result: epiviz.events.EventResult<*>}} args | |
* @private | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype._fireEvent = function(event, args) { | |
if (!this._useUI) { | |
args.result.success = true; | |
return; | |
} | |
event.notify(args); | |
}; | |
/** | |
* @returns {boolean} | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype.connected = function () { | |
return (this._socket != null); | |
}; | |
/** | |
* @param {epiviz.data.Request} request | |
* @param {function(epiviz.data.Response)} callback | |
* @override | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype.getData = function (request, callback) { | |
var message = JSON.stringify(request.raw()); | |
this._callbacks[request.id()] = callback; | |
this._sendMessage(message); | |
}; | |
// This is the interface to the websocket | |
/** | |
* @param {epiviz.data.Request} request | |
* @private | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype._addMeasurements = function (request) { | |
var result = new epiviz.events.EventResult(); | |
var measurements = new epiviz.measurements.MeasurementSet(); | |
/** | |
* @type {Array.<{ | |
* id: string, | |
* name: string, | |
* type: string, | |
* datasourceId: string, | |
* datasourceGroup: string, | |
* defaultChartType: ?string, | |
* annotation: ?Object.<string, string>, | |
* minValue: ?number, | |
* maxValue: ?number, | |
* metadata: ?Array.<string>}>} | |
*/ | |
var rawMeasurements = JSON.parse(request.get('measurements')); | |
for (var i = 0; i < rawMeasurements.length; ++i) { | |
measurements.add(new epiviz.measurements.Measurement( | |
rawMeasurements[i]['id'], | |
rawMeasurements[i]['name'], | |
rawMeasurements[i]['type'], | |
rawMeasurements[i]['datasourceId'], | |
rawMeasurements[i]['datasourceGroup'], | |
this.id(), | |
null, | |
rawMeasurements[i]['defaultChartType'], | |
rawMeasurements[i]['annotation'], | |
rawMeasurements[i]['minValue'], | |
rawMeasurements[i]['maxValue'], | |
rawMeasurements[i]['metadata'] | |
)); | |
} | |
this._fireEvent(this.onRequestAddMeasurements(), {measurements: measurements, result: result}); | |
var response = new epiviz.data.Response(request.id(), result); | |
this._sendMessage(JSON.stringify(response.raw())); | |
}; | |
/** | |
* @param {epiviz.data.Request} request | |
* @private | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype._removeMeasurements = function (request) { | |
var result = new epiviz.events.EventResult(); | |
var measurements = new epiviz.measurements.MeasurementSet(); | |
/** | |
* @type {Array.<{ | |
* id: string, | |
* name: string, | |
* type: string, | |
* datasourceId: string, | |
* datasourceGroup: string, | |
* defaultChartType: ?string, | |
* annotation: ?Object.<string, string>, | |
* minValue: ?number, | |
* maxValue: ?number, | |
* metadata: ?Array.<string>}>} | |
*/ | |
var rawMeasurements = JSON.parse(request.get('measurements')); | |
for (var i = 0; i < rawMeasurements.length; ++i) { | |
measurements.add(new epiviz.measurements.Measurement( | |
rawMeasurements[i]['id'], | |
rawMeasurements[i]['name'], | |
rawMeasurements[i]['type'], | |
rawMeasurements[i]['datasourceId'], | |
rawMeasurements[i]['datasourceGroup'], | |
this.id(), | |
null, | |
rawMeasurements[i]['defaultChartType'], | |
rawMeasurements[i]['annotation'], | |
rawMeasurements[i]['minValue'], | |
rawMeasurements[i]['maxValue'], | |
rawMeasurements[i]['metadata'] | |
)); | |
} | |
this._fireEvent(this.onRequestRemoveMeasurements(), {measurements: measurements, result: result}); | |
var response = new epiviz.data.Response(request.id(), result); | |
this._sendMessage(JSON.stringify(response.raw())); | |
}; | |
/** | |
* @param {epiviz.data.Request} request | |
* @private | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype._addSeqInfos = function (request) { | |
var result = new epiviz.events.EventResult(); | |
/** | |
* @type {Array.<Array>} | |
*/ | |
var seqInfos = JSON.parse(request.get('seqInfos')); | |
this._fireEvent(this.onRequestAddSeqInfos(), {seqInfos: seqInfos, result: result}); | |
var response = new epiviz.data.Response(request.id(), result); | |
this._sendMessage(JSON.stringify(response.raw())); | |
}; | |
/** | |
* @param {epiviz.data.Request} request | |
* @private | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype._removeSeqNames = function (request) { | |
var result = new epiviz.events.EventResult(); | |
/** | |
* @type {Array.<string>} | |
*/ | |
var seqNames = JSON.parse(request.get('seqNames')); | |
this._fireEvent(this.onRequestRemoveSeqNames(), {seqNames: seqNames, result: result}); | |
var response = new epiviz.data.Response(request.id(), result); | |
this._sendMessage(JSON.stringify(response.raw())); | |
}; | |
/** | |
* @param {epiviz.data.Request} request | |
* @private | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype._addChart = function (request) { | |
/** @type {epiviz.events.EventResult.<{id: string}>} */ | |
var result = new epiviz.events.EventResult(); | |
var measurements, datasource, datasourceGroup; | |
if (request.get('measurements') != undefined) { | |
measurements = new epiviz.measurements.MeasurementSet(); | |
/** | |
* @type {Array.<{ | |
* id: string, | |
* name: string, | |
* type: string, | |
* datasourceId: string, | |
* datasourceGroup: string, | |
* defaultChartType: ?string, | |
* annotation: ?Object.<string, string>, | |
* minValue: ?number, | |
* maxValue: ?number, | |
* metadata: ?Array.<string>}>} | |
*/ | |
var rawMeasurements = JSON.parse(request.get('measurements')); | |
for (var i = 0; i < rawMeasurements.length; ++i) { | |
measurements.add(new epiviz.measurements.Measurement( | |
rawMeasurements[i]['id'], | |
rawMeasurements[i]['name'], | |
rawMeasurements[i]['type'], | |
rawMeasurements[i]['datasourceId'], | |
rawMeasurements[i]['datasourceGroup'], | |
this.id(), | |
null, | |
rawMeasurements[i]['defaultChartType'], | |
rawMeasurements[i]['annotation'], | |
rawMeasurements[i]['minValue'], | |
rawMeasurements[i]['maxValue'], | |
rawMeasurements[i]['metadata'] | |
)); | |
} | |
} | |
datasource = request.get('datasource'); | |
datasourceGroup = request.get('datasourceGroup') || datasource; | |
this._fireEvent(this.onRequestAddChart(), { | |
type: request.get('type'), | |
visConfigSelection: new epiviz.ui.controls.VisConfigSelection(measurements, datasource, datasourceGroup), | |
result: result | |
}); | |
var response = new epiviz.data.Response(request.id(), result); | |
this._sendMessage(JSON.stringify(response.raw())); | |
}; | |
/** | |
* @param {epiviz.data.Request} request | |
* @private | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype._removeChart = function (request) { | |
var chartId = request.get('chartId'); | |
var result = new epiviz.events.EventResult(); | |
this._fireEvent(this.onRequestRemoveChart(), { | |
id: chartId, | |
result: result | |
}); | |
var response = new epiviz.data.Response(request.id(), result); | |
this._sendMessage(JSON.stringify(response.raw())); | |
}; | |
/** | |
* @param {epiviz.data.Request} request | |
* @private | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype._clearDatasourceGroupCache = function (request) { | |
var result = new epiviz.events.EventResult(); | |
this._fireEvent(this.onRequestClearDatasourceGroupCache(), { | |
datasourceGroup: request.get('datasourceGroup'), | |
result: result | |
}); | |
var response = new epiviz.data.Response(request.id(), result); | |
this._sendMessage(JSON.stringify(response.raw())); | |
}; | |
/** | |
* @param {epiviz.data.Request} request | |
* @private | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype._flushCache = function (request) { | |
var result = new epiviz.events.EventResult(); | |
this._fireEvent(this.onRequestFlushCache(), { | |
result: result | |
}); | |
var response = new epiviz.data.Response(request.id(), result); | |
this._sendMessage(JSON.stringify(response.raw())); | |
}; | |
/** | |
* @param {epiviz.data.Request} request | |
* @private | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype._navigate = function (request) { | |
/** | |
* @type {{seqName: string, start: number, end: number}} | |
*/ | |
var range = JSON.parse(request.get('range')); | |
var result = new epiviz.events.EventResult(); | |
this._fireEvent(this.onRequestNavigate(), { | |
range: epiviz.datatypes.GenomicRange.fromStartEnd(range.seqName, range.start, range.end), | |
result: result | |
}); | |
var response = new epiviz.data.Response(request.id(), result); | |
this._sendMessage(JSON.stringify(response.raw())); | |
}; | |
/** | |
* @param {epiviz.data.Request} request | |
* @private | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype._redraw = function (request) { | |
var result = new epiviz.events.EventResult(); | |
this._fireEvent(this.onRequestRedraw(), { | |
result: result | |
}); | |
var response = new epiviz.data.Response(request.id(), result); | |
this._sendMessage(JSON.stringify(response.raw())); | |
}; | |
/** | |
* @param {epiviz.data.Request} request | |
* @private | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype._getCurrentLocation = function(request) { | |
var result = new epiviz.events.EventResult(); | |
this._fireEvent(this.onRequestCurrentLocation(), { | |
result: result | |
}); | |
var response = new epiviz.data.Response(request.id(), result); | |
this._sendMessage(JSON.stringify(response.raw())); | |
}; | |
/** | |
* @param {epiviz.data.Request} request | |
* @private | |
*/ | |
epiviz.data.WebsocketDataProvider.prototype._writeDebugMsg = function(request) { | |
var msg = request.get('msg'); | |
var msgDiv = document.createElement("pre"); | |
msgDiv.innerHTML = msg.replace(/&/g, "&").replace(/\\</g,"<"); | |
var response = new epiviz.data.Response(request.id(), {msg: "that msg"}); | |
document.getElementById("chart-container").appendChild(msgDiv); | |
this._sendMessage(JSON.stringify(response.raw())); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment