-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.cpp
510 lines (414 loc) · 15.8 KB
/
main.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <string>
#include <iostream>
#include <map>
#include <cmath>
#include <float.h>
#include <algorithm>
#include <sstream>
#include <sys/stat.h>
#include "mm.h"
#include "main.h"
#ifdef OPENMP
#include <omp.h>
#endif
#define VERBOSITY 3
#define BUILD_COMMAND "build"
#define PREDICT_COMMAND "predict"
struct prediction{
size_t hostIdx;
double ll;
};
struct by_ll {
bool operator()(prediction const &a, prediction const &b) {
return a.ll > b.ll;
}
};
void die(std::string text,std::string complement=std::string())
{
std::cout<< "ERROR:" << text << complement << "." <<std::endl;
exit(-1);
}
void build(std::string genomeDir, std::string modelDir, unsigned int order, double alpha, unsigned int threads = 1)
{
DIR* dir;
struct dirent *genomeFile;
std::vector<std::string> genomeFiles;
#ifdef OPENMP
omp_set_num_threads(threads);
#endif
if((dir = opendir(modelDir.c_str())) == NULL)
die("Cannot open model directory ", modelDir);
if((dir = opendir(genomeDir.c_str())) == NULL)
die("Cannot open genome directory ", genomeDir);
while ((genomeFile = readdir (dir)) != NULL) {
if (std::string(genomeFile->d_name) != "." && std::string(genomeFile->d_name) != "..")
genomeFiles.push_back(genomeDir + "/" + std::string(genomeFile->d_name));
}
closedir (dir);
#pragma omp parallel for schedule(static)
for (size_t i = 0; i < genomeFiles.size() ; i++)
{
mm model(order,alpha,VERBOSITY);
if (VERBOSITY > 2)
std::cout << "Processing "<<genomeFiles[i]<<std::endl;
model.trainOn(genomeFiles[i]);
model.printParameters();
if (model.write(modelDir) < 0)
die("Cannot write to model directory ",modelDir);
}
}
double mean(std::vector<double> v)
{
double mean = 0.0;
for (size_t i = 0; i < v.size();i++)
{
mean += v[i];
}
return mean/v.size();
}
double sd(std::vector<double> v, double mean)
{
double sd = 0.0;
for (size_t i = 0; i < v.size();i++)
{
double x = (v[i]-mean);
sd += x*x;
}
return (double)(v.size() -1 ) * sqrt(sd/v.size()) / v.size();
}
double getPval(std::pair<double,double> param, double ll)
{
return 0.5 - 0.5*erf((ll-param.first)/(sqrt(2.0)*param.second));
}
void predict(std::string genomeDir, std::string modelDir,std::string resultDir, unsigned int threads = 1, bool writeLLMatrix=true,size_t writeBestPred = 0,std::string negFitsFile = std::string(),bool zScores = false,bool writePvalMatrix=false)
{
DIR* dir;
struct dirent *genomeFile,*modelFile;
std::vector<std::string> genomeFiles,modelFiles, genomeNames;
std::vector< std::vector<double> > ll;
#ifdef OPENMP
omp_set_num_threads(threads);
#endif
// List files in genome and model dir
if((dir = opendir(resultDir.c_str())) == NULL)
die("Cannot open result directory ", resultDir);
closedir(dir);
if((dir = opendir(genomeDir.c_str())) == NULL)
die("Cannot open genome directory ", genomeDir);
while ((genomeFile = readdir (dir)) != NULL) {
if (std::string(genomeFile->d_name) != "." && std::string(genomeFile->d_name) != "..")
{
genomeFiles.push_back(std::string(genomeFile->d_name));
genomeNames.push_back(genomeFiles.back().substr(0, genomeFiles.back().find_last_of(".")));
}
}
closedir (dir);
if((dir = opendir(modelDir.c_str())) == NULL)
die("Cannot open model directory ", modelDir);
while ((modelFile = readdir (dir)) != NULL) {
if (std::string(modelFile->d_name) != "." && std::string(modelFile->d_name) != "..")
{
modelFiles.push_back(std::string(modelFile->d_name));
ll.push_back(std::vector<double>());
}
}
closedir (dir);
std::map<std::string,std::pair<double, double> > negFits;
std::vector<std::string> predictionWithoutFit;
// If we need to compute a p-value, read the fits from file
if (negFitsFile != std::string())
{
std::ifstream fin(negFitsFile.c_str(), std::ios::in);
if (!fin.good())
die("Cannot open negative fits file ",negFitsFile);
std::string line;
while(std::getline(fin,line))
{
if(!line.empty())
{
int secondField = line.find("\t");
if (secondField+1<line.size())
{
int thirdField = line.find("\t",secondField + 1);
if (thirdField+1<line.size())
{
std::string bactName = line.substr(0,secondField);
double mu = std::stod(line.substr(secondField+1,thirdField));
double s = std::stod(line.substr(thirdField+1,line.size()));
negFits[bactName] = std::make_pair(mu,s);
} else {
std::cout << "Warning: "<< negFitsFile <<" is missformatted. Should be [bactName]\\t[mu]\\t[standard deviation]"<<std::endl;
}
} else {
std::cout << "Warning: "<< negFitsFile <<" is missformatted. Should be [bactName]\\t[mu]\\t[standard deviation]"<<std::endl;
}
}
}
fin.close();
} else {
negFitsFile = "negFits.csv";
}
std::vector<std::vector<std::string> > bactGenomes;
for (size_t j = 0 ; j < genomeFiles.size() ; j++) {
bactGenomes.push_back(mm::readGenome(genomeDir + "/" + genomeFiles[j]));
}
std::vector<std::string> modelNames(modelFiles.size(),std::string());
// Compute log-likelihoods
size_t progressCount = 0;
#pragma omp parallel for schedule(static)
for (size_t i = 0; i < modelFiles.size() ; i++)
{
mm model(modelDir + "/" + modelFiles[i],VERBOSITY);
if (VERBOSITY>2)
{
progressCount++;
std::cout<<"Processing "<<model.getName()<<" (approx. "<<progressCount<<"/"<<modelFiles.size()<<")"<<std::endl; //model.printParameters();
}
modelNames[i] = model.getName();
for (size_t j = 0 ; j < genomeFiles.size() ; j++) {
ll[i].push_back(model.evaluate(bactGenomes[j]));
}
}
if (writeBestPred)
{
std::ofstream fout((resultDir + "/prediction.list").c_str(), std::ios::out);
if (!fout.good())
die("Cannot open ",resultDir);
fout << "\"Phage\"\t\"Best hit among provided hosts\"\t\"LogLikelihood\"\t\"p-value if null parameters provided\"\n";
for (size_t j = 0 ; j < genomeNames.size() ; j++)
{
std::vector<prediction> llToSort;
for (size_t i = 0; i < modelNames.size() ; i++)
{
llToSort.push_back({i,ll[i][j]});
}
std::sort(llToSort.begin(), llToSort.end(), by_ll());
size_t nbOfPredToOutput = writeBestPred<modelNames.size() ? writeBestPred:modelNames.size();
for (size_t k=0;k<nbOfPredToOutput;k++)
{
size_t host = llToSort[k].hostIdx;
fout << genomeNames[j] <<'\t'<<modelNames[host]<<'\t'<<ll[host][j];
if ( negFits.find(modelNames[host]) != negFits.end() )
{
fout << '\t' << getPval(negFits[modelNames[host]],ll[host][j]);
} else {
predictionWithoutFit.push_back(modelNames[host]);
fout << "\tNA";
}
fout << std::endl;
std::cout<<k<<","<<nbOfPredToOutput<<std::endl;
}
}
fout.close();
}
// Normalize the log-likelihood to z-scores
if(zScores)
{
#pragma omp parallel for schedule(static)
for (size_t i = 0; i < modelFiles.size() ; i++)
{
double mu = mean(ll[i]);
double s = sd(ll[i],mu);
for (size_t j = 0 ; j < genomeFiles.size() ; j++) {
ll[i][j] -= mu;
ll[i][j] /= s;
}
}
}
// Output results according to user choice
if (writeLLMatrix) {
std::ofstream fout((resultDir + "/llikelihood.matrix").c_str(), std::ios::out);
if (!fout.good())
die("Cannot open ",resultDir);
if (genomeNames.size())
fout<<genomeNames[0];
for (size_t j = 1 ; j < genomeNames.size() ; j++)
{
fout << '\t'<< genomeNames[j];
}
fout << std::endl;
for (size_t i = 0; i < modelNames.size() ; i++)
{
fout << modelNames[i];
for (size_t j = 0 ; j < genomeNames.size() ; j++)
{
fout << '\t' << ll[i][j];
}
fout << std::endl;
}
fout.close();
}
// Output all p-values according to user choice
if (writePvalMatrix) {
std::ofstream fout((resultDir + "/pvalues.matrix").c_str(), std::ios::out);
if (!fout.good())
die("Cannot open ",resultDir);
if (genomeNames.size())
fout<<genomeNames[0];
for (size_t j = 1 ; j < genomeNames.size() ; j++)
{
fout << '\t'<< genomeNames[j];
}
fout << std::endl;
for (size_t i = 0; i < modelNames.size() ; i++)
{
bool hasNegFit;
std::pair<double, double> negFit;
fout << modelNames[i];
if ( negFits.find(modelNames[i]) != negFits.end() )
{
hasNegFit = true;
negFit = negFits[modelNames[i]];
} else {
hasNegFit = false;
predictionWithoutFit.push_back(modelNames[i]);
}
for (size_t j = 0 ; j < genomeNames.size() ; j++)
{
if(hasNegFit)
{
fout << '\t' << getPval(negFit,ll[i][j]);
} else {
fout << "\tNA";
}
}
fout << std::endl;
}
fout.close();
}
if (predictionWithoutFit.size())
{
std::cout<<"WARNING: ";
std::sort(predictionWithoutFit.begin(),predictionWithoutFit.end());
std::vector<std::string>::iterator it = std::unique(predictionWithoutFit.begin(),predictionWithoutFit.end());
predictionWithoutFit.resize( std::distance(predictionWithoutFit.begin(),it) );
for ( it = predictionWithoutFit.begin() ; it!=predictionWithoutFit.end() ; it++ )
std::cout<<*it<<",";
std::cout<<" do(es) not have null-model parameters, and their p-value calculation will be missing in the prediction.list file.\n\nHere is the procedure to fit those parameters:\n\
\n\n\
The parameters for their null-distribution can be computed this way:\n\
- build models with WIsH for each of the listed genomes that miss the null-parameters,\n\
- run the WIsH prediction of the models against a large set of various phages,\n\
- for every model M, fit (e.g. using maximum likelihood) a normal distribution over the scores of M (in the likelihood.matrix file) obtained on phage genomes that are known not to infect the same genus as M,\n\
- for every model M, add a line to the file " + negFitsFile + " with the following format:\n\
M<TAB>Mean<TAB>StandardDeviation\n\
\n\
To get the p-values associated to a prediction, you can then run the prediction with the options -b -n " + negFitsFile + "\n\
when running a prediction.\n";
}
}
int main(int argc, char **argv)
{
std:: string genomeDir,modelDir,resultDir,command,negFitFile;
unsigned int order = 8;
unsigned int threads = 1;
double alpha = 16.0;
unsigned int bestPred = 0;
bool zScores = false;
bool printHelp = false;
bool writePvalMatrix = false;
int option;
while ((option = getopt (argc, argv, "g:m:r:c:k:b:n:pzha:t:")) != -1)
{
switch(option)
{
case 'g':
genomeDir = optarg;
break;
case 'm':
modelDir = optarg;
break;
case 'r':
resultDir = optarg;
break;
case 'c':
command = optarg;
break;
case 'n':
negFitFile = optarg;
break;
case 'b':
bestPred = atoi(optarg);
break;
case 'k':
order = atoi(optarg);
break;
case 't':
threads = atoi(optarg);
break;
case 'a':
alpha = std::stod(optarg);
break;
case 'z':
zScores = true;
break;
case 'p':
writePvalMatrix = true;
break;
case 'h':
printHelp = true;
break;
default:
die("Bad option");
break;
}
}
std::string helpText;
helpText = "WIsH (v" + std::to_string(WIsH_VERSION_MAJOR) + "." + std::to_string(WIsH_VERSION_MINOR) + ") is a tool for predicting bacterial hosts from phage (meta)genomic data.\n\
© Clovis Galiez ([email protected])\n\n\
Usage :" + std::string(argv[0]) + " [options] \n\
Options:\n\
\t-c\tCommand to be executed (build or predict)\n\
\t-k\tOrder for building the Markov chain (default is " + std::to_string(order) +")\n\
\t-a\tPseudo-count parameter (default is " + std::to_string(alpha) +")\n\
\t-t\tNumber of threads to be used (default is " + std::to_string(threads) +")\n\n\
Path specifications:\n\
\t-g\tSpecifies the genome directory (read access)\n\
\t-m\tSpecifies the model directory (read/write access)\n\
\t-r\tSpecifies the result directory (write access)\n\n\
Score options:\n\
\t-b\tOutputs a file containing for each viral sequence the host with the b best likelihood\n\
\t-p\tOutputs a matrix of p-values for every prediction (slows down the predictions)\n\
\t-z\tNormalize the matrix of log-likelihood as z-scores\n\
\t-n\tSpecifies the parameters for the distribution of negative values of each model\n\
\t\t\tFormat should be: modelName<Tab>mean<Tab>standardDeviation\n\n\
Example for building models:\n\n\
\t\tWIsH -c build -g prokaryoteGenomesDir -m modelDir\n\n\
Example for predicting hosts:\n\n\
\t\tWIsH -c predict -g virusGenomesDir -m modelDir -r outputResultDir\n\n";
if (printHelp)
{
std::cout<<helpText;
#ifdef OPENMP
std::cout<<"OpenMP supported."<<std::endl;
#else
std::cout<<"Compiled *without* OpenMP support."<<std::endl;
#endif
exit(0);
}
// Make the modelDir if it does not exist.
struct stat st;
// Check for existence.
if (stat(modelDir.c_str(), &st) < 0) {
// If not, create the directory.
// read/write/search permissions for owner and group.
// read/search permissions for others.
if (mkdir(modelDir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
die("Cannot create model directory ", modelDir);
}
}
if (command == BUILD_COMMAND)
{
build(genomeDir,modelDir,order,alpha, threads);
} else if (command == PREDICT_COMMAND)
{
predict(genomeDir,modelDir,resultDir, threads, true, bestPred, negFitFile,zScores,writePvalMatrix);
} else {
die(std::string("Bad options.\n") + helpText);
}
return 0;
}