Skip to content

Commit

Permalink
add labels at the beginning of the path ranges. buggy
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaGuarracino committed Feb 7, 2024
1 parent 519cc18 commit 8c83537
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
15 changes: 14 additions & 1 deletion src/algorithms/draw.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "draw.hpp"
#include "split.hpp"

namespace odgi {

Expand Down Expand Up @@ -106,7 +107,8 @@ void draw_svg(std::ostream &out,
const double& scale,
const double& border,
const double& line_width,
std::vector<algorithms::color_t>& node_id_to_color) {
std::vector<algorithms::color_t>& node_id_to_color,
ska::flat_hash_map<handlegraph::nid_t, std::string>& node_id_to_label_map) {

std::vector<std::vector<handle_t>> weak_components;
coord_range_2d_t rendered_range;
Expand Down Expand Up @@ -160,6 +162,17 @@ void draw_svg(std::ostream &out,
} else {
highlights.push_back(handle);
}

// Check if number_bool_packing::unpack_number(handle) is even, becasue each node is present twice
if (node_id_to_label_map.count(graph.get_id(handle)) && (number_bool_packing::unpack_number(handle) % 2) == 0){
out << "<text font-family=\"Arial\" font-size=\"20\" fill=\"#000000\" stroke=\"#000000\">";
auto vals = split(node_id_to_label_map[graph.get_id(handle)], '\n');
for (auto x : vals){
out << "<tspan x=\"" << (X[a] * scale) - x_off << "\" dy=\"1.0em\">" << x << "</tspan>";
}
out << "</text>"
<< std::endl;
}
}

// color highlights
Expand Down
3 changes: 2 additions & 1 deletion src/algorithms/draw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ void draw_svg(std::ostream &out,
const double& scale,
const double& border,
const double& line_width,
std::vector<algorithms::color_t>& node_id_to_color);
std::vector<algorithms::color_t>& node_id_to_color,
ska::flat_hash_map<handlegraph::nid_t, std::string>& node_id_to_label_map);

std::vector<uint8_t> rasterize(const std::vector<double> &X,
const std::vector<double> &Y,
Expand Down
24 changes: 19 additions & 5 deletions src/subcommand/draw_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ int main_draw(int argc, char **argv) {
// handle targets from BED
std::vector<odgi::path_range_t> path_ranges;
std::vector<algorithms::color_t> node_id_to_color;
ska::flat_hash_map<handlegraph::nid_t, std::string> node_id_to_label_map; // To remember the unique node to label for each path range
if (_path_bed_file && !args::get(_path_bed_file).empty()) {
std::ifstream bed_in(args::get(_path_bed_file));
std::string line;
Expand All @@ -137,6 +138,8 @@ int main_draw(int argc, char **argv) {
if (!path_range.name.empty()) {
auto vals = split(path_range.name, '#');
if (vals.size() == 2 && vals[1].length() == 6) {
path_range.name = vals[0]; // Remove the color from the name

// Colors are given in RRGGBB in the BED file, but they are taken in BBGGRR, so we need to switch BB/RR

char temp = vals[1][0];
Expand All @@ -155,11 +158,15 @@ int main_draw(int argc, char **argv) {
} else {
path_color = algorithms::hash_color(path_range.name);
}
}




const step_handle_t first_step = graph.path_begin(path_range.begin.path);
const handle_t first_handle = graph.get_handle_of_step(first_step);
if (node_id_to_label_map.find(graph.get_id(first_handle)) == node_id_to_label_map.end()) {
node_id_to_label_map[graph.get_id(first_handle)] = path_range.name;
} else{
node_id_to_label_map[graph.get_id(first_handle)] = node_id_to_label_map[graph.get_id(first_handle)] + "\n" + path_range.name;
}
}

algorithms::for_handle_in_path_range(
graph, path_handle, path_range.begin.offset, path_range.end.offset,
Expand All @@ -170,6 +177,13 @@ int main_draw(int argc, char **argv) {
}
}


std::cerr << node_id_to_label_map.size() << " labels found" << std::endl;
for (auto x : node_id_to_label_map) {
std::cerr << "Node " << x.first << " has label " << x.second << std::endl;
}


const uint64_t _png_height = png_height ? args::get(png_height) : 1000;
const double _png_line_width = png_line_width ? args::get(png_line_width) : 10.0;
const bool _color_paths = args::get(color_paths);
Expand Down Expand Up @@ -216,7 +230,7 @@ int main_draw(int argc, char **argv) {
// 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);
algorithms::draw_svg(f, X, Y, graph, svg_scale, border_bp, _png_line_width, node_id_to_color, node_id_to_label_map);
f.close();
}

Expand Down

0 comments on commit 8c83537

Please sign in to comment.