Skip to content

Commit

Permalink
Initial experiment with lineage
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris7 committed Aug 28, 2024
1 parent 39060ba commit 7b1ae61
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
9 changes: 9 additions & 0 deletions migrations/01-initial/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ CREATE TABLE block_group (
);
CREATE UNIQUE INDEX block_group_uidx ON block_group(collection_name, sample_name, name);

CREATE TABLE block_group_lineage {
id INTEGER PRIMARY KEY NOT NULL,
source_id INTEGER NOT NULL,
target_id INTEGER NOT NULL,
FOREIGN KEY(source_id) REFERENCES block_group(id),
FOREIGN KEY(target_id) REFERENCES block_group(id)
}
CREATE UNIQUE INDEX block_group_lineage_uidx ON block_group_lineage(source_id, target_id);

CREATE TABLE path (
id INTEGER PRIMARY KEY NOT NULL,
block_group_id INTEGER NOT NULL,
Expand Down
1 change: 1 addition & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::fmt::*;

pub mod block_group;
pub mod block_group_edge;
pub mod block_group_lineage;
pub mod edge;
pub mod path;
pub mod path_edge;
Expand Down
4 changes: 3 additions & 1 deletion src/models/block_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use std::collections::{HashMap, HashSet};

use crate::graph::all_simple_paths;
use crate::models::block_group_edge::BlockGroupEdge;
use crate::models::block_group_lineage::BlockGroupLineage;
use crate::models::edge::{Edge, EdgeData};
use crate::models::path::{NewBlock, Path};
use crate::models::path_edge::PathEdge;
use crate::models::sequence::{NewSequence, Sequence};
use crate::models::sequence::Sequence;

#[derive(Debug)]
pub struct BlockGroup {
Expand Down Expand Up @@ -127,6 +128,7 @@ impl BlockGroup {
}
}
let new_bg_id = BlockGroup::create(conn, collection_name, Some(sample_name), group_name);
BlockGroupLineage::create(conn, bg_id, new_bg_id.id);

// clone parent blocks/edges/path
BlockGroup::clone(conn, bg_id, new_bg_id.id);
Expand Down
46 changes: 46 additions & 0 deletions src/models/block_group_lineage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use rusqlite::Connection;

#[derive(Debug)]
pub struct BlockGroupLineage {
pub id: i32,
pub source_id: i32,
pub target_id: i32,
}

impl BlockGroupLineage {
pub fn create(conn: &Connection, source_id: i32, target_id: i32) -> BlockGroupLineage {
let query =
"INSERT INTO block_group_lineage (source_id, target_id) VALUES (?1, ?2) RETURNING (id)";
let mut stmt = conn.prepare(query).unwrap();
match stmt.query_row((source_id, target_id), |row| {
Ok(BlockGroupLineage {
id: row.get(0)?,
source_id,
target_id,
})
}) {
Ok(res) => res,
Err(rusqlite::Error::SqliteFailure(err, details)) => {
if err.code == rusqlite::ErrorCode::ConstraintViolation {
println!("{err:?} {details:?}");
BlockGroupLineage {
id: conn
.query_row(
"select id from block_group_lineage where source_id = ?1 and target_id = ?2",
(source_id, target_id),
|row| row.get(0),
)
.unwrap(),
source_id,
target_id,
}
} else {
panic!("something bad happened querying the database")
}
}
Err(_) => {
panic!("something bad happened querying the database")
}
}
}
}

0 comments on commit 7b1ae61

Please sign in to comment.