-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrinterface.c
1315 lines (1194 loc) · 49.7 KB
/
rinterface.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
/*
R interface for l2p
compiled as part of R build package
example compilation is :
/usr/local/GCC/11.3.0/bin/gcc -I"/usr/local/apps/R/4.4/4.4.0/lib64/R/include" -DNDEBUG -DL2P_USING_R -Wall -Wextra -I/usr/local/include -DL2P_USING_R -Wall -Wextra -fpic -march=haswell -mtune=generic -O2 -c rinterface.c -o rinterface.o
*/
// VVV this should be set to make this file work
#ifdef L2P_USING_R
#include <R.h>
#include <Rdefines.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <math.h>
#include <limits.h>
#include <errno.h>
#include "pathworks.h"
#if 0
#include "small.h"
#endif
#include "big.h"
#define RDEBUG 0
extern unsigned short int pwgenes[];
extern struct smallgenetype genes[];
extern struct pwtype pws[];
extern int numpws;
extern int numgenes;
extern int numpwgenes;
extern struct smallgenetype *by_egids; // a copy of "genes" ,but sorted by egid (entrez gene id )
extern unsigned int ingenes[MAX_INGENES];
extern unsigned int ingenecnt;
void category_code_to_string(unsigned int cat,char puthere[])
{
if (cat & CAT_NCBI_BIOCYC) strcpy(puthere,"BIOCYC");
else if (cat & CAT_NCBI_GO ) strcpy(puthere,"GO");
else if (cat & CAT_NCBI_KEGG ) strcpy(puthere,"KEGG");
else if (cat & CAT_NCBI_PANTH ) strcpy(puthere,"PANTH");
else if (cat & CAT_NCBI_PID ) strcpy(puthere,"PID");
else if (cat & CAT_NCBI_REACTOME ) strcpy(puthere,"REACTOME");
else if (cat & CAT_NCBI_WikiPathways ) strcpy(puthere,"WikiPathways");
else if (cat & CAT_MSIG_C1 ) strcpy(puthere,"C1");
else if (cat & CAT_MSIG_C2 ) strcpy(puthere,"C2");
else if (cat & CAT_MSIG_C3 ) strcpy(puthere,"C3");
else if (cat & CAT_MSIG_C4 ) strcpy(puthere,"C4");
else if (cat & CAT_MSIG_C5 ) strcpy(puthere,"C5");
else if (cat & CAT_MSIG_C6 ) strcpy(puthere,"C6");
else if (cat & CAT_MSIG_C7 ) strcpy(puthere,"C7");
else if (cat & CAT_MSIG_C8 ) strcpy(puthere,"C8");
else if (cat & CAT_MSIG_H ) strcpy(puthere,"H");
else if (cat & CAT_CUSTOM ) strcpy(puthere,"CUSTOM");
else strcpy(puthere,"UNKNOWN");
// else if (cat & CAT_MSIG_ARCHIVED ) strcpy(puthere,"ARCHIVED");
}
struct ordertype // used for benjamini hochberg FDR
{
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;
}
void benjaminihochberg(int n,double pvals[], double returnpvals[])
{
int j,k;
struct ordertype *i;
struct ordertype *o;
struct ordertype *po;
struct ordertype *cummin;
// struct ordertype *ro;
// struct ordertype *intermed;
i = (struct ordertype *)malloc((sizeof(struct ordertype))*n);
for (k=n,j=0;j<n;j++,k--) (i+j)->order=k;
#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);
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 0
double lngamm(double z)
// Reference: "Lanczos, C. 'A precision approximation
// of the gamma double ', J. SIAM Numer. Anal., B, 1, 86-96, 1964."
// Translation of Alan Miller's FORTRAN-implementation
// See http://lib.stat.cmu.edu/apstat/245
{
double x = 0;
x += 0.1659470187408462e-06/(z+7);
x += 0.9934937113930748e-05/(z+6);
x -= 0.1385710331296526 /(z+5);
x += 12.50734324009056 /(z+4);
x -= 176.6150291498386 /(z+3);
x += 771.3234287757674 /(z+2);
x -= 1259.139216722289 /(z+1);
x += 676.5203681218835 /(z);
x += 0.9999999999995183;
// return(Math.log(x)-5.58106146679532777-z+(z-0.5)*Math.log(z+6.5));
return(log(x)-5.58106146679532777-z+(z-0.5)*log(z+6.5));
}
double left,right,twotail;
#endif
void int2bin(int n, char s[])
{
int i;
for (i=0;i<40;i++) s[0] = (char)0;
// determine the number of bits needed ("sizeof" returns bytes)
int nbits = sizeof(n) * 8;
// forcing evaluation as an unsigned value prevents complications
// with negative numbers at the left-most bit
unsigned int u = *(unsigned int*)&n;
unsigned int mask = 1 << (nbits-1); // fill in values right-to-left
for (i = 0; i < nbits; i++, mask >>= 1)
{
s[i] = ((u & mask) != 0) + '0';
s[i+1] = (char)0;
}
return;
}
static int bitCount_unsigned(unsigned int n)
{
int cnt = 0;
while (n)
{
cnt += n % 2;
n >>= 1;
}
return cnt;
}
static int parsecats(char *z, unsigned int *catspat)
{
char ts[16][16];
int bit;
int j,k;
int toks;
for (j=0 ; *(z+j) ; j++)
{
if (*(z+j) == ',') *(z+j) = ' ';
}
memset(ts,0,sizeof(ts));
toks = sscanf(z,"%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s ",
ts[0], ts[1], ts[2], ts[3], ts[4], ts[5], ts[6], ts[7], ts[8], ts[9],
ts[10], ts[11], ts[12], ts[13], ts[14], ts[15]);
for (k=0;k<toks;k++)
{
bit=string_to_category_code(ts[k]);
if (bit)
*catspat = bit | *catspat;
else
{
fprintf(stderr,"ERROR: invalid category = %s\n",ts[k]);
return 0;
}
}
j = bitCount_unsigned(*catspat) ;
if (j == 0)
{
#ifdef L2P_USING_R
return 0;
#else
fprintf(stderr,"ERROR: no categories specified\n");
usage();
return -1;
#endif
}
return 0;
}
int l2pfunc_R(unsigned int *user_in_genes, unsigned int user_incnt, struct used_path_type *usedpaths,unsigned int num_used_paths,unsigned int real_universe_cnt, unsigned int *real_universe,int permute_flag,int gpcc_flag)
{
struct used_path_type *uptr; // used path pointer
unsigned int ui_uk;
unsigned int ui_ej;
unsigned int i;
unsigned int j,k,ll;
int ret = 0;
// this shuts up "unused" compiler warning
#define UNUSED(x) (void)(x)
UNUSED(permute_flag);
// fprintf(stderr,"l2pfunc_R : %p user_incnt=%u %p %u %u %p %u %u\n", user_in_genes, user_incnt, usedpaths,num_used_paths,real_universe_cnt, real_universe,permute_flag,gpcc_flag);
for (i=0 ; i<num_used_paths ; i++)
{
uptr = (usedpaths+i);
if (!uptr->egids) continue;
j = k = ll = 0;
while ((j<uptr->numfixedgenes) && (k < user_incnt))
{
ui_ej = *(uptr->egids+j);
ui_uk = *(user_in_genes+k);
if (ui_ej == ui_uk)
{
*((uptr->genehits) + (ll++)) = ui_uk; // remember, because need to print out later
k++;
j++;
continue;
}
else if (ui_ej < ui_uk) j++;
else k++;
}
uptr->hitcnt = ll;
}
if (gpcc_flag == 1)
{
//fprintf(stderr,"gpccdbg in GPCC flag, user_incnt=%d\n",user_incnt); fflush(stderr);
for (i=0;i<user_incnt;i++)
ingenes[i] = *(user_in_genes+i);
ingenecnt = user_incnt;
// fprintf(stderr,"gpccdbg before GPCC %p %u %u %p\n",usedpaths,num_used_paths,real_universe_cnt,real_universe); fflush(stderr);
GPCC(usedpaths,num_used_paths,real_universe_cnt,real_universe);
//fprintf(stderr,"gpccdbg after GPCC\n"); fflush(stderr);
}
return ret;
}
SEXP l2p(SEXP lst, SEXP categories, SEXP universe, SEXP custompws, SEXP customfn, SEXP gmtfld2, SEXP universefn, SEXP permute_arg, SEXP oneside_arg, SEXP gpcc_arg,
SEXP legacy_arg, SEXP extra_arg)
{
char tmps2[PATH_MAX]; // temp string for "category"
char tmps[512];
char universe_file[PATH_MAX];
int gmtfld2_i = 0; // treat field 2 in gmt custom pathways file as an accession
char custom_file[PATH_MAX*5];
unsigned int i,k,k2;
unsigned int j;
unsigned int len_of_user_pws = 0; // length of list of lists , from Rf_length() which return R_len_t ( which in int? )
unsigned int len_of_vector = 0;
struct used_path_type *uptr;
unsigned int *user_in_univ_ptr = (unsigned int *)0;
unsigned int *in_universe_original = (unsigned int *)0;
unsigned int in_universe_cnt = 0;
unsigned int *real_universe = (unsigned int *)0;
unsigned int real_universe_cnt = 0;
int extra_flag = 0;
int legacy_flag = 0;
int permute_flag = 0;
int gpcc_flag = 0;
int user_universe_flag = 0;
unsigned int user_incnt = 0;
int oneside = 0;
unsigned int num_used_paths = 0;
unsigned int len = 0;
unsigned int *user_in_genes = (unsigned int *)0;
unsigned int *user_in_genes_original = (unsigned int *)0;
char *p2 = (char *)0;
char *zz = (char *)0;
unsigned int catspat = 0; // categories pattern , if 0, use all
unsigned int ui = 0;
struct used_path_type *u = (struct used_path_type *)0;
int protect_cnt = 0;
unsigned int maxflds = 0;
struct custom_type *mycustompw = (struct custom_type *)0;
struct custom_type *mycustompwptr = (struct custom_type *)0;
double fdr_for_output;
SEXP list = (SEXP)0;
SEXP pval = (SEXP)0; //
SEXP fdr = (SEXP)0; //
SEXP GPCC_pval = (SEXP)0;
SEXP GPCC_FDR = (SEXP)0;
SEXP number_genes_in_pathway = (SEXP)0;
SEXP percent_hits = (SEXP)0;
SEXP enrichment_score = (SEXP)0; // if positive, genes are OVER REPRESENTED, if negative genes are UNDER REPRESENTED
SEXP percent_gene_hits_per_pathway = (SEXP)0; //
SEXP number_hits = (SEXP)0; // number of genes hit in pathway
SEXP number_misses = (SEXP)0; // number of genes in the pathway
SEXP number_user_genes = (SEXP)0; // total count of user genes (user input)
SEXP total_genes_minus_input = (SEXP)0; // total number of unique genes in all pathways
SEXP pathway_id = (SEXP)0; // canonical accession ( if available, otherwise assigned by us )
// SEXP pathway_type = (SEXP)0;
SEXP category = (SEXP)0; // KEGG,REACTOME,GO,PANT(=PANTHER),PID=(pathway interaciton database)
SEXP pathway_name = (SEXP)0; // Name of pathway
SEXP hits = (SEXP)0; // genes_space_separated HGNC genes from user that hit the pathway
SEXP allgenesinpathway= (SEXP)0; // genes_space_separated HGNC genes from user that hit the pathway
SEXP allgenesinpw= (SEXP)0; // genes_space_separated HGNC genes from user that hit the pathway
SEXP permute_pval = (SEXP)0;
SEXP universe_count = (SEXP)0;
SEXP user_input_count = (SEXP)0;
SEXP odds_ratio = (SEXP)0;
SEXP Rret = (SEXP)0;
SEXP cls = (SEXP)0; // R class
SEXP nam = (SEXP)0; // R name
SEXP rownam = (SEXP)0; // row names
(void)setup_by_egids();
if (Rf_isNull(permute_arg)) { permute_flag = 0; } else { permute_flag = asInteger(permute_arg); }
if (Rf_isNull(extra_arg)) { extra_flag = 0; } else { extra_flag = asInteger(extra_arg); }
if (Rf_isNull(legacy_arg)) { legacy_flag = 0; } else { legacy_flag = asInteger(legacy_arg); }
if (Rf_isNull(gpcc_arg)) { gpcc_flag = 0; } else { gpcc_flag = asInteger(gpcc_arg); }
len = length(lst);
user_in_genes = (unsigned int *)malloc(sizeof(unsigned int)*len); // remember to free this
if (!user_in_genes)
return (SEXP) -1; // why not 0 ?
user_in_genes_original = (unsigned int *)malloc(sizeof(unsigned int)*len); // remember to free this
if (!user_in_genes_original)
{
free(user_in_genes);
user_in_genes = (unsigned int *)0;
return (SEXP) -1; // why not 0 ?
}
for (k = i = 0; i < len; i++)
{
strcpy(tmps,CHAR(STRING_ELT(lst, i)));
ui = hugo2egid(tmps);
if (ui != UINT_MAX) { *(user_in_genes_original+k) = ui; k++; }
}
#if RADIX
radix_ui(user_in_genes_original,k);
#else
qsort(user_in_genes_original,k,sizeof(unsigned int),cmp_ui);
#endif
// fprintf(stderr,"adbg legacy_flag is %d",legacy_flag);
// fprintf(stderr,"gpccdbg , in l2p, gpcc_arg=%p\n",gpcc_arg);
if (Rf_isNull(oneside_arg))
{
oneside = 0;
}
else
{
oneside = asInteger(oneside_arg);
if ((oneside < 0) || (oneside > 2)) oneside = 0; // just give them twosided fisher's exact test
}
universe_file[0] = custom_file[0] = tmps[0] = tmps2[0] = (char)0;
if (Rf_isNull(custompws))
{
len_of_user_pws = 0;
}
else
{
/* struct custom_type { char *name; char *optional; unsigned int numgenes; unsigned int *genes; }; */
//xxx
len_of_user_pws = (int)length(custompws);
mycustompw = (struct custom_type *)malloc(sizeof(struct custom_type )*len_of_user_pws); // remember to free this
memset(mycustompw,0,sizeof(struct custom_type )*len_of_user_pws);
for (i=0;i<len_of_user_pws;i++)
{
mycustompwptr = mycustompw + i;
list = VECTOR_ELT(custompws, i);
len_of_vector = length(list);
mycustompwptr->genes = (unsigned int *)malloc(sizeof(unsigned int)*len_of_vector);
memset( mycustompwptr->genes,0,(sizeof(unsigned int)*len_of_vector));
mycustompwptr->numgenes = 0; // will set this properly later , initialize to zero for now
for (j=0 ; j<len_of_vector ; j++)
{
memset(tmps2,0,sizeof(tmps2));
strncpy(tmps2,CHAR(STRING_ELT(list, j)),PATH_MAX-2);
if (j == 0) mycustompwptr->name = strdup(tmps2); // name. check: must free this!
else if (j == 1)
{
// gmt (gene matrix transposed ) spec says second field meaning is unspecified. could this be a URL someday?
mycustompwptr->optional = strdup(tmps2);
}
else
{
ui = hugo2egid(tmps2);
// fprintf(stderr,"custgene is i=%d j=%d %u %s\n",i,j,ui,tmps2); fflush(stderr);
if (ui != UINT_MAX)
{
*(mycustompwptr->genes + j - 2) = ui; // nb:-2 . remember fields 1 and 2 are not the genes, fields3 and greater are
mycustompwptr->numgenes++;
}
}
}
#if RADIX
radix_ui(mycustompwptr->genes,mycustompwptr->numgenes);
#else
qsort(mycustompwptr->genes,mycustompwptr->numgenes,sizeof(unsigned int),cmp_ui);
#endif
}
}
if (Rf_isNull(universe)) // if (universe == (SEXP)0)
user_universe_flag = 0;
else
user_universe_flag = 1;
if (Rf_isNull(categories))
{
category_set_all(&catspat);
// fprintf(stderr,"no parsecats after category_set_all: %u (null categories) \n",catspat);
}
else
{
if (isVector(categories)) // it's really a dam string
{
tmps[0] = (char)0;
len = length(categories);
for (k2 = 0; k2 < len; k2++)
{
if (k2) strcat(tmps,",");
strncpy(tmps2,CHAR(STRING_ELT(categories, k2)),PATH_MAX-2);
strcat(tmps,tmps2);
}
(void)parsecats(tmps,&catspat); // set catpats
}
else
{
fprintf(stderr,"NOTE: Not sure what's up. Can't parse categories \n"); fflush(NULL);
}
}
categories_pattern_to_strings(catspat,tmps);
// fprintf(stderr,"GOTEST rpfdbg categories here 9, catspats=%x = \"%s\"\n",catspat,tmps); fflush(NULL);
if (Rf_isNull(customfn)) {} else strncpy(custom_file,CHAR(STRING_ELT(customfn, 0)),sizeof(custom_file)-2);
if (Rf_isNull(universefn)) {} else strncpy(universe_file,CHAR(STRING_ELT(universefn, 0)),PATH_MAX-2);
if (Rf_isNull(gmtfld2)) {}
else
{
gmtfld2_i = asInteger(oneside_arg);
// fprintf(stderr,"rpf after gmtflds as integer. gmtfld2_i=%d\n",gmtfld2_i); fflush(NULL);
}
for (j=i=0;i<k;i++)
{ // de duplicate list
if (i > 0)
{
if ( *(user_in_genes_original+i) == *(user_in_genes_original+i-1) )
continue;
*(user_in_genes+j) = *(user_in_genes_original+i);
j++;
}
else
{
*(user_in_genes+j) = *(user_in_genes_original+i);
j++;
}
}
user_incnt = (unsigned int )j;
len = length(universe);
if (user_universe_flag == 1)
{
user_in_univ_ptr = (unsigned int *)malloc(sizeof(unsigned int)*len); // remember to free this
if (!user_in_univ_ptr) return (SEXP) -1;
in_universe_original = (unsigned int *)malloc(sizeof(unsigned int)*len); // remember to free this
if (!in_universe_original)
{
free(user_in_univ_ptr);
return (SEXP) -1;
}
for (k = i = 0; i < len; i++)
{
strcpy(tmps,CHAR(STRING_ELT(universe, i)));
ui = hugo2egid(tmps);
if (ui != UINT_MAX) { *(in_universe_original+k) = ui; k++; }
}
#if RADIX
radix_ui( in_universe_original,k);
#else
qsort(in_universe_original,k,sizeof(unsigned int),cmp_ui);
#endif
for (j=i=0;i<k;i++)
{ // de duplicate universe
if (i > 0)
{
if ( *(in_universe_original+i) == *(in_universe_original+i-1) )
continue;
*(user_in_univ_ptr+j) = *(in_universe_original+i);
j++;
}
else
{
*(user_in_univ_ptr+j) = *(in_universe_original+i);
j++;
}
}
in_universe_cnt = j;
}
if (in_universe_original) { free(in_universe_original); in_universe_original = (void *)0; }
// xxx fix
// fprintf(stderr,"GOTEST before setup_used_paths\n"); fflush(NULL);
u = setup_used_paths(&num_used_paths, catspat,universe_file,in_universe_cnt,user_in_univ_ptr,custom_file,gmtfld2_i,&real_universe_cnt,&real_universe,len_of_user_pws,mycustompw);
// fprintf(stderr,"GOTEST after setup_used_paths\n"); fflush(NULL);
//fprintf(stderr,"in gpccdbg after setup_used_path cats=%x after setup_used_paths() \n",catspat); fflush(stderr);
// NO, freed in setup_used_paths if (user_in_univ_ptr) { free(user_in_univ_ptr); user_in_univ_ptr = (void *)0; }
#if 0
FILE *fptmp=fopen("tmp.uig.preint","w");
for (ui=0;ui<user_incnt;ui++)
{
char *zz;
zz = egid2hugo(*(user_in_genes+ui));
fprintf(fptmp,"%s\n",zz);
}
fclose(fptmp);
#endif
// xxx
unsigned int *tmp_in_genes;
unsigned int uj,tmpegid;
unsigned int *uiptr;
tmp_in_genes = (unsigned int *)malloc(sizeof(unsigned int)*user_incnt); // remember to free this
for (ui=uj=0;ui<user_incnt;ui++)
{
tmpegid = *(user_in_genes+ui);
uiptr = (unsigned int *)bsearch(&tmpegid,real_universe,real_universe_cnt,sizeof(unsigned int),cmp_ui);
if (uiptr)
{
*(tmp_in_genes+uj) = *uiptr;
uj++;
}
}
user_incnt = uj;
memcpy(user_in_genes,tmp_in_genes,user_incnt*sizeof(unsigned int));
free(tmp_in_genes);
#if 0
FILE *fptmp2=fopen("tmp.uig.afterint","w");
for (ui=0;ui<user_incnt;ui++)
{
char *zzz;
zzz = egid2hugo(*(user_in_genes+ui));
fprintf(fptmp2,"%s\n",zzz);
}
fclose(fptmp2);
#endif
// fprintf(stderr,"gpccdbg in l2p, before l2pfunc_R gpcc_flag=%d user_incnt=%u\n",gpcc_flag,user_incnt); fflush(stderr);
GetRNGstate();
(void)l2pfunc_R(user_in_genes,user_incnt,u,num_used_paths,real_universe_cnt,real_universe,permute_flag,gpcc_flag);
PutRNGstate();
//fprintf(stderr,"gpccdbg in l2p, after l2pfunc_R\n"); fflush(stderr);
// fprintf(stderr,"abcdef 2\n"); fflush(NULL);
if (gpcc_flag)
{
// fprintf(stderr,"gpccdbg: in l2p - in gpcc_flag output\n"); fflush(stderr);
#if 0
double gpcc_p; double gpcc_fdr; double enrichment_score; // ratio
Columns that will be output with L2P:
8 "genes_in_pathway"
9 "percent_hits"
10 "pathway_id"
11 "category"
12 "hits"
13 "genesinpathway"
8 Number_genes_in_pathway Number of genes in the pathway (size of pathway)*
9 Percent_hits Number_hits/total genes in pathway* <- tie-breaker #3
10 Pathway_id Canonical accession ( if available, otherwise assigned by us )
11 Category KEGG,REACTOME,GO,PANT(=PANTHER),PID=(pathway interaction database) note: was ,source,
12 Pathway_type Functional_set,pathway,structural_complex,custom
13 Genes_in_pathway HGNC genes from user that hit the pathway
Optional Columns: This is for Rich to put into a flag where optional columns can be added to the L2P output (Optionalcols = 1, by default Optionalcols = 0)
* A – universe – (DEG count + pathway count)
B – pathway count – number of hits XXX already
C – total DEG count - number of hits
D – number of hits
Odds_ratio odds ratio
Sum_of_pathways Sum of unique pathways where pathway genes are also found
universe
inputlistcount
or
#endif
if (extra_flag == 2) maxflds = 17; else maxflds = 13;
PROTECT(Rret = Rf_allocVector(VECSXP, maxflds)); // a list
protect_cnt++;
// fprintf(stderr,"gpccdbg: in l2p - in gpcc_flag output 1\n"); fflush(stderr);
PROTECT(pathway_name=Rf_allocVector(STRSXP, num_used_paths)); // 1
PROTECT(enrichment_score=Rf_allocVector(REALSXP, num_used_paths)); // 2
PROTECT(GPCC_pval=Rf_allocVector(REALSXP, num_used_paths )); // 3
PROTECT(GPCC_FDR=Rf_allocVector(REALSXP, num_used_paths )); // 4
PROTECT(pval=Rf_allocVector(REALSXP, num_used_paths )); // 5
PROTECT(fdr=Rf_allocVector(REALSXP, num_used_paths)); // 6
PROTECT(number_hits=Rf_allocVector(INTSXP, num_used_paths)); // 7
PROTECT(number_genes_in_pathway =Rf_allocVector(INTSXP, num_used_paths)); // 8
PROTECT(percent_hits=Rf_allocVector(REALSXP, num_used_paths)); // 9
PROTECT(pathway_id=Rf_allocVector(STRSXP, num_used_paths)); // 10
PROTECT(category=Rf_allocVector(STRSXP, num_used_paths)); // 11 is "l2p internal" an integer, but convert to string
// NO PROTECT(pathway_type=Rf_allocVector(STRSXP, num_used_paths)); // 12
PROTECT(hits=Rf_allocVector(STRSXP, num_used_paths)); // 13
PROTECT(allgenesinpw=Rf_allocVector(STRSXP, num_used_paths)); // 14
if (extra_flag == 2)
{
PROTECT(universe_count=Rf_allocVector(INTSXP, num_used_paths)); // 14
PROTECT(user_input_count=Rf_allocVector(INTSXP, num_used_paths)); // 15
PROTECT(odds_ratio=Rf_allocVector(REALSXP, num_used_paths)); // 16
}
// note: protect_cnt is not zero at this point
protect_cnt += maxflds;
for (ui=0 ; ui<num_used_paths ; ui++)
{
uptr = (u+ui);
SET_STRING_ELT(pathway_name, ui, mkChar(uptr->name) ); // 1
REAL(enrichment_score)[ui] = uptr->enrichment_score; // 2
// if (strcmp(uptr->acc,"ko00010") == 0) { fprintf(stderr,"adbg : l2p (rinterface.c) ko00010 enrichment_score is %f \n",uptr->enrichment_score); }
REAL(GPCC_pval)[ui] = uptr->gpcc_p; // 3
REAL(GPCC_FDR)[ui] = uptr->gpcc_fdr; // 4
REAL(pval)[ui] = uptr->pval; // 5
REAL(fdr)[ui] = uptr->fdr; // 6
INTEGER(number_hits)[ui] = uptr->hitcnt; // 7
INTEGER(number_genes_in_pathway)[ui] = uptr->numfixedgenes; // 8
if ( (uptr->a == 0) && (uptr->b == 0) ) REAL(percent_hits)[ui] = (double)0;
else
{
REAL(percent_hits)[ui] = (double)((double)uptr->hitcnt / (double)(uptr->numfixedgenes));
}
SET_STRING_ELT(pathway_id, ui, mkChar(uptr->acc) );
category_code_to_string( uptr->category , tmps);
SET_STRING_ELT(category, ui, mkChar(tmps));
INTEGER(number_hits)[ui] = uptr->hitcnt;
p2 = malloc((uptr->hitcnt +2) * 34); // plus some extra space
memset(p2,0,(uptr->hitcnt +2) * 34);
for (j=0 ; j<uptr->hitcnt ; j++)
{
zz = egid2hugo(*((uptr->genehits)+j));
if (zz)
{
if (j == 0) sprintf(tmps,"%s",zz);
else sprintf(tmps," %s",zz);
strcat(p2,tmps);
}
}
SET_STRING_ELT( allgenesinpathway, ui, mkChar( p2 ) );
if (p2) { free(p2); p2 = (char *)0; }
if (extra_flag == 2)
{
INTEGER(universe_count)[ui] = uptr->d ; // 14
INTEGER(user_input_count)[ui] = uptr->c ; // 15
REAL(odds_ratio)[ui] = uptr->gpcc_OR ; // 16
}
}
// fprintf(stderr,"gpccdbg: in l2p - in gpcc_flag output 3\n"); fflush(stderr);
#if 0
1 Pathway_name Name of pathway
2 Enrichment_score ((number_hits /(number_hits+number_misses)) - (number_user_genes/(number_user_genes+total_gens_minus_input))) * 100 <- tie-breaker #2
3 GPCC_pval Gpcc (gene pathway count correction) pval <- sort on this first (#1)
4 GPCC_FDR False discovery rate: benjamini hochberg (of GPCC p-value) <- this is what we recommend users use
5 FET_pval Fishers exact p-value (legacy)
6 FET_FDR False discovery rate (FET)
7 Number_hits Number of genes hit in pathway
8 Number_genes_in_pathway Number of genes in the pathway (size of pathway)*
9 Percent_hits Number_hits/total genes in pathway* <- tie-breaker #3
10 Pathway_id Canonical accession ( if available, otherwise assigned by us )
11 Category KEGG,REACTOME,GO,PANT(=PANTHER),PID=(pathway interaction database) -- was ,source,
12 Pathway_type Functional_set,pathway,structural_complex,custom
13 Genes_in_pathway HGNC genes from user that hit the pathway
Optional Columns: This is for Rich to put into a flag where optional columns can be added to the L2P output (Optionalcols = 1, by default Optionalcols = 0)
A – universe – (DEG count + pathway count)
B – pathway count – number of hits
C – total DEG count - number of hits
D – number of hits
Odds_ratio odds ratio
Sum_of_pathways Sum of unique pathways where pathway genes are also found
#endif
SET_VECTOR_ELT( Rret,0, pathway_name);
SET_VECTOR_ELT( Rret,1, enrichment_score);
SET_VECTOR_ELT( Rret,2, GPCC_pval);
SET_VECTOR_ELT( Rret,3, GPCC_FDR);
SET_VECTOR_ELT( Rret,4, pval);
SET_VECTOR_ELT( Rret,5, fdr);
SET_VECTOR_ELT( Rret,6, number_hits);
SET_VECTOR_ELT( Rret,7, number_genes_in_pathway);
SET_VECTOR_ELT( Rret,8, percent_hits);
SET_VECTOR_ELT( Rret,9, pathway_id);
SET_VECTOR_ELT( Rret,10,category);
SET_VECTOR_ELT( Rret,11,hits); // list of genes that matched
SET_VECTOR_ELT( Rret,12,allgenesinpw); // list of all genes in pw
if (extra_flag == 2)
{
SET_VECTOR_ELT( Rret,13,universe_count);
SET_VECTOR_ELT( Rret,14,user_input_count);
SET_VECTOR_ELT( Rret,15,odds_ratio);
}
PROTECT(cls = allocVector(STRSXP, 1)); // class attribute
protect_cnt++;
SET_STRING_ELT(cls, 0, mkChar("data.frame"));
classgets(Rret, cls);
PROTECT(nam = allocVector(STRSXP, maxflds)); // names attribute (column names)
protect_cnt++;
SET_STRING_ELT( nam, 0, mkChar("pathway_name"));
SET_STRING_ELT( nam, 1, mkChar("enrichment_score"));
SET_STRING_ELT( nam, 2, mkChar("GPCC_pval"));
SET_STRING_ELT( nam, 3, mkChar("GPCC_FDR"));
SET_STRING_ELT( nam, 4, mkChar("pval"));
SET_STRING_ELT( nam, 5, mkChar("fdr"));
SET_STRING_ELT( nam, 6, mkChar("number_hits"));
SET_STRING_ELT( nam, 7, mkChar("genes_in_pathway")); // number_genes_in_pathway
SET_STRING_ELT( nam, 8, mkChar("percent_hits"));
SET_STRING_ELT( nam, 9, mkChar("pathway_id"));
SET_STRING_ELT( nam, 10, mkChar("category"));
SET_STRING_ELT( nam, 11, mkChar("hits")); // list of genes that matched
SET_STRING_ELT( nam, 12, mkChar("allgenesinpw")); // list of all genes
if (extra_flag == 2)
{
SET_STRING_ELT( nam, 13, mkChar("universe_count"));
SET_STRING_ELT( nam, 14, mkChar("user_input_count"));
SET_STRING_ELT( nam, 15, mkChar("odds_ratio"));
}
namesgets(Rret, nam);
PROTECT(rownam = allocVector(STRSXP, num_used_paths )); // row.names attribute
protect_cnt++;
for (i=0 ; i<num_used_paths ; i++)
{
uptr = (u+i);
SET_STRING_ELT(rownam, i, mkChar( (char *)uptr->acc) );
}
setAttrib(Rret, R_RowNamesSymbol, rownam);
}
else if (permute_flag)
{
do_just_bh(user_incnt, u,num_used_paths,real_universe_cnt);
PROTECT(Rret = Rf_allocVector(VECSXP, 15)); // a list with 15 elements
protect_cnt++;
maxflds = 15;
PROTECT(pathway_name=Rf_allocVector(STRSXP, num_used_paths));
PROTECT(pval=Rf_allocVector(REALSXP, num_used_paths ));
PROTECT(fdr=Rf_allocVector(REALSXP, num_used_paths));
PROTECT(odds_ratio=Rf_allocVector(REALSXP, num_used_paths ));
PROTECT(permute_pval=Rf_allocVector(REALSXP, num_used_paths ));
PROTECT(enrichment_score=Rf_allocVector(REALSXP, num_used_paths));
PROTECT(percent_gene_hits_per_pathway =Rf_allocVector(REALSXP, num_used_paths));
PROTECT(number_hits=Rf_allocVector(INTSXP, num_used_paths));
PROTECT(number_misses=Rf_allocVector(INTSXP, num_used_paths));
PROTECT(number_user_genes=Rf_allocVector(INTSXP, num_used_paths));
PROTECT(total_genes_minus_input=Rf_allocVector(INTSXP, num_used_paths));
PROTECT(pathway_id=Rf_allocVector(STRSXP, num_used_paths));
PROTECT(category=Rf_allocVector(STRSXP, num_used_paths));// is "l2p internal" an integer, but convert to string
// NO PROTECT(pathway_type=Rf_allocVector(STRSXP, num_used_paths));
PROTECT(hits=Rf_allocVector(STRSXP, num_used_paths));
protect_cnt += 15;
for (ui=0 ; ui<num_used_paths ; ui++)
{
uptr = (u+ui);
REAL(pval)[ui] = uptr->pval; // 0
fdr_for_output = uptr->fdr;
REAL(fdr)[ui] = fdr_for_output; // 1
REAL(pval)[ui] = uptr->OR; // 2
REAL(pval)[ui] = uptr->permute_pval; // 3
REAL(enrichment_score)[ui] = uptr->enrichment_score; // 4
if ( (uptr->a == 0) && (uptr->b == 0) )
REAL(percent_gene_hits_per_pathway)[ui] = (double)0;
else
REAL(percent_gene_hits_per_pathway)[ui] = (double)uptr->a/(double)((uptr->a)+(uptr->b)); // 5
INTEGER(number_hits)[ui] = uptr->a; // 6
INTEGER(number_misses)[ui] = uptr->b; // 7
INTEGER(number_user_genes)[ui] = uptr->c; // 8
INTEGER(total_genes_minus_input)[ui] = uptr->d; // 9
SET_STRING_ELT(pathway_id, ui, mkChar(uptr->acc) ); // 10
category_code_to_string( uptr->category , tmps); // 11
SET_STRING_ELT(category, ui, mkChar(tmps)); // 12
SET_STRING_ELT(pathway_name, ui, mkChar(uptr->name) ); // 13
p2 = malloc((uptr->hitcnt +2) * 34); // plus some extra space
memset(p2,0,(uptr->hitcnt +2) * 34);
for (j=0 ; j<uptr->hitcnt ; j++)
{
zz = egid2hugo(*((uptr->genehits)+j));
if (zz)
{
// rpf xxx abc
if (j==0) sprintf(tmps,"%s",zz);
else sprintf(tmps," %s",zz);
strcat(p2,tmps);
}
}
SET_STRING_ELT( hits, ui, mkChar( p2 ) );
if (p2) { free(p2); p2 = (char *)0; }
}
//fprintf(stderr,"pmf 2\n"); fflush(stderr);
SET_VECTOR_ELT( Rret,0,pathway_name);
SET_VECTOR_ELT( Rret,1, category);
SET_VECTOR_ELT( Rret,2, pval);
SET_VECTOR_ELT( Rret,3, fdr);
SET_VECTOR_ELT( Rret,4, odds_ratio);
SET_VECTOR_ELT( Rret,5, permute_pval);
SET_VECTOR_ELT( Rret,6, enrichment_score);
SET_VECTOR_ELT( Rret,7, percent_gene_hits_per_pathway);
SET_VECTOR_ELT( Rret,8, number_hits);
SET_VECTOR_ELT( Rret,9, number_misses);
SET_VECTOR_ELT( Rret,10, number_user_genes);
SET_VECTOR_ELT( Rret,11, total_genes_minus_input);
SET_VECTOR_ELT( Rret,12, pathway_id);
// NO SET_VECTOR_ELT( Rret,13, pathway_type);
SET_VECTOR_ELT( Rret,13, hits);
maxflds = 13;
PROTECT(cls = allocVector(STRSXP, 1)); // class attribute
protect_cnt++;
SET_STRING_ELT(cls, 0, mkChar("data.frame"));
classgets(Rret, cls);
PROTECT(nam = allocVector(STRSXP, maxflds)); // names attribute (column names)
protect_cnt++;
SET_STRING_ELT( nam, 0, mkChar("pathway_name"));
SET_STRING_ELT( nam, 1, mkChar("category"));
SET_STRING_ELT( nam, 2, mkChar("pval"));
SET_STRING_ELT( nam, 3, mkChar("fdr"));
SET_STRING_ELT( nam, 4, mkChar("OR"));
SET_STRING_ELT( nam, 5, mkChar("permute_pval"));
SET_STRING_ELT( nam, 6, mkChar("enrichment_score"));
SET_STRING_ELT( nam, 7, mkChar("percent_gene_hits_per_pathway"));
SET_STRING_ELT( nam, 8, mkChar("number_hits"));
SET_STRING_ELT( nam, 9, mkChar("number_misses"));
SET_STRING_ELT( nam, 10, mkChar("number_user_genes"));
SET_STRING_ELT( nam, 11, mkChar("total_genes_minus_input"));
SET_STRING_ELT( nam, 12, mkChar("pathway_id"));
SET_STRING_ELT( nam, 13, mkChar("hits"));
namesgets(Rret, nam);
PROTECT(rownam = allocVector(STRSXP, num_used_paths )); // row.names attribute
protect_cnt++;
for (i=0 ; i<num_used_paths ; i++)
{
uptr = (u+i);
SET_STRING_ELT(rownam, i, mkChar( (char *)uptr->acc) );
}
setAttrib(Rret, R_RowNamesSymbol, rownam);
}
else if (legacy_flag == 0)
{
// fprintf(stderr,"abcdef 3\n"); fflush(NULL);
maxflds = 13;
if (extra_flag == 2) maxflds = 17;
do_pvals_and_bh(user_incnt, u, num_used_paths,real_universe_cnt,oneside);
PROTECT(Rret = Rf_allocVector(VECSXP, maxflds)); // a list with 14 elements
protect_cnt += 1;
// fprintf(stderr,"abcdef 3.1\n"); fflush(NULL);
PROTECT(pathway_name=Rf_allocVector(STRSXP, num_used_paths));
PROTECT(category=Rf_allocVector(STRSXP, num_used_paths)); // convert integer code to string later
PROTECT(pval=Rf_allocVector(REALSXP, num_used_paths ));
PROTECT(fdr=Rf_allocVector(REALSXP, num_used_paths));
PROTECT(enrichment_score=Rf_allocVector(REALSXP, num_used_paths));
PROTECT(percent_gene_hits_per_pathway =Rf_allocVector(REALSXP, num_used_paths));
PROTECT(number_hits=Rf_allocVector(INTSXP, num_used_paths));
PROTECT(number_misses=Rf_allocVector(INTSXP, num_used_paths));
PROTECT(number_user_genes=Rf_allocVector(INTSXP, num_used_paths));
PROTECT(total_genes_minus_input=Rf_allocVector(INTSXP, num_used_paths));
PROTECT(pathway_id=Rf_allocVector(STRSXP, num_used_paths));
PROTECT(hits=Rf_allocVector(STRSXP, num_used_paths));
PROTECT(allgenesinpw=Rf_allocVector(STRSXP, num_used_paths));
protect_cnt += maxflds;
// xxx xxx
// fprintf(stderr,"abcdef 3.2, num_used_paths=%d\n",num_used_paths); fflush(NULL);
for (ui=0 ; ui<num_used_paths ; ui++)
{
// fprintf(stderr,"abcdef 3.2.1xxx, num_used_paths=%d, ui=%u\n",num_used_paths,ui); fflush(NULL);
uptr = (u+ui);
SET_STRING_ELT(pathway_name, ui, mkChar(uptr->name) ); // 1
if (uptr->custom_category_name)
{
sprintf(tmps,"CUSTOM_%s",uptr->custom_category_name);
// fprintf(stderr,"abcdef cust=%s, ui=%u of %u\n",tmps,ui,num_used_paths); fflush(NULL);
}
else
{
category_code_to_string( uptr->category , tmps);
}
// fprintf(stderr,"abcdef 3.2.2zzz, num_used_paths=%d, ui=%u, before SET_STRING_ELT\n",num_used_paths,ui); fflush(NULL);
SET_STRING_ELT(category, ui, mkChar(tmps)); // 2
// fprintf(stderr,"abcdef 3.2.2xxx, num_used_paths=%d, ui=%u\n",num_used_paths,ui); fflush(NULL);
REAL(pval)[ui] = uptr->pval; // 3
fdr_for_output = uptr->fdr;
REAL(fdr)[ui] = fdr_for_output; // 4