def _sam_header_to_ref_list(header):
    """Modified code from Peter Cock's [BamWriter](https://github.com/peterjc/biopython/blob/SamBam2015/Bio/Sequencing/SamBam/__init__.py#L1714)

    Removes blank lines, ensures trailing newlines, spots some errors.

    """
    references = []
    for line in header.split('\n'):
        if line.startswith('@SQ\t'):
            r = None
            l = None
            for part in line[3:].rstrip().split('\t'):
                if part.startswith('SN:'):
                    r = part[3:]
                elif part.startswith('LN:'):
                    l = int(part[3:])
            if r is None or l is None:
                raise ValueError(
                    'Malformed @SQ header (SN and LN required):\n%r' % line)
            references.append((r, l))
    return references
