From dd2fadacdc26cc363b7f1e65203b6fc748d9db86 Mon Sep 17 00:00:00 2001
From: Jouni Siren <jouni.siren@iki.fi>
Date: Tue, 17 Oct 2023 14:56:27 -0700
Subject: [PATCH] Better error messages from vg haplotypes

---
 deps/kff-cpp-api                   |  2 +-
 src/kff.hpp                        |  2 +-
 src/subcommand/haplotypes_main.cpp | 31 +++++++++++++++++++++++-------
 3 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/deps/kff-cpp-api b/deps/kff-cpp-api
index d12fe041838..c490ddb6030 160000
--- a/deps/kff-cpp-api
+++ b/deps/kff-cpp-api
@@ -1 +1 @@
-Subproject commit d12fe0418387933bcc928805e18ea00125577525
+Subproject commit c490ddb603074b0c4c3efecc3a3c8506b059ffaf
diff --git a/src/kff.hpp b/src/kff.hpp
index 1b5e03fa5fc..d182f28d64e 100644
--- a/src/kff.hpp
+++ b/src/kff.hpp
@@ -84,7 +84,7 @@ class ParallelKFFReader {
     typedef gbwtgraph::Key64::value_type kmer_type;
 
     /// Creates a new reader for the given file. Throws `std::runtime_error` if the
-    /// sanity checks fail.
+    /// file cannot be opened or the sanity checks fail.
     ParallelKFFReader(const std::string& filename);
 
     /// Reads the next `n` kmers and counts from the file. This can be called safely
diff --git a/src/subcommand/haplotypes_main.cpp b/src/subcommand/haplotypes_main.cpp
index 451a964b8b5..da559775818 100644
--- a/src/subcommand/haplotypes_main.cpp
+++ b/src/subcommand/haplotypes_main.cpp
@@ -136,7 +136,12 @@ int main_haplotypes(int argc, char** argv) {
         if (config.verbosity >= Haplotypes::verbosity_basic) {
             std::cerr << "Loading haplotype information from " << config.haplotype_input << std::endl;
         }
-        sdsl::simple_sds::load_from(haplotypes, config.haplotype_input);
+        try {
+            sdsl::simple_sds::load_from(haplotypes, config.haplotype_input);
+        } catch (const std::runtime_error& e) {
+            std::cerr << "error: [vg haplotypes] " << e.what() << std::endl;
+            std::exit(EXIT_FAILURE);
+        }
     }
 
     // Save haplotype information if necessary.
@@ -518,7 +523,7 @@ void preprocess_graph(const gbwtgraph::GBZ& gbz, Haplotypes& haplotypes, Haploty
         haplotypes = partitioner.partition_haplotypes(config.partitioner_parameters);
     }
     catch (const std::runtime_error& e) {
-        std::cerr << e.what() << std::endl;
+        std::cerr << "error: [vg haplotypes] " << e.what() << std::endl;
         std::exit(EXIT_FAILURE);
     }
     if (config.verbosity >= Haplotypes::verbosity_basic) {
@@ -544,7 +549,13 @@ void validate_subgraph(const gbwtgraph::GBWTGraph& graph, const gbwtgraph::GBWTG
 void sample_haplotypes(const gbwtgraph::GBZ& gbz, const Haplotypes& haplotypes, const HaplotypesConfig& config) {
     omp_set_num_threads(threads_to_jobs(config.threads));
     Recombinator recombinator(gbz, config.verbosity);
-    gbwt::GBWT merged = recombinator.generate_haplotypes(haplotypes, config.kmer_input, config.recombinator_parameters);
+    gbwt::GBWT merged;
+    try {
+        merged = recombinator.generate_haplotypes(haplotypes, config.kmer_input, config.recombinator_parameters);
+    } catch (const std::runtime_error& e) {
+        std::cerr << "error: [vg haplotypes] " << e.what() << std::endl;
+        std::exit(EXIT_FAILURE);
+    }
     omp_set_num_threads(config.threads); // Restore the number of threads.
 
     // Build and serialize GBWTGraph.
@@ -860,10 +871,16 @@ void extract_haplotypes(const gbwtgraph::GBZ& gbz, const Haplotypes& haplotypes,
     }
 
     Recombinator recombinator(gbz, config.verbosity);
-    auto result = recombinator.extract_sequences(
-        haplotypes, config.kmer_input,
-        config.chain_id, config.subchain_id, config.recombinator_parameters
-    );
+    std::vector<Recombinator::LocalHaplotype> result;
+    try {
+        result = recombinator.extract_sequences(
+            haplotypes, config.kmer_input,
+            config.chain_id, config.subchain_id, config.recombinator_parameters
+        );
+    } catch (const std::runtime_error& e) {
+        std::cerr << "error: [vg haplotypes] " << e.what() << std::endl;
+        std::exit(EXIT_FAILURE);
+    }
     if (config.verbosity >= Haplotypes::verbosity_detailed) {
         std::cerr << "Found " << result.size() << " haplotypes" << std::endl;
     }