Last active
August 29, 2015 14:01
-
-
Save florin-chelaru/05a14d64a8d7b04c5dfc to your computer and use it in GitHub Desktop.
UCSC Demo 2
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: 5/6/14 | |
* Time: 2:41 PM | |
*/ | |
goog.provide('epiviz.plugins.data.UCSCDataProvider'); | |
/** | |
* @constructor | |
* @param {string} id | |
* @param {string} endpoint | |
* @extends {epiviz.data.DataProvider} | |
*/ | |
epiviz.plugins.data.UCSCDataProvider = function (id, endpoint) { | |
epiviz.data.DataProvider.call(this, id || epiviz.plugins.data.UCSCDataProvider.DEFAULT_ID); | |
/** | |
* @type {string} | |
* @private | |
*/ | |
this._endpoint = endpoint; | |
this._refGene = new epiviz.measurements.Measurement( | |
'refGene', // The column in the data source table that contains the values for this feature measurement | |
'refGene', // A name not containing any special characters (only alphanumeric and underscores) | |
epiviz.measurements.Measurement.Type.RANGE, | |
'refGene', // Data source: the table/data frame containing the data | |
'ucsc_refGene', // An identifier for use to group with other measurements from different data providers | |
// that have the same seqName, start and end values | |
this.id(), // Data provider | |
null, // Formula: always null for measurements coming directly from the data provider | |
'any', // Default chart type filter | |
null, // Annotation | |
null, // Min Value | |
null, // Max Value | |
['bin', 'exonCount', 'exonFrames', 'exon_starts', 'exon_ends', 'gene'] // Metadata | |
); | |
this._heartMet = new epiviz.measurements.Measurement( | |
'percentMeth', // The column in the data source table that contains the values for this feature measurement | |
'BioChain_heart:Ventricle_L__A705026_rep2', // A name not containing any special characters (only alphanumeric and underscores) | |
epiviz.measurements.Measurement.Type.FEATURE, | |
'wgEncodeHaibMethylRrbsBcleftventriclen41BiochainSitesRep2', // Data source: the table/data frame containing the data | |
'ucsc_wgEncodeHaibMethylRrbsBcleftventriclen41BiochainSitesRep2', // An identifier for use to group with other measurements from different data providers | |
// that have the same seqName, start and end values | |
this.id(), // Data provider | |
null, // Formula: always null for measurements coming directly from the data provider | |
'any', // Default chart type filter | |
null, // Annotation | |
0, // Min Value | |
100, // Max Value | |
['score', 'reserved', 'readCount'] // Metadata | |
); | |
this._colMap = { | |
refGene: { | |
seqName: 'chrom', | |
start: 'txStart', | |
end: 'txEnd', | |
strand: 'strand' | |
}, | |
wgEncodeHaibMethylRrbsBcleftventriclen41BiochainSitesRep2: { | |
seqName: 'chrom', | |
start: 'chromStart', | |
end: 'chromEnd', | |
strand: 'strand' | |
} | |
}; | |
}; | |
/** | |
* Copy methods from upper class | |
*/ | |
epiviz.plugins.data.UCSCDataProvider.prototype = epiviz.utils.mapCopy(epiviz.data.DataProvider.prototype); | |
epiviz.plugins.data.UCSCDataProvider.constructor = epiviz.plugins.data.UCSCDataProvider; | |
epiviz.plugins.data.UCSCDataProvider.DEFAULT_ID = 'ucsc'; | |
/** | |
* @param {epiviz.data.Request} request | |
* @param {function(epiviz.data.Response)} callback | |
* @override | |
*/ | |
epiviz.plugins.data.UCSCDataProvider.prototype.getData = function (request, callback) { | |
var requestId = request.id(); | |
var action = request.get('action'); | |
var seqName = request.get('seqName'); | |
var start = request.get('start'); | |
var end = request.get('end'); | |
var datasource = request.get('datasource'); | |
if (datasource) { | |
var colMap = this._colMap[datasource]; | |
var query = | |
'select * from ' + | |
'(select *, @rownum:=@rownum+1 AS globalIndex ' + | |
'from ' + datasource + ' JOIN (SELECT @rownum:=0) r ' + | |
'order by ' + colMap.seqName + ',' + colMap.start + ',' + colMap.end +') t ' + | |
'where ' + colMap.seqName +'=\'' + seqName + '\' and ' + colMap.start + '< \'' + end + '\' and ' + colMap.end + '> ' + | |
'(select min(' + colMap.start + ') ' + | |
'from ' + datasource + ' where ' + | |
colMap.seqName + '=\'' + seqName + '\' and ' + colMap.end + ' > \'' + start + '\' and ' + colMap.start + '< \'' + end + '\');'; | |
var getQuery = | |
this._endpoint + '?server=genome-mysql.cse.ucsc.edu&db=hg19&u=genome&q=' + | |
encodeURIComponent(query); | |
} | |
switch (action) { | |
case epiviz.data.Request.Action.GET_ROWS: | |
var metadata = {}; | |
var metadataCols = (datasource == this._refGene.datasourceId()) ? this._refGene.metadata() : this._heartMet.metadata(); | |
for (var i = 0; i < metadataCols.length; ++i) { | |
metadata[metadataCols[i]] = []; | |
} | |
var data = {data:{ values: {id:[], start:[], end:[], strand:[], metadata:metadata}, globalStartIndex:null, useOffset:false}, requestId:requestId}; | |
epiviz.data.WebServerDataProvider.makeGetRequest(getQuery, function(jsondata) { | |
if (!jsondata['results'] || jsondata['results'].length == 0) { | |
// Nothing to return | |
callback(epiviz.data.Response.fromRawObject(data)); | |
return; | |
} | |
var ids = data.data.values.id, starts = data.data.values.start, ends = data.data.values.end, | |
strands = data.data.values.strand, metadata = data.data.values.metadata; | |
var columns = epiviz.utils.arrayFlip(jsondata['columns']); | |
var results = jsondata['results']; | |
for (var i = 0; i < results.length; ++i) { | |
ids.push(parseInt(results[i][columns['globalIndex']])); | |
starts.push(parseInt(results[i][columns[colMap.start]])); | |
ends.push(parseInt(results[i][columns[colMap.end]])); | |
strands.push(results[i][columns[colMap.strand]]); | |
for (var col in metadata) { | |
if (!metadata.hasOwnProperty(col)) { continue; } | |
switch (col) { | |
case 'exon_starts': | |
var exonStarts = results[i][columns['exonStarts']]; | |
if (exonStarts && epiviz.utils.stringEndsWith(exonStarts, ',')) { | |
exonStarts = exonStarts.substr(0, exonStarts.length - 1); | |
} | |
metadata[col].push(exonStarts); | |
break; | |
case 'exon_ends': | |
var exonEnds = results[i][columns['exonEnds']]; | |
if (exonEnds && epiviz.utils.stringEndsWith(exonEnds, ',')) { | |
exonEnds = exonEnds.substr(0, exonEnds.length - 1); | |
} | |
metadata[col].push(exonEnds); | |
break; | |
case 'gene': | |
metadata[col].push(results[i][columns['name2']]); | |
break; | |
default: | |
metadata[col].push(results[i][columns[col]]); | |
break; | |
} | |
} | |
} | |
data.data.globalStartIndex = ids[0]; | |
callback(epiviz.data.Response.fromRawObject(data)); | |
}); | |
return; | |
case epiviz.data.Request.Action.GET_VALUES: | |
if (datasource != this._heartMet.datasourceId()) { | |
// Nothing to return | |
callback(epiviz.data.Response.fromRawObject({ | |
data: { values: [], globalStartIndex: null }, | |
requestId: requestId | |
})); | |
return; | |
} | |
var self = this; | |
epiviz.data.WebServerDataProvider.makeGetRequest(getQuery, function(jsondata) { | |
if (!jsondata['results'] || jsondata['results'].length == 0) { | |
// Nothing to return | |
callback(epiviz.data.Response.fromRawObject({ | |
data: { values: [], globalStartIndex: null }, | |
requestId: requestId | |
})); | |
return; | |
} | |
var columns = epiviz.utils.arrayFlip(jsondata['columns']); | |
var results = jsondata['results']; | |
var globalStartIndex = parseInt(results[0][columns['globalIndex']]); | |
var values = []; | |
for (var i = 0; i < results.length; ++i) { | |
values.push(results[i][columns[self._heartMet.id()]]); | |
} | |
callback(epiviz.data.Response.fromRawObject({ | |
data: { values: values, globalStartIndex: globalStartIndex }, | |
requestId: requestId | |
})); | |
}); | |
return; | |
case epiviz.data.Request.Action.GET_MEASUREMENTS: | |
callback(epiviz.data.Response.fromRawObject({ | |
requestId: request.id(), | |
data: { | |
id: [this._refGene.id(), this._heartMet.id()], | |
name: [this._refGene.name(), this._heartMet.name()], | |
type: [this._refGene.type(), this._heartMet.type()], | |
datasourceId: [this._refGene.datasourceId(), this._heartMet.datasourceId()], | |
datasourceGroup: [this._refGene.datasourceGroup(), this._heartMet.datasourceGroup()], | |
defaultChartType: [this._refGene.defaultChartType(), this._heartMet.defaultChartType()], | |
annotation: [this._refGene.annotation(), this._heartMet.annotation()], | |
minValue: [this._refGene.minValue(), this._heartMet.minValue()], | |
maxValue: [this._refGene.maxValue(), this._heartMet.maxValue()], | |
metadata: [this._refGene.metadata(), this._heartMet.metadata()] | |
} | |
})); | |
return; | |
case epiviz.data.Request.Action.GET_SEQINFOS: | |
var seqInfoQuery = | |
this._endpoint + '?server=genome-mysql.cse.ucsc.edu&db=hg19&u=genome&q=' + | |
encodeURIComponent( | |
'select seqName, min(start), max(end) from (' + | |
'select chrom as seqName, min(txStart) as start, max(txEnd) as end from refGene group by chrom union all ' + | |
'select chrom as seqName, min(chromStart) as start, max(chromEnd) as end from wgEncodeHaibMethylRrbsBcleftventriclen41BiochainSitesRep2 group by chrom) t ' + | |
'group by seqName order by seqName;'); | |
epiviz.data.WebServerDataProvider.makeGetRequest(seqInfoQuery, function(jsondata) { | |
if (!jsondata['results'] || jsondata['results'].length == 0) { | |
// Nothing to return | |
callback(epiviz.data.Response.fromRawObject({ | |
requestId: request.id(), | |
data: [] | |
})); | |
} else { | |
callback(epiviz.data.Response.fromRawObject({ | |
requestId: request.id(), | |
data: jsondata['results'] | |
})); | |
} | |
}); | |
return; | |
default: | |
epiviz.data.DataProvider.prototype.getData.call(this, request, callback); | |
break; | |
} | |
}; | |
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
epiviz.EpiViz.SETTINGS.dataProviders.push( | |
sprintf('epiviz.plugins.data.UCSCDataProvider,%s,%s', | |
'', 'http://epiviz.cbcb.umd.edu/data/mysql.php')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment