Skip to content

Commit

Permalink
going back to old code from commit a0ab429 to troubleshoot queries. n…
Browse files Browse the repository at this point in the history
…ot currently using dbConstants file
  • Loading branch information
ceciliazaragoza committed Feb 26, 2025
1 parent 0eaa53c commit 1e57de5
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 169 deletions.
121 changes: 35 additions & 86 deletions server/dals/network-dal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,98 +2,63 @@ const Sequelize = require("sequelize");
require("dotenv").config();
var env = process.env.NODE_ENV || "development";
var config = require("../config/config")[env];
var sequelize = new Sequelize(
config.databaseName,
process.env.DB_USERNAME,
process.env.DB_PASSWORD,
{
host: config.databaseHost,
dialect: config.databaseDialect,
pool: {
max: 5,
min: 0,
idle: 10000,
},
}
);

//import { GRN_DATABASE_NAMESPACE, GRN_DATABASE_NAMESPACE_OLD, timestampNamespace, timestampOld } from "./constants";
const GRN_DATABASE_NAMESPACE = "gene_regulatory_network_new";
const GRN_DATABASE_NAMESPACE_OLD = "gene_regulatory_network";
const DATABASE_TIMESTAMP_CUTOFF = new Date("2025-01-01");

const timestampNamespace = function(timestamp, namespace, oldNamespace) {
return timestamp < new Date("2025-01-01") ? namespace : oldNamespace;
};

const timestampOld = function(timestamp) {
return timestamp < DATABASE_TIMESTAMP_CUTOFF;
};
var sequelize = new Sequelize(config.databaseName, process.env.DB_USERNAME, process.env.DB_PASSWORD, {
host: config.databaseHost,
dialect: config.databaseDialect,
pool: {
max: 5,
min: 0,
idle: 10000,
},
});

const buildNetworkSourceQuery = function () {
return `SELECT * FROM ${GRN_DATABASE_NAMESPACE_OLD}.source
const buildNetworkSourceQuery = function() {

Check failure on line 15 in server/dals/network-dal.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing space before function parentheses
return `SELECT * FROM gene_regulatory_network.source
UNION ALL
SELECT * FROM ${GRN_DATABASE_NAMESPACE}.source
SELECT * FROM gene_regulatory_network_new.source
ORDER BY time_stamp DESC;`;
};

const buildNetworkGeneFromSourceQuery = function (gene, source, timestamp) {
const namespace = `${timestampNamespace(timestamp,
GRN_DATABASE_NAMESPACE,
GRN_DATABASE_NAMESPACE_OLD
)}.gene`;
const timestamp_query = timestampOld(timestamp)
? ""
: `AND gene.time_stamp ='${timestamp}`;
const buildNetworkGeneFromSourceQuery = function(gene, source, timestamp) {

Check failure on line 22 in server/dals/network-dal.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing space before function parentheses
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}` : "";

Check failure on line 25 in server/dals/network-dal.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Identifier 'timestamp_query' is not in camel case
return `SELECT DISTINCT gene_id, display_gene_id FROM
${namespace} WHERE (gene.gene_id ='${gene}'
OR gene.display_gene_id ='${gene}') ${timestamp_query};`;

Check failure on line 28 in server/dals/network-dal.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Identifier 'timestamp_query' is not in camel case
};

const buildNetworkGenesQuery = function (geneString) {
const buildNetworkGenesQuery = function(geneString) {

Check failure on line 31 in server/dals/network-dal.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing space before function parentheses
let genes = "(";
let geneList = geneString.split(",");
geneList.forEach(
(x) => (genes += `(network.regulator_gene_id =\'${x}\') OR `)
);
geneList.forEach((x) => (genes += `(network.regulator_gene_id =\'${x}\') OR `));
genes = `${genes.substring(0, genes.length - 4)}) AND (`;
geneList.forEach(
(x) => (genes += `(network.target_gene_id =\'${x}\') OR `)
);
geneList.forEach((x) => (genes += `(network.target_gene_id =\'${x}\') OR `));
return `${genes.substring(0, genes.length - 4)})`;
};

const buildGenerateNetworkQuery = function (genes, source, timestamp) {
const namespace = `${timestampNamespace(
timestamp,
GRN_DATABASE_NAMESPACE,
GRN_DATABASE_NAMESPACE_OLD
)}.network`;
const annotation = timestampOld(timestamp) ? "" : ", annotation_type";
const buildGenerateNetworkQuery = function(genes, source, timestamp) {

Check failure on line 40 in server/dals/network-dal.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing space before function parentheses
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";
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),
GenerateNetwork: () =>
buildGenerateNetworkQuery(
query.genes,
query.source,
query.timestamp
),
NetworkGeneFromSource: () => buildNetworkGeneFromSourceQuery(query.gene),
GenerateNetwork: () => buildGenerateNetworkQuery(query.genes, query.source, query.timestamp),
};
if (Object.keys(networkQueries).includes(query.type)) {
return networkQueries[query.type]();
}
};

