Help to understand the code for dipeptide composition calculation (in python)

Dipeptide composition of a protein sequence is the number of times a particular dipeptide (e.g. Arginine-Histidine) occurs in a sequence divided by the total number of dipeptides in the sequence (which is the length of the sequence – 1)

I have found the following code for this:

    import re
    def DPC(fastas):
        AA = 'ACDEFGHIKLMNPQRSTVWY'
        encodings = []
        diPeptides = [aa1 + aa2 for aa1 in AA for aa2 in AA]
        header = ['#'] + diPeptides
        encodings.append(header)

        AADict = {}
        for i in range(len(AA)):
            AADict[AA[i]] = i

        for i in fastas:
            name, sequence = i[0], re.sub('-', '', i[1])
            code = [name]
            tmpCode = [0] * 400
            for j in range(len(sequence) - 2 + 1):
                tmpCode[AADict[sequence[j]] * 20 + AADict[sequence[j+1]]] = tmpCode[AADict[sequence[j]] * 20 + AADict[sequence[j+1]]] +1
            if sum(tmpCode) != 0:
                tmpCode = [i/sum(tmpCode) for i in tmpCode]
            code = code + tmpCode
            encodings.append
         return encodings

There is another file (main) from where the arguments fastas come from which is basically a list of strings where every string is a protein sequence. I am finding it really difficult to understand the nested loops that were used:

for i in fastas:
    name, sequence = i[0], re.sub('-', '', i[1])
    code = [name]
    tmpCode = [0] * 400
    for j in range(len(sequence) - 2 + 1):
        tmpCode[AADict[sequence[j]] * 20 + AADict[sequence[j+1]]] = tmpCode[AADict[sequence[j]] * 20 + AADict[sequence[j+1]]] +1
    if sum(tmpCode) != 0:
        tmpCode = [i/sum(tmpCode) for i in tmpCode]
    code = code + tmpCode
    encodings.append

I have spent 4-5 hours on this but I am still finding it really difficult to follow. I would really appreciate if anyone can explain the steps involved in these loops. Thank you!

Edit: The github link to the files are :

  1. DPC.py
  2. iFeature.py

The github link to the python toolkit: iFeature

Read more here: Source link