-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtRNA.cpp
More file actions
434 lines (377 loc) · 21.2 KB
/
tRNA.cpp
File metadata and controls
434 lines (377 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
Need to rework a little for memmory efficiency
>can stop storing knockout, just draw on the fly during fitness calcs
>store all of the tRNAs w/ pointers and/or ints pointing towards vector positions
> create tRNAs with an additional non-functional version one up on the list/vector
*/
/// standard headers
#include <string>
#include <string.h>
#include <time.h>
#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <math.h>
#include <map>
#include <set>
#include <utility>
#include <list>
#include <random>
#include <iterator>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_rng.h>
using namespace std ;
/// declare our gsl
const gsl_rng *rng ;
/// our headers
#include "cmd_line.h"
#include "trna.h"
#include "individual.h"
#include "initialize_population.h"
#include "reduce_ne.h"
#include "assign_genotype.h"
#include "duplication.h"
#include "mutate.h"
#include "fitness.h"
#include "gene_conversion.h"
#include "reproduce.h"
#include "stats.h"
#include "sample.h"
#include "final_stats.h"
#include "transition.h"
#include "final_vectors.h"
/// main
int main ( int argc, char **argv ) {
//// for calculating runtime of code
clock_t tStart = clock() ;
/// trna counter to give new name to every trna that arises
int trna_counter = 0 ;
//// read command line options
cmd_line options ;
options.read_cmd_line( argc, argv ) ;
// initialize rng for gsl lookup table
rng = gsl_rng_alloc( gsl_rng_taus2 ) ;
gsl_rng_set( rng, (long) options.seed ) ;
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////// MUTATION PATHWAYS //////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
/**
In this section:
- if dual_rates == FALSE and mutation_pathways == FALSE:
-- each SNP incurred by a tRNA gene will result in a change to its fitness
-- the resulting fitness is drawn from the functionDists folder, where each numbered
file represents the fitness of a yeast tRNA that has incurred that number of SNPs
- if dual_rates == FALSE and mutation_pathways == TRUE:
-- each SNP incurred by a tRNA gene will result in a change to its fitness, stored
as its genotype in a string
-- the fitness associated with that string is drawn from yeastGenotypePathways.txt,
which contains the actual SNPs incurred by the tRNA genes in the same experiment.
-- going forward, a tRNA can only get mutations for which there is a fitness associated
with the resulting genotype.
-- for example, if the first mutation is A1, from there it can only become A1-G4,
A1-A10, etc..
- if dual_rates == true, we are using either the gamma model or the model4 model
*/
// for random fitness drawing:
std::map<int, vector<double>> mutations_to_function ;
// for pathway-specific fitness drawing:
std::map<string, double> genotype_to_fitness ;
std::map<string, vector<string>> genotype_to_genotypes ;
std::map<string, vector<double>> genotype_to_fitnesses ;
// load in different distributions of functions of tRNAs with given number of mutations:
if ( options.dual_rates == false ){
if ( options.mutation_pathways == false ){
for ( int i = 1 ; i < 11 ; ++i ){
std::string myNum = std::to_string(i) ;
std::ifstream is(options.path + "functionDists/functionDists"+myNum+".txt") ;
std::istream_iterator<double> start(is), end ;
std::vector<double> mutation_penalties(start, end) ;
mutations_to_function[i] = mutation_penalties ;
}
}
// assign each SNP to associated sequence score
else {
ifstream input(options.path + "functionDists/yeastGenotypePathways.txt") ;
char const row_delim = '\n' ;
char const field_delim = '\t' ;
char const entry_delim = ',' ;
for (string row; getline(input, row, row_delim); ) {
istringstream iss(row) ;
vector<string> tokens{istream_iterator<string>{iss}, istream_iterator<string>{}} ;
// get genotype
string genotype ;
std::istringstream line_stream(row) ;
line_stream >> genotype ;
// if mutation chain ends here, add fitness of genotype + 0 as only option after
if ( tokens.size() == 2 ) {
double fitness ;
while (line_stream >> fitness) {
genotype_to_fitness[genotype] = fitness ;
vector<string> genotypes ;
genotypes.push_back( "x" );
vector<double> fitnesses ;
fitnesses.push_back( 0.0 ) ;
genotype_to_genotypes[genotype] = genotypes ;
genotype_to_fitnesses[genotype] = fitnesses ;
}
}
// if mutation chain keeps going, add index 1 as fitness
// then create vectors of posssible options for future genotypes and fitnesses
else if ( tokens.size() > 2 ){
string temp_fitness = tokens.at( 1 ) ;
double fitness = std::stod( temp_fitness ) ;
genotype_to_fitness[genotype] = fitness ;
vector<string> genotypes ;
vector<double> fitnesses ;
string temp_genotypes = tokens.at( 2 ) ;
string temp_fitnesses = tokens.at( 3 ) ;
int g_count = std::count(temp_genotypes.begin(), temp_genotypes.end(), ',') ;
int f_count = std::count(temp_fitnesses.begin(), temp_fitnesses.end(), ',') ;
// if only one option, create empty vector and add them in
if ( g_count == 0 ){
genotypes.push_back( temp_genotypes ) ;
double temp_f = std::stod( temp_fitnesses ) ;
fitnesses.push_back( temp_f ) ;
genotype_to_genotypes[genotype] = genotypes ;
genotype_to_fitnesses[genotype] = fitnesses ;
}
// if multiple options, split strings by comma and add all elements to vectors
else {
istringstream gss(temp_genotypes) ;
for (string temp_g; getline(gss, temp_g, entry_delim); ) {
genotypes.push_back( temp_g ) ;
}
istringstream fss(temp_fitnesses) ;
for (string temp_f; getline(fss, temp_f, entry_delim); ) {
double temp_f_each = std::stod( temp_f ) ;
fitnesses.push_back( temp_f_each ) ;
}
genotype_to_genotypes[genotype] = genotypes ;
genotype_to_fitnesses[genotype] = fitnesses ;
}
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////// DEMOGRAPHY FILE ////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
/**
In this section, we read in a demography file.
-- These files are formatted as such:
node1 node2 branch_length ne1 ne2
-- all branches making up a tree must be present
-- there must also be a root as node1 for one branch
-- before the root is the burn-in phase
-- the branch lengths and effective population sizes may be scaled if the scaling_factor
flag is used.
**/
std::map<int, int> branch_to_length ;
std::map<int, string> branch_to_node1 ;
std::map<int, string> branch_to_node2 ;
std::map<string, int> node_to_Ne ;
std::map<string,vector<double>> node_to_final_active_loci ;
std::map<string,vector<double>> node_to_final_inactive_loci ;
std::map<string,map<string,int>> node_to_final_genotypes ;
std::map<string, vector<individual>> node_to_population ;
std::map<string, vector<gene*>> node_to_trna_bank ;
int last_branch = 0 ;
// add burn-in step, with Ne val from the root
branch_to_length[0] = options.burn_in ;
branch_to_node1[0] = "burn_in" ;
branch_to_node2[0] = "root" ;
if ( options.demography != "" ) {
ifstream file( options.demography , ios::in );
string node1, node2 ;
int branchLength, node1Ne, node2Ne ;
while ( file >> node1 >> node2 >> branchLength >> node1Ne >> node2Ne ) {
last_branch ++ ;
branch_to_length[last_branch] = branchLength / options.scaling_factor ;
branch_to_node1[last_branch] = node1 ;
branch_to_node2[last_branch] = node2 ;
node_to_Ne[node1] = node1Ne / options.scaling_factor ;
node_to_Ne[node2] = node2Ne / options.scaling_factor ;
// cout << last_branch << "\t" << branchLength << "\t" << node1 << "\t" << node2 << endl ;
// cout << last_branch << "\t" << branch_to_length[last_branch] << "\t" << node_to_Ne[node1] << "\t" << node_to_Ne[node2] << endl ;
}
file.close();
}
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// SIMULATION //////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
/**
If the sampling flag is used, after X generations, an XXX_sample.txt file will be written
with a sampling from the population at that specific time.
**/
// enable sampling
if ( options.sample == true ){
std::string sampling_out = std::to_string(options.run_num) + "_sample.txt" ;
ofstream stream( sampling_out ) ;
stream << "" ;
}
// optimal fitness based on inputs (for use only if fitness is gaussian)
double opt_fit = (1 / sqrt( 2 * 3.14159265358979323846 * pow(options.fitness_sd, 2) )) ;
/////////////////////////////////////////
/////////////////////////////////////////
/////// NOT USING DEMOGRAPHY FILE ///////
/////////////////////////////////////////
/////////////////////////////////////////
/**
If a demography file is not used, will simulate one continuous population, with
no changes to the population size, for the input number of generations. This is
the most straightforward use of the simulation framework.
The simulation main loop is as follows:
-- mutate (go through all genes in all individuals and add mutations)
-- gene_conversion (at user input rate, allow gene conversion within individuals)
-- compute_fitness (induce somatic mutations, and, based on user input function,
compute fitness according to one of several functions)
-- reproduce (using fitness including somatic mutations, as well as Haldane's map
function for recombination events, create next generation)
-- swap (built-in function to save new population)
-- print_stats (give the user )
**/
if ( options.demography == "" ) {
// evolve the population forward in time
// create population of size n with two tRNAs of equivalent function
vector<gene*> trna_bank ;
initialize_population ( options, trna_bank, trna_counter ) ;
/// map of tRNA lifespans to count number of tRNAs that lived that long
std::map<double, int> loci_to_lifespans ;
std::map<int, int> lifespan_to_count ;
// fitness vector
double fitness [options.n] ;
/// now copy to population of size n
vector<individual> population ( options.n ) ;
for ( int i = 0 ; i < population.size() ; ++i ) {
for ( auto t : trna_bank ) {
population[i].maternal_trnas.push_back( t ) ;
population[i].paternal_trnas.push_back( t ) ;
}
}
/// run simulation for g generationså
for ( int g = 1 ; g <= options.generations ; g ++ ) {
// for first generation give user a quick reminder of what they did:
if ( g == 1 ){
cout << "somatic = " << options.somatic_rate << ", germline = " << options.germline_rate << ", sdup = " << options.duplication_rate << ", del = " ;
cout << options.deletion_rate << ", fitness function = " << options.fitness_func << ", gene conversion rate = " << options.gene_conversion_rate << endl ;
}
/// vector to swap with
vector<individual> new_population ( options.n ) ;
mutate( population, options, trna_bank, g, trna_counter, mutations_to_function, genotype_to_fitness, genotype_to_genotypes, genotype_to_fitnesses ) ;
gene_conversion ( population, options, trna_bank, g, trna_counter ) ;
compute_fitness( fitness, population, mutations_to_function, genotype_to_fitness, genotype_to_genotypes, genotype_to_fitnesses, opt_fit, options ) ;
reproduce( fitness, population, new_population, options ) ;
swap( population, new_population ) ;
print_stats( fitness, population, g, options.generations, "default", trna_bank, loci_to_lifespans, lifespan_to_count, options ) ;
// cout << "TEST: " << options.sample_all << "\t" << g << "\t" << options.burn_in << "\t" << g % options.sampling_frequency << endl ;
if (( options.sample_all == true ) and ( g >= options.burn_in ) and ( g % options.sampling_frequency == 0 )){
std::string sampling_out = std::to_string(options.run_num) + "_sample_all.txt" ;
sample_all( g, population, sampling_out, options ) ;
}
else if (( options.sample == true ) and ( g >= options.burn_in ) and ( g % options.sampling_frequency == 0 )){
sample_individuals( g, population, options ) ;
}
}
}
/////////////////////////////////////
/////////////////////////////////////
/////// USING DEMOGRAPHY FILE ///////
/////////////////////////////////////
/////////////////////////////////////
/**
If a demography file IS used, we need to simulate each branch, and then store
the population at the end of each branch (so that if there are multiple branches
coming from a single node, we can simulate both from the same starting point).
For the burn-in, we start from default settings.
**/
else {
for ( int branch = 0 ; branch <= last_branch ; branch ++ ) {
vector<individual> population ( node_to_Ne[branch_to_node2[branch]] ) ;
double fitness [node_to_Ne[branch_to_node2[branch]]] ;
vector<gene*> trna_bank ;
/// map of tRNA lifespans to count number of tRNAs that lived that long
std::map<double, int> loci_to_lifespans ;
std::map<int, int> lifespan_to_count ;
// cout << "BRANCH: " << branch << ", length: " << branch_to_length[branch] << ", Ne: " << node_to_Ne[branch_to_node2[branch]] ;
// cout << ", node1: " << branch_to_node1[branch] << ", node2: " << branch_to_node2[branch] << endl ;
// if no saved population, start from default settings
if ( !node_to_population.count( branch_to_node1[branch] ) ) {
initialize_population ( options, trna_bank, trna_counter ) ;
for ( int i = 0 ; i < population.size() ; ++i ) {
for ( auto t : trna_bank ) {
population[i].maternal_trnas.push_back( t ) ;
population[i].paternal_trnas.push_back( t ) ;
}
}
}
// if population we already built, take it and then subsample to get one fitting the new ne
else {
// cout << "\n\n" << population.size() << "\n\n" ;
std::map<gene*, gene*> old_trna_to_new_trna ;
reduce_ne( options, node_to_population[branch_to_node1[branch]], population, node_to_trna_bank[branch_to_node1[branch]], trna_bank, old_trna_to_new_trna ) ;
}
cout << "length: " << branch_to_length[branch] << endl ;
// evolve the population forward in time
for ( int g = 1 ; g <= branch_to_length[branch] ; g ++ ) {
// for first generation give user a quick reminder of what they did:
if ( g == 1 ){
cout << "somatic = " << options.somatic_rate << ", germline = " << options.germline_rate << ", sdup = " << options.duplication_rate << ", del = " ;
cout << options.deletion_rate << ", fitness function = " << options.fitness_func << ", gene conversion rate = " << options.gene_conversion_rate << endl ;
}
/// vector to swap with
vector<individual> new_population ( population.size() ) ;
mutate( population, options, trna_bank, g, trna_counter, mutations_to_function, genotype_to_fitness, genotype_to_genotypes, genotype_to_fitnesses ) ;
gene_conversion ( population, options, trna_bank, g, trna_counter ) ;
compute_fitness( fitness, population, mutations_to_function, genotype_to_fitness, genotype_to_genotypes, genotype_to_fitnesses, opt_fit, options ) ;
reproduce( fitness, population, new_population, options ) ;
swap( population, new_population ) ;
print_stats( fitness, population, g, branch_to_length[branch], branch_to_node2[branch], trna_bank, loci_to_lifespans, lifespan_to_count, options ) ;
if (( options.sample_all == true ) and ( g % options.sampling_frequency == 0 )){
std::string sampling_out = std::to_string(options.run_num) + "_" + branch_to_node1[branch] + "_to_" + branch_to_node2[branch] + "_sample_all.txt" ;
sample_all( g, population, sampling_out, options ) ;
}
else if (( options.sample == true ) and ( g % options.sampling_frequency == 0 )){
sample_individuals( g, population, options ) ;
}
}
node_to_population[branch_to_node2[branch]] = population ;
cout << branch_to_node2[branch] << ", " << population.size() << endl ;
node_to_trna_bank[branch_to_node2[branch]] = trna_bank ;
// std::string sampling_out = std::to_string(options.run_num) + "_" + branch_to_node1[branch] + "_to_" + branch_to_node2[branch] + "_final_population.txt" ;
// get_final_stats( population, sampling_out, options ) ;
update_found( population, branch_to_node2[branch], node_to_final_active_loci, node_to_final_inactive_loci, node_to_final_genotypes, options ) ;
}
cout << "running final vectors" << endl ;
std::string vector_out = std::to_string(options.run_num) + "_final_vector.txt" ;
final_vectors( node_to_final_active_loci, node_to_final_inactive_loci, node_to_final_genotypes, node_to_Ne, vector_out, options ) ;
}
printf("Total time: %.2f seconds. ", (double)(clock() - tStart)/CLOCKS_PER_SEC) ;
if ( options.demography == "" ){
cout << "Total generations: " << options.generations << "." ;
}
cout << "\n" ;
return(0) ;
}