-
Notifications
You must be signed in to change notification settings - Fork 6
/
ngsF-HMM.cpp
171 lines (141 loc) · 5.03 KB
/
ngsF-HMM.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
169
170
171
/*
*
* ngsF-HMM - NGS data individual inbreeding coefficients estimation.
* Copyright (C) 2012 Filipe G. Vieira
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "ngsF-HMM.hpp"
char const* version = "1.1.0";
int main (int argc, char** argv) {
/////////////////////
// Parse Arguments //
/////////////////////
params* pars = new params;
init_pars(pars);
parse_cmd_args(pars, argc, argv);
// Adjust number of threads
if(pars->n_threads > pars->n_ind){
warn(__FUNCTION__, "adjusting threads (--n_threads) to match number of individuals!");
pars->n_threads = pars->n_ind;
}
///////////////////////
// Adjust parameters //
///////////////////////
// Get file total size
struct stat st;
if(stat(pars->in_geno, &st) != 0)
error(__FUNCTION__, "cannot check GENO file size!");
if(strcmp(strrchr(pars->in_geno, '.'), ".gz") == 0){
if(pars->verbose >= 1)
printf("==> GZIP input file (not BINARY)\n");
pars->in_bin = false;
}else{
if(pars->verbose >= 1)
printf("==> BINARY input file (always lkl)\n");
pars->in_bin = true;
pars->in_lkl = true;
if(pars->n_sites != st.st_size/sizeof(double)/pars->n_ind/N_GENO)
error(__FUNCTION__, "invalid/corrupt genotype input file!");
}
/////////////////////
// Read input data //
/////////////////////
// Read positions from file
if(pars->verbose >= 1){
printf("==> Reading data\n");
printf("> Sites coordinates\n");
}
if(pars->in_pos){
// Temp FIX since ngsF-HMM uses 1-based arrays
double *pos_dist = read_dist(pars->in_pos, 0, pars->n_sites);
pars->pos_dist = init_ptr(pars->n_sites+1, (double) INFINITY);
memcpy(pars->pos_dist+1, pos_dist, pars->n_sites * sizeof(double));
free_ptr((void*) pos_dist);
}else{
pars->pos_dist = init_ptr(pars->n_sites+1, (double) INFINITY);
}
// Convert position distances to Mb
for(uint64_t s = 1; s <= pars->n_sites; s++)
pars->pos_dist[s] /= 1e6;
if(pars->verbose >= 7){
for(uint64_t s = 1; s <= min(10,pars->n_sites); s++){
printf("%f\n", pars->pos_dist[s]);
}
}
// Read data from GENO file
if(pars->verbose >= 1)
printf("> GENO data\n");
pars->geno_lkl = read_geno(pars->in_geno, pars->in_bin, pars->in_lkl, &pars->in_loglkl, pars->n_ind, pars->n_sites);
// Transp GL matrix
pars->geno_lkl_s = transp_matrix(pars->geno_lkl, pars->n_ind, pars->n_sites+1);
// If input is not genotypes, check whether to call genotypes
for(uint64_t i = 0; i < pars->n_ind; i++)
for(uint64_t s = 1; s <= pars->n_sites; s++){
// Call genotypes
if(pars->call_geno)
call_geno(pars->geno_lkl[i][s], N_GENO);
/*
// Ensure minimum GL allowed
if( pars->geno_lkl[i][s][array_min_pos(pars->geno_lkl[i][s], N_GENO)] < log(0.001) ){
for(uint64_t g = 0; g < N_GENO; g++)
if(pars->geno_lkl[i][s][g] < log(0.001))
pars->geno_lkl[i][s][g] = log(0.001);
// Re-normalize GL
post_prob(pars->geno_lkl[i][s], pars->geno_lkl[i][s], NULL, N_GENO);
}
*/
post_prob(pars->geno_lkl[i][s], pars->geno_lkl[i][s], NULL, N_GENO);
}
/////////////////////////////////////////////
// Declare and initialize output variables //
/////////////////////////////////////////////
if(pars->verbose >= 6)
printf("> Init output\n");
init_output(pars);
//////////////////
// Analyze Data //
//////////////////
if(pars->verbose >= 6)
printf("> Create threadpool\n");
if( (pars->thread_pool = threadpool_create(pars->n_threads, 2*pars->n_ind, 0)) == NULL )
error(__FUNCTION__, "failed to create thread pool!");
// Run EM!
EM(pars);
/////////////////
// Free Memory //
/////////////////
if(pars->verbose >= 1)
printf("Freeing memory...\n");
// thread pool
threadpool_wait(pars->thread_pool);
if(threadpool_destroy(pars->thread_pool, threadpool_graceful) != 0)
error(__FUNCTION__, "cannot free thread pool!");
// pars struct
//free_ptr((void*) pars->in_geno);
free_ptr((void***) pars->geno_lkl, pars->n_ind, pars->n_sites+1);
free_ptr((void**) pars->geno_lkl_s, pars->n_sites+1);
free_ptr((void*) pars->pos_dist);
free_ptr((void*) pars->freq);
free_ptr((void**) pars->path, pars->n_ind);
free_ptr((void***) pars->marg_prob, pars->n_ind, pars->n_sites+1);
free_ptr((void*) pars->indF);
free_ptr((void*) pars->alpha);
free_ptr((void*) pars->ind_lkl);
if(pars->verbose >= 1)
printf("Done!\n");
delete pars;
return 0;
}