Skip to content

Commit

Permalink
made helper funcs for checking timestamp and added constants to dals/…
Browse files Browse the repository at this point in the history
…constants.js, removed trailing spaces.
  • Loading branch information
ceciliazaragoza committed Feb 26, 2025
1 parent d0fdb9a commit 8b8a088
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 77 deletions.
8 changes: 4 additions & 4 deletions database2/network-database/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ class Constants:
# database namespace
GRN_DATABASE_NAMESPACE = "gene_regulatory_network_new"
PPI_DATABASE_NAMESPACE = "protein_protein_interactions_new"

# network types
GRN_NETWORK_MODE = "grn"
PPI_NETWORK_MODE = "ppi"

# data file paths
DATA_DIRECTORY = "script-results"
GENE_DATA_FILEPATH = DATA_DIRECTORY + "/gene_data.tsv"
PROTEIN_DATA_FILEPATH = DATA_DIRECTORY + "/protein_data.tsv"
GENE_REGULATORY_NETWORK_DATA_FILEPATH = DATA_DIRECTORY + "/gene_regulatory_network_data.tsv"
PROTEIN_PROTEIN_INTERACTIONS_DATA_FILEPATH = DATA_DIRECTORY + "/protein_protein_interactions_data.tsv"
SOURCE_DATA_FILEPATH = DATA_DIRECTORY + "/source_data.tsv"

# missing and update file paths
MISSING_DATA_DIRECTORY = DATA_DIRECTORY + "/missing_data"
UPDATE_DATA_DIRECTORY = DATA_DIRECTORY + "/update_data"
Expand All @@ -24,4 +24,4 @@ class Constants:
UPDATE_PPI_GENE_DATA_FILEPATH = UPDATE_DATA_DIRECTORY + "/update_ppi_gene_data.tsv"
MISSING_PROTEIN_DATA_FILEPATH = MISSING_DATA_DIRECTORY + "/missing_protein_data.tsv"
UPDATE_PROTEIN_DATA_FILEPATH = UPDATE_DATA_DIRECTORY + "/update_protein_data.tsv"
UPDATE_PROTEIN_NAME_DATA_FILEPATH = UPDATE_DATA_DIRECTORY + "/update_protein_name_data.tsv"
UPDATE_PROTEIN_NAME_DATA_FILEPATH = UPDATE_DATA_DIRECTORY + "/update_protein_name_data.tsv"
13 changes: 13 additions & 0 deletions server/dals/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const GRN_DATABASE_NAMESPACE = "gene_regulatory_network_new";
export const GRN_DATABASE_NAMESPACE_OLD = "gene_regulatory_network";
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) {

Check failure on line 7 in server/dals/constants.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing space before function parentheses
return timestamp < new Date("2025-01-01") ? namespace : oldNamespace;

Check failure on line 8 in server/dals/constants.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected indentation of 4 spaces but found 2
};

export const timestampOld = function(timestamp) {

Check failure on line 11 in server/dals/constants.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing space before function parentheses
return timestamp < DATABASE_TIMESTAMP_CUTOFF;
}

Check failure on line 13 in server/dals/constants.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing semicolon
143 changes: 89 additions & 54 deletions server/dals/network-dal.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import {
GRN_DATABASE_NAMESPACE,
GRN_DATABASE_NAMESPACE_OLD,
timestampNamespace,
timestampOld,
} from "./constants";

const Sequelize = require("sequelize");
require("dotenv").config();
var env = process.env.NODE_ENV || "development";
Expand All @@ -12,24 +19,26 @@ var sequelize = new Sequelize(
pool: {
max: 5,
min: 0,
idle: 10000
}
idle: 10000,
},
}
);

