-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoseg.c
executable file
·7263 lines (6658 loc) · 197 KB
/
coseg.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
/**
** COSEG
**
** A program to identify repeat subfamilies using significant
** co-segregating ( 2-3 bp ) mutations.
**
** This program is derived from three C programs and several perl
** scripts written by Alkes Price as part of an analysis of Alu
** elements in the human genome ( "Whole-genome analysis of Alu repeat
** elements reveals complex evolutionary history", Alkes L. Price,
** Eleazar Eskin, and Pavel Pevzner, 2004 Genome Research ). The program
** was first adapted for use with other repeat families and then
** extended to support consideration of three co-segregating mutations
** using Alkes statistical model. In 2008 with the help of Andy Siegel
** an alternative statistical model was developed and the codebase
** repackaged into the single source file.
**
** Major changes from the original codebase include:
** - Removal of ALU-specific hardcoded parameters
** o Ignore consensus positions bp 116-135 ( ALU variable region )
** NOTE: This functionality was generalized and any region(s)
** can be disabled in this analysis.
** o Constant sigma threshold for single point mutations is
** is now calculated given the desired pValue, conLen, and
** a conservative choice for the number of tests being performed.
** o The consensus length is calculated based on the input file
** - Command line parameters rather than hard-coded filenames.
** - Repbase comparison analysis removed.
** - Output files streamlined.
**
** Current Authors: Robert Hubley
** Andy Siegel
** Arian Smit
**
** Original Author: Alkes Price
**
**/
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <getopt.h>
#include <ctype.h>
#include "coseg.h"
extern const char *Version;
//
// Critical Parameters
//
#define MAXS 400 // Max number of subfamilies to build
#define DISALLOW_DASHDASH 1 // Do not allow two indels to be diagnostic
#define MINDISTDEFAULT 10 // Default Min distance between diagnostic sites
#define SCORETHRESH 3.0 // Minimum number of standard deviations away
// from mean before mutation will get scored.
#define SCAFFOLD_PVALUE_THRESH .001 // The pValue significance threshold for
// scaffolds ( bi/tri mutations )
#define DISALLOW_CG 1 // Filter out CpG's in first diagnostic site
#define DISALLOW_CG2 1 // Filter out CpG's in second diagnostic site
#define DISALLOW_CG3 1 // Filter out CpG's in third diagnostic site
#define SINGLE_MUTATION_INSPENALTY 11 // Penalty for a single insertion
// during the single mutation
// search phase.
#define MULTIPLE_MUTATION_INSPENALTY 1 // Penalty for a single insertion
// during the bi/tri mutation
// search phase.
#define FORCE_DIAGNOSTIC_POS 0 // Experimental code to force preservation
// of diagnostic positions. Experimental...not
// validated -- only coded for bi-mutations.
#define PVALUETHRESH log(SCAFFOLD_PVALUE_THRESH) // Convert threshold to
// logspace
#define MAXITER 100 // Maximum iterations for scaffold loop
#define BIGITER 10
#define VERBOSE 0 // Deprecated: Use DEBUG now
// Single mutation specific parameters
#define SINGLE_PVALUE_THRESH .001 // The pValue significance threshold for
// single point mutations
#define NEW_CG 0 // Experimental filter of CG sites when
// considering single mutations. TODO:
// Test this more and document.
#define DISALLOW_INDEL 1 // flag to disallow indel in single diagnostic test
#define NEW_MUT_ONLY 1 // flag to disallow mut to value already present
// -- only used in adding new singlmut families
// Double and triple do this by default.
//
// GLOBAL VARIABLES
//
int useTRI = 0;
int DEBUG = 0; // DEBUG Levels: 0 - Quiet
// 1 - Overall state
// 2 - Detailed state
// 3 - Subroutine Calls
char **ele; // maxN*L 0=A 1=C 2=G 3=T 4=-
char **elei; // maxN*L 0= 1=+
int N;
int maxN; // The total number of sequences in
// the input file.
int assigncount[MAXS]; // Number of elements in a subfamily
int trimut[MAXS];
double mstLogPValues[MAXS]; // pvalue of each subfamily relative
// to [one] of it's closest neighbors
// ( consensus distance ).
int *assign; // Element to subfamily assignment array
int *sortedAssign;
int sortedAssignIndices[MAXS];
int *localassign;
double *logfactorial;
int localassigncount[2];
char **pattern; // MAXS*L : The most frequent char at pos x in m.a.
char **patterni; // MAXS*L
char **localpattern; // 2*L
char **localpatterni; // 2*L
int parent[MAXS]; // Subfamily to parent assignment array
int bestmutSS, bestmutx, bestmuta, bestmutxx, bestmutaa;
int bestmutxxx, bestmutaaa;
double bestmutpvalue;
char *Sxsequence; // Consensus sequence passed to program
int S, MALLOCS;
int totdist, oldtotdist, localtotdist, localoldtotdist;
int ***count; /* MAXS*L*5, count of position x value a */
int ***counti; /* MAXS*L*2, count of position x value b */
int *****bicount; /* MAXS*L*5*L*5, count of pos x val a pos xx val aa */
int ***localcount; /* 2*L*5, count of position x value a */
int ***localcounti; /* 2*L*2, count of position x value b */
int *****localbicount; /* 2*L*5*L*5, count of pos x val a pos xx val aa */
double sigmaThreshold; // Number of std devs away for single point mutation
// to be significant ( pVal 0.001 )
double sngBonferroni; // Single mutation bonferroni
double dblBonferroni; // Double mutation bonferroni
double triBonferroni; // Tripple mutation bonferroni
int ***numer, ***denom; /* MAXS*L*5 */
double **mutfrac; /* L*5 */
int labels[MAXS];
int **distance; /* MAXS*MAXS */
int we_want_best_pvalue = 0;
int **existingval; /* L*5 */
char *seqFile; /* Input sequences file */
char *conFile; /* Consensus sequence file */
char *logFile;
int minCount; /* min count of a new subfamily */
int minDist; /* min distance between diagnostic sites. min = 1 */
int conLen;
FILE *outFP;
double dblEpsilon; // Minimum number that a double can hold
int useSiegelPvalue = 1; // Flag to change statistical models
int useOriginalGraphColors = 0; // Turn on the original graph heatmap colors.
int useDisabledSites = 0; // Turn on special meaning of lower/upper
// case characters in the consensus file.
int disabledPosCount = 0; // Number of positions in cosensus which are
// disabled ( see below ).
char *disabledSites; // An array to hold a flag per consensus position
// which indicates that the site has been
// disabled by the user. I.e there may be a
// highly variable region inside an consensus
// which shouldn't be used in this type of
// analysis.
int numScaffolds = 0; // The number of total subfamilies that were
// built using co-segregating mutations.
// For singlemut
int lastScaffoldIndex = 0;
int mark[MAXS];
double bestmutsigma;
double mutsigma[MAXS]; // Holds bestmutsigma for all 1-mut families
double ***profile; /* S*L*5 0=A 1=C 2=G 3=T 4=- */
double ***profilei; /* S*L*2 0= 1=+ */
double age[MAXS];
double ***mutperage; /* 5*L*5 */
double ***mutperagei; /* 2*L*2 */
double ***mut; /* 5*L*5 */
double ***muti; /* 2*L*2 */
double **perage; /* 5*L */
double **peragei; /* 2*L */
int **edges; /* MAXS*MAXS* */
char *graphVizFile;
char *outAssignFile;
char *subfamFile;
// end for singlemut
//
// EXPERIMENTAL : New EM for intermediate check of sites
//
int *** l_count;
int *** l_counti;
int * l_assign;
char ** l_pattern;
char ** l_patterni;
//
// End Experimental
//
static char *options[] = {
"\nusage: %s [-m #] [-t] [-k] [-d] [-u #] -c <consensus file>\n",
" -s <sequence file> \n",
" Options\n",
" -c <file> - Fasta file containing the consensus sequence.\n",
" -s <file> - Sequence file containing one sequence per line\n",
" -t - Use tri-segregating mutations in calculation\n",
" -m # - The minimum number of elements in a subfamily ( default 50 )\n",
" -k - Use original Alkes Price statistical model to calculate pValues\n",
" Default: Use Andy Siegel's pvalue calculation instead.\n",
" -d - Use character case in consensus to flag disabled regions\n",
" -o - Use original colormap for graphviz/svg graphs: warm=young,\n",
" cold=older more highly diverged subfamiles. The new default\n",
" reverses this scheme: cold=young, warm=older.\n",
" -u # - Minimum distance between diagnostic sites ( min 1, default 10 )\n",
" -v # - Verbosity. Use values 0-3 for increasing verbosity ( default 0 )\n\n",
" A program to identify repeat subfamilies using significant\n",
" co-segregating ( 2-3 bp ) mutations.\n\n",
" This program is derived from three C programs and several perl\n",
" scripts written by Alkes Price as part of an analysis of Alu \n",
" elements in the human genome ( Whole-genome analysis of Alu repeat\n",
" elements reveals complex evolutionary history, Alkes L. Price,\n",
" Eleazar Eskin, and Pavel Pevzner, 2004 Genome Research ). The program\n",
" was first adapted for use with other repeat families and then\n",
" extended to support consideration of three co-segregating mutations\n",
" using Alkes statistical model. In 2008 with the help of Andy Siegel\n",
" an alternative statistical model was developed and the codebase\n",
" repackaged into the single source file.\n\n",
" Current Authors: Robert Hubley\n",
" Andy Siegel\n",
" Arian Smit\n\n",
" Original Code Author: Alkes Price\n",
0,
};
//
// MAIN
//
int
main(int argc, char **argv)
{
int origMinCount;
time_t start, finish;
int n, s, t, x, a, xx, aa, S_BEFORE_PRUNE, S_AFTER_PRUNE[BIGITER];
int iter, length, B;
double duration;
double minCountFactor;
//
// Option Processing
//
int option = 0;
minCount = 50;
minDist = MINDISTDEFAULT;
while ((option = getopt(argc, argv, "c:s:m:u:v:tkd")) != EOF)
{
switch (option)
{
case 'c':
conFile = (char *) malloc((strlen(optarg) + 1) * sizeof(char));
strcpy(conFile, optarg);
break;
case 's':
seqFile = (char *) malloc((strlen(optarg) + 1) * sizeof(char));
strcpy(seqFile, optarg);
// Log file -- TODO: Consider working this into other files
logFile = (char *) malloc((strlen(optarg) + 10) * sizeof(char));
strcpy(logFile, optarg);
strcat(logFile, ".log");
// Main output file
subfamFile = (char *) malloc((strlen(optarg) + 13) * sizeof(char));
strcpy(subfamFile, optarg);
strcat(subfamFile, ".subfamilies");
// Main graph file
graphVizFile = (char *) malloc((strlen(optarg) + 10) * sizeof(char));
strcpy(graphVizFile, optarg);
strcat(graphVizFile, ".tree.viz");
// Main assignment file
outAssignFile = (char *) malloc((strlen(optarg) + 20) * sizeof(char));
strcpy(outAssignFile, optarg);
strcat(outAssignFile, ".assign");
break;
case 'o':
useOriginalGraphColors = 1;
break;
case 'm':
sscanf(optarg, "%d", &minCount);
if (minCount <= 2)
{
printf("-m ( minCount ) must be >= 3\n");
exit(1);
}
break;
case 'u':
sscanf(optarg, "%d", &minDist);
if (minDist < 1)
{
printf("-u ( minDist ) must be >= 1\n");
exit(1);
}
break;
case 'v':
sscanf(optarg, "%d", &DEBUG);
break;
case 'k':
useSiegelPvalue = 0;
break;
case 'd':
useDisabledSites = 1;
break;
case 't':
useTRI = 1;
break;
default:
usage();
break;
}
}
if (!conFile || !seqFile)
{
usage();
}
//
if ((outFP = fopen(logFile, "w")) == NULL)
{
printf("Cannot open %s for writing!\n", logFile);
exit(1);
}
start = time(0);
length = build_sequence(conFile);
if (disabledPosCount == length)
{
printf
("ERROR: The consensus supplied to the program has all lowercase\n"
" (disabled) bases. No positions are available for\n"
" analysis. Please edit the consensus file and change\n"
" some bases to upper case and resubmit.\n");
exit(1);
}
conLen = length;
maxN = count_seqs(seqFile);
printf("COSEG - %s\n", Version);
printf("Number of sequences = %d\n", maxN);
if (useDisabledSites)
printf("Consensus length = %d ( %d disabled positions )\n",
conLen, disabledPosCount);
else
printf("Consensus length = %d\n", conLen);
printf("Min dist between sites = %d\n", minDist);
printf("Min subfamily size = %d\n", minCount);
allocate_memory();
build_eles(seqFile);
initialize_subfamily0();
origMinCount = minCount;
minCountFactor = 0.20;
//
// Calculate machine epsilon for doubles
//
dblEpsilon = getDoubleEpsilon();
printf("Machine epsilon (double) = %le\n\n\n", dblEpsilon);
//
// Main loops
//
for (B = 0; B < BIGITER; B++)
{
fprintf(outFP, "ADDING SUBFAMILIES\n");
// TRI Mutations
if (useTRI)
{
if ((((double) assigncount[S - 1]) * minCountFactor) > origMinCount)
{
minCount = (int) (((double) assigncount[S - 1]) * minCountFactor);
}
while (S < MAXS)
{
if (S == MALLOCS)
allocate_memoryS();
if (S > MALLOCS)
{
printf("OOPS S=%d MALLOCS=%d\n", S, MALLOCS);
exit(1);
}
printf("\nComputing tri-mutations S=%d minCount=%d out of %d:\n",
S, minCount, assigncount[S - 1]);
compute_tri_bestmut();
if (bestmutpvalue >= PVALUETHRESH)
{
printf(" No significant mutations at this minCount.\n");
if (minCount == origMinCount)
break;
minCount *= 0.60;
if (minCount < origMinCount)
minCount = origMinCount;
printf(" Trying new minCount = %d\n", minCount);
}
else
{
printf
(" Found significant mutation pvalue = %lf ( < %lf )\n",
bestmutpvalue, PVALUETHRESH);
build_new_tri_subfamily(); /* parent, S, assign, pattern_to_assign */
if ((((double) assigncount[S - 1]) * minCountFactor) > origMinCount)
{
minCount = (int) (((double) assigncount[S - 1]) * minCountFactor);
printf(" Trying new minCount = %d out of N=%d\n",
minCount, assigncount[S - 1]);
}
}
// DEBUG
printf("redisplaying counts:\n");
int uu = 0;
for( uu = 0; uu < S; uu++ )
{
printf("S=%d members = %d\n", uu, assigncount[uu]);
}
// END DEBUG
}
minCount = origMinCount;
}
// BI Mutations
while (S < MAXS)
{
if (S == MALLOCS)
allocate_memoryS();
if (S > MALLOCS)
{
printf("OOPS S=%d MALLOCS=%d\n", S, MALLOCS);
exit(1);
}
printf("\nComputing bi-mutations:\n");
compute_bestmut();
if (bestmutpvalue >= PVALUETHRESH)
{
printf(" No significant mutations\n");
break;
}
else
{
printf(" Found significant mutation pvalue = %lf ( < %lf )\n",
bestmutpvalue, PVALUETHRESH);
build_new_subfamily(); /* parent, S, assign, pattern_to_assign */
}
fflush(outFP);
sync();
// DEBUG
printf("redisplaying counts:\n");
int uu = 0;
for( uu = 0; uu < S; uu++ )
{
printf("S=%d members = %d\n", uu, assigncount[uu]);
}
// END DEBUG
}
fprintf(outFP, "VERIFYING SUBFAMILIES (PRUNE IF NECESSARY)\n");
printf("\nVERIFYING %d SUBFAMILIES (PRUNE IF NECESSARY)\n", S);
// DEBUG
int uu = 0;
for( uu = 0; uu < S; uu++ )
{
printf("S=%d members = %d\n", uu, assigncount[uu]);
}
// END DEBUG
S_BEFORE_PRUNE = S;
prune_subfamilies();
S_AFTER_PRUNE[B] = S;
if (S == S_BEFORE_PRUNE)
{
fprintf(outFP, "S=%d BEFORE/AFTER PRUNE\n", S);
break;
}
else
{
printf("%d Subfamilies pruned %d remaining\n", (S_BEFORE_PRUNE - S), S);
}
if ((B > 0) && (S == S_AFTER_PRUNE[B - 1]))
{
fprintf(outFP, "S_AFTER_PRUNE=%d AFTER BIG ITER (B=%d)\n", S, B);
break;
}
}
fprintf(outFP, "THERE ARE %d SUBFAMILIES IN SCAFFOLD\n", S);
printf("\nTHERE ARE %d SUBFAMILIES IN SCAFFOLD\n", S);
we_want_best_pvalue = 1;
// Build tree for scaffold subfamilies only
// - This calculates the p_value for scaffold members distinctly
// from the one contianing the scaffold and single point mutations.
// Saves results in mstLogPValues() for use by build_MST_full().
build_MST_scaffold(NULL);
numScaffolds = S;
duration = difftime(time(0), start);
printf("Scaffold duration is %.1f sec = %.1f min = %.1f hr\n",
duration, duration / 60.0, duration / 3600.0);
// Singlemut stuff
printf("\nRunning single mutation algorithm...\n");
assign_to_pattern_singlemut(); // M-step first?
lastScaffoldIndex = S - 1;
for (s = 0; s < S; s++)
{
for (t = 0; t < S; t++)
edges[s][t] = 0;
}
//
// add internal 1-mutations (along existing edge of tree)
//
build_singlemut_MST();
/*
add new 1-mutations
*/
while (S < MAXS)
{
compute_bestmut1(); /* bestmutx, bestmuta, bestmutSS */
if (bestmutsigma <= sigmaThreshold)
break;
mutsigma[S] = bestmutsigma;
build_new_subfamily2();
}
build_MST_full(graphVizFile);
print_subfamilies(S);
print_assign(outAssignFile);
// END Singlemut stuff
finish = time(0);
duration = difftime(finish, start);
fprintf(outFP, "Program duration is %.1f sec = %.1f min = %.1f hr\n",
duration, duration / 60.0, duration / 3600.0);
printf("Program duration is %.1f sec = %.1f min = %.1f hr\n\n",
duration, duration / 60.0, duration / 3600.0);
}
/************************ S U B R O U T I N E S ************************/
//
// Print program usage
//
void
usage(void)
{
int i;
char *s;
for (i = 0; (s = options[i]); ++i)
{
if (i == 0)
{
printf(s, "coseg");
}
else
{
printf("%s", s);
}
}
printf("\nVersion: %s\n\n", Version);
exit(1);
}
//
//
//
void
initialize_subfamily0()
{
int x, a, xx, xxx, aa, aaa, n;
if (DEBUG == 3)
printf("initialize_subfamily0(): Called\n");
parent[0] = -1;
for (x = 0; x < conLen; x++)
{
for (a = 0; a < 5; a++)
count[0][x][a] = 0;
// RMH added 11/8/12
for (a = 0; a < 2; a++)
counti[0][x][a] = 0;
}
// Clear bicount array
for (x = minDist; x < conLen; x++)
{
for (a = 0; a < 5; a++)
{
for (xx = 0; xx <= x - minDist; xx++)
{
for (aa = 0; aa < 5; aa++)
bicount[0][x][a][xx][aa] = 0;
}
}
}
// Initialize count/bicount/assign arrays
for (n = 0; n < N; n++)
{
for (x = 0; x < conLen; x++)
{
count[0][x][ele[n][x]]++;
// RMH added 11/8/12
counti[0][x][elei[n][x]]++;
}
for (x = minDist; x < conLen; x++)
{
for (xx = 0; xx <= x - minDist; xx++)
{
bicount[0][x][ele[n][x]][xx][ele[n][xx]]++;
}
}
assign[n] = 0;
}
assigncount[0] = N;
S = 1;
assign_to_pattern();
} // initialize_subfamily0(...
//
// Allocate memory assoicated with adding a new subfamily
//
void
allocate_memoryS()
{
int x, a, xx, xxx, aa, aaa;
if (DEBUG == 3)
printf("allocate_memoryS(): Called\n");
/*
allocate bicount[S]
*/
if ((bicount[S] = (int ****) malloc(conLen * sizeof(*bicount[S]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
for (x = minDist; x < conLen; x++)
{
if ((bicount[S][x] =
(int ***) malloc(5 * sizeof(*bicount[S][x]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
for (a = 0; a < 5; a++)
{
if ((bicount[S][x][a] =
(int **) malloc((x - minDist + 1) *
sizeof(*bicount[S][x][a]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
for (xx = 0; xx <= x - minDist; xx++)
{
if ((bicount[S][x][a][xx] =
(int *) malloc(5 * sizeof(*bicount[S][x][a][xx]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
}
}
}
MALLOCS = S + 1;
} // allocate_memoryS(...
//
//
//
void
allocate_memory()
{
int x, s, a, xx, aa, xxx, aaa, n, w, cc, dd;
double sum;
char c;
FILE *fp;
if (DEBUG == 3)
printf("allocate_memory(): Called\n");
// Based on N
if ((assign = (int *) malloc(maxN * sizeof(int))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
if ((localassign = (int *) malloc(maxN * sizeof(int))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
if ((logfactorial = (double *) malloc((maxN + 1) * sizeof(double))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
// compute logfactorial
sum = 0.0;
logfactorial[0] = 0.0;
for (n = 1; n <= maxN; n++)
{
sum += log(((double) n));
logfactorial[n] = sum;
}
if (DEBUG >= 1)
printf("logFactorial computed up to %d\n", maxN);
// Compute bonferroni & sigmaThreshold
// TODO: Consider better way to adjust when disabled positions
// are in play and for ignored CG sites
dblBonferroni = 0.5 * ((double) (conLen - disabledPosCount - minDist)) *
((double) (conLen - disabledPosCount - minDist + 1)) * 4.0 * 4.0;
dblBonferroni = log(dblBonferroni);
triBonferroni = (((double) (conLen - disabledPosCount - minDist))
*
((double) (conLen - disabledPosCount - minDist - minDist))
*
((double)
(conLen - disabledPosCount - minDist - minDist +
1)) * 4.0 * 4.0 * 4.0) / ((double) 6);
triBonferroni = log(triBonferroni);
sngBonferroni = (conLen - disabledPosCount) * 4 * MAXS;
sigmaThreshold = -inverseNormalCDF(SINGLE_PVALUE_THRESH / sngBonferroni);
printf("Bonferroni = %lf, triBonferroni = %lf, sigmaThreshold = %lf\n",
dblBonferroni, triBonferroni, sigmaThreshold);
// Allocate the sortedAssign list for optimizing the getTRICount
// function.
if (useTRI)
{
if ((sortedAssign = (int *) malloc(maxN * sizeof(int))) == NULL)
{
printf("Could not allocate sortedAssign: Out of memory?\n");
exit(1);
}
}
if ((ele = (char **) malloc(maxN * sizeof(*ele))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
if ((elei = (char **) malloc(maxN * sizeof(*elei))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
for (N = 0; N < maxN; N++)
{
if ((ele[N] = (char *) malloc(conLen * sizeof(*ele[N]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
if ((elei[N] = (char *) malloc(conLen * sizeof(*elei[N]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
}
if ((pattern = (char **) malloc(MAXS * sizeof(*pattern))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
if ((patterni = (char **) malloc(MAXS * sizeof(*patterni))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
for (s = 0; s < MAXS; s++)
{
if ((pattern[s] = (char *) malloc(conLen * sizeof(*pattern[s]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
if ((patterni[s] =
(char *) malloc(conLen * sizeof(*patterni[s]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
}
if ((localpattern = (char **) malloc(2 * sizeof(*localpattern))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
if ((localpatterni = (char **) malloc(2 * sizeof(*localpatterni))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
for (s = 0; s < 2; s++)
{
if ((localpattern[s] =
(char *) malloc(conLen * sizeof(*localpattern[s]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
if ((localpatterni[s] =
(char *) malloc(conLen * sizeof(*localpatterni[s]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
}
if ((count = (int ***) malloc(MAXS * sizeof(*count))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
for (s = 0; s < MAXS; s++)
{
if ((count[s] = (int **) malloc(conLen * sizeof(*count[s]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
for (x = 0; x < conLen; x++)
{
if ((count[s][x] = (int *) malloc(5 * sizeof(*count[s][x]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
}
}
if ((counti = (int ***) malloc(MAXS * sizeof(*counti))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
for (s = 0; s < MAXS; s++)
{
if ((counti[s] = (int **) malloc(conLen * sizeof(*counti[s]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
for (x = 0; x < conLen; x++)
{
if ((counti[s][x] = (int *) malloc(2 * sizeof(*counti[s][x]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
}
}
if ((bicount = (int *****) malloc(MAXS * sizeof(*bicount))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
S = 0;
if ((bicount[S] = (int ****) malloc(conLen * sizeof(*bicount[S]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
for (x = minDist; x < conLen; x++)
{
if ((bicount[S][x] =
(int ***) malloc(5 * sizeof(*bicount[S][x]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
for (a = 0; a < 5; a++)
{
if ((bicount[S][x][a] =
(int **) malloc((x - minDist + 1) *
sizeof(*bicount[S][x][a]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
for (xx = 0; xx <= x - minDist; xx++)
{
if ((bicount[S][x][a][xx] =
(int *) malloc(5 * sizeof(*bicount[S][x][a][xx]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
}
}
}
MALLOCS = 1;
if ((localcount = (int ***) malloc(2 * sizeof(*localcount))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
for (w = 0; w < 2; w++)
{
if ((localcount[w] =
(int **) malloc(conLen * sizeof(*localcount[w]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
for (x = 0; x < conLen; x++)
{
if ((localcount[w][x] =
(int *) malloc(5 * sizeof(*localcount[w][x]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
}
}
if ((localcounti = (int ***) malloc(2 * sizeof(*localcounti))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
for (w = 0; w < 2; w++)
{
if ((localcounti[w] =
(int **) malloc(conLen * sizeof(*localcounti[w]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
for (x = 0; x < conLen; x++)
{
if ((localcounti[w][x] =
(int *) malloc(2 * sizeof(*localcounti[w][x]))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
}
}
if ((localbicount =
(int *****) malloc(2 * conLen * sizeof(*localbicount))) == NULL)
{
printf("Out of memory\n");
exit(1);
}
for (w = 0; w < 2; w++)
{
if ((localbicount[w] =
(int ****) malloc(conLen * sizeof(*localbicount[w]))) == NULL)