// 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":
Expand All @@ -102,30 +67,20 @@ const convertResponseToJSON = function (queryType, query, totalOutput) {
const timestamp = connection.time_stamp;
const source = connection.source;
const displayName = connection.display_name;
JSONOutput.sources[
`${displayName}: ${timestamp.toISOString().split("T")[0]}`
] = { timestamp, source };
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;
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,
];
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
);
JSONOutput.links[connection.regulator_gene_id].push(connection.target_gene_id);
}
}
return JSONOutput;
Expand All @@ -138,15 +93,9 @@ module.exports = {
buildNetworkSourceQuery: buildNetworkSourceQuery,
queryNetworkDatabase: function(req, res) {
sequelize
.query(buildQueryByType(req.query.type, req.query), {
type: sequelize.QueryTypes.SELECT,
})
.query(buildQueryByType(req.query.type, req.query), { type: sequelize.QueryTypes.SELECT })
.then(function(stdname) {
const response = convertResponseToJSON(
req.query.type,
req.query,
stdname
);
const response = convertResponseToJSON(req.query.type, req.query, stdname);
return res.send(response);
});
},
Expand Down
148 changes: 65 additions & 83 deletions server/dals/protein-dal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,122 +2,104 @@ const Sequelize = require("sequelize");
require("dotenv").config();
var env = process.env.NODE_ENV || "development";
var config = require("../config/config")[env];
var sequelize = new Sequelize(
config.databaseName,
process.env.DB_USERNAME,
process.env.DB_PASSWORD,
{
host: config.databaseHost,
dialect: config.databaseDialect,
pool: {
max: 5,
min: 0,
idle: 10000
}
}
);

//import { PPI_DATABASE_NAMESPACE, PPI_DATABASE_NAMESPACE_OLD, timestampNamespace, timestampOld } from "./constants";
const PPI_DATABASE_NAMESPACE = "protein_protein_interactions_new";
const PPI_DATABASE_NAMESPACE_OLD = "protein_protein_interactions";
const DATABASE_TIMESTAMP_CUTOFF = new Date("2025-01-01");

const timestampNamespace = function(timestamp, namespace, oldNamespace) {
return timestamp < new Date("2025-01-01") ? namespace : oldNamespace;
};
var sequelize = new Sequelize(config.databaseName, process.env.DB_USERNAME, process.env.DB_PASSWORD, {
host: config.databaseHost,
dialect: config.databaseDialect,
pool: {
max: 5,
min: 0,
idle: 10000,
},
});

const timestampOld = function(timestamp) {
return timestamp < DATABASE_TIMESTAMP_CUTOFF;
};

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

const buildNetworkFromGeneProteinQuery = function (geneProtein, timestamp) {
const namespace = `${timestampNamespace(timestamp, PPI_DATABASE_NAMESPACE, PPI_DATABASE_NAMESPACE_OLD)}`;
const timestamp_query = timestampOld(timestamp) ? "": `AND gene.time_stamp ='${timestamp}`;
const buildNetworkFromGeneProteinQuery = function(geneProtein, timestamp) {
const namespace =
timestamp < new Date("2025-01-01") ? "protein_protein_interactions" : "protein_protein_interactions_new";
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) ${timestamp_query};`;
LOWER(gene.gene_id) = LOWER(protein.gene_systematic_name) AND gene.time_stamp = ${timestamp};`;
};

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

const buildGenerateProteinNetworkQuery = function (proteins, timestamp, source) {
const namespace = `${timestampNamespace(timestamp, PPI_DATABASE_NAMESPACE, PPI_DATABASE_NAMESPACE_OLD)}`;
const annotation = timestampOld(timestamp) ? "" : ", annotation_type";
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 buildQueryByType = function (query) {
const buildQueryByType = function(query) {
const networkQueries = {
"NetworkSource": () => buildNetworkSourceQuery(),
"NetworkFromGeneProtein": () => buildNetworkFromGeneProteinQuery(query.geneProtein),
"GenerateProteinNetwork": () => buildGenerateProteinNetworkQuery(query.proteins, query.timestamp, query.source)
NetworkSource: () => buildNetworkSourceQuery(),
NetworkFromGeneProtein: () => buildNetworkFromGeneProteinQuery(query.geneProtein),
GenerateProteinNetwork: () => buildGenerateProteinNetworkQuery(query.proteins, query.timestamp, query.source),
};
if (Object.keys(networkQueries).includes(query.type)) {
return networkQueries[query.type]();
}
};

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) {
const response = convertResponseToJSON(req.query.type, stdname);
return res.send(response);
});
}
};
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);
});
},
};

0 comments on commit 1e57de5

Please sign in to comment.