-
Notifications
You must be signed in to change notification settings - Fork 2
/
alleleset.h
135 lines (105 loc) · 2.06 KB
/
alleleset.h
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
#ifndef ALLELESET_H
#define ALLELESET_H
#include <vector>
// Default implementation for AlleleSet. Note that this will become rather
// inefficient for larger alphabets (e.g. microsatellites).
template<class STATE>
class AlleleSet
{
public:
typedef STATE state_t;
protected:
int _n_alleles;
std::vector<int> _count;
public:
AlleleSet()
: _n_alleles(0), _count(0)
{}
size_t s2i(const state_t & a) const
{
return a;
}
state_t i2s(size_t i) const
{
return i;
}
void add(const state_t & a)
{
// cerr << "ADD " << a << ";";
const size_t i = s2i(a);
if (_count.size() <= i)
_count.resize(i+1, 0);
_count[i]++;
}
int count(const state_t & a) const
{
const size_t i = s2i(a);
return _count.size() > i ? _count[i] : 0;
}
// unsafe version
int operator[](const state_t & a) const
{
return _count[s2i(a)];
}
// find first allele with count>0
state_t find(size_t start = 0) const
{
for (size_t i=start; i<_count.size(); i++)
if (_count[i] != 0)
return i2s(i);
return i2s(_count.size());
}
size_t size() const
{
return _count.size();
}
void resize(size_t size)
{
_count.resize(size);
}
void clear()
{
_count.clear();
_n_alleles = 0;
}
void do_count()
{
_n_alleles = 0;
for (size_t i=0; i<_count.size(); i++)
if (_count[i] != 0)
_n_alleles++;
//cout << "na: " << _n_alleles << endl;
}
int product(const AlleleSet<STATE> & o) const
{
int p = 0;
for (size_t i=0; i<_count.size() && i<o._count.size(); i++)
p += _count[i] * o._count[i];
return p;
}
int pairwise_difference() const
{
int diff = 0;
for (size_t i=0; (i+1)<_count.size(); i++)
for (size_t j=i+1; j<_count.size(); j++)
diff += _count[i] * _count[j];
//cerr << "diff: " << diff << endl;
return diff;
}
int n_singletons() const
{
int n = 0;
for (size_t i=0; i<_count.size(); i++)
if (_count[i] == 1) n++;
return n;
}
bool is_singleton(const state_t & s) const
{
return count(s) == 1;
}
int n_alleles() const
{
return _n_alleles;
}
};
#endif // ALLELESET_H