Skip to content

Commit 1895f49

Browse files
Merge pull request #591 from pangenome/read_and_check_gfa
check the GFA while reading it
2 parents 2151dd8 + de43bb0 commit 1895f49

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

src/gfa_to_handle.cpp

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@ void gfa_to_handle(const string& gfa_filename,
4545
gg.for_each_sequence_line_in_file(
4646
filename,
4747
[&](gfak::sequence_elem s) {
48-
uint64_t id = stol(s.name);
49-
min_id = std::min(min_id, id);
50-
max_id = std::max(max_id, id);
48+
try {
49+
uint64_t id = stol(s.name);
50+
min_id = std::min(min_id, id);
51+
max_id = std::max(max_id, id);
52+
} catch (const std::exception& e) {
53+
std::cerr << "[odgi::gfa_to_handle] Error parsing segment '" << s.name << "': " << e.what() << std::endl;
54+
exit(1);
55+
}
5156
});
5257
});
5358
line_counts = gfa_line_counts(filename);
@@ -67,8 +72,13 @@ void gfa_to_handle(const string& gfa_filename,
6772
gg.for_each_sequence_line_in_file(
6873
filename,
6974
[&](const gfak::sequence_elem& s) {
70-
uint64_t id = stol(s.name);
71-
graph->create_handle(s.sequence, id - id_increment);
75+
try {
76+
uint64_t id = stol(s.name);
77+
graph->create_handle(s.sequence, id - id_increment);
78+
} catch (const std::exception& e) {
79+
std::cerr << "[odgi::gfa_to_handle] Error creating node '" << s.name << ": " << e.what() << std::endl;
80+
exit(1);
81+
}
7282
if (progress) progress_meter->increment(1);
7383
});
7484
if (progress) {
@@ -86,9 +96,22 @@ void gfa_to_handle(const string& gfa_filename,
8696
filename,
8797
[&](const gfak::edge_elem& e) {
8898
if (e.source_name.empty()) return;
89-
handlegraph::handle_t a = graph->get_handle(stol(e.source_name) - id_increment, !e.source_orientation_forward);
90-
handlegraph::handle_t b = graph->get_handle(stol(e.sink_name) - id_increment, !e.sink_orientation_forward);
91-
graph->create_edge(a, b);
99+
try {
100+
uint64_t source_id = stol(e.source_name) - id_increment;
101+
uint64_t sink_id = stol(e.sink_name) - id_increment;
102+
if (graph->has_node(source_id) && graph->has_node(sink_id)) {
103+
handlegraph::handle_t a = graph->get_handle(stol(e.source_name) - id_increment, !e.source_orientation_forward);
104+
handlegraph::handle_t b = graph->get_handle(stol(e.sink_name) - id_increment, !e.sink_orientation_forward);
105+
graph->create_edge(a, b);
106+
} else {
107+
std::cerr << "[odgi::gfa_to_handle] Error creating edge '" << e.source_name << " <--> " << e.sink_name << "' due to missing node(s)" << std::endl;
108+
exit(1);
109+
}
110+
} catch (const std::exception& exc) {
111+
std::cerr << "[odgi::gfa_to_handle] Error creating edge '" << e.source_name << " <--> " << e.sink_name << "': " << exc.what() << std::endl;
112+
exit(1);
113+
}
114+
92115
if (progress) progress_meter->increment(1);
93116
});
94117
if (progress) {
@@ -116,17 +139,21 @@ void gfa_to_handle(const string& gfa_filename,
116139
uint64_t id = 0;
117140
try {
118141
id = std::stoull(s) - id_increment;
142+
if (graph->has_node(id)) {
143+
graph->append_step(p->path,
144+
graph->get_handle(id,
145+
// in gfak, true == +
146+
!p->gfak.orientations[i++]));
147+
} else {
148+
std::cerr << "[odgi::gfa_to_handle] Error creating path '" << graph->get_path_name(p->path) << "' due to missing node '" << s << "'" << std::endl;
149+
exit(1);
150+
}
119151
} catch (...) {
120-
std::cerr << std::endl // pad
121-
<< "[odgi::gfa_to_handle] id parsing failure for path "
152+
std::cerr << "[odgi::gfa_to_handle] id parsing failure for path "
122153
<< graph->get_path_name(p->path)
123154
<< " attempting to parse node id from '" << s << "'" << std::endl;
124-
throw;
155+
exit(1);
125156
}
126-
graph->append_step(p->path,
127-
graph->get_handle(id,
128-
// in gfak, true == +
129-
!p->gfak.orientations[i++]));
130157
}
131158
delete p;
132159
if (progress) progress_meter->increment(1);

0 commit comments

Comments
 (0)