From 81e6325e2ecb79e31c7ac2a77c559245d84b5287 Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Wed, 3 Dec 2014 15:32:58 -0500 Subject: [PATCH] Inline /contigs and /spec calls --- cycledash/static/js/examine/RecordStore.js | 51 +++++++------------ .../static/js/examine/components/QueryBox.js | 8 ++- cycledash/static/js/examine/examine.js | 2 +- cycledash/views.py | 15 ++---- tests/js/DataUtils-test.js | 4 +- tests/js/DataUtils.js | 12 ++--- tests/js/ExaminePage-test.js | 9 ++-- tests/js/Utils.js | 6 +-- 8 files changed, 39 insertions(+), 68 deletions(-) diff --git a/cycledash/static/js/examine/RecordStore.js b/cycledash/static/js/examine/RecordStore.js index ddd5d81..ac696c4 100644 --- a/cycledash/static/js/examine/RecordStore.js +++ b/cycledash/static/js/examine/RecordStore.js @@ -27,9 +27,10 @@ var ENTIRE_GENOME = {start: null, end: null, contig: types.ALL_CHROMOSOMES}; // opt_testDataSource is provided for testing. // Its type is function(url, done_callback). -function createRecordStore(vcfId, dispatcher, opt_testDataSource) { +function createRecordStore(run, dispatcher, opt_testDataSource) { // Initial state of the store. This is mutable. There be monsters. - var hasLoaded = false, + var vcfId = run.id, + hasLoaded = false, loadError = null, records = [], @@ -41,8 +42,8 @@ function createRecordStore(vcfId, dispatcher, opt_testDataSource) { sortBys = DEFAULT_SORT_BYS, range = ENTIRE_GENOME, - contigs = [], - columns = {}; + contigs = run.contigs, + columns = run.spec; // State for paging the server for records. Page should be reset to 0 on most // operations. @@ -120,6 +121,7 @@ function createRecordStore(vcfId, dispatcher, opt_testDataSource) { $.when(deferredGenotypes(vcfId, query)) .done(response => { + hasLoaded = true; if (append) { // TODO: BUG: This can result in a out-of-order records, if a later // XHR returns before an earlier XHR. @@ -219,42 +221,23 @@ function createRecordStore(vcfId, dispatcher, opt_testDataSource) { range = query.range || ENTIRE_GENOME; } - // Initialize the RecordStore with basic information (columns, the contigs - // in the VCF), and request first records to display. - $.when(deferredSpec(vcfId), deferredContigs(vcfId)) - .done((columnsResponse, contigsResponse) => { - hasLoaded = true; - columns = columnsResponse.spec; - contigs = contigsResponse.contigs; - - var existingQuery = getQueryStringValue('query'); - if (existingQuery) { - try { - var jsonQuery = JSON.parse(existingQuery); - setQuery(jsonQuery); - } catch (e) { - // query is invalid - } - } + var existingQuery = getQueryStringValue('query'); + if (existingQuery) { + try { + var jsonQuery = JSON.parse(existingQuery); + setQuery(jsonQuery); + } catch (e) { + // query is invalid + } + } - // no need to debounce this update -- make it so now! - _updateGenotypes({append: false}); - }); + // no need to debounce this update -- make it so now! + _updateGenotypes({append: false}); function notifyChange() { _.each(listenerCallbacks, cb => { cb(); }); } - // Return deferred GET for the column spec for a given VCF. - function deferredSpec(vcfId) { - return callbackToPromise(dataSource, '/runs/' + vcfId + '/spec'); - } - - // Return deferred GET for the contigs in a given VCF. - function deferredContigs(vcfId) { - return callbackToPromise(dataSource, '/runs/' + vcfId + '/contigs'); - } - // Return a deferred GET returning genotypes and stats. function deferredGenotypes(vcfId, query) { var queryString = encodeURIComponent(JSON.stringify(query)); diff --git a/cycledash/static/js/examine/components/QueryBox.js b/cycledash/static/js/examine/components/QueryBox.js index 3a1a2c9..ed97568 100644 --- a/cycledash/static/js/examine/components/QueryBox.js +++ b/cycledash/static/js/examine/components/QueryBox.js @@ -51,12 +51,10 @@ var QueryBox = React.createClass({ this.props.handleQueryChange(parsedQuery); } }, + componentDidMount: function(prevProps, prevState) { + this.initQueryBox(); + }, componentDidUpdate: function(prevProps, prevState) { - // Watch for the first update with a populated set of columns. - if (_.isEmpty(prevProps.columns) && !_.isEmpty(this.props.columns)) { - this.initQueryBox(); - } - if (prevProps.query != this.props.query) { this.setQueryBoxToQuery(); } diff --git a/cycledash/static/js/examine/examine.js b/cycledash/static/js/examine/examine.js index 31bf827..5acd645 100644 --- a/cycledash/static/js/examine/examine.js +++ b/cycledash/static/js/examine/examine.js @@ -10,7 +10,7 @@ var React = require('react'), window.renderExaminePage = function(el, run, igvHttpfsUrl) { var dispatcher = new Dispatcher(); var recordActions = getRecordActions(dispatcher); - var recordStore = createRecordStore(run.id, dispatcher); + var recordStore = createRecordStore(run, dispatcher); React.render(/spec') -def examine_spec(run_id): - return jsonify({'spec': gt.spec(run_id)}) - - -@app.route('/runs//contigs') -def contigs(run_id): - return jsonify({'contigs': gt.contigs(run_id)}) - - @app.route('/runs//examine') def examine(run_id): with db.engine.connect() as con: select_vcf_sql = 'select * from vcfs where id = {};'.format(run_id) vcf = dict(con.execute(select_vcf_sql).fetchall()[0]) - return render_template('examine.html', run=dict(vcf)) + run = dict(vcf) + run['spec'] = gt.spec(run_id) + run['contigs'] = gt.contigs(run_id) + return render_template('examine.html', run=run) @app.route('/runs//concordance', methods=['GET', 'PUT']) diff --git a/tests/js/DataUtils-test.js b/tests/js/DataUtils-test.js index aabff28..cc0336d 100644 --- a/tests/js/DataUtils-test.js +++ b/tests/js/DataUtils-test.js @@ -6,7 +6,7 @@ var dataUtils = require('./DataUtils'), vcf = require('vcf.js'), _ = require('underscore'); -describe('test data', function() { +describe('DataUtils', function() { var vcfData; before(function() { var parseVcf = vcf.parser(); // Note: the real deal, not a fake! @@ -19,7 +19,7 @@ describe('test data', function() { }); it('should generate a column spec', function() { - var spec = dataUtils.getSpec(vcfData).spec; + var spec = dataUtils.getSpec(vcfData); assert.deepEqual(['INFO', 'SAMPLE'], _.keys(spec)); assert.deepEqual(['DP','SOMATIC','SS','SSC','GPV','SPV'], _.keys(spec.INFO)); assert.deepEqual(['GT','GQ','DP','RD','AD','FREQ','DP4'], _.keys(spec.SAMPLE)); diff --git a/tests/js/DataUtils.js b/tests/js/DataUtils.js index 8169923..6992d5e 100644 --- a/tests/js/DataUtils.js +++ b/tests/js/DataUtils.js @@ -34,7 +34,7 @@ function getSpec(vcfData) { // TODO: add sample_name - return {spec: cols}; + return cols; } function getContigs(vcfData) { @@ -42,7 +42,7 @@ function getContigs(vcfData) { } function getRecords(vcfData) { - var spec = getSpec(vcfData).spec; + var spec = getSpec(vcfData); return _.flatten(vcfData.records.map((record) => { var baseProps = { contig: record.CHROM, @@ -81,11 +81,7 @@ function makeFakeServer(vcfPath) { var genotypesUrl = '/runs/1/genotypes'; var get = function(path, callback) { - if (path == '/runs/1/spec') { - callback(spec); - } else if (path == '/runs/1/contigs') { - callback({contigs: contigs}); - } else if (path.slice(0, genotypesUrl.length) == genotypesUrl) { + if (path.slice(0, genotypesUrl.length) == genotypesUrl) { callback({ records: records, stats: { @@ -94,7 +90,7 @@ function makeFakeServer(vcfPath) { } }); } else { - throw new Error('Unexpected request'); + throw new Error('Unexpected request for ' + path); } }; diff --git a/tests/js/ExaminePage-test.js b/tests/js/ExaminePage-test.js index 7aafb09..9d7a0b5 100644 --- a/tests/js/ExaminePage-test.js +++ b/tests/js/ExaminePage-test.js @@ -38,16 +38,19 @@ describe('ExaminePage', function() { }); it('should display and select records', function() { - var dispatcher = new Dispatcher(); - var recordActions = RecordActions.getRecordActions(dispatcher); - var recordStore = createRecordStore(1, dispatcher, fakeServer); var run = { id: 1, + spec: fakeServer.spec, + contigs: fakeServer.contigs, caller_name: 'test', dataset_name: 'test', created_at: '', uri: '/tests/js/data/snv.vcf' }; + + var dispatcher = new Dispatcher(); + var recordActions = RecordActions.getRecordActions(dispatcher); + var recordStore = createRecordStore(run, dispatcher, fakeServer); var examine = TestUtils.renderIntoDocument(