def choose_best_aligned(aligned):
    """Choose the longest profile match as the "reference" chain.

    Returns a tuple: (sequence ID, sequence string)
    """
    def aligned_len(seq):
        return sum(c.isupper() for c in seq.replace('X', ''))

    if not aligned:
        raise RuntimeError("No PDB sequences were aligned by the profile!")
    elif len(aligned) == 1:
        ref_id, ref_aln = aligned.items()[0]
    else:
        ref_id = max(aligned.iteritems(), key=lambda kv: aligned_len(kv[1]))
        ref_aln = aligned[ref_id]
    return ref_id, ref_aln