Skip to content

Commit dd55ec7

Browse files
committed
dont crash with multiple references
1 parent 5fa4fbc commit dd55ec7

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/rgfa.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,9 +1099,9 @@ void RGFACover::print_stats(ostream& os) {
10991099
vector<pair<int64_t, nid_t>> ref_nodes = this->get_reference_nodes(first_node, false);
11001100
// interval is open ended, so we go back to last node
11011101
step_handle_t last_step;
1102-
if (interval.second == graph->path_end(graph->get_path_handle_of_step(interval.second))) {
1102+
if (interval.second == graph->path_end(graph->get_path_handle_of_step(interval.first))) {
11031103
// can't get previous step of end in gbz, so we treat as special case here
1104-
last_step = graph->path_back(graph->get_path_handle_of_step(interval.second));
1104+
last_step = graph->path_back(graph->get_path_handle_of_step(interval.first));
11051105
} else {
11061106
last_step = graph->get_previous_step(interval.second);
11071107
}
@@ -1110,23 +1110,32 @@ void RGFACover::print_stats(ostream& os) {
11101110
int64_t min_ref_pos = numeric_limits<int64_t>::max();
11111111
int64_t max_ref_pos = -1;
11121112
int64_t min_rank = numeric_limits<int64_t>::max();
1113-
string r_chrom;
1113+
string r_chrom;
1114+
map<string, tuple<int64_t, int64_t, int64_t>> chrom_pos;
11141115
for (const pair<int64_t, nid_t>& rank_node : ref_nodes) {
11151116
step_handle_t ref_step = this->rgfa_intervals.at(node_to_interval.at(rank_node.second)).first;
11161117
path_handle_t ref_path = graph->get_path_handle_of_step(ref_step);
11171118
string name = graph->get_path_name(ref_path);
11181119
// we assume one reference contig (which is built into the whole structure)
11191120
assert(r_chrom.empty() || r_chrom == name);
1120-
r_chrom = name;
11211121
int64_t ref_pos = node_to_ref_pos.at(rank_node.second);
1122-
// assume snarl is forward on both reference nodes
1123-
// todo: this won't be exact for some inversion cases, I don't think --
1124-
// need to test these and either add check / or move to oriented search
1125-
min_ref_pos = min(min_ref_pos, ref_pos + (int64_t)graph->get_length(graph->get_handle(rank_node.second)));
1126-
max_ref_pos = max(max_ref_pos, ref_pos);
1127-
min_rank = min(min_rank, rank_node.first);
1128-
}
1129-
1122+
int64_t last_len = (int64_t)graph->get_length(graph->get_handle(rank_node.second));
1123+
if (chrom_pos.count(name)) {
1124+
tuple<int64_t, int64_t, int64_t>& pos = chrom_pos[name];
1125+
// assume snarl is forward on both reference nodes
1126+
// todo: this won't be exact for some inversion cases, I don't think --
1127+
// need to test these and either add check / or move to oriented search
1128+
pos = make_tuple(min(get<0>(pos), ref_pos + last_len), max(get<1>(pos), ref_pos), min(get<2>(pos), rank_node.first));
1129+
} else {
1130+
chrom_pos[name] = make_tuple(ref_pos + last_len, ref_pos, rank_node.first);
1131+
}
1132+
if (rank_node.first < min_rank) {
1133+
// todo: is there a better heuristic to use when choosing a reference?
1134+
// also: need to fix vcf annotate maybe to just list them all...
1135+
min_rank = rank_node.first;
1136+
r_chrom = name;
1137+
}
1138+
}
11301139

11311140
os << sample_locus.first << "\t"
11321141
<< sample_locus.second << "\t"
@@ -1141,8 +1150,8 @@ void RGFACover::print_stats(ostream& os) {
11411150
<< std::fixed << std::setprecision(2) << (tot_depth / tot_steps) << "\t"
11421151
<< std::fixed << std::setprecision(2) << (tot_sample_depth / tot_steps) << "\t"
11431152
<< Paths::strip_subrange(r_chrom) << "\t"
1144-
<< min_ref_pos << "\t"
1145-
<< max_ref_pos << "\n";
1153+
<< get<0>(chrom_pos[r_chrom]) << "\t"
1154+
<< get<1>(chrom_pos[r_chrom]) << "\n";
11461155
}
11471156
}
11481157

0 commit comments

Comments
 (0)