File: eprimer32.c

package info (click to toggle)
emboss 6.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 571,248 kB
  • ctags: 39,971
  • sloc: ansic: 460,578; java: 29,439; perl: 13,573; sh: 12,740; makefile: 3,275; csh: 706; asm: 351; xml: 239; pascal: 237; modula3: 8
file content (1373 lines) | stat: -rw-r--r-- 43,437 bytes parent folder | download | duplicates (7)
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
/* @source eprimer32 application
** Picks PCR primers and hybridization oligos
**
** @author Copyright (C) Gary Williams (gwilliam@hgmp.mrc.ac.uk)
** 5 Nov 2001 - GWW - written
** @@
**
** This program 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 2
** of the License, or (at your option) any later version.
**
** This program 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 this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
******************************************************************************/

#include "emboss.h"
#ifndef WIN32
#include <unistd.h>
#else
#include <io.h>
#include <fcntl.h>
#include <windows.h>
#define fdopen _fdopen
#endif


/* estimate size of a sequence's output table */
#define TABLEGUESS 251




static FILE*  eprimer32_start_write(int fd);
static void   eprimer32_write(const AjPStr str, FILE *stream);
static void   eprimer32_end_write(FILE *stream);
static void   eprimer32_read(int fd, AjPStr * result);
static void   eprimer32_send_range(FILE * stream, const char * tag,
                                   const AjPRange value, 
                                   ajint begin);
static void   eprimer32_send_range_pair(FILE * stream, const char * tag,
                                        const AjPRange avalue,
                                        const AjPRange bvalue, 
                                        ajint begin);
static void eprimer32_send_range2(FILE * stream, const char * tag,
                                  const AjPRange value);
static void eprimer32_send_int(FILE * stream, const char * tag, ajint value);
static void eprimer32_send_float(FILE * stream, const char * tag, float value);
static void eprimer32_send_bool(FILE * stream, const char * tag, AjBool value);
static void eprimer32_send_string(FILE * stream, const char * tag,
                                  const AjPStr value);
static void eprimer32_send_stringC(FILE * stream, const char * tag,
                                   const char * value);
static void eprimer32_send_end(FILE * stream);
static void eprimer32_report (AjPFile outfile, const AjPStr output, 
                              ajint numreturn, ajint begin);
static void eprimer32_output_report(AjPFile outfile, const AjPTable table,
                                    ajint numreturn, ajint begin);
static const AjPStr eprimer32_tableget(const char * key1, ajint number,
                                       const char *key2,
                                       const AjPTable table);
static void eprimer32_write_primer(AjPFile outfile, const char *tag,
                                   const AjPStr pos,
                                   const AjPStr tm, const AjPStr gc,
                                   const AjPStr seq,
                                   AjBool rev, ajint begin);




/* @prog eprimer32 ************************************************************
**
** EMBOSS wrapper for the Whitehead's primer3 program
**
******************************************************************************/

