File: genefrequency.c

package info (click to toggle)
wise 2.4.1-21
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 27,140 kB
  • sloc: ansic: 276,365; makefile: 1,003; perl: 886; lex: 93; yacc: 81; sh: 24
file content (1836 lines) | stat: -rw-r--r-- 46,385 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
#ifdef _cplusplus
extern "C" {
#endif
#include "genefrequency.h"

/* Function:  RandomCodon_from_cds_triplet(gf)
 *
 * Descrip:    makes a randomcodon probability emission
 *             from the counts in genefrequency
 *
 *
 * Arg:        gf [UNKN ] Undocumented argument [GeneFrequency21 *]
 *
 * Return [UNKN ]  Undocumented return value [RandomCodon *]
 *
 */
# line 83 "genefrequency.dy"
RandomCodon * RandomCodon_from_cds_triplet(GeneFrequency21 * gf)
{
  int i;
  int a,b,c;
  double total;
  double lit;

  RandomCodon * out;

  out = RandomCodon_alloc();

  for(i=0,total=0;i<64;i++)
    total += gf->cds_triplet[i];
  
  for(i=0;i<125;i++) {
    if( has_random_bases(i) ) {
      lit = 0.0;
      for(a=0;a<4;a++)
	for(b=0;b<4;b++)
	  for(c=0;c<4;c++)
	    lit += gf->cds_triplet[base4_codon_from_codon(permute_possible_random_bases(i,a,b,c))];
      out->codon[i] = lit/(64*total);
    } else {
      out->codon[i] = gf->cds_triplet[base4_codon_from_codon(i)]/(total);
    }
  }

  return out;
}


/* Function:  RandomModelDNA_from_central_GeneFrequency21(gf)
 *
 * Descrip:    Makes a random model from the central gene 
 *             model of an intron. Ideal for tieing intron
 *             state distribution to the randommodel
 *
 *
 * Arg:        gf [UNKN ] Undocumented argument [GeneFrequency21 *]
 *
 * Return [UNKN ]  Undocumented return value [RandomModelDNA *]
 *
 */
# line 119 "genefrequency.dy"
RandomModelDNA * RandomModelDNA_from_central_GeneFrequency21(GeneFrequency21 * gf)
{
  RandomModelDNA * out;
  double total;

  total = sum_Probability_array(gf->central,4);


  out = RandomModelDNA_alloc();

  out->base[BASE_A] = gf->central[BASE_A]/total;
  out->base[BASE_T] = gf->central[BASE_T]/total;
  out->base[BASE_G] = gf->central[BASE_G]/total;
  out->base[BASE_C] = gf->central[BASE_C]/total;
  out->base[BASE_N] = 1.0;

  return out;
}

/* Function:  ComplexConsensi_5SS_from_GeneFrequency(gf)
 *
 * Descrip:    makes 5'SS ComplexConsensi from GeneFrequency21 structure using
 *
 *               CCC|XXXXXXX score = no(5'SS with CCC|XXXXXXX) / no(CCC in cds).
 *
 *
 * Arg:        gf [UNKN ] Undocumented argument [GeneFrequency21 *]
 *
 * Return [UNKN ]  Undocumented return value [ComplexConsensi *]
 *
 */
# line 143 "genefrequency.dy"
ComplexConsensi * ComplexConsensi_5SS_from_GeneFrequency(GeneFrequency21 * gf)
{
  register int i;
  double nocds;
  ComplexConsensi * out;

  out = ComplexConsensi_alloc_len(gf->ss5->len);



  for(i=0;i<gf->ss5->len;i++) {
    nocds = nocds_from_ambiguous_codon(gf->ss5->gsc[i]->string,gf->cds_triplet);
    if( nocds < 20 ) {
      warn("In making 5'SS consensi, got %g cds for codon %s ... not happy about this",nocds,gf->ss5->gsc[i]->string);
    }
    add_ComplexConsensi(out,ComplexConsensusWord_from_string_and_prob(gf->ss5->gsc[i]->string,gf->ss5->gsc[i]->number / nocds));
  }

  return out;
  
}

/* Function:  ComplexConsensi_3SS_from_GeneFrequency(gf)
 *
 * Descrip:    makes 3'SS ComplexConsensi from GeneFrequency21 structure using
 *
 *               ZZZ|CCC score = no(3'SS with ZZZ|CCC) / no(CCC in cds).
 *
 *
 * Arg:        gf [UNKN ] Undocumented argument [GeneFrequency21 *]
 *
 * Return [UNKN ]  Undocumented return value [ComplexConsensi *]
 *
 */
# line 170 "genefrequency.dy"
ComplexConsensi * ComplexConsensi_3SS_from_GeneFrequency(GeneFrequency21 * gf)
{
  register int i;
  double nocds;
  ComplexConsensi * out;

  out = ComplexConsensi_alloc_len(gf->ss3->len);

  for(i=0;i<gf->ss3->len;i++) {
    nocds = nocds_from_ambiguous_codon(gf->ss3->gsc[i]->string+3,gf->cds_triplet);
    if( nocds < 20 ) {
      warn("In making 3'SS consensi, got %g cds for codon %s ... not happy about this!",nocds,gf->ss3->gsc[i]->string);
    }
    add_ComplexConsensi(out,ComplexConsensusWord_from_string_and_prob(gf->ss3->gsc[i]->string,gf->ss3->gsc[i]->number / nocds));
  }

  return out;
  
}

/* Function:  nocds_from_ambiguous_codon(codon,codon_freq_array)
 *
 * Descrip:    helper function for above guys
 *
 *
 * Arg:                   codon [UNKN ] Undocumented argument [char *]
 * Arg:        codon_freq_array [UNKN ] Undocumented argument [double *]
 *
 * Return [UNKN ]  Undocumented return value [double]
 *
 */
# line 194 "genefrequency.dy"
double nocds_from_ambiguous_codon(char * codon,double * codon_freq_array)
{
  int factor = 1;
  int one;
  int two;
  int three;
  int i,j,k;
  double ret = 0.0;

  one = base_from_char(*codon == '-' ? 'N' : *codon);
  two = base_from_char(*(codon+1) == '-' ? 'N' : *(codon+1));
  three = base_from_char(*(codon+2) == '-' ? 'N' : *(codon+2));


  if(one == BASE_N)
    factor *= 4;
  if(two == BASE_N)
    factor *= 4;
  if(three == BASE_N)
    factor *= 4;
  

  for(i=0;i<4;i++)
    for(j=0;j<4;j++) 
      for(k=0;k<4;k++) 
	if( (one == i || one == BASE_N) && (two == j || two == BASE_N) && (three == k || three == BASE_N)) { 
	  ret += codon_freq_array[i*16+j*4+k];
	}
  

  ret = ret / factor;

  if( ret < 0.0000000000000001 ) {
    warn("For codon  %c%c%c we have a frequency of %g",*codon,*(codon+1),*(codon+2),ret);
    ret = 0.0000000000000001;
  }

  return ret;
}

/* Function:  ComplexConsensusWord_from_string_and_prob(string,p)
 *
 * Descrip:    convienent constructor
 *
 *
 * Arg:        string [UNKN ] Undocumented argument [char *]
 * Arg:             p [UNKN ] Undocumented argument [Probability]
 *
 * Return [UNKN ]  Undocumented return value [ComplexConsensusWord *]
 *
 */
# line 238 "genefrequency.dy"
ComplexConsensusWord * ComplexConsensusWord_from_string_and_prob(char * string,Probability p)
{
  ComplexConsensusWord * out;

  out = ComplexConsensusWord_alloc();

  out->pattern = stringalloc(string);
  out->p = p;
  out->score = Probability2Score(p);

  return out;
}


/* Function:  CodonFrequency_from_GeneFrequency21(gf,ct)
 *
 * Descrip:    Builds a codon frequency table from raw counts
 *             in the counts file
 *
 *
 * Arg:        gf [UNKN ] Undocumented argument [GeneFrequency21 *]
 * Arg:        ct [UNKN ] Undocumented argument [CodonTable *]
 *
 * Return [UNKN ]  Undocumented return value [CodonFrequency *]
 *
 */
# line 256 "genefrequency.dy"
CodonFrequency * CodonFrequency_from_GeneFrequency21(GeneFrequency21 * gf,CodonTable * ct)
{
  return CodonFrequence_from_raw_counts(gf->codon,ct);
}


/* Function:  show_flat_GeneFrequency21(gf21,ofp)
 *
 * Descrip:    For debugging
 *
 *
 * Arg:        gf21 [UNKN ] Undocumented argument [GeneFrequency21 *]
 * Arg:         ofp [UNKN ] Undocumented argument [FILE *]
 *
 */
# line 266 "genefrequency.dy"
void show_flat_GeneFrequency21(GeneFrequency21 * gf21,FILE * ofp)
{
  if( gf21->ss5 != NULL ) {
    fprintf(ofp,"5'SS\n");
    show_GeneConsensus(gf21->ss5,ofp);
  }
  if( gf21->ss3 != NULL ) {
    fprintf(ofp,"3'SS\n");
    show_GeneConsensus(gf21->ss3,ofp);
  }

  fprintf(ofp,"Codon frequency\n");
  show_codon_emission(gf21->codon,ofp);

  fprintf(ofp,"Central emission\n");
  show_base_emission(gf21->central,ofp);

  fprintf(ofp,"Pyrimidine emission\n");
  show_base_emission(gf21->py,ofp);

  fprintf(ofp,"Spacer emission\n");
  show_base_emission(gf21->spacer,ofp);

  fprintf(ofp,"Transitions\n");
  show_Probability_array(gf21->transition,GENEFREQUENCY21_TRANSITION_LEN,ofp);

  fprintf(ofp,"\n\n");
}

/* Function:  show_codon_emission(codon,ofp)
 *
 * Descrip:    For debugging
 *
 *
 * Arg:        codon [UNKN ] Undocumented argument [double *]
 * Arg:          ofp [UNKN ] Undocumented argument [FILE *]
 *
 */
# line 299 "genefrequency.dy"
void show_codon_emission(double * codon,FILE * ofp)
{
  register int i;

  fprintf(ofp,"begin consensus\n");

  for(i=0;i<64;i++) 
    show_single_codon_emission(codon[i],i,ofp);

  fprintf(ofp,"end consensus\n");
}

/* Function:  show_single_codon_emission(no,base4codon,ofp)
 *
 * Descrip:    For debugging
 *
 *
 * Arg:                no [UNKN ] Undocumented argument [double]
 * Arg:        base4codon [UNKN ] Undocumented argument [int]
 * Arg:               ofp [UNKN ] Undocumented argument [FILE *]
 *
 */
# line 315 "genefrequency.dy"
void show_single_codon_emission(double no,int base4codon,FILE * ofp)
{
  codon c;
  base one;
  base two;
  base three;

  c = codon_from_base4_codon(base4codon);

  all_bases_from_codon(c,&one,&two,&three);

  fprintf(ofp,"%c%c%c %.2f\n",char_from_base(one),char_from_base(two),char_from_base(three),no);
}

/* Function:  show_base_emission(base,ofp)
 *
 * Descrip:    For debugging
 *
 *
 * Arg:        base [UNKN ] Undocumented argument [double *]
 * Arg:         ofp [UNKN ] Undocumented argument [FILE *]
 *
 */
# line 333 "genefrequency.dy"
void show_base_emission(double * base,FILE * ofp)
{
  register int i;

  fprintf(ofp,"begin consensus\n");

  for(i=0;i<4;i++)
    fprintf(ofp,"%c %.2f\n",char_from_base(i),base[i]);

  fprintf(ofp,"end consensus\n");
}


/* Function:  show_GeneConsensus(gc,ofp)
 *
 * Descrip:    For debugging
 *
 *
 * Arg:         gc [UNKN ] Undocumented argument [GeneConsensus *]
 * Arg:        ofp [UNKN ] Undocumented argument [FILE *]
 *
 */
# line 350 "genefrequency.dy"
void show_GeneConsensus(GeneConsensus * gc,FILE * ofp)
{
  register int i;

  fprintf(ofp,"begin consensus\n");

  for(i=0;i<gc->len;i++)
    show_GeneSingleCons(gc->gsc[i],ofp);

  fprintf(ofp,"end consensus\n");
}


/* Function:  show_GeneSingleCons(gsc,ofp)
 *
 * Descrip:    For debugging
 *
 *
 * Arg:        gsc [UNKN ] Undocumented argument [GeneSingleCons *]
 * Arg:        ofp [UNKN ] Undocumented argument [FILE *]
 *
 */
# line 367 "genefrequency.dy"
void show_GeneSingleCons(GeneSingleCons * gsc,FILE * ofp)
{
  fprintf(ofp,"%s %f\n",gsc->string,gsc->number);
}




 /*** reading in ***/

/* Function:  read_GeneFrequency21_file(filename)
 *
 * Descrip:    Opens the file with /openfile
 *
 *             Reads in a GeneFrequency (Mor-Ewan style)
 *
 *
 *
 * Arg:        filename [UNKN ] will open from WISECONFIGDIR etc via openfile [char *]
 *
 * Return [UNKN ]  a newly allocated structure [GeneFrequency21 *]
 *
 */
# line 386 "genefrequency.dy"
GeneFrequency21 * read_GeneFrequency21_file(char * filename)
{
  GeneFrequency21 * out;
  FILE * ifp;

  ifp = openfile(filename,"r");

  if( ifp == NULL ) {
    warn("Could not open %s as a genefrequency file",filename);
    return NULL;
  }

  out = read_GeneFrequency21(ifp);

  fclose(ifp);

  return out;
}

/* Function:  read_GeneFrequency21(ifp)
 *
 * Descrip:    Reads in a GeneFrequency (Mor-Ewan style)
 *             file from ifp
 *
 *
 * Arg:        ifp [UNKN ] file pointer [FILE *]
 *
 * Return [UNKN ]  a newly allocated structure [GeneFrequency21 *]
 *
 */
# line 412 "genefrequency.dy"
GeneFrequency21 * read_GeneFrequency21(FILE * ifp)
{
  GeneFrequency21 * out;
  GeneConsensus   * temp;
  char buffer[MAXLINE];
  int phase;
  int center;
  int type;
  boolean err = FALSE;

  out = GeneFrequency21_alloc();

  while( fgets(buffer,MAXLINE,ifp) != NULL ) {
    if( buffer[0] == '#' )
      continue;


    if( strwhitestartcmp(buffer,"type",spacestr) == 0 ) {

      phase = 3; /** if no phase, assumme it is for all phases **/

      type = check_type_GeneFrequency(buffer,ifp,&center,&phase);
      
      switch(type) {
      case GeneConsensusType_5SS :
	if( phase == 3) {
	  temp = read_line_GeneConsensus(buffer,ifp);
	  temp->center = center;
	  out->ss5 = temp;
	}
	else {
	  if( skip_consensus(ifp) == FALSE ) {
	    warn("Unable to skip phase'd 5'SS information ... problem!");
	    break;
	  }
	}
	break;
      case GeneConsensusType_3SS :
	if( phase == 3) {
	  temp = read_line_GeneConsensus(buffer,ifp);
	  temp->center = center;
	  out->ss3 = temp;
	}
	else {
	  if( skip_consensus(ifp) == FALSE ) {
	    warn("Unable to skip phase'd 5'SS information ... problem!");
	    err = TRUE;
	  }
	}
	break;
      case GeneConsensusType_CDS :
	if( phase == 0) {
	  if( read_codon_GeneConsensus(out->codon,buffer,ifp) == FALSE ) {
	    warn("Unable to read codon information in GeneFrequency21... problem!");
	    break;
	  }
	}
	else if( phase == 3 ) {
	  /*** we need this! ***/
	  if( read_codon_GeneConsensus(out->cds_triplet,buffer,ifp) == FALSE ) {
	    warn("Unable to read codon information in GeneFrequency21... problem!");
	    break;
	  }
	}
	else { /** in a different phase **/
	  if( skip_consensus(ifp) == FALSE ) {
	    warn("Unable to skip phase'd CDS information ... problem!");
	    err = TRUE;
	  }
	}
	break;
      case GeneConsensusType_Intron_emission :
	if( phase == 3 ) {
	  if( read_base_GeneConsensus(out->central,buffer,ifp) == FALSE ) {
	    warn("Unable to read Intron emissions in genefrequency21 ... problem!");
	    err = TRUE;
	  }
	}
	else {
	  if( skip_consensus(ifp) == FALSE ) {
	    warn("Unable to skip phase'd CDS information ... problem!");
	    err = TRUE;
	  }
	}
	break;
      case GeneConsensusType_Pyrimidine_emission :
	if( phase == 3 ) {
	  if( read_base_GeneConsensus(out->py,buffer,ifp) == FALSE ) {
	    warn("Unable to read pyrimidine emissions in genefrequency21 ... problem!");
	    err = TRUE;
	  }
	}
	else {
	  if( skip_consensus(ifp) == FALSE ) {
	    warn("Unable to skip phase'd pyrimidine information ... problem!");
	    err = TRUE;
	  }
	}
	break;
      case GeneConsensusType_Spacer_emission :
	if( phase == 3 ) {
	  if( read_base_GeneConsensus(out->spacer,buffer,ifp) == FALSE ) {
	    warn("Unable to read spacer emissions in genefrequency21 ... problem!");
	    err = TRUE;
	  }
	}
	else {
	  if( skip_consensus(ifp) == FALSE ) {
	    warn("Unable to skip phase'd spacer information ... problem!");
	    err = TRUE;
	  }
	}
	break;
      case GeneConsensusType_Central_stay :
	out->transition[GF21_CENTRAL_STAY] = double_from_line(buffer);
	break;
      case GeneConsensusType_Pyrimidine_stay :
	out->transition[GF21_PY_STAY] = double_from_line(buffer);
	break;
      case GeneConsensusType_Spacer_stay :
	out->transition[GF21_SPACER_STAY] = double_from_line(buffer);
	break;
      case GeneConsensusType_No_spacer :
	out->transition[GF21_NO_SPACER] = double_from_line(buffer);
	break;
      case GeneConsensusType_Intron_Corr_Term :
	switch(phase) {
	case 0 :
/*	  out->transition[GF21_INTRON_CORR_TERM_0] = double_from_line(buffer); */
	  break;
	case 1 :
/*	  out->transition[GF21_INTRON_CORR_TERM_1] = double_from_line(buffer); */
	  break;
	case 2 :
/*	  out->transition[GF21_INTRON_CORR_TERM_2] = double_from_line(buffer); */
	  break;
	case 3 :
	  out->transition[GF21_INTRON_CORR_TERM] = double_from_line(buffer); 
	  break;
	default :
	  warn("Well... I have got some bad news for you. We found a phase of %d in Intron correction term. ",phase);
	  break;
	}
	break;
      default :
	warn("Got an unidenitifable type in GeneFrequency21 parse. Skippping");
	if( skip_consensus(ifp) == FALSE ) {
	  warn("Unable to skip phase'd 5'SS information ... problem!");
	  err = TRUE;
	}
	
      }

      if( err == TRUE ) {
	warn("You have had an unrecoverable error in GeneFrequency21 parsing");
	break;
      }

    }
    else {
      striptoprint(buffer);
      warn("Could not understand line [%s] in GeneFrequency21 parse",buffer);
    }
  }

  return out;
}
	
	
/* Function:  double_from_line(buffer)
 *
 * Descrip:    helper string function
 *
 *
 * Arg:        buffer [UNKN ] Undocumented argument [char *]
 *
 * Return [UNKN ]  Undocumented return value [double]
 *
 */
# line 584 "genefrequency.dy"
double double_from_line(char * buffer)
{
  char * runner;
  char * end;
  double ret;

  runner = strtok(buffer,spacestr);

  if( runner == NULL ) {
    warn("Unable to read a number in double_from_line");
    return -1.0;
  }

  ret = strtod(runner,&end);

  if( end == runner || isalnum((int)*end) ) {
    warn("Bad conversion of string [%s] to double [%f] occurred",runner,ret);
  }

  return ret;
}
  
/* Function:  skip_consensus(ifp)
 *
 * Descrip:    helper function for
 *             teh file parsing
 *
 *
 * Arg:        ifp [UNKN ] Undocumented argument [FILE *]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
# line 611 "genefrequency.dy"
boolean skip_consensus(FILE * ifp)
{
  char buffer[MAXLINE];


  while(fgets(buffer,MAXLINE,ifp) != NULL ) 
    if( strwhitestartcmp(buffer,"end",spacestr) == 0)
      break;

  if( feof(ifp) || ferror(ifp) )
    return FALSE;
  return TRUE;
}


/* Function:  check_type_GeneFrequency(*line,ifp,center,phase)
 *
 * Descrip:    Pretty sneaky function 
 *
 *             give 
 *
 *               line starting with "type xxx"
 *               ifp  file pointer
 *               centre a &int for returning the centre value of the consensus if any.
 *               phase  a &int for returning the phase value of the consensus if any.
 *
 *             you get *back* the line with the line "begin consensus" or "number" in it.
 *
 *               
 *             It returns a GeneConsensusType
 *
 *             with GeneConsensusType_Error on error
 *
 *
 *
 * Arg:         *line [UNKN ] Undocumented argument [char]
 * Arg:           ifp [UNKN ] Undocumented argument [FILE *]
 * Arg:        center [UNKN ] Undocumented argument [int *]
 * Arg:         phase [UNKN ] Undocumented argument [int *]
 *
 * Return [UNKN ]  Undocumented return value [int]
 *
 */
# line 644 "genefrequency.dy"
int check_type_GeneFrequency(char *line,FILE * ifp,int * center,int * phase)  
{
  int ret = GeneConsensusType_Error;
  char * runner;


  if( strwhitestartcmp(line,"type",spacestr) != 0 ) {
    warn("Attempting to check phase of consensus with no type line...");
    return GeneConsensusType_Error;
  }

  runner = strtok(line,spacestr);
  runner = strtok(NULL,spacestr);

  if( runner == NULL ) {
    warn("GeneFrequency type with no type. Can't read type, must set to error, but problem in later parsing");
    ret = GeneConsensusType_Error;
  }

  else {
    ret = string_to_GeneConsensusType(runner);
  }

  while( fgets(line,MAXLINE,ifp) != NULL ) {
    if( line[0] == '#' )
      continue;

    else if( strwhitestartcmp(line,"phase",spacestr) == 0 ) {
      runner = strtok(line,spacestr);
      runner = strtok(NULL,spacestr);

      if( runner == NULL ) {
	warn("Got phase line with no phase. Sad....");
	continue;
      }
      

      if( phase != NULL ) {
	if( strcmp(runner,"all") ==0 || strcmp(runner,"All") == 0)
	  *phase = 3;
	else *phase = atoi(runner);
      }
    }

    else if( strwhitestartcmp(line,"center",spacestr) == 0 || strwhitestartcmp(line,"centre",spacestr) == 0) {
      runner = strtok(line,spacestr);
      runner = strtok(NULL,spacestr);

      if( runner == NULL ) {
	warn("Got center line with no phase. Sad....");
	continue;
      }

      if( center != NULL ) {
	*center = atoi(runner);
      }
    }
    else {
      break;
    }
  }



  return ret;
}

/* Function:  string_to_GeneConsensusType(string)
 *
 * Descrip:    flips string 5SS to enum type
 *
 *
 * Arg:        string [UNKN ] Undocumented argument [char *]
 *
 * Return [UNKN ]  Undocumented return value [int]
 *
 */
# line 715 "genefrequency.dy"
int string_to_GeneConsensusType(char * string)
{


  if( strcmp(string,"5SS") == 0 )
    return GeneConsensusType_5SS;
  else if( strcmp(string,"3SS") == 0 )
    return GeneConsensusType_3SS;
  else if( strcmp(string,"CDS") == 0 )
    return GeneConsensusType_CDS;
  else if( strcmp(string,"Intron_Corr_Term") == 0 )
    return  GeneConsensusType_Intron_Corr_Term;
  else if( strcmp(string,"Intron_emission") == 0 )
    return  GeneConsensusType_Intron_emission;
  else if( strcmp(string,"Pyrimidine_emission") == 0 )
    return  GeneConsensusType_Pyrimidine_emission;
  else if( strcmp(string,"Spacer_emission") == 0 )
    return GeneConsensusType_Spacer_emission;
  else if( strcmp(string,"Central_Intron_Stay_Prob") == 0 )
    return GeneConsensusType_Central_stay;
  else if( strcmp(string,"Pyrimidine_Stay_Prob") == 0 )
    return GeneConsensusType_Pyrimidine_stay;
  else if( strcmp(string,"Spacer_Stay_Prob") == 0 )
    return GeneConsensusType_Spacer_stay;
  else if( strcmp(string,"No_Spacer_Prob") == 0 )
    return GeneConsensusType_No_spacer;
  else {
    warn("Could convert string [%s] into a gene frequency type",string);
    return GeneConsensusType_Error;
  }
}
  
/* Function:  read_base_GeneConsensus(base_array,line,ifp)
 *
 * Descrip:    assummes base_array is 4 positions long
 *               
 *             line should have begin consensus on it and be of MAXLINE length as it will be used as the buffer.
 *               
 *             This does **not** check that you have filled up all 4 positions.
 *
 *
 * Arg:        base_array [UNKN ] Undocumented argument [double *]
 * Arg:              line [UNKN ] Undocumented argument [char*]
 * Arg:               ifp [UNKN ] Undocumented argument [FILE *]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
# line 754 "genefrequency.dy"
boolean read_base_GeneConsensus(double * base_array,char* line,FILE * ifp)
{
  boolean ret = TRUE;
  int b;
  char * base;
  char * number;


  if( strwhitestartcmp(line,"begin",spacestr) != 0 || strstr(line,"consensus") == NULL ) {
    warn("In reading base GeneConsensus line, got no 'begin consensus' tag [%s]",line);
    return FALSE;
  }


  while( fgets(line,MAXLINE,ifp) != NULL ) {
    if( line[0] == '#' )
      continue;

    if( strwhitestartcmp(line,"end",spacestr) == 0 )
      break;

    base = strtok(line,spacestr);
    number = strtok(NULL,spacestr);

    if( base == NULL ) {
      warn("Found an uncommented line in base consensus with no leading base word");
      continue;
    }

    if( number == NULL ) {
      warn("For base %s, no number found",base);
      ret = FALSE;
      continue;
    }

    if( strlen(base) > 1 || (b=base_from_char(*base)) == BASE_N ) {
      warn("Could not interpret %s as an actual DNA base in read_base_GeneConsensus");
      ret = FALSE;
      continue;
    }

    base_array[b]= atof(number);

  }

  return ret;
}



/* Function:  read_codon_GeneConsensus(codon_array,line,ifp)
 *
 * Descrip:    assummes codon_array is 64 positions long
 *               
 *             line should have begin consensus on it and be of MAXLINE length as it will be used as the buffer.
 *
 *             This does **not** check that you have filled up all 64 positions.
 *
 *
 * Arg:        codon_array [UNKN ] Undocumented argument [double *]
 * Arg:               line [UNKN ] Undocumented argument [char*]
 * Arg:                ifp [UNKN ] Undocumented argument [FILE *]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
# line 811 "genefrequency.dy"
boolean read_codon_GeneConsensus(double * codon_array,char* line,FILE * ifp)
{
  boolean ret = TRUE;
  char * codon;
  char * number;


  if( strwhitestartcmp(line,"begin",spacestr) != 0 || strstr(line,"consensus") == NULL ) {
    warn("In reading codon GeneConsensus line, got no 'begin consensus' tag [%s]",line);
    return FALSE;
  }


  while( fgets(line,MAXLINE,ifp) != NULL ) {
    if( line[0] == '#' )
      continue;

    if( strwhitestartcmp(line,"end",spacestr) == 0 )
      break;

    codon = strtok(line,spacestr);
    number = strtok(NULL,spacestr);

    if( codon == NULL ) {
      warn("Found an uncommented line in codon consensus with no leading codon word");
      continue;
    }

    if( number == NULL ) {
      warn("For codon %s, no number found",codon);
      ret = FALSE;
      continue;
    }

    if( strchr(codon,'N') != NULL ) 
      continue;

    if( is_non_ambiguous_codon_seq(codon) == FALSE ) {
      warn("Codon %s is not really a codon... problem!");
      ret = FALSE;
      continue;
    }



    codon_array[base4_codon_from_seq(codon)]= atof(number);

  }

  return ret;
}
    
/* Function:  read_line_GeneConsensus(line,ifp)
 *
 * Descrip:    Reads a single GeneConsensus from a file
 *
 *
 * Arg:        line [UNKN ] Undocumented argument [char *]
 * Arg:         ifp [UNKN ] Undocumented argument [FILE *]
 *
 * Return [UNKN ]  Undocumented return value [GeneConsensus  *]
 *
 */
# line 866 "genefrequency.dy"
GeneConsensus  * read_line_GeneConsensus(char * line,FILE * ifp)
{
  GeneConsensus * out;
  GeneSingleCons * temp;
  char buffer[MAXLINE];
  char * runner;



  if( strwhitestartcmp(line,"begin",spacestr) != 0 ) {
    warn("Attempting to read a GeneConsensus structure with a line not starting with 'begin' [%s]",line);
    return NULL;
  }

  runner = strtok(line,spacestr);
  runner = strtok(NULL,spacestr);

  if( runner == NULL || strcmp(runner,"consensus") != 0 ) {
    warn("Attempting to read a GeneConsensus structure without a 'begin consensus' tag [%s]",line);
    return NULL;
  }

  out = GeneConsensus_alloc_std();

  while( fgets(buffer,MAXLINE,ifp) != NULL ) {
    if( buffer[0] == '#' )
      continue;
    if( strwhitestartcmp(buffer,"end",spacestr) == 0 )
      break;
    
   
    temp = read_line_GeneSingleCons(buffer);

    if( temp == NULL ) {
      warn("Unable to process GeneSingleCons line... dropping out...");
      break;
    }

    add_GeneConsensus(out,temp);
  }


  return out;
}
    


# line 913 "genefrequency.dy"
GeneSingleCons * read_line_GeneSingleCons(char * line)
{
  GeneSingleCons * out;
  char * runner;
  char * run2;


  runner = strtok(line,spacestr);
  run2   = strtok(NULL,spacestr);

  if( runner == NULL || run2 == NULL ) {
    warn("In read_line_GeneSingleCons was not give two different words in line [%s]",line);
    return NULL;
  }

  out = GeneSingleCons_alloc();

  out->string = stringalloc(runner);

  out->number = strtod(run2,&runner);

  if( runner == run2 || *runner != '\0' ) {
    warn("In read_line_GeneSingleCons, for string [%s], unable to convert the number [%s]",out->string,run2);
  }

  return out;
}


# line 942 "genefrequency.dy"
GeneFrequency21  * untouched_GeneFrequency21(void)
{
  register int i;
  GeneFrequency21 * out;

  out = GeneFrequency21_alloc();

  for(i=0;i<64;i++)
    out->codon[i] = (-1);

  for(i=0;i<4;i++)
    out->central[i] = out->py[i] = out->spacer[i] = (-1);
  
  for(i=0;i<GENEFREQUENCY21_TRANSITION_LEN;i++) 
    out->transition[i] = (-1.0);

  return out;
}



# line 1013 "genefrequency.c"
/* Function:  hard_link_GeneSingleCons(obj)
 *
 * Descrip:    Bumps up the reference count of the object
 *             Meaning that multiple pointers can 'own' it
 *
 *
 * Arg:        obj [UNKN ] Object to be hard linked [GeneSingleCons *]
 *
 * Return [UNKN ]  Undocumented return value [GeneSingleCons *]
 *
 */
GeneSingleCons * hard_link_GeneSingleCons(GeneSingleCons * obj) 
{
    if( obj == NULL )    {  
      warn("Trying to hard link to a GeneSingleCons object: passed a NULL object");  
      return NULL;   
      }  
    obj->dynamite_hard_link++;   
    return obj;  
}    


/* Function:  GeneSingleCons_alloc(void)
 *
 * Descrip:    Allocates structure: assigns defaults if given 
 *
 *
 *
 * Return [UNKN ]  Undocumented return value [GeneSingleCons *]
 *
 */
GeneSingleCons * GeneSingleCons_alloc(void) 
{
    GeneSingleCons * out;   /* out is exported at end of function */ 


    /* call ckalloc and see if NULL */ 
    if((out=(GeneSingleCons *) ckalloc (sizeof(GeneSingleCons))) == NULL)    {  
      warn("GeneSingleCons_alloc failed ");  
      return NULL;  /* calling function should respond! */ 
      }  
    out->dynamite_hard_link = 1; 
#ifdef PTHREAD   
    pthread_mutex_init(&(out->dynamite_mutex),NULL);     
#endif   
    out->string = NULL;  
    out->number = 0; 


    return out;  
}    


/* Function:  free_GeneSingleCons(obj)
 *
 * Descrip:    Free Function: removes the memory held by obj
 *             Will chain up to owned members and clear all lists
 *
 *
 * Arg:        obj [UNKN ] Object that is free'd [GeneSingleCons *]
 *
 * Return [UNKN ]  Undocumented return value [GeneSingleCons *]
 *
 */
GeneSingleCons * free_GeneSingleCons(GeneSingleCons * obj) 
{
    int return_early = 0;    


    if( obj == NULL) {  
      warn("Attempting to free a NULL pointer to a GeneSingleCons obj. Should be trappable");    
      return NULL;   
      }  


#ifdef PTHREAD   
    assert(pthread_mutex_lock(&(obj->dynamite_mutex)) == 0); 
#endif   
    if( obj->dynamite_hard_link > 1)     {  
      return_early = 1;  
      obj->dynamite_hard_link--; 
      }  
#ifdef PTHREAD   
    assert(pthread_mutex_unlock(&(obj->dynamite_mutex)) == 0);   
#endif   
    if( return_early == 1)   
      return NULL;   
    if( obj->string != NULL) 
      ckfree(obj->string);   


    ckfree(obj); 
    return NULL; 
}    


/* Function:  swap_GeneConsensus(list,i,j)
 *
 * Descrip:    swap function: an internal for qsort_GeneConsensus
 *             swaps two positions in the array
 *
 *
 * Arg:        list [UNKN ] List of structures to swap in [GeneSingleCons **]
 * Arg:           i [UNKN ] swap position [int]
 * Arg:           j [UNKN ] swap position [int]
 *
 */
/* swap function for qsort function */ 
void swap_GeneConsensus(GeneSingleCons ** list,int i,int j)  
{
    GeneSingleCons * temp;   
    temp=list[i];    
    list[i]=list[j]; 
    list[j]=temp;    
}    


/* Function:  qsort_GeneConsensus(list,left,right,comp)
 *
 * Descrip:    qsort - lifted from K&R 
 *             sorts the array using quicksort
 *             Probably much better to call sort_GeneConsensus which sorts from start to end
 *
 *
 * Arg:         list [UNKN ] List of structures to swap in [GeneSingleCons **]
 * Arg:         left [UNKN ] left position [int]
 * Arg:        right [UNKN ] right position [int]
 * Arg:         comp [FUNCP] Function which returns -1 or 1 to sort on [int (*comp]
 *
 */
void qsort_GeneConsensus(GeneSingleCons ** list,int left,int right,int (*comp)(GeneSingleCons * ,GeneSingleCons * )) 
{
    int i,last;  
    if( left >= right )  
      return;    


    swap_GeneConsensus(list,left,(left+right)/2);    
    last = left; 
    for ( i=left+1; i <= right;i++)  {  
      if( (*comp)(list[i],list[left]) < 0)   
        swap_GeneConsensus (list,++last,i);  
      }  
    swap_GeneConsensus (list,left,last); 
    qsort_GeneConsensus(list,left,last-1,comp);  
    qsort_GeneConsensus(list,last+1,right,comp); 
}    


/* Function:  sort_GeneConsensus(obj,comp)
 *
 * Descrip:    sorts from start to end using comp 
 *             sorts the array using quicksort by calling qsort_GeneConsensus
 *
 *
 * Arg:         obj [UNKN ] Object containing list [GeneConsensus *]
 * Arg:        comp [FUNCP] Function which returns -1 or 1 to sort on [int (*comp]
 *
 */
void sort_GeneConsensus(GeneConsensus * obj,int (*comp)(GeneSingleCons *, GeneSingleCons *)) 
{
    qsort_GeneConsensus(obj->gsc,0,obj->len-1,comp); 
    return;  
}    


/* Function:  expand_GeneConsensus(obj,len)
 *
 * Descrip:    Really an internal function for add_GeneConsensus
 *
 *
 * Arg:        obj [UNKN ] Object which contains the list [GeneConsensus *]
 * Arg:        len [UNKN ] Length to add one [int]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
boolean expand_GeneConsensus(GeneConsensus * obj,int len) 
{


    if( obj->maxlen > obj->len )     {  
      warn("expand_GeneConsensus called with no need");  
      return TRUE;   
      }  


    if( (obj->gsc = (GeneSingleCons ** ) ckrealloc (obj->gsc,sizeof(GeneSingleCons *)*len)) == NULL)     {  
      warn("ckrealloc failed for expand_GeneConsensus, returning FALSE");    
      return FALSE;  
      }  
    obj->maxlen = len;   
    return TRUE; 
}    


/* Function:  add_GeneConsensus(obj,add)
 *
 * Descrip:    Adds another object to the list. It will expand the list if necessary
 *
 *
 * Arg:        obj [UNKN ] Object which contains the list [GeneConsensus *]
 * Arg:        add [OWNER] Object to add to the list [GeneSingleCons *]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
/* will expand function if necessary */ 
boolean add_GeneConsensus(GeneConsensus * obj,GeneSingleCons * add) 
{
    if( obj->len >= obj->maxlen) {  
      if( expand_GeneConsensus(obj,obj->len + GeneConsensusLISTLENGTH) == FALSE) 
        return FALSE;    
      }  


    obj->gsc[obj->len++]=add;    
    return TRUE; 
}    


/* Function:  flush_GeneConsensus(obj)
 *
 * Descrip:    Frees the list elements, sets length to 0
 *             If you want to save some elements, use hard_link_xxx
 *             to protect them from being actually destroyed in the free
 *
 *
 * Arg:        obj [UNKN ] Object which contains the list  [GeneConsensus *]
 *
 * Return [UNKN ]  Undocumented return value [int]
 *
 */
int flush_GeneConsensus(GeneConsensus * obj) 
{
    int i;   


    for(i=0;i<obj->len;i++)  { /*for i over list length*/ 
      if( obj->gsc[i] != NULL)   {  
        free_GeneSingleCons(obj->gsc[i]);    
        obj->gsc[i] = NULL;  
        }  
      } /* end of for i over list length */ 


    obj->len = 0;    
    return i;    
}    


/* Function:  GeneConsensus_alloc_std(void)
 *
 * Descrip:    Equivalent to GeneConsensus_alloc_len(GeneConsensusLISTLENGTH)
 *
 *
 *
 * Return [UNKN ]  Undocumented return value [GeneConsensus *]
 *
 */
GeneConsensus * GeneConsensus_alloc_std(void) 
{
    return GeneConsensus_alloc_len(GeneConsensusLISTLENGTH); 
}    


/* Function:  GeneConsensus_alloc_len(len)
 *
 * Descrip:    Allocates len length to all lists
 *
 *
 * Arg:        len [UNKN ] Length of lists to allocate [int]
 *
 * Return [UNKN ]  Undocumented return value [GeneConsensus *]
 *
 */
GeneConsensus * GeneConsensus_alloc_len(int len) 
{
    GeneConsensus * out;/* out is exported at the end of function */ 


    /* Call alloc function: return NULL if NULL */ 
    /* Warning message alread in alloc function */ 
    if((out = GeneConsensus_alloc()) == NULL)    
      return NULL;   


    /* Calling ckcalloc for list elements */ 
    if((out->gsc = (GeneSingleCons ** ) ckcalloc (len,sizeof(GeneSingleCons *))) == NULL)    {  
      warn("Warning, ckcalloc failed in GeneConsensus_alloc_len");   
      return NULL;   
      }  
    out->len = 0;    
    out->maxlen = len;   


    return out;  
}    


/* Function:  hard_link_GeneConsensus(obj)
 *
 * Descrip:    Bumps up the reference count of the object
 *             Meaning that multiple pointers can 'own' it
 *
 *
 * Arg:        obj [UNKN ] Object to be hard linked [GeneConsensus *]
 *
 * Return [UNKN ]  Undocumented return value [GeneConsensus *]
 *
 */
GeneConsensus * hard_link_GeneConsensus(GeneConsensus * obj) 
{
    if( obj == NULL )    {  
      warn("Trying to hard link to a GeneConsensus object: passed a NULL object");   
      return NULL;   
      }  
    obj->dynamite_hard_link++;   
    return obj;  
}    


/* Function:  GeneConsensus_alloc(void)
 *
 * Descrip:    Allocates structure: assigns defaults if given 
 *
 *
 *
 * Return [UNKN ]  Undocumented return value [GeneConsensus *]
 *
 */
GeneConsensus * GeneConsensus_alloc(void) 
{
    GeneConsensus * out;/* out is exported at end of function */ 


    /* call ckalloc and see if NULL */ 
    if((out=(GeneConsensus *) ckalloc (sizeof(GeneConsensus))) == NULL)  {  
      warn("GeneConsensus_alloc failed ");   
      return NULL;  /* calling function should respond! */ 
      }  
    out->dynamite_hard_link = 1; 
#ifdef PTHREAD   
    pthread_mutex_init(&(out->dynamite_mutex),NULL);     
#endif   
    out->center = 0; 
    out->gsc = NULL; 
    out->len = out->maxlen = 0;  


    return out;  
}    


/* Function:  free_GeneConsensus(obj)
 *
 * Descrip:    Free Function: removes the memory held by obj
 *             Will chain up to owned members and clear all lists
 *
 *
 * Arg:        obj [UNKN ] Object that is free'd [GeneConsensus *]
 *
 * Return [UNKN ]  Undocumented return value [GeneConsensus *]
 *
 */
GeneConsensus * free_GeneConsensus(GeneConsensus * obj) 
{
    int return_early = 0;    
    int i;   


    if( obj == NULL) {  
      warn("Attempting to free a NULL pointer to a GeneConsensus obj. Should be trappable"); 
      return NULL;   
      }  


#ifdef PTHREAD   
    assert(pthread_mutex_lock(&(obj->dynamite_mutex)) == 0); 
#endif   
    if( obj->dynamite_hard_link > 1)     {  
      return_early = 1;  
      obj->dynamite_hard_link--; 
      }  
#ifdef PTHREAD   
    assert(pthread_mutex_unlock(&(obj->dynamite_mutex)) == 0);   
#endif   
    if( return_early == 1)   
      return NULL;   
    if( obj->gsc != NULL)    {  
      for(i=0;i<obj->len;i++)    {  
        if( obj->gsc[i] != NULL) 
          free_GeneSingleCons(obj->gsc[i]);  
        }  
      ckfree(obj->gsc);  
      }  


    ckfree(obj); 
    return NULL; 
}    


/* Function:  hard_link_GeneFrequency21(obj)
 *
 * Descrip:    Bumps up the reference count of the object
 *             Meaning that multiple pointers can 'own' it
 *
 *
 * Arg:        obj [UNKN ] Object to be hard linked [GeneFrequency21 *]
 *
 * Return [UNKN ]  Undocumented return value [GeneFrequency21 *]
 *
 */
GeneFrequency21 * hard_link_GeneFrequency21(GeneFrequency21 * obj) 
{
    if( obj == NULL )    {  
      warn("Trying to hard link to a GeneFrequency21 object: passed a NULL object"); 
      return NULL;   
      }  
    obj->dynamite_hard_link++;   
    return obj;  
}    


/* Function:  GeneFrequency21_alloc(void)
 *
 * Descrip:    Allocates structure: assigns defaults if given 
 *
 *
 *
 * Return [UNKN ]  Undocumented return value [GeneFrequency21 *]
 *
 */
GeneFrequency21 * GeneFrequency21_alloc(void) 
{
    GeneFrequency21 * out;  /* out is exported at end of function */ 


    /* call ckalloc and see if NULL */ 
    if((out=(GeneFrequency21 *) ckalloc (sizeof(GeneFrequency21))) == NULL)  {  
      warn("GeneFrequency21_alloc failed "); 
      return NULL;  /* calling function should respond! */ 
      }  
    out->dynamite_hard_link = 1; 
#ifdef PTHREAD   
    pthread_mutex_init(&(out->dynamite_mutex),NULL);     
#endif   
    out->ss5 = NULL; 
    out->ss3 = NULL; 
    /* codon[64] is an array: no default possible */ 
    /* central[4] is an array: no default possible */ 
    /* py[4] is an array: no default possible */ 
    /* spacer[4] is an array: no default possible */ 
    /* transition[GENEFREQUENCY21_TRANSITION_LEN] is an array: no default possible */ 
    /* cds_triplet[64] is an array: no default possible */ 


    return out;  
}    


/* Function:  free_GeneFrequency21(obj)
 *
 * Descrip:    Free Function: removes the memory held by obj
 *             Will chain up to owned members and clear all lists
 *
 *
 * Arg:        obj [UNKN ] Object that is free'd [GeneFrequency21 *]
 *
 * Return [UNKN ]  Undocumented return value [GeneFrequency21 *]
 *
 */
GeneFrequency21 * free_GeneFrequency21(GeneFrequency21 * obj) 
{
    int return_early = 0;    


    if( obj == NULL) {  
      warn("Attempting to free a NULL pointer to a GeneFrequency21 obj. Should be trappable");   
      return NULL;   
      }  


#ifdef PTHREAD   
    assert(pthread_mutex_lock(&(obj->dynamite_mutex)) == 0); 
#endif   
    if( obj->dynamite_hard_link > 1)     {  
      return_early = 1;  
      obj->dynamite_hard_link--; 
      }  
#ifdef PTHREAD   
    assert(pthread_mutex_unlock(&(obj->dynamite_mutex)) == 0);   
#endif   
    if( return_early == 1)   
      return NULL;   
    if( obj->ss5 != NULL)    
      free_GeneConsensus(obj->ss5);  
    if( obj->ss3 != NULL)    
      free_GeneConsensus(obj->ss3);  


    ckfree(obj); 
    return NULL; 
}    


/* Function:  replace_ss5_GeneFrequency21(obj,ss5)
 *
 * Descrip:    Replace member variable ss5
 *             For use principly by API functions
 *
 *
 * Arg:        obj [UNKN ] Object holding the variable [GeneFrequency21 *]
 * Arg:        ss5 [OWNER] New value of the variable [GeneConsensus *]
 *
 * Return [SOFT ]  member variable ss5 [boolean]
 *
 */
boolean replace_ss5_GeneFrequency21(GeneFrequency21 * obj,GeneConsensus * ss5) 
{
    if( obj == NULL)     {  
      warn("In replacement function ss5 for object GeneFrequency21, got a NULL object"); 
      return FALSE;  
      }  
    obj->ss5 = ss5;  
    return TRUE; 
}    


/* Function:  access_ss5_GeneFrequency21(obj)
 *
 * Descrip:    Access member variable ss5
 *             For use principly by API functions
 *
 *
 * Arg:        obj [UNKN ] Object holding the variable [GeneFrequency21 *]
 *
 * Return [SOFT ]  member variable ss5 [GeneConsensus *]
 *
 */
GeneConsensus * access_ss5_GeneFrequency21(GeneFrequency21 * obj) 
{
    if( obj == NULL)     {  
      warn("In accessor function ss5 for object GeneFrequency21, got a NULL object");    
      return NULL;   
      }  
    return obj->ss5;     
}    


/* Function:  replace_ss3_GeneFrequency21(obj,ss3)
 *
 * Descrip:    Replace member variable ss3
 *             For use principly by API functions
 *
 *
 * Arg:        obj [UNKN ] Object holding the variable [GeneFrequency21 *]
 * Arg:        ss3 [OWNER] New value of the variable [GeneConsensus *]
 *
 * Return [SOFT ]  member variable ss3 [boolean]
 *
 */
boolean replace_ss3_GeneFrequency21(GeneFrequency21 * obj,GeneConsensus * ss3) 
{
    if( obj == NULL)     {  
      warn("In replacement function ss3 for object GeneFrequency21, got a NULL object"); 
      return FALSE;  
      }  
    obj->ss3 = ss3;  
    return TRUE; 
}    


/* Function:  access_ss3_GeneFrequency21(obj)
 *
 * Descrip:    Access member variable ss3
 *             For use principly by API functions
 *
 *
 * Arg:        obj [UNKN ] Object holding the variable [GeneFrequency21 *]
 *
 * Return [SOFT ]  member variable ss3 [GeneConsensus *]
 *
 */
GeneConsensus * access_ss3_GeneFrequency21(GeneFrequency21 * obj) 
{
    if( obj == NULL)     {  
      warn("In accessor function ss3 for object GeneFrequency21, got a NULL object");    
      return NULL;   
      }  
    return obj->ss3;     
}    


/* Function:  replace_center_GeneConsensus(obj,center)
 *
 * Descrip:    Replace member variable center
 *             For use principly by API functions
 *
 *
 * Arg:           obj [UNKN ] Object holding the variable [GeneConsensus *]
 * Arg:        center [OWNER] New value of the variable [int]
 *
 * Return [SOFT ]  member variable center [boolean]
 *
 */
boolean replace_center_GeneConsensus(GeneConsensus * obj,int center) 
{
    if( obj == NULL)     {  
      warn("In replacement function center for object GeneConsensus, got a NULL object");    
      return FALSE;  
      }  
    obj->center = center;    
    return TRUE; 
}    


/* Function:  access_center_GeneConsensus(obj)
 *
 * Descrip:    Access member variable center
 *             For use principly by API functions
 *
 *
 * Arg:        obj [UNKN ] Object holding the variable [GeneConsensus *]
 *
 * Return [SOFT ]  member variable center [int]
 *
 */
int access_center_GeneConsensus(GeneConsensus * obj) 
{
    if( obj == NULL)     {  
      warn("In accessor function center for object GeneConsensus, got a NULL object");   
      return 0;  
      }  
    return obj->center;  
}    


/* Function:  access_gsc_GeneConsensus(obj,i)
 *
 * Descrip:    Access members stored in the gsc list
 *             For use principly by API functions
 *
 *
 * Arg:        obj [UNKN ] Object holding the list [GeneConsensus *]
 * Arg:          i [UNKN ] Position in the list [int]
 *
 * Return [SOFT ]  Element of the list [GeneSingleCons *]
 *
 */
GeneSingleCons * access_gsc_GeneConsensus(GeneConsensus * obj,int i) 
{
    if( obj == NULL)     {  
      warn("In accessor function gsc for object GeneConsensus, got a NULL object");  
      return NULL;   
      }  
    if( obj->len <= i )  {  
      warn("In accessor function gsc for object GeneConsensus, index %%d is greater than list length %%d",i,obj->len);   
      return NULL;   
      }  
    return obj->gsc[i];  
}    


/* Function:  length_gsc_GeneConsensus(obj)
 *
 * Descrip:    discover the length of the list
 *             For use principly by API functions
 *
 *
 * Arg:        obj [UNKN ] Object holding the list [GeneConsensus *]
 *
 * Return [UNKN ]  length of the list [int]
 *
 */
int length_gsc_GeneConsensus(GeneConsensus * obj) 
{
    if( obj == NULL)     {  
      warn("In length function gsc for object GeneConsensus, got a NULL object");    
      return -1;     
      }  
    return obj->len;     
}    


/* Function:  replace_string_GeneSingleCons(obj,string)
 *
 * Descrip:    Replace member variable string
 *             For use principly by API functions
 *
 *
 * Arg:           obj [UNKN ] Object holding the variable [GeneSingleCons *]
 * Arg:        string [OWNER] New value of the variable [char *]
 *
 * Return [SOFT ]  member variable string [boolean]
 *
 */
boolean replace_string_GeneSingleCons(GeneSingleCons * obj,char * string) 
{
    if( obj == NULL)     {  
      warn("In replacement function string for object GeneSingleCons, got a NULL object");   
      return FALSE;  
      }  
    obj->string = string;    
    return TRUE; 
}    


/* Function:  access_string_GeneSingleCons(obj)
 *
 * Descrip:    Access member variable string
 *             For use principly by API functions
 *
 *
 * Arg:        obj [UNKN ] Object holding the variable [GeneSingleCons *]
 *
 * Return [SOFT ]  member variable string [char *]
 *
 */
char * access_string_GeneSingleCons(GeneSingleCons * obj) 
{
    if( obj == NULL)     {  
      warn("In accessor function string for object GeneSingleCons, got a NULL object");  
      return NULL;   
      }  
    return obj->string;  
}    


/* Function:  replace_number_GeneSingleCons(obj,number)
 *
 * Descrip:    Replace member variable number
 *             For use principly by API functions
 *
 *
 * Arg:           obj [UNKN ] Object holding the variable [GeneSingleCons *]
 * Arg:        number [OWNER] New value of the variable [double]
 *
 * Return [SOFT ]  member variable number [boolean]
 *
 */
boolean replace_number_GeneSingleCons(GeneSingleCons * obj,double number) 
{
    if( obj == NULL)     {  
      warn("In replacement function number for object GeneSingleCons, got a NULL object");   
      return FALSE;  
      }  
    obj->number = number;    
    return TRUE; 
}    


/* Function:  access_number_GeneSingleCons(obj)
 *
 * Descrip:    Access member variable number
 *             For use principly by API functions
 *
 *
 * Arg:        obj [UNKN ] Object holding the variable [GeneSingleCons *]
 *
 * Return [SOFT ]  member variable number [double]
 *
 */
double access_number_GeneSingleCons(GeneSingleCons * obj) 
{
    if( obj == NULL)     {  
      warn("In accessor function number for object GeneSingleCons, got a NULL object");  
      return 0;  
      }  
    return obj->number;  
}    



#ifdef _cplusplus
}
#endif