1 #ifdef _cplusplus
2 extern "C" {
3 #endif
4 #include "genewisemodeldb.h"
5 
6 
7 
8 /* Function:  init_GeneWiseDB(gdb,return_status)
9  *
10  * Descrip:    inits a genewise database. Remember this is used
11  *             for both genomic and cdna searches
12  *
13  *
14  *
15  * Arg:                  gdb [UNKN ] Undocumented argument [GeneWiseDB *]
16  * Arg:        return_status [UNKN ] Undocumented argument [int *]
17  *
18  * Return [UNKN ]  Undocumented return value [GeneWiseScore *]
19  *
20  */
21 # line 47 "genewisemodeldb.dy"
init_GeneWiseDB(GeneWiseDB * gdb,int * return_status)22 GeneWiseScore * init_GeneWiseDB(GeneWiseDB * gdb,int * return_status)
23 {
24   ThreeStateModel * tsm;
25   GeneWise * gw;
26   GeneWiseScore * gws;
27 
28 
29   *return_status = DB_RETURN_ERROR;
30 
31   if( gdb->is_single == TRUE ) {
32     *return_status = DB_RETURN_OK;
33     return gdb->gws;
34   }
35 
36   if( open_ThreeStateDB(gdb->tdb) == FALSE ) {
37     warn("Could not open three state db, so no genewisemodel possible!");
38     return NULL;
39   }
40 
41   tsm = read_TSM_ThreeStateDB(gdb->tdb,return_status);
42 
43   if( *return_status == DB_RETURN_ERROR) {
44     warn("Cannot read a ThreeStateModelDB for the GeneWiseDB. Problem!");
45     return NULL;
46   }
47 
48   gw  =  GeneWise_from_ThreeStateModel_GDB(tsm,gdb);
49   gws =  GeneWiseScore_from_GeneWise(gw);
50 
51   free_ThreeStateModel(tsm);
52   free_GeneWise(gw);
53 
54 
55   return gws;
56 }
57 
58 /* Function:  reload_GeneWiseDB(prev,gdb,return_status)
59  *
60  * Descrip:    Reloads a genewise database
61  *
62  *
63  *
64  * Arg:                 prev [UNKN ] Undocumented argument [GeneWiseScore *]
65  * Arg:                  gdb [UNKN ] Undocumented argument [GeneWiseDB *]
66  * Arg:        return_status [UNKN ] Undocumented argument [int *]
67  *
68  * Return [UNKN ]  Undocumented return value [GeneWiseScore *]
69  *
70  */
71 # line 87 "genewisemodeldb.dy"
reload_GeneWiseDB(GeneWiseScore * prev,GeneWiseDB * gdb,int * return_status)72 GeneWiseScore * reload_GeneWiseDB(GeneWiseScore * prev,GeneWiseDB * gdb,int * return_status)
73 {
74   ThreeStateModel * tsm;
75   GeneWise * gw;
76   GeneWiseScore * gws;
77 
78 
79 
80   if( gdb->is_single == TRUE ) {
81     *return_status = DB_RETURN_END;
82     return NULL;
83   }
84 
85   if( prev != NULL)
86     free_GeneWiseScore(prev);
87 
88 
89   *return_status = DB_RETURN_ERROR;
90 
91   tsm = read_TSM_ThreeStateDB(gdb->tdb,return_status);
92 
93   if( *return_status != DB_RETURN_OK) {
94     if( *return_status == DB_RETURN_END) {
95       return NULL;
96     }
97 
98     warn("Cannot read a ThreeStateModelDB for the GeneWiseDB. Problem!");
99     return NULL;
100   }
101 
102   gw  =  GeneWise_from_ThreeStateModel_GDB(tsm,gdb);
103   gws =  GeneWiseScore_from_GeneWise(gw);
104 
105 
106 
107   free_ThreeStateModel(tsm);
108   free_GeneWise(gw);
109 
110   *return_status = DB_RETURN_OK;
111 
112   return gws;
113 }
114 
115 /* Function:  close_GeneWiseDB(gws,gdb)
116  *
117  * Descrip:    closes a GeneWiseDB
118  *
119  *
120  * Arg:        gws [UNKN ] Undocumented argument [GeneWiseScore *]
121  * Arg:        gdb [UNKN ] Undocumented argument [GeneWiseDB *]
122  *
123  * Return [UNKN ]  Undocumented return value [boolean]
124  *
125  */
126 # line 133 "genewisemodeldb.dy"
close_GeneWiseDB(GeneWiseScore * gws,GeneWiseDB * gdb)127 boolean close_GeneWiseDB(GeneWiseScore * gws,GeneWiseDB * gdb)
128 {
129   if( gdb->is_single == TRUE ) {
130     return TRUE;
131   }
132 
133   if( gws != NULL)
134     free_GeneWiseScore(gws);
135 
136   return close_ThreeStateDB(NULL,gdb->tdb);
137 }
138 
139 /* Function:  dataentry_add_GeneWiseDB(de,gws,gdb)
140  *
141  * Descrip:    adds dataentry stuff to a query. Relies completely
142  *             on threestatedb
143  *
144  *
145  * Arg:         de [UNKN ] Undocumented argument [DataEntry *]
146  * Arg:        gws [UNKN ] Undocumented argument [GeneWiseScore *]
147  * Arg:        gdb [UNKN ] Undocumented argument [GeneWiseDB *]
148  *
149  * Return [UNKN ]  Undocumented return value [boolean]
150  *
151  */
152 # line 149 "genewisemodeldb.dy"
dataentry_add_GeneWiseDB(DataEntry * de,GeneWiseScore * gws,GeneWiseDB * gdb)153 boolean dataentry_add_GeneWiseDB(DataEntry * de,GeneWiseScore * gws,GeneWiseDB * gdb)
154 {
155   if( gdb->is_single == TRUE) {
156     if ( gdb->gws->name == NULL ) {
157       warn("No name for a single GeneWiseDB unit.");
158       de->name = stringalloc("NoName");
159     } else {
160       de->name = stringalloc(gdb->gws->name);
161     }
162 
163     return TRUE;
164   }
165 
166 
167   /*de->name = stringalloc(gws->name);*/
168 
169   /* otherwise, pass it on to tdb */
170   dataentry_add_ThreeStateDB(de,NULL,gdb->tdb);
171   if( de->name == NULL ) {
172     if( gws->name != NULL ) {
173       de->name = stringalloc(gws->name);
174     }
175   }
176   return TRUE;
177 }
178 
179 /* Function:  GeneWise_from_ThreeStateModel_GDB(tsm,gdb)
180  *
181  * Descrip:    Makes a genewise models from the threestatemodel
182  *             and parameters held in the GeneWiseDB
183  *
184  *
185  * Arg:        tsm [UNKN ] Undocumented argument [ThreeStateModel *]
186  * Arg:        gdb [UNKN ] Undocumented argument [GeneWiseDB *]
187  *
188  * Return [UNKN ]  Undocumented return value [GeneWise *]
189  *
190  */
191 # line 180 "genewisemodeldb.dy"
GeneWise_from_ThreeStateModel_GDB(ThreeStateModel * tsm,GeneWiseDB * gdb)192 GeneWise * GeneWise_from_ThreeStateModel_GDB(ThreeStateModel * tsm,GeneWiseDB * gdb)
193 {
194   int i;
195   GeneWise * out;
196 
197 
198   if( gdb->gpara != NULL ) {
199     out=GeneWise_from_ThreeStateModel(tsm,gdb->gpara->gp,gdb->gpara->cm,gdb->allN,gdb->gpara->gwcm);
200     if( out == NULL ) {
201       return NULL;
202     }
203 
204     if( gdb->gpara->modelled_splice == FALSE)
205       flatten_balance_scores_GeneWise(out);
206   } else {
207     out = GeneWise_alloc_len(tsm->len);
208     out->name = stringalloc(tsm->name);
209 
210     for(i=0;i<tsm->len;i++) {
211       add_GeneWise(out,GeneWiseSegment_from_ThreeStateUnit(tsm->unit[i],gdb->cds_factor,gdb->cm,NULL,gdb->allN));
212     }
213   }
214 
215 
216   if( gdb->is_syn == TRUE ) {
217     if( tsm->rm == NULL ) {
218       warn("Bad error. ThreeStateModel does not have a random model!");
219       return free_GeneWise(out);
220     }
221     GeneWise_fold_in_synchronised_RandomModel(out,tsm->rm,gdb->cm,gdb->cm->ct,0.5);
222   } else {
223     GeneWise_fold_in_RandomModelDNA(out,gdb->rmd);
224   }
225 
226   if( gdb->flat_insert == TRUE ) {
227     check_flat_insert(out,1,0,gdb->cm->ct);
228   }
229 
230   return out;
231 }
232 
233 /* Function:  new_GeneWiseDB_cdna(syn,tdb,cp,cm,rmd,use_syn,flat_insert,allN)
234  *
235  * Descrip:    makes a new GeneWiseDB from its component parts,
236  *             assumming a cDNA db.
237  *
238  *             All the objects are hard-linked internally, so you can, if you
239  *             wish, free them once passing them into this function
240  *
241  *
242  * Arg:                syn [UNKN ] if ture, use a synchronous coding model vs internally stored tdb rm's [NullString]
243  * Arg:                tdb [UNKN ] three state model db to use [ThreeStateDB *]
244  * Arg:                 cp [UNKN ] codon parser function to remove from match state [cDNAParser *]
245  * Arg:                 cm [UNKN ] Undocumented argument [CodonMapper *]
246  * Arg:                rmd [UNKN ] random model (dna) [RandomModelDNA *]
247  * Arg:            use_syn [UNKN ] Undocumented argument [boolean]
248  * Arg:        flat_insert [UNKN ] Undocumented argument [boolean]
249  * Arg:               allN [UNKN ] Undocumented argument [Probability]
250  *
251  * Return [UNKN ]  Undocumented return value [GeneWiseDB *]
252  *
253  */
254 # line 233 "genewisemodeldb.dy"
new_GeneWiseDB_cdna(ThreeStateDB * tdb,cDNAParser * cp,CodonMapper * cm,RandomModelDNA * rmd,boolean use_syn,boolean flat_insert,Probability allN)255 GeneWiseDB * new_GeneWiseDB_cdna(ThreeStateDB * tdb,cDNAParser * cp,CodonMapper * cm,RandomModelDNA * rmd,boolean use_syn,boolean flat_insert,Probability allN)
256 {
257   GeneWiseDB * out;
258 
259   out = GeneWiseDB_alloc();
260 
261   out->tdb = hard_link_ThreeStateDB(tdb);
262   out->is_single = FALSE;
263 
264   out->rmd = hard_link_RandomModelDNA(rmd);
265   out->cm = hard_link_CodonMapper(cm);
266   out->cds_factor = (1.0 - removed_probability_from_cds_cdna(cp));
267   out->is_syn = use_syn;
268   out->allN = allN;
269   out->flat_insert = flat_insert;
270   return out;
271 }
272 
273 /* Function:  new_GeneWiseDB(tdb,gp,rmd,use_syn,allN)
274  *
275  * Descrip:    makes a new GeneWiseDB from its component parts.
276  *             All the objects are hard-linked internally, so you can, if you
277  *             wish, free them once passing them into this function
278  *
279  *
280  * Arg:            tdb [UNKN ] Undocumented argument [ThreeStateDB *]
281  * Arg:             gp [UNKN ] Undocumented argument [GeneParameter21 *]
282  * Arg:            rmd [UNKN ] Undocumented argument [RandomModelDNA *]
283  * Arg:        use_syn [UNKN ] Undocumented argument [boolean]
284  * Arg:           allN [UNKN ] Undocumented argument [Probability]
285  *
286  * Return [UNKN ]  Undocumented return value [GeneWiseDB *]
287  *
288  */
289 # line 256 "genewisemodeldb.dy"
new_GeneWiseDB(ThreeStateDB * tdb,GeneParameter21 * gp,RandomModelDNA * rmd,boolean use_syn,Probability allN)290 GeneWiseDB * new_GeneWiseDB(ThreeStateDB * tdb,GeneParameter21 * gp,RandomModelDNA * rmd,boolean use_syn,Probability allN)
291 {
292   GeneWiseDB * out;
293 
294   out = GeneWiseDB_alloc();
295 
296   out->tdb = hard_link_ThreeStateDB(tdb);
297   out->is_single = FALSE;
298 
299   out->rmd = hard_link_RandomModelDNA(rmd);
300   out->gpara = hard_link_GeneParameter21(gp);
301   out->cm = hard_link_CodonMapper(gp->cm);
302   out->is_syn = use_syn;
303   out->allN   = allN;
304   return out;
305 }
306 
307 /* Function:  new_single_GeneWiseDB(gws)
308  *
309  * Descrip:    makes a new GeneWiseDB from a single GeneWiseScore.
310  *             It hard links it, so you should free it afterwards
311  *             in its own scope.
312  *
313  *
314  * Arg:        gws [UNKN ] Undocumented argument [GeneWiseScore *]
315  *
316  * Return [UNKN ]  Undocumented return value [GeneWiseDB *]
317  *
318  */
319 # line 278 "genewisemodeldb.dy"
new_single_GeneWiseDB(GeneWiseScore * gws)320 GeneWiseDB * new_single_GeneWiseDB(GeneWiseScore * gws)
321 {
322   GeneWiseDB * out;
323 
324   out = GeneWiseDB_alloc();
325   out->gws = hard_link_GeneWiseScore(gws);
326   out->is_single = TRUE;
327 
328   return out;
329 }
330 
331 
332 /* Function:  init_GwLite_GeneWiseDB(gdb,return_status)
333  *
334  * Descrip:    inits a genewise database for gwlite models
335  *
336  *
337  *
338  *
339  * Arg:                  gdb [UNKN ] Undocumented argument [GeneWiseDB *]
340  * Arg:        return_status [UNKN ] Undocumented argument [int *]
341  *
342  * Return [UNKN ]  Undocumented return value [GwLiteScore *]
343  *
344  */
345 # line 295 "genewisemodeldb.dy"
init_GwLite_GeneWiseDB(GeneWiseDB * gdb,int * return_status)346 GwLiteScore * init_GwLite_GeneWiseDB(GeneWiseDB * gdb,int * return_status)
347 {
348   ThreeStateModel * tsm;
349   GeneWise * gw;
350   GwLite * gl;
351   GwLiteScore * gws;
352 
353 
354   *return_status = DB_RETURN_ERROR;
355 
356   if( gdb->is_single == TRUE ) {
357     *return_status = DB_RETURN_OK;
358     /* at the moment, die horribly */
359     fatal("Apologies. We don't handle single genewise model databases for gwlite yet");
360     return gdb->gwls;
361   }
362 
363   if( open_ThreeStateDB(gdb->tdb) == FALSE ) {
364     warn("Could not open three state db, so no genewisemodel possible!");
365     return NULL;
366   }
367 
368   tsm = read_TSM_ThreeStateDB(gdb->tdb,return_status);
369 
370   if( *return_status == DB_RETURN_ERROR) {
371     warn("Cannot read a ThreeStateModelDB for the GeneWiseDB. Problem!");
372     return NULL;
373   }
374 
375   gw  =  GeneWise_from_ThreeStateModel_GDB(tsm,gdb);
376   gl  =  GwLite_from_GeneWise(gw);
377   gws =  GwLiteScore_from_GwLite(gl);
378 
379   free_ThreeStateModel(tsm);
380   free_GeneWise(gw);
381   free_GwLite(gl);
382 
383   return gws;
384 }
385 
386 /* Function:  reload_GwLite_GeneWiseDB(prev,gdb,return_status)
387  *
388  * Descrip:    Reloads a genewise database for a GwLite database
389  *
390  *
391  *
392  * Arg:                 prev [UNKN ] Undocumented argument [GwLiteScore *]
393  * Arg:                  gdb [UNKN ] Undocumented argument [GeneWiseDB *]
394  * Arg:        return_status [UNKN ] Undocumented argument [int *]
395  *
396  * Return [UNKN ]  Undocumented return value [GwLiteScore *]
397  *
398  */
399 # line 339 "genewisemodeldb.dy"
reload_GwLite_GeneWiseDB(GwLiteScore * prev,GeneWiseDB * gdb,int * return_status)400 GwLiteScore * reload_GwLite_GeneWiseDB(GwLiteScore * prev,GeneWiseDB * gdb,int * return_status)
401 {
402   ThreeStateModel * tsm;
403   GeneWise * gw;
404   GwLite * gl;
405   GwLiteScore * gws;
406 
407 
408 
409   if( gdb->is_single == TRUE ) {
410     *return_status = DB_RETURN_END;
411     return NULL;
412   }
413 
414   if( prev != NULL)
415     free_GwLiteScore(prev);
416 
417 
418   *return_status = DB_RETURN_ERROR;
419 
420   tsm = read_TSM_ThreeStateDB(gdb->tdb,return_status);
421 
422   if( *return_status != DB_RETURN_OK) {
423     if( *return_status == DB_RETURN_END) {
424       return NULL;
425     }
426 
427     warn("Cannot read a ThreeStateModelDB for the GeneWiseDB. Problem!");
428     return NULL;
429   }
430 
431   gw  =  GeneWise_from_ThreeStateModel_GDB(tsm,gdb);
432   gl  =  GwLite_from_GeneWise(gw);
433   gws =  GwLiteScore_from_GwLite(gl);
434 
435   free_ThreeStateModel(tsm);
436   free_GeneWise(gw);
437   free_GwLite(gl);
438 
439   *return_status = DB_RETURN_OK;
440 
441   return gws;
442 }
443 
444 /* Function:  close_GwLite_GeneWiseDB(gws,gdb)
445  *
446  * Descrip:    closes a GeneWiseDB
447  *
448  *
449  * Arg:        gws [UNKN ] Undocumented argument [GwLiteScore *]
450  * Arg:        gdb [UNKN ] Undocumented argument [GeneWiseDB *]
451  *
452  * Return [UNKN ]  Undocumented return value [boolean]
453  *
454  */
455 # line 386 "genewisemodeldb.dy"
close_GwLite_GeneWiseDB(GwLiteScore * gws,GeneWiseDB * gdb)456 boolean close_GwLite_GeneWiseDB(GwLiteScore * gws,GeneWiseDB * gdb)
457 {
458   if( gdb->is_single == TRUE ) {
459     return TRUE;
460   }
461 
462   if( gws != NULL)
463     free_GwLiteScore(gws);
464 
465   return close_ThreeStateDB(NULL,gdb->tdb);
466 }
467 
468 /* Function:  dataentry_add_GwLite_GeneWiseDB(de,gws,gdb)
469  *
470  * Descrip:    adds dataentry stuff to a query. Relies completely
471  *             on threestatedb
472  *
473  *
474  * Arg:         de [UNKN ] Undocumented argument [DataEntry *]
475  * Arg:        gws [UNKN ] Undocumented argument [GwLiteScore *]
476  * Arg:        gdb [UNKN ] Undocumented argument [GeneWiseDB *]
477  *
478  * Return [UNKN ]  Undocumented return value [boolean]
479  *
480  */
481 # line 402 "genewisemodeldb.dy"
dataentry_add_GwLite_GeneWiseDB(DataEntry * de,GwLiteScore * gws,GeneWiseDB * gdb)482 boolean dataentry_add_GwLite_GeneWiseDB(DataEntry * de,GwLiteScore * gws,GeneWiseDB * gdb)
483 {
484   if( gdb->is_single == TRUE) {
485     if ( gdb->gws->name == NULL ) {
486       warn("No name for a single GeneWiseDB unit.");
487       de->name = stringalloc("NoName");
488     } else {
489       de->name = stringalloc(gdb->gws->name);
490     }
491 
492     return TRUE;
493   }
494 
495 
496   /*de->name = stringalloc(gws->name);*/
497 
498   /* otherwise, pass it on to tdb */
499   dataentry_add_ThreeStateDB(de,NULL,gdb->tdb);
500   if( de->name == NULL ) {
501     if( gws->name != NULL ) {
502       de->name = stringalloc(gws->name);
503     }
504   }
505   return TRUE;
506 }
507 
508 # line 483 "genewisemodeldb.c"
509 /* Function:  hard_link_GeneWiseDB(obj)
510  *
511  * Descrip:    Bumps up the reference count of the object
512  *             Meaning that multiple pointers can 'own' it
513  *
514  *
515  * Arg:        obj [UNKN ] Object to be hard linked [GeneWiseDB *]
516  *
517  * Return [UNKN ]  Undocumented return value [GeneWiseDB *]
518  *
519  */
hard_link_GeneWiseDB(GeneWiseDB * obj)520 GeneWiseDB * hard_link_GeneWiseDB(GeneWiseDB * obj)
521 {
522     if( obj == NULL )    {
523       warn("Trying to hard link to a GeneWiseDB object: passed a NULL object");
524       return NULL;
525       }
526     obj->dynamite_hard_link++;
527     return obj;
528 }
529 
530 
531 /* Function:  GeneWiseDB_alloc(void)
532  *
533  * Descrip:    Allocates structure: assigns defaults if given
534  *
535  *
536  *
537  * Return [UNKN ]  Undocumented return value [GeneWiseDB *]
538  *
539  */
GeneWiseDB_alloc(void)540 GeneWiseDB * GeneWiseDB_alloc(void)
541 {
542     GeneWiseDB * out;   /* out is exported at end of function */
543 
544 
545     /* call ckalloc and see if NULL */
546     if((out=(GeneWiseDB *) ckalloc (sizeof(GeneWiseDB))) == NULL)    {
547       warn("GeneWiseDB_alloc failed ");
548       return NULL;  /* calling function should respond! */
549       }
550     out->dynamite_hard_link = 1;
551 #ifdef PTHREAD
552     pthread_mutex_init(&(out->dynamite_mutex),NULL);
553 #endif
554     out->tdb = NULL;
555     out->cds_factor = 0;
556     out->cm = NULL;
557     out->rmd = NULL;
558     out->gws = NULL;
559     out->gpara = NULL;
560     out->is_single = FALSE;
561     out->is_syn = TRUE;
562     out->allN = 1.0;
563     out->flat_insert = FALSE;
564     out->gwls = NULL;
565 
566 
567     return out;
568 }
569 
570 
571 /* Function:  free_GeneWiseDB(obj)
572  *
573  * Descrip:    Free Function: removes the memory held by obj
574  *             Will chain up to owned members and clear all lists
575  *
576  *
577  * Arg:        obj [UNKN ] Object that is free'd [GeneWiseDB *]
578  *
579  * Return [UNKN ]  Undocumented return value [GeneWiseDB *]
580  *
581  */
free_GeneWiseDB(GeneWiseDB * obj)582 GeneWiseDB * free_GeneWiseDB(GeneWiseDB * obj)
583 {
584     int return_early = 0;
585 
586 
587     if( obj == NULL) {
588       warn("Attempting to free a NULL pointer to a GeneWiseDB obj. Should be trappable");
589       return NULL;
590       }
591 
592 
593 #ifdef PTHREAD
594     assert(pthread_mutex_lock(&(obj->dynamite_mutex)) == 0);
595 #endif
596     if( obj->dynamite_hard_link > 1)     {
597       return_early = 1;
598       obj->dynamite_hard_link--;
599       }
600 #ifdef PTHREAD
601     assert(pthread_mutex_unlock(&(obj->dynamite_mutex)) == 0);
602 #endif
603     if( return_early == 1)
604       return NULL;
605     if( obj->tdb != NULL)
606       free_ThreeStateDB(obj->tdb);
607     if( obj->cm != NULL)
608       free_CodonMapper(obj->cm);
609     if( obj->rmd != NULL)
610       free_RandomModelDNA(obj->rmd);
611     if( obj->gws != NULL)
612       free_GeneWiseScore(obj->gws);
613     if( obj->gpara != NULL)
614       free_GeneParameter21(obj->gpara);
615     if( obj->gwls != NULL)
616       free_GwLiteScore(obj->gwls);
617 
618 
619     ckfree(obj);
620     return NULL;
621 }
622 
623 
624 
625 #ifdef _cplusplus
626 }
627 #endif
628