Skip to content

Commit

Permalink
add a node sparsification factor for SVG output
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaGuarracino committed Feb 10, 2024
1 parent 7bbd41d commit 2aba37b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
25 changes: 24 additions & 1 deletion src/algorithms/draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ bool is_too_close(double x, double y, const std::string& content, double thresho
}
return false;
}

uint64_t node_hash(const nid_t& node_id) {
uint64_t x = node_id;
x = (~x) + (x << 21); // x = (x << 21) - x - 1;
x = x ^ (x >> 24);
x = (x + (x << 3)) + (x << 8); // x * 265
x = x ^ (x >> 14);
x = (x + (x << 2)) + (x << 4); // x * 21
x = x ^ (x >> 28);
x = x + (x << 31);
return x;
}
bool keep_node(const nid_t& node_id, const float f) {
// hash the node_id and check if it's accepted given our sparsification factor
return node_hash(node_id) < std::numeric_limits<uint64_t>::max() * f;
}

void draw_svg(std::ostream &out,
const std::vector<double> &X,
const std::vector<double> &Y,
Expand All @@ -121,7 +138,8 @@ void draw_svg(std::ostream &out,
const double& border,
const double& line_width,
std::vector<algorithms::color_t>& node_id_to_color,
ska::flat_hash_map<handlegraph::nid_t, std::set<std::string>>& node_id_to_label_map) {
ska::flat_hash_map<handlegraph::nid_t, std::set<std::string>>& node_id_to_label_map,
const float& sparsification_factor) {

std::vector<std::vector<handle_t>> weak_components;
coord_range_2d_t rendered_range;
Expand Down Expand Up @@ -161,6 +179,11 @@ void draw_svg(std::ostream &out,
for (auto& handle : component) {
uint64_t a = 2 * number_bool_packing::unpack_number(handle);
algorithms::color_t color = node_id_to_color.empty() ? COLOR_BLACK : node_id_to_color[graph.get_id(handle)];

if (!(sparsification_factor == 0 || keep_node(graph.get_id(handle), sparsification_factor) || node_id_to_label_map.count(graph.get_id(handle)))) {
continue; // Skip this node to output a lighter SVG (do not nodes with labels, if any)
}

if (color == COLOR_BLACK || color == COLOR_LIGHTGRAY) {
out << "<line x1=\""
<< (X[a] * scale) - x_off
Expand Down
3 changes: 2 additions & 1 deletion src/algorithms/draw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ void draw_svg(std::ostream &out,
const double& border,
const double& line_width,
std::vector<algorithms::color_t>& node_id_to_color,
ska::flat_hash_map<handlegraph::nid_t, std::set<std::string>>& node_id_to_label_map);
ska::flat_hash_map<handlegraph::nid_t, std::set<std::string>>& node_id_to_label_map,
const float& sparsification_factor);

std::vector<uint8_t> rasterize(const std::vector<double> &X,
const std::vector<double> &Y,
Expand Down
6 changes: 4 additions & 2 deletions src/subcommand/draw_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ int main_draw(int argc, char **argv) {
"Colors are derived from the 4th column, if present, else from the path name."
"If the 4th column value is in the format 'string#RRGGBB', the RRGGBB color (in hex notation) will be used.",
{'b', "bed-file"});
args::ValueFlag<float> node_sparsification(parser, "N", "Remove this fraction of nodes from the SVG output (to output smaller files) (default: 0.0, keep all nodes).", {'f', "sparse-factor-svg"});
args::Group threading(parser, "[ Threading ]");
args::ValueFlag<uint64_t> nthreads(threading, "N", "Number of threads to use for parallel operations.", {'t', "threads"});
args::Group processing_info_opts(parser, "[ Processing Information ]");
Expand Down Expand Up @@ -182,7 +183,6 @@ int main_draw(int argc, char **argv) {
const double _png_line_width = png_line_width ? args::get(png_line_width) : 10.0;
const bool _color_paths = args::get(color_paths);
const double _png_path_line_spacing = png_path_line_spacing ? args::get(png_path_line_spacing) : 0.0;
const double svg_scale = !svg_render_scale ? 0.01 : args::get(svg_render_scale);
size_t max_node_depth = 0;
graph.for_each_handle(
[&](const handle_t& h) {
Expand Down Expand Up @@ -219,12 +219,14 @@ int main_draw(int argc, char **argv) {
}

if (svg_out_file) {
const double svg_scale = !svg_render_scale ? 0.01 : args::get(svg_render_scale);
const float sparse_nodes = node_sparsification ? args::get(node_sparsification) : 0.0;
auto& outfile = args::get(svg_out_file);
ofstream f(outfile.c_str());
// todo could be done with callbacks
std::vector<double> X = layout.get_X();
std::vector<double> Y = layout.get_Y();
algorithms::draw_svg(f, X, Y, graph, svg_scale, border_bp, _png_line_width, node_id_to_color, node_id_to_label_map);
algorithms::draw_svg(f, X, Y, graph, svg_scale, border_bp, _png_line_width, node_id_to_color, node_id_to_label_map, sparse_nodes);
f.close();
}

Expand Down

0 comments on commit 2aba37b

Please sign in to comment.