From 28da1d88b1f8688e8784c064b1aab6c5772b3c87 Mon Sep 17 00:00:00 2001 From: Manuel Lera Ramirez Date: Tue, 30 Jan 2024 18:19:16 +0000 Subject: [PATCH] closes #161 --- src/pydna/dseqrecord.py | 3 +++ tests/test_module_dseq.py | 17 +++++++++++++++-- tests/test_module_dseqrecord.py | 12 +++++++++--- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/pydna/dseqrecord.py b/src/pydna/dseqrecord.py index f183e134..00de60b4 100644 --- a/src/pydna/dseqrecord.py +++ b/src/pydna/dseqrecord.py @@ -904,6 +904,9 @@ def __getitem__(self, sl): answer.features = [f for f in answer.features if ( _location_boundaries(f.location)[1] <= answer.seq.length and _location_boundaries(f.location)[0] <= _location_boundaries(f.location)[1])] + elif self.circular and sl_start == sl_stop: + cut = ((sl_start, 0), None) + return self.apply_cut(cut, cut) else: answer = Dseqrecord("") identifier = "part_{id}".format(id=self.id) diff --git a/tests/test_module_dseq.py b/tests/test_module_dseq.py index 4d49bd10..d0f00e9c 100644 --- a/tests/test_module_dseq.py +++ b/tests/test_module_dseq.py @@ -610,6 +610,12 @@ def test_Dseq___getitem__(): assert s[9:1] == Dseq("") assert t[9:1] == Dseq("") + # Indexing of full circular molecule (https://github.com/BjornFJohansson/pydna/issues/161) + s = Dseq("GGATCC", circular=True) + str_seq = str(s) + for shift in range(len(s)): + assert str(s[shift : shift]) == str_seq[shift:] + str_seq[:shift] + def test_cut_circular(): from pydna.dseq import Dseq @@ -890,16 +896,23 @@ def test_apply_cut(): if start < 0: start += len(seq) # Cut with negative ovhg - new_cut = (( start, -3), None) + new_cut = ((start, -3), None) out = seq_shifted.apply_cut(new_cut, new_cut) assert str(out) == 'ATGaattacgtATG' # Cut with positive ovhg start = (start + 3) % len(seq) - new_cut = (( start, 3), None) + new_cut = ((start, 3), None) out = seq_shifted.apply_cut(new_cut, new_cut) assert str(out) == 'ATGaattacgtATG' + # A blunt cut + start = 4 - shift + new_cut = ((start, 0), None) + out = seq_shifted.apply_cut(new_cut, new_cut) + assert str(out) == 'ATGaattacgt' + + def test_cutsite_is_valid(): from pydna.dseq import Dseq diff --git a/tests/test_module_dseqrecord.py b/tests/test_module_dseqrecord.py index 4473764b..43630448 100644 --- a/tests/test_module_dseqrecord.py +++ b/tests/test_module_dseqrecord.py @@ -1867,7 +1867,7 @@ def test___getitem__(): assert s[1:5].seq == Dseqrecord("GATC", circular=False).seq assert s[5:1:-1].seq == Dseqrecord("CCTA", circular=False).seq - assert t[1:1].seq == Dseqrecord("").seq + assert t[1:1].seq == Dseqrecord("GATCCG").seq assert t[5:1].seq == Dseqrecord("CG", circular=False).seq assert t[9:1].seq == Dseqrecord("").seq assert t[1:9].seq == Dseqrecord("").seq @@ -1876,8 +1876,8 @@ def test___getitem__(): # Test how slicing works with features (using sequence as in test_features_change_ori) seqRecord = Dseqrecord("aaagGTACCTTTGGATCcggg", circular=True) - f1 = SeqFeature(SimpleLocation(4, 17), type="misc_feature", strand=1) - f2 = SeqFeature(SimpleLocation(17, 21, 1) + SimpleLocation(0, 4), type="misc_feature", strand=1) + f1 = SeqFeature(SimpleLocation(4, 17, 1), type="misc_feature") + f2 = SeqFeature(SimpleLocation(17, 21, 1) + SimpleLocation(0, 4, 1), type="misc_feature") seqRecord.features = [f1, f2] # Exact feature sliced for normal and origin-spanning features @@ -1888,6 +1888,12 @@ def test___getitem__(): assert len(seqRecord[2:20].features) == 1 assert len(seqRecord[13:8].features) == 1 + # Indexing of full circular molecule (https://github.com/BjornFJohansson/pydna/issues/161) + s = Dseqrecord("GGATCC", circular=True) + str_seq = str(s.seq) + for shift in range(len(s)): + assert str(s[shift : shift].seq) == str_seq[shift:] + str_seq[:shift] + def test___eq__(): from pydna.dseqrecord import Dseqrecord