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

Implement hictk metadata command #204

Merged
merged 13 commits into from
Aug 16, 2024
Next Next commit
Initial implementation of hictk metadata command
  • Loading branch information
robomics committed Aug 15, 2024
commit 9c353154fb3744d482b5a998e1903066a0d5fbe9
1 change: 1 addition & 0 deletions docs/generate_cli_reference.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ subcommands=(
fix-mcool
load
merge
metadata
rename-chromosomes
validate
zoomify
Expand Down
2 changes: 2 additions & 0 deletions src/hictk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ target_sources(
${CMAKE_CURRENT_SOURCE_DIR}/cli/cli_fix_mcool.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cli/cli_load.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cli/cli_merge.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cli/cli_metadata.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cli/cli_rename_chromosomes.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cli/cli_validate.cpp
${CMAKE_CURRENT_SOURCE_DIR}/cli/cli_zoomify.cpp
Expand All @@ -33,6 +34,7 @@ target_sources(
${CMAKE_CURRENT_SOURCE_DIR}/fix_mcool/fix_mcool.cpp
${CMAKE_CURRENT_SOURCE_DIR}/load/load.cpp
${CMAKE_CURRENT_SOURCE_DIR}/merge/merge.cpp
${CMAKE_CURRENT_SOURCE_DIR}/metadata/metadata.cpp
${CMAKE_CURRENT_SOURCE_DIR}/rename_chromosomes/rename_chromosomes.cpp
${CMAKE_CURRENT_SOURCE_DIR}/validate/validate.cpp
${CMAKE_CURRENT_SOURCE_DIR}/zoomify/zoomify.cpp)
Expand Down
10 changes: 10 additions & 0 deletions src/hictk/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
_subcommand = subcommand::load;
} else if (_cli.get_subcommand("merge")->parsed()) {
_subcommand = subcommand::merge;
} else if (_cli.get_subcommand("metadata")->parsed()) {
_subcommand = subcommand::metadata;

Check warning on line 46 in src/hictk/cli/cli.cpp

View check run for this annotation

Codecov / codecov/patch

src/hictk/cli/cli.cpp#L46

Added line #L46 was not covered by tests
} else if (_cli.get_subcommand("rename-chromosomes")->parsed()) {
_subcommand = subcommand::rename_chromosomes;
} else if (_cli.get_subcommand("validate")->parsed()) {
Expand Down Expand Up @@ -92,6 +94,8 @@
return "load";
case merge:
return "merge";
case metadata:
return "metadata";

Check warning on line 98 in src/hictk/cli/cli.cpp

View check run for this annotation

Codecov / codecov/patch

src/hictk/cli/cli.cpp#L97-L98

Added lines #L97 - L98 were not covered by tests
case rename_chromosomes:
return "rename-chromosomes";
case validate:
Expand All @@ -116,6 +120,7 @@
make_fix_mcool_subcommand();
make_load_subcommand();
make_merge_subcommand();
make_metadata_subcommand();
make_rename_chromosomes_subcommand();
make_validate_subcommand();
make_zoomify_subcommand();
Expand All @@ -141,6 +146,8 @@
case merge:
validate_merge_subcommand();
break;
case metadata:
break;

Check warning on line 150 in src/hictk/cli/cli.cpp

View check run for this annotation

Codecov / codecov/patch

src/hictk/cli/cli.cpp#L149-L150

Added lines #L149 - L150 were not covered by tests
case rename_chromosomes:
validate_rename_chromosomes_subcommand();
break;
Expand Down Expand Up @@ -174,6 +181,9 @@
case merge:
transform_args_merge_subcommand();
break;
case metadata:
transform_args_metadata_subcommand();
break;

Check warning on line 186 in src/hictk/cli/cli.cpp

View check run for this annotation

Codecov / codecov/patch

src/hictk/cli/cli.cpp#L184-L186

Added lines #L184 - L186 were not covered by tests
case rename_chromosomes:
transform_args_rename_chromosomes_subcommand();
break;
Expand Down
66 changes: 66 additions & 0 deletions src/hictk/cli/cli_metadata.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (C) 2024 Roberto Rossini <roberros@uio.no>
//
// SPDX-License-Identifier: MIT

#include <fmt/format.h>
#include <fmt/ranges.h>
#include <spdlog/spdlog.h>

#include <CLI/CLI.hpp>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <limits>
#include <stdexcept>
#include <string>
#include <variant>
#include <vector>

#include "hictk/tools/cli.hpp"
#include "hictk/tools/config.hpp"

namespace hictk::tools {

void Cli::make_metadata_subcommand() {
auto& sc = *_cli.add_subcommand("metadata", "Print file metadata to stdout.")
->fallthrough()
->preparse_callback([this]([[maybe_unused]] std::size_t i) {
assert(_config.index() == 0);
_config = MetadataConfig{};
});

Check warning on line 31 in src/hictk/cli/cli_metadata.cpp

View check run for this annotation

Codecov / codecov/patch

src/hictk/cli/cli_metadata.cpp#L29-L31

Added lines #L29 - L31 were not covered by tests

_config = MetadataConfig{};
auto& c = std::get<MetadataConfig>(_config);

// clang-format off
sc.add_option(
"uri",
c.uri,
"Path to a .hic or .[ms]cool file (Cooler URI syntax supported).")
->check(IsValidCoolerFile | IsValidMultiresCoolerFile | IsValidSingleCellCoolerFile | IsValidHiCFile)
->required();

sc.add_option(
"-f,--output-format",
c.output_format,
"Format used to return file metadata.\n"
"Should be one of: json, toml, tsv, or yaml.")
->check(CLI::IsMember({"json", "tsv", "toml", "yaml"}))
->capture_default_str();
// clang-format on

_config = std::monostate{};
}

void Cli::transform_args_metadata_subcommand() {
auto& c = std::get<MetadataConfig>(_config);

Check warning on line 57 in src/hictk/cli/cli_metadata.cpp

View check run for this annotation

Codecov / codecov/patch

src/hictk/cli/cli_metadata.cpp#L56-L57

Added lines #L56 - L57 were not covered by tests

c.input_format = infer_input_format(c.uri);

Check warning on line 59 in src/hictk/cli/cli_metadata.cpp

View check run for this annotation

Codecov / codecov/patch

src/hictk/cli/cli_metadata.cpp#L59

Added line #L59 was not covered by tests

// in spdlog, high numbers correspond to low log levels
assert(c.verbosity > 0 && c.verbosity < 5);
c.verbosity = static_cast<std::uint8_t>(spdlog::level::critical) - c.verbosity;

Check warning on line 63 in src/hictk/cli/cli_metadata.cpp

View check run for this annotation

Codecov / codecov/patch

src/hictk/cli/cli_metadata.cpp#L62-L63

Added lines #L62 - L63 were not covered by tests
}

} // namespace hictk::tools
3 changes: 3 additions & 0 deletions src/hictk/include/hictk/tools/cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ class Cli {
fix_mcool,
load,
merge,
metadata,
rename_chromosomes,
validate,
zoomify,
Expand Down Expand Up @@ -235,6 +236,7 @@ class Cli {
void make_fix_mcool_subcommand();
void make_load_subcommand();
void make_merge_subcommand();
void make_metadata_subcommand();
void make_rename_chromosomes_subcommand();
void make_validate_subcommand();
void make_zoomify_subcommand();
Expand All @@ -259,6 +261,7 @@ class Cli {
void transform_args_fix_mcool_subcommand();
void transform_args_load_subcommand();
void transform_args_merge_subcommand();
void transform_args_metadata_subcommand();
void transform_args_rename_chromosomes_subcommand();
void transform_args_zoomify_subcommand();
void transform_args();
Expand Down
9 changes: 9 additions & 0 deletions src/hictk/include/hictk/tools/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ struct MergeConfig {
std::uint8_t verbosity{4};
};

struct MetadataConfig {
std::filesystem::path uri{};
std::string input_format{};
std::string output_format{"json"};

std::uint8_t verbosity{2};
};

struct RenameChromosomesConfig {
std::string uri{};
std::filesystem::path path_to_name_mappings{};
Expand Down Expand Up @@ -227,6 +235,7 @@ using Config = std::variant<std::monostate,
FixMcoolConfig,
LoadConfig,
MergeConfig,
MetadataConfig,
RenameChromosomesConfig,
ValidateConfig,
ZoomifyConfig>;
Expand Down
1 change: 1 addition & 0 deletions src/hictk/include/hictk/tools/tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace hictk::tools {
[[nodiscard]] int fix_mcool_subcmd(const FixMcoolConfig& c);
[[nodiscard]] int load_subcmd(const LoadConfig& c);
[[nodiscard]] int merge_subcmd(const MergeConfig& c);
[[nodiscard]] int metadata_subcmd(const MetadataConfig& c);
[[nodiscard]] int rename_chromosomes_subcmd(const RenameChromosomesConfig& c);
[[nodiscard]] int validate_subcmd(const ValidateConfig& c);
[[nodiscard]] int zoomify_subcmd(const ZoomifyConfig& c);
Expand Down
2 changes: 2 additions & 0 deletions src/hictk/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@
return load_subcmd(std::get<LoadConfig>(config));
case sc::merge:
return merge_subcmd(std::get<MergeConfig>(config));
case sc::metadata:
return metadata_subcmd(std::get<MetadataConfig>(config));

Check warning on line 116 in src/hictk/main.cpp

View check run for this annotation

Codecov / codecov/patch

src/hictk/main.cpp#L115-L116

Added lines #L115 - L116 were not covered by tests
case sc::rename_chromosomes:
return rename_chromosomes_subcmd(std::get<RenameChromosomesConfig>(config));
case sc::validate:
Expand Down
Loading
Loading