def _replace_Ns_with_ref(ref, seq):
    """ Replace Ns in a sequence given an aligned reference """

    assert len(ref) == len(seq)

    if 'N' not in seq and 'n' not in seq:
        return seq
    else:
        replaced = ''
        for r, s in zip(ref, seq):
            if s == 'N' or s == 'n':
                replaced += r
            else:
                replaced += s

        assert len(replaced) == len(seq)

        return replaced