Skip to content

Commit

Permalink
add: add combined cgm tracing view generator class #29
Browse files Browse the repository at this point in the history
  • Loading branch information
MeetAnithaVarghese committed Oct 3, 2024
1 parent 7607856 commit ccc34af
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { DB } from "https://deno.land/x/sqlite/mod.ts";

// Path to your existing SQLite database
const dbFilePath = "resource-surveillance.sqlite.db";

// Open the existing database
const db = new DB(dbFilePath);
console.log(`Opened database: ${dbFilePath}`);

// Create error log table if it doesn't exist
db.execute(`
CREATE TABLE IF NOT EXISTS error_log (
errorLogId INTEGER PRIMARY KEY AUTOINCREMENT,
datetime TEXT DEFAULT (datetime('now')),
error_message TEXT
);
`);

console.log("Error log table created or already exists.");

// Get the list of participant IDs from the view
const participants = db.query("SELECT DISTINCT patient_id FROM drh_participant_file_names;");

// Array to hold table names for the combined view
const tableNames: string[] = [];

for (const [patient_id_raw] of participants) {
// Cast patient_id_raw to a string type
const patient_id: string = patient_id_raw as string;

console.log(`Processing participant: ${patient_id}`);

// Get the file names associated with the participant
const [file_names_row] = db.query("SELECT file_names FROM drh_participant_file_names WHERE patient_id = ?", [patient_id]);
if (!file_names_row) {
console.log(`No file names found for participant ${patient_id}.`);
continue;
}

const file_names = file_names_row[0];
console.log(`File names for participant ${patient_id}: ${file_names}`);

if (file_names) {
// Construct table names
const participantTableNames = file_names.split(', ').map(fileName => `uniform_resource_${fileName}`);
tableNames.push(...participantTableNames); // Add participant's tables to the main array
} else {
console.log(`No file names found for participant ${patient_id}.`);
}
}

// Create the combined view for all participants
if (tableNames.length > 0) {
const combinedUnionAllQuery = tableNames.map(tableName =>
`SELECT
'${tableName}' as participant_id,
strftime('%Y-%m-%d %H:%M:%S', date_time) as Date_Time,
CAST(CGM_Value as REAL) as CGM_Value
FROM ${tableName}`
).join(' UNION ALL ');

const createCombinedViewSql = `CREATE VIEW IF NOT EXISTS combined_cgm_tracing AS ${combinedUnionAllQuery};`;

console.log(`Creating combined view with SQL:\n${createCombinedViewSql}`);

try {
db.execute(createCombinedViewSql);
console.log("Combined view 'combined_cgm_tracing' created successfully.");
} catch (error) {
console.error("Error creating combined view 'combined_cgm_tracing':", error);
const sqlQuery = "INSERT INTO error_log (error_message) VALUES (?);";
const params = JSON.stringify({ message: error.message });

// Combine into a single string
const combinedString = `${sqlQuery} Params: ${params}`;

console.log(combinedString);

// Insert error log as JSON
db.execute(combinedString);
}
} else {
console.log("No participant tables found, so the combined view will not be created.");
}

