Debian Codegraph @

src/cm_tophits.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* CM_TOPHITS: implementation of ranked list of top-scoring hits based
2 * closely on HMMER3's P7_TOPHITS but with some complexity removed
3 * since we don't have to worry about domains nor with iterative
4 * searches (yet) with CM hits, and some extra complexity for bridging
5 * the gap with older versions of Infernal.
6 *
7 * Contents:
8 * 1. The CM_TOPHITS object.
9 * 2. Standard (human-readable) output of pipeline results.
10 * 3. Tabular (parsable) output of pipeline results.
11 * 4. Debugging/dev code.
12 * 5. Benchmark driver.
13 * 6. Test driver.
14 *
15 * EPN, Tue May 24 13:03:31 2011
16 */
17 #include <esl_config.h>
18 #include <p7_config.h>
19 #include "config.h"
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <limits.h>
24
25 #include "easel.h"
26
27 #include "hmmer.h"
28
29 #include "infernal.h"
30
31 static int remove_or_mark_overlaps_one_seq_fast (CM_TOPHITS *th, int64_t idx1, int64_t idx2, int do_remove, char *errbuf);
32 static int remove_or_mark_overlaps_one_seq_memeff(CM_TOPHITS *th, int64_t idx1, int64_t idx2, int do_remove, char *errbuf);
33
34 /*****************************************************************
35 * 1. The CM_TOPHITS object
36 *****************************************************************/
37
38 /* Function: cm_tophits_Create()
39 * Synopsis: Allocate a hit list.
40 * Incept: EPN, Tue May 24 13:11:06 2011
41 * SRE, Fri Dec 28 07:17:51 2007 [Janelia] (p7_tophits.c)
42 *
43 * Purpose: Allocates a new <CM_TOPHITS> hit list and return a pointer
44 * to it.
45 *
46 * Throws: <NULL> on allocation failure.
47 */
48 CM_TOPHITS *
49 cm_tophits_Create(void)
50 {
51 CM_TOPHITS *h = NULL;
52 int default_nalloc = 256;
53 int status;
54
55 ESL_ALLOC(h, sizeof(CM_TOPHITS));
56 h->hit = NULL;
57 h->unsrt = NULL;
58
59 ESL_ALLOC(h->hit, sizeof(CM_HIT *) * default_nalloc);
60 ESL_ALLOC(h->unsrt, sizeof(CM_HIT) * default_nalloc);
61 h->Nalloc = default_nalloc;
62 h->N = 0;
63 h->nreported = 0;
64 h->nincluded = 0;
65 h->is_sorted_by_evalue = TRUE; /* but only because there's 0 hits */
66 h->is_sorted_for_overlap_removal = FALSE; /* actually this is true with 0 hits, but for safety,
67 * we don't want multiple sorted_* fields as TRUE */
68 h->is_sorted_by_position = FALSE; /* ditto */
69 h->hit[0] = h->unsrt; /* if you're going to call it "sorted" when it contains just one hit, you need this */
70 return h;
71
72 ERROR:
73 cm_tophits_Destroy(h);
74 return NULL;
75 }
76
77 /* Function: cm_tophits_Grow()
78 * Synopsis: Reallocates a larger hit list, if needed.
79 * Incept: EPN, Tue May 24 13:13:52 2011
80 * SRE, Fri Dec 28 07:37:27 2007 [Janelia] (p7_tophits.c)
81 *
82 * Purpose: If list <h> cannot hold another hit, doubles
83 * the internal allocation.
84 *
85 * Returns: <eslOK> on success.
86 *
87 * Throws: <eslEMEM> on allocation failure; in this case,
88 * the data in <h> are unchanged.
89 */
90 int
91 cm_tophits_Grow(CM_TOPHITS *h)
92 {
93 void *p;
94 CM_HIT *ori = h->unsrt;
95 int Nalloc = h->Nalloc * 2; /* grow by doubling */
96 int i;
97 int status;
98
99 if (h->N < h->Nalloc) return eslOK; /* we have enough room for another hit */
100
101 ESL_RALLOC(h->hit, p, sizeof(CM_HIT *) * Nalloc);
102 ESL_RALLOC(h->unsrt, p, sizeof(CM_HIT) * Nalloc);
103
104 /* If we grow a sorted list, we have to translate the pointers
105 * in h->hit, because h->unsrt might have just moved in memory.
106 */
107 if (h->is_sorted_by_evalue || h->is_sorted_for_overlap_removal || h->is_sorted_by_position) {
108 for (i = 0; i < h->N; i++)
109 h->hit[i] = h->unsrt + (h->hit[i] - ori);
110 }
111
112 h->Nalloc = Nalloc;
113 return eslOK;
114
115 ERROR:
116 return eslEMEM;
117 }
118
119 /* Function: cm_tophits_CreateNextHit()
120 * Synopsis: Get pointer to new structure for recording a hit.
121 * Incept: EPN, Tue May 24 13:14:12 2011
122 * SRE, Tue Mar 11 08:44:53 2008 [Janelia] (p7_tophits.c)
123 *
124 * Purpose: Ask the top hits object <h> to do any necessary
125 * internal allocation and bookkeeping to add a new,
126 * empty hit to its list; return a pointer to
127 * this new <CM_HIT> structure for data to be filled
128 * in by the caller.
129 *
130 * Returns: <eslOK> on success.
131 *
132 * Throws: <eslEMEM> on allocation error.
133 */
134 int
135 cm_tophits_CreateNextHit(CM_TOPHITS *h, CM_HIT **ret_hit)
136 {
137 CM_HIT *hit = NULL;
138 int status;
139
140 if ((status = cm_tophits_Grow(h)) != eslOK) goto ERROR;
141
142 hit = &(h->unsrt[h->N]);
143 h->N++;
144 if (h->N >= 2) {
145 h->is_sorted_by_evalue = FALSE;
146 h->is_sorted_for_overlap_removal = FALSE;
147 h->is_sorted_for_overlap_markup = FALSE;
148 h->is_sorted_by_position = FALSE;
149 }
150
151 hit->name = NULL;
152 hit->acc = NULL;
153 hit->desc = NULL;
154
155 hit->start = 1;
156 hit->stop = 1;
157 hit->in_rc = FALSE;
158 hit->score = 0.0;
159 hit->bias = 0.0;
160 hit->pvalue = 0.0;
161 hit->evalue = 0.0;
162 hit->has_evalue = FALSE;
163
164 hit->cm_idx = -1;
165 hit->clan_idx = -1;
166 hit->seq_idx = -1;
167 hit->pass_idx = -1;
168 hit->hit_idx = h->N-1;
169
170 hit->srcL = -1;
171 hit->hmmonly = FALSE;
172 hit->glocal = FALSE;
173
174 hit->ad = NULL;
175 hit->flags = CM_HIT_FLAGS_DEFAULT;
176
177 hit->any_oidx = -1;
178 hit->win_oidx = -1;
179 hit->any_bitE = 0.0;
180 hit->win_bitE = 0.0;
181
182 *ret_hit = hit;
183 return eslOK;
184
185 ERROR:
186 *ret_hit = NULL;
187 return status;
188 }
189
190 /* hit_sorter_by_evalue(), hit_sorter_for_overlap_removal, hit_sorter_for_overlap_markup_clans_only,
191 * hit_sorter_for_overlap_markup_clans_agnostic and hit_sorter_by_position: qsort's pawns, below */
192 static int
193 hit_sorter_by_evalue(const void *vh1, const void *vh2)
194 {
195 CM_HIT *h1 = *((CM_HIT **) vh1); /* don't ask. don't change. Don't Panic. */
196 CM_HIT *h2 = *((CM_HIT **) vh2);
197
198 if (h1->evalue > h2->evalue) return 1; /* first key, E-value, low to high */
199 else if (h1->evalue < h2->evalue) return -1;
200 else {
201 if (h1->score < h2->score) return 1; /* second key, bit score, high to low */
202 else if (h1->score > h2->score) return -1;
203 else {
204 if (h1->seq_idx > h2->seq_idx) return 1; /* second key, seq_idx (unique id for sequences), low to high */
205 else if (h1->seq_idx < h2->seq_idx) return -1;
206 else {
207 if (h1->start > h2->start) return 1; /* third key, start position, low to high */
208 else if (h1->start < h2->start) return -1;
209 else {
210 if (h1->pass_idx < h2->pass_idx) return 1; /* fourth key, pass_idx, high to low */
211 else if(h1->pass_idx > h2->pass_idx) return -1;
212 else return 0;
213 }
214 }
215 }
216 }
217 }
218
219 static int
220 hit_sorter_for_overlap_removal(const void *vh1, const void *vh2)
221 {
222 CM_HIT *h1 = *((CM_HIT **) vh1); /* don't ask. don't change. Don't Panic. */
223 CM_HIT *h2 = *((CM_HIT **) vh2);
224
225 if (h1->cm_idx > h2->cm_idx) return 1; /* first key, cm_idx (unique id for models), low to high */
226 else if (h1->cm_idx < h2->cm_idx) return -1;
227 else {
228 if (h1->seq_idx > h2->seq_idx) return 1; /* second key, seq_idx (unique id for sequences), low to high */
229 else if (h1->seq_idx < h2->seq_idx) return -1;
230 else {
231 /* same model and same sequence; sort by strand, then score, then start position */
232 if (h1->in_rc > h2->in_rc) return 1; /* third key, strand (h1->in_rc = 1, h1->in_rc = 0), forward, then reverse */
233 else if(h1->in_rc < h2->in_rc) return -1; /* (h1->in_rc = 0, h2->in_rc = 1), forward, then reverse */
234 else {
235 if (h1->score < h2->score) return 1; /* fourth key is bit score, high to low */
236 else if(h1->score > h2->score) return -1;
237 else {
238 if (h1->start > h2->start) return 1; /* fifth key is start position, low to high (irregardless of in_rc value) */
239 else if(h1->start < h2->start) return -1;
240 else return 0;
241 }
242 }
243 }
244 }
245 }
246
247 static int
248 hit_sorter_for_overlap_markup_clans_only(const void *vh1, const void *vh2)
249 {
250 CM_HIT *h1 = *((CM_HIT **) vh1); /* don't ask. don't change. Don't Panic. */
251 CM_HIT *h2 = *((CM_HIT **) vh2);
252
253 if (h1->seq_idx > h2->seq_idx) return 1; /* first key, seq_idx (unique id for sequences), low to high */
254 else if (h1->seq_idx < h2->seq_idx) return -1;
255 else {
256 /* same sequence; sort by strand, then clan_idx, then E-value, then score, then start position, then CM index */
257 if (h1->in_rc > h2->in_rc) return 1; /* second key, strand (h1->in_rc = 1, h1->in_rc = 0), forward, then reverse */
258 else if(h1->in_rc < h2->in_rc) return -1; /* (h1->in_rc = 0, h2->in_rc = 1), forward, then reverse */
259 else {
260 if (h1->clan_idx > h2->clan_idx) return 1; /* third key is clan_idx, low to high */
261 else if(h1->clan_idx < h2->clan_idx) return -1;
262 else {
263 if (h1->evalue > h2->evalue) return 1; /* fourth key is E-value, low to high */
264 else if(h1->evalue < h2->evalue) return -1;
265 else {
266 if (h1->score < h2->score) return 1; /* fifth key is bit score, high to low */
267 else if(h1->score > h2->score) return -1;
268 else {
269 if (h1->start > h2->start) return 1; /* sixth key is start position, low to high (irregardless of in_rc value) */
270 else if(h1->start < h2->start) return -1;
271 else {
272 if (h1->cm_idx > h2->cm_idx) return 1; /* seventh key is cm_idx (unique id for models), low to high */
273 else if(h1->cm_idx < h2->cm_idx) return -1;
274 else return 0;
275 }
276 }
277 }
278 }
279 }
280 }
281 }
282
283 static int
284 hit_sorter_for_overlap_markup_clans_agnostic(const void *vh1, const void *vh2)
285 {
286 CM_HIT *h1 = *((CM_HIT **) vh1); /* don't ask. don't change. Don't Panic. */
287 CM_HIT *h2 = *((CM_HIT **) vh2);
288
289 if (h1->seq_idx > h2->seq_idx) return 1; /* first key, seq_idx (unique id for sequences), low to high */
290 else if (h1->seq_idx < h2->seq_idx) return -1;
291 else {
292 /* same sequence; sort by strand, then E-value, then score, then start position, then CM index */
293 if (h1->in_rc > h2->in_rc) return 1; /* second key, strand (h1->in_rc = 1, h1->in_rc = 0), forward, then reverse */
294 else if(h1->in_rc < h2->in_rc) return -1; /* (h1->in_rc = 0, h2->in_rc = 1), forward, then reverse */
295 else {
296 if (h1->evalue > h2->evalue) return 1; /* fourth key is E-value, low to high */
297 else if(h1->evalue < h2->evalue) return -1;
298 else {
299 if (h1->score < h2->score) return 1; /* fifth key is bit score, high to low */
300 else if(h1->score > h2->score) return -1;
301 else {
302 if (h1->start > h2->start) return 1; /* sixth key is start position, low to high (irregardless of in_rc value) */
303 else if(h1->start < h2->start) return -1;
304 else {
305 if (h1->cm_idx > h2->cm_idx) return 1; /* seventh key is cm_idx (unique id for models), low to high */
306 else if(h1->cm_idx < h2->cm_idx) return -1;
307 else return 0;
308 }
309 }
310 }
311 }
312 }
313 }
314
315 static int
316 hit_sorter_by_position(const void *vh1, const void *vh2)
317 {
318 CM_HIT *h1 = *((CM_HIT **) vh1); /* don't ask. don't change. Don't Panic. */
319 CM_HIT *h2 = *((CM_HIT **) vh2);
320
321 if (h1->cm_idx > h2->cm_idx) return 1; /* first key, cm_idx (unique id for models), low to high */
322 else if (h1->cm_idx < h2->cm_idx) return -1; /* first key, cm_idx (unique id for models), low to high */
323 else {
324 if (h1->seq_idx > h2->seq_idx) return 1; /* second key, seq_idx (unique id for sequences), low to high */
325 else if (h1->seq_idx < h2->seq_idx) return -1;
326 else {
327 /* same sequence, sort by strand, stop position then start position (if revcomp) or start position then stop position (if !revcomp) */
328 if (h1->in_rc > h2->in_rc) return 1; /* third key, strand (h1->in_rc = 1, h1->in_rc = 0), forward, then reverse */
329 else if(h1->in_rc < h2->in_rc) return -1; /* (h1->in_rc = 0, h2->in_rc = 1), forward, then reverse */
330 else if(h1->in_rc) {
331 if (h1->stop > h2->stop) return 1; /* both revcomp: fourth key is stop position, low to high */
332 else if(h1->stop < h2->stop) return -1;
333 else {
334 if (h1->start > h2->start) return 1; /* both revcomp, same stop position, fifth key is start position, low to high */
335 else if(h1->start < h2->start) return -1;
336 else return 0;
337 }
338 }
339 else {
340 if (h1->start > h2->start) return 1; /* both !revcomp: fourth key is start position, low to high */
341 else if(h1->start < h2->start) return -1;
342 else {
343 if (h1->stop > h2->stop) return 1; /* both !revcomp, same start position, fifth key is stop position, low to high */
344 else if(h1->stop < h2->stop) return -1;
345 else return 0;
346 }
347 }
348 }
349 }
350 }
351
352 /* Function: cm_tophits_SortByEvalue()
353 * Synopsis: Sorts a hit list by E-value.
354 * Incept: EPN, Tue May 24 13:30:23 2011
355 * SRE, Fri Dec 28 07:51:56 2007 (p7_tophits_Sort())
356 *
357 * Purpose: Sorts a top hit list by E-value. After this call,
358 * <h->hit[i]> points to the i'th ranked <CM_HIT> for all
359 * <h->N> hits. First sort key is E-value (low to high),
360 * second is score (high to low), third is seq_idx
361 * (low to high), fourth is start position (low to high).
362
363 * Returns: <eslOK> on success.
364 */
365 int
366 cm_tophits_SortByEvalue(CM_TOPHITS *h)
367 {
368 int i;
369
370 if (h->is_sorted_by_evalue) {
371 h->is_sorted_for_overlap_removal = FALSE;
372 h->is_sorted_for_overlap_markup = FALSE;
373 h->is_sorted_by_position = FALSE;
374 return eslOK;
375 }
376 /* initialize hit ptrs, this also unsorts if already sorted by seq_idx */
377 for (i = 0; i < h->N; i++) h->hit[i] = h->unsrt + i;
378 if (h->N > 1) qsort(h->hit, h->N, sizeof(CM_HIT *), hit_sorter_by_evalue);
379 h->is_sorted_for_overlap_removal = FALSE;
380 h->is_sorted_for_overlap_markup = FALSE;
381 h->is_sorted_by_position = FALSE;
382 h->is_sorted_by_evalue = TRUE;
383 return eslOK;
384 }
385
386 /* Function: cm_tophits_SortForOverlapRemoval()
387 * Synopsis: Sorts a hit list by cm index, sequence index, strand, then score.
388 * Incept: EPN, Tue Dec 20 09:17:47 2011
389 *
390 * Purpose: Sorts a top hit list to ease removal of duplicates.
391 * After this call, <h->hit[i]> points to the i'th ranked
392 * <CM_HIT> for all <h->N> hits. First sort key is cm_idx
393 * (low to high), second is seq_idx (low to high) position
394 * (low to high), third is strand (forward then reverse),
395 * fourth key is score.
396 *
397 * Returns: <eslOK> on success.
398 */
399 int
400 cm_tophits_SortForOverlapRemoval(CM_TOPHITS *h)
401 {
402 int i;
403
404 if (h->is_sorted_for_overlap_removal) {
405 h->is_sorted_by_evalue = FALSE;
406 h->is_sorted_by_position = FALSE;
407 h->is_sorted_for_overlap_markup = FALSE;
408 return eslOK;
409 }
410 /* initialize hit ptrs, this also unsorts if already sorted by score */
411 for (i = 0; i < h->N; i++) h->hit[i] = h->unsrt + i;
412 if (h->N > 1) qsort(h->hit, h->N, sizeof(CM_HIT *), hit_sorter_for_overlap_removal);
413 h->is_sorted_by_evalue = FALSE;
414 h->is_sorted_by_position = FALSE;
415 h->is_sorted_for_overlap_markup = FALSE;
416 h->is_sorted_for_overlap_removal = TRUE;
417
418 return eslOK;
419 }
420
421 /* Function: cm_tophits_SortForOverlapMarkup()
422 * Synopsis: Sorts a hit list by sequence index, strand, score, then model.
423 * Incept: EPN, Tue Dec 20 09:17:47 2011
424 *
425 * Purpose: Sorts a top hit list to ease markup of overlaps that
426 * overlap on the same strand of the same sequence by
427 * different models.
428 *
429 * After this call, <h->hit[i]> points to the i'th ranked
430 * <CM_HIT> for all <h->N> hits. First sort key is seq_idx
431 * (low to high), second is strand (forward then reverse),
432 * third key is E-value (low to high), fourth is score (high
433 * to low), fifth is start position (low to high) and
434 * sixth key is CM idx (unique id for models, low to high).
435 *
436 * Args: do_clans_only - TRUE: we are only marking overlaps within clans
437 * FALSE: we are marking all overlaps
438 * This has implications on how we sort.
439 *
440 * Returns: <eslOK> on success.
441 */
442 int
443 cm_tophits_SortForOverlapMarkup(CM_TOPHITS *h, int do_clans_only)
444 {
445 int i;
446
447 if (h->is_sorted_for_overlap_markup) {
448 h->is_sorted_by_evalue = FALSE;
449 h->is_sorted_by_position = FALSE;
450 h->is_sorted_for_overlap_removal = FALSE;
451 return eslOK;
452 }
453 /* initialize hit ptrs, this also unsorts if already sorted by score */
454 for (i = 0; i < h->N; i++) h->hit[i] = h->unsrt + i;
455 if (h->N > 1) {
456 if(do_clans_only) {
457 qsort(h->hit, h->N, sizeof(CM_HIT *), hit_sorter_for_overlap_markup_clans_only);
458 }
459 else {
460 qsort(h->hit, h->N, sizeof(CM_HIT *), hit_sorter_for_overlap_markup_clans_agnostic);
461 }
462 }
463 h->is_sorted_by_evalue = FALSE;
464 h->is_sorted_by_position = FALSE;
465 h->is_sorted_for_overlap_removal = FALSE;
466 h->is_sorted_for_overlap_markup = TRUE;
467
468 return eslOK;
469 }
470
471 /* Function: cm_tophits_SortByPosition()
472 * Synopsis: Sorts a hit list by cm index, sequence index, strand, position, then bit score.
473 * Incept: EPN, Tue Dec 20 09:17:47 2011
474 *
475 * Purpose: Sorts a top hit list to ease merging of nearby hits after
476 * padding start and stop (like we do at the end of
477 * cm_pli_SeqCYKFilter()).
478 * After this call, <h->hit[i]> points to the i'th ranked
479 * <CM_HIT> for all <h->N> hits. First sort key is cm_idx
480 * (low to high), second is seq_idx (low to high) position
481 * (low to high), third is strand (forward then reverse),
482 * fourth key is start (if ! in_rc) or stop (if in_rc)
483 * (low to high for both strands).
484 *
485 * Returns: <eslOK> on success.
486 */
487 int
488 cm_tophits_SortByPosition(CM_TOPHITS *h)
489 {
490 int i;
491
492 if (h->is_sorted_by_position) {
493 h->is_sorted_by_evalue = FALSE;
494 h->is_sorted_for_overlap_removal = FALSE;
495 h->is_sorted_for_overlap_markup = FALSE;
496 return eslOK;
497 }
498 /* initialize hit ptrs, this also unsorts if already sorted by score */
499 for (i = 0; i < h->N; i++) h->hit[i] = h->unsrt + i;
500 if (h->N > 1) qsort(h->hit, h->N, sizeof(CM_HIT *), hit_sorter_by_position);
501 h->is_sorted_by_evalue = FALSE;
502 h->is_sorted_for_overlap_removal = FALSE;
503 h->is_sorted_for_overlap_markup = FALSE;
504 h->is_sorted_by_position = TRUE;
505
506 return eslOK;
507 }
508
509 /* Function: cm_tophits_Merge()
510 * Synopsis: Merge two top hits lists.
511 * Incept: EPN, Tue May 24 13:30:39 2011
512 * SRE, Fri Dec 28 09:32:12 2007 [Janelia] (p7_tophits.c)
513 *
514 * Purpose: Merge <h2> into <h1>. Upon return, <h1>
515 * contains the unsorted, merged list. <h2>
516 * is effectively destroyed; caller should
517 * not access it further, and may as well free
518 * it immediately.
519 *
520 * Returns: <eslOK> on success.
521 *
522 * Throws: <eslEMEM> on allocation failure, and
523 * both <h1> and <h2> remain valid.
524 */
525 int
526 cm_tophits_Merge(CM_TOPHITS *h1, CM_TOPHITS *h2)
527 {
528 void *p;
529 CM_HIT *new2;
530 int i;
531 int Nalloc = h1->Nalloc + h2->Nalloc;
532 int status;
533
534 /* Attempt our allocations, so we fail early if we fail.
535 * Reallocating h1->unsrt screws up h1->hit, so fix it.
536 */
537 ESL_RALLOC(h1->unsrt, p, sizeof(CM_HIT) * Nalloc);
538 ESL_RALLOC(h1->hit, p, sizeof(CM_HIT *) * Nalloc);
539
540 /* Update h2's data that relates to hit indices */
541 for (i = 0; i < h2->N; i++)
542 {
543 if(h2->unsrt[i].hit_idx != -1) { h2->unsrt[i].hit_idx += h1->N; }
544 if(h2->unsrt[i].any_oidx != -1) { h2->unsrt[i].any_oidx += h1->N; }
545 if(h2->unsrt[i].win_oidx != -1) { h2->unsrt[i].win_oidx += h1->N; }
546 }
547
548 /* Append h2's unsorted data array to h1. h2's data begin at <new2> */
549 new2 = h1->unsrt + h1->N;
550 memcpy(new2, h2->unsrt, sizeof(CM_HIT) * h2->N);
551
552 /* h2 now turns over management of name, acc, desc memory to h1;
553 * nullify its pointers, to prevent double free
554 */
555 for (i = 0; i < h2->N; i++)
556 {
557 h2->unsrt[i].name = NULL;
558 h2->unsrt[i].acc = NULL;
559 h2->unsrt[i].desc = NULL;
560 h2->unsrt[i].ad = NULL;
561 }
562
563 /* Construct the new grown h1 */
564 h1->Nalloc = Nalloc;
565 h1->N += h2->N;
566 h1->is_sorted_by_evalue = FALSE;
567 h1->is_sorted_for_overlap_removal = FALSE;
568 h1->is_sorted_by_position = FALSE;
569
570 /* reset pointers in sorted list (not really nec because we're not sorted) */
571 for (i = 0; i < h1->N; i++) h1->hit[i] = h1->unsrt + i;
572
573 return eslOK;
574
575 ERROR:
576 return status;
577 }
578
579 static int
580 integer_textwidth(long n)
581 {
582 int w = (n < 0)? 1 : 0;
583 while (n != 0) { n /= 10; w++; }
584 return w;
585 }
586
587 /* Function: cm_tophits_GetMaxPositionLength()
588 * Synopsis: Returns maximum position length in hit list (targets).
589 * Incept: EPN, Tue May 24 13:31:13 2011
590 * TJW, Mon May 24 14:16:16 EDT 2010 [Janelia] (p7_tophits.c)
591 *
592 * Purpose: Returns the length of the longest hit location (start/end)
593 * of all the registered hits, in chars. This is useful when
594 * deciding how to format output.
595 *
596 * The maximum is taken over all registered hits. This
597 * opens a possible side effect: caller might print only
598 * the top hits, and the max name length in these top hits
599 * may be different than the max length over all the hits.
600 *
601 * If there are no hits in <h>, or none of the
602 * hits have names, returns 0.
603 */
604 int
605 cm_tophits_GetMaxPositionLength(CM_TOPHITS *h)
606 {
607 int i, max;
608
609 max = 0;
610 for (i = 0; i < h->N; i++) {
611 max = ESL_MAX(max,
612 ESL_MAX(integer_textwidth(h->unsrt[i].start), integer_textwidth(h->unsrt[i].stop)));
613 }
614 return max;
615 }
616
617 /* Function: cm_tophits_GetMaxTargetLength()
618 * Synopsis: Returns maximum target length in hit list.
619 * Incept: EPN, Wed Oct 22 10:36:11 2014
620 *
621 * Purpose: Returns the length of the longest target length (srcL)
622 * of all the registered hits, in chars. This is useful when
623 * deciding how to format output.
624 */
625 int
626 cm_tophits_GetMaxTargetLength(CM_TOPHITS *h)
627 {
628 int i, max;
629
630 max = 0;
631 for (i = 0; i < h->N; i++) {
632 max = ESL_MAX(max, integer_textwidth(h->unsrt[i].srcL));
633 }
634 return max;
635 }
636
637 /* Function: cm_tophits_GetMaxNameLength()
638 * Synopsis: Returns maximum name length in hit list (targets).
639 * Incept: EPN, Tue May 24 13:34:12 2011
640 * SRE, Fri Dec 28 09:00:13 2007 [Janelia] (p7_tophits.c)
641 *
642 * Purpose: Returns the maximum name length of all the registered
643 * hits, in chars. This is useful when deciding how to
644 * format output.
645 *
646 * The maximum is taken over all registered hits. This
647 * opens a possible side effect: caller might print only
648 * the top hits, and the max name length in these top hits
649 * may be different than the max length over all the hits.
650 *
651 * If there are no hits in <h>, or none of the
652 * hits have names, returns 0.
653 */
654 int
655 cm_tophits_GetMaxNameLength(CM_TOPHITS *h)
656 {
657 int i, max;
658 for (max = 0, i = 0; i < h->N; i++)
659 if (h->unsrt[i].name != NULL) {
660 max = ESL_MAX(max, strlen(h->unsrt[i].name));
661 }
662 return max;
663 }
664
665 /* Function: cm_tophits_GetMaxDescLength()
666 * Synopsis: Returns maximum length of the desc field in hit list.
667 * Incept: EPN, Wed Oct 22 10:36:11 2014
668 *
669 * Purpose: Returns the length of the longest description (desc)
670 * of all the registered hits, in chars. This is useful when
671 * deciding how to format output.
672 */
673 int
674 cm_tophits_GetMaxDescLength(CM_TOPHITS *h)
675 {
676 int i, max;
677
678 max = 0;
679 for (i = 0; i < h->N; i++) {
680 if(h->unsrt[i].desc != NULL) {
681 max = ESL_MAX(max, strlen(h->unsrt[i].desc));
682 }
683 }
684 return max;
685 }
686
687 /* Function: cm_tophits_GetMaxAccessionLength()
688 * Synopsis: Returns maximum accession length in hit list (targets).
689 * Incept: EPN, Tue May 24 13:35:23 2011
690 * SRE, Tue Aug 25 09:18:33 2009 [Janelia] (p7_tophits.c)
691 *
692 * Purpose: Same as <cm_tophits_GetMaxNameLength()>, but for
693 * accessions. If there are no hits in <h>, or none
694 * of the hits have accessions, returns 0.
695 */
696 int
697 cm_tophits_GetMaxAccessionLength(CM_TOPHITS *h)
698 {
699 int i, max, n;
700 for (max = 0, i = 0; i < h->N; i++)
701 if (h->unsrt[i].acc != NULL) {
702 n = strlen(h->unsrt[i].acc);
703 max = ESL_MAX(n, max);
704 }
705 return max;
706 }
707
708 /* Function: cm_tophits_GetMaxShownLength()
709 * Synopsis: Returns max shown name/accession length in hit list.
710 * Incept: EPN, Tue May 24 13:35:50 2011
711 * SRE, Tue Aug 25 09:30:43 2009 [Janelia] (p7_tophits.c)
712 *
713 * Purpose: Same as <cm_tophits_GetMaxNameLength()>, but
714 * for the case when --acc is on, where
715 * we show accession if one is available, and
716 * fall back to showing the name if it is not.
717 * Returns the max length of whatever is being
718 * shown as the reported "name".
719 */
720 int
721 cm_tophits_GetMaxShownLength(CM_TOPHITS *h)
722 {
723 int i, max, n;
724 for (max = 0, i = 0; i < h->N; i++)
725 {
726 if (h->unsrt[i].acc != NULL && h->unsrt[i].acc[0] != '\0')
727 {
728 n = strlen(h->unsrt[i].acc);
729 max = ESL_MAX(n, max);
730 }
731 else if (h->unsrt[i].name != NULL)
732 {
733 n = strlen(h->unsrt[i].name);
734 max = ESL_MAX(n, max);
735 }
736 }
737 return max;
738 }
739
740 /* Function: cm_tophits_GetMaxClanLength()
741 * Synopsis: Returns maximum length of a clan name in hit list (targets).
742 * Incept: EPN, Wed Jan 28 09:11:43 2015
743 *
744 * Purpose: Returns the length of the longest clan name of all of the
745 * registered hits, in chars. This is useful when deciding
746 * how to format output.
747 *
748 * The maximum is taken over all registered hits. This
749 * opens a possible side effect: caller might print only
750 * the top hits, and the max name length in these top hits
751 * may be different than the max length over all the hits.
752 *
753 * If there are no hits in <h>, <clan_name_kh> is NULL, or
754 * none of the hits have clans, returns 0.
755 */
756 int
757 cm_tophits_GetMaxClanLength(CM_TOPHITS *h, ESL_KEYHASH *clan_name_kh)
758 {
759 int i, max;
760
761 if(clan_name_kh == NULL) return 0;
762
763 max = 0;
764 for (i = 0; i < h->N; i++) {
765 if(h->unsrt[i].clan_idx != -1) {
766 max = ESL_MAX(max,
767 strlen(esl_keyhash_Get(clan_name_kh, h->unsrt[i].clan_idx)));
768 }
769 }
770 return max;
771 }
772
773 /* Function: cm_tophits_GetMaxModelLength()
774 * Synopsis: Returns the length of the maximum model length in hit list.
775 * Incept: EPN, Thu Oct 6 15:19:40 2022
776 *
777 * Purpose: Returns the length of the longest model clen (ad->clen)
778 * of all the registered hits, in chars. This is useful when
779 * deciding how to format output.
780 */
781 int
782 cm_tophits_GetMaxModelLength(CM_TOPHITS *h)
783 {
784 int i, max;
785
786 max = 0;
787 for (i = 0; i < h->N; i++) {
788 if(h->unsrt[i].ad) {
789 max = ESL_MAX(max, integer_textwidth(h->unsrt[i].ad->clen));
790 }
791 }
792 return max;
793 }
794
795 /* Function: cm_tophits_Reuse()
796 * Synopsis: Reuse a hit list, freeing internals.
797 * Incept: EPN, Tue May 24 13:36:15 2011
798 * SRE, Fri Jun 6 15:39:05 2008 [Janelia] (p7_tophits.c)
799 *
800 * Purpose: Reuse the tophits list <h>; save as
801 * many malloc/free cycles as possible,
802 * as opposed to <Destroy()>'ing it and
803 * <Create>'ing a new one.
804 */
805 int
806 cm_tophits_Reuse(CM_TOPHITS *h)
807 {
808 int i;
809
810 if (h == NULL) return eslOK;
811 if (h->unsrt != NULL)
812 {
813 for (i = 0; i < h->N; i++)
814 {
815 if (h->unsrt[i].name != NULL) free(h->unsrt[i].name);
816 if (h->unsrt[i].acc != NULL) free(h->unsrt[i].acc);
817 if (h->unsrt[i].desc != NULL) free(h->unsrt[i].desc);
818 if (h->unsrt[i].ad != NULL) cm_alidisplay_Destroy(h->unsrt[i].ad);
819 }
820 }
821 h->N = 0;
822 h->is_sorted_by_evalue = TRUE; /* because there's no hits */
823 h->is_sorted_for_overlap_removal = FALSE; /* actually this is true with 0 hits, but for safety,
824 * we don't want multiple sorted_* fields as TRUE */
825 h->is_sorted_by_position = FALSE; /* ditto */
826
827 h->hit[0] = h->unsrt;
828 return eslOK;
829 }
830
831 /* Function: cm_tophits_Destroy()
832 * Synopsis: Frees a hit list.
833 * Incept: EPN, Tue May 24 13:36:49 2011
834 * SRE, Fri Dec 28 07:33:21 2007 [Janelia] (p7_tophits.c)
835 */
836 void
837 cm_tophits_Destroy(CM_TOPHITS *h)
838 {
839 int i;
840 if (h == NULL) return;
841 if (h->hit != NULL) free(h->hit);
842 if (h->unsrt != NULL) {
843 for (i = 0; i < h->N; i++) {
844 if (h->unsrt[i].name != NULL) free(h->unsrt[i].name);
845 if (h->unsrt[i].acc != NULL) free(h->unsrt[i].acc);
846 if (h->unsrt[i].desc != NULL) free(h->unsrt[i].desc);
847 if (h->unsrt[i].ad != NULL) cm_alidisplay_Destroy(h->unsrt[i].ad);
848 }
849 free(h->unsrt);
850 }
851 free(h);
852 return;
853 }
854
855 /* Function: cm_tophits_CloneHitMostly()
856 * Synopsis: Add a new hit to <dest_th>, a clone of hit <h> in <src_th>.
857 * Incept: EPN, Wed May 25 08:17:35 2011
858 *
859 * Purpose: Create a new hit in the CM_TOPHITS object <dest_th>
860 * and copy the information from hit <h> in the sorted
861 * hitlist <src_th> into it.
862 *
863 * An exception of copying info:
864 * the <hit_idx> for the cloned hit in <dest_th> will
865 * be dest_th->N, not <src_th->hit[h]->hit_idx.
866 *
867 * NOTE: we do not copy name, acc, desc, or ad,
868 * and also note that any_oidx and win_oidx
869 * of new hit in <dest_th> will be set to -1
870 * no matter what src_th->h's values are.
871 *
872 * Returns: <eslOK> on success.
873 *
874 * Throws: <eslEMEM> on allocation error.
875 */
876 int
877 cm_tophits_CloneHitMostly(CM_TOPHITS *src_th, int h, CM_TOPHITS *dest_th)
878 {
879 CM_HIT *hit = NULL;
880 int status;
881
882 if ((status = cm_tophits_CreateNextHit(dest_th, &hit)) != eslOK) goto ERROR;
883 hit->cm_idx = src_th->hit[h]->cm_idx;
884 hit->clan_idx = src_th->hit[h]->clan_idx;
885 hit->seq_idx = src_th->hit[h]->seq_idx;
886 hit->pass_idx = src_th->hit[h]->pass_idx;
887 /* don't update hit->hit_idx, it will stay as set by CreateNextHit (dest_th->N-1) */
888 hit->start = src_th->hit[h]->start;
889 hit->stop = src_th->hit[h]->stop;
890 hit->in_rc = src_th->hit[h]->in_rc;
891 hit->root = src_th->hit[h]->root;
892 hit->mode = src_th->hit[h]->mode;
893 hit->score = src_th->hit[h]->score;
894 hit->bias = src_th->hit[h]->bias;
895 hit->pvalue = src_th->hit[h]->pvalue;
896 hit->evalue = src_th->hit[h]->evalue;
897 hit->has_evalue = src_th->hit[h]->has_evalue;
898 hit->srcL = src_th->hit[h]->srcL;
899 hit->hmmonly = src_th->hit[h]->hmmonly;
900 hit->glocal = src_th->hit[h]->glocal;
901 hit->flags = src_th->hit[h]->flags;
902 /* don't update hit->any_oidx, nor hit->win_oidx, they'll stay as -1 */
903 hit->any_bitE = src_th->hit[h]->any_bitE;
904 hit->win_bitE = src_th->hit[h]->win_bitE;
905 hit->ad = NULL;
906
907 return eslOK;
908
909 ERROR:
910 return status;
911 }
912
913 /* Function: cm_tophits_ComputeEvalues()
914 * Synopsis: Compute E-values given effective database size.
915 * Incept: EPN, Tue Sep 28 05:26:20 2010
916 *
917 * Purpose: After the cm pipeline has completed, the CM_TOPHITS object
918 * contains objects with p-values that haven't yet been
919 * converted to e-values. Compute E-values here based
920 * on the effective database size <eZ>.
921 *
922 * Normally the effective database size is calculated based
923 * on the number of expected hits in the database, as determined
924 * for a set database size in cmcalibrate and scaled up for
925 * current database size. However, if the pipeline was run in
926 * HMM only mode we use nhmmer's convention of defining
927 * the effective database size as the total database size
928 * divided by a window length, usually om->max_length.
929 *
930 * Returns: <eslOK> on success.
931 */
932 int
933 cm_tophits_ComputeEvalues(CM_TOPHITS *th, double eZ, int istart)
934 {
935 int i;
936
937 for (i = istart; i < th->N ; i++) {
938 th->unsrt[i].evalue = th->unsrt[i].pvalue * eZ;
939 }
940 return eslOK;
941 }
942
943 /* Helper functions for removing overlapping hits from a set of hits
944 * that all use the same model, are to the same sequence and are on
945 * the same strand. The hits in the set are sorted by score.
946 *
947 * remove_or_mark_overlaps_one_seq_fast(): O(N) with number of hits N, but
948 * less memory efficient (O(L), requiring an allocation of a char
949 * array the length of the sequence). This is how RSEARCH and infernal
950 * 1.0.2 did overlap removal.
951 *
952 * remove_or_mark_overlaps_one_seq_memeff(): O(N^2), but O(1) memory (not
953 * counting the O(N) memory hit list). This is fast for small numbers
954 * of sequences; up to about 10k seqs (10,000 hits takes about ~0.3
955 * seconds with benchmark_cm_tophits, but 100,000 hits takes 43s).
956 *
957 * cm_tophits_RemoveOrMarkOverlaps() picks a function to call based on
958 * length of the sequence L and number of hits N.
959 *
960 * If <do_remove> we'll remove duplicate hits, else we'll just
961 * mark them up (this is used by cmscan to mark overlapping hits
962 * to different models). If <do_remove> is FALSE, the hits do not
963 * need to be to the same model, else they do need to be.
964 *
965 * Both functions:
966 * Returns eslOK on success
967 * Returns eslEINVAL if not all hits in the set have equal srcL, seq_idx, in_rc values (and cm_idx if do_remove == TRUE)
968 * Returns eslERANGE if a hit includes positions outside of 1..srcL
969 * Returns eslEMEM if out of memory (remove_overlaps_one_seq_fast() only)
970 * errbuf is filled if not returning eslOK
971 */
972 int remove_or_mark_overlaps_one_seq_fast(CM_TOPHITS *th, int64_t idx1, int64_t idx2, int do_remove, char *errbuf)
973 {
974 int status;
975 int64_t i;
976 char *covered = NULL; /* [1..pos..srcL] is position pos covered by a hit we've examined and kept (if do_remove)? */
977 int64_t *covered_any_idx = NULL; /* [1..pos..srcL] highest scoring hit_idx that covers position pos */
978 int64_t *covered_win_idx = NULL; /* [1..pos..srcL] highest scoring *winning* hit_idx that covers position pos */
979 int overlap_flag = FALSE;
980 int64_t srcL = th->hit[idx1]->srcL;
981 int64_t cm_idx = th->hit[idx1]->cm_idx;
982 int64_t seq_idx = th->hit[idx1]->seq_idx;
983 int in_rc = th->hit[idx1]->in_rc;
984 int64_t pos, min, max; /* position indices in the sequence */
985 int64_t max_value = idx2+1; /* default value for covered_any_idx and covered_win_idx, so we can use ESL_MIN with range of valid values idx1..idx2 */
986
987 if( do_remove && (! th->is_sorted_for_overlap_removal)) ESL_FAIL(eslEINVAL, errbuf, "removing overlapping hits but hit list is not sorted properly");
988 if((! do_remove) && (! th->is_sorted_for_overlap_markup)) ESL_FAIL(eslEINVAL, errbuf, "marking overlapping hits but hit list is not sorted properly");
989
990 /*printf("in remove_or_mark_overlaps_one_seq_fast() do_remove: %d i: %" PRId64 " j: %" PRId64 "\n", do_remove, idx1, idx2);
991 cm_tophits_Dump(stdout, th);*/
992
993 ESL_ALLOC(covered, sizeof(char) * (srcL+1));
994 for(i = 0; i <= srcL; i++) covered[i] = FALSE;
995 if(! do_remove) {
996 ESL_ALLOC(covered_any_idx, sizeof(int64_t) * (srcL+1));
997 ESL_ALLOC(covered_win_idx, sizeof(int64_t) * (srcL+1));
998 for(i = 0; i <= srcL; i++) covered_any_idx[i] = max_value;
999 for(i = 0; i <= srcL; i++) covered_win_idx[i] = max_value;
1000 }
1001
1002 for(i = idx1; i <= idx2; i++) {
1003 /* verify that what we think is true is true */
1004 if(th->hit[i]->srcL != srcL) ESL_FAIL(eslEINVAL, errbuf, "removing/marking overlapping hits, srcL inconsistent, hit %" PRId64, i);
1005 if(th->hit[i]->seq_idx != seq_idx) ESL_FAIL(eslEINVAL, errbuf, "removing/marking overlapping hits, seq_idx is inconsistent, hit %" PRId64, i);
1006 if(th->hit[i]->in_rc != in_rc) ESL_FAIL(eslEINVAL, errbuf, "removing/marking overlapping hits, in_rc is inconsistent, hit %" PRId64, i);
1007 if(th->hit[i]->flags & CM_HIT_IS_MARKED_OVERLAP) ESL_FAIL(eslEINVAL, errbuf, "marking overlapping hits, overlap flag already up for hit %" PRId64 "\n", i);
1008 if(th->hit[i]->start < 1 || th->hit[i]->start > srcL) ESL_FAIL(eslERANGE, errbuf, "removing/marking overlapping hits, start posn is inconsistent, hit %" PRId64, i);
1009 if(th->hit[i]->stop < 1 || th->hit[i]->stop > srcL) ESL_FAIL(eslERANGE, errbuf, "removing/marking overlapping hits, stop posn is inconsistent, hit %" PRId64, i);
1010
1011 if(do_remove && th->hit[i]->cm_idx != cm_idx) ESL_FAIL(eslEINVAL, errbuf, "removing/marking overlapping hits, cm_idx is inconsistent, hit %" PRId64, i);
1012
1013 if(! (th->hit[i]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) {
1014 /* i is not a duplicate that's already been removed */
1015
1016 min = ESL_MIN(th->hit[i]->start, th->hit[i]->stop);
1017 max = ESL_MAX(th->hit[i]->start, th->hit[i]->stop);
1018 overlap_flag = FALSE;
1019 for(pos = min; pos <= max; pos++) {
1020 if(covered[pos] == TRUE) { overlap_flag = TRUE; break; }
1021 }
1022
1023 /* how we process the hit differs significantly between
1024 * whether we're in remove mode (do_remove is TRUE) or
1025 * mark mode (do_remove is FALSE)
1026 */
1027 if(do_remove) {
1028 if(overlap_flag) {
1029 th->hit[i]->flags |= CM_HIT_IS_REMOVED_DUPLICATE;
1030 th->hit[i]->flags &= ~CM_HIT_IS_REPORTED; /* could be set if pli->use_bit_cutoffs (--cut_ga, --cut_nc, --cut_tc) */
1031 th->hit[i]->flags &= ~CM_HIT_IS_INCLUDED; /* could be set if pli->use_bit_cutoffs (--cut_ga, --cut_nc, --cut_tc) */
1032 }
1033 else {
1034 for(pos = min; pos <= max; pos++) covered[pos] = TRUE;
1035 }
1036 }
1037 else { /* do_remove is FALSE */
1038 if(overlap_flag) {
1039 /* marking overlaps, not removing them */
1040 th->hit[i]->flags |= CM_HIT_IS_MARKED_OVERLAP;
1041 /* determine the hit_idx of the best hit it overlaps with (any_oidx)
1042 * and the best non-marked hit it overlaps with (win_oidx),
1043 * these will often be the same hit_idx
1044 */
1045 /* first, the best hit (any_oidx) */
1046 th->hit[i]->any_oidx = covered_any_idx[min];
1047 for(pos = min+1; pos <= max; pos++) {
1048 th->hit[i]->any_oidx = ESL_MIN(th->hit[i]->any_oidx, covered_any_idx[pos]);
1049 }
1050 if(th->hit[i]->any_oidx == max_value) th->hit[i]->any_oidx = -1; /* no overlap */
1051
1052 /* second, the best winning hit (win_oidx) */
1053 th->hit[i]->win_oidx = covered_win_idx[min];
1054 for(pos = min+1; pos <= max; pos++) {
1055 th->hit[i]->win_oidx = ESL_MIN(th->hit[i]->win_oidx, covered_win_idx[pos]);
1056 }
1057 if(th->hit[i]->win_oidx == max_value) th->hit[i]->win_oidx = -1; /* no overlap */
1058
1059 /* now th->hit[i]->any_oidx and th->hit[i]->win_oidx point to
1060 * the sorted hit index, change it to the actual hit_idx
1061 */
1062 if(th->hit[i]->any_oidx != -1) th->hit[i]->any_oidx = th->hit[th->hit[i]->any_oidx]->hit_idx;
1063 if(th->hit[i]->win_oidx != -1) th->hit[i]->win_oidx = th->hit[th->hit[i]->win_oidx]->hit_idx;
1064 } /* end of 'if(overlap_flag)' */
1065 else { /* no overlap */
1066 /* need to keep track that this hit is the best scoring winning hit
1067 * that covers any previously uncovered positions
1068 */
1069 for(pos = min; pos <= max; pos++) {
1070 if(covered_win_idx[pos] == max_value) covered_win_idx[pos] = i;
1071 }
1072 }
1073 /* regardless of whether we found an overlap or not, we need
1074 * to keep track that this hit is the best scoring hit that
1075 * covers any previously uncovered positions, and update
1076 * covered[] as well.
1077 */
1078 for(pos = min; pos <= max; pos++) {
1079 if(covered_any_idx[pos] == max_value) covered_any_idx[pos] = i;
1080 covered[pos] = TRUE;
1081 }
1082 }
1083 }
1084 }
1085
1086 if(covered != NULL) free(covered);
1087 if(covered_any_idx != NULL) free(covered_any_idx);
1088 if(covered_win_idx != NULL) free(covered_win_idx);
1089 return eslOK;
1090
1091 ERROR:
1092 if(covered != NULL) free(covered);
1093 ESL_FAIL(status, errbuf, "removing/marking overlapping hits, out of memory");
1094 return status; /* NOT REACHED */
1095 }
1096
1097 int remove_or_mark_overlaps_one_seq_memeff(CM_TOPHITS *th, int64_t idx1, int64_t idx2, int do_remove, char *errbuf)
1098 {
1099 int64_t i, j;
1100 int64_t srcL = th->hit[idx1]->srcL;
1101 int64_t cm_idx = th->hit[idx1]->cm_idx;
1102 int64_t seq_idx = th->hit[idx1]->seq_idx;
1103 int in_rc = th->hit[idx1]->in_rc;
1104 int overlap_flag = FALSE;
1105
1106 /*printf("in remove_overlaps_one_seq_memeff() do_remove: %d i: %" PRId64 " j: %" PRId64 "\n", do_remove, idx1, idx2);
1107 cm_tophits_Dump(stdout, th);*/
1108 if( do_remove && (! th->is_sorted_for_overlap_removal)) ESL_FAIL(eslEINVAL, errbuf, "removing overlapping hits but hit list is not sorted properly");
1109 if((! do_remove) && (! th->is_sorted_for_overlap_markup)) ESL_FAIL(eslEINVAL, errbuf, "marking overlapping hits but hit list is not sorted properly");
1110
1111 for(i = idx1; i <= idx2; i++) {
1112 /* verify that what we think is true is true */
1113 if(th->hit[i]->srcL != srcL) ESL_FAIL(eslEINVAL, errbuf, "removing/marking overlapping hits, srcL inconsistent, hit %" PRId64, i);
1114 if(th->hit[i]->seq_idx != seq_idx) ESL_FAIL(eslEINVAL, errbuf, "removing/marking overlapping hits, seq_idx is inconsistent, hit %" PRId64, i);
1115 if(th->hit[i]->in_rc != in_rc) ESL_FAIL(eslEINVAL, errbuf, "removing/marking overlapping hits, in_rc is inconsistent, hit %" PRId64, i);
1116 if(th->hit[i]->start < 1 || th->hit[i]->start > srcL) ESL_FAIL(eslERANGE, errbuf, "removing/marking overlapping hits, start posn is inconsistent, hit %" PRId64, i);
1117 if(th->hit[i]->stop < 1 || th->hit[i]->stop > srcL) ESL_FAIL(eslERANGE, errbuf, "removing/marking overlapping hits, stop posn is inconsistent, hit %" PRId64, i);
1118 if(do_remove && th->hit[i]->cm_idx != cm_idx) ESL_FAIL(eslEINVAL, errbuf, "removing/marking overlapping hits, cm_idx is inconsistent, hit %" PRId64, i);
1119
1120 if(! (th->hit[i]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) {
1121 /* i is not a duplicate that's already been removed */
1122 for(j = i+1; j <= idx2; j++) {
1123 if(! (th->hit[j]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) {
1124 /* j has not already been removed */
1125 overlap_flag = FALSE; /* set to TRUE below if i and j overlap */
1126 /*printf("comparing %" PRId64 " and %" PRId64 "\n", i, j);*/
1127 if(th->hit[j]->in_rc == FALSE && /* both i and j are on forward strand */
1128 (! (th->hit[j]->stop < th->hit[i]->start)) && /* one of two ways in which i and j DO NOT overlap */
1129 (! (th->hit[i]->stop < th->hit[j]->start))) { /* the other way in which i and j DO NOT overlap */
1130 /* i and j overlap, i is better scoring so remove/mark j */
1131 overlap_flag = TRUE;
1132 }
1133 else if(th->hit[j]->in_rc == TRUE && /* both i and j are on reverse strand */
1134 (! (th->hit[j]->start < th->hit[i]->stop)) && /* one of two ways in which i and j DO NOT overlap */
1135 (! (th->hit[i]->start < th->hit[j]->stop))) { /* the other way in which i and j DO NOT overlap */
1136 /* i and j overlap, i is better scoring so remove or mark j */
1137 overlap_flag = TRUE;
1138 }
1139 if(overlap_flag) {
1140 if(do_remove) {
1141 th->hit[j]->flags |= CM_HIT_IS_REMOVED_DUPLICATE;
1142 th->hit[j]->flags &= ~CM_HIT_IS_REPORTED; /* could be set if pli->use_bit_cutoffs (--cut_ga, --cut_nc, --cut_tc) */
1143 th->hit[j]->flags &= ~CM_HIT_IS_INCLUDED; /* could be set if pli->use_bit_cutoffs (--cut_ga, --cut_nc, --cut_tc) */
1144 /*printf("\tremoved j (FWD) %5" PRId64 "..%5" PRId64 " overlaps with %5" PRId64 "..%5" PRId64 "\n", th->hit[i]->start, th->hit[i]->stop, th->hit[j]->start, th->hit[j]->stop);*/
1145 }
1146 else {
1147 th->hit[j]->flags |= CM_HIT_IS_MARKED_OVERLAP;
1148 /* don't change reported/included flags */
1149
1150 /* update any_oidx and win_oidx */
1151 if(th->hit[j]->any_oidx == -1) th->hit[j]->any_oidx = th->hit[i]->hit_idx;
1152 /* if j's any_oidx or win_oidx is not -1, it must have
1153 * already been set to a better hit, i.e. a lower i
1154 * (and since we're sorted by score we know that's a
1155 * better hit).
1156 */
1157 if((! (th->hit[i]->flags & CM_HIT_IS_MARKED_OVERLAP)) &&
1158 (th->hit[j]->win_oidx == -1)) {
1159 th->hit[j]->win_oidx = th->hit[i]->hit_idx;
1160 }
1161 }
1162 }
1163 }
1164 }
1165 }
1166 }
1167 return eslOK;
1168 }
1169
1170 /* Function: cm_tophits_RemoveOrMarkOverlaps()
1171 * Synopsis: Remove or mark overlapping hits from a tophits object sorted by seq_idx.
1172 * Incept: EPN, Tue Jun 14 05:42:31 2011
1173 *
1174 * Purpose: After the CM pipeline has completed, the CM_TOPHITS
1175 * object may contain overlapping hits if the target was
1176 * broken into overlapping windows. Scan through the
1177 * tophits object, which, upon entering, is sorted by cm
1178 * index, sequence index, strand, and score and remove hits
1179 * greedily. For each hit for the same model, sequence and
1180 * strand, remove all lower scoring hits that overlap with
1181 * it. This is done by one of two helper functions
1182 * (remove_or_mark_overlaps_one_seq_fast() or
1183 * remove_or_mark_overlaps_one_seq_memeff() described near
1184 * their definition above) depending on the length of the
1185 * sequence and the number of hits.
1186 *
1187 * This function can be used to either mark overlaps or
1188 * remove them, determined by the
1189 * th->is_sorted_for_overlap_removal and
1190 * th->is_sorted_for_overlap_markup flags, exactly one
1191 * of which must be true, otherwise we'll die.
1192 *
1193 * <do_clans_only>: TRUE to only markup overlaps for hits in
1194 * the same clan, FALSE to markup overlaps between all
1195 * hits. IRRELEVANT if th->is_sorted_for_overlap_removal.
1196 *
1197 * Returns: eslOK on success.
1198 * eslEINVAL if th is not sorted appropriately, errbuf filled
1199 * eslERANGE if a hit includes positions outside of 1..srcL, errbuf filled
1200 * eslEMEM if we run out of memory, errbuf filled
1201 */
1202 int
1203 cm_tophits_RemoveOrMarkOverlaps(CM_TOPHITS *th, int do_clans_only, char *errbuf)
1204 {
1205 int status;
1206 int64_t i, j;
1207 int64_t nhits = 0;
1208 int do_remove = FALSE;
1209 int srcL_limit;
1210
1211 if (th->is_sorted_for_overlap_removal &&
1212 th->is_sorted_for_overlap_markup) {
1213 ESL_FAIL(eslEINVAL, errbuf, "cm_tophits_RemoveOrMarkOverlaps() list is not sorted appropriately");
1214 }
1215
1216 if (th->is_sorted_for_overlap_removal) {
1217 do_remove = TRUE;
1218 srcL_limit = 256000000;
1219 }
1220 else if (th->is_sorted_for_overlap_markup) {
1221 do_remove = FALSE;
1222 srcL_limit = 256000000 / 16;
1223 /* we require about 16X more memory in remove_or_mark_overlaps_one_seq_fast() if we're sorting for
1224 * overlap markup, so our limit is 1/16 the length.
1225 */
1226 }
1227 else {
1228 ESL_FAIL(eslEINVAL, errbuf, "cm_tophits_RemoveOrMarkOverlaps() list is not sorted appropriately");
1229 }
1230 if(do_remove && do_clans_only) {
1231 ESL_FAIL(eslEINVAL, errbuf, "cm_tophits_RemoveOrMarkOverlaps() list is sorted for overlap removal and we're trying to respect clan membership...shouldn't happen");
1232 }
1233
1234 if (th->N<2) return eslOK;
1235
1236 i = 0;
1237 while(i < th->N) {
1238 j = i+1;
1239 while(j < th->N &&
1240 ((! do_remove) || (th->hit[j]->cm_idx == th->hit[i]->cm_idx)) &&
1241 th->hit[j]->seq_idx == th->hit[i]->seq_idx &&
1242 th->hit[j]->in_rc == th->hit[i]->in_rc &&
1243 ((! do_clans_only) || (th->hit[j]->clan_idx == th->hit[i]->clan_idx))) {
1244 j++;
1245 }
1246 if(j != (i+1) && /* more than one hit in set i..j-1 */
1247 ((! do_clans_only) || th->hit[i]->clan_idx != -1)) { /* we're not only marking overlaps within same clan OR
1248 * we are and all hits are in same clan, this way if
1249 * do_clans_only is TRUE we won't remove overlaps between
1250 * hits in families that are not members of any clan */
1251 /* Hits i to j-1 form a set of hits that all share cm_idx (if
1252 * 'do_remove == TRUE'), seq_idx, in_rc and clan_idx (if
1253 * 'do_clans_only == TRUE'). Remove overlaps from this set in 1 of 2
1254 * ways, depending on length of source sequence and number of
1255 * hits. remove_overlaps_one_seq_fast() will need to allocate a
1256 * char array of size srcL, but it is significantly faster when
1257 * nhits is big. If (do_remove) is FALSE then we need about 16X
1258 * more memory in 'one_seq_fast()' method so our srcL_limit is
1259 * 16-fold lower (calc'ed above).
1260 */
1261 nhits = (j-1)-i+1;
1262 if(nhits < 5000 || th->hit[i]->srcL > srcL_limit) {
1263 if((status = remove_or_mark_overlaps_one_seq_memeff(th, i, j-1, do_remove, errbuf)) != eslOK) return status;
1264 }
1265 else { /* use fast, non mem-efficient way if >= 5000 hits and L is < srcL_limit */
1266 if((status = remove_or_mark_overlaps_one_seq_fast (th, i, j-1, do_remove, errbuf)) != eslOK) return status;
1267 }
1268 }
1269 i = j; /* skip ahead to begin next set */
1270 }
1271 /*printf("Leaving cm_tophits_RemoveOverlaps()\n");
1272 cm_tophits_Dump(stdout, th);*/
1273
1274 return eslOK;
1275 }
1276
1277 /* Function: cm_tophits_UpdateHitPositions()
1278 * Synopsis: Update sequence positions in a hit list.
1279 * Incept: EPN, Wed May 25 09:12:52 2011
1280 *
1281 * Purpose: For hits <hit_start..th->N-1> in a hit list, update the
1282 * positions of start, stop, ad->sqto and ad->sqfrom. This
1283 * is necessary when we've searched a chunk of subsequence
1284 * that originated somewhere within a larger sequence.
1285 *
1286 *
1287 * Returns: <eslOK> on success.
1288 */
1289 int
1290 cm_tophits_UpdateHitPositions(CM_TOPHITS *th, int hit_start, int64_t seq_start, int in_revcomp)
1291 {
1292 int i;
1293 if(in_revcomp) {
1294 for (i = hit_start; i < th->N ; i++) {
1295 th->unsrt[i].start = seq_start - th->unsrt[i].start + 1;
1296 th->unsrt[i].stop = seq_start - th->unsrt[i].stop + 1;
1297 th->unsrt[i].in_rc = TRUE;
1298 if(th->unsrt[i].ad != NULL) {
1299 th->unsrt[i].ad->sqfrom = seq_start - th->unsrt[i].ad->sqfrom + 1;
1300 th->unsrt[i].ad->sqto = seq_start - th->unsrt[i].ad->sqto + 1;
1301 }
1302 }
1303
1304 }
1305 else {
1306 for (i = hit_start; i < th->N ; i++) {
1307 th->unsrt[i].start += seq_start-1;
1308 th->unsrt[i].stop += seq_start-1;
1309 th->unsrt[i].in_rc = FALSE;
1310 if(th->unsrt[i].ad != NULL) {
1311 th->unsrt[i].ad->sqfrom += seq_start-1;
1312 th->unsrt[i].ad->sqto += seq_start-1;
1313 }
1314 }
1315 }
1316 return eslOK;
1317 }
1318
1319 /* Function: cm_tophits_SetSourceLengths()
1320 * Synopsis: Update hit->srcL for all hits in a hitlist.
1321 * Incept: EPN, Wed Nov 23 14:57:53 2011
1322 *
1323 * Purpose: For all hits in a hitlist, set the
1324 * srcL value (full length of source sequence the hit originated
1325 * in) given an array of <nseqs> source lengths indexed by seq_idx.
1326 *
1327 * Returns: <eslOK> on success.
1328 * <eslEINVAL> if hit->seq_idx >= nseqs (we have a hit to a sequence
1329 * we don't know the length of - something went wrong)
1330 */
1331 int
1332 cm_tophits_SetSourceLengths(CM_TOPHITS *th, int64_t *srcL, uint64_t nseqs)
1333 {
1334 int i;
1335 for (i = 0; i < th->N ; i++) {
1336 if(th->unsrt[i].seq_idx >= nseqs) return eslEINVAL;
1337 th->unsrt[i].srcL = srcL[th->unsrt[i].seq_idx];
1338 }
1339
1340 return eslOK;
1341 }
1342
1343 /* Function: cm_tophits_OverlapNres()
1344 * Synopsis: Helper function for determining overlap fractions.
1345 * Incept: EPN, Tue Jun 5 09:29:41 2018
1346 *
1347 * Purpose: Determines the number of residues overlapping between
1348 * from1..to1 and from2..to2, and returns it in <*ret_nres>.
1349 * in) given an array of <nseqs> source lengths indexed by seq_idx.
1350 *
1351 * Args: from1 - start position of region 1, must be <= to1
1352 * to1 - stop position of region 1, must be >= from1
1353 * from2 - start position of region 2, must be <= to2
1354 * to2 - stop position of region 2, must be >= from1
1355 * ret_nres - RETURN: number of residues of overlap between region 1 and 2, 0 if none
1356 * errbuf - error buffer for error message, if eslOK is not returned
1357 *
1358 * Returns: eslOK on success
1359 * eslEINVAL if the following is not true: from1 <= to1, from2 <= to2.
1360 * and errbuf filled with an error message.
1361 * eslEINCONCEIVABLE if inconceivable situation arises
1362 */
1363 int64_t cm_tophits_OverlapNres(int64_t from1, int64_t to1, int64_t from2, int64_t to2, int64_t *ret_nres, char *errbuf)
1364 {
1365 int64_t tmp;
1366 int64_t nres;
1367
1368 if(from1 > to1) ESL_FAIL(eslEINVAL, errbuf, "in cm_tophits_OverlapNres, from1 (%" PRId64 ") > to1 (%" PRId64 ")", from1, to1);
1369 if(from2 > to2) ESL_FAIL(eslEINVAL, errbuf, "in cm_tophits_OverlapNres, from2 (%" PRId64 ") > to2 (%" PRId64 ")", from2, to2);
1370
1371 /* wwap if nec so that from1 <= <from2. */
1372 if(from1 > from2) {
1373 tmp = from1; from1 = from2; from2 = tmp;
1374 tmp = to1; to1 = to2; to2 = tmp;
1375 }
1376
1377 /* 3 possible cases:
1378 * Case 1. from1 <= to1 < from2 <= to2 overlap is 0
1379 * Case 2. from1 <= from2 <= to1 < to2
1380 * Case 3. from1 <= from2 <= to2 <= to1
1381 */
1382 if (to1 < from2) { nres = 0; } /* case 1 */
1383 else if(to1 < to2) { nres = (to1 - from2 + 1); } /* case 2 */
1384 else if(to2 <= to1) { nres = (to2 - from2 + 1); } /* case 3 */
1385 else { ESL_FAIL(eslEINCONCEIVABLE, errbuf, "unforeseen case in cm_tophits_OverlapNres(), from1..to1 (%" PRId64 "..%" PRId64 ") from2..to 2(%" PRId64 "..%" PRId64 ")", from1, to1, from2, to2); }
1386
1387 *ret_nres = nres;
1388 return eslOK;
1389 }
1390
1391 /*---------------- end, CM_TOPHITS object -----------------------*/
1392
1393 /*****************************************************************
1394 * 2. Standard (human-readable) output of pipeline results
1395 *****************************************************************/
1396
1397
1398 /* Function: cm_tophits_Threshold()
1399 * Synopsis: Apply score and E-value thresholds to a hitlist before output.
1400 * Incept: EPN, Tue May 24 13:44:13 2011
1401 * SRE, Tue Dec 9 09:04:55 2008 [Janelia] (p7_tophits.c)
1402 *
1403 * Purpose: After a pipeline has completed, go through it and mark all
1404 * the targets and domains that are "significant" (satisfying
1405 * the reporting thresholds set for the pipeline).
1406 *
1407 * Also sets the final total number of reported and
1408 * included targets, the number of reported and included
1409 * targets in each target, and the size of the search space
1410 * for per-domain conditional E-value calculations,
1411 * <pli->domZ>. By default, <pli->domZ> is the number of
1412 * significant targets reported.
1413 *
1414 * If model-specific thresholds were used in the pipeline,
1415 * we cannot apply those thresholds now. They were already
1416 * applied in the pipeline. In this case all we're
1417 * responsible for here is counting them (setting
1418 * nreported, nincluded counters).
1419 *
1420 * Returns: <eslOK> on success.
1421 */
1422 int
1423 cm_tophits_Threshold(CM_TOPHITS *th, CM_PIPELINE *pli)
1424 {
1425 int h; /* counter */
1426
1427 /* Flag reported, included targets (if we're using general thresholds) */
1428 if (! pli->use_bit_cutoffs) {
1429 for (h = 0; h < th->N; h++) {
1430 if (! (th->hit[h]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) {
1431 if (cm_pli_TargetReportable(pli, th->hit[h]->score, th->hit[h]->evalue)) {
1432 th->hit[h]->flags |= CM_HIT_IS_REPORTED;
1433 if (cm_pli_TargetIncludable(pli, th->hit[h]->score, th->hit[h]->evalue))
1434 th->hit[h]->flags |= CM_HIT_IS_INCLUDED;
1435 }
1436 }
1437 }
1438 }
1439
1440 /* Count reported, included targets */
1441 th->nreported = 0;
1442 th->nincluded = 0;
1443 for (h = 0; h < th->N; h++) {
1444 if (! (th->hit[h]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) {
1445 if (th->hit[h]->flags & CM_HIT_IS_REPORTED) th->nreported++;
1446 if (th->hit[h]->flags & CM_HIT_IS_INCLUDED) th->nincluded++;
1447 }
1448 }
1449
1450 return eslOK;
1451 }
1452
1453 /* Function: cm_tophits_Targets()
1454 * Synopsis: Standard output format for a top target hits list.
1455 * Incept: EPN, Tue May 24 13:48:29 2011
1456 * SRE, Tue Dec 9 09:10:43 2008 [Janelia] (p7_tophits.c)
1457 *
1458 * Purpose: Output a list of the reportable top target hits in <th>
1459 * in human-readable ASCII text format to stream <ofp>, using
1460 * final pipeline accounting stored in <pli>.
1461 *
1462 * Hits must have a valid alignment display in order
1463 * to determine if they're truncated or not.
1464 *
1465 * The tophits list <th> should already be sorted (see
1466 * <cm_tophits_Sort()> and thresholded (see
1467 * <cm_tophits_Threshold>).
1468 *
1469 * Returns: <eslOK> on success.
1470 */
1471 int
1472 cm_tophits_Targets(FILE *ofp, CM_TOPHITS *th, CM_PIPELINE *pli, int textw)
1473 {
1474 int status;
1475 int h,i;
1476 int namew;
1477 int posw;
1478 int descw;
1479 int rankw;
1480 char *showname;
1481 int have_printed_incthresh = FALSE;
1482 int nprinted = 0;
1483
1484 char *namestr = NULL;
1485 char *posstr = NULL;
1486 char *rankstr = NULL;
1487 char *cur_rankstr = NULL;
1488
1489 /* when --acc is on, we'll show accession if available, and fall back to name */
1490 if (pli->show_accessions) namew = ESL_MAX((pli->mode == CM_SEARCH_SEQS ? 8 : 9), cm_tophits_GetMaxShownLength(th));
1491 else namew = ESL_MAX((pli->mode == CM_SEARCH_SEQS ? 8 : 9), cm_tophits_GetMaxNameLength(th));
1492
1493 posw = ESL_MAX(6, cm_tophits_GetMaxPositionLength(th));
1494
1495 if (textw > 0) descw = ESL_MAX(32, textw - namew - (2*posw) - 31); /* 31 chars excluding desc is from the format: 1 + 2 + 9+2(E-value) + 6+2(score) + 1+2(strand) + 2+2+2+2(spacing) */
1496 else descw = 0; /* unlimited desc length is handled separately */
1497
1498 rankw = ESL_MAX(4, (integer_textwidth(th->nreported)+2));
1499
1500 ESL_ALLOC(namestr, sizeof(char) * (namew+1));
1501 ESL_ALLOC(posstr, sizeof(char) * (posw+1));
1502 ESL_ALLOC(rankstr, sizeof(char) * (rankw+1));
1503 ESL_ALLOC(cur_rankstr, sizeof(char) * (rankw+1));
1504
1505 for(i = 0; i < namew; i++) { namestr[i] = '-'; } namestr[namew] = '\0';
1506 for(i = 0; i < posw; i++) { posstr[i] = '-'; } posstr[posw] = '\0';
1507 for(i = 0; i < rankw; i++) { rankstr[i] = '-'; } rankstr[rankw] = '\0';
1508 cur_rankstr[rankw] = '\0';
1509
1510 fprintf(ofp, "Hit scores:\n");
1511 fprintf(ofp, " %*s %1s %9s %6s %5s %-*s %*s %*s %1s %3s %5s %4s %s\n", rankw, "rank", "", "E-value", " score", " bias", namew, (pli->mode == CM_SEARCH_SEQS ? "sequence":"modelname"), posw, "start", posw, "end", "", "mdl", "trunc", "gc", "description");
1512 fprintf(ofp, " %*s %1s %9s %6s %5s %-*s %*s %*s %1s %3s %5s %4s %s\n", rankw, rankstr, "", "---------", "------", "-----", namew, namestr, posw, posstr, posw, posstr, "", "---", "-----", "----", "-----------");
1513
1514 nprinted = 0;
1515 for (h = 0; h < th->N; h++) {
1516 if (th->hit[h]->flags & CM_HIT_IS_REPORTED) {
1517
1518 if (! (th->hit[h]->flags & CM_HIT_IS_INCLUDED) && ! have_printed_incthresh) {
1519 fprintf(ofp, " ------ inclusion threshold ------\n");
1520 have_printed_incthresh = TRUE;
1521 }
1522
1523 if (pli->show_accessions) {
1524 /* the --acc option: report accessions rather than names if possible */
1525 if (th->hit[h]->acc != NULL && th->hit[h]->acc[0] != '\0') showname = th->hit[h]->acc;
1526 else showname = th->hit[h]->name;
1527 }
1528 else {
1529 showname = th->hit[h]->name;
1530 }
1531
1532 snprintf(cur_rankstr, rankw+1, "(%d)", nprinted+1); /* +1 for null termination char */
1533
1534 fprintf(ofp, " %*s %c %9.2g %6.1f %5.1f %-*s %*" PRId64 " %*" PRId64 " %c %3s %5s %4.2f ",
1535 rankw, cur_rankstr,
1536 (th->hit[h]->flags & CM_HIT_IS_INCLUDED ? '!' : '?'),
1537 th->hit[h]->evalue,
1538 th->hit[h]->score,
1539 th->hit[h]->bias,
1540 namew, showname,
1541 posw, th->hit[h]->start,
1542 posw, th->hit[h]->stop,
1543 (th->hit[h]->in_rc == TRUE) ? '-' : '+',
1544 th->hit[h]->hmmonly ? "hmm" : "cm",
1545 cm_alidisplay_TruncString(th->hit[h]->ad),
1546 th->hit[h]->ad->gc);
1547
1548 if (textw > 0) fprintf(ofp, "%-.*s\n", descw, th->hit[h]->desc == NULL ? "-" : th->hit[h]->desc);
1549 else fprintf(ofp, "%s\n", th->hit[h]->desc == NULL ? "-" : th->hit[h]->desc);
1550 /* do NOT use *s with unlimited (INT_MAX) line length. Some systems
1551 * have an fprintf() bug here (we found one on an Opteron/SUSE Linux
1552 * system (#h66))
1553 */
1554 nprinted++;
1555 }
1556 }
1557 if (th->nreported == 0) fprintf(ofp, "\n [No hits detected that satisfy reporting thresholds]\n");
1558
1559 if(namestr != NULL) free(namestr);
1560 if(posstr != NULL) free(posstr);
1561 if(rankstr != NULL) free(rankstr);
1562 if(cur_rankstr != NULL) free(cur_rankstr);
1563
1564 return eslOK;
1565
1566 ERROR:
1567 if(namestr != NULL) free(namestr);
1568 if(posstr != NULL) free(posstr);
1569
1570 return status;
1571 }
1572
1573 /* Function: cm_tophits_F3Targets()
1574 * Synopsis: Standard output format for a top target hits list
1575 * in special terminate after filter stage F3 mode.
1576 *
1577 * Incept: EPN, Wed Oct 22 13:29:23 2014
1578 * SRE, Tue Dec 9 09:10:43 2008 [Janelia] (p7_tophits.c)
1579 *
1580 * Purpose: Output a list of the reportable top target hits in <th>
1581 * in human-readable ASCII text format to stream <ofp>, using
1582 * final pipeline accounting stored in <pli>. This version
1583 * is similar to cm_tophits_Targets() but reports a different
1584 * set of fields.
1585 *
1586 * The tophits list <th> should already be sorted (see
1587 * <cm_tophits_Sort()> and thresholded (see
1588 * <cm_tophits_Threshold>).
1589 *
1590 * Returns: <eslOK> on success.
1591 */
1592 int
1593 cm_tophits_F3Targets(FILE *ofp, CM_TOPHITS *th, CM_PIPELINE *pli)
1594 {
1595 int status;
1596 int h,i;
1597 int namew;
1598 int posw;
1599 int descw;
1600 char *showname;
1601 char lseq, rseq;
1602
1603 char *namestr = NULL;
1604 char *descstr = NULL;
1605 char *posstr = NULL;
1606
1607 /* when --acc is on, we'll show accession if available, and fall back to name */
1608 if (pli->show_accessions) namew = ESL_MAX((pli->mode == CM_SEARCH_SEQS ? 8 : 9), cm_tophits_GetMaxShownLength(th));
1609 else namew = ESL_MAX((pli->mode == CM_SEARCH_SEQS ? 8 : 9), cm_tophits_GetMaxNameLength(th));
1610 descw = ESL_MAX((pli->mode == CM_SEARCH_SEQS ? 9 : 8), cm_tophits_GetMaxDescLength(th));
1611 posw = ESL_MAX(6, cm_tophits_GetMaxPositionLength(th));
1612
1613 ESL_ALLOC(namestr, sizeof(char) * (namew+1));
1614 ESL_ALLOC(descstr, sizeof(char) * (descw+1));
1615 ESL_ALLOC(posstr, sizeof(char) * (posw+1));
1616
1617 for(i = 0; i < namew; i++) { namestr[i] = '-'; } namestr[namew] = '\0';
1618 for(i = 0; i < descw; i++) { descstr[i] = '-'; } descstr[descw] = '\0';
1619 for(i = 0; i < posw; i++) { posstr[i] = '-'; } posstr[posw] = '\0';
1620
1621 fprintf(ofp, "Windows that survived F3 filter stage:\n");
1622 fprintf(ofp, " %1s %-*s %-*s %6s %*s %*s\n", "", namew, (pli->mode == CM_SEARCH_SEQS) ? "sequence" : "modelname", descw, (pli->mode == CM_SEARCH_SEQS) ? "modelname" : "sequence", " score", posw, "start", posw, "end");
1623 fprintf(ofp, " %1s %*s %*s %6s %*s %*s\n", "", namew, namestr, descw, descstr, "------", posw, posstr, posw, posstr);
1624
1625 for (h = 0; h < th->N; h++) {
1626 if (th->hit[h]->flags & CM_HIT_IS_REPORTED) {
1627
1628 if (pli->show_accessions) {
1629 /* the --acc option: report accessions rather than names if possible */
1630 if (th->hit[h]->acc != NULL && th->hit[h]->acc[0] != '\0') showname = th->hit[h]->acc;
1631 else showname = th->hit[h]->name;
1632 }
1633 else {
1634 showname = th->hit[h]->name;
1635 }
1636 if(th->hit[h]->in_rc) {
1637 lseq = th->hit[h]->start == th->hit[h]->srcL ? '[' : '.';
1638 rseq = th->hit[h]->stop == 1 ? ']' : '.';
1639 }
1640 else {
1641 lseq = th->hit[h]->start == 1 ? '[' : '.';
1642 rseq = th->hit[h]->stop == th->hit[h]->srcL ? ']' : '.';
1643 }
1644
1645 fprintf(ofp, " %c %-*s %-*s %6.1f %*" PRId64 " %*" PRId64 " %c %c%c\n",
1646 (th->hit[h]->flags & CM_HIT_IS_INCLUDED ? '!' : '?'),
1647 namew, showname,
1648 descw, th->hit[h]->desc,
1649 th->hit[h]->score,
1650 posw, th->hit[h]->start,
1651 posw, th->hit[h]->stop,
1652 (th->hit[h]->in_rc == TRUE) ? '-' : '+',
1653 lseq, rseq);
1654 }
1655 }
1656 if (th->nreported == 0) fprintf(ofp, "\n [No hits detected that satisfy reporting thresholds]\n");
1657
1658 if(namestr != NULL) free(namestr);
1659 if(descstr != NULL) free(descstr);
1660 if(posstr != NULL) free(posstr);
1661
1662 return eslOK;
1663
1664 ERROR:
1665 if(namestr != NULL) free(namestr);
1666 if(descstr != NULL) free(descstr);
1667 if(posstr != NULL) free(posstr);
1668
1669 return status;
1670 }
1671
1672 /* Function: cm_tophits_HitAlignments()
1673 * Synopsis: Standard output format for alignments.
1674 * Incept: EPN, Tue May 24 13:50:53 2011
1675 * SRE, Tue Dec 9 09:32:32 2008 [Janelia] (p7_tophits.c::p7_tophits_Domains())
1676 *
1677 * Purpose: For each reportable target sequence, output a tabular summary
1678 * of each hit followed by its alignment.
1679 *
1680 * Similar to <cm_tophits_Targets()>; see additional notes there.
1681 */
1682 int
1683 cm_tophits_HitAlignments(FILE *ofp, CM_TOPHITS *th, CM_PIPELINE *pli, int textw)
1684 {
1685 int status;
1686 int h, i, nprinted;
1687 int namew, descw, rankw;
1688 char *showname;
1689 char *rankstr = NULL;
1690 char *cur_rankstr = NULL;
1691
1692 /* next 4 characters indicate whether alignment ends internal to model/sequence
1693 * and if it is in a truncated alignment mode. */
1694 char lmod;
1695 char rmod;
1696 char lseq;
1697 char rseq;
1698
1699 rankw = ESL_MAX(4, (integer_textwidth(th->nreported)+2));
1700 ESL_ALLOC(rankstr, sizeof(char) * (rankw+1));
1701 ESL_ALLOC(cur_rankstr, sizeof(char) * (rankw+1));
1702 for(i = 0; i < rankw; i++) { rankstr[i] = '-'; } rankstr[rankw] = '\0';
1703 cur_rankstr[rankw] = '\0';
1704
1705 fprintf(ofp, "Hit alignments:\n");
1706
1707 nprinted = 0;
1708 for (h = 0; h < th->N; h++) {
1709 if (th->hit[h]->flags & CM_HIT_IS_REPORTED) {
1710 if (pli->show_accessions && th->hit[h]->acc != NULL && th->hit[h]->acc[0] != '\0') {
1711 showname = th->hit[h]->acc;
1712 namew = strlen(th->hit[h]->acc);
1713 }
1714 else {
1715 showname = th->hit[h]->name;
1716 namew = strlen(th->hit[h]->name);
1717 }
1718
1719 if (textw > 0) {
1720 descw = ESL_MAX(32, textw - namew - 5);
1721 fprintf(ofp, ">> %s %-.*s\n", showname, descw, (th->hit[h]->desc == NULL ? "" : th->hit[h]->desc));
1722 }
1723 else {
1724 fprintf(ofp, ">> %s %s\n", showname, (th->hit[h]->desc == NULL ? "" : th->hit[h]->desc));
1725 }
1726
1727 /* The hit info display is 97+rankw char wide:, where rankw is the maximum of 4 and 2 plus the number of digits in th->N.
1728 * If (pli->be_verbose), the width grows by 58 chars.
1729 *
1730 * rank E-value score bias mdl mdl from mdl to seq from seq to acc trunc gc| bands tau mx Mb seconds pass cfg mdllen seqlen
1731 * ---- --------- ------ ----- --- -------- -------- ----------- ----------- ---- ----- ----|------ ------- ------- ------- ---- --- ------ -----------
1732 * (1) ! 6.8e-9 123.4 0.3 cm 3 72 [] 412 492 + .. 0.98 no 0.48| hmm 5e-6 1.30 0.04 1 loc 123456 12345678901
1733 * (12) ? 1.8e-3 123.4 12.7 hmm 1 72 [] 180 103 - .. 0.90 no 0.60| hmm 0.01 0.65 2.23 3 glb 71 1000000
1734 * rankw 1 123456789 123456 12345 123 12345678 12345678 12 12345678901 12345678901 1 12 1234 12345 1234| 12345 1234567 1234567 1234567 1234 123 123456 12345789012
1735 * 0 1 2 3 4 5 6 7 8 9 | 10 11 12 13 14 15
1736 * 01234567890123456789012345678901234567890123456789012345678901234567890123456789012345789012345678901234567890123456789012345678901234567890123456789012345
1737 * |-> only shown if pli->be_verbose
1738 * In rare cases, when CYK alignment is chosen or when computing
1739 * posteriors is not feasible in allowable memory, the "acc"
1740 * column will be replaced by a "cyksc" colum which is 6
1741 * characters wide instead of 4.
1742 */
1743
1744 fprintf(ofp, " %*s %1s %9s %6s %5s %-3s %8s %8s %2s %11s %11s %1s %2s", rankw, "rank", "", "E-value", "score", "bias", "mdl", "mdl from", "mdl to", "", "seq from", "seq to", "", "");
1745 if(th->hit[h]->ad->ppline) { fprintf(ofp, " %4s %5s %4s", "acc", "trunc", "gc"); }
1746 else { fprintf(ofp, " %6s %5s %4s", "cyksc", "trunc", "gc"); }
1747 if(pli->be_verbose) { fprintf(ofp, " %5s %7s %7s %7s %4s %3s %6s %11s", "bands", "tau", "mx Mb", "seconds", "pass", "cfg", "mdllen", "seqlen"); }
1748 fprintf(ofp, "\n");
1749
1750 fprintf(ofp, " %*s %1s %9s %6s %5s %-3s %8s %8s %2s %11s %11s %1s %2s", rankw, rankstr, "", "---------", "------", "-----", "---", "--------", "--------", "", "-----------", "-----------", "", "");
1751 if(th->hit[h]->ad->ppline) { fprintf(ofp, " %4s %5s %4s", "----", "-----", "----"); }
1752 else { fprintf(ofp, " %6s %5s %4s", "------", "-----", "----"); }
1753 if(pli->be_verbose) { fprintf(ofp, " %5s %7s %7s %7s %4s %3s %6s %11s", "-----", "-------", "-------", "-------", "----", "---", "------", "-----------"); }
1754 fprintf(ofp, "\n");
1755
1756 if(cm_alidisplay_Is5PTrunc(th->hit[h]->ad)) { /* 5' truncated */
1757 lmod = '~';
1758 lseq = '~';
1759 }
1760 else { /* not 5' truncated */
1761 lmod = th->hit[h]->ad->cfrom_emit == 1 ? '[' : '.';
1762 if(th->hit[h]->in_rc) { lseq = th->hit[h]->ad->sqfrom == th->hit[h]->srcL ? '[' : '.'; }
1763 else { lseq = th->hit[h]->ad->sqfrom == 1 ? '[' : '.'; }
1764 }
1765 if(cm_alidisplay_Is3PTrunc(th->hit[h]->ad)) { /* 3' truncated */
1766 rmod = '~';
1767 rseq = '~';
1768 }
1769 else { /* not 3' truncated */
1770 rmod = th->hit[h]->ad->cto_emit == th->hit[h]->ad->clen ? ']' : '.';
1771 if(th->hit[h]->in_rc) { rseq = th->hit[h]->ad->sqto == 1 ? ']' : '.'; }
1772 else { rseq = th->hit[h]->ad->sqto == th->hit[h]->srcL ? ']' : '.'; }
1773 }
1774
1775 snprintf(cur_rankstr, rankw+1, "(%d)", nprinted+1); /* +1 is for null termination char */
1776
1777 fprintf(ofp, " %*s %c %9.2g %6.1f %5.1f %3s %8d %8d %c%c %11" PRId64 " %11" PRId64 " %c %c%c",
1778 rankw, cur_rankstr,
1779 (th->hit[h]->flags & CM_HIT_IS_INCLUDED ? '!' : '?'),
1780 th->hit[h]->evalue,
1781 th->hit[h]->score,
1782 th->hit[h]->bias,
1783 th->hit[h]->hmmonly ? "hmm" : "cm",
1784 th->hit[h]->ad->cfrom_emit,
1785 th->hit[h]->ad->cto_emit,
1786 lmod, rmod,
1787 th->hit[h]->start,
1788 th->hit[h]->stop,
1789 (th->hit[h]->in_rc == TRUE) ? '-' : '+',
1790 lseq, rseq);
1791 if(th->hit[h]->ad->ppline) { fprintf(ofp, " %4.2f %5s %4.2f", th->hit[h]->ad->avgpp, cm_alidisplay_TruncString(th->hit[h]->ad), th->hit[h]->ad->gc); }
1792 else { fprintf(ofp, " %6.1f %5s %4.2f", th->hit[h]->ad->sc, cm_alidisplay_TruncString(th->hit[h]->ad), th->hit[h]->ad->gc); }
1793 if(pli->be_verbose) {
1794 if(th->hit[h]->ad->tau > -0.5) { /* tau is -1. if aln did not use HMM bands */
1795 fprintf(ofp, " %5s %7.2g", "hmm", th->hit[h]->ad->tau);
1796 }
1797 else {
1798 fprintf(ofp, " %5s %7s", (pli->final_cm_search_opts & CM_SEARCH_QDB) ? "qdb" : "no", "-");
1799 }
1800 fprintf(ofp, " %7.2f %7.2f %4d %3s %6d %11" PRId64 "",
1801 th->hit[h]->ad->matrix_Mb,
1802 th->hit[h]->ad->elapsed_secs,
1803 th->hit[h]->pass_idx,
1804 th->hit[h]->glocal ? "glb" : "loc",
1805 th->hit[h]->ad->clen,
1806 th->hit[h]->srcL);
1807 }
1808 fputs("\n\n", ofp);
1809
1810 /*cm_alidisplay_Dump(ofp, th->hit[h]->ad);*/
1811 cm_alidisplay_Print(ofp, th->hit[h]->ad, 40, textw, pli->show_accessions);
1812 fputs("\n", ofp);
1813 nprinted++;
1814 }
1815 }
1816 if (th->nreported == 0) {
1817 fprintf(ofp, "\n [No hits detected that satisfy reporting thresholds]\n");
1818 }
1819
1820 if(rankstr != NULL) free(rankstr);
1821 if(cur_rankstr != NULL) free(cur_rankstr);
1822 return eslOK;
1823
1824 ERROR:
1825 if(rankstr != NULL) free(rankstr);
1826 if(cur_rankstr != NULL) free(cur_rankstr);
1827 return status;
1828
1829 }
1830
1831 /* Function: cm_tophits_HitAlignmentStatistics()
1832 * Synopsis: Final alignment statistics output from a list of hits.
1833 * Incept: EPN, Thu Jun 16 04:22:11 2011
1834 *
1835 * Purpose: Print summary statistics on alignments performed
1836 * for all hits in tophits object <th> to stream <ofp>.
1837 * Statistics are compiled and reported for all hits,
1838 * as well as for reported, included and removed duplicate
1839 * hits independently.
1840 *
1841 * Returns: <eslOK> on success.
1842 */
1843 int
1844 cm_tophits_HitAlignmentStatistics(FILE *ofp, CM_TOPHITS *th, int used_hb, int used_cyk, double default_tau)
1845 {
1846 uint64_t h;
1847 int is_reported;
1848 int is_included;
1849 int is_removed_duplicate;
1850 /*int is_marked_duplicate;*/ /* not used, currently */
1851
1852 /* variables for alignments for all hits */
1853 int64_t all_naln = 0; /* total number alignments */
1854 int64_t all_noverflow_hb = 0; /* # HMM banded alns PPs couldn't be calc'ed for due to mx size */
1855 int64_t all_ntau_mod_hb = 0; /* # HMM banded alns for which was increased due to mx size */
1856 double all_tot_matrix_Mb = 0.; /* summed Mb req'd for matrices used for alignments */
1857 double all_min_matrix_Mb = eslINFINITY; /* min Mb req'd over all alns */
1858 double all_max_matrix_Mb = 0.; /* max Mb req'd over all alns */
1859 double all_avg_matrix_Mb = 0.; /* avg Mb req'd over all alns */
1860 double all_tot_elapsed_secs = 0.; /* summed seconds for alignments */
1861 double all_min_elapsed_secs = eslINFINITY; /* min seconds over all alns */
1862 double all_max_elapsed_secs = 0.; /* max seconds over all alns */
1863 double all_avg_elapsed_secs = 0.; /* avg seconds over all alns */
1864
1865 /* variables for alignments for reported hits */
1866 int64_t rep_naln = 0; /* total number alignments */
1867 int64_t rep_noverflow_hb = 0; /* # HMM banded alns PPs couldn't be calc'ed for due to mx size */
1868 int64_t rep_ntau_mod_hb = 0; /* # HMM banded alns for which was increased due to mx size */
1869 double rep_tot_matrix_Mb = 0.; /* summed Mb req'd for matrices used for alignments */
1870 double rep_min_matrix_Mb = eslINFINITY; /* min Mb req'd over all alns */
1871 double rep_max_matrix_Mb = 0.; /* max Mb req'd over all alns */
1872 double rep_avg_matrix_Mb = 0.; /* avg Mb req'd over all alns */
1873 double rep_tot_elapsed_secs = 0.; /* summed seconds for alignments */
1874 double rep_min_elapsed_secs = eslINFINITY; /* min seconds over all alns */
1875 double rep_max_elapsed_secs = 0.; /* max seconds over all alns */
1876 double rep_avg_elapsed_secs = 0.; /* avg seconds over all alns */
1877
1878 /* variables for alignments for included hits */
1879 int64_t inc_naln = 0; /* total number alignments */
1880 int64_t inc_noverflow_hb = 0; /* # HMM banded alns PPs couldn't be calc'ed for due to mx size */
1881 int64_t inc_ntau_mod_hb = 0; /* # HMM banded alns for which was increased due to mx size */
1882 double inc_tot_matrix_Mb = 0.; /* summed Mb req'd for matrices used for alignments */
1883 double inc_min_matrix_Mb = eslINFINITY; /* min Mb req'd over all alns */
1884 double inc_max_matrix_Mb = 0.; /* max Mb req'd over all alns */
1885 double inc_avg_matrix_Mb = 0.; /* avg Mb req'd over all alns */
1886 double inc_tot_elapsed_secs = 0.; /* summed seconds for alignments */
1887 double inc_min_elapsed_secs = eslINFINITY; /* min seconds over all alns */
1888 double inc_max_elapsed_secs = 0.; /* max seconds over all alns */
1889 double inc_avg_elapsed_secs = 0.; /* avg seconds over all alns */
1890
1891 /* variables for alignments for removed duplicate hits */
1892 int64_t dup_naln = 0; /* total number alignments */
1893 int64_t dup_noverflow_hb = 0; /* # HMM banded alns PPs couldn't be calc'ed for due to mx size */
1894 int64_t dup_ntau_mod_hb = 0; /* # HMM banded alns for which was increased due to mx size */
1895 double dup_tot_matrix_Mb = 0.; /* summed Mb req'd for matrices used for alignments */
1896 double dup_min_matrix_Mb = eslINFINITY; /* min Mb req'd over all alns */
1897 double dup_max_matrix_Mb = 0.; /* max Mb req'd over all alns */
1898 double dup_avg_matrix_Mb = 0.; /* avg Mb req'd over all alns */
1899 double dup_tot_elapsed_secs = 0.; /* summed seconds for alignments */
1900 double dup_min_elapsed_secs = eslINFINITY; /* min seconds over all alns */
1901 double dup_max_elapsed_secs = 0.; /* max seconds over all alns */
1902 double dup_avg_elapsed_secs = 0.; /* avg seconds over all alns */
1903
1904 /* variables for alignments for hmm only hits */
1905 int64_t hmmonly_naln = 0; /* total number alignments */
1906
1907 for(h = 0; h < th->N; h++) {
1908 if(th->unsrt[h].ad->hmmonly) {
1909 hmmonly_naln++;
1910 /* we don't have stats on time, Mb used, so we can't summarize them */
1911 }
1912 else {
1913 is_reported = (th->unsrt[h].flags & CM_HIT_IS_REPORTED) ? TRUE : FALSE;
1914 is_included = (th->unsrt[h].flags & CM_HIT_IS_INCLUDED) ? TRUE : FALSE;
1915 is_removed_duplicate = (th->unsrt[h].flags & CM_HIT_IS_REMOVED_DUPLICATE) ? TRUE : FALSE;
1916 /*is_marked_duplicate = (th->unsrt[h].flags & CM_HIT_IS_MARKED_OVERLAP) ? TRUE : FALSE;*/
1917 if(th->unsrt[h].ad != NULL) {
1918 all_naln++;
1919 all_tot_matrix_Mb += th->unsrt[h].ad->matrix_Mb;
1920 all_tot_elapsed_secs += th->unsrt[h].ad->elapsed_secs;
1921 all_min_matrix_Mb = ESL_MIN(all_min_matrix_Mb, th->unsrt[h].ad->matrix_Mb);
1922 all_max_matrix_Mb = ESL_MAX(all_max_matrix_Mb, th->unsrt[h].ad->matrix_Mb);
1923 all_min_elapsed_secs = ESL_MIN(all_min_elapsed_secs, th->unsrt[h].ad->elapsed_secs);
1924 all_max_elapsed_secs = ESL_MAX(all_max_elapsed_secs, th->unsrt[h].ad->elapsed_secs);
1925 if(used_hb) {
1926 if(fabs(default_tau - th->unsrt[h].ad->tau) > eslSMALLX1) all_ntau_mod_hb++;
1927 }
1928 if(used_hb && (! used_cyk)) {
1929 if(th->unsrt[h].ad->ppline == NULL) all_noverflow_hb++;
1930 }
1931
1932 /* update reported stats */
1933 if(is_reported) {
1934 rep_naln++;
1935 rep_tot_matrix_Mb += th->unsrt[h].ad->matrix_Mb;
1936 rep_tot_elapsed_secs += th->unsrt[h].ad->elapsed_secs;
1937 rep_min_matrix_Mb = ESL_MIN(rep_min_matrix_Mb, th->unsrt[h].ad->matrix_Mb);
1938 rep_max_matrix_Mb = ESL_MAX(rep_max_matrix_Mb, th->unsrt[h].ad->matrix_Mb);
1939 rep_min_elapsed_secs = ESL_MIN(rep_min_elapsed_secs, th->unsrt[h].ad->elapsed_secs);
1940 rep_max_elapsed_secs = ESL_MAX(rep_max_elapsed_secs, th->unsrt[h].ad->elapsed_secs);
1941 if(used_hb && (! th->unsrt[h].ad->hmmonly)) {
1942 if(fabs(default_tau - th->unsrt[h].ad->tau) > eslSMALLX1) rep_ntau_mod_hb++;
1943 }
1944 if(used_hb && (! used_cyk)) {
1945 if(th->unsrt[h].ad->ppline == NULL) rep_noverflow_hb++;
1946 }
1947 }
1948
1949 /* update included stats */
1950 if(is_included) {
1951 inc_naln++;
1952 inc_tot_matrix_Mb += th->unsrt[h].ad->matrix_Mb;
1953 inc_tot_elapsed_secs += th->unsrt[h].ad->elapsed_secs;
1954 inc_min_matrix_Mb = ESL_MIN(inc_min_matrix_Mb, th->unsrt[h].ad->matrix_Mb);
1955 inc_max_matrix_Mb = ESL_MAX(inc_max_matrix_Mb, th->unsrt[h].ad->matrix_Mb);
1956 inc_min_elapsed_secs = ESL_MIN(inc_min_elapsed_secs, th->unsrt[h].ad->elapsed_secs);
1957 inc_max_elapsed_secs = ESL_MAX(inc_max_elapsed_secs, th->unsrt[h].ad->elapsed_secs);
1958 if(used_hb && (! th->unsrt[h].ad->hmmonly)) {
1959 if(fabs(default_tau - th->unsrt[h].ad->tau) > eslSMALLX1) inc_ntau_mod_hb++;
1960 }
1961 if(used_hb && (! used_cyk)) {
1962 if(th->unsrt[h].ad->ppline == NULL) inc_noverflow_hb++;
1963 }
1964 }
1965
1966 /* update removed duplicate stats */
1967 if(is_removed_duplicate) {
1968 dup_naln++;
1969 dup_tot_matrix_Mb += th->unsrt[h].ad->matrix_Mb;
1970 dup_tot_elapsed_secs += th->unsrt[h].ad->elapsed_secs;
1971 dup_min_matrix_Mb = ESL_MIN(dup_min_matrix_Mb, th->unsrt[h].ad->matrix_Mb);
1972 dup_max_matrix_Mb = ESL_MAX(dup_max_matrix_Mb, th->unsrt[h].ad->matrix_Mb);
1973 dup_min_elapsed_secs = ESL_MIN(dup_min_elapsed_secs, th->unsrt[h].ad->elapsed_secs);
1974 dup_max_elapsed_secs = ESL_MAX(dup_max_elapsed_secs, th->unsrt[h].ad->elapsed_secs);
1975 if(used_hb) {
1976 if(fabs(default_tau - th->unsrt[h].ad->tau) > eslSMALLX1) dup_ntau_mod_hb++;
1977 }
1978 if(used_hb && (! used_cyk)) {
1979 if(th->unsrt[h].ad->ppline == NULL) dup_noverflow_hb++;
1980 }
1981 }
1982 }
1983 }
1984 }
1985 /* Output */
1986 if(all_naln > 0) {
1987 all_avg_matrix_Mb = all_tot_matrix_Mb / (double) all_naln;
1988 all_avg_elapsed_secs = all_tot_elapsed_secs / (double) all_naln;
1989 }
1990 if(rep_naln > 0) {
1991 rep_avg_matrix_Mb = rep_tot_matrix_Mb / (double) rep_naln;
1992 rep_avg_elapsed_secs = rep_tot_elapsed_secs / (double) rep_naln;
1993 }
1994 if(inc_naln > 0) {
1995 inc_avg_matrix_Mb = inc_tot_matrix_Mb / (double) inc_naln;
1996 inc_avg_elapsed_secs = inc_tot_elapsed_secs / (double) inc_naln;
1997 }
1998 if(dup_naln > 0) {
1999 dup_avg_matrix_Mb = dup_tot_matrix_Mb / (double) dup_naln;
2000 dup_avg_elapsed_secs = dup_tot_elapsed_secs / (double) dup_naln;
2001 }
2002
2003 fprintf(ofp, "Hit alignment statistics summary:\n");
2004 fprintf(ofp, "---------------------------------\n");
2005 if(all_naln > 0 || hmmonly_naln > 0) {
2006 fprintf(ofp, "%21s %9s %25s %34s\n", "", "", " matrix size (Mb) ", " alignment time (secs) ");
2007 fprintf(ofp, "%21s %9s %25s %34s\n", "", "", "-------------------------", "----------------------------------");
2008 fprintf(ofp, "%-21s %9s %7s %7s %7s %7s %7s %7s %7s", "category", "# alns", "minimum", "average", "maximum", "minimum", "average", "maximum", "total");
2009 if(used_hb) {
2010 fprintf(ofp, " %7s", "ntaumod");
2011 if(! used_cyk) {
2012 fprintf(ofp, " %7s", "novrflw");
2013 }
2014 }
2015 fprintf(ofp, "\n");
2016 fprintf(ofp, "%21s %9s %7s %7s %7s %7s %7s %7s %7s", "---------------------", "---------", "-------", "-------", "-------", "-------", "-------", "-------", "-------");
2017 if(used_hb) {
2018 fprintf(ofp, " %7s", "-------");
2019 if(! used_cyk) {
2020 fprintf(ofp, " %7s", "-------");
2021 }
2022 }
2023 fprintf(ofp, "\n");
2024
2025 /* reported */
2026 if(rep_naln > 0) {
2027 fprintf(ofp, "%-21s %9" PRId64 " %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f", "reported",
2028 rep_naln, rep_min_matrix_Mb, rep_avg_matrix_Mb, rep_max_matrix_Mb,
2029 rep_min_elapsed_secs, rep_avg_elapsed_secs, rep_max_elapsed_secs, rep_tot_elapsed_secs);
2030 if(used_hb) {
2031 fprintf(ofp, " %7" PRId64 "", rep_ntau_mod_hb);
2032 if(! used_cyk) {
2033 fprintf(ofp, " %7" PRId64 "", rep_noverflow_hb);
2034 }
2035 }
2036 }
2037 else {
2038 fprintf(ofp, "%-21s %9" PRId64 " %7s %7s %7s %7s %7s %7s %7s", "reported",
2039 rep_naln, "-", "-", "-", "-", "-", "-", "-");
2040 if(used_hb) {
2041 fprintf(ofp, " %7s", "-");
2042 if(! used_cyk) {
2043 fprintf(ofp, " %7s", "-");
2044 }
2045 }
2046 }
2047 fprintf(ofp, "\n");
2048
2049 /* included */
2050 if(inc_naln > 0) {
2051 fprintf(ofp, "%-21s %9" PRId64 " %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f", "included",
2052 inc_naln, inc_min_matrix_Mb, inc_avg_matrix_Mb, inc_max_matrix_Mb,
2053 inc_min_elapsed_secs, inc_avg_elapsed_secs, inc_max_elapsed_secs, inc_tot_elapsed_secs);
2054 if(used_hb) {
2055 fprintf(ofp, " %7" PRId64 "", inc_ntau_mod_hb);
2056 if(! used_cyk) {
2057 fprintf(ofp, " %7" PRId64 "", inc_noverflow_hb);
2058 }
2059 }
2060 }
2061 else {
2062 fprintf(ofp, "%-21s %9" PRId64 " %7s %7s %7s %7s %7s %7s %7s", "included",
2063 inc_naln, "-", "-", "-", "-", "-", "-", "-");
2064 if(used_hb) {
2065 fprintf(ofp, " %7s", "-");
2066 if(! used_cyk) {
2067 fprintf(ofp, " %7s", "-");
2068 }
2069 }
2070 }
2071 fprintf(ofp, "\n");
2072
2073 /* removed duplicates */
2074 if(dup_naln > 0) {
2075 fprintf(ofp, "%-21s %9" PRId64 " %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f", "removed duplicates",
2076 dup_naln, dup_min_matrix_Mb, dup_avg_matrix_Mb, dup_max_matrix_Mb,
2077 dup_min_elapsed_secs, dup_avg_elapsed_secs, dup_max_elapsed_secs, dup_tot_elapsed_secs);
2078 if(used_hb) {
2079 fprintf(ofp, " %7" PRId64 "", dup_ntau_mod_hb);
2080 if(! used_cyk) {
2081 fprintf(ofp, " %7" PRId64 "", dup_noverflow_hb);
2082 }
2083 }
2084 }
2085 else {
2086 fprintf(ofp, "%-21s %9" PRId64 " %7s %7s %7s %7s %7s %7s %7s", "removed duplicates",
2087 dup_naln, "-", "-", "-", "-", "-", "-", "-");
2088 if(used_hb) {
2089 fprintf(ofp, " %7s", "-");
2090 if(! used_cyk) {
2091 fprintf(ofp, " %7s", "-");
2092 }
2093 }
2094 }
2095 fprintf(ofp, "\n");
2096
2097 /* all except hmm only */
2098 if(all_naln > 0) {
2099 fprintf(ofp, "%-21s %9" PRId64 " %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f", "all (except HMM only)",
2100 all_naln, all_min_matrix_Mb, all_avg_matrix_Mb, all_max_matrix_Mb,
2101 all_min_elapsed_secs, all_avg_elapsed_secs, all_max_elapsed_secs, all_tot_elapsed_secs);
2102 if(used_hb) {
2103 fprintf(ofp, " %7" PRId64 "", all_ntau_mod_hb);
2104 if(! used_cyk) {
2105 fprintf(ofp, " %7" PRId64 "", all_noverflow_hb);
2106 }
2107 }
2108 }
2109 else {
2110 fprintf(ofp, "%-21s %9" PRId64 " %7s %7s %7s %7s %7s %7s %7s", "all (except HMM only)",
2111 all_naln, "-", "-", "-", "-", "-", "-", "-");
2112 if(used_hb) {
2113 fprintf(ofp, " %7s", "-");
2114 if(! used_cyk) {
2115 fprintf(ofp, " %7s", "-");
2116 }
2117 }
2118 }
2119 fprintf(ofp, "\n");
2120
2121 /* hmm only */
2122 if(hmmonly_naln > 0) {
2123 fprintf(ofp, "%-21s %9" PRId64 " %7s %7s %7s %7s %7s %7s %7s", "HMM only",
2124 hmmonly_naln, "?", "?", "?", "?", "?", "?", "?");
2125 }
2126 else {
2127 fprintf(ofp, "%-21s %9" PRId64 " %7s %7s %7s %7s %7s %7s %7s", "HMM only",
2128 hmmonly_naln, "-", "-", "-", "-", "-", "-", "-");
2129 }
2130 if(used_hb) {
2131 fprintf(ofp, " %7s", "-");
2132 if(! used_cyk) {
2133 fprintf(ofp, " %7s", "-");
2134 }
2135 }
2136 fprintf(ofp, "\n");
2137 }
2138 else {
2139 fprintf(ofp, "\n [No hits detected]\n");
2140 }
2141 return eslOK;
2142 }
2143
2144 /* Function: cm_tophits_Alignment()
2145 * Incept: EPN, Wed Mar 21 16:21:21 2012
2146 * Synopsis: Create a multiple alignment of all the included hits.
2147 *
2148 * Purpose: Create a multiple alignment of all hits marked
2149 * "includable" in the top hits list <th>, and return it in
2150 * <*ret_msa>.
2151 *
2152 * Returns: <eslOK> on success, if any hits were aligned then
2153 * <*ret_msa> points to a new MSA that the caller is
2154 * responsible for freeing, else if there are no
2155 * reported hits that satisfy inclusion thresholds,
2156 * <eslOK> is still returned but ret_msa is <NULL>.
2157 *
2158 * Throws: <eslEINVAL> on contract violation. <eslEMEM> on
2159 * allocation failure; <eslECORRUPT> on unexpected internal
2160 * data corruption. Potentially other non-eslOK
2161 * values. <errbuf> is filled in all cases.
2162 */
2163 int
2164 cm_tophits_Alignment(CM_t *cm, const CM_TOPHITS *th, char *errbuf, int allow_trunc, ESL_MSA **ret_msa)
2165 {
2166 ESL_SQ **sqarr = NULL; /* [0..ninc-1] array of sequences, one for each hit */
2167 Parsetree_t **trarr = NULL; /* [0..ninc-1] array of parsetrees, one for each hit */
2168 char **pparr = NULL; /* [0..ninc-1] array of pp strings, one for each hit */
2169 ESL_MSA *msa = NULL;
2170 int ninc = 0;
2171 int h, i, y;
2172 int status;
2173
2174 if(cm->cmcons == NULL) ESL_FAIL(eslEINVAL, errbuf, "cm_tophits_Alignment(): cm->cmcons is NULL");
2175
2176 /* How many hits will be included in the new alignment?
2177 */
2178 for (h = 0; h < th->N; h++) {
2179 if (th->hit[h]->flags & CM_HIT_IS_INCLUDED) {
2180 ninc++;
2181 }
2182 }
2183
2184 if (ninc == 0) { status = eslOK; goto ERROR; /* caller will know that no msa was built b/c ret_msa will be NULL */ }
2185
2186 /* Allocation */
2187 ESL_ALLOC(sqarr, sizeof(ESL_SQ *) * (ninc));
2188 ESL_ALLOC(trarr, sizeof(Parsetree_t *) * (ninc));
2189 ESL_ALLOC(pparr, sizeof(char *) * (ninc));
2190 for (i = 0; i < ninc; i++) sqarr[i] = NULL;
2191 for (i = 0; i < ninc; i++) trarr[i] = NULL;
2192 for (i = 0; i < ninc; i++) pparr[i] = NULL;
2193
2194 /* Make faux sequences, parsetrees from hit list */
2195 y = 0;
2196 for (h = 0; h < th->N; h++) {
2197 if (th->hit[h]->flags & CM_HIT_IS_INCLUDED) {
2198 if ((status = cm_alidisplay_Backconvert(cm, th->hit[h]->ad, errbuf, &(sqarr[y]), &(trarr[y]), &(pparr[y]))) != eslOK) goto ERROR;
2199 y++;
2200 }
2201 }
2202
2203 /* create the alignment */
2204 if((status = Parsetrees2Alignment(cm, errbuf, cm->abc, sqarr, NULL, trarr, pparr, ninc, NULL, NULL,
2205 /*do_full=*/TRUE, /*do_matchonly=*/FALSE, /*allow_trunc=*/allow_trunc, &msa)) != eslOK) goto ERROR;
2206
2207 /* Clean up */
2208 for (y = 0; y < ninc; y++) esl_sq_Destroy(sqarr[y]);
2209 for (y = 0; y < ninc; y++) FreeParsetree(trarr[y]);
2210 for (y = 0; y < ninc; y++) free(pparr[y]);
2211 free(sqarr);
2212 free(trarr);
2213 free(pparr);
2214
2215 *ret_msa = msa;
2216 return eslOK;
2217
2218 ERROR:
2219 if (sqarr != NULL) { for (y = 0; y < ninc; y++) if (sqarr[y] != NULL) esl_sq_Destroy(sqarr[y]); free(sqarr); }
2220 if (trarr != NULL) { for (y = 0; y < ninc; y++) if (trarr[y] != NULL) FreeParsetree(trarr[y]); free(trarr); }
2221 if (pparr != NULL) { for (y = 0; y < ninc; y++) if (pparr[y] != NULL) free(pparr[y]); free(pparr); }
2222 if (msa != NULL) esl_msa_Destroy(msa);
2223 *ret_msa = NULL;
2224 return status;
2225 }
2226 /*---------------- end, standard output format ------------------*/
2227
2228 /*****************************************************************
2229 * 3. Tabular (parsable) output of pipeline results.
2230 *****************************************************************/
2231
2232 /* Function: cm_tophits_TabularTargets1()
2233 * Synopsis: Output parsable table of per-sequence hits in format '1'.
2234 * Incept: EPN, Tue May 24 14:24:06 2011
2235 * SRE, Wed Mar 18 15:26:17 2009 [Janelia] (p7_tophits.c)
2236 *
2237 * Purpose: Output a parseable table of reportable per-sequence hits
2238 * in sorted tophits list <th> in an easily parsed ASCII
2239 * tabular form to stream <ofp>, using final pipeline
2240 * accounting stored in <pli>. Format #1 (only format
2241 * output by 1.1rc1->1.1.1).
2242 *
2243 * Designed to be concatenated for multiple queries and
2244 * multiple top hits list.
2245 *
2246 * Returns: <eslOK> on success.
2247 */
2248 int
2249 cm_tophits_TabularTargets1(FILE *ofp, char *qname, char *qacc, CM_TOPHITS *th, CM_PIPELINE *pli, int show_header)
2250 {
2251 int status;
2252 int i;
2253 int tnamew = ESL_MAX(20, cm_tophits_GetMaxNameLength(th));
2254 int qnamew = ESL_MAX(20, strlen(qname));
2255 int qaccw = ((qacc != NULL) ? ESL_MAX(9, strlen(qacc)) : 9);
2256 int taccw = ESL_MAX(9, cm_tophits_GetMaxAccessionLength(th));
2257 int posw = ESL_MAX(8, cm_tophits_GetMaxPositionLength(th));
2258
2259 char *qnamestr = NULL;
2260 char *tnamestr = NULL;
2261 char *qaccstr = NULL;
2262 char *taccstr = NULL;
2263 char *posstr = NULL;
2264
2265 ESL_ALLOC(tnamestr, sizeof(char) * (tnamew));
2266 ESL_ALLOC(taccstr, sizeof(char) * (taccw+1));
2267 ESL_ALLOC(qnamestr, sizeof(char) * (qnamew+1));
2268 ESL_ALLOC(qaccstr, sizeof(char) * (qaccw+1));
2269 ESL_ALLOC(posstr, sizeof(char) * (posw+1));
2270
2271 for(i = 0; i < tnamew-1; i++) { tnamestr[i] = '-'; } tnamestr[tnamew-1] = '\0'; /* need to account for single '#' */
2272 for(i = 0; i < taccw; i++) { taccstr[i] = '-'; } taccstr[taccw] = '\0';
2273 for(i = 0; i < qnamew; i++) { qnamestr[i] = '-'; } qnamestr[qnamew] = '\0';
2274 for(i = 0; i < qaccw; i++) { qaccstr[i] = '-'; } qaccstr[qaccw] = '\0';
2275 for(i = 0; i < posw; i++) { posstr[i] = '-'; } posstr[posw] = '\0';
2276
2277 int h;
2278
2279 if (show_header) {
2280 fprintf(ofp, "#%-*s %-*s %-*s %-*s %3s %8s %8s %*s %*s %6s %5s %4s %4s %5s %6s %9s %3s %-s\n",
2281 tnamew-1, "target name", taccw, "accession", qnamew, "query name", qaccw, "accession",
2282 "mdl", "mdl from", "mdl to",
2283 posw, "seq from", posw, "seq to", "strand", "trunc", "pass", "gc", "bias", "score", "E-value", "inc", "description of target");
2284 fprintf(ofp, "#%-*s %-*s %-*s %-*s %-3s %-7s %-7s %*s %*s %6s %5s %4s %4s %5s %6s %9s %3s %s\n",
2285 tnamew-1, tnamestr, taccw, taccstr, qnamew, qnamestr, qaccw, qaccstr,
2286 "---", "--------", "--------",
2287 posw, posstr, posw, posstr, "------", "-----", "----", "----", "-----", "------", "---------", "---", "---------------------");
2288 }
2289 for (h = 0; h < th->N; h++) {
2290 if (th->hit[h]->flags & CM_HIT_IS_REPORTED) {
2291 // fprintf(ofp, "%-*s %-*s %-*s %-*s %3s %8d %8d %*" PRId64 " %*" PRId64 " %6s %5s %4d %4.2f %5.1f %6.1f %9.2g %-3s %s\n",
2292 fprintf(ofp, "%-*s %-*s %-*s %-*s %3s %8d %8d %*" PRId64 " %*" PRId64 " %6s %5s %4d %4.2f %5.1f %6.1f %9.2g %-3s %s\n",
2293 tnamew, th->hit[h]->name,
2294 taccw, ((th->hit[h]->acc != NULL && th->hit[h]->acc[0] != '\0') ? th->hit[h]->acc : "-"),
2295 qnamew, qname,
2296 qaccw, ((qacc != NULL && qacc[0] != '\0') ? qacc : "-"),
2297 th->hit[h]->hmmonly ? "hmm" : "cm",
2298 th->hit[h]->ad->cfrom_emit, th->hit[h]->ad->cto_emit,
2299 posw, th->hit[h]->start,
2300 posw, th->hit[h]->stop,
2301 (th->hit[h]->in_rc == TRUE) ? "-" : "+",
2302 cm_alidisplay_TruncString(th->hit[h]->ad),
2303 th->hit[h]->pass_idx,
2304 th->hit[h]->ad->gc,
2305 th->hit[h]->bias,
2306 th->hit[h]->score,
2307 th->hit[h]->evalue,
2308 (th->hit[h]->flags & CM_HIT_IS_INCLUDED ? "!" : "?"),
2309 (th->hit[h]->desc != NULL) ? th->hit[h]->desc : "-");
2310 }
2311 }
2312 if(qnamestr != NULL) free(qnamestr);
2313 if(tnamestr != NULL) free(tnamestr);
2314 if(qaccstr != NULL) free(qaccstr);
2315 if(taccstr != NULL) free(taccstr);
2316 if(posstr != NULL) free(posstr);
2317
2318 return eslOK;
2319
2320 ERROR:
2321 if(qnamestr != NULL) free(qnamestr);
2322 if(tnamestr != NULL) free(tnamestr);
2323 if(qaccstr != NULL) free(qaccstr);
2324 if(taccstr != NULL) free(taccstr);
2325 if(posstr != NULL) free(posstr);
2326
2327 return status;
2328 }
2329
2330 /* Function: cm_tophits_TabularTargets2()
2331 * Synopsis: Output parsable table of per-sequence hits in format '2'.
2332 * Incept: EPN, Thu Dec 18 15:06:58 2014
2333 * EPN, Tue May 24 14:24:06 2011 (cm_tophits_TabularTargets())
2334 * SRE, Wed Mar 18 15:26:17 2009 [Janelia] (p7_tophits.c)
2335 *
2336 * Purpose: Output a parseable table of reportable per-sequence hits
2337 * in sorted tophits list <th> in an easily parsed ASCII
2338 * tabular form to stream <ofp>, using final pipeline
2339 * accounting stored in <pli>. Format #2.
2340 *
2341 * Format #2 differs from format 1 (cm_tophits_TabularTargets1(),
2342 * the only tabular format available in Infernal 1.1rc1-->1.1.1)
2343 * in that overlap information is output here (whether each hit
2344 * has a higher scoring overlap and the index of the overlap
2345 * as well as fractional overlap), as well as clan information
2346 * if it's available.
2347 *
2348 * Designed to be concatenated for multiple queries and
2349 * multiple top hits list.
2350 *
2351 * Returns: <eslOK> on success.
2352 */
2353 int
2354 cm_tophits_TabularTargets2(FILE *ofp, char *qname, char *qacc, CM_TOPHITS *th, CM_PIPELINE *pli, int show_header, ESL_KEYHASH *clan_name_kh, int skip_overlaps, char *errbuf)
2355 {
2356 int status;
2357 int i;
2358 int tnamew = ESL_MAX(20, cm_tophits_GetMaxNameLength(th));
2359 int qnamew = ESL_MAX(20, strlen(qname));
2360 int qaccw = ((qacc != NULL) ? ESL_MAX(9, strlen(qacc)) : 9);
2361 int taccw = ESL_MAX(9, cm_tophits_GetMaxAccessionLength(th));
2362 int posw = ESL_MAX(8, cm_tophits_GetMaxPositionLength(th));
2363 int idxw1 = ESL_MAX(4, integer_textwidth(th->N));
2364 int idxw2 = ESL_MAX(6, integer_textwidth(th->N));
2365 int clanw = ESL_MAX(9, cm_tophits_GetMaxClanLength(th, clan_name_kh));
2366 int clenw = ESL_MAX(7, cm_tophits_GetMaxModelLength(th));
2367 int srcLw = ESL_MAX(7, cm_tophits_GetMaxTargetLength(th));
2368
2369 /* variables used only if pli->do_trm_F3 */
2370 char lseq, rseq;
2371 /* variables used for dealing with overlap annotation */
2372 int h; /* counter over hits */
2373 int64_t as, ws; /* current hit's sorted index for h->any_oidx and h->win_oidx */
2374 int64_t ao, wo; /* current hit's output index for h->any_oidx and h->win_oidx */
2375 int64_t *sorted_idxA = NULL; /* [0..h..th->N-1] index of hit h in <th>, the sorted list of hits we're outputting */
2376 int64_t *output_idxA = NULL; /* [0..h..th->N-1] index of hit h in output list of hits (not all hits are output, some (often many) are overlaps) */
2377 int *has_overlapA = NULL; /* [0..h..th->N-1] 'TRUE' if hit h has any overlapping hits, else 'FALSE' */
2378 int maybe_skip; /* set to TRUE or FALSE depending on whether this hit overlaps with another better
2379 * scoring one, we'll skip a hit for which maybe_skip==TRUE if skip_overlaps is TRUE
2380 */
2381 int64_t noutput = 0; /* keeps track of the number of this we have output thus far */
2382 int64_t nres, len1, len2; /* number of overlapping residues, and lengths of hits */
2383
2384 char *qnamestr = NULL;
2385 char *tnamestr = NULL;
2386 char *qaccstr = NULL;
2387 char *taccstr = NULL;
2388 char *clanstr = NULL;
2389 char *posstr = NULL;
2390 char *idxstr1 = NULL;
2391 char *idxstr2 = NULL;
2392 char *olp_str = NULL;
2393 char *any_oidxstr = NULL;
2394 char *win_oidxstr = NULL;
2395 char *any_ofctstr1 = NULL;
2396 char *any_ofctstr2 = NULL;
2397 char *win_ofctstr1 = NULL;
2398 char *win_ofctstr2 = NULL;
2399 char *clannamestr = NULL;
2400 char *clenstr = NULL;
2401 char *srcLstr = NULL;
2402
2403 if(pli->mode != CM_SCAN_MODELS) { /* we'll only possibly have overlaps if in scan mode */
2404 ESL_XFAIL(eslEINVAL, errbuf, "Trying to output in format 2, but not in SCAN mode");
2405 }
2406
2407 ESL_ALLOC(idxstr1, sizeof(char) * (idxw1));
2408 ESL_ALLOC(idxstr2, sizeof(char) * (idxw2+1));
2409 ESL_ALLOC(tnamestr, sizeof(char) * (tnamew+1));
2410 ESL_ALLOC(taccstr, sizeof(char) * (taccw+1));
2411 ESL_ALLOC(qnamestr, sizeof(char) * (qnamew+1));
2412 ESL_ALLOC(qaccstr, sizeof(char) * (qaccw+1));
2413 ESL_ALLOC(clanstr, sizeof(char) * (clanw+1));
2414 ESL_ALLOC(posstr, sizeof(char) * (posw+1));
2415 ESL_ALLOC(olp_str, sizeof(char) * 4);
2416 ESL_ALLOC(any_oidxstr, sizeof(char) * (idxw2+1)); /* string for hit index of best scoring overlap */
2417 ESL_ALLOC(win_oidxstr, sizeof(char) * (idxw2+1)); /* string for hit index of best scoring overlap that is a 'winner' (has no better scoring overlap) */
2418 ESL_ALLOC(any_ofctstr1, sizeof(char) * 7); /* string for fractional overlap b/t current hit and hit any_oidx wrt current hit */
2419 ESL_ALLOC(any_ofctstr2, sizeof(char) * 7); /* string for fractional overlap b/t current hit and hit any_oidx wrt hit any_oidx */
2420 ESL_ALLOC(win_ofctstr1, sizeof(char) * 7); /* string for fractional overlap b/t current hit and hit win_oidx wrt current hit */
2421 ESL_ALLOC(win_ofctstr2, sizeof(char) * 7); /* string for fractional overlap b/t current hit and hit win_oidx wrt hit win_oidx */
2422 ESL_ALLOC(clannamestr, sizeof(char) * clanw); /* string for output of clan name */
2423 ESL_ALLOC(clenstr, sizeof(char) * (clenw+1));
2424 ESL_ALLOC(srcLstr, sizeof(char) * (srcLw+1));
2425
2426 for(i = 0; i < idxw1-1; i++) { idxstr1[i] = '-'; } idxstr1[idxw1-1] = '\0'; /* need to account for single '#' */
2427 for(i = 0; i < idxw2; i++) { idxstr2[i] = '-'; } idxstr2[idxw2] = '\0';
2428 for(i = 0; i < tnamew; i++) { tnamestr[i] = '-'; } tnamestr[tnamew] = '\0';
2429 for(i = 0; i < taccw; i++) { taccstr[i] = '-'; } taccstr[taccw] = '\0';
2430 for(i = 0; i < qnamew; i++) { qnamestr[i] = '-'; } qnamestr[qnamew] = '\0';
2431 for(i = 0; i < qaccw; i++) { qaccstr[i] = '-'; } qaccstr[qaccw] = '\0';
2432 for(i = 0; i < clanw; i++) { clanstr[i] = '-'; } clanstr[clanw] = '\0';
2433 for(i = 0; i < posw; i++) { posstr[i] = '-'; } posstr[posw] = '\0';
2434 for(i = 0; i < clenw; i++) { clenstr[i] = '-'; } clenstr[clenw] = '\0';
2435 for(i = 0; i < srcLw; i++) { srcLstr[i] = '-'; } srcLstr[srcLw] = '\0';
2436
2437 if(th->N > 0) {
2438 ESL_ALLOC(sorted_idxA, sizeof(int64_t) * th->N);
2439 ESL_ALLOC(output_idxA, sizeof(int64_t) * th->N);
2440 ESL_ALLOC(has_overlapA, sizeof(int) * th->N);
2441 for(h = 0; h < th->N; h++) sorted_idxA[h] = -1;
2442 for(h = 0; h < th->N; h++) output_idxA[h] = -1;
2443 for(h = 0; h < th->N; h++) has_overlapA[h] = FALSE;
2444 /* determine which hits are listed as any_oidx or win_oidx for any other hits */
2445 for(h = 0; h < th->N; h++) {
2446 if(th->hit[h]->any_oidx != -1) has_overlapA[th->hit[h]->any_oidx] = TRUE;
2447 if(th->hit[h]->win_oidx != -1) has_overlapA[th->hit[h]->win_oidx] = TRUE;
2448 sorted_idxA[th->hit[h]->hit_idx] = h; /* save sorted idx */
2449 /* and save the output index we'll use for this hit */
2450 maybe_skip = ((th->hit[h]->flags & CM_HIT_IS_MARKED_OVERLAP) && (th->hit[h]->win_oidx != -1)) ? 1 : 0;
2451 if ((th->hit[h]->flags & CM_HIT_IS_REPORTED) && /* hit is REPORTED */
2452 ((skip_overlaps == FALSE) || (maybe_skip == FALSE))) { /* hit won't be skipped b/c it's an overlap */
2453 noutput++;
2454 output_idxA[h] = noutput; /* save output idx */
2455 }
2456 }
2457 noutput = 0; /* very impt to reset this, so we list first hit as index 1, and not th->N+1 */
2458 }
2459
2460 if (show_header) {
2461 if(pli->do_trm_F3) { /* terminated after F3, more compact output (we don't have all the info for the default output mode) */
2462 fprintf(ofp, "#%-*s %-*s %-*s %-*s %6s %*s %*s %6s %6s %11s %3s %*s %6s %6s %*s %6s %6s\n",
2463 idxw1-1, "idx", tnamew, "target name", qnamew, "query name", clanw, "clan name",
2464 "score",
2465 posw, "seq from", posw, "seq to",
2466 "strand", "bounds", "seqlen",
2467 "olp", idxw2, "anyidx", "afrct1", "afrct2", idxw2, "winidx", "wfrct1", "wfrct2");
2468 fprintf(ofp, "#%-*s %-*s %-*s %-*s %6s %*s %*s %6s %6s %11s %3s %s %s %s %s %s %s\n",
2469 idxw1-1, idxstr1, tnamew, tnamestr, qnamew, qnamestr, clanw, clanstr,
2470 "------",
2471 posw, posstr, posw, posstr, "------", "------", "-----------", "---", idxstr2, "------", "------", idxstr2, "------", "------");
2472 }
2473 else { /* pli->do_trm_F3 is FALSE, default output mode */
2474 fprintf(ofp, "#%-*s %-*s %-*s %-*s %-*s %-*s %3s %8s %8s %*s %*s %6s %5s %4s %4s %5s %6s %9s %3s %3s %*s %6s %6s %*s %6s %6s %*s %*s %-s\n",
2475 idxw1-1, "idx", tnamew, "target name", taccw, "accession", qnamew, "query name", qaccw, "accession", clanw, "clan name",
2476 "mdl", "mdl from", "mdl to",
2477 posw, "seq from", posw, "seq to",
2478 "strand", "trunc", "pass", "gc", "bias", "score", "E-value", "inc",
2479 "olp", idxw2, "anyidx", "afrct1", "afrct2", idxw2, "winidx", "wfrct1", "wfrct2",
2480 clenw, "mdl len", srcLw, "seq len", "description of target");
2481 fprintf(ofp, "#%-*s %-*s %-*s %-*s %-*s %-*s %-3s %-7s %-7s %*s %*s %6s %5s %4s %4s %5s %6s %9s %3s %3s %s %s %s %s %s %s %*s %*s %s\n",
2482 idxw1-1, idxstr1, tnamew, tnamestr, taccw, taccstr, qnamew, qnamestr, qaccw, qaccstr, clanw, clanstr,
2483 "---", "--------", "--------",
2484 posw, posstr, posw, posstr, "------", "-----", "----", "----", "-----", "------", "---------", "---", "---", idxstr2, "------", "------", idxstr2, "------", "------", clenw, clenstr, srcLw, srcLstr, "---------------------");
2485 }
2486 }
2487 for (h = 0; h < th->N; h++) {
2488 /* next complex 'if' statement checks if will we output info on this hit */
2489 maybe_skip = ((th->hit[h]->flags & CM_HIT_IS_MARKED_OVERLAP) && (th->hit[h]->win_oidx != -1)) ? 1 : 0;
2490 if ((th->hit[h]->flags & CM_HIT_IS_REPORTED) && /* hit is REPORTED */
2491 ((skip_overlaps == FALSE) || (maybe_skip == FALSE))) { /* hit won't be skipped b/c it's an overlap */
2492 as = (th->hit[h]->any_oidx == -1) ? -1 : sorted_idxA[th->hit[h]->any_oidx]; /* for convenience */
2493 ws = (th->hit[h]->win_oidx == -1) ? -1 : sorted_idxA[th->hit[h]->win_oidx]; /* for convenience */
2494 ao = (th->hit[h]->any_oidx == -1) ? -1 : output_idxA[sorted_idxA[th->hit[h]->any_oidx]]; /* for convenience */
2495 wo = (th->hit[h]->win_oidx == -1) ? -1 : output_idxA[sorted_idxA[th->hit[h]->win_oidx]]; /* for convenience */
2496
2497 /* format the strings to print for overlap indices and fractions */
2498 if(as != -1) {
2499 snprintf(any_oidxstr, idxw2+1, "%" PRId64, ao); /* +1 for null termination char */
2500 if(th->hit[h]->in_rc) {
2501 if(! th->hit[as]->in_rc) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "hit %d in_rc (%" PRId64 "..%" PRId64 ") but any_oidx (%" PRId64 " %" PRId64 "..%" PRId64 ") is not", h, th->hit[h]->start, th->hit[h]->stop, as, th->hit[as]->start, th->hit[as]->stop);
2502 len1 = th->hit[h]->start - th->hit[h]->stop + 1;
2503 len2 = th->hit[as]->start - th->hit[as]->stop + 1;
2504 status = cm_tophits_OverlapNres(th->hit[h]->stop, th->hit[h]->start, th->hit[as]->stop, th->hit[as]->start, &nres, errbuf);
2505 if(status != eslOK) goto ERROR;
2506 }
2507 else {
2508 if(th->hit[as]->in_rc) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "hit %d not in_rc (%" PRId64 "..%" PRId64 ") but any_oidx (%" PRId64 " %" PRId64 "..%" PRId64 ") is ", h, th->hit[h]->start, th->hit[h]->stop, as, th->hit[as]->start, th->hit[as]->stop);
2509 len1 = th->hit[h]->stop - th->hit[h]->start + 1;
2510 len2 = th->hit[as]->stop - th->hit[as]->start + 1;
2511 status = cm_tophits_OverlapNres(th->hit[h]->start, th->hit[h]->stop, th->hit[as]->start, th->hit[as]->stop, &nres, errbuf);
2512 if(status != eslOK) goto ERROR;
2513 }
2514 snprintf(any_ofctstr1, 7, "%6.3f", (float) nres / (float) len1);
2515 snprintf(any_ofctstr2, 7, "%6.3f", (float) nres / (float) len2);
2516 }
2517 if(ws != -1 && ws != as) { /* only calculate the win_* values if it's not identical to the any_* */
2518 snprintf(win_oidxstr, idxw2+1, "%" PRId64, wo); /* +1 for null termination char */
2519 if(th->hit[h]->in_rc) {
2520 if(! th->hit[ws]->in_rc) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "hit %d in_rc (%" PRId64 "..%" PRId64 ") but win_oidx (%" PRId64 " %" PRId64 "..%" PRId64 ") is not", h, th->hit[h]->start, th->hit[h]->stop, ws, th->hit[ws]->start, th->hit[ws]->stop);
2521 len1 = th->hit[h]->start - th->hit[h]->stop + 1;
2522 len2 = th->hit[ws]->start - th->hit[ws]->stop + 1;
2523 status = cm_tophits_OverlapNres(th->hit[h]->stop, th->hit[h]->start, th->hit[ws]->stop, th->hit[ws]->start, &nres, errbuf);
2524 if(status != eslOK) goto ERROR;
2525 }
2526 else {
2527 if(th->hit[ws]->in_rc) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "hit %d not in_rc (%" PRId64 "..%" PRId64 ") but win_oidx (%" PRId64 " %" PRId64 "..%" PRId64 ") is ", h, th->hit[h]->start, th->hit[h]->stop, ws, th->hit[ws]->start, th->hit[ws]->stop);
2528 len1 = th->hit[h]->stop - th->hit[h]->start + 1;
2529 len2 = th->hit[ws]->stop - th->hit[ws]->start + 1;
2530 status = cm_tophits_OverlapNres(th->hit[h]->start, th->hit[h]->stop, th->hit[ws]->start, th->hit[ws]->stop, &nres, errbuf);
2531 if(status != eslOK) goto ERROR;
2532 }
2533 snprintf(win_ofctstr1, 7, "%6.3f", (float) nres / (float) len1);
2534 snprintf(win_ofctstr2, 7, "%6.3f", (float) nres / (float) len2);
2535 }
2536 // determine overlap string
2537 if(th->hit[h]->flags & CM_HIT_IS_MARKED_OVERLAP) {
2538 if(ws == -1) {
2539 // has >= 1 overlaps but none of these are '^' hits,
2540 // these hits were grouped together with other '=' hits
2541 // in versions 1.1.2 and 1.1.3
2542 snprintf(olp_str, 4, " $ ");
2543 }
2544 else {
2545 // has => 1 overlaps, and >= 1 of them are higher scoring
2546 // and is itself a '^' hit
2547 snprintf(olp_str, 4, " = ");
2548 }
2549 }
2550 else if(has_overlapA[th->hit[h]->hit_idx] == TRUE) {
2551 // has >= 1 overlaps but all are below it in hit list
2552 snprintf(olp_str, 4, " ^ ");
2553 }
2554 else {
2555 // zero overlaps
2556 snprintf(olp_str, 4, " * ");
2557 }
2558
2559 /* make sure the clan name string makes sense */
2560 if(th->hit[h]->clan_idx != -1) {
2561 if(clan_name_kh == NULL) ESL_XFAIL(eslEINVAL, errbuf, "trying to output tabular output of clans, but clan data structure is missing...");
2562 if(th->hit[h]->clan_idx >= esl_keyhash_GetNumber(clan_name_kh)) ESL_XFAIL(eslEINVAL, errbuf, "trying to output tabular output of clans, but clan index is invalid...");
2563 }
2564
2565 if((skip_overlaps == FALSE) || (maybe_skip == FALSE)) { /* if skip_overlaps is TRUE, potentially skip this hit in the tabular output */
2566 noutput++;
2567 if(pli->do_trm_F3) { /* special 'terminate after F3 mode', different output */
2568 if(th->hit[h]->in_rc) {
2569 lseq = th->hit[h]->start == th->hit[h]->srcL ? '[' : '.';
2570 rseq = th->hit[h]->stop == 1 ? ']' : '.';
2571 }
2572 else {
2573 lseq = th->hit[h]->start == 1 ? '[' : '.';
2574 rseq = th->hit[h]->stop == th->hit[h]->srcL ? ']' : '.';
2575 }
2576 fprintf(ofp, "%-*" PRId64 " %-*s %-*s %-*s %6.1f %*" PRId64 " %*" PRId64 " %6s %4s%c%c %11" PRId64 " %3s %*s %6s %6s %*s %6s %6s\n",
2577 idxw1, noutput,
2578 tnamew, th->hit[h]->name,
2579 qnamew, qname,
2580 clanw, (th->hit[h]->clan_idx == -1) ? "-" : esl_keyhash_Get(clan_name_kh, th->hit[h]->clan_idx),
2581 th->hit[h]->score,
2582 posw, th->hit[h]->start,
2583 posw, th->hit[h]->stop,
2584 (th->hit[h]->in_rc == TRUE) ? "-" : "+",
2585 "", lseq, rseq, th->hit[h]->srcL,
2586 olp_str,
2587 idxw2, (as == -1) ? "-" : any_oidxstr,
2588 (as == -1) ? "-" : any_ofctstr1,
2589 (as == -1) ? "-" : any_ofctstr2,
2590 idxw2, (ws == -1 || ws == as) ? ((ws == -1) ? "-" : "\"") : win_oidxstr,
2591 (ws == -1 || ws == as) ? ((ws == -1) ? "-" : "\"") : win_ofctstr1,
2592 (ws == -1 || ws == as) ? ((ws == -1) ? "-" : "\"") : win_ofctstr2);
2593 }
2594 else { /* pli->do_trm_F3 is FALSE, default output mode */
2595 fprintf(ofp, "%-*" PRId64 " %-*s %-*s %-*s %-*s %-*s %3s %8d %8d %*" PRId64 " %*" PRId64 " %6s %5s %4d %4.2f %5.1f %6.1f %9.2g %3s %3s %*s %6s %6s %*s %6s %6s %*d %*" PRId64 " %s\n",
2596 idxw1, noutput,
2597 tnamew, th->hit[h]->name,
2598 taccw, ((th->hit[h]->acc != NULL && th->hit[h]->acc[0] != '\0') ? th->hit[h]->acc : "-"),
2599 qnamew, qname,
2600 qaccw, ((qacc != NULL && qacc[0] != '\0') ? qacc : "-"),
2601 clanw, (th->hit[h]->clan_idx == -1) ? "-" : esl_keyhash_Get(clan_name_kh, th->hit[h]->clan_idx),
2602 th->hit[h]->hmmonly ? "hmm" : "cm",
2603 th->hit[h]->ad->cfrom_emit, th->hit[h]->ad->cto_emit,
2604 posw, th->hit[h]->start,
2605 posw, th->hit[h]->stop,
2606 (th->hit[h]->in_rc == TRUE) ? "-" : "+",
2607 cm_alidisplay_TruncString(th->hit[h]->ad),
2608 th->hit[h]->pass_idx,
2609 th->hit[h]->ad->gc,
2610 th->hit[h]->bias,
2611 th->hit[h]->score,
2612 th->hit[h]->evalue,
2613 (th->hit[h]->flags & CM_HIT_IS_INCLUDED ? " ! " : " ? "),
2614 olp_str,
2615 idxw2, (as == -1) ? "-" : any_oidxstr,
2616 (as == -1) ? "-" : any_ofctstr1,
2617 (as == -1) ? "-" : any_ofctstr2,
2618 idxw2, (ws == -1 || ws == as) ? ((ws == -1) ? "-" : "\"") : win_oidxstr,
2619 (ws == -1 || ws == as) ? ((ws == -1) ? "-" : "\"") : win_ofctstr1,
2620 (ws == -1 || ws == as) ? ((ws == -1) ? "-" : "\"") : win_ofctstr2,
2621 clenw, th->hit[h]->ad->clen, srcLw, th->hit[h]->srcL,
2622 (th->hit[h]->desc != NULL) ? th->hit[h]->desc : "-");
2623 }
2624 }
2625 }
2626 }
2627 if(qnamestr != NULL) free(qnamestr);
2628 if(tnamestr != NULL) free(tnamestr);
2629 if(qaccstr != NULL) free(qaccstr);
2630 if(taccstr != NULL) free(taccstr);
2631 if(clanstr != NULL) free(clanstr);
2632 if(posstr != NULL) free(posstr);
2633 if(idxstr1 != NULL) free(idxstr1);
2634 if(idxstr2 != NULL) free(idxstr2);
2635 if(olp_str != NULL) free(olp_str);
2636 if(any_oidxstr != NULL) free(any_oidxstr);
2637 if(win_oidxstr != NULL) free(win_oidxstr);
2638 if(any_ofctstr1 != NULL) free(any_ofctstr1);
2639 if(any_ofctstr2 != NULL) free(any_ofctstr2);
2640 if(win_ofctstr1 != NULL) free(win_ofctstr1);
2641 if(win_ofctstr2 != NULL) free(win_ofctstr2);
2642 if(clannamestr != NULL) free(clannamestr);
2643 if(clenstr != NULL) free(clenstr);
2644 if(srcLstr != NULL) free(srcLstr);
2645 if(sorted_idxA != NULL) free(sorted_idxA);
2646 if(output_idxA != NULL) free(output_idxA);
2647 if(has_overlapA != NULL) free(has_overlapA);
2648
2649 return eslOK;
2650
2651 ERROR:
2652 if(qnamestr != NULL) free(qnamestr);
2653 if(tnamestr != NULL) free(tnamestr);
2654 if(qaccstr != NULL) free(qaccstr);
2655 if(taccstr != NULL) free(taccstr);
2656 if(clanstr != NULL) free(clanstr);
2657 if(posstr != NULL) free(posstr);
2658 if(idxstr1 != NULL) free(idxstr1);
2659 if(idxstr2 != NULL) free(idxstr2);
2660 if(olp_str != NULL) free(olp_str);
2661 if(any_oidxstr != NULL) free(any_oidxstr);
2662 if(win_oidxstr != NULL) free(win_oidxstr);
2663 if(any_ofctstr1 != NULL) free(any_ofctstr1);
2664 if(any_ofctstr2 != NULL) free(any_ofctstr2);
2665 if(win_ofctstr1 != NULL) free(win_ofctstr1);
2666 if(win_ofctstr2 != NULL) free(win_ofctstr2);
2667 if(clannamestr != NULL) free(clannamestr);
2668 if(clenstr != NULL) free(clenstr);
2669 if(srcLstr != NULL) free(srcLstr);
2670 if(output_idxA != NULL) free(output_idxA);
2671 if(has_overlapA != NULL) free(has_overlapA);
2672
2673 return status;
2674 }
2675
2676 /* Function: cm_tophits_TabularTargets3()
2677 * Synopsis: Output parsable table of per-sequence hits in format '3'.
2678 * Incept: EPN, Thu Oct 6 14:15:09 2022
2679 *
2680 * Purpose: Output a parseable table of reportable per-sequence hits
2681 * in sorted tophits list <th> in an easily parsed ASCII
2682 * tabular form to stream <ofp>, using final pipeline
2683 * accounting stored in <pli>. Format #3 (introduced
2684 * in 1.1.5). Identical to format 1 but with 'mdl_len' and
2685 # 'seq_len' fields added prior to 'description'.
2686 *
2687 * Designed to be concatenated for multiple queries and
2688 * multiple top hits list.
2689 *
2690 * Returns: <eslOK> on success.
2691 */
2692 int
2693 cm_tophits_TabularTargets3(FILE *ofp, char *qname, char *qacc, CM_TOPHITS *th, CM_PIPELINE *pli, int show_header)
2694 {
2695 int status;
2696 int i;
2697 int tnamew = ESL_MAX(20, cm_tophits_GetMaxNameLength(th));
2698 int qnamew = ESL_MAX(20, strlen(qname));
2699 int qaccw = ((qacc != NULL) ? ESL_MAX(9, strlen(qacc)) : 9);
2700 int taccw = ESL_MAX(9, cm_tophits_GetMaxAccessionLength(th));
2701 int posw = ESL_MAX(8, cm_tophits_GetMaxPositionLength(th));
2702 int clenw = ESL_MAX(7, cm_tophits_GetMaxModelLength(th));
2703 int srcLw = ESL_MAX(7, cm_tophits_GetMaxTargetLength(th));
2704
2705 char *qnamestr = NULL;
2706 char *tnamestr = NULL;
2707 char *qaccstr = NULL;
2708 char *taccstr = NULL;
2709 char *posstr = NULL;
2710 char *clenstr = NULL;
2711 char *srcLstr = NULL;
2712
2713 ESL_ALLOC(tnamestr, sizeof(char) * (tnamew));
2714 ESL_ALLOC(taccstr, sizeof(char) * (taccw+1));
2715 ESL_ALLOC(qnamestr, sizeof(char) * (qnamew+1));
2716 ESL_ALLOC(qaccstr, sizeof(char) * (qaccw+1));
2717 ESL_ALLOC(posstr, sizeof(char) * (posw+1));
2718 ESL_ALLOC(clenstr, sizeof(char) * (clenw+1));
2719 ESL_ALLOC(srcLstr, sizeof(char) * (srcLw+1));
2720
2721 for(i = 0; i < tnamew-1; i++) { tnamestr[i] = '-'; } tnamestr[tnamew-1] = '\0'; /* need to account for single '#' */
2722 for(i = 0; i < taccw; i++) { taccstr[i] = '-'; } taccstr[taccw] = '\0';
2723 for(i = 0; i < qnamew; i++) { qnamestr[i] = '-'; } qnamestr[qnamew] = '\0';
2724 for(i = 0; i < qaccw; i++) { qaccstr[i] = '-'; } qaccstr[qaccw] = '\0';
2725 for(i = 0; i < posw; i++) { posstr[i] = '-'; } posstr[posw] = '\0';
2726 for(i = 0; i < clenw; i++) { clenstr[i] = '-'; } clenstr[clenw] = '\0';
2727 for(i = 0; i < srcLw; i++) { srcLstr[i] = '-'; } srcLstr[srcLw] = '\0';
2728
2729 int h;
2730
2731 if (show_header) {
2732 fprintf(ofp, "#%-*s %-*s %-*s %-*s %3s %8s %8s %*s %*s %6s %5s %4s %4s %5s %6s %9s %3s %*s %*s %-s\n",
2733 tnamew-1, "target name", taccw, "accession", qnamew, "query name", qaccw, "accession",
2734 "mdl", "mdl from", "mdl to",
2735 posw, "seq from", posw, "seq to", "strand", "trunc", "pass", "gc", "bias", "score", "E-value", "inc", clenw, "mdl len", srcLw, "seq len", "description of target");
2736 fprintf(ofp, "#%-*s %-*s %-*s %-*s %-3s %-7s %-7s %*s %*s %6s %5s %4s %4s %5s %6s %9s %3s %*s %*s %s\n",
2737 tnamew-1, tnamestr, taccw, taccstr, qnamew, qnamestr, qaccw, qaccstr,
2738 "---", "--------", "--------",
2739 posw, posstr, posw, posstr, "------", "-----", "----", "----", "-----", "------", "---------", "---",
2740 clenw, clenstr, srcLw, srcLstr, "---------------------");
2741 }
2742 for (h = 0; h < th->N; h++) {
2743 if (th->hit[h]->flags & CM_HIT_IS_REPORTED) {
2744 // fprintf(ofp, "%-*s %-*s %-*s %-*s %3s %8d %8d %*" PRId64 " %*" PRId64 " %6s %5s %4d %4.2f %5.1f %6.1f %9.2g %-3s %s\n",
2745 fprintf(ofp, "%-*s %-*s %-*s %-*s %3s %8d %8d %*" PRId64 " %*" PRId64 " %6s %5s %4d %4.2f %5.1f %6.1f %9.2g %-3s %*d %*" PRId64 " %s\n",
2746 tnamew, th->hit[h]->name,
2747 taccw, ((th->hit[h]->acc != NULL && th->hit[h]->acc[0] != '\0') ? th->hit[h]->acc : "-"),
2748 qnamew, qname,
2749 qaccw, ((qacc != NULL && qacc[0] != '\0') ? qacc : "-"),
2750 th->hit[h]->hmmonly ? "hmm" : "cm",
2751 th->hit[h]->ad->cfrom_emit, th->hit[h]->ad->cto_emit,
2752 posw, th->hit[h]->start,
2753 posw, th->hit[h]->stop,
2754 (th->hit[h]->in_rc == TRUE) ? "-" : "+",
2755 cm_alidisplay_TruncString(th->hit[h]->ad),
2756 th->hit[h]->pass_idx,
2757 th->hit[h]->ad->gc,
2758 th->hit[h]->bias,
2759 th->hit[h]->score,
2760 th->hit[h]->evalue,
2761 (th->hit[h]->flags & CM_HIT_IS_INCLUDED ? "!" : "?"),
2762 clenw, th->hit[h]->ad->clen, srcLw, th->hit[h]->srcL,
2763 (th->hit[h]->desc != NULL) ? th->hit[h]->desc : "-");
2764 }
2765 }
2766 if(qnamestr != NULL) free(qnamestr);
2767 if(tnamestr != NULL) free(tnamestr);
2768 if(qaccstr != NULL) free(qaccstr);
2769 if(taccstr != NULL) free(taccstr);
2770 if(posstr != NULL) free(posstr);
2771 if(clenstr != NULL) free(clenstr);
2772 if(srcLstr != NULL) free(srcLstr);
2773
2774 return eslOK;
2775
2776 ERROR:
2777 if(qnamestr != NULL) free(qnamestr);
2778 if(tnamestr != NULL) free(tnamestr);
2779 if(qaccstr != NULL) free(qaccstr);
2780 if(taccstr != NULL) free(taccstr);
2781 if(posstr != NULL) free(posstr);
2782 if(clenstr != NULL) free(clenstr);
2783 if(srcLstr != NULL) free(srcLstr);
2784
2785 return status;
2786 }
2787
2788 /* Function: cm_tophits_F3TabularTargets1()
2789 * Synopsis: Output format for a top target hits list in special
2790 * 'terminate after filter stage F3' mode.
2791 *
2792 * Incept: EPN, Wed Oct 22 14:42:48 2014
2793 * SRE, Tue Dec 9 09:10:43 2008 [Janelia] (p7_tophits.c)
2794 *
2795 * Purpose: Output a parseable table of reportable per-sequence hits
2796 * in sorted tophits list <th> in an easily parsed ASCII
2797 * tabular form to stream <ofp>, using final pipeline
2798 * accounting stored in <pli>. This version is similar
2799 * to cm_tophits_TabularTargets() but reports a
2800 * different set of fields. Mostly the same fields that are
2801 * printed in cm_tophits_F3Targets() are printed here.
2802 *
2803 * Purpose: Output a list of the reportable top target hits in <th>
•2804 * in human-readable ASCII text format to stream <ofp>, using
2805 * final pipeline accounting stored in <pli>. This version
2806 * is similar to cm_tophits_TabularTargets() but reports a different
2807 * set of fields.
2808 *
2809 * The tophits list <th> should already be sorted (see
2810 * <cm_tophits_Sort*()> and thresholded (see
2811 * <cm_tophits_Threshold>).
2812 *
2813 * Returns: <eslOK> on success.
2814 */
2815 int
2816 cm_tophits_F3TabularTargets1(FILE *ofp, CM_TOPHITS *th, CM_PIPELINE *pli, int show_header)
2817 {
2818 int status;
2819 int h,i;
2820 int namew;
2821 int posw;
2822 int descw;
2823 char *showname;
2824 char lseq, rseq;
2825
2826 char *namestr = NULL;
2827 char *descstr = NULL;
2828 char *posstr = NULL;
2829
2830 /* when --acc is on, we'll show accession if available, and fall back to name */
2831 if (pli->show_accessions) namew = ESL_MAX((pli->mode == CM_SEARCH_SEQS ? 9 : 10), cm_tophits_GetMaxShownLength(th));
2832 else namew = ESL_MAX((pli->mode == CM_SEARCH_SEQS ? 9 : 10), cm_tophits_GetMaxNameLength(th));
2833 descw = ESL_MAX((pli->mode == CM_SEARCH_SEQS ? 9 : 8), cm_tophits_GetMaxDescLength(th));
2834 posw = ESL_MAX(6, cm_tophits_GetMaxPositionLength(th));
2835
2836 ESL_ALLOC(namestr, sizeof(char) * (namew+1));
2837 ESL_ALLOC(descstr, sizeof(char) * (descw+1));
2838 ESL_ALLOC(posstr, sizeof(char) * (posw+1));
2839
2840 namestr[0] = '#';
2841 for(i = 1; i < namew; i++) { namestr[i] = '-'; } namestr[namew] = '\0';
2842 for(i = 0; i < descw; i++) { descstr[i] = '-'; } descstr[descw] = '\0';
2843 for(i = 0; i < posw; i++) { posstr[i] = '-'; } posstr[posw] = '\0';
2844
2845 if(show_header) {
2846 fprintf(ofp, "%-*s %-*s %6s %*s %*s %6s %6s %3s %11s\n", namew, (pli->mode == CM_SEARCH_SEQS) ? "#sequence" : "#modelname", descw, (pli->mode == CM_SEARCH_SEQS) ? "modelname" : "sequence", " score", posw, "start", posw, "end", "strand", "bounds", "ovp", "seqlen");
2847 fprintf(ofp, "%*s %*s %6s %*s %*s %6s %6s %3s %11s\n", namew, namestr, descw, descstr, "------", posw, posstr, posw, posstr, "------", "------", "---", "-----------");
2848 }
2849
2850 for (h = 0; h < th->N; h++) {
2851 if (th->hit[h]->flags & CM_HIT_IS_REPORTED) {
2852
2853 if (pli->show_accessions) {
2854 /* the --acc option: report accessions rather than names if possible */
2855 if (th->hit[h]->acc != NULL && th->hit[h]->acc[0] != '\0') showname = th->hit[h]->acc;
2856 else showname = th->hit[h]->name;
2857 }
2858 else {
2859 showname = th->hit[h]->name;
2860 }
2861 if(th->hit[h]->in_rc) {
2862 lseq = th->hit[h]->start == th->hit[h]->srcL ? '[' : '.';
2863 rseq = th->hit[h]->stop == 1 ? ']' : '.';
2864 }
2865 else {
2866 lseq = th->hit[h]->start == 1 ? '[' : '.';
2867 rseq = th->hit[h]->stop == th->hit[h]->srcL ? ']' : '.';
2868 }
2869
2870 fprintf(ofp, "%-*s %-*s %6.1f %*" PRId64 " %*" PRId64 " %6s %4s%c%c %3s %11" PRId64 "\n",
2871 namew, showname,
2872 descw, th->hit[h]->desc,
2873 th->hit[h]->score,
2874 posw, th->hit[h]->start,
2875 posw, th->hit[h]->stop,
2876 (th->hit[h]->in_rc == TRUE) ? "-" : "+",
2877 "", lseq, rseq,
2878 ((pli->mode == CM_SCAN_MODELS) ? (th->hit[h]->flags & CM_HIT_IS_MARKED_OVERLAP ? " = " : " * ") : " ? "),
2879 th->hit[h]->srcL);
2880 }
2881 }
2882 if(namestr != NULL) free(namestr);
2883 if(descstr != NULL) free(descstr);
2884 if(posstr != NULL) free(posstr);
2885
2886 return eslOK;
2887
2888 ERROR:
2889 if(namestr != NULL) free(namestr);
2890 if(descstr != NULL) free(descstr);
2891 if(posstr != NULL) free(posstr);
2892
2893 return status;
2894 }
2895
2896 /* Function: cm_tophits_TabularTail()
2897 * Synopsis: Print a trailer on a tabular output file.
2898 * Incept: SRE, Tue Jan 11 16:13:30 2011 [Janelia]
2899 *
2900 * Purpose: Print some metadata as a trailer on a tabular output file:
2901 * date/time, the program, INFERNAL version info, the
2902 * pipeline mode (SCAN or SEARCH), the query and target
2903 * filenames, a spoof commandline recording the entire
2904 * program configuration, and a "fini!" that's useful for
2905 * detecting successful output completion.
2906 *
2907 * Args: ofp - open tabular output file (--tblout)
2908 * progname - "cmscan", for example
2909 * pipemode - CM_SEARCH_SEQS | CM_SCAN_MODELS
2910 * qfile - name of query file, or '-' for stdin, or '[none]' if NULL
2911 * tfile - name of target file, or '-' for stdin, or '[none]' if NULL
2912 * go - program configuration; used to generate spoofed command line
2913 *
2914 * Returns: <eslOK>.
2915 *
2916 * Throws: <eslEMEM> on allocation failure.
2917 * <eslESYS> if time() or ctime_r() system calls fail.
2918 *
2919 * Xref: J7/54
2920 */
2921 int
2922 cm_tophits_TabularTail(FILE *ofp, const char *progname, enum cm_pipemodes_e pipemode, const char *qfile, const char *tfile, const ESL_GETOPTS *go)
2923 {
2924 time_t date = time(NULL);
2925 char *spoof_cmd = NULL;
2926 char *cwd = NULL;
2927 char timestamp[32];
2928 char modestamp[16];
2929 int status;
2930
2931 if ((status = esl_opt_SpoofCmdline(go, &spoof_cmd)) != eslOK) goto ERROR;
2932 if (date == -1) ESL_XEXCEPTION(eslESYS, "time() failed");
2933 if ((ctime_r(&date, timestamp)) == NULL) ESL_XEXCEPTION(eslESYS, "ctime_r() failed");
2934 switch (pipemode) {
2935 case CM_SEARCH_SEQS: strcpy(modestamp, "SEARCH"); break;
2936 case CM_SCAN_MODELS: strcpy(modestamp, "SCAN"); break;
2937 default: ESL_EXCEPTION(eslEINCONCEIVABLE, "wait, what? no such pipemode");
2938 }
2939 esl_getcwd(&cwd);
2940
2941 fprintf(ofp, "#\n");
2942 fprintf(ofp, "# Program: %s\n", (progname == NULL) ? "[none]" : progname);
2943 fprintf(ofp, "# Version: %s (%s)\n", INFERNAL_VERSION, INFERNAL_DATE);
2944 fprintf(ofp, "# Pipeline mode: %s\n", modestamp);
2945 fprintf(ofp, "# Query file: %s\n", (qfile == NULL) ? "[none]" : qfile);
2946 fprintf(ofp, "# Target file: %s\n", (tfile == NULL) ? "[none]" : tfile);
2947 fprintf(ofp, "# Option settings: %s\n", spoof_cmd);
2948 if (getenv("SOURCE_DATE_EPOCH") != NULL) {
2949 fprintf(ofp, "# Current dir: %s\n", (cwd == NULL) ? "[unknown]" : cwd);
2950 fprintf(ofp, "# Date: %s", timestamp); /* timestamp ends in \n */
2951 }
2952 fprintf(ofp, "# [ok]\n");
2953
2954 free(spoof_cmd);
2955 if (cwd) free(cwd);
2956 return eslOK;
2957
2958 ERROR:
2959 if (spoof_cmd) free(spoof_cmd);
2960 if (cwd) free(cwd);
2961 return status;
2962 }
2963 /*------------------- end, tabular output -----------------------*/
2964
2965 /*****************************************************************
2966 * 4. Debugging/dev code
2967 *****************************************************************/
2968
2969 /* Function: cm_tophits_Dump()
2970 * Synopsis: Print contents of CM_TOPHITS for inspection.
2971 *
2972 * Purpose: Print contents of the <CM_TOPHITS> <th> to
2973 * stream <fp> for inspection.
2974 *
2975 * Returns: <eslOK>
2976 */
2977 int
2978 cm_tophits_Dump(FILE *fp, const CM_TOPHITS *th)
2979 {
2980 uint64_t i;
2981
2982 fprintf(fp, "CM_TOPHITS dump\n");
2983 fprintf(fp, "------------------\n");
2984
2985 fprintf(fp, "N = %" PRId64 "\n", th->N);
2986 fprintf(fp, "Nalloc = %" PRId64 "\n", th->Nalloc);
2987 fprintf(fp, "nreported = %" PRId64 "\n", th->nreported);
2988 fprintf(fp, "nincluded = %" PRId64 "\n", th->nincluded);
2989 fprintf(fp, "is_sorted_by_evalue = %s\n", th->is_sorted_by_evalue ? "TRUE" : "FALSE");
2990 fprintf(fp, "is_sorted_for_overlap_removal = %s\n", th->is_sorted_for_overlap_removal ? "TRUE" : "FALSE");
2991 fprintf(fp, "is_sorted_by_overlap_markup = %s\n", th->is_sorted_for_overlap_markup ? "TRUE" : "FALSE");
2992 fprintf(fp, "is_sorted_by_position = %s\n", th->is_sorted_by_position ? "TRUE" : "FALSE");
2993 if(th->is_sorted_by_evalue) {
2994 for (i = 0; i < th->N; i++) {
2995 fprintf(fp, "E-VALUE SORTED HIT %" PRId64 ":\n", i);
2996 cm_hit_Dump(fp, th->hit[i]);
2997 }
2998 }
2999 else if(th->is_sorted_for_overlap_removal) {
3000 for (i = 0; i < th->N; i++) {
3001 fprintf(fp, "OVERLAP REMOVAL SORTED HIT %" PRId64 ":\n", i);
3002 cm_hit_Dump(fp, th->hit[i]);
3003 }
3004 }
3005 else if(th->is_sorted_by_position) {
3006 for (i = 0; i < th->N; i++) {
3007 fprintf(fp, "POSITION SORTED HIT %" PRId64 ":\n", i);
3008 cm_hit_Dump(fp, th->hit[i]);
3009 }
3010 }
3011 else if(th->is_sorted_for_overlap_markup) {
3012 for (i = 0; i < th->N; i++) {
3013 fprintf(fp, "OVERLAP MARKUP SORTED HIT %" PRId64 ":\n", i);
3014 cm_hit_Dump(fp, th->hit[i]);
3015 }
3016 }
3017 else {
3018 for (i = 0; i < th->N; i++) {
3019 fprintf(fp, "UNSORTED HIT %" PRId64 ":\n", i);
3020 cm_hit_Dump(fp, &(th->unsrt[i]));
3021 }
3022 }
3023 return eslOK;
3024 }
3025
3026 /* Function: cm_hit_AllowTruncation()
3027 * Synopsis: Determine if a hit returned from a truncated DP scanner
3028 * is allowed, and return TRUE if it is and should be
3029 * reported. Otherwise return FALSE.
3030 *
3031 * The decision depends on the pipeline pass index, the
3032 * mode of the hit, the locality mode of the CM and whether
3033 * or not the hit contains the first and/or final residue
3034 * of its source sequence.
3035 *
3036 * Args: cm - the model, we need its emitmap and clen
3037 * pass_idx - pass index hit was found in, if
3038 * PLI_PASS_STD_ANY or PLI_PASS_5P_AND_3P_ANY
3039 * or PLI_PASS_HMM_ONLY_ANY, we allow all hits.
3040 * start - start position of the hit
3041 * stop - end position of the hit
3042 * i0 - start position of source sequence
3043 * j0 - end position of source sequence
3044 * mode - marginal mode of hit
3045 * b - entry state of the hit, we used a
3046 * truncated begin into this state.
3047 * Returns: informative string
3048 */
3049 int
3050 cm_hit_AllowTruncation(CM_t *cm, int pass_idx, int64_t start, int64_t stop, int64_t i0, int64_t j0, char mode, int b)
3051 {
3052 int in_local_mode = (cm->flags & CMH_LOCAL_BEGIN) ? TRUE : FALSE;
3053 int nd = cm->ndidx[b];
3054 int lpos = (cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATL_nd) ? cm->emap->lpos[nd] : cm->emap->lpos[nd] + 1;
3055 int rpos = (cm->ndtype[nd] == MATP_nd || cm->ndtype[nd] == MATR_nd) ? cm->emap->rpos[nd] : cm->emap->rpos[nd] - 1;
3056
3057 /* if our pass index allows 'any' hit, return TRUE */
3058 if(pass_idx == PLI_PASS_STD_ANY || pass_idx == PLI_PASS_5P_AND_3P_ANY || pass_idx == PLI_PASS_HMM_ONLY_ANY) return TRUE;
3059
3060 /* always allow full sequence hits i0..j0 */
3061 if(start == i0 && stop == j0) return TRUE;
3062
3063 /* if we get here, hit does not include full seq i0..j0 */
3064 if(in_local_mode) {
3065 switch(mode) {
3066 case TRMODE_J: /* local J hit that doesn't span full seq: always allow it */
3067 return TRUE; break;
3068 case TRMODE_L: /* local L hit that doesn't span full seq: j0 must be included */
3069 return (stop == j0) ? TRUE : FALSE; break;
3070 case TRMODE_R: /* local R hit that doesn't span full seq: i0 must be included */
3071 return (start == i0) ? TRUE : FALSE; break;
3072 case TRMODE_T: /* local T hit that doesn't span full seq: don't allow it */
3073 return FALSE; break;
3074 default:
3075 return FALSE; break;
3076 }
3077 }
3078 else { /* local begins are off */
3079 switch(mode) {
3080 case TRMODE_J: /* global J hit that doesn't span full seq: b must span 1..clen to allow it*/
3081 return (lpos == 1 && rpos == cm->clen) ? TRUE : FALSE; break;
3082 case TRMODE_L: /* global L hit that doesn't span full seq: j0 must be included and b must span 1 to allow it */
3083 return (stop == j0 && lpos == 1) ? TRUE : FALSE; break;
3084 case TRMODE_R: /* global R hit that doesn't span full seq: i0 must be included and b must span clen to allow it */
3085 return (start == i0 && rpos == cm->clen) ? TRUE : FALSE; break;
3086 case TRMODE_T: /* global T hit that doesn't span full seq: don't allow it */
3087 return FALSE; break;
3088 default:
3089 return FALSE; break;
3090 }
3091 }
3092 return FALSE; /* not reached */
3093 }
3094
3095 /* Function: cm_hit_Dump()
3096 * Synopsis: Print contents of a CM_HIT for inspection.
3097 *
3098 * Purpose: Print contents of the <CM_HIT> <h> to
3099 * stream <fp> for inspection.
3100 *
3101 * Returns: <eslOK>
3102 */
3103 int
3104 cm_hit_Dump(FILE *fp, const CM_HIT *h)
3105 {
3106 fprintf(fp, "CM_HIT dump\n");
3107 fprintf(fp, "------------------\n");
3108 fprintf(fp, "hit_idx = %" PRId64 "\n", h->hit_idx);
3109 fprintf(fp, "name = %s\n", h->name);
3110 fprintf(fp, "acc = %s\n", (h->acc != NULL) ? h->acc : "NULL");
3111 fprintf(fp, "desc = %s\n", (h->desc != NULL) ? h->desc : "NULL");
3112 fprintf(fp, "cm_idx = %" PRId64 "\n", h->cm_idx);
3113 fprintf(fp, "clan_idx = %d\n", h->clan_idx);
3114 fprintf(fp, "seq_idx = %" PRId64 "\n", h->seq_idx);
3115 fprintf(fp, "pass_idx = %d\n", h->pass_idx);
3116 fprintf(fp, "start = %" PRId64 "\n", h->start);
3117 fprintf(fp, "stop = %" PRId64 "\n", h->stop);
3118 fprintf(fp, "srcL = %" PRId64 "\n", h->srcL);
3119 fprintf(fp, "in_rc = %s\n", h->in_rc ? "TRUE" : "FALSE");
3120 fprintf(fp, "root = %d\n", h->root);
3121 fprintf(fp, "mode = %s\n", MarginalMode(h->mode));
3122 fprintf(fp, "score = %f\n", h->score);
3123 fprintf(fp, "pvalue = %f\n", h->pvalue);
3124 fprintf(fp, "evalue = %f\n", h->evalue);
3125 fprintf(fp, "has_evalue = %s\n", h->has_evalue ? "TRUE" : "FALSE");
3126 fprintf(fp, "any_oidx = %" PRId64 "\n", h->any_oidx);
3127 fprintf(fp, "win_oidx = %" PRId64 "\n", h->win_oidx);
3128 fprintf(fp, "any_bitE = %f\n", h->any_bitE);
3129 fprintf(fp, "win_bitE = %f\n", h->win_bitE);
3130 if(h->flags == 0) {
3131 fprintf(fp, "flags = NONE\n");
3132 }
3133 else {
3134 fprintf(fp, "flags:\n");
3135 if(h->flags & CM_HIT_IS_REPORTED) fprintf(fp, "\tCM_HIT_IS_REPORTED\n");
3136 if(h->flags & CM_HIT_IS_INCLUDED) fprintf(fp, "\tCM_HIT_IS_INCLUDED\n");
3137 if(h->flags & CM_HIT_IS_REMOVED_DUPLICATE) fprintf(fp, "\tCM_HIT_IS_REMOVED_DUPLICATE\n");
3138 if(h->flags & CM_HIT_IS_MARKED_OVERLAP) fprintf(fp, "\tCM_HIT_IS_MARKED_OVERLAP\n");
3139 }
3140 if(h->ad == NULL) {
3141 fprintf(fp, "ad = NULL\n");
3142 }
3143 else {
3144 cm_alidisplay_Dump(fp, h->ad);
3145 }
3146 return eslOK;
3147 }
3148
3149
3150 /*****************************************************************
3151 * 4. Benchmark driver
3152 *****************************************************************/
3153 #ifdef CM_TOPHITS_BENCHMARK
3154 /*
3155 gcc -o benchmark-cm-tophits -std=gnu99 -g -O2 -I. -L. -I../hmmer/src -L../hmmer/src -I../easel -L../easel -DCM_TOPHITS_BENCHMARK cm_tophits.c -linfernal -lhmmer -leasel -lm
3156 ./benchmark-cm-tophits
3157
3158 As of 28 Dec 07, shows 0.20u for 10 lists of 10,000 hits each (at least ~100x normal expectation),
3159 so we expect top hits list time to be negligible for typical hmmsearch/hmmscan runs.
3160
3161 If needed, we do have opportunity for optimization, however - especially in memory handling.
3162 */
3163 #include <esl_config.h>
3164 #include <p7_config.h>
3165 #include "config.h"
3166
3167 #include <stdlib.h>
3168 #include <stdio.h>
3169
3170 #include "easel.h"
3171 #include "esl_getopts.h"
3172 #include "esl_stopwatch.h"
3173 #include "esl_random.h"
3174
3175 #include "hmmer.h"
3176
3177 #include "infernal.h"
3178
3179 static ESL_OPTIONS options[] = {
3180 /* name type default env range toggles reqs incomp help docgroup*/
3181 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
3182 { "-s", eslARG_INT, "181", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
3183 { "-M", eslARG_INT, "10", NULL, NULL, NULL, NULL, NULL, "number of top hits lists to simulate and merge", 0 },
3184 { "-N", eslARG_INT, "10000", NULL, NULL, NULL, NULL, NULL, "number of top hits to simulate per list", 0 },
3185 { "-X", eslARG_INT, "1000", NULL, NULL, NULL, NULL, NULL, "number of target sequences hits can come from", 0 },
3186 { "-Y", eslARG_INT, "10", NULL, NULL, NULL, NULL, NULL, "number of models hits can be to", 0 },
3187 { "-Z", eslARG_INT, "100", NULL, NULL, NULL, NULL, NULL, "length of hits (fixed)", 0 },
3188 { "-L", eslARG_INT, "100000", NULL, NULL, NULL, NULL, NULL, "length of target sequences", 0 },
3189 { "-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "dump hit list after removing overlaps", 0 },
3190 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
3191 };
3192 static char usage[] = "[-options]";
3193 static char banner[] = "benchmark driver for CM_TOPHITS";
3194
3195 int
3196 main(int argc, char **argv)
3197 {
3198 ESL_GETOPTS *go = cm_CreateDefaultApp(options, 0, argc, argv, banner, usage);
3199 ESL_STOPWATCH *w = esl_stopwatch_Create();
3200 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
3201 int N = esl_opt_GetInteger(go, "-N");
3202 int M = esl_opt_GetInteger(go, "-M");
3203 int X = esl_opt_GetInteger(go, "-X");
3204 int Y = esl_opt_GetInteger(go, "-Y");
3205 int L = esl_opt_GetInteger(go, "-L");
3206 int hitlen = esl_opt_GetInteger(go, "-Z");
3207 CM_TOPHITS **h = NULL;
3208 CM_HIT *hit = NULL;
3209 float *scores = NULL;
3210 int *seq_idxes= NULL;
3211 int *cm_idxes = NULL;
3212 int *starts = NULL;
3213 int *stops = NULL;
3214 char name[] = "not_unique_name";
3215 char acc[] = "not_unique_acc";
3216 char desc[] = "Test description for the purposes of making the benchmark allocate space";
3217 int i,j;
3218 int nhits;
3219 int nhits_unmarked;
3220 int status;
3221 char errbuf[eslERRBUFSIZE];
3222
3223 /* prep work: generate our sort keys before starting to time anything */
3224 ESL_ALLOC(h, sizeof(CM_TOPHITS *) * M); /* allocate pointers for M lists */
3225 ESL_ALLOC(scores, sizeof(double) * N * M);
3226 ESL_ALLOC(seq_idxes, sizeof(int) * N * M);
3227 ESL_ALLOC(cm_idxes, sizeof(int) * N * M);
3228 ESL_ALLOC(starts, sizeof(int) * N * M);
3229 ESL_ALLOC(stops, sizeof(int) * N * M);
3230 for (i = 0; i < N*M; i++) scores[i] = esl_random(r);
3231 for (i = 0; i < N*M; i++) seq_idxes[i] = esl_rnd_Roll(r, X);
3232 for (i = 0; i < N*M; i++) cm_idxes[i] = esl_rnd_Roll(r, Y);
3233 for (i = 0; i < N*M; i++) starts[i] = esl_rnd_Roll(r, L) + 1;
3234 for (i = 0; i < N*M; i++) stops[i] = (esl_rnd_Roll(r, 2) == 0) ? ESL_MIN(starts[i] + hitlen - 1, L) : ESL_MAX(starts[i] - hitlen + 1, 1);
3235
3236 esl_stopwatch_Start(w);
3237
3238 /* generate M "random" lists and sort them */
3239 for (j = 0; j < M; j++)
3240 {
3241 h[j] = cm_tophits_Create();
3242 for (i = 0; i < N; i++)
3243 {
3244 cm_tophits_CreateNextHit(h[j], &hit);
3245 esl_strdup(name, -1, &(hit->name));
3246 esl_strdup(acc, -1, &(hit->acc));
3247 esl_strdup(desc, -1, &(hit->desc));
3248 hit->start = starts[j*N+i];
3249 hit->stop = stops[j*N+i];
3250 hit->score = scores[j*N+i];
3251 hit->seq_idx = seq_idxes[j*N+i];
3252 hit->cm_idx = cm_idxes[j*N+i];
3253 hit->srcL = L;
3254 if (hit->start < hit->stop) { hit->in_rc = FALSE; }
3255 else if(hit->start > hit->stop) { hit->in_rc = TRUE; }
3256 else { hit->in_rc = (esl_rnd_Roll(r, 2) == 0) ? FALSE : TRUE; }
3257 }
3258 if(esl_opt_GetBoolean(go, "-v")) cm_tophits_Dump(stdout, h[j]);
3259 }
3260
3261 esl_stopwatch_Stop(w);
3262 esl_stopwatch_Display(stdout, w, "# CPU time hit creation: ");
3263 esl_stopwatch_Start(w);
3264
3265 /* then merge them into one big list in h[0] */
3266 for (j = 1; j < M; j++)
3267 {
3268 cm_tophits_Merge(h[0], h[j]);
3269 cm_tophits_Destroy(h[j]);
3270 }
3271 esl_stopwatch_Stop(w);
3272 esl_stopwatch_Display(stdout, w, "# CPU time cm_tophits_Merge(): ");
3273 esl_stopwatch_Start(w);
3274
3275 cm_tophits_SortForOverlapRemoval(h[0]);
3276
3277 esl_stopwatch_Stop(w);
3278 esl_stopwatch_Display(stdout, w, "# CPU time cm_tophits_SortForOverlapRemoval(): ");
3279 esl_stopwatch_Start(w);
3280
3281 if(esl_opt_GetBoolean(go, "-v")) cm_tophits_Dump(stdout, h[0]);
3282
3283 if((status = cm_tophits_RemoveOrMarkOverlaps(h[0], /*do_clans_only=*/FALSE, errbuf)) != eslOK) cm_Fail(errbuf);
3284
3285 esl_stopwatch_Stop(w);
3286 esl_stopwatch_Display(stdout, w, "# CPU time cm_tophits_RemoveOrMarkOverlaps() (removing): ");
3287 esl_stopwatch_Start(w);
3288
3289 cm_tophits_SortForOverlapMarkup(h[0], FALSE);
3290
3291 esl_stopwatch_Stop(w);
3292 esl_stopwatch_Display(stdout, w, "# CPU time cm_tophits_SortForOverlapMarkup(): ");
3293 esl_stopwatch_Start(w);
3294
3295 if((status = cm_tophits_RemoveOrMarkOverlaps(h[0], /*do_clans=*/FALSE, errbuf)) != eslOK) cm_Fail(errbuf);
3296
3297 if(esl_opt_GetBoolean(go, "-v")) cm_tophits_Dump(stdout, h[0]);
3298
3299 esl_stopwatch_Stop(w);
3300 esl_stopwatch_Display(stdout, w, "# CPU time cm_tophits_RemoveOrMarkOverlaps() (marking): ");
3301 esl_stopwatch_Start(w);
3302
3303 cm_tophits_SortByEvalue(h[0]);
3304
3305 esl_stopwatch_Stop(w);
3306 esl_stopwatch_Display(stdout, w, "# CPU time cm_tophits_SortByEvalue(): ");
3307
3308 if(esl_opt_GetBoolean(go, "-v")) cm_tophits_Dump(stdout, h[0]);
3309
3310 /* determine number of valid (not removed) hits */
3311 nhits = 0;
3312 nhits_unmarked = 0;
3313 for(i = 0; i < h[0]->N; i++) {
3314 if(! (h[0]->hit[i]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) {
3315 nhits++;
3316 }
3317 if(! (h[0]->hit[i]->flags & CM_HIT_IS_MARKED_OVERLAP)) {
3318 nhits_unmarked++;
3319 }
3320 }
3321
3322 printf("# number of lists: %d\n", M);
3323 printf("# sequence length %d\n", L);
3324 printf("# number of sequences %d\n", X);
3325 printf("# number of models %d\n", Y);
3326 printf("# initial number of hits %d\n", N*M);
3327 printf("# hit length %d\n", hitlen);
3328 printf("# number of non-removed hits %d\n", nhits);
3329 printf("# number of non-marked hits %d\n", nhits_unmarked);
3330
3331 cm_tophits_Destroy(h[0]);
3332 status = eslOK;
3333
3334 ERROR:
3335 esl_getopts_Destroy(go);
3336 esl_stopwatch_Destroy(w);
3337 esl_randomness_Destroy(r);
3338 if (scores != NULL) free(scores);
3339 if (seq_idxes != NULL) free(seq_idxes);
3340 if (cm_idxes != NULL) free(cm_idxes);
3341 if (starts != NULL) free(starts);
3342 if (stops != NULL) free(stops);
3343 if (h != NULL) free(h);
3344 return status;
3345 }
3346 #endif /*CM_TOPHITS_BENCHMARK*/
3347 /*****************************************************************
3348 * 5. Test driver
3349 *****************************************************************/
3350
3351 #ifdef CM_TOPHITS_TESTDRIVE
3352 /*
3353 gcc -o cm_tophits_utest -std=gnu99 -g -O2 -I. -L. -I../hmmer/src -L../hmmer/src -I../easel -L../easel -DCM_TOPHITS_TESTDRIVE cm_tophits.c -linfernal -lhmmer -leasel -lm
3354 ./cm_tophits_utest
3355 */
3356 #include <esl_config.h>
3357 #include <p7_config.h>
3358 #include "config.h"
3359
3360 #include <stdlib.h>
3361 #include <stdio.h>
3362
3363 #include "easel.h"
3364 #include "esl_getopts.h"
3365 #include "esl_stopwatch.h"
3366 #include "esl_random.h"
3367
3368 #include "hmmer.h"
3369
3370 #include "infernal.h"
3371
3372 static ESL_OPTIONS options[] = {
3373 /* name type default env range toggles reqs incomp help docgroup*/
3374 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
3375 { "-s", eslARG_INT, "181", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
3376 { "-N", eslARG_INT, "100", NULL, NULL, NULL, NULL, NULL, "number of top hits to simulate", 0 },
3377 { "-X", eslARG_INT, "1000", NULL, NULL, NULL, NULL, NULL, "number of target sequences hits can come from", 0 },
3378 { "-Y", eslARG_INT, "10", NULL, NULL, NULL, NULL, NULL, "number of CMs hits can be to", 0 },
3379 { "-L", eslARG_INT, "100000", NULL, NULL, NULL, NULL, NULL, "length of target sequences", 0 },
3380 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
3381 };
3382
3383 static char usage[] = "[-options]";
3384 static char banner[] = "test driver for CM_TOPHITS";
3385
3386 int
3387 main(int argc, char **argv)
3388 {
3389 ESL_GETOPTS *go = cm_CreateDefaultApp(options, 0, argc, argv, banner, usage);
3390 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
3391 int N = esl_opt_GetInteger(go, "-N");
3392 int L = esl_opt_GetInteger(go, "-L");
3393 int X = esl_opt_GetInteger(go, "-X");
3394 int Y = esl_opt_GetInteger(go, "-Y");
3395 CM_TOPHITS *h1 = NULL;
3396 CM_TOPHITS *h2 = NULL;
3397 CM_TOPHITS *h3 = NULL;
3398 CM_TOPHITS *h4 = NULL;
3399 CM_TOPHITS *h5 = NULL;
3400 char name[] = "not_unique_name";
3401 char acc[] = "not_unique_acc";
3402 char desc[] = "Test description for the purposes of making the test driver allocate space";
3403 CM_HIT *hit = NULL;
3404 int i;
3405 int status;
3406 char errbuf[eslERRBUFSIZE];
3407
3408 h1 = cm_tophits_Create();
3409 h2 = cm_tophits_Create();
3410 h3 = cm_tophits_Create();
3411 h4 = cm_tophits_Create();
3412 h5 = cm_tophits_Create();
3413
3414 for (i = 0; i < N; i++)
3415 {
3416 /* add hit to h1 */
3417 cm_tophits_CreateNextHit(h1, &hit);
3418 esl_strdup(name, -1, &(hit->name));
3419 esl_strdup(acc, -1, &(hit->acc));
3420 esl_strdup(desc, -1, &(hit->desc));
3421 hit->start = esl_rnd_Roll(r, L);
3422 hit->stop = esl_rnd_Roll(r, L);
3423 hit->score = esl_random(r);
3424 hit->evalue = esl_random(r) + 5.0 * (double) (esl_rnd_Roll(r, 10) + 1); /* impt that this is greater than 0.1 and less than 200 */
3425 hit->seq_idx = esl_rnd_Roll(r, X);
3426 hit->cm_idx = esl_rnd_Roll(r, Y);
3427 hit->srcL = L;
3428
3429 /* add hit to h2 */
3430 cm_tophits_CreateNextHit(h2, &hit);
3431 esl_strdup(name, -1, &(hit->name));
3432 esl_strdup(acc, -1, &(hit->acc));
3433 esl_strdup(desc, -1, &(hit->desc));
3434 hit->start = esl_rnd_Roll(r, L);
3435 hit->stop = esl_rnd_Roll(r, L);
3436 hit->score = esl_random(r);
3437 hit->evalue = esl_random(r) + 1.0 * (double) (esl_rnd_Roll(r, 10) + 1); /* impt that this is greater than 0.1 and less than 200 */
3438 hit->seq_idx = esl_rnd_Roll(r, X);
3439 hit->cm_idx = esl_rnd_Roll(r, Y);
3440 hit->srcL = L;
3441
3442 /* add hit to h3 */
3443 cm_tophits_CreateNextHit(h3, &hit);
3444 esl_strdup(name, -1, &(hit->name));
3445 esl_strdup(acc, -1, &(hit->acc));
3446 esl_strdup(desc, -1, &(hit->desc));
3447 hit->start = esl_rnd_Roll(r, L);
3448 hit->stop = esl_rnd_Roll(r, L);
3449 hit->score = esl_random(r);
3450 hit->evalue = esl_random(r) + 10.0 * (double) (esl_rnd_Roll(r, 10) + 1); /* impt that this is greater than 0.1 and less than 200 */
3451 hit->seq_idx = esl_rnd_Roll(r, X);
3452 hit->cm_idx = esl_rnd_Roll(r, Y);
3453 hit->srcL = L;
3454
3455 /* add hit to h4 */
3456 cm_tophits_CreateNextHit(h4, &hit);
3457 esl_strdup(name, -1, &(hit->name));
3458 esl_strdup(acc, -1, &(hit->acc));
3459 esl_strdup(desc, -1, &(hit->desc));
3460 hit->start = esl_rnd_Roll(r, L);
3461 hit->stop = esl_rnd_Roll(r, L);
3462 hit->score = esl_random(r);
3463 hit->evalue = esl_random(r) + 15.0 * (double) (esl_rnd_Roll(r, 10) + 1); /* impt that this is greater than 0.1 and less than 200 */
3464 hit->seq_idx = esl_rnd_Roll(r, X);
3465 hit->cm_idx = esl_rnd_Roll(r, Y);
3466 hit->srcL = L;
3467 }
3468
3469 /* add a few more hits */
3470 cm_tophits_CreateNextHit(h1, &hit);
3471 esl_strdup("third", -1, &(hit->name));
3472 hit->start = L;
3473 hit->stop = L;
3474 hit->score = 20.0;
3475 hit->evalue = 0.1;
3476 hit->cm_idx = 0;
3477 hit->seq_idx = 0;
3478 hit->srcL = L+1;
3479
3480 cm_tophits_CreateNextHit(h2, &hit);
3481 esl_strdup("second", -1, &(hit->name));
3482 hit->start = L;
3483 hit->stop = L;
3484 hit->score = 30.0;
3485 hit->evalue = 0.01;
3486 hit->cm_idx = 0;
3487 hit->seq_idx = 0;
3488 hit->srcL = L+1;
3489
3490 cm_tophits_CreateNextHit(h3, &hit);
3491 esl_strdup("first", -1, &(hit->name));
3492 hit->start = L;
3493 hit->stop = L;
3494 hit->score = 40.0;
3495 hit->evalue = 0.001;
3496 hit->cm_idx = 0;
3497 hit->seq_idx = 0;
3498 hit->srcL = L+1;
3499
3500 cm_tophits_CreateNextHit(h1, &hit);
3501 esl_strdup("thirdtolast", -1, &(hit->name));
3502 hit->start = L+1;
3503 hit->stop = L+1;
3504 hit->score = -1.0;
3505 hit->evalue = 200.0;
3506 hit->cm_idx = 0;
3507 hit->seq_idx = 0;
3508 hit->srcL = L+1;
3509
3510 cm_tophits_CreateNextHit(h2, &hit);
3511 esl_strdup("secondtolast", -1, &(hit->name));
3512 hit->start = L+1;
3513 hit->stop = L+1;
3514 hit->score = -11.0;
3515 hit->evalue = 210.0;
3516 hit->cm_idx = 0;
3517 hit->seq_idx = 0;
3518 hit->srcL = L+1;
3519
3520 cm_tophits_CreateNextHit(h3, &hit);
3521 esl_strdup("last", -1, &(hit->name));
3522 hit->start = L+1;
3523 hit->stop = L+1;
3524 hit->score = -21.0;
3525 hit->evalue = 220.0;
3526 hit->cm_idx = 0;
3527 hit->seq_idx = 0;
3528 hit->srcL = L+1;
3529
3530 /* add similar hits with a different model, to test overlap markup
3531 * (as opposed to overlap removal which was tested above)
3532 */
3533 cm_tophits_CreateNextHit(h4, &hit);
3534 esl_strdup("Bthird", -1, &(hit->name));
3535 hit->start = L;
3536 hit->stop = L;
3537 hit->score = 20.0;
3538 hit->evalue = 0.09;
3539 hit->cm_idx = 1;
3540 hit->seq_idx = 0;
3541 hit->srcL = L+1;
3542
3543 cm_tophits_CreateNextHit(h4, &hit);
3544 esl_strdup("Bsecond", -1, &(hit->name));
3545 hit->start = L;
3546 hit->stop = L;
3547 hit->score = 30.0;
3548 hit->evalue = 0.009;
3549 hit->cm_idx = 2;
3550 hit->seq_idx = 0;
3551 hit->srcL = L+1;
3552
3553 cm_tophits_CreateNextHit(h4, &hit);
3554 esl_strdup("Bfirst", -1, &(hit->name));
3555 hit->start = L;
3556 hit->stop = L;
3557 hit->score = 40.0;
3558 hit->evalue = 0.0009;
3559 hit->cm_idx = 3;
3560 hit->seq_idx = 0;
3561 hit->srcL = L+1;
3562
3563 cm_tophits_CreateNextHit(h4, &hit);
3564 esl_strdup("Bthirdtolast", -1, &(hit->name));
3565 hit->start = L+1;
3566 hit->stop = L+1;
3567 hit->score = -1.;
3568 hit->evalue = 201.0;
3569 hit->cm_idx = 4;
3570 hit->seq_idx = 0;
3571 hit->srcL = L+1;
3572
3573 cm_tophits_CreateNextHit(h4, &hit);
3574 esl_strdup("Bsecondtolast", -1, &(hit->name));
3575 hit->start = L+1;
3576 hit->stop = L+1;
3577 hit->score = -11.;
3578 hit->evalue = 211.0;
3579 hit->cm_idx = 5;
3580 hit->seq_idx = 0;
3581 hit->srcL = L+1;
3582
3583 cm_tophits_CreateNextHit(h4, &hit);
3584 esl_strdup("Blast", -1, &(hit->name));
3585 hit->start = L+1;
3586 hit->stop = L+1;
3587 hit->score = -21.;
3588 hit->evalue = 221.0;
3589 hit->cm_idx = 6;
3590 hit->seq_idx = 0;
3591 hit->srcL = L+1;
3592
3593 cm_tophits_SortForOverlapRemoval(h1);
3594 if((status = cm_tophits_RemoveOrMarkOverlaps(h1, /*do_clans_only=*/FALSE, errbuf)) != eslOK) cm_Fail(errbuf);
3595 cm_tophits_SortByEvalue(h1);
3596 if (strcmp(h1->hit[0]->name, "third") != 0) esl_fatal("sort 1 failed (top is %s = %f)", h1->hit[0]->name, h1->hit[0]->score);
3597 if (strcmp(h1->hit[N+1]->name, "thirdtolast") != 0) esl_fatal("sort 1 failed (last is %s = %f)", h1->hit[N+1]->name, h1->hit[N+1]->score);
3598
3599 cm_tophits_Merge(h1, h2);
3600 cm_tophits_SortForOverlapRemoval(h1);
3601 if((status = cm_tophits_RemoveOrMarkOverlaps(h1, /*do_clans_only=*/FALSE, errbuf)) != eslOK) cm_Fail(errbuf);
3602 cm_tophits_SortByEvalue(h1);
3603 if (strcmp(h1->hit[0]->name, "second") != 0) esl_fatal("sort 2 failed (top is %s = %f)", h1->hit[0]->name, h1->hit[0]->score);
3604 if (strcmp(h1->hit[1]->name, "third") != 0) esl_fatal("sort 2 failed (second is %s = %f)", h1->hit[1]->name, h1->hit[1]->score);
3605 if (strcmp(h1->hit[2*N+2]->name, "thirdtolast") != 0) esl_fatal("sort 2 failed (second to last is %s = %f)", h1->hit[2*N+2]->name, h1->hit[2*N+2]->score);
3606 if (strcmp(h1->hit[2*N+3]->name, "secondtolast") != 0) esl_fatal("sort 2 failed (last is %s = %f)", h1->hit[2*N+3]->name, h1->hit[2*N+3]->score);
3607 if ( h1->hit[0]->flags & CM_HIT_IS_REMOVED_DUPLICATE) esl_fatal("RemoveOrMarkOverlaps failed 1");
3608 if (! (h1->hit[1]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) esl_fatal("RemoveOrMarkOverlaps failed 2");
3609 if ( h1->hit[2*N+2]->flags & CM_HIT_IS_REMOVED_DUPLICATE) esl_fatal("RemoveOrMarkOverlaps failed 3");
3610 if (! (h1->hit[2*N+3]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) esl_fatal("RemoveOrMarkOverlaps failed 4");
3611
3612 cm_tophits_Merge(h1, h3);
3613 cm_tophits_SortForOverlapRemoval(h1);
3614 if((status = cm_tophits_RemoveOrMarkOverlaps(h1, /*do_clans_only=*/FALSE, errbuf)) != eslOK) cm_Fail(errbuf);
3615 cm_tophits_SortByEvalue(h1);
3616 if (strcmp(h1->hit[0]->name, "first") != 0) esl_fatal("sort 3 failed (top is %s = %f)", h1->hit[0]->name, h1->hit[0]->score);
3617 if (strcmp(h1->hit[1]->name, "second") != 0) esl_fatal("sort 3 failed (second is %s = %f)", h1->hit[1]->name, h1->hit[1]->score);
3618 if (strcmp(h1->hit[2]->name, "third") != 0) esl_fatal("sort 3 failed (third is %s = %f)", h1->hit[2]->name, h1->hit[2]->score);
3619 if (strcmp(h1->hit[3*N+3]->name, "thirdtolast") != 0) esl_fatal("sort 3 failed (third to last is %s = %f)", h1->hit[3*N+3]->name, h1->hit[3*N+3]->score);
3620 if (strcmp(h1->hit[3*N+4]->name, "secondtolast") != 0) esl_fatal("sort 3 failed (second to last is %s = %f)", h1->hit[3*N+4]->name, h1->hit[3*N+4]->score);
3621 if (strcmp(h1->hit[3*N+5]->name, "last") != 0) esl_fatal("sort 3 failed (last is %s = %f)", h1->hit[3*N+5]->name, h1->hit[3*N+5]->score);
3622 if ( h1->hit[0]->flags & CM_HIT_IS_REMOVED_DUPLICATE) esl_fatal("RemoveOrMarkOverlaps failed 5");
3623 if (! (h1->hit[1]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) esl_fatal("RemoveOrMarkOverlaps failed 6");
3624 if (! (h1->hit[2]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) esl_fatal("RemoveOrMarkOverlaps failed 7");
3625 if ( h1->hit[3*N+3]->flags & CM_HIT_IS_REMOVED_DUPLICATE) esl_fatal("RemoveOrMarkOverlaps failed 8");
3626 if (! (h1->hit[3*N+4]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) esl_fatal("RemoveOrMarkOverlaps failed 9");
3627 if (! (h1->hit[3*N+5]->flags & CM_HIT_IS_REMOVED_DUPLICATE)) esl_fatal("RemoveOrMarkOverlaps failed 10");
3628
3629 /* test markup, first sort for removal and make sure we don't mark up any overlaps of different models */
3630 cm_tophits_Merge(h1, h4);
3631 cm_tophits_SortForOverlapRemoval(h1);
3632 if((status = cm_tophits_RemoveOrMarkOverlaps(h1, /*do_clans_only=*/FALSE, errbuf)) != eslOK) cm_Fail(errbuf);
3633 cm_tophits_SortByEvalue(h1);
3634 if (strcmp(h1->hit[0]->name, "Bfirst") != 0) esl_fatal("sort 4 failed (first is %s = %f)", h1->hit[0]->name, h1->hit[0]->score);
3635 if (strcmp(h1->hit[1]->name, "first") != 0) esl_fatal("sort 4 failed (second is %s = %f)", h1->hit[1]->name, h1->hit[1]->score);
3636 if (strcmp(h1->hit[2]->name, "Bsecond") != 0) esl_fatal("sort 4 failed (third is %s = %f)", h1->hit[2]->name, h1->hit[2]->score);
3637 if (strcmp(h1->hit[3]->name, "second") != 0) esl_fatal("sort 4 failed (fourth is %s = %f)", h1->hit[3]->name, h1->hit[3]->score);
3638 if (strcmp(h1->hit[4]->name, "Bthird") != 0) esl_fatal("sort 4 failed (fifth is %s = %f)", h1->hit[4]->name, h1->hit[4]->score);
3639 if (strcmp(h1->hit[5]->name, "third") != 0) esl_fatal("sort 4 failed (sixth is %s = %f)", h1->hit[5]->name, h1->hit[5]->score);
3640 if (strcmp(h1->hit[4*N+6]->name, "thirdtolast") != 0) esl_fatal("sort 4 failed (sixth to last is %s = %f)", h1->hit[4*N+6]->name, h1->hit[4*N+6]->score);
3641 if (strcmp(h1->hit[4*N+7]->name, "Bthirdtolast") != 0) esl_fatal("sort 4 failed (fifth to last is %s = %f)", h1->hit[4*N+7]->name, h1->hit[4*N+7]->score);
3642 if (strcmp(h1->hit[4*N+8]->name, "secondtolast") != 0) esl_fatal("sort 4 failed (fourth to last is %s = %f)", h1->hit[4*N+8]->name, h1->hit[4*N+8]->score);
3643 if (strcmp(h1->hit[4*N+9]->name, "Bsecondtolast") != 0) esl_fatal("sort 4 failed (third to last is %s = %f)", h1->hit[4*N+9]->name, h1->hit[4*N+9]->score);
3644 if (strcmp(h1->hit[4*N+10]->name, "last") != 0) esl_fatal("sort 4 failed (second to last is %s = %f)", h1->hit[4*N+10]->name, h1->hit[4*N+10]->score);
3645 if (strcmp(h1->hit[4*N+11]->name, "Blast") != 0) esl_fatal("sort 4 failed (last is %s = %f)", h1->hit[4*N+11]->name, h1->hit[4*N+11]->score);
3646 if (h1->hit[0]->flags & CM_HIT_IS_REMOVED_DUPLICATE) esl_fatal("RemoveOrMarkOverlaps failed 11");
3647 if (h1->hit[1]->flags & CM_HIT_IS_REMOVED_DUPLICATE) esl_fatal("RemoveOrMarkOverlaps failed 12");
3648 if (h1->hit[4*N+6]->flags & CM_HIT_IS_REMOVED_DUPLICATE) esl_fatal("RemoveOrMarkOverlaps failed 13");
3649 if (h1->hit[4*N+7]->flags & CM_HIT_IS_REMOVED_DUPLICATE) esl_fatal("RemoveOrMarkOverlaps failed 14");
3650
3651 /* second sort for markup and make sure we DO mark up overlaps of different models */
3652 cm_tophits_SortForOverlapMarkup(h1, /*do_clans_only=*/FALSE);
3653 if((status = cm_tophits_RemoveOrMarkOverlaps(h1, /*do_clans_only=*/FALSE, errbuf)) != eslOK) cm_Fail(errbuf);
3654 cm_tophits_SortByEvalue(h1);
3655 if (strcmp(h1->hit[0]->name, "Bfirst") != 0) esl_fatal("sort 5 failed (first is %s = %f)", h1->hit[0]->name, h1->hit[0]->score);
3656 if (strcmp(h1->hit[1]->name, "first") != 0) esl_fatal("sort 5 failed (second is %s = %f)", h1->hit[1]->name, h1->hit[1]->score);
3657 if (strcmp(h1->hit[2]->name, "Bsecond") != 0) esl_fatal("sort 5 failed (third is %s = %f)", h1->hit[2]->name, h1->hit[2]->score);
3658 if (strcmp(h1->hit[3]->name, "second") != 0) esl_fatal("sort 5 failed (fourth is %s = %f)", h1->hit[3]->name, h1->hit[3]->score);
3659 if (strcmp(h1->hit[4]->name, "Bthird") != 0) esl_fatal("sort 5 failed (fifth is %s = %f)", h1->hit[4]->name, h1->hit[4]->score);
3660 if (strcmp(h1->hit[5]->name, "third") != 0) esl_fatal("sort 5 failed (sixth is %s = %f)", h1->hit[5]->name, h1->hit[5]->score);
3661 if (strcmp(h1->hit[4*N+6]->name, "thirdtolast") != 0) esl_fatal("sort 5 failed (sixth to last is %s = %f)", h1->hit[4*N+6]->name, h1->hit[4*N+6]->score);
3662 if (strcmp(h1->hit[4*N+7]->name, "Bthirdtolast") != 0) esl_fatal("sort 5 failed (fifth to last is %s = %f)", h1->hit[4*N+7]->name, h1->hit[4*N+7]->score);
3663 if (strcmp(h1->hit[4*N+8]->name, "secondtolast") != 0) esl_fatal("sort 5 failed (fourth to last is %s = %f)", h1->hit[4*N+8]->name, h1->hit[4*N+8]->score);
3664 if (strcmp(h1->hit[4*N+9]->name, "Bsecondtolast") != 0) esl_fatal("sort 5 failed (third to last is %s = %f)", h1->hit[4*N+9]->name, h1->hit[4*N+9]->score);
3665 if (strcmp(h1->hit[4*N+10]->name, "last") != 0) esl_fatal("sort 5 failed (second to last is %s = %f)", h1->hit[4*N+10]->name, h1->hit[4*N+10]->score);
3666 if (strcmp(h1->hit[4*N+11]->name, "Blast") != 0) esl_fatal("sort 5 failed (last is %s = %f)", h1->hit[4*N+11]->name, h1->hit[4*N+11]->score);
3667 if ( h1->hit[0]->flags & CM_HIT_IS_REMOVED_DUPLICATE) esl_fatal("RemoveOrMarkOverlaps failed 15");
3668 if ( h1->hit[0]->flags & CM_HIT_IS_MARKED_OVERLAP) esl_fatal("RemoveOrMarkOverlaps failed 16");
3669 if ( h1->hit[1]->flags & CM_HIT_IS_REMOVED_DUPLICATE) esl_fatal("RemoveOrMarkOverlaps failed 17");
3670 if (! (h1->hit[1]->flags & CM_HIT_IS_MARKED_OVERLAP)) esl_fatal("RemoveOrMarkOverlaps failed 18");
3671 if ( h1->hit[4*N+6]->flags & CM_HIT_IS_REMOVED_DUPLICATE) esl_fatal("RemoveOrMarkOverlaps failed 19");
3672 if ( h1->hit[4*N+6]->flags & CM_HIT_IS_MARKED_OVERLAP) esl_fatal("RemoveOrMarkOverlaps failed 20");
3673 if ( h1->hit[4*N+7]->flags & CM_HIT_IS_REMOVED_DUPLICATE) esl_fatal("RemoveOrMarkOverlaps failed 21");
3674 if (! (h1->hit[4*N+7]->flags & CM_HIT_IS_MARKED_OVERLAP)) esl_fatal("RemoveOrMarkOverlaps failed 22");
3675
3676 if (h1->hit[1]->any_oidx != (4*N)+9-1) esl_fatal("RemoveOrMarkOverlaps failed 23");
3677 if (h1->hit[1]->win_oidx != (4*N)+9-1) esl_fatal("RemoveOrMarkOverlaps failed 24");
3678
3679 /* One final test of the overlap any_oidx and win_oidx values,
3680 * with a fabricated example of a rare case where they're not identical.
3681 * We do this in two passes, the first pass will only do 5 hits and
3682 * will use the function remove_or_mark_overlaps_one_seq_memeff(),
3683 * the second will do 5005 hits and so will use the function
3684 * remove_or_mark_overlaps_one_seq_fast().
3685 */
3686 int p, z;
3687 for(p = 0; p <= 1; p++) {
3688 if(p > 0) {
3689 cm_tophits_Destroy(h5);
3690 h5 = cm_tophits_Create();
3691 }
3692
3693 cm_tophits_CreateNextHit(h5, &hit);
3694 esl_strdup("hit1", -1, &(hit->name));
3695 hit->start = 1;
3696 hit->stop = 200;
3697 hit->score = 100.;
3698 hit->evalue = 0.0001;
3699 hit->cm_idx = 0;
3700 hit->seq_idx = 0;
3701 hit->srcL = 20000;
3702
3703 cm_tophits_CreateNextHit(h5, &hit);
3704 esl_strdup("hit2", -1, &(hit->name));
3705 hit->start = 1;
3706 hit->stop = 300;
3707 hit->score = 90.;
3708 hit->evalue = 0.001;
3709 hit->cm_idx = 1;
3710 hit->seq_idx = 0;
3711 hit->srcL = 20000;
3712
3713 cm_tophits_CreateNextHit(h5, &hit);
3714 esl_strdup("hit3", -1, &(hit->name));
3715 hit->start = 305;
3716 hit->stop = 500;
3717 hit->score = 80.;
3718 hit->evalue = 0.01;
3719 hit->cm_idx = 2;
3720 hit->seq_idx = 0;
3721 hit->srcL = 20000;
3722
3723 cm_tophits_CreateNextHit(h5, &hit);
3724 esl_strdup("hit4", -1, &(hit->name));
3725 hit->start = 201;
3726 hit->stop = 500;
3727 hit->score = 70.;
3728 hit->evalue = 0.1;
3729 hit->cm_idx = 3;
3730 hit->seq_idx = 0;
3731 hit->srcL = 20000;
3732
3733 cm_tophits_CreateNextHit(h5, &hit);
3734 esl_strdup("hit5", -1, &(hit->name));
3735 hit->start = 201;
3736 hit->stop = 299;
3737 hit->score = 60.;
3738 hit->evalue = 1;
3739 hit->cm_idx = 4;
3740 hit->seq_idx = 0;
3741 hit->srcL = 20000;
3742
3743 if(p > 0) {
3744 for(z = 0; z < 10000; z++) {
3745 cm_tophits_CreateNextHit(h5, &hit);
3746 esl_strdup("extrahit", -1, &(hit->name));
3747 hit->start = 1000 + z;
3748 hit->stop = 1000 + z;
3749 hit->score = 30.;
3750 hit->evalue = 5;
3751 hit->cm_idx = 5;
3752 hit->seq_idx = 0;
3753 hit->srcL = 20000;
3754 }
3755 }
3756
3757 cm_tophits_SortForOverlapRemoval(h5);
3758 if((status = cm_tophits_RemoveOrMarkOverlaps(h5, /*do_clans_only=*/FALSE, errbuf)) != eslOK) cm_Fail(errbuf);
3759 cm_tophits_SortForOverlapMarkup(h5, /*do_clans_only=*/FALSE);
3760 if((status = cm_tophits_RemoveOrMarkOverlaps(h5, /*do_clans_only=*/FALSE, errbuf)) != eslOK) cm_Fail(errbuf);
3761
3762 cm_tophits_SortByEvalue(h5);
3763 if (strcmp(h5->hit[0]->name, "hit1") != 0) esl_fatal("sort 6 failed pass %d (first is %s = %f)", p+1, h5->hit[0]->name, h5->hit[0]->score);
3764 if (strcmp(h5->hit[1]->name, "hit2") != 0) esl_fatal("sort 6 failed pass %d (first is %s = %f)", p+1, h5->hit[1]->name, h5->hit[1]->score);
3765 if (strcmp(h5->hit[2]->name, "hit3") != 0) esl_fatal("sort 6 failed pass %d (first is %s = %f)", p+1, h5->hit[2]->name, h5->hit[2]->score);
3766 if (strcmp(h5->hit[3]->name, "hit4") != 0) esl_fatal("sort 6 failed pass %d (first is %s = %f)", p+1, h5->hit[3]->name, h5->hit[3]->score);
3767 if (strcmp(h5->hit[4]->name, "hit5") != 0) esl_fatal("sort 6 failed pass %d (first is %s = %f)", p+1, h5->hit[4]->name, h5->hit[4]->score);
3768
3769 if (h5->hit[0]->any_oidx != -1) esl_fatal("RemoveOrMarkOverlaps failed 25 (pass %d)", p+1);
3770 if (h5->hit[0]->win_oidx != -1) esl_fatal("RemoveOrMarkOverlaps failed 26 (pass %d)", p+1);
3771
3772 if (h5->hit[1]->any_oidx != 0) esl_fatal("RemoveOrMarkOverlaps failed 27 (pass %d)", p+1);
3773 if (h5->hit[1]->any_oidx != 0) esl_fatal("RemoveOrMarkOverlaps failed 28 (pass %d)", p+1);
3774
3775 if (h5->hit[2]->any_oidx != -1) esl_fatal("RemoveOrMarkOverlaps failed 29 (pass %d)", p+1);
3776 if (h5->hit[2]->win_oidx != -1) esl_fatal("RemoveOrMarkOverlaps failed 30 (pass %d)", p+1);
3777
3778 if (h5->hit[3]->any_oidx != 1) esl_fatal("RemoveOrMarkOverlaps failed 31 (pass %d)", p+1);
3779 if (h5->hit[3]->win_oidx != 2) esl_fatal("RemoveOrMarkOverlaps failed 32 (pass %d)", p+1);
3780
3781 if (h5->hit[4]->any_oidx != 1) esl_fatal("RemoveOrMarkOverlaps failed 33 (pass %d)", p+1);
3782 if (h5->hit[4]->win_oidx != -1) esl_fatal("RemoveOrMarkOverlaps failed 34 (pass %d)", p+1);
3783
3784 } /* end of 'for(p = 0; p <= 1; p++)' */
3785
3786 if (cm_tophits_GetMaxNameLength(h1) != strlen(name)) esl_fatal("GetMaxNameLength() failed");
3787
3788 cm_tophits_Destroy(h1);
3789 cm_tophits_Destroy(h2);
3790 cm_tophits_Destroy(h3);
3791 cm_tophits_Destroy(h4);
3792 cm_tophits_Destroy(h5);
3793 esl_randomness_Destroy(r);
3794 esl_getopts_Destroy(go);
3795 return eslOK;
3796 }
3797 #endif /*CM_TOPHITS_TESTDRIVE*/
3798
3799