Skip to content

Instantly share code, notes, and snippets.

@florin-chelaru
Last active August 29, 2015 14:01

Revisions

  1. florin-chelaru revised this gist May 6, 2014. 1 changed file with 0 additions and 10 deletions.
    10 changes: 0 additions & 10 deletions ucsc-data-provider.js
    Original file line number Diff line number Diff line change
    @@ -163,16 +163,6 @@ epiviz.plugins.data.UCSCDataProvider.prototype.getData = function (request, call
    }
    var globalStartIndex = ids[0];

    console.log('request: ' + JSON.stringify(request.raw()));
    console.log('response: ' + JSON.stringify({
    data: {
    values: { id: ids },
    globalStartIndex: globalStartIndex,
    useOffset: false
    },
    requestId: requestId
    }));

    callback(epiviz.data.Response.fromRawObject({
    data: {
    values: { id: ids, start: starts, end: ends, strand: strands, metadata: metadata },
  2. florin-chelaru revised this gist May 6, 2014. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion ucsc-data-provider.js
    Original file line number Diff line number Diff line change
    @@ -65,7 +65,11 @@ epiviz.plugins.data.UCSCDataProvider.prototype.getData = function (request, call
    '(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 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);
  3. florin-chelaru revised this gist May 6, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ucsc-data-provider.js
    Original file line number Diff line number Diff line change
    @@ -162,7 +162,7 @@ epiviz.plugins.data.UCSCDataProvider.prototype.getData = function (request, call
    console.log('request: ' + JSON.stringify(request.raw()));
    console.log('response: ' + JSON.stringify({
    data: {
    values: { id: ids, start: starts, end: ends, strand: strands, metadata: metadata },
    values: { id: ids },
    globalStartIndex: globalStartIndex,
    useOffset: false
    },
  4. florin-chelaru revised this gist May 6, 2014. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions ucsc-data-provider.js
    Original file line number Diff line number Diff line change
    @@ -159,6 +159,16 @@ epiviz.plugins.data.UCSCDataProvider.prototype.getData = function (request, call
    }
    var globalStartIndex = ids[0];

    console.log('request: ' + JSON.stringify(request.raw()));
    console.log('response: ' + JSON.stringify({
    data: {
    values: { id: ids, start: starts, end: ends, strand: strands, metadata: metadata },
    globalStartIndex: globalStartIndex,
    useOffset: false
    },
    requestId: requestId
    }));

    callback(epiviz.data.Response.fromRawObject({
    data: {
    values: { id: ids, start: starts, end: ends, strand: strands, metadata: metadata },
  5. florin-chelaru revised this gist May 6, 2014. 1 changed file with 0 additions and 54 deletions.
    54 changes: 0 additions & 54 deletions ucsc-data-provider.js
    Original file line number Diff line number Diff line change
    @@ -6,60 +6,6 @@

    goog.provide('epiviz.plugins.data.UCSCDataProvider');

    // TODO: Remove later
    /**
    * @private
    */
    epiviz.plugins.charts.GenesTrack.prototype._addTooltip = function() {
    var self = this;
    this._container.tooltip({
    items: '.item',
    content:function () {
    if (!self._showTooltip) { return false; }

    /** @type {epiviz.ui.charts.UiObject} */
    var uiObj = d3.select(this).data()[0];

    var maxMetadataValueLength = 15;

    var metadataCols = uiObj.measurements[0].metadata();
    var colsHeader = sprintf('<th><b>Start</b></th><th><b>End</b></th>%s%s',
    metadataCols ? '<th><b>' + metadataCols.join('</b></th><th><b>') + '</b></th>' : '',
    uiObj.values ? '<th><b>' + uiObj.measurements.join('</b></th><th><b>') + '</b></th>': '');

    var rows = '';
    for (var j = 0; j < uiObj.valueItems[0].length && j < 10; ++j) {
    var row = '';
    var rowItem = uiObj.valueItems[0][j].rowItem;
    row += sprintf('<td>%s</td><td>%s</td>', Globalize.format(rowItem.start(), 'n0'), Globalize.format(rowItem.end(), 'n0'));
    var rowMetadata = rowItem.rowMetadata();
    if (rowMetadata) {
    for (var k = 0; k < metadataCols.length; ++k) {
    var metadataCol = metadataCols[k];
    var metadataValue = rowMetadata[metadataCol] || '';
    row += sprintf('<td>%s</td>', metadataValue.length <= maxMetadataValueLength ? metadataValue : metadataValue.substr(0, maxMetadataValueLength) + '...');
    }
    }

    if (uiObj.values) {
    for (var i = 0; i < uiObj.measurements.length; ++i) {
    row += sprintf('<td>%s</td>', Globalize.format(uiObj.valueItems[i][j].value, 'n3'));
    }
    }

    rows += sprintf('<tr>%s</tr>', row);
    }
    if (j < uiObj.valueItems[0].length) {
    var colspan = 2 + (metadataCols ? metadataCols.length : 0) + (uiObj.values ? uiObj.measurements.length : 0);
    rows += sprintf('<tr><td colspan="%s" style="text-align: center;">...</td></tr>', colspan)
    }

    return sprintf('<table class="tooltip-table"><thead><tr>%s</tr></thead><tbody>%s</tbody></table>', colsHeader, rows);
    },
    track: true,
    show: false
    });
    };

    /**
    * @constructor
  6. florin-chelaru revised this gist May 6, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ucsc-data-provider.js
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ goog.provide('epiviz.plugins.data.UCSCDataProvider');
    /**
    * @private
    */
    epiviz.ui.charts.Chart.prototype._addTooltip = function() {
    epiviz.plugins.charts.GenesTrack.prototype._addTooltip = function() {
    var self = this;
    this._container.tooltip({
    items: '.item',
  7. florin-chelaru revised this gist May 6, 2014. 1 changed file with 55 additions and 0 deletions.
    55 changes: 55 additions & 0 deletions ucsc-data-provider.js
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,61 @@

    goog.provide('epiviz.plugins.data.UCSCDataProvider');

    // TODO: Remove later
    /**
    * @private
    */
    epiviz.ui.charts.Chart.prototype._addTooltip = function() {
    var self = this;
    this._container.tooltip({
    items: '.item',
    content:function () {
    if (!self._showTooltip) { return false; }

    /** @type {epiviz.ui.charts.UiObject} */
    var uiObj = d3.select(this).data()[0];

    var maxMetadataValueLength = 15;

    var metadataCols = uiObj.measurements[0].metadata();
    var colsHeader = sprintf('<th><b>Start</b></th><th><b>End</b></th>%s%s',
    metadataCols ? '<th><b>' + metadataCols.join('</b></th><th><b>') + '</b></th>' : '',
    uiObj.values ? '<th><b>' + uiObj.measurements.join('</b></th><th><b>') + '</b></th>': '');

    var rows = '';
    for (var j = 0; j < uiObj.valueItems[0].length && j < 10; ++j) {
    var row = '';
    var rowItem = uiObj.valueItems[0][j].rowItem;
    row += sprintf('<td>%s</td><td>%s</td>', Globalize.format(rowItem.start(), 'n0'), Globalize.format(rowItem.end(), 'n0'));
    var rowMetadata = rowItem.rowMetadata();
    if (rowMetadata) {
    for (var k = 0; k < metadataCols.length; ++k) {
    var metadataCol = metadataCols[k];
    var metadataValue = rowMetadata[metadataCol] || '';
    row += sprintf('<td>%s</td>', metadataValue.length <= maxMetadataValueLength ? metadataValue : metadataValue.substr(0, maxMetadataValueLength) + '...');
    }
    }

    if (uiObj.values) {
    for (var i = 0; i < uiObj.measurements.length; ++i) {
    row += sprintf('<td>%s</td>', Globalize.format(uiObj.valueItems[i][j].value, 'n3'));
    }
    }

    rows += sprintf('<tr>%s</tr>', row);
    }
    if (j < uiObj.valueItems[0].length) {
    var colspan = 2 + (metadataCols ? metadataCols.length : 0) + (uiObj.values ? uiObj.measurements.length : 0);
    rows += sprintf('<tr><td colspan="%s" style="text-align: center;">...</td></tr>', colspan)
    }

    return sprintf('<table class="tooltip-table"><thead><tr>%s</tr></thead><tbody>%s</tbody></table>', colsHeader, rows);
    },
    track: true,
    show: false
    });
    };

    /**
    * @constructor
    * @param {string} id
  8. florin-chelaru revised this gist May 6, 2014. 1 changed file with 0 additions and 10 deletions.
    10 changes: 0 additions & 10 deletions ucsc-data-provider.js
    Original file line number Diff line number Diff line change
    @@ -4,16 +4,6 @@
    * Time: 2:41 PM
    */

    // TODO: Remove later
    /**
    * @param {string} str
    * @param {string} suffix
    * @returns {boolean}
    */
    epiviz.utils.stringEndsWith = function(str, suffix) {
    return str.lastIndexOf(suffix) == str.length - suffix.length;
    };

    goog.provide('epiviz.plugins.data.UCSCDataProvider');

    /**
  9. florin-chelaru revised this gist May 6, 2014. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions ucsc-data-provider.js
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,16 @@
    * Time: 2:41 PM
    */

    // TODO: Remove later
    /**
    * @param {string} str
    * @param {string} suffix
    * @returns {boolean}
    */
    epiviz.utils.stringEndsWith = function(str, suffix) {
    return str.lastIndexOf(suffix) == str.length - suffix.length;
    };

    goog.provide('epiviz.plugins.data.UCSCDataProvider');

    /**
  10. florin-chelaru revised this gist May 6, 2014. 1 changed file with 10 additions and 2 deletions.
    12 changes: 10 additions & 2 deletions ucsc-data-provider.js
    Original file line number Diff line number Diff line change
    @@ -134,10 +134,18 @@ epiviz.plugins.data.UCSCDataProvider.prototype.getData = function (request, call

    switch (col) {
    case 'exon_starts':
    metadata[col].push(results[i][columns['exonStarts']]);
    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':
    metadata[col].push(results[i][columns['exonEnds']]);
    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']]);
  11. florin-chelaru created this gist May 6, 2014.
    217 changes: 217 additions & 0 deletions ucsc-data-provider.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,217 @@
    /**
    * 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 + '\';';
    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':
    metadata[col].push(results[i][columns['exonStarts']]);
    break;
    case 'exon_ends':
    metadata[col].push(results[i][columns['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;
    }
    };

    4 changes: 4 additions & 0 deletions ucsc-settings-override.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    epiviz.EpiViz.SETTINGS.dataProviders.push(
    sprintf('epiviz.plugins.data.UCSCDataProvider,%s,%s',
    '', 'http://epiviz.cbcb.umd.edu/data/mysql.php'));