loading data

def computeCovarianceMatrixPython(out_dir,bfile,cfile,sim_type='RRM'):
    print("Using python to create covariance matrix. This might be slow. We recommend using plink instead.")

    if sim_type is not 'RRM':
        raise Exception('sim_type %s is not known'%sim_type)

    """ loading data """
    data = plink_reader.readBED(bfile,useMAFencoding=True)
    iid  = data['iid']
    X = np.ascontiguousarray(data['snps'])
    N = X.shape[1]
    print(('%d variants loaded.'%N))
    print(('%d people loaded.'%X.shape[0]))
    """ normalizing markers """
    print('Normalizing SNPs...')
    p_ref = X.mean(axis=0)/2.
    X -= 2*p_ref

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        X /= sp.sqrt(2*p_ref*(1-p_ref))

    hasNan = sp.any(sp.isnan(X),axis=0)
    if sp.any(hasNan):
        print(('%d SNPs have a nan entry. Exluding them for computing the covariance matrix.'%hasNan.sum()))

    """ computing covariance matrix """
    print('Computing relationship matrix...')
    K = sp.dot(X[:,~hasNan],X[:,~hasNan].T)
    K/= 1.*N
    print('Relationship matrix calculation complete')
    print(('Relationship matrix written to %s.cov.'%cfile))
    print(('IDs written to %s.cov.id.'%cfile))

    """ saving to output """
    np.savetxt(cfile + '.cov', K, delimiter='\t',fmt='%.6f')
    np.savetxt(cfile + '.cov.id', iid, delimiter=' ',fmt='%s')