Reads the summarized intensities from 6 files.

    :param prefix: the prefix of the six files.

    :type prefix: str

    :returns: a :py:class`numpy.recarray` containing the following columns (for
              each of the samples): ``sampleID``, ``chr23``, ``chr24``,
              ``gender`` and ``status``.

    Instead of reading a final report (like :py:func:`read_intensities`), this
    function reads six files previously created by this module to gather sample
    information. Here are the content of the six files:

    * ``prefix.ok_females.txt``: information about females without sex problem.
    * ``prefix.ok_males.txt``: information about males without sex problem.
    * ``prefix.ok_unknowns.txt``: information about unknown gender without sex
                                  problem.
    * ``prefix.problematic_females.txt``: information about females with sex
                                          problem.
    * ``prefix.problematic_males.txt``: information about males with sex
                                        problem.
    * ``prefix.problematic_unknowns.txt``: information about unknown gender
                                           with sex problem.

    Each file contains the following columns: ``sampleID``, ``chr23``,
    ``chr24``, ``gender`` and ``status``.

    The final data set contains the following information for each sample:

    * ``sampleID``: the sample ID.
    * ``chr23``: the summarized intensities for chromosome 23.
    * ``chr24``: the summarized intensities for chromosome 24.
    * ``gender``: the gender of the sample (``Male`` or ``Female``).
    * ``status``: the status of the sample (``OK`` or ``Problem``).

    The summarized intensities for a chromosome (:math:`S_{chr}`) is computed
    using this formula (where :math:`I_{chr}` is the set of all marker
    intensities on chromosome :math:`chr`):

    .. math::
        S_{chr} = \\frac{\\sum{I_{chr}}}{||I_{chr}||}

def read_summarized_intensities(prefix):
    """Reads the summarized intensities from 6 files.

    :param prefix: the prefix of the six files.

    :type prefix: str

    :returns: a :py:class`numpy.recarray` containing the following columns (for
              each of the samples): ``sampleID``, ``chr23``, ``chr24``,
              ``gender`` and ``status``.

    Instead of reading a final report (like :py:func:`read_intensities`), this
    function reads six files previously created by this module to gather sample
    information. Here are the content of the six files:

    * ``prefix.ok_females.txt``: information about females without sex problem.
    * ``prefix.ok_males.txt``: information about males without sex problem.
    * ``prefix.ok_unknowns.txt``: information about unknown gender without sex
                                  problem.
    * ``prefix.problematic_females.txt``: information about females with sex
                                          problem.
    * ``prefix.problematic_males.txt``: information about males with sex
                                        problem.
    * ``prefix.problematic_unknowns.txt``: information about unknown gender
                                           with sex problem.

    Each file contains the following columns: ``sampleID``, ``chr23``,
    ``chr24``, ``gender`` and ``status``.

    The final data set contains the following information for each sample:

    * ``sampleID``: the sample ID.
    * ``chr23``: the summarized intensities for chromosome 23.
    * ``chr24``: the summarized intensities for chromosome 24.
    * ``gender``: the gender of the sample (``Male`` or ``Female``).
    * ``status``: the status of the sample (``OK`` or ``Problem``).

    The summarized intensities for a chromosome (:math:`S_{chr}`) is computed
    using this formula (where :math:`I_{chr}` is the set of all marker
    intensities on chromosome :math:`chr`):

    .. math::
        S_{chr} = \\frac{\\sum{I_{chr}}}{||I_{chr}||}

    """
    data = []
    for suffix in {".ok_females.txt", ".ok_males.txt", ".ok_unknowns.txt",
                   ".problematic_females.txt", ".problematic_males.txt",
                   ".problematic_unknowns.txt"}:
        with open(prefix + suffix, 'r') as input_file:
            header_index = None
            for line_nb, line in enumerate(input_file):
                row = line.rstrip("\r\n").split("\t")

                if line_nb == 0:
                    # This is the header
                    header_index = dict([
                        (col_name, i) for i, col_name in enumerate(row)
                    ])
                    for col_name in {"sampleID", "chr23", "chr24", "gender",
                                     "status"}:
                        if col_name not in header_index:
                            msg = "{}: no column named {}".format(
                                prefix+suffix,
                                col_name,
                            )
                            raise ProgramError(msg)

                else:
                    sampleID = row[header_index["sampleID"]]
                    chr23 = row[header_index["chr23"]]
                    chr24 = row[header_index["chr24"]]
                    gender = row[header_index["gender"]]
                    status = row[header_index["status"]]

                    try:
                        chr23 = float(chr23)
                        chr24 = float(chr24)
                    except ValueError:
                        msg = ("{} and {}: bad summarized intensities for "
                               "sample {}".format(chr23, chr24, sampleID))
                        raise ProgramError(msg)

                    data.append((sampleID, chr23, chr24, gender, status))

    # Creating the data structure
    data = np.array(
        data,
        dtype=[("sampleID", "a{}".format(max([len(i[0]) for i in data]))),
               ("chr23", float), ("chr24", float),
               ("gender", "a{}".format(max([len(i[3]) for i in data]))),
               ("status", "a{}".format(max([len(i[4]) for i in data])))],
    )
    return data