1 #ifdef _cplusplus
2 extern "C" {
3 #endif
4 #include "dnaprofileengine.h"
5 
6 
7 # line 66 "dnaprofileengine.dy"
TransFactorSet_from_DnaProfileSet(DnaProfileSet * in)8 TransFactorSet * TransFactorSet_from_DnaProfileSet(DnaProfileSet * in)
9 {
10   int i;
11   char name_buf[512];
12   TransFactorSet * out;
13 
14   out = TransFactorSet_alloc_len(in->len);
15 
16 
17 
18 
19 }
20 
21 # line 79 "dnaprofileengine.dy"
balanced_4_Sequence_fasta_stream(FILE * ifp)22 DnaProfileNode * balanced_4_Sequence_fasta_stream(FILE * ifp)
23 {
24   Sequence * one;
25   Sequence * two;
26   Sequence * three;
27   Sequence * four;
28 
29   DnaProfileNode * leafone;
30   DnaProfileNode * leaftwo;
31   DnaProfileNode * leafthree;
32   DnaProfileNode * leaffour;
33 
34   DnaProfileNode * midonetwo;
35   DnaProfileNode * midthreefour;
36 
37   DnaProfileNode * root;
38 
39   one = read_fasta_Sequence(ifp);
40   fprintf(stderr,"Got 1 %s\n",one->name);
41   two = read_fasta_Sequence(ifp);
42   fprintf(stderr,"Got 2 %s\n",two->name);
43   three = read_fasta_Sequence(ifp);
44   fprintf(stderr,"Got 3 %s\n",three->name);
45   four = read_fasta_Sequence(ifp);
46   fprintf(stderr,"Got 4 %s\n",four->name);
47 
48   assert(four != NULL);
49 
50   leafone   = new_leaf_DnaProfileNode(one);
51   leaftwo   = new_leaf_DnaProfileNode(two);
52   leafthree = new_leaf_DnaProfileNode(three);
53   leaffour  = new_leaf_DnaProfileNode(four);
54 
55   midonetwo = DnaProfileNode_alloc();
56   midonetwo->type  = DnaProfileNode_SET;
57   midonetwo->left  = leafone;
58   midonetwo->right = leaftwo;
59 
60   midthreefour = DnaProfileNode_alloc();
61   midthreefour->type  = DnaProfileNode_SET;
62   midthreefour->left  = leafthree;
63   midthreefour->right = leaffour;
64 
65   root = DnaProfileNode_alloc();
66   root->type  = DnaProfileNode_SET;
67   root->left  = midonetwo;
68   root->right = midthreefour;
69 
70   return root;
71 }
72 
73 
74 # line 131 "dnaprofileengine.dy"
simple_cascade_Sequence_fasta_stream(FILE * ifp)75 DnaProfileNode * simple_cascade_Sequence_fasta_stream(FILE * ifp)
76 {
77   DnaProfileNode * head;
78   DnaProfileNode * leaf;
79   DnaProfileNode * first;
80   DnaProfileNode * temp;
81 
82   Sequence * read;
83 
84   read = read_fasta_Sequence(ifp);
85   uppercase_Sequence(read);
86 
87   if( read == NULL ) {
88     fatal("Attempting to build a cascade with only no sequences! impossible!");
89   }
90   first = new_leaf_DnaProfileNode(read);
91 
92   read = read_fasta_Sequence(ifp);
93   uppercase_Sequence(read);
94   if( read == NULL ) {
95     fatal("Attempting to build a cascade with only one sequence! impossible!");
96   }
97 
98   leaf = new_leaf_DnaProfileNode(read);
99 
100   head = DnaProfileNode_alloc();
101   head->type = DnaProfileNode_SET;
102   head->left = first;
103   head->right = leaf;
104 
105 
106   /* now loop over all remaining sequences */
107 
108   while( (read = read_fasta_Sequence(ifp)) != NULL ) {
109     uppercase_Sequence(read);
110     leaf = new_leaf_DnaProfileNode(read);
111     temp = DnaProfileNode_alloc();
112     temp->type  = DnaProfileNode_SET;
113     temp->left  = head;
114     temp->right = leaf;
115 
116     head = temp;
117   }
118 
119   return head;
120 }
121 
122 
123 # line 179 "dnaprofileengine.dy"
new_leaf_DnaProfileNode(Sequence * seq)124 DnaProfileNode * new_leaf_DnaProfileNode(Sequence * seq)
125 {
126   DnaProfileNode * out;
127 
128   assert(seq != NULL);
129 
130   out = DnaProfileNode_alloc();
131   out->type = DnaProfileNode_LEAF;
132   out->leaf = seq;
133 
134   return out;
135 }
136 
137 
138 
139 
140 
141 # line 196 "dnaprofileengine.dy"
populate_DnaProfileNode_from_root(DnaProfileNode * root,DnaProfileEnginePara * dpep)142 void populate_DnaProfileNode_from_root(DnaProfileNode * root,DnaProfileEnginePara * dpep)
143 {
144   assert(root != NULL);
145   if( root->left->type == DnaProfileNode_SET ) {
146     populate_DnaProfileNode_from_root(root->left,dpep);
147   }
148   if( root->right->type == DnaProfileNode_SET ) {
149     populate_DnaProfileNode_from_root(root->right,dpep);
150   }
151 
152   /* left and right now populated */
153 
154   root->set = join_two_DnaProfileNode(root->left,root->right,dpep);
155 }
156 
157 # line 211 "dnaprofileengine.dy"
join_two_DnaProfileNode(DnaProfileNode * left,DnaProfileNode * right,DnaProfileEnginePara * dpep)158 DnaProfileSet * join_two_DnaProfileNode(DnaProfileNode * left,DnaProfileNode * right,DnaProfileEnginePara * dpep)
159 {
160   /* big switch around the types of left and right */
161 
162   fprintf(stderr,"Entering join with %d vs %d\n",left->type,right->type);
163 
164   if( left->type == DnaProfileNode_LEAF && right->type == DnaProfileNode_LEAF ) {
165     assert(left->leaf);
166     assert(right->leaf);
167     return DnaProfileSet_from_leaf_leaf(left->leaf,right->leaf,dpep);
168   }
169 
170   if( left->type == DnaProfileNode_SET && right->type == DnaProfileNode_LEAF ) {
171     assert(left->set);
172     assert(right->leaf);
173     return DnaProfileSet_from_leaf_node(right->leaf,left->set,dpep);
174   }
175 
176   if( left->type == DnaProfileNode_LEAF && right->type == DnaProfileNode_SET ) {
177     assert(left->leaf);
178     assert(right->set);
179     return DnaProfileSet_from_leaf_node(left->leaf,right->set,dpep);
180   }
181 
182   if( left->type == DnaProfileNode_SET && right->type == DnaProfileNode_SET ) {
183     assert(left->set);
184     assert(right->set);
185     return DnaProfileSet_from_node_node(left->set,right->set,dpep);
186   }
187 
188 
189   fatal("Should not get here. Weird no leaf/node case");
190 
191   return NULL;
192 
193 }
194 
195 
196 
197 
198 # line 251 "dnaprofileengine.dy"
new_DnaProfileEnginePara_from_argv(int * argc,char ** argv)199 DnaProfileEnginePara * new_DnaProfileEnginePara_from_argv(int * argc,char ** argv)
200 {
201   DnaProfileEnginePara * out;
202 
203   out = DnaProfileEnginePara_alloc();
204 
205   out->dpri = new_DPRunImpl_from_argv(argc,argv);
206 
207   out->setpara = new_LocalCisHitSetPara_from_argv(argc,argv);
208   out->lchs    = standard_LocalCisHitScore(NMaskType_VARIABLE);
209 
210   out->rm = RandomModelDNA_std();
211 
212   out->pseudo = 0.5;
213   out->open_unmatched = 0.001;
214   out->ext_unmatched = 0.8;
215   out->gap_unmatched = 0.5;
216   out->seq_id = 0.8;
217   out->m2i = 0.1;
218   out->m2d = 0.1;
219   out->i2i = 0.8;
220   out->d2d = 0.8;
221   out->min_seq_prof = 400;
222 
223   strip_out_float_argument(argc,argv,"dnap_pseudo",&out->pseudo);
224   strip_out_float_argument(argc,argv,"dnap_open_un",&out->open_unmatched);
225   strip_out_float_argument(argc,argv,"dnap_ext_un",&out->ext_unmatched);
226   strip_out_float_argument(argc,argv,"dnap_gap_un",&out->ext_unmatched);
227   strip_out_float_argument(argc,argv,"dnap_seq_self",&out->seq_id);
228   strip_out_float_argument(argc,argv,"dnap_m2i",&out->m2i);
229   strip_out_float_argument(argc,argv,"dnap_m2d",&out->m2d);
230   strip_out_float_argument(argc,argv,"dnap_i2i",&out->i2i);
231   strip_out_float_argument(argc,argv,"dnap_d2d",&out->d2d);
232   strip_out_integer_argument(argc,argv,"dnap_min_seq_prof",&out->min_seq_prof);
233 
234   return out;
235 
236 }
237 
238 # line 290 "dnaprofileengine.dy"
show_help_DnaProfileEnginePara(FILE * ofp)239 void show_help_DnaProfileEnginePara(FILE * ofp)
240 {
241   fprintf(ofp,"DnaProfile build/matching parameters\n");
242   fprintf(ofp,"  -dnap_pseudo   [0.1]  pseudo count used in profile construction\n");
243   fprintf(ofp,"  -dnap_open_un  [0.4]  unmatched probability open\n");
244   fprintf(ofp,"  -dnap_ext_un   [0.95] unmatched extend probability\n");
245   fprintf(ofp,"  -dnap_ext_un   [0.5]  unmatched gap    probability\n");
246   fprintf(ofp,"  -dnap_seq_self [0.8]  %% identity for pure sequence matching\n");
247   fprintf(ofp,"  -dnap_m2i      [0.1]  Match 2 insert transition in dnaprofiles\n");
248   fprintf(ofp,"  -dnap_m2d      [0.1]  Match 2 delete transitions in dnaprofiles\n");
249   fprintf(ofp,"  -dnap_i2i      [0.8]  Insert 2 Insert transition in dnaprofiles\n");
250   fprintf(ofp,"  -dnap_d2d      [0.8]  Delete 2 Delete transition in dnaprofiles\n");
251   fprintf(ofp,"  -dnap_min_seq_prof [400] minimum score for sequence profile matching\n");
252 
253   fprintf(ofp,"Local CisHit para for sequence to sequnence matching\n");
254   show_help_LocalCisHitSetPara(ofp);
255 
256   show_help_DPRunImpl(ofp);
257 
258 }
259 
260 # line 311 "dnaprofileengine.dy"
filter_DnaProfileSet(DnaProfileSet * in,int min_length,int min_score)261 DnaProfileSet * filter_DnaProfileSet(DnaProfileSet * in,int min_length,int min_score)
262 {
263   int i;
264   DnaProfileSet * out;
265 
266   out = DnaProfileSet_alloc_std();
267 
268   for(i=0;i<in->len;i++) {
269     if( in->dnap[i]->sa->seq[0]->len <= min_length ) {
270       continue;
271     }
272 
273     add_DnaProfileSet(out,hard_link_DnaProfile(in->dnap[i]));
274   }
275 
276   return out;
277 
278 }
279 
280 # line 330 "dnaprofileengine.dy"
DnaProfileSet_from_leaf_leaf(Sequence * one,Sequence * two,DnaProfileEnginePara * dpep)281 DnaProfileSet * DnaProfileSet_from_leaf_leaf(Sequence * one,Sequence * two,DnaProfileEnginePara * dpep)
282 {
283   DnaProfileSet * out;
284   DnaMatrix * dm;
285   DnaProbMatrix * dmp;
286   PairwiseShortDna * psd;
287   LocalCisHitSet * set;
288   Sequence * two_rev;
289   DnaProfile * dp;
290   SeqAlign * sa;
291 
292   Sequence * temp1;
293   Sequence * temp2;
294 
295   char * temp_seq1;
296   char * temp_seq2;
297 
298   int unmatched;
299   int seq1_i,seq2_i;
300 
301   AlnColumn * alc;
302   int i;
303 
304   two_rev = reverse_complement_Sequence(two);
305 
306 
307   dmp = DnaProbMatrix_from_match(0.65,NMaskType_BANNED);
308   assert(dmp);
309   flat_null_DnaProbMatrix(dmp);
310 
311   dm = DnaMatrix_from_DnaProbMatrix(dmp);
312 
313   show_DnaMatrix(dm,stderr);
314 
315   psd = query_to_reverse_target(one,two,dm,0,one->len,0,two->len);
316 
317 
318   set = make_LocalCisHitSet(one,two,two_rev,psd->forward,psd->reverse,dpep->setpara,dpep->lchs,NULL,NULL,NULL,NULL,0,dpep->dpri);
319 
320   temp_seq1 = calloc(one->len > two->len ? one->len : two->len,sizeof(char));
321   temp_seq2 = calloc(one->len > two->len ? one->len : two->len,sizeof(char));
322 
323   out = DnaProfileSet_alloc_std();
324 
325   for(i=0;i<set->len;i++) {
326     unmatched = 1;
327     sa = NULL;
328 
329     /*
330      * Main loop over DBA style alignment. We need to make one
331      * DnaProfile per matching block, which are separated by unmatched
332      * blocks. Could potentially be no blocks.
333      *
334      * Extra annoyance provided by the "wrong" convention being used in
335      * DBA alignments, meaning that "inserts" label the "sequence" containing
336      * strand, not the non-sequence containing strand. Stupid, but dbadisplay
337      * uses this convention, so if we changed, would have to fix lots of exisiting
338      * code. Not ideal.
339      *
340      */
341 
342 
343     for(alc=set->lch[i]->alb->start;alc != NULL;alc=alc->next) {
344 
345       /* hitting an unmatched block */
346       if( unmatched == 0 && (strcmp(alc->alu[0]->text_label,"UM") == 0 ||
347 	  strcmp(alc->alu[0]->text_label,"UI") == 0 || strcmp(alc->alu[0]->text_label,"END") == 0) ) {
348 	/* if we have an alignment, put it away now */
349 	if( sa != NULL ) {
350 	  temp_seq1[seq1_i] = '\0';
351 	  temp_seq2[seq2_i] = '\0';
352 
353 	  temp1 = Sequence_from_static_memory(one->name,temp_seq1);
354 	  temp2 = Sequence_from_static_memory(two->name,temp_seq2);
355 
356 	  add_SeqAlign(sa,temp1);
357 	  add_SeqAlign(sa,temp2);
358 
359 	  dp = naive_DnaProfile_from_SeqAlign(sa,0.15,0.1,0.1,0.8,0.8);
360 	  fold_RandomModel_DnaProfile(dp,dpep->rm);
361 
362 	  add_DnaProfileSet(out,dp);
363 	  free_SeqAlign(sa); /* hard linked inside DP */
364 	  sa = NULL;
365 	}
366 
367 	continue;
368       } else if( unmatched == 1 && (strstartcmp(alc->alu[0]->text_label,"MM") == 0 ||
369 				    strstartcmp(alc->alu[0]->text_label,"MI") == 0 ) ) {
370 	unmatched = 0;
371 
372 	sa = SeqAlign_alloc_len(2);
373 	seq1_i = 0;
374 	seq2_i = 0;
375       }
376 
377       /* only if we are in a matched block */
378       if( unmatched == 0 ) {
379 	/* Bloody twisted DBA convention - Niclas has alot to answer for.
380 	   Evil stuff -- MI is on the wrong strand! */
381 	if( strstartcmp(alc->alu[0]->text_label,"MI") == 0 ) {
382 	  /* means 0 has sequence, other has gap */
383 	  temp_seq1[seq1_i++] = one->seq[alc->alu[0]->end];
384 	  temp_seq2[seq2_i++] = '-';
385 	} else if ( strstartcmp(alc->alu[1]->text_label,"MI") == 0 ) {
386 	  temp_seq1[seq1_i++] = '-';
387 	  temp_seq2[seq2_i++] = two->seq[alc->alu[1]->end];
388 	} else if ( strstartcmp(alc->alu[0]->text_label,"MM") == 0 &&
389 		    strstartcmp(alc->alu[1]->text_label,"MM") == 0 ) {
390 	  temp_seq1[seq1_i++] = one->seq[alc->alu[0]->end];
391 	  temp_seq2[seq2_i++] = two->seq[alc->alu[1]->end];
392 	} else {
393 	  warn("Impossible label pair reached in matched block local cis hit stuff, %s,%s",alc->alu[0]->text_label,alc->alu[1]->text_label);
394 	}
395       }
396 
397     }
398   }
399 
400 
401   free(temp_seq1);
402   free(temp_seq2);
403   free_PairwiseShortDna(psd);
404   free_LocalCisHitSet(set);
405   free_DnaMatrix(dm);
406   free_DnaProbMatrix(dmp);
407 
408   return out;
409 }
410 
411 
412 
413 # line 462 "dnaprofileengine.dy"
DnaProfileSet_from_node_node(DnaProfileSet * one,DnaProfileSet * two,DnaProfileEnginePara * dpep)414 DnaProfileSet * DnaProfileSet_from_node_node(DnaProfileSet * one,DnaProfileSet * two,DnaProfileEnginePara * dpep)
415 {
416   DnaProfile * new_dnap;
417   DnaProfileSet * out;
418   DnaProfileMatchPairSet * dpmps;
419   SeqAlign * sa;
420   int i;
421   int j;
422 
423   dpmps = DnaProfileMatchPairSet_alloc_std();
424 
425   for(i=0;i<one->len;i++) {
426     for(j=0;j<two->len;j++) {
427 	add_DnaProfileMatchPairSet(dpmps,DnaProfileMatchPair_from_DnaProfile(one->dnap[i],two->dnap[j],dpep));
428     }
429   }
430 
431   sort_DnaProfileMatchPairSet_by_score(dpmps);
432 
433 
434   out = DnaProfileSet_alloc_std();
435 
436   for(i=0;i<dpmps->len;i++) {
437     /* check this profile has not already been used */
438     /* not done yet */
439 
440     if( dpmps->pair[i]->score < dpep->min_seq_prof ) {
441       fprintf(stderr,"Warning... rejecting match due to score %d vs %d\n",dpmps->pair[i]->score,dpep->min_seq_prof);
442       break;
443     }
444 
445 
446     sa = merged_SeqAlign(dpmps->pair[i]->query,dpmps->pair[i]->target,dpmps->pair[i]->alb);
447 
448     fprintf(stderr,"Node/Node Accepting score at %d length %d\n",dpmps->pair[i]->score,sa->seq[0]->len);
449 
450     new_dnap = naive_DnaProfile_from_SeqAlign(sa,dpep->pseudo,dpep->m2i,dpep->m2d,dpep->i2i,dpep->d2d);
451 
452     assert(new_dnap != NULL);
453     /* need to log-odds dnap here */
454 
455     fold_RandomModel_DnaProfile(new_dnap,dpep->rm);
456 
457 
458     add_DnaProfileSet(out,new_dnap);
459   }
460 
461 
462   fprintf(stderr,"Returing %d profiles\n",out->len);
463   return out;
464 }
465 
466 # line 514 "dnaprofileengine.dy"
DnaProfileSet_from_leaf_node(Sequence * one,DnaProfileSet * two,DnaProfileEnginePara * dpep)467 DnaProfileSet * DnaProfileSet_from_leaf_node(Sequence * one,DnaProfileSet * two,DnaProfileEnginePara * dpep)
468 {
469   DnaProfileSet * out;
470   DnaProfile * dnap;
471   DnaProfile * dnapr;
472   DnaProfileMatchPairSet * dpmps;
473   Sequence * rev;
474   SeqAlign * sa;
475   DnaProfile * new_dnap;
476   int i;
477   int j;
478 
479 
480   dpmps = DnaProfileMatchPairSet_alloc_std();
481 
482   out = DnaProfileSet_alloc_std();
483 
484   rev = reverse_complement_Sequence(one);
485 
486   dnap = naive_DnaProfile_from_Sequence(one,dpep->seq_id,dpep->m2i,dpep->m2d,dpep->i2i,dpep->d2d);
487   dnapr = naive_DnaProfile_from_Sequence(rev,dpep->seq_id,dpep->m2i,dpep->m2d,dpep->i2i,dpep->d2d);
488 
489   fold_RandomModel_DnaProfile(dnap,dpep->rm);
490   fold_RandomModel_DnaProfile(dnapr,dpep->rm);
491 
492 
493   for(i=0;i<two->len;i++) {
494     fprintf(stderr,"Processing %d\n",i);
495     add_DnaProfileMatchPairSet(dpmps,DnaProfileMatchPair_from_DnaProfile(dnap,two->dnap[i],dpep));
496     add_DnaProfileMatchPairSet(dpmps,DnaProfileMatchPair_from_DnaProfile(dnapr,two->dnap[i],dpep));
497   }
498 
499   fprintf(stderr,"Sorting....\n");
500 
501   sort_DnaProfileMatchPairSet_by_score(dpmps);
502 
503   for(i=0;i<dpmps->len;i++) {
504     /* check this profile has not already been used */
505     /* not done yet */
506 
507     if( dpmps->pair[i]->score < dpep->min_seq_prof ) {
508       fprintf(stderr,"Warning... rejecting match due to score %d vs %d\n",dpmps->pair[i]->score,dpep->min_seq_prof);
509       break;    }
510 
511     sa = merged_SeqAlign(dpmps->pair[i]->query,dpmps->pair[i]->target,dpmps->pair[i]->alb);
512 
513 
514     new_dnap = naive_DnaProfile_from_SeqAlign(sa,dpep->pseudo,dpep->m2i,dpep->m2d,dpep->i2i,dpep->d2d);
515 
516     /* need to log-odds dnap here */
517 
518     fold_RandomModel_DnaProfile(new_dnap,dpep->rm);
519 
520 
521     add_DnaProfileSet(out,new_dnap);
522   }
523 
524 
525   fprintf(stderr,"Freeing DNA profiles...\n");
526 
527   free_DnaProfile(dnap);
528   free_DnaProfile(dnapr);
529 
530   fprintf(stderr,"Freeing sequences\n");
531 
532   free_Sequence(rev);
533 
534 
535 
536   return out;
537 }
538 
539 
540 # line 587 "dnaprofileengine.dy"
DnaProfileMatchPair_from_DnaProfile(DnaProfile * query,DnaProfile * target,DnaProfileEnginePara * dpep)541 DnaProfileMatchPair * DnaProfileMatchPair_from_DnaProfile(DnaProfile * query,DnaProfile * target,DnaProfileEnginePara * dpep)
542 {
543   DnaProfileMatchPair * out;
544   DnaProfileScore * query_s;
545   DnaProfileScore * target_s;
546   DnaProfileMatchScore * match;
547 
548   PackAln * pal;
549 
550 
551   assert(query != NULL);
552   assert(target != NULL);
553   /*
554   assert(query->len > 4 );
555   assert(target->len > 4);
556   */
557 
558   out = DnaProfileMatchPair_alloc();
559   out->query = hard_link_DnaProfile(query);
560   out->target = hard_link_DnaProfile(target);
561 
562 
563   query_s  = DnaProfileScore_from_DnaProfile(query);
564   target_s = DnaProfileScore_from_DnaProfile(target);
565 
566   fprintf(stderr,"Matching %d to %d\n",query->len,target->len);
567 
568   match= new_ALLR_DnaProfileMatchScore(query,target);
569 
570 
571 
572   pal = PackAln_bestmemory_DnaProfileMat(query_s,target_s,match,Probability2Score(dpep->open_unmatched),Probability2Score(dpep->ext_unmatched),Probability2Score(dpep->gap_unmatched),NULL,dpep->dpri);
573 
574   fprintf(stderr,"...Made pal %d\n",pal);
575   out->alb = convert_PackAln_to_AlnBlock_DnaProfileMat(pal);
576   out->score = pal->score;
577 
578   fprintf(stderr,"...freeing pal\n");
579   free_PackAln(pal);
580 
581 
582   fprintf(stderr,"...freeing match\n");
583   free_DnaProfileMatchScore(match);
584   fprintf(stderr,"...freeing query\n");
585   free_DnaProfileScore(query_s);
586   fprintf(stderr,"...freeing target\n");
587   free_DnaProfileScore(target_s);
588 
589 
590   return out;
591 }
592 
593 
594 # line 640 "dnaprofileengine.dy"
sort_DnaProfileMatchPairSet_by_score(DnaProfileMatchPairSet * set)595 void sort_DnaProfileMatchPairSet_by_score(DnaProfileMatchPairSet * set)
596 {
597   sort_DnaProfileMatchPairSet(set,compare_DnaProfileMatchPair);
598 }
599 
600 # line 645 "dnaprofileengine.dy"
compare_DnaProfileMatchPair(DnaProfileMatchPair * one,DnaProfileMatchPair * two)601 int compare_DnaProfileMatchPair(DnaProfileMatchPair * one,DnaProfileMatchPair * two)
602 {
603   return two->score - one->score;
604 }
605 
606 
607 # line 651 "dnaprofileengine.dy"
show_DnaProfileSet(DnaProfileSet * dnaps,RandomModelDNA * rm,FILE * ofp)608 void show_DnaProfileSet(DnaProfileSet * dnaps,RandomModelDNA * rm,FILE * ofp)
609 {
610   int i;
611 
612   for(i=0;i<dnaps->len;i++) {
613     show_DnaProfile(dnaps->dnap[i],rm,ofp);
614   }
615 }
616 
617 
618 # line 617 "dnaprofileengine.c"
619 /* Function:  hard_link_DnaProfileEnginePara(obj)
620  *
621  * Descrip:    Bumps up the reference count of the object
622  *             Meaning that multiple pointers can 'own' it
623  *
624  *
625  * Arg:        obj [UNKN ] Object to be hard linked [DnaProfileEnginePara *]
626  *
627  * Return [UNKN ]  Undocumented return value [DnaProfileEnginePara *]
628  *
629  */
hard_link_DnaProfileEnginePara(DnaProfileEnginePara * obj)630 DnaProfileEnginePara * hard_link_DnaProfileEnginePara(DnaProfileEnginePara * obj)
631 {
632     if( obj == NULL )    {
633       warn("Trying to hard link to a DnaProfileEnginePara object: passed a NULL object");
634       return NULL;
635       }
636     obj->dynamite_hard_link++;
637     return obj;
638 }
639 
640 
641 /* Function:  DnaProfileEnginePara_alloc(void)
642  *
643  * Descrip:    Allocates structure: assigns defaults if given
644  *
645  *
646  *
647  * Return [UNKN ]  Undocumented return value [DnaProfileEnginePara *]
648  *
649  */
DnaProfileEnginePara_alloc(void)650 DnaProfileEnginePara * DnaProfileEnginePara_alloc(void)
651 {
652     DnaProfileEnginePara * out; /* out is exported at end of function */
653 
654 
655     /* call ckalloc and see if NULL */
656     if((out=(DnaProfileEnginePara *) ckalloc (sizeof(DnaProfileEnginePara))) == NULL)    {
657       warn("DnaProfileEnginePara_alloc failed ");
658       return NULL;  /* calling function should respond! */
659       }
660     out->dynamite_hard_link = 1;
661 #ifdef PTHREAD
662     pthread_mutex_init(&(out->dynamite_mutex),NULL);
663 #endif
664     out->dpri = NULL;
665     out->rm = NULL;
666     out->setpara = NULL;
667     out->lchs = NULL;
668     out->pseudo = 0.0;
669     out->open_unmatched = 0.0;
670     out->ext_unmatched = 0.0;
671     out->gap_unmatched = 0.0;
672     out->seq_id = 0.0;
673     out->m2i = 0.0;
674     out->m2d = 0.0;
675     out->i2i = 0.0;
676     out->d2d = 0.0;
677     out->min_seq_prof = 0;
678 
679 
680     return out;
681 }
682 
683 
684 /* Function:  free_DnaProfileEnginePara(obj)
685  *
686  * Descrip:    Free Function: removes the memory held by obj
687  *             Will chain up to owned members and clear all lists
688  *
689  *
690  * Arg:        obj [UNKN ] Object that is free'd [DnaProfileEnginePara *]
691  *
692  * Return [UNKN ]  Undocumented return value [DnaProfileEnginePara *]
693  *
694  */
free_DnaProfileEnginePara(DnaProfileEnginePara * obj)695 DnaProfileEnginePara * free_DnaProfileEnginePara(DnaProfileEnginePara * obj)
696 {
697     int return_early = 0;
698 
699 
700     if( obj == NULL) {
701       warn("Attempting to free a NULL pointer to a DnaProfileEnginePara obj. Should be trappable");
702       return NULL;
703       }
704 
705 
706 #ifdef PTHREAD
707     assert(pthread_mutex_lock(&(obj->dynamite_mutex)) == 0);
708 #endif
709     if( obj->dynamite_hard_link > 1)     {
710       return_early = 1;
711       obj->dynamite_hard_link--;
712       }
713 #ifdef PTHREAD
714     assert(pthread_mutex_unlock(&(obj->dynamite_mutex)) == 0);
715 #endif
716     if( return_early == 1)
717       return NULL;
718     if( obj->dpri != NULL)
719       free_DPRunImpl(obj->dpri);
720     if( obj->rm != NULL)
721       free_RandomModelDNA(obj->rm);
722     if( obj->setpara != NULL)
723       free_LocalCisHitSetPara(obj->setpara);
724     if( obj->lchs != NULL)
725       free_LocalCisHitScore(obj->lchs);
726 
727 
728     ckfree(obj);
729     return NULL;
730 }
731 
732 
733 /* Function:  swap_DnaProfileSet(list,i,j)
734  *
735  * Descrip:    swap function: an internal for qsort_DnaProfileSet
736  *             swaps two positions in the array
737  *
738  *
739  * Arg:        list [UNKN ] List of structures to swap in [DnaProfile **]
740  * Arg:           i [UNKN ] swap position [int]
741  * Arg:           j [UNKN ] swap position [int]
742  *
743  */
744 /* swap function for qsort function */
swap_DnaProfileSet(DnaProfile ** list,int i,int j)745 void swap_DnaProfileSet(DnaProfile ** list,int i,int j)
746 {
747     DnaProfile * temp;
748     temp=list[i];
749     list[i]=list[j];
750     list[j]=temp;
751 }
752 
753 
754 /* Function:  qsort_DnaProfileSet(list,left,right,comp)
755  *
756  * Descrip:    qsort - lifted from K&R
757  *             sorts the array using quicksort
758  *             Probably much better to call sort_DnaProfileSet which sorts from start to end
759  *
760  *
761  * Arg:         list [UNKN ] List of structures to swap in [DnaProfile **]
762  * Arg:         left [UNKN ] left position [int]
763  * Arg:        right [UNKN ] right position [int]
764  * Arg:         comp [FUNCP] Function which returns -1 or 1 to sort on [int (*comp]
765  *
766  */
qsort_DnaProfileSet(DnaProfile ** list,int left,int right,int (* comp)(DnaProfile *,DnaProfile *))767 void qsort_DnaProfileSet(DnaProfile ** list,int left,int right,int (*comp)(DnaProfile * ,DnaProfile * ))
768 {
769     int i,last;
770     if( left >= right )
771       return;
772 
773 
774     swap_DnaProfileSet(list,left,(left+right)/2);
775     last = left;
776     for ( i=left+1; i <= right;i++)  {
777       if( (*comp)(list[i],list[left]) < 0)
778         swap_DnaProfileSet (list,++last,i);
779       }
780     swap_DnaProfileSet (list,left,last);
781     qsort_DnaProfileSet(list,left,last-1,comp);
782     qsort_DnaProfileSet(list,last+1,right,comp);
783 }
784 
785 
786 /* Function:  sort_DnaProfileSet(obj,comp)
787  *
788  * Descrip:    sorts from start to end using comp
789  *             sorts the array using quicksort by calling qsort_DnaProfileSet
790  *
791  *
792  * Arg:         obj [UNKN ] Object containing list [DnaProfileSet *]
793  * Arg:        comp [FUNCP] Function which returns -1 or 1 to sort on [int (*comp]
794  *
795  */
sort_DnaProfileSet(DnaProfileSet * obj,int (* comp)(DnaProfile *,DnaProfile *))796 void sort_DnaProfileSet(DnaProfileSet * obj,int (*comp)(DnaProfile *, DnaProfile *))
797 {
798     qsort_DnaProfileSet(obj->dnap,0,obj->len-1,comp);
799     return;
800 }
801 
802 
803 /* Function:  expand_DnaProfileSet(obj,len)
804  *
805  * Descrip:    Really an internal function for add_DnaProfileSet
806  *
807  *
808  * Arg:        obj [UNKN ] Object which contains the list [DnaProfileSet *]
809  * Arg:        len [UNKN ] Length to add one [int]
810  *
811  * Return [UNKN ]  Undocumented return value [boolean]
812  *
813  */
expand_DnaProfileSet(DnaProfileSet * obj,int len)814 boolean expand_DnaProfileSet(DnaProfileSet * obj,int len)
815 {
816 
817 
818     if( obj->maxlen > obj->len )     {
819       warn("expand_DnaProfileSet called with no need");
820       return TRUE;
821       }
822 
823 
824     if( (obj->dnap = (DnaProfile ** ) ckrealloc (obj->dnap,sizeof(DnaProfile *)*len)) == NULL)   {
825       warn("ckrealloc failed for expand_DnaProfileSet, returning FALSE");
826       return FALSE;
827       }
828     obj->maxlen = len;
829     return TRUE;
830 }
831 
832 
833 /* Function:  add_DnaProfileSet(obj,add)
834  *
835  * Descrip:    Adds another object to the list. It will expand the list if necessary
836  *
837  *
838  * Arg:        obj [UNKN ] Object which contains the list [DnaProfileSet *]
839  * Arg:        add [OWNER] Object to add to the list [DnaProfile *]
840  *
841  * Return [UNKN ]  Undocumented return value [boolean]
842  *
843  */
844 /* will expand function if necessary */
add_DnaProfileSet(DnaProfileSet * obj,DnaProfile * add)845 boolean add_DnaProfileSet(DnaProfileSet * obj,DnaProfile * add)
846 {
847     if( obj->len >= obj->maxlen) {
848       if( expand_DnaProfileSet(obj,obj->len + DnaProfileSetLISTLENGTH) == FALSE)
849         return FALSE;
850       }
851 
852 
853     obj->dnap[obj->len++]=add;
854     return TRUE;
855 }
856 
857 
858 /* Function:  flush_DnaProfileSet(obj)
859  *
860  * Descrip:    Frees the list elements, sets length to 0
861  *             If you want to save some elements, use hard_link_xxx
862  *             to protect them from being actually destroyed in the free
863  *
864  *
865  * Arg:        obj [UNKN ] Object which contains the list  [DnaProfileSet *]
866  *
867  * Return [UNKN ]  Undocumented return value [int]
868  *
869  */
flush_DnaProfileSet(DnaProfileSet * obj)870 int flush_DnaProfileSet(DnaProfileSet * obj)
871 {
872     int i;
873 
874 
875     for(i=0;i<obj->len;i++)  { /*for i over list length*/
876       if( obj->dnap[i] != NULL)  {
877         free_DnaProfile(obj->dnap[i]);
878         obj->dnap[i] = NULL;
879         }
880       } /* end of for i over list length */
881 
882 
883     obj->len = 0;
884     return i;
885 }
886 
887 
888 /* Function:  DnaProfileSet_alloc_std(void)
889  *
890  * Descrip:    Equivalent to DnaProfileSet_alloc_len(DnaProfileSetLISTLENGTH)
891  *
892  *
893  *
894  * Return [UNKN ]  Undocumented return value [DnaProfileSet *]
895  *
896  */
DnaProfileSet_alloc_std(void)897 DnaProfileSet * DnaProfileSet_alloc_std(void)
898 {
899     return DnaProfileSet_alloc_len(DnaProfileSetLISTLENGTH);
900 }
901 
902 
903 /* Function:  DnaProfileSet_alloc_len(len)
904  *
905  * Descrip:    Allocates len length to all lists
906  *
907  *
908  * Arg:        len [UNKN ] Length of lists to allocate [int]
909  *
910  * Return [UNKN ]  Undocumented return value [DnaProfileSet *]
911  *
912  */
DnaProfileSet_alloc_len(int len)913 DnaProfileSet * DnaProfileSet_alloc_len(int len)
914 {
915     DnaProfileSet * out;/* out is exported at the end of function */
916 
917 
918     /* Call alloc function: return NULL if NULL */
919     /* Warning message alread in alloc function */
920     if((out = DnaProfileSet_alloc()) == NULL)
921       return NULL;
922 
923 
924     /* Calling ckcalloc for list elements */
925     if((out->dnap = (DnaProfile ** ) ckcalloc (len,sizeof(DnaProfile *))) == NULL)   {
926       warn("Warning, ckcalloc failed in DnaProfileSet_alloc_len");
927       return NULL;
928       }
929     out->len = 0;
930     out->maxlen = len;
931 
932 
933     return out;
934 }
935 
936 
937 /* Function:  hard_link_DnaProfileSet(obj)
938  *
939  * Descrip:    Bumps up the reference count of the object
940  *             Meaning that multiple pointers can 'own' it
941  *
942  *
943  * Arg:        obj [UNKN ] Object to be hard linked [DnaProfileSet *]
944  *
945  * Return [UNKN ]  Undocumented return value [DnaProfileSet *]
946  *
947  */
hard_link_DnaProfileSet(DnaProfileSet * obj)948 DnaProfileSet * hard_link_DnaProfileSet(DnaProfileSet * obj)
949 {
950     if( obj == NULL )    {
951       warn("Trying to hard link to a DnaProfileSet object: passed a NULL object");
952       return NULL;
953       }
954     obj->dynamite_hard_link++;
955     return obj;
956 }
957 
958 
959 /* Function:  DnaProfileSet_alloc(void)
960  *
961  * Descrip:    Allocates structure: assigns defaults if given
962  *
963  *
964  *
965  * Return [UNKN ]  Undocumented return value [DnaProfileSet *]
966  *
967  */
DnaProfileSet_alloc(void)968 DnaProfileSet * DnaProfileSet_alloc(void)
969 {
970     DnaProfileSet * out;/* out is exported at end of function */
971 
972 
973     /* call ckalloc and see if NULL */
974     if((out=(DnaProfileSet *) ckalloc (sizeof(DnaProfileSet))) == NULL)  {
975       warn("DnaProfileSet_alloc failed ");
976       return NULL;  /* calling function should respond! */
977       }
978     out->dynamite_hard_link = 1;
979 #ifdef PTHREAD
980     pthread_mutex_init(&(out->dynamite_mutex),NULL);
981 #endif
982     out->dnap = NULL;
983     out->len = out->maxlen = 0;
984 
985 
986     return out;
987 }
988 
989 
990 /* Function:  free_DnaProfileSet(obj)
991  *
992  * Descrip:    Free Function: removes the memory held by obj
993  *             Will chain up to owned members and clear all lists
994  *
995  *
996  * Arg:        obj [UNKN ] Object that is free'd [DnaProfileSet *]
997  *
998  * Return [UNKN ]  Undocumented return value [DnaProfileSet *]
999  *
1000  */
free_DnaProfileSet(DnaProfileSet * obj)1001 DnaProfileSet * free_DnaProfileSet(DnaProfileSet * obj)
1002 {
1003     int return_early = 0;
1004     int i;
1005 
1006 
1007     if( obj == NULL) {
1008       warn("Attempting to free a NULL pointer to a DnaProfileSet obj. Should be trappable");
1009       return NULL;
1010       }
1011 
1012 
1013 #ifdef PTHREAD
1014     assert(pthread_mutex_lock(&(obj->dynamite_mutex)) == 0);
1015 #endif
1016     if( obj->dynamite_hard_link > 1)     {
1017       return_early = 1;
1018       obj->dynamite_hard_link--;
1019       }
1020 #ifdef PTHREAD
1021     assert(pthread_mutex_unlock(&(obj->dynamite_mutex)) == 0);
1022 #endif
1023     if( return_early == 1)
1024       return NULL;
1025     if( obj->dnap != NULL)   {
1026       for(i=0;i<obj->len;i++)    {
1027         if( obj->dnap[i] != NULL)
1028           free_DnaProfile(obj->dnap[i]);
1029         }
1030       ckfree(obj->dnap);
1031       }
1032 
1033 
1034     ckfree(obj);
1035     return NULL;
1036 }
1037 
1038 
1039 /* Function:  hard_link_DnaProfileNode(obj)
1040  *
1041  * Descrip:    Bumps up the reference count of the object
1042  *             Meaning that multiple pointers can 'own' it
1043  *
1044  *
1045  * Arg:        obj [UNKN ] Object to be hard linked [DnaProfileNode *]
1046  *
1047  * Return [UNKN ]  Undocumented return value [DnaProfileNode *]
1048  *
1049  */
hard_link_DnaProfileNode(DnaProfileNode * obj)1050 DnaProfileNode * hard_link_DnaProfileNode(DnaProfileNode * obj)
1051 {
1052     if( obj == NULL )    {
1053       warn("Trying to hard link to a DnaProfileNode object: passed a NULL object");
1054       return NULL;
1055       }
1056     obj->dynamite_hard_link++;
1057     return obj;
1058 }
1059 
1060 
1061 /* Function:  DnaProfileNode_alloc(void)
1062  *
1063  * Descrip:    Allocates structure: assigns defaults if given
1064  *
1065  *
1066  *
1067  * Return [UNKN ]  Undocumented return value [DnaProfileNode *]
1068  *
1069  */
DnaProfileNode_alloc(void)1070 DnaProfileNode * DnaProfileNode_alloc(void)
1071 {
1072     DnaProfileNode * out;   /* out is exported at end of function */
1073 
1074 
1075     /* call ckalloc and see if NULL */
1076     if((out=(DnaProfileNode *) ckalloc (sizeof(DnaProfileNode))) == NULL)    {
1077       warn("DnaProfileNode_alloc failed ");
1078       return NULL;  /* calling function should respond! */
1079       }
1080     out->dynamite_hard_link = 1;
1081 #ifdef PTHREAD
1082     pthread_mutex_init(&(out->dynamite_mutex),NULL);
1083 #endif
1084     out->type = 0;
1085     out->leaf = NULL;
1086     out->set = NULL;
1087     out->left = NULL;
1088     out->right = NULL;
1089 
1090 
1091     return out;
1092 }
1093 
1094 
1095 /* Function:  free_DnaProfileNode(obj)
1096  *
1097  * Descrip:    Free Function: removes the memory held by obj
1098  *             Will chain up to owned members and clear all lists
1099  *
1100  *
1101  * Arg:        obj [UNKN ] Object that is free'd [DnaProfileNode *]
1102  *
1103  * Return [UNKN ]  Undocumented return value [DnaProfileNode *]
1104  *
1105  */
free_DnaProfileNode(DnaProfileNode * obj)1106 DnaProfileNode * free_DnaProfileNode(DnaProfileNode * obj)
1107 {
1108     int return_early = 0;
1109 
1110 
1111     if( obj == NULL) {
1112       warn("Attempting to free a NULL pointer to a DnaProfileNode obj. Should be trappable");
1113       return NULL;
1114       }
1115 
1116 
1117 #ifdef PTHREAD
1118     assert(pthread_mutex_lock(&(obj->dynamite_mutex)) == 0);
1119 #endif
1120     if( obj->dynamite_hard_link > 1)     {
1121       return_early = 1;
1122       obj->dynamite_hard_link--;
1123       }
1124 #ifdef PTHREAD
1125     assert(pthread_mutex_unlock(&(obj->dynamite_mutex)) == 0);
1126 #endif
1127     if( return_early == 1)
1128       return NULL;
1129     if( obj->leaf != NULL)
1130       free_Sequence(obj->leaf);
1131     if( obj->set != NULL)
1132       free_DnaProfileSet(obj->set);
1133     if( obj->left != NULL)
1134       free_DnaProfileNode(obj->left);
1135     if( obj->right != NULL)
1136       free_DnaProfileNode(obj->right);
1137     /* obj->parent is linked in */
1138 
1139 
1140     ckfree(obj);
1141     return NULL;
1142 }
1143 
1144 
1145 /* Function:  hard_link_DnaProfileMatchPair(obj)
1146  *
1147  * Descrip:    Bumps up the reference count of the object
1148  *             Meaning that multiple pointers can 'own' it
1149  *
1150  *
1151  * Arg:        obj [UNKN ] Object to be hard linked [DnaProfileMatchPair *]
1152  *
1153  * Return [UNKN ]  Undocumented return value [DnaProfileMatchPair *]
1154  *
1155  */
hard_link_DnaProfileMatchPair(DnaProfileMatchPair * obj)1156 DnaProfileMatchPair * hard_link_DnaProfileMatchPair(DnaProfileMatchPair * obj)
1157 {
1158     if( obj == NULL )    {
1159       warn("Trying to hard link to a DnaProfileMatchPair object: passed a NULL object");
1160       return NULL;
1161       }
1162     obj->dynamite_hard_link++;
1163     return obj;
1164 }
1165 
1166 
1167 /* Function:  DnaProfileMatchPair_alloc(void)
1168  *
1169  * Descrip:    Allocates structure: assigns defaults if given
1170  *
1171  *
1172  *
1173  * Return [UNKN ]  Undocumented return value [DnaProfileMatchPair *]
1174  *
1175  */
DnaProfileMatchPair_alloc(void)1176 DnaProfileMatchPair * DnaProfileMatchPair_alloc(void)
1177 {
1178     DnaProfileMatchPair * out;  /* out is exported at end of function */
1179 
1180 
1181     /* call ckalloc and see if NULL */
1182     if((out=(DnaProfileMatchPair *) ckalloc (sizeof(DnaProfileMatchPair))) == NULL)  {
1183       warn("DnaProfileMatchPair_alloc failed ");
1184       return NULL;  /* calling function should respond! */
1185       }
1186     out->dynamite_hard_link = 1;
1187 #ifdef PTHREAD
1188     pthread_mutex_init(&(out->dynamite_mutex),NULL);
1189 #endif
1190     out->query = NULL;
1191     out->target = NULL;
1192     out->alb = NULL;
1193     out->score = 0;
1194     out->accepted = 0;
1195 
1196 
1197     return out;
1198 }
1199 
1200 
1201 /* Function:  free_DnaProfileMatchPair(obj)
1202  *
1203  * Descrip:    Free Function: removes the memory held by obj
1204  *             Will chain up to owned members and clear all lists
1205  *
1206  *
1207  * Arg:        obj [UNKN ] Object that is free'd [DnaProfileMatchPair *]
1208  *
1209  * Return [UNKN ]  Undocumented return value [DnaProfileMatchPair *]
1210  *
1211  */
free_DnaProfileMatchPair(DnaProfileMatchPair * obj)1212 DnaProfileMatchPair * free_DnaProfileMatchPair(DnaProfileMatchPair * obj)
1213 {
1214     int return_early = 0;
1215 
1216 
1217     if( obj == NULL) {
1218       warn("Attempting to free a NULL pointer to a DnaProfileMatchPair 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->query != NULL)
1236       free_DnaProfile(obj->query);
1237     if( obj->target != NULL)
1238       free_DnaProfile(obj->target);
1239     if( obj->alb != NULL)
1240       free_AlnBlock(obj->alb);
1241 
1242 
1243     ckfree(obj);
1244     return NULL;
1245 }
1246 
1247 
1248 /* Function:  swap_DnaProfileMatchPairSet(list,i,j)
1249  *
1250  * Descrip:    swap function: an internal for qsort_DnaProfileMatchPairSet
1251  *             swaps two positions in the array
1252  *
1253  *
1254  * Arg:        list [UNKN ] List of structures to swap in [DnaProfileMatchPair **]
1255  * Arg:           i [UNKN ] swap position [int]
1256  * Arg:           j [UNKN ] swap position [int]
1257  *
1258  */
1259 /* swap function for qsort function */
swap_DnaProfileMatchPairSet(DnaProfileMatchPair ** list,int i,int j)1260 void swap_DnaProfileMatchPairSet(DnaProfileMatchPair ** list,int i,int j)
1261 {
1262     DnaProfileMatchPair * temp;
1263     temp=list[i];
1264     list[i]=list[j];
1265     list[j]=temp;
1266 }
1267 
1268 
1269 /* Function:  qsort_DnaProfileMatchPairSet(list,left,right,comp)
1270  *
1271  * Descrip:    qsort - lifted from K&R
1272  *             sorts the array using quicksort
1273  *             Probably much better to call sort_DnaProfileMatchPairSet which sorts from start to end
1274  *
1275  *
1276  * Arg:         list [UNKN ] List of structures to swap in [DnaProfileMatchPair **]
1277  * Arg:         left [UNKN ] left position [int]
1278  * Arg:        right [UNKN ] right position [int]
1279  * Arg:         comp [FUNCP] Function which returns -1 or 1 to sort on [int (*comp]
1280  *
1281  */
qsort_DnaProfileMatchPairSet(DnaProfileMatchPair ** list,int left,int right,int (* comp)(DnaProfileMatchPair *,DnaProfileMatchPair *))1282 void qsort_DnaProfileMatchPairSet(DnaProfileMatchPair ** list,int left,int right,int (*comp)(DnaProfileMatchPair * ,DnaProfileMatchPair * ))
1283 {
1284     int i,last;
1285     if( left >= right )
1286       return;
1287 
1288 
1289     swap_DnaProfileMatchPairSet(list,left,(left+right)/2);
1290     last = left;
1291     for ( i=left+1; i <= right;i++)  {
1292       if( (*comp)(list[i],list[left]) < 0)
1293         swap_DnaProfileMatchPairSet (list,++last,i);
1294       }
1295     swap_DnaProfileMatchPairSet (list,left,last);
1296     qsort_DnaProfileMatchPairSet(list,left,last-1,comp);
1297     qsort_DnaProfileMatchPairSet(list,last+1,right,comp);
1298 }
1299 
1300 
1301 /* Function:  sort_DnaProfileMatchPairSet(obj,comp)
1302  *
1303  * Descrip:    sorts from start to end using comp
1304  *             sorts the array using quicksort by calling qsort_DnaProfileMatchPairSet
1305  *
1306  *
1307  * Arg:         obj [UNKN ] Object containing list [DnaProfileMatchPairSet *]
1308  * Arg:        comp [FUNCP] Function which returns -1 or 1 to sort on [int (*comp]
1309  *
1310  */
sort_DnaProfileMatchPairSet(DnaProfileMatchPairSet * obj,int (* comp)(DnaProfileMatchPair *,DnaProfileMatchPair *))1311 void sort_DnaProfileMatchPairSet(DnaProfileMatchPairSet * obj,int (*comp)(DnaProfileMatchPair *, DnaProfileMatchPair *))
1312 {
1313     qsort_DnaProfileMatchPairSet(obj->pair,0,obj->len-1,comp);
1314     return;
1315 }
1316 
1317 
1318 /* Function:  expand_DnaProfileMatchPairSet(obj,len)
1319  *
1320  * Descrip:    Really an internal function for add_DnaProfileMatchPairSet
1321  *
1322  *
1323  * Arg:        obj [UNKN ] Object which contains the list [DnaProfileMatchPairSet *]
1324  * Arg:        len [UNKN ] Length to add one [int]
1325  *
1326  * Return [UNKN ]  Undocumented return value [boolean]
1327  *
1328  */
expand_DnaProfileMatchPairSet(DnaProfileMatchPairSet * obj,int len)1329 boolean expand_DnaProfileMatchPairSet(DnaProfileMatchPairSet * obj,int len)
1330 {
1331 
1332 
1333     if( obj->maxlen > obj->len )     {
1334       warn("expand_DnaProfileMatchPairSet called with no need");
1335       return TRUE;
1336       }
1337 
1338 
1339     if( (obj->pair = (DnaProfileMatchPair ** ) ckrealloc (obj->pair,sizeof(DnaProfileMatchPair *)*len)) == NULL)     {
1340       warn("ckrealloc failed for expand_DnaProfileMatchPairSet, returning FALSE");
1341       return FALSE;
1342       }
1343     obj->maxlen = len;
1344     return TRUE;
1345 }
1346 
1347 
1348 /* Function:  add_DnaProfileMatchPairSet(obj,add)
1349  *
1350  * Descrip:    Adds another object to the list. It will expand the list if necessary
1351  *
1352  *
1353  * Arg:        obj [UNKN ] Object which contains the list [DnaProfileMatchPairSet *]
1354  * Arg:        add [OWNER] Object to add to the list [DnaProfileMatchPair *]
1355  *
1356  * Return [UNKN ]  Undocumented return value [boolean]
1357  *
1358  */
1359 /* will expand function if necessary */
add_DnaProfileMatchPairSet(DnaProfileMatchPairSet * obj,DnaProfileMatchPair * add)1360 boolean add_DnaProfileMatchPairSet(DnaProfileMatchPairSet * obj,DnaProfileMatchPair * add)
1361 {
1362     if( obj->len >= obj->maxlen) {
1363       if( expand_DnaProfileMatchPairSet(obj,obj->len + DnaProfileMatchPairSetLISTLENGTH) == FALSE)
1364         return FALSE;
1365       }
1366 
1367 
1368     obj->pair[obj->len++]=add;
1369     return TRUE;
1370 }
1371 
1372 
1373 /* Function:  flush_DnaProfileMatchPairSet(obj)
1374  *
1375  * Descrip:    Frees the list elements, sets length to 0
1376  *             If you want to save some elements, use hard_link_xxx
1377  *             to protect them from being actually destroyed in the free
1378  *
1379  *
1380  * Arg:        obj [UNKN ] Object which contains the list  [DnaProfileMatchPairSet *]
1381  *
1382  * Return [UNKN ]  Undocumented return value [int]
1383  *
1384  */
flush_DnaProfileMatchPairSet(DnaProfileMatchPairSet * obj)1385 int flush_DnaProfileMatchPairSet(DnaProfileMatchPairSet * obj)
1386 {
1387     int i;
1388 
1389 
1390     for(i=0;i<obj->len;i++)  { /*for i over list length*/
1391       if( obj->pair[i] != NULL)  {
1392         free_DnaProfileMatchPair(obj->pair[i]);
1393         obj->pair[i] = NULL;
1394         }
1395       } /* end of for i over list length */
1396 
1397 
1398     obj->len = 0;
1399     return i;
1400 }
1401 
1402 
1403 /* Function:  DnaProfileMatchPairSet_alloc_std(void)
1404  *
1405  * Descrip:    Equivalent to DnaProfileMatchPairSet_alloc_len(DnaProfileMatchPairSetLISTLENGTH)
1406  *
1407  *
1408  *
1409  * Return [UNKN ]  Undocumented return value [DnaProfileMatchPairSet *]
1410  *
1411  */
DnaProfileMatchPairSet_alloc_std(void)1412 DnaProfileMatchPairSet * DnaProfileMatchPairSet_alloc_std(void)
1413 {
1414     return DnaProfileMatchPairSet_alloc_len(DnaProfileMatchPairSetLISTLENGTH);
1415 }
1416 
1417 
1418 /* Function:  DnaProfileMatchPairSet_alloc_len(len)
1419  *
1420  * Descrip:    Allocates len length to all lists
1421  *
1422  *
1423  * Arg:        len [UNKN ] Length of lists to allocate [int]
1424  *
1425  * Return [UNKN ]  Undocumented return value [DnaProfileMatchPairSet *]
1426  *
1427  */
DnaProfileMatchPairSet_alloc_len(int len)1428 DnaProfileMatchPairSet * DnaProfileMatchPairSet_alloc_len(int len)
1429 {
1430     DnaProfileMatchPairSet * out;   /* out is exported at the end of function */
1431 
1432 
1433     /* Call alloc function: return NULL if NULL */
1434     /* Warning message alread in alloc function */
1435     if((out = DnaProfileMatchPairSet_alloc()) == NULL)
1436       return NULL;
1437 
1438 
1439     /* Calling ckcalloc for list elements */
1440     if((out->pair = (DnaProfileMatchPair ** ) ckcalloc (len,sizeof(DnaProfileMatchPair *))) == NULL) {
1441       warn("Warning, ckcalloc failed in DnaProfileMatchPairSet_alloc_len");
1442       return NULL;
1443       }
1444     out->len = 0;
1445     out->maxlen = len;
1446 
1447 
1448     return out;
1449 }
1450 
1451 
1452 /* Function:  hard_link_DnaProfileMatchPairSet(obj)
1453  *
1454  * Descrip:    Bumps up the reference count of the object
1455  *             Meaning that multiple pointers can 'own' it
1456  *
1457  *
1458  * Arg:        obj [UNKN ] Object to be hard linked [DnaProfileMatchPairSet *]
1459  *
1460  * Return [UNKN ]  Undocumented return value [DnaProfileMatchPairSet *]
1461  *
1462  */
hard_link_DnaProfileMatchPairSet(DnaProfileMatchPairSet * obj)1463 DnaProfileMatchPairSet * hard_link_DnaProfileMatchPairSet(DnaProfileMatchPairSet * obj)
1464 {
1465     if( obj == NULL )    {
1466       warn("Trying to hard link to a DnaProfileMatchPairSet object: passed a NULL object");
1467       return NULL;
1468       }
1469     obj->dynamite_hard_link++;
1470     return obj;
1471 }
1472 
1473 
1474 /* Function:  DnaProfileMatchPairSet_alloc(void)
1475  *
1476  * Descrip:    Allocates structure: assigns defaults if given
1477  *
1478  *
1479  *
1480  * Return [UNKN ]  Undocumented return value [DnaProfileMatchPairSet *]
1481  *
1482  */
DnaProfileMatchPairSet_alloc(void)1483 DnaProfileMatchPairSet * DnaProfileMatchPairSet_alloc(void)
1484 {
1485     DnaProfileMatchPairSet * out;   /* out is exported at end of function */
1486 
1487 
1488     /* call ckalloc and see if NULL */
1489     if((out=(DnaProfileMatchPairSet *) ckalloc (sizeof(DnaProfileMatchPairSet))) == NULL)    {
1490       warn("DnaProfileMatchPairSet_alloc failed ");
1491       return NULL;  /* calling function should respond! */
1492       }
1493     out->dynamite_hard_link = 1;
1494 #ifdef PTHREAD
1495     pthread_mutex_init(&(out->dynamite_mutex),NULL);
1496 #endif
1497     out->pair = NULL;
1498     out->len = out->maxlen = 0;
1499 
1500 
1501     return out;
1502 }
1503 
1504 
1505 /* Function:  free_DnaProfileMatchPairSet(obj)
1506  *
1507  * Descrip:    Free Function: removes the memory held by obj
1508  *             Will chain up to owned members and clear all lists
1509  *
1510  *
1511  * Arg:        obj [UNKN ] Object that is free'd [DnaProfileMatchPairSet *]
1512  *
1513  * Return [UNKN ]  Undocumented return value [DnaProfileMatchPairSet *]
1514  *
1515  */
free_DnaProfileMatchPairSet(DnaProfileMatchPairSet * obj)1516 DnaProfileMatchPairSet * free_DnaProfileMatchPairSet(DnaProfileMatchPairSet * obj)
1517 {
1518     int return_early = 0;
1519     int i;
1520 
1521 
1522     if( obj == NULL) {
1523       warn("Attempting to free a NULL pointer to a DnaProfileMatchPairSet obj. Should be trappable");
1524       return NULL;
1525       }
1526 
1527 
1528 #ifdef PTHREAD
1529     assert(pthread_mutex_lock(&(obj->dynamite_mutex)) == 0);
1530 #endif
1531     if( obj->dynamite_hard_link > 1)     {
1532       return_early = 1;
1533       obj->dynamite_hard_link--;
1534       }
1535 #ifdef PTHREAD
1536     assert(pthread_mutex_unlock(&(obj->dynamite_mutex)) == 0);
1537 #endif
1538     if( return_early == 1)
1539       return NULL;
1540     if( obj->pair != NULL)   {
1541       for(i=0;i<obj->len;i++)    {
1542         if( obj->pair[i] != NULL)
1543           free_DnaProfileMatchPair(obj->pair[i]);
1544         }
1545       ckfree(obj->pair);
1546       }
1547 
1548 
1549     ckfree(obj);
1550     return NULL;
1551 }
1552 
1553 
1554 
1555 #ifdef _cplusplus
1556 }
1557 #endif
1558