-
Notifications
You must be signed in to change notification settings - Fork 1
/
elf.cpp
1198 lines (889 loc) · 25 KB
/
elf.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
//////////////////////////////////////////////////////////////////
// //
// PLINK (c) 2005-2009 Shaun Purcell //
// //
// This file is distributed under the GNU General Public //
// License, Version 2. Please see the file COPYING for more //
// details //
// //
//////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include "plink.h"
#include "helper.h"
#include "options.h"
#include "perm.h"
#include "stats.h"
#include "linear.h"
#include "logistic.h"
extern Plink * PP;
class RCount
{
public:
RCount(Plink * p_, map<Range,int2> * rl_)
{
P = p_;
// not used now:
rangeLookup = rl_;
rval.resize(P->n,0); // Potentially weighted score
aval.resize(P->n,0); // 0/1/2 for present rare allele
gval.resize(P->n,0); // 0/1 for genotyped or not
nsnps = nalleles = 0;
npc = 0;
pcMode = false;
domModel = true;
}
Plink * P;
map<Range,int2> * rangeLookup;
vector_t rval;
vector<int> gval;
vector<int> aval;
// Current SNPs in window -> SNP specific counts
map<int,vector_t > rwin;
map<int,vector<int> > gwin;
map<int,vector<int> > awin;
double acnt, ucnt;
int acnt2, ucnt2;
int nsnps;
int nalleles;
int npc;
bool pcMode;
bool domModel;
bool addSNP(int l);
bool removeSNP(int l);
bool setWindow(int chr, int bp);
void displayWindow();
void loadCovariate();
void loadPCACovariate();
void mainStats();
bool ignorePosition();
};
bool RCount::ignorePosition()
{
if ( nsnps < 1 )
return true;
return nalleles < 5;
}
void RCount::loadCovariate()
{
// Place as last covariate
for (int i=0; i<P->n; i++)
{
P->sample[i]->clist[ par::clist_number - 1 ] =
gval[i] > 0 ?
rval[i] / (double)(gval[i]) :
0;
}
}
void RCount::loadPCACovariate()
{
// In place of simply counting all the rare SNPs, perform a PCA on the
// rare SNP data matrix, then sum the standardized PCs above a certian
// threshold (i.e. this way giving equal weight to equal independently
// detected component of rare variation)
vector<int> snplist;
map<int,vector_t>::iterator i = rwin.begin();
while ( i != rwin.end() )
{
snplist.push_back( i->first );
++i;
}
boolmatrix_t mask;
matrix_t g;
bool dominantModel = true;
geno2matrix( snplist , g , mask , dominantModel );
vector_t pc;
matrix_t ps;
matrix_t pv;
matrix_t g0 = g;
// Setting last flag to false implies no mean-centering
// This version of PCA return U.W*.V' in 'g', where
// W* is an editted eigen-value set, such that they
// equal eithe 0 or 1
bool meanCentre = par::elf_pcmode_2sided;
int pcn = pca( g , mask , pc , ps , pv, meanCentre);
int ntot = g[0].size();
if ( ! par::elf_pcmode_2sided )
{
// Calculate score which is sum of squares of U.W*.V'
vector_t sc(P->n,0);
for (int i=0; i<P->n; i++)
{
for (int p = 0 ; p < ntot ; p++ )
sc[i] += g[i][p] * g[i][p];
}
// Standardize and threshold at 4SD
double m = 0;
double ssq = 0;
double v = 0;
for (int i=0; i<P->n; i++)
{
m += sc[i];
ssq += sc[i] * sc[i];
}
m /= P->n;
ssq /= P->n;
v = ssq - m*m;
double sd = sqrt(v);
// Now assign...
for (int i=0; i<P->n; i++)
{
double z = (sc[i]-m)/sd;
if ( z > 4) z = 4;
P->sample[i]->clist[ par::clist_number - 1 ] = z;
}
}
else
{
// Resize covariate load
par::clist_number -= npc;
// Place as set of covariates, 1 past last covariate
int start = par::clist_number;
par::clist_number += pcn;
// End
int end = par::clist_number-1;
P->clistname.resize( par::clist_number );
// cout << "pcn, npc = " << pcn << " " << npc << "\n";
// cout << "Loading covars : " << start << " to " << end << "\n";
for (int i=0; i<P->n; i++)
{
P->sample[i]->clist.resize( par::clist_number );
int k=0;
for (int j=start; j<=end; j++)
{
P->sample[i]->clist[ j ] = ps[i][k++];
}
}
// Track number of PCs so they can be accounted for in the next analysis
npc = pcn;
}
}
bool RCount::setWindow(int chr, int bp)
{
// Find all SNPs with x kb of bp, and add to rwin, if not already in
// there Also, keep track of what we have added, and remove any SNPs
// that should no longer be in the window
// Return true if window actually changes since last position
bool changed = false;
set<int> nwin;
// Use a Range to lookup the SNPs in this range
Range r;
r.chr = chr;
r.start = bp - (int)par::rarer_dist_threshold;
r.stop = bp + (int)par::rarer_dist_threshold;
// Start and stop sites for this range:
int2 l2 = mapSNPs2Range( *PP , &r );
/////////////////////////////
// Need to add any new SNPs?
if ( l2.p1 != -1 )
for (int l = l2.p1 ; l <= l2.p2 ; l++ )
{
if ( addSNP(l) )
changed = true;
nwin.insert(l);
}
////////////////////////
// Need to remove any?
map<int, vector_t >::iterator iter = rwin.begin();
set<int> toRemove;
while ( iter != rwin.end() )
{
if ( nwin.find( iter->first ) == nwin.end() )
toRemove.insert( iter->first );
++iter;
}
set<int>::iterator i2 = toRemove.begin();
while( i2 != toRemove.end() )
{
if ( removeSNP( *i2 ) )
changed = true;
++i2;
}
return changed;
}
void RCount::displayWindow()
{
int rmin = 9999999;
int rmax = -1;
map<int, vector_t >::iterator iter = rwin.begin();
while ( iter != rwin.end() )
{
if ( iter->first < rmin )
rmin = iter->first;
if ( iter->first > rmax )
rmax = iter->first;
++iter;
}
acnt = 0;
ucnt = 0;
for (int i=0; i<P->n; i++)
if ( P->sample[i]->pperson->aff )
acnt += rval[i];
else
ucnt += rval[i];
cout << "Window from "
<< P->locus[rmin]->name << "("
<< P->locus[rmin]->bp << ") to "
<< P->locus[rmax]->name << "("
<< P->locus[rmax]->bp << ") to "
<< rwin.size() << " SNPs with vals (A/U) "
<< acnt << " and "
<< ucnt
<< "\n";
}
void RCount::mainStats()
{
nsnps = rwin.size();
acnt = 0;
ucnt = 0;
acnt2 = 0;
ucnt2 = 0;
int acnt3 = 0;
int ucnt3 = 0;
for (int i=0; i<P->n; i++)
{
if ( ! P->sample[i]->missing )
{
if ( par::bt )
{
if ( P->sample[i]->pperson->aff )
{
acnt += rval[i];
++acnt2;
acnt3 += aval[i];
}
else
{
ucnt += rval[i];
++ucnt2;
ucnt3 += aval[i];
}
}
else
{
ucnt += rval[i];
++ucnt2;
ucnt3 += aval[i];
}
}
}
// Number of low-frequency alleles in
// this window
nalleles = (int)acnt3 + (int)ucnt3;
// The proportion of alleles that are LF
if ( par::bt && acnt2>0)
acnt /= (double)acnt2;
if ( ucnt2>0 )
ucnt /= (double)ucnt2;
}
bool RCount::addSNP(int l)
{
if ( rwin.find(l) != rwin.end() )
return false;
if ( P->locus[l]->freq > par::rarer_maf_threshold )
{
return false;
}
vector_t r(P->n,0);
vector<int> g(P->n,0);
vector<int> a(P->n,0);
double wt;
if ( par::rare_test_weight1 )
wt = 1/P->locus[l]->freq;
CSNP * snp = P->SNP[l];
for (int i=0; i<P->n; i++)
{
//////////////////////////////////////////
// Get and parse genotypes
bool one = snp->one[i];
bool two = snp->two[i];
// Skip if missing
if ( one && !two )
continue;
if ( domModel )
{
// Dominant coding
if ( ( ! one ) || ( ! two ) )
{
r[i] += par::rare_test_weight1 ? wt : 1 ;
++a[i];
}
g[i] += 1;
}
else
{
// Additive coding
if ( ! one )
{
r[i] += par::rare_test_weight1 ? wt : 1 ;
++a[i];
}
if ( ! two )
{
r[i] += par::rare_test_weight1 ? wt : 1 ;
++a[i];
}
g[i] += 2;
}
// Add to current total per person
rval[i] += r[i];
aval[i] += a[i];
if ( domModel )
gval[i] += 1;
else
gval[i] += 2;
}
rwin.insert(make_pair( l , r ));
gwin.insert(make_pair( l , g ));
awin.insert(make_pair( l , a ));
return true;
}
bool RCount::removeSNP(int l)
{
map<int, vector_t >::iterator iter = rwin.find(l);
// If SNP never added to window, nothing to do
if ( iter == rwin.end() )
return false;
map<int, vector<int> >::iterator giter = gwin.find(l);
map<int, vector<int> >::iterator aiter = awin.find(l);
for (int i=0; i<P->n; i++)
{
rval[i] -= iter->second[i];
gval[i] -= giter->second[i];
aval[i] -= aiter->second[i];
}
rwin.erase(iter);
gwin.erase(giter);
awin.erase(aiter);
return true;
}
// Other output helper functions
void displayScoresPerson(ofstream & O, RCount & rc)
{
for (int i = 0 ; i < PP->n ; i++ )
{
O << setw(par::pp_maxfid ) << PP->sample[i]->fid << " "
<< setw(par::pp_maxiid ) << PP->sample[i]->iid << " ";
if ( PP->sample[i]->missing )
O << "NA" << "\t" << "NA" << "\t" << "NA" << "\n";
else
O << PP->sample[i]->phenotype << "\t"
<< PP->sample[i]->clist[ par::clist_number - 1 ] << "\t"
<< rc.aval[i] << "\t"
<< rc.gval[i] << "\n";
}
}
void displayScoresRegion(ofstream & O, RCount & rc)
{
map<int,vector<int> >::iterator i = rc.awin.begin();
while ( i != rc.awin.end() )
{
int count = 0;
for ( int k = 0 ; k < i->second.size(); k++)
count += i->second[k];
O << setw(4) << PP->locus[ i->first ]->chr << " "
<< setw(par::pp_maxsnp ) << PP->locus[ i->first ]->name << " "
<< setw(12) << PP->locus[ i->first ]->bp << " "
<< setw(12) << PP->locus[ i->first ]->freq << " "
<< setw(12) << PP->locus[ i->first ]->allele1 << " "
<< setw(12) << count << "\n";
++i;
}
}
void Plink::permTestRareDistribution(Perm & perm)
{
printLOG("Testing for Enrichment of Low Frequency variants ");
if ( par::rare_test_weight1 )
printLOG(" (1/MAF weighting, ");
else
printLOG(" ( MAF < "
+dbl2str(par::rarer_maf_threshold)
+", ");
printLOG("within "
+int2str(int(par::rarer_dist_threshold/1000))
+" kb)\n");
////////////////////////////
// Use last covariate slot
par::assoc_glm_without_main_snp = true;
par::clist = true;
if ( !par::elf_pcmode_2sided )
{
++par::clist_number;
clistname.push_back("RCNT");
for (int i=0; i<n; i++)
sample[i]->clist.push_back(0);
}
// NOTE: Not used now
map<Range,int2> ranges;
///////////////////////
// Original
vector_t original = testRareDistribution(perm, true, ranges);
///////////////////////
// Set up permutations
perm.setTests( original.size() );
perm.setPermClusters(*this);
perm.originalOrder();
if ( ! par::permute )
return;
if (par::mperm_rank)
perm.setOriginalRanking(original);
//////////////////////
// Begin permutations
bool finished = false;
while(!finished)
{
perm.permuteInCluster();
vector_t pr = testRareDistribution(perm,false, ranges);
finished = perm.update(pr,original);
}
if (!par::silent)
cout << "\n\n";
/////////////////////////////////////////////////////////////////////
// Write results to file
ofstream ASC;
string f;
if (par::adaptive_perm) f = par::output_file_name + ".elf.perm";
else f = par::output_file_name + ".elf.mperm";
ASC.open(f.c_str(),ios::out);
ASC.precision(4);
printLOG("Writing permutation association results to [ " + f + " ] \n");
ASC << setw(4) << "CHR" << " "
<< setw(par::pp_maxsnp)<< "SNP" << " "
<< setw(12)<< "STAT" << " "
<< setw(12) << "EMP1" << " ";
if (par::adaptive_perm)
ASC << setw(12)<< "NP" << " ";
else if ( par::mperm_rank )
ASC << setw(12)<< "EMP3" << " "
<< setw(12)<< "RANK" << " ";
else
ASC << setw(12)<< "EMP2" << " ";
ASC << "\n";
for (int l=0; l< original.size(); l++)
{
// Skip?, if filtering p-values
if ( par::pfilter && perm.pvalue(l) > par::pfvalue )
continue;
// ASC << setw(4) << locus[l]->chr << " "
// << setw(par::pp_maxsnp) << locus[l]->name << " ";
ASC << setw(8) << l << " ";
ASC << setw(12) << original[l] << " "
<< setw(12) << perm.pvalue(l) << " ";
if (par::adaptive_perm)
ASC << setw(12) << perm.reps_done(l) << " ";
else if ( par::mperm_rank )
ASC << setw(12) << perm.max_pvalue(l) << " "
<< setw(12) << perm.rank(l) << " ";
else
ASC << setw(12) << perm.max_pvalue(l) << " ";
ASC << "\n";
}
ASC.close();
}
vector_t Plink::testRareDistribution(Perm & perm , bool disp,
map<Range,int2> & ranges)
{
/////////////////////////////////////////////////////////////////////
// Write results to file
ofstream OUT;
OUT.precision(4);
ofstream SUM;
const double pthresh = 0.01;
bool one_sided = true;
ofstream SDET_SNP;
ofstream SDET_IND;
if ( disp )
{
string f = par::output_file_name + ".elf";
OUT.open(f.c_str(),ios::out);
printLOG("Writing results to [ " + f + " ]\n");
OUT << setw(4) << "CHR" << " "
<< setw(12) << "BP1" << " "
<< setw(12) << "BP2" << " "
<< setw(12) << "BP" << " "
<< setw(6) << "NSNP" << " "
<< setw(8) << "NALLELE" << " ";
if ( par::bt )
{OUT << setw(8) << "ACNT" << " "
<< setw(8) << "UCNT" << " ";
if ( ! par::elf_pcmode_2sided )
OUT << setw(10) << "OR" << " ";
}
else
{
OUT << setw(8) << "CNT" << " ";
if ( ! par::elf_pcmode_2sided )
OUT << setw(10) << "BETA" << " ";
}
OUT << setw(10) << "CHISQ" << " "
<< setw(10) << "P" << "\n";
}
// Use regression model: put # of rare variants per individual as a
// covariate, and use glmAssoc()
// We do not know how many results we will obtain to start off with
vector_t results;
RCount rc(this,&ranges);
if ( par::elf_pcmode )
rc.pcMode = true;
vector_t b;
double chisq;
double pvalue;
int srange_cnt = 0;
bool inRange = false;
int startChromosome = locus[ 0 ]->chr;
int finalChromosome = locus[ nl_all - 1]->chr;
for (int chr = startChromosome ; chr <= finalChromosome; chr++)
{
int bpstart = scaffold[chr].bpstart;
int bpstop = scaffold[chr].bpstop;
for ( int bp = bpstart; bp <= bpstop; bp += par::rarer_interval )
{
bool windowMoved = rc.setWindow(chr,bp);
//rc.displayWindow();
// If no new SNPs have been added or removed from window,
// then just serve up the same results as last time
if ( ! windowMoved )
{
continue;
}
rc.mainStats();
// Enough to be bothering with?
if ( rc.ignorePosition() )
{
continue;
}
// Perform actual test
if ( rc.pcMode )
rc.loadPCACovariate();
else
rc.loadCovariate();
glmAssoc( false , perm );
// Get results
double beta;
if ( ! par::elf_pcmode_2sided )
{
model->testParameter = par::clist_number;
b = model->getCoefs();
chisq = model->getStatistic();
pvalue = chiprobP(chisq,1);
beta = b[ par::clist_number ];
}
else
{
b = model->getCoefs();
beta = 1; // no direction
vector_t h; // dim = number of fixes (to =0)
matrix_t H; // row = number of fixes; cols = np
h.resize(rc.npc,0);
sizeMatrix(H,rc.npc,model->getNP());
int startpc = par::clist_number - rc.npc;
for (int i=0; i<rc.npc; i++)
H[i][startpc + i + 1 ] = 1;
chisq = model->isValid() ? model->linearHypothesis(H,h) : 0;
pvalue = model->isValid() ? chiprobP(chisq, rc.npc) : -1;
}
// Permutation test is 1-sided
if ( beta < 0 )
results.push_back( 0 );
else
results.push_back( chisq );
// Clean up
delete model;
// Write results to a file?
if ( disp )
{
double coef = par::bt ? exp( beta ) : beta ;
int bp1 = bp - (int)par::rarer_dist_threshold < bpstart ?
bpstart : bp - (int)par::rarer_dist_threshold;
int bp2 = bp + (int)par::rarer_dist_threshold > bpstop ?
bpstop : bp + (int)par::rarer_dist_threshold;
OUT << setw(4) << chr << " "
<< setw(12) << bp1 << " "
<< setw(12) << bp2 << " "
<< setw(12) << (int)((bp1+bp2)/2.0) << " "
<< setw(6) << rc.nsnps << " "
<< setw(8) << rc.nalleles << " ";
if ( par::bt ) OUT << setw(8) << rc.acnt << " ";
OUT << setw(8) << rc.ucnt << " ";
if ( ! par::elf_pcmode_2sided )
OUT << setw(10) << coef << " ";
OUT << setw(10) << chisq << " "
<< setw(10) << pvalue << "\n";
OUT.flush();
if ( par::rare_test_print_details &&
int2str(chr)+":"+int2str( (int)((bp1+bp2)/2.0)) == par::rare_test_print_details_snp )
{
printLOG("Printing details for region around " + par::rare_test_print_details_snp + "\n");
SDET_SNP.open( ( par::output_file_name+".elf.det."
+ par::rare_test_print_details_snp + ".snp" ).c_str() , ios::out );
SDET_IND.open( ( par::output_file_name+".elf.det."
+ par::rare_test_print_details_snp + ".ind" ).c_str() , ios::out );
//////////////////////////////////
// Print scores per person, and per SNP
displayScoresPerson( SDET_IND , rc );
SDET_IND.close();
displayScoresRegion( SDET_SNP , rc );
SDET_SNP.close();
}
} // end if verbose display mode
} // Next window location
}
// Finished, close any open streams
if ( disp )
{
OUT.close();
}
return results;
}
void Plink::displayRareRange()
{
map<string, set<Range> > ranges = readRange( par::rare_test_score_range_file );
printLOG("Reading ELF results file from [ "
+ par::rare_test_score_results_file + " ]\n");
printLOG("Reading ELF ranges from [ "
+ par::rare_test_score_range_file + " ]\n");
checkFileExists( par::rare_test_score_results_file );
ifstream IN;
IN.open( par::rare_test_score_results_file.c_str() );
// Read first row
int pcol = -1;
int bpcol = -1;
int bcol = -1;
// int snpcol = -1;
int chrcol = -1;
vector<string> tokens = tokenizeLine(IN);
for (int i = 0 ; i < tokens.size() ; i++)
{
if ( tokens[i] == "P" )
pcol = i;
if ( tokens[i] == "OR" || tokens[i] == "BETA" )
bcol = i;
// if ( tokens[i] == "SNP" )
// snpcol = i;
if ( tokens[i] == "CHR" )
chrcol = i;
if ( tokens[i] == "BP" )
bpcol = i;
}
int ncol = tokens.size();
if ( pcol == -1 )
error("Could not find P field in header");
// if ( snpcol == -1 )
// error("Could not find SNP field in header");
if ( bpcol == -1 )
error("Could not find BP field in header");
if ( chrcol == -1 )
error("Could not find CHR field in header");
bool no_beta = false;
if ( bcol == -1 )
{
no_beta = true;
printLOG("Couldn't find OR/BETA field, so reporting all regions\n");
}
// map<int2, string> snpmap;
map<int2, double> pmap;
map<int2, double> bmap;
while ( !IN.eof() )
{
vector<string> tokens = tokenizeLine(IN);
if ( tokens.size() == 0 )
continue;
if ( tokens.size() != ncol )
error("Wrong number of columns in input file");
double p, b=1;
int chr, bp;
// string snp;
//snp = tokens[snpcol];
chr = getChromosomeCode( tokens[chrcol ] );
if ( ! from_string<double>( p , tokens[pcol] , std::dec ) )
p = 1;
if ( ! no_beta )
{
if ( ! from_string<double>( b , tokens[bcol] , std::dec ) )
b = 1;
}
if ( ! from_string<int>( bp , tokens[bpcol] , std::dec ) )
error("Problem converting BP value: " + tokens[bpcol] );
int2 t( chr , bp );
// snpmap.insert( make_pair( t , snp ) );
pmap.insert( make_pair( t , p ) );
bmap.insert( make_pair( t , b ) );
}
IN.close();
ofstream SUM;
printLOG("Writing range summary to [ " + par::output_file_name + ".elf.summary ]\n");
SUM.open( ( par::output_file_name + ".elf.summary").c_str() , ios::out );
SUM << setw(4) << "CHR" << " "
<< setw(12) << "BP1" << " "
<< setw(12) << "BP2" << " "
<< setw(12) << "BESTP" << " "
<< "GENES" << "\n";
map<int2,double>::iterator i = pmap.begin();
int srange_cnt = 0;
bool inRange = false;
Range srange;
int l = 0;
int ntot = pmap.size() - 1;
double bestp = 1;
while ( i != pmap.end() )
{
double pvalue = i->second;
double coef;
if ( no_beta )
coef = 99 ;
else