#include "genotype.h" Genotype::Genotype(int *input_code, const map & hap_list, int size){ if(input_code==0 || size==0) return; this->size = size; // # of SNPs gcode = new int[size]; memset(gcode,0,size*sizeof(int)); int flag = 0; // if there is missing genotype at each locus for(int i=0;i(); geno_code->push_back(gcode); } this->weighted_count=0; this->count=0; this->case_num=0; this->control_num=0; record_hap_structure(hap_list); } vector * Genotype::complete_genotype(int index){ int code = gcode[index]; if(index==size-1){ vector *vec = new vector(); if(code==0 || code==1 || code==2){ int *a1 = new int[size]; memset(a1,0,size*sizeof(int)); a1[size-1] = code; vec->push_back(a1); } else if(code==-1){ int *a1 = new int[size]; int *a2 = new int[size]; int *a3 = new int[size]; memset(a1,0,size*sizeof(int)); memset(a2,0,size*sizeof(int)); memset(a3,0,size*sizeof(int)); a1[size-1]=0; a2[size-1]=1; a3[size-1]=2; vec->push_back(a1); vec->push_back(a2); vec->push_back(a3); } return vec; } vector *vec = complete_genotype(index+1); int pos = index; if(code==0 || code==1 || code==2){ for(int i=0;isize();i++) (*vec)[i][pos] = code; return vec; } // else if code == -1 int vsize = vec->size(); for(int i=0;ipush_back(a1); vec->push_back(a2); } return vec; } void Genotype::show_gstat(){ // print gcode with its count for(int i=0;isize();i++){ for(int j=0;j>j; // get the jth last bit printf("%d", b1); if(j!=size-1) printf("-"); else printf("\n"); } int h2 = hap_vec[i].second; for(int j=0;j>j; printf("%d", b2); if(j!=size-1) printf("-"); else printf("\n"); } printf("\n"); } } vector * Genotype::hap_decompose(int *geno, int index, int &bit){ // enumerate all possible haplotype pairs int code = geno[index]; if(index==size-1){ vector *vec = new vector(); int *a1 = new int[size]; int *a2 = new int[size]; memset(a1,0,size*sizeof(int)); memset(a2,0,size*sizeof(int)); if(code==0) a1[size-1]=a2[size-1]=0; else if(code==1){ a1[size-1]=0; a2[size-1]=1; bit = 1; // record heterozygous sites } else if(code==2) a1[size-1]=a2[size-1]=1; vec->push_back(a1); vec->push_back(a2); return vec; } vector *vec = hap_decompose(geno,index+1,bit); int pos = index; // first heterozygous genotype, no permutation, reset bit flag if(code==1 && bit==0){ for(int i=0;isize();i++) (*vec)[i][pos] = i%2; bit = 1; return vec; } if(code==0 || code==2){ for(int i=0;isize();i++) (*vec)[i][pos] = code/2; return vec; } // else if code == 1 and not the first heterozygous site int vsize = vec->size(); for(int i=0;ipush_back(a1); vec->push_back(a2); } return vec; } void Genotype::record_hap_structure(const map & hap_list){ int tot = 1<size();k++){ int bit=0; vector *rst = hap_decompose((*geno_code)[k],0,bit); for(int i=0;isize();i+=2){ int h0=0; int h1=0; for(int j=0;j(min,max)); // record into hap_vec } // clean up the container rst->clear(); delete rst; } if(hap_vec.size()==0 || hap_vec.size()==tot*(tot+1)/2) info = 0; else info = 1; } else{ for(int k=0;ksize();k++){ int bit=0; vector *rst = hap_decompose((*geno_code)[k],0,bit); for(int i=0;isize();i+=2){ int h0=0; int h1=0; for(int j=0;j(min,max)); // record into hap_vec } // clean up the container rst->clear(); delete rst; } if(hap_vec.size()==0 || hap_vec.size()==hap_list.size()*(hap_list.size()+1)/2) info = 0; else info = 1; } } void Genotype::expected_hap_num(double *freq, map, double> & geno_freq_table, map, int> & hap_pair_map, int **count, const vector & sites){ hap_num.clear(); int loc; if(hap_vec.size()==1){ loc = hap_pair_map[hap_vec[0]]; for(int j=0;j1){ for(int j=0;j0) hap_num[k]+=count[sites[k]][loc]*geno_freq_table[hap_vec[j]]; } } for(int j=0;j, double> & geno_freq_table){ prob = 0; if(hap_vec.size()==1) prob = geno_freq_table[hap_vec[0]]; else if(hap_vec.size()>1){ for(int j=0;j & hap_freq_table){ prob_IBD1 = 0; if(hap_vec.size()==1) prob_IBD1 = expfortwo(hap_vec[0],hap_freq_table); else if(hap_vec.size()>1){ for(int i=0;isize()>1) delete[] gcode; for(int k=0;ksize();k++) delete[] (*geno_code)[k]; geno_code->clear(); delete geno_code; hap_vec.clear(); hap_num.clear(); } void translate_hap(FILE *fp, int code, int size, vector & a0v, vector & a1v){ for(int j=0;j>j; if(b1==0) fprintf(fp,"%c", a0v[j]); else if(b1==1) fprintf(fp,"%c", a1v[j]); if(j!=size-1) fprintf(fp,"-"); } } void translate_hap(FILE *fp, int code, int size){ for(int j=0;j>j; fprintf(fp, "%d", b1); if(j!=size-1) fprintf(fp,"-"); } }