Skip to content

Commit

Permalink
now import constants and funcs from dbConstants file into network-dal…
Browse files Browse the repository at this point in the history
…s and protein-dals. correctly make query based on timestamp, need to check if add source to query
  • Loading branch information
ceciliazaragoza committed Feb 26, 2025
1 parent 1e57de5 commit 87709ff
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 92 deletions.
11 changes: 6 additions & 5 deletions server/dals/dbConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ export const PPI_DATABASE_NAMESPACE = "protein_protein_interactions_new";
export const PPI_DATABASE_NAMESPACE_OLD = "protein_protein_interactions";
export const DATABASE_TIMESTAMP_CUTOFF = new Date("2025-01-01");

export const timestampNamespace = function(timestamp, namespace, oldNamespace) {
return timestamp < new Date("2025-01-01") ? namespace : oldNamespace;
export const timestampNamespace = function (timestamp, isGrn) {
return new Date(timestamp) > new Date("2025-01-01") ? (isGrn ? GRN_DATABASE_NAMESPACE : PPI_DATABASE_NAMESPACE) :
(isGrn ? GRN_DATABASE_NAMESPACE_OLD : PPI_DATABASE_NAMESPACE_OLD);
};

export const timestampOld = function(timestamp) {
return timestamp < DATABASE_TIMESTAMP_CUTOFF;
}
export const isTimestampOld = function (timestamp) {
return new Date(timestamp) < DATABASE_TIMESTAMP_CUTOFF;
};
80 changes: 40 additions & 40 deletions server/dals/network-dal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Sequelize = require("sequelize");
const { timestampNamespace, isTimestampOld } = require("./dbConstants");
require("dotenv").config();
var env = process.env.NODE_ENV || "development";
var config = require("../config/config")[env];
Expand All @@ -12,23 +13,23 @@ var sequelize = new Sequelize(config.databaseName, process.env.DB_USERNAME, proc
},
});

