-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathGenotype.cpp
168 lines (147 loc) · 2.49 KB
/
Genotype.cpp
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
#include "Genotype.hh"
Genotype::Genotype(int g0,int g1)
{
setGenotype(g0,g1);
}
void Genotype::setGenotype(int g0,int g1)
{
_is_haploid=false;
if(bcf_gt_is_missing(g0))
{
_g0 = -1;
}
else
{
_g0=bcf_gt_allele(g0);
}
if(bcf_gt_is_missing(g1))
{
_g1 = -1;
}
else if(g1==bcf_int32_vector_end)
{
_g1=bcf_int32_vector_end;
_is_haploid=true;
}
else
{
_g1=bcf_gt_allele(g1);
}
}
Genotype::Genotype(int idx,int *gt_array,int *ps_array)
{
if(idx<0)
{
_g0=-1;
_g1=-1;
_is_haploid=false;
_is_phased=false;
_ps=bcf_int32_missing;
}
else
{
_is_phased = ps_array && ps_array[idx]!=bcf_int32_missing;
_ps = ps_array ? ps_array[idx] : bcf_int32_missing;
setGenotype(gt_array[2*idx],gt_array[2*idx+1]);
}
}
int Genotype::ps()
{
return _ps;
}
bool Genotype::is_phased()
{
return( _is_phased || !(isHet() || isHaploid()) );
}
bool Genotype::isHet()
{
return(_g0!=-1 && _g1!=-1 && !_is_haploid && _g0!=_g1);
}
bool Genotype::isMissing()
{
return(_g0==-1||_g1==-1);
}
bool Genotype::isHaploid()
{
return(_is_haploid);
}
int Genotype::first()
{
return(_g0);
}
int Genotype::second()
{
if(_is_haploid)
{
return(bcf_int32_vector_end);
}
else
{
return(_g1);
}
}
int Genotype::getGenotype(int idx)
{
if(idx==0)
{
return(_g0);
}
if(idx==1)
{
if(_is_haploid)
{
return(bcf_int32_vector_end);
}
else
{
return(_g1);
}
}
std::cerr << "idx="<<idx<<std::endl;
die("invalid genotype.");
return(-1);
}
int Genotype::swap()
{
if(!_is_haploid)
{
int tmp = _g0;
_g0 = _g1;
_g1 = tmp;
}
return(0);
}
int Genotype::update_bcf_gt_array(int *gt_array,int index,int32_t *ps_array)
{
if(!isMissing())
gt_array[index * 2] = _is_phased ? bcf_gt_phased(first()) : bcf_gt_unphased(first());
else
gt_array[index * 2] = bcf_gt_missing;
if(isHaploid())
gt_array[index * 2 + 1] = bcf_int32_vector_end;
else
gt_array[index * 2 + 1] = _is_phased ? bcf_gt_phased(second()) : bcf_gt_unphased(second());
if(ps_array)
ps_array[index]=_ps;
return(0);
}
void Genotype::setPhase(bool phased)
{
_is_phased=phased;
}
std::string Genotype::print()
{
if(isMissing()) return "./.";
std::stringstream ss;
ss<<first();
if(isHaploid())
return ss.str();
if(_is_phased)
ss<<"|";
else
ss<<"/";
ss<<second();
if(_ps!=bcf_int32_missing)
ss<<":"<<_ps;
return ss.str();
}