Source code for geneblocks.sequence_modification_utils

"""These methods are only useful to build examples and tests for Geneblocks."""

from .biotools import reverse_complement


[docs]def change(seq, start, end, change): """Return the sequence with ``seq[start:end]`` replaced by ``change``""" return seq[:start] + change + seq[end:]
[docs]def insert(seq, pos, inserted): """Return the sequence with ``inserted`` inserted, starting at index 'pos' """ return seq[:pos] + inserted + seq[pos:]
[docs]def delete(seq, pos, deletions): """Return the sequence with a number of deletions from position pos.""" return seq[:pos] + seq[pos + deletions :]
[docs]def reverse(seq, start, end): """Return the sequence with segment seq[start:end] reverse-complemented.""" return seq[:start] + reverse_complement(seq[start:end]) + seq[end:]
[docs]def move(seq, start, end, diff): """Move a subsequence by "diff" nucleotides the left or the right.""" sub = seq[start:end] if diff > 0: return seq[:start] + seq[end : end + diff] + sub + seq[end + diff :] else: return ( seq[: start + diff] + sub + seq[start + diff : start] + seq[end:] )
[docs]def swap(seq, pos1, pos2): """Return a new sequence with segments at position pos1 and pos2 swapped. pos1, pos2 are both of the form (start1, end1), (start2, end2) """ (start1, end1), (start2, end2) = sorted([pos1, pos2]) return ( seq[:start1] + seq[start2:end2] + seq[end1:start2] + seq[start1:end1] + seq[end2:] )
[docs]def copy(seq, start, end, new_start): """Return the sequence with segment [start, end] also copied elsewhere, starting in new_start.""" return insert(seq, new_start, seq[start:end])