const buildNetworkSourceQuery = function () {
return `SELECT * FROM gene_regulatory_network.source
UNION ALL
SELECT * FROM gene_regulatory_network_new.source
return `SELECT * FROM ${GRN_DATABASE_NAMESPACE_OLD}.source
UNION ALL
SELECT * FROM ${GRN_DATABASE_NAMESPACE}.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 namespace = `${timestampNamespace(timestamp,
GRN_DATABASE_NAMESPACE,
GRN_DATABASE_NAMESPACE_OLD
)}.gene`;
const timestamp_query = timestampOld(timestamp)

Check failure on line 39 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
? ""
: `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};`;

Check failure on line 44 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
Expand All @@ -38,29 +47,39 @@ const buildNetworkGeneFromSourceQuery = function (gene, source, timestamp) {
const buildNetworkGenesQuery = function (geneString) {
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 =
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
const namespace = `${timestampNamespace(
timestamp,
GRN_DATABASE_NAMESPACE,
GRN_DATABASE_NAMESPACE_OLD
)}.network`;
const annotation = timestampOld(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 networkQueries = {
"NetworkSource": () => buildNetworkSourceQuery(),
"NetworkGeneFromSource": () => buildNetworkGeneFromSourceQuery(query.gene),
"GenerateNetwork": () => buildGenerateNetworkQuery(query.genes, query.source, query.timestamp)
NetworkSource: () => buildNetworkSourceQuery(),
NetworkGeneFromSource: () =>
buildNetworkGeneFromSourceQuery(query.gene),
GenerateNetwork: () =>
buildGenerateNetworkQuery(
query.genes,
query.source,
query.timestamp
),
};
if (Object.keys(networkQueries).includes(query.type)) {
return networkQueries[query.type]();
Expand All @@ -71,42 +90,58 @@ const buildQueryByType = function (queryType, query) {
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":

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

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected indentation of 4 spaces but found 8
JSONOutput.sources = {};

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

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected indentation of 8 spaces but found 12
totalOutput.forEach(function(connection) {

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

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected indentation of 8 spaces but found 12

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

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing space before function parentheses
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) {
sequelize.query(buildQueryByType(req.query.type, req.query), { type: sequelize.QueryTypes.SELECT })
.then(function (stdname) {
const response = convertResponseToJSON(req.query.type, req.query, stdname);
queryNetworkDatabase: function(req, res) {
sequelize
.query(buildQueryByType(req.query.type, req.query), {
type: sequelize.QueryTypes.SELECT,
})
.then(function(stdname) {
const response = convertResponseToJSON(
req.query.type,
req.query,
stdname
);
return res.send(response);
});
}
};
},
};
32 changes: 13 additions & 19 deletions server/dals/protein-dal.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import {
PPI_DATABASE_NAMESPACE,
PPI_DATABASE_NAMESPACE_OLD,
timestampNamespace,
timestampOld,
} from "./constants";

const Sequelize = require("sequelize");
require("dotenv").config();
var env = process.env.NODE_ENV || "development";
Expand All @@ -18,22 +25,15 @@ var sequelize = new Sequelize(
);

const buildNetworkSourceQuery = function () {
return `SELECT * FROM protein_protein_interactions.source
return `SELECT * FROM ${PPI_DATABASE_NAMESPACE_OLD}.source
UNION ALL
SELECT * FROM protein_protein_interactions_new.source
SELECT * FROM ${PPI_DATABASE_NAMESPACE}.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 timestamp_query =
timestamp < new Date("2025-01-01")
? `AND gene.time_stamp ='${timestamp}`
: "";
const namespace = `${timestampNamespace(timestamp, PPI_DATABASE_NAMESPACE, PPI_DATABASE_NAMESPACE_OLD)}`;
const timestamp_query = timestampOld(timestamp) ? "": `AND gene.time_stamp ='${timestamp}`;
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}')
Expand All @@ -48,16 +48,11 @@ const buildNetworkProteinsQuery = function (proteinString) {
proteins = `${proteins.substring(0, proteins.length - 4)}) AND (`;
proteinList.forEach(x => proteins += ( `(physical_interactions.protein2 =\'${x}\') OR `));
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";
const namespace = `${timestampNamespace(timestamp, PPI_DATABASE_NAMESPACE, PPI_DATABASE_NAMESPACE_OLD)}`;
const annotation = timestampOld(timestamp) ? "" : ", 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;`;
Expand Down Expand Up @@ -109,7 +104,6 @@ const convertResponseToJSON = function (queryType, totalOutput) {
default:
return JSONOutput;
}

};

module.exports = {
Expand Down

0 comments on commit 8b8a088

Please sign in to comment.