Repository navigation
Expand file tree
/
Copy pathgeva.cpp
More file actions
238 lines (174 loc) · 6.81 KB
/
Copy pathgeva.cpp
File metadata and controls
238 lines (174 loc) · 6.81 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
//
// Copyright (c) 2018 Patrick K. Albers. All rights reserved.
//
#include <algorithm>
#include <unistd.h>
#include <ios>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <unordered_map>
#include <exception>
#include <stdexcept>
#include "load_map.h"
#include "load_vcf.h"
//#include "load_gen.h"
//#include "load_hap.h"
#include "load_bin.h"
//#include "join_bin.h"
#include "load_hmm.h"
//#include "load_sim.h"
//#include "detect_share.h"
#include "select_share.h"
#include "load_share.h"
//#include "ibd_by_pair.h"
//#include "ibd_by_site.h"
//#include "print_ibd.h"
//#include "count_share.h"
#include "infer_age.h"
#include "Command.hpp"
#include "Redirect.hpp"
#include "Clock.hpp"
//
// Main
//
int main(int argc, const char * argv[])
{
Command::Line line(argc, argv);
// common arguments
Command::Value< std::string > output('o', "out", "Prefix for generated output files");
Command::Value< size_t > thread('t', "treads", "Number of threads for parallel execution (default: 1)");
Command::Value< size_t > buffer('b', "buffer", "Memory buffer, upper limit in megabytes (default: no limit)");
Command::Value< size_t > seed("seed", "Set seed for random operations (may not be reproducible if t>1)");
// input arguments
Command::Value< std::string > input_bin_file('i', "input", "Pre-processed binary input file");
Command::Value< std::string > input_vcf_file("vcf", "VCF input file (optionally GZIP compressed)");
Command::Value< std::string > input_map_file("map", "Gen map input file (optionally GZIP compressed)");
Command::Value< double > input_rec_rate("rec", "Recombination rate, per site per generation (default: 1e-08)");
Command::Value< size_t > input_lines("maxLines", "Number of lines to be buffered while processing input file (default: 500000)");
Command::Bool local_tmp_files("localTmpFiles", "Temporary files are stored in local directory when parsing input file");
Command::Array< std::string, 2 > input_hmm_file("hmm", "Hidden Markov Model, 2 input files: (1) empirical initial state and (2) emission probabilties");
// genomic position arguments
Command::Value< size_t > share_position("position", "Target position");
Command::Value< std::string > share_batch("positions", "Batch file containing target positions (any white-space separation)");
// age estimation parameters
Command::Value< size_t > effective_size("Ne", "Effective population size (default: 10000)");
Command::Value< double > mutation_rate("mut", "Mutation rate, per site per generation (default: 1e-08)");
Command::Value< size_t > age_limit_sharers("maxConcordant", "Maximim number of concordant pairs to be selected (default: 100)");
Command::Value< size_t > age_outgroup_size("maxDiscordant", "Maximum number of discordant pairs to be selected (default: 100)");
Command::Value< double > max_missing("maxMissing", "Maximum missing proportion of sites in HMM (default: 5%)");
// parse command line
try
{
line.parse();
line.get(output, true);
line.get(thread, false, size_t(1));
line.get(buffer, false, std::numeric_limits<size_t>::max()); // default: all individuals
line.get(seed, false);
if (!line.get(input_bin_file, false)) // BIN
{
// VCF file
line.get(input_vcf_file, false);
// genetic map
line.get(input_map_file, false);
// optionally constant recombination rate
line.get(input_rec_rate, false, double(1e-08)); // default: 1e-08
// number of lines read
line.get(input_lines, false, size_t(5e05)); // default: 0.5 million lines
// locally save tmp files
line.get(local_tmp_files, false, false);
}
else
{
// target position
const bool do_position = line.get(share_position, false);
// batch file of positions
const bool do_batch = line.get(share_batch, false);
if (do_position && do_batch)
throw std::invalid_argument("Conflicting target position input");
if (!do_position && !do_batch)
throw std::invalid_argument("Missing target position input");
line.get(input_hmm_file, true);
line.get(effective_size, false, size_t(10000)); // default: 10000
line.get(mutation_rate, false, double(1e-08)); // default: 1e-08
line.get(max_missing, false, double(0.05)); // defaul: 5%
line.get(age_limit_sharers, false, size_t(100)); // default: 100
line.get(age_outgroup_size, false, size_t(100)); // default: 100
}
line.finish();
}
catch(const int & help)
{
return EXIT_SUCCESS;
}
catch(const std::exception & error)
{
std::cout << error.what() << std::endl;
return EXIT_FAILURE;
}
// set seed
if (seed.good())
{
set_random_seed(seed);
}
// redirect log/err output
Redirect redirect_log(std::clog, output.value + ".log");
Redirect redirect_err(std::cerr, output.value + ".err");
std::cout << std::endl;
std::cout << "Genealogical estimation of variant age (GEVA)" << std::endl;
std::cout << "(c) Patrick K. Albers" << std::endl;
std::cout << std::endl;
// print programme call to log
line.print(std::clog);
// begin timer
Clock runtime;
// main algorithm
try
{
// process input data
Gen::Grid::Data grid;
if (!input_bin_file.good())
{
// switch between map rec rate or fixed rec rate
Gen::Map gmap = (input_map_file.good()) ? load_map(input_map_file): load_map(input_rec_rate);
input_bin_file = load_vcf(input_vcf_file, gmap, output, input_lines, false, local_tmp_files);
grid = load_bin(input_bin_file);
grid->print_sample(output.value + ".sample.txt");
grid->print_marker(output.value + ".marker.txt");
}
else
{
grid = load_bin(input_bin_file);
grid->cache(buffer);
Gen::Share::Data share = std::make_shared<Gen::Share>();
if (share_position.good())
select_share(share, grid, share_position);
if (share_batch.good())
select_share(share, grid, share_batch);
// prepare hmm
IBD::DetectMethod method = IBD::DETECT_HMM;
IBD::HMM::Model::Data hmm_model;
hmm_model = load_hmm(grid, input_hmm_file[0], input_hmm_file[1], effective_size, output);
// execute age estimation
Age::Param::Data param = std::make_shared< Age::Param >(grid, effective_size, mutation_rate);
if (age_limit_sharers.good())
param->limit_sharers = age_limit_sharers.value;
if (age_outgroup_size.good())
param->outgroup_size = age_outgroup_size.value;
param->threads = thread;
infer_age(param, method, max_missing, output, false, false, share, grid, runtime, hmm_model, nullptr, thread);
}
}
catch(const std::exception & error)
{
std::cout << error.what() << std::endl;
return EXIT_FAILURE;
}
std::cout << "This is the end!" << std::endl;
std::clog << "This is the end!" << std::endl;
runtime.print(std::cout);
runtime.print(std::clog);
return EXIT_SUCCESS;
}