int main(int argc, char **argv)
{
    /* Global details */
    AjBool explain_flag;
    AjBool file_flag;
    AjPStr* task;
    AjBool do_primer;
    AjBool do_hybrid;
    ajint num_return;

    /* "Sequence" Input Tags */
    AjPSeqall sequence;
    AjPRange included_region;
    AjPRange target;
    AjPRange excluded_region;
    AjPStr left_input;
    AjPStr right_input;

    /* Primer details */
    AjBool pick_anyway;
    AjPFile mispriming_library;
    float max_mispriming;
    float pair_max_mispriming;
    ajint gc_clamp;
    ajint opt_size;
    ajint min_size;
    ajint max_size;
    float opt_tm;
    float min_tm;
    float max_tm;
    float max_diff_tm;
    float opt_gc_percent;
    float min_gc;
    float max_gc;
    float salt_conc;
    float dna_conc;
    ajint num_ns_accepted;
    float self_any;
    float self_end;
    AjPStr tmfstr = NULL;
    AjPStr scstr  = NULL;
    ajint tm_formula;
    ajint salt_corr;
    ajint max_poly_x;

    /* Sequence Quality. These are not (yet) implemented */
    /*
       AjPFile sequence_quality;
       ajint	min_quality;
       ajint	min_end_quality;
       ajint	quality_range_min;
       ajint	quality_range_max;
       */

    /* Product details */
    ajint product_opt_size;
    AjPRange product_size_range;
    float product_opt_tm;
    float product_min_tm;
    float product_max_tm;

    /* Objective Function Penalty Weights for Primers */
    float max_end_stability;

    /* these are not (yet) implemented */
    /*
       float		inside_penalty;
       float		outside_penalty;
    */

    /* Primer penalties */
    /* these are not (yet) implemented */

    /* Internal Oligo "Sequence" Input Tags */
    AjPRange internal_oligo_excluded_region;
    AjPRange okleft_region;
    AjPRange okright_region;

    /* Internal Oligo "Global" Input Tags */
    AjPStr internal_oligo_input;
    ajint internal_oligo_opt_size;
    ajint internal_oligo_min_size;
    ajint internal_oligo_max_size;
    float internal_oligo_opt_tm;
    float internal_oligo_min_tm;
    float internal_oligo_max_tm;
    float internal_oligo_opt_gc_percent;
    float internal_oligo_min_gc;
    float internal_oligo_max_gc;
    float internal_oligo_salt_conc;
    float internal_oligo_dna_conc;
    float internal_oligo_self_any;
    float internal_oligo_self_end;
    ajint internal_oligo_max_poly_x;
    AjPFile internal_oligo_mishyb_library;
    float internal_oligo_max_mishyb;

    /*
       ajint		internal_oligo_min_quality;
    */

    /* Internal Oligo penalties */
    /* these are not (yet) implemented */

    /* EMBOSS-wrapper-specific stuff */
    AjPFile	outfile;

    /* other variables */
    AjPStr result = NULL;
    AjPStr strand = NULL;
    AjPStr substr = NULL;
    AjPSeq seq    = NULL;
    ajint begin   = 0;
    ajint end;
    FILE* stream;
    AjPStr taskstr  = NULL;
    const AjPStr program = NULL;

    /* pipe variables */

    int *pipeto;	  /* pipe to feed the exec'ed program input */
    int *pipefrom;	  /* pipe to get the exec'ed program output */

    embInit("eprimer32", argc, argv);

    /* Global details */
    explain_flag     = ajAcdGetBoolean("explainflag");
    file_flag        = ajAcdGetBoolean("fileflag");
    task             = ajAcdGetList("task");
    do_primer        = ajAcdGetToggle("primer");
    do_hybrid        = ajAcdGetToggle("hybridprobe");
    num_return       = ajAcdGetInt("numreturn");

    /* "Sequence" Input Tags */
    sequence        = ajAcdGetSeqall("sequence");
    included_region = ajAcdGetRange("includedregion");
    target          = ajAcdGetRange("targetregion");
    excluded_region = ajAcdGetRange("excludedregion");
    left_input      = ajAcdGetString("forwardinput");
    right_input     = ajAcdGetString("reverseinput");
    okleft_region   = ajAcdGetRange("okleftregion");
    okright_region  = ajAcdGetRange("okrightregion");

    /* Primer details */
    pick_anyway         = ajAcdGetBoolean("pickanyway");
    mispriming_library  = ajAcdGetInfile("mispriminglibraryfile");
    max_mispriming      = ajAcdGetFloat("maxmispriming");
    pair_max_mispriming = ajAcdGetFloat("pairmaxmispriming");
    gc_clamp            = ajAcdGetInt("gcclamp");
    opt_size            = ajAcdGetInt("optsize");
    min_size            = ajAcdGetInt("minsize");
    max_size            = ajAcdGetInt("maxsize");
    opt_tm              = ajAcdGetFloat("opttm");
    min_tm              = ajAcdGetFloat("mintm");
    max_tm              = ajAcdGetFloat("maxtm");
    max_diff_tm         = ajAcdGetFloat("maxdifftm");
    opt_gc_percent      = ajAcdGetFloat("ogcpercent");
    min_gc              = ajAcdGetFloat("mingc");
    max_gc              = ajAcdGetFloat("maxgc");
    salt_conc           = ajAcdGetFloat("saltconc");
    dna_conc            = ajAcdGetFloat("dnaconc");
    num_ns_accepted     = ajAcdGetInt("numnsaccepted");
    self_any            = ajAcdGetFloat("selfany");
    self_end            = ajAcdGetFloat("selfend");
    scstr               = ajAcdGetListSingle("scorrection");
    tmfstr              = ajAcdGetListSingle("tmformula");
    max_poly_x          = ajAcdGetInt("maxpolyx");

    ajStrToInt(scstr,&salt_corr);
    ajStrToInt(tmfstr,&tm_formula);
    
    AJCNEW0(pipeto,2);
    AJCNEW0(pipefrom,2);
    
    /* Sequence Quality */
    /* these are not (yet) implemented */
    /*
       sequence_quality  = ajAcdGetInfile("sequencequality");
       min_quality       = ajAcdGetInt("minquality");
       min_end_quality   = ajAcdGetInt("minendquality");
       quality_range_min = ajAcdGetInt("qualityrangemin");
       quality_range_max = ajAcdGetInt("qualityrangemax");
       */

    /* Product details */
    product_opt_size    = ajAcdGetInt("psizeopt");
    product_size_range  = ajAcdGetRange("prange");
    product_opt_tm      = ajAcdGetFloat("ptmopt");
    product_min_tm      = ajAcdGetFloat("ptmmin");
    product_max_tm      = ajAcdGetFloat("ptmmax");

    /* Objective Function Penalty Weights for Primers */
    max_end_stability   = ajAcdGetFloat("maxendstability");
    /* these are not (yet) implemented */
    /*
       inside_penalty      = ajAcdGetFloat("insidepenalty");
       outside_penalty     = ajAcdGetFloat("outsidepenalty");
    */

    /* Primer penalties */
    /* these are not (yet) implemented */

    /* Internal Oligo "Sequence" Input Tags */
    internal_oligo_excluded_region = ajAcdGetRange("oexcludedregion");
    internal_oligo_input           = ajAcdGetString("oligoinput");

    /* Internal Oligo "Global" Input Tags */
    internal_oligo_opt_size       = ajAcdGetInt("osizeopt");
    internal_oligo_min_size       = ajAcdGetInt("ominsize");
    internal_oligo_max_size       = ajAcdGetInt("omaxsize");
    internal_oligo_opt_tm         = ajAcdGetFloat("otmopt");
    internal_oligo_min_tm         = ajAcdGetFloat("otmmin");
    internal_oligo_max_tm         = ajAcdGetFloat("otmmax");
    internal_oligo_opt_gc_percent = ajAcdGetFloat("ogcopt");
    internal_oligo_min_gc         = ajAcdGetFloat("ogcmin");
    internal_oligo_max_gc         = ajAcdGetFloat("ogcmax");
    internal_oligo_salt_conc      = ajAcdGetFloat("osaltconc");
    internal_oligo_dna_conc       = ajAcdGetFloat("odnaconc");
    internal_oligo_self_any       = ajAcdGetFloat("oanyself");
    internal_oligo_self_end       = ajAcdGetFloat("oendself");
    internal_oligo_max_poly_x     = ajAcdGetInt("opolyxmax");
    internal_oligo_mishyb_library = ajAcdGetInfile("mishyblibraryfile");
    internal_oligo_max_mishyb     = ajAcdGetFloat("omishybmax");
    /*
       internal_oligo_min_quality    = ajAcdGetInt("oligominquality");
    */

    /* Internal Oligo penalties */
    /* these are not (yet) implemented */
    

    /* EMBOSS-wrapper-specific stuff */
    outfile = ajAcdGetOutfile("outfile");
    
    
    ajStrRemoveWhite(&left_input);
    ajStrRemoveWhite(&right_input);

    /*
    ** OK - we will now try to do a separate fork-exec for each sequence.
    */

    result = ajStrNew();

    while(ajSeqallNext(sequence, &seq))
    {
        program = ajAcdGetpathC("primer32_core");

        if(!ajSysExecRedirectC(ajStrGetPtr(program),&pipeto,&pipefrom))
            ajFatal("eprimer32: Could not exec primer32_core");
        
        stream = eprimer32_start_write(pipeto[1]);
        
        /* send primer3 Primer "Global" parameters */
        eprimer32_send_bool(stream, "PRIMER_EXPLAIN_FLAG", explain_flag);
        eprimer32_send_bool(stream, "P3_FILE_FLAG", file_flag);
        
        if(do_hybrid)
        {
            if(!ajStrCmpC(task[0], "1"))
                ajStrAssignC(&taskstr, "pick_pcr_primers_and_hyb_probe");
            else if(!ajStrCmpC(task[0], "2"))
                ajStrAssignC(&taskstr, "pick_left_only");
            else if(!ajStrCmpC(task[0], "3"))
                ajStrAssignC(&taskstr, "pick_right_only");
            else if(!ajStrCmpC(task[0], "4"))
                ajStrAssignC(&taskstr, "pick_hyb_probe_only");
        
            if (!do_primer)
                ajStrAssignC(&taskstr, "pick_hyb_probe_only");
        }
        else
        {
            if(!ajStrCmpC(task[0], "1"))
                ajStrAssignC(&taskstr, "pick_pcr_primers");
            else if(!ajStrCmpC(task[0], "2"))
                ajStrAssignC(&taskstr, "pick_left_only");
            else if(!ajStrCmpC(task[0], "3"))
                ajStrAssignC(&taskstr, "pick_right_only");
            else if(!ajStrCmpC(task[0], "4"))
                ajStrAssignC(&taskstr, "pick_hyb_probe_only");
        }
        
        eprimer32_send_string(stream, "PRIMER_TASK", taskstr);
        eprimer32_send_int(stream, "PRIMER_NUM_RETURN", num_return);
        eprimer32_send_int(stream, "PRIMER_FIRST_BASE_INDEX", 
                           1);
        eprimer32_send_bool(stream, "PRIMER_PICK_ANYWAY", pick_anyway);
        
        /* mispriming library may not have been specified */
        if(mispriming_library)
            eprimer32_send_stringC(stream, "PRIMER_MISPRIMING_LIBRARY",
                                   ajFileGetPrintnameC(mispriming_library));
    
        eprimer32_send_float(stream, "PRIMER_MAX_LIBRARY_MISPRIMING", 
                             max_mispriming);
        eprimer32_send_float(stream, "PRIMER_PAIR_MAX_LIBRARY_MISPRIMING",
                             pair_max_mispriming);
        eprimer32_send_int(stream, "PRIMER_GC_CLAMP", gc_clamp);
        eprimer32_send_int(stream, "PRIMER_OPT_SIZE", opt_size);
        eprimer32_send_int(stream, "PRIMER_MIN_SIZE", min_size);
        eprimer32_send_int(stream, "PRIMER_MAX_SIZE", max_size);
        eprimer32_send_float(stream, "PRIMER_OPT_TM", opt_tm);
        eprimer32_send_float(stream, "PRIMER_MIN_TM", min_tm);
        eprimer32_send_float(stream, "PRIMER_MAX_TM", max_tm);
        eprimer32_send_float(stream, "PRIMER_MAX_DIFF_TM", max_diff_tm);
        eprimer32_send_float(stream, "PRIMER_OPT_GC_PERCENT", 
                             opt_gc_percent);
        eprimer32_send_float(stream, "PRIMER_MIN_GC", min_gc);
        eprimer32_send_float(stream, "PRIMER_MAX_GC", max_gc);
        eprimer32_send_float(stream, "PRIMER_SALT_MONOVALENT", salt_conc);
        eprimer32_send_float(stream, "PRIMER_DNA_CONC", dna_conc);
        eprimer32_send_int(stream, "PRIMER_MAX_NS_ACCEPTED", 
                           num_ns_accepted);
        eprimer32_send_float(stream, "PRIMER_MAX_SELF_ANY", self_any);
        eprimer32_send_float(stream, "PRIMER_MAX_SELF_END", self_end);
        eprimer32_send_int(stream, "PRIMER_TM_FORMULA", tm_formula);
        eprimer32_send_int(stream, "PRIMER_SALT_CORRECTIONS", salt_corr);
        eprimer32_send_int(stream, "PRIMER_MAX_POLY_X", max_poly_x);
        eprimer32_send_int(stream, "PRIMER_PRODUCT_OPT_SIZE", 
                           product_opt_size);
        eprimer32_send_range2(stream, "PRIMER_PRODUCT_SIZE_RANGE",
                              product_size_range);
        eprimer32_send_float(stream, "PRIMER_PRODUCT_OPT_TM", 
                             product_opt_tm);
        eprimer32_send_float(stream, "PRIMER_PRODUCT_MIN_TM", 
                             product_min_tm);
        eprimer32_send_float(stream, "PRIMER_PRODUCT_MAX_TM", 
                             product_max_tm);
        eprimer32_send_float(stream, "PRIMER_MAX_END_STABILITY",
                             max_end_stability);
    
        /* send primer3 Internal Oligo "Global" parameters */
        eprimer32_send_int(stream, "PRIMER_INTERNAL_OPT_SIZE",
                           internal_oligo_opt_size);
        eprimer32_send_int(stream, "PRIMER_INTERNAL_MIN_SIZE",
                           internal_oligo_min_size);
        eprimer32_send_int(stream, "PRIMER_INTERNAL_MAX_SIZE",
                           internal_oligo_max_size);
        eprimer32_send_float(stream, "PRIMER_INTERNAL_OPT_TM",
                             internal_oligo_opt_tm);
        eprimer32_send_float(stream, "PRIMER_INTERNAL_MIN_TM",
                             internal_oligo_min_tm);
        eprimer32_send_float(stream, "PRIMER_INTERNAL_MAX_TM",
                             internal_oligo_max_tm);
        eprimer32_send_float(stream, "PRIMER_INTERNAL_OPT_GC_PERCENT",
                             internal_oligo_opt_gc_percent);
        eprimer32_send_float(stream, "PRIMER_INTERNAL_MIN_GC",
                             internal_oligo_min_gc);
        eprimer32_send_float(stream, "PRIMER_INTERNAL_MAX_GC",
                             internal_oligo_max_gc);
        eprimer32_send_float(stream, "PRIMER_INTERNAL_SALT_MONOVALENT",
                             internal_oligo_salt_conc);
        eprimer32_send_float(stream, "PRIMER_INTERNAL_DNA_CONC",
                             internal_oligo_dna_conc);
        eprimer32_send_float(stream, "PRIMER_INTERNAL_MAX_SELF_ANY",
                             internal_oligo_self_any);
        eprimer32_send_float(stream, "PRIMER_INTERNAL_MAX_SELF_END",
                             internal_oligo_self_end);
        eprimer32_send_int(stream, "PRIMER_INTERNAL_MAX_POLY_X",
                           internal_oligo_max_poly_x);

        /* 
        ** internal oligo mishybridising library may not have been
        ** specified 
        */
        if(internal_oligo_mishyb_library)
            eprimer32_send_stringC(stream,
                         "PRIMER_INTERNAL_MISHYB_LIBRARY",
                         ajFileGetPrintnameC(internal_oligo_mishyb_library));

        eprimer32_send_float(stream, "PRIMER_INTERNAL_MAX_LIBRARY_MISHYB",
                             internal_oligo_max_mishyb);
        
        
        /* 
        ** Start sequence-specific stuff 
        */

        begin = ajSeqallGetseqBegin(sequence) - 1;
        end   = ajSeqallGetseqEnd(sequence) - 1;

        strand = ajSeqGetSeqCopyS(seq);

        ajStrFmtUpper(&strand);
        ajStrAssignSubC(&substr,ajStrGetPtr(strand), begin, end);

        /* send flags to turn on using optimal product size */
        eprimer32_send_float(stream, "PRIMER_PAIR_WT_PRODUCT_SIZE_GT",
                             (float)0.05);
        eprimer32_send_float(stream, "PRIMER_PAIR_WT_PRODUCT_SIZE_LT",
                             (float)0.05);

        /* send primer3 Primer "Sequence" parameters */
        eprimer32_send_string(stream, "SEQUENCE_TEMPLATE", substr);

        /* if no ID name, use the USA */
        if(ajStrMatchC(ajSeqGetNameS(seq),""))
            eprimer32_send_string(stream, "SEQUENCE_ID",
                                  ajSeqGetUsaS(seq));
        else
            eprimer32_send_string(stream, "PRIMER_SEQUENCE_ID",
                                  ajSeqGetNameS(seq));

        eprimer32_send_range(stream, "SEQUENCE_INCLUDED_REGION",
                             included_region, begin);
        eprimer32_send_range(stream, "SEQUENCE_TARGET", target, begin);
        eprimer32_send_range(stream, "SEQUENCE_EXCLUDED_REGION",
                             excluded_region, begin);
        eprimer32_send_string(stream, "SEQUENCE_PRIMER", left_input);
        eprimer32_send_string(stream, "SEQUENCE_PRIMER_REVCOMP", right_input);

        /* send primer3 Internal Oligo "Sequence" parameters */
        eprimer32_send_range(stream,
                            "SEQUENCE_INTERNAL_EXCLUDED_REGION",
                            internal_oligo_excluded_region, begin);
        eprimer32_send_string(stream, "SEQUENCE_INTERNAL_OLIGO",
                             internal_oligo_input);
        eprimer32_send_range_pair(stream, "SEQUENCE_PRIMER_PAIR_OK_REGION_LIST",
                                  okleft_region, okright_region, begin);


        /* end the primer3 input sequence record with a '=' */
        eprimer32_send_end(stream);
        /* and close the ouput pipe stream */
        eprimer32_end_write(stream);
    
        /* read the primer3 output */
        eprimer32_read(pipefrom[0], &result);
            
        eprimer32_report(outfile, result, num_return, begin);
    
        ajStrSetClear(&result);

#ifndef WIN32
        close(pipeto[1]);
        close(pipefrom[0]);
#endif            
    }	/* end of sequence loop */


    ajStrDel(&result);
    ajSeqDel(&seq);
    ajStrDel(&strand);
    ajStrDel(&substr);
    ajFileClose(&outfile);
    ajStrDel(&taskstr);
    ajStrDel(&scstr);
    ajStrDel(&tmfstr);
    ajStrDelarray(&task);

    ajSeqallDel(&sequence);
    ajSeqDel(&seq);

    ajRangeDel(&included_region);
    ajRangeDel(&target);
    ajRangeDel(&excluded_region);
    ajRangeDel(&product_size_range);
    ajRangeDel(&internal_oligo_excluded_region);
    ajRangeDel(&okleft_region);
    ajRangeDel(&okright_region);

    ajStrDel(&left_input);
    ajStrDel(&right_input);
    ajStrDel(&internal_oligo_input);

    AJFREE(pipeto);
    AJFREE(pipefrom);
    
    ajFileClose(&mispriming_library);

    embExit();

    return 0;
}




