-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSet.c
More file actions
1466 lines (1364 loc) · 43 KB
/
Set.c
File metadata and controls
1466 lines (1364 loc) · 43 KB
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
/* RNAStructProfiling -- Profiles RNA structures and produces a summary graph in graphviz format.
* Copyright 2013, 2014, 2018 Emily Rogers
*
* This file is part of RNAStructProfiling.
*
* RNAStructProfiling is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RNAStructProfiling is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RNAStructProfiling. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Set.h"
#include "hashtbl.h"
#include "Options.h"
#include "helix_class.h"
#include "Profile.h"
#include "Profnode.h"
#include <math.h>
#include <float.h>
static HASHTBL *bp;
static HASHTBL *translate_hc;
static HASHTBL *consensus;
node* createNode(char *name)
{
node* newNode;
newNode = (node*)malloc(sizeof(node));
newNode->label = name;
newNode->neighbors = NULL;
//newNode->neighbors = (node**)malloc(sizeof(node*)*GRAPHSIZE);
newNode->nsize = 1;
newNode->numNeighbors = 0;
newNode->sfreq = 0;
newNode->gfreq = 0;
newNode->bracket = NULL;
newNode->diff = NULL;
newNode->DFS = 0;
newNode->sum = 0;
return newNode;
}
Set* make_Set(char *name) {
Set *set = (Set*) malloc(sizeof(Set));
set->seq = NULL;
set->structfile = (char *) malloc((strlen(name) + 1) * sizeof(char));
strcpy(set->structfile, name);
set->hc_size = 5;
set->hc_num = 0;
set->helsum = 0;
set->num_fhc = 0;
set->helices = (HC**) malloc(sizeof(HC*)*ARRAYSIZE*5);
/*
set->joint = (HASHTBL**) malloc(sizeof(HASHTBL*)*ARRAYSIZE*5);
for (i=0; i < ARRAYSIZE*5; i++)
set->joint[i] = NULL;
*/
set->prof_size = 5;
set->prof_num = 0;
set->num_sprof = 0;
set->profiles = (Profile**) malloc(sizeof(Profile*)*ARRAYSIZE*5);
set->proftree = (Profnode***) malloc(sizeof(Profnode**)*ARRAYSIZE*2);
set->treeindex = (int*) malloc(sizeof(int)*ARRAYSIZE*2);
set->treesize = 2;
set->h_cutoff = 0;
set->p_cutoff = 0;
set->opt = make_options();
set->inputnode = NULL;
set->graph = NULL;
return set;
}
void input_seq(Set *set,char *seqfile) {
FILE * fp;
int size = 5,fasta = 0;
char temp[100],*blank = (char*)" \n",*part,*final;
fp = fopen(seqfile,(char*)"r");
if (fp == NULL) {
fprintf(stderr, "can't open %s\n",seqfile);
}
final = (char*) malloc(sizeof(char)*ARRAYSIZE*size);
final[0] = '\0';
while (fgets(temp,100,fp)) {
//put error handling in case first line has more than 100 chars
//skipping first line if fasta header and not seq
if (temp[0] == '>' || fasta) {
//printf("found fasta header %s",temp);
if (strlen(temp) < 99 || (strlen(temp) == 99 && temp[98] == '\n')) {
fasta = 0;
continue;
}
else
fasta = 1;
continue;
}
for (part = strtok(temp,blank); part; part = strtok(NULL,blank)) {
if (strlen(final)+strlen(part) > (unsigned int)ARRAYSIZE*size-1) {
while ((unsigned int)(++size*ARRAYSIZE - 1) < strlen(final)+strlen(part)) ;
final = (char*) realloc(final,sizeof(char)*ARRAYSIZE*size);
}
final = strcat(final,part);
}
}
if (set->opt->VERBOSE)
printf("seq in %s is %s with length %d\n",seqfile,final,(signed int)strlen(final));
//printf("final char is %c\n",final[strlen(final)-1]);
set->seq = final;
}
void process_structs(Set *set) {
FILE *fp;
int i,j,k,*helixid,idcount=1,*lg,last = 0, toosmall = 0,numhelix=0,*profile=NULL,size=1,seqsize;
double *trip;
char *tmp,*key,dbl[ARRAYSIZE],*max, *delim = (char*)" ,\t\n", *val;
HASHTBL *halfbrac,*extra,*avetrip;
HC *hc;
fp = fopen(set->structfile,(char*)"r");
if (fp == NULL) {
fprintf(stderr, "can't open %s\n",set->structfile);
}
if (!(bp = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for bp failed");
exit(EXIT_FAILURE);
}
if (!(avetrip = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for avetrip failed");
exit(EXIT_FAILURE);
}
if (!(extra = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for extra failed");
exit(EXIT_FAILURE);
}
if (!(halfbrac = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for halfbrac failed");
exit(EXIT_FAILURE);
}
profile = (int*) malloc(sizeof(int)*ARRAYSIZE);
key = (char*) malloc(sizeof(char)*ARRAYSIZE);
seqsize = strlen(set->seq)*10;
tmp = (char*) malloc(sizeof(char)*seqsize);
while (fgets(tmp,seqsize,fp)) {
numhelix=0;
val = strtok(tmp,delim);
val = strtok(NULL,delim);
while ((val = strtok(NULL,delim))) {
i = atoi(val);
if ((val = strtok(NULL,delim)))
j = atoi(val);
else
fprintf(stderr, "Error in file input format: struct %d\n",set->opt->NUMSTRUCTS);
if ((val = strtok(NULL,delim)))
k = atoi(val);
else
fprintf(stderr, "Error in file input format: struct %d\n",set->opt->NUMSTRUCTS);
if (k < set->opt->MIN_HEL_LEN) {
toosmall++;
//printf("too small %d %d %d\n",i,j,k);
continue;
}
sprintf(dbl,"%d %d",i,j);
helixid = (int*)hashtbl_get(bp,dbl);
numhelix++;
//printf("%d %d %d\n",i,j,k);
if (!helixid) {
max = longest_possible(idcount,i,j,k,set->seq);
hc = create_HC(idcount,max);
addHC(set,hc,idcount);
//triplet stats
trip = (double*) malloc(sizeof(double)*3);
trip[0] = i;
trip[1] = j;
trip[2] = k;
sprintf(key,"%d",idcount);
hashtbl_insert(avetrip,key,trip);
last = idcount++;
}
else {
//printf("Found %d %d with id %d\n",i,j,*helixid);
sprintf(key,"%d",*helixid);
if (last != *helixid) {
hc = set->helices[*helixid-1];
hc->freq++;
} else {
if ((lg = (int*) hashtbl_get(extra,key)))
++*lg;
else {
lg = (int*) malloc(sizeof(int));
*lg = 1;
hashtbl_insert(extra,key,lg);
}
//if (VERBOSE)
//printf("Found repeat id %d:%s\n",last,hashtbl_get(idhash,key));
}
//average stats
trip = (double*) hashtbl_get(avetrip,key);
trip[0] += i;
trip[1] += j;
trip[2] += k;
last = *helixid;
}
if (numhelix >= ARRAYSIZE*size)
profile = (int*) realloc(profile,sizeof(int)*ARRAYSIZE*++size);
profile[numhelix-1] = last;
//calc_joint(set,prof,numhelix);
}
set->opt->NUMSTRUCTS++;
}
for (i = 1; i < idcount; i++) {
j = set->helices[i-1]->freq;
sprintf(key,"%d",i);
if ((lg = (int*) hashtbl_get(extra,key)))
k = j + *lg;
else
k = j;
trip = (double*) hashtbl_get(avetrip,key);
sprintf(key,"%.1f %.1f %.1f", trip[0]/k,trip[1]/k,trip[2]/k);
hc = set->helices[i-1];
hc->avetrip = mystrdup(key);
}
if (fclose(fp))
fprintf(stderr, "File %s not closed successfully\n",set->structfile);
hashtbl_destroy(extra);
hashtbl_destroy(avetrip);
}
/*calculates joint for last value in prof vs everything else
void calc_joint(Set *set, int *prof, int num) {
int k, *freq;
char val[ARRAYSIZE];
HASHTBL *hash;
if (!set->joint[prof[num-1]-1])
set->joint[prof[num-1]-1] = hashtbl_create(HASHSIZE,NULL);
if (num < 2)
return;
for (k = 0; k < num-1; k++) {
if (prof[k] < prof[num-1]) {
sprintf(val,"%d",prof[num-1]);
hash = set->joint[prof[k]-1];
if (!hash) fprintf(stderr,"hash doesn't exist in calc_joint\n");
freq = (int*) hashtbl_get(hash,val);
if (freq)
*freq++;
else {
freq = (int*) malloc(sizeof(int));
*freq = 1;
hashtbl_insert(hash,val,freq);
}
}
else {
sprintf(val,"%d",prof[k]);
hash = set->joint[prof[num-1]-1];
if (!hash) fprintf(stderr,"hash doesn't exist in calc_joint\n");
freq = (int*) hashtbl_get(hash,val);
if (freq)
*freq++;
else {
freq = (int*) malloc(sizeof(int));
*freq = 1;
hashtbl_insert(hash,val,freq);
}
}
}
return;
}
*/
void process_structs_sfold(Set *set) {
FILE *fp;
int i,j,k,*helixid,idcount=1,*lg,last = 0, toosmall = 0,numhelix=0,*profile=NULL,size=1;
double *trip;
char tmp[100],*key,dbl[ARRAYSIZE],*max;
HASHTBL *halfbrac,*extra,*avetrip;
HC *hc;
fp = fopen(set->structfile,"r");
if (fp == NULL) {
fprintf(stderr, "can't open %s\n",set->structfile);
}
if (!(bp = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for bp failed");
exit(EXIT_FAILURE);
}
if (!(avetrip = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for avetrip failed");
exit(EXIT_FAILURE);
}
if (!(extra = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for extra failed");
exit(EXIT_FAILURE);
}
if (!(halfbrac = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for halfbrac failed");
exit(EXIT_FAILURE);
}
key = (char*) malloc(sizeof(char)*ARRAYSIZE);
while (fgets(tmp,100,fp) != NULL) {
if (sscanf(tmp,"%d %d %d",&i,&j,&k) == 3) {
if (i == 0) continue;
if (k < set->opt->MIN_HEL_LEN) {
toosmall++;
//printf("too small %d %d %d\n",i,j,k);
continue;
}
sprintf(dbl,"%d %d",i,j);
helixid = (int*) hashtbl_get(bp,dbl);
//printf("%d %d %d\n",i,j,k);
if (!helixid) {
max = longest_possible(idcount,i,j,k,set->seq);
hc = create_HC(idcount,max);
addHC(set,hc,idcount);
//triplet stats
trip = (double*)malloc(sizeof(double)*3);
trip[0] = i;
trip[1] = j;
trip[2] = k;
sprintf(key,"%d",idcount);
hashtbl_insert(avetrip,key,trip);
if (set->opt->TOPDOWN) {
if (numhelix >= ARRAYSIZE*size)
profile = (int*) realloc(profile,sizeof(int)*ARRAYSIZE*++size);
profile[numhelix++]=idcount;
make_brackets(halfbrac,i,j,idcount);
}
last = idcount++;
}
else {
//printf("Found %d %d with id %d\n",i,j,*helixid);
sprintf(key,"%d",*helixid);
if (last != *helixid) {
hc = set->helices[*helixid-1];
hc->freq++;
if (set->opt->TOPDOWN) {
if (numhelix >= ARRAYSIZE*size)
profile = (int*)realloc(profile,sizeof(int)*ARRAYSIZE*++size);
profile[numhelix++]=*helixid;
make_brackets(halfbrac,i,j,*helixid);
}
} else {
if ((lg = (int*) hashtbl_get(extra,key)))
++*lg;
else {
lg = (int*) malloc(sizeof(int));
*lg = 1;
hashtbl_insert(extra,key,lg);
}
//if (VERBOSE)
//printf("Found repeat id %d:%s\n",last,hashtbl_get(idhash,key));
}
//average stats
trip = (double*) hashtbl_get(avetrip,key);
trip[0] += i;
trip[1] += j;
trip[2] += k;
last = *helixid;
}
}
else if (sscanf(tmp,"Structure %d",&i) == 1) {
if (set->opt->TOPDOWN) {
if (profile) {
process_profile(halfbrac,profile,numhelix,set);
if (!(halfbrac = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for halfbrac failed");
exit(EXIT_FAILURE);
}
} else
profile = (int*) malloc(sizeof(int)*ARRAYSIZE);
numhelix = 0;
}
set->opt->NUMSTRUCTS++;
}
}
if (set->opt->TOPDOWN)
process_profile(halfbrac,profile,numhelix,set);
for (i = 1; i < idcount; i++) {
j = set->helices[i-1]->freq;
sprintf(key,"%d",i);
if ((lg = (int*) hashtbl_get(extra,key)))
k = j + *lg;
else
k = j;
trip = (double*) hashtbl_get(avetrip,key);
sprintf(key,"%.1f %.1f %.1f", trip[0]/k,trip[1]/k,trip[2]/k);
hc = set->helices[i-1];
hc->avetrip = mystrdup(key);
}
if (fclose(fp))
fprintf(stderr, "File %s not closed successfully\n",set->structfile);
hashtbl_destroy(extra);
hashtbl_destroy(avetrip);
}
char* longest_possible(int id,int i,int j,int k,char *seq) {
int m = 1,*check,*num,diff;
char *val;
val = (char*) malloc(sizeof(char)*ARRAYSIZE);
for (diff = j-i-2*(k+1); diff >= 2 && match(i+k,j-k,seq); diff = j-i-2*(k+1)) k++;
//if (diff < 2 && match(i+k,j-k)) printf("found overlap for %d %d %d\n",i,j,k+1);
while (match(i-m,j+m,seq)) m++;
m--;
i -= m;
j += m;
k+= m;
sprintf(val,"%d %d %d",i,j,k);
num = (int*) malloc(sizeof(int));
*num = id;
for (m = 0; m < k; m++) {
sprintf(val,"%d %d",i+m,j-m);
if ((check = (int*) hashtbl_get(bp,val)))
printf("%s (id %d) already has id %d\n",val,id,*check);
hashtbl_insert(bp,val,num);
}
sprintf(val,"%d %d %d",i,j,k);
return val;
}
//finds longest possible helix based on i,j
//populates all bp in longest possible with id in hash
int match(int i,int j,char *seq) {
char l,r;
if (i >= j) return 0;
if (i < 1) return 0;
if ((unsigned int) j > strlen(seq)) return 0;
l = seq[i-1];
r = seq[j-1];
//printf("l(%d) is %c and r(%d) is %c\n",i,l,j,r);
if ((l == 'a' || l == 'A') && (r == 'u' || r == 'U' || r == 't' || r == 'T'))
return 1;
else if ((l == 'u' || l == 'U' || l == 't' || l == 'T') && (r == 'a' || r == 'A' || r == 'g' || r == 'G'))
return 1;
else if ((l == 'c' || l == 'C') && (r == 'g' || r == 'G'))
return 1;
else if ((l == 'g' || l == 'G') && (r == 'c' || r == 'C' || r == 'u' || r == 'U' || r == 't' || r == 'T' ))
return 1;
else
return 0;
}
void addHC(Set *set, HC *hc, int idcount) {
int i,k;
if (idcount > ARRAYSIZE*set->hc_size) {
set->hc_size++;
k = set->hc_size;
set->helices = (HC**) realloc(set->helices, sizeof(HC*)*ARRAYSIZE*k);
/*set->joint = (int**) realloc(set->joint, sizeof(int*)*ARRAYSIZE*k);
for (i=ARRAYSIZE*(k - 1); i < ARRAYSIZE*k; i++) {
set->joint[i] = (int*) malloc(sizeof(int)*(k-1-i));
}
*/
}
set->helices[idcount-1] = hc;
set->hc_num = idcount;
}
void reorder_helices(Set *set) {
int i,*nw, total, sum=0;
char *old;
HC **helices = set->helices;
total = set->hc_num;
if (!(translate_hc = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for translate_hc failed");
exit(EXIT_FAILURE);
}
qsort(helices,total,sizeof(HC*),freqcompare);
for (i = 0; i < total; i++) {
old = helices[i]->id;
nw = (int*) malloc(sizeof(int)*ARRAYSIZE);
*nw = i+1;
hashtbl_insert(translate_hc,old,nw);
sprintf(old,"%d",i+1);
sum += helices[i]->freq;
}
set->helsum = sum;
}
//will sort to have descending freq
int freqcompare(const void *v1, const void *v2) {
HC *h1,*h2;
h1 = *((HC**)v1);
int i1 = h1->freq;
h2 = *((HC**)v2);
int i2 = h2->freq;
return (i2 - i1);
}
double set_threshold_entropy(Set *set) {
int i=0;
double ent =0,frac,last=0,ave,norm;
HC **list = set->helices;
norm = (double)list[i]->freq;
for (i=0; i < set->hc_num; i++) {
frac = (double)list[i]->freq/norm;
ent -= frac*log(frac);
if (frac != 1)
ent -= (1-frac)*log(1-frac);
ave = ent/(double)(i+1);
if ((ave > last) || (fabs(ave-last) < FLT_EPSILON*2)) {
last = ave;
}
else {
//printf("%f is lower than %f\n",ent/(i+1), last);
set->num_fhc = i;
//init_joint(set);
return (100*(double) list[i-1]->freq/(double) set->opt->NUMSTRUCTS);
}
}
return 0;
}
/*LU array
void init_joint(Set *set) {
int i,j,k=set->num_fhc;
set->joint = (int**) malloc(sizeof(int*)*(k-1));
for (i = 0; i < k-1; i++) {
set->joint[i] = (int*) malloc(sizeof(int)*(k-1-i));
for (j = 0; j < k-1-i; j++) {
set->joint[i][j] = 0;
}
}
}
*/
int compare(const void *v1, const void *v2) {
return (*(int*)v1 - *(int*)v2);
}
//looks up and prints actual helices for all id's
int print_all_helices(Set *set) {
HC **helices = set->helices;
char *val,*trip;
int i,k,m,total = set->hc_num,target=0,cov=0;
target = set->opt->COVERAGE*set->helsum;
for (i = 0; i < total; i++) {
val = helices[i]->maxtrip;
m = helices[i]->freq;
trip = helices[i]->avetrip;
if (set->opt->VERBOSE) {
if (val != NULL)
printf("Helix %d is %s (%s) with freq %d\n",i+1,val,trip,m);
else
printf("No entry for %d\n",i);
}
if (cov < target) {
cov += helices[i]->freq;
k = i;
}
}
printf("%.2f coverage of helices after first %d HC\n",set->opt->COVERAGE,k+1);
return k+1;
}
double set_num_fhc(Set *set) {
int marg;
double percent;
if (set->opt->NUM_FHC > set->hc_num) {
printf("Number of requested fhc %d greater than total number of hc %d\n",set->opt->NUM_FHC, set->hc_num);
set->opt->NUM_FHC = set->hc_num;
}
if (set->opt->NUM_FHC > 63)
printf("Warning: requested num fhc %d exceeds limit of 63\n",set->opt->NUM_FHC);
marg = set->helices[set->opt->NUM_FHC-1]->freq;
percent = ((double) marg)*100.0/((double)set->opt->NUMSTRUCTS);
return percent;
}
/*
void find_bools(Set *set) {
int i,j,k,joint;
double cond1,cond2,freqthresh=0.1,thresh=0.1;
//find 10% thresh
for (k = set->num_fhc; k < set->hc_num && set->helices[k]->freq > NUMSTRUCTS*freqthresh; k++) ;
//i only ranges in entropy features, j to 10%
for (i=0; i < set->num_fhc; i++) {
for (j=i+1; j < k; j++) {
joint = set->joint[i];
cond1 = (double)set->joint[i][j]/(double)set->helices[i+j+1]->freq;
cond2 = (double)set->joint[i][j]/(double)set->helices[i]->freq;
if (set->opt->VERBOSE) {
printf("p(%d|%d) = %.3f\n",i+1,i+j+2,cond1);
printf("p(%d|%d) = %.3f\n",i+j+2,i+1,cond2);
}
if (cond1 > 1-thresh && cond2 > 1-thresh) {
printf("%d AND %d\n", i+1,i+j+2);
} else if (cond1 < thresh && cond2 < thresh)
printf("%d XOR %d\n",i+1,i+j+2);
}
}
}
*/
void print_meta(Set *set) {
int i,j,k=set->num_fhc-1;
double cond1,cond2,thresh=0.1;
for (i=0; i < k; i++) {
for (j=0; j < k-i; j++) {
cond1 = (double)set->joint[i][j]/(double)set->helices[i+j+1]->freq;
cond2 = (double)set->joint[i][j]/(double)set->helices[i]->freq;
if (set->opt->VERBOSE) {
printf("p(%d|%d) = %.3f\n",i+1,i+j+2,cond1);
printf("p(%d|%d) = %.3f\n",i+j+2,i+1,cond2);
}
if (cond1 > 1-thresh && cond2 > 1-thresh) {
printf("%d AND %d\n", i+1,i+j+2);
} else if (cond1 < thresh && cond2 < thresh)
printf("%d XOR %d\n",i+1,i+j+2);
}
}
}
/*assumes threshold found, now setting actual helices
calculates conditionals, augments features with appropriate
*/
void find_freq(Set *set) {
int marg,i,total;
double percent,cov=0;
HC *hc;
total = set->hc_num;
set->num_fhc = set->hc_num;
for (i = 0; i < total; i++) {
hc = set->helices[i];
marg = hc->freq;
percent = ((double) marg*100.0)/((double)set->opt->NUMSTRUCTS);
if (percent >= set->opt->HC_FREQ) {
if (set->opt->VERBOSE)
printf("Featured helix %d: %s with freq %d\n",i+1,hc->maxtrip,marg);
hc->isfreq = 1;
hc->binary = 1<<i;
cov += (double)marg/(double)set->helsum;;
}
else {
set->num_fhc = i;
i = total;
}
}
if (set->num_fhc > 63) {
//fprintf(stderr,"number of helices greater than allowed in find_freq()\n");
set->num_fhc = 63;
marg = set->helices[62]->freq;
printf("Capping at 63 fhc with freq %d\n",marg);
set->opt->HC_FREQ = ((double) marg)*100.0/((double)set->opt->NUMSTRUCTS);
}
printf("Coverage by featured helix classes: %.3f\n",cov);
}
void make_profiles(Set *set) {
FILE *fp,*file;
int num=1,*id = 0,i,j,k,last = -1, lastfreq = -1,*profile,totalhc=0,allhelix=0;
int numhelix = 0,size=1,tripsize = INIT_SIZE,allbp=0,fbp=0,seqsize;
double coverage=0,bpcov=0;
char *temp,val[ARRAYSIZE],*trips,*name,*prof,*sub,*delim = (char*)" ,\t\n";
HASHTBL *halfbrac;
name = set->structfile;
profile = (int*) malloc(sizeof(int)*ARRAYSIZE);
if (!(halfbrac = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for halfbrac failed");
exit(EXIT_FAILURE);
}
if (set->opt->REP_STRUCT) {
if (!(consensus = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for consensus failed");
exit(EXIT_FAILURE);
}
trips = (char*) malloc(sizeof(char)*tripsize*ARRAYSIZE);
trips[0] = '\0';
}
fp = fopen(name,"r");
if (fp == NULL) {
fprintf(stderr, "can't open %s\n",name);
return;
}
file = fopen("structure.out","w");
if (file == NULL)
fprintf(stderr,"Error: can't open structure.out\n");
fprintf(file,"Processing %s\n",name);
seqsize = strlen(set->seq)*10;
temp = (char*) malloc(sizeof(char)*seqsize);
while (fgets(temp,seqsize,fp)) {
fprintf(file,"Structure %d: ",num++);
sub = strtok(temp,delim);
sub = strtok(NULL,delim);
while ((sub = strtok(NULL,delim))) {
i = atoi(sub);
if ((sub = strtok(NULL,delim)))
j = atoi(sub);
else
fprintf(stderr, "Error in file input format, processing %s\n",sub);
if ((sub = strtok(NULL,delim)))
k = atoi(sub);
else
fprintf(stderr, "Error in file input format, processing %s\n",sub);
if (k < set->opt->MIN_HEL_LEN)
continue;
sprintf(val,"%d %d",i,j);
id = (int*) hashtbl_get(bp,val);
sprintf(val,"%d",*id);
totalhc++;
allhelix++;
allbp+=k;
id = (int*) hashtbl_get(translate_hc,val);
if (!id) fprintf(stderr,"no valid translation hc exists for %s\n",val);
if (*id != -1 && *id != last) {
fprintf(file,"%d ",*id);
if (*id <= set->num_fhc && *id != lastfreq) {
numhelix++;
fbp+=k;
if (numhelix >= ARRAYSIZE*size)
profile = (int*) realloc(profile,sizeof(int)*ARRAYSIZE*++size);
profile[numhelix-1] = *id;
//calc_joint(set,profile,numhelix);
make_brackets(halfbrac,i,j,*id);
lastfreq = *id;
}
last = *id;
//printf("assigning %d to %d %d\n",*id,i,j);
}
if (set->opt->REP_STRUCT) {
sprintf(val,"%d %d %d ",i,j,k);
while (strlen(trips)+strlen(val) > (unsigned int)(ARRAYSIZE*tripsize-1))
trips = (char*) realloc(trips,sizeof(char)*++tripsize*ARRAYSIZE);
strcat(trips,val);
}
}
prof = process_profile(halfbrac,profile,numhelix,set);
//printf("processing %d with profile %s\n",num,prof);
fprintf(file,"\n\t-> %s\n",prof);
if (set->opt->REP_STRUCT) {
make_rep_struct(consensus,prof,trips);
}
if (!(halfbrac = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for halfbrac failed");
exit(EXIT_FAILURE);
}
last = 0;
lastfreq = 0;
coverage += ((double)numhelix/(double)allhelix);
bpcov += ((double)fbp/(double)allbp);
numhelix = 0;
allhelix = 0;
if (set->opt->REP_STRUCT)
trips[0] = '\0';
}
if (set->opt->VERBOSE) {
printf("Ave number of HC per structure: %.1f\n",(double)totalhc/(double) set->opt->NUMSTRUCTS);
printf("Ave structure coverage by fhc: %.2f\n",coverage/(double)set->opt->NUMSTRUCTS);
printf("Ave structure coverage by fbp: %.2f\n",bpcov/(double)set->opt->NUMSTRUCTS);
}
free(profile);
fclose(fp);
fclose(file);
}
/*calculates joint for last value in prof vs everything else
Moved up to process_structs
void calc_joint(Set *set, int *prof, int num) {
int k;
if (num < 2)
return;
for (k = 0; k < num-1; k++) {
if (prof[k] < prof[num-1])
set->joint[prof[k]-1][prof[num-1]-prof[k]-1]++;
else
set->joint[prof[num-1]-1][prof[k]-prof[num-1]-1]++;
//if (j)
}
return;
}
*/
void make_profiles_sfold(Set *set) {
FILE *fp,*file;
int num=0,*id = 0,i,j,k,last = -1, lastfreq = -1,*profile,totalhc=0,allhelix=0;
int numhelix = 0,size=1,tripsize = INIT_SIZE,allbp=0,fbp=0;
double coverage=0,bpcov=0;
char temp[100],val[ARRAYSIZE],*trips,*name,*prof;
HASHTBL *halfbrac;
name = set->structfile;
profile = (int*) malloc(sizeof(int)*ARRAYSIZE);
if (!(halfbrac = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for halfbrac failed");
exit(EXIT_FAILURE);
}
if (set->opt->REP_STRUCT) {
if (!(consensus = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for consensus failed");
exit(EXIT_FAILURE);
}
trips = (char*) malloc(sizeof(char)*tripsize*ARRAYSIZE);
trips[0] = '\0';
}
fp = fopen(name,"r");
if (fp == NULL) {
fprintf(stderr, "can't open %s\n",name);
return;
}
file = fopen("structure.out","w");
if (file == NULL)
fprintf(stderr,"Error: can't open structure.out\n");
fprintf(file,"Processing %s\n",name);
while (fgets(temp,100,fp) != NULL) {
if (sscanf(temp,"Structure %d",&num) == 1) {
if (last == -1) {
fprintf(file,"Structure %d: ",num);
continue;
}
prof = process_profile(halfbrac,profile,numhelix,set);
//printf("processing %d with profile %s\n",num,prof);
fprintf(file,"\n\t-> %s\nStructure %d: ",prof,num);
if (set->opt->REP_STRUCT) {
make_rep_struct(consensus,prof,trips);
}
if (!(halfbrac = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for halfbrac failed");
exit(EXIT_FAILURE);
}
last = 0;
lastfreq = 0;
coverage += ((double)numhelix/(double)allhelix);
bpcov += ((double)fbp/(double)allbp);
numhelix = 0;
allhelix = 0;
if (set->opt->REP_STRUCT)
trips[0] = '\0';
}
else if (sscanf(temp,"%d %d %d",&i,&j,&k) == 3) {
if (k < set->opt->MIN_HEL_LEN)
continue;
sprintf(val,"%d %d",i,j);
id = (int*) hashtbl_get(bp,val);
sprintf(val,"%d",*id);
totalhc++;
allhelix++;
allbp+=k;
id = (int*) hashtbl_get(translate_hc,val);
if (!id) fprintf(stderr,"no valid translation hc exists for %s\n",val);
if (*id != -1 && *id != last) {
fprintf(file,"%d ",*id);
if (*id <= set->num_fhc && *id != lastfreq) {
numhelix++;
fbp+=k;
if (numhelix >= ARRAYSIZE*size)
profile = (int*) realloc(profile,sizeof(int)*ARRAYSIZE*++size);
profile[numhelix-1] = *id;
make_brackets(halfbrac,i,j,*id);
lastfreq = *id;
}
last = *id;
//printf("assigning %d to %d %d\n",*id,i,j);
}
if (set->opt->REP_STRUCT) {
sprintf(val,"%d %d %d ",i,j,k);
while (strlen(trips)+strlen(val) > (unsigned int)(ARRAYSIZE*tripsize-1))
trips = (char*) realloc(trips,sizeof(char)*++tripsize*ARRAYSIZE);
strcat(trips,val);
}
}
}
fprintf(file,"\n\t-> %s ",prof);
process_profile(halfbrac,profile,numhelix,set);
//fprintf(file,"Structure %d: %s\n",num,profile);
printf("Ave number of HC per structure: %.1f\n",(double)totalhc/(double) set->opt->NUMSTRUCTS);
printf("Ave structure coverage by fhc: %.2f\n",coverage/(double)set->opt->NUMSTRUCTS);
printf("Ave structure coverage by fbp: %.2f\n",bpcov/(double)set->opt->NUMSTRUCTS);
free(profile);
fclose(fp);
fclose(file);
}
char* process_profile(HASHTBL *halfbrac,int *profile,int numhelix,Set *set) {
int i,size=INIT_SIZE;
char val[ARRAYSIZE],*dup;
Profile **profiles;
profiles = set->profiles;
qsort(profile,numhelix,sizeof(int),compare);
dup = (char*) malloc(sizeof(char)*ARRAYSIZE*size);
dup[0] = '\0';
for (i = 0; i < numhelix; i++) {
sprintf(val,"%d ",profile[i]);
if (strlen(dup) + strlen(val) >= (unsigned int) ARRAYSIZE*size-1) {
dup = (char*) realloc(dup,sizeof(char)*ARRAYSIZE*++size);
}
dup = strcat(dup,val);
}
for (i = 0; i < set->prof_num; i++) {
if (!strcmp(profiles[i]->profile,dup)) {
profiles[i]->freq++;
break;
}
}
if (i == set->prof_num) {
if (i >= ARRAYSIZE*set->prof_size) {
set->prof_size++;
profiles = (Profile**) realloc(profiles,sizeof(Profile*)*ARRAYSIZE*set->prof_size);
set->profiles = profiles;
}
profiles[i] = create_profile(dup);
//printf("adding profile[%d] %s\n",i,profiles[i]->profile);
set->prof_num++;
make_bracket_rep(halfbrac,profiles[i]);
}
hashtbl_destroy(halfbrac);
return dup;
}
//makes the bracket representation of dup, using values in hashtbl brac
//dup is a (mod) profile in graph
//called by process_profile()
void make_bracket_rep(HASHTBL *brac,Profile *prof) {
int num,*array,k=0,size = INIT_SIZE,total;
char *profile,*val;
KEY *node = NULL;
num = hashtbl_numkeys(brac);
array = (int*) malloc(sizeof(int)*num);
for (node = hashtbl_getkeys(brac); node; node=node->next)
array[k++] = atoi(node->data);
//sort by i,j position
qsort(array,num,sizeof(int),compare);
profile = (char*) malloc(sizeof(char)*ARRAYSIZE*size);
profile[0] = '\0';
val = (char*) malloc(sizeof(char)*ARRAYSIZE);
for (k = 0; k < num; k++) {
sprintf(val,"%d",array[k]);
val = (char*) hashtbl_get(brac,val);
if ((total = strlen(profile)+strlen(val)) > ARRAYSIZE*size-1)
profile = (char*) realloc(profile,sizeof(char)*ARRAYSIZE*++size);
strcat(profile,val);
}
prof->bracket = profile;
//free(val);
free(array);
}
//inserts bracket representation for i,j into a hash
void make_brackets(HASHTBL *brac, int i, int j, int id) {
char key[ARRAYSIZE],*val;
sprintf(key,"%d",i);
val = (char*) malloc(sizeof(char)*ARRAYSIZE);
sprintf(val,"[%d",id);
// printf("making bracket %s for %d\n",val,i);
hashtbl_insert(brac,key,val);
sprintf(key,"%d",j);
val = (char*) malloc(sizeof(char)*2);
val[0] = ']';
val[1] = '\0';
hashtbl_insert(brac,key,val);
}
void make_rep_struct(HASHTBL *consensus,char *profile, char* trips) {
int *bpfreq,i,j,k;
char *val,*blank = (char*)" ",bpair[ARRAYSIZE];
HASHTBL *ij;
ij = (HASHTBL*) hashtbl_get(consensus,profile);
if (!ij) {
if (!(ij = hashtbl_create(HASHSIZE,NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() for ij failed");
exit(EXIT_FAILURE);
}