8 #ifndef INLINE_GOFISH_DATA_FUNCTIONS_HPP_ 9 #define INLINE_GOFISH_DATA_FUNCTIONS_HPP_ 14 inline mutID::mutID() : origin_generation(0), origin_population(0), origin_threadID(0), reserved(0) {}
20 std::string tostring(
const T& value)
22 std::ostringstream oss;
33 inline allele_trajectories::sim_constants::sim_constants(): seed1(0xbeeff00d), seed2(0xdecafbad), num_generations(0), num_sites(1000), num_populations(1), init_mse(
true), prev_sim_sample(-1), compact_interval(35), device(-1) {}
35 inline allele_trajectories::time_sample::time_sample(): num_mutations(0), sampled_generation(0) { mutations_freq = NULL; extinct = NULL; Nchrom_e = NULL; }
36 inline allele_trajectories::time_sample::~time_sample(){
37 if(mutations_freq){
delete [] mutations_freq; mutations_freq = NULL; }
38 if(extinct){
delete [] extinct; extinct = NULL; }
39 if(Nchrom_e){
delete [] Nchrom_e; Nchrom_e = NULL; }
47 sim_run_constants = in.sim_run_constants;
48 num_samples = in.num_samples;
49 all_mutations = in.all_mutations;
51 if(all_mutations > 0){
52 mutations_ID = (
mutID *)malloc(all_mutations*
sizeof(
mutID));
53 std::memcpy(mutations_ID, in.mutations_ID, all_mutations*
sizeof(
mutID));
55 else{ mutations_ID = NULL; }
58 time_samples =
new time_sample*[num_samples];
59 for(
int i = 0; i < num_samples; i++){
60 time_samples[i] =
new time_sample;
61 int num_mutations = in.time_samples[i]->num_mutations;
62 time_samples[i]->num_mutations = num_mutations;
63 time_samples[i]->sampled_generation = in.time_samples[i]->sampled_generation;
68 time_samples[i]->Nchrom_e[j] = in.time_samples[i]->Nchrom_e[j];
69 time_samples[i]->extinct[j] = in.time_samples[i]->extinct[j];
71 time_samples[i]->mutations_freq =
new float[num_populations*num_mutations];
72 std::memcpy(time_samples[i]->mutations_freq,in.time_samples[i]->mutations_freq,num_populations*num_mutations*
sizeof(
float));
75 else{ time_samples = NULL; }
94 if(!time_samples || num_samples == 0){ fprintf(stderr,
"num_mutations error: empty allele_trajectories\n"); exit(1); }
95 if(sample_index < 0 || sample_index > num_samples){ fprintf(stderr,
"num_mutations error: requested sample index out of bounds: sample %d [0 %d)\n",sample_index,num_samples); exit(1); }
96 return time_samples[sample_index]->num_mutations;
102 if(!time_samples || num_samples == 0){ fprintf(stderr,
"sampled_generation error: empty allele_trajectories\n"); exit(1); }
103 if(sample_index < 0 || sample_index > num_samples){ fprintf(stderr,
"sampled_generation error: requested sample index out of bounds: sample %d [0 %d)\n",sample_index,num_samples); exit(1); }
104 return time_samples[sample_index]->sampled_generation;
108 if(!time_samples || num_samples == 0){ fprintf(stderr,
"extinct error: empty allele_trajectories\n"); exit(1); }
110 if((sample_index < 0 || sample_index >= num_samples) || (population_index < 0 || population_index >= num_populations)){ fprintf(stderr,
"extinct error: index out of bounds: sample %d [0 %d), population %d [0 %d)\n",sample_index,num_samples,population_index,num_populations); exit(1); }
111 return time_samples[sample_index]->extinct[population_index];
115 if(!time_samples || num_samples == 0){ fprintf(stderr,
"effective_number_of_chromosomes error: empty allele_trajectories\n"); exit(1); }
117 if((sample_index < 0 || sample_index >= num_samples) || (population_index < 0 || population_index >= num_populations)){ fprintf(stderr,
"effective_number_of_chromosomes error: index out of bounds: sample %d [0 %d), population %d [0 %d)\n",sample_index,num_samples,population_index,num_populations); exit(1); }
118 return time_samples[sample_index]->Nchrom_e[population_index];
125 if((sample_index >= 0 && sample_index < num_samples) && (population_index >= 0 && population_index < num_populations) && (mutation_index >= 0 && mutation_index < time_samples[num_samples-1]->num_mutations)){
126 num_mutations = time_samples[num_samples-1]->num_mutations;
127 int num_mutations_in_sample = time_samples[sample_index]->num_mutations;
128 if(mutation_index >= num_mutations_in_sample){
return 0; }
129 return time_samples[sample_index]->mutations_freq[mutation_index+population_index*num_mutations_in_sample];
132 if(!time_samples || num_samples == 0){ fprintf(stderr,
"frequency error: empty allele_trajectories\n"); exit(1); }
133 num_mutations = time_samples[num_samples-1]->num_mutations;
134 fprintf(stderr,
"frequency error: index out of bounds: sample %d [0 %d), population %d [0 %d), mutation %d [0 %d)\n",sample_index,num_samples,population_index,num_populations,mutation_index,num_mutations); exit(1);
139 if(num_samples > 0 && time_samples){
140 if(mutation_index >= 0 && mutation_index < all_mutations){
return mutID(mutations_ID[mutation_index].origin_generation,mutations_ID[mutation_index].origin_population,abs(mutations_ID[mutation_index].origin_threadID),mutations_ID[mutation_index].reserved); }
141 fprintf(stderr,
"mutation_ID error: requested mutation index out of bounds: mutation %d [0 %d)\n",mutation_index,
maximal_num_mutations()); exit(1);
142 }
else{ fprintf(stderr,
"mutation_ID error: empty allele_trajectories\n"); exit(1); }
153 if(sample_index >= 0 && sample_index < num_samples){
154 if(num_samples == 1){
reset(); }
156 delete time_samples[sample_index];
157 time_sample ** temp =
new time_sample * [num_samples-1];
158 for(
int i = 0; i < num_samples; i++){
159 if(i < sample_index){ temp[i] = time_samples[i]; }
160 else if (i > sample_index){ temp[i-1] = time_samples[i]; }
162 delete [] time_samples;
165 all_mutations = time_samples[num_samples-1]->num_mutations;
168 if(!time_samples || num_samples == 0){ fprintf(stderr,
"delete_time_sample error: empty allele_trajectories\n"); exit(1); }
169 fprintf(stderr,
"delete_time_sample error: requested sample index out of bounds: sample %d [0 %d)\n",sample_index,num_samples); exit(1);
176 for(
int i = 0; i < num_samples; i++){
delete time_samples[i]; }
177 delete [] time_samples;
179 if(mutations_ID){ free(mutations_ID); }
180 time_samples = NULL; num_samples = 0; mutations_ID = NULL; all_mutations = 0;
187 inline void allele_trajectories::initialize_sim_result_vector(
int new_length){
190 num_samples = new_length;
191 time_samples =
new time_sample *[num_samples];
192 for(
int i = 0; i < num_samples; i++){ time_samples[i] =
new time_sample(); }
198 inline std::ostream &
operator<<(std::ostream & stream,
const mutID &
id){ stream <<
id.toString();
return stream; }
209 stream <<
"seed1" <<
"\t" << A.sim_run_constants.
seed1 << std::endl;
210 stream <<
"seed2" <<
"\t" << A.sim_run_constants.
seed2 << std::endl;
211 stream <<
"num_generations" <<
"\t" << A.sim_run_constants.
num_generations << std::endl;
212 stream <<
"num_sites" <<
"\t" << A.sim_run_constants.
num_sites << std::endl;
213 stream <<
"num_populations" <<
"\t" << A.sim_run_constants.
num_populations << std::endl;
214 stream <<
"init_mse" <<
"\t" << A.sim_run_constants.
init_mse << std::endl;
215 stream <<
"prev_sim_sample" <<
"\t" << A.sim_run_constants.
prev_sim_sample << std::endl;
216 stream <<
"compact_interval" <<
"\t" << A.sim_run_constants.
compact_interval << std::endl;
217 stream <<
"device" <<
"\t" << A.sim_run_constants.
device << std::endl << std::endl;
219 if(A.num_samples == 0){ stream <<
"no simulation stored" << std::endl;
return stream; }
223 stream <<
"time sample:";
224 for(
int j = 0; j < A.num_samples; j++){
226 for(
int k = 0; k < num_populations-1; k++){ stream <<
"\t"; }
227 } stream << std::endl;
229 stream <<
"generation:";
230 for(
int j = 0; j < A.num_samples; j++){
232 for(
int k = 0; k < num_populations-1; k++){ stream <<
"\t"; }
233 } stream << std::endl <<
"number of mutations reported:";
235 for(
int j = 0; j < A.num_samples; j++){
237 for(
int k = 0; k < num_populations-1; k++){ stream <<
"\t"; }
238 } stream << std::endl <<
"population:" ;
240 for(
int j = 0; j < A.num_samples; j++){
242 } stream << std::endl <<
"effective population size (chromosomes):";
244 for(
int j = 0; j < A.num_samples; j++){
246 } stream << std::endl <<
"population extinct:";
248 for(
int j = 0; j < A.num_samples; j++){
250 } stream << std::endl;
252 if(A.all_mutations == 0){ stream << std::endl <<
"no mutations stored" << std::endl;
return stream; }
254 stream <<
"mutation ID (origin_generation,origin_population,origin_threadID,reserved)";
255 for(
int j = 0; j < A.num_samples; j++){
for(
int k = 0; k <
num_populations; k++){ stream <<
"\t" <<
"frequency"; } }
258 for(
int i = 0; i < A.all_mutations; i++) {
260 for(
int j = 0; j < A.num_samples; j++){
274 temp = a.sim_run_constants;
275 a.sim_run_constants = b.sim_run_constants;
276 b.sim_run_constants = temp;
277 std::swap(a.all_mutations,b.all_mutations);
279 std::swap(a.mutations_ID,b.mutations_ID);
280 std::swap(a.time_samples,b.time_samples);
int prev_sim_sample
time sample of previous simulation to use for initializing current simulation
int origin_generation
generation in which mutation appeared in simulation
int seed2
random number seed 2 of 2
control and output data structure for GO_Fish simulation
int compact_interval
how often to compact the simulation and remove fixed or lost mutations
int effective_number_of_chromosomes(int sample_index, int population_index)
returns the effective number of chromosomes of population population_index in time sample sample_inde...
allele_trajectories & operator=(allele_trajectories in)
copy assignment
int final_generation()
returns final generation of simulation
int num_populations()
returns the number of populations in the simulation
bool extinct(int sample_index, int population_index)
returns whether or not population population_index is extinct in time sample sample_index ...
specification of simulation constants
int num_populations
number of populations in simulation
void reset()
deletes all memory held by allele_trajectories, resets constants to default
int maximal_num_mutations()
returns number of reported mutations in the final time sample (maximal number of stored mutations in ...
int origin_threadID
threadID that generated mutation
int num_time_samples()
returns number of time samples taken during simulation run
int seed1
random number seed 1 of 2
structure specifying the ID for a mutation in a GO_Fish simulation
int num_mutations_time_sample(int sample_index)
number of reported mutations in the time sample sample_index
void swap(allele_trajectories &a, allele_trajectories &b)
swaps data held by allele_trajectories a and b
int origin_population
population in which mutation first arose
void delete_time_sample(int sample_index)
deletes a single time sample, sample_index
float num_sites
number of sites in simulation
std::ostream & operator<<(std::ostream &stream, const mutID &id)
insertion operator: sends mutID id into the ostream stream
bool init_mse
true: initialize simulation in mutation_selection_equilibrium; false: initialize blank simulation or ...
int device
GPU identity to run simulation on, if -1 next available GPU will be assigned.
int num_sites()
returns the number of sites in the simulation
mutID()
default constructor
Namespace for single-locus, forward, Monte-Carlo Wright-Fisher simulation and output data structures...
sim_constants sim_input_constants
constants for initializing the next simulation
~allele_trajectories()
destructor
int reserved
reserved for later use, currently 0
float frequency(int sample_index, int population_index, int mutation_index)
returns the frequency of the mutation at time sample sample_index, population population_index, mutation mutation_index
allele_trajectories()
default constructor
mutID mutation_ID(int mutation_index)
returns the mutation ID at mutation_index
int sampled_generation(int sample_index)
return generation of simulation in the time sample sample_index
std::string toString()
returns a string constant of mutID
sim_constants last_run_constants()
returns sim_constants of the simulation currently held by allele_trajectories
int num_generations
number of generations in simulation