Skip to content

Instantly share code, notes, and snippets.

@renzon
Forked from vmesel/sequence_x_removal.py
Last active November 22, 2017 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renzon/0e86be7137d8bbdab9b6cf4e1fff8e9c to your computer and use it in GitHub Desktop.
Save renzon/0e86be7137d8bbdab9b6cf4e1fff8e9c to your computer and use it in GitHub Desktop.
Problema do Jimmy Neutron
from collections import deque
from itertools import cycle
import pytest
REPLACEMENTS = ["CCTTGG", "CTATGG", "CCACGG"]
def replace_gen_sub_seq(seq, replacements):
seq = list(seq)
replacements_len = len(replacements[0])
x_pattern = deque('X' * replacements_len)
current_substring = deque(maxlen=replacements_len)
def append_and_return_exceeding_item(item_to_append):
"""Append item do current substring and returns exceding char if its maxlen is reached
:param item_to_append:
:return: item or None
"""
exceeding_item = None
if len(current_substring) == replacements_len:
exceeding_item = current_substring[0]
current_substring.append(item_to_append)
return exceeding_item
replacements_cycle = cycle(replacements)
next(replacements_cycle) # advancing one
for char, replacement in zip(seq, replacements_cycle):
exceding_char = append_and_return_exceeding_item(char)
if exceding_char:
yield exceding_char
if current_substring == x_pattern:
yield from replacement
current_substring.clear()
yield from current_substring
@pytest.mark.parametrize(
'seq,expected_seq',
[
("XXXXXXXXXXXXTC", "CCTTGGCCTTGGTC"),
("CXXXXXXXXXXXXT", "CCTATGGCTATGGT"),
("CTXXXXXXXXXXXX", "CTCCACGGCCACGG"),
("CTXXXXXXXXXXXXXXXXXX", "CTCCACGGCCACGGCCACGG"),
("XXXXXXCXXXXXXTXXXXXX", "CCTTGGCCTATGGTCCACGG"),
]
)
def test_repeats_juntas(seq, expected_seq):
seq_output = ''.join(replace_gen_sub_seq(seq, REPLACEMENTS))
assert expected_seq == seq_output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment