forked from lhhunghimself/fastBMA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastBMA.hpp
2452 lines (2324 loc) · 77.3 KB
/
fastBMA.hpp
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
#include <iostream>
#include <sstream>
#include <string>
#include <set>
#include <map>
#include <limits>
#include "my_sort.hpp"
#include <unordered_set>
#include <omp.h>
#include <time.h>
#include <inttypes.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <boost/math/tools/minima.hpp>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <bitset>
#include "MurmurHash3.h"
#ifdef USEMPI
#include <boost/mpi.hpp>
#include <boost/mpi/environment.hpp>
#include <boost/mpi/communicator.hpp>
#include <boost/serialization/string.hpp>
namespace mpi = boost::mpi;
#endif
#include "OpenBLAS/cblas.h" //this must go AFTER and not before the boost mpi headers for some reason
//include the following because macs don't have clockgettime
#ifdef MAC_OS
#include <mach/clock.h>
#include <mach/mach.h>
#endif
#define UNIFORM_PRIOR 2.76/6000.0
#define EPSILONFACTOR 1e-6 //will be added to uniform prior if correlation matrix is given to casue the variables to be ranked
#define MAXPRIOR 0.9999999 //needed for stability
#define NFILTERS 64
using namespace std;
template <class T> void print_array(T* array,int n,int stride);
template <class T> void print_array(T* array,int n,int stride,char *format);
void current_utc_time(struct timespec *ts);
double get_elapsed_time(const struct timespec *start_time,const struct timespec *end_time);
bool isTimedOut(struct timespec *regStart,float timeout);
//globals
//unsigned int total_checks=0;
//unsigned int total_collisions=0;
//timers
bool gtime=0;
struct timespec start_time;
struct timespec end_time;
uint32_t gMaxKeptModels=100000;
uint32_t gMaxActiveModels=100000;
double hashTime=0;
uint32_t *hashLUT=0; //hash lookup table
uint8_t gModelBits=1; //number of bits needed to represent nModels
//function to initialize hashLUT
//precalculate hashes
void initHashLUT(int nVars){
if(hashLUT) delete[] hashLUT;
hashLUT=new uint32_t[nVars];
const int32_t seed=0xfaf6cdb3;
for (int i=0;i<nVars;i++){
uint32_t h=i;
MurmurHash3_x86_32(&h,4,seed,hashLUT+i);
}
}
void findModelBits(int nVars){
uint8_t nBits=1;
uint64_t bitValue=2;
while(nVars > bitValue){
bitValue*=2;
nBits++;
}
gModelBits=nBits;
}
//set up for timeSeries
template <class T> T TimeSeriesValuesToResiduals(T *values, T *residuals,int nTimes,int nGroups);
//class definitions
//DenseTrMatrix stores an upper triangular matrix to save room for storage of models
//The Indices classes take advantage of the fact that our set of variables is always limited in size and that this is known beforehand
//So at a minor cost in memory we can avoid a linked list
//Instead we have a list that stores the indices and an index array that points to the location in the list where an index resides
//ie. list[index[model]-1]=model (zero is reserved to indicate that a model is not in the list so we add 1 to the locations when we store them in the index array)
//The basic implementation is the ModelIndices class for this
//CompactModelIndices eliminates the index altogether. Presently we don't need random access to the list except during insert deletion so this saves the memory cost of the index - important for the hash which keeps track of all the modelsets that have been evaluated
//A bitArray index is generated when needed to compare two sets of indices as needed
//memory could be further reduced by templating the indices to allow for char and short unsigned int indices (256 and 64K limit) or using a bitarray to store the elements which would also be faster. Alternatively use a suffix array to store the model sets similar to what is done for short read sequencing. For maintaining a list of very large sets that would probably be the best way to go instead of the current hash system
//some definitions needed for Dijkstra
//add early abort if the route is already worse than the existing route
class Comparator{
public:
int operator() ( const pair<int,float>& p1, const pair<int,float> &p2)
{
return p1.second>p2.second;
}
};
class BitArray{
public:
unsigned char *array=0;
size_t nBytes;
BitArray(size_t nBits){
nBytes=(nBits%8) ? nBits/8+1: nBits/8;
array=new unsigned char[nBytes];
memset(array,0,nBytes);
}
BitArray(const BitArray &A){
nBytes=A.nBytes;
if(array){delete[] array;array=0;}
array=new unsigned char[nBytes];
memmove(array,A.array,nBytes);
}
unsigned char getBit(size_t index) {
return (array[index/8] >> 7-(index & 0x7)) & 0x1;
}
unsigned char setBit(size_t index) {
array[index/8] = array[index/8] | 1 << 7-(index & 0x7);
}
unsigned char getSetBit(size_t index) {
const unsigned char retValue=array[index/8] >> (7-(index & 0x7)) & 0x1;
if(!retValue)array[index/8] = array[index/8] | 1 << (7-(index & 0x7));
return(retValue);
}
void clear(){
memset(array,0,nBytes);
}
~BitArray(){
if(array)delete [] array;
}
};
template <class T> class PackedBitArray{
//T is an UNSIGNED int type
//returns a 64 bit returnvalue - this is necessary for the algorithm to work endian independently
//if the number of bits return value is less than size of T - then you need to worry about endianness in constructing the final output
//T of uint64_t is the fastest because of fewer word overlaps and the extra size is minimal - probably stores 64 bit offset arrays anyway
public:
T *array=0;
PackedBitArray(){
array=0;
}
PackedBitArray(uint8_t elementSize,size_t nElements){
if(array)delete[] array;
const size_t nBits=nElements*elementSize;
const uint8_t tBits=sizeof(T) *8;
size_t nWords=(nBits&(tBits-1)) ? (nBits/tBits)+1: nBits/tBits;
array=new T [nWords];
memset(array,0,nWords*sizeof(T));
}
uint64_t get (uint8_t elementSize,size_t n) {
//want nth element
const uint8_t tBits=sizeof(T) *8;
size_t index=n*elementSize;
size_t startIndex=index/tBits;
size_t endIndex=(index+elementSize-1)/tBits;
uint8_t bitOffset=index & (tBits-1);
//case 1 start and end Index is same - just shift
if(startIndex==endIndex){
//careful if you do it all in the same expression it does always not lose the bits - maybe it keeps it in a 64 bit or 32 bit format natively
//shift bits left to zero left bits and shift right to correct possition
//example for 3 bits
//aaabbbcc -> bbbcc000 ->00000bbb
T lshift=(array[startIndex] << bitOffset);
return(lshift >> (tBits-elementSize));
}
//if it is over several words
//eg for case of 13 bits and T unint32
//aaaaaaaa aaaaaxxx xxxxxxxx xxbbbbbb
//read leading bits
uint8_t bitsRead=tBits-bitOffset;
T ones=~0;
uint64_t retValue=( ones >> bitOffset) & array[startIndex];
//00000000 00000000 00000000 000000xxx
retValue=retValue << (elementSize-bitsRead); //00000000 00000000 00xxx000 00000000
//read internal bits
for(int i=startIndex+1;i<endIndex;i++){
retValue |= ((uint64_t) array[i]) << elementSize-bitsRead-tBits;
bitsRead+=8;
}
//read trailing bits
//right shift to zero out rightmost bits
//xxbbbbbb -> 00000000 00000000 00000000 xxbbbbbb -> 00000000 00000000 00000000 000000xx
retValue |= ((uint64_t) array[endIndex]) >> (tBits-(elementSize-bitsRead));
return retValue;
}
void set(uint8_t elementSize, size_t n, uint64_t value){
const uint8_t tBits=sizeof(T) *8;
size_t index=n*elementSize;
size_t startIndex=index/tBits;
size_t endIndex=(index+elementSize-1)/tBits;
uint8_t bitOffset=index & (tBits-1);
//eg for case of 13 bits and T unint32
//aaaaaaaa aaaaaxxx xxxxxxxx xxbbbbbb
//fprintf(stderr,"start %d end %d offset %d\n",startIndex,endIndex,bitOffset);
if(startIndex==endIndex){
//prepare mask
//example for elementSize 11111111 -> 00000111 -> 00111000 -> 11000111 <-desired mask
T mask= ~0; //can't do this in an expression or it defaults to a longer word
mask= mask >> (tBits-elementSize);
mask= mask << (tBits- bitOffset -elementSize);
//00000xxx -> 00xxx000
array[startIndex]=array[startIndex] & ~mask | ((T) value << (tBits- bitOffset -elementSize));
}
else{
//example
//00000000 00000000 000xxxxx xxxxxxxx
//into
//aaaaaaab bbbbbbbb bbbbcccc cccccccc
//leading bit mask is 11111110
//value is shifted right by elementSize - (8-index%8)
//00000000 00000000 00000000 0000000x -> cast -> 0000000x
//aaaaaaab & 11111110 | 0000000x = aaaaaaax
T ones=~0;
T mask= ones << (tBits-bitOffset);
array[startIndex]=(array[startIndex] & mask) | (T)(value >> elementSize - (tBits-bitOffset));
size_t bitsWritten=tBits-bitOffset;
//internal bits is a simple shift - no mask needed when nElemet
for(int i=startIndex+1;i<endIndex;i++){
array[i]=(T) (value >> elementSize-bitsWritten-tBits);
bitsWritten+=8;
}
//trailing bit mask is 00001111
//shift left the value by the number of remaining bits
//00000000 00000000 000xxxxx xxxxxxxx -> 00000000 0000000x xxxxxxxx xxxx0000 -> cast -> xxxx0000
mask= ones >> elementSize-bitsWritten;
array[endIndex]=array[endIndex] & mask | (T) (value << (tBits-(elementSize-bitsWritten)));
}
}
~PackedBitArray(){
if(array)delete [] array;
}
};
template <class T> class BitIndex{//T is for type of list or type of PackedBit array
//simple class meant for throwaway comparisons
BitArray *bitArray=0;
uint32_t minValue;
uint32_t maxValue;
size_t arraySize=0;
public:
BitIndex(T *list,T nModels){
maxValue=list[0];
minValue=list[0];
for(T i=1;i<nModels;i++){
if(list[i] > maxValue)maxValue=list[i];
if(list[i] < minValue)minValue=list[i];
}
bitArray=new BitArray(maxValue-minValue);
for(T i=0;i<nModels;i++){
bitArray->setBit(list[i]-minValue);
}
}
BitIndex(PackedBitArray<T> *list,uint8_t nElementSize,size_t nModels){
maxValue=list->get(nElementSize,0);
minValue=maxValue;
for(uint32_t i=1;i<nModels;i++){
const uint32_t value= list->get(nElementSize,i);
if(value > maxValue)maxValue=value;
if(value < minValue)minValue=value;
}
bitArray=new BitArray(maxValue-minValue);
for(uint32_t i=0;i<nModels;i++){
bitArray->setBit(list->get(nElementSize,i)-minValue);
}
}
bool compare(PackedBitArray<T> *list,uint8_t modelBits, uint8_t nModels){
for(int i=0;i<nModels;i++){
size_t value= list->get(modelBits,i);
if(value < minValue || value > maxValue || ! getBit(value-minValue)){
return(0);
}
}
}
bool compare(T *list,T nModels){
for(int i=0;i<nModels;i++){
if(list[i]<minValue || list[i] > maxValue || !getBit(list[i]-minValue)){
//total_collisions++;
return(0);
}
}
return(1);
}
unsigned char getBit(size_t index) {
return (bitArray->getBit(index));
}
unsigned char setBit(size_t index) {
return (bitArray->setBit(index));
}
~BitIndex(){
if(bitArray)delete bitArray;
}
};
template <class T>class DenseTrMatrix{
//includes diagonal - single block of numbers - optimized for upper diagonal column major (FORTRAN style) access
//not used for optimization but for storage requirements
public:
T *matrix; //matrix
int size; //number rows or columns
DenseTrMatrix(){
matrix=0;
size=0;
}
DenseTrMatrix(int m){
matrix=new T [m*(m+1)/2];
size=m;
}
DenseTrMatrix(const DenseTrMatrix &A){
size=A.size;
if(size){
matrix=new T [size*(size+1)/2];
memmove(matrix,A.matrix,size*(size+1)/2*sizeof(T));
}
else{
matrix=0;
}
}
DenseTrMatrix & operator = (const DenseTrMatrix &rhs){
if(rhs.size ==0){
if(size) delete [] matrix;
matrix=0;
size=0;
}
else if(rhs.size != size){
if(size) delete [] matrix;
size=rhs.size;
matrix=new T [size*(size+1)/2];
memmove(matrix,rhs.matrix,size*(size+1)/2*sizeof(T));
}
else{
memmove(matrix,rhs.matrix,size*(size+1)/2*sizeof(T));
}
}
void sq_to_tr(T *sqmatrix, int ldn)const{
//leading dimension size provided - dense storage
//upper column major expected
T *s=matrix;
size_t inc=sizeof(T);
size_t blocksize=inc;
for (int i=1;i<=size;i++){
memmove(s,sqmatrix,blocksize);
blocksize+=inc;
s+=i;
sqmatrix+=ldn;
}
}
void tr_to_sq (T *sqmatrix,int ldn)const{
//leading dimension size provided - dense storage
//upper column major expected
//copies whol matrix
T *s=matrix;
size_t inc=sizeof(T);
size_t blocksize=sizeof(T);
for (int i=1;i<=size;i++){
memmove(sqmatrix,s,blocksize);
blocksize+=inc;
s+=i;
sqmatrix+=ldn;
}
}
void tr_to_sq_delj (T *d, int ldn,int j)const{
//remove jth column
T *s=matrix;
size_t inc=sizeof(T);
size_t blocksize=inc;
int i=1;
while(i<=j){
memmove(d,s,blocksize);
blocksize+=inc;
d+=ldn;
s+=i;
i++;
}
blocksize+=inc;
s+=i;
i++;
while(i<=size){
memmove(d,s,blocksize);
blocksize+=inc;
d+=ldn;
s+=i;
i++;
}
}
void print()const{
const int n=size*(size+1)/2;
for(int row=0;row<size;row++){
for(int col=0;col<row;col++){
cout << 0 <<'\t';
}
int k=(row+1)*(row+2)/2-1;
for(int col=row;col<size;col++){
cout << matrix[k] <<'\t';
k+=col+1;
}
cout <<endl;
}
cout << endl;
}
~DenseTrMatrix(){
if(matrix){
delete[] matrix;
}
}
};
class DoubleHashTable{
//uses hash of upper 16 and lower 16
BitArray *filters[NFILTERS];
public:
DoubleHashTable(){
for (int i=0;i<NFILTERS;i++)filters[i]=new BitArray(65536);
}
bool countInsert(uint32_t hashValue,uint8_t nModels){
uint16_t size=nModels %16;
uint16_t uWord=hashValue >> 16 | size;
uint16_t lWord=hashValue;
return (filters[uWord%NFILTERS]->getSetBit(lWord));
}
void clear(){
for (int i=0;i<NFILTERS;i++) filters[i]->clear();
}
~DoubleHashTable(){
for (int i=0;i<NFILTERS;i++)
delete filters[i];
}
};
class ModelIndices{
//unordered list of indices of models - can add and delete
//save room in list for this - allocates maxModel space instead of nModel space
public:
uint16_t maxModels=0;
uint16_t nModels=0;
uint16_t *index=0;
uint16_t *list=0;
uint_fast32_t hashValue;
ModelIndices(): maxModels(0),nModels(0),index(0),list(0),hashValue(0){
}
ModelIndices(uint16_t h): maxModels(h),nModels(0),hashValue(0){
index=new uint16_t[maxModels];
memset(index,0,maxModels*sizeof(uint16_t));
list=new uint16_t[maxModels];
}
ModelIndices(const ModelIndices &A):nModels(A.nModels),hashValue(A.hashValue){
//doesn't copy any of the gunk that might be at the end of the list array
if(A.maxModels){
if(maxModels != A.maxModels){
maxModels=A.maxModels;
if(list)delete[] list;
if(index)delete[] index;
index=new uint16_t[maxModels];
list=new uint16_t[maxModels];
}
memmove(index,A.index,A.maxModels*sizeof(uint16_t));
memmove(list,A.list,A.maxModels*sizeof(uint16_t));
}
else{
if(list)delete[] list;
if(index)delete[] index;
list=0;
index=0;
}
}
//full copy done when ModelIndices copied to modelIndices
ModelIndices & operator = (const ModelIndices &rhs){
nModels=rhs.nModels;
hashValue=rhs.hashValue;
if(rhs.maxModels){
if(maxModels != rhs.maxModels){
maxModels=rhs.maxModels;
if(list)delete[] list;
if(index)delete[] index;
index=new uint16_t[maxModels];
list=new uint16_t[maxModels];
}
memmove(index,rhs.index,rhs.maxModels*sizeof(uint16_t));
memmove(list,rhs.list,rhs.maxModels*sizeof(uint16_t));
}
else{
if(list)delete[] list;
if(index)delete[] index;
list=0;
index=0;
}
}
int insertElement(uint16_t m){
if(index[m]) return(0); //no insert
if(!index[m]){
list[nModels++]=m; //add to end of list - we *want* nModels to be incremented before assignment
index[m]=nModels; //put a pointer to where the index is for deletion. This is incremented by 1 so that zero can indicate non-membership
hashValue= hashValue ^ hashLUT[m];
return(1);
}
}
uint_fast32_t insertElementHash(uint16_t m){
if(index[m])return(0); //this element is already in set return 0;
return(hashValue ^ hashLUT[m]);
}
uint16_t deleteElement_unordered(uint16_t m){
//switches element with last element of list for quick removal
//is not necessarily the same order as the variables in the R matrix
//useful for hashing to see if set has been seen
if(nModels <1){
fprintf(stderr,"trying to delete from an empty set\n");
exit(0);
}
if(nModels ==1){
//empty set
index[m]=0;
nModels=0;
hashValue=0;
return(-1);
}
uint16_t i=index[m]-1; //index to be deleted
if(i >=0){
index[m]=0; //delete pointer
if(i != nModels-1){
//put end of the list where the deletion is
const uint16_t oldm=list[nModels-1];
list[i]=oldm;
index[oldm]=i+1;
}
//adjust nModels and hash
nModels--;
hashValue=hashValue ^ hashLUT[m];
//return value of i for reordering
}
return(i);
}
uint_fast32_t deleteElement_unorderedHash(uint16_t m){
return(hashValue ^ hashLUT[m]);
}
int deleteElement(uint16_t m){
//contracts list
//keeps list in same order as variables in matrix
if(nModels){
uint16_t i=index[m]-1; //index to be deleted
index[m]=0;
for(uint16_t k=i+1;k<nModels;k++){
list[k-1]=list[k];
index[list[k]]=k;
}
nModels--;
hashValue= hashValue ^ hashLUT[m] ;
}
hashValue=0;
return(0);
}
int order_deletion(uint16_t i){
//converts an unordered deletion to one that matches R
//need to know the ith column
//check for case when the last column was actually delted
if(i<0 || i >= nModels){
return(0);
}
uint16_t temp=list[i];
for(uint16_t k=i+1;k<nModels;k++){
list[k-1]=list[k];
index[list[k]]=k;
}
list[nModels-1]=temp;
index[temp]=nModels; //add to index...
return(1);
}
bool operator==(const ModelIndices &b)const{
if(nModels != b.nModels || hashValue != b.hashValue)return(0);
if(nModels==0)return(1);
for (uint16_t i=0;i<nModels;i++){
if(!b.index[list[i]])return(0);
}
return(1);
}
void print_list()const{
if(nModels == 0){
cerr << "NULL";
}
for(uint16_t i=0;i<nModels;i++){
cerr <<list[i] << '.';
}
cerr << endl;
}
void print_ordered_list()const{
if(nModels == 0){
cerr << "NULL";
}
uint16_t nSize=0;
for(uint16_t i=0;i<maxModels && nSize < nModels;i++){
if(index[i]){
cerr <<i << '.';
nSize++;
}
}
cerr << endl;
}
~ModelIndices(){
if(maxModels){
if(index)delete [] index;
if(list)delete [] list;
}
}
};
template <class T> class CompactModelIndices{
public:
uint_fast32_t hashValue;
uint8_t modelBits=gModelBits;
uint8_t nModels=0; //never more than 256 variables in a model
PackedBitArray <T> *list=0;
CompactModelIndices(): hashValue(0){
list=0;
uint8_t modelBits=gModelBits;
uint8_t nModels=0;
}
CompactModelIndices(size_t _modelBits,size_t _nModels) : modelBits(_modelBits),nModels(_nModels),hashValue(0){
list=new PackedBitArray<T>(modelBits,nModels);
}
CompactModelIndices(const CompactModelIndices &A): hashValue(A.hashValue),modelBits(A.modelBits),nModels(A.nModels){
if(list){delete list;list=0;}
if(A.list){
list=new PackedBitArray<T>(modelBits,nModels);
const size_t tBits=sizeof(T)*8;
size_t nWords=((modelBits*nModels) & (tBits-1)) ? (modelBits*nModels)/tBits+1: (modelBits*nModels)/tBits;
memmove(list->array,A.list->array,nWords*sizeof(T));
}
}
CompactModelIndices & operator = (const CompactModelIndices &rhs){
hashValue=rhs.hashValue;
modelBits=rhs.modelBits;
nModels=rhs.nModels;
if(list){delete list;list=0;}
if(rhs.list){
list=new PackedBitArray<T>(modelBits,nModels);
const size_t tBits=sizeof(T)*8;
size_t nWords=((modelBits*nModels) & (tBits-1)) ? (modelBits*nModels)/tBits+1: (modelBits*nModels)/tBits;
memmove(list->array,rhs.list->array,nWords*sizeof(T));
}
}
bool operator==(const CompactModelIndices &b)const{
if(nModels == b.nModels && hashValue == b.hashValue){
return(1);
BitIndex<T> bitIndex(list,modelBits,nModels);
bool retvalue=bitIndex.compare(b.list,modelBits,nModels);
return(retvalue);
}
return(0);
}
CompactModelIndices( ModelIndices &mind): nModels(mind.nModels),hashValue(mind.hashValue){
if(list){delete list;list=0;}
if(nModels){
const uint8_t modelBits=gModelBits;
list=new PackedBitArray<T>(modelBits,nModels);
for(int i=0;i<mind.nModels;i++){
list->set(modelBits,i,mind.list[i]);
}
}
}
CompactModelIndices & operator = (const ModelIndices &rhs){
nModels=rhs.nModels;
hashValue=rhs.hashValue;
if(list){delete list;list=0;}
if(rhs.nModels){
const uint8_t modelBits=gModelBits;
list=new PackedBitArray<T>(modelBits,nModels);
for(int i=0;i<rhs.nModels;i++){
list->set(modelBits,i,rhs.list[i]);
}
}
}
bool operator==(const ModelIndices &b)const{
if(nModels == b.nModels && hashValue == b.hashValue){
return(1);
BitIndex<T> bitIndex(list,modelBits,nModels);
bool retvalue=bitIndex.compare(b.list,nModels);
return(retvalue);
}
return(0);
}
void print_list()const{
if(nModels == 0){
cerr << "NULL" << endl;
}
for(int i=0;i<nModels;i++){
cerr <<list->get(modelBits,i) << '.';
}
cerr << endl;
}
~CompactModelIndices(){
if(list)delete list;
}
};
namespace std {
template <>
struct hash <CompactModelIndices<uint64_t>>{
uint_fast32_t operator()(const CompactModelIndices<uint64_t>& k) const
{
const uint32_t nModels=k.nModels;
const uint32_t size=nModels %16;
const uint8_t modelBits=k.modelBits;
if(!k.hashValue){
uint_fast32_t hash=0;
//pairwise XOR each element
for (uint16_t i=0;i<k.nModels;i++){
hash=hash ^ hashLUT[k.list->get(modelBits,i)];
}
return(hash & 0xFFFFFFF0 | size);
}
return(k.hashValue & 0xFFFFFFF0 | size);
}
};
}
template <class T> class ModelIndicesHash{
//class just using 32 or 64 bit hash representation of indices
public:
T hashValue=0;
uint8_t nModels=0;
ModelIndicesHash():nModels(0),hashValue(0){}
ModelIndicesHash(uint8_t _nModels,T _hashValue):nModels(_nModels),hashValue(_hashValue){
}
template <class T1> ModelIndicesHash (CompactModelIndices<T1> &SCMI){
hashValue=SCMI.hashValue;
nModels=SCMI.nModels;
}
ModelIndicesHash(ModelIndices& MI){
hashValue=MI.hashValue;
nModels=MI.nModels;
}
ModelIndicesHash(const ModelIndicesHash &A): hashValue(A.hashValue),nModels(A.nModels){}
ModelIndicesHash & operator = (const ModelIndicesHash &rhs){
hashValue=rhs.hashValue;
nModels=rhs.nModels;
}
bool operator==(const ModelIndicesHash &b)const{
if( hashValue == b.hashValue && nModels == b.nModels){
return(1);
}
return(0);
}
};
namespace std {
template <>
struct hash <ModelIndicesHash<uint32_t>>{
uint_fast32_t operator()(const ModelIndicesHash<uint32_t>& k) const
{
const uint32_t size=k.nModels %16;
return(k.hashValue & 0xFFFFFFF0 | size);
}
};
}
template <class T> class ModelSet{
public:
CompactModelIndices<uint64_t> modelIndices;
T r2;
T bic;
double logprior;
DenseTrMatrix <T> R;
ModelSet() {
modelIndices =0;
r2 =0;
bic =0;
logprior=0;
R=DenseTrMatrix<T>();
}
ModelSet(ModelIndices mInd, T mr2, T mbic, double mlogprior) {
modelIndices=CompactModelIndices<uint64_t>(mInd);
r2 = mr2;
bic = mbic;
logprior=mlogprior;
R=DenseTrMatrix<T>();
}
ModelSet(CompactModelIndices<uint64_t> mInd, T mr2, T mbic,double mlogprior ) {
modelIndices = mInd;
r2 = mr2;
bic = mbic;
logprior=mlogprior;
R=DenseTrMatrix<T>();
}
ModelSet( ModelIndices mInd, T mr2, T mbic,double mlogprior, T *sqR, int nRows,int ldn) {
modelIndices = mInd;
r2 = mr2;
bic = mbic;
R=DenseTrMatrix<T>(nRows);
R.sq_to_tr (sqR,ldn);
logprior=mlogprior;
}
ModelSet( CompactModelIndices<uint64_t> mInd, T mr2, T mbic,double mlogprior,T *sqR, int nRows,int ldn) {
modelIndices = mInd;
r2 = mr2;
bic = mbic;
R=DenseTrMatrix<T>(nRows);
R.sq_to_tr (sqR,ldn);
logprior=mlogprior;
}
ModelSet (const ModelSet &A){
R=A.R;
modelIndices=A.modelIndices;
bic=A.bic;
r2=A.r2;
logprior=A.logprior;
}
ModelSet & operator = (const ModelSet &rhs){
//deep copy of R
R=rhs.R;
bic=rhs.bic;
r2=rhs.r2;
modelIndices=rhs.modelIndices;
logprior=rhs.logprior;
}
//bool operator<(const ModelSet& b) const { return (bic < b.bic) && ! (modelIndices == b.modelIndices); }
//bool operator>(const ModelSet& b) const { return (bic > b.bic) && ! (modelIndices == b.modelIndices); }
bool operator<(const ModelSet& b) const { return (bic < b.bic) && ! (modelIndices == b.modelIndices); }
bool operator>(const ModelSet& b) const { return (bic > b.bic) && ! (modelIndices == b.modelIndices); }
bool operator>=(const ModelSet& b) const { return !(*this < b); }
bool operator<=(const ModelSet& b) const { return !(*this > b); }
bool operator==(const ModelSet &b)const{
return(modelIndices == b.modelIndices);
}
bool operator!=(const ModelSet& b) const { return !(*this == b); }
};
//for sorting by edgeweights we need data structure where there is a pair and a score
//simple edge list class with weights and sort routine
class EdgeList{
public:
int **parents;
float **edgeWeights;
int *nParents;
int nNodes;
EdgeList(){
nNodes=0;parents=0;nParents=0;edgeWeights=0;
}
EdgeList(int nGenes,float minWeight,float **weights){
nNodes=nGenes;
nParents=new int[nGenes];
memset(nParents,0,sizeof(int)*nGenes);
//get sizes
for(int i=0;i<nGenes;i++){ //children
for(int j=0;j<nGenes;j++){ //parents;
if(weights[i][j] > minWeight){
nParents[i]++;
}
}
}
//allocate
edgeWeights=new float*[nGenes];
parents=new int*[nGenes];
for(int i=0;i<nGenes;i++){
edgeWeights[i]=0;
parents[i]=0;
if(nParents[i]){
edgeWeights[i]=new float [nParents[i]];
parents[i]=new int [nParents[i]];
}
}
memset(nParents,0,sizeof(int)*nGenes);
for(int i=0;i<nGenes;i++){ //children
for(int j=0;j<nGenes;j++){ //parents
if(weights[i][j] > minWeight){
parents[i][nParents[i]]=j;
edgeWeights[i][nParents[i]]=weights[i][j];
nParents[i]++;
}
}
}
}
EdgeList(int n){
nNodes=n;
parents=new int*[n];
nParents=new int[n];
edgeWeights=new float*[n];
for (int i=0;i<n;i++){
parents[i]=0;
nParents[i]=0;
edgeWeights[i]=0;
}
}
EdgeList(int _nNodes,set<pair<pair<int,int>,float>> edgeSet){
nNodes=_nNodes;
parents=new int*[nNodes];
nParents=new int[nNodes];
edgeWeights=new float*[nNodes];
for (int i=0;i<nNodes;i++){
parents[i]=0;
nParents[i]=0;
edgeWeights[i]=0;
}
//find sizes of parents
for(auto f : edgeSet){
nParents[f.first.second]++;
}
//allocate
for (int i=0;i<nNodes;i++){
if(nParents[i]){
parents[i]=new int[nParents[i]];
edgeWeights[i]=new float[nParents[i]];
nParents[i]=0; //so that it can be used as a counter
}
}
//assign values
for(auto f : edgeSet){
const int c=f.first.second;
parents[c][nParents[c]]=f.first.first;
edgeWeights[c][nParents[c]]=f.second;
nParents[c]++;
}
}
EdgeList(int _nNodes,vector<int>nEdges,vector<int>inParents,vector<int>inChildren,vector<double>inWeights){
nNodes=_nNodes;
parents=new int*[nNodes];
nParents=new int[nNodes];
edgeWeights=new float*[nNodes];
for (int i=0;i<nNodes;i++){
parents[i]=0;
nParents[i]=0;
edgeWeights[i]=0;
}
int totalEdges=0;
for(int i=0;i<inChildren.size();i++){
const int c=inChildren[i];
nParents[c]=nEdges[i];
parents[c]=new int[nEdges[i]];
memmove(parents[c],&(inParents[totalEdges]),nEdges[i]*sizeof(int));
edgeWeights[c]=new float[nEdges[i]];
double *weights=&inWeights[totalEdges];
for(int k=0;k<nEdges[i];k++) edgeWeights[c][k]=weights[k];
totalEdges+=nEdges[i];
}
}
EdgeList(const EdgeList &A){
nNodes=A.nNodes;
parents=new int*[nNodes];
nParents=new int[nNodes];
edgeWeights=new float*[nNodes];
memmove(nParents,A.nParents,nNodes*sizeof(int));
for (int i=0;i<nNodes;i++){
if(nParents[i]){
parents[i]=new int[nParents[i]*sizeof(int)];
memmove(parents[i],A.parents[i],nParents[i]*sizeof(int));
edgeWeights[i]=new float[nParents[i]*sizeof(float)];
memmove(edgeWeights[i],A.edgeWeights[i],nParents[i]*sizeof(float));
}
else{
parents[i]=0;
nParents[i]=0;
edgeWeights[i]=0;
}
}
}
~EdgeList(){
for (int i=0;i<nNodes;i++){
if(nParents[i]){
delete[] parents[i];
delete[] edgeWeights[i];
}
}
if(nNodes){
delete[] nParents;delete[] edgeWeights;delete[] parents;
}
}
EdgeList nonSelfList(){
EdgeList nonSelfList(nNodes);
//count edges
for(int i=0;i<nNodes;i++){
int j;
for (j=0;j<nParents[i];j++){
if(parents[i][j] == i && edgeWeights[i][j] > 0){
//allocate parents
//copy every thing but i
nonSelfList.nParents[i]=nParents[i]-1;
if(nonSelfList.nParents[i]){
nonSelfList.parents[i]=new int[nonSelfList.nParents[i]*sizeof(int)];
nonSelfList.edgeWeights[i]=new float[nonSelfList.nParents[i]*sizeof(float)];
if(j){
memmove(nonSelfList.parents[i],parents[i],j*sizeof(int));
memmove(nonSelfList.edgeWeights[i],edgeWeights[i],j*sizeof(float));
}
if(j<nonSelfList.nParents[i]){
memmove(nonSelfList.parents[i]+j,parents[i]+j+1,(nonSelfList.nParents[i]-j)*sizeof(int));
memmove(nonSelfList.edgeWeights[i]+j,edgeWeights[i]+j+1,(nonSelfList.nParents[i]-j)*sizeof(float));
}