Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

odgi similarity masked #600

Merged
merged 3 commits into from
Jan 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/subcommand/similarity_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ int main_similarity(int argc, char** argv) {
args::ArgumentParser parser("Provides a sparse similarity matrix for paths or groups of paths. Each line prints in a tab-delimited format to stdout.");
args::Group mandatory_opts(parser, "[ MANDATORY ARGUMENTS ]");
args::ValueFlag<std::string> dg_in_file(mandatory_opts, "FILE", "Load the succinct variation graph in ODGI format from this *FILE*. The file name usually ends with *.og*. It also accepts GFAv1, but the on-the-fly conversion to the ODGI format requires additional time!", {'i', "idx"});
args::Group node_mask_opts(parser, "[ Node Masking Options ]");
args::ValueFlag<std::string> mask_file(node_mask_opts, "FILE", "Load node mask from this file. Each line contains a 0 or 1, where 0 means the node at that position should be ignored in similarity computation.", {'m', "mask"});
args::Group path_investigation_opts(parser, "[ Path Investigation Options ]");
args::ValueFlag<std::string> path_delim(path_investigation_opts, "CHAR", "The part of each path name before this delimiter CHAR is a group identifier.",
{'D', "delim"});
Expand Down Expand Up @@ -198,9 +200,42 @@ args::Group threading_opts(parser, "[ Threading ]");
}

// ska::flat_hash_map<std::pair<uint64_t, uint64_t>, uint64_t> leads to huge memory usage with deep graphs
// Load mask if specified
std::vector<bool> node_mask(graph.get_node_count(), true); // Default all nodes included
if (mask_file) {
std::ifstream mask_in(args::get(mask_file));
std::string line;
uint64_t line_count = 0;
while (std::getline(mask_in, line)) {
if (line_count >= graph.get_node_count()) {
std::cerr << "[odgi::similarity] error: mask file has more lines than graph nodes ("
<< graph.get_node_count() << ")" << std::endl;
return 1;
}
if (line == "0") {
node_mask[line_count] = false;
} else if (line == "1") {
node_mask[line_count] = true;
} else {
std::cerr << "[odgi::similarity] error: mask file should contain only 0 or 1 values, found: " << line << std::endl;
return 1;
}
line_count++;
}
if (line_count != graph.get_node_count()) {
std::cerr << "[odgi::similarity] error: mask file should have exactly " << graph.get_node_count()
<< " lines, found: " << line_count << std::endl;
return 1;
}
}

ska::flat_hash_map<uint64_t, uint64_t> path_intersection_length;
graph.for_each_handle(
[&](const handle_t& h) {
// Skip masked-out nodes
if (!node_mask[graph.get_id(h) - 1]) {
return;
}
ska::flat_hash_map<uint32_t, uint64_t> local_path_lengths;
size_t l = graph.get_length(h);
graph.for_each_step_on_handle(
Expand Down