K-mers: Every Overlapping Window
Slice a synthetic read into every length-k window, including repeated strings.
Original synthetic inputs; computed records, not biological motion. The written trace below is the text equivalent.The problem
A sequencing read is a string over the DNA alphabet. For a chosen k, every start position from zero through the length minus k produces one window. The window count is therefore the read length minus k plus one. A repeated window at two positions remains two observations, because the input is a bag of windows rather than a set of labels. This distinction matters when a later graph must preserve parallel edges or when a count filter must see multiplicity.
Trace the program
For the demo read ATGCG and k equal to three, positions zero, one and two produce ATG, TGC and GCG. Position three cannot start a three-letter window. The program loops over every valid start rather than stepping by k, so adjacent windows overlap. When k equals the read length, exactly one window is returned. A lowercase letter, an N, or a k below two belongs to the declared invalid boundary rather than being silently repaired.
The live graph
Edit the demo input or source and run the program. The result is the complete ordered list of windows. Reset restores the demonstration read.
import json
from bio_data import DNA_BASES
data = json.loads(input())
s, k = (data['sequence'], data['k'])
if k < 2 or k > len(s) or any((b not in DNA_BASES for b in s)):
raise ValueError('invalid input')
result = [s[i:i + k] for i in range(len(s) - k + 1)]
print(result)
[
"ATG",
"TGC",
"GCG"
]
Browse the declared helper library
"""Declared input/model data only; algorithms are written in each program."""
DNA_BASES = 'ACGT'
Your turn
Return every overlapping length-k substring of sequence from left to right, preserving repeated strings. Reject k outside two through the read length or any non-ACGT letter. Set result to the list of windows.
import json
from bio_data import DNA_BASES
data = json.loads(input())
s, k = (data['sequence'], data['k'])
if k < 2 or k > len(s) or any((b not in DNA_BASES for b in s)):
raise ValueError('invalid input')
result = [s[:k]]
print(result)
Return every overlapping length-k substring of sequence from left to right, preserving repeated strings. Reject k outside two through the read length or any non-ACGT letter. Set result to the list of windows.
[
"AA"
]
Browse the declared helper library
"""Declared input/model data only; algorithms are written in each program."""
DNA_BASES = 'ACGT'
Boundaries and interpretation
This page slices a declared alphabet; it does not read FASTQ, quality scores, paired ends, or reverse complements. Overlap is a textual property, not proof that two windows came from the same biological molecule. A duplicate window can come from a repeat, from a sequencing artifact, or from two chance occurrences. The program reports positions only through the resulting list order, and it makes no coverage or error estimate.
The list preserves the order of start positions, so two identical windows at different positions are still distinct list entries. A sorted set would erase that positional evidence. The function also returns a window when k equals the read length, which is the smallest nonempty boundary rather than an error.
A window is identified by its text and its start position. Two windows with the same text at different starts are separate observations, so the output list can contain duplicates. The program does not deduplicate them, does not sort them, and does not add a sentinel base; those choices belong to the graph and count pages that follow.
Recap
- A length-n read has n minus k plus one windows when the window fits.
- Overlapping windows are produced by start positions, not by steps of k.
- Repeated window text remains repeated until an aggregation rule says otherwise.
Synthetic data: CC0-1.0. No personal data are sent anywhere.