/* @funcstatic eprimer32_read *************************************************
**
** Reads the output from primer3_core into a returned AjPStr until EOF
**
** @param [r] fd [int] file descriptor
** @param [u] result [AjPStr*] Returned string
** @return [void]
**
******************************************************************************/

static void eprimer32_read(int fd, AjPStr* result)
{
    FILE *stream;
    int ch;

    ajDebug("reading primer3_core output\n");

    if((stream = fdopen(fd, "r")) == NULL)
        ajFatal("fdopen() read error");

    while((ch = getc( stream )) != EOF)
        ajStrAppendK(result, ch);

    ajDebug("primer3_core output:\n%S\n", *result);


    fclose(stream);
    ajDebug("reading done\n");

    return;
}




/* @funcstatic eprimer32_send_end *********************************************
**
** Writes the end-of-input flag '=' to the input stream of primer3_core
**
** @param [u] stream [FILE *] File handle
** @return [void]
**
******************************************************************************/

static void eprimer32_send_end(FILE * stream)
{
    fputs( "=\n", stream );

    return;
}




/* @funcstatic eprimer32_send_range *******************************************
**
** Write range data to primer3_core as 'start,length start2,length2,etc'
**
** @param [u] stream [FILE *] File handle
** @param [r] tag [const char *] Tag of primer3 data type
** @param [r] value [const AjPRange] Ranges to write
** @param [r] begin [ajint] Start position of subsequence (-sbegin)
** @return [void]
**
******************************************************************************/

