import re


def isSynchronous(key1, key2, mali, exons):
    """check if two transcripts are on synchronous positions.

    Two transcripts are synchroneous, if their position in the
    alignment and on the genomic sequence are both a < b.

    Both front and end are taken into account. Note that they
    can still be overlapping.
    """

    def getStartEnd(s):
        start, end = 0, len(s)
        x = re.match("([-.]+)", s)
        if x:
            start = len(x.groups()[0])
        x = re.search("([-.]+)$", s)
        if x:
            end -= len(x.groups()[0])

        return start, end

    mstart1, mend1 = getStartEnd(mali[key1])
    mstart2, mend2 = getStartEnd(mali[key2])

    gstart1, gend1 = exons[key1][0].mGenomeFrom, exons[key1][-1].mGenomeTo
    gstart2, gend2 = exons[key2][0].mGenomeFrom, exons[key2][-1].mGenomeTo

    return (mstart1 < mstart2 and mend1 < mend2 and gstart1 < gstart2 and gend1 < gend2) or \
           (mstart2 < mstart1 and mend2 <
            mend1 and gstart2 < gstart1 and gend2 < gend1)