forked from aehrc/BitEpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitEpi.cpp
2990 lines (2610 loc) · 78.3 KB
/
BitEpi.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Uncomment below line to perform performance test and see accurate timing of different parts of the program.
// It is only used for performance testing and may not be maintained
//#define PTEST
//#define DEBUG
// In order to compile BitEpi in VisualStudio we define a pthread empty shell here
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
typedef int pthread_mutex_t;
int pthread_mutex_trylock(pthread_mutex_t *x)
{
if (*x == 0)
{
*x = 1;
return 0;
}
else
return 1;
}
void pthread_mutex_init(pthread_mutex_t *x, int *y)
{
*x = 0;
}
typedef int pthread_t;
typedef int pthread_attr_t;
void pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg)
{
(*start_routine)(arg);
}
void pthread_join(pthread_t thread, void **retval)
{
return;
}
#else
#include "pthread.h"
#include "unistd.h"
#endif
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include <algorithm>
#include <chrono>
#include <iostream>
#include <fstream>
#include <vector>
#include "math.h"
#include "csvparser.h"
//#define V2
#define MIN_COMB_IN_JOB 9000.0
#define MIN_TOP 1000
#ifdef PTEST
clock_t elapse[100];
#endif
typedef unsigned char uint8;
typedef unsigned short int uint16;
typedef unsigned int uint32;
typedef unsigned long long int uint64;
typedef int int32;
typedef unsigned int varIdx;
typedef unsigned short int sampleIdx; // This type used in contingency table. This table should be kept in the cache so choose the smallest possible type here. Note that short int is 16 bit and can deal with up to 2^16 (~65,000) samples.
typedef unsigned long long int word; // for parallel processing
// we use 2 bits (4 states) to represent a genotype. However a genotype has only 3 states.
// for 4-SNP, blow table is used to translate (4 states)^(4 SNPs) state to (3 states)^(4 SNPs)
const uint8 cti[81] = { 0,1,2,4,5,6,8,9,10,16,17,18,20,21,22,24,25,26,32,33,34,36,37,38,40,41,42,64,65,66,68,69,70,72,73,74,80,81,82,84,85,86,88,89,90,96,97,98,100,101,102,104,105,106,128,129,130,132,133,134,136,137,138,144,145,146,148,149,150,152,153,154,160,161,162,164,165,166,168,169,170 };
const uint32 byte_in_word = sizeof(word);
#define MAX_ORDER 4
template <uint32_t EXP>
uint32_t integerPow(uint32_t x)
{
return integerPow<EXP-1>(x)*x;
}
template<>
uint32_t integerPow<1>(uint32_t x){return x;}
template<>
uint32_t integerPow<0>(uint32_t){return 1;}
#define P2(X) (X*X)
#define P3(X) (X*X*X)
#define P4(X) (X*X*X*X)
#define ERROR(X) {printf("\n *** ERROR: %s (line:%u - File %s)\n", X, __LINE__, __FILE__); exit(0);}
#define NULL_CHECK(X) {if(!X) {printf("\n *** ERROR: %s is null (line:%u - File %s)\n", #X, __LINE__, __FILE__); exit(0);}}
double ***tripletBeta;
double **PairBeta;
double *SnpBeta;
template <class D1,class D2>
double difftime(D1 end, D2 begin)
{
return std::chrono::duration<double,std::ratio<1,1>>(end - begin).count();
}
double factorial(uint32 n)
{
double res = 1;
for (uint32 i = 1; i <= n; i++)
res *= i;
return res;
}
double Combination(uint32 v, uint32 o)
{
double nc = (double)v;
if (o == 1)
return nc;
if (o == v)
return 1;
if (o > v)
return 0;
switch (o)
{
case 2: return (nc * (nc - 1)) / 2;
case 3: return (nc * (nc - 1) * (nc - 2)) / 6;
case 4: return (nc * (nc - 1) * (nc - 2) * (nc - 3)) / 24;
default: ERROR("Does not support combination above 4");
}
}
struct JOB
{
uint32 id;
uint32 s[2];
uint32 e[2];
double comb;
double diff; // Difference to average.
double aDiff; // Accumulative difference to average.
uint64 counted;
void Print()
{
printf("\n %6u (%6u,%6u) (%6u,%6u) %15.0f %15.0f %15.0f", id+1, s[0]+1, s[1]+1, e[0]+1, e[1]+1, comb, diff, aDiff);
}
void PrintHead()
{
printf("\n Outer loop iterates from S1 to E1");
printf("\n Second loop iterates from S2 to E2");
printf("\n Combinations : SNP combinations to be tested in each job");
printf("\n diffToAvg : Difference to average number of combination to be tested in each job");
printf("\n AccumDiffToAvg : Accumulative diffToAvg");
printf("\n Job ID ( S1, S2) ( E1, E2) Combinations diffToAvg AccumDiffToAvg");
}
void Print1SNP()
{
printf("\nJob:%6u process %15.0f SNPs (%10u ... %10u)", id + 1, comb, s[0] + 1, e[0] + 1);
}
};
struct ARGS
{
bool computeBeta[MAX_ORDER]; // [N] should we compute beta of order of N
bool printBeta[MAX_ORDER]; // [N] should we report beta of order of N if it meets the threshold b[N]
bool saveBeta[MAX_ORDER]; // [N] should we save beta of order of N to compute alpha of order N+1
bool computeAlpha[MAX_ORDER];// [N] should we compute alpha of order of N
bool printAlpha[MAX_ORDER]; // [N] should we report alpha of order of N in it meets the threshold a[N]
bool best; // should we compute the best intractions for each SNPs
bool betaGiven[MAX_ORDER]; // [N] if beta N is given in the parameters
bool alphaGiven[MAX_ORDER]; // [N] if alpha N is given in the parameters
double beta[MAX_ORDER];
double alpha[MAX_ORDER];
char input[1024];
char output[1024];
bool inputGiven;
bool readBfile;
// In the local mode (default) the program will parallelize the outter loop over all 'numThreads' and run all of them in parallele.
// Basically in the default mode the numJob is set to numThreads.
// In the cloud/cluster mode (-c) the program will parallelize the outter loop over all 'numJobs' and run 'numThreads' jobs in parallele.
// The first and the last job index to run are 'firstJobIdx' and 'firstJobIdx+numThreads-1' respectively
bool clusterMode;
bool clusterAlpha;
uint32 clusterOrder;
uint32 numJobs; // the best value is the total number of threads in the cluster.
JOB *jobs;
uint32 numThreads; // Number of threads on the processing nodes;
uint32 firstJobIdx; // 0 <= firstJobIdx <= numJobs-1
uint32 lastJobIdx; // firstJobIdx <= lastJobIdx <= numJobs-1
uint32 jobsToDo; // number of jobs to do on this computer (could be less than number of threads)
double bufRatio;
bool topNbeta[MAX_ORDER]; // the the beta thrashold is top count
bool topNalpha[MAX_ORDER]; // the the alpha thrashold is top count
bool master;
char configFileName[1024];
char clusterCmd[1024];
char awsKey[1024];
char sshCmd[1024];
char scpCmd[1024];
bool aws;
uint32 maxOrder;
bool sort;
// internal use
double numComb;
double avgJobNumCombinations;
ARGS()
{
memset(this, 0, sizeof(ARGS));
numThreads = 1;
firstJobIdx = -1;
numJobs = -1;
for (uint32 o = 0; o < MAX_ORDER; o++)
beta[o] = alpha[o] = -1;
srand(std::chrono::high_resolution_clock::now().time_since_epoch().count());
sprintf(output, "OUTPUT_BitEpi_%012u", rand());
sprintf(sshCmd, "ssh ");
sprintf(scpCmd, "scp ");
bufRatio = 10;
clusterAlpha = false;
clusterOrder = -1;
}
~ARGS()
{
if (jobs)
{
delete[]jobs;
}
}
void WorkloadDividerHigherOrder(uint32 order, uint32 numVar, uint32 numJobsToDivide)
{
uint32 lorder = order - 2;
jobs[0].s[0] = 0;
jobs[0].s[1] = 1;
if (numJobsToDivide == 1)
{
jobs[0].e[0] = numVar - order;
jobs[0].e[1] = (numVar - order) + 1;
jobs[0].comb = numComb;
}
else
{
uint32 idxJob = 0;
double sumComb = 0;
double remainingComb = numComb;
double aDiff = 0; // Accumulative difference to average.
for (uint32 i = 0; i <= (numVar - order); i++)
{
for (uint32 j = i + 1; j <= (numVar - order + 1); j++)
{
double comb = Combination(numVar - (j + 1), lorder);
if ((sumComb + comb) >= avgJobNumCombinations)
{
if(aDiff < 0)
{
jobs[idxJob].e[0] = i;
jobs[idxJob].e[1] = j;
jobs[idxJob].comb = (sumComb + comb);
jobs[idxJob].diff = jobs[idxJob].comb - avgJobNumCombinations;
aDiff += jobs[idxJob].diff;
jobs[idxJob].aDiff = aDiff;
remainingComb -= jobs[idxJob].comb;
idxJob++;
if ((j + 1) == (numVar - order + 2)) // boarder condition
{
jobs[idxJob].s[0] = i+1;
jobs[idxJob].s[1] = i+2;
}
else
{
jobs[idxJob].s[0] = i;
jobs[idxJob].s[1] = j + 1;
}
sumComb = 0;
}
else // if(aDiff >= 0)
{
if ((j - 1) == i) // boarder condition
{
jobs[idxJob].e[0] = i - 1;
jobs[idxJob].e[1] = numVar - order + 1;
}
else
{
jobs[idxJob].e[0] = i;
jobs[idxJob].e[1] = j - 1;
}
jobs[idxJob].comb = sumComb;
jobs[idxJob].diff = jobs[idxJob].comb - avgJobNumCombinations;
aDiff += jobs[idxJob].diff;
jobs[idxJob].aDiff = aDiff;
remainingComb -= jobs[idxJob].comb;
idxJob++;
jobs[idxJob].s[0] = i;
jobs[idxJob].s[1] = j;
sumComb = comb;
}
if (idxJob == (numJobsToDivide - 1))
{
break;
}
}
else
sumComb += comb;
}
if (idxJob == (numJobsToDivide - 1))
{
break;
}
}
jobs[idxJob].e[0] = numVar - order;
jobs[idxJob].e[1] = numVar - order + 1;
jobs[idxJob].comb = remainingComb;
jobs[idxJob].diff = jobs[idxJob].comb - avgJobNumCombinations;
aDiff += jobs[idxJob].diff;
jobs[idxJob].aDiff = aDiff;
}
}
void WorkloadDivider2Snp(uint32 order, uint32 numVar, uint32 numJobsToDivide)
{
uint32 lorder = order - 1;
jobs[0].s[0] = 0;
if (numJobs == 1)
{
jobs[0].e[0] = numVar - order;
jobs[0].comb = numComb;
}
else
{
uint32 idxJob = 0;
double sumComb = 0;
double remainingComb = numComb;
double aDiff = 0; // Accumulative difference to average.
for (uint32 i = 0; i <= (numVar - order); i++)
{
double comb = Combination(numVar - (i + 1), lorder);
if ((sumComb + comb) >= avgJobNumCombinations)
{
if (aDiff < 0)
{
jobs[idxJob].e[0] = i;
jobs[idxJob].comb = (sumComb + comb);
jobs[idxJob].diff = jobs[idxJob].comb - avgJobNumCombinations;
aDiff += jobs[idxJob].diff;
jobs[idxJob].aDiff = aDiff;
remainingComb -= jobs[idxJob].comb;
idxJob++;
jobs[idxJob].s[0] = i+1;
sumComb = 0;
}
else // if(aDiff >= 0)
{
jobs[idxJob].e[0] = i - 1;
jobs[idxJob].comb = sumComb;
jobs[idxJob].diff = jobs[idxJob].comb - avgJobNumCombinations;
aDiff += jobs[idxJob].diff;
jobs[idxJob].aDiff = aDiff;
remainingComb -= jobs[idxJob].comb;
idxJob++;
jobs[idxJob].s[0] = i;
sumComb = comb;
}
if (idxJob == (numJobs - 1))
{
break;
}
}
else
sumComb += comb;
}
jobs[idxJob].e[0] = numVar - order;
jobs[idxJob].comb = remainingComb;
jobs[idxJob].diff = jobs[idxJob].comb - avgJobNumCombinations;
aDiff += jobs[idxJob].diff;
jobs[idxJob].aDiff = aDiff;
}
}
void WorkloadDivider(uint32 order, uint32 numVar, uint32 numJobsToDivide)
{
if (jobs)
{
delete[] jobs;
}
jobs = new JOB[numJobsToDivide];
NULL_CHECK(jobs);
memset(jobs, 0, sizeof(JOB)*numJobsToDivide);
numComb = Combination(numVar, order);
avgJobNumCombinations = numComb / numJobsToDivide;
printf("\n Total number of %u-SNP combinations to be tested: %20.0f", order, numComb);
printf("\n Total number of jobs: %20u", numJobsToDivide);
printf("\n Average number of combintions to be tested in each job: %20.0f", avgJobNumCombinations);
printf("\n");
if ((avgJobNumCombinations < MIN_COMB_IN_JOB) && (numJobsToDivide != 1))
{
printf("\n Average number of combination to be tested in each job is less than minimum (%10f)", MIN_COMB_IN_JOB);
uint32 iJob = (numJobsToDivide / (MIN_COMB_IN_JOB / avgJobNumCombinations)) - 1;
if (iJob < 1)
iJob = 1;
if (clusterMode)
printf("\n Reduce the number of jobs to %u", iJob);
else
printf("\n Reduce the number of thread to %u", iJob);
printf("\n");
ERROR("So Many Threads");
}
printf("\n Breaking the program into similar sized jobs");
if (order == 1) // 1-SNP
{
for (uint32 i = 0; i < numJobsToDivide; i++)
{
jobs[i].s[0] = i * avgJobNumCombinations;
jobs[i].e[0] = ((i + 1) * avgJobNumCombinations) - 1;
jobs[i].comb = avgJobNumCombinations;
}
jobs[numJobsToDivide-1].e[0] = numVar-1;
jobs[numJobsToDivide - 1].comb = jobs[numJobsToDivide - 1].e[0] - jobs[numJobsToDivide - 1].s[0] + 1;
}
else if (order == 2) // 2-SNP Only breaks outer loop
WorkloadDivider2Snp(order, numVar, numJobsToDivide);
else // 3-SNP 4-SNP breaks to two most outer loops
WorkloadDividerHigherOrder(order, numVar, numJobsToDivide);
if (order >= 2)
jobs[0].PrintHead();
for (uint32 i = 0; i < numJobsToDivide; i++)
{
if (order == 2)
{
jobs[i].s[1] = jobs[i].s[0] + 1;
jobs[i].e[1] = numVar - order + 1;
}
jobs[i].id = i;
if (order == 1)
jobs[i].Print1SNP();
else
jobs[i].Print();
}
printf("\n");
}
void PrintHelp(char* exec)
{
printf("\n\n\n========================================================\n");
printf(" -i [path] Input CSV file\n");
printf(" * First row includes labels:\n");
printf(" 1 and 0 for case and controls\n");
printf(" * First column includes SNP uniqe ids\n");
printf(" BitEpi does not check the uinquness\n");
printf(" * First entry (first col and first row) is ignored\n");
printf(" * All other entry can be 0, 1 or 2\n");
printf(" (0/0, 0/1 and 1/1 genotype respectively)\n");
printf(" -o [str] Output prefix\n");
printf(" -bfile Read input file as a *.bed file generated by PLINK 1.9\n");
printf(" The *.bim and *.fam files must also be present.\n");
printf(" -sort Sort output files by Beta and Information-Gained\n");
printf(" by alpha and beta value in decending order\n");
printf(" -t [int] number of threads\n");
#ifdef V2
printf(" -c Cloud/Cluster mode");
printf(" -j [int] Total number of jobs\n");
printf(" -f [int] first job index (starting from 1)\n");
printf(" -m [path] Run master program\n");
printf(" Path to the config file\n");
printf(" -k [path] Path to the key (aws)\n");
#endif
printf(" -best find the best interactions for each SNP\n");
printf(" -b1 [thr] Compute 1-SNP beta test\n");
printf(" -b2 [thr] Compute 2-SNP beta test\n");
printf(" -b3 [thr] Compute 3-SNP beta test\n");
printf(" -b4 [thr] Compute 4-SNP beta test\n");
printf(" -a1 [thr] Compute 1-SNP alpha test\n");
printf(" -a2 [thr] Compute 2-SNP alpha test\n");
printf(" -a3 [thr] Compute 3-SNP alpha test\n");
printf(" -a4 [thr] Compute 4-SNP alpha test\n");
printf("\n");
printf("* if thr<1 then thr is the min threshold on alpha and beta test.\n");
printf("* if thr>=1 then thr is the number of top hits in alpha and beta test.\n");
printf("* if thr>=1 and more than 1 thread is used each thread reports\n");
printf(" thr top hits. So (t*thr) top hits will be reported.\n");
printf(" You can use -sort option and only consider the top thr record.\n");
printf("* thr is optional.\n");
printf(" If you don't pass a thr the program computes the metric but\n");
printf(" it does not report anything (performance testing).\n");
printf("* if you want all interactions set thr to 0.\n");
//printf("* -r [float] is a buffer ratio set to 2 by default.\n");
printf("\n==========================================================\n");
ERROR("Please enter valid arguments");
return;
}
void Parse(int argc, char* argv[])
{
double d = -1;
uint32 o = 0;
char str[100];
bool next = false;
// Read arguments
for (int i = 1; i < argc; i++)
{
next = false;
// check if beta flag is passed for any order
for (uint32 o = 0; o < MAX_ORDER; o++)
{
sprintf(str, "-b%u", o + 1);
if (!strcmp(argv[i], str))
{
// set the computep flag
computeBeta[o] = true;
betaGiven[o] = true;
sprintf(clusterCmd, "%s -b%u", clusterCmd, o + 1);
// check if there is any threashold argument to this option
if ((i + 1) != argc)
{
if (argv[i + 1][0] != '-')
{
d = atof(argv[i + 1]);
if ((d == 0) && (argv[i + 1][0] != '0'))
{
printf("\n Cannot parse -b%u [%s]", o + 1, argv[i + 1]);
PrintHelp(argv[0]);
}
// set the threshold and print flag for beta
sprintf(clusterCmd, "-b%u %f", o+1, d);
beta[o] = d;
printBeta[o] = true;
if (beta[o] >= 1)
topNbeta[o] = true;
i++;
}
}
next = true;
break;
}
}
// if this option is processed move to the next option
if (next) continue;
// check if flag is passed for any order
for (uint32 o = 0; o < MAX_ORDER; o++)
{
sprintf(str, "-a%u", o + 1);
if (!strcmp(argv[i], str))
{
// set the computep flag and beta flag of previous order
computeBeta[o] = computeAlpha[o] = true;
alphaGiven[o] = true;
sprintf(clusterCmd, "%s -a%u", clusterCmd, o + 1);
if (o>0)
computeBeta[o - 1] = saveBeta[o - 1] = true;
// check if there is any threashold argument to this option
if ((i + 1) != argc)
{
if (argv[i + 1][0] != '-')
{
d = atof(argv[i + 1]);
if ((d == 0) && (argv[i + 1][0] != '0'))
{
printf("\n Cannot parse -a%u [%s]", o + 1, argv[i + 1]);
PrintHelp(argv[0]);
}
// set the threshold and print flag for a
sprintf(clusterCmd, "-a%u %f", o+1, d);
alpha[o] = d;
printAlpha[o] = true;
if (alpha[o] >= 1)
topNalpha[o] = true;
i++;
}
}
next = true;
break;
}
}
if (next) continue;
// read input file name
if (!strcmp(argv[i], "-i"))
{
if ((i + 1) == argc)
{
printf("\n Please enter path to the input file");
PrintHelp(argv[0]);
}
if (argv[i + 1][0] != '-')
{
strcpy(input, argv[i + 1]);
inputGiven = true;
}
else
{
printf("\n Please enter path to the input file");
PrintHelp(argv[0]);
}
i++;
continue;
}
// read output file prefix
if (!strcmp(argv[i], "-o"))
{
if ((i + 1) == argc)
{
printf("\n Please enter an output prefix");
PrintHelp(argv[0]);
}
if (argv[i + 1][0] != '-')
strcpy(output, argv[i + 1]);
else
{
printf("\n Please enter an output prefix");
PrintHelp(argv[0]);
}
i++;
continue;
}
// read number of threads
if (!strcmp(argv[i], "-t"))
{
if ((i + 1) == argc)
{
printf("\n Please enter the number of threads");
PrintHelp(argv[0]);
}
if (argv[i + 1][0] != '-')
{
numThreads = atoi(argv[i + 1]);
if (numThreads == 0)
{
printf("\n Please enter a valid number of threads greater than 0 but not [%s]", argv[i + 1]);
PrintHelp(argv[0]);
}
}
else
{
printf("\n Please enter the number of threads");
PrintHelp(argv[0]);
}
i++;
continue;
}
// read bfile flag
if (!strcmp(argv[i], "-bfile"))
{
readBfile = true;
continue;
}
// read best flag
if (!strcmp(argv[i], "-best"))
{
best = true;
continue;
}
// read best flag
if (!strcmp(argv[i], "-sort"))
{
sprintf(clusterCmd, "%s -sort", clusterCmd);
sort = true;
continue;
}
// read bufRatio
if (!strcmp(argv[i], "-r"))
{
if ((i + 1) == argc)
{
printf("\n Please enter the buffer ratio");
PrintHelp(argv[0]);
}
if (argv[i + 1][0] != '-')
{
bufRatio = atof(argv[i + 1]);
if (bufRatio < 2)
{
printf("\n Please enter a valid ratio for buffer greater than 2 but not [%s]", argv[i + 1]);
PrintHelp(argv[0]);
}
}
else
{
printf("\n Please enter the buffer ratio");
PrintHelp(argv[0]);
}
i++;
continue;
}
#ifdef V2
// read cluster flag
if (!strcmp(argv[i], "-c"))
{
clusterMode = true;
continue;
}
// read number of jobs
if (!strcmp(argv[i], "-j"))
{
if ((i + 1) == argc)
{
printf("\n Please enter the number of jobs");
PrintHelp(argv[0]);
}
if (argv[i + 1][0] != '-')
{
numJobs = atoi(argv[i + 1]);
if (numJobs == 0)
{
printf("\n Please enter a valid number of jobs greater than 0 but not [%s]", argv[i + 1]);
PrintHelp(argv[0]);
}
}
else
{
printf("\n Please enter the number of jobs");
PrintHelp(argv[0]);
}
i++;
continue;
}
// read first job index
if (!strcmp(argv[i], "-f"))
{
if ((i + 1) == argc)
{
printf("\n Please enter the first job index (starting from 1)");
PrintHelp(argv[0]);
}
if (argv[i + 1][0] != '-')
{
firstJobIdx = atoi(argv[i + 1]);
if (firstJobIdx < 1)
{
printf("\n Please enter a valid first job index (>0) but not [%s]", argv[i + 1]);
PrintHelp(argv[0]);
}
firstJobIdx--;
}
else
{
printf("\n Please enter the first job index (starting from 1)");
PrintHelp(argv[0]);
}
i++;
continue;
}
// read serverlist file name
if (!strcmp(argv[i], "-m"))
{
if ((i + 1) == argc)
{
printf("\n Please enter path to the file containing serverlists");
PrintHelp(argv[0]);
}
if (argv[i + 1][0] != '-')
{
strcpy(configFileName, argv[i + 1]);
master = true;
}
else
{
printf("\n Please enter path to the file containing serverlists");
PrintHelp(argv[0]);
}
i++;
continue;
}
// read key file name
if (!strcmp(argv[i], "-k"))
{
if ((i + 1) == argc)
{
printf("\n Please enter path to the key file");
PrintHelp(argv[0]);
}
if (argv[i + 1][0] != '-')
{
strcpy(awsKey, argv[i + 1]);
aws = true;
sprintf(sshCmd, "ssh -i %s", awsKey);
sprintf(scpCmd, "scp -i %s", awsKey);
}
else
{
printf("\n Please enter path to the key file");
PrintHelp(argv[0]);
}
i++;
continue;
}
#endif
printf("\n Invalid option %s", argv[i]);
PrintHelp(argv[0]);
}
// check arguments
// There should be an input file in all cases
// if the output prefix does not pass we use default output prefix that is "OUTPUT_BitEpi"
if (!inputGiven)
{
printf("\n You should specify the path to the input file (-i)");
PrintHelp(argv[0]);
}
if(master && best)
{
printf("\n best mode does not work in master mode yet");
PrintHelp(argv[0]);
}
if (!clusterMode)
{
if (numJobs != -1 || firstJobIdx != -1)
{
printf("\n -j and -f are only used in cluster/cloud mode (-c presented)");
PrintHelp(argv[0]);
}
firstJobIdx = 0;
numJobs = numThreads;
}
else
{
if (master)
{
printf("\n You run the program in master mode. You cannot use -c in master mode");
PrintHelp(argv[0]);
}
if (best)
{
printf("\n best mode does not work in cluster/cloud mode yet");
PrintHelp(argv[0]);
}
uint anCnt = 0;
for (uint32 i = 0; i < 4; i++)
{
if (betaGiven[i])
{
anCnt++;
clusterAlpha = false;
clusterOrder = i;
}
if (alphaGiven[i])
{
anCnt++;
clusterAlpha = true;
clusterOrder = i;
}
}
if (anCnt > 1)
{
printf("\n In cluster mode only one analysis (alpha or beta) can be executed");
PrintHelp(argv[0]);
}
if(clusterOrder==0)
{
printf("\n cluster mode works only for 2-SNP, 3-SNP and 4-SNP tests but not 1-SNP test");
PrintHelp(argv[0]);
}
if(numJobs==-1 || firstJobIdx==-1)
{
printf("\n Specify number of jobs (-j) and first job index (-f) in cluster/cloud mode (-c presented)");
PrintHelp(argv[0]);
}
}
jobsToDo = ((numJobs - firstJobIdx) < numThreads) ? (numJobs - firstJobIdx) : numThreads;
lastJobIdx = firstJobIdx + jobsToDo - 1;
if (firstJobIdx >= numJobs)
{
printf("\n First job index out of range [%u>=%u]", firstJobIdx, numJobs);
PrintHelp(argv[0]);
}
// apply best
if (best)
for (uint32 o = 0; o < MAX_ORDER; o++)
{
computeBeta[o] = saveBeta[o] = computeAlpha[o] = true;
}
if (computeBeta[0]) maxOrder = 1;
if (computeBeta[1]) maxOrder = 2;
if (computeBeta[2]) maxOrder = 3;
if (computeBeta[3]) maxOrder = 4;
if (!maxOrder)
{
printf("\n No test to be performed.");
PrintHelp(argv[0]);
}
}
void Print() // print the given option for debugging
{
printf("\n\n=========================================");
printf("\n Given Arguments:");
printf("\n input %s", input);
if (master)
{
printf("\n master %s", configFileName);
printf("\n key %s", awsKey);
return;
}
printf("\n output %s", output);
printf("\n threads %u", numThreads);
if (clusterMode)
{
printf("\n total jobs %u", numJobs);
printf("\n jobs on this machine %u", jobsToDo);
printf("\n first job index %u", firstJobIdx);
printf("\n last job index %u", lastJobIdx);
if (numThreads > jobsToDo)
{
printf("\n >>> *** Warning *** There are less jobs (%u jobs) [%u...%u] than the number of threads [%u] on this computer.", jobsToDo, firstJobIdx, numJobs - 1, numThreads);
printf("\n >>> The program utilise only %u threads to run %u jobs in parallel.", jobsToDo, jobsToDo);
}
}
if (best)
printf("\n best");
if (sort)
printf("\n sort");
for (uint32 o = 0; o < MAX_ORDER; o++)
{
if (beta[o] != -1)
printf("\n beta%u %f", o + 1, beta[o]);
else if (betaGiven[o])
printf("\n beta%u", o + 1);
if (alpha[o] != -1)
printf("\n alpha%u %f", o + 1, alpha[o]);
else if (alphaGiven[o])
printf("\n alpha%u", o + 1);
}
#ifdef DEBUG
printf("\n\n=========================================");
printf("\n Internal flags and values (For Debugging):");
printf("\n maxOrder %u", maxOrder);
for (uint32 o = 0; o < MAX_ORDER; o++)
{
printf("\n computeBeta[%u] %s", o + 1, computeBeta[o] ? "true" : "false");
printf("\n printBeta[%u] %s", o + 1, printBeta[o] ? "true" : "false");
printf("\n saveBeta[%u] %s", o + 1, saveBeta[o] ? "true" : "false");
printf("\n computeAlpha[%u] %s", o + 1, computeAlpha[o] ? "true" : "false");
printf("\n printAlpha[%u] %s", o + 1, printAlpha[o] ? "true" : "false");
}
#endif // DEBUG
printf("\n\n=========================================\n\n");
}
};
void AllocateBeta(varIdx n, ARGS args)
{
if (args.saveBeta[0])
{
SnpBeta = new double[n];