static void eprimer32_send_range(FILE * stream, const char * tag,
                                 const AjPRange value, 
                                 ajint begin)
{
    AjPStr str;
    ajuint n;
    ajuint start;
    ajuint end;

    str = ajStrNew();

    if(ajRangeGetSize(value))
    {
        ajFmtPrintS(&str, "%s=", tag);
        eprimer32_write(str, stream);
        ajStrSetClear(&str);

        for(n=0; n < ajRangeGetSize(value); n++)
        {
            ajRangeElementGetValues(value, n, &start, &end);
            start -= begin;
            end   -= begin;
            ajFmtPrintS(&str, "%d,%d ", start, end-start+1);
            eprimer32_write(str, stream);
            ajStrSetClear(&str);
        }

        ajFmtPrintS(&str, "\n");
        eprimer32_write(str, stream);
    }

    ajStrDel(&str);

    return;
}




/* @funcstatic eprimer32_send_range2 ******************************************
**
** Write alternate display of ranges as 'a-b c-d' to primer3_core
**
** @param [u] stream [FILE *] File handle
** @param [r] tag [const char *] Tag of primer3 data type
** @param [r] value [const AjPRange] Ranges to write
** @return [void]
**
******************************************************************************/

static void eprimer32_send_range2(FILE * stream, const char * tag,
                                  const AjPRange value)
{
    AjPStr str;
    ajuint n;
    ajuint start;
    ajuint end;

    str=ajStrNew();

    if(ajRangeGetSize(value))
    {
        ajFmtPrintS(&str, "%s=", tag);
        eprimer32_write(str, stream);
        ajStrSetClear(&str);

        for(n=0; n < ajRangeGetSize(value); n++)
        {
            ajRangeElementGetValues(value, n, &start, &end);
            ajFmtPrintS(&str, "%d-%d ", start, end);
            eprimer32_write(str, stream);
            ajStrSetClear(&str);
        }

        ajFmtPrintS(&str, "\n");
        eprimer32_write(str, stream);
    }

    ajStrDel(&str);

    return;
}




