fromtypingimportGeneratordefkmers(sequence:str,k:int)->Generator[str,None,None]:"""
Generates k-mers of a given length from a DNA sequence.
Args:
sequence (str): The DNA sequence.
k (int): The length of the k-mers to generate.
Yields:
str: Each generated k-mer.
Examples:
>>> dna_sequence = "ATCGATCG"
>>> kmer_generator = kmers(dna_sequence, 3)
>>> for kmer in kmer_generator:
... print(kmer)
ATC
TCG
CGA
...
"""foriinrange(len(sequence)-k+1):yieldsequence[i:i+k]defmotif_finder(sequence:str,motif:str)->Generator[int,None,None]:"""
Finds occurrences of a motif within a DNA sequence.
Args:
sequence (str): The DNA sequence.
motif (str): The motif to search for.
Yields:
int: The indices of the occurrences of the motif within the sequence (1-based index).
Examples:
>>> dna_sequence = "ATCGATCG"
>>> motif_indices = motif_finder(dna_sequence, "ATC")
>>> for index in motif_indices:
... print(index)
1
"""fori,kmerinenumerate(kmers(sequence,len(motif))):ifkmer==motif:yieldi+1