forked from tplinderoth/ngsParalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngsParalog.cpp
317 lines (280 loc) · 8.54 KB
/
ngsParalog.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* ngsParalog.cpp
*
* ngsParalog - Genomic duplication detection from NGS data
* Copyright (C) 2016 Tyler P. Linderoth
*
* 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/>.
*
* Tyler's contact information: [email protected]
*
*
* hidden arguments (for troubleshooting):
* -numericGrad INT; runs the bfgs optimization using a numeric gradient if 1 [0]
* -verbose INT; amount of optimization output: < 0 is none, = 1 is one line, 2-98 is some, 99-100 is a lot, > 100 is max [-1]
* -printML 0|1; prints ML estimates of alternate allele frequency (field 6) and admixture proportion (field 7) if set to 1
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <time.h>
#include "generalUtils.h"
#include "parsePileup.h"
#include "optim.h"
#include "Stats.h"
#include "ngsParalog.h"
int main (int argc, char** argv)
{
int rc = 0; // store function return values
Argparser runpar; // stores input parameters
// parse user input
if((rc=runpar.parseInput(argc, argv, version)) != 0)
{
if (rc < 0)
{
endMessage(rc);
return 1;
}
else
return 0;
}
else
{
// set IO streams
if((rc=runpar.setStreams(runpar.infile_name(), runpar.outfile_name())))
{
endMessage(rc);
return 1;
}
}
// run the task
if (strcmp(runpar.task(), "calcLR") == 0)
rc = doLR(&runpar);
else if (strcmp(runpar.task(), "findRegion") == 0)
rc = doRegion ();
else
{
std::cerr << "Undefined task in call to main\n";
rc=1;
endMessage(rc);
}
// close IO streams
runpar.closeStreams();
// exit program
endMessage(rc);
return rc;
}
int doLR (Argparser* params)
{
int rv = 0;
// set likelihood functions
//double (*likefn)(const double x[], const void*) = Stats::negLogfn;
double (*likefn)(const double x[], const void*) = &Stats::negLogfn;
void (*dlikefn)(const double x[], double y[], const void*) = NULL; // could adjust this with Argparser _numericGrade member
// initialize optimization objects
Optim altmodel;
if (setOptim(altmodel, 1, params->verblevel()))
{
std::cerr << "Problem encountered in function setOptim for alternate model\n";
return 1;
}
Optim nullmodel;
if (setOptim(nullmodel, 0, params->verblevel()))
{
std::cerr << "Problem encountered in function setOptim for null model\n";
return 1;
}
// analyze pileup data
try
{
rv = processPileup(params->input(), params->output(), &altmodel, &nullmodel, likefn, dlikefn, params->offsetQ(),
params->minQ(), params->mincov(), params->minind(), params->printML());
}
catch (PileupException& error)
{
std::cerr << error.what() << "\nGracefully terminating\n";
return -1;
}
catch (std::exception& error)
{
std::cerr << error.what() << "\nGracefully terminating\n";
return -1;
}
return rv;
}
int doRegion ()
{
std::cerr << "Dang, findRegion still not implemented ...\n";
return 0;
}
int setOptim (Optim& model, bool isalt, int verb)
{
// sets up optim objects
const unsigned int dim = isalt ? 2 : 1; // number of parameters to optimize
int optconditions = 6; // number of conditions that must be set to perform bfgs optimization
const double startpoints[2] = {0.2, 0.05}; // f & m anchor points for optimization
const double step[2] = {0.7, 0.48}; // f & m step distance from optimization anchor
int nullidx = 0; // index of null parameters in parameter arrays
double* min = new double[dim]; // minimum parameter values
double* max = new double[dim]; // maximum parameter values
unsigned int* nbd = new unsigned int[dim]; // number of boundary conditions
unsigned int i = 0;
// set boundary values
for (i = 0; i < dim; i++)
min[i] = 0.0;
for (i = 0; i < dim; i++)
max[i] = 1.0;
for (i = 0; i < dim; i++)
nbd[i] = 2;
// set values for optim
if (!model.setParN(dim))
return false;
if (!model.setBoundCntrl(max, min, nbd, dim))
return false;
// set amount of output
model.setVerbose(verb);
// set optimization start points
if (isalt)
{
if (model.initStartMatrix(dim, startpoints, step, 1))
return false;
}
else
{
if (model.initStartMatrix(dim, &startpoints[nullidx], &step[nullidx]))
return false;
}
// make sure everything has been initialized
if (model.conditions() < optconditions)
{
fprintf(stderr,"Optim object has only %i out of %i optimization conditions set in call to setOptim\n", model.conditions(), optconditions);
return false;
}
delete [] min;
delete [] max;
delete [] nbd;
return 0;
}
int processPileup (std::istream& indat, std::ostream& os, Optim* altmodel, Optim* nullmodel, double (*fn)(const double x[], const void*),
void (*dfn)(const double x[], double y[], const void*), const double qoffset, const double minq, const unsigned int mindepth,
const unsigned int minind, int printML)
{
const char delimiter = '\t'; // assume pileup is tab delimited
const bool weightcount = true;
const int neglog = 1; // 1 = likelihoods are -log, otherwise 0
double lr = 0.0;
int optfail = 0;
std::string(line);
// initialize Pileup object
Pileup piledat;
piledat.setQualCode(qoffset);
piledat.setMinQ(minq);
getline(indat, line);
piledat.setn(line, delimiter);
// calculate sequence ID buffer size for pretty printing
unsigned int idbuffer = calcIdBuffer(Pileup::idSize(line, delimiter));
// check that there at at least minind individuals in pileup
if (minind > piledat.nInd())
{
fprintf(stderr,"Fewer than %u individuals in dataset --> decrease -minind argument\n",minind);
return 1;
}
// loop through pileup input
while (!line.empty())
{
// store data in Pileup object
piledat.getSeqDat(line, delimiter);
if (piledat.fail())
{
fprintf(stderr, "Error parsing pileup data\n");
return 1;
}
// check for excessive missing data
if (numCovered(&piledat, mindepth) < minind)
{
fprintf(stderr, "skipping %s %u --> inadequate coverage\n", piledat.seqName().c_str(), piledat.position());
getline(indat,line);
continue;
}
// set major and minor allele and store pileup data in optim objects
piledat.setMajor(piledat.empiricalMajor(weightcount)); // assign most common allele as major
piledat.setMinor(piledat.empiricalMinorFast(weightcount)); // assign second most common allele as minor
altmodel->data = nullmodel->data = &piledat;
// perform optimization and calculate LR
try
{
lr = Stats::optimLR(nullmodel, altmodel, fn, dfn, neglog, &optfail);
printLR(piledat.seqName(), piledat.position(), nullmodel, altmodel, lr, os, idbuffer, printML);
}
catch (const OptimFailureException& error)
{
std::cerr << error.what() << " -> skipping " << piledat.seqName() << " " << piledat.position() << "\n";
}
catch (const NoDataException& error)
{
std::cerr << error.what() << " -> skipping site\n";
}
// fetch next line
getline(indat,line);
}
return 0;
}
unsigned int numCovered (const Pileup* data, const unsigned int mincov)
{
static std::vector<SiteData>::const_iterator indIter;
unsigned int ncovered = 0;
for (indIter = data->seqdat.begin(); indIter != data->seqdat.end(); ++indIter)
{
if (indIter->cov() >= mincov)
++ncovered;
}
return ncovered;
}
void printLR (const std::string chr, const unsigned int pos, Optim* null, Optim* alt, double lr, std::ostream& os,
const unsigned int nameBuffer, bool printml)
{
const double thresh = -1e-07;
const int prec = 8;
int i = 0;
if (lr < 0.0) // prevents reporting negative zero (numerical noise)
{
if (lr > thresh)
lr = 0.0;
}
os << std::setw(nameBuffer) << std::left << chr
<< '\t' << std::setw(12) << pos
<< '\t' << std::fixed << std::setprecision(prec) << null->llh()
<< '\t' << std::fixed << std::setprecision(prec) << alt->llh()
<< '\t' << std::fixed << std::setprecision(prec) << lr;
if (printml)
for (i = 0; i < alt->getDim(); ++i)
os << '\t' << std::fixed << std::setprecision(prec) << alt->mlparam(i);
os << '\n';
}
unsigned int calcIdBuffer (const unsigned int nameLength)
{
const int extra = 9;
unsigned int len = nameLength + extra;
while (len%8)
++len;
return len;
}
void endMessage (int status)
{
if (!status)
std::cerr << "Finished!\n";
else
std::cerr << "-->exiting\n";
}