Last active
August 29, 2015 14:01
-
-
Save florin-chelaru/c41a2df3671395d8e4ad to your computer and use it in GitHub Desktop.
UCSC Demo
This file contains 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 | |
); | |
}; | |
/** | |
* 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'); | |
var query = | |
'select * from ' + | |
'(select *, @rownum:=@rownum+1 AS globalIndex ' + | |
'from refGene JOIN (SELECT @rownum:=0) r ' + | |
'order by chrom, txStart, txEnd) t ' + | |
//'where chrom=\'' + seqName + '\' and txEnd > \'' + start + '\' and txStart < \'' + end + '\';'; | |
'where chrom=\'' + seqName + '\' and txStart < \'' + end + '\' and txEnd > ' + | |
'(select min(txStart) ' + | |
'from refGene where ' + | |
'chrom=\'' + seqName + '\' and txEnd > \'' + start + '\' and txStart < \'' + 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: | |
if (datasource != this._refGene.datasourceId()) { | |
// Nothing to return | |
callback(epiviz.data.Response.fromRawObject({ | |
data: { | |
values: { id: null, start: [], end:[], strand: [], metadata:{ | |
'bin': [], | |
'exonCount': [], | |
'exon_starts': [], | |
'exon_ends': [], | |
'gene': [], | |
'exonFrames': [] | |
}}, | |
globalStartIndex: null, | |
useOffset: false | |
}, | |
requestId: requestId | |
})); | |
return; | |
} | |
epiviz.data.WebServerDataProvider.makeGetRequest(getQuery, function(jsondata) { | |
if (!jsondata['results'] || jsondata['results'].length == 0) { | |
// Nothing to return | |
callback(epiviz.data.Response.fromRawObject({ | |
data: { | |
values: { id: null, start: [], end:[], strand: [], metadata:{ | |
'bin': [], | |
'exonCount': [], | |
'exon_starts': [], | |
'exon_ends': [], | |
'gene': [], | |
'exonFrames': [] | |
}}, | |
globalStartIndex: null, | |
useOffset: false | |
}, | |
requestId: requestId | |
})); | |
return; | |
} | |
var ids = [], starts = [], ends = [], strands = [], metadata = { | |
'bin': [], | |
'exonCount': [], | |
'exon_starts': [], | |
'exon_ends': [], | |
'gene': [], | |
'exonFrames': [] | |
}; | |
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['txStart']])); | |
ends.push(parseInt(results[i][columns['txEnd']])); | |
strands.push(results[i][columns['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; | |
} | |
} | |
} | |
var globalStartIndex = ids[0]; | |
callback(epiviz.data.Response.fromRawObject({ | |
data: { | |
values: { id: ids, start: starts, end: ends, strand: strands, metadata: metadata }, | |
globalStartIndex: globalStartIndex, | |
useOffset: false | |
}, | |
requestId: requestId | |
})); | |
}); | |
return; | |
case epiviz.data.Request.Action.GET_VALUES: | |
// Nothing to return | |
callback(epiviz.data.Response.fromRawObject({ | |
data: { values: [], globalStartIndex: null }, | |
requestId: requestId | |
})); | |
return; | |
case epiviz.data.Request.Action.GET_MEASUREMENTS: | |
callback(epiviz.data.Response.fromRawObject({ | |
requestId: request.id(), | |
data: { | |
id: [this._refGene.id()], | |
name: [this._refGene.name()], | |
type: [this._refGene.type()], | |
datasourceId: [this._refGene.datasourceId()], | |
datasourceGroup: [this._refGene.datasourceGroup()], | |
defaultChartType: [this._refGene.defaultChartType()], | |
annotation: [this._refGene.annotation()], | |
minValue: [this._refGene.minValue()], | |
maxValue: [this._refGene.maxValue()], | |
metadata: [this._refGene.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 chrom, min(txStart), max(txEnd) from refGene group by chrom order by chrom;'); | |
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 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