Search code examples
pythonslicesequences

whole DNA file Slice to a fixed length substring by index/location


I am new to python, I am writing code to slice a DNA sequence. the idea is to : firstly to count the sequence length in the text file, secondly, to slice the sequence into substrings with length of 5. and finally to write each substring in a line in output text file.

I couldn't figure out how to split the sequence from each character with the same length, let us say the text file contain [ACTGTATGCATACACGTA...] the results should be like ACTGT , CTGTA, TGTAT, ...

I need help with the incremental loop function that can help to split/slice by index/location


Solution

  • Try this one :

    st = "ACTGTATGCATACACGTA"
    for i in range(0,len(st[:-4])):
        print(st[i:i+5])
    

    O/P will be like :

    ACTGT CTGTA TGTAT GTATG TATGC ATGCA TGCAT GCATA CATAC ATACA TACAC ACACG CACGT ACGTA