@@ -180,6 +180,11 @@ class BasePackedGraph {
180
180
// / May **NOT** be called during iteration along a path, if it would destroy that path.
181
181
void destroy_handle (const handle_t & handle);
182
182
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
+
183
188
// / Shorten a node by truncating either the left or right side of the node, relative to the orientation
184
189
// / of the handle, starting from a given offset along the nodes sequence. Any edges on the truncated
185
190
// / 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) {
1752
1757
defragment (get_node_count () == 0 );
1753
1758
}
1754
1759
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
+
1755
1791
template <typename Backend>
1756
1792
handle_t BasePackedGraph<Backend>::truncate_handle(const handle_t & handle, bool trunc_left, size_t offset) {
1757
1793
// TODO: This duplicates the libhandlegraph implementation
0 commit comments