1
2%{
3
4#include "dynfile.h"
5#include "dyna2.h"
6#include "labelmaster.h"
7#include "linesubs.h"
8#include "dynashadow.h"
9#include "dynadb.h"
10#include "dpimpl.h"
11#include "dbthread.h"
12#include "probal.h"
13#include "dynadebug.h"
14
15%}
16
17
18
19
20%{
21#include "dynafunc.h"
22
23
24
25void one_shot_aln_func(DYNFILE * dfp,GenericMatrix * gm,DPImplementation * dpi)
26{
27  FuncInfo * fi;
28  char * arg_str;
29  char * chainstr;
30
31
32  /*** prepare function information ***/
33
34
35  fi = FuncInfo_named_from_varstr(FI_CALLABLE,"PackAln_bestmemory_%s",gm->name);
36  add_line_to_Ftext(fi->ft,"This function chooses the best memory set-up for the alignment",gm->name);
37  add_line_to_Ftext(fi->ft,"using calls to basematrix, and then implements either a large");
38  add_line_to_Ftext(fi->ft,"or small memory model.");
39  add_break_to_Ftext(fi->ft);
40  add_line_to_Ftext(fi->ft,"It is the best function to use if you just want an alignment",gm->name);
41  add_break_to_Ftext(fi->ft);
42  add_line_to_Ftext(fi->ft,"If you want a label alignment, you will need");
43  add_line_to_Ftext(fi->ft,"/convert_PackAln_to_AlnBlock_%s",gm->name);
44
45  arg_str = get_argstr_GenericMatrix(gm);
46  add_args_GenericMatrix_FuncInfo(fi,gm);
47
48
49  start_function_FuncInfo(fi,dfp,"PackAln * PackAln_bestmemory_%s(%s,DPEnvelope * dpenv,DPRunImpl * dpri)",gm->name,arg_str);
50
51  /*** clean up ***/
52  ckfree(arg_str);
53  chainstr= get_chainstr_GenericMatrix(gm);
54  /** started function, now see what basematrix says about memory **/
55
56
57  expr(dfp,"long long total;");
58  expr(dfp,"%s * mat;",gm->name);
59  expr(dfp,"PackAln * out;");
60  expr(dfp,"DebugMatrix * de");
61  expr(dfp,"DPRunImplMemory strategy;");
62
63  expr(dfp,"assert(dpri);");
64  add_break(dfp);
65
66  expr(dfp,"total = %s->%s * %s->%s",gm->query->name,gm->query_len,gm->target->name,gm->target_len);
67
68  expr(dfp,"if( dpri->memory == DPIM_Default ) ");
69  startbrace(dfp);
70  expr(dfp,"if( (total * %d * sizeof(int)) > 1000*dpri->kbyte_size)",gm->len);
71  startbrace(dfp);
72  expr(dfp,"strategy = DPIM_Linear;");
73  closebrace(dfp);
74  expr(dfp,"else");
75  startbrace(dfp);
76  expr(dfp,"strategy = DPIM_Explicit;");
77  closebrace(dfp);
78  closebrace(dfp);
79  expr(dfp,"else");
80  startbrace(dfp);
81  expr(dfp,"strategy = dpri->memory;");
82  closebrace(dfp);
83  add_break(dfp);
84
85  expr(dfp,"if( dpenv != NULL )");
86  startbrace(dfp);
87   expr(dfp,"if( strategy == DPIM_Explicit)");
88   startbrace(dfp);
89    expr(dfp,"if( (mat=allocate_Expl_%s(%s,dpri)) == NULL )",gm->name,chainstr);
90    startbrace(dfp);
91    warn_expr(dfp,"Unable to allocate large %s version",gm->name);
92    expr(dfp,"return NULL;");
93    closebrace(dfp);
94    expr(dfp,"calculate_dpenv_%s(mat,dpenv)",gm->name);
95    expr(dfp,"out =  PackAln_read_Expl_%s(mat);",gm->name);
96   closebrace(dfp);
97   expr(dfp,"else");
98   startbrace(dfp);
99   expr(dfp,"mat = allocate_%s_only(%s)",gm->name,chainstr);
100   expr(dfp,"calculate_shatter_%s(mat,dpenv)",gm->name);
101   expr(dfp,"out = PackAln_read_Shatter_%s(mat)",gm->name);
102   closebrace(dfp);
103  closebrace(dfp);
104  expr(dfp,"else");
105  startbrace(dfp);
106  expr(dfp,"if( strategy == DPIM_Linear )",gm->len);
107  startbrace(dfp);
108  add_block_comment(dfp,"use small implementation");
109  expr(dfp,"if( (mat=allocate_Small_%s(%s)) == NULL )",gm->name,chainstr);
110  startbrace(dfp);
111  warn_expr(dfp,"Unable to allocate small %s version",gm->name);
112  expr(dfp,"return NULL;");
113  closebrace(dfp);
114  expr(dfp,"out = PackAln_calculate_Small_%s(mat,dpenv)",gm->name);
115  closebrace(dfp);
116  expr(dfp,"else");
117  startbrace(dfp);
118  add_block_comment(dfp,"use Large implementation");
119  expr(dfp,"if( (mat=allocate_Expl_%s(%s,dpri)) == NULL )",gm->name,chainstr);
120  startbrace(dfp);
121  warn_expr(dfp,"Unable to allocate large %s version",gm->name);
122  expr(dfp,"return NULL;");
123  closebrace(dfp);
124  expr(dfp,"if( dpri->debug == TRUE)");
125  startbrace(dfp);
126  if( dpi->dydebug == TRUE ) {
127    expr(dfp,"de = create_debug_%s();",gm->name);
128    expr(dfp,"de->matrix = (void*) mat;");
129    expr(dfp,"explicit_debug_%s(de,0);",gm->name);
130    expr(dfp,"out =  PackAln_read_Expl_%s(mat);",gm->name);
131
132    expr(dfp,"if( dpri->paldebug == TRUE ) ");
133    hang_expr(dfp,"show_PackAln_Debug(de,out,stdout);");
134    expr(dfp,"free_DebugMatrix(de);");
135  } else {
136    expr(dfp,"fatal(\"Asked for dydebug, but dynamite file not compiled with -g. Need to recompile dynamite source\");");
137  }
138  closebrace(dfp);
139  expr(dfp,"else");
140  startbrace(dfp);
141  expr(dfp,"calculate_%s(mat)",gm->name);
142  expr(dfp,"out =  PackAln_read_Expl_%s(mat);",gm->name);
143  closebrace(dfp);
144
145  closebrace(dfp);
146  closebrace(dfp);
147
148  add_break(dfp);
149
150  expr(dfp,"mat = free_%s(mat)",gm->name);
151
152  expr(dfp,"return out;");
153
154
155  close_function(dfp);
156
157  add_break(dfp);
158
159  ckfree(chainstr);
160}
161
162%func
163makes the recalculate packaln function
164%%
165void recalculate_PackAln_func(DYNFILE * dfp,const GenericMatrix * gm)
166{
167  FuncInfo * fi;
168  /* ArgInfo  * ai; */
169  int i;
170  int j;
171
172  fi = FuncInfo_named_from_varstr(FI_CALLABLE,"recalculate_PackAln_%s",gm->name);
173  add_line_to_Ftext(fi->ft,"This function recalculates the PackAln structure produced by %s",gm->name);
174  add_line_to_Ftext(fi->ft,"For example, in linear space methods this is used to score them",gm->name);
175
176  start_function_FuncInfo(fi,dfp,"void recalculate_PackAln_%s(PackAln * pal,%s * mat)",gm->name,gm->name);
177
178  expr(dfp,"int i,j,k,offi,offj;");
179  expr(dfp,"PackAlnUnit * prev;");
180  expr(dfp,"PackAlnUnit * pau;");
181  add_break(dfp);
182
183
184  expr(dfp,"for(k=1,prev=pal->pau[0];k < pal->len;k++,prev=pau)");
185  startbrace(dfp);
186  expr(dfp,"pau = pal->pau[k];");
187  expr(dfp,"i = pau->i;");
188  expr(dfp,"j = pau->j;");
189  expr(dfp,"offi = pau->i - prev->i");
190  expr(dfp,"offj = pau->j - prev->j");
191  expr(dfp,"switch(pau->state)");
192  startbrace(dfp);
193  for(i=0;i<gm->len;i++) {
194    expr(dfp,"case %s : ",gm->state[i]->name);
195    startcase(dfp);
196
197
198    /*** about to move to if statements... ***/
199
200
201    for(j=0;j<gm->state[i]->len;j++) {
202      if( gm->state[i]->source[j]->isspecial == TRUE)
203	expr(dfp,"if( offj == %d && prev->state == (%s+%d) )",gm->state[i]->source[j]->offj,gm->state[i]->source[j]->state_source,gm->len);
204      else
205	expr(dfp,"if( offi == %d && offj == %d && prev->state == %s )",gm->state[i]->source[j]->offi,gm->state[i]->source[j]->offj,gm->state[i]->source[j]->state_source);
206      startbrace(dfp);
207      expr(dfp,"pau->score = %s + (%s)",gm->state[i]->source[j]->calc_expr,gm->state[i]->calc_expr == NULL ? "0" : gm->state[i]->calc_expr);
208      expr(dfp,"continue;");
209      closebrace(dfp);
210    }
211    expr(dfp,"warn(\"In recaluclating PackAln with state %s, from [%%d,%%d,%%d], got a bad source state. Error!\",offi,offj,prev->state);",gm->state[i]->name);
212    expr(dfp,"break;");
213    closecase(dfp);
214  }
215
216  for(i=0;i<gm->spec_len;i++) {
217    expr(dfp,"case (%s+%d) : ",gm->special[i]->name,gm->len);
218    startcase(dfp);
219    for(j=0;j<gm->special[i]->len;j++) {
220      if( gm->special[i]->source[j]->isspecial == TRUE)
221	expr(dfp,"if( offj == %d && prev->state == (%s+%d) )",gm->special[i]->source[j]->offj,gm->special[i]->source[j]->state_source,gm->len);
222      else
223	expr(dfp,"if( offj == %d && prev->state == %s )",gm->special[i]->source[j]->offj,gm->special[i]->source[j]->state_source);
224      startbrace(dfp);
225
226      if( gm->special[i]->source[j]->isspecial == FALSE) {
227	add_block_comment(dfp,"i here comes from the previous state ;) - not the real one");
228	expr(dfp,"i = prev->i;");
229      }
230      expr(dfp,"pau->score = %s + (%s)",gm->special[i]->source[j]->calc_expr,gm->special[i]->calc_expr == NULL ? "0" : gm->special[i]->calc_expr);
231      expr(dfp,"continue;");
232      closebrace(dfp);
233    }
234    warn_expr(dfp,"In recaluclating PackAln with state %s, got a bad source state. Error!",gm->special[i]->name);
235    expr(dfp,"break;");
236    closecase(dfp);
237  }
238
239  expr(dfp,"default : ");
240  startcase(dfp);
241  warn_expr(dfp,"In recaluclating PackAln got a bad recipient state. Error!");
242  closecase(dfp);
243  closebrace(dfp); /* end of switch on pau->state */
244  expr(dfp,"prev = pau;");
245  closebrace(dfp);
246
247  expr(dfp,"return;");
248  close_function(dfp);
249}
250
251
252
253
254
255
256
257%func
258makes the allocate_Expl_xxx function.
259This calls allocate_xxx_only function
260(made by /write_safe_alloc_function)
261and then allocates basematrix stuff as well.
262%arg
263dfp dynamite file pointer
264gm  r generic matrix structure
265%%
266void alloc_expl_func_GenericMatrix(DYNFILE * dfp,const GenericMatrix * gm)
267{
268  FuncInfo * fi;
269  char * arg_str;
270  char * chain_str;
271
272
273  /*** prepare function information ***/
274
275
276  fi = FuncInfo_named_from_varstr(FI_CALLABLE,"allocate_Expl_%s",gm->name);
277  add_line_to_Ftext(fi->ft,"This function allocates the %s structure",gm->name);
278  add_line_to_Ftext(fi->ft,"and the basematrix area for explicit memory implementations");
279  add_line_to_Ftext(fi->ft,"It calls /allocate_%s_only",gm->name);
280
281  arg_str = get_argstr_GenericMatrix(gm);
282  add_args_GenericMatrix_FuncInfo(fi,gm);
283
284
285
286
287  start_function_FuncInfo(fi,dfp,"%s * allocate_Expl_%s(%s,DPRunImpl * dpri)",gm->name,gm->name,arg_str);
288
289
290  /*** clean up ***/
291  ckfree(arg_str);
292
293
294
295  /*** into function body ***/
296
297
298  expr(dfp,"%s * out;",gm->name);
299
300  add_break(dfp);
301  chain_str = get_chainstr_GenericMatrix(gm);
302  expr(dfp,"out = allocate_%s_only(%s);",gm->name,chain_str);
303  ckfree(chain_str);
304
305  expr(dfp,"if( out == NULL )");
306  hang_expr(dfp,"return NULL;");
307
308  expr(dfp,"if( dpri->should_cache == TRUE )");
309  startbrace(dfp);
310  expr(dfp,"if( dpri->cache != NULL )");
311  startbrace(dfp);
312  expr(dfp,"if( dpri->cache->maxleni >= (out->lenj+%d)*%d && dpri->cache->maxlenj >= (out->leni+%d))",gm->window_j,gm->len,gm->window_i);
313  hang_expr(dfp,"out->basematrix = hard_link_BaseMatrix(dpri->cache)");
314  expr(dfp,"else");
315  hang_expr(dfp,"dpri->cache = free_BaseMatrix(dpri->cache)");
316  closebrace(dfp);
317  closebrace(dfp);
318
319  expr(dfp,"if( out->basematrix == NULL )");
320  startbrace(dfp);
321  expr(dfp,"if( (out->basematrix = BaseMatrix_alloc_matrix_and_specials((out->lenj+%d)*%d,(out->leni+%d),%d,out->lenj+%d)) == NULL)",gm->window_j,gm->len,gm->window_i,gm->spec_len,gm->window_j);
322  startbrace(dfp);
323  expr(dfp,"warn(\"Explicit matrix %s cannot be allocated, (asking for %%d by %%d main cells)\",out->leni,out->lenj);",gm->name);
324  expr(dfp,"free_%s(out)",gm->name);
325  expr(dfp,"return NULL;");
326  closebrace(dfp);
327  closebrace(dfp);
328
329  expr(dfp,"if( dpri->should_cache == TRUE && dpri->cache == NULL)");
330  hang_expr(dfp,"dpri->cache = hard_link_BaseMatrix(out->basematrix)");
331
332  expr(dfp,"out->basematrix->type = BASEMATRIX_TYPE_EXPLICIT;");
333  expr(dfp,"init_%s(out)",gm->name);
334
335  expr(dfp,"return out");
336
337  close_function(dfp);
338
339  add_break(dfp);
340
341}
342
343
344
345%func
346produces the allocate_xxx_only function,
347which allocates the matrix structure, checks
348resources which it can check, but does NOT
349allocate basematrix area
350
351This function will be called by allocate_Expl_xxxx
352and allocate_Small_xxxx etc.
353%arg
354dfp dynmaite file pointer to func/head
355gm generic matrix structure
356%%
357void write_safe_alloc_function(DYNFILE * dfp,GenericMatrix * gm)
358{
359  int i;
360  FuncInfo * fi;
361  char * arg_str;
362
363  fi = FuncInfo_named_from_varstr(FI_INTERNAL,"allocate_%s_only",gm->name);
364
365  add_line_to_Ftext(fi->ft,"This function only allocates the %s structure",gm->name);
366  add_line_to_Ftext(fi->ft,"checks types where possible and determines leni and lenj");
367  add_line_to_Ftext(fi->ft,"The basematrix area is delt with elsewhere");
368
369  arg_str = get_argstr_GenericMatrix(gm);
370  add_args_GenericMatrix_FuncInfo(fi,gm);
371
372
373  start_function_FuncInfo(fi,dfp,"%s * allocate_%s_only(%s)",gm->name,gm->name,arg_str);
374
375  /*** clean up ***/
376  ckfree(arg_str);
377
378
379  expr(dfp,"%s * out",gm->name); add_break(dfp);
380
381  expr(dfp,"if((out= %s_alloc()) == NULL)",gm->name);
382  startbrace(dfp);
383  warn_expr(dfp,"Allocation of basic %s structure failed...",gm->name);
384  expr(dfp,"return NULL;");
385  closebrace(dfp);
386
387  add_break(dfp);
388
389  expr(dfp,"out->%s = %s",gm->query->name,gm->query->name);
390  expr(dfp,"out->%s = %s",gm->target->name,gm->target->name);
391
392  for(i=0;i<gm->res_len;i++) {
393    expr(dfp,"out->%s = %s",gm->resource[i]->name,gm->resource[i]->name);
394  }
395
396  expr(dfp,"out->leni = %s->%s",gm->query->name,gm->query_len);
397  expr(dfp,"out->lenj = %s->%s",gm->target->name,gm->target_len);
398
399  expr(dfp,"return out");
400
401  close_function(dfp);
402  add_break(dfp);
403}
404
405
406%func
407Information partner to /get_argstr_GenericMatrix.
408Loads up the arglist information for the GenericMatrix,
409ie query-type query-name "what information" etc.
410
411%arg
412fi w FuncInfo structure to add ArgInfo to
413gm r generic matrix structure to read from
414%%
415boolean add_args_GenericMatrix_FuncInfo(FuncInfo * fi,const GenericMatrix * gm)
416{
417  int i;
418  ArgInfo * ai;
419
420  /** ArgInfo_in_FuncInfo_from_varstr automatically adds ai to fi **/
421
422  ai =  ArgInfo_in_FuncInfo_from_varstr(fi,gm->query->name);
423  ai->desc=stringalloc("query data structure");
424
425  ai =  ArgInfo_in_FuncInfo_from_varstr(fi,gm->target->name);
426  ai->desc=stringalloc("target data structure");
427
428
429  for(i=0;i<gm->res_len;i++) {
430    ai =  ArgInfo_in_FuncInfo_from_varstr(fi,gm->resource[i]->name);
431    ai->desc=stringalloc("Resource");
432  }
433
434  return TRUE;
435}
436
437%func
438makes an the argument calling string which
439is compatible with the arg_str from
440/get_argstr_GenericMatrix
441
442eg "query,target,comp_mat"
443%arg
444gm r structure holding generic matrix
445return allocated string (must free) with chained-args
446%%
447char * get_chainstr_GenericMatrix(const GenericMatrix * gm)
448{
449  char buffer[MAXLINE];
450  int i;
451
452  sprintf(buffer,"%s, %s ",gm->query->name,gm->target->name);
453
454  for(i=0;i<gm->res_len;i++) {
455    strcat(buffer,", ");
456    strcat(buffer,gm->resource[i]->name);
457  }
458
459  return stringalloc(buffer);
460
461}
462
463%func
464makes the argument list for a generic matrix alloc, ie
465query-type query-name,target-type target-name etc
466
467To load up info, use /add_arg_GenericMatrix_FuncInfo
468To chain up along function calls, use /get_chainstr_GenericMatrix
469%arg
470gm r structure holding generic matrix
471return allocated string (must free) with args
472%%
473char * get_argstr_GenericMatrix(const GenericMatrix * gm)
474{
475  char buffer[MAXLINE];
476  int i;
477
478  sprintf(buffer,"%s %s,%s %s ",gm->query->element_type,gm->query->name,gm->target->element_type,gm->target->name);
479
480  for(i=0;i<gm->res_len;i++) {
481    strcat(buffer,",");
482    strcat(buffer,gm->resource[i]->element_type);
483    strcat(buffer," ");
484    strcat(buffer,gm->resource[i]->name);
485  }
486
487  return stringalloc(buffer);
488}
489
490void write_dotter_dump(DYNFILE * dfp,GenericMatrix * gm)
491{
492
493  start_function(dfp,"void dump_dotter_%s(%s * mat,FILE * ofp)",gm->name,gm->name);
494
495  expr(dfp,"register int i");
496  expr(dfp,"register int j");
497  expr(dfp,"register int k");
498  expr(dfp,"int hlen");
499  expr(dfp,"int vlen");
500  expr(dfp,"char form = 1;");
501  expr(dfp,"int zoom = 1;");
502  expr(dfp,"register int max = (-1000000)");
503
504/***
505  expr(dfp,"fprintf(ofp,\"fileformat 1\\nzoomfactor 10\\nhorizontal_len %%d\\nvertical_len %%d\\n\",mat->%s->%s + (4-((mat->%s->%s)%%4)),mat->%s->%s + (4-((mat->%s->%s)%%4)));",
506gm->query->name,gm->query_len,gm->query->name,gm->query_len,gm->target->name,gm->target_len,gm->target->name,gm->target_len);
507***/
508
509
510  expr(dfp,"hlen = mat->%s->%s",gm->query->name,gm->query_len);
511  expr(dfp,"if( hlen%%4 != 0 ) ");
512  hang_expr(dfp,"hlen += 4 - hlen%%4;");
513
514  expr(dfp,"vlen = mat->%s->%s",gm->target->name,gm->target_len);
515  expr(dfp,"if( vlen%%4 != 0 ) ");
516  hang_expr(dfp,"vlen += 4 - hlen%%4;");
517
518  expr(dfp,"fwrite(&form,sizeof(char),1,ofp)");
519  expr(dfp,"fwrite(&zoom,sizeof(int),1,ofp)");
520  expr(dfp,"fwrite(&hlen,sizeof(int),1,ofp)");
521  expr(dfp,"fwrite(&vlen,sizeof(int),1,ofp)");
522
523
524  expr(dfp,"for(i=0;i<mat->%s->%s;i++)",gm->query->name,gm->query_len);
525  startbrace(dfp);
526  expr(dfp,"for(j=0;j<mat->%s->%s;j++)",gm->target->name,gm->target_len);
527  startbrace(dfp);
528  expr(dfp,"if( %s_EXPL_MATRIX(mat,i,j,1) > max )",gm->name);
529  expr(dfp,"max = %s_EXPL_MATRIX(mat,i,j,1)",gm->name);
530  closebrace(dfp);
531  closebrace(dfp);
532
533
534  expr(dfp,"for(i=0;i<mat->%s->%s;i++)",gm->query->name,gm->query_len);
535  startbrace(dfp);
536  expr(dfp,"for(j=0;j<mat->%s->%s;j++)",gm->target->name,gm->target_len);
537  startbrace(dfp);
538  expr(dfp,"fputc( (char) (%s_EXPL_MATRIX(mat,i,j,1)*256 / max),ofp )",gm->name);
539  closebrace(dfp);
540  closebrace(dfp);
541
542
543  close_function(dfp);
544}
545
546
547void write_memory_macros(DYNFILE * dfp,GenericMatrix * gm)
548{
549  register int i;
550
551  for(i=0;i<gm->len;i++)
552    macro(dfp,"#define %s %d",gm->state[i]->name,i);
553  add_break(dfp);
554  for(i=0;i<gm->spec_len;i++)
555    macro(dfp,"#define %s %d",gm->special[i]->name,i);
556
557  add_break(dfp);
558  macro(dfp,"#define %s_EXPL_MATRIX(this_matrix,i,j,STATE) this_matrix->basematrix->matrix[((j+%d)*%d)+STATE][i+%d]",gm->name,gm->window_j,gm->len,gm->window_i);
559  macro(dfp,"#define %s_EXPL_SPECIAL(matrix,i,j,STATE) matrix->basematrix->specmatrix[STATE][j+%d]",gm->name,gm->window_j);
560  macro(dfp,"#define %s_READ_OFF_ERROR -%d\n",gm->name,gm->window_i+gm->window_j+1);
561  add_break(dfp);
562}
563
564void write_search_macros(DYNFILE * dfp,const GenericMatrix * gm,DPImplementation * dpi)
565{
566
567  if(gm->qtype != NULL && gm->qtype->maxlen != NULL) {
568    macro(dfp,"#define %s_VSMALL_MATRIX(mat,i,j,STATE) internal_matrix[(j+%d)%%%d][((i+%d)*%d)+STATE]",gm->name,gm->window_j+1,gm->window_j+1,gm->window_i,gm->len);
569    macro(dfp,"#define %s_VSMALL_SPECIAL(mat,i,j,STATE) internal_specials[(j+%d)%%%d][STATE]",gm->name,gm->window_j+1,gm->window_j+1);
570
571  } else {
572    if( dpi->largemem == TRUE ) {
573      macro(dfp,"#define %s_VSMALL_MATRIX(mat,i,j,STATE) internal_pointer_db[((j+%d)%%%d * (mat->leni+%d) * %d) +((i+%d)*%d)+STATE]",gm->name,gm->window_j+1,gm->window_j+1,gm->window_i,gm->len,gm->window_i,gm->len);
574      macro(dfp,"#define %s_VSMALL_SPECIAL(mat,i,j,STATE) internal_special_db[((j+%d)%%%d * %d)+ STATE]",gm->name,gm->window_j+1,gm->window_j+1,gm->spec_len);
575    } else {
576      macro(dfp,"#define %s_VSMALL_MATRIX(mat,i,j,STATE) mat->basematrix->matrix[(j+%d)%%%d][((i+%d)*%d)+STATE]",gm->name,gm->window_j+1,gm->window_j+1,gm->window_i,gm->len);
577      macro(dfp,"#define %s_VSMALL_SPECIAL(mat,i,j,STATE) mat->basematrix->specmatrix[(j+%d)%%%d][STATE]",gm->name,gm->window_j+1,gm->window_j+1);
578    }
579  }
580
581  add_break(dfp);
582}
583
584void write_expl_access_funcs(DYNFILE * dfp,GenericMatrix * gm)
585{
586  FuncInfo * fi;
587
588  fi = FuncInfo_named_from_varstr(FI_INTERNAL,"%s_explicit_access_main",gm->name);
589  start_function_FuncInfo(fi,dfp,"int %s_explicit_access_main(%s * mat,int i,int j,int state)",gm->name,gm->name);
590  expr(dfp,"return %s_EXPL_MATRIX(mat,i,j,state);",gm->name);
591  close_function(dfp);
592  add_break(dfp);
593
594  fi = FuncInfo_named_from_varstr(FI_INTERNAL,"%s_explicit_access_special",gm->name);
595  start_function_FuncInfo(fi,dfp,"int %s_explicit_access_special(%s * mat,int i,int j,int state)",gm->name,gm->name);
596  expr(dfp,"return %s_EXPL_SPECIAL(mat,i,j,state);",gm->name);
597  close_function(dfp);
598  add_break(dfp);
599}
600
601
602void write_expl_read_func(DYNFILE * dfp,GenericMatrix * gm)
603{
604
605  FuncInfo * fi;
606
607  fi = FuncInfo_named_from_varstr(FI_CALLABLE,"PackAln_read_Expl_%s",gm->name);
608  add_line_to_Ftext(fi->ft,"Reads off PackAln from explicit matrix structure",gm->name);
609
610  start_function_FuncInfo(fi,dfp,"PackAln * PackAln_read_Expl_%s(%s * mat)",gm->name,gm->name);
611  expr(dfp,"%s_access_func_holder holder",gm->name);
612  add_break(dfp);
613  expr(dfp,"holder.access_main    = %s_explicit_access_main;",gm->name);
614  expr(dfp,"holder.access_special = %s_explicit_access_special;",gm->name);
615
616  expr(dfp,"return PackAln_read_generic_%s(mat,holder);",gm->name);
617
618  close_function(dfp);
619  add_break(dfp);
620}
621
622
623void write_basic_read_func(DYNFILE * dfp,GenericMatrix * gm)
624{
625  register int i;
626  register int st;
627  FuncInfo * fi;
628
629  for(i=0;i<gm->spec_len;i++)
630    if( gm->special[i]->is_start == TRUE )
631      break;
632
633  if( i >= gm->spec_len )
634    log_full_error(FATAL,0,"Cannot make matrix with no !start state (must be a special)");
635
636  st = i;
637
638  fi = FuncInfo_named_from_varstr(FI_CALLABLE,"PackAln_read_generic_%s",gm->name);
639  add_line_to_Ftext(fi->ft,"Reads off PackAln from explicit matrix structure",gm->name);
640
641  start_function_FuncInfo(fi,dfp,"PackAln * PackAln_read_generic_%s(%s * mat,%s_access_func_holder h)",gm->name,gm->name,gm->name);
642
643  expr(dfp,"register PackAln * out;");
644  expr(dfp,"int i;");
645  expr(dfp,"int j;");
646  expr(dfp,"int state;");
647  expr(dfp,"int cellscore = (-1);");
648  expr(dfp,"boolean isspecial");
649
650  expr(dfp,"PackAlnUnit * pau = NULL;");
651  expr(dfp,"PackAlnUnit * prev = NULL;");
652
653  add_break(dfp);
654
655  expr(dfp,"assert(mat)");
656  expr(dfp,"assert(h.access_main)");
657  expr(dfp,"assert(h.access_special)");
658
659  add_break(dfp);
660  expr(dfp,"out = PackAln_alloc_std()");
661  expr(dfp,"if( out == NULL )");
662  hang_expr(dfp,"return NULL;");
663
664  add_break(dfp);
665
666  /*** Find the end of the matrix ***/
667
668  expr(dfp,"out->score =  find_end_%s(mat,&i,&j,&state,&isspecial,h);",gm->name);
669
670  add_break(dfp);
671
672  add_block_comment(dfp,"Add final end transition (at the moment we have not got the score!");
673  expr(dfp,"if( (pau= PackAlnUnit_alloc()) == NULL  || add_PackAln(out,pau) == FALSE )");
674  startbrace(dfp);
675  expr(dfp,"warn(\"Failed the first PackAlnUnit alloc, %%d length of Alignment in %s_basic_read, returning a mess.(Sorry!)\",out->len);",gm->name);
676  expr(dfp,"return out;");
677  closebrace(dfp);
678
679  add_break(dfp);
680
681  /** put in numbers from block **/
682  add_block_comment(dfp,"Put in positions for end trans. Remember that coordinates in C style");
683
684  expr(dfp,"pau->i = i;");
685  expr(dfp,"pau->j = j;");
686  expr(dfp,"if( isspecial != TRUE) ");
687  hang_expr(dfp,"pau->state = state;");
688  expr(dfp,"else pau->state = state + %d",gm->len);
689/***  expr(dfp,"pau->score = cellscore"); ***/
690  expr(dfp,"prev=pau;");
691
692  /**** do the loop ****/
693
694  /*** expr(dfp,"start_reporting(\"Alignment [Score %%d]:\",out->score);"); ***/
695
696  expr(dfp,"while( state != %s || isspecial != TRUE)",gm->special[st]->name);
697  startbrace_tag(dfp,"while state != START");
698
699
700  add_break(dfp);
701  expr(dfp,"if( isspecial == TRUE )");
702  hang_expr(dfp,"max_calc_special_%s(mat,i,j,state,isspecial,&i,&j,&state,&isspecial,&cellscore,h)",gm->name);
703  expr(dfp,"else");
704  hang_expr(dfp,"max_calc_%s(mat,i,j,state,isspecial,&i,&j,&state,&isspecial,&cellscore,h)",gm->name);
705
706
707  /*** check we haven't boobied ***/
708
709  expr(dfp,"if(i == %s_READ_OFF_ERROR || j == %s_READ_OFF_ERROR || state == %s_READ_OFF_ERROR )",gm->name,gm->name,gm->name);
710
711  startbrace(dfp);
712  expr(dfp,"warn(\"Problem - hit bad read off system, exiting now\");");
713  expr(dfp,"break;");
714  closebrace(dfp);
715
716  /** allocate position **/
717
718  expr(dfp,"if( (pau= PackAlnUnit_alloc()) == NULL  || add_PackAln(out,pau) == FALSE )");
719  startbrace(dfp);
720  expr(dfp,"warn(\"Failed a PackAlnUnit alloc, %%d length of Alignment in %s_basic_read, returning partial alignment\",out->len);",gm->name);
721  expr(dfp,"break;");
722  closebrace(dfp);
723
724  add_break(dfp);
725
726  /** put in numbers from block **/
727  add_block_comment(dfp,"Put in positions for block. Remember that coordinates in C style");
728
729  expr(dfp,"pau->i = i;");
730  expr(dfp,"pau->j = j;");
731  expr(dfp,"if( isspecial != TRUE) ");
732  hang_expr(dfp,"pau->state = state;");
733  expr(dfp,"else pau->state = state + %d",gm->len);
734  expr(dfp,"prev->score = cellscore");
735  expr(dfp,"prev = pau;");
736
737  closebrace(dfp);
738
739  /**** Add in alignment flips (or do I need to do that later!) ****/
740  add_break(dfp);
741
742  /*** expr(dfp,"stop_reporting()"); ***/
743
744  expr(dfp,"invert_PackAln(out);");
745  expr(dfp,"return out;");
746
747  close_function(dfp);
748  add_break(dfp);
749}
750
751void write_pal_to_ars_func(DYNFILE * dfp,GenericMatrix * gm)
752{
753  FuncInfo * fi;
754
755  fi = FuncInfo_named_from_varstr(FI_CALLABLE,"AlnRangeSet_from_PackAln_%s",gm->name);
756  add_line_to_Ftext(fi->ft,"Converts a PackAln (full alignment) to start/stop range");
757  add_line_to_Ftext(fi->ft,"The point being that you may have the PackAln and someone wants a range",gm->name);
758
759  start_function_FuncInfo(fi,dfp,"AlnRangeSet * AlnRangeSet_from_PackAln_%s(PackAln * pal)",gm->name);
760  expr(dfp,"int unit;");
761  expr(dfp,"int cum_score = 0;");
762  expr(dfp,"AlnRangeSet * out;");
763  expr(dfp,"AlnRange * temp;");
764
765  add_break(dfp);
766
767  expr(dfp,"out = AlnRangeSet_alloc_std();");
768
769  expr(dfp,"for(unit = 0;unit < pal->len;unit++)");
770  startbrace_tag(dfp,"while there are more units");
771  expr(dfp,"cum_score += pal->pau[unit]->score;");
772  expr(dfp,"if( pal->pau[unit]->state < %d)",gm->len);
773  startbrace(dfp);
774  add_block_comment(dfp,"It is out of the specials");
775  expr(dfp,"temp = AlnRange_alloc();");
776  expr(dfp,"temp->starti = pal->pau[unit]->i;");
777  expr(dfp,"temp->startj = pal->pau[unit]->j;");
778  expr(dfp,"temp->startstate = pal->pau[unit]->state;");
779  expr(dfp,"temp->startscore = cum_score;");
780
781  expr(dfp,"for(;unit < pal->len && pal->pau[unit]->state < %d;unit++)",gm->len);
782  hang_expr(dfp,"cum_score += pal->pau[unit]->score;");
783  expr(dfp,"temp->stopi = pal->pau[unit-1]->i;");
784  expr(dfp,"temp->stopj = pal->pau[unit-1]->j;");
785  expr(dfp,"temp->stopstate = pal->pau[unit-1]->state;");
786  expr(dfp,"temp->stopscore = cum_score;");
787  expr(dfp,"add_AlnRangeSet(out,temp)");
788  closebrace(dfp);
789  closebrace(dfp);
790
791  add_break(dfp);
792
793  expr(dfp,"return out");
794
795  close_function(dfp);
796
797  add_break(dfp);
798
799}
800
801
802void write_alncconvert_make_func(DYNFILE * dfp,GenericMatrix * gm)
803{
804  int i;
805  int j;
806  LabelMaster * lm;
807  char * qs;
808  char * ts;
809  int qlabel;
810  int tlabel;
811  FuncInfo * fi;
812
813
814
815  lm = LabelMaster_from_GenericMatrix(gm);
816  qs =  query_quoted_string_from_LabelMaster(lm);
817  ts = target_quoted_string_from_LabelMaster(lm);
818
819  expr(dfp," static char * query_label[] = { %s };",qs);
820  expr(dfp," static char * target_label[] = { %s };",ts);
821
822  ckfree(qs);
823  ckfree(ts);
824
825  fi = FuncInfo_named_from_varstr(FI_INTERNAL,"AlnConvertSet_%s",gm->name);
826
827  start_function_FuncInfo(fi,dfp,"AlnConvertSet * AlnConvertSet_%s(void)",gm->name);
828  expr(dfp,"AlnConvertUnit * acu;");
829  expr(dfp,"AlnConvertSet  * out;");
830  add_break(dfp);
831  expr(dfp,"out = AlnConvertSet_alloc_std();");
832  add_break(dfp);
833
834  for(i=0;i<gm->len;i++) {
835    for(j=0;j<gm->state[i]->len;j++) {
836      qlabel = index_for_query_label(gm->state[i]->source[j]->query_label,lm);
837      if( qlabel == (-1) ) {
838	warn("Query label [%s] unfound. Consider this a bad compiler bug!",gm->state[i]->source[j]->query_label);
839	qlabel = 0;
840      }
841      tlabel = index_for_target_label(gm->state[i]->source[j]->target_label,lm);
842      if( tlabel == (-1) ) {
843	warn("Query label [%s] unfound. Consider this a bad compiler bug!",gm->state[i]->source[j]->target_label);
844	tlabel = 0;
845      }
846
847      expr(dfp,"acu = AlnConvertUnit_alloc();");
848      expr(dfp,"add_AlnConvertSet(out,acu);");
849      if( gm->state[i]->source[j]->isspecial == FALSE ) {
850	expr(dfp,"acu->state1 = %s;",gm->state[i]->source[j]->state_source);
851      }
852      else {
853	expr(dfp,"acu->state1 = %s + %d;",gm->state[i]->source[j]->state_source,gm->len);
854	expr(dfp,"acu->is_from_special = TRUE;");
855      }
856
857      expr(dfp,"acu->state2 = %s",gm->state[i]->name);
858
859      if( gm->state[i]->source[j]->isspecial == FALSE )
860	expr(dfp,"acu->offi = %d;",gm->state[i]->source[j]->offi);
861      else expr(dfp,"acu->offi = (-1);");
862      expr(dfp,"acu->offj = %d;",gm->state[i]->source[j]->offj);
863      expr(dfp,"acu->label1 = query_label[%d];",qlabel);
864      expr(dfp,"acu->label2 = target_label[%d];",tlabel);
865    }
866  }
867
868  for(i=0;i<gm->spec_len;i++) {
869    for(j=0;j<gm->special[i]->len;j++) {
870      qlabel = index_for_query_label(gm->special[i]->source[j]->query_label,lm);
871      if( qlabel == (-1) ) {
872	warn("Query label [%s] unfound. Consider this a bad compiler bug!",gm->special[i]->source[j]->query_label);
873	qlabel = 0;
874      }
875      tlabel = index_for_target_label(gm->special[i]->source[j]->target_label,lm);
876      if( tlabel == (-1) ) {
877	warn("Query label [%s] unfound. Consider this a bad compiler bug!",gm->special[i]->source[j]->target_label);
878	tlabel = 0;
879      }
880
881      expr(dfp,"acu = AlnConvertUnit_alloc();");
882      expr(dfp,"add_AlnConvertSet(out,acu);");
883      if( gm->special[i]->source[j]->isspecial == FALSE )
884	expr(dfp,"acu->state1 = %s;",gm->special[i]->source[j]->state_source);
885      else expr(dfp,"acu->state1 = %s + %d;",gm->special[i]->source[j]->state_source,gm->len);
886      expr(dfp,"acu->state2 = %s + %d",gm->special[i]->name,gm->len);
887
888      expr(dfp,"acu->offi = (-1);");
889      expr(dfp,"acu->offj = %d;",gm->special[i]->source[j]->offj);
890      expr(dfp,"acu->label1 = query_label[%d];",qlabel);
891      expr(dfp,"acu->label2 = target_label[%d];",tlabel);
892    }
893  }
894
895  for(i=0;i<gm->cal_len;i++) {
896    expr(dfp,"add_collapse_label_AlnConvertSet(out,\"%s\",\"%s\");",gm->cal[i]->query,gm->cal[i]->target);
897  }
898
899
900
901  expr(dfp,"return out;");
902  close_function(dfp);
903  add_break(dfp);
904
905}
906
907void write_aln_conversion_func(DYNFILE * dfp,GenericMatrix * gm)
908{
909  FuncInfo * fi;
910
911  fi = FuncInfo_named_from_varstr(FI_CALLABLE,"convert_PackAln_to_AlnBlock_%s",gm->name);
912  add_line_to_Ftext(fi->ft,"Converts a path alignment to a label alignment");
913  add_line_to_Ftext(fi->ft,"The label alignment is probably much more useful than the path",gm->name);
914
915  start_function_FuncInfo(fi,dfp,"AlnBlock * convert_PackAln_to_AlnBlock_%s(PackAln * pal)",gm->name,gm->name);
916  expr(dfp,"AlnConvertSet * acs;");
917  expr(dfp,"AlnBlock * alb;");
918  add_break(dfp);
919  expr(dfp,"acs = AlnConvertSet_%s();",gm->name);
920  expr(dfp,"alb = AlnBlock_from_PackAln(acs,pal);");
921  expr(dfp,"free_AlnConvertSet(acs);");
922  expr(dfp,"return alb;");
923  close_function(dfp);
924
925  add_break(dfp);
926}
927
928
929%func
930init explicit matrix
931%%
932void init_matrix_func(DYNFILE * dfp,GenericMatrix * gm)
933{
934  FuncInfo * fi;
935  ArgInfo * ai;
936  register int i;
937
938  fi = FuncInfo_named_from_varstr(FI_INTERNAL,"init_%s",gm->name);
939  add_line_to_Ftext(fi->ft,"This function initates %s matrix when in explicit mode",gm->name);
940  add_line_to_Ftext(fi->ft,"Called in /allocate_Expl_%s",gm->name);
941
942
943  /** ArgInfo_in_FuncInfo_from_varstr automatically adds ai to fi **/
944
945  ai =  ArgInfo_in_FuncInfo_from_varstr(fi,"mat");
946  ai->desc=stringallocf("%s which contains explicit basematrix memory",gm->name);
947
948  start_function_FuncInfo(fi,dfp,"void init_%s(%s * mat)",gm->name,gm->name);
949
950  expr(dfp,"register int i;");
951  expr(dfp,"register int j;");
952
953  expr(dfp,"if( mat->basematrix->type != BASEMATRIX_TYPE_EXPLICIT)");
954  startbrace(dfp);
955  warn_expr(dfp,"Cannot iniate matrix, is not an explicit memory type and you have assummed that");
956  expr(dfp,"return");
957  closebrace(dfp);
958
959  add_break(dfp);
960
961  expr(dfp,"for(i= (-%d);i<mat->%s->%s;i++)",gm->window_i,gm->query->name,gm->query_len);
962  startbrace(dfp);
963  expr(dfp,"for(j= (-%d);j<%d;j++)",gm->window_j,gm->window_j+1);
964  startbrace(dfp);
965  for(i=0;i<gm->len;i++) {
966    expr(dfp,"%s_EXPL_MATRIX(mat,i,j,%s) = %s;",gm->name,gm->state[i]->name,gm->state[i]->def_score);
967  }
968  closebrace(dfp);
969  closebrace(dfp);
970
971  expr(dfp,"for(j= (-%d);j<mat->%s->%s;j++)",gm->window_j,gm->target->name,gm->target_len);
972  startbrace(dfp);
973  expr(dfp,"for(i= (-%d);i<%d;i++)",gm->window_i,gm->window_i+1);
974  startbrace(dfp);
975  for(i=0;i<gm->len;i++) {
976    expr(dfp,"%s_EXPL_MATRIX(mat,i,j,%s) = %s;",gm->name,gm->state[i]->name,gm->state[i]->def_score);
977  }
978  closebrace(dfp);
979  for(i=0;i<gm->spec_len;i++) {
980    expr(dfp,"%s_EXPL_SPECIAL(mat,i,j,%s) = %s;",gm->name,gm->special[i]->name,gm->special[i]->def_score);
981  }
982  closebrace(dfp);
983
984
985  expr(dfp,"return");
986  close_function(dfp);
987
988  add_break(dfp);
989
990}
991
992
993void find_end_func(DYNFILE * dfp,GenericMatrix * gm)
994{
995  register int i;
996  FuncInfo * fi;
997
998
999
1000  for(i=0;i<gm->spec_len;i++)
1001    if( gm->special[i]->is_end == TRUE)
1002      break;
1003
1004  if( i >= gm->spec_len ) {
1005    log_full_error(WARNING,0,"Cannot build find end function due to lack of end state");
1006    return;
1007  }
1008
1009  fi = FuncInfo_named_from_varstr(FI_INTERNAL,"find_end_%s",gm->name);
1010  start_function_FuncInfo(fi,dfp,"int find_end_%s(%s * mat,int * ri,int * rj,int * state,boolean * isspecial,%s_access_func_holder h)",gm->name,gm->name,gm->name);
1011
1012  expr(dfp,"int j");
1013  expr(dfp,"int max;");
1014  expr(dfp,"int maxj");
1015  expr(dfp,"int temp");
1016
1017  add_break(dfp);
1018
1019  expr(dfp,"max = (*h.access_special)(mat,0,mat->%s->%s-1,%s);",gm->target->name,gm->target_len,gm->special[i]->name);
1020  expr(dfp,"maxj = mat->%s->%s-1",gm->target->name,gm->target_len);
1021  expr(dfp,"for(j= mat->%s->%s-2 ;j >= 0 ;j--)",gm->target->name,gm->target_len);
1022
1023  startbrace(dfp);
1024  expr(dfp,"if( (temp =(*h.access_special)(mat,0,j,%s)) > max )",gm->special[i]->name);
1025  startbrace(dfp);
1026  expr(dfp,"max = temp;");
1027  expr(dfp,"maxj = j;");
1028  closebrace(dfp);
1029  closebrace(dfp);
1030
1031  add_break(dfp);
1032
1033  expr(dfp,"if( ri != NULL)");
1034  hang_expr(dfp," *ri = 0; ");
1035  expr(dfp,"if( rj != NULL)");
1036  hang_expr(dfp," *rj = maxj; ");
1037  expr(dfp,"if( state != NULL)");
1038  hang_expr(dfp," *state = %s;",gm->special[i]->name);
1039  expr(dfp,"if( isspecial != NULL)");
1040  hang_expr(dfp," *isspecial = TRUE;");
1041
1042
1043  add_break(dfp);
1044
1045  expr(dfp,"return max");
1046
1047  close_function(dfp);
1048
1049  add_break(dfp);
1050
1051}
1052
1053
1054void debug_func(DYNFILE * dfp,GenericMatrix * gm)
1055{
1056  register int i;
1057  FuncInfo * fi;
1058
1059  fi = FuncInfo_named_from_varstr(FI_INTERNAL,"%s_debug_show_matrix",gm->name);
1060
1061  start_function_FuncInfo(fi,dfp,"void %s_debug_show_matrix(%s * mat,int starti,int stopi,int startj,int stopj,FILE * ofp)",gm->name,gm->name);
1062
1063  expr(dfp,"register int i;");
1064  expr(dfp,"register int j;");
1065
1066  add_break(dfp);
1067
1068  expr(dfp,"for(i=starti;i<stopi && i < mat->%s->%s;i++)",gm->query->name,gm->query_len);
1069  startbrace(dfp);
1070  expr(dfp,"for(j=startj;j<stopj && j < mat->%s->%s;j++)",gm->target->name,gm->target_len);
1071  startbrace(dfp);
1072  expr(dfp,"fprintf(ofp,\"Cell [%%d - %%d]\\n\",i,j)");
1073  for(i=0;i<gm->len;i++)
1074    expr(dfp,"fprintf(ofp,\"State %s %%d\\n\",%s_EXPL_MATRIX(mat,i,j,%s));",gm->state[i]->name,gm->name,gm->state[i]->name);
1075  expr(dfp,"fprintf(ofp,\"\\n\\n\");");
1076  closebrace(dfp);
1077  closebrace(dfp);
1078	add_break(dfp);
1079  close_function(dfp);
1080  add_break(dfp);
1081
1082  return;
1083}
1084
1085void write_special_max_calc_func_debug(DYNFILE * dfp,GenericMatrix * gm,int debug)
1086{
1087  register int i;
1088  register int j;
1089  FuncInfo * fi;
1090  char * matrix_tag = "EXPL_MATRIX";
1091  char * special_tag = "EXPL_SPECIAL";
1092
1093  fi = FuncInfo_named_from_varstr(FI_INTERNAL,"max_calc_special_%s",gm->name);
1094
1095
1096  start_function_FuncInfo(fi,dfp,"int max_calc_special_%s(%s * mat,int i,int j,int state,boolean isspecial,int * reti,int * retj,int * retstate,boolean * retspecial,int * cellscore,%s_access_func_holder h)",gm->name,gm->name,gm->name);
1097
1098  expr(dfp,"register int temp;");
1099  expr(dfp,"register int cscore;");
1100
1101  add_break(dfp);
1102
1103  expr(dfp,"*reti = (*retj) = (*retstate) = %s_READ_OFF_ERROR;",gm->name);
1104
1105  add_break(dfp);
1106
1107  expr(dfp,"if( j < 0 || j > mat->%s->%s)",gm->target->name,gm->target_len);
1108  startbrace(dfp);
1109  expr(dfp,"warn(\"In %s matrix special read off - out of bounds on matrix [j is %%d in special]\",j);",gm->name);
1110  expr(dfp,"return -1");
1111  closebrace(dfp);
1112
1113  add_break(dfp);
1114
1115  expr(dfp,"cscore = (*h.access_special)(mat,i,j,state);");
1116
1117  if( debug)
1118    expr(dfp,"fprintf(stderr,\"Looking for score %%d\\n\",cscore);");
1119
1120  expr(dfp,"switch(state)");
1121  startbrace_tag(dfp,"switch on special states");
1122
1123  for(i=0;i<gm->spec_len;i++) {
1124    auto CellState * cell;
1125    cell = gm->special[i];
1126    expr(dfp,"case %s : ",cell->name);
1127    startcase(dfp);
1128
1129
1130    /*** ok, this is a problem. probably should do special to special first, ***/
1131    /*** the others. Hmmm.,.. ***/
1132
1133
1134    for(j=cell->len-1;j>=0;j--) {
1135
1136      if( cell->source[j]->isspecial == FALSE ) {
1137	add_block_comment(dfp,"source %s is from main matrix",cell->source[j]->state_source);
1138	expr(dfp,"for(i= mat->%s->%s-1;i >= 0 ;i--)",gm->query->name,gm->query_len);
1139	startbrace_tag(dfp,"for i >= 0");
1140
1141	expr(dfp,"temp = cscore - (%s) - (%s)",cell->source[j]->calc_expr,
1142	     cell->calc_expr == NULL ? "0" : cell->calc_expr);
1143
1144	if( debug )
1145	  expr(dfp,"fprintf(stderr,\"Trying %%d for case %s\\n\",temp)",cell->source[j]->state_source);
1146
1147	expr(dfp,"if( temp == (*h.access_main)(mat,i - %d,j - %d,%s) )",cell->source[j]->offi,
1148	       cell->source[j]->offj,cell->source[j]->state_source);
1149
1150	startbrace(dfp);
1151	expr(dfp,"*reti = i - %d;",cell->source[j]->offi);
1152	expr(dfp,"*retj = j - %d;",cell->source[j]->offj);
1153	expr(dfp,"*retstate = %s;",cell->source[j]->state_source);
1154
1155	if( cell->source[j]->isspecial == TRUE)
1156	  expr(dfp,"*retspecial = TRUE;");
1157	else	expr(dfp,"*retspecial = FALSE;");
1158
1159
1160	/*** if they want the score, calc the difference in the number and give it back ***/
1161	expr(dfp,"if( cellscore != NULL)");
1162	startbrace(dfp);
1163	expr(dfp,"*cellscore = cscore - (*h.access_main)(mat,i-%d,j-%d,%s)",cell->source[j]->offi,
1164	     cell->source[j]->offj,cell->source[j]->state_source);
1165	closebrace(dfp);
1166
1167	expr(dfp,"return (*h.access_main)(mat,i - %d,j - %d,%s) ",cell->source[j]->offi,
1168	     cell->source[j]->offj,cell->source[j]->state_source);
1169	closebrace(dfp);
1170	closebrace(dfp); /** this closes the for brace ***/
1171      }
1172
1173
1174      else {
1175	add_block_comment(dfp,"source %s is a special",cell->source[j]->state_source);
1176
1177	expr(dfp,"temp = cscore - (%s) - (%s)",cell->source[j]->calc_expr,
1178	     cell->calc_expr == NULL ? "0" : cell->calc_expr);
1179
1180	if( debug )
1181	  expr(dfp,"fprintf(stderr,\"Trying %%d for case %s\\n\",temp)",cell->source[j]->state_source);
1182
1183	expr(dfp,"if( temp == (*h.access_special)(mat,i - %d,j - %d,%s) )",cell->source[j]->offi,
1184	     cell->source[j]->offj,cell->source[j]->state_source);
1185
1186
1187	startbrace(dfp);
1188	expr(dfp,"*reti = i - %d;",cell->source[j]->offi);
1189	expr(dfp,"*retj = j - %d;",cell->source[j]->offj);
1190	expr(dfp,"*retstate = %s;",cell->source[j]->state_source);
1191
1192	if( cell->source[j]->isspecial == TRUE)
1193	  expr(dfp,"*retspecial = TRUE;");
1194	else	expr(dfp,"*retspecial = FALSE;");
1195
1196	/*** if they want the score, calc the difference in the number and give it back ***/
1197	expr(dfp,"if( cellscore != NULL)");
1198	startbrace(dfp);
1199	expr(dfp,"*cellscore = cscore - (*h.access_special)(mat,i-%d,j-%d,%s)",cell->source[j]->offi,
1200	     cell->source[j]->offj,cell->source[j]->state_source);
1201	closebrace(dfp);
1202
1203	expr(dfp,"return (*h.access_special)(mat,i - %d,j - %d,%s) ",cell->source[j]->offi,
1204	     cell->source[j]->offj,cell->source[j]->state_source);
1205	closebrace(dfp);
1206
1207      }
1208    }
1209    closecase(dfp);
1210  }
1211
1212  expr(dfp,"default:");
1213  startcase(dfp);
1214  expr(dfp,"warn(\"Major problem (!) - in %s read off, position %%d,%%d state %%d no source found  dropped into default on source switch!\",i,j,state);",gm->name);
1215  expr(dfp,"return (-1);");
1216  closecase(dfp);
1217
1218  closebrace(dfp);
1219
1220  close_function(dfp);
1221
1222  add_break(dfp);
1223}
1224
1225void write_special_max_calc_func(DYNFILE * dfp,GenericMatrix * gm)
1226{
1227  write_special_max_calc_func_debug(dfp,gm,0);
1228}
1229
1230 /****
1231   This function requires alot of stuff assummed.
1232
1233   int * reti, int *retj and *retstate *retspecial as parameters which will be set
1234   by this block.
1235
1236   This block is really to facillate different underlying memory structures
1237   which can be used.
1238
1239   Please see functions write_max_calc_func and write_hidden_max_func (dynashadow.dy)
1240   to see how it is used
1241
1242   ***/
1243
1244void write_max_calc_block(DYNFILE * dfp,GenericMatrix * gm,char * matrix_tag,char * special_tag,boolean use_special,boolean use_holder)
1245{
1246  register int i;
1247  register int j;
1248  char matrix_access[128];
1249  char special_access[128];
1250
1251
1252  if( use_holder == TRUE ) {
1253    sprintf(matrix_access,"(*h.access_main)");
1254    sprintf(special_access,"(*h.access_special)");
1255  } else {
1256    sprintf(matrix_access,"%s_%s",gm->name,matrix_tag);
1257    sprintf(special_access,"%s_%s",gm->name,special_tag);
1258  }
1259
1260
1261  add_break(dfp);
1262  add_block_comment(dfp,"Then you have to select the correct switch statement to figure out the readoff     ");
1263  add_block_comment(dfp,"Somewhat odd - reverse the order of calculation and return as soon as it is correct");
1264
1265
1266  expr(dfp,"cscore = %s(mat,i,j,state);",matrix_access);
1267  expr(dfp,"switch(state)");
1268
1269  startbrace_tag(dfp,"Switch state ");
1270  for(i=0;i<gm->len;i++) {
1271    auto CellState * cell;
1272    cell = gm->state[i];
1273    expr(dfp,"case %s : ",cell->name);
1274    startcase(dfp);
1275    for(j=cell->len-1;j>=0;j--) {
1276      if( use_special == FALSE && cell->source[j]->isspecial == TRUE ) {
1277	add_block_comment(dfp,"Not allowing special sources.. skipping %s",cell->source[j]->state_source);
1278	continue;
1279      }
1280
1281      if( cell->source[j]->position != SOURCE_POS_ALL) {
1282	add_block_comment(dfp,"Has restricted position");
1283	expr(dfp,"if( %s )",source_allowed_statement(cell->source[j]->position,cell->source[j]->offi,cell->source[j]->offj));
1284	startbrace(dfp);
1285      }
1286
1287      expr(dfp,"temp = cscore - (%s) -  (%s);",cell->source[j]->calc_expr,
1288	   cell->calc_expr == NULL ? "0" : cell->calc_expr);
1289
1290      if( cell->source[j]->isspecial == TRUE)
1291	expr(dfp,"if( temp == %s(mat,i - %d,j - %d,%s) )",special_access,cell->source[j]->offi,
1292	     cell->source[j]->offj,cell->source[j]->state_source);
1293      else	expr(dfp,"if( temp == %s(mat,i - %d,j - %d,%s) )",matrix_access,cell->source[j]->offi,
1294		     cell->source[j]->offj,cell->source[j]->state_source);
1295
1296      startbrace(dfp);
1297      expr(dfp,"*reti = i - %d;",cell->source[j]->offi);
1298      expr(dfp,"*retj = j - %d;",cell->source[j]->offj);
1299      expr(dfp,"*retstate = %s;",cell->source[j]->state_source);
1300      if( cell->source[j]->isspecial == TRUE)
1301	expr(dfp,"*retspecial = TRUE;");
1302      else expr(dfp,"*retspecial = FALSE;");
1303
1304      /*** if they want the score, calc the difference in the number and give it back ***/
1305      expr(dfp,"if( cellscore != NULL)");
1306      startbrace(dfp);
1307
1308
1309      expr(dfp,"*cellscore = cscore - %s(mat,i-%d,j-%d,%s);",
1310	   cell->source[j]->isspecial == TRUE ? special_access : matrix_access,cell->source[j]->offi,
1311	   cell->source[j]->offj,cell->source[j]->state_source);
1312      closebrace(dfp);
1313
1314
1315      expr(dfp,"return %s(mat,i - %d,j - %d,%s); ",matrix_access,cell->source[j]->offi,
1316	   cell->source[j]->offj,cell->source[j]->state_source);
1317
1318      if( cell->source[j]->position != SOURCE_POS_ALL) {
1319	closebrace(dfp);
1320      }
1321
1322      closebrace(dfp);
1323    }
1324    expr(dfp,"warn(\"Major problem (!) - in %s read off, position %%d,%%d state %%d no source found!\",i,j,state);",gm->name);
1325    expr(dfp,"return (-1);");
1326    closecase(dfp);
1327  }
1328  expr(dfp,"default:");
1329  startcase(dfp);
1330  expr(dfp,"warn(\"Major problem (!) - in %s read off, position %%d,%%d state %%d no source found!\",i,j,state);",gm->name);
1331  expr(dfp,"return (-1);");
1332  closecase(dfp);
1333
1334
1335  closebrace(dfp);
1336}
1337
1338
1339
1340void write_max_calc_func(DYNFILE * dfp,GenericMatrix * gm)
1341{
1342  FuncInfo * fi;
1343
1344  fi = FuncInfo_named_from_varstr(FI_INTERNAL,"max_calc_%s",gm->name);
1345
1346  start_function_FuncInfo(fi,dfp,"int max_calc_%s(%s * mat,int i,int j,int state,boolean isspecial,int * reti,int * retj,int * retstate,boolean * retspecial,int * cellscore,%s_access_func_holder h)",gm->name,gm->name,gm->name);
1347
1348  expr(dfp,"register int temp;");
1349  expr(dfp,"register int cscore;");
1350
1351  add_break(dfp);
1352
1353  expr(dfp,"*reti = (*retj) = (*retstate) = %s_READ_OFF_ERROR;",gm->name);
1354
1355  add_break(dfp);
1356
1357  expr(dfp,"if( i < 0 || j < 0 || i > mat->%s->%s || j > mat->%s->%s)",gm->query->name,
1358       gm->query_len,gm->target->name,gm->target_len);
1359  startbrace(dfp);
1360  expr(dfp,"warn(\"In %s matrix special read off - out of bounds on matrix [i,j is %%d,%%d state %%d in standard matrix]\",i,j,state);",gm->name);
1361  expr(dfp,"return -1");
1362  closebrace(dfp);
1363
1364  write_max_calc_block(dfp,gm,"EXPL_MATRIX","EXPL_SPECIAL",TRUE,TRUE);
1365
1366  close_function(dfp);
1367
1368  add_break(dfp);
1369
1370  return;
1371
1372
1373}
1374
1375static char source_buffer[125];
1376
1377char * source_allowed_statement(int position,int offi,int offj)
1378{
1379  switch(position) {
1380  case SOURCE_POS_ALL :
1381    warn("Bad point in source to - you have an ALL source positions. Hmmmmm");
1382    return "1";
1383  case SOURCE_POS_TOPLEFT :
1384    sprintf(source_buffer,"(i-%d) == 0 && (j-%d) == 0",offi,offj);
1385    return source_buffer;
1386  case SOURCE_POS_TOP :
1387    sprintf(source_buffer,"(i-%d) == 0 ",offi);
1388    return source_buffer;
1389  case SOURCE_POS_LEFT :
1390    sprintf(source_buffer,"(j-%d) == 0 ",offj);
1391    return source_buffer;
1392  case SOURCE_POS_RIGHT :
1393    return "j == mat->lenj-1";
1394  case SOURCE_POS_BOTTOM :
1395    return "i == mat->leni-1";
1396  case SOURCE_POS_BOTTOMRIGHT :
1397    return "i == mat->leni-1 && j == mat->lenj-1";
1398  default :
1399    warn("Bad error, got an impossible source position %d",position);
1400    return("1");
1401  }
1402}
1403
1404
1405
1406void write_score_block_debug(DYNFILE * dfp,GenericMatrix * gm,char * matrixtag,char * pointertag,char * specialtag,boolean use_special,int debug)
1407{
1408  register int i;
1409  register int j;
1410  register int k;
1411
1412
1413
1414  for(i=0;i<gm->len;i++) {
1415    auto CellState * state;
1416    state = gm->state[i];
1417
1418    add_break(dfp);
1419
1420    if( state->footprint_start > 1 || state->footprint_end < 0)   {
1421      add_block_comment(dfp,"State %s has a footprint of %d - %d",state->name,state->footprint_start,state->footprint_end);
1422      expr(dfp,"if( SEQENDWITHIN(%d) != TRUE || SEQSTARTWITHIN(%d) != TRUE )",state->footprint_end,state->footprint_start);
1423      startbrace_tag(dfp,"Footprint exists");
1424    }
1425
1426
1427
1428    add_block_comment(dfp,"For state %s",state->name);
1429    add_block_comment(dfp,"setting first movement to score",state->name);
1430
1431
1432
1433    if( state->source[0]->position != SOURCE_POS_ALL) {
1434      add_block_comment(dfp,"Has restricted position");
1435      expr(dfp,"if( %s )",source_allowed_statement(state->source[0]->position,state->source[0]->offi,state->source[0]->offj));
1436      startbrace(dfp);
1437    }
1438
1439    /*********************************************************************/
1440    /* this line looks like                                              */
1441    /*   score = ProteinMatrix_EXPL_MATRIX(mat,i-1,j-1,MATCH) + xxxxx    */
1442    /*********************************************************************/
1443
1444    expr(dfp,"score = %s_%s(%s,i-%d,j-%d,%s) + %s",gm->name,matrixtag,pointertag,
1445	 state->source[0]->offi,state->source[0]->offj,state->source[0]->state_source,
1446	 state->source[0]->calc_expr);
1447
1448
1449    if( state->source[0]->isspecial == TRUE ) {
1450      fatal("Cannot have a special to matrix transition as the first transition");
1451    }
1452
1453    if( state->source[0]->position != SOURCE_POS_ALL) {
1454      closebrace(dfp);
1455    }
1456
1457
1458    if( dfp->code_debug_level > 5) {
1459      expr(dfp,"if( score > IMPOSSIBLY_HIGH_SCORE )");
1460      hang_expr(dfp,"log_full_error(WARNING,5,\"[%%4d][%%4d] State %s source %s Impossibly high score [%%d]\",i,j,score);",state->name,state->source[0]->state_source);
1461    }
1462
1463    if( dfp->code_debug_level > 100 ) {
1464      expr(dfp,"fprintf(stderr,\"MATRIX: [%%4d][%%4d] State %s source %s got score %%d\\n\",i,j,score);",state->name,state->source[0]->state_source);
1465    }
1466
1467    /**** ok this is to stop underflow, but is v.v.v. hacky ****/
1468    /*** removing underflow hack
1469    expr(dfp,"if(score < (-10000000) )");
1470    hang_expr(dfp,"score = (-10000000)");
1471    ****/
1472
1473    if( debug == 2)
1474      expr(dfp,"fprintf(stderr,\"[%%4d,%%4d] First score of %s gets %%d\\n\",i,j,score)",state->name);
1475
1476    /****************************************/
1477    /* now we do if then on score and temp  */
1478    /****************************************/
1479
1480    for(j=1;j<state->len;j++)	{
1481
1482      if( use_special == FALSE && state->source[j]->isspecial == TRUE )
1483	continue; /** don't use the special! **/
1484
1485      if( state->source[j]->position != SOURCE_POS_ALL) {
1486	add_block_comment(dfp,"Has restricted position");
1487	expr(dfp,"if( %s )",source_allowed_statement(state->source[j]->position,state->source[j]->offi,state->source[j]->offj));
1488	startbrace(dfp);
1489      }
1490
1491
1492
1493      add_block_comment(dfp,"From state %s to state %s",state->source[j]->state_source,
1494			state->name);
1495      if( state->source[j]->isspecial == TRUE )
1496	expr(dfp,"temp = %s_%s(%s,i-%d,j-%d,%s) + %s",gm->name,specialtag,pointertag,
1497	     state->source[j]->offi,state->source[j]->offj,state->source[j]->state_source,
1498	     state->source[j]->calc_expr);
1499      else	expr(dfp,"temp = %s_%s(%s,i-%d,j-%d,%s) + %s",gm->name,matrixtag,pointertag,
1500		     state->source[j]->offi,state->source[j]->offj,state->source[j]->state_source,
1501		     state->source[j]->calc_expr);
1502
1503
1504
1505      if( dfp->code_debug_level > 5) {
1506	expr(dfp,"if( temp > IMPOSSIBLY_HIGH_SCORE )");
1507      hang_expr(dfp,"log_full_error(WARNING,5,\"[%%4d][%%4d] State %s source %s Impossibly high score [%%d]\",i,j,temp);",state->name,state->source[j]->state_source);
1508      }
1509
1510      if( dfp->code_debug_level > 100 ) {
1511	expr(dfp,"fprintf(stderr,\"MATRIX: [%%4d][%%4d] State %s source %s got score %%d\\n\",i,j,temp);",state->name,state->source[0]->state_source);
1512      }
1513
1514
1515      /**** ok this is to stop underflow, but is v.v.v. hacky ****/
1516      /**** removing underflow hack
1517      expr(dfp,"if(score < (-10000000) )");
1518      hang_expr(dfp,"score = (-10000000)");
1519      ****/
1520
1521      if( debug == 2 )
1522	expr(dfp,"fprintf(stderr,\"[%%4d,%%4d] Score of %s from %s gets %%d\\n\",i,j,temp)",state->name,state->source[j]->state_source );
1523
1524      /** if we have a specified calcfunc - use it here **/
1525      if(gm->calcfunc != NULL ) {
1526	expr(dfp,"score = %s(score,temp);",gm->calcfunc);
1527      } else{
1528
1529	expr(dfp,"if( temp  > score )");
1530	startbrace(dfp);
1531	expr(dfp,"score = temp;");
1532	/** ok for shadow matrix should put things in here */
1533	closebrace(dfp);
1534
1535      }
1536      if( state->source[j]->position != SOURCE_POS_ALL) {
1537	closebrace(dfp);
1538      }
1539
1540    }
1541
1542    /************************/
1543    /* finished blocks      */
1544    /* put in global calc   */
1545    /************************/
1546    add_break(dfp);
1547    add_block_comment(dfp,"Ok - finished max calculation for %s",state->name);
1548    add_block_comment(dfp,"Add any movement independant score and put away");
1549
1550    if( state->calc_expr != NULL)
1551      expr(dfp," score += %s",state->calc_expr);
1552
1553    /***************************/
1554    /* put away score          */
1555    /***************************/
1556
1557    expr(dfp," %s_%s(%s,i,j,%s) = score;",gm->name,matrixtag,pointertag,state->name);
1558
1559
1560    if( use_special == FALSE ) {
1561      add_block_comment(dfp,"Finished calculating state %s",state->name);
1562      continue;
1563    }
1564
1565
1566
1567    /************************/
1568    /* for each special     */
1569    /* thats has this as    */
1570    /* source we have to    */
1571    /* update               */
1572    /************************/
1573
1574    for(j=0;j<gm->spec_len;j++) {
1575      auto CellState * specstate;
1576      specstate = gm->special[j];
1577
1578
1579      for(k=0;k<specstate->len;k++) {
1580	if( strcmp(specstate->source[k]->state_source,state->name) == 0) {
1581	  /********************************/
1582	  /* is a special source!         */
1583	  /********************************/
1584	  add_break(dfp);
1585	  add_block_comment(dfp,"state %s is a source for special %s",state->name,specstate->name);
1586
1587
1588	  if( specstate->source[k]->position != SOURCE_POS_ALL) {
1589	    add_block_comment(dfp,"Has restricted position");
1590	    expr(dfp,"if( %s )",source_allowed_statement(specstate->source[k]->position,specstate->source[k]->offi,specstate->source[k]->offj));
1591	    startbrace(dfp);
1592	  }
1593
1594
1595	  expr(dfp,"temp = score + (%s) + (%s) ",specstate->source[k]->calc_expr,specstate->calc_expr == NULL ? "0" : specstate->calc_expr );
1596
1597
1598	  if(gm->calcfunc != NULL ) {
1599	    expr(dfp,"%s_%s(%s,i,j,%s) = %s(%s_%s(%s,i,j,%s),temp);",
1600		 gm->name,specialtag,pointertag,specstate->name,
1601		 gm->calcfunc,
1602		 gm->name,specialtag,pointertag,specstate->name);
1603
1604	  } else{
1605
1606	    expr(dfp,"if( temp > %s_%s(%s,i,j,%s) ) ",gm->name,specialtag,pointertag,specstate->name);
1607	    startbrace(dfp);
1608	    expr(dfp,"%s_%s(%s,i,j,%s) = temp",gm->name,specialtag,pointertag,specstate->name);
1609	    if( debug >= 1 )
1610	      expr(dfp,"fprintf(stderr,\"[%%4d,%%4d] new score of %s is %%d\\n\",i,j,temp)",state->name);
1611
1612	    closebrace(dfp);
1613	    add_break(dfp);
1614
1615	  }
1616	  if( specstate->source[k]->position != SOURCE_POS_ALL) {
1617	    closebrace(dfp);
1618	  }
1619
1620	} /* end of if this special state was a source for previous guy */
1621
1622
1623      } /* end for each source of the special state */
1624
1625      if( dfp->code_debug_level > 4) {
1626	expr(dfp,"if( %s_%s(mat,0,j,%s) > IMPOSSIBLY_HIGH_SCORE )",gm->name,specialtag,specstate->name);
1627	hang_expr(dfp,"log_full_error(WARNING,5,\"[%%4d][%%4d] Special state %s Impossibly high score [%%d] found\",i,j,%s_%s(mat,0,j,%s));",specstate->name,gm->name,specialtag,specstate->name);
1628      }
1629
1630
1631    }  /* end for each special state */
1632
1633
1634
1635    if( state->footprint_start < 0 || state->footprint_end > 1 )
1636      {
1637	closebrace(dfp);
1638      }
1639
1640
1641    add_break(dfp);
1642    add_block_comment(dfp,"Finished calculating state %s",state->name);
1643
1644  } /* end of for each state */
1645
1646}
1647
1648
1649void write_score_block(DYNFILE * dfp,GenericMatrix * gm,char * matrixtag,char * pointertag,char * specialtag,boolean use_special)
1650{
1651  write_score_block_debug(dfp,gm,matrixtag,pointertag,specialtag,use_special,0);
1652}
1653
1654
1655%func
1656makes calculate_xxx functions, which
1657is for explicit matrix implementations
1658%%
1659void matrix_calculate_func(DYNFILE * dfp,GenericMatrix * gm)
1660{
1661  FuncInfo * fi;
1662  ArgInfo * ai;
1663
1664  fi = FuncInfo_named_from_varstr(FI_CALLABLE,"calculate_%s",gm->name);
1665  add_line_to_Ftext(fi->ft,"This function calculates the %s matrix when in explicit mode",gm->name);
1666  add_line_to_Ftext(fi->ft,"To allocate the matrix use /allocate_Expl_%s",gm->name);
1667
1668
1669  /** ArgInfo_in_FuncInfo_from_varstr automatically adds ai to fi **/
1670
1671  ai =  ArgInfo_in_FuncInfo_from_varstr(fi,"mat");
1672  ai->desc=stringallocf("%s which contains explicit basematrix memory",gm->name);
1673
1674
1675  start_function_FuncInfo(fi,dfp,"boolean calculate_%s(%s * mat)",gm->name,gm->name);
1676  expr(dfp,"int i;");
1677  expr(dfp,"int j;");
1678  expr(dfp,"int leni;");
1679  expr(dfp,"int lenj;");
1680  expr(dfp,"int tot;");
1681  expr(dfp,"int num;");
1682  add_break(dfp);
1683
1684  expr(dfp,"if( mat->basematrix->type != BASEMATRIX_TYPE_EXPLICIT )");
1685  startbrace(dfp);
1686  warn_expr(dfp,"in calculate_%s, passed a non Explicit matrix type, cannot calculate!",gm->name);
1687  expr(dfp,"return FALSE;");
1688  closebrace(dfp);
1689  add_break(dfp);
1690  expr(dfp,"leni = mat->leni;");
1691  expr(dfp,"lenj = mat->lenj;");
1692
1693
1694  /*** make num and tot ***/
1695
1696  expr(dfp,"tot = leni * lenj;");
1697  expr(dfp,"num = 0;");
1698
1699
1700  /*** see if there any specials to specials to do ***/
1701
1702  add_break(dfp);
1703  expr(dfp,"start_reporting(\"%s Matrix calculation: \");",gm->name);
1704
1705  expr(dfp,"for(j=0;j<lenj;j++)");
1706  startbrace(dfp);
1707  expr(dfp,"auto int score");
1708  expr(dfp,"auto int temp");
1709  expr(dfp,"for(i=0;i<leni;i++)");
1710  startbrace(dfp);
1711
1712  expr(dfp,"if( num%%1000 == 0 )");
1713  hang_expr(dfp,"log_full_error(REPORT,0,\"[%%7d] Cells %%2d%%%%%%%%\",num,num*100/tot);");
1714
1715
1716/**  expr(dfp,"(void) do_cell_%s(mat,i,j)",gm->name); **/
1717
1718  expr(dfp,"num++;");
1719
1720  write_score_block(dfp,gm,"EXPL_MATRIX","mat","EXPL_SPECIAL",TRUE);
1721
1722
1723  closebrace(dfp);
1724  /**** if there are any specials do them here ****/
1725
1726  write_special_block(dfp,gm,"EXPL_MATRIX","EXPL_SPECIAL",NULL);
1727
1728  closebrace(dfp);
1729
1730  /*** stop reporting ***/
1731
1732  expr(dfp,"stop_reporting()");
1733
1734  expr(dfp,"return TRUE");
1735
1736  close_function(dfp);
1737
1738  add_break(dfp);
1739
1740}
1741
1742
1743
1744%func
1745makes calculate_xxx functions, which
1746is for explicit matrix implementations
1747%%
1748void matrix_calculate_func_dpenv(DYNFILE * dfp,GenericMatrix * gm)
1749{
1750  FuncInfo * fi;
1751  ArgInfo * ai;
1752  int i;
1753
1754  fi = FuncInfo_named_from_varstr(FI_CALLABLE,"calculate_dpenv_%s",gm->name);
1755  add_line_to_Ftext(fi->ft,"This function calculates the %s matrix when in explicit mode, subject to the envelope",gm->name);
1756
1757
1758  /** ArgInfo_in_FuncInfo_from_varstr automatically adds ai to fi **/
1759
1760  ai =  ArgInfo_in_FuncInfo_from_varstr(fi,"mat");
1761  ai->desc=stringallocf("%s which contains explicit basematrix memory",gm->name);
1762
1763
1764  start_function_FuncInfo(fi,dfp,"boolean calculate_dpenv_%s(%s * mat,DPEnvelope * dpenv)",gm->name,gm->name);
1765  expr(dfp,"int i;");
1766  expr(dfp,"int j;");
1767  expr(dfp,"int k;");
1768  expr(dfp,"int starti;");
1769  expr(dfp,"int startj;");
1770  expr(dfp,"int endi;");
1771  expr(dfp,"int endj;");
1772  expr(dfp,"int tot;");
1773  expr(dfp,"int num;");
1774  expr(dfp,"int should_calc;");
1775  add_break(dfp);
1776
1777  expr(dfp,"if( mat->basematrix->type != BASEMATRIX_TYPE_EXPLICIT )");
1778  startbrace(dfp);
1779  warn_expr(dfp,"in calculate_%s, passed a non Explicit matrix type, cannot calculate!",gm->name);
1780  expr(dfp,"return FALSE;");
1781  closebrace(dfp);
1782  add_break(dfp);
1783
1784
1785  /*** make num and tot ***/
1786
1787  expr(dfp,"prepare_DPEnvelope(dpenv)");
1788
1789  expr(dfp,"starti = dpenv->starti");
1790  expr(dfp,"if( starti < 0 )");
1791  hang_expr(dfp,"starti = 0");
1792
1793  expr(dfp,"startj = dpenv->startj");
1794  expr(dfp,"if( startj < 0 )");
1795  hang_expr(dfp,"startj = 0");
1796
1797  expr(dfp,"endi = dpenv->endi");
1798  expr(dfp,"if( endi > mat->leni )");
1799  hang_expr(dfp,"endi = mat->leni");
1800
1801  expr(dfp,"endj = dpenv->endj");
1802  expr(dfp,"if( endj > mat->lenj )");
1803  hang_expr(dfp,"endj = mat->lenj");
1804
1805
1806  expr(dfp,"tot = (endi-starti) * (endj-startj);");
1807  expr(dfp,"num = 0;");
1808
1809  add_break(dfp);
1810  expr(dfp,"for(j=startj-%d;j<endj;j++)",gm->window_j);
1811  startbrace(dfp);
1812  expr(dfp,"for(i=%d;i<mat->leni;i++)",gm->window_i);
1813  startbrace(dfp);
1814  for(i=0;i<gm->len;i++) {
1815    expr(dfp,"%s_EXPL_MATRIX(mat,i,j,%s) = %s;",gm->name,gm->state[i]->name,gm->state[i]->def_score);
1816  }
1817  closebrace(dfp);
1818  closebrace(dfp);
1819
1820
1821  expr(dfp,"for(j=-%d;j<mat->lenj;j++)",gm->window_j);
1822  startbrace(dfp);
1823  for(i=0;i<gm->spec_len;i++) {
1824    expr(dfp,"%s_EXPL_SPECIAL(mat,i,j,%s) = %s;",gm->name,gm->special[i]->name,gm->special[i]->def_score);
1825  }
1826  closebrace(dfp);
1827
1828
1829  /*** see if there any specials to specials to do ***/
1830
1831  add_break(dfp);
1832  expr(dfp,"start_reporting(\"%s Matrix calculation: \");",gm->name);
1833
1834  expr(dfp,"for(j=startj;j<endj;j++)");
1835  startbrace(dfp);
1836  expr(dfp,"auto int score");
1837  expr(dfp,"auto int temp");
1838  expr(dfp,"for(i=starti;i<endi;i++)");
1839  startbrace(dfp);
1840
1841  add_block_comment(dfp,"Check if is in envelope - code identical to is_in_DPEnvelope, but aggressively inlined here for speed");
1842
1843  expr(dfp,"should_calc = 0;");
1844  expr(dfp,"for(k=0;k<dpenv->len;k++)");
1845  startbrace(dfp);
1846  expr(dfp,"auto DPUnit * u;");
1847  expr(dfp,"u = dpenv->dpu[k];");
1848  expr(dfp,"switch(u->type)");
1849  startbrace(dfp);
1850  expr(dfp,"case DPENV_RECT :");
1851  startcase(dfp);
1852  expr(dfp,"if( i >= u->starti && j >= u->startj && i <= (u->starti+u->height) && j <= (u->startj+u->length))");
1853  hang_expr(dfp,"should_calc = 1");
1854  expr(dfp,"break;");
1855  closecase(dfp);
1856  expr(dfp,"case DPENV_DIAG :");
1857  startcase(dfp);
1858  expr(dfp,"if(  abs( (i-j) - (u->starti-u->startj)) <= u->height && i+j >= u->starti+u->startj && i+j+u->length >= u->starti+u->startj)");
1859  hang_expr(dfp,"should_calc = 1");
1860  expr(dfp,"break;");
1861  closecase(dfp);
1862  closebrace(dfp);
1863  expr(dfp,"if( should_calc == 1 )");
1864  hang_expr(dfp,"break;");
1865  closebrace(dfp);
1866
1867  expr(dfp,"if( should_calc == 0)");
1868  startbrace(dfp);
1869  for(i=0;i<gm->len;i++) {
1870    expr(dfp,"%s_EXPL_MATRIX(mat,i,j,%s) = %s;",gm->name,gm->state[i]->name,gm->state[i]->def_score);
1871  }
1872  expr(dfp,"continue;");
1873  closebrace(dfp);
1874
1875
1876  add_break(dfp);
1877
1878
1879  expr(dfp,"if( num%%1000 == 0 )");
1880  hang_expr(dfp,"log_full_error(REPORT,0,\"[%%7d] Cells %%2d%%%%%%%%\",num,num*100/tot);");
1881
1882
1883/**  expr(dfp,"(void) do_cell_%s(mat,i,j)",gm->name); **/
1884
1885  expr(dfp,"num++;");
1886
1887  write_score_block(dfp,gm,"EXPL_MATRIX","mat","EXPL_SPECIAL",TRUE);
1888
1889
1890  closebrace(dfp);
1891  /**** if there are any specials do them here ****/
1892
1893  write_special_block(dfp,gm,"EXPL_MATRIX","EXPL_SPECIAL",NULL);
1894
1895  closebrace(dfp);
1896
1897  /*** stop reporting ***/
1898
1899  expr(dfp,"stop_reporting()");
1900
1901  expr(dfp,"return TRUE");
1902
1903  close_function(dfp);
1904
1905  add_break(dfp);
1906
1907}
1908
1909
1910
1911void do_special_function(DYNFILE * dfp,GenericMatrix * gm)
1912{
1913
1914  if( gm->specialtospecial == FALSE ) {
1915    warn("Cannot make special cell function if no special to special use");
1916    return;
1917  }
1918
1919  start_function(dfp,"void do_special_cells_%s(%s * mat,int j)",gm->name,gm->name);
1920
1921  expr(dfp,"register int score");
1922  expr(dfp,"register int temp");
1923
1924  write_special_block(dfp,gm,"EXPL_MATRIX","EXPL_SPECIAL",NULL);
1925
1926  add_break(dfp);
1927
1928  add_break(dfp);
1929  expr(dfp,"return;");
1930  close_function(dfp);
1931  add_break(dfp);
1932
1933}
1934
1935%func
1936useful function for debugging, but not
1937currently used
1938%%
1939void do_cell_function(DYNFILE * dfp,GenericMatrix * gm)
1940{
1941  start_function(dfp,"int do_cell_%s(%s * mat,int i,int j)",gm->name,gm->name);
1942
1943  expr(dfp,"register int score;");
1944  expr(dfp,"register int temp;");
1945  expr(dfp,"register int state;");
1946
1947  write_score_block(dfp,gm,"EXPL_MATRIX","mat","EXPL_SPECIAL",TRUE);
1948
1949  expr(dfp,"return 0");
1950
1951  close_function(dfp);
1952
1953
1954  add_break(dfp);
1955}
1956
1957%func
1958This writes the special to special block movements, if needed.
1959
1960If bestscore is not NULL, it assummes that this is the name of
1961an int variable which should hold the bestscore by updating
1962from the end state
1963%type internal
1964%arg
1965dfp dynfile output
1966gm  r matrix structure
1967matrix r name of main matrix tag eg EXPL_MATRIX
1968special r name of special tag  eg   EXPL_SPECIAL
1969bestscore r if not NULL, name of scoped variable for updating bestscore
1970%%
1971void write_special_block(DYNFILE * dfp,GenericMatrix * gm,char * matrix,char * special,char * bestscore)
1972{
1973  register int i;
1974  register int j;
1975
1976  for(i=0;i<gm->spec_len;i++) {
1977    auto CellState * state;
1978    state = gm->special[i];
1979    add_break(dfp);
1980    if( state->specialtospecial == FALSE ) {
1981      add_block_comment(dfp,"Special state %s has no special to special movements",state->name);
1982      continue;
1983    }
1984
1985    add_block_comment(dfp,"Special state %s has special to speical",state->name);
1986
1987    add_block_comment(dfp,"Set score to current score (remember, state probably updated during main loop");
1988    expr(dfp,"score = %s_%s(mat,0,j,%s);",gm->name,special,state->name);
1989    add_break(dfp);
1990    for(j=0;j < state->len;j++) {
1991      auto CellSource * source;
1992      source = state->source[j];
1993
1994
1995
1996      if( source->isspecial == FALSE ) {
1997	add_block_comment(dfp,"Source %s for state %s is not special... already calculated",source->state_source,state->name);
1998	continue; /** back to for j **/
1999      }
2000
2001      add_block_comment(dfp,"Source %s is a special source for %s",source->state_source,state->name);
2002
2003      if( state->source[j]->position != SOURCE_POS_ALL) {
2004	add_block_comment(dfp,"Has restricted position");
2005	expr(dfp,"if( %s )",source_allowed_statement(state->source[j]->position,state->source[j]->offi,state->source[j]->offj));
2006	startbrace(dfp);
2007      }
2008
2009
2010      expr(dfp,"temp = %s_%s(mat,0,j - %d,%s) + (%s) + (%s)",gm->name,special,source->offj,source->state_source,
2011source->calc_expr,state->calc_expr == NULL ? "0" : state->calc_expr);
2012
2013      if( gm->calcfunc != NULL ) {
2014	expr(dfp,"score = %s(score,temp);",gm->calcfunc);
2015      } else {
2016	expr(dfp,"if( temp > score )");
2017	hang_expr(dfp,"score = temp");
2018      }
2019
2020      if( state->source[j]->position != SOURCE_POS_ALL) {
2021	closebrace(dfp);
2022      }
2023
2024      add_break(dfp);
2025    }
2026    add_block_comment(dfp,"Put back score... (now updated!)");
2027    expr(dfp,"%s_%s(mat,0,j,%s) = score;",gm->name,special,state->name);
2028    add_block_comment(dfp,"Finished updating state %s",state->name);
2029    add_break(dfp);
2030
2031    if( dfp->code_debug_level > 5) {
2032      expr(dfp,"if( score > IMPOSSIBLY_HIGH_SCORE )");
2033      hang_expr(dfp,"log_full_error(WARNING,5,\"[%%4d][%%4d] Special state %s Impossibly high score [%%d] found\",i,j,score);",state->name);
2034    }
2035  }
2036  /*
2037  if( bestscore == NULL && gm->calcfunc == NULL )
2038    return;
2039  */
2040
2041  /** ok - update of endscore stuff **/
2042
2043  for(i=0;i<gm->spec_len;i++) {
2044    auto CellState * state;
2045    state = gm->special[i];
2046
2047    if( state->is_end == TRUE ) {
2048
2049      if( bestscore == NULL ) {
2050	/* means we have a calc func with no bestscore */
2051	/*expr(dfp,"%s_%s(mat,0,j,%s) = %s(%s_%s(mat,0,j,%s),%s_%s(mat,0,j-1,%s));",gm->name,special,state->name,gm->calcfunc,gm->name,special,state->name,gm->name,special,state->name);*/
2052      } else {
2053	/* we are in a database search routine */
2054	if( gm->calcfunc != NULL ) {
2055	  expr(dfp,"bestscore = %s(bestscore,%s_%s(mat,0,j,%s));",gm->calcfunc,gm->name,special,state->name);
2056	} else {
2057	  expr(dfp,"if( bestscore < %s_%s(mat,0,j,%s) )",gm->name,special,state->name);
2058	  hang_expr(dfp,"bestscore = %s_%s(mat,0,j,%s)",gm->name,special,state->name);
2059	}
2060      }
2061
2062      break;
2063    }
2064  }
2065
2066}
2067
2068void write_search_distributor_func(DYNFILE * dfp,GenericMatrix * gm,DPImplementation * dpi)
2069{
2070  int i;
2071  char buffer[MAXLINE];
2072  FuncInfo * fi;
2073  boolean qdb = FALSE;
2074  boolean tdb = FALSE;
2075
2076  fi = FuncInfo_named_from_varstr(FI_CALLABLE,"search_%s",gm->name);
2077  add_line_to_Ftext(fi->ft,"This function makes a database search of %s",gm->name);
2078  add_line_to_Ftext(fi->ft,"It uses the dbsi structure to choose which implementation");
2079  add_line_to_Ftext(fi->ft,"to use of the database searching. This way at run time you");
2080  add_line_to_Ftext(fi->ft,"can switch between single threaded/multi-threaded or hardware");
2081
2082  if( gm->qtype != NULL && gm->qtype->is_database == TRUE)
2083    qdb = TRUE;
2084  if( gm->ttype != NULL && gm->ttype->is_database == TRUE)
2085    tdb = TRUE;
2086
2087  if( qdb == TRUE)
2088    sprintf(buffer,"Search_Return_Type search_%s(DBSearchImpl * dbsi,Hscore * out,%s querydb,",gm->name,gm->qtype->database_type);
2089  else sprintf(buffer,"Search_Return_Type search_%s(DBSearchImpl * dbsi,Hscore * out,%s %s,",gm->name,gm->query->element_type,gm->query->name);
2090
2091  if( tdb == TRUE) {
2092    strcat(buffer,gm->ttype->database_type);
2093    strcat(buffer," targetdb ");
2094  } else {
2095    strcat(buffer,gm->target->element_type);
2096    strcat(buffer," ");
2097    strcat(buffer,gm->target->name);
2098    strcat(buffer," ");
2099  }
2100
2101  for(i=0;i<gm->res_len;i++) {
2102    strcat(buffer,",");
2103    strcat(buffer,gm->resource[i]->element_type);
2104    strcat(buffer," ");
2105    strcat(buffer,gm->resource[i]->name);
2106  }
2107  strcat(buffer,")");
2108
2109
2110  start_function_FuncInfo(fi,dfp,buffer);
2111  if( dpi->do_threads == TRUE && can_do_threads(gm) == TRUE ) {
2112    macro(dfp,"#ifdef PTHREAD");
2113    expr(dfp,"int i;");
2114    expr(dfp,"int thr_no;");
2115    expr(dfp,"pthread_attr_t pat;");
2116    expr(dfp,"struct thread_pool_holder_%s * holder",gm->name);
2117    macro(dfp,"#endif");
2118  }
2119
2120  expr(dfp,"if( out == NULL )");
2121  startbrace(dfp);
2122  warn_expr(dfp,"Passed in a null Hscore object into search_%s. Can't process results!",gm->name);
2123  expr(dfp,"return SEARCH_ERROR;");
2124  closebrace(dfp);
2125
2126  expr(dfp,"if( dbsi == NULL )");
2127  startbrace(dfp);
2128  warn_expr(dfp,"Passed in a null DBSearchImpl object into search_%s. Can't process results!",gm->name);
2129  expr(dfp,"return SEARCH_ERROR;");
2130  closebrace(dfp);
2131
2132  if( dpi->db_trace_level == 0 ) {
2133    expr(dfp,"if( dbsi->trace_level > 0 ) ");
2134    hang_expr(dfp,"warn(\"Although you are asking at run-time for database tracing, the %s matrix was not compiled with database tracing. No tracing will be made\");",gm->name);
2135  } else {
2136    expr(dfp,"if( dbsi->trace_level > %d ) ",dpi->db_trace_level);
2137    hang_expr(dfp,"warn(\"Asking for trace level of %%d in database search for %s, but it was compiled with a trace level of %d. Not all trace statements can be shown\",dbsi->trace_level);",gm->name);
2138  }
2139
2140
2141  /* now to figure out what implementations should be run! */
2142
2143  expr(dfp,"switch(dbsi->type)");
2144  startbrace_tag(dfp,"switch on implementation");
2145  expr(dfp,"case DBSearchImpl_Serial :");
2146  startcase(dfp);
2147  if( qdb == TRUE)
2148    sprintf(buffer,"return serial_search_%s(out,querydb,",gm->name);
2149  else sprintf(buffer,"return serial_search_%s(out,%s,",gm->name,gm->query->name);
2150
2151  if( tdb == TRUE) {
2152    strcat(buffer," targetdb ");
2153  } else {
2154    strcat(buffer,gm->target->name);
2155    strcat(buffer," ");
2156  }
2157
2158  for(i=0;i<gm->res_len;i++) {
2159    strcat(buffer,",");
2160    strcat(buffer,gm->resource[i]->name);
2161  }
2162  strcat(buffer,");");
2163
2164  expr(dfp,buffer);
2165  closecase(dfp);
2166
2167  expr(dfp,"case DBSearchImpl_Pthreads :");
2168  startcase(dfp);
2169
2170  if( dpi->do_threads == TRUE && can_do_threads(gm) == TRUE ) {
2171    macro(dfp,"#ifdef PTHREAD");
2172    expr(dfp,"holder = (struct thread_pool_holder_%s *) ckalloc(sizeof(struct thread_pool_holder_%s));",gm->name,gm->name);
2173    expr(dfp,"if( holder == NULL ) ");
2174    startbrace(dfp);
2175    warn_expr(dfp,"Unable to allocated thread pool datastructure...");
2176    expr(dfp,"return SEARCH_ERROR;");
2177    closebrace(dfp);
2178    expr(dfp,"holder->out = out;");
2179    if( dpi->db_trace_level > 0 )
2180      expr(dfp,"holder->dbsi = dbsi;");
2181
2182    if( qdb == TRUE ) {
2183      expr(dfp,"holder->querydb = querydb;");
2184    } else {
2185      expr(dfp,"holder->%s = %s;",gm->query->name,gm->query->name);
2186    }
2187
2188    if( tdb == TRUE ) {
2189      expr(dfp,"holder->targetdb = targetdb;");
2190    } else {
2191      expr(dfp,"holder->%s = %s;",gm->target->name,gm->target->name);
2192    }
2193
2194    for(i=0;i<gm->res_len;i++) {
2195      expr(dfp,"holder->%s = %s;",gm->resource[i]->name,gm->resource[i]->name);
2196    }
2197    expr(dfp,"if( pthread_mutex_init(&(holder->input_lock),NULL) != 0 ) ");
2198    expr(dfp,"fatal(\"Unable to iniated input mutex lock\");");
2199    expr(dfp,"if( pthread_mutex_init(&(holder->output_lock),NULL) != 0 ) ");
2200    expr(dfp,"fatal(\"Unable to iniated output mutex lock\");");
2201
2202
2203    add_block_comment(dfp,"Let us rock!");
2204    expr(dfp,"thr_no = number_of_threads_DBSearchImpl(dbsi);");
2205    expr(dfp,"holder->pool = ckcalloc (thr_no,sizeof(pthread_t));");
2206    expr(dfp,"if( holder->pool == NULL ) ");
2207    startbrace(dfp);
2208    warn_expr(dfp,"Unable to allocated thread pools");
2209    expr(dfp,"return SEARCH_ERROR;");
2210    closebrace(dfp);
2211
2212    add_block_comment(dfp,"Build a thread attribute to make sure we get the most out of SMP boxes");
2213    expr(dfp,"pthread_attr_init(&pat)");
2214    add_block_comment(dfp,"Give thread libraries a hint that threads should be kernel threads");
2215    macro(dfp,"#ifndef __sgi /* SGI can't set system scope ... */");
2216    macro(dfp,"#ifdef  HAS_PTHREAD_SETSCOPE");
2217    expr(dfp,"pthread_attr_setscope(&pat, PTHREAD_SCOPE_SYSTEM);");
2218    macro(dfp,"#endif /* set scope */");
2219    macro(dfp,"#endif /* sgi */");
2220    add_block_comment(dfp,"Give thread libraries a hint that there are num of threads to run");
2221    macro(dfp,"#ifdef HAS_PTHREAD_SETCONCURRENCY");
2222    expr(dfp,"pthread_setconcurrency(thr_no+1);");
2223    macro(dfp,"#endif /* set concurrency */");
2224
2225    expr(dfp,"for(i=0;i<thr_no;i++)");
2226    startbrace(dfp);
2227    expr(dfp,"if( pthread_create(holder->pool+i,&pat,thread_loop_%s,(void *)holder) )",gm->name);
2228    hang_expr(dfp,"fatal(\"Unable to create a thread!\");");
2229
2230    closebrace(dfp);
2231
2232
2233    add_block_comment(dfp,"Now - wait for all the threads to exit");
2234
2235    expr(dfp,"for(i=0;i<thr_no;i++)");
2236    startbrace(dfp);
2237    expr(dfp,"if( pthread_join(holder->pool[i],NULL) != 0 )",gm->name);
2238    hang_expr(dfp,"fatal(\"Unable to join a thread!\");");
2239
2240    closebrace(dfp);
2241
2242    /*    expr(dfp,"pthread_attr_destroy(&pat);"); */
2243
2244    add_block_comment(dfp,"Deallocate the thread structures");
2245    expr(dfp,"ckfree(holder->pool)");
2246    expr(dfp,"ckfree(holder)");
2247
2248    expr(dfp,"return SEARCH_OK;");
2249    macro(dfp,"#else /* not compiled with threads */");
2250    warn_expr(dfp,"You did not specifiy the PTHREAD compile when compiled the C code for %s",gm->name);
2251    macro(dfp,"#endif /* finished threads */");
2252  } else {
2253    warn_expr(dfp,"This matrix %s was not dyc compiled with thread support",gm->name);
2254    expr(dfp,"return SEARCH_ERROR;");
2255  }
2256
2257  closecase(dfp);
2258  expr(dfp,"default :");
2259  startcase(dfp);
2260  expr(dfp,"warn(\"database search implementation %%s was not provided in the compiled dynamite file from %s\",impl_string_DBSearchImpl(dbsi));",gm->name);
2261  expr(dfp,"return SEARCH_ERROR;");
2262  closecase(dfp);
2263  closebrace(dfp);
2264
2265  add_break(dfp);
2266  close_function(dfp);
2267  add_break(dfp);
2268
2269}
2270
2271
2272
2273void write_GenericMatrix_header(DYNFILE * dfp,GenericMatrix * gm,DPImplementation * dpi)
2274{
2275  StructHolder * temp;
2276  StructElement * se;
2277  char buffer[MAXLINE];
2278
2279  write_StructHolder_header(dfp,gm->sh);
2280  if( dpi->do_threads == TRUE ) {
2281    if( can_do_threads(gm) == FALSE ) {
2282      warn("Asking for threads, but the types in this matrix %s are not thread safe",gm->name);
2283    } else {
2284      write_thread_struct(dfp,dpi,gm);
2285    }
2286  }
2287
2288  temp = StructHolder_alloc_std();
2289  sprintf(buffer,"%s_access_func_holder",gm->name);
2290  temp->name = stringalloc(buffer);
2291
2292  sprintf(buffer,"int (*access_main)(%s*,int,int,int)",gm->name);
2293  se = basic_add_StructElement(temp,"access_main","int");
2294  se->element_type = stringalloc(buffer);
2295  se->isfunc = TRUE;
2296
2297  sprintf(buffer,"int (*access_special)(%s*,int,int,int)",gm->name);
2298  se = basic_add_StructElement(temp,"access_special","int");
2299  se->element_type = stringalloc(buffer);
2300  se->isfunc = TRUE;
2301
2302  write_StructHolder_header(dfp,temp);
2303
2304  free_StructHolder(temp);
2305
2306/**	write_StructHolder_header(gm->search_sh,ofp); **/
2307
2308  /**
2309     load_file(ofp);
2310
2311     write_memory_macros(gm);
2312
2313     close_file();
2314  **/
2315
2316}
2317
2318/*****************************************
2319   this function writes the C functions themselves
2320
2321   it does not load up ofp into the header part of
2322   writec, and you must do that first if you want
2323   the function prototypes to be put into the header
2324   files
2325
2326******************************************/
2327
2328void write_GenericMatrix_func(DYNFILE * dfp,GenericMatrix * gm,DPImplementation * dpi)
2329{
2330
2331  fprintf(dfp->func,"\n\n  /*****************   C functions  ****************/\n");
2332  fprintf(dfp->func,"  /*             Written using dynamite            */\n");
2333  fprintf(dfp->func,"  /*            %s           */\n",now_string());
2334  fprintf(dfp->func,"  /*            email birney@sanger.ac.uk          */\n");
2335  fprintf(dfp->func,"  /* http://www.sanger.ac.uk/Users/birney/dynamite */\n");
2336  fprintf(dfp->func,"  /*************************************************/\n");
2337  fprintf(dfp->func,"\n\n");
2338  fprintf(dfp->func,"  /* Please report any problems or bugs to         */\n");
2339  fprintf(dfp->func,"  /* Ewan Birney, birney@sanger.ac.uk              */\n");
2340  fprintf(dfp->func,"\n\n");
2341
2342
2343
2344  add_block_comment(dfp,"basic set of macros to map states to numbers");
2345
2346  write_memory_macros(dfp,gm);
2347  write_search_macros(dfp,gm,dpi);
2348
2349  add_break(dfp);
2350
2351  if( dpi->dydebug == TRUE ) {
2352    write_debug_funcs(dfp,gm);
2353  }
2354
2355  write_shatter_functions(dfp,gm,dpi);
2356
2357
2358  write_search_distributor_func(dfp,gm,dpi);
2359
2360  if( dpi->do_threads == TRUE && can_do_threads(gm) ) {
2361    write_thread_loop(dfp,dpi,gm);
2362  }
2363
2364  if( dpi->doprob == TRUE ) {
2365    write_probabilistic_models(dfp,gm,dpi);
2366  }
2367
2368  make_search_loop_function(dfp,gm);
2369
2370
2371
2372  write_one_score_GenericMatrix(dfp,gm,dpi);
2373
2374
2375/**  write_database_scan_function(gm); **/
2376/**
2377  if( make_Compugen_functions_if_possible(gm) == TRUE) {
2378    log_full_error(INFO,0,"Able to make Compugen functions");
2379  }
2380**/
2381
2382  /*** testing ***/
2383
2384  one_shot_aln_func(dfp,gm,dpi);
2385
2386  write_safe_alloc_function(dfp,gm);
2387
2388  alloc_expl_func_GenericMatrix(dfp,gm);
2389
2390  init_matrix_func(dfp,gm);
2391
2392  recalculate_PackAln_func(dfp,gm);
2393
2394  /*** this is found in dynshadow.dy ***/
2395
2396  add_block_comment(dfp,"divide and conquor macros are next");
2397  write_dc_functions(dfp,gm);
2398
2399  /*write_pal_to_ars_func(dfp,gm);*/
2400
2401  write_aln_conversion_func(dfp,gm);
2402
2403  write_alncconvert_make_func(dfp,gm);
2404
2405  write_expl_read_func(dfp,gm);
2406
2407  write_expl_access_funcs(dfp,gm);
2408
2409
2410  write_basic_read_func(dfp,gm);
2411
2412
2413
2414  find_end_func(dfp,gm);
2415
2416  debug_func(dfp,gm);
2417
2418  write_max_calc_func(dfp,gm);
2419
2420  write_special_max_calc_func(dfp,gm);
2421
2422  matrix_calculate_func(dfp,gm);
2423
2424  matrix_calculate_func_dpenv(dfp,gm);
2425
2426  write_simplealloc_function(dfp,gm->sh);
2427  write_free_function(dfp,gm->sh);
2428
2429
2430  add_break(dfp);
2431
2432  return;
2433}
2434
2435%}
2436
2437