Load covariate data from file.

        Unlike phenofiles, if we already have data, we keep it (that would be the sex covariate)

def load_covarfile(self, file, indices=[], names=[], sample_file=False):
        """Load covariate data from file.

        Unlike phenofiles, if we already have data, we keep it (that would be the sex covariate)"""

        # Clean up input in case we are given some empty values
        var_indices = []
        for x in indices:
            try:
                var_indices.append(int(x) + 1)
            except:
                pass

        var_names = []
        for name in names:
            if name.strip() != "":
                var_names.append(name)

        if PhenoCovar.sex_as_covariate:
            self.covariate_labels = ["SEX"]
        file.seek(0)
        if file:
            header = file.readline().strip().split()
            line_number = 0

            # Rows ignored because family/individual missing from our pedigree data
            ignored_data = []
            # We can accept a default phenotype column if we only have 3 columns
            if len(header) == 3:
                if len(var_names) + len(var_indices) == 0:
                    var_indices.append(2)

            if header[0].upper() == "FID":
                for name in var_names:
                    if name.strip() != "":
                        try:
                            var_indices.append(header.index(name))
                        except:
                            raise InvalidSelection("The name, %s, was not found in %s" % (name, file.name))
                line_number = 1


                if len(var_indices) > 0 and max(var_indices) > (len(header)):
                    raise InvalidSelection("The index, %s, is larger than the number of entries in the file, %s:%s" % (max(var_indices), file.name, line_number))

                for index in var_indices:
                    self.covariate_labels.append(header[index])
                # Dump the second line for sample_file==True
                if sample_file:
                    file.readline()
            else:
                file.seek(0)
                if len(var_names) > 0:
                    raise MalformedInputFile("Names only work with covariate files with headers: %s" % (",".join(names), file.name))
                if len(var_indices) > 0 and max(var_indices) > (len(header)):
                    raise InvalidSelection("The index, %s, is larger than the number of entries in the file, %s:%s" % (max(var_indices), file.name, line_number))

                for i in xrange(0, len(var_indices)):
                    self.covariate_labels.append("Cov-%s" % (var_indices[i]-1))

            covar_data = numpy.empty((len(var_indices)+len(self.covariate_data), len(self.pedigree_data)))
            covar_data.fill(PhenoCovar.missing_encoding)

            # We have to be careful to keep the sex covariate data if it came from the pedigree
            if PhenoCovar.sex_as_covariate:
                covar_data[0] = self.covariate_data[0]
            self.covariate_data = covar_data

            for line in file:
                line_number += 1
                words   = line.split()
                iid = ":".join(words[0:2])

                # Indexes are 1 based...silly humans
                if len(var_indices) > 0 and max(var_indices) > (len(words)):
                    raise InvalidSelection("The index, %s, is larger than the number of entries in the file, %s:%s" % (max(var_indices), file.name, line_number))

                cidx = 0
                if PhenoCovar.sex_as_covariate:
                    cidx += 1
                for idx in var_indices:
                    cov = float(words[idx])
                    if iid not in self.pedigree_data:
                        ignored_data.append(iid)
                    else:
                        self.covariate_data[cidx, self.pedigree_data[iid]] = cov
                    cidx += 1


        for covar in self.covariate_data:
            if sum(covar !=PhenoCovar.missing_encoding ) == 0:
                raise NoMatchedPhenoCovars("No matching individuals were found in the covariate file")