/* @funcstatic eprimer32_send_range_pair **************************************
**
** Write range data to primer3_core as 'start,length start2,length2,etc'
**
** @param [u] stream [FILE *] File handle
** @param [r] tag [const char *] Tag of primer3 data type
** @param [r] avalue [const AjPRange] First ranges to write
** @param [r] bvalue [const AjPRange] Second ranges to write
** @param [r] begin [ajint] Start position of subsequence (-sbegin)
** @return [void]
**
******************************************************************************/

static void eprimer32_send_range_pair(FILE * stream, const char * tag,
                                      const AjPRange avalue, 
                                      const AjPRange bvalue, 
                                      ajint begin)
{
    AjPStr str;
    ajuint n = 0;
    ajuint start;
    ajuint end;
    ajuint asize;
    ajuint bsize;

    asize = ajRangeGetSize(avalue);
    bsize = ajRangeGetSize(bvalue);

    if(asize || bsize)
    {
        str = ajStrNew();

        ajFmtPrintS(&str, "%s=", tag);

        for(n=0; n < asize; n++)
        {
            if(n)
                ajFmtPrintAppS(&str,";");
            ajRangeElementGetValues(avalue, n, &start, &end);
            start -= begin;
            end   -= begin;
            ajFmtPrintAppS(&str, "%u,%u", start, end-start+1);
            if(n < bsize)
            {
                ajRangeElementGetValues(bvalue, n, &start, &end);
                start -= begin;
                end   -= begin;
                ajFmtPrintAppS(&str, ",%u,%u", start, end-start+1);
            }
            else 
            {
                ajFmtPrintAppS(&str, ",,");
            }
        }

        for(n=asize; n < bsize; n++)
        {
            if(n)
                ajFmtPrintAppS(&str,";");
            ajFmtPrintAppS(&str, ",,");
            ajRangeElementGetValues(bvalue, n, &start, &end);
            start -= begin;
            end   -= begin;
            ajFmtPrintAppS(&str, ",%u,%u", start, end-start+1);
        }

        if(ajStrGetLen(str))
        {
            ajFmtPrintAppS(&str, "\n");
            eprimer32_write(str, stream);
        }

        ajStrDel(&str);
    }

    return;
}




/* @funcstatic eprimer32_send_int *********************************************
**
** Write integer to primer32_core
**
** @param [u] stream [FILE *] File handle
** @param [r] tag [const char *] Tag of primer3 data type
** @param [r] value [ajint] Integer value to write
** @return [void]
**
******************************************************************************/

