Reads a MDS file.

    :param file_name: the name of the ``mds`` file.
    :param c1: the first component to read (x axis).
    :param c2: the second component to read (y axis).
    :param pops: the population of each sample.

    :type file_name: str
    :type c1: str
    :type c2: str
    :type pops: dict

    :returns: a :py:class:`numpy.recarray` (one sample per line) with the
              information about the family ID, the individual ID, the first
              component to extract, the second component to extract and the
              population.

    The ``mds`` file is the result of Plink (as produced by the
    :py:mod:`pyGenClean.Ethnicity.check_ethnicity` module).

def read_mds_file(file_name, c1, c2, pops):
    """Reads a MDS file.

    :param file_name: the name of the ``mds`` file.
    :param c1: the first component to read (x axis).
    :param c2: the second component to read (y axis).
    :param pops: the population of each sample.

    :type file_name: str
    :type c1: str
    :type c2: str
    :type pops: dict

    :returns: a :py:class:`numpy.recarray` (one sample per line) with the
              information about the family ID, the individual ID, the first
              component to extract, the second component to extract and the
              population.

    The ``mds`` file is the result of Plink (as produced by the
    :py:mod:`pyGenClean.Ethnicity.check_ethnicity` module).

    """
    mds = []
    max_fid = 0
    max_iid = 0
    with open(file_name, 'rb') as input_file:
        # Getting and checking the header
        header_index = dict([
            (col_name, i) for i, col_name in
            enumerate(create_row(input_file.readline()))
        ])
        for col_name in {"FID", "IID", c1, c2}:
            if col_name not in header_index:
                msg = "{}: no column named {}".format(file_name, col_name)
                raise ProgramError(msg)

        for row in map(create_row, input_file):
            # Getting the sample ID
            sample_id = (row[header_index["FID"]], row[header_index["IID"]])

            # Checking we have a population for this sample
            if sample_id not in pops:
                msg = "{} {}: not in population file".format(sample_id[0],
                                                             sample_id[1])
                raise ProgramError(msg)

            # Saving the data
            mds.append((sample_id[0], sample_id[1], row[header_index[c1]],
                        row[header_index[c2]], pops[sample_id]))

            # Cheking to find the max FID
            if len(sample_id[0]) > max_fid:
                max_fid = len(sample_id[0])
            if len(sample_id[1]) > max_iid:
                max_iid = len(sample_id[1])

    # Creating the numpy array
    mds = np.array(mds, dtype=[("fid", "a{}".format(max_fid)),
                               ("iid", "a{}".format(max_iid)),
                               ("c1", float), ("c2", float),
                               ("pop", "a7")])

    return mds