-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.hpp
More file actions
256 lines (247 loc) · 7.15 KB
/
Copy pathutil.hpp
File metadata and controls
256 lines (247 loc) · 7.15 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
#ifndef _UTIL_HPP_
#define _UTIL_HPP_
#include <isbinary.hpp>
#include <forward_types.hpp>
#include <set>
#include <map>
#include <algorithm>
#include <cassert>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <boost/static_assert.hpp>
#include <boost/type_traits/is_base_and_derived.hpp>
#include <boost/type_traits/is_convertible.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/device/file.hpp>
#include <iostream>
#include <Sequence/SimData.hpp>
template< typename gamete_type,
typename vector_allocator_type,
typename mutation_type,
typename list_allocator_type,
template<typename,typename> class vector_type,
template<typename,typename> class list_type >
void valid_copy( const vector_type<gamete_type,vector_allocator_type> & gametes,
const list_type<mutation_type,list_allocator_type> & mutations,
vector_type<gamete_type,vector_allocator_type> & gametes_destination,
list_type<mutation_type,list_allocator_type> & mutations_destination )
/*!
If you ever need to store (and later restore) the state of the population, a naive copy operation
is not sufficient, because of all the pointers from the gametes container to elements
of the mutations container. Use this function instead.
*/
{
BOOST_STATIC_ASSERT( (boost::is_same<typename gamete_type::mutation_type,mutation_type>::value) );
typedef typename list_type<mutation_type,list_allocator_type>::iterator literator;
typedef typename list_type<mutation_type,list_allocator_type>::const_iterator cliterator;
typedef typename gamete_type::mutation_container::const_iterator gciterator;
gametes_destination.clear();
mutations_destination.clear();
//copying the mutations is trivial
std::map<double,literator> mutlookup;
for( cliterator i = mutations.begin();
i!=mutations.end();++i)
{
literator j = mutations_destination.insert(mutations_destination.end(),*i);
mutlookup[j->pos]=j;
}
/*
std::copy(mutations.begin(),mutations.end(),
std::back_inserter(mutations_destination));
*/
for(unsigned i=0;i<gametes.size();++i)
{
//copy construct so that all public, etc., data
//are properly initialized
gamete_type new_gamete(gametes[i]);
new_gamete.mutations.clear();
new_gamete.smutations.clear();
for(gciterator itr = gametes[i].mutations.begin() ;
itr != gametes[i].mutations.end() ; ++itr)
{
new_gamete.mutations.push_back( mutlookup[(*itr)->pos] );
/*
literator itr2 = std::find(mutations_destination.begin(),
mutations_destination.end(),
**itr);
assert(itr2 != mutations_destination.end());
new_gamete.mutations.push_back(itr2);
*/
}
for(gciterator itr = gametes[i].smutations.begin() ;
itr != gametes[i].smutations.end() ; ++itr)
{
new_gamete.smutations.push_back( mutlookup[(*itr)->pos] );
/*
literator itr2 = std::find(mutations_destination.begin(),
mutations_destination.end(),
**itr);
assert(itr2 != mutations_destination.end());
new_gamete.smutations.push_back(itr2);
*/
}
gametes_destination.push_back(new_gamete);
}
}
template<typename mutationtype,
typename vector_type_allocator1,
typename vector_type_allocator2,
typename list_type_allocator,
template <typename,typename> class vector_type,
template <typename,typename> class list_type >
void remove_fixed_lost( list_type<mutationtype,list_type_allocator> * mutations,
vector_type<mutationtype,vector_type_allocator1> * fixations,
vector_type<unsigned,vector_type_allocator2> * fixation_times,
const unsigned & generation,const unsigned & N)
{
BOOST_STATIC_ASSERT( ( boost::is_base_and_derived<mutation_base,mutationtype>::value) );
typename list_type<mutationtype,list_type_allocator>::iterator i = mutations->begin();
while(i != mutations->end())
{
assert(i->n <= N);
i->checked=false;
if(i->n==N )
{
fixations->push_back(*i);
fixation_times->push_back(generation);
}
if( i->n == 0 || i->n == N )
{
mutations->erase(i);
i=mutations->begin();
}
else
{
++i;
}
}
}
template<typename gamete_type>
void adjust_mutation_counts( gamete_type * g , const unsigned & n)
/*!
\note Will need a specialization if types have other data that need updating
*/
{
for(unsigned j=0;j< g->mutations.size();++j)
{
if( g->mutations[j]->checked==false)
{
g->mutations[j]->n=n;
g->mutations[j]->checked=true;
}
else
{
g->mutations[j]->n += n;
}
}
for(unsigned j=0;j< g->smutations.size();++j)
{
if( g->smutations[j]->checked==false)
{
g->smutations[j]->n=n;
g->smutations[j]->checked=true;
}
else
{
g->smutations[j]->n += n;
}
}
}
template< typename gamete_type,
typename vector_type_allocator,
template <typename,typename> class vector_type >
void update_gamete_list( vector_type<gamete_type,vector_type_allocator > * gametes,
const unsigned & N)
{
int g = gametes->size()-1;
typename vector_type<gamete_type,vector_type_allocator >::iterator gbeg;
while(g>=0)
{
gbeg=gametes->begin()+g;
assert(gbeg->n <= N);
if( gbeg->n == 0 )
{
gametes->erase(gbeg);
}
else
{
int j = gbeg->mutations.size()-1;
while(j >= 0)
{
assert( gbeg->mutations[j]->n <= N );
if(gbeg->mutations[j]->n == N|| gbeg->mutations[j]->n == 0)
{
gbeg->mutations.erase(gbeg->mutations.begin()+j);
}
--j;
}
j = gbeg->smutations.size()-1;
while(j >= 0)
{
assert( gbeg->smutations[j]->n <= N );
if(gbeg->smutations[j]->n == N|| gbeg->smutations[j]->n == 0)
{
gbeg->smutations.erase(gbeg->smutations.begin()+j);
}
--j;
}
}
--g;
}
//go backwards through gamete list, and see if there are
//any non-unique gametes that we can remove.
// seems to have no effect, which is good...
/*
typename vector_type<gamete_type,vector_type_allocator >::iterator itr;
g = gametes->size()-1;
unsigned s = gametes->size();
while( g >= 0 )
{
gbeg = (gametes->begin()+g);
itr = std::find(gametes->begin(),gbeg,*gbeg);
if( itr < gbeg )
{
std::cerr << "erasing: " << (itr-gbeg) << ' ' << g << '\n';
itr->n += gbeg->n;
size_t diff = g;
gametes->erase(gbeg);
//gbeg = gametes->begin();
//gend = gametes->end()-1;//gbeg+diff-1;
}
--g;
}
if( s != gametes->size() )
{
std::cerr << s << ' ' << gametes->size() << '\n';
}
*/
}
template< typename floating_type,
typename iterator_type>
void multinomial_sample_gametes(gsl_rng * r,iterator_type gbegin,
const size_t & ngametes,
const floating_type * efreqs,
const unsigned & N)
{
BOOST_STATIC_ASSERT( (boost::is_convertible<floating_type, double>::value) );
unsigned n = N;
double sum=1.;
for(unsigned i=0;i<ngametes;++i)
{
if( (*(efreqs+i)) > 0. )
{
(gbegin+i)->n = gsl_ran_binomial(r,*(efreqs+i)/sum,n);
sum -= *(efreqs+i);
}
else
{
(gbegin+i)->n = 0;
}
n -= (gbegin+i)->n;
adjust_mutation_counts( &*(gbegin+i), (gbegin+i)->n );
}
//assert(n==0);
}
#endif /* _UTIL_HPP_ */