// Close the database connection
db.close();
console.log(`Closed database: ${dbFilePath}`);
34 changes: 17 additions & 17 deletions lib/service/diabetes-research-hub/package.sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,15 @@ export class DRHSqlPages extends spn.TypicalSqlPageNotebook {
SELECT
'These are scientific professionals and medical experts who design and conduct studies related to diabetes management and treatment. Their expertise ranges from clinical research to data analysis, and they are crucial in interpreting results and guiding future research directions.Principal investigators lead the research projects, overseeing the study design, implementation, and data collection. They ensure the research adheres to ethical standards and provides valuable insights into diabetes management.' as contents;
SELECT 'table' as component, 1 as search, 1 as sort, 1 as hover, 1 as striped_rows;
SELECT * from drh_investigator_data;
SELECT * from drh_investigator;
SELECT
'text' as component,
'Institution' as title;
SELECT
'The researchers and investigators are associated with various institutions, including universities, research institutes, and hospitals. These institutions provide the necessary resources, facilities, and support for conducting high-quality research. Each institution brings its unique strengths and expertise to the collaborative research efforts.' as contents;
SELECT 'table' as component, 1 as search, 1 as sort, 1 as hover, 1 as striped_rows;
SELECT * from drh_institution_data;
SELECT * from drh_institution;
SELECT
Expand All @@ -320,7 +320,7 @@ export class DRHSqlPages extends spn.TypicalSqlPageNotebook {
SELECT
'Within these institutions, specialized labs are equipped with state-of-the-art technology to conduct diabetes research. These labs focus on different aspects of diabetes studies, such as glucose monitoring, metabolic analysis, and data processing. They play a critical role in executing experiments, analyzing samples, and generating data that drive research conclusions.' as contents;
SELECT 'table' as component, 1 as search, 1 as sort, 1 as hover, 1 as striped_rows;
SELECT * from drh_lab_data;
SELECT * from drh_lab;
Expand All @@ -334,7 +334,7 @@ export class DRHSqlPages extends spn.TypicalSqlPageNotebook {
siblingOrder: 5,
})
"drh/study-related-data/index.sql"() {
const viewName = `drh_participant_data`;
const viewName = `drh_participant`;
const pagination = this.pagination({ tableOrViewName: viewName });
return this.SQL`
${this.activePageTitle()}
Expand All @@ -357,7 +357,7 @@ export class DRHSqlPages extends spn.TypicalSqlPageNotebook {
' as contents_md;
SELECT 'table' as component, 1 as search, 1 as sort, 1 as hover, 1 as striped_rows;
SELECT * from drh_study_data;
SELECT * from drh_study;
SELECT
Expand All @@ -378,7 +378,7 @@ Research sites are locations where the studies are conducted. They include clini
' as contents_md;
SELECT 'table' as component, 1 as search, 1 as sort, 1 as hover, 1 as striped_rows;
SELECT * from drh_site_data;
SELECT * from drh_site;
Expand All @@ -391,7 +391,7 @@ Research sites are locations where the studies are conducted. They include clini
siblingOrder: 6,
})
"drh/uniform-resource-participant.sql"() {
const viewName = `drh_participant_data`;
const viewName = `drh_participant`;
const pagination = this.pagination({ tableOrViewName: viewName });
return this.SQL`
${this.activePageTitle()}
Expand Down Expand Up @@ -441,7 +441,7 @@ This section contains information about the authors involved in study publicatio
' as contents_md;
SELECT 'table' as component, 1 as search, 1 as sort, 1 as hover, 1 as striped_rows;
SELECT * from drh_author_data;
SELECT * from drh_author;
SELECT
'text' as component,
'
Expand All @@ -461,7 +461,7 @@ This section provides information about the publications resulting from a study.
' as contents_md;
SELECT 'table' as component, 1 as search, 1 as sort, 1 as hover, 1 as striped_rows;
SELECT * from drh_publication_data;
SELECT * from drh_publication;
`;
Expand Down Expand Up @@ -694,43 +694,43 @@ select
'MRN: ' || participant_id || '' AS title,
' ' AS description
FROM
drh_participant_data
drh_participant
WHERE participant_id = $participant_id;
SELECT
'Study: ' || study_arm || '' AS title,
' ' AS description
FROM
drh_participant_data
drh_participant
WHERE participant_id = $participant_id;
SELECT
'Age: '|| age || ' Years' AS title,
' ' AS description
FROM
drh_participant_data
drh_participant
WHERE participant_id = $participant_id;
SELECT
'hba1c: ' || baseline_hba1c || '' AS title,
' ' AS description
FROM
drh_participant_data
drh_participant
WHERE participant_id = $participant_id;
SELECT
'BMI: '|| bmi || '' AS title,
' ' AS description
FROM
drh_participant_data
drh_participant
WHERE participant_id = $participant_id;
SELECT
'Diabetes Type: '|| diabetes_type || '' AS title,
' ' AS description
FROM
drh_participant_data
drh_participant
WHERE participant_id = $participant_id;
SELECT
Expand Down Expand Up @@ -766,7 +766,7 @@ select
siblingOrder: 12,
})
"drh/study-participant-dashboard/index.sql"() {
const viewName = `drh_participant_data`;
const viewName = `drh_participant`;
const pagination = this.pagination({ tableOrViewName: viewName });
return this.SQL`
${this.activePageTitle()}
Expand Down Expand Up @@ -987,7 +987,7 @@ SELECT
siblingOrder: 19,
})
"drh/participant-related-data/index.sql"() {
const viewName = `drh_participant_data`;
const viewName = `drh_participant`;
const pagination = this.pagination({ tableOrViewName: viewName });
return this.SQL`
${this.activePageTitle()}
Expand Down
58 changes: 29 additions & 29 deletions lib/service/diabetes-research-hub/stateless.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- Drop and recreate the device_data view
DROP VIEW IF EXISTS drh_device_data;
CREATE VIEW drh_device_data AS
-- Drop and recreate the device view
DROP VIEW IF EXISTS drh_device;
CREATE VIEW drh_device AS
SELECT device_id, name, created_at
FROM device d;

Expand Down Expand Up @@ -64,9 +64,9 @@ SELECT
input_text, exec_error_text, output_text, output_nature, narrative_md
FROM orchestration_session_exec;

-- Drop and recreate the study_data view
DROP VIEW IF EXISTS drh_study_data;
CREATE VIEW drh_study_data AS
-- Drop and recreate the study view
DROP VIEW IF EXISTS drh_study;
CREATE VIEW drh_study AS
SELECT
study_id, study_name, start_date, end_date, treatment_modalities,
funding_source, nct_number, study_description
Expand All @@ -82,54 +82,54 @@ SELECT
data_end_date, study_id
FROM uniform_resource_cgm_file_metadata;

-- Drop and recreate the author_data view
DROP VIEW IF EXISTS drh_author_data;
CREATE VIEW drh_author_data AS
-- Drop and recreate the author view
DROP VIEW IF EXISTS drh_author;
CREATE VIEW drh_author AS
SELECT
author_id, name, email, investigator_id, study_id
FROM uniform_resource_author;

-- Drop and recreate the institution_data view
DROP VIEW IF EXISTS drh_institution_data;
CREATE VIEW drh_institution_data AS
-- Drop and recreate the institution view
DROP VIEW IF EXISTS drh_institution;
CREATE VIEW drh_institution AS
SELECT
institution_id, institution_name, city, state, country
FROM uniform_resource_institution;

-- Drop and recreate the investigator_data view
DROP VIEW IF EXISTS drh_investigator_data;
CREATE VIEW drh_investigator_data AS
-- Drop and recreate the investigator view
DROP VIEW IF EXISTS drh_investigator;
CREATE VIEW drh_investigator AS
SELECT
investigator_id, investigator_name, email, institution_id, study_id
FROM uniform_resource_investigator;

-- Drop and recreate the lab_data view
DROP VIEW IF EXISTS drh_lab_data;
CREATE VIEW drh_lab_data AS
-- Drop and recreate the lab view
DROP VIEW IF EXISTS drh_lab;
CREATE VIEW drh_lab AS
SELECT
lab_id, lab_name, lab_pi, institution_id, study_id
FROM uniform_resource_lab;

-- Drop and recreate the participant_data view
DROP VIEW IF EXISTS drh_participant_data;
CREATE VIEW drh_participant_data AS
-- Drop and recreate the participant view
DROP VIEW IF EXISTS drh_participant;
CREATE VIEW drh_participant AS
SELECT
participant_id, study_id, site_id, diagnosis_icd, med_rxnorm,
treatment_modality, gender, race_ethnicity, age, bmi, baseline_hba1c,
diabetes_type, study_arm
FROM uniform_resource_participant;

-- Drop and recreate the publication_data view
DROP VIEW IF EXISTS drh_publication_data;
CREATE VIEW drh_publication_data AS
-- Drop and recreate the publication view
DROP VIEW IF EXISTS drh_publication;
CREATE VIEW drh_publication AS
SELECT
publication_id, publication_title, digital_object_identifier,
publication_site, study_id
FROM uniform_resource_publication;

-- Drop and recreate the site_data view
DROP VIEW IF EXISTS drh_site_data;
CREATE VIEW drh_site_data AS
-- Drop and recreate the site view
DROP VIEW IF EXISTS drh_site;
CREATE VIEW drh_site AS
SELECT
study_id, site_id, site_name, site_type
FROM uniform_resource_site;
Expand Down Expand Up @@ -268,8 +268,8 @@ LEFT JOIN uniform_resource_investigator i ON s.study_id = i.study_id
GROUP BY s.study_id, s.study_name, s.study_description, s.start_date, s.end_date, s.nct_number;


DROP TABLE IF EXISTS raw_cgm_data_lst_cached;
CREATE TABLE raw_cgm_data_lst_cached AS
DROP TABLE IF EXISTS raw_cgm_lst_cached;
CREATE TABLE raw_cgm_lst_cached AS
SELECT * FROM drh_raw_cgm_table_lst;

DROP VIEW IF EXISTS drh_study_files_table_info;
Expand Down

0 comments on commit ccc34af

Please sign in to comment.