import csv


def count_samples_from_tsv(phenofile, keep_hpos, invert=False):
    """
    Count number of samples in phenofile with at least one of keep_hpos
    """

    n = 0

    with open(phenofile) as fin:
        for sid, phenos in csv.reader(fin, delimiter='\t'):
            hits = [h in phenos.split(';') for h in keep_hpos]
            if any(hits):
                if not invert:
                    n += 1
            else:
                if invert:
                    n += 1

    return n