Skip to content

Commit 076e21a

Browse files
committed
add implementations for change seq
1 parent 2116643 commit 076e21a

5 files changed

+65
-2
lines changed

bdsg/include/bdsg/hash_graph.hpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,12 @@ class HashGraph : public MutablePathDeletableHandleGraph, public SerializableHan
132132
/// May **NOT** be called during iteration over paths, if it would destroy a path.
133133
/// May **NOT** be called during iteration along a path, if it would destroy that path.
134134
void destroy_handle(const handle_t& handle);
135-
135+
136+
/// Change the sequence of handle to a new sequence. Returns a (possibly alterered)
137+
/// handle to the node with the new sequence. May invalidate the existing handle. Updates
138+
/// paths if called through an inheriting MutablePath interface.
139+
handle_t change_sequence(const handle_t& handle, const std::string& sequence);
140+
136141
/// Create an edge connecting the given handles in the given order and orientations.
137142
/// Ignores existing edges.
138143
void create_edge(const handle_t& left, const handle_t& right);

bdsg/include/bdsg/internal/base_packed_graph.hpp

+36
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ class BasePackedGraph {
180180
/// May **NOT** be called during iteration along a path, if it would destroy that path.
181181
void destroy_handle(const handle_t& handle);
182182

183+
/// Change the sequence of handle to a new sequence. Returns a (possibly alterered)
184+
/// handle to the node with the new sequence. May invalidate the existing handle. Updates
185+
/// paths if called through an inheriting MutablePath interface.
186+
handle_t change_sequence(const handle_t& handle, const std::string& sequence);
187+
183188
/// Shorten a node by truncating either the left or right side of the node, relative to the orientation
184189
/// of the handle, starting from a given offset along the nodes sequence. Any edges on the truncated
185190
/// end of the node are deleted. Returns a (possibly altered) handle to the truncated node.
@@ -1752,6 +1757,37 @@ void BasePackedGraph<Backend>::destroy_handle(const handle_t& handle) {
17521757
defragment(get_node_count() == 0);
17531758
}
17541759

1760+
template<typename Backend>
1761+
handle_t BasePackedGraph<Backend>::change_sequence(const handle_t& handle, const std::string& sequence) {
1762+
1763+
size_t g_iv_index = graph_iv_index(handle);
1764+
size_t seq_start = seq_start_iv.get(graph_index_to_seq_start_index(g_iv_index));
1765+
size_t seq_len = seq_length_iv.get(graph_index_to_seq_len_index(g_iv_index));
1766+
if (seq_len >= sequence.size()) {
1767+
// we can fit the new sequence in the same location
1768+
for (size_t i = 0; i < sequence.size(); ++i) {
1769+
seq_iv.set(seq_start + i, encode_nucleotide(sequence[i]));
1770+
}
1771+
deleted_bases += (seq_len - sequence.size());
1772+
}
1773+
else {
1774+
// the new sequence doesn't fit, add it at the end
1775+
seq_start_iv.set(graph_index_to_seq_start_index(g_iv_index), seq_iv.size());
1776+
for (size_t i = 0; i < sequence.size(); ++i) {
1777+
seq_iv.append(encode_nucleotide(sequence[i]));
1778+
}
1779+
deleted_bases += seq_len;
1780+
}
1781+
seq_length_iv.set(graph_index_to_seq_len_index(g_iv_index), sequence.size());
1782+
1783+
// FIXME: disabling since deleting bases can't currently trigger a defrag
1784+
//if (seq_len != sequence.size()) {
1785+
// defragment();
1786+
//}
1787+
1788+
return handle;
1789+
}
1790+
17551791
template<typename Backend>
17561792
handle_t BasePackedGraph<Backend>::truncate_handle(const handle_t& handle, bool trunc_left, size_t offset) {
17571793
// TODO: This duplicates the libhandlegraph implementation

bdsg/include/bdsg/internal/graph_proxy_mutable_path_deletable_handle_graph_fragment.classfragment

+8
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ public:
202202
return this->get()->truncate_handle(handle, trunc_left, offset);
203203
}
204204

205+
206+
/// Change the sequence of handle's forward orientation to a new sequence. Returns a (possibly
207+
/// handle to the node with the new sequence. May invalidate the existing handle. Updates
208+
/// alterered) paths if called through a class inheriting a MutablePathHandleGraph interface.
209+
virtual handle_t change_sequence(const handle_t& handle, const std::string& sequence) {
210+
return this->get()->change_sequence(handle, sequence);
211+
}
212+
205213
/// Remove all nodes and edges. May also remove all paths, if applicable.
206214
virtual void clear() {
207215
this->get()->clear();

bdsg/src/hash_graph.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,13 @@ namespace bdsg {
398398
// remove this node from the relevant indexes
399399
graph.erase(get_id(handle));
400400
}
401+
402+
handle_t HashGraph::change_sequence(const handle_t& handle, const std::string& sequence) {
403+
404+
graph[get_id(handle)].sequence = sequence;
405+
406+
return handle;
407+
}
401408

402409
void HashGraph::destroy_edge(const handle_t& left, const handle_t& right) {
403410

bdsg/src/test_libbdsg.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,13 @@ void test_deletable_handle_graphs() {
17811781
assert(graph.get_degree(h7, false) == 0);
17821782
assert(graph.get_degree(h6, false) == 0);
17831783
assert(graph.get_degree(h8, true) == 0);
1784+
1785+
h6 = graph.change_sequence(h6, "AAAT");
1786+
h7 = graph.change_sequence(h7, "G");
1787+
assert(graph.get_sequence(h6) == "AAAT");
1788+
assert(graph.get_sequence(graph.flip(h6)) == "ATTT");
1789+
assert(graph.get_sequence(h7) == "G");
1790+
assert(graph.get_sequence(graph.flip(h7)) == "C");
17841791
}
17851792
}
17861793

@@ -4685,8 +4692,8 @@ int main(void) {
46854692
test_paged_vector<PagedVector<5, MappedBackend>>();
46864693
test_packed_deque();
46874694
test_packed_set();
4688-
test_deletable_handle_graphs();
46894695
test_mutable_path_handle_graphs();
4696+
test_deletable_handle_graphs();
46904697
test_serializable_handle_graphs();
46914698
test_packed_graph();
46924699
test_path_position_overlays();

0 commit comments

Comments
 (0)