Skip to content

Commit

Permalink
better classes
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaGuarracino committed Mar 14, 2024
1 parent 25142e4 commit e52f88f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/subcommand/paths_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,14 @@ int main_paths(int argc, char** argv) {
if (last_class != -1 && (last_class != current_class)) {
// Emit the previous range, if any
if (end > start && (end - start) >= min_size_in_bp) {
const std::string symbol = (last_class == 0) ? "< " : ">= ";
std::string seq_class;
if (last_class == 0){
seq_class = "<" + utils::to_string_custom(sorted_levels[last_class]);
} else if (last_class == set_of_handles_for_level.size() - 1){
seq_class = ">=" + utils::to_string_custom(sorted_levels[last_class]);
} else {
seq_class = utils::to_string_custom(sorted_levels[last_class]) + "<=x<" + utils::to_string_custom(sorted_levels[last_class + 1]);
}
if (_show_step_ranges) {
std::string step_range_str = "";
for (auto& step : step_range) {
Expand All @@ -688,10 +695,10 @@ int main_paths(int argc, char** argv) {
}

#pragma omp critical (cout)
std::cout << graph.get_path_name(path) << "\t" << start << "\t" << end << "\t" << symbol << sorted_levels[last_class] << "\t" << step_range_str.substr(0, step_range_str.size() - 1) << std::endl; // trim the trailing comma from step_range
std::cout << graph.get_path_name(path) << "\t" << start << "\t" << end << "\t" << seq_class << "\t" << step_range_str.substr(0, step_range_str.size() - 1) << std::endl; // trim the trailing comma from step_range
} else {
#pragma omp critical (cout)
std::cout << graph.get_path_name(path) << "\t" << start << "\t" << end << "\t" << symbol << sorted_levels[last_class] << std::endl;
std::cout << graph.get_path_name(path) << "\t" << start << "\t" << end << "\t" << seq_class << std::endl;
}
}
start = end;
Expand All @@ -710,7 +717,14 @@ int main_paths(int argc, char** argv) {

// Emit last range, if any
if (end > start && (end - start) >= min_size_in_bp) {
const std::string symbol = (last_class == 0) ? "< " : ">= ";
std::string seq_class;
if (last_class == 0){
seq_class = "<" + utils::to_string_custom(sorted_levels[last_class]);
} else if (last_class == set_of_handles_for_level.size() - 1){
seq_class = ">=" + utils::to_string_custom(sorted_levels[last_class]);
} else {
seq_class = utils::to_string_custom(sorted_levels[last_class]) + "<=x<" + utils::to_string_custom(sorted_levels[last_class + 1]);
}
if (_show_step_ranges) {
std::string step_range_str = "";
for (auto& step : step_range) {
Expand All @@ -719,10 +733,10 @@ int main_paths(int argc, char** argv) {
}

#pragma omp critical (cout)
std::cout << graph.get_path_name(path) << "\t" << start << "\t" << end << "\t" << symbol << sorted_levels[last_class] << "\t" << step_range_str.substr(0, step_range_str.size() - 1) << std::endl; // trim the trailing comma from step_range
std::cout << graph.get_path_name(path) << "\t" << start << "\t" << end << "\t" << seq_class << "\t" << step_range_str.substr(0, step_range_str.size() - 1) << std::endl; // trim the trailing comma from step_range
} else {
#pragma omp critical (cout)
std::cout << graph.get_path_name(path) << "\t" << start << "\t" << end << "\t" << symbol << sorted_levels[last_class] << std::endl;
std::cout << graph.get_path_name(path) << "\t" << start << "\t" << end << "\t" << seq_class << std::endl;
}
}
}
Expand All @@ -733,8 +747,15 @@ int main_paths(int argc, char** argv) {
const uint64_t hl = graph.get_length(h);

if (hl >= min_size_in_bp) {
const std::string symbol = (i == 0) ? "< " : ">= ";
std::cout << graph.get_id(h) << "\t" << hl << "\t" << symbol << sorted_levels[i] << std::endl;
std::string seq_class;
if (i == 0){
seq_class = "<" + utils::to_string_custom(sorted_levels[i]);
} else if (i == set_of_handles_for_level.size() - 1){
seq_class = ">=" + utils::to_string_custom(sorted_levels[i]);
} else {
seq_class = utils::to_string_custom(sorted_levels[i]) + "<=x<" + utils::to_string_custom(sorted_levels[i + 1]);
}
std::cout << graph.get_id(h) << "\t" << hl << "\t" << seq_class << std::endl;
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,29 @@ namespace utils {
return !s.empty() && std::all_of(s.begin(), s.end(), ::isdigit);
}

std::string to_string_custom(double value) {
std::ostringstream out;

// Start with fixed-point notation.
out << std::fixed << std::setprecision(5) << value;

std::string str = out.str();

// Check if there is a decimal point in the string.
size_t decimal_pos = str.find('.');
if (decimal_pos != std::string::npos) {
// Remove trailing zeros.
str.erase(str.find_last_not_of('0') + 1, std::string::npos);

// If the decimal point is now the last character, remove it too.
if (str.back() == '.') {
str.erase(str.length() - 1);
}
}

return str;
}

void graph_deep_copy(const odgi::graph_t& source,
odgi::graph_t* target) {
// copy the now-compacted graph to our output_graph
Expand Down
1 change: 1 addition & 0 deletions src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ using namespace handlegraph;

namespace utils {
bool is_number(const std::string &s);
std::string to_string_custom(double value);
void graph_deep_copy(const odgi::graph_t &source,
odgi::graph_t* target);
bool ends_with(const std::string &fullString, const std::string &ending);
Expand Down

0 comments on commit e52f88f

Please sign in to comment.