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 = {};
beacon.info = require('./beacon-info.js').info
beacon.info = require('./beacon-info.js').getDatasetsFromArrayMap;
beacon.checkResultAndGetResponse = require('./beacon-query.js').checkResultAndGetResponse
beacon.checkPreconditions = require('./beacon-query.js').checkPreconditions
beacon.buildMongoQuery = require('./beacon-query.js').buildMongoQuery
beacon.checkResultAndGetResponse = require('./beacon-query.js').checkResultAndGetResponse;
beacon.checkPreconditions = require('./beacon-query.js').checkPreconditions;
beacon.buildMongoQuery = require('./beacon-query.js').buildMongoQuery;
beacon.checkDatasetIdentifier = require('./beacon-dataset.js').checkDatasetIdentifier
beacon.buildMongoDatasetQuery = require('./beacon-dataset.js').buildMongoDatasetQuery
beacon.checkDatasetResultAndGetResponse = require('./beacon-dataset.js').checkDatasetResultAndGetResponse
beacon.checkDatasetIdentifier = require('./beacon-dataset.js').checkDatasetIdentifier;
beacon.getDatasets = require('./beacon-dataset.js').getDatasets;
module.exports = beacon;
\ No newline at end of file
......@@ -2,17 +2,17 @@
* 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
var allDatasetIds = [];
info['info']['info']['datasets'].forEach(function(dataset){
allDatasetIds.push(dataset.id);
info.getDatasetsFromArrayMap(function (callback){
infoObject = callback;
});
var infoObject = {};
function checkDatasetIdentifier(params) {
if (!params.id) {
if (!params.info && !params.id) {
return {
hasError: true,
msg: "No identifier specified"
......@@ -23,82 +23,44 @@ function checkDatasetIdentifier(params) {
};
}
function buildMongoDatasetQuery(params)
{
console.log("ID=" + params.id);
var groupCondition = {
$group: {
_id: "$ICDMORPHOLOGYCODE",
sampleCount: {$sum: 1},
name: { $first: "$ICDMORPHOLOGY"}
function getDatasets(params) {
if (params.info) {
return infoObject;
}
};
if (params.id == 'all') {
return [
groupCondition
];
return infoObject.info.info.datasets;
}
else {
var orConditions = [];
var found = {};
var datasets = infoObject.info.info.datasets;
var identifiers = params.id.split(','); // comma separated list of datasets
var output = [];
identifiers.forEach(function (id) {
orConditions.push({ICDMORPHOLOGYCODE: id});
});
return [
{
$match: {
$or: orConditions
}
},
groupCondition
];
found = {};
datasets.forEach(function (dataset) {
if (dataset.id == id) {
found = dataset;
}
}
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) {
if (JSON.stringify(found)=='{}') {
var json = {
"_id": missing,
"sampleCount": 0,
"name": "Dataset not found"
"id": id,
"description": "Not found",
"reference": "reference genome",
"size": {
"variants": "-1",
"samples": 0
}
};
datasets.push(json);
output.push(json);
}
);
else {
output.push(found);
}
return datasets;
}
});
return output;
}
}
module.exports.checkDatasetIdentifier = checkDatasetIdentifier;
module.exports.buildMongoDatasetQuery = buildMongoDatasetQuery;
module.exports.checkDatasetResultAndGetResponse = checkDatasetResultAndGetResponse;
\ No newline at end of file
module.exports.getDatasets = getDatasets;
\ 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
// find a better place for this?
var allDatasets = [];
var datasetObj;
info['info']['info']['datasets'].forEach(function(dataset){
datasetObj = {
"id": dataset.id,
"nbSamples": dataset.size.samples
};
allDatasets.push(datasetObj);
info.getDatasetsFromArrayMap(function (callback){
var datasets = callback.info.info.datasets;
for (var i = 0; i < datasets.length; i++) {
allDatasets.push({id: datasets[i].id, nbSamples: datasets[i].size.samples});
}
});
var allDatasets = [];
var referenceMap = {
'GRCh36': 'SEGMENTS_HG18',
'GRCh37': 'SEGMENTS_HG19',
......@@ -46,7 +43,7 @@ function checkPreconditions(params) {
msg: params.referenceName + " reference name not valid"
};
if (Number(params.referenceName)) {
if (!isNaN(params.referenceName)) {
if (params.referenceName < 1 || params.referenceName > 23) {
return referenceNameInvalid;
}
......@@ -64,11 +61,7 @@ function checkPreconditions(params) {
};
}
/*
TODO: Change method for numeric test, since positions are 0-based.
*/
if (!Number(params.start)) {
if (isNaN(params.start)) {
return {
hasError: true,
msg: "position not a number"
......@@ -83,11 +76,11 @@ function checkPreconditions(params) {
}
if (params.datasetIds != 'all') {
var ids = [];
allDatasets.forEach(function (dataset) {
ids.push(dataset.id);
var allDatasetIdentifiers = [];
allDatasets.forEach(function(dataset){
allDatasetIdentifiers.push(dataset.id);
});
var error = checkDatasetIdentifiers(params.datasetIds, ids);
var error = checkDatasetIdentifiers(params.datasetIds, allDatasetIdentifiers);
if (error.length > 0) {
return {
hasError: true,
......@@ -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
return {
......@@ -152,8 +138,11 @@ function checkPreconditions(params) {
* */
function buildMongoQuery(params) {
var position = parseInt(params.start);
var length = params.alternateBases.replace(/(DUP|DEL)/, "") ? parseInt(params.alternateBases.replace(/(DUP|DEL)/, "")) : 0;
var position = parseInt(params.start, 10);
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 orConditions = [];
......@@ -256,7 +245,7 @@ function checkResultAndGetResponse(params, datasets) {
"frequency": 0
};
}else {
var totalSample = getTotalNbSample(dataset._id);
var totalSample = getTotalNbSample(dataset._id, allDatasets);
var frequency = totalSample > 1 ? (dataset.observed / totalSample).toPrecision(3) : 0;
response = {
"exists": true,
......@@ -304,14 +293,14 @@ function checkResultAndGetResponse(params, datasets) {
// BeaconAlleleResponse
return {
"beaconId": info['info']['info']['id'],
"beaconId": 'arraymap-beacon',
"datasetAlleleResponses": responses,
"alleleRequest": queryResource,
"error": null
};
}
function getTotalNbSample(id) {
function getTotalNbSample(id, allDatasets) {
var nbSamples = 1; // and not 0: Division
allDatasets.forEach(function(dataset){
if (dataset.id == id) {
......
This diff is collapsed.
......@@ -94,5 +94,6 @@
<a target="beacon" ng-href="{{getDatasetUrl()}}">{{getDatasetUrl()}}</a>
</div>
</div>
</form>
</div>
\ No newline at end of file
......@@ -6,25 +6,17 @@ var beacon = require('../arraymap-beacon/v0.4/arraymap-beacon.js');
/* GET home page. */
router.get('/', function (req, res, next) {
res.sendfile('index.html');
//res.render('index', { title: 'Express' });
});
/* GET home page. */
/* GET doc page. */
router.get('/documentation', function (req, res, next) {
res.sendfile('public/index.html');
//res.render('index', { title: 'Express' });
});
/* GET documentation page.
router.get('/documentation', function (req, res) {
res.render('doc', {
title: 'Beacon arrayMap - Documentation'
});
});*/
/* GET home page. */
/* GET info page. */
router.get('/info', function (req, res, next) {
res.json(beacon.info);
res.redirect('/v0.4/dataset?info=true');
//res.json(beacon.info);
});
/* Shows samples */
......@@ -91,18 +83,9 @@ router.get('/v0.4/dataset', function (req, res) {
res.json(preconditions.msg);
return;
}
var mongoQuery = beacon.buildMongoDatasetQuery(req.query);
console.log("Building MongoDB query params: " + JSON.stringify(mongoQuery));
req.db.samples.aggregate(mongoQuery, function(err, docs){
if (docs && docs.length > 0) {
var response = beacon.checkDatasetResultAndGetResponse(req.query, docs);
res.json(response);
}
else {
res.json("No dataset found");
}
});
var datasets = beacon.getDatasets(req.query);
res.json(datasets);
});
/* 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