from typing import OrderedDict


def find_overlaps(orfs_found):

    """Associates overlapping ORFs based on their start-stop intervals"""

    # initialize dictionary to store associations for each ORF
    overlaps_found = { i : [] for i in range(1, len(orfs_found)+1) }
    processed_orfs = []

    # sort ORFs by ascending start position
    sorted_orfs = OrderedDict(sorted(orfs_found.items(), key = lambda kvls: kvls[1]['start']))

    # Loop through all combinations and store associations
    for orf, data in sorted_orfs.items():
        processed_orfs.append(orf)
        for orf_to_compare, data_to_compare in sorted_orfs.items():
            if orf_to_compare in processed_orfs:
                continue
            if data['stop'] >= data_to_compare['start']:
                overlaps_found[orf].append(orf_to_compare)
                overlaps_found[orf_to_compare].append(orf)
            else:
                break

    return(overlaps_found)