-
Notifications
You must be signed in to change notification settings - Fork 0
/
l2p.c
4621 lines (4256 loc) · 168 KB
/
l2p.c
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
/*
* todo :
1) make sure custom works.
2) updategenes()
3) ensembl2gene()
4) pretty print , pvals if want raw data frame DOUBLE CHECK AGAINST EMAIL
5) Another column will be called “%Gene Hits per Pathway”, explained as (A/(A+B))
6) bench marks permute vs. non . plot results.
vi l2p2.c ; make
gcc --Wall -Os -o l2p2 pwgenes.c small.c
example command line debug: cat deglist | valgrind -v --tool=memcheck --leak-check=full --show-leak-kinds=all --track-origins=yes ./l2p -permute
NUMBER_HITS … becomes Significant genes IN Pathway (A)
NUMBER_MISSES … becomes Non-Significant genes IN Pathway (B)
NUMBER_USER_GENES … becomes Significant genes NOT IN Pathway (C)
TOTAL_GENES_MINUS_INPUT … becomes Non-Significant Genes NOT IN Pathway (D)
new_name old_name description
1 pval pval fisher's 2x2 exact test
2 fdr fdr false discovery rate: default is benjamini hochberg, GPCC method if permute=1
3 enrichment_score ratio same as old but multiplied by 100 : ((number_hits /(number_hits+number_misses)) - (number_user_genes/(number_user_genes+total_gens_minus_input))) * 100
** 4 percent_gene_hits_per_pathway NEW FIELD (number_hits/(number_hits+number_misses))
5 number_hits pwhitcount number of genes hit in pathway
6 number_misses pwnohitcount pathway number of genes in the pathway
7 number_user_genes inputcount total count of user genes (user input)
8 total_genes_minus_input pwuniverseminuslist total number of unique genes in all pathways
9 pathway_id pathwayaccessionidentifier canonical accession ( if available, otherwise assigned by us )
10 category category KEGG,REACTOME,GO,PANT(=PANTHER),PID=(pathway interaction database) *was "source"*
11 pathway_name pathwayname Name of pathway
12 pathway_type pathwaytype functional_set,pathway,structural_complex,custom
13 genes genes_space_separated HUGO genes from user that hit the pathway
The Fisher’s exact test on the permutation table is basically stand-alone, it doesn’t need a permutation test initially. But it looks like I’m testing something different from what is usually tested; for my test the total number of genes in the pathway isn’t relevant. Good, I want to do something different.
The contingency table: So I can think clearly I’ll set this up the way I’m used to (from epidemiology, first column is no disease, second is disease; first row in no exposure, second is exposure). For us “disease” means occurring in the pathway in question; “exposure” means being on the investigator’s list of genes.
First without the correction:
A: Count of genes in the universe but not on the list and not in the pathway
B: Count of genes in the universe but not on the list, in the pathway
C: Count of genes on the list but not in the pathway
D: Count of genes on the list and in the pathway
c d b a
a 4 pwhitcount number of genes hit in pathway
b 5 pwnohitcount pathway number of genes in the pathway
c 6 inputcount total count of user genes (user input)
d 7 pwuniverseminuslist unique genes in universe minus inputcount
Now with the correction:
B and D are unchanged.
For A and C: Sum over the genes, the number of pathways (in the universe of pathways) each gene occurs in.
For example suppose there are 3 genes in C (must be a very short list of genes!) so in the uncorrected table the entry in C is 3.
The first gene occurs in 5 pathways, the second in 2, the third in 10. The entry in C is 17.
Same for A, but this will always be a sum over a large number of genes.
The sum of the entries in the table will now be larger, so the table will need to be corrected, but ignore this for now.
original GPCC
a=hits d
b=pathway-hits b
c=list-hits c
d=universe-pathway-list+hits a
so ...
UUUUUUUUUUUUUUUUUUUUUUUUUUUUU
U U
U universe PPPPPPPPPPPP U
U P P U
U P pathway P U
U P P U
U LLLLLLLLLLLLLL P U
U L P L P U
U L P hits L P U
U L P L P U
U L PPPPPPPPLPPP U
U L L U
U L list L U
U L L U
U LLLLLLLLLLLLLL U
U U
UUUUUUUUUUUUUUUUUUUUUUUUUUUUU
UUUUUUUUUUUUUUUUUUUUUUUUUUUUU UUUUUUUUUUUUUUUUUUUUUUUUUUUUU UUUUUUUUUUUUUUUUUUUUUUUUUUUUU
U U U U U U
U universe U U PPPPPPPPPPPP U U U
U U U P P U U U
U U U P pathway P U U U
U U U P P U U U
U U U P P U U LLLLLLLLLLLLLL U
U U U P P U U L L U
U U U P P U U L L U
U U U P P U U L L U
U U U PPPPPPPPPPPP U U L L U
U U U U U L L U
U U U U U L list L U
U U U U U L L U
U U U U U LLLLLLLLLLLLLL U
U U U U U U
UUUUUUUUUUUUUUUUUUUUUUUUUUUUU UUUUUUUUUUUUUUUUUUUUUUUUUUUUU UUUUUUUUUUUUUUUUUUUUUUUUUUUUU
A = u - p - h
B = u - l
C = l - h
D = h
dcba
see https://www.biostars.org/p/252937/ on how DAVID does it.
list=300 hits=3 pw=40 genome=30001
usergenes genome
ipw 3-1 40
notinpw 297 29960
a=hits
b=list-hits
c=list
d=u-list
problem #1
problem #2
One sided test - count
diseased healthy
exposed DE HE
notexposed DN HN
OR=(DE/HE)/(DN/HN)
a/b / c/d
*/
#define NELSON_C 1
#define NELSON_TEST 0
// RICH: for me running C program:
#if NELSON_TEST
#define DEGLISTNAME "brca_up_in_meta500.txt"
#define OUTPUTFILE "test_GWN_output.txt"
#define TECHOUTPUTFILE "test_GWN_tech_output.txt"
//#define DEGLISTNAME "deglistupinreactive.txt"
// RICH: will need to be passed paramter:
#endif
#define SCALE_UNIVERSE 1
// try 1000000 for testing
#define NUM_PERMUTES 200000
#define NUM_TEST_PERMUTES 200000
#define SIG_P 0.05
#ifdef L2P_USING_R
#include <R.h>
#include <Rdefines.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <math.h>
#include <inttypes.h>
#include "pathworks.h"
#ifdef WEBASSEMBLY
#include "small.h"
#else
#if L2PUSETHREADS
#include <pthread.h>
#endif
#include "big.h"
#endif
extern unsigned short int pwgenes[];
extern struct smallgenetype genes[];
extern struct pwtype pws[];
extern unsigned int numpws;
extern unsigned int numgenes;
extern unsigned int numpwgenes;
struct smallgenetype *by_egids; // a copy of "genes" ,but sorted by egid (entrez gene id )
// George's globals
unsigned int ingenecnt;
static struct tree_with_count **aug_treepointers;
static struct tree_with_count **treepointers;
static struct used_path_type **sig_path_ptr = (struct used_path_type **)0;
static unsigned int deg_count = 0;
static unsigned int *gene_path_cts;
static unsigned int *deg_path_cts;
static unsigned int aug_deg_count = 0; // number of times the userinput appears in all the pathways
static unsigned int deg_count_sum;
static unsigned int real_genect = 0;
static unsigned int aug_gene_ct = 0;
static unsigned int *auguniverse;
static double mean_deg_gpcount;
static double mean_u_gpcount;
unsigned int ingenes[MAX_INGENES];
// static unsigned int n_sig_paths = 0; not used, for debugging
// static struct used_path_type **augreverse = (struct used_path_type **)0;
// static struct tree_with_count *head = (struct tree_with_count *)0; Got moved to stack
#if NELSON_TEST
FILE *path_gene_countsfile;
// FILE *gene_path_countsfile;
#endif
#if 0
// debug routine
void dump_used_paths(struct used_path_type *u, int num, char *msg)
{
struct used_path_type *uptr; // used path pointer
int i,j;
char *z;
fprintf(stderr,"in dump_used_paths() num=%d msg=[%s]\n",num,msg); fflush(stderr);
for (i=0 ; i<100 ; i++) // for each line of custom file
{
uptr = (u+i);
fprintf(stderr,"acc %d %s\n",i,uptr->acc); fflush(stderr);
fprintf(stderr,"name %d %s\n",i,uptr->name);
fprintf(stderr,"numgenes %d %u\n",i,uptr->numgenes);
fprintf(stderr,"numfixed %d %u\n",i,uptr->numfixedgenes);
fprintf(stderr,"genehits %d %p\n",i,uptr->genehits);
fprintf(stderr,"egids %d %p\n",i,uptr->egids);
for (j=0;j<10;j++)
// for (j=0;j<uptr->numfixedgenes;j++)
{
z = egid2hugo(*(uptr->egids+j));
fprintf(stderr,"gene %d %d = %u = %s [%s]\n",i,j,*(uptr->egids+j),z,msg);
}
fflush(stderr);
}
return;
}
#endif
#if RADIX_SORT
// from awardofsky's github . This is faster than qsort.
void radix_ui(register unsigned int vector[], register const unsigned int size)
{
/* Support for variable sized integers without overflow warnings */
const int MAX_UINT__ = ((((1 << ((sizeof(unsigned int) << 3) - 2)) - 1) << 1) + 1);
// const int LAST_EXP__ = (sizeof(unsigned int) - 1) << 3;
/* Define std preliminary, constrain and expression to check if all bytes are sorted */
#define PRELIMINARY__ 100
#define MISSING_BITS__ exp < (sizeof(unsigned int) << 3) && (max >> exp) > 0
/* Check for biggest integer in [a, b[ array segment */
#define LOOP_MAX__(a, b) \
for(s = &vector[a], k = &vector[b]; s < k; ++s) { \
if(*s > max) { \
max = *s; \
} \
}
/* b = helper array pointer ; s, k and i = array iterators */
/* exp = bits sorted, max = maximun range in array */
/* point = array of pointers to the helper array */
register unsigned int *b, *s, *k;
register unsigned int exp = 0;
register unsigned int max = exp;
unsigned int i, *point[0x100];
int swap = 0;
/* Set preliminary according to size */
const unsigned int preliminary = (size > PRELIMINARY__) ? PRELIMINARY__ : (size >> 3);
/* If we found a integer with more than 24 bits in preliminar, */
/* will have to sort all bytes either way, so max = MAX_UINT__ */
LOOP_MAX__(1, preliminary);
if(max <= (MAX_UINT__ >> 7)) {
LOOP_MAX__(preliminary, size);
}
/* Helper array initialization */
b = (unsigned int *)malloc(sizeof(unsigned int) * size);
/* Core algorithm: for a specific byte, fill the buckets array, */
/* rearrange the array and reset the initial array accordingly. */
#define SORT_BYTE__(vec, bb, shift) \
unsigned int bucket[0x100] = {0}; \
register unsigned char *n = (unsigned char *)(vec) + (exp >> 3),*m; \
for(m = (unsigned char *)(&vec[size & 0xFFFFFFFC]); n < m;) { \
++bucket[*n]; n += sizeof(int); \
++bucket[*n]; n += sizeof(int); \
++bucket[*n]; n += sizeof(int); \
++bucket[*n]; n += sizeof(int); \
} \
for(n = (unsigned char *)(&vec[size & 0xFFFFFFFC]) + (exp >> 3), \
m = (unsigned char *)(&vec[size]); n < m;) { \
++bucket[*n]; n += sizeof(int); \
} \
s = bb; \
int next = 0; \
for(i = 0; i < 0x100; ++i) { \
if(bucket[i] == size) { \
next = 1; \
break; \
} \
} \
if(next) { \
exp += 8; \
continue; \
} \
for(i = 0; i < 0x100; s += bucket[i++]) { \
point[i] = s; \
} \
for(s = vec, k = &vec[size]; s < k; ++s) { \
*point[(*s shift) & 0xFF]++ = *s; \
} \
swap = 1 - swap; \
exp += 8;
/* Sort each byte (if needed) */
while(MISSING_BITS__) {
if(exp) {
if(swap) {
SORT_BYTE__(b, vector, >> exp);
} else {
SORT_BYTE__(vector, b, >> exp);
}
} else {
SORT_BYTE__(vector, b, );
}
}
if(swap) {
memcpy(vector, b, sizeof(unsigned int) * size);
}
/* Free helper array */
free(b);
/* Undefine function scoped macros for eventual later use */
#undef PRELIMINARY__
#undef MISSING_BITS__
#undef LOOP_MAX__
#undef SORT_BYTE__
}
#endif
// seems to be a bug in bsearch2 , valgrind reports invalid access. so user c stdlib bsearch for now
int bsearch2(const unsigned int key, const unsigned int *base, size_t nmemb)
{
unsigned int current_element;
int medium;
int first = 0;
int last = nmemb;
while (first <= last)
{
medium = first + (last - first) / 2;
current_element = *(base + medium);
if (key < current_element)
last = medium - 1;
else if (key > current_element)
first = medium + 1;
else
return 1;
}
return 0;
}
#if 0
int binsearch_6( unsigned int array[], size_t size, unsigned int key, size_t *index )
{
if( !array || !size ) return 0;
arr_t *p=array;
switch( size )
{
#define C(n) case ((size_t)1<<n)-1: if( p[1<<(n-1)]<=key ) p+=1<<(n-1);
#if SIZE_MAX == UINT64_MAX
C(63) C(62) C(61)
C(60) C(59) C(58) C(57) C(56) C(55) C(54) C(53) C(52) C(51)
C(50) C(49) C(48) C(47) C(46) C(45) C(44) C(43) C(42) C(41)
C(40) C(39) C(38) C(37) C(36) C(35) C(34) C(33) C(32)
#endif
C(31)
C(30) C(29) C(28) C(27) C(26) C(25) C(24) C(23) C(22) C(21)
C(20) C(19) C(18) C(17) C(16) C(15) C(14) C(13) C(12) C(11)
C(10) C( 9) C( 8) C( 7) C( 6) C( 5) C( 4) C( 3) C( 2) C( 1)
#undef C
break;
default:
while( size > 0 ){
size_t w=size/2;
if( p[w] < key ){ p+=w+1; size-=w+1; } else size=w;
}
}
*index=p-array; return p[0]==key;
}
#endif
char *type2string(int type)
{
static char functional_set[] = "functional_set";
static char pathway [] = "pathway";
static char structural_complex [] = "structural_complex";
static char custom_string [] = "custom";
static char null_info [] = "NA";
if (type == type_functional_set) return &functional_set[0];
else if (type == type_pathway) return &pathway[0];
else if (type == type_structural_complex) return &structural_complex[0];
else if (type == type_custom) return &custom_string[0];
return &null_info[0];
}
int cmp_hugo(const void *a, const void *b)
{
return strcmp(((struct smallgenetype *)a)->hugo, ((struct smallgenetype *)b)->hugo);
}
int cmp_by_egid(const void *a, const void *b) // compare entrez gene id
{
if ( ((struct smallgenetype *)a)->egid < ((struct smallgenetype *)b)->egid ) return -1;
else if ( ((struct smallgenetype *)a)->egid > ((struct smallgenetype *)b)->egid ) return 1;
return 0;
}
char *egid2hugo(int egid)
{
struct smallgenetype glocal;
struct smallgenetype *gptr;
glocal.egid = egid;
gptr = (struct smallgenetype *)bsearch(&glocal,by_egids,numgenes,sizeof(struct smallgenetype),cmp_by_egid);
if (gptr) return gptr->hugo;
else return (void *)0;
}
unsigned int hugo2egid(char *h)
{
struct smallgenetype glocal;
struct smallgenetype *gptr;
glocal.hugo = h;
gptr = (struct smallgenetype *)bsearch(&glocal,genes,numgenes,sizeof(struct smallgenetype),cmp_hugo);
if (gptr) return gptr->egid;
else return (unsigned int)UINT_MAX;
}
struct smallgenetype *hugo2geneptr(char *h)
{
struct smallgenetype glocal;
struct smallgenetype *gptr;
glocal.hugo = h;
gptr = (struct smallgenetype *)bsearch(&glocal,genes,numgenes,sizeof(struct smallgenetype),cmp_hugo);
return gptr;
}
unsigned short int hugo2usedindex(char *h)
{
struct smallgenetype glocal;
struct smallgenetype *gptr;
unsigned short int ret;
int idx;
glocal.hugo = h;
gptr = (struct smallgenetype *)bsearch(&glocal,genes,numgenes,sizeof(struct smallgenetype),cmp_hugo);
if (gptr)
{
idx = gptr - &genes[0];
if (idx > 20000) {fprintf(stderr,"ERROR in hub2usedindex %s gptr=%p idx=%d\n",h,gptr,idx); fflush(NULL); exit(0); }
ret = (unsigned short int)idx;
}
else
{
ret = (unsigned short int)USHRT_MAX;
}
return ret;
}
int cmp_ui(const void *a, const void *b)
{
if ( *(unsigned int *)a < *(unsigned int *)b ) return -1;
else if ( *(unsigned int *)a > *(unsigned int *)b ) return 1;
return 0;
}
int cmp_double(const void *a, const void *b)
{
if ( *(double *)a < *(double *)b ) return -1;
else if ( *(double *)a > *(double *)b ) return 1;
return 0;
}
#if 0
int cmp_usi(const void *a, const void *b)
{
if ( *(unsigned short int *)a < *(unsigned short int *)b ) return -1;
else if ( *(unsigned short int *)a > *(unsigned short int *)b ) return 1;
return 0;
}
not used
int cmp_float(const void *a, const void *b)
{
if ( *(float *)a < *(float *)b ) return -1;
else if ( *(float *)a > *(float *)b ) return 1;
return 0;
}
#endif
#if 0
/*
ReservoirSample(S[1..n], R[1..k])
R[1] := S[1]
for i from 2 to k do
j := randomInteger(1, i) // inclusive range
R[i] := R[j]
R[j] := S[i]
for i from k + 1 to n do
j := randomInteger(1, i) // inclusive range
if (j <= k)
R[j] := S[i]
*/
static inline void reservoir(unsigned int s[], int n, unsigned int r[],int k)
{
int i,j;
r[0] = s[0];
for (i=1;i<k;i++)
{
j = rand() % i;
r[i] = r[j];
r[j] = s[i];
}
for (i=k+1;i<n;i++)
{
j = rand() % i;
if (j <= k)
r[j] = s[i];
}
return;
}
#endif
// -- for benjamini hochberg FDR ...
struct ordertype
{
double val;
int order;
};
static int cmp_ordertype_by_order(const void *a, const void *b)
{
struct ordertype *aa;
struct ordertype *bb;
aa = (void *)a;
bb = (void *)b;
if (aa->order > bb->order) return 1;
else if (aa->order < bb->order) return -1;
return 0;
}
int cmp_ordertype_by_val_REV(const void *a, const void *b)
{
struct ordertype *aa;
struct ordertype *bb;
aa = (void *)a;
bb = (void *)b;
if (aa->val < bb->val) return 1;
else if (aa->val > bb->val) return -1;
// if (aa>bb) return 1; else if (aa<bb) return -1;
return 0;
}
static void benjaminihochberg(int n,double pvals[], double returnpvals[])
{
/*
here's the code from R that I re-imagined
i <- lp:1L
o <- order(p, decreasing = TRUE)
ro <- order(o)
pmin(1, cummin( n / i * p[o] ))[ro]
*/
int j,k;
struct ordertype *i;
struct ordertype *o;
struct ordertype *po;
struct ordertype *cummin;
// struct ordertype *ro;
// struct ordertype *intermed;
// fprintf(stderr,"rpf in benjaminihochberg\n"); fflush(stderr);
i = (struct ordertype *)malloc(sizeof(struct ordertype)*n);
for (k=n,j=0;j<n;j++,k--) (i+j)->order=k;
#define RDEBUG 0
#if RDEBUG
FILE *fp;
fp = fopen("test.pvals","w");
#endif
o = (struct ordertype *)malloc(sizeof(struct ordertype)*n);
for (j=0 ; j<n ; j++)
{
#if RDEBUG
fprintf(fp,"%20.18f\n",pvals[j]);
#endif
(o+j)->val=pvals[j];
(o+j)->order=j+1;
}
#if RDEBUG
fclose(fp);
#endif
qsort(o,n,sizeof(struct ordertype),cmp_ordertype_by_val_REV);
#if 0
ro = (struct ordertype *)malloc((sizeof(struct ordertype))*n);
for (j=0;j<n;j++)
{
(ro+j)->val = (double)(o+j)->order;
(ro+j)->order = j+1;
}
qsort(ro,n,sizeof(struct ordertype),cmp_ordertype_by_val);
#endif
po = (struct ordertype *)malloc(sizeof(struct ordertype)*n);
memset(po,0,sizeof(struct ordertype)*n);
for (j=0;j<n;j++)
{
(po+j)->val = (double)pvals[j];
(po+j)->order = (o->order); // why the hell isn't this ro? what the what?
}
qsort(po,n,sizeof(struct ordertype),cmp_ordertype_by_val_REV); // == p[o]
cummin = (struct ordertype *)malloc((sizeof(struct ordertype))*n); // holds n / i * po
for (j=0;j<n;j++)
{
(cummin+j)->val = (double)n / (double)(i+j)->order * ((po+j)->val) ;
}
// Rcode: pmin(1, cummin( n / i * p[o] ))[ro] ******************
for (j=1;j<n;j++)
{
if ((cummin+j)->val > (cummin+j-1)->val)
(cummin+j)->val = (cummin+j-1)->val;
}
for (j=0;j<n;j++)
{
if ((cummin+j)->val > 1)
(cummin+j)->val = 1;
(cummin+j)->order = (o+j)->order ;
}
qsort(cummin,n,sizeof(struct ordertype),cmp_ordertype_by_order);
#if RDEBUG
FILE *fp2;
fp2 = fopen("test.fdrs","w");
#endif
for (j=0;j<n;j++)
{
returnpvals[j] = (cummin+j)->val;
#if RDEBUG
fprintf(fp2,"%20.18f\n",returnpvals[j]);
#endif
}
#if RDEBUG
fclose(fp2);
#endif
if (i) free(i);
if (o) free(o);
if (po) free(po);
if (cummin) free(cummin);
return;
}
#if 1
inline void subsamp(unsigned int s[], int n, int k)
{
int i,j,dncnt;
unsigned int ui;
for (dncnt=n,i=0;i<k;i++,dncnt--)
{
j = (rand() % dncnt) + i;
ui = s[i];
s[i] = s[j];
s[j] = ui;
}
return;
}
void shuffle(unsigned int s[], int n)
{
int i,j;
unsigned int ui;
// for i from 0 to n−2 do
// j ← random integer such that i ≤ j < n
// exchange a[i] and a[j]
// or
// for i from n−1 downto 1 do
// j ← random integer such that 0 ≤ j ≤ i
// exchange a[j] and a[i]
for (i=n-1;i>=1;i--)
{
#ifdef L2P_USING_R
j = (int)(unif_rand() * (double)i); // unif_rand() appears to return between 0.0 and 1.0
#else
j = rand() % i;
#endif
ui = s[i];
s[i] = s[j];
s[j] = ui;
}
return;
}
#if 0
int permute_test(struct used_path_type used_paths[],unsigned int num_used_paths,unsigned int *real_universe,unsigned int real_universe_cnt, int incnt)
{
unsigned int r[40002];
double p[NUM_PERMUTES];
struct used_path_type *uptr; // used path pointer
int j,k;
unsigned int ui;
unsigned int usi_j;
double d;
int tmphitcnt;
unsigned int ui_uk,ui_ej;
int kickat;
for (ui=0 ; ui<num_used_paths ; ui++)
{
uptr = &used_paths[ui];
uptr->pval4 = uptr->fdr4 = (double)1.0;
if (uptr->pval == 1.0)
{
uptr->pval4 = 1.0;
continue;
// don't bother
}
memset(p,0,sizeof(p));
memcpy(r,real_universe,real_universe_cnt*sizeof(unsigned int));
radix_ui(r,ingenecnt);
shuffle(r,real_universe_cnt);
// subsamp(r, real_universe_cnt,ingenecnt); // inlined if optimization on
#if 0
for (k=0;k<ingenecnt;k++)
{
fprintf(stderr,"k=%d %u ",k,*(r+k));
z = egid2hugo(*(r+k));
if (z) fprintf(stderr,"%s",z);
else fprintf(stderr,"ERROR");
fprintf(stderr,"\n");
}
#endif
for (j=0 ; j<NUM_PERMUTES ; j++)
{
usi_j = k = tmphitcnt = 0;
while ((usi_j<uptr->numfixedgenes) && (k < ingenecnt))
{
ui_ej = *(uptr->egids+usi_j);
ui_uk = *(r+k);
if (ui_ej == ui_uk)
{
#if 0
char *z;
z = egid2hugo(ui_ej);
if (z) fprintf(stderr,"got %s %d",z,ui_ej);
else fprintf(stderr,"ERROR");
fprintf(stderr,"\n");
#endif
tmphitcnt++;
k++;
usi_j++;
continue;
}
else if (ui_ej < ui_uk) usi_j++;
else k++;
}
if (tmphitcnt == 0) d = 1.0;
else
{
d = exact22((int)tmphitcnt,uptr->numfixedgenes-tmphitcnt,ingenecnt,real_universe_cnt-ingenecnt);
// fprintf(stderr,"ex22=%f %d %d %d %d\n",d,(int)tmphitcnt,uptr->numfixedgenes-tmphitcnt,ingenecnt,real_universe_cnt-ingenecnt);
}
p[j] = d;
if ((d < 0.05) && (d < uptr->pval))
{
fprintf(stderr,"upv:%f p[%d]=%f tmphitcnt=%d %d %d %d %d %s\n",uptr->pval,j,d,tmphitcnt,(int)tmphitcnt,uptr->numfixedgenes-tmphitcnt,ingenecnt,real_universe_cnt-ingenecnt,uptr->name);
for (k=0 ; k<NUM_PERMUTES ; k++) fprintf(stderr,"%f ",p[k]);
fprintf(stderr,"\n");
}
}
qsort(p,NUM_PERMUTES,sizeof(double),cmp_double);
if (p[0] == 1.0)
{
uptr->pval4 = 1.0;
continue;
}
kickat = NUM_PERMUTES;
for (j=0;j<NUM_PERMUTES;j++)
{
if (p[j] > uptr->pval)
{
kickat = j;
break;
}
}
fprintf(stderr,"kick at %d d: %f pv= %f\n",kickat,d,uptr->pval);
d = (double)kickat/(double)NUM_PERMUTES;
uptr->pval4 = d;
if (test9999)
{
fprintf(stderr,"test9999\n");
for (j=0 ; j<NUM_PERMUTES ; j++) fprintf(stderr,"%f ",p[j]);
fprintf(stderr,"\n");
}
// fprintf(stderr,"pv4=%f\n",d);
}
return 0;
}
#endif
struct otype // order type
{
unsigned int val;
int order;
};
void FisherYates(unsigned int *p, int n)
{ //implementation of Fisher Yates shuffle
int i, j;
unsigned int tmp;
for (i = n - 1; i > 0; i--)
{
j = random() % (i + 1); //randomize
tmp = p[j];
p[j] = p[i];
p[i] = tmp;
}
return;
}
#if 1
int permute2(struct used_path_type used_paths[],unsigned int num_used_paths, unsigned int incnt, unsigned int num_permutes)
{
unsigned int *tmphits = (unsigned int *)0;
unsigned int *hyper_verse = (unsigned int *)0;
unsigned int num_hyper_genes;
struct used_path_type *uptr; // used path pointer
unsigned int i,j,k;
unsigned int usi_j;
unsigned int ll,ui;
int tmphitcnt;
unsigned int ui_uk,ui_ej;
size_t sizet;
int ret = -1;
unsigned int perm_count;
unsigned int sofar ;
double d;
unsigned int *zhits = (unsigned int *)0; // [num_used_paths][num_permutes];
// unsigned int r[40002];
// char *z;
fprintf(stderr,"rpf permute2 in permute2 incnt=%d num_use_paths=%u, num_permutes=%u\n",incnt,num_used_paths,num_permutes); fflush(stderr);
for (i=0 ; i<num_used_paths ; i++)
{
uptr = (used_paths+i);
if (!uptr->egids) continue;
j = k = ll = 0;
while ((j<uptr->numfixedgenes) && (k < incnt))
{
ui_ej = *(uptr->egids+j);
ui = ingenes[k];
if (ui_ej == ui)
{
*((uptr->genehits) + (ll++)) = ui; // remember, because need to print out later
k++;
j++;
// aug hit count for aug contingency table
continue;
}
else if (ui_ej < ui) j++;
else k++;
}
uptr->hitcnt = ll;
}
for (num_hyper_genes=i=0 ; i<num_used_paths ; i++)
num_hyper_genes = num_hyper_genes + used_paths[i].numfixedgenes; // get num_hyper_genes
fprintf(stderr,"rpf permute2 num_hyper_genes=%d\n",num_hyper_genes); fflush(stderr);
hyper_verse = malloc(num_hyper_genes*(sizeof (unsigned int)));
if (!hyper_verse) goto PERM2_END;
zhits = malloc(num_used_paths * num_permutes * sizeof (unsigned int) );
if (!zhits) goto PERM2_END;
sizet = sizeof(unsigned int) * incnt;
tmphits = malloc(sizet);
if (!tmphits) goto PERM2_END;
for (j=i=0 ; i<num_used_paths ; i++)
{ // get hyper_verse
for (k=0 ; k < used_paths[i].numfixedgenes ; k++)
*(hyper_verse + j++) = *(used_paths[i].egids+k);
}
fprintf(stderr,"rpf permute2 hyper_verse setup , shuffle next , num_hyper_genes=%u\n",num_hyper_genes); fflush(stderr);
#if 0
FILE *fp;
fp=fopen("test7","w");
for (i=0;i<num_hyper_genes;i++)
fprintf(fp,"%u\n",*(hyper_verse+i));
fclose(fp);
#endif
FisherYates(hyper_verse,num_hyper_genes);
#if 0
fp=fopen("test8","w");
for (i=0;i<num_hyper_genes;i++)
fprintf(fp,"%u\n",*(hyper_verse+i));
fclose(fp);
#endif
for (perm_count=0 ; perm_count < num_permutes ; perm_count++)
{
if ((perm_count%1000)==0) { fprintf(stderr,"rpf permute2 loop %d\n",perm_count); fflush(stderr); }
for (sofar=0 ; sofar < incnt ; ) // get same number of unique genes and user incnt (input list)
{
k = (unsigned int)(rand() % num_hyper_genes);
k = *(hyper_verse + k);
#if 0
fprintf(stderr,"rand = %u of %u\n",k,num_hyper_genes);
z = egid2hugo(k);
if (z) fprintf(stderr,"got %s %d hits=%d",z,k,tmphitcnt);
else fprintf(stderr,"ERROR");
fprintf(stderr,"\n");
#endif
for (j=0 ; j<sofar ; j++)
if ((*tmphits+j) == k) break;
if (j < sofar) // already there
continue;
*(tmphits+sofar) = k;
sofar++;
}
qsort(tmphits,incnt,sizeof(unsigned int),cmp_ui);
for (i=0 ; i<num_used_paths ; i++)
{
uptr = &used_paths[i];
uptr->pval4 = uptr->fdr4 = (double)1.0;
usi_j = k = tmphitcnt = 0;
while ((usi_j<uptr->numfixedgenes) && (k < incnt))
{
ui_ej = *(uptr->egids+usi_j);
ui_uk = *(tmphits+k);
if (ui_ej == ui_uk)
{
tmphitcnt++;
#if 0
z = egid2hugo(ui_uk);
if (z) fprintf(stderr,"got %s %d hits=%d",z,ui_ej,tmphitcnt);
else fprintf(stderr,"ERROR");
fprintf(stderr,"\n");
#endif
k++;
usi_j++;
continue;
}
else if (ui_ej < ui_uk) usi_j++;
else k++;