forked from MaselLab/MutationLoad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharedfunc_flag.c
188 lines (163 loc) · 8.1 KB
/
sharedfunc_flag.c
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
#include <stdio.h>
#include <float.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <gsl/gsl_sf_gamma.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_fit.h>
#include <err.h>
#include "dependencies/pcg_basic.h"
#include "relative_functions.h"
#include "absolute_functions.h"
#include "sharedfunc_flag.h"
#include "main.h"
#include <tskit.h>
#include <tskit/tables.h>
#include <kastore.h>
#include <tskit/core.h>
#include <tskit/trees.h>
void MutateGamete(int tskitstatus, int isburninphaseover, tsk_table_collection_t * treesequencetablecollection, tsk_id_t * wholepopulationsitesarray, tsk_id_t childnode, int totaltimesteps, double currenttimestep, bool isabsolute, int totalindividualgenomelength, double *gamete, double mutationeffectsize)
{
tsk_id_t idofnewmutation;
int mutatedsite = DetermineMutationSite(totalindividualgenomelength/2);
if(isabsolute){
gamete[mutatedsite] += (mutationeffectsize);
}else{
if(mutationeffectsize < 0){
gamete[mutatedsite] += (1 + mutationeffectsize)/6.0;
}else{
gamete[mutatedsite] += 2.0*(1 + mutationeffectsize);
}
}
char derivedstate[400];
sprintf(derivedstate, "%.11f", mutationeffectsize);
if (tskitstatus != 0){
if(isabsolute){
if (isburninphaseover != 0){
idofnewmutation = tsk_mutation_table_add_row(&treesequencetablecollection->mutations, wholepopulationsitesarray[mutatedsite], childnode, TSK_NULL, ((double) totaltimesteps - currenttimestep), derivedstate, 12, NULL, 0);
check_tsk_error(idofnewmutation);
}
}else{
idofnewmutation = tsk_mutation_table_add_row(&treesequencetablecollection->mutations, wholepopulationsitesarray[mutatedsite], childnode, TSK_NULL, ((double) totaltimesteps - currenttimestep), derivedstate, 12, NULL, 0);
check_tsk_error(idofnewmutation);
}
}
}
double PerformDeath(bool isabsolute, int tskitstatus, int isburninphaseover, int maxPopSize, int *pPopSize, int victim, int deleteriousdistribution, long double *wholepopulationselectiontree, long double *wholepopulationwisarray, long double *wholepopulationdeathratesarray, int *wholepopulationindex, bool *wholepopulationisfree, long double *psumofloads, long double *psumofdeathrates, long double *psumofdeathratessquared, double b_0, double r, int i_init, double s, long double *psumofload, long double *psumofloadsquared, tsk_id_t * wholepopulationnodesarray, FILE *miscfilepointer)
{
int placeinindex;
long double individualload;
if(isabsolute){
if(wholepopulationisfree[victim]){
fprintf(miscfilepointer, "\n Array of free indexes is corrupted in PerformDeath. \n");
exit(0);
}
*psumofdeathrates -= wholepopulationdeathratesarray[victim];
*psumofdeathratessquared -= pow(wholepopulationdeathratesarray[victim], 2);
if(r == 1.0){
individualload = i_init - (b_0 - wholepopulationdeathratesarray[victim])/s;
}
else{
if(deleteriousdistribution == 0){
individualload = i_init - log(1 - ((b_0 - wholepopulationdeathratesarray[victim])*(1 - r))/s)/log(r);
}
else if(deleteriousdistribution == 1)
individualload = i_init - log(r + ((b_0 - wholepopulationdeathratesarray[victim])*r*log(r))/s)/log(r);
}
*psumofload -= individualload;
*psumofloadsquared -= pow(individualload, 2);
wholepopulationisfree[victim] = true;
wholepopulationdeathratesarray[victim] = 0.0;
if (tskitstatus != 0){
if (isabsolute)
{
if (isburninphaseover !=0){
wholepopulationnodesarray[victim*2] = TSK_NULL;
wholepopulationnodesarray[victim*2 + 1] = TSK_NULL;
}
}
}
//Joseph way of doing the tree just with popsize is better, once this is working change it
placeinindex = findinindex(wholepopulationindex, victim, *pPopSize, miscfilepointer);
indexArrayFlipDeath(wholepopulationindex, placeinindex, *pPopSize);
*pPopSize -= 1;
}
else{
*psumofloads -= wholepopulationwisarray[victim];
wholepopulationwisarray[victim] = 0.0;
}
Fen_set(wholepopulationselectiontree, maxPopSize, 0.0, victim);
}
void PerformBirth(int tskitstatus, int isburninphaseover, bool ismodular, int elementsperlb, tsk_table_collection_t * treesequencetablecollection, tsk_id_t * wholepopulationnodesarray, tsk_id_t childnode1, tsk_id_t childnode2, bool isabsolute, double *parent1gamete, double *parent2gamete, int maxPopSize, int *pPopSize, int birthplace, double *wholepopulationgenomes, int totalindividualgenomelength, int deleteriousdistribution, long double *wholepopulationselectiontree, long double *wholepopulationwisarray, long double *wholepopulationdeathratesarray, int *wholepopulationindex, bool *wholepopulationisfree, long double *psumofloads, long double *psumofdeathrates, long double *psumofdeathratessquared, double b_0, double r, int i_init, double s, long double *psumofload, long double *psumofloadsquared, FILE *miscfilepointer)
{
int i;
long double newwi;
long double inddeathrate, individualload;
bool placefound = false;
if(isabsolute){
if(*pPopSize >= maxPopSize){
fprintf(miscfilepointer, "\n Trying to use PerformBirth with a population size greater than maxPopSize. \n");
exit(0);
}
birthplace = wholepopulationindex[*pPopSize];
if(!wholepopulationisfree[birthplace]){
printf("wholepopulationisfree is corrupted in PerformBirth \n");
exit(0);
}
inddeathrate = (long double) CalculateDeathRate(ismodular, elementsperlb, parent1gamete, parent2gamete, totalindividualgenomelength, deleteriousdistribution, b_0, r, i_init, s);
if(inddeathrate < 0.0){
fprintf(miscfilepointer, "\n The individual death rate of a new born is less than 0.0 (he is even more than immortal). \n");
exit(0);
}
}
else{
newwi = (long double) CalculateWidominance(parent1gamete, parent2gamete, totalindividualgenomelength);
}
for (i = 0; i < (totalindividualgenomelength/2); i++) {
wholepopulationgenomes[birthplace*totalindividualgenomelength + i] = parent1gamete[i];
wholepopulationgenomes[birthplace*totalindividualgenomelength + totalindividualgenomelength/2 + i] = parent2gamete[i];
}
if(isabsolute){
Fen_set(wholepopulationselectiontree, maxPopSize, inddeathrate, birthplace);
wholepopulationdeathratesarray[birthplace] = inddeathrate;
wholepopulationisfree[birthplace] = false;
*psumofdeathrates += inddeathrate;
*psumofdeathratessquared += pow(inddeathrate, 2);
if(r == 1.0){
individualload = i_init - (b_0 - inddeathrate)/s;
}
else{
if(deleteriousdistribution == 0){
individualload = i_init - log(1 - ((b_0 - inddeathrate)*(1 - r))/s)/log(r);
}
else if(deleteriousdistribution == 1)
individualload = i_init - log(r + ((b_0 - inddeathrate)*r*log(r))/s)/log(r);
}
*psumofload += individualload;
*psumofloadsquared += pow(individualload, 2);
*pPopSize += 1;
}
else{
Fen_set(wholepopulationselectiontree, maxPopSize, newwi, birthplace);
wholepopulationwisarray[birthplace] = newwi;
*psumofloads += newwi;
}
if (tskitstatus != 0){
if (isabsolute)
{
if (isburninphaseover !=0){
wholepopulationnodesarray[birthplace*2] = childnode1;
wholepopulationnodesarray[birthplace*2 + 1] = childnode2;
}
}
else{
wholepopulationnodesarray[birthplace*2] = childnode1;
wholepopulationnodesarray[birthplace*2 + 1] = childnode2;
}
}
}