Given a list of header lines (made by either
    vcf_file_to_dict() or vcf_file_to_list()),
    returns the sample name. Assumes only one sample
    in the file.
    Raises error if badly formatted #CHROM line.
    Returns None if no #CHROM line found

def get_sample_name_from_vcf_header_lines(header_lines):
    '''Given a list of header lines (made by either
    vcf_file_to_dict() or vcf_file_to_list()),
    returns the sample name. Assumes only one sample
    in the file.
    Raises error if badly formatted #CHROM line.
    Returns None if no #CHROM line found'''
    # We want the #CHROM line, which should be the last line
    # of the header
    for line in reversed(header_lines):
        if line.startswith('#CHROM'):
            fields = line.rstrip().split('\t')
            required_cols = ['#CHROM', 'POS', 'ID', 'REF', 'ALT', 'QUAL', 'FILTER', 'INFO']
            if fields[:len(required_cols)] != required_cols:
                raise Error('Error! #CHROM line must have these for first 8 columns: ' + ', '.join(required_cols) + '\nat this line of file: ' + line)

            if len(fields) == len(required_cols):
                return None

            required_cols.append('FORMAT')
            format_column = fields[len(required_cols) - 1]
            if format_column != required_cols[-1]:
                raise Error('Error! #CHROM line has 9^th column, which should be "FORMAT" but is: "' + format_column + '" at this line of file: ' + line)

            if len(fields) == len(required_cols):
                logging.warning('FORMAT column in file, but no sample names at line: ' + line.rstrip())
                return None

            sample = fields[len(required_cols)]

            if len(fields) > len(required_cols) + 1:
                logging.warning('More than one sample found. Using the name of the first sample (' + sample + ') at line: ' + line.rstrip())

            return sample

    return None