def stats_from_aligned_read(read, references, lengths):
    """Create summary information for an aligned read

    :param read: :class:`pysam.AlignedSegment` object
    """
    tags = dict(read.tags)
    try:
        tags.get('NM')
    except:
        raise IOError(
            "Read is missing required 'NM' tag. Try running 'samtools fillmd -S - ref.fa'."
            )
    name = read.qname
    counts, _ = read.get_cigar_stats()
    match = counts[0]
    ins = counts[1]
    delt = counts[2]
    sub = tags['NM'] - ins - delt
    length = match + ins + delt
    iden = 100 * float(match - sub) / match
    acc = 100 - 100 * float(tags['NM']) / length
    read_length = read.infer_read_length()
    coverage = 100 * float(read.query_alignment_length) / read_length
    direction = '-' if read.is_reverse else '+'
    results = {'name': name, 'coverage': coverage, 'direction': direction,
        'length': length, 'read_length': read_length, 'match': match, 'ins':
        ins, 'del': delt, 'sub': sub, 'iden': iden, 'acc': acc, 'qstart':
        read.query_alignment_start, 'qend': read.query_alignment_end,
        'rstart': read.reference_start, 'rend': read.reference_end, 'ref':
        references[read.reference_id], 'aligned_ref_len': read.
        reference_length, 'ref_coverage': 100 * float(read.reference_length
        ) / lengths[read.reference_id]}
    return results
