Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris7 committed Aug 4, 2024
1 parent a592b5a commit f6ec6dd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 60 deletions.
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ pub fn get_connection(db_path: &str) -> Connection {
conn
}

pub fn run_query(conn: &Connection, query: &str) {
let mut stmt = conn.prepare(query).unwrap();
for entry in stmt.query_map([], |row| Ok(println!("{row:?}"))).unwrap() {
println!("{entry:?}");
}
}

pub fn calculate_hash(t: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(t);
Expand Down
61 changes: 2 additions & 59 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,22 +202,19 @@ impl BlockGroup {

row = it.next().unwrap();
}
println!("new edges {edge_map:?}");

let existing_paths = Path::get_paths(
conn,
"SELECT * from path where block_group_id = ?1",
vec![Value::from(source_block_group_id)],
);
println!("eps {existing_paths:?}");

for path in existing_paths {
let mut new_edges = vec![];
for edge in path.edges {
new_edges.push(*edge_map.get(&edge).unwrap());
}
let new_p = Path::create(conn, &path.name, target_block_group_id, new_edges);
println!("made {new_p:?}");
Path::create(conn, &path.name, target_block_group_id, new_edges);
}
}

Expand Down Expand Up @@ -359,7 +356,6 @@ impl BlockGroup {
// that means we have an edge with the chromosome index, that connects our start/end coordinates with the new block id

let path = Path::get(conn, path_id);
let block_group_id = path.block_group_id;
let graph = PathEdge::edges_to_graph(conn, path.id);
println!("{path:?} {graph:?}");
let query = format!("SELECT id, sequence_hash, block_group_id, start, end, strand from block where id in ({block_ids})", block_ids = graph.nodes().map(|k| format!("{k}")).collect::<Vec<_>>().join(","));
Expand Down Expand Up @@ -396,18 +392,12 @@ impl BlockGroup {
let mut path_end = 0;
let mut new_edges = vec![];
let mut previous_block: Option<&Block> = None;
let mut next_node = dfs.next(&graph);
println!("{blocks:?}");
while next_node.is_some() {
let nx = next_node.unwrap();
println!("nx is {nx}");
while let Some(nx) = dfs.next(&graph) {
let block = blocks.get(&(nx as i32)).unwrap();
let block_length = (block.end - block.start);
path_end += block_length;

// do stuff here
println!("{nx} {block:?} {start} {end} {path_start} {path_end}");

let contains_start = path_start <= start && start < path_end;
let contains_end = path_start <= end && end < path_end;
let overlap = path_start <= end && start <= path_end;
Expand Down Expand Up @@ -501,7 +491,6 @@ impl BlockGroup {
break;
}
previous_block = Some(block);
next_node = dfs.next(&graph);
}

println!("change is {path:?} {graph:?} {blocks:?} {new_edges:?}");
Expand All @@ -510,52 +499,6 @@ impl BlockGroup {
Edge::create(conn, new_edge.0, new_edge.1, chromosome_index, phased);
}
}

// TODO: move this to path, doesn't belong in block group
pub fn sequence(
conn: &mut Connection,
collection_name: &str,
sample_name: Option<&String>,
block_group_name: &str,
) -> String {
struct SequenceBlock {
sequence: String,
strand: String,
}
let mut query;
let mut placeholders: Vec<rusqlite::types::Value> =
vec![collection_name.to_string().into()];

if sample_name.is_some() {
query = "WITH RECURSIVE traverse(block_id, block_sequence, block_start, block_end, block_strand, depth) AS (
SELECT edges.source_id, substr(seq.sequence, block.start + 1, block.end - block.start), block.start, block.end, block.strand, 0 as depth FROM block_group left join block on (block_group.id = block.block_group_id) left join sequence seq on (seq.hash = block.sequence_hash) left join edges on (block.id = edges.source_id or block.id = edges.target_id) WHERE block_group.collection_name = ?1 AND block_group.sample_name = ?2 AND block_group.name = ?3 and edges.target_id is null
UNION
SELECT e2.source_id, substr(seq2.sequence, b2.start + 1, b2.end - b2.start), b2.start, b2.end, b2.strand, depth + 1 FROM edges e2 left join block b2 on (b2.id = e2.source_id) left join sequence seq2 on (seq2.hash = b2.sequence_hash) JOIN traverse t2 ON e2.target_id = t2.block_id order by depth desc
) SELECT block_sequence as sequence, block_strand as strand FROM traverse;";
placeholders.push(sample_name.unwrap().clone().into());
} else {
query = "WITH RECURSIVE traverse(block_id, block_sequence, block_start, block_end, block_strand, depth) AS (
SELECT edges.source_id, substr(seq.sequence, block.start + 1, block.end - block.start), block.start, block.end, block.strand, 0 as depth FROM block_group left join block on (block_group.id = block.block_group_id) left join sequence seq on (seq.hash = block.sequence_hash) left join edges on (block.id = edges.source_id or block.id = edges.target_id) WHERE block_group.collection_name = ?1 AND block_group.sample_name is null AND block_group.name = ?2 and edges.target_id is null
UNION
SELECT e2.source_id, substr(seq2.sequence, b2.start + 1, b2.end - b2.start), b2.start, b2.end, b2.strand, depth + 1 FROM edges e2 left join block b2 on (b2.id = e2.source_id) left join sequence seq2 on (seq2.hash = b2.sequence_hash) JOIN traverse t2 ON e2.target_id = t2.block_id order by depth desc
) SELECT block_sequence as sequence, block_strand as strand FROM traverse;"
}
placeholders.push(block_group_name.to_string().into());
let mut stmt = conn.prepare(query).unwrap();
let mut blocks = stmt
.query_map(params_from_iter(placeholders), |row| {
Ok(SequenceBlock {
sequence: row.get(0)?,
strand: row.get(1)?,
})
})
.unwrap();
let mut sequence = "".to_string();
for block in blocks {
sequence.push_str(&block.unwrap().sequence);
}
sequence
}
}

#[cfg(test)]
Expand Down
1 change: 0 additions & 1 deletion src/models/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ impl PathEdge {
let mut graph = DiGraphMap::new();
for edge in rows {
let (source, target) = edge.unwrap();
println!("edg eis {source:?} {target:?}");
if let Some(source_value) = source {
graph.add_node(source_value);
if let Some(target_value) = target {
Expand Down

0 comments on commit f6ec6dd

Please sign in to comment.