static void eprimer32_send_int(FILE * stream, const char * tag, ajint value)
{
    AjPStr str;

    str = ajStrNew();

    ajFmtPrintS(&str, "%s=%d\n", tag, value);
    eprimer32_write(str, stream);

    ajStrDel(&str);

    return;
}




/* @funcstatic eprimer32_send_float *******************************************
**
** Write float to primer32_core
**
** @param [u] stream [FILE *] File handle
** @param [r] tag [const char *] Tag of primer3 data type
** @param [r] value [float] Float value to write
** @return [void]
**
******************************************************************************/

static void eprimer32_send_float(FILE * stream, const char * tag, float value)
{
    AjPStr str;

    str = ajStrNew();

    ajFmtPrintS(&str, "%s=%f\n", tag, value);
    eprimer32_write(str, stream);

    ajStrDel(&str);

    return;
}




/* @funcstatic eprimer32_send_bool ********************************************
**
** Write boolean to primer32_core
**
** @param [u] stream [FILE *] File handle
** @param [r] tag [const char *] Tag of primer3 data type
** @param [r] value [AjBool] Boolean value to write
** @return [void]
**
******************************************************************************/

static void eprimer32_send_bool(FILE * stream, const char * tag, AjBool value)
{
    AjPStr str;

    str = ajStrNew();

    ajFmtPrintS(&str, "%s=%d\n", tag, value?1:0);
    eprimer32_write(str, stream);

    ajStrDel(&str);

    return;
}




/* @funcstatic eprimer32_send_string ******************************************
**
** Write string to primer32_core
**
**
** @param [u] stream [FILE *] File handle
** @param [r] tag [const char *] Tag of primer3 data type
** @param [r] value [const AjPStr] String value to write
** @return [void]
**
******************************************************************************/

static void eprimer32_send_string(FILE * stream, const char * tag,
                                  const AjPStr value)
{
    AjPStr str;

    str = ajStrNew();

    if(ajStrGetLen(value))
    {
        ajFmtPrintS(&str, "%s=%S\n", tag, value);
        eprimer32_write(str, stream);
    }

    ajStrDel(&str);

    return;
}




/* @funcstatic eprimer32_send_stringC *****************************************
**
** Write char * to primer32_core
**
** @param [u] stream [FILE*] File handle
** @param [r] tag [const char*] Tag of primer3 data type
** @param [r] value [const char*] Char * value to write
** @return [void]
**
******************************************************************************/

static void eprimer32_send_stringC(FILE *stream, const char *tag,
                                   const char *value)
{
    AjPStr str;

    str = ajStrNew();

    if(strlen(value))
    {
        ajFmtPrintS(&str, "%s=%s\n", tag, value);
        eprimer32_write(str, stream);
    }

    ajStrDel(&str);

    return;
}




/* @funcstatic eprimer32_start_write ******************************************
**
** Open a file descriptor as a stream to pipe to primer32_core
**
** @param [r] fd [int] File descriptor
** @return [FILE*] File stream
**
******************************************************************************/

static FILE* eprimer32_start_write(int fd)
{
    FILE *stream;

    ajDebug( "start writing\n");
    if((stream = fdopen( fd, "w" )) == NULL)
        ajFatal("Couldn't open pipe with fdopen()");

    return stream;
}




/* @funcstatic eprimer32_write ************************************************
**
** Write a tag=value AjPStr to the primer32_core input stream
**
** @param [r] str [const AjPStr] Input string
** @param [u] stream [FILE*] Stream piped to primer3_core
** @return [void]
**
******************************************************************************/

static void eprimer32_write(const AjPStr str, FILE *stream)
{
    fputs(ajStrGetPtr(str), stream);
    ajDebug("eprimer32_write '%S'\n", str);

    return;
}




/* @funcstatic eprimer32_end_write ********************************************
**
** Close the stream piping in to primer32_core
**
** @param [u] stream [FILE *] Stream
** @return [void]
**
******************************************************************************/

static void eprimer32_end_write(FILE *stream)
{
    ajDebug("Closing output pipe stream\n");
    fclose(stream);

    return;
}




/* @funcstatic eprimer32_report ***********************************************
**
** Read output of primer32_core into a temporary table of tag/value results
**
** @param [u] outfile [AjPFile] Report outfile
** @param [r] output [const AjPStr] Output from primer3_core
** @param [r] numreturn [ajint] Number of results to return for each sequence
** @param [r] begin [ajint] Start position of subsequence (-sbegin)
** @return [void]
**
******************************************************************************/

static void eprimer32_report(AjPFile outfile, const AjPStr output, 
                             ajint numreturn, ajint begin)
{
    AjPStr line = NULL;
    AjPStrTok linetokenhandle;
    char eol[] = "\n\r";
    AjPStrTok keytokenhandle;
    char equals[] = "=";
    AjPStr key   = NULL;
    AjPStr value = NULL;
    AjBool gotsequenceid = AJFALSE;
    AjPTable table;

    linetokenhandle = ajStrTokenNewC(output, eol);

    /* get next line of relevant results */
    while(ajStrTokenNextParseC(linetokenhandle, eol, &line))
    {
        if(!gotsequenceid)
        {
            /*
            ** Att the start of another sequence's results?
            ** Start storing the results in the table.
            */

            if(ajStrCmpLenC(line, "PRIMER_SEQUENCE_ID=", 19) == 0)
            {
                gotsequenceid = AJTRUE;
                table = ajTablestrNew(TABLEGUESS);

            }
            else
                continue;
        }
        else
        {
            /*
            ** At the end of this sequence? - marked by a '=' in the primer3
            ** output - then output the results.
            */
            if(ajStrCmpC(line, "=") == 0)
            {
                gotsequenceid = AJFALSE;
                eprimer32_output_report(outfile, table, numreturn, begin);
                ajTablestrFree(&table);
                continue;
            }
        }

        /*
        ** store key and value in table and parse values
        ** when have all of the sequence
        ** results in the table because the LEFT, RIGHT
        ** and INTERNAL results for each
        ** resulting primer are interleaved
        */

        keytokenhandle = ajStrTokenNewC(line, equals);

        key = ajStrNew();
        ajStrTokenNextParse(keytokenhandle, &key);

        value = ajStrNew();
        ajStrTokenNextParse(keytokenhandle, &value);

        ajDebug("key=%S\tvalue=%S\n", key, value);

        ajTablePut(table,(void *)key, (void *)value);

        ajStrTokenDel(&keytokenhandle);
    }

    ajStrDel(&line);
    ajStrTokenDel(&linetokenhandle);
    ajTablestrFree(&table);

    return;
}




