I have a fasta file with millions of DNA sequences - for each DNA sequence, there is an ID as well.
>seq1
AATCAG #----> 5mers of this line are AATCA and ATCAG
>seq3
AAATTACTACTCTCTA
>seq19
ATTACG #----> 5mers of this line are ATTAC and TTACG
what I want to do is that if the length of DNA sequence is 6 then I make 5mers of that line (shown in the code and above). So the thing that I have could not solve is that I want to make a dictionary in a way that dictionary shows which 5mers come from which sequence id
.
here is my code but it is halfway through:
from Bio import SeqIO
from Bio.Seq import Seq
with open(file, 'r') as f:
lst = []
dict = {}
for record in SeqIO.parse(f, 'fasta'): #reads the fast file
if len(record.seq) == 6: #considers the DNA sequence length
for i in range(len(record.seq)):
kmer = str(record.seq[i:i + 5])
if len(kmer) == 5:
lst.append(kmer)
dict[record.id] = kmer #record.id picks the ids of DNA sequences
#print(lst)
print(dict)
the desired dictionary should look like:
dict = {'seq1':'AATCA', 'seq1':'ATCAG', 'seq19':'ATTAC','seq19':'TTACG'}