const buildNetworkSourceQuery = function() {
const buildNetworkSourceQuery = function () {
return `SELECT * FROM gene_regulatory_network.source
UNION ALL
SELECT * FROM gene_regulatory_network_new.source
ORDER BY time_stamp DESC;`;
};

const buildNetworkGeneFromSourceQuery = function(gene, source, timestamp) {
const namespace =
timestamp < new Date("2025-01-01") ? "gene_regulatory_network.gene" : "gene_regulatory_network_new.gene";
const timestamp_query = timestamp < new Date("2025-01-01") ? `AND gene.time_stamp ='${timestamp}` : "";
const buildNetworkGeneFromSourceQuery = function (gene, source, timestamp) {
const namespace = `${timestampNamespace(timestamp, true)}.gene`;
const timestampQuery = isTimestampOld(timestamp) ? "" : `AND gene.time_stamp='${timestamp}'`;

return `SELECT DISTINCT gene_id, display_gene_id FROM
${namespace} WHERE (gene.gene_id ='${gene}'
OR gene.display_gene_id ='${gene}') ${timestamp_query};`;
OR gene.display_gene_id ='${gene}')${timestampQuery};`;
};

const buildNetworkGenesQuery = function(geneString) {
const buildNetworkGenesQuery = function (geneString) {
let genes = "(";
let geneList = geneString.split(",");
geneList.forEach((x) => (genes += `(network.regulator_gene_id =\'${x}\') OR `));
Expand All @@ -37,19 +38,18 @@ const buildNetworkGenesQuery = function(geneString) {
return `${genes.substring(0, genes.length - 4)})`;
};

const buildGenerateNetworkQuery = function(genes, source, timestamp) {
const namespace =
timestamp < new Date("2025-01-01") ? "gene_regulatory_network.network" : "gene_regulatory_network_new.network";
const annotation = timestamp < new Date("2025-01-01") ? "" : ", annotation_type";
const buildGenerateNetworkQuery = function (genes, source, timestamp) {
const namespace = `${timestampNamespace(timestamp, true)}.network`;
const annotation = isTimestampOld(timestamp) ? "" : ", annotation_type";
return `SELECT DISTINCT regulator_gene_id, target_gene_id${annotation} FROM ${namespace}
WHERE time_stamp='${timestamp}' AND source='${source}' AND
${buildNetworkGenesQuery(genes)} ORDER BY regulator_gene_id DESC;`;
};

const buildQueryByType = function(queryType, query) {
const buildQueryByType = function (queryType, query) {
const networkQueries = {
NetworkSource: () => buildNetworkSourceQuery(),
NetworkGeneFromSource: () => buildNetworkGeneFromSourceQuery(query.gene),
NetworkGeneFromSource: () => buildNetworkGeneFromSourceQuery(query.gene, query.source, query.timestamp),
GenerateNetwork: () => buildGenerateNetworkQuery(query.genes, query.source, query.timestamp),
};
if (Object.keys(networkQueries).includes(query.type)) {
Expand All @@ -58,43 +58,43 @@ const buildQueryByType = function(queryType, query) {
};

// const response = convertResponseToJSON(req.query.type, req.query, stdname);
const convertResponseToJSON = function(queryType, query, totalOutput) {
const convertResponseToJSON = function (queryType, query, totalOutput) {
let JSONOutput = {};
switch (queryType) {
case "NetworkSource":
JSONOutput.sources = {};
totalOutput.forEach(function(connection) {
const timestamp = connection.time_stamp;
const source = connection.source;
const displayName = connection.display_name;
JSONOutput.sources[`${displayName}: ${timestamp.toISOString().split("T")[0]}`] = { timestamp, source };
});
return JSONOutput;
case "NetworkGeneFromSource":
JSONOutput.displayGeneId = totalOutput.length > 0 ? totalOutput[0].display_gene_id : null;
JSONOutput.geneId = totalOutput.length > 0 ? totalOutput[0].gene_id : null;
return JSONOutput;
case "GenerateNetwork":
JSONOutput.links = {};
for (let connection of totalOutput) {
if (JSONOutput.links[connection.regulator_gene_id] === undefined) {
JSONOutput.links[connection.regulator_gene_id] = [connection.target_gene_id];
} else {
JSONOutput.links[connection.regulator_gene_id].push(connection.target_gene_id);
}
case "NetworkSource":
JSONOutput.sources = {};
totalOutput.forEach(function (connection) {
const timestamp = connection.time_stamp;
const source = connection.source;
const displayName = connection.display_name;
JSONOutput.sources[`${displayName}: ${timestamp.toISOString().split("T")[0]}`] = { timestamp, source };
});
return JSONOutput;
case "NetworkGeneFromSource":
JSONOutput.displayGeneId = totalOutput.length > 0 ? totalOutput[0].display_gene_id : null;
JSONOutput.geneId = totalOutput.length > 0 ? totalOutput[0].gene_id : null;
return JSONOutput;
case "GenerateNetwork":
JSONOutput.links = {};
for (let connection of totalOutput) {
if (JSONOutput.links[connection.regulator_gene_id] === undefined) {
JSONOutput.links[connection.regulator_gene_id] = [connection.target_gene_id];
} else {
JSONOutput.links[connection.regulator_gene_id].push(connection.target_gene_id);
}
return JSONOutput;
default:
return JSONOutput;
}
return JSONOutput;
default:
return JSONOutput;
}
};

module.exports = {
buildNetworkSourceQuery: buildNetworkSourceQuery,
queryNetworkDatabase: function(req, res) {
queryNetworkDatabase: function (req, res) {
sequelize
.query(buildQueryByType(req.query.type, req.query), { type: sequelize.QueryTypes.SELECT })
.then(function(stdname) {
.then(function (stdname) {
const response = convertResponseToJSON(req.query.type, req.query, stdname);
return res.send(response);
});
Expand Down
98 changes: 51 additions & 47 deletions server/dals/protein-dal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Sequelize = require("sequelize");
const { isTimestampOld, timestampNamespace } = require("./dbConstants");
require("dotenv").config();
var env = process.env.NODE_ENV || "development";
var config = require("../config/config")[env];
Expand All @@ -12,24 +13,27 @@ var sequelize = new Sequelize(config.databaseName, process.env.DB_USERNAME, proc
},
});

const buildNetworkSourceQuery = function() {
const buildNetworkSourceQuery = function () {
return `SELECT * FROM protein_protein_interactions.source
UNION ALL
SELECT * FROM protein_protein_interactions_new.source
ORDER BY time_stamp DESC;`;
};

const buildNetworkFromGeneProteinQuery = function(geneProtein, timestamp) {
const namespace =
timestamp < new Date("2025-01-01") ? "protein_protein_interactions" : "protein_protein_interactions_new";
const buildNetworkFromGeneProteinQuery = function (geneProtein, timestamp) {
const namespace = timestampNamespace(timestamp, false);
const timestampQuery = isTimestampOld(timestamp) ? "" : `AND gene.time_stamp='${timestamp}'`;
console.log("timestampquery", timestampQuery);
return `SELECT DISTINCT gene_id, display_gene_id, standard_name, length, molecular_weight, PI FROM
${namespace}.gene, ${namespace}.protein WHERE
(LOWER(gene.gene_id)=LOWER('${geneProtein}') OR LOWER(gene.display_gene_id)=LOWER('${geneProtein}')
OR LOWER(protein.standard_name) =LOWER('${geneProtein}')) AND
LOWER(gene.gene_id) = LOWER(protein.gene_systematic_name) AND gene.time_stamp = ${timestamp};`;
LOWER(gene.gene_id) = LOWER(protein.gene_systematic_name)${timestampQuery};`;

// do I need to check for source each time I check for timestamp?
};

const buildNetworkProteinsQuery = function(proteinString) {
const buildNetworkProteinsQuery = function (proteinString) {
let proteins = "(";
let proteinList = proteinString.split(",");
proteinList.forEach((x) => (proteins += `(physical_interactions.protein1 =\'${x}\') OR `));
Expand All @@ -38,16 +42,16 @@ const buildNetworkProteinsQuery = function(proteinString) {
return `${proteins.substring(0, proteins.length - 4)})`;
};

const buildGenerateProteinNetworkQuery = function(proteins, timestamp, source) {
const namespace =
timestamp < new Date("2025-01-01") ? "protein_protein_interactions" : "protein_protein_interactions_new";
const annotation = timestamp < new Date("2025-01-01") ? "" : ", annotation_type";
return `SELECT DISTINCT protein1, protein2${annotation} FROM ${namespace}.physical_interactions
${namespace}.time_stamp='${timestamp}' AND ${namespace}.source='${source}' AND
${buildNetworkProteinsQuery(proteins)} ORDER BY protein1 DESC;`;
const buildGenerateProteinNetworkQuery = function (proteins, timestamp, source) {
const namespace = timestampNamespace(timestamp, false);
const annotation = isTimestampOld(timestamp) ? "" : ", annotation_type";
return `SELECT DISTINCT protein1, protein2${annotation} FROM
${namespace}.physical_interactions WHERE
physical_interactions.time_stamp='${timestamp}' AND physical_interactions.source='${source}' AND
${buildNetworkProteinsQuery(proteins)} ORDER BY protein1 DESC;`;
};

const buildQueryByType = function(query) {
const buildQueryByType = function (query) {
const networkQueries = {
NetworkSource: () => buildNetworkSourceQuery(),
NetworkFromGeneProtein: () => buildNetworkFromGeneProteinQuery(query.geneProtein),
Expand All @@ -58,46 +62,46 @@ const buildQueryByType = function(query) {
}
};

const convertResponseToJSON = function(queryType, totalOutput) {
const convertResponseToJSON = function (queryType, totalOutput) {
let JSONOutput = {};
switch (queryType) {
case "NetworkSource":
JSONOutput.sources = {};
totalOutput.forEach(function(x) {
const timestamp = x.time_stamp;
const source = x.source;
const displayName = x.display_name;
JSONOutput.sources[`${displayName}: ${timestamp.toISOString().split("T")[0]}`] = { timestamp, source };
});
return JSONOutput;
case "NetworkFromGeneProtein":
if (totalOutput.length > 0) {
JSONOutput.standardName = totalOutput[0].standard_name;
JSONOutput.displayGeneId = totalOutput[0].display_gene_id;
JSONOutput.geneId = totalOutput[0].gene_id;
JSONOutput.length = totalOutput[0].length;
JSONOutput.molecularWeight = totalOutput[0].molecular_weight;
JSONOutput.PI = totalOutput[0].PI;
}
return JSONOutput;
case "GenerateProteinNetwork":
JSONOutput.links = {};
for (let connection of totalOutput) {
if (JSONOutput.links[connection.protein1] === undefined) {
JSONOutput.links[connection.protein1] = [connection.protein2];
} else {
JSONOutput.links[connection.protein1].push(connection.protein2);
}
case "NetworkSource":
JSONOutput.sources = {};
totalOutput.forEach(function (x) {
const timestamp = x.time_stamp;
const source = x.source;
const displayName = x.display_name;
JSONOutput.sources[`${displayName}: ${timestamp.toISOString().split("T")[0]}`] = { timestamp, source };
});
return JSONOutput;
case "NetworkFromGeneProtein":
if (totalOutput.length > 0) {
JSONOutput.standardName = totalOutput[0].standard_name;
JSONOutput.displayGeneId = totalOutput[0].display_gene_id;
JSONOutput.geneId = totalOutput[0].gene_id;
JSONOutput.length = totalOutput[0].length;
JSONOutput.molecularWeight = totalOutput[0].molecular_weight;
JSONOutput.PI = totalOutput[0].PI;
}
return JSONOutput;
case "GenerateProteinNetwork":
JSONOutput.links = {};
for (let connection of totalOutput) {
if (JSONOutput.links[connection.protein1] === undefined) {
JSONOutput.links[connection.protein1] = [connection.protein2];
} else {
JSONOutput.links[connection.protein1].push(connection.protein2);
}
return JSONOutput;
default:
return JSONOutput;
}
return JSONOutput;
default:
return JSONOutput;
}
};

module.exports = {
queryProteinDatabase: function(req, res) {
sequelize.query(buildQueryByType(req.query), { type: sequelize.QueryTypes.SELECT }).then(function(stdname) {
queryProteinDatabase: function (req, res) {
sequelize.query(buildQueryByType(req.query), { type: sequelize.QueryTypes.SELECT }).then(function (stdname) {
const response = convertResponseToJSON(req.query.type, stdname);
return res.send(response);
});
Expand Down

0 comments on commit 87709ff

Please sign in to comment.