/* @funcstatic eprimer32_output_report ****************************************
**
** Read the results out of the tag/value table and write to report
**
** @param [u] outfile [AjPFile] Report outfile
** @param [r] table [const AjPTable] Table of tag/value result pairs
** @param [r] numreturn [ajint] Number of results to return for each sequence
** @param [r] begin [ajint] Start position of subsequence (-sbegin)
** @return [void]
**
******************************************************************************/

static void eprimer32_output_report(AjPFile outfile, const AjPTable table,
                                    ajint numreturn, ajint begin)
{
    AjPStr key   = NULL;
    AjPStr explain    = NULL;
    AjPStr explainstr = NULL;
    const AjPStr error = NULL;		/*  value from table - do not delete */
    const AjPStr seqid      = NULL;
    ajint i;
    const AjPStr size = NULL;
    const AjPStr seq  = NULL;
    const AjPStr pos  = NULL;
    const AjPStr tm   = NULL;
    const AjPStr gc   = NULL;

     /*
     ** Typical primer3 output looks like:
     ** PRIMER_SEQUENCE_ID=em-id:HSFAU
     ** PRIMER_PAIR_PENALTY=0.1974
     ** PRIMER_LEFT_PENALTY=0.007073
     ** PRIMER_RIGHT_PENALTY=0.190341
     ** PRIMER_INTERNAL_OLIGO_PENALTY=0.132570
     ** PRIMER_LEFT_SEQUENCE=AGTCGCCAATATGCAGCTCT
     ** PRIMER_RIGHT_SEQUENCE=GGAGCACGACTTGATCTTCC
     ** PRIMER_INTERNAL_OLIGO_SEQUENCE=GGAGCTACACACCTTCGAGG
     ** PRIMER_LEFT=47,20
     ** PRIMER_RIGHT=183,20
     ** PRIMER_INTERNAL_OLIGO=80,20
     ** PRIMER_LEFT_TM=60.007
     ** PRIMER_RIGHT_TM=59.810
     ** PRIMER_INTERNAL_OLIGO_TM=59.867
     ** PRIMER_LEFT_GC_PERCENT=50.000
     ** PRIMER_RIGHT_GC_PERCENT=55.000
     ** PRIMER_INTERNAL_OLIGO_GC_PERCENT=60.000
     ** PRIMER_LEFT_SELF_ANY=4.00
     ** PRIMER_RIGHT_SELF_ANY=5.00
     ** PRIMER_INTERNAL_OLIGO_SELF_ANY=4.00
     ** PRIMER_LEFT_SELF_END=2.00
     ** PRIMER_RIGHT_SELF_END=1.00
     ** PRIMER_INTERNAL_OLIGO_SELF_END=4.00
     ** PRIMER_LEFT_END_STABILITY=7.9000
     ** PRIMER_RIGHT_END_STABILITY=8.2000
     ** PRIMER_PAIR_COMPL_ANY=5.00
     ** PRIMER_PAIR_COMPL_END=1.00
     ** PRIMER_PRODUCT_SIZE=137
     */
  
    /* check for errors */
    ajStrAssignC(&key, "PRIMER_ERROR");
    error = ajTableFetchS(table, key);
    if(error != NULL)
        ajErr("%S", error);

    ajStrAssignC(&key, "PRIMER_WARNING");
    error = ajTableFetchS(table, key);
    if(error != NULL)
        ajWarn("%S", error);

  
    /* get the sequence id */
    ajStrAssignC(&key, "PRIMER_SEQUENCE_ID");
    seqid = ajTableFetchS(table, key);
    ajFmtPrintF(outfile, "\n# EPRIMER32 RESULTS FOR %S\n\n", seqid);
  
    /* get information on the analysis */
    ajStrAssignC(&key, "PRIMER_LEFT_EXPLAIN");
    explain = ajTableFetchmodS(table, key);

    if(explain != NULL)
    {
        ajStrAssignS(&explainstr, explain);
        ajStrExchangeCC(&explainstr, ",", "\n#");
        ajFmtPrintF(outfile, "# FORWARD PRIMER STATISTICS:\n# %S\n\n",
                    explainstr);
    }
    ajStrAssignC(&key, "PRIMER_RIGHT_EXPLAIN");
    explain = ajTableFetchmodS(table, key);

    if(explain != NULL)
    {
        ajStrAssignS(&explainstr, explain);
        ajStrExchangeCC(&explainstr, ",", "\n#");
        ajFmtPrintF(outfile, "# REVERSE PRIMER STATISTICS:\n# %S\n\n",
                    explainstr);
    }
    ajStrAssignC(&key, "PRIMER_PAIR_EXPLAIN");
    explain = ajTableFetchmodS(table, key);

    if(explain != NULL)
    {
        ajStrAssignS(&explainstr, explain);
        ajStrExchangeCC(&explainstr, ",", "\n#");
        ajFmtPrintF(outfile, "# PRIMER PAIR STATISTICS:\n# %S\n\n",
                    explainstr);
    }
    ajStrAssignC(&key, "PRIMER_INTERNAL_EXPLAIN");
    explain = ajTableFetchmodS(table, key);

    if(explain != NULL)
    {
        ajStrAssignS(&explainstr, explain);
        ajStrExchangeCC(&explainstr, ",", "\n#");
        ajFmtPrintF(outfile, "# INTERNAL OLIGO STATISTICS:\n# %S\n\n",
                    explainstr);
    }
  
    /* table header */
    ajFmtPrintF(outfile,"#                      Start  Len   Tm     "
                "GC%%   Sequence\n\n");
  
    /* get the results */
    for(i=0; i <= numreturn; i++)
    {
        /* product data */
        size = eprimer32_tableget("PRIMER_PAIR", i, "PRODUCT_SIZE", table);
        if(size != NULL)
            ajFmtPrintF(outfile, "%4d PRODUCT SIZE: %S\n",
                        i+1, size);

        /* left primer data */
        tm = eprimer32_tableget("PRIMER_LEFT", i, "TM", table);
        gc = eprimer32_tableget("PRIMER_LEFT", i, "GC_PERCENT", table);
        pos = eprimer32_tableget("PRIMER_LEFT", i, "", table);
        seq = eprimer32_tableget("PRIMER_LEFT", i, "SEQUENCE", table);
        eprimer32_write_primer(outfile, "FORWARD PRIMER",
                               pos, tm, gc, seq, AJFALSE, begin);


        /* right primer data */
        tm = eprimer32_tableget("PRIMER_RIGHT", i, "TM", table);
        gc = eprimer32_tableget("PRIMER_RIGHT", i, "GC_PERCENT", table);
        pos = eprimer32_tableget("PRIMER_RIGHT", i, "", table);
        seq = eprimer32_tableget("PRIMER_RIGHT", i, "SEQUENCE", table);
        eprimer32_write_primer(outfile, "REVERSE PRIMER", 
                               pos, tm, gc, seq, AJTRUE, begin);


        /* internal oligo data */

        tm = eprimer32_tableget("PRIMER_INTERNAL_OLIGO", i, "TM", table);
        gc = eprimer32_tableget("PRIMER_INTERNAL_OLIGO", i, "GC_PERCENT",
                                table);
        pos = eprimer32_tableget("PRIMER_INTERNAL_OLIGO", i, "", table);
        seq = eprimer32_tableget("PRIMER_INTERNAL_OLIGO", i, "SEQUENCE", table);
        eprimer32_write_primer(outfile, "INTERNAL OLIGO",
                               pos, tm, gc, seq, AJFALSE, begin);

        ajFmtPrintF(outfile, "\n");

    }
  

    ajStrDel(&key);
    ajStrDel(&explainstr);
 
    return;
}




