-
Notifications
You must be signed in to change notification settings - Fork 0
/
condensed.c
1226 lines (1124 loc) · 37.7 KB
/
condensed.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
/*Used with main.c,cluster.c in cluster graph
Finds freq helices in 1000 structs; finds their LCA
Creates Hasse diagram with these vertices
Condensed version of graph.c
*/
#include "hashtbl.h"
#include "cluster.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
static HASHTBL *edges;
static HASHTBL **graph;
static HASHTBL *input;
static char **table;
static char **profileID;
static char **modprofileID;
static long *sums;
static int most;
static int graphsize;
//runs through nodes in cluster, finding their LCA's
int insert_graph(FILE *fp,char *file,int gsize) {
int i,*freq,k = 0,numkeys,total;
char *profile;
KEY *node;
struct hashnode_s *begin;
if (!(edges = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() failed");
exit(EXIT_FAILURE);
}
fputs("digraph G {\n",fp);
fprintf(fp,"\tlabel = \"%s\";\n",file);
fprintf(fp,"\tpad = 0.5;\n");
fprintf(fp,"\tnodesep = 0.5;\n");
fprintf(fp,"\"\" [label=\"(0/%d)\"]\n",NUMSTRUCTS);
//fputs("{ node [shape = plaintext]; ",fp);
graphsize = gsize;
if (INPUT) {
i = process_one_input(fp);
if (i > gsize)
graphsize = i;
}
//printf("graphsize is %d for %s\n",*count,node->data);
graph = malloc(sizeof(HASHTBL*)*(graphsize+1));
for (i = 0; i <= graphsize; i++) graph[i] = NULL;
/*print ranks
for (i = 0; i < *count; i++) {
fprintf(fp,"%d",i+1);
if (i == *count-1)
fprintf(fp,"; }\n");
else
fprintf(fp,"->");
}
*/
numkeys = hashtbl_numkeys(cluster);
sums = malloc(sizeof(long)*numkeys);
k = numkeys-1;
profileID = malloc(sizeof(char*)*numkeys);
modprofileID = malloc(sizeof(char*)*numkeys);
most = 0;
//for each profile, insert with freq
for (node = hashtbl_getkeys(cluster); node; node = node->next) {
//printf("node data is %s with k = %d\n",node->data,k);
freq = hashtbl_get(cluster,node->data);
//need to insert into graph
if (freq) {
if (most < *freq)
most = *freq;
profile = malloc(strlen(node->data)+1);
sums[k] = insert_and_binary(node->data,profile,*freq);
profileID[k] = node->data;
modprofileID[k--] = profile;
if (*freq > 0)
fprintf(fp,"\"%s\" [shape = box];\n",profile);
}
else
fprintf(stderr,"No entry for %s\n",node->data);
//printf("for profile %s binary rep is %d with ID %d\n",node->data,sums[k+1],k+1);
}
make_key();
begin = insert_LCAs(fp,numkeys);
//printf("inserted LCA's with numkeys %d\n",numkeys);
add_infreq(begin);
find_LCA_edges(fp,begin,numkeys);
total = print_vertices(fp);
printf("Total number of vertices: %d\n",total);
if (INPUT)
hashtbl_destroy(input);
for (i = 0; i < graphsize; i++) {
if (graph[i])
hashtbl_destroy(graph[i]);
}
free(sums);
free(table);
free(profileID);
free(modprofileID);
hashtbl_destroy(edges);
return 0;
}
//inserts key into graph and converts from rep struct to binary notation
long insert_and_binary(char *key,char *profile,int freq) {
char *blank = " ",*helix,*copy = strdup(key);
unsigned long sum = 0;
int i,length = atoi(strtok(copy,blank)),*val;
HASHTBL *hash = graph[length-1];
profile[0] = '\0';
for (i = 0; i < length; i++) {
helix = strtok(NULL,blank);
strcat(profile,helix);
strcat(profile," ");
sum += *((long*) hashtbl_get(binary,helix));
//printf("sum is now %d\n",sum);
}
//printf("profile is %s\n",profile);
if (!hash) {
if (!(hash = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() failed");
exit(EXIT_FAILURE);
}
graph[length-1] = hash;
}
val = malloc(sizeof(int));
*val = freq;
hashtbl_insert(hash,profile,val);
//printf("profile %sinserted with length %d and freq %d\n",profile,length,*val);
return sum;
}
//finds the LCA of powerset of set of profiles and insert if necessary
//graph holds the final graph, table[freq ID] = ID, profileID[profileID] = profile
//sums[profileID] = binary rep, k = number of profiles
//returns beginning of linked list containing all unique LCA's found
struct hashnode_s* insert_LCAs(FILE *fp,int k) {
int count,frq,*val,bit,s=0,j,*found,size = INIT_SIZE;
unsigned long num;
unsigned int i;
char *profile;
struct hashnode_s *node = NULL,*temp = NULL;
KEY *top;
found = malloc(sizeof(int)*k);
//for every subset of minimal elements...
//checking i = 1,2 is redundant (only 1 element in bin rep), so skip
//printf("k is %d\n",k);
for (i = 3; i < (1<<k); i++) {
count = 0;
frq = 0;
j = hashtbl_numkeys(binary);
//printf("number of freq helices %d\n",j);
num = (1<<j)-1;
//printf("original num is %d with j %d\n",num,j);
//...take their LCA...
for (bit = 0, j = i; j > 0 && bit < k ; j >>= 1, bit++)
if ((1 & j) == 1) {
found[count++] = bit;
//printf("found profile %s for bit %d\n",profileID[bit],bit);
//sprintf(key,"%s",table[bit]);
if (!(val = hashtbl_get(cluster,profileID[bit])))
fprintf(stderr, "No such %s in cluster with bit %d\n",profileID[bit],bit);
frq += *val;
num &= sums[bit];
//printf("num is %d after & with %d from bit %d: %s\n",num,sums[bit],bit,profileID[bit]);
}
//printf("binary profile is %d, k is %d,frq is %d, and binary helices is %d\n",i,k,frq,num);
//...and insert
if (num != 0) {
profile = malloc(sizeof(char)*ARRAYSIZE*size);
profile = convert_binary(profile,num,&s);
find_edges(fp,profile,found,count);
temp = insert_LCA(profile,num,s,frq);
if (temp) {
//printf("LCA profile %s and i %d\n",profile,i);
temp->next = node;
node = temp;
} else
free(profile);
}
}
//add final connection from root to smallest element in graph
for (i = 0; !graph[i]; i++) ;
for (top = hashtbl_getkeys(graph[i]); top; top = top->next)
check_insert_edge(fp,"",top->data);
free(found);
return node;
}
//inserts profile into graph, updates freq if already exists
//saves LCA into linked list with num = binary rep
//s = number of helices in profile
struct hashnode_s* insert_LCA(char *profile,int num,int s,int frq) {
int size,*val,*data;
char *key;
HASHTBL *hash = NULL;
struct hashnode_s *temp = NULL;
//KEY *node;
hash = graph[s-1];
if (!hash) {
if (!(hash = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() failed");
exit(EXIT_FAILURE);
}
graph[s-1] = hash;
}
val = hashtbl_get(hash,profile);
if (!val) {
val = malloc(sizeof(int));
*val = frq;
hashtbl_insert(hash,profile,val);
//printf("inserted %s into graph\n",profile);
//if LCA isn't an original, save; will have to compare them later
//below assumes s is double digits or less
for (size = 1; size < strlen(profile)+3; size++) ;
key = malloc(sizeof(char)*ARRAYSIZE*size);
sprintf(key,"%d %s",s,profile);
if (!hashtbl_get(cluster,key)) {
//printf("%s as new vertex with bit %d\n",profile,num);print_vertices(fp
temp = malloc(sizeof(struct hashnode_s));
data = malloc(sizeof(int));
*data = num;
temp->key = profile;
temp->data = data;
//for (;temp;temp = temp->next)
//printf("node is %s with bit %d\n",temp->key,*((int*)temp->data));
}
free(key);
} else if (*val < frq) {
//printf("profile %s inserted with freq %d, old freq %d\n",profile,frq,*val);
*val = frq;
}
return temp;
}
//table[freq id] = helix id
//produces table,where ith element is val
//where (1<<i) = hash_get(binary,val)
void make_key() {
int count;
KEY *node = hashtbl_getkeys(binary);
count = hashtbl_numkeys(binary);
//for (count = 0; (*num & (1<<count)) == 0; count++) ;
table = malloc(sizeof(char*)*count);
for (count--; count >= 0; count--) {
//printf("table[%d] id is %s\n",count,node->data);
table[count] = node->data;
node = node->next;
}
}
//converts binary rep to string of helices (profile)
//returns number of helices in profile
//change count to reflect number of helices in profile
char* convert_binary(char *profile,long binary, int *count) {
int k,size = INIT_SIZE,num=0;
profile[0] = '\0';
for (k = 0; binary > 0; binary >>= 1, k++)
if ((binary & 1) == 1) {
if (strlen(profile)+strlen(table[k]) > ARRAYSIZE*size-2)
profile = resize(&size,strlen(profile)+strlen(table[k])+2,profile);
strcat(profile,table[k]);
strcat(profile," ");
num++;
}
*count = num;
//printf("convert binary's profile is %s for binary %d\n",profile,binary);
return profile;
}
//adds infreq profiles to general and specific frequency when applicable
//begin is the list of LCA's generated
void add_infreq(struct hashnode_s *begin) {
//general freq lives in graph with all vertices
int length,sum,k,*val,bin,selected;
char *blank = " ",*helix,*orig,*fprof,*copy;
struct hashnode_s *listnode;
KEY *node;
HASHTBL *hash;
selected = hashtbl_numkeys(cluster);
//for every infreq node, add to appropriate gen/spec freq
for (node = hashtbl_getkeys(infreq); node; node = node->next) {
//prof = malloc(strlen(node->data));
//printf("processing infreq %s\n",node->data);
orig = mystrdup(node->data);
length = atoi(strtok(node->data,blank));
//prof[0] = '\0';
sum = 0;
//find its binary rep
for (k = 0; k < length; k++) {
helix = strtok(NULL,blank);
//all helices are freq helices in binary hash
sum += *((int*) hashtbl_get(binary,helix));
}
node->data = orig;
//check all oval LCA for parentage/identity
for (listnode = begin; listnode; listnode = listnode->next) {
//key is mod profile, data is bin rep of profile
//if identity, change sfreq in cluster
bin = *((int*)listnode->data);
if (bin == sum)
hashtbl_insert(cluster,orig,hashtbl_get(infreq,node->data));
//if parent, change gfreq in graph
else if ((bin & sum) == bin) {
//printf("found oval parent %s of %s\n",listnode->key,orig);
for (length = 0; bin > 0; bin >>= 1)
if (bin & 1)
length++;
hash = graph[length-1];
if (hash) {
val = hashtbl_get(hash,listnode->key);
*val += *((int*)hashtbl_get(infreq,node->data));
}
else
fprintf(stderr,"error:didn't find hash at level %d in add_infreq()\n",length-1);
}
}
//check all selected nodes for parentage
for (k = 0; k < selected; k++) {
fprof = profileID[k];
//if intersection (ie parent), add to gfreq
if ((sum & sums[k]) == sums[k]) {
//printf("found parent %s of %s\n",fprof,orig);
copy = mystrdup(fprof);
length = atoi(strtok(copy,blank));
hash = graph[length-1];
fprof = modprofileID[k];
val = hashtbl_get(hash,fprof);
if (val)
*val += *((int*)hashtbl_get(infreq,node->data));
else
fprintf(stderr,"error:val not found in add_infreq()\n");
free(copy);
}
}
//free(orig);
}
}
//processes native helices if necessary; called by process_input
//returns id for i,j,k helix
int* process_native(int i, int j, int k) {
int *id = NULL,l;
char *key,tmp[ARRAYSIZE];
for (l=1; l < k; l++) {
sprintf(tmp,"%d %d",i+l,j-l);
id = hashtbl_get(bp,tmp);
if (id) {
for (l-- ; l >= 0; l--) {
sprintf(tmp,"%d %d",i+l,j+l);
hashtbl_insert(bp,tmp,id);
}
return id;
}
}
//printf("helix %d %d %d doesn't exist in sfold sample\n",i,j,k);
id = malloc(sizeof(int));
*id = hashtbl_numkeys(idhash)+1;
hashtbl_insert(bp,tmp,id);
sprintf(tmp,"%d",*id);
key = malloc(sizeof(char)*ARRAYSIZE);
sprintf(key,"%d %d %d",i,j,k);
hashtbl_insert(idhash,tmp,key);
return id;
}
//process input into three profiles Hs,Hu,Hn
//s = selected profiles, u = unselected in sample,n=in native only
//connects them, puts Hs in cluster with freq = 0 if not already there
int process_one_input(FILE *fp) {
HASHTBL *halfbrac;
FILE *file;
char temp[100],tmp[ARRAYSIZE],*profile,*fullprofile,*diff,*native,*diffn,*dup;
int i,j,k,*id,last=0,insample;
int numhelix = 0,fullnum = 0,natnum = 0;
int size = INIT_SIZE,size2 = INIT_SIZE,size3 = INIT_SIZE,size4 = INIT_SIZE,size5 = INIT_SIZE;
if (!(halfbrac = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for halfbrac failed");
exit(EXIT_FAILURE);
}
if (!(input = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for input failed");
exit(EXIT_FAILURE);
}
//longest = hashtbl_get(max,"longest");
profile = malloc(sizeof(char)*ARRAYSIZE*size);
fullprofile = malloc(sizeof(char)*ARRAYSIZE*size2);
diff = malloc(sizeof(char)*ARRAYSIZE*size3);
native = malloc(sizeof(char)*ARRAYSIZE*size4);
diffn = malloc(sizeof(char)*ARRAYSIZE*size5);
profile[0] = '\0';
fullprofile[0] = '\0';
diff[0] = '\0';
native[0] = '\0';
diffn[0] = '\0';
if (!(file = fopen(INPUT,"r")))
fprintf(stderr,"Cannot open %s\n",INPUT);
while (fgets(temp,100,file)) {
// if (sscanf(temp,"Structure %d (%d)",&i,&prob) == 2)
if (sscanf(temp,"%d %d %d",&i,&j,&k) == 3) {
insample = 1;
sprintf(tmp,"%d %d",i,j);
id = hashtbl_get(bp,tmp);
if (!id) {
id = process_native(i,j,k);
//printf("number in marginals %d\n",hashtbl_numkeys(marginals));
if (*id > hashtbl_numkeys(marginals))
insample = 0;
}
if (*id != last) {
sprintf(tmp,"%d",*id);
if (strlen(native)+strlen(tmp) > (ARRAYSIZE*size4-2))
native = realloc(native,ARRAYSIZE*(++size4));
sprintf(native,"%s%s ",native,tmp);
natnum++;
if (insample) {
fullnum++;
if (strlen(fullprofile)+strlen(tmp) > (ARRAYSIZE*size2-2))
fullprofile = realloc(fullprofile,ARRAYSIZE*(++size2));
sprintf(fullprofile,"%s%s ",fullprofile,tmp);
if (hashtbl_get(freq,tmp)) { //is a freq helix, save to profile
numhelix++;
if (strlen(profile)+strlen(tmp) > (ARRAYSIZE*size-2))
profile = realloc(profile,ARRAYSIZE*(++size));
//printf("adding %d to profile\n",*id);
sprintf(profile,"%s%s ",profile,tmp);
}
else { //if not freq record diff
if (strlen(diff)+strlen(tmp)+2 > ARRAYSIZE*size3)
diff = realloc(diff,ARRAYSIZE*(++size3));
sprintf(diff,"%s%s ",diff,tmp);
//printf("printing diff %s\n",diff);
}
}
else {
if (strlen(diffn)+strlen(tmp)+2 > ARRAYSIZE*size5)
diffn = realloc(diffn,ARRAYSIZE*(++size5));
sprintf(diffn,"%s%s ",diffn,tmp);
}
last = *id;
make_brackets(halfbrac,i,j,*id);
}
else if (VERBOSE)
printf("helix %d %d %d is duplicate with id %d: process_input()\n",i,j,k,*id);
}
}
native = sort_input(native,natnum);
//printf("native is now %s\n",native);
make_bracket_rep(halfbrac,native);
hashtbl_destroy(halfbrac);
fullprofile = sort_input(fullprofile,fullnum);
profile = sort_input(profile,numhelix);
//printf("native %s, fullprofile %s, profile %s, diff %s, diffn %s\n",native,fullprofile,profile,diff,diffn);
if (fullnum != natnum)
make_edge_and_node(fp,fullprofile,native,diffn,natnum);
if (numhelix != fullnum)
make_edge_and_node(fp,profile,fullprofile,diff,fullnum);
fprintf(fp,"\"%s\" [style = filled, fillcolor = gray60];\n",profile);
sprintf(tmp,"%d ",numhelix);
if (strlen(tmp)+strlen(profile) > ARRAYSIZE*size+1)
profile = realloc(profile,ARRAYSIZE*(++size));
dup = mystrdup(profile);
sprintf(profile,"%s%s",tmp,dup);
id = hashtbl_get(cluster,profile);
free(dup);
if (!id) {
id = malloc(sizeof(int));
*id = 0;
hashtbl_insert(cluster,profile,id);
//printf("inserting input %s into cluster\n",profile);
}
free(profile);
return numhelix;
}
//writes the destination node and edge to output file
void make_edge_and_node(FILE *fp,char *from, char *to,char *diff,int fullnum) {
char *brac;
HASHTBL *temp;
if (!(temp = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for temp in make_edge_and_node() failed");
exit(EXIT_FAILURE);
}
fprintf(fp,"\"%s\" [style = filled,fillcolor = gray70];\n",to);
//how to handle getting general frequency?
diff = insert_diff(temp,diff);
//printf("diff is %s\n",diff);
brac = edge_label(temp,from,to,fullnum);
fprintf(fp,"\"%s\"-> \"%s\" [label =\" %s\\n%s \",fontsize=8];\n",from,to,diff,brac);
hashtbl_destroy(temp);
}
//processes input file containing structures of interest, in triplet form
//similar code to make_cluster: converts triplet to profile,
void process_input(FILE *fp) {
HASHTBL *halfbrac;
FILE *file;
char temp[100],tmp[ARRAYSIZE],*profile,*fullprofile,*diff;
int i,j,k,*id,last=0,lastprob;
int numhelix = 0,fullnum = 0,size = INIT_SIZE,size2 = INIT_SIZE,size3 = INIT_SIZE;
if (!(halfbrac = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for halfbrac failed");
exit(EXIT_FAILURE);
}
if (!(input = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for input failed");
exit(EXIT_FAILURE);
}
//longest = hashtbl_get(max,"longest");
profile = malloc(sizeof(char)*ARRAYSIZE*size);
fullprofile = malloc(sizeof(char)*ARRAYSIZE*size2);
diff = malloc(sizeof(char)*ARRAYSIZE*size3);
profile[0] = '\0';
fullprofile[0] = '\0';
diff[0] = '\0';
if (!(file = fopen(INPUT,"r")))
fprintf(stderr,"Cannot open %s\n",INPUT);
while (fgets(temp,100,file)) {
if (sscanf(temp,"%d %d %d",&i,&j,&k) == 3) {
sprintf(tmp,"%d %d",i,j);
id = hashtbl_get(bp,tmp);
//printf("id is %d for %d %d %d\n",id,i,j,k);
if (!id)
id = process_native(i,j,k);
if (*id != last) {
sprintf(tmp,"%d",*id);
if (hashtbl_get(freq,tmp)) { //is a freq helix, save to profile
numhelix++;
if (strlen(profile)+strlen(tmp) > (ARRAYSIZE*size-2))
profile = resize(&size,strlen(profile)+strlen(tmp)+1,profile);
//printf("adding %d to profile\n",*id);
sprintf(profile,"%s%s ",profile,tmp);
}
else {
if (strlen(diff)+strlen(tmp)+2 > ARRAYSIZE*size3)
diff = resize(&size3,strlen(diff)+strlen(tmp)+2,diff);
sprintf(diff,"%s%s ",diff,tmp);
//printf("printing diff %s\n",diff);
}
fullnum++;
if (strlen(fullprofile)+strlen(tmp) > (ARRAYSIZE*size-2))
fullprofile = resize(&size2,strlen(fullprofile)+strlen(tmp)+2,fullprofile);
sprintf(fullprofile,"%s%s ",fullprofile,tmp);
//printf("helix %d added is %s\n",fullnum,tmp);
last = *id;
make_brackets(halfbrac,i,j,*id);
}
else //if id == last
printf("helix %d %d %d is duplicate with id %d: process_input()\n",i,j,k,*id);
}
else if (sscanf(temp,"Structure %d (%d)",&i,&j) == 2) {
if (strlen(fullprofile) == 0) {
lastprob = j;
continue;
}
//printf("profile is %s, fullprofile %s with diff %s\n\n",profile,fullprofile,diff);
halfbrac = process_input_profile(fp,halfbrac,fullprofile,fullnum,profile,numhelix,diff,lastprob);
numhelix = 0;
fullnum = 0;
profile[0] = '\0';
fullprofile[0] = '\0';
diff[0] = '\0';
lastprob = j;
}
}
//printf("input profile is %s with fullprofile %s and diff %s\n",profile,fullprofile,diff);
halfbrac = process_input_profile(fp,halfbrac,fullprofile,fullnum,profile,numhelix,diff,lastprob);
free(profile);
hashtbl_destroy(halfbrac);
//finds edges between centroids
find_centroid_edges(fp);
}
//takes input profile and checks if in graph
//if so, changes node shape to hexagon
//if not, insert new vertex
HASHTBL* process_input_profile(FILE *fp,HASHTBL *brac,char *fullprofile, int fullnum,char *profile,int numhelix, char *diff, int prob) {
HASHTBL *hash, *temp=NULL;
char *diff1,*bracket,*difftrip;
int *val,k1=0,k2=0;
KEY *parent,*next;
if (!(temp = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() failed");
exit(EXIT_FAILURE);
}
make_bracket_rep(brac,fullprofile);
hashtbl_destroy(brac);
profile = sort_input(profile,numhelix);
//printf("(sorted) profile is %s with fullprofile %s and diff %s\n",profile,fullprofile,diff);
if ((hash = graph[numhelix-1]) && (hashtbl_get(hash,profile))) {
if (numhelix == fullnum) {
//printf("case 1: full profile found in graph\n");
fullprofile = sort_input(fullprofile,numhelix);
} else {
//cannot use find_diff because fullprofile has helices not in table[]
//puts("case 2: profile found in graph");
diff = insert_diff(temp,diff);
bracket = edge_label(temp,profile,fullprofile,fullnum);
fprintf(fp,"\"%s\"-> \"%s\" [label =\" %s\\n%s \",fontsize=8,style = dotted];\n",profile,fullprofile,diff,bracket);
}
}
else {
/*
if (numhelix == fullnum)
puts("case 3: full profile not found");
else
puts("case 4: profile not found");
*/
for (parent = find_parents(profile); parent; parent = next) {
diff1 = find_diff(temp,parent->data,profile,&k1,&k2);
if (numhelix != fullnum) {
difftrip = insert_diff(temp,diff);
diff1 = realloc(diff1,strlen(diff1)+strlen(difftrip)+4);
//printf("for parent %s, diff1 is now %s, diff is %s and difftrip is %s\n",parent->data,diff1,diff,difftrip);
sprintf(diff1,"%s\\n%s",diff1,difftrip);
//printf("Diff is %s for parent %s of profile %s; diff %s for %s\n",diff1,parent->data,profile,difftrip,fullprofile);
}
bracket = edge_label(temp,parent->data,fullprofile,fullnum);
fprintf(fp,"\"%s\"-> \"%s\" [label =\" %s\\n%s \",fontsize=8,style = dotted];\n",parent->data,fullprofile,bracket,diff1);
next = parent->next;
free(parent);
}
}
//printf("%s has size %d and prob %d\n",fullprofile,fullnum,prob);
fprintf(fp,"\"%s\" [shape = hexagon];\n",fullprofile);
val = malloc(sizeof(int)*2);
val[0] = fullnum;
val[1] = prob;
hashtbl_insert(input,fullprofile,val);
hashtbl_destroy(temp);
if (!(brac = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() failed");
exit(EXIT_FAILURE);
}
return brac;
}
//find all parents of profile in graph; profile has only freq helices
//transitive reduction to the rescue again
KEY* find_parents(char *profile) {
int i,b1,b2,N,num = 0,k = 0;
HASHTBL *hash;
KEY *node,*parent,*begin = NULL;
begin = malloc(sizeof(KEY));
begin->data = "";
begin->next = NULL;
b2 = make_binary(profile,&num);
//printf("finding parents for %s of length %d and binary %d\n",profile,num,b2);
for (i = 0; i < num-1; i++) {
if (!(hash = graph[i])) continue;
for (node = hashtbl_getkeys(hash); node; node = node->next) {
b1 = make_binary(node->data,&k);
//printf("investigating %sof size %d with binary %d\n",node->data,i+1,b1);
N = b1 & b2;
if (N == b1) {
//printf("found parent %sof size %d with binary %d\n",node->data,i+1,b1);
parent = malloc(sizeof(KEY));
parent->data = node->data;
parent->next = begin;
begin = parent;
}
}
}
return begin;
}
//diff contains helix nums that are inserted into a hash
//to be used before edge_label
//add triplet info to diff
char* insert_diff(HASHTBL *temp,char *diff) {
int size = INIT_SIZE;
char *blank = " ", *copy,*k,*val;
if (strlen(diff) == 0) return "";
copy = malloc(sizeof(char)*ARRAYSIZE*size);
copy[0] = '\0';
for (k = strtok(diff,blank); k; k = strtok(NULL,blank)) {
hashtbl_insert(temp,k,"1");
//printf("insert_diff: inserting %s into hash\n",k);
val = hashtbl_get(idhash,k);
if (strlen(copy)+strlen(k)+strlen(val)+5 > ARRAYSIZE*size)
copy = resize(&size,strlen(copy)+strlen(k)+strlen(val)+5,copy);
if (strlen(copy)> 0)
strcat(copy,"\\n");
sprintf(copy,"%s%s: %s",copy,k,val);
}
//free(diff);
return copy;
}
//sorts input profile; code similar to quicksort()
char* sort_input(char *profile,int length) {
int *array,i=0;
char *blank = " ", *k,*copy = mystrdup(profile);
array = malloc(sizeof(int)*length);
for (k = strtok(copy,blank); k ; k = strtok(NULL,blank))
array[i++] = atoi(k);
qsort(array,length,sizeof(int),compare);
profile[0] = '\0';
for (i = 0; i < length; i++) {
sprintf(profile,"%s%d ",profile,array[i]);
}
// printf("input profile sorted now %s\n",profile);
free(copy);
free(array);
return profile;
}
//inserts centroids into graph
//finds and prints edges between them
void find_centroid_edges(FILE *fp) {
int *i,*zero;
KEY *node;
HASHTBL *hash = NULL;
zero = malloc(sizeof(int));
*zero = 0;
for (node = hashtbl_getkeys(input); node; node = node->next) {
i = hashtbl_get(input,node->data);
if (*i-1 > graphsize)
*i = graphsize+1;
//printf("inserting %s into graph[%d]\n",node->data,*i-1);
if (!(hash = graph[*i-1])) {
if (!(hash = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for input failed");
exit(EXIT_FAILURE);
}
graph[*i-1] = hash;
}
if (!hashtbl_get(hash,node->data))
hashtbl_insert(hash,node->data,zero);
}
}
int print_vertices(FILE *fp) {
int i,*val,size = 5,total = 0,size2=INIT_SIZE,*frq = NULL,zero = 0,start,end;
char *rank,*v;;
HASHTBL *hash;
KEY *node = NULL;
rank = malloc(sizeof(char)*ARRAYSIZE*size);
v = malloc(sizeof(char)*ARRAYSIZE*size2);
for (i = 0; !graph[i]; i++) ;
start = i;
//for (node = hashtbl_getkeys(graph[i]); node; node = node->next)
//check_insert_edge(fp,"",node->data);
//print ranks
fputs("{ node [shape = plaintext]; ",fp);
if (graph[graphsize])
end = graphsize;
else
end = graphsize-1;
for ( ; i <= end; i++) {
if (!graph[i]) continue;
fprintf(fp,"%d",i+1);
if (i == end)
fprintf(fp,"; }\n");
else
fprintf(fp,"->");
}
for (i = start; i <= end; i++) {
if (!(hash = graph[i])) continue;
//printf("printing level %d\n",i+1);
node = hashtbl_getkeys(hash);
sprintf(rank,"{ rank = same; %d;",i+1);
for (; node; node = node->next) {
if (strlen(node->data)+ 4 > ARRAYSIZE*size2-1) {
v = resize(&size2,strlen(node->data)+5,v);
//printf("resizing v of size %d to %d\n",strlen(v),size2);
}
sprintf(v,"%d %s",i+1,node->data);
frq = hashtbl_get(cluster,v);
if (!frq)
frq = &zero;
sprintf(v," \"%s\";",node->data);
val = hashtbl_get(hash,node->data);
// printf("found %s for %s\n",hashtbl_get(bracket,node->data),node->data);
if (VERBOSE)
printf("Vertex %swith frequency %d, originally %d\n",node->data,*val,*frq);
if (strlen(rank)+strlen(v) > ARRAYSIZE*size-1) {
// printf("resizing rank %s and v %s of size %d to %d\n",rank,v,strlen(rank)+strlen(v),size);
rank = resize(&size,strlen(rank)+strlen(v)+1,rank);
}
strcat(rank,v);
//fprintf(fp,"\"%s\" [label = \"%s ",node->data,hashtbl_get(bracket,node->data));
fprintf(fp,"\"%s\" [label = \"(%d/%d)\"];",node->data,*frq,*val);
/*
if (*frq == most)
fprintf(fp,"**");
fprintf(fp,"%s",hashtbl_get(bracket,node->data));
fprintf(fp,"(%d/%d)",*frq,*val);
if (*frq == most)
fprintf(fp,"**");
if (INPUT && (val = hashtbl_get(input,node->data)))
fprintf(fp,"\\n(%d)",val[1]);
fprintf(fp,"\",shape = box,style=filled,color=black,fillcolor=grey%d];",(1000-*frq)/20+49);
fprintf(fp,"\"%s\" [shape = box, label = \"%s (%d)\",style=filled,color=black,fillcolor=grey%d];\n",node->data,*val,(1000-*frq)/20+49);
*/
}
fprintf(fp,"%s }\n",rank);
total += hashtbl_numkeys(hash);
//v]0] = '\0';
}
return total;
}
//edges[LCA ID] = profile ID
//profile is LCA found, found[] is array of contributing originals, count their number
int find_edges(FILE *fp,char *profile, int *found, int count) {
int i;
char *origprof;
//for each contributing original profile...
for (i = 0; i < count; i++) {
origprof = modprofileID[found[i]];
//printf("original profile is %s, and subprofile is %swith total length %d\n",origprof,profile,strlen(profile)+strlen(origprof));
if (!strcmp(profile,origprof)) continue;
check_insert_edge(fp,profile,origprof);
}
return 0;
}
//find all edges originating from generated LCA vertices
//node is beginning of LCA list, k is number of original profiles
void find_LCA_edges(FILE *fp,struct hashnode_s *node,int k) {
int LCA,size = INIT_SIZE,i,count=0;
char *profile;
struct hashnode_s *last = NULL,*temp = NULL;
if (!node) return;
profile = malloc(sizeof(char)*ARRAYSIZE*size);
for (; node; node = node->next) {
if (last) {
free(last->data);
free(last->key);
free(last);
}
last = node;
//printf("considering LCA %s with bin %d\n",(char*)node->key,*((int*)node->data));
//check LCA against original profiles for edges
for (i = 0; i < k; i++) {
LCA = *((int*)node->data) & sums[i];
if (LCA == 0) continue;
profile = convert_binary(profile,LCA,&count);
if (LCA != *((int*)node->data))
check_insert_edge(fp,profile,node->key);
}
// count = 0;
if (!node->next) continue;
//check LCA against other LCA's
for (temp = node->next; temp; temp = temp->next) {
//printf("considering %s and %s\n",node->key,temp->key);
LCA = *((int*)node->data) & *((int*)temp->data);
if (LCA == 0) continue;
profile = convert_binary(profile,LCA,&count);
if (LCA != *((int*)node->data))
check_insert_edge(fp,profile,node->key);
else if (LCA != *((int*)temp->data))
check_insert_edge(fp,profile,temp->key);
}
}
free(last->data);
free(last->key);
free(last);
free(profile);
}
//checks whether edge exists and inserts if not
//ie insert if profile -> origprof doesn't exist yet
void check_insert_edge(FILE *fp,char *profile,char *origprof) {
int k1=0,k2=0; //*f1,*f2;
//double ratio;
char *diff,*copy,*brac;
HASHTBL *hash = NULL;
/*
hash = graph[k1-1];
f1 = hashtbl_get(hash,profile);
hash = graph[k2-1];
f2 = hashtbl_get(hash,origprof);
ratio = ((double)*f2)/((double)*f1);
// printf("ratio for %s and %s is %d/%d = %.2f\n",origprof,profile,*f2,*f1,ratio);
*/
copy = malloc(sizeof(char)*(strlen(profile)+strlen(origprof)+4));
// if (strlen(profile)+strlen(origprof) > (ARRAYSIZE*size-4))
//copy = resize(&size,strlen(profile)+strlen(origprof)+4,copy);
sprintf(copy,"%s-> %s",profile,origprof);
if (hashtbl_get(edges,copy)) return;
hashtbl_insert(edges,copy,"1");
if (!(hash = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() failed");
exit(EXIT_FAILURE);
}
diff = find_diff(hash,profile,origprof,&k1,&k2);
brac = edge_label(hash,profile,origprof,k2);
//color=gray%.0f,100-(ratio*100);
if (strlen(brac) > 1)
fprintf(fp,"\"%s\"-> \"%s\" [label=\" %s\\n%s \",fontsize=8];\n",profile,origprof,brac,diff);
//if (VERBOSE)
//printf("inserting edge %s\n",copy);
free(copy);
free(diff);
free(brac);
hashtbl_destroy(hash);
}
//finds helical difference between profile -> origprof
//k1 and k2 are num of helices in profile and origprof
//return difference with triplets
char* find_diff(HASHTBL *hash,char *profile, char *origprof, int *k1, int *k2) {
int size = INIT_SIZE,b1,b2,xor,i;
char *diff,*id;
diff = malloc(sizeof(char)*ARRAYSIZE*size);
diff[0] = '\0';
b1 = make_binary(profile,k1);
b2 = make_binary(origprof,k2);
xor = b1 ^ b2;
//printf("b1 is %d, b2 is %d, and xor is %d\n",b1,b2,xor);
for (i = 0; xor > 0; xor>>=1,i++) {
if ((xor & 1)==1) {
hashtbl_insert(hash,table[i],"1");
id = hashtbl_get(idhash,table[i]);
if (strlen(diff)+strlen(table[i])+strlen(id)+6 > ARRAYSIZE*size)
diff = resize(&size,strlen(diff)+strlen(table[i])+strlen(id)+6,diff);
if (strlen(diff)>1)
strcat(diff,"\\n");
sprintf(diff,"%s%s: %s",diff,table[i],id);
}
}
//printf("diff is %s between %s and %s\n",diff,origprof,profile);
return diff;
}
//finds the bracket notation for profile->origprof based on originating profile of size k
//difference between profile and origprof is in hash
//make sure origprof has a bracket rep (should be done in make_bracket_rep)
char* edge_label(HASHTBL *hash,char *profile, char *origprof,int k) {
int i=0,count = 0,j=0,num=0,*diff,*save,ind=0,m=0;
char *origbrac,*brac,*blank = "[]",*copy,*val;
char **array;
if (!(origbrac = hashtbl_get(bracket,origprof))) {
fprintf(stderr,"Error: origprof %s has no bracket representation in bracket: edge_label()\n",origprof);
return "";
}
// if (strlen(profile) == 0)
copy = mystrdup(origbrac);
brac = malloc(strlen(origbrac));
//printf("finding edge label between %s and %s\n",origbrac,profile);
array = malloc(sizeof(char*)*k);
diff = malloc(sizeof(int)*hashtbl_numkeys(hash));
save = malloc(sizeof(int)*hashtbl_numkeys(hash));