| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::Sandy::PieceTable; | 
| 2 |  |  |  |  |  |  | # ABSTRACT: Implement a piece table data structure class | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 6 |  |  | 6 |  | 3292 | use App::Sandy::Base 'class'; | 
|  | 6 |  |  |  |  | 17 |  | 
|  | 6 |  |  |  |  | 47 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | with 'App::Sandy::Role::BSearch'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | our $VERSION = '0.24'; # VERSION | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | has 'orig' => ( | 
| 11 |  |  |  |  |  |  | is         => 'ro', | 
| 12 |  |  |  |  |  |  | isa        => 'ScalarRef', | 
| 13 |  |  |  |  |  |  | required   => 1 | 
| 14 |  |  |  |  |  |  | ); | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | has 'len' => ( | 
| 17 |  |  |  |  |  |  | is         => 'ro', | 
| 18 |  |  |  |  |  |  | isa        => 'My:IntGt0', | 
| 19 |  |  |  |  |  |  | lazy_build => 1, | 
| 20 |  |  |  |  |  |  | builder    => '_build_len' | 
| 21 |  |  |  |  |  |  | ); | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | has 'piece_table' => ( | 
| 24 |  |  |  |  |  |  | traits     => ['Array'], | 
| 25 |  |  |  |  |  |  | is         => 'ro', | 
| 26 |  |  |  |  |  |  | isa        => 'ArrayRef[My:Piece]', | 
| 27 |  |  |  |  |  |  | lazy_build => 1, | 
| 28 |  |  |  |  |  |  | builder    => '_build_piece_table', | 
| 29 |  |  |  |  |  |  | handles    => { | 
| 30 |  |  |  |  |  |  | _get_piece     => 'get', | 
| 31 |  |  |  |  |  |  | _splice_piece  => 'splice', | 
| 32 |  |  |  |  |  |  | _count_pieces  => 'count', | 
| 33 |  |  |  |  |  |  | _all_pieces    => 'elements' | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  | ); | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | has 'logical_len' => ( | 
| 38 |  |  |  |  |  |  | is         => 'rw', | 
| 39 |  |  |  |  |  |  | isa        => 'My:IntGt0', | 
| 40 |  |  |  |  |  |  | lazy_build => 1, | 
| 41 |  |  |  |  |  |  | builder    => '_build_logical_len' | 
| 42 |  |  |  |  |  |  | ); | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | sub _build_len { | 
| 45 | 256 |  |  | 256 |  | 378 | my $self = shift; | 
| 46 | 256 |  |  |  |  | 6570 | my $orig = $self->orig; | 
| 47 | 256 |  |  |  |  | 6482 | return length $$orig; | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | sub _build_logical_len { | 
| 51 | 0 |  |  | 0 |  | 0 | my $self = shift; | 
| 52 | 0 |  |  |  |  | 0 | return $self->len; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub _build_piece_table { | 
| 56 | 256 |  |  | 256 |  | 413 | my $self = shift; | 
| 57 | 256 |  |  |  |  | 6808 | my $piece = $self->_piece_new($self->orig, 1, 0, $self->len, 0); | 
| 58 | 256 |  |  |  |  | 8839 | return [$piece]; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | sub _piece_new { | 
| 62 | 375 |  |  | 375 |  | 965 | my ($self, $ref, $is_orig, $start, $len, $pos, $annot1, $annot2) = @_; | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 375 | 50 |  |  |  | 2479 | my $piece = { | 
| 65 |  |  |  |  |  |  | 'ref'     => $ref,     # reference to sequence | 
| 66 |  |  |  |  |  |  | 'is_orig' => $is_orig, # set 1 if it is the orig sequence | 
| 67 |  |  |  |  |  |  | 'start'   => $start,   # start position at reference | 
| 68 |  |  |  |  |  |  | 'len'     => $len,     # length | 
| 69 |  |  |  |  |  |  | 'pos'     => $pos,     # position at original sequence | 
| 70 |  |  |  |  |  |  | 'offset'  => 0,        # position at the changed sequence | 
| 71 |  |  |  |  |  |  | 'annot1'  => $annot1,  # custom annotation - slot 1 | 
| 72 |  |  |  |  |  |  | 'annot2'  => $annot2 ? [$annot2] : [] # custom annotation - slot 2 | 
| 73 |  |  |  |  |  |  | }; | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 375 |  |  |  |  | 836 | return $piece; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | sub insert { | 
| 79 | 36 |  |  | 36 | 0 | 10246 | my ($self, $ref, $pos, $annot) = @_; | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | # Test if the position is inside the original sequence boundary | 
| 82 | 36 | 50 |  |  |  | 1056 | if ($pos > $self->len) { | 
| 83 | 0 |  |  |  |  | 0 | croak "Trying to insert outside the original sequence"; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | # My length | 
| 87 | 36 |  |  |  |  | 123 | my $len = length $$ref; | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | # Create piece data | 
| 90 | 36 |  |  |  |  | 116 | my $new_piece = $self->_piece_new($ref, 0, 0, $len, $pos, $annot); | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | # Insert at end position or split | 
| 93 |  |  |  |  |  |  | # piece found at position 'pos'. | 
| 94 | 36 | 100 |  |  |  | 1026 | my $index = $pos == $self->len | 
| 95 |  |  |  |  |  |  | ? $self->_count_pieces | 
| 96 |  |  |  |  |  |  | : $self->_split_piece($pos); | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 36 |  |  |  |  | 1426 | my $piece = $self->_get_piece($index); | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 36 | 50 | 66 |  |  | 97 | if (defined $piece && @{ $piece->{annot2} } && ($pos == $piece->{pos})) { | 
|  | 31 |  | 33 |  |  | 137 |  | 
| 101 | 0 |  |  |  |  | 0 | unshift @{ $new_piece->{annot2} } => @{ $piece->{annot2} }; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 102 | 0 |  |  |  |  | 0 | $piece->{annot2} = []; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | # Then insert new_piece | 
| 106 | 36 |  |  |  |  | 1293 | $self->_splice_piece($index, 0, $new_piece); | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | sub delete { | 
| 110 | 36 |  |  | 36 | 0 | 25039 | my ($self, $pos, $len, $annot) = @_; | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | # Test if the removed region is inside the original sequence boundary | 
| 113 | 36 | 50 |  |  |  | 1239 | if (($pos + $len) > $self->len) { | 
| 114 | 0 |  |  |  |  | 0 | croak "Trying to delete a region outside the original sequence"; | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | # SPECIAL CASE: Delete at the very end | 
| 118 | 36 | 100 |  |  |  | 981 | if (($pos + $len) == $self->len) { | 
| 119 | 5 |  |  |  |  | 30 | return $self->_delete_at_end($len); | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | # Split piece at $pos. It will correctly fix the original | 
| 123 |  |  |  |  |  |  | # piece before the split and insert a new piece afterward. | 
| 124 |  |  |  |  |  |  | # So I need to catch tha last and fix the start and len fields | 
| 125 | 31 |  |  |  |  | 196 | my $index = $self->_split_piece($pos); | 
| 126 | 31 |  |  |  |  | 1105 | my $piece = $self->_get_piece($index); | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | # Fix position and len | 
| 129 | 31 |  |  |  |  | 64 | my $new_start = $pos + $len; | 
| 130 | 31 |  |  |  |  | 95 | my $new_len = $piece->{len} - $len; | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | # Update! | 
| 133 | 31 |  |  |  |  | 64 | $piece->{start} = $piece->{pos} = $new_start; | 
| 134 | 31 |  |  |  |  | 52 | $piece->{len} = $new_len; | 
| 135 | 31 |  |  |  |  | 58 | push @{ $piece->{annot2} } => $annot; | 
|  | 31 |  |  |  |  | 95 |  | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | # If the new len is zero, then remove | 
| 138 |  |  |  |  |  |  | # this piece | 
| 139 | 31 | 50 |  |  |  | 136 | if ($new_len == 0) { | 
| 140 | 0 |  |  |  |  | 0 | $self->_splice_piece($index, 1); | 
| 141 | 0 |  |  |  |  | 0 | my $next_piece = $self->_get_piece($index); | 
| 142 | 0 | 0 | 0 |  |  | 0 | if (defined $next_piece && @{ $piece->{annot2} }) { | 
|  | 0 |  |  |  |  | 0 |  | 
| 143 | 0 |  |  |  |  | 0 | unshift @{ $next_piece->{annot2} } => @{ $piece->{annot2} }; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 144 |  |  |  |  |  |  | } | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  | } | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | sub _delete_at_end { | 
| 149 | 5 |  |  | 5 |  | 20 | my ($self, $len) = @_; | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | # Just catch the last piece and remove the | 
| 152 |  |  |  |  |  |  | # last sequence length | 
| 153 | 5 |  |  |  |  | 180 | my $index = $self->_count_pieces - 1; | 
| 154 | 5 |  |  |  |  | 180 | my $piece = $self->_get_piece($index); | 
| 155 |  |  |  |  |  |  |  | 
| 156 | 5 |  |  |  |  | 15 | my $new_len = $piece->{len} - $len; | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | # Update! | 
| 159 | 5 |  |  |  |  | 15 | $piece->{len} = $new_len; | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | # If the new len is zero, then remove | 
| 162 |  |  |  |  |  |  | # this piece | 
| 163 | 5 | 50 |  |  |  | 30 | if ($new_len == 0) { | 
| 164 | 0 |  |  |  |  | 0 | $self->_splice_piece($index, 1); | 
| 165 |  |  |  |  |  |  | } | 
| 166 |  |  |  |  |  |  | } | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | sub change { | 
| 169 |  |  |  |  |  |  | # A delete and insert operations. | 
| 170 |  |  |  |  |  |  | # delete from pos until len and insert ref at pos | 
| 171 | 26 |  |  | 26 | 0 | 14031 | my ($self, $ref, $pos, $len, $annot) = @_; | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | # Test if the changing region is inside the original sequence boundary | 
| 174 | 26 | 50 |  |  |  | 942 | if (($pos + $len) > $self->len) { | 
| 175 | 0 |  |  |  |  | 0 | croak "Trying to change a region outside the original sequence"; | 
| 176 |  |  |  |  |  |  | } | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | # My length | 
| 179 | 26 |  |  |  |  | 74 | my $ref_len = length $$ref; | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | # Create piece data | 
| 182 | 26 |  |  |  |  | 98 | my $new_piece = $self->_piece_new($ref, 0, 0, $ref_len, $pos, $annot); | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | # SPECIAL CASE: Change at the very end | 
| 185 | 26 | 100 |  |  |  | 693 | if (($pos + $len) == $self->len) { | 
| 186 | 16 |  |  |  |  | 94 | return $self->_change_at_end($new_piece, $len); | 
| 187 |  |  |  |  |  |  | } | 
| 188 |  |  |  |  |  |  |  | 
| 189 |  |  |  |  |  |  | # Split piece found at position 'pos'. | 
| 190 |  |  |  |  |  |  | # Update old piece, insert piece and return | 
| 191 |  |  |  |  |  |  | # index where I can find the piece to remove | 
| 192 |  |  |  |  |  |  | # from and insert the change | 
| 193 | 10 |  |  |  |  | 35 | my $index = $self->_split_piece($pos); | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | # Catch the piece from where I will remove | 
| 196 | 10 |  |  |  |  | 345 | my $piece = $self->_get_piece($index); | 
| 197 |  |  |  |  |  |  |  | 
| 198 | 10 | 50 | 33 |  |  | 25 | if (@{ $piece->{annot2} } && ($pos == $piece->{pos})) { | 
|  | 10 |  |  |  |  | 50 |  | 
| 199 | 0 |  |  |  |  | 0 | unshift @{ $new_piece->{annot2} } => @{ $piece->{annot2} }; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 200 | 0 |  |  |  |  | 0 | $piece->{annot2} = []; | 
| 201 |  |  |  |  |  |  | } | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | # Fix position and len | 
| 204 | 10 |  |  |  |  | 35 | my $new_start = $pos + $len; | 
| 205 | 10 |  |  |  |  | 20 | my $new_len = $piece->{len} - $len; | 
| 206 |  |  |  |  |  |  |  | 
| 207 |  |  |  |  |  |  | # Update! | 
| 208 | 10 |  |  |  |  | 20 | $piece->{start} = $piece->{pos} = $new_start; | 
| 209 | 10 |  |  |  |  | 20 | $piece->{len} = $new_len; | 
| 210 |  |  |  |  |  |  |  | 
| 211 |  |  |  |  |  |  | # If the new len is zero, then remove | 
| 212 |  |  |  |  |  |  | # this piece | 
| 213 | 10 | 50 |  |  |  | 35 | if ($new_len == 0) { | 
| 214 | 0 |  |  |  |  | 0 | $self->_splice_piece($index, 1); | 
| 215 |  |  |  |  |  |  | } | 
| 216 |  |  |  |  |  |  |  | 
| 217 |  |  |  |  |  |  | # Then insert new_piece | 
| 218 | 10 |  |  |  |  | 360 | $self->_splice_piece($index, 0, $new_piece); | 
| 219 |  |  |  |  |  |  | } | 
| 220 |  |  |  |  |  |  |  | 
| 221 |  |  |  |  |  |  | sub _change_at_end { | 
| 222 | 16 |  |  | 16 |  | 35 | my ($self, $new_piece, $len) = @_; | 
| 223 |  |  |  |  |  |  |  | 
| 224 |  |  |  |  |  |  | # Just catch the last piece and remove the | 
| 225 |  |  |  |  |  |  | # last sequence length | 
| 226 | 16 |  |  |  |  | 605 | my $index = $self->_count_pieces - 1; | 
| 227 | 16 |  |  |  |  | 555 | my $piece = $self->_get_piece($index); | 
| 228 |  |  |  |  |  |  |  | 
| 229 | 16 |  |  |  |  | 103 | my $new_len = $piece->{len} - $len; | 
| 230 | 16 |  |  |  |  | 27 | $piece->{len} = $new_len; | 
| 231 |  |  |  |  |  |  |  | 
| 232 |  |  |  |  |  |  | # If the new len is zero, then remove | 
| 233 |  |  |  |  |  |  | # this piece | 
| 234 | 16 | 100 |  |  |  | 55 | if ($new_len == 0) { | 
| 235 | 11 |  |  |  |  | 493 | $self->_splice_piece($index--, 1); | 
| 236 |  |  |  |  |  |  | } | 
| 237 |  |  |  |  |  |  |  | 
| 238 |  |  |  |  |  |  | # Then insert new_piece | 
| 239 | 16 |  |  |  |  | 650 | $self->_splice_piece($index + 1, 0, $new_piece); | 
| 240 |  |  |  |  |  |  | } | 
| 241 |  |  |  |  |  |  |  | 
| 242 |  |  |  |  |  |  | sub calculate_logical_offset { | 
| 243 |  |  |  |  |  |  | # Before lookup() it is necessary to calculate | 
| 244 |  |  |  |  |  |  | # the positions according to the shift caused by | 
| 245 |  |  |  |  |  |  | # the structural variations. | 
| 246 | 217 |  |  | 217 | 0 | 1082 | my $self = shift; | 
| 247 |  |  |  |  |  |  |  | 
| 248 | 217 |  |  |  |  | 354 | my $offset_acm = 0; | 
| 249 |  |  |  |  |  |  |  | 
| 250 |  |  |  |  |  |  | # Insert each piece reference into a tree | 
| 251 | 217 |  |  |  |  | 8201 | for my $piece ($self->_all_pieces) { | 
| 252 |  |  |  |  |  |  | # Update piece offset | 
| 253 | 298 |  |  |  |  | 513 | $piece->{offset} = $offset_acm; | 
| 254 |  |  |  |  |  |  |  | 
| 255 |  |  |  |  |  |  | # Update offset acumulator | 
| 256 | 298 |  |  |  |  | 591 | $offset_acm += $piece->{len}; | 
| 257 |  |  |  |  |  |  | } | 
| 258 |  |  |  |  |  |  |  | 
| 259 |  |  |  |  |  |  | # Update logical offset with the corrected length | 
| 260 | 217 |  |  |  |  | 6001 | $self->logical_len($offset_acm); | 
| 261 |  |  |  |  |  |  | } | 
| 262 |  |  |  |  |  |  |  | 
| 263 |  |  |  |  |  |  | sub lookup { | 
| 264 |  |  |  |  |  |  | # Run 'calculate_logical_offset' before | 
| 265 | 7735 |  |  | 7735 | 0 | 15700 | my ($self, $start, $len) = @_; | 
| 266 |  |  |  |  |  |  |  | 
| 267 |  |  |  |  |  |  | state $comm = sub { | 
| 268 | 7828 |  |  | 7828 |  | 14009 | my ($pos, $piece) = @_; | 
| 269 | 7828 | 100 |  |  |  | 24537 | if ($self->_is_pos_inside_range($pos, $piece->{offset}, $piece->{len})) { | 
|  |  | 100 |  |  |  |  |  | 
| 270 | 7735 |  |  |  |  | 21092 | return 0; | 
| 271 |  |  |  |  |  |  | } elsif ($pos > $piece->{offset}) { | 
| 272 | 49 |  |  |  |  | 141 | return 1; | 
| 273 |  |  |  |  |  |  | } else { | 
| 274 | 44 |  |  |  |  | 130 | return -1; | 
| 275 |  |  |  |  |  |  | } | 
| 276 | 7735 |  |  |  |  | 11877 | }; | 
| 277 |  |  |  |  |  |  |  | 
| 278 | 7735 |  |  |  |  | 230217 | my $index = $self->with_bsearch($start, $self->piece_table, | 
| 279 |  |  |  |  |  |  | $self->_count_pieces, $comm); | 
| 280 |  |  |  |  |  |  |  | 
| 281 | 7735 | 50 |  |  |  | 17299 | if (not defined $index) { | 
| 282 | 0 |  |  |  |  | 0 | croak "Not found index for range: start = $start, length = $len"; | 
| 283 |  |  |  |  |  |  | } | 
| 284 |  |  |  |  |  |  |  | 
| 285 | 7735 |  |  |  |  | 277632 | my $piece = $self->_get_piece($index); | 
| 286 | 7735 |  |  |  |  | 12835 | my @pieces; | 
| 287 |  |  |  |  |  |  |  | 
| 288 |  |  |  |  |  |  | do { | 
| 289 | 8015 |  |  |  |  | 15528 | push @pieces => $piece; | 
| 290 | 8015 |  |  |  |  | 275885 | $piece = $self->_get_piece(++$index); | 
| 291 | 7735 |  | 100 |  |  | 11718 | } while ($piece && $self->_do_overlap($start, $len, $piece->{offset}, $piece->{len})); | 
| 292 |  |  |  |  |  |  |  | 
| 293 | 7735 |  |  |  |  | 20923 | return \@pieces; | 
| 294 |  |  |  |  |  |  | } | 
| 295 |  |  |  |  |  |  |  | 
| 296 |  |  |  |  |  |  | sub _do_overlap { | 
| 297 | 434 |  |  | 434 |  | 971 | my ($self, $start1, $len1, $start2, $len2) = @_; | 
| 298 | 434 |  |  |  |  | 1091 | my ($end1, $end2) = ($start1 + $len1 - 1, $start2 + $len2 - 1); | 
| 299 | 868 |  | 66 |  |  | 1878 | return $start1 <= $end2 && $start2 <= $end1; | 
| 300 |  |  |  |  |  |  | } | 
| 301 |  |  |  |  |  |  |  | 
| 302 |  |  |  |  |  |  | sub _split_piece { | 
| 303 | 72 |  |  | 72 |  | 151 | my ($self, $pos) = @_; | 
| 304 |  |  |  |  |  |  |  | 
| 305 |  |  |  |  |  |  | # Catch orig index where pos is inside | 
| 306 | 72 |  |  |  |  | 228 | my $index = $self->_piece_at($pos); | 
| 307 |  |  |  |  |  |  |  | 
| 308 |  |  |  |  |  |  | # Get piece which will be updated | 
| 309 | 72 |  |  |  |  | 2435 | my $old_piece = $self->_get_piece($index); | 
| 310 |  |  |  |  |  |  |  | 
| 311 |  |  |  |  |  |  | # Split at the beggining of a piece, | 
| 312 |  |  |  |  |  |  | # or this piece has length 1 | 
| 313 | 72 | 100 |  |  |  | 197 | if ($pos == $old_piece->{start}) { | 
| 314 | 15 |  |  |  |  | 35 | return $index; | 
| 315 |  |  |  |  |  |  | } | 
| 316 |  |  |  |  |  |  |  | 
| 317 |  |  |  |  |  |  | # Calculate piece end | 
| 318 | 57 |  |  |  |  | 140 | my $old_end = $old_piece->{start} + $old_piece->{len} - 1; | 
| 319 |  |  |  |  |  |  |  | 
| 320 |  |  |  |  |  |  | # Calculate the corrected length according to the split | 
| 321 | 57 |  |  |  |  | 85 | my $new_len = $pos - $old_piece->{start}; | 
| 322 |  |  |  |  |  |  |  | 
| 323 |  |  |  |  |  |  | # Update piece | 
| 324 | 57 |  |  |  |  | 93 | $old_piece->{len} = $new_len; | 
| 325 |  |  |  |  |  |  |  | 
| 326 |  |  |  |  |  |  | # Create the second part of the split after the break position | 
| 327 |  |  |  |  |  |  | my $piece = $self->_piece_new($old_piece->{ref}, $old_piece->{is_orig}, | 
| 328 | 57 |  |  |  |  | 169 | $pos, $old_end - $pos + 1, $pos); | 
| 329 |  |  |  |  |  |  |  | 
| 330 |  |  |  |  |  |  | # Insert second part after updated piece | 
| 331 | 57 |  |  |  |  | 2167 | $self->_splice_piece(++$index, 0, $piece); | 
| 332 |  |  |  |  |  |  |  | 
| 333 |  |  |  |  |  |  | # return corrected index that resolves to | 
| 334 |  |  |  |  |  |  | # the position between the breaked piece | 
| 335 | 57 |  |  |  |  | 152 | return $index; | 
| 336 |  |  |  |  |  |  | } | 
| 337 |  |  |  |  |  |  |  | 
| 338 |  |  |  |  |  |  | sub _is_pos_inside_range { | 
| 339 | 7921 |  |  | 7921 |  | 15150 | my ($self, $pos, $start, $len) = @_; | 
| 340 | 7921 |  |  |  |  | 12279 | my $end = $start + $len - 1; | 
| 341 | 7921 |  | 100 |  |  | 37333 | return $pos >= $start && $pos <= $end; | 
| 342 |  |  |  |  |  |  | } | 
| 343 |  |  |  |  |  |  |  | 
| 344 |  |  |  |  |  |  | sub _piece_at { | 
| 345 | 72 |  |  | 72 |  | 144 | my ($self, $pos) = @_; | 
| 346 |  |  |  |  |  |  |  | 
| 347 |  |  |  |  |  |  | # State the function to compare at bsearch | 
| 348 |  |  |  |  |  |  | state $comm = sub { | 
| 349 | 93 |  |  | 93 |  | 187 | my ($pos, $piece) = @_; | 
| 350 | 93 | 100 |  |  |  | 302 | if ($self->_is_pos_inside_range($pos, $piece->{pos}, $piece->{len})) { | 
|  |  | 50 |  |  |  |  |  | 
| 351 | 72 |  |  |  |  | 228 | return 0; | 
| 352 |  |  |  |  |  |  | } elsif ($pos > $piece->{pos}) { | 
| 353 | 21 |  |  |  |  | 64 | return 1; | 
| 354 |  |  |  |  |  |  | } else { | 
| 355 | 0 |  |  |  |  | 0 | return -1; | 
| 356 |  |  |  |  |  |  | } | 
| 357 | 72 |  |  |  |  | 165 | }; | 
| 358 |  |  |  |  |  |  |  | 
| 359 |  |  |  |  |  |  | # Search the piece index where $pos is inside the boundaries | 
| 360 | 72 |  |  |  |  | 1932 | my $index = $self->with_bsearch($pos, $self->piece_table, | 
| 361 |  |  |  |  |  |  | $self->_count_pieces, $comm); | 
| 362 |  |  |  |  |  |  |  | 
| 363 |  |  |  |  |  |  | # Maybe it is undef. I need to take care to not | 
| 364 |  |  |  |  |  |  | # search to a position that was removed before. | 
| 365 |  |  |  |  |  |  | # I can avoid it when parsing the snv file | 
| 366 | 72 | 50 |  |  |  | 191 | if (not defined $index) { | 
| 367 | 0 |  |  |  |  | 0 | croak "Not found pos = $pos into piece_table. Maybe the region was removed?"; | 
| 368 |  |  |  |  |  |  | } | 
| 369 |  |  |  |  |  |  |  | 
| 370 |  |  |  |  |  |  | # Catch the piece at index | 
| 371 | 72 |  |  |  |  | 2711 | my $piece = $self->_get_piece($index); | 
| 372 |  |  |  |  |  |  |  | 
| 373 |  |  |  |  |  |  | # It needs to catch a orig piece, so if the piece | 
| 374 |  |  |  |  |  |  | # is an insertion, it will search forward and backward | 
| 375 |  |  |  |  |  |  | # from the actual index | 
| 376 | 72 | 50 |  |  |  | 196 | if (not $piece->{is_orig}) { | 
| 377 | 0 |  |  |  |  | 0 | my $new_index; | 
| 378 |  |  |  |  |  |  |  | 
| 379 |  |  |  |  |  |  | # Search forward | 
| 380 | 0 |  |  |  |  | 0 | for (my $i = $index + 1; $i < $self->_count_pieces; $i++) { | 
| 381 | 0 |  |  |  |  | 0 | my $piece = $self->_get_piece($i); | 
| 382 | 0 | 0 |  |  |  | 0 | if ($piece->{is_orig}) { | 
| 383 | 0 | 0 |  |  |  | 0 | if ($self->_is_pos_inside_range($pos, $piece->{pos}, $piece->{len})) { | 
| 384 | 0 |  |  |  |  | 0 | $new_index = $i; | 
| 385 |  |  |  |  |  |  | } | 
| 386 |  |  |  |  |  |  |  | 
| 387 | 0 |  |  |  |  | 0 | last; | 
| 388 |  |  |  |  |  |  | } | 
| 389 |  |  |  |  |  |  | } | 
| 390 |  |  |  |  |  |  |  | 
| 391 | 0 | 0 |  |  |  | 0 | if (not defined $new_index) { | 
| 392 |  |  |  |  |  |  | # Search backward | 
| 393 | 0 |  |  |  |  | 0 | for (my $i = $index - 1; $i >= 0; $i--) { | 
| 394 | 0 |  |  |  |  | 0 | my $piece = $self->_get_piece($i); | 
| 395 | 0 | 0 |  |  |  | 0 | if ($piece->{is_orig}) { | 
| 396 | 0 | 0 |  |  |  | 0 | if ($self->_is_pos_inside_range($pos, $piece->{pos}, $piece->{len})) { | 
| 397 | 0 |  |  |  |  | 0 | $new_index = $i; | 
| 398 |  |  |  |  |  |  | } | 
| 399 |  |  |  |  |  |  |  | 
| 400 | 0 |  |  |  |  | 0 | last; | 
| 401 |  |  |  |  |  |  | } | 
| 402 |  |  |  |  |  |  | } | 
| 403 |  |  |  |  |  |  |  | 
| 404 |  |  |  |  |  |  | # Not found at all :( | 
| 405 | 0 | 0 |  |  |  | 0 | if (not defined $new_index) { | 
| 406 | 0 |  |  |  |  | 0 | croak "There is no orig position to '$pos'"; | 
| 407 |  |  |  |  |  |  | } | 
| 408 |  |  |  |  |  |  | } | 
| 409 |  |  |  |  |  |  |  | 
| 410 | 0 |  |  |  |  | 0 | $index = $new_index; | 
| 411 |  |  |  |  |  |  | } | 
| 412 |  |  |  |  |  |  |  | 
| 413 | 72 |  |  |  |  | 195 | return $index; | 
| 414 |  |  |  |  |  |  | } | 
| 415 |  |  |  |  |  |  |  | 
| 416 |  |  |  |  |  |  | __END__ | 
| 417 |  |  |  |  |  |  |  | 
| 418 |  |  |  |  |  |  | =pod | 
| 419 |  |  |  |  |  |  |  | 
| 420 |  |  |  |  |  |  | =encoding UTF-8 | 
| 421 |  |  |  |  |  |  |  | 
| 422 |  |  |  |  |  |  | =head1 NAME | 
| 423 |  |  |  |  |  |  |  | 
| 424 |  |  |  |  |  |  | App::Sandy::PieceTable - Implement a piece table data structure class | 
| 425 |  |  |  |  |  |  |  | 
| 426 |  |  |  |  |  |  | =head1 VERSION | 
| 427 |  |  |  |  |  |  |  | 
| 428 |  |  |  |  |  |  | version 0.24 | 
| 429 |  |  |  |  |  |  |  | 
| 430 |  |  |  |  |  |  | =head1 AUTHORS | 
| 431 |  |  |  |  |  |  |  | 
| 432 |  |  |  |  |  |  | =over 4 | 
| 433 |  |  |  |  |  |  |  | 
| 434 |  |  |  |  |  |  | =item * | 
| 435 |  |  |  |  |  |  |  | 
| 436 |  |  |  |  |  |  | Thiago L. A. Miller <tmiller@mochsl.org.br> | 
| 437 |  |  |  |  |  |  |  | 
| 438 |  |  |  |  |  |  | =item * | 
| 439 |  |  |  |  |  |  |  | 
| 440 |  |  |  |  |  |  | J. Leonel Buzzo <lbuzzo@mochsl.org.br> | 
| 441 |  |  |  |  |  |  |  | 
| 442 |  |  |  |  |  |  | =item * | 
| 443 |  |  |  |  |  |  |  | 
| 444 |  |  |  |  |  |  | Felipe R. C. dos Santos <fsantos@mochsl.org.br> | 
| 445 |  |  |  |  |  |  |  | 
| 446 |  |  |  |  |  |  | =item * | 
| 447 |  |  |  |  |  |  |  | 
| 448 |  |  |  |  |  |  | Helena B. Conceição <hconceicao@mochsl.org.br> | 
| 449 |  |  |  |  |  |  |  | 
| 450 |  |  |  |  |  |  | =item * | 
| 451 |  |  |  |  |  |  |  | 
| 452 |  |  |  |  |  |  | Rodrigo Barreiro <rbarreiro@mochsl.org.br> | 
| 453 |  |  |  |  |  |  |  | 
| 454 |  |  |  |  |  |  | =item * | 
| 455 |  |  |  |  |  |  |  | 
| 456 |  |  |  |  |  |  | Gabriela Guardia <gguardia@mochsl.org.br> | 
| 457 |  |  |  |  |  |  |  | 
| 458 |  |  |  |  |  |  | =item * | 
| 459 |  |  |  |  |  |  |  | 
| 460 |  |  |  |  |  |  | Fernanda Orpinelli <forpinelli@mochsl.org.br> | 
| 461 |  |  |  |  |  |  |  | 
| 462 |  |  |  |  |  |  | =item * | 
| 463 |  |  |  |  |  |  |  | 
| 464 |  |  |  |  |  |  | Rafael Mercuri <rmercuri@mochsl.org.br> | 
| 465 |  |  |  |  |  |  |  | 
| 466 |  |  |  |  |  |  | =item * | 
| 467 |  |  |  |  |  |  |  | 
| 468 |  |  |  |  |  |  | Rodrigo Barreiro <rbarreiro@mochsl.org.br> | 
| 469 |  |  |  |  |  |  |  | 
| 470 |  |  |  |  |  |  | =item * | 
| 471 |  |  |  |  |  |  |  | 
| 472 |  |  |  |  |  |  | Pedro A. F. Galante <pgalante@mochsl.org.br> | 
| 473 |  |  |  |  |  |  |  | 
| 474 |  |  |  |  |  |  | =back | 
| 475 |  |  |  |  |  |  |  | 
| 476 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 477 |  |  |  |  |  |  |  | 
| 478 |  |  |  |  |  |  | This software is Copyright (c) 2023 by Teaching and Research Institute from Sírio-Libanês Hospital. | 
| 479 |  |  |  |  |  |  |  | 
| 480 |  |  |  |  |  |  | This is free software, licensed under: | 
| 481 |  |  |  |  |  |  |  | 
| 482 |  |  |  |  |  |  | The GNU General Public License, Version 3, June 2007 | 
| 483 |  |  |  |  |  |  |  | 
| 484 |  |  |  |  |  |  | =cut |