def dnaa_max(dnaa_boxes, ORI_rrnas, sequence_length, name):
    """
    Finds the 1000 bp interval containing the greatest amount of bp in dnaA boxes

    :param dnaa_boxes:      dnaA boxes
    :param ORI_rrnas:       Predicted interval for ORI from rRNAs
    :param sequence_length: The length of the entire sequence
    :param name:            Species
    :return:                Index with maximum number of dnaA box bases
    """
    start = ORI_rrnas[0]
    end = ORI_rrnas[1]
    """Dictionary for storing the dnaa boxes and their information"""
    dnaa_dict = dict()
    for rec in dnaa_boxes:
        if rec[0] <= rec[1]:
            dnaa_dict[rec[0]] = [rec[0], rec[1], rec[2]]
        else:
            dnaa_dict[rec[1]] = [rec[1], rec[0], rec[2]]
    keylist = sorted(dnaa_dict)
    motifs_sorted = {}
    """Dictionary for storing sorted dnaA boxes"""
    for key in keylist:
        motifs_sorted[key] = dnaa_dict[key]
    window_size = 1000
    dnaa_list = [0] * sequence_length
    dnaa_count = [0] * sequence_length
    """If the interval does not span over the end and beginning of the sequence, 
    loop over the interval and save the bp that are within dnaA boxes, 
    and count the number of bp within a 1000 bp window. 
    If the interval does span the end and beginning, loop between the start of the interval 
    and end of sequence, and beginning of sequence to the end of the interval. Save and count."""
    if start <= end:
        for dnaa in motifs_sorted:
            if start <= motifs_sorted[dnaa][0
                ] <= end and start <= motifs_sorted[dnaa][1] <= end:
                for y in range(motifs_sorted[dnaa][0], motifs_sorted[dnaa][1]):
                    dnaa_list[y] = dnaa_list[y] + 1
        for i in range(start, end, 500):
            if i < end - window_size - 1:
                dnaa_count[i] = sum(dnaa_list[i:i + window_size])
            else:
                for x in range(window_size):
                    dnaa_count[i] = sum(dnaa_list[i - x:i - x + window_size])
    elif start > end:
        for dnaa in motifs_sorted:
            if 0 <= motifs_sorted[dnaa][0] <= end and 0 <= motifs_sorted[dnaa][
                1] <= end or start <= motifs_sorted[dnaa][0
                ] <= sequence_length and start <= motifs_sorted[dnaa][1
                ] <= sequence_length:
                for y in range(motifs_sorted[dnaa][0], motifs_sorted[dnaa][1]):
                    dnaa_list[y] = dnaa_list[y] + 1
            elif start <= motifs_sorted[dnaa][0
                ] <= sequence_length and 0 <= motifs_sorted[dnaa][1] <= end:
                for y in range(motifs_sorted[dnaa][0], sequence_length):
                    dnaa_list[y] = dnaa_list[y] + 1
                for y in range(0, motifs_sorted[dnaa][1]):
                    dnaa_list[y] = dnaa_list[y] + 1
        for i in range(start, sequence_length, 500):
            if i < end - window_size - 1:
                dnaa_count[i] = sum(dnaa_list[i:i + window_size])
            else:
                for x in range(window_size):
                    dnaa_count[i] = sum(dnaa_list[i - x:i - x + window_size])
        for i in range(0, end, 500):
            if i < end - window_size - 1:
                dnaa_count[i] = sum(dnaa_list[i:i + window_size])
            else:
                for x in range(window_size):
                    dnaa_count[i] = sum(dnaa_list[i - x:i - x + window_size])
    """Find the maximum number and the index where this occurs"""
    dif_max = dnaa_count.index(max(dnaa_count))
    return dif_max
