from csv import reader


def read_len_distrib(filename, minlen=35, maxlen=100):
    """Reads a tsv file with counts of read per length

    Parameters
    ----------
    filename : str
        tsv file containing the read length count
    minlen : int
        minimum length for the distribution
    maxlen : int
        maximum length for the distribution

    Returns
    -------
    list of float
        The computed proportion of each read length
    """

    with open(filename, "r", newline="") as csvfile:
        csvreader = reader(csvfile, delimiter="\t")
        first_row = csvreader.__next__()
        read_distrib = [0] * int(first_row[1]) + [int(first_row[0])]
        read_distrib.extend([int(row[0]) for row in csvreader])
    if len(read_distrib) < maxlen:
        print(
            "Read length distribution provides values only until",
            len(read_distrib),
            "; expected:",
            maxlen,
        )
    s = sum(read_distrib[minlen : maxlen + 1])
    return [r / s for r in read_distrib[minlen : maxlen + 1]]