Skip to content

Commit

Permalink
#1030 change EXPRESSION_DB_USERNAME -> DB_USERNAME and EXPRESSION_DB_…
Browse files Browse the repository at this point in the history
…PASSWORD -> DB_PASSWORD
  • Loading branch information
ntran18 committed Nov 27, 2024
1 parent 0acca88 commit d44b7d3
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 162 deletions.
135 changes: 88 additions & 47 deletions server/dals/expression-dal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,85 +4,110 @@ var env = process.env.NODE_ENV || "development";
var config = require("../config/config")[env];
var sequelize = new Sequelize(
config.databaseName,
process.env.EXPRESSION_DB_USERNAME,
process.env.EXPRESSION_DB_PASSWORD,
process.env.DB_USERNAME,
process.env.DB_PASSWORD,
{
host: config.databaseHost,
dialect: config.databaseDialect,
pool: {
max: 5,
min: 0,
idle: 10000
}
idle: 10000,
},
}
);

const buildExpressionGenesQuery = function (geneString) {
const buildExpressionGenesQuery = function(geneString) {
let genes = "";
let geneList = geneString.split(",");
geneList.forEach(x => genes += ( `(LOWER(gene_expression.gene.display_gene_id) = LOWER(\'${x}\')) OR `));
geneList.forEach(
(x) =>
(genes += `(LOWER(gene_expression.gene.display_gene_id) = LOWER(\'${x}\')) OR `)
);
return genes.substring(0, genes.length - 4);
};

const buildExpressionProductionDegradationRatesQuery = function (rateType, genes) {
const buildExpressionProductionDegradationRatesQuery = function(
rateType,
genes
) {
return `
SELECT gene.display_gene_id, ${rateType} FROM gene_expression.${rateType}, gene_expression.gene WHERE
((${buildExpressionGenesQuery(genes)})
AND gene_expression.gene.gene_id = gene_expression.${rateType}.gene_id) ORDER BY display_gene_id;`;
};

const buildExpressionTimepointsFromDatasetQuery = function (dataset) {
const buildExpressionTimepointsFromDatasetQuery = function(dataset) {
return `
SELECT DISTINCT time_point, sample_id FROM gene_expression.expression WHERE
dataset = '${dataset}' ORDER BY time_point ASC;`;
};

const buildExpressionDataQuery = function (dataset, genes) {
const buildExpressionDataQuery = function(dataset, genes) {
return `SELECT * FROM gene_expression.expression, gene_expression.gene
WHERE gene_expression.expression.dataset='${dataset}'
AND ((${buildExpressionGenesQuery(genes)})
AND gene_expression.gene.gene_id = gene_expression.expression.gene_id) ORDER BY sort_index;`;
};



const buildExpressionQuery = function (query) {
const buildExpressionQuery = function(query) {
const expressionQueries = {
"DegradationRates": () => buildExpressionProductionDegradationRatesQuery("degradation_rate", query.genes),
"ProductionRates" : () => buildExpressionProductionDegradationRatesQuery("production_rate", query.genes),
"ExpressionData" : () => buildExpressionDataQuery(query.dataset, query.genes),
"ExpressionDatasets" : () => "SELECT DISTINCT dataset FROM gene_expression.expression ORDER BY dataset ASC;",
"ExpressionTimePoints": () => buildExpressionTimepointsFromDatasetQuery(query.dataset)
DegradationRates: () =>
buildExpressionProductionDegradationRatesQuery(
"degradation_rate",
query.genes
),
ProductionRates: () =>
buildExpressionProductionDegradationRatesQuery(
"production_rate",
query.genes
),
ExpressionData: () =>
buildExpressionDataQuery(query.dataset, query.genes),
ExpressionDatasets: () =>
"SELECT DISTINCT dataset FROM gene_expression.expression ORDER BY dataset ASC;",
ExpressionTimePoints: () =>
buildExpressionTimepointsFromDatasetQuery(query.dataset),
};
if (Object.keys(expressionQueries).includes(query.type)) {
return expressionQueries[query.type]();
}
};

const listExpressionGeneData = function (gene, totalOutput) {
const listExpressionGeneData = function(gene, totalOutput) {
let listOfData = [];
totalOutput.forEach(function (x) {
totalOutput.forEach(function(x) {
if (x.display_gene_id.toLowerCase() === gene.toLowerCase()) {
listOfData.push(Number(x.expression));
}
});
return listOfData;
};

const convertExpressionToJSON = function (totalOutput, dataset, timePoints, allGenes) {
const convertExpressionToJSON = function(
totalOutput,
dataset,
timePoints,
allGenes
) {
let JSONOutput = {
timePoints,
data: {
id: timePoints
}
id: timePoints,
},
};
allGenes.forEach(x => JSONOutput.data[x.toString()] = listExpressionGeneData(x, totalOutput));
allGenes.forEach(
(x) =>
(JSONOutput.data[x.toString()] = listExpressionGeneData(
x,
totalOutput
))
);
return JSONOutput;
};

const ProductionDegradationRateToJSON = (totalOutput, rateType) => {
const JSONOutput = {
};
const JSONOutput = {};
for (let gene of totalOutput) {
JSONOutput[gene.display_gene_id] = gene[rateType];
}
Expand All @@ -91,7 +116,7 @@ const ProductionDegradationRateToJSON = (totalOutput, rateType) => {

const DatasetToJSON = (totalOutput) => {
const JSONOutput = {
expressionDatasets : []
expressionDatasets: [],
};
for (let dataset of totalOutput) {
JSONOutput.expressionDatasets.push(dataset.dataset);
Expand All @@ -109,24 +134,40 @@ const TimePointsToJSON = (totalOutput, dataset) => {
};

module.exports = {
queryExpressionDatabase: function (req, res) {
return sequelize.query(buildExpressionQuery(req.query),
{ type: sequelize.QueryTypes.SELECT })
.then(function (stdname) {
const convertToJSON = {
"DegradationRates" : () => ProductionDegradationRateToJSON(stdname, "degradation_rate"),
"ProductionRates" : () => ProductionDegradationRateToJSON(stdname, "production_rate"),
"ExpressionData" : () => convertExpressionToJSON(
stdname,
req.query.dataset,
req.query.timepoints.split(",").map(x => Number(x)),
req.query.genes.split(",")),
"ExpressionDatasets": () => DatasetToJSON(stdname),
"ExpressionTimePoints": () => TimePointsToJSON(stdname, req.query.dataset)
};
const type = req.query.type;
return (Object.keys(convertToJSON).includes(type)) ? res.send(convertToJSON[type]()) :
res.send(500, { errors: "Something went wrong."});
});
}
};
queryExpressionDatabase: function(req, res) {
return sequelize
.query(buildExpressionQuery(req.query), {
type: sequelize.QueryTypes.SELECT,
})
.then(function(stdname) {
const convertToJSON = {
DegradationRates: () =>
ProductionDegradationRateToJSON(
stdname,
"degradation_rate"
),
ProductionRates: () =>
ProductionDegradationRateToJSON(
stdname,
"production_rate"
),
ExpressionData: () =>
convertExpressionToJSON(
stdname,
req.query.dataset,
req.query.timepoints
.split(",")
.map((x) => Number(x)),
req.query.genes.split(",")
),
ExpressionDatasets: () => DatasetToJSON(stdname),
ExpressionTimePoints: () =>
TimePointsToJSON(stdname, req.query.dataset),
};
const type = req.query.type;
return Object.keys(convertToJSON).includes(type)
? res.send(convertToJSON[type]())
: res.send(500, { errors: "Something went wrong." });
});
},
};
34 changes: 18 additions & 16 deletions server/dals/grnsetting-dal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,48 @@ var env = process.env.NODE_ENV || "development";
var config = require("../config/config")[env];
var sequelize = new Sequelize(
config.databaseName,
process.env.EXPRESSION_DB_USERNAME,
process.env.EXPRESSION_DB_PASSWORD,
process.env.DB_USERNAME,
process.env.DB_PASSWORD,
{
host: config.databaseHost,
dialect: config.databaseDialect,
pool: {
max: 5,
min: 0,
idle: 10000
}
idle: 10000,
},
}
);

const buildGetDefaultDataset = function () {
const buildGetDefaultDataset = function() {
return `
SELECT grnsettings.expression_dataset FROM settings.grnsettings;`;
};

const DefaultDatasetToJSON = (totalOutput) => {
const JSONOutput = {
defaultDataset : []
defaultDataset: [],
};
for (let dataset of totalOutput) {
JSONOutput.defaultDataset.push(dataset.expression_dataset);
}
return JSONOutput;
};


module.exports = {
queryDefaultDataset: function (req, res) {
return sequelize.query(buildGetDefaultDataset(),
{ type: sequelize.QueryTypes.SELECT })
.then(function (stdname) {
queryDefaultDataset: function(req, res) {
return sequelize
.query(buildGetDefaultDataset(), {
type: sequelize.QueryTypes.SELECT,
})
.then(function(stdname) {
const convertToJSON = {
"DefaultDataset" : () => DefaultDatasetToJSON(stdname)
DefaultDataset: () => DefaultDatasetToJSON(stdname),
};
const type = req.query.type;
return (Object.keys(convertToJSON).includes(type)) ? res.send(convertToJSON[type]()) :
res.status(500).send({ errors: "Something went wrong." });
return Object.keys(convertToJSON).includes(type)
? res.send(convertToJSON[type]())
: res.status(500).send({ errors: "Something went wrong." });
});
}
};
},
};
Loading

0 comments on commit d44b7d3

Please sign in to comment.