Run the actual analysis on all valid loci for each phenotype

    :param dataset: GWAS parser object
    :param pheno_covar: holds all of the variables

    This acts as a standard iterator, returning a single MVResult for
    each locus/phenotype combination.

    Missing is evaluated as anything missing in any of the
    phenotype, covariate(s) or genotype

def RunAnalysis(dataset, pheno_covar):
    """Run the actual analysis on all valid loci for each phenotype

    :param dataset: GWAS parser object
    :param pheno_covar: holds all of the variables

    This acts as a standard iterator, returning a single MVResult for
    each locus/phenotype combination.

    Missing is evaluated as anything missing in any of the
    phenotype, covariate(s) or genotype

    """
    covar_missing = numpy.empty(dataset.ind_count, dtype=bool)
    covar_missing[:] = False

    total_covar_count = len(pheno_covar.covariate_data)
    iteration_count = []

    unsolved = []

    pcount = 2+total_covar_count

    std = get_standardizer()

    for snp in dataset:
        for y in pheno_covar:
            st = SimpleTimer()
            (pheno, covariates, nonmissing) = y.get_variables((snp.genotype_data==DataParser.missing_storage))


            genotypes  = snp.genotype_data[nonmissing]

            try:
                pvalt, estimates, pvalues, se, v = RunMeanVar(pheno, genotypes, covariates)

                lmgeno = genotypes
                for c in covariates:
                    lmgeno = lmgeno + c
                lm = scipy.stats.linregress(pheno, lmgeno)[3]


                betavars, se, pvalues = y.destandardize(estimates, se, pvalues=pvalues,v=v, nonmissing=nonmissing)

                betas = betavars[0:pcount]
                betase = se[0:pcount]
                vars = betavars[pcount:]
                varse = se[pcount:]

                # Check for invalid output
                for var in betavars + se + list(pvalues):
                    if numpy.isnan(var):
                        raise NanInResult()

                nonmissing_ct = numpy.sum(nonmissing)

                result = MVResult(
                                snp.chr,
                                snp.pos,
                                snp.rsid,
                                snp.major_allele,
                                snp.minor_allele,
                                dataset.get_effa_freq(genotypes),
                                non_miss_count=nonmissing_ct,
                                ph_label=y.get_phenotype_name(),
                                p_mvtest=pvalt,
                                beta_values=list(betas)+list(vars),
                                pvalues=pvalues,
                                stderrors=list(betase) + list(varse),
                                maf=snp.maf,
                                covar_labels=y.get_covariate_names(),
                                lm=lm,
                                runtime = st.runtime(),
                )

                result.blin = estimates[0:pcount]
                result.bvar = estimates[pcount:]
                yield result
            except NanInResult as e:
                print >> sys.stderr, "\t".join([str(x) for x in [
                                snp.chr,
                                snp.pos,
                                snp.rsid,
                                y.get_phenotype_name(),
                                "%d" % (numpy.sum(nonmissing)),
                                snp.major_allele,
                                snp.minor_allele,
                                snp.allele_count2,
                                "NAN-Found",
                                "MAF=%0.4f" % (snp.maf)]])
            except ValueError as e:
                print >> sys.stderr, "\t".join([str(x) for x in [
                                snp.chr,
                                snp.pos,
                                snp.rsid,
                                y.get_phenotype_name(),
                                "%d" % (numpy.sum(nonmissing)),
                                snp.major_allele,
                                snp.minor_allele,
                                snp.allele_count2,
                                "Unsolvable",
                                "MAF=%0.4f" % (snp.maf)]])
            except UnsolvedLocus as e:
                print >> sys.stderr, "\t".join([str(x) for x in [
                                snp.chr,
                                snp.pos,
                                snp.rsid,
                                y.get_phenotype_name(),
                                "%d" % (numpy.sum(nonmissing)),
                                snp.major_allele,
                                snp.minor_allele,
                                snp.allele_count2,
                                "Unsolved",
                                "MAF=%0.4f" % (snp.maf)]])
                unsolved.append(snp)
    if len(unsolved)>0:
        print >> sys.stderr, "Total unsolvable loci: ", len(unsolved)