-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpepx.c
1639 lines (1540 loc) · 59.2 KB
/
pepx.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
// For now update both at git github.com/calipho-sib/pepx and svn http://trac/svn/NextProt/main/trunk/com.genebio.nextprot.tools.sequence
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
//#include <sys/syscall.h>
#include <errno.h>
#include <getopt.h>
#define FALSE 0
#define TRUE 1
#define MAXSEQ 32768
// MAXSEQ max number of results for a query
#define MAXHEADSIZE 1500000
// MAXHEADSIZE size of titin longest isoform header line in PEFF
#define MAXSEQSIZE 36500
// MAXSEQSIZE = size of titin longest isoform
#define MAXISO 100
#define MAXVARINPEP 127
#define MAXVARPERAA 8
#define LINELEN 1024
#define ACLEN 16
#define MAXQUERYLENGTH 2000000
//#define BINSIZE 32 // adjust w stats
#define BINSIZE 16 // adjust w stats, this is becoming a problem, have to reduce to 12 when indexing with variants !
// optimal binsizes: 2->1000, 3->100, 4->25, 5->10, 6->?
#define MINPEPSIZE 3
#define MAXPEPSIZE 7
// Allowing 7 implies adapting BINSIZE t0 low value in 7-mers (memory pbs)
#define SILENT 1
#define INDEX2WIDTH 12
#ifndef max
#define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
#endif
// Globals
typedef struct idxdata
{
FILE *fh;
FILE *ffh;
int elemcnt;
} IDXDATA;
IDXDATA idxinfo[MAXPEPSIZE], idxinfoIL[MAXPEPSIZE];
FILE *currentindex;
char currentresult[32];
static int currentpepsize; // weird but alters idxinfo[6] when changed if not static
typedef struct bin
{
int ac_cnt;
struct bin *nextbinptr;
char ac[BINSIZE][ACLEN];
} BIN;
BIN **toplevel[MAXPEPSIZE + 1];
int varindex_overflow_cnt = 0;
char variants[MAXSEQSIZE][MAXVARPERAA];
char results[MAXSEQ][ACLEN];
int aacode[26] = {0, 2, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10, 11, -1, 12, 13, 14, 15, 16, 1, 17, 18, -1, 19, 3};
// B is D, Z is E, U is C, J is I
char aarevcode[] = "ACDEFGHIKLMNPQRSTVWY";
char aarevcodeIL[] = "ACDEFGHJKLMNPQRSTVWY";
char matchmode[10] = "ACISO";
int pow20[] = {1, 20, 400, 8000, 160000, 3200000, 64000000, 1280000000};
int binsizes[] = {0, 0, 1000, 100, 25, 10, 6, 4};
char masterseq[MAXSEQSIZE];
char currAC[ACLEN];
char currISO[ACLEN];
char outputmode[16];
char linesep[4] = "\n";
char envstring[MAXQUERYLENGTH];
char version[] = "1.73";
// debug/profiling/stats stuff
int debug;
int totalbins = 0;
clock_t start;
double duration;
int umatchcnt;
int maxoccur = 0;
int usscnt = 0;
int totalvarcnt = 0;
int varmisscnt1 = 0, varmisscnt2 = 0, varmisscnt3 = 0;
int ignore_variants = 0;
int IL_merge = 0;
int sevenmers_only = 0;
int json = 0;
int FASTAinput = 0;
int mINPEPSIZE = 3;
int mAXPEPSIZE = MAXPEPSIZE - 1; // default to 6-mers
static char jsonBuffer[8192];// weird but alters idxinfo[6] when changed if not static
char varfolder[LINELEN];
char indexfolder[LINELEN] = ".";
// ---------------- code4tenAA ---------------------
char *code4tenAA(char *currentAC)
{
int i, found = 0;
static char codedAC[16] = "";
static char *tab[] = {"A A0A024", "B A0A044", "C A0A067", "D A0A075", "E A0A087", "F A0A088", "G A0A096", "H A0A0A0", "I A0A0A6", "J A0A0B4", "K A0A0C3",
"L A0A0C4", "M A0A0C5", "N A0A0D2", "O A0A0D5", "P A0A0D9", "Q A0A0E3", "R A0A0G2", "S A0A0H2", "T A0A0J9", "U A0A0K0", "V A0A0M3", "W A0A0N4",
"X A0A0R4", "Y A0A0S2", "Z A0A0U1", "a A0A0U5", "b A0A0X1", "c A0A140", "d A0A182", "e A0A1B0", "f A0A1C7", "g A0A1D5", "h A0A1W2", "i A0A286",
"j A0A2R8", "k A0A0K2", "l A0A2Z4", "m A0A494", "n A0A5F9", "o A0A3B3", "p A0A455", "q A0A590", "r A0A6I8", "s A0A804",
"x A0A1L1", "y A0A338", "z A0A5K7"
}; // x,y,z are for mouse SP
if(!strncmp(currentAC, "P", 1))
// convert back to 10 digit AC
{
strncpy(codedAC, currentAC + 1, 1);
codedAC[1] = 0;
for(i = 0; i < sizeof(tab) / 8; i++)
if(!strncmp(tab[i], codedAC, 1))
{
found = 1;
break;
}
if(found)
{
strcpy(codedAC, tab[i] + 2);
strcat(codedAC, currentAC + 2);
//fprintf(stderr,"output: %s\n",codedAC);
return(codedAC);
}
else
strcpy(codedAC, "XXXXXX");
}
else
// convert to 6 digit AC
{
strncpy(codedAC, currentAC, 6);
codedAC[6] = 0;
for(i = 0; i < sizeof(tab) / 8; i++)
if(strstr(tab[i], codedAC))
{
found = 1;
break;
}
if(found)
{
strcpy(codedAC, "P");
strncat(codedAC, tab[i], 1);
strcat(codedAC, currentAC + 6);
}
else
strcpy(codedAC, "XXXXXX");
}
//fprintf(stderr,"output: %s\n",codedAC);
return(codedAC);
}
// ---------------- code2sseq ---------------------
void code2sseq(int sseqcode, int pepsize, char *subseq)
{
char sseq[16] = "";
int i, j;
for(i = pepsize - 1; i >= 0; i--)
{
j = sseqcode / pow20[i];
sseqcode %= pow20[i];
if(IL_merge)
strncat(sseq, &aarevcodeIL[j], 1);
else
strncat(sseq, &aarevcode[j], 1);
}
strcpy(subseq, sseq);
}
// ---------------- sscode ---------------------
int sscode(char *subseq)
{
int sseqcode = 0, len, i;
len = strlen(subseq);
for(i = len; i > 0; i--)
sseqcode += pow20[len - i] * (aacode[subseq[i - 1] - 'A']);
return(sseqcode);
}
// ---------------- pepx_json_header---------------------
void pepx_json_header(char *title, char *querystring, int modeIL)
// recapitulates given parameters
{
sprintf(jsonBuffer, "\"%s\":{\n", title);
fputs(jsonBuffer, stdout); // outputs header title
if(querystring != NULL)
{
sprintf(jsonBuffer, "\"peplist\":\"%s\",\n\"modeIL\": %d\n},", querystring, modeIL);
fprintf(stdout, "%s", jsonBuffer);
//fprintf(stderr,"jsonBuffer: %s\n",jsonBuffer);
}
}
// ---------------- pepx_tojson ---------------------
char *pepx_tojson(char *finalresult)
{
int varpos = 0;
char *dashptr;
//fprintf(stdout,"pepx_tojson receives %s\n",finalresult);
dashptr = strrchr(finalresult, '-');
if((dashptr != finalresult + 6) && (dashptr != finalresult + 10)) // don't forget 10 digirs
// we have a variant pos in the last digits (eg: NX_Q8IWA4-2-121)
{
varpos = atoi(dashptr + 1);
*dashptr = 0; // remove parsed pos from string
}
sprintf(jsonBuffer, "{\n\"isoformName\":\"%s\",\n\"position\":%d\n}", finalresult, varpos);
return(jsonBuffer);
}
// ---------------- pepx_create_variant_files ---------------------
int pepx_create_variant_files(char *varfolder)
{
FILE *varmaster, *out;
char *token, buf[LINELEN], entryvarbuf[1000000] = "", org[64], dest[128], fname[128], lastac[16] = "", ac[16], nxid[16], annotId[32];
int startpos, endpos, i = 0;
fprintf(stderr, "Building for first time with variants, please wait...\n");
sprintf(fname, "pepx-variantdata.csv");
if((varmaster = fopen(fname, "r")) == NULL)
{
perror(fname);
return(0);
}
while(fgets(buf, LINELEN, varmaster))
// Sample line: "NX_P16333-1,AN_P16333_000975,183,183,V,I", actually commas are replaced by spaces
{
if(strncmp(buf, "NX_", 3))
continue;
*dest = 0; //To avoid that last value is keeped when scaning a miss variant (dest empty)
sscanf(buf, "%s %s %d %d %s %s\n", nxid, annotId, &startpos, &endpos, org, dest);
//fprintf(stderr,"read %s\n",buf);
//fprintf(stderr,"nxid: %s\n",nxid);
strcpy(ac, nxid + 3);
if(strlen(lastac) && strcmp(ac, lastac))
// write previous iso record
{
sprintf(fname, "%s/%s.csv", varfolder, lastac);
//fprintf(stderr,"writing %s\n",fname);
out = fopen(fname, "w");
fprintf(out, "%s", entryvarbuf);
fclose(out);
if((++i % 2500) == 0)
fprintf(stderr, "%d isoform variant files created...\n", i);
sprintf(buf, "%d %d %s %s\n", startpos, endpos, org, dest);
strcpy(entryvarbuf, buf);
strcpy(lastac, ac);
}
else
{
sprintf(buf, "%d %d %s %s\n", startpos, endpos, org, dest);
strcat(entryvarbuf, buf);
strcpy(lastac, ac);
}
}
// write last record
sprintf(fname, "%s/%s.csv", varfolder, ac);
out = fopen(fname, "w");
fprintf(out, "%s", entryvarbuf);
fclose(out);
sprintf(fname, "%s/VARIANTS_OK", varfolder); // flag file
out = fopen(fname, "w");
fclose(out);
return(1);
}
// ---------------- pepx_build_varindex ---------------------
int pepx_build_varindex(char *isoname)
{
char buf[LINELEN], orgAA[16], lastorgAA[16], varAA[16], fname[64], *ptr;
char iso[16];
int i, skipvar = 0, cmdres, fpos, lastfpos, lpos, localvarcnt = 0;
FILE *NextProtvariants;
if(debug)
fprintf(stderr, "Building variants for %s\n", currISO);
if(is10digits(isoname))
// get around 10-len accs
{
strcpy(iso, code4tenAA(isoname));
}
else strcpy(iso, isoname);
// Get variants from NextProt files
sprintf(fname, "%s/%s.csv", varfolder, iso);
if(debug) fprintf(stderr, "opening file: %s\n", fname);
if((NextProtvariants = fopen(fname, "r")) == NULL)
// THe variant file doesn't exist for this iso : no variants
return(0);
// Reset table each time
memset(variants, 0, MAXSEQSIZE * MAXVARPERAA);
// Todo: check that orgAA is exactly the same (seq/vars desynchronization)
while(fgets(buf, LINELEN, NextProtvariants))
{
*varAA = 0; //To avoid that last value is keeped when scaning a miss variant (varAA empty)
if(debug) fprintf(stderr, "variants line: %s\n", buf);
sscanf(buf, "%d %d %s %s\n", &fpos, &lpos, orgAA, varAA);
//if(strstr(iso,"P04637") && strstr(buf,"description") && strstr(buf,"sporadic"))
if(fpos == 1 || strchr(varAA, '*') || (strlen(orgAA) > 2) || (strlen(varAA) > 2))
// No variants on Met-1 allowed, No stop variants allowed, No long variants
{
if((strlen(varAA) == 1 && orgAA[strlen(varAA) - 1] == varAA[0]) || strlen(varAA) == 0)
varmisscnt3++;
skipvar = 1;
}
if(skipvar)
{
skipvar = 0;
continue;
}
if((fpos != lpos)) // not a simple snp
{
if((strlen(orgAA) == 2) && (strlen(varAA) == 2) && orgAA[0] == varAA[0])// like A7XYQ1(180-181)
// Store variant at 2nd pos
{
if(IL_merge && (varAA[1] == 'I' || varAA[1] == 'L') )
varAA[1] = 'J';
variants[fpos][0] = varAA[1];
localvarcnt++;
}
else if((strlen(orgAA) == 2) && (strlen(varAA) == 1) && varAA[0] == orgAA[1]) // like A6NFR6-3
// Store variant
{
if((fpos == lastfpos + 1) && (orgAA[0] == lastorgAA[0])) // To avoid damage like in O00555-1 2219-2220 and 2220-2221
{
lastfpos = fpos;
continue;
}
variants[fpos - 1][0] = 'X'; // Missing
varmisscnt2++;
strcpy(lastorgAA, orgAA);
lastfpos = fpos;
localvarcnt++;
}
}
else
{
if(IL_merge && ((orgAA[0] == 'I' && varAA[0] == 'L') || (orgAA[0] == 'L' && varAA[0] == 'I'))) // In IL mode variants I->L or L->I would just add noise and are ignored
continue;
// Store variant
if(strlen(varAA) == 0)
// code for 'Missing' AA
{
strcpy(varAA, "X");
varmisscnt1++;
}
for(i = 0; i < MAXVARPERAA; i++)
// we allow 8 different variants at each position: Check if we already have a variant at this pos
if(variants[fpos - 1][i] == 0)
break;
//if(i >= MAXVARPERAA) fprintf(stderr,"%s: multiple variants at %d-%d\n",currISO,fpos,lpos);
if(IL_merge && (varAA[0] == 'I' || varAA[0] == 'L') )
varAA[0] = 'J';
variants[fpos - 1][i] = varAA[0];
localvarcnt++;
}
}
fclose(NextProtvariants);
if(debug)
fprintf(stderr, "Variants done.\n");
return(localvarcnt);
}
// ---------------- pepx_loadall ---------------------
void pepx_loadall()
{
int i, pepcnt, filelen;
char fname[LINELEN], *ptr, idxpath[128] = "";
FILE *idx;
if(strlen(indexfolder) > 8)
{
strcpy(idxpath, indexfolder);
strcat(idxpath, "/");
}
else if((ptr = getenv("PEPX")) != NULL)
{
strcpy(idxpath, ptr);
strcat(idxpath, "/");
}
// Check if we've indexed small-mers
sprintf(fname, "%spepxIL7.idx", idxpath);
if((idx = fopen(fname, "r")) == NULL)
{
sprintf(fname, "%spepx7.idx", idxpath);
if((idx = fopen(fname, "r")) != NULL)
// Indexed only with 7-mers
mINPEPSIZE = mAXPEPSIZE = MAXPEPSIZE;
}
else
mINPEPSIZE = mAXPEPSIZE = MAXPEPSIZE;
for(i = mINPEPSIZE; i <= mAXPEPSIZE; i++)
{
usscnt = 0;
if(IL_merge)
sprintf(fname, "%spepxIL%d.idx", idxpath, i);
else
sprintf(fname, "%spepx%d.idx", idxpath, i);
if((idx = fopen(fname, "r")) == NULL)
{
perror(fname);
exit(2);
}
// save flat file handle
if(IL_merge)
idxinfoIL[i].ffh = idx;
else
idxinfo[i].ffh = idx;
strcat(fname, "2"); // secundary index name
if((idx = fopen(fname, "r")) == NULL)
{
perror(fname);
exit(2);
}
fseek(idx, 0, SEEK_END);
filelen = ftell(idx);
pepcnt = filelen / (i + INDEX2WIDTH);
//printf("%d-mers size: %d bytes -> %d elts\n",i,filelen,pepcnt);
if(IL_merge)
{
idxinfoIL[i].fh = idx;
idxinfoIL[i].elemcnt = pepcnt;
}
else
{
idxinfo[i].fh = idx;
idxinfo[i].elemcnt = pepcnt;
}
}
}
// ---------------- pepx_initindexes ---------------------
void pepx_initindexes()
{
int pepsize;
BIN **ptr;
for(pepsize = mINPEPSIZE; pepsize <= mAXPEPSIZE; pepsize++)
{
if((ptr = calloc(1, pow20[pepsize] * sizeof(BIN *))) == NULL)
{
fprintf(stderr, "Allocation failed\n");
exit(11);
}
//printf("%d empty ints reserved at address %p\n",pow20[pepsize],ptr);
toplevel[pepsize] = ptr;
}
}
// ---------------- pepx_save ---------------------
void pepx_save(char *fname, int pepsize)
{
int sseqcode, i, maxcode, ac_cnt, iso_cnt;
char ac[ACLEN], ac_noiso[ACLEN], lastac[ACLEN], subseq[16], maxseq[16] = "";
FILE *out, *out2;
BIN *binptr, **binarray;
int idxpos;
if((out = fopen(fname, "w")) == NULL)
{
perror(fname);
exit(2);
}
// secundary index
strcat(fname, "2");
if((out2 = fopen(fname, "w")) == NULL)
{
perror(fname);
exit(2);
}
// index header line (1rst pep line must not be at pos 0 for new index schema)
for(i = 0; i < pepsize + INDEX2WIDTH - 1; i++)
fprintf(out2, "0");
fprintf(out2, "\n");
usscnt = maxoccur = umatchcnt = 0;
fprintf(stderr, "Writing index for %d-mers ...\n", pepsize);
binarray = toplevel[pepsize];
maxcode = pow20[pepsize];
for(sseqcode = 0; sseqcode < maxcode; sseqcode++)
if((binptr = binarray[sseqcode]) != NULL)
// This pep has some matches
{
code2sseq(sseqcode, pepsize, subseq);
idxpos = ftell(out);
if(fprintf(out, "%s\n", subseq) <= 0)
fprintf(stderr, "writing acs for %s failed\n", subseq);
// write secundary index
fprintf(out2, "%s %10u\n", subseq, idxpos);
ac_cnt = iso_cnt = 0;
usscnt++;
while(binptr->nextbinptr != NULL)
{
// add the next BINSIZE ac
for(i = 0; i < BINSIZE; i++)
{
strcpy(ac, binptr->ac[i]);
fprintf(out, "%s\n", ac);
//fprintf(stderr,"ac: %s\n",ac);
iso_cnt++;
strcpy(ac_noiso, ac);
ac_noiso[6] = 0;
if(ac_noiso != lastac)
ac_cnt++;
strcpy(lastac, ac_noiso);
}
// set pointer to next bin
binptr = binptr->nextbinptr;
}
// dump last bin
for(i = 0; i < binptr->ac_cnt; i++)
{
strcpy(ac, binptr->ac[i]);
fprintf(out, "%s\n", ac);
iso_cnt++;
strcpy(ac_noiso, ac);
ac_noiso[6] = 0;
if(ac_noiso != lastac)
ac_cnt++;
strcpy(lastac, ac_noiso);
}
if(ac_cnt == 1)
umatchcnt++;
else if(ac_cnt > maxoccur)
{
maxoccur = ac_cnt;
strcpy(maxseq, subseq);
}
}
fprintf(stderr, "%d distincts %d-mer, %d are uniques (found in just 1 entry), most frequent: %s (%d)\n", usscnt, pepsize, umatchcnt, maxseq, maxoccur);
fclose(out);
fclose(out2);
}
// ---------------- pepx_saveall ---------------------
void pepx_saveall()
{
int i;
char fname[LINELEN];
for(i = mINPEPSIZE; i <= mAXPEPSIZE; i++)
{
if(!IL_merge)
sprintf(fname, "%s/pepx%d.idx", indexfolder, i);
else
sprintf(fname, "%s/pepxIL%d.idx", indexfolder, i);
pepx_save(fname, i);
}
}
/******* rescompare **************************************************/
int rescompare (char *str1, char *str2)
{
return (strncmp(str1, str2, ACLEN));
}
/******* pepcompare **************************************************/
int pepcompare (char *str1, char *str2)
{
fseek(currentindex, (long)str2, 0);
fgets(currentresult, 32, currentindex);
//fprintf(stderr,"current idx2 file pos: %li, fseek returned %d\n",(int)str2,fret);
//fprintf(stderr,"res: %s\n",currentresult);
return (strncmp(str1, currentresult, currentpepsize));
}
// ---------------- pepx_reportnomatch ---------------------
int pepx_reportnomatch(char *orgquerystring)
{
//fprintf(stdout,"NO_MATCH %s\n", outputmode);
if(json)
fprintf(stdout, "]\n}"); // close empty entrymatches
else
fprintf(stdout, "\nNO_MATCH %s%s", orgquerystring, linesep);
return(0);
}
// ---------------- pepx_merge_with_prev_res ---------------------
int pepx_merge_with_prev_res(char endres[MAXSEQ][ACLEN + 4], char curres[MAXSEQ][ACLEN], char *acstr, int rescnt)
{
char isoonly[ACLEN], isovar[ACLEN] = "", prevmatch[ACLEN] = "", *dashptr, *matchptr;
int i, j, prevmatchvar, dupcnt = 0;
//fprintf(stderr,"acstring: %s\n",acstr);
j = rescnt;
if(strlen(acstr) == 0)
// first pep of a query
{
//fprintf(stderr,"\nfirst 6-mer: %d hits\n",j);
for(i = 0; i < rescnt; i++)
{
if(i && !strcmp(curres[i], curres[i - 1]))
{
dupcnt++;
continue; // fprintf(stderr,"\ndup: %s\n",curres[i]);, TODO: investigate why it occurs, eg: ASQSIS
}
//fprintf(stderr,"%d: %s (dupcnt: %d)\n",i,curres[i],dupcnt);
strcpy(endres[i], curres[i]);
strcat(acstr, curres[i]);
strcat(acstr, ",");
}
j = rescnt - dupcnt;
}
else // Check the match did exist for previous peps
{
for(i = j = 0; i < rescnt; i++)
{
strcpy(isoonly, curres[i]);
if((dashptr = strrchr(isoonly, '-')) != isoonly + 6)
// Remove variant pos before searching prev results
{
//fprintf(stderr,"removing varpos for %s\n",isoonly);
strcpy(isovar, isoonly);
//fprintf(stderr,"isovar now: %s\n",isovar);
*dashptr = 0;
}
//fprintf(stderr,"checking %s (result %d/%d)\n",curres[i],i+1,rescnt);
if(matchptr = strstr(acstr, curres[i]))
for(; matchptr; matchptr = strstr(matchptr + 1, curres[i]))
{
strncpy(prevmatch, matchptr, ACLEN);
*strchr(prevmatch, ',') = 0;
if((dashptr = strrchr(prevmatch, '-')) != prevmatch + 6)
prevmatchvar = TRUE;
else
prevmatchvar = FALSE;
//fprintf(stderr,"prevmatchvar: %d\n",prevmatchvar);
if(!strcmp(prevmatch, curres[i]) || (prevmatchvar && strstr(prevmatch, curres[i])))
// If a new match without variant falls on a previous match with variant: keep variant
{
//fprintf(stderr,"keeping prevmatch: %s\n",prevmatch);
strcpy(endres[j++], prevmatch);
}
//else fprintf(stderr,"discarding prevmatch: %s\n",prevmatch);
}
else if (isovar && (matchptr = strstr(acstr, isoonly)))
// new match is a variant
{
strncpy(prevmatch, matchptr, ACLEN);
*strchr(prevmatch, ',') = 0;
//fprintf(stderr,"isovar: %s, prevmatch: %s\n",isovar,prevmatch);
if(strrchr(prevmatch, '-') == prevmatch + 6)
// replace simple match by new variant match
strcpy(endres[j++], isovar);
else if(strstr(prevmatch, isoonly))
// reconduct previous variant pos
strcpy(endres[j++], prevmatch);
//else fprintf(stderr,"%s not added to endres\n\n",isovar);
}
*isovar = 0;
}
if(j)
// regenerate actring
{
//fprintf(stderr,"j equals %d, regenerating ACstring\n",j);
*acstr = 0;
for(i = 0; i < j; i++)
{
strcat(acstr, endres[i]);
strcat(acstr, ",");
}
//fprintf(stderr,"regenerated ACstring: %s\n\n",acstr);
}
}
return(j);
}
// ---------------- pepx_search ---------------------
int pepx_search(char *query, IDXDATA *idx)
{
typedef char actab[ACLEN];
int i, j, jpos = -1, jcnt = 0, found, rescnt = 0, pepsize, fpos;
char querystring[MAXPEPSIZE], ac[ACLEN], newquery[MAXPEPSIZE], fname[LINELEN], buf[MAXPEPSIZE + 1], *ptr;
char acholder[ACLEN + 1], jokerAA;
actab *currentresults;
FILE *ffh;
strcpy(querystring, query);
pepsize = strlen(query);
if(pepsize < mINPEPSIZE) // eg: six AA peptide with 7-mer indexes
{
strcat(querystring,"X"); // add a trailing joker to match index size
pepsize++;
}
currentpepsize = pepsize;
currentindex = idx[pepsize].fh;
currentresults = results;
for(i = 0; i < pepsize; i++)
if(querystring[i] == 'X')
{
jpos = i;
jcnt++;
}
else if(IL_merge && (querystring[i] == 'I' || querystring[i] == 'L'))
// We are querying against the IL_merged index
querystring[i] = 'J';
if(jcnt > 1)
// too many jokers
{
fprintf(stderr, "\n%s: No more than 1 joker/6AA\n", query);
return(0);
}
else if(jcnt == 1) // we have one joker
{
for(i = 0; i < strlen(aarevcode); i++)
// For each of the 20 AAs, or 19 if in ILmode
{
memset(newquery, 0, mAXPEPSIZE);
strncpy(newquery, querystring, jpos);
jokerAA = aarevcode[i];
if(IL_merge && jokerAA == 'I') // skip this one
continue;
if(IL_merge && jokerAA == 'L')
jokerAA = 'J';
strncat(newquery, &jokerAA, 1); // append single char joker
strcat(newquery, querystring + jpos + 1);
//fprintf(stderr,"\nnewquery: %s\n",newquery);
if(bsearch(newquery, NULL, idx[pepsize].elemcnt, pepsize + INDEX2WIDTH, pepcompare))
{
fpos = atoi(currentresult + pepsize);
ffh = idx[pepsize].ffh;
fseek(ffh, fpos, SEEK_SET);
// skip peptide
fgets(ac, ACLEN, ffh); // read matched peptide
while(fgets(ac, ACLEN, ffh))
// get all ACs
{
if(strlen(ac) > MAXPEPSIZE + 1) // We're still an accession
{
if(ptr = strrchr(ac, '\n'))
* ptr = 0;
else // max len identifiere eg:Q8WZ42-11-11658
ac[ACLEN - 1] = 0;
// make results uniques
for(found = FALSE, j = 0; j < rescnt; j++)
if(!strcmp(ac, currentresults[j]))
{
found = TRUE;
break;
}
if(!found) // TODO: Investigate why it doesn't work 100%, there are still duplicate variants in results when using a joker (eg: HTXEKPY)
strcpy(currentresults[rescnt++], ac);
}
else
break; // We've reached the next peptide
}
}
}
return(rescnt);
}
// No jokers
if(!bsearch(querystring, NULL, idx[pepsize].elemcnt, pepsize + INDEX2WIDTH, pepcompare))
// No match
{
//fprintf(stderr,"\n%s: not found in %s\n",querystring,idx==idxinfoIL?"ILindex":"BaseIndex");
return(0);
}
//fprintf(stderr,"\n%s: found in %s\n",querystring,idx==idxinfoIL?"ILindex":"BaseIndex");
fpos = atoi(currentresult + pepsize);
ffh = idx[pepsize].ffh;
fseek(ffh, fpos, SEEK_SET);
// skip peptide
fgets(ac, ACLEN, ffh);
//fprintf(stderr,"\nReading match ACs for %s\n",ac);
while(fgets(acholder, ACLEN + 1, ffh))
// get all ACs
{
strncpy(ac, acholder, ACLEN);
if(strlen(ac) > MAXPEPSIZE + 1)
{
if(ptr = strrchr(ac, '\n'))
* ptr = 0;
else // max len identifiere eg:Q8WZ42-11-11658
ac[ACLEN - 1] = 0;
//fprintf(stderr,"\nrescnt %d %s: %s(%d)\n",rescnt, query,ac,strlen(ac));
strcpy(currentresults[rescnt++], ac);
}
else
{
//fprintf(stderr,"\nBreaking at %s\n",ac);
break;
}
}
//fprintf(stderr,"\nReturning %d matches\n",rescnt);
return(rescnt);
}
int is10digits(char *match)
{
if(match[0] == 'P' && isalpha(match[1]))
/*if(!strncmp(match,"PA",2) || !strncmp(match,"PB",2) || !strncmp(match,"PC",2) ||
!strncmp(match,"PD",2) || !strncmp(match,"PE",2) || !strncmp(match,"PF",2) ||
!strncmp(match,"PG",2) || !strncmp(match,"PH",2) || !strncmp(match,"PI",2)) */
return(1);
return(0);
}
// ---------------- pepx_displaymatches ---------------------
void pepx_displaymatches(char matches[][ACLEN + 4] , int nbmatch, char *orgquerystring, char *querystring)
// orgquerystring the original modified or commented peptide (GCS*PLKKT*V), querystring the peptide stripped on non AA characters
{
int ac_cnt, i;
char ac[ACLEN], lastac[ACLEN] = "";
// Sort results by AC
if(nbmatch > 2)
qsort(matches, nbmatch, ACLEN + 4, rescompare);
if(!strcmp(matchmode, "ACONLY"))
// Re-filter without iso ids (it is already sorted)
{
ac_cnt = 0;
for(i = 0; i < nbmatch; i++)
{
if(!strchr(matches[i], '-'))
// it can happen at pos 0, eg: VIDLT, to investigate
{
//fprintf(stderr,"No dash at %d %s\n",i,ac);
continue;
}
strcpy(ac, matches[i]);
*strchr(ac, '-') = 0;
if(strcmp(ac, lastac))
strcpy(results[ac_cnt++], ac);
strcpy(lastac, ac);
}
// re-fill final results
nbmatch = ac_cnt;
for(i = 0; i < nbmatch; i++)
strcpy(matches[i], results[i]);
}
if(!strcmp(outputmode, "BATCH"))
// input from file (results are comma-separated)
{
//printf(stdout,"\n");
if(nbmatch == 0)
fprintf(stdout, "NO_MATCH");
else for(i = 0; i < nbmatch; i++)
{
// Check for coded 10-digit ACs
fprintf(stdout, "%s", is10digits(matches[i]) ? code4tenAA(matches[i]) : matches[i]);
if(i != nbmatch - 1)
fprintf(stdout, ","); // match separator
}
// Reminder of the query
fprintf(stdout, " %s\n", orgquerystring);
}
else
{
// input from stdin or envstring in web cgi mode (results are crlf-separated)
//fprintf(stdout,"\n%s: %d match(s)%s",querystring,cnt,linesep);
if(!json)
fprintf(stdout, "%s%s: %d match(s)%s", linesep, querystring, nbmatch, linesep);
for(i = 0; i < nbmatch; i++)
{
if(json)
{
strcpy(ac, matches[i]);
if(!strchr(matches[i], '-')) // it can happen that after qsort, element 0 is empty: investigate with AAAAAA
continue;
*strchr(ac, '-') = 0;
if(strcmp(ac, lastac))
{
if(i)
// close previous iso and entrymatch tag
fprintf(stdout, "]\n},");
if(is10digits(ac)) // Check for coded 10-digit ACs
fprintf(stdout, "{\n\"entryName\":\"%s\",\n\"isoforms\":[", code4tenAA(ac));
else
fprintf(stdout, "{\n\"entryName\":\"%s\",\n\"isoforms\":[", ac);
strcpy(lastac, ac);
}
else fprintf(stdout, ",");
if(is10digits(ac))
fprintf(stdout, "%s", pepx_tojson(code4tenAA(matches[i])));
else
fprintf(stdout, "%s", pepx_tojson(matches[i]));
}
else fprintf(stdout, "%s%s", is10digits(matches[i]) ? code4tenAA(matches[i]) : matches[i], linesep);
}
if(json)
fprintf(stdout, "]\n}\n]\n}\n"); // finished, close iso and entrymatch tags tags
else
fprintf(stdout, "%s: %d match(s)%s", querystring, nbmatch, linesep);
}
}
// ---------------- pepx_filterquery ---------------------
int pepx_filterquery(char *query, char *querystring)
{
char *qptr;
int i;
for(i = 0, qptr = querystring; i < strlen(query); i++)
{
// uppercase
query[i] = toupper(query[i]);
if(!strchr(aarevcode, query[i]))
{
if(isspace(query[i]) || isdigit(query[i])
|| (query[i] == '*')
|| (query[i] == '#')
|| (query[i] == '.')
|| (query[i] == '(')
|| (query[i] == ')'))
// just don't copy unwanted, and allow to keep eventually tagged seq for result display
i = i;
else if((query[i] == 'X') || (query[i] == 'J' && IL_merge))
// This is OK
*qptr++ = query[i];
else
{
// Todo: Maybe consider U as C
fprintf(stderr, "\n%c is not an AA\n", query[i]);
return(0);
}
}
else
*qptr++ = query[i];
}
*qptr = 0;
return(1);
}
// ---------------- pepx_processquery ---------------------
int pepx_processquery(char *orgquerystring)
{
char query[8192], querystring[8192], nextquery[8192], subquery[MAXPEPSIZE + 1] = "", acstring[400000] = "", finalres[MAXSEQ][ACLEN + 4];
char *qptr;
int row = 0, i, j, cnt;
// TODO: rewrite and iterate one by one AA, or 1rst and last and one by one ?
strcpy(query, orgquerystring);
if(json) fprintf(stdout, "{\n\"peptide\":\"%s\",\n\"entryMatches\":[", query);
if(!strcmp(outputmode, "BATCH"))
// Only first token is the peptide
{
for(i = 0; i < strlen(query); i++)
if(isspace(query[i]))
{
query[i] = 0;
break;
}
}
// quick filter
if(!pepx_filterquery(query, querystring))
// invalid query
return(0);
strcpy(query, querystring);
//if(strlen(query) >= 4096) {strncpy(subquery,query,12); subquery[12] = 0; fprintf(stderr,"\nQuery length: %d %s...\n",strlen(query),subquery);}
while(strlen(query) > mAXPEPSIZE)
{
// Split query in overlaping subqueries of maximum length
//fprintf(stderr,"pepx_processquery filtered query: %s, copying %d AAs in subquery\n",query,MAXPEPSIZE);
strncpy(subquery, query, mAXPEPSIZE);
subquery[mAXPEPSIZE] = 0;
//fprintf(stderr,"pepx_processquery subquery %s\n",subquery);
//strcpy(nextquery, query + mAXPEPSIZE - 1); // Faster but more false pos
// Prepare next subquery
if(strlen(querystring) > 25)
strcpy(nextquery,query + MAXPEPSIZE-1); // Faster but more false pos
else if(strlen(querystring) > 16)
strcpy(nextquery,query + 2);
else
strcpy(nextquery,query + 1);
strcpy(query, nextquery);
//fprintf(stderr,"will look for %s\n",query);
if(IL_merge)
cnt = pepx_search(subquery, idxinfoIL);
else
cnt = pepx_search(subquery, idxinfo);
if(cnt == 0)
{
//fprintf(stderr,"pepx_processquery: no match for subquery %s\n",subquery);
return(pepx_reportnomatch(orgquerystring));
}
if((cnt = pepx_merge_with_prev_res(finalres, results, acstring, cnt) == 0))
return(pepx_reportnomatch(orgquerystring));
}