int hivpopulation::write_genotypes(ostream &out, int sample_size, string gt_label, int start, int length){ if (HIVPOP_VERBOSE) cerr<<"hivpopulation::write_genotypes()..."; if (HIVPOP_VERBOSE) cerr<<"start = "<<start<<"..."; if (HIVPOP_VERBOSE) cerr<<"length = "<<length<<"..."; if (out.bad()){ cerr<<"hivpopulation::write_genotypes(): BAD OUTPUT FILE!"<<endl; return HIVPOP_BADARG; }else{ int gti; string temp; if (length <= 0) length = number_of_loci - start; produce_random_sample(sample_size); if (sample_size>get_population_size()){ cerr<<"hivpopulation::write_genotypes(): requested sample size exceeds population size"<<endl; return HIVPOP_BADARG; }else{ for (int s=0; s<sample_size; s++){ gti=random_clone(); out <<">GT-"<<gt_label<<"_"<<gti<<'\n'; for (int i =start; i<start+length; i++ ){ if (population[gti].genotype[i]) out <<'1'; else out <<'0'; } out<<'\n'; } } if (HIVPOP_VERBOSE) cerr<<"...done."<<endl; return 0; } }
/** * Computing the Fisher's Exact Test (FET) and the corresponding standard deviation for a given thread. * @param threadarg: the arguments to the thread: a thread_data struct */ void mycompute(void *threadarg) { struct thread_data *my_data; int tid; int start, stop, regend, num_windows, wsize, wstep, alen, blen, asize, bsize, totalpos, npos, nsamples, my_task_id = 0; double perc; int *apos; int *bpos; double *avals; double *bvals; double *scores; double *stddev; /* fetch data from threadarg */ my_data = (struct thread_data *) threadarg; tid = my_data->thread_id; regend = my_data->regend; num_windows = my_data->num_windows; wsize = my_data->wsize; wstep = my_data->wstep; apos = my_data->apos; bpos = my_data->bpos; avals = my_data->avals; bvals = my_data->bvals; alen = my_data->alen; blen = my_data->blen; perc = my_data->perc; scores = my_data->scores; stddev = my_data->stddev; int wcount = 0; int wstart; int wstop; int *aidx; int *bidx; int *f; int *tmp; double *results; double *fetscores = NULL; double *samples = NULL; double *stdsamples; /* get size of population a and b*/ asize = get_population_size(apos); bsize = get_population_size(bpos); wcount = 0; start = 0; stop = wsize; nsamples = 100; /* setup idx arrays */ aidx = (int*)malloc(2*sizeof(int)); bidx = (int*)malloc(2*sizeof(int)); /* setup arrays for calc. of FET */ f = (int*)malloc(4*sizeof(int)); tmp = (int*)malloc(4*sizeof(int)); results = (double*)malloc(2*sizeof(double)); stdsamples = (double*)malloc(nsamples*sizeof(double)); int idx = 0; /* generate seed for the PRNG */ unsigned short state[3] = {0,0,0}; unsigned short seed = time(NULL) + (unsigned short) pthread_self(); memcpy(state, &seed, sizeof(seed)); // fetch new tasks and calculate FET while (my_task_id < num_tasks) { // try to fetch new task pthread_mutex_lock (&mutexTASK_ID); my_task_id= task_id; task_id++; pthread_mutex_unlock(&mutexTASK_ID); // if no more tasks, break if (my_task_id > num_tasks) break; // get chromosome position of the current task get_positions(wsize, wstep, my_task_id, &start, &stop, regend); // to include the last (possibly smaller) window if (stop >= regend) { stop = regend + wstep; } wstart = start; wstop = start+wsize; aidx[0] = 0; aidx[1] = 0; bidx[0] = 0; bidx[1] = 0; // calculate FET for each window while (wstart + wsize <= stop) { slide_right(aidx, apos, wstart, wstop, alen); slide_right(bidx, bpos, wstart, wstop, blen); npos = (aidx[1] - aidx[0])/asize; if (npos > 0) { fetscores = (double*)realloc(fetscores, npos*sizeof(double)); samples = (double*)realloc(samples, npos*sizeof(double)); idx = wstart/wstep; fisher_exact_test(results, &(avals[aidx[0]]), &(bvals[bidx[0]]), asize, bsize, npos, f, tmp, samples, stdsamples, nsamples, fetscores, state, perc); scores[idx] = results[0]; stddev[idx] = results[1]; } wstart += wstep; wstop += wstep; wcount++; } } /* cleanup */ free(aidx); free(bidx); free(f); free(tmp); free(results); free(samples); free(stdsamples); free(fetscores); }