1 #ifdef _cplusplus
2 extern "C" {
3 #endif
4 #include "assembly.h"
5 
6 
7 
8 # line 80 "assembly.dy"
show_help_AssemblyOutputPara(FILE * ofp)9 void show_help_AssemblyOutputPara(FILE * ofp)
10 {
11   fprintf(ofp,"Assembly Output options\n");
12   fprintf(ofp,"  -as_min_size [500] min size of contig to show\n");
13 
14 }
15 
16 # line 87 "assembly.dy"
new_AssemblyOutputPara_from_argv(int * argc,char ** argv)17 AssemblyOutputPara * new_AssemblyOutputPara_from_argv(int * argc,char ** argv)
18 {
19   AssemblyOutputPara * out;
20 
21   out = AssemblyOutputPara_alloc();
22 
23   out->min_size = 500;
24 
25   strip_out_integer_argument(argc,argv,"as_min_size",&out->min_size);
26 
27   return out;
28 }
29 
30 # line 100 "assembly.dy"
homopolymer_AssemblyOpaqueTypeSet(void)31 AssemblyOpaqueTypeSet * homopolymer_AssemblyOpaqueTypeSet(void)
32 {
33   AssemblyOpaqueTypeSet * out;
34   AssemblyOpaqueType * t;
35   char * base = "ATGC";
36   int i;
37 
38   out = AssemblyOpaqueTypeSet_alloc_std();
39 
40   for(i=0;i<4;i++) {
41     t = new_homopolymer_AssemblyOpaqueType(i,base[i]);
42     add_AssemblyOpaqueTypeSet(out,t);
43   }
44 
45   return out;
46 }
47 
48 
49 # line 118 "assembly.dy"
annotate_AssemblyOpaqueFeatures(AssemblyOpaqueTypeSet * aots,AssemblySequence * aseq,int kmer_size)50 int annotate_AssemblyOpaqueFeatures(AssemblyOpaqueTypeSet * aots,AssemblySequence * aseq,int kmer_size)
51 {
52   int count = 0;
53   int i,j;
54   int k;
55   AssemblyOpaqueFeature * aof;
56   AssemblyOpaqueFeature * prev;
57 
58   for(i=0;i<aots->len;i++) {
59     prev = NULL;
60     for(j=0;j<aseq->len;) {
61       if( aseq->seq->seq[j] == aots->type[i]->base ) {
62 	for(k=0;k<kmer_size;k++) {
63 	  if( aseq->seq->seq[j+k] != aots->type[i]->base ) {
64 	    break;
65 	  }
66 	}
67 
68 	if( k < kmer_size ) {
69 	  j += k;
70 	  continue;
71 	}
72 
73 	/* else, is a good position, extend to the end of this run */
74 	for(;k+j<aseq->len;k++) {
75 	  if( aseq->seq->seq[j+k] != aots->type[i]->base ) {
76 	    break;
77 	  }
78 	}
79 
80 	/* if the start is within kmer of prev, then simply extend prev */
81 
82 	if( prev != NULL && prev->start+prev->length+kmer_size >= j ) {
83 	  prev->length = j+k - prev->start;
84 	} else {
85 	  /* new feature */
86 	  aof = AssemblyOpaqueFeature_alloc();
87 	  aof->start = j;
88 	  aof->length = k;
89 	  aof->type = aots->type[i];
90 	  add_opq_AssemblySequence(aseq,aof);
91 	  prev = aof;
92 	  count++;
93 	}
94       } else {
95 	j++;
96       }
97     }
98   }
99 
100   return count;
101 }
102 
103 
104 # line 172 "assembly.dy"
show_AssemblySequence(AssemblySequence * aseq,FILE * ofp)105 void show_AssemblySequence(AssemblySequence * aseq,FILE * ofp)
106 {
107   int i;
108   assert(aseq!=NULL);
109 
110   for(i=0;i<aseq->opq_len;i++) {
111     fprintf(ofp,"Opaque type %c from %d to %d\n",aseq->opaque[i]->type->base,aseq->opaque[i]->start,aseq->opaque[i]->start+aseq->opaque[i]->length);
112   }
113   write_fasta_Sequence(aseq->seq,ofp);
114   fprintf(ofp,"//\n");
115 
116 }
117 
118 # line 185 "assembly.dy"
new_homopolymer_AssemblyOpaqueType(int int_type,char base)119 AssemblyOpaqueType * new_homopolymer_AssemblyOpaqueType(int int_type,char base)
120 {
121   AssemblyOpaqueType * out;
122 
123   out = AssemblyOpaqueType_alloc();
124   out->base = base;
125   out->int_type = int_type;
126 
127   return out;
128 }
129 
130 # line 196 "assembly.dy"
read_plain_fasta_AssemblySequence(FILE * ifp,int report_log,FILE * report)131 AssemblySequence * read_plain_fasta_AssemblySequence(FILE * ifp,int report_log,FILE * report)
132 {
133   AssemblySequence * out;
134   Sequence * seq;
135 
136   seq = read_large_dna_Sequence(ifp,report_log,report);
137 
138   if( seq == NULL ) {
139     return NULL;
140   }
141 
142   out = AssemblySequence_alloc_std();
143 
144   out->seq = seq;
145 
146   return out;
147 }
148 
149 # line 214 "assembly.dy"
mirrored_AssemblySequence(AssemblySequence * aseq)150 AssemblySequence * mirrored_AssemblySequence(AssemblySequence * aseq)
151 {
152   AssemblySequence * out;
153 
154   out = AssemblySequence_alloc();
155   out->seq = reverse_complement_Sequence(aseq->seq);
156   out->mirror_seq = 1;
157 
158   out->mirror   = aseq;
159   aseq->mirror  = out;
160 
161   return out;
162 }
163 
164 # line 228 "assembly.dy"
dump_contigs_as_fasta_Assembly(Assembly * assembly,AssemblyOutputPara * aop,FILE * ofp)165 void dump_contigs_as_fasta_Assembly(Assembly * assembly,AssemblyOutputPara * aop,FILE * ofp)
166 {
167   int i;
168 
169   for(i=0;i<assembly->len;i++) {
170     AssemblyContig * c = assembly->contig[i];
171     if( aop->min_size > c->consensus->len ) {
172       continue;
173     }
174 
175     fprintf(ofp,">%s max_depth=%d clean_start=%d clean_end=%d\n",c->consensus->name,c->max_depth,c->clean_start,c->clean_end);
176     show_line(c->consensus->seq,60,ofp);
177   }
178 
179 }
180 
181 
182 # line 181 "assembly.c"
183 /* Function:  hard_link_AssemblySequenceEdit(obj)
184  *
185  * Descrip:    Bumps up the reference count of the object
186  *             Meaning that multiple pointers can 'own' it
187  *
188  *
189  * Arg:        obj [UNKN ] Object to be hard linked [AssemblySequenceEdit *]
190  *
191  * Return [UNKN ]  Undocumented return value [AssemblySequenceEdit *]
192  *
193  */
hard_link_AssemblySequenceEdit(AssemblySequenceEdit * obj)194 AssemblySequenceEdit * hard_link_AssemblySequenceEdit(AssemblySequenceEdit * obj)
195 {
196     if( obj == NULL )    {
197       warn("Trying to hard link to a AssemblySequenceEdit object: passed a NULL object");
198       return NULL;
199       }
200     obj->dynamite_hard_link++;
201     return obj;
202 }
203 
204 
205 /* Function:  AssemblySequenceEdit_alloc(void)
206  *
207  * Descrip:    Allocates structure: assigns defaults if given
208  *
209  *
210  *
211  * Return [UNKN ]  Undocumented return value [AssemblySequenceEdit *]
212  *
213  */
AssemblySequenceEdit_alloc(void)214 AssemblySequenceEdit * AssemblySequenceEdit_alloc(void)
215 {
216     AssemblySequenceEdit * out; /* out is exported at end of function */
217 
218 
219     /* call ckalloc and see if NULL */
220     if((out=(AssemblySequenceEdit *) ckalloc (sizeof(AssemblySequenceEdit))) == NULL)    {
221       warn("AssemblySequenceEdit_alloc failed ");
222       return NULL;  /* calling function should respond! */
223       }
224     out->dynamite_hard_link = 1;
225 #ifdef PTHREAD
226     pthread_mutex_init(&(out->dynamite_mutex),NULL);
227 #endif
228     out->type = 'u';
229     out->base = 'u';
230 
231 
232     return out;
233 }
234 
235 
236 /* Function:  free_AssemblySequenceEdit(obj)
237  *
238  * Descrip:    Free Function: removes the memory held by obj
239  *             Will chain up to owned members and clear all lists
240  *
241  *
242  * Arg:        obj [UNKN ] Object that is free'd [AssemblySequenceEdit *]
243  *
244  * Return [UNKN ]  Undocumented return value [AssemblySequenceEdit *]
245  *
246  */
free_AssemblySequenceEdit(AssemblySequenceEdit * obj)247 AssemblySequenceEdit * free_AssemblySequenceEdit(AssemblySequenceEdit * obj)
248 {
249     int return_early = 0;
250 
251 
252     if( obj == NULL) {
253       warn("Attempting to free a NULL pointer to a AssemblySequenceEdit obj. Should be trappable");
254       return NULL;
255       }
256 
257 
258 #ifdef PTHREAD
259     assert(pthread_mutex_lock(&(obj->dynamite_mutex)) == 0);
260 #endif
261     if( obj->dynamite_hard_link > 1)     {
262       return_early = 1;
263       obj->dynamite_hard_link--;
264       }
265 #ifdef PTHREAD
266     assert(pthread_mutex_unlock(&(obj->dynamite_mutex)) == 0);
267 #endif
268     if( return_early == 1)
269       return NULL;
270 
271 
272     ckfree(obj);
273     return NULL;
274 }
275 
276 
277 /* Function:  hard_link_AssemblyOpaqueType(obj)
278  *
279  * Descrip:    Bumps up the reference count of the object
280  *             Meaning that multiple pointers can 'own' it
281  *
282  *
283  * Arg:        obj [UNKN ] Object to be hard linked [AssemblyOpaqueType *]
284  *
285  * Return [UNKN ]  Undocumented return value [AssemblyOpaqueType *]
286  *
287  */
hard_link_AssemblyOpaqueType(AssemblyOpaqueType * obj)288 AssemblyOpaqueType * hard_link_AssemblyOpaqueType(AssemblyOpaqueType * obj)
289 {
290     if( obj == NULL )    {
291       warn("Trying to hard link to a AssemblyOpaqueType object: passed a NULL object");
292       return NULL;
293       }
294     obj->dynamite_hard_link++;
295     return obj;
296 }
297 
298 
299 /* Function:  AssemblyOpaqueType_alloc(void)
300  *
301  * Descrip:    Allocates structure: assigns defaults if given
302  *
303  *
304  *
305  * Return [UNKN ]  Undocumented return value [AssemblyOpaqueType *]
306  *
307  */
AssemblyOpaqueType_alloc(void)308 AssemblyOpaqueType * AssemblyOpaqueType_alloc(void)
309 {
310     AssemblyOpaqueType * out;   /* out is exported at end of function */
311 
312 
313     /* call ckalloc and see if NULL */
314     if((out=(AssemblyOpaqueType *) ckalloc (sizeof(AssemblyOpaqueType))) == NULL)    {
315       warn("AssemblyOpaqueType_alloc failed ");
316       return NULL;  /* calling function should respond! */
317       }
318     out->dynamite_hard_link = 1;
319 #ifdef PTHREAD
320     pthread_mutex_init(&(out->dynamite_mutex),NULL);
321 #endif
322     out->int_type = 0;
323     out->base = 'u';
324 
325 
326     return out;
327 }
328 
329 
330 /* Function:  free_AssemblyOpaqueType(obj)
331  *
332  * Descrip:    Free Function: removes the memory held by obj
333  *             Will chain up to owned members and clear all lists
334  *
335  *
336  * Arg:        obj [UNKN ] Object that is free'd [AssemblyOpaqueType *]
337  *
338  * Return [UNKN ]  Undocumented return value [AssemblyOpaqueType *]
339  *
340  */
free_AssemblyOpaqueType(AssemblyOpaqueType * obj)341 AssemblyOpaqueType * free_AssemblyOpaqueType(AssemblyOpaqueType * obj)
342 {
343     int return_early = 0;
344 
345 
346     if( obj == NULL) {
347       warn("Attempting to free a NULL pointer to a AssemblyOpaqueType obj. Should be trappable");
348       return NULL;
349       }
350 
351 
352 #ifdef PTHREAD
353     assert(pthread_mutex_lock(&(obj->dynamite_mutex)) == 0);
354 #endif
355     if( obj->dynamite_hard_link > 1)     {
356       return_early = 1;
357       obj->dynamite_hard_link--;
358       }
359 #ifdef PTHREAD
360     assert(pthread_mutex_unlock(&(obj->dynamite_mutex)) == 0);
361 #endif
362     if( return_early == 1)
363       return NULL;
364 
365 
366     ckfree(obj);
367     return NULL;
368 }
369 
370 
371 /* Function:  swap_AssemblyOpaqueTypeSet(list,i,j)
372  *
373  * Descrip:    swap function: an internal for qsort_AssemblyOpaqueTypeSet
374  *             swaps two positions in the array
375  *
376  *
377  * Arg:        list [UNKN ] List of structures to swap in [AssemblyOpaqueType **]
378  * Arg:           i [UNKN ] swap position [int]
379  * Arg:           j [UNKN ] swap position [int]
380  *
381  */
382 /* swap function for qsort function */
swap_AssemblyOpaqueTypeSet(AssemblyOpaqueType ** list,int i,int j)383 void swap_AssemblyOpaqueTypeSet(AssemblyOpaqueType ** list,int i,int j)
384 {
385     AssemblyOpaqueType * temp;
386     temp=list[i];
387     list[i]=list[j];
388     list[j]=temp;
389 }
390 
391 
392 /* Function:  qsort_AssemblyOpaqueTypeSet(list,left,right,comp)
393  *
394  * Descrip:    qsort - lifted from K&R
395  *             sorts the array using quicksort
396  *             Probably much better to call sort_AssemblyOpaqueTypeSet which sorts from start to end
397  *
398  *
399  * Arg:         list [UNKN ] List of structures to swap in [AssemblyOpaqueType **]
400  * Arg:         left [UNKN ] left position [int]
401  * Arg:        right [UNKN ] right position [int]
402  * Arg:         comp [FUNCP] Function which returns -1 or 1 to sort on [int (*comp]
403  *
404  */
qsort_AssemblyOpaqueTypeSet(AssemblyOpaqueType ** list,int left,int right,int (* comp)(AssemblyOpaqueType *,AssemblyOpaqueType *))405 void qsort_AssemblyOpaqueTypeSet(AssemblyOpaqueType ** list,int left,int right,int (*comp)(AssemblyOpaqueType * ,AssemblyOpaqueType * ))
406 {
407     int i,last;
408     if( left >= right )
409       return;
410 
411 
412     swap_AssemblyOpaqueTypeSet(list,left,(left+right)/2);
413     last = left;
414     for ( i=left+1; i <= right;i++)  {
415       if( (*comp)(list[i],list[left]) < 0)
416         swap_AssemblyOpaqueTypeSet (list,++last,i);
417       }
418     swap_AssemblyOpaqueTypeSet (list,left,last);
419     qsort_AssemblyOpaqueTypeSet(list,left,last-1,comp);
420     qsort_AssemblyOpaqueTypeSet(list,last+1,right,comp);
421 }
422 
423 
424 /* Function:  sort_AssemblyOpaqueTypeSet(obj,comp)
425  *
426  * Descrip:    sorts from start to end using comp
427  *             sorts the array using quicksort by calling qsort_AssemblyOpaqueTypeSet
428  *
429  *
430  * Arg:         obj [UNKN ] Object containing list [AssemblyOpaqueTypeSet *]
431  * Arg:        comp [FUNCP] Function which returns -1 or 1 to sort on [int (*comp]
432  *
433  */
sort_AssemblyOpaqueTypeSet(AssemblyOpaqueTypeSet * obj,int (* comp)(AssemblyOpaqueType *,AssemblyOpaqueType *))434 void sort_AssemblyOpaqueTypeSet(AssemblyOpaqueTypeSet * obj,int (*comp)(AssemblyOpaqueType *, AssemblyOpaqueType *))
435 {
436     qsort_AssemblyOpaqueTypeSet(obj->type,0,obj->len-1,comp);
437     return;
438 }
439 
440 
441 /* Function:  expand_AssemblyOpaqueTypeSet(obj,len)
442  *
443  * Descrip:    Really an internal function for add_AssemblyOpaqueTypeSet
444  *
445  *
446  * Arg:        obj [UNKN ] Object which contains the list [AssemblyOpaqueTypeSet *]
447  * Arg:        len [UNKN ] Length to add one [int]
448  *
449  * Return [UNKN ]  Undocumented return value [boolean]
450  *
451  */
expand_AssemblyOpaqueTypeSet(AssemblyOpaqueTypeSet * obj,int len)452 boolean expand_AssemblyOpaqueTypeSet(AssemblyOpaqueTypeSet * obj,int len)
453 {
454 
455 
456     if( obj->maxlen > obj->len )     {
457       warn("expand_AssemblyOpaqueTypeSet called with no need");
458       return TRUE;
459       }
460 
461 
462     if( (obj->type = (AssemblyOpaqueType ** ) ckrealloc (obj->type,sizeof(AssemblyOpaqueType *)*len)) == NULL)   {
463       warn("ckrealloc failed for expand_AssemblyOpaqueTypeSet, returning FALSE");
464       return FALSE;
465       }
466     obj->maxlen = len;
467     return TRUE;
468 }
469 
470 
471 /* Function:  add_AssemblyOpaqueTypeSet(obj,add)
472  *
473  * Descrip:    Adds another object to the list. It will expand the list if necessary
474  *
475  *
476  * Arg:        obj [UNKN ] Object which contains the list [AssemblyOpaqueTypeSet *]
477  * Arg:        add [OWNER] Object to add to the list [AssemblyOpaqueType *]
478  *
479  * Return [UNKN ]  Undocumented return value [boolean]
480  *
481  */
482 /* will expand function if necessary */
add_AssemblyOpaqueTypeSet(AssemblyOpaqueTypeSet * obj,AssemblyOpaqueType * add)483 boolean add_AssemblyOpaqueTypeSet(AssemblyOpaqueTypeSet * obj,AssemblyOpaqueType * add)
484 {
485     if( obj->len >= obj->maxlen) {
486       if( expand_AssemblyOpaqueTypeSet(obj,obj->len + AssemblyOpaqueTypeSetLISTLENGTH) == FALSE)
487         return FALSE;
488       }
489 
490 
491     obj->type[obj->len++]=add;
492     return TRUE;
493 }
494 
495 
496 /* Function:  flush_AssemblyOpaqueTypeSet(obj)
497  *
498  * Descrip:    Frees the list elements, sets length to 0
499  *             If you want to save some elements, use hard_link_xxx
500  *             to protect them from being actually destroyed in the free
501  *
502  *
503  * Arg:        obj [UNKN ] Object which contains the list  [AssemblyOpaqueTypeSet *]
504  *
505  * Return [UNKN ]  Undocumented return value [int]
506  *
507  */
flush_AssemblyOpaqueTypeSet(AssemblyOpaqueTypeSet * obj)508 int flush_AssemblyOpaqueTypeSet(AssemblyOpaqueTypeSet * obj)
509 {
510     int i;
511 
512 
513     for(i=0;i<obj->len;i++)  { /*for i over list length*/
514       if( obj->type[i] != NULL)  {
515         free_AssemblyOpaqueType(obj->type[i]);
516         obj->type[i] = NULL;
517         }
518       } /* end of for i over list length */
519 
520 
521     obj->len = 0;
522     return i;
523 }
524 
525 
526 /* Function:  AssemblyOpaqueTypeSet_alloc_std(void)
527  *
528  * Descrip:    Equivalent to AssemblyOpaqueTypeSet_alloc_len(AssemblyOpaqueTypeSetLISTLENGTH)
529  *
530  *
531  *
532  * Return [UNKN ]  Undocumented return value [AssemblyOpaqueTypeSet *]
533  *
534  */
AssemblyOpaqueTypeSet_alloc_std(void)535 AssemblyOpaqueTypeSet * AssemblyOpaqueTypeSet_alloc_std(void)
536 {
537     return AssemblyOpaqueTypeSet_alloc_len(AssemblyOpaqueTypeSetLISTLENGTH);
538 }
539 
540 
541 /* Function:  AssemblyOpaqueTypeSet_alloc_len(len)
542  *
543  * Descrip:    Allocates len length to all lists
544  *
545  *
546  * Arg:        len [UNKN ] Length of lists to allocate [int]
547  *
548  * Return [UNKN ]  Undocumented return value [AssemblyOpaqueTypeSet *]
549  *
550  */
AssemblyOpaqueTypeSet_alloc_len(int len)551 AssemblyOpaqueTypeSet * AssemblyOpaqueTypeSet_alloc_len(int len)
552 {
553     AssemblyOpaqueTypeSet * out;/* out is exported at the end of function */
554 
555 
556     /* Call alloc function: return NULL if NULL */
557     /* Warning message alread in alloc function */
558     if((out = AssemblyOpaqueTypeSet_alloc()) == NULL)
559       return NULL;
560 
561 
562     /* Calling ckcalloc for list elements */
563     if((out->type = (AssemblyOpaqueType ** ) ckcalloc (len,sizeof(AssemblyOpaqueType *))) == NULL)   {
564       warn("Warning, ckcalloc failed in AssemblyOpaqueTypeSet_alloc_len");
565       return NULL;
566       }
567     out->len = 0;
568     out->maxlen = len;
569 
570 
571     return out;
572 }
573 
574 
575 /* Function:  hard_link_AssemblyOpaqueTypeSet(obj)
576  *
577  * Descrip:    Bumps up the reference count of the object
578  *             Meaning that multiple pointers can 'own' it
579  *
580  *
581  * Arg:        obj [UNKN ] Object to be hard linked [AssemblyOpaqueTypeSet *]
582  *
583  * Return [UNKN ]  Undocumented return value [AssemblyOpaqueTypeSet *]
584  *
585  */
hard_link_AssemblyOpaqueTypeSet(AssemblyOpaqueTypeSet * obj)586 AssemblyOpaqueTypeSet * hard_link_AssemblyOpaqueTypeSet(AssemblyOpaqueTypeSet * obj)
587 {
588     if( obj == NULL )    {
589       warn("Trying to hard link to a AssemblyOpaqueTypeSet object: passed a NULL object");
590       return NULL;
591       }
592     obj->dynamite_hard_link++;
593     return obj;
594 }
595 
596 
597 /* Function:  AssemblyOpaqueTypeSet_alloc(void)
598  *
599  * Descrip:    Allocates structure: assigns defaults if given
600  *
601  *
602  *
603  * Return [UNKN ]  Undocumented return value [AssemblyOpaqueTypeSet *]
604  *
605  */
AssemblyOpaqueTypeSet_alloc(void)606 AssemblyOpaqueTypeSet * AssemblyOpaqueTypeSet_alloc(void)
607 {
608     AssemblyOpaqueTypeSet * out;/* out is exported at end of function */
609 
610 
611     /* call ckalloc and see if NULL */
612     if((out=(AssemblyOpaqueTypeSet *) ckalloc (sizeof(AssemblyOpaqueTypeSet))) == NULL)  {
613       warn("AssemblyOpaqueTypeSet_alloc failed ");
614       return NULL;  /* calling function should respond! */
615       }
616     out->dynamite_hard_link = 1;
617 #ifdef PTHREAD
618     pthread_mutex_init(&(out->dynamite_mutex),NULL);
619 #endif
620     out->type = NULL;
621     out->len = out->maxlen = 0;
622 
623 
624     return out;
625 }
626 
627 
628 /* Function:  free_AssemblyOpaqueTypeSet(obj)
629  *
630  * Descrip:    Free Function: removes the memory held by obj
631  *             Will chain up to owned members and clear all lists
632  *
633  *
634  * Arg:        obj [UNKN ] Object that is free'd [AssemblyOpaqueTypeSet *]
635  *
636  * Return [UNKN ]  Undocumented return value [AssemblyOpaqueTypeSet *]
637  *
638  */
free_AssemblyOpaqueTypeSet(AssemblyOpaqueTypeSet * obj)639 AssemblyOpaqueTypeSet * free_AssemblyOpaqueTypeSet(AssemblyOpaqueTypeSet * obj)
640 {
641     int return_early = 0;
642     int i;
643 
644 
645     if( obj == NULL) {
646       warn("Attempting to free a NULL pointer to a AssemblyOpaqueTypeSet obj. Should be trappable");
647       return NULL;
648       }
649 
650 
651 #ifdef PTHREAD
652     assert(pthread_mutex_lock(&(obj->dynamite_mutex)) == 0);
653 #endif
654     if( obj->dynamite_hard_link > 1)     {
655       return_early = 1;
656       obj->dynamite_hard_link--;
657       }
658 #ifdef PTHREAD
659     assert(pthread_mutex_unlock(&(obj->dynamite_mutex)) == 0);
660 #endif
661     if( return_early == 1)
662       return NULL;
663     if( obj->type != NULL)   {
664       for(i=0;i<obj->len;i++)    {
665         if( obj->type[i] != NULL)
666           free_AssemblyOpaqueType(obj->type[i]);
667         }
668       ckfree(obj->type);
669       }
670 
671 
672     ckfree(obj);
673     return NULL;
674 }
675 
676 
677 /* Function:  hard_link_AssemblyOpaqueFeature(obj)
678  *
679  * Descrip:    Bumps up the reference count of the object
680  *             Meaning that multiple pointers can 'own' it
681  *
682  *
683  * Arg:        obj [UNKN ] Object to be hard linked [AssemblyOpaqueFeature *]
684  *
685  * Return [UNKN ]  Undocumented return value [AssemblyOpaqueFeature *]
686  *
687  */
hard_link_AssemblyOpaqueFeature(AssemblyOpaqueFeature * obj)688 AssemblyOpaqueFeature * hard_link_AssemblyOpaqueFeature(AssemblyOpaqueFeature * obj)
689 {
690     if( obj == NULL )    {
691       warn("Trying to hard link to a AssemblyOpaqueFeature object: passed a NULL object");
692       return NULL;
693       }
694     obj->dynamite_hard_link++;
695     return obj;
696 }
697 
698 
699 /* Function:  AssemblyOpaqueFeature_alloc(void)
700  *
701  * Descrip:    Allocates structure: assigns defaults if given
702  *
703  *
704  *
705  * Return [UNKN ]  Undocumented return value [AssemblyOpaqueFeature *]
706  *
707  */
AssemblyOpaqueFeature_alloc(void)708 AssemblyOpaqueFeature * AssemblyOpaqueFeature_alloc(void)
709 {
710     AssemblyOpaqueFeature * out;/* out is exported at end of function */
711 
712 
713     /* call ckalloc and see if NULL */
714     if((out=(AssemblyOpaqueFeature *) ckalloc (sizeof(AssemblyOpaqueFeature))) == NULL)  {
715       warn("AssemblyOpaqueFeature_alloc failed ");
716       return NULL;  /* calling function should respond! */
717       }
718     out->dynamite_hard_link = 1;
719 #ifdef PTHREAD
720     pthread_mutex_init(&(out->dynamite_mutex),NULL);
721 #endif
722     out->start = 0;
723     out->length = 0;
724 
725 
726     return out;
727 }
728 
729 
730 /* Function:  free_AssemblyOpaqueFeature(obj)
731  *
732  * Descrip:    Free Function: removes the memory held by obj
733  *             Will chain up to owned members and clear all lists
734  *
735  *
736  * Arg:        obj [UNKN ] Object that is free'd [AssemblyOpaqueFeature *]
737  *
738  * Return [UNKN ]  Undocumented return value [AssemblyOpaqueFeature *]
739  *
740  */
free_AssemblyOpaqueFeature(AssemblyOpaqueFeature * obj)741 AssemblyOpaqueFeature * free_AssemblyOpaqueFeature(AssemblyOpaqueFeature * obj)
742 {
743     int return_early = 0;
744 
745 
746     if( obj == NULL) {
747       warn("Attempting to free a NULL pointer to a AssemblyOpaqueFeature obj. Should be trappable");
748       return NULL;
749       }
750 
751 
752 #ifdef PTHREAD
753     assert(pthread_mutex_lock(&(obj->dynamite_mutex)) == 0);
754 #endif
755     if( obj->dynamite_hard_link > 1)     {
756       return_early = 1;
757       obj->dynamite_hard_link--;
758       }
759 #ifdef PTHREAD
760     assert(pthread_mutex_unlock(&(obj->dynamite_mutex)) == 0);
761 #endif
762     if( return_early == 1)
763       return NULL;
764     /* obj->type is linked in */
765 
766 
767     ckfree(obj);
768     return NULL;
769 }
770 
771 
772 /* Function:  swap_AssemblySequence(list,i,j)
773  *
774  * Descrip:    swap function: an internal for qsort_AssemblySequence
775  *             swaps two positions in the array
776  *
777  *
778  * Arg:        list [UNKN ] List of structures to swap in [AssemblySequenceEdit **]
779  * Arg:           i [UNKN ] swap position [int]
780  * Arg:           j [UNKN ] swap position [int]
781  *
782  */
783 /* swap function for qsort function */
swap_AssemblySequence(AssemblySequenceEdit ** list,int i,int j)784 void swap_AssemblySequence(AssemblySequenceEdit ** list,int i,int j)
785 {
786     AssemblySequenceEdit * temp;
787     temp=list[i];
788     list[i]=list[j];
789     list[j]=temp;
790 }
791 
792 
793 /* Function:  qsort_AssemblySequence(list,left,right,comp)
794  *
795  * Descrip:    qsort - lifted from K&R
796  *             sorts the array using quicksort
797  *             Probably much better to call sort_AssemblySequence which sorts from start to end
798  *
799  *
800  * Arg:         list [UNKN ] List of structures to swap in [AssemblySequenceEdit **]
801  * Arg:         left [UNKN ] left position [int]
802  * Arg:        right [UNKN ] right position [int]
803  * Arg:         comp [FUNCP] Function which returns -1 or 1 to sort on [int (*comp]
804  *
805  */
qsort_AssemblySequence(AssemblySequenceEdit ** list,int left,int right,int (* comp)(AssemblySequenceEdit *,AssemblySequenceEdit *))806 void qsort_AssemblySequence(AssemblySequenceEdit ** list,int left,int right,int (*comp)(AssemblySequenceEdit * ,AssemblySequenceEdit * ))
807 {
808     int i,last;
809     if( left >= right )
810       return;
811 
812 
813     swap_AssemblySequence(list,left,(left+right)/2);
814     last = left;
815     for ( i=left+1; i <= right;i++)  {
816       if( (*comp)(list[i],list[left]) < 0)
817         swap_AssemblySequence (list,++last,i);
818       }
819     swap_AssemblySequence (list,left,last);
820     qsort_AssemblySequence(list,left,last-1,comp);
821     qsort_AssemblySequence(list,last+1,right,comp);
822 }
823 
824 
825 /* Function:  sort_AssemblySequence(obj,comp)
826  *
827  * Descrip:    sorts from start to end using comp
828  *             sorts the array using quicksort by calling qsort_AssemblySequence
829  *
830  *
831  * Arg:         obj [UNKN ] Object containing list [AssemblySequence *]
832  * Arg:        comp [FUNCP] Function which returns -1 or 1 to sort on [int (*comp]
833  *
834  */
sort_AssemblySequence(AssemblySequence * obj,int (* comp)(AssemblySequenceEdit *,AssemblySequenceEdit *))835 void sort_AssemblySequence(AssemblySequence * obj,int (*comp)(AssemblySequenceEdit *, AssemblySequenceEdit *))
836 {
837     qsort_AssemblySequence(obj->edit,0,obj->len-1,comp);
838     return;
839 }
840 
841 
842 /* Function:  expand_AssemblySequence(obj,len)
843  *
844  * Descrip:    Really an internal function for add_AssemblySequence
845  *
846  *
847  * Arg:        obj [UNKN ] Object which contains the list [AssemblySequence *]
848  * Arg:        len [UNKN ] Length to add one [int]
849  *
850  * Return [UNKN ]  Undocumented return value [boolean]
851  *
852  */
expand_AssemblySequence(AssemblySequence * obj,int len)853 boolean expand_AssemblySequence(AssemblySequence * obj,int len)
854 {
855 
856 
857     if( obj->maxlen > obj->len )     {
858       warn("expand_AssemblySequence called with no need");
859       return TRUE;
860       }
861 
862 
863     if( (obj->edit = (AssemblySequenceEdit ** ) ckrealloc (obj->edit,sizeof(AssemblySequenceEdit *)*len)) == NULL)   {
864       warn("ckrealloc failed for expand_AssemblySequence, returning FALSE");
865       return FALSE;
866       }
867     obj->maxlen = len;
868     return TRUE;
869 }
870 
871 
872 /* Function:  add_AssemblySequence(obj,add)
873  *
874  * Descrip:    Adds another object to the list. It will expand the list if necessary
875  *
876  *
877  * Arg:        obj [UNKN ] Object which contains the list [AssemblySequence *]
878  * Arg:        add [OWNER] Object to add to the list [AssemblySequenceEdit *]
879  *
880  * Return [UNKN ]  Undocumented return value [boolean]
881  *
882  */
883 /* will expand function if necessary */
add_AssemblySequence(AssemblySequence * obj,AssemblySequenceEdit * add)884 boolean add_AssemblySequence(AssemblySequence * obj,AssemblySequenceEdit * add)
885 {
886     if( obj->len >= obj->maxlen) {
887       if( expand_AssemblySequence(obj,obj->len + AssemblySequenceLISTLENGTH) == FALSE)
888         return FALSE;
889       }
890 
891 
892     obj->edit[obj->len++]=add;
893     return TRUE;
894 }
895 
896 
897 /* Function:  flush_AssemblySequence(obj)
898  *
899  * Descrip:    Frees the list elements, sets length to 0
900  *             If you want to save some elements, use hard_link_xxx
901  *             to protect them from being actually destroyed in the free
902  *
903  *
904  * Arg:        obj [UNKN ] Object which contains the list  [AssemblySequence *]
905  *
906  * Return [UNKN ]  Undocumented return value [int]
907  *
908  */
flush_AssemblySequence(AssemblySequence * obj)909 int flush_AssemblySequence(AssemblySequence * obj)
910 {
911     int i;
912 
913 
914     for(i=0;i<obj->len;i++)  { /*for i over list length*/
915       if( obj->edit[i] != NULL)  {
916         free_AssemblySequenceEdit(obj->edit[i]);
917         obj->edit[i] = NULL;
918         }
919       } /* end of for i over list length */
920 
921 
922     obj->len = 0;
923     return i;
924 }
925 
926 
927 /* Function:  swap_opq_AssemblySequence(list,i,j)
928  *
929  * Descrip:    swap function: an internal for qsort_opq_AssemblySequence
930  *             swaps two positions in the array
931  *
932  *
933  * Arg:        list [UNKN ] List of structures to swap in [AssemblyOpaqueFeature **]
934  * Arg:           i [UNKN ] swap position [int]
935  * Arg:           j [UNKN ] swap position [int]
936  *
937  */
938 /* swap function for qsort function */
swap_opq_AssemblySequence(AssemblyOpaqueFeature ** list,int i,int j)939 void swap_opq_AssemblySequence(AssemblyOpaqueFeature ** list,int i,int j)
940 {
941     AssemblyOpaqueFeature * temp;
942     temp=list[i];
943     list[i]=list[j];
944     list[j]=temp;
945 }
946 
947 
948 /* Function:  qsort_opq_AssemblySequence(list,left,right,comp)
949  *
950  * Descrip:    qsort - lifted from K&R
951  *             sorts the array using quicksort
952  *             Probably much better to call sort_opq_AssemblySequence which sorts from start to end
953  *
954  *
955  * Arg:         list [UNKN ] List of structures to swap in [AssemblyOpaqueFeature **]
956  * Arg:         left [UNKN ] left position [int]
957  * Arg:        right [UNKN ] right position [int]
958  * Arg:         comp [FUNCP] Function which returns -1 or 1 to sort on [int (*comp]
959  *
960  */
qsort_opq_AssemblySequence(AssemblyOpaqueFeature ** list,int left,int right,int (* comp)(AssemblyOpaqueFeature *,AssemblyOpaqueFeature *))961 void qsort_opq_AssemblySequence(AssemblyOpaqueFeature ** list,int left,int right,int (*comp)(AssemblyOpaqueFeature * ,AssemblyOpaqueFeature * ))
962 {
963     int i,last;
964     if( left >= right )
965       return;
966 
967 
968     swap_opq_AssemblySequence(list,left,(left+right)/2);
969     last = left;
970     for ( i=left+1; i <= right;i++)  {
971       if( (*comp)(list[i],list[left]) < 0)
972         swap_opq_AssemblySequence (list,++last,i);
973       }
974     swap_opq_AssemblySequence (list,left,last);
975     qsort_opq_AssemblySequence(list,left,last-1,comp);
976     qsort_opq_AssemblySequence(list,last+1,right,comp);
977 }
978 
979 
980 /* Function:  sort_opq_AssemblySequence(obj,comp)
981  *
982  * Descrip:    sorts from start to end using comp
983  *             sorts the array using quicksort by calling qsort_opq_AssemblySequence
984  *
985  *
986  * Arg:         obj [UNKN ] Object containing list [AssemblySequence *]
987  * Arg:        comp [FUNCP] Function which returns -1 or 1 to sort on [int (*comp]
988  *
989  */
sort_opq_AssemblySequence(AssemblySequence * obj,int (* comp)(AssemblyOpaqueFeature *,AssemblyOpaqueFeature *))990 void sort_opq_AssemblySequence(AssemblySequence * obj,int (*comp)(AssemblyOpaqueFeature *, AssemblyOpaqueFeature *))
991 {
992     qsort_opq_AssemblySequence(obj->opaque,0,obj->opq_len-1,comp);
993     return;
994 }
995 
996 
997 /* Function:  expand_opq_AssemblySequence(obj,len)
998  *
999  * Descrip:    Really an internal function for add_opq_AssemblySequence
1000  *
1001  *
1002  * Arg:        obj [UNKN ] Object which contains the list [AssemblySequence *]
1003  * Arg:        len [UNKN ] Length to add one [int]
1004  *
1005  * Return [UNKN ]  Undocumented return value [boolean]
1006  *
1007  */
expand_opq_AssemblySequence(AssemblySequence * obj,int len)1008 boolean expand_opq_AssemblySequence(AssemblySequence * obj,int len)
1009 {
1010 
1011 
1012     if( obj->opq_maxlen > obj->opq_len )     {
1013       warn("expand_AssemblySequenceopq_ called with no need");
1014       return TRUE;
1015       }
1016 
1017 
1018     if( (obj->opaque = (AssemblyOpaqueFeature ** ) ckrealloc (obj->opaque,sizeof(AssemblyOpaqueFeature *)*len)) == NULL)     {
1019       warn("ckrealloc failed for expand_AssemblySequence, returning FALSE");
1020       return FALSE;
1021       }
1022     obj->opq_maxlen = len;
1023     return TRUE;
1024 }
1025 
1026 
1027 /* Function:  add_opq_AssemblySequence(obj,add)
1028  *
1029  * Descrip:    Adds another object to the list. It will expand the list if necessary
1030  *
1031  *
1032  * Arg:        obj [UNKN ] Object which contains the list [AssemblySequence *]
1033  * Arg:        add [OWNER] Object to add to the list [AssemblyOpaqueFeature *]
1034  *
1035  * Return [UNKN ]  Undocumented return value [boolean]
1036  *
1037  */
1038 /* will expand function if necessary */
add_opq_AssemblySequence(AssemblySequence * obj,AssemblyOpaqueFeature * add)1039 boolean add_opq_AssemblySequence(AssemblySequence * obj,AssemblyOpaqueFeature * add)
1040 {
1041     if( obj->opq_len >= obj->opq_maxlen) {
1042       if( expand_opq_AssemblySequence(obj,obj->opq_len + AssemblySequenceLISTLENGTH) == FALSE)
1043         return FALSE;
1044       }
1045 
1046 
1047     obj->opaque[obj->opq_len++]=add;
1048     return TRUE;
1049 }
1050 
1051 
1052 /* Function:  flush_opq_AssemblySequence(obj)
1053  *
1054  * Descrip:    Frees the list elements, sets length to 0
1055  *             If you want to save some elements, use hard_link_xxx
1056  *             to protect them from being actually destroyed in the free
1057  *
1058  *
1059  * Arg:        obj [UNKN ] Object which contains the list  [AssemblySequence *]
1060  *
1061  * Return [UNKN ]  Undocumented return value [int]
1062  *
1063  */
flush_opq_AssemblySequence(AssemblySequence * obj)1064 int flush_opq_AssemblySequence(AssemblySequence * obj)
1065 {
1066     int i;
1067 
1068 
1069     for(i=0;i<obj->opq_len;i++)  { /*for i over list length*/
1070       if( obj->opaque[i] != NULL)    {
1071         free_AssemblyOpaqueFeature(obj->opaque[i]);
1072         obj->opaque[i] = NULL;
1073         }
1074       } /* end of for i over list length */
1075 
1076 
1077     obj->opq_len = 0;
1078     return i;
1079 }
1080 
1081 
1082 /* Function:  AssemblySequence_alloc_std(void)
1083  *
1084  * Descrip:    Equivalent to AssemblySequence_alloc_len(AssemblySequenceLISTLENGTH)
1085  *
1086  *
1087  *
1088  * Return [UNKN ]  Undocumented return value [AssemblySequence *]
1089  *
1090  */
AssemblySequence_alloc_std(void)1091 AssemblySequence * AssemblySequence_alloc_std(void)
1092 {
1093     return AssemblySequence_alloc_len(AssemblySequenceLISTLENGTH);
1094 }
1095 
1096 
1097 /* Function:  AssemblySequence_alloc_len(len)
1098  *
1099  * Descrip:    Allocates len length to all lists
1100  *
1101  *
1102  * Arg:        len [UNKN ] Length of lists to allocate [int]
1103  *
1104  * Return [UNKN ]  Undocumented return value [AssemblySequence *]
1105  *
1106  */
AssemblySequence_alloc_len(int len)1107 AssemblySequence * AssemblySequence_alloc_len(int len)
1108 {
1109     AssemblySequence * out; /* out is exported at the end of function */
1110 
1111 
1112     /* Call alloc function: return NULL if NULL */
1113     /* Warning message alread in alloc function */
1114     if((out = AssemblySequence_alloc()) == NULL)
1115       return NULL;
1116 
1117 
1118     /* Calling ckcalloc for list elements */
1119     if((out->edit = (AssemblySequenceEdit ** ) ckcalloc (len,sizeof(AssemblySequenceEdit *))) == NULL)   {
1120       warn("Warning, ckcalloc failed in AssemblySequence_alloc_len");
1121       return NULL;
1122       }
1123     out->len = 0;
1124     out->maxlen = len;
1125 
1126 
1127     if((out->opaque = (AssemblyOpaqueFeature ** ) ckcalloc (len,sizeof(AssemblyOpaqueFeature *))) == NULL)   {
1128       warn("Warning, ckcalloc failed in AssemblySequence_alloc_len");
1129       return NULL;
1130       }
1131     out->opq_len = 0;
1132     out->opq_maxlen = len;
1133 
1134 
1135     return out;
1136 }
1137 
1138 
1139 /* Function:  hard_link_AssemblySequence(obj)
1140  *
1141  * Descrip:    Bumps up the reference count of the object
1142  *             Meaning that multiple pointers can 'own' it
1143  *
1144  *
1145  * Arg:        obj [UNKN ] Object to be hard linked [AssemblySequence *]
1146  *
1147  * Return [UNKN ]  Undocumented return value [AssemblySequence *]
1148  *
1149  */
hard_link_AssemblySequence(AssemblySequence * obj)1150 AssemblySequence * hard_link_AssemblySequence(AssemblySequence * obj)
1151 {
1152     if( obj == NULL )    {
1153       warn("Trying to hard link to a AssemblySequence object: passed a NULL object");
1154       return NULL;
1155       }
1156     obj->dynamite_hard_link++;
1157     return obj;
1158 }
1159 
1160 
1161 /* Function:  AssemblySequence_alloc(void)
1162  *
1163  * Descrip:    Allocates structure: assigns defaults if given
1164  *
1165  *
1166  *
1167  * Return [UNKN ]  Undocumented return value [AssemblySequence *]
1168  *
1169  */
AssemblySequence_alloc(void)1170 AssemblySequence * AssemblySequence_alloc(void)
1171 {
1172     AssemblySequence * out; /* out is exported at end of function */
1173 
1174 
1175     /* call ckalloc and see if NULL */
1176     if((out=(AssemblySequence *) ckalloc (sizeof(AssemblySequence))) == NULL)    {
1177       warn("AssemblySequence_alloc failed ");
1178       return NULL;  /* calling function should respond! */
1179       }
1180     out->dynamite_hard_link = 1;
1181 #ifdef PTHREAD
1182     pthread_mutex_init(&(out->dynamite_mutex),NULL);
1183 #endif
1184     out->seq = NULL;
1185     out->quality = NULL;
1186     out->edit = NULL;
1187     out->len = out->maxlen = 0;
1188     out->state = 'u';
1189     out->mirror_seq = 0;
1190     out->opaque = NULL;
1191     out->opq_len = out->opq_maxlen = 0;
1192     out->orig = NULL;
1193     out->abrev_repeat = NULL;
1194 
1195 
1196     return out;
1197 }
1198 
1199 
1200 /* Function:  free_AssemblySequence(obj)
1201  *
1202  * Descrip:    Free Function: removes the memory held by obj
1203  *             Will chain up to owned members and clear all lists
1204  *
1205  *
1206  * Arg:        obj [UNKN ] Object that is free'd [AssemblySequence *]
1207  *
1208  * Return [UNKN ]  Undocumented return value [AssemblySequence *]
1209  *
1210  */
free_AssemblySequence(AssemblySequence * obj)1211 AssemblySequence * free_AssemblySequence(AssemblySequence * obj)
1212 {
1213     int return_early = 0;
1214     int i;
1215 
1216 
1217     if( obj == NULL) {
1218       warn("Attempting to free a NULL pointer to a AssemblySequence obj. Should be trappable");
1219       return NULL;
1220       }
1221 
1222 
1223 #ifdef PTHREAD
1224     assert(pthread_mutex_lock(&(obj->dynamite_mutex)) == 0);
1225 #endif
1226     if( obj->dynamite_hard_link > 1)     {
1227       return_early = 1;
1228       obj->dynamite_hard_link--;
1229       }
1230 #ifdef PTHREAD
1231     assert(pthread_mutex_unlock(&(obj->dynamite_mutex)) == 0);
1232 #endif
1233     if( return_early == 1)
1234       return NULL;
1235     if( obj->seq != NULL)
1236       free_Sequence(obj->seq);
1237     if( obj->quality != NULL)
1238       ckfree(obj->quality);
1239     if( obj->edit != NULL)   {
1240       for(i=0;i<obj->len;i++)    {
1241         if( obj->edit[i] != NULL)
1242           free_AssemblySequenceEdit(obj->edit[i]);
1243         }
1244       ckfree(obj->edit);
1245       }
1246     /* obj->mirror is linked in */
1247     /* obj->pair is linked in */
1248     if( obj->opaque != NULL) {
1249       for(i=0;i<obj->opq_len;i++)    {
1250         if( obj->opaque[i] != NULL)
1251           free_AssemblyOpaqueFeature(obj->opaque[i]);
1252         }
1253       ckfree(obj->opaque);
1254       }
1255     if( obj->orig != NULL)
1256       free_Sequence(obj->orig);
1257     if( obj->abrev_repeat != NULL)
1258       ckfree(obj->abrev_repeat);
1259 
1260 
1261     ckfree(obj);
1262     return NULL;
1263 }
1264 
1265 
1266 /* Function:  hard_link_AssemblySequencePlacement(obj)
1267  *
1268  * Descrip:    Bumps up the reference count of the object
1269  *             Meaning that multiple pointers can 'own' it
1270  *
1271  *
1272  * Arg:        obj [UNKN ] Object to be hard linked [AssemblySequencePlacement *]
1273  *
1274  * Return [UNKN ]  Undocumented return value [AssemblySequencePlacement *]
1275  *
1276  */
hard_link_AssemblySequencePlacement(AssemblySequencePlacement * obj)1277 AssemblySequencePlacement * hard_link_AssemblySequencePlacement(AssemblySequencePlacement * obj)
1278 {
1279     if( obj == NULL )    {
1280       warn("Trying to hard link to a AssemblySequencePlacement object: passed a NULL object");
1281       return NULL;
1282       }
1283     obj->dynamite_hard_link++;
1284     return obj;
1285 }
1286 
1287 
1288 /* Function:  AssemblySequencePlacement_alloc(void)
1289  *
1290  * Descrip:    Allocates structure: assigns defaults if given
1291  *
1292  *
1293  *
1294  * Return [UNKN ]  Undocumented return value [AssemblySequencePlacement *]
1295  *
1296  */
AssemblySequencePlacement_alloc(void)1297 AssemblySequencePlacement * AssemblySequencePlacement_alloc(void)
1298 {
1299     AssemblySequencePlacement * out;/* out is exported at end of function */
1300 
1301 
1302     /* call ckalloc and see if NULL */
1303     if((out=(AssemblySequencePlacement *) ckalloc (sizeof(AssemblySequencePlacement))) == NULL)  {
1304       warn("AssemblySequencePlacement_alloc failed ");
1305       return NULL;  /* calling function should respond! */
1306       }
1307     out->dynamite_hard_link = 1;
1308 #ifdef PTHREAD
1309     pthread_mutex_init(&(out->dynamite_mutex),NULL);
1310 #endif
1311     out->aseq = NULL;
1312     out->contig_start = 0;
1313     out->contig_end = 0;
1314     out->seq_start = 0;
1315     out->seq_end = 0;
1316     out->ungapped = FALSE;
1317 
1318 
1319     return out;
1320 }
1321 
1322 
1323 /* Function:  free_AssemblySequencePlacement(obj)
1324  *
1325  * Descrip:    Free Function: removes the memory held by obj
1326  *             Will chain up to owned members and clear all lists
1327  *
1328  *
1329  * Arg:        obj [UNKN ] Object that is free'd [AssemblySequencePlacement *]
1330  *
1331  * Return [UNKN ]  Undocumented return value [AssemblySequencePlacement *]
1332  *
1333  */
free_AssemblySequencePlacement(AssemblySequencePlacement * obj)1334 AssemblySequencePlacement * free_AssemblySequencePlacement(AssemblySequencePlacement * obj)
1335 {
1336     int return_early = 0;
1337 
1338 
1339     if( obj == NULL) {
1340       warn("Attempting to free a NULL pointer to a AssemblySequencePlacement obj. Should be trappable");
1341       return NULL;
1342       }
1343 
1344 
1345 #ifdef PTHREAD
1346     assert(pthread_mutex_lock(&(obj->dynamite_mutex)) == 0);
1347 #endif
1348     if( obj->dynamite_hard_link > 1)     {
1349       return_early = 1;
1350       obj->dynamite_hard_link--;
1351       }
1352 #ifdef PTHREAD
1353     assert(pthread_mutex_unlock(&(obj->dynamite_mutex)) == 0);
1354 #endif
1355     if( return_early == 1)
1356       return NULL;
1357     if( obj->aseq != NULL)
1358       free_AssemblySequence(obj->aseq);
1359 
1360 
1361     ckfree(obj);
1362     return NULL;
1363 }
1364 
1365 
1366 /* Function:  swap_AssemblyContig(list,i,j)
1367  *
1368  * Descrip:    swap function: an internal for qsort_AssemblyContig
1369  *             swaps two positions in the array
1370  *
1371  *
1372  * Arg:        list [UNKN ] List of structures to swap in [AssemblySequencePlacement **]
1373  * Arg:           i [UNKN ] swap position [int]
1374  * Arg:           j [UNKN ] swap position [int]
1375  *
1376  */
1377 /* swap function for qsort function */
swap_AssemblyContig(AssemblySequencePlacement ** list,int i,int j)1378 void swap_AssemblyContig(AssemblySequencePlacement ** list,int i,int j)
1379 {
1380     AssemblySequencePlacement * temp;
1381     temp=list[i];
1382     list[i]=list[j];
1383     list[j]=temp;
1384 }
1385 
1386 
1387 /* Function:  qsort_AssemblyContig(list,left,right,comp)
1388  *
1389  * Descrip:    qsort - lifted from K&R
1390  *             sorts the array using quicksort
1391  *             Probably much better to call sort_AssemblyContig which sorts from start to end
1392  *
1393  *
1394  * Arg:         list [UNKN ] List of structures to swap in [AssemblySequencePlacement **]
1395  * Arg:         left [UNKN ] left position [int]
1396  * Arg:        right [UNKN ] right position [int]
1397  * Arg:         comp [FUNCP] Function which returns -1 or 1 to sort on [int (*comp]
1398  *
1399  */
qsort_AssemblyContig(AssemblySequencePlacement ** list,int left,int right,int (* comp)(AssemblySequencePlacement *,AssemblySequencePlacement *))1400 void qsort_AssemblyContig(AssemblySequencePlacement ** list,int left,int right,int (*comp)(AssemblySequencePlacement * ,AssemblySequencePlacement * ))
1401 {
1402     int i,last;
1403     if( left >= right )
1404       return;
1405 
1406 
1407     swap_AssemblyContig(list,left,(left+right)/2);
1408     last = left;
1409     for ( i=left+1; i <= right;i++)  {
1410       if( (*comp)(list[i],list[left]) < 0)
1411         swap_AssemblyContig (list,++last,i);
1412       }
1413     swap_AssemblyContig (list,left,last);
1414     qsort_AssemblyContig(list,left,last-1,comp);
1415     qsort_AssemblyContig(list,last+1,right,comp);
1416 }
1417 
1418 
1419 /* Function:  sort_AssemblyContig(obj,comp)
1420  *
1421  * Descrip:    sorts from start to end using comp
1422  *             sorts the array using quicksort by calling qsort_AssemblyContig
1423  *
1424  *
1425  * Arg:         obj [UNKN ] Object containing list [AssemblyContig *]
1426  * Arg:        comp [FUNCP] Function which returns -1 or 1 to sort on [int (*comp]
1427  *
1428  */
sort_AssemblyContig(AssemblyContig * obj,int (* comp)(AssemblySequencePlacement *,AssemblySequencePlacement *))1429 void sort_AssemblyContig(AssemblyContig * obj,int (*comp)(AssemblySequencePlacement *, AssemblySequencePlacement *))
1430 {
1431     qsort_AssemblyContig(obj->reads,0,obj->len-1,comp);
1432     return;
1433 }
1434 
1435 
1436 /* Function:  expand_AssemblyContig(obj,len)
1437  *
1438  * Descrip:    Really an internal function for add_AssemblyContig
1439  *
1440  *
1441  * Arg:        obj [UNKN ] Object which contains the list [AssemblyContig *]
1442  * Arg:        len [UNKN ] Length to add one [int]
1443  *
1444  * Return [UNKN ]  Undocumented return value [boolean]
1445  *
1446  */
expand_AssemblyContig(AssemblyContig * obj,int len)1447 boolean expand_AssemblyContig(AssemblyContig * obj,int len)
1448 {
1449 
1450 
1451     if( obj->maxlen > obj->len )     {
1452       warn("expand_AssemblyContig called with no need");
1453       return TRUE;
1454       }
1455 
1456 
1457     if( (obj->reads = (AssemblySequencePlacement ** ) ckrealloc (obj->reads,sizeof(AssemblySequencePlacement *)*len)) == NULL)   {
1458       warn("ckrealloc failed for expand_AssemblyContig, returning FALSE");
1459       return FALSE;
1460       }
1461     obj->maxlen = len;
1462     return TRUE;
1463 }
1464 
1465 
1466 /* Function:  add_AssemblyContig(obj,add)
1467  *
1468  * Descrip:    Adds another object to the list. It will expand the list if necessary
1469  *
1470  *
1471  * Arg:        obj [UNKN ] Object which contains the list [AssemblyContig *]
1472  * Arg:        add [OWNER] Object to add to the list [AssemblySequencePlacement *]
1473  *
1474  * Return [UNKN ]  Undocumented return value [boolean]
1475  *
1476  */
1477 /* will expand function if necessary */
add_AssemblyContig(AssemblyContig * obj,AssemblySequencePlacement * add)1478 boolean add_AssemblyContig(AssemblyContig * obj,AssemblySequencePlacement * add)
1479 {
1480     if( obj->len >= obj->maxlen) {
1481       if( expand_AssemblyContig(obj,obj->len + AssemblyContigLISTLENGTH) == FALSE)
1482         return FALSE;
1483       }
1484 
1485 
1486     obj->reads[obj->len++]=add;
1487     return TRUE;
1488 }
1489 
1490 
1491 /* Function:  flush_AssemblyContig(obj)
1492  *
1493  * Descrip:    Frees the list elements, sets length to 0
1494  *             If you want to save some elements, use hard_link_xxx
1495  *             to protect them from being actually destroyed in the free
1496  *
1497  *
1498  * Arg:        obj [UNKN ] Object which contains the list  [AssemblyContig *]
1499  *
1500  * Return [UNKN ]  Undocumented return value [int]
1501  *
1502  */
flush_AssemblyContig(AssemblyContig * obj)1503 int flush_AssemblyContig(AssemblyContig * obj)
1504 {
1505     int i;
1506 
1507 
1508     for(i=0;i<obj->len;i++)  { /*for i over list length*/
1509       if( obj->reads[i] != NULL) {
1510         free_AssemblySequencePlacement(obj->reads[i]);
1511         obj->reads[i] = NULL;
1512         }
1513       } /* end of for i over list length */
1514 
1515 
1516     obj->len = 0;
1517     return i;
1518 }
1519 
1520 
1521 /* Function:  AssemblyContig_alloc_std(void)
1522  *
1523  * Descrip:    Equivalent to AssemblyContig_alloc_len(AssemblyContigLISTLENGTH)
1524  *
1525  *
1526  *
1527  * Return [UNKN ]  Undocumented return value [AssemblyContig *]
1528  *
1529  */
AssemblyContig_alloc_std(void)1530 AssemblyContig * AssemblyContig_alloc_std(void)
1531 {
1532     return AssemblyContig_alloc_len(AssemblyContigLISTLENGTH);
1533 }
1534 
1535 
1536 /* Function:  AssemblyContig_alloc_len(len)
1537  *
1538  * Descrip:    Allocates len length to all lists
1539  *
1540  *
1541  * Arg:        len [UNKN ] Length of lists to allocate [int]
1542  *
1543  * Return [UNKN ]  Undocumented return value [AssemblyContig *]
1544  *
1545  */
AssemblyContig_alloc_len(int len)1546 AssemblyContig * AssemblyContig_alloc_len(int len)
1547 {
1548     AssemblyContig * out;   /* out is exported at the end of function */
1549 
1550 
1551     /* Call alloc function: return NULL if NULL */
1552     /* Warning message alread in alloc function */
1553     if((out = AssemblyContig_alloc()) == NULL)
1554       return NULL;
1555 
1556 
1557     /* Calling ckcalloc for list elements */
1558     if((out->reads = (AssemblySequencePlacement ** ) ckcalloc (len,sizeof(AssemblySequencePlacement *))) == NULL)    {
1559       warn("Warning, ckcalloc failed in AssemblyContig_alloc_len");
1560       return NULL;
1561       }
1562     out->len = 0;
1563     out->maxlen = len;
1564 
1565 
1566     return out;
1567 }
1568 
1569 
1570 /* Function:  hard_link_AssemblyContig(obj)
1571  *
1572  * Descrip:    Bumps up the reference count of the object
1573  *             Meaning that multiple pointers can 'own' it
1574  *
1575  *
1576  * Arg:        obj [UNKN ] Object to be hard linked [AssemblyContig *]
1577  *
1578  * Return [UNKN ]  Undocumented return value [AssemblyContig *]
1579  *
1580  */
hard_link_AssemblyContig(AssemblyContig * obj)1581 AssemblyContig * hard_link_AssemblyContig(AssemblyContig * obj)
1582 {
1583     if( obj == NULL )    {
1584       warn("Trying to hard link to a AssemblyContig object: passed a NULL object");
1585       return NULL;
1586       }
1587     obj->dynamite_hard_link++;
1588     return obj;
1589 }
1590 
1591 
1592 /* Function:  AssemblyContig_alloc(void)
1593  *
1594  * Descrip:    Allocates structure: assigns defaults if given
1595  *
1596  *
1597  *
1598  * Return [UNKN ]  Undocumented return value [AssemblyContig *]
1599  *
1600  */
AssemblyContig_alloc(void)1601 AssemblyContig * AssemblyContig_alloc(void)
1602 {
1603     AssemblyContig * out;   /* out is exported at end of function */
1604 
1605 
1606     /* call ckalloc and see if NULL */
1607     if((out=(AssemblyContig *) ckalloc (sizeof(AssemblyContig))) == NULL)    {
1608       warn("AssemblyContig_alloc failed ");
1609       return NULL;  /* calling function should respond! */
1610       }
1611     out->dynamite_hard_link = 1;
1612 #ifdef PTHREAD
1613     pthread_mutex_init(&(out->dynamite_mutex),NULL);
1614 #endif
1615     out->consensus = NULL;
1616     out->reads = NULL;
1617     out->len = out->maxlen = 0;
1618     out->clean_start = FALSE;
1619     out->clean_end = FALSE;
1620     out->max_depth = 0;
1621 
1622 
1623     return out;
1624 }
1625 
1626 
1627 /* Function:  free_AssemblyContig(obj)
1628  *
1629  * Descrip:    Free Function: removes the memory held by obj
1630  *             Will chain up to owned members and clear all lists
1631  *
1632  *
1633  * Arg:        obj [UNKN ] Object that is free'd [AssemblyContig *]
1634  *
1635  * Return [UNKN ]  Undocumented return value [AssemblyContig *]
1636  *
1637  */
free_AssemblyContig(AssemblyContig * obj)1638 AssemblyContig * free_AssemblyContig(AssemblyContig * obj)
1639 {
1640     int return_early = 0;
1641     int i;
1642 
1643 
1644     if( obj == NULL) {
1645       warn("Attempting to free a NULL pointer to a AssemblyContig obj. Should be trappable");
1646       return NULL;
1647       }
1648 
1649 
1650 #ifdef PTHREAD
1651     assert(pthread_mutex_lock(&(obj->dynamite_mutex)) == 0);
1652 #endif
1653     if( obj->dynamite_hard_link > 1)     {
1654       return_early = 1;
1655       obj->dynamite_hard_link--;
1656       }
1657 #ifdef PTHREAD
1658     assert(pthread_mutex_unlock(&(obj->dynamite_mutex)) == 0);
1659 #endif
1660     if( return_early == 1)
1661       return NULL;
1662     if( obj->consensus != NULL)
1663       free_Sequence(obj->consensus);
1664     if( obj->reads != NULL)  {
1665       for(i=0;i<obj->len;i++)    {
1666         if( obj->reads[i] != NULL)
1667           free_AssemblySequencePlacement(obj->reads[i]);
1668         }
1669       ckfree(obj->reads);
1670       }
1671 
1672 
1673     ckfree(obj);
1674     return NULL;
1675 }
1676 
1677 
1678 /* Function:  swap_Assembly(list,i,j)
1679  *
1680  * Descrip:    swap function: an internal for qsort_Assembly
1681  *             swaps two positions in the array
1682  *
1683  *
1684  * Arg:        list [UNKN ] List of structures to swap in [AssemblyContig **]
1685  * Arg:           i [UNKN ] swap position [int]
1686  * Arg:           j [UNKN ] swap position [int]
1687  *
1688  */
1689 /* swap function for qsort function */
swap_Assembly(AssemblyContig ** list,int i,int j)1690 void swap_Assembly(AssemblyContig ** list,int i,int j)
1691 {
1692     AssemblyContig * temp;
1693     temp=list[i];
1694     list[i]=list[j];
1695     list[j]=temp;
1696 }
1697 
1698 
1699 /* Function:  qsort_Assembly(list,left,right,comp)
1700  *
1701  * Descrip:    qsort - lifted from K&R
1702  *             sorts the array using quicksort
1703  *             Probably much better to call sort_Assembly which sorts from start to end
1704  *
1705  *
1706  * Arg:         list [UNKN ] List of structures to swap in [AssemblyContig **]
1707  * Arg:         left [UNKN ] left position [int]
1708  * Arg:        right [UNKN ] right position [int]
1709  * Arg:         comp [FUNCP] Function which returns -1 or 1 to sort on [int (*comp]
1710  *
1711  */
qsort_Assembly(AssemblyContig ** list,int left,int right,int (* comp)(AssemblyContig *,AssemblyContig *))1712 void qsort_Assembly(AssemblyContig ** list,int left,int right,int (*comp)(AssemblyContig * ,AssemblyContig * ))
1713 {
1714     int i,last;
1715     if( left >= right )
1716       return;
1717 
1718 
1719     swap_Assembly(list,left,(left+right)/2);
1720     last = left;
1721     for ( i=left+1; i <= right;i++)  {
1722       if( (*comp)(list[i],list[left]) < 0)
1723         swap_Assembly (list,++last,i);
1724       }
1725     swap_Assembly (list,left,last);
1726     qsort_Assembly(list,left,last-1,comp);
1727     qsort_Assembly(list,last+1,right,comp);
1728 }
1729 
1730 
1731 /* Function:  sort_Assembly(obj,comp)
1732  *
1733  * Descrip:    sorts from start to end using comp
1734  *             sorts the array using quicksort by calling qsort_Assembly
1735  *
1736  *
1737  * Arg:         obj [UNKN ] Object containing list [Assembly *]
1738  * Arg:        comp [FUNCP] Function which returns -1 or 1 to sort on [int (*comp]
1739  *
1740  */
sort_Assembly(Assembly * obj,int (* comp)(AssemblyContig *,AssemblyContig *))1741 void sort_Assembly(Assembly * obj,int (*comp)(AssemblyContig *, AssemblyContig *))
1742 {
1743     qsort_Assembly(obj->contig,0,obj->len-1,comp);
1744     return;
1745 }
1746 
1747 
1748 /* Function:  expand_Assembly(obj,len)
1749  *
1750  * Descrip:    Really an internal function for add_Assembly
1751  *
1752  *
1753  * Arg:        obj [UNKN ] Object which contains the list [Assembly *]
1754  * Arg:        len [UNKN ] Length to add one [int]
1755  *
1756  * Return [UNKN ]  Undocumented return value [boolean]
1757  *
1758  */
expand_Assembly(Assembly * obj,int len)1759 boolean expand_Assembly(Assembly * obj,int len)
1760 {
1761 
1762 
1763     if( obj->maxlen > obj->len )     {
1764       warn("expand_Assembly called with no need");
1765       return TRUE;
1766       }
1767 
1768 
1769     if( (obj->contig = (AssemblyContig ** ) ckrealloc (obj->contig,sizeof(AssemblyContig *)*len)) == NULL)   {
1770       warn("ckrealloc failed for expand_Assembly, returning FALSE");
1771       return FALSE;
1772       }
1773     obj->maxlen = len;
1774     return TRUE;
1775 }
1776 
1777 
1778 /* Function:  add_Assembly(obj,add)
1779  *
1780  * Descrip:    Adds another object to the list. It will expand the list if necessary
1781  *
1782  *
1783  * Arg:        obj [UNKN ] Object which contains the list [Assembly *]
1784  * Arg:        add [OWNER] Object to add to the list [AssemblyContig *]
1785  *
1786  * Return [UNKN ]  Undocumented return value [boolean]
1787  *
1788  */
1789 /* will expand function if necessary */
add_Assembly(Assembly * obj,AssemblyContig * add)1790 boolean add_Assembly(Assembly * obj,AssemblyContig * add)
1791 {
1792     if( obj->len >= obj->maxlen) {
1793       if( expand_Assembly(obj,obj->len + AssemblyLISTLENGTH) == FALSE)
1794         return FALSE;
1795       }
1796 
1797 
1798     obj->contig[obj->len++]=add;
1799     return TRUE;
1800 }
1801 
1802 
1803 /* Function:  flush_Assembly(obj)
1804  *
1805  * Descrip:    Frees the list elements, sets length to 0
1806  *             If you want to save some elements, use hard_link_xxx
1807  *             to protect them from being actually destroyed in the free
1808  *
1809  *
1810  * Arg:        obj [UNKN ] Object which contains the list  [Assembly *]
1811  *
1812  * Return [UNKN ]  Undocumented return value [int]
1813  *
1814  */
flush_Assembly(Assembly * obj)1815 int flush_Assembly(Assembly * obj)
1816 {
1817     int i;
1818 
1819 
1820     for(i=0;i<obj->len;i++)  { /*for i over list length*/
1821       if( obj->contig[i] != NULL)    {
1822         free_AssemblyContig(obj->contig[i]);
1823         obj->contig[i] = NULL;
1824         }
1825       } /* end of for i over list length */
1826 
1827 
1828     obj->len = 0;
1829     return i;
1830 }
1831 
1832 
1833 /* Function:  swap_chaff_Assembly(list,i,j)
1834  *
1835  * Descrip:    swap function: an internal for qsort_chaff_Assembly
1836  *             swaps two positions in the array
1837  *
1838  *
1839  * Arg:        list [UNKN ] List of structures to swap in [AssemblySequence **]
1840  * Arg:           i [UNKN ] swap position [int]
1841  * Arg:           j [UNKN ] swap position [int]
1842  *
1843  */
1844 /* swap function for qsort function */
swap_chaff_Assembly(AssemblySequence ** list,int i,int j)1845 void swap_chaff_Assembly(AssemblySequence ** list,int i,int j)
1846 {
1847     AssemblySequence * temp;
1848     temp=list[i];
1849     list[i]=list[j];
1850     list[j]=temp;
1851 }
1852 
1853 
1854 /* Function:  qsort_chaff_Assembly(list,left,right,comp)
1855  *
1856  * Descrip:    qsort - lifted from K&R
1857  *             sorts the array using quicksort
1858  *             Probably much better to call sort_chaff_Assembly which sorts from start to end
1859  *
1860  *
1861  * Arg:         list [UNKN ] List of structures to swap in [AssemblySequence **]
1862  * Arg:         left [UNKN ] left position [int]
1863  * Arg:        right [UNKN ] right position [int]
1864  * Arg:         comp [FUNCP] Function which returns -1 or 1 to sort on [int (*comp]
1865  *
1866  */
qsort_chaff_Assembly(AssemblySequence ** list,int left,int right,int (* comp)(AssemblySequence *,AssemblySequence *))1867 void qsort_chaff_Assembly(AssemblySequence ** list,int left,int right,int (*comp)(AssemblySequence * ,AssemblySequence * ))
1868 {
1869     int i,last;
1870     if( left >= right )
1871       return;
1872 
1873 
1874     swap_chaff_Assembly(list,left,(left+right)/2);
1875     last = left;
1876     for ( i=left+1; i <= right;i++)  {
1877       if( (*comp)(list[i],list[left]) < 0)
1878         swap_chaff_Assembly (list,++last,i);
1879       }
1880     swap_chaff_Assembly (list,left,last);
1881     qsort_chaff_Assembly(list,left,last-1,comp);
1882     qsort_chaff_Assembly(list,last+1,right,comp);
1883 }
1884 
1885 
1886 /* Function:  sort_chaff_Assembly(obj,comp)
1887  *
1888  * Descrip:    sorts from start to end using comp
1889  *             sorts the array using quicksort by calling qsort_chaff_Assembly
1890  *
1891  *
1892  * Arg:         obj [UNKN ] Object containing list [Assembly *]
1893  * Arg:        comp [FUNCP] Function which returns -1 or 1 to sort on [int (*comp]
1894  *
1895  */
sort_chaff_Assembly(Assembly * obj,int (* comp)(AssemblySequence *,AssemblySequence *))1896 void sort_chaff_Assembly(Assembly * obj,int (*comp)(AssemblySequence *, AssemblySequence *))
1897 {
1898     qsort_chaff_Assembly(obj->chaff,0,obj->chaff_len-1,comp);
1899     return;
1900 }
1901 
1902 
1903 /* Function:  expand_chaff_Assembly(obj,len)
1904  *
1905  * Descrip:    Really an internal function for add_chaff_Assembly
1906  *
1907  *
1908  * Arg:        obj [UNKN ] Object which contains the list [Assembly *]
1909  * Arg:        len [UNKN ] Length to add one [int]
1910  *
1911  * Return [UNKN ]  Undocumented return value [boolean]
1912  *
1913  */
expand_chaff_Assembly(Assembly * obj,int len)1914 boolean expand_chaff_Assembly(Assembly * obj,int len)
1915 {
1916 
1917 
1918     if( obj->chaff_maxlen > obj->chaff_len )     {
1919       warn("expand_Assemblychaff_ called with no need");
1920       return TRUE;
1921       }
1922 
1923 
1924     if( (obj->chaff = (AssemblySequence ** ) ckrealloc (obj->chaff,sizeof(AssemblySequence *)*len)) == NULL)     {
1925       warn("ckrealloc failed for expand_Assembly, returning FALSE");
1926       return FALSE;
1927       }
1928     obj->chaff_maxlen = len;
1929     return TRUE;
1930 }
1931 
1932 
1933 /* Function:  add_chaff_Assembly(obj,add)
1934  *
1935  * Descrip:    Adds another object to the list. It will expand the list if necessary
1936  *
1937  *
1938  * Arg:        obj [UNKN ] Object which contains the list [Assembly *]
1939  * Arg:        add [OWNER] Object to add to the list [AssemblySequence *]
1940  *
1941  * Return [UNKN ]  Undocumented return value [boolean]
1942  *
1943  */
1944 /* will expand function if necessary */
add_chaff_Assembly(Assembly * obj,AssemblySequence * add)1945 boolean add_chaff_Assembly(Assembly * obj,AssemblySequence * add)
1946 {
1947     if( obj->chaff_len >= obj->chaff_maxlen) {
1948       if( expand_chaff_Assembly(obj,obj->chaff_len + AssemblyLISTLENGTH) == FALSE)
1949         return FALSE;
1950       }
1951 
1952 
1953     obj->chaff[obj->chaff_len++]=add;
1954     return TRUE;
1955 }
1956 
1957 
1958 /* Function:  flush_chaff_Assembly(obj)
1959  *
1960  * Descrip:    Frees the list elements, sets length to 0
1961  *             If you want to save some elements, use hard_link_xxx
1962  *             to protect them from being actually destroyed in the free
1963  *
1964  *
1965  * Arg:        obj [UNKN ] Object which contains the list  [Assembly *]
1966  *
1967  * Return [UNKN ]  Undocumented return value [int]
1968  *
1969  */
flush_chaff_Assembly(Assembly * obj)1970 int flush_chaff_Assembly(Assembly * obj)
1971 {
1972     int i;
1973 
1974 
1975     for(i=0;i<obj->chaff_len;i++)    { /*for i over list length*/
1976       if( obj->chaff[i] != NULL) {
1977         free_AssemblySequence(obj->chaff[i]);
1978         obj->chaff[i] = NULL;
1979         }
1980       } /* end of for i over list length */
1981 
1982 
1983     obj->chaff_len = 0;
1984     return i;
1985 }
1986 
1987 
1988 /* Function:  Assembly_alloc_std(void)
1989  *
1990  * Descrip:    Equivalent to Assembly_alloc_len(AssemblyLISTLENGTH)
1991  *
1992  *
1993  *
1994  * Return [UNKN ]  Undocumented return value [Assembly *]
1995  *
1996  */
Assembly_alloc_std(void)1997 Assembly * Assembly_alloc_std(void)
1998 {
1999     return Assembly_alloc_len(AssemblyLISTLENGTH);
2000 }
2001 
2002 
2003 /* Function:  Assembly_alloc_len(len)
2004  *
2005  * Descrip:    Allocates len length to all lists
2006  *
2007  *
2008  * Arg:        len [UNKN ] Length of lists to allocate [int]
2009  *
2010  * Return [UNKN ]  Undocumented return value [Assembly *]
2011  *
2012  */
Assembly_alloc_len(int len)2013 Assembly * Assembly_alloc_len(int len)
2014 {
2015     Assembly * out; /* out is exported at the end of function */
2016 
2017 
2018     /* Call alloc function: return NULL if NULL */
2019     /* Warning message alread in alloc function */
2020     if((out = Assembly_alloc()) == NULL)
2021       return NULL;
2022 
2023 
2024     /* Calling ckcalloc for list elements */
2025     if((out->contig = (AssemblyContig ** ) ckcalloc (len,sizeof(AssemblyContig *))) == NULL) {
2026       warn("Warning, ckcalloc failed in Assembly_alloc_len");
2027       return NULL;
2028       }
2029     out->len = 0;
2030     out->maxlen = len;
2031 
2032 
2033     if((out->chaff = (AssemblySequence ** ) ckcalloc (len,sizeof(AssemblySequence *))) == NULL)  {
2034       warn("Warning, ckcalloc failed in Assembly_alloc_len");
2035       return NULL;
2036       }
2037     out->chaff_len = 0;
2038     out->chaff_maxlen = len;
2039 
2040 
2041     return out;
2042 }
2043 
2044 
2045 /* Function:  hard_link_Assembly(obj)
2046  *
2047  * Descrip:    Bumps up the reference count of the object
2048  *             Meaning that multiple pointers can 'own' it
2049  *
2050  *
2051  * Arg:        obj [UNKN ] Object to be hard linked [Assembly *]
2052  *
2053  * Return [UNKN ]  Undocumented return value [Assembly *]
2054  *
2055  */
hard_link_Assembly(Assembly * obj)2056 Assembly * hard_link_Assembly(Assembly * obj)
2057 {
2058     if( obj == NULL )    {
2059       warn("Trying to hard link to a Assembly object: passed a NULL object");
2060       return NULL;
2061       }
2062     obj->dynamite_hard_link++;
2063     return obj;
2064 }
2065 
2066 
2067 /* Function:  Assembly_alloc(void)
2068  *
2069  * Descrip:    Allocates structure: assigns defaults if given
2070  *
2071  *
2072  *
2073  * Return [UNKN ]  Undocumented return value [Assembly *]
2074  *
2075  */
Assembly_alloc(void)2076 Assembly * Assembly_alloc(void)
2077 {
2078     Assembly * out; /* out is exported at end of function */
2079 
2080 
2081     /* call ckalloc and see if NULL */
2082     if((out=(Assembly *) ckalloc (sizeof(Assembly))) == NULL)    {
2083       warn("Assembly_alloc failed ");
2084       return NULL;  /* calling function should respond! */
2085       }
2086     out->dynamite_hard_link = 1;
2087 #ifdef PTHREAD
2088     pthread_mutex_init(&(out->dynamite_mutex),NULL);
2089 #endif
2090     out->contig = NULL;
2091     out->len = out->maxlen = 0;
2092     out->chaff = NULL;
2093     out->chaff_len = out->chaff_maxlen = 0;
2094 
2095 
2096     return out;
2097 }
2098 
2099 
2100 /* Function:  free_Assembly(obj)
2101  *
2102  * Descrip:    Free Function: removes the memory held by obj
2103  *             Will chain up to owned members and clear all lists
2104  *
2105  *
2106  * Arg:        obj [UNKN ] Object that is free'd [Assembly *]
2107  *
2108  * Return [UNKN ]  Undocumented return value [Assembly *]
2109  *
2110  */
free_Assembly(Assembly * obj)2111 Assembly * free_Assembly(Assembly * obj)
2112 {
2113     int return_early = 0;
2114     int i;
2115 
2116 
2117     if( obj == NULL) {
2118       warn("Attempting to free a NULL pointer to a Assembly obj. Should be trappable");
2119       return NULL;
2120       }
2121 
2122 
2123 #ifdef PTHREAD
2124     assert(pthread_mutex_lock(&(obj->dynamite_mutex)) == 0);
2125 #endif
2126     if( obj->dynamite_hard_link > 1)     {
2127       return_early = 1;
2128       obj->dynamite_hard_link--;
2129       }
2130 #ifdef PTHREAD
2131     assert(pthread_mutex_unlock(&(obj->dynamite_mutex)) == 0);
2132 #endif
2133     if( return_early == 1)
2134       return NULL;
2135     if( obj->contig != NULL) {
2136       for(i=0;i<obj->len;i++)    {
2137         if( obj->contig[i] != NULL)
2138           free_AssemblyContig(obj->contig[i]);
2139         }
2140       ckfree(obj->contig);
2141       }
2142     if( obj->chaff != NULL)  {
2143       for(i=0;i<obj->chaff_len;i++)  {
2144         if( obj->chaff[i] != NULL)
2145           free_AssemblySequence(obj->chaff[i]);
2146         }
2147       ckfree(obj->chaff);
2148       }
2149 
2150 
2151     ckfree(obj);
2152     return NULL;
2153 }
2154 
2155 
2156 /* Function:  hard_link_AssemblyOutputPara(obj)
2157  *
2158  * Descrip:    Bumps up the reference count of the object
2159  *             Meaning that multiple pointers can 'own' it
2160  *
2161  *
2162  * Arg:        obj [UNKN ] Object to be hard linked [AssemblyOutputPara *]
2163  *
2164  * Return [UNKN ]  Undocumented return value [AssemblyOutputPara *]
2165  *
2166  */
hard_link_AssemblyOutputPara(AssemblyOutputPara * obj)2167 AssemblyOutputPara * hard_link_AssemblyOutputPara(AssemblyOutputPara * obj)
2168 {
2169     if( obj == NULL )    {
2170       warn("Trying to hard link to a AssemblyOutputPara object: passed a NULL object");
2171       return NULL;
2172       }
2173     obj->dynamite_hard_link++;
2174     return obj;
2175 }
2176 
2177 
2178 /* Function:  AssemblyOutputPara_alloc(void)
2179  *
2180  * Descrip:    Allocates structure: assigns defaults if given
2181  *
2182  *
2183  *
2184  * Return [UNKN ]  Undocumented return value [AssemblyOutputPara *]
2185  *
2186  */
AssemblyOutputPara_alloc(void)2187 AssemblyOutputPara * AssemblyOutputPara_alloc(void)
2188 {
2189     AssemblyOutputPara * out;   /* out is exported at end of function */
2190 
2191 
2192     /* call ckalloc and see if NULL */
2193     if((out=(AssemblyOutputPara *) ckalloc (sizeof(AssemblyOutputPara))) == NULL)    {
2194       warn("AssemblyOutputPara_alloc failed ");
2195       return NULL;  /* calling function should respond! */
2196       }
2197     out->dynamite_hard_link = 1;
2198 #ifdef PTHREAD
2199     pthread_mutex_init(&(out->dynamite_mutex),NULL);
2200 #endif
2201     out->min_size = 0;
2202 
2203 
2204     return out;
2205 }
2206 
2207 
2208 /* Function:  free_AssemblyOutputPara(obj)
2209  *
2210  * Descrip:    Free Function: removes the memory held by obj
2211  *             Will chain up to owned members and clear all lists
2212  *
2213  *
2214  * Arg:        obj [UNKN ] Object that is free'd [AssemblyOutputPara *]
2215  *
2216  * Return [UNKN ]  Undocumented return value [AssemblyOutputPara *]
2217  *
2218  */
free_AssemblyOutputPara(AssemblyOutputPara * obj)2219 AssemblyOutputPara * free_AssemblyOutputPara(AssemblyOutputPara * obj)
2220 {
2221     int return_early = 0;
2222 
2223 
2224     if( obj == NULL) {
2225       warn("Attempting to free a NULL pointer to a AssemblyOutputPara obj. Should be trappable");
2226       return NULL;
2227       }
2228 
2229 
2230 #ifdef PTHREAD
2231     assert(pthread_mutex_lock(&(obj->dynamite_mutex)) == 0);
2232 #endif
2233     if( obj->dynamite_hard_link > 1)     {
2234       return_early = 1;
2235       obj->dynamite_hard_link--;
2236       }
2237 #ifdef PTHREAD
2238     assert(pthread_mutex_unlock(&(obj->dynamite_mutex)) == 0);
2239 #endif
2240     if( return_early == 1)
2241       return NULL;
2242 
2243 
2244     ckfree(obj);
2245     return NULL;
2246 }
2247 
2248 
2249 
2250 #ifdef _cplusplus
2251 }
2252 #endif
2253