/* @funcstatic eprimer32_tableget *********************************************
**
** Read the results out of the tag/value table
**
** @param [r] key1 [const char *] First half of table key base string
** @param [r] number [ajint] Table key numeric part
** @param [r] key2 [const char *] Second half of table key base string
**                                (minus '_')
** @param [r] table [const AjPTable] Table of tag/value result pairs
** @return [const AjPStr] Table value
**
******************************************************************************/

static const AjPStr eprimer32_tableget(const char *key1, ajint number,
                                 const char *key2,
                                 const AjPTable table)
{
    AjPStr fullkey = NULL;
    AjPStr keynum  = NULL;
    const AjPStr value   = NULL;

    ajStrAssignC(&fullkey, key1);

    ajStrAppendC(&fullkey, "_");
    ajStrFromInt(&keynum, number);
    ajStrAppendS(&fullkey, keynum);

    if(strcmp(key2, ""))
        ajStrAppendC(&fullkey, "_");

    ajStrAppendC(&fullkey, key2);
    ajDebug("Constructed key=%S\n", fullkey);
    value = ajTableFetchS(table, fullkey);


    ajStrDel(&fullkey);
    ajStrDel(&keynum);

    return value;
}




/* @funcstatic eprimer32_write_primer *****************************************
**
** Write out one primer or oligo line to the output file
**
** @param [u] outfile [AjPFile] Report outfile
** @param [r] tag [const char *] Tag on output line
** @param [r] pos [const AjPStr] Start and length string
** @param [r] tm [const AjPStr] Tm of primer
** @param [r] gc [const AjPStr] GC% of primer
** @param [r] seq [const AjPStr] Sequence of primer
** @param [r] rev [AjBool] Sequence is the reverse-complement primer
** @param [r] begin [ajint] Start position of subsequence (-sbegin)
** @return [void]
**
******************************************************************************/

static void eprimer32_write_primer(AjPFile outfile, const char *tag,
                                   const AjPStr pos,
                                   const AjPStr tm, const AjPStr gc,
                                   const AjPStr seq,
                                   AjBool rev, ajint begin)
{
    ajint startint;
    ajint lenint;
    float tmfloat;
    float gcfloat;
    AjPStr start = NULL;
    AjPStr lenstr = NULL;
    ajlong comma;

    if(ajStrGetLen(pos))
    {
        ajStrToFloat(tm, &tmfloat);
        ajStrToFloat(gc, &gcfloat);
        comma = ajStrFindC(pos, ",");
        ajStrAssignS(&start, pos);
        ajStrCutRange(&start, comma, ajStrGetLen(start)-1);
        ajStrToInt(start, &startint);
        startint += begin;
        ajStrAssignS(&lenstr, pos);
        ajStrCutRange(&lenstr, 0, comma);
        ajStrToInt(lenstr, &lenint);
        if(rev)
            startint = startint - lenint + 1;

        ajFmtPrintF(outfile, "     %s  %6d %4d  %2.2f  %2.2f  %S\n\n",
                    tag, startint, lenint, tmfloat, gcfloat, seq);
    }


    ajStrDel(&start);
    ajStrDel(&lenstr);

    return;
}