Skip to content

Commit bde522c

Browse files
authored
Merge pull request #169 from ginkgobioworks/empty-graph-view
Allow no option viewing
2 parents 2c64126 + c9978be commit bde522c

File tree

3 files changed

+63
-41
lines changed

3 files changed

+63
-41
lines changed

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,11 @@ enum Commands {
181181
gfa: Option<String>,
182182
},
183183
/// Show a visual representation of a graph in the terminal
184-
#[command(arg_required_else_help(true))]
184+
#[command()]
185185
View {
186186
/// The name of the graph to view
187187
#[clap(index = 1)]
188-
graph: String,
188+
graph: Option<String>,
189189
/// View the graph for a specific sample
190190
#[arg(short, long)]
191191
sample: Option<String>,
@@ -673,7 +673,7 @@ fn main() {
673673
// view_block_group is a long-running operation that manages its own transactions
674674
view_block_group(
675675
&conn,
676-
graph,
676+
graph.clone(),
677677
sample.clone(),
678678
collection_name,
679679
position.clone(),

src/views/block_group.rs

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::models::{block_group::BlockGroup, node::Node, path::Path, traits::Query};
1+
use crate::graph::{GenGraph, GraphNode};
2+
use crate::models::{
3+
block_group::BlockGroup, node::Node, node::PATH_START_NODE_ID, path::Path, traits::Query,
4+
};
25
use crate::progress_bar::{get_handler, get_time_elapsed_bar};
36
use crate::views::block_group_viewer::{PlotParameters, Viewer};
47
use crate::views::collection::{CollectionExplorer, CollectionExplorerState, FocusZone};
@@ -21,9 +24,20 @@ use std::time::{Duration, Instant};
2124
// Frequency by which we check for external updates to the db
2225
const REFRESH_INTERVAL: u64 = 3; // seconds
2326

27+
fn get_empty_graph() -> GenGraph {
28+
let mut g = GenGraph::new();
29+
g.add_node(GraphNode {
30+
block_id: -1,
31+
node_id: PATH_START_NODE_ID,
32+
sequence_start: 0,
33+
sequence_end: 0,
34+
});
35+
g
36+
}
37+
2438
pub fn view_block_group(
2539
conn: &Connection,
26-
name: &str,
40+
name: Option<String>,
2741
sample_name: Option<String>,
2842
collection_name: &str,
2943
position: Option<String>, // Node ID and offset
@@ -32,27 +46,6 @@ pub fn view_block_group(
3246
let bar = progress_bar.add(get_time_elapsed_bar());
3347
let _ = progress_bar.println("Loading block group");
3448

35-
// Get the block group for two cases: with and without a sample
36-
let block_group = if let Some(ref sample_name) = sample_name {
37-
BlockGroup::get(conn, "select * from block_groups where collection_name = ?1 AND sample_name = ?2 AND name = ?3",
38-
params![collection_name, sample_name, name])
39-
} else {
40-
// modified version:
41-
BlockGroup::get(conn, "select * from block_groups where collection_name = ?1 AND sample_name is null AND name = ?2", params![collection_name, name])
42-
};
43-
44-
if block_group.is_err() {
45-
panic!(
46-
"No block group found with name {} and sample {:?} in collection {} ",
47-
name,
48-
sample_name.clone().unwrap_or_else(|| "null".to_string()),
49-
collection_name
50-
);
51-
}
52-
53-
let block_group = block_group.unwrap();
54-
let block_group_id = block_group.id;
55-
5649
// Get the node object corresponding to the position given by the user
5750
let origin = if let Some(position_str) = position {
5851
let parts = position_str.split(":").collect::<Vec<&str>>();
@@ -68,12 +61,51 @@ pub fn view_block_group(
6861
} else {
6962
None
7063
};
64+
65+
// Create explorer and its state that persists across frames
66+
let mut explorer = CollectionExplorer::new(conn, collection_name);
67+
let mut explorer_state = CollectionExplorerState::new();
68+
if let Some(ref s) = sample_name {
69+
explorer_state.toggle_sample(s);
70+
}
71+
72+
let mut block_graph;
73+
let mut block_group_id: Option<i64> = None;
74+
let mut focus_zone = FocusZone::Sidebar;
75+
76+
if let Some(name) = name {
77+
// Get the block group for two cases: with and without a sample
78+
let block_group = if let Some(ref sample_name) = sample_name {
79+
BlockGroup::get(conn, "select * from block_groups where collection_name = ?1 AND sample_name = ?2 AND name = ?3",
80+
params![collection_name, sample_name, name])
81+
} else {
82+
// modified version:
83+
BlockGroup::get(conn, "select * from block_groups where collection_name = ?1 AND sample_name is null AND name = ?2", params![collection_name, name])
84+
};
85+
86+
if block_group.is_err() {
87+
panic!(
88+
"No block group found with name {:?} and sample {:?} in collection {} ",
89+
name,
90+
sample_name.clone().unwrap_or_else(|| "null".to_string()),
91+
collection_name
92+
);
93+
}
94+
95+
let block_group = block_group.unwrap();
96+
block_group_id = Some(block_group.id);
97+
block_graph = BlockGroup::get_graph(conn, block_group.id);
98+
explorer_state.selected_block_group_id = Some(block_group.id);
99+
focus_zone = FocusZone::Canvas;
100+
} else {
101+
block_graph = get_empty_graph();
102+
}
103+
71104
bar.finish();
72105

73106
// Create the viewer and the initial graph
74107
let bar = progress_bar.add(get_time_elapsed_bar());
75108
let _ = progress_bar.println("Pre-computing layout in chunks");
76-
let mut block_graph = BlockGroup::get_graph(conn, block_group_id);
77109

78110
let mut viewer = if let Some(origin) = origin {
79111
Viewer::with_origin(&block_graph, conn, PlotParameters::default(), origin)
@@ -96,19 +128,8 @@ pub fn view_block_group(
96128
let show_sidebar = true;
97129
let mut tui_layout_change = false;
98130

99-
// Focus management
100-
let mut focus_zone = FocusZone::Canvas;
101-
102-
// Create explorer and its state that persists across frames
103-
let mut explorer = CollectionExplorer::new(conn, collection_name);
104-
let mut explorer_state =
105-
CollectionExplorerState::with_selected_block_group(Some(block_group_id));
106-
if let Some(ref s) = sample_name {
107-
explorer_state.toggle_sample(s);
108-
}
109-
110131
// Track the last selected block group to detect changes
111-
let mut last_selected_block_group_id = Some(block_group_id);
132+
let mut last_selected_block_group_id = block_group_id;
112133
// Track if we're loading a new block group
113134
let mut is_loading = false;
114135
let mut last_refresh = Instant::now();

src/views/block_group_viewer.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,8 +631,9 @@ impl<'a> Viewer<'a> {
631631
self.state.selected_block = Some(origin);
632632
}
633633
self.state.world = self.compute_bounding_box();
634-
self.center_on_block(self.state.selected_block.unwrap())
635-
.unwrap();
634+
if let Some(center_block) = self.state.selected_block {
635+
self.center_on_block(center_block).unwrap();
636+
}
636637
self.state.first_render = false;
637638
}
638639
}

0 commit comments

Comments
 (0)