Commit 7ad71679 authored by Severine Duvaud's avatar Severine Duvaud
Browse files

Hopefully resolves issue #1 + dataset retrieval from arraymap API (preliminary...

Hopefully resolves issue #1 + dataset retrieval from arraymap API (preliminary - still issues with callbacks)
parent 0c03c9d8
var beacon = {}; var beacon = {};
beacon.info = require('./beacon-info.js').info beacon.info = require('./beacon-info.js').getDatasetsFromArrayMap;
beacon.checkResultAndGetResponse = require('./beacon-query.js').checkResultAndGetResponse beacon.checkResultAndGetResponse = require('./beacon-query.js').checkResultAndGetResponse;
beacon.checkPreconditions = require('./beacon-query.js').checkPreconditions beacon.checkPreconditions = require('./beacon-query.js').checkPreconditions;
beacon.buildMongoQuery = require('./beacon-query.js').buildMongoQuery beacon.buildMongoQuery = require('./beacon-query.js').buildMongoQuery;
beacon.checkDatasetIdentifier = require('./beacon-dataset.js').checkDatasetIdentifier beacon.checkDatasetIdentifier = require('./beacon-dataset.js').checkDatasetIdentifier;
beacon.buildMongoDatasetQuery = require('./beacon-dataset.js').buildMongoDatasetQuery beacon.getDatasets = require('./beacon-dataset.js').getDatasets;
beacon.checkDatasetResultAndGetResponse = require('./beacon-dataset.js').checkDatasetResultAndGetResponse
module.exports = beacon; module.exports = beacon;
\ No newline at end of file
...@@ -2,17 +2,17 @@ ...@@ -2,17 +2,17 @@
* Created by sduvaud on 26/05/16. * Created by sduvaud on 26/05/16.
*/ */
var info = require('./beacon-info.js').info; var info = require('./beacon-info.js');
// store all the dataset identifiers info.getDatasetsFromArrayMap(function (callback){
var allDatasetIds = []; infoObject = callback;
info['info']['info']['datasets'].forEach(function(dataset){
allDatasetIds.push(dataset.id);
}); });
var infoObject = {};
function checkDatasetIdentifier(params) { function checkDatasetIdentifier(params) {
if (!params.id) { if (!params.info && !params.id) {
return { return {
hasError: true, hasError: true,
msg: "No identifier specified" msg: "No identifier specified"
...@@ -23,82 +23,44 @@ function checkDatasetIdentifier(params) { ...@@ -23,82 +23,44 @@ function checkDatasetIdentifier(params) {
}; };
} }
function buildMongoDatasetQuery(params) function getDatasets(params) {
{ if (params.info) {
console.log("ID=" + params.id); return infoObject;
}
var groupCondition = {
$group: {
_id: "$ICDMORPHOLOGYCODE",
sampleCount: {$sum: 1},
name: { $first: "$ICDMORPHOLOGY"}
}
};
if (params.id == 'all') { if (params.id == 'all') {
return [ return infoObject.info.info.datasets;
groupCondition
];
} }
else { else {
var found = {};
var orConditions = []; var datasets = infoObject.info.info.datasets;
var identifiers = params.id.split(','); // comma separated list of datasets var identifiers = params.id.split(','); // comma separated list of datasets
var output = [];
identifiers.forEach(function (id) { identifiers.forEach(function (id) {
orConditions.push({ICDMORPHOLOGYCODE: id}); found = {};
}); datasets.forEach(function (dataset) {
if (dataset.id == id) {
return [ found = dataset;
{
$match: {
$or: orConditions
} }
}, });
groupCondition if (JSON.stringify(found)=='{}') {
];
}
}
function checkDatasetResultAndGetResponse(params, datasets) {
var matchedIdentifiers = [];
datasets.forEach(function(dataset){
matchedIdentifiers.push(dataset._id);
});
var submittedIdentifiers = allDatasetIds;
if (params.id != 'all') {
submittedIdentifiers = params.id.split(',');
}
if (submittedIdentifiers.length != matchedIdentifiers.length) {
var missingIdentifiers = [];
submittedIdentifiers.forEach(function(submitted) {
var found = false;
matchedIdentifiers.forEach(function(matched) {
if (matched == submitted) {
found = true;
}
}
);
if (!found) {
missingIdentifiers.push(submitted);
}
}
);
missingIdentifiers.forEach(function (missing) {
var json = { var json = {
"_id": missing, "id": id,
"sampleCount": 0, "description": "Not found",
"name": "Dataset not found" "reference": "reference genome",
"size": {
"variants": "-1",
"samples": 0
}
}; };
datasets.push(json); output.push(json);
}
else {
output.push(found);
} }
); });
return output;
} }
return datasets;
} }
module.exports.checkDatasetIdentifier = checkDatasetIdentifier; module.exports.checkDatasetIdentifier = checkDatasetIdentifier;
module.exports.buildMongoDatasetQuery = buildMongoDatasetQuery; module.exports.getDatasets = getDatasets;
module.exports.checkDatasetResultAndGetResponse = checkDatasetResultAndGetResponse; \ No newline at end of file
\ No newline at end of file
var info = require('./beacon-info.js').info; var info = require('./beacon-info.js');
// store all the dataset identifiers
// not used when a list of datasets is entered info.getDatasetsFromArrayMap(function (callback){
// find a better place for this? var datasets = callback.info.info.datasets;
var allDatasets = []; for (var i = 0; i < datasets.length; i++) {
var datasetObj; allDatasets.push({id: datasets[i].id, nbSamples: datasets[i].size.samples});
info['info']['info']['datasets'].forEach(function(dataset){ }
datasetObj = {
"id": dataset.id,
"nbSamples": dataset.size.samples
};
allDatasets.push(datasetObj);
}); });
var allDatasets = [];
var referenceMap = { var referenceMap = {
'GRCh36': 'SEGMENTS_HG18', 'GRCh36': 'SEGMENTS_HG18',
'GRCh37': 'SEGMENTS_HG19', 'GRCh37': 'SEGMENTS_HG19',
...@@ -46,7 +43,7 @@ function checkPreconditions(params) { ...@@ -46,7 +43,7 @@ function checkPreconditions(params) {
msg: params.referenceName + " reference name not valid" msg: params.referenceName + " reference name not valid"
}; };
if (Number(params.referenceName)) { if (!isNaN(params.referenceName)) {
if (params.referenceName < 1 || params.referenceName > 23) { if (params.referenceName < 1 || params.referenceName > 23) {
return referenceNameInvalid; return referenceNameInvalid;
} }
...@@ -64,11 +61,7 @@ function checkPreconditions(params) { ...@@ -64,11 +61,7 @@ function checkPreconditions(params) {
}; };
} }
/* if (isNaN(params.start)) {
TODO: Change method for numeric test, since positions are 0-based.
*/
if (!Number(params.start)) {
return { return {
hasError: true, hasError: true,
msg: "position not a number" msg: "position not a number"
...@@ -83,11 +76,11 @@ function checkPreconditions(params) { ...@@ -83,11 +76,11 @@ function checkPreconditions(params) {
} }
if (params.datasetIds != 'all') { if (params.datasetIds != 'all') {
var ids = []; var allDatasetIdentifiers = [];
allDatasets.forEach(function (dataset) { allDatasets.forEach(function(dataset){
ids.push(dataset.id); allDatasetIdentifiers.push(dataset.id);
}); });
var error = checkDatasetIdentifiers(params.datasetIds, ids); var error = checkDatasetIdentifiers(params.datasetIds, allDatasetIdentifiers);
if (error.length > 0) { if (error.length > 0) {
return { return {
hasError: true, hasError: true,
...@@ -113,13 +106,6 @@ function checkPreconditions(params) { ...@@ -113,13 +106,6 @@ function checkPreconditions(params) {
}; };
} }
if (params.length && !Number(params.length)) {
return {
hasError: true,
msg: "Length not a number"
};
}
//TODO add additional checks //TODO add additional checks
return { return {
...@@ -152,8 +138,11 @@ function checkPreconditions(params) { ...@@ -152,8 +138,11 @@ function checkPreconditions(params) {
* */ * */
function buildMongoQuery(params) { function buildMongoQuery(params) {
var position = parseInt(params.start); var position = parseInt(params.start, 10);
var length = params.alternateBases.replace(/(DUP|DEL)/, "") ? parseInt(params.alternateBases.replace(/(DUP|DEL)/, "")) : 0; var length = params.alternateBases.replace(/(DUP|DEL)/, "") ? parseInt(params.alternateBases.replace(/(DUP|DEL)/, ""), 10) : 0;
var tmp2 = parseInt(params.start, 10);
console.log("TMP IS: " + tmp2);
var andConditions = []; var andConditions = [];
var orConditions = []; var orConditions = [];
...@@ -256,7 +245,7 @@ function checkResultAndGetResponse(params, datasets) { ...@@ -256,7 +245,7 @@ function checkResultAndGetResponse(params, datasets) {
"frequency": 0 "frequency": 0
}; };
}else { }else {
var totalSample = getTotalNbSample(dataset._id); var totalSample = getTotalNbSample(dataset._id, allDatasets);
var frequency = totalSample > 1 ? (dataset.observed / totalSample).toPrecision(3) : 0; var frequency = totalSample > 1 ? (dataset.observed / totalSample).toPrecision(3) : 0;
response = { response = {
"exists": true, "exists": true,
...@@ -304,14 +293,14 @@ function checkResultAndGetResponse(params, datasets) { ...@@ -304,14 +293,14 @@ function checkResultAndGetResponse(params, datasets) {
// BeaconAlleleResponse // BeaconAlleleResponse
return { return {
"beaconId": info['info']['info']['id'], "beaconId": 'arraymap-beacon',
"datasetAlleleResponses": responses, "datasetAlleleResponses": responses,
"alleleRequest": queryResource, "alleleRequest": queryResource,
"error": null "error": null
}; };
} }
function getTotalNbSample(id) { function getTotalNbSample(id, allDatasets) {
var nbSamples = 1; // and not 0: Division var nbSamples = 1; // and not 0: Division
allDatasets.forEach(function(dataset){ allDatasets.forEach(function(dataset){
if (dataset.id == id) { if (dataset.id == id) {
......
This diff is collapsed.
...@@ -94,5 +94,6 @@ ...@@ -94,5 +94,6 @@
<a target="beacon" ng-href="{{getDatasetUrl()}}">{{getDatasetUrl()}}</a> <a target="beacon" ng-href="{{getDatasetUrl()}}">{{getDatasetUrl()}}</a>
</div> </div>
</div> </div>
</form> </form>
</div> </div>
\ No newline at end of file
...@@ -6,25 +6,17 @@ var beacon = require('../arraymap-beacon/v0.4/arraymap-beacon.js'); ...@@ -6,25 +6,17 @@ var beacon = require('../arraymap-beacon/v0.4/arraymap-beacon.js');
/* GET home page. */ /* GET home page. */
router.get('/', function (req, res, next) { router.get('/', function (req, res, next) {
res.sendfile('index.html'); res.sendfile('index.html');
//res.render('index', { title: 'Express' });
}); });
/* GET home page. */ /* GET doc page. */
router.get('/documentation', function (req, res, next) { router.get('/documentation', function (req, res, next) {
res.sendfile('public/index.html'); res.sendfile('public/index.html');
//res.render('index', { title: 'Express' });
}); });
/* GET documentation page. /* GET info page. */
router.get('/documentation', function (req, res) {
res.render('doc', {
title: 'Beacon arrayMap - Documentation'
});
});*/
/* GET home page. */
router.get('/info', function (req, res, next) { router.get('/info', function (req, res, next) {
res.json(beacon.info); res.redirect('/v0.4/dataset?info=true');
//res.json(beacon.info);
}); });
/* Shows samples */ /* Shows samples */
...@@ -91,18 +83,9 @@ router.get('/v0.4/dataset', function (req, res) { ...@@ -91,18 +83,9 @@ router.get('/v0.4/dataset', function (req, res) {
res.json(preconditions.msg); res.json(preconditions.msg);
return; return;
} }
var mongoQuery = beacon.buildMongoDatasetQuery(req.query);
console.log("Building MongoDB query params: " + JSON.stringify(mongoQuery));
req.db.samples.aggregate(mongoQuery, function(err, docs){ var datasets = beacon.getDatasets(req.query);
if (docs && docs.length > 0) { res.json(datasets);
var response = beacon.checkDatasetResultAndGetResponse(req.query, docs);
res.json(response);
}
else {
res.json("No dataset found");
}
});
}); });
/* Returns response for API v 0.4 */ /* Returns response for API v 0.4 */
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment