def get_genome_pos(location, x, invert=False):
    """Get the genome position of the x-th base in a sequence."""
    if invert:
        offset = 0
        for i in range(1, len(location)):
            if i % 2:
                pos = location[i]
            elif pos <= x <= location[i]:
                break
            else:
                offset += location[i] - pos + 1
        else:
            if len(location) % 2:
                raise ValueError('Position %i is outside sequence range' % x)
        return offset + x - pos
    else:
        for i in range(1, len(location)):
            if i % 2:
                pos = location[i]
            elif location[i] - pos < x:
                x -= location[i] - pos + 1
            else:
                break
        return pos + x
