Debian Codegraph @

hmmer/src/p7_tophits.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* P7_TOPHITS: implementation of ranked list of top-scoring hits
2 *
3 * Contents:
4 * 1. The P7_TOPHITS object.
5 * 2. Standard (human-readable) output of pipeline results.
6 * 3. Tabular (parsable) output of pipeline results.
7 * 4. Benchmark driver.
8 * 5. Test driver.
9 */
10 #include <p7_config.h>
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include <limits.h>
15
16 #include "easel.h"
17 #include "hmmer.h"
18
19 /*****************************************************************
20 *= 1. The P7_TOPHITS object
21 *****************************************************************/
22
23 /* Function: p7_tophits_Create()
24 * Synopsis: Allocate a hit list.
25 *
26 * Purpose: Allocates a new <P7_TOPHITS> hit list and return a pointer
27 * to it.
28 *
29 * Throws: <NULL> on allocation failure.
30 */
31 P7_TOPHITS *
32 p7_tophits_Create(void)
33 {
34 P7_TOPHITS *h = NULL;
35 int default_nalloc = 256;
36 int status;
37
38 ESL_ALLOC(h, sizeof(P7_TOPHITS));
39 h->hit = NULL;
40 h->unsrt = NULL;
41
42 ESL_ALLOC(h->hit, sizeof(P7_HIT *) * default_nalloc);
43 ESL_ALLOC(h->unsrt, sizeof(P7_HIT) * default_nalloc);
44 h->Nalloc = default_nalloc;
45 h->N = 0;
46 h->nreported = 0;
47 h->nincluded = 0;
48 h->is_sorted_by_sortkey = TRUE; /* but only because there's 0 hits */
49 h->is_sorted_by_seqidx = FALSE;
50 h->hit[0] = h->unsrt; /* if you're going to call it "sorted" when it contains just one hit, you need this */
51 return h;
52
53 ERROR:
54 p7_tophits_Destroy(h);
55 return NULL;
56 }
57
58
59 /* Function: p7_tophits_Clone()
60 * Synopsis: Create a duplicate of an existing <P7_TOPHITS> object.
61 *
62 * Purpose: Creates a duplicate of the existing <P7_TOPHITS> object <h>.
63 *
64 * Returns: ptr to the duplicate <P7_TOPHITS> object.
65 *
66 * Throws: <NULL> on allocation failure.
67 */
68 P7_TOPHITS *
69 p7_tophits_Clone(const P7_TOPHITS *h)
70 {
71 P7_TOPHITS *h2 = NULL;
72 int i;
73 int status;
74
75 ESL_ALLOC(h2, sizeof(P7_TOPHITS));
76
77 h2->nreported = h->nreported;
78 h2->nincluded = h->nincluded;
79 h2->is_sorted_by_sortkey = h->is_sorted_by_sortkey;
80 h2->is_sorted_by_seqidx = h->is_sorted_by_seqidx;
81
82 h2->N = h->N;
83 h2->Nalloc = h->N;
84
85 h2->hit = NULL;
86 h2->unsrt = NULL;
87
88 ESL_ALLOC(h2->hit, sizeof(P7_HIT *) * h2->N);
89 ESL_ALLOC(h2->unsrt, sizeof(P7_HIT) * h2->N);
90
91 // set NULL pointers everywhere before allocating to avoid double free on error
92 for (i = 0; i < h2->N; i++) {
93 h2->unsrt[i].name = NULL;
94 h2->unsrt[i].acc = NULL;
95 h2->unsrt[i].desc = NULL;
96 h2->unsrt[i].dcl = NULL;
97 }
98
99 // copy domains and update offsets in the sorted hit array
100 for (i = 0; i < h2->N; i++) {
101 if ((status = p7_hit_Copy(&(h->unsrt[i]), &(h2->unsrt[i]))) != eslOK) goto ERROR;
102 h2->hit[i] = h2->unsrt + (h->hit[i] - h->unsrt);
103 }
104
105 return h2;
106
107 ERROR:
108 p7_tophits_Destroy(h2);
109 return NULL;
110 }
111
112
113 /* Function: p7_tophits_Grow()
114 * Synopsis: Reallocates a larger hit list, if needed.
115 *
116 * Purpose: If list <h> cannot hold another hit, doubles
117 * the internal allocation.
118 *
119 * Returns: <eslOK> on success.
120 *
121 * Throws: <eslEMEM> on allocation failure; in this case,
122 * the data in <h> are unchanged.
123 */
124 int
125 p7_tophits_Grow(P7_TOPHITS *h)
126 {
127 void *p;
128 P7_HIT *ori = h->unsrt;
129 uint64_t Nalloc = h->Nalloc * 2; /* grow by doubling */
130 int i;
131 int status;
132
133 if (h->N < h->Nalloc) return eslOK; /* we have enough room for another hit */
134
135 ESL_RALLOC(h->hit, p, sizeof(P7_HIT *) * Nalloc);
136 ESL_RALLOC(h->unsrt, p, sizeof(P7_HIT) * Nalloc);
137
138 /* If we grow a sorted list, we have to translate the pointers
139 * in h->hit, because h->unsrt might have just moved in memory.
140 */
141 if (h->is_sorted_by_seqidx || h->is_sorted_by_sortkey)
142 {
143 for (i = 0; i < h->N; i++)
144 h->hit[i] = h->unsrt + (h->hit[i] - ori);
145 }
146
147 h->Nalloc = Nalloc;
148 return eslOK;
149
150 ERROR:
151 return eslEMEM;
152 }
153
154
155 /* Function: p7_tophits_CreateNextHit()
156 * Synopsis: Get pointer to new structure for recording a hit.
157 *
158 * Purpose: Ask the top hits object <h> to do any necessary
159 * internal allocation and bookkeeping to add a new,
160 * empty hit to its list; return a pointer to
161 * this new <P7_HIT> structure for data to be filled
162 * in by the caller.
163 *
164 * Returns: <eslOK> on success.
165 *
166 * Throws: <eslEMEM> on allocation error.
167 */
168 int
169 p7_tophits_CreateNextHit(P7_TOPHITS *h, P7_HIT **ret_hit)
170 {
171 P7_HIT *hit = NULL;
172 int status;
173
174 if ((status = p7_tophits_Grow(h)) != eslOK) goto ERROR;
175
176 hit = &(h->unsrt[h->N]);
177 h->N++;
178 if (h->N >= 2)
179 {
180 h->is_sorted_by_seqidx = FALSE;
181 h->is_sorted_by_sortkey = FALSE;
182 }
183
184 hit->name = NULL;
185 hit->acc = NULL;
186 hit->desc = NULL;
187 hit->sortkey = 0.0;
188
189 hit->score = 0.0;
190 hit->pre_score = 0.0;
191 hit->sum_score = 0.0;
192
193 hit->lnP = 0.0;
194 hit->pre_lnP = 0.0;
195 hit->sum_lnP = 0.0;
196
197 hit->ndom = 0;
198 hit->nexpected = 0.0;
199 hit->nregions = 0;
200 hit->nclustered = 0;
201 hit->noverlaps = 0;
202 hit->nenvelopes = 0;
203
204 hit->flags = p7_HITFLAGS_DEFAULT;
205 hit->nreported = 0;
206 hit->nincluded = 0;
207 hit->best_domain = -1;
208 hit->dcl = NULL;
209 hit->offset = 0;
210
211 *ret_hit = hit;
212 return eslOK;
213
214 ERROR:
215 *ret_hit = NULL;
216 return status;
217 }
218
219
220
221 /* Function: p7_tophits_Add()
222 * Synopsis: Add a hit to the top hits list.
223 *
224 * Purpose: Adds a hit to the top hits list <h>.
225 *
226 * <name>, <acc>, and <desc> are copied, so caller may free
227 * them if it likes.
228 *
229 * Only the pointer <ali> is kept. Caller turns over memory
230 * management of <ali> to the top hits object; <ali> will
231 * be free'd when the top hits structure is free'd.
232 *
233 * Args: h - active top hit list
234 * name - name of target
235 * acc - accession of target (may be NULL)
236 * desc - description of target (may be NULL)
237 * sortkey - value to sort by: bigger is better
238 * score - score of this hit
239 * lnP - log P-value of this hit
240 * mothersc - score of parent whole sequence
241 * mother_lnP - log P-value of parent whole sequence
242 * sqfrom - 1..L pos in target seq of start
243 * sqto - 1..L pos; sqfrom > sqto if rev comp
244 * sqlen - length of sequence, L
245 * hmmfrom - 0..M+1 pos in HMM of start
246 * hmmto - 0..M+1 pos in HMM of end
247 * hmmlen - length of HMM, M
248 * domidx - number of this domain
249 * ndom - total # of domains in sequence
250 * ali - optional printable alignment info
251 *
252 * Returns: <eslOK> on success.
253 *
254 * Throws: <eslEMEM> if reallocation failed.
255 *
256 * Note: Is this actually used anywhere? (SRE, 10 Dec 08)
257 * I think it's not up to date.
258 *
259 * That's right. This function is obsolete.
260 * But it is used in benchmark and test code, so you can't
261 * delete it yet; benchmarks and test code should be
262 * revised (SRE, 26 Oct 09)
263 */
264 int
265 p7_tophits_Add(P7_TOPHITS *h,
266 char *name, char *acc, char *desc,
267 double sortkey,
268 float score, double lnP,
269 float mothersc, double mother_lnP,
270 int sqfrom, int sqto, int sqlen,
271 int hmmfrom, int hmmto, int hmmlen,
272 int domidx, int ndom,
273 P7_ALIDISPLAY *ali)
274 {
275 int status;
276
277 if ((status = p7_tophits_Grow(h)) != eslOK) return status;
278 if ((status = esl_strdup(name, -1, &(h->unsrt[h->N].name))) != eslOK) return status;
279 if ((status = esl_strdup(acc, -1, &(h->unsrt[h->N].acc))) != eslOK) return status;
280 if ((status = esl_strdup(desc, -1, &(h->unsrt[h->N].desc))) != eslOK) return status;
281 h->unsrt[h->N].sortkey = sortkey;
282 h->unsrt[h->N].score = score;
283 h->unsrt[h->N].pre_score = 0.0;
284 h->unsrt[h->N].sum_score = 0.0;
285 h->unsrt[h->N].lnP = lnP;
286 h->unsrt[h->N].pre_lnP = 0.0;
287 h->unsrt[h->N].sum_lnP = 0.0;
288 h->unsrt[h->N].nexpected = 0;
289 h->unsrt[h->N].nregions = 0;
290 h->unsrt[h->N].nclustered = 0;
291 h->unsrt[h->N].noverlaps = 0;
292 h->unsrt[h->N].nenvelopes = 0;
293 h->unsrt[h->N].ndom = ndom;
294 h->unsrt[h->N].flags = 0;
295 h->unsrt[h->N].nreported = 0;
296 h->unsrt[h->N].nincluded = 0;
297 h->unsrt[h->N].best_domain= 0;
298 h->unsrt[h->N].dcl = NULL;
299 h->N++;
300
301 if (h->N >= 2) {
302 h->is_sorted_by_seqidx = FALSE;
303 h->is_sorted_by_sortkey = FALSE;
304 }
305 return eslOK;
306 }
307
308 /* hit_sorter(): qsort's pawn, below */
309 static int
310 hit_sorter_by_sortkey(const void *vh1, const void *vh2)
311 {
312 P7_HIT *h1 = *((P7_HIT **) vh1); /* don't ask. don't change. Don't Panic. */
313 P7_HIT *h2 = *((P7_HIT **) vh2);
314 int c;
315
316 if (h1->sortkey < h2->sortkey) return 1;
317 else if (h1->sortkey > h2->sortkey) return -1;
318 else {
319 if ( (c = strcmp(h1->name, h2->name)) != 0) return c;
320
321 /* if on different strand, the positive strand goes first, else use position */
322 int dir1 = (h1->dcl[0].iali < h1->dcl[0].jali ? 1 : -1);
323 int dir2 = (h2->dcl[0].iali < h2->dcl[0].jali ? 1 : -1);
324 if (dir1 != dir2) return dir2; // so if dir1 is pos (1), and dir2 is neg (-1), this will return -1, placing h1 before h2; otherwise, vice versa
325 else {
326 if (h1->dcl[0].iali > h2->dcl[0].iali) return 1;
327 else if(h1->dcl[0].iali < h2->dcl[0].iali) return -1;
328 else return 0;
329 }
330 }
331 }
332
333 /* used before duplicate hit removal in an nhmmer longtarget pipeline: */
334 static int
335 hit_sorter_by_seqidx_aliposition(const void *vh1, const void *vh2)
336 {
337 P7_HIT *h1 = *((P7_HIT **) vh1);
338 P7_HIT *h2 = *((P7_HIT **) vh2);
339 int64_t s1, e1, s2, e2;
340 int dir1, dir2;
341
342 if (h1->seqidx > h2->seqidx) return 1; /* first key, seq_idx (unique id for sequences), low to high */
343 else if (h1->seqidx < h2->seqidx) return -1;
344
345 // if on different strand, the positive strand goes first, else use position
346 s1 = h1->dcl[0].iali; e1 = h1->dcl[0].jali; if (s1 < e1) { dir1 = 1; } else { dir1 = -1; ESL_SWAP(s1, e1, int64_t); }
347 s2 = h2->dcl[0].iali; e2 = h2->dcl[0].jali; if (s2 < e2) { dir2 = 1; } else { dir2 = -1; ESL_SWAP(s2, e2, int64_t); }
348
349 if (dir1 != dir2) return dir2; // so if dir1 is pos (1), and dir2 is neg (-1), this will return -1, placing h1 before h2; otherwise, vice versa
350
351 if (s1 > s2) return 1; // sort primarily from smallest to largest start pos
352 else if (s1 < s2) return -1;
353 else if (e1 < e2) return 1; // secondarily, larger to smallest end position (i.e. longer hit first)
354 else if (e1 > e2) return -1;
355 else return 0;
356 }
357
358 /* similarly, for nhmmscan longtarget pipeline: */
359 static int
360 hit_sorter_by_modelname_aliposition(const void *vh1, const void *vh2)
361 {
362 P7_HIT *h1 = *((P7_HIT **) vh1);
363 P7_HIT *h2 = *((P7_HIT **) vh2);
364 int64_t s1, e1, s2, e2;
365 int dir1, dir2;
366 int res;
367
368 if ( ( res = esl_strcmp(h1->name, h2->name)) != 0 ) return res; /* first key, seq_idx (unique id for sequences), low to high */
369
370 s1 = h1->dcl[0].iali; e1 = h1->dcl[0].jali; if (s1 < e1) { dir1 = 1; } else { dir1 = -1; ESL_SWAP(s1, e1, int64_t); }
371 s2 = h2->dcl[0].iali; e2 = h2->dcl[0].jali; if (s2 < e2) { dir2 = 1; } else { dir2 = -1; ESL_SWAP(s2, e2, int64_t); }
372
373 if (dir1 != dir2) return dir2;
374
375 if (s1 > s2) return 1;
376 else if (s1 < s2) return -1;
377 else if (e1 < e2) return 1;
378 else if (e1 > e2) return -1;
379 else return 0;
380 }
381
382
383 /* Function: p7_tophits_SortBySortkey()
384 * Synopsis: Sorts a hit list.
385 *
386 * Purpose: Sorts a top hit list. After this call,
387 * <h->hit[i]> points to the i'th ranked
388 * <P7_HIT> for all <h->N> hits.
389 *
390 * Returns: <eslOK> on success.
391 */
392 int
393 p7_tophits_SortBySortkey(P7_TOPHITS *h)
394 {
395 int i;
396
397 if (h->is_sorted_by_sortkey) return eslOK;
398 for (i = 0; i < h->N; i++) h->hit[i] = h->unsrt + i;
399 if (h->N > 1) qsort(h->hit, h->N, sizeof(P7_HIT *), hit_sorter_by_sortkey);
400 h->is_sorted_by_seqidx = FALSE;
401 h->is_sorted_by_sortkey = TRUE;
402 return eslOK;
403 }
404
405
406 /* Function: p7_tophits_SortBySeqidxAndAlipos()
407 * Synopsis: Sorts a hit list by sequence index and position for nhmmer.
408 * sequence at which the hit's first domain begins (used in nhmmer)
409 *
410 * Purpose: Sorts a top hit list, suitable for subsequent hit
411 * duplicate removal by `p7_tophits_RemoveDuplicates()` in
412 * an nhmmer longtarget pipeline: by sequence index, then
413 * by strand (+ before - hits), then by smallest-to-largest
414 * start position (in watson coords), then by
415 * largest-to-smallest end position (in watson
416 * coords). Correctness of logic of
417 * `p7_tophits_RemoveDuplicates()` is sensitive to this
418 * ordering.
419 *
420 * After this call, <h->hit[i]> points to the i'th ranked
421 * <P7_HIT> for all <h->N> hits.
422 *
423 * Returns: <eslOK> on success.
424 */
425 int
426 p7_tophits_SortBySeqidxAndAlipos(P7_TOPHITS *h)
427 {
428 int i;
429
430 if (h->is_sorted_by_seqidx) return eslOK;
431 for (i = 0; i < h->N; i++) h->hit[i] = h->unsrt + i;
432 if (h->N > 1) qsort(h->hit, h->N, sizeof(P7_HIT *), hit_sorter_by_seqidx_aliposition);
433 h->is_sorted_by_sortkey = FALSE;
434 h->is_sorted_by_seqidx = TRUE;
435 return eslOK;
436 }
437
438 /* Function: p7_tophits_SortByModelnameAndAlipos()
439 * Synopsis: Sorts a hit list by model name and position for nhmmscan.
440 *
441 * Purpose: Like `p7_tophits_SortBySeqidxAndAlipos()` but for
442 * nhmmscan, which sorts on model name not target seq idx.
443 *
444 * Returns: <eslOK> on success.
445 */
446 int
447 p7_tophits_SortByModelnameAndAlipos(P7_TOPHITS *h)
448 {
449 int i;
450
451 if (h->is_sorted_by_seqidx) return eslOK;
452 for (i = 0; i < h->N; i++) h->hit[i] = h->unsrt + i;
453 if (h->N > 1) qsort(h->hit, h->N, sizeof(P7_HIT *), hit_sorter_by_modelname_aliposition);
454 h->is_sorted_by_sortkey = FALSE;
455 h->is_sorted_by_seqidx = TRUE;
456 return eslOK;
457 }
458
459
460 /* Function: p7_tophits_Merge()
461 * Synopsis: Merge two top hits lists.
462 *
463 * Purpose: Merge <h2> into <h1>. Upon return, <h1>
464 * contains the sorted, merged list. <h2>
465 * is effectively destroyed; caller should
466 * not access it further, and may as well free
467 * it immediately.
468 *
469 * Returns: <eslOK> on success.
470 *
471 * Throws: <eslEMEM> on allocation failure, and
472 * both <h1> and <h2> remain valid.
473 */
474 int
475 p7_tophits_Merge(P7_TOPHITS *h1, P7_TOPHITS *h2)
476 {
477 void *p;
478 P7_HIT **new_hit = NULL;
479 P7_HIT *ori1 = h1->unsrt; /* original base of h1's data */
480 P7_HIT *new2;
481 int i,j,k;
482 uint64_t Nalloc = h1->N + h2->N;
483 int status;
484
485 if(h2->N <= 0) return eslOK;
486
487 /* Make sure the two lists are sorted */
488 if ((status = p7_tophits_SortBySortkey(h1)) != eslOK) goto ERROR;
489 if ((status = p7_tophits_SortBySortkey(h2)) != eslOK) goto ERROR;
490
491 /* Attempt our allocations, so we fail early if we fail.
492 * Reallocating h1->unsrt screws up h1->hit, so fix it.
493 */
494 ESL_RALLOC(h1->unsrt, p, sizeof(P7_HIT) * Nalloc);
495 ESL_ALLOC (new_hit, sizeof(P7_HIT *) * Nalloc);
496 for (i = 0; i < h1->N; i++)
497 h1->hit[i] = h1->unsrt + (h1->hit[i] - ori1);
498
499 /* Append h2's unsorted data array to h1. h2's data begin at <new2> */
500 new2 = h1->unsrt + h1->N;
501 memcpy(new2, h2->unsrt, sizeof(P7_HIT) * h2->N);
502
503 /* Merge the sorted hit lists */
504 for (i=0,j=0,k=0; i < h1->N && j < h2->N ; k++)
505 new_hit[k] = (hit_sorter_by_sortkey(&h1->hit[i], &h2->hit[j]) > 0) ? new2 + (h2->hit[j++] - h2->unsrt) : h1->hit[i++];
506 while (i < h1->N) new_hit[k++] = h1->hit[i++];
507 while (j < h2->N) new_hit[k++] = new2 + (h2->hit[j++] - h2->unsrt);
508
509 /* h2 now turns over management of name, acc, desc memory to h1;
510 * nullify its pointers, to prevent double free. */
511 for (i = 0; i < h2->N; i++)
512 {
513 h2->unsrt[i].name = NULL;
514 h2->unsrt[i].acc = NULL;
515 h2->unsrt[i].desc = NULL;
516 h2->unsrt[i].dcl = NULL;
517 }
518
519 /* Construct the new grown h1 */
520 free(h1->hit);
521 h1->hit = new_hit;
522 h1->Nalloc = Nalloc;
523 h1->N += h2->N;
524 /* and is_sorted is TRUE, as a side effect of p7_tophits_Sort() above. */
525 return eslOK;
526
527 ERROR:
528 if (new_hit != NULL) free(new_hit);
529 return status;
530 }
531
532 /* Function: p7_tophits_GetMaxPositionLength()
533 * Synopsis: Returns maximum position length in hit list (targets).
534 *
535 * Purpose: Returns the length of the longest hit location (start/end)
536 * of all the registered hits, in chars. This is useful when
537 * deciding how to format output.
538 *
539 * The maximum is taken over all registered hits. This
540 * opens a possible side effect: caller might print only
541 * the top hits, and the max name length in these top hits
542 * may be different than the max length over all the hits.
543 *
544 * Used specifically for nhmmer output, so expects only one
545 * domain per hit
546 *
547 * If there are no hits in <h>, or none of the
548 * hits have names, returns 0.
549 */
550 int
551 p7_tophits_GetMaxPositionLength(P7_TOPHITS *h)
552 {
553 int64_t i;
554 int max = 0;
555 int n;
556 char buffer [26]; // SRE: this was [13], not sufficient for int64.
557 // rule of thumb: 3*sizeof(int) + 2
558
559 for (i = 0; i < h->N; i++) {
560 if (h->unsrt[i].dcl[0].iali > 0) {
561 n = snprintf (buffer, 26, "%" PRId64 "", h->unsrt[i].dcl[0].iali); // 26 matches buffer[26] allocation
562 max = ESL_MAX(n, max);
563 n = snprintf (buffer, 26, "%" PRId64 "", h->unsrt[i].dcl[0].jali);
564 max = ESL_MAX(n, max);
565 }
566 }
567 return max;
568 }
569
570 /* Function: p7_tophits_GetMaxNameLength()
571 * Synopsis: Returns maximum name length in hit list (targets).
572 *
573 * Purpose: Returns the maximum name length of all the registered
574 * hits, in chars. This is useful when deciding how to
575 * format output.
576 *
577 * The maximum is taken over all registered hits. This
578 * opens a possible side effect: caller might print only
579 * the top hits, and the max name length in these top hits
580 * may be different than the max length over all the hits.
581 *
582 * If there are no hits in <h>, or none of the
583 * hits have names, returns 0.
584 */
585 int
586 p7_tophits_GetMaxNameLength(P7_TOPHITS *h)
587 {
588 int i, max, n;
589 for (max = 0, i = 0; i < h->N; i++)
590 if (h->unsrt[i].name != NULL) {
591 n = strlen(h->unsrt[i].name);
592 max = ESL_MAX(n, max);
593 }
594 return max;
595 }
596
597 /* Function: p7_tophits_GetMaxAccessionLength()
598 * Synopsis: Returns maximum accession length in hit list (targets).
599 *
600 * Purpose: Same as <p7_tophits_GetMaxNameLength()>, but for
601 * accessions. If there are no hits in <h>, or none
602 * of the hits have accessions, returns 0.
603 */
604 int
605 p7_tophits_GetMaxAccessionLength(P7_TOPHITS *h)
606 {
607 int i, max, n;
608 for (max = 0, i = 0; i < h->N; i++)
609 if (h->unsrt[i].acc != NULL) {
610 n = strlen(h->unsrt[i].acc);
611 max = ESL_MAX(n, max);
612 }
613 return max;
614 }
615
616 /* Function: p7_tophits_GetMaxShownLength()
617 * Synopsis: Returns max shown name/accession length in hit list.
618 *
619 * Purpose: Same as <p7_tophits_GetMaxNameLength()>, but
620 * for the case when --acc is on, where
621 * we show accession if one is available, and
622 * fall back to showing the name if it is not.
623 * Returns the max length of whatever is being
624 * shown as the reported "name".
625 */
626 int
627 p7_tophits_GetMaxShownLength(P7_TOPHITS *h)
628 {
629 int i, max, n;
630 for (max = 0, i = 0; i < h->N; i++)
631 {
632 if (h->unsrt[i].acc != NULL && h->unsrt[i].acc[0] != '\0')
633 {
634 n = strlen(h->unsrt[i].acc);
635 max = ESL_MAX(n, max);
636 }
637 else if (h->unsrt[i].name != NULL)
638 {
639 n = strlen(h->unsrt[i].name);
640 max = ESL_MAX(n, max);
641 }
642 }
643 return max;
644 }
645
646
647 /* Function: p7_tophits_Reuse()
648 * Synopsis: Reuse a hit list, freeing internals.
649 *
650 * Purpose: Reuse the tophits list <h>; save as
651 * many malloc/free cycles as possible,
652 * as opposed to <Destroy()>'ing it and
653 * <Create>'ing a new one.
654 */
655 int
656 p7_tophits_Reuse(P7_TOPHITS *h)
657 {
658 int i, j;
659
660 if (h == NULL) return eslOK;
661 if (h->unsrt != NULL)
662 {
663 for (i = 0; i < h->N; i++)
664 {
665 if (h->unsrt[i].name != NULL) free(h->unsrt[i].name);
666 if (h->unsrt[i].acc != NULL) free(h->unsrt[i].acc);
667 if (h->unsrt[i].desc != NULL) free(h->unsrt[i].desc);
668 if (h->unsrt[i].dcl != NULL) {
669 for (j = 0; j < h->unsrt[i].ndom; j++)
670 if (h->unsrt[i].dcl[j].ad != NULL) p7_alidisplay_Destroy(h->unsrt[i].dcl[j].ad);
671 free(h->unsrt[i].dcl);
672 }
673 }
674 }
675 h->N = 0;
676 h->is_sorted_by_seqidx = FALSE;
677 h->is_sorted_by_sortkey = TRUE; /* because there are 0 hits */
678 h->hit[0] = h->unsrt;
679 return eslOK;
680 }
681
682 /* Function: p7_tophits_Destroy()
683 * Synopsis: Frees a hit list.
684 */
685 void
686 p7_tophits_Destroy(P7_TOPHITS *h)
687 {
688 int i,j;
689 if (h == NULL) return;
690 if (h->hit != NULL) free(h->hit);
691 if (h->unsrt != NULL)
692 {
693 for (i = 0; i < h->N; i++)
694 {
695 if (h->unsrt[i].name != NULL) free(h->unsrt[i].name);
696 if (h->unsrt[i].acc != NULL) free(h->unsrt[i].acc);
697 if (h->unsrt[i].desc != NULL) free(h->unsrt[i].desc);
698 if (h->unsrt[i].dcl != NULL) {
699 for (j = 0; j < h->unsrt[i].ndom; j++) {
700 if (h->unsrt[i].dcl[j].ad != NULL) p7_alidisplay_Destroy(h->unsrt[i].dcl[j].ad);
701 if (h->unsrt[i].dcl[j].scores_per_pos != NULL) free (h->unsrt[i].dcl->scores_per_pos);
702 }
703 free(h->unsrt[i].dcl);
704 }
705 }
706 free(h->unsrt);
707 }
708 free(h);
709 return;
710 }
711 /*---------------- end, P7_TOPHITS object -----------------------*/
712
713
714
715
716
717
718 /*****************************************************************
719 * 2. Standard (human-readable) output of pipeline results
720 *****************************************************************/
721
722 /* workaround_bug_h74():
723 * Different envelopes, identical alignment
724 *
725 * Bug #h74, though extremely rare, arises from a limitation in H3's
726 * implementation of Forward/Backward, as follows:
727 *
728 * 1. A multidomain region is analyzed by stochastic clustering
729 * 2. Overlapping envelopes are found (w.r.t sequence coords), though
730 * trace clusters are distinct if HMM endpoints are also considered.
731 * 3. We have no facility for limiting Forward/Backward to a specified
732 * range of profile coordinates, so each envelope is passed to
733 * rescore_isolated_domain() and analyzed independently.
734 * 4. Optimal accuracy alignment may identify exactly the same alignment
735 * in the overlap region shared by the two envelopes.
736 *
737 * The disturbing result is two different envelopes that have
738 * identical alignments and alignment endpoints.
739 *
740 * The correct fix is to define envelopes not only by sequence
741 * endpoints but also by profile endpoints, passing them to
742 * rescore_isolated_domain(), and limiting F/B calculations to this
743 * pieces of the DP lattice. This requires a fair amount of work,
744 * adding to the optimized API.
745 *
746 * The workaround is to detect when there are duplicate alignments,
747 * and only display one. We show the one with the best bit score.
748 *
749 * If we ever implement envelope-limited versions of F/B, revisit this
750 * fix.
751 *
752 * SRE, Tue Dec 22 16:27:04 2009
753 * xref J5/130; notebook/2009/1222-hmmer-bug-h74
754 */
755 static int
756 workaround_bug_h74(P7_TOPHITS *th)
757 {
758 int h;
759 int d1, d2;
760 int dremoved;
761
762 for (h = 0; h < th->N; h++)
763 if (th->hit[h]->noverlaps)
764 {
765 for (d1 = 0; d1 < th->hit[h]->ndom; d1++)
766 for (d2 = d1+1; d2 < th->hit[h]->ndom; d2++)
767 if (th->hit[h]->dcl[d1].iali == th->hit[h]->dcl[d2].iali &&
768 th->hit[h]->dcl[d1].jali == th->hit[h]->dcl[d2].jali)
769 {
770 dremoved = (th->hit[h]->dcl[d1].bitscore >= th->hit[h]->dcl[d2].bitscore) ? d2 : d1;
771 if (th->hit[h]->dcl[dremoved].is_reported) { th->hit[h]->dcl[dremoved].is_reported = FALSE; th->hit[h]->nreported--; }
772 if (th->hit[h]->dcl[dremoved].is_included) { th->hit[h]->dcl[dremoved].is_included = FALSE; th->hit[h]->nincluded--; }
773 }
774 }
775 return eslOK;
776 }
777
778
779
780 /* Function: p7_tophits_ComputeNhmmerEvalues()
781 * Synopsis: Compute e-values based on pvalues and window sizes.
782 *
783 * Purpose: After nhmmer pipeline has completed, the <th> object
784 * contains hits where the P-values haven't yet been
785 * converted to E-values. That modification depends on an
786 * established number of sequences. In nhmmer, this is
787 * computed as N/W, for a database of N residues, where W
788 * is some standardized window length (nhmmer passes
789 * om->max_length). E-values are set here based on that
790 * formula. We also set the sortkey so the output will be
791 * sorted correctly.
792 *
793 * Returns: <eslOK> on success.
794 */
795 int
796 p7_tophits_ComputeNhmmerEvalues(P7_TOPHITS *th, double N, int W)
797 {
798 int i; /* counters over hits */
799
800 for (i = 0; i < th->N ; i++)
801 {
802 th->unsrt[i].lnP += log((float)N / (float)W);
803 th->unsrt[i].dcl[0].lnP = th->unsrt[i].lnP;
804 th->unsrt[i].sortkey = -1.0 * th->unsrt[i].lnP;
805 }
806 return eslOK;
807 }
808
809
810 /* Function: p7_tophits_RemoveDuplicates()
811 * Synopsis: Remove overlapping hits.
812 *
813 * Purpose: After nhmmer pipeline has completed, the TopHits object may
814 * contain duplicates if the target was broken into overlapping
815 * windows. Scan through, and remove duplicates. Since the
816 * duplicates may be incomplete (one sequence is a partial
817 * hit because it's window didn't cover the full length of
818 * the hit), keep the one with better p-value
819 *
820 * Returns: <eslOK> on success.
821 */
822 int
823 p7_tophits_RemoveDuplicates(P7_TOPHITS *th, int using_bit_cutoffs)
824 {
825 int i; /* counter over hits */
826 int j; /* previous un-duplicated hit */
827 int s_i, s_j, e_i, e_j, dir_i, dir_j, len_i, len_j;
828 int intersect_alistart, intersect_aliend, intersect_alilen;
829 int intersect_hmmstart, intersect_hmmend, intersect_hmmlen;
830 double p_i, p_j;
831 int remove;
832
833 if (th->N<2) return eslOK;
834
835 j=0;
836 for (i = 1; i < th->N; i++)
837 {
838 p_j = th->hit[j]->lnP;
839 s_j = th->hit[j]->dcl[0].iali;
840 e_j = th->hit[j]->dcl[0].jali;
841 dir_j = (s_j < e_j ? 1 : -1);
842 if (dir_j == -1) ESL_SWAP(s_j, e_j, int);
843 len_j = e_j - s_j + 1 ;
844
845 p_i = th->hit[i]->lnP;
846 s_i = th->hit[i]->dcl[0].iali;
847 e_i = th->hit[i]->dcl[0].jali;
848 dir_i = (s_i < e_i ? 1 : -1);
849 if (dir_i == -1) ESL_SWAP(s_i, e_i, int);
850 len_i = e_i - s_i + 1 ;
851
852 // these will only matter if seqidx and strand are the same
853 intersect_alistart = s_i>s_j ? s_i : s_j;
854 intersect_aliend = e_i<e_j ? e_i : e_j;
855 intersect_alilen = intersect_aliend - intersect_alistart + 1;
856
857 intersect_hmmstart = (th->hit[i]->dcl[0].ad->hmmfrom > th->hit[j]->dcl[0].ad->hmmfrom) ? th->hit[i]->dcl[0].ad->hmmfrom : th->hit[j]->dcl[0].ad->hmmfrom;
858 intersect_hmmend = (th->hit[i]->dcl[0].ad->hmmto < th->hit[j]->dcl[0].ad->hmmto) ? th->hit[i]->dcl[0].ad->hmmto : th->hit[j]->dcl[0].ad->hmmto;
859 intersect_hmmlen = intersect_hmmend - intersect_hmmstart + 1;
860
861 if ( esl_strcmp(th->hit[i]->name, th->hit[i-1]->name) == 0 && // same model
862 th->hit[i]->seqidx == th->hit[i-1]->seqidx && // same source sequence
863 dir_i == dir_j && // only bother removing if the overlapping hits are on the same strand
864 intersect_hmmlen > 0 && // only if they're both hitting similar parts of the model
865 (
866 ( s_i >= s_j-3 && s_i <= s_j+3) || // at least one side is essentially flush
867 ( e_i >= e_j-3 && e_i <= e_j+3) ||
868 ( intersect_alilen >= len_i * 0.95) || // or one of the hits covers >90% of the other
869 ( intersect_alilen >= len_j * 0.95)
870 ))
871 {
872 /* Force one to go unreported. I prefer to keep the one with the
873 * better e-value. This addresses two issues
874 * (1) longer hits sometimes encounter higher bias corrections,
875 * leading to lower scores; seems better to focus on the
876 * high-scoring heart of the alignment, if we have a
877 * choice
878 * (2) it is possible that a lower-scoring longer hit (see #1)
879 * that is close to threshold will pass the pipeline in
880 * one condition and not the other (e.g. --toponly, or
881 * single vs multi threaded), and if longer hits obscure
882 * shorter higher-scoring ones, a shorter "hit" might be
883 * lost by being obscured by a longer one that is subsequently
884 * removed due to insufficient score.
885 * see late notes in ~wheelert/notebook/2012/0518-dfam-scripts/00NOTES
886 */
887 //remove = 0; // 1 := keep i, 0 := keep i-1
888 remove = p_i < p_j ? j : i;
889
890 th->hit[remove]->flags |= p7_IS_DUPLICATE;
891 if (using_bit_cutoffs) { // report/include flags were already included, need to remove them here
892 th->hit[remove]->flags &= ~p7_IS_REPORTED;
893 th->hit[remove]->flags &= ~p7_IS_INCLUDED;
894 }
895
896 j = (remove == j ? i : j);
897 }
898 else j = i;
899 }
900 return eslOK;
901 }
902
903
904
905 /* Function: p7_tophits_Threshold()
906 * Synopsis: Apply score and E-value thresholds to a hitlist before output.
907 *
908 * Purpose: After a pipeline has completed, go through it and mark all
909 * the targets and domains that are "significant" (satisfying
910 * the reporting thresholds set for the pipeline).
911 *
912 * Also sets the final total number of reported and
913 * included targets, the number of reported and included
914 * targets in each target, and the size of the search space
915 * for per-domain conditional E-value calculations,
916 * <pli->domZ>. By default, <pli->domZ> is the number of
917 * significant targets reported.
918 *
919 * If model-specific thresholds were used in the pipeline,
920 * we cannot apply those thresholds now. They were already
921 * applied in the pipeline. In this case all we're
922 * responsible for here is counting them (setting
923 * nreported, nincluded counters).
924 *
925 * Returns: <eslOK> on success.
926 */
927 int
928 p7_tophits_Threshold(P7_TOPHITS *th, P7_PIPELINE *pli)
929 {
930 int h, d; /* counters over sequence hits, domains in sequences */
931
932 /* Flag reported, included targets (if we're using general thresholds) */
933 if (! pli->use_bit_cutoffs)
934 {
935 for (h = 0; h < th->N; h++)
936 {
937
938 if ( !(th->hit[h]->flags & p7_IS_DUPLICATE) &&
939 p7_pli_TargetReportable(pli, th->hit[h]->score, th->hit[h]->lnP))
940 {
941 th->hit[h]->flags |= p7_IS_REPORTED;
942 if (p7_pli_TargetIncludable(pli, th->hit[h]->score, th->hit[h]->lnP))
943 th->hit[h]->flags |= p7_IS_INCLUDED;
944
945 if (pli->long_targets) { // no domains in dna search, so:
946 th->hit[h]->dcl[0].is_reported = th->hit[h]->flags & p7_IS_REPORTED;
947 th->hit[h]->dcl[0].is_included = th->hit[h]->flags & p7_IS_INCLUDED;
948 }
949 }
950 }
951 }
952
953 /* Count reported, included targets */
954 th->nreported = 0;
955 th->nincluded = 0;
956 for (h = 0; h < th->N; h++)
957 {
958 if (th->hit[h]->flags & p7_IS_REPORTED) th->nreported++;
959 if (th->hit[h]->flags & p7_IS_INCLUDED) th->nincluded++;
960 }
961
962 /* Now we can determined domZ, the effective search space in which additional domains are found */
963 if (pli->domZ_setby == p7_ZSETBY_NTARGETS) pli->domZ = (double) th->nreported;
964
965
966 /* Second pass is over domains, flagging reportable/includable ones.
967 * Depends on knowing the domZ we just set.
968 * Note how this enforces a hierarchical logic of
969 * (sequence|domain) must be reported to be included, and
970 * domain can only be (reported|included) if whole sequence is too.
971 */
972 if (! pli->use_bit_cutoffs && !pli->long_targets)
973 {
974 for (h = 0; h < th->N; h++)
975 {
976 if (th->hit[h]->flags & p7_IS_REPORTED)
977 {
978 for (d = 0; d < th->hit[h]->ndom; d++)
979 {
980 if (p7_pli_DomainReportable(pli, th->hit[h]->dcl[d].bitscore, th->hit[h]->dcl[d].lnP))
981 th->hit[h]->dcl[d].is_reported = TRUE;
982 if ((th->hit[h]->flags & p7_IS_INCLUDED) &&
983 p7_pli_DomainIncludable(pli, th->hit[h]->dcl[d].bitscore, th->hit[h]->dcl[d].lnP))
984 th->hit[h]->dcl[d].is_included = TRUE;
985 }
986 }
987 }
988 }
989
990 /* Count the reported, included domains */
991 for (h = 0; h < th->N; h++) {
992 th->hit[h]->nreported = 0;
993 th->hit[h]->nincluded = 0;
994 for (d = 0; d < th->hit[h]->ndom; d++)
995 {
996 if (th->hit[h]->dcl[d].is_reported) th->hit[h]->nreported++;
997 if (th->hit[h]->dcl[d].is_included) th->hit[h]->nincluded++;
998 }
999 }
1000
1001 workaround_bug_h74(th); /* blech. This function is defined above; see commentary and crossreferences there. */
1002
1003 return eslOK;
1004 }
1005
1006
1007
1008
1009
1010 /* Function: p7_tophits_CompareRanking()
1011 * Synopsis: Compare current top hits to previous top hits ranking.
1012 *
1013 * Purpose: Using a keyhash <kh> of the previous top hits and the
1014 * their ranks, look at the current top hits list <th>
1015 * and flag new hits that are included for the first time
1016 * (by setting <p7_IS_NEW> flag) and hits that were
1017 * included previously, but are now below the inclusion
1018 * threshold in the list (<by setting <p7_IS_DROPPED>
1019 * flag).
1020 *
1021 * The <th> must already have been processed by
1022 * <p7_tophits_Threshold()>. We assume the <is_included>,
1023 * <is_reported> flags are set on the appropriate hits.
1024 *
1025 * Upon return, the keyhash <kh> is updated to hash the
1026 * current top hits list and their ranks.
1027 *
1028 * Optionally, <*opt_nnew> is set to the number of
1029 * newly included hits. jackhmmer uses this as part of
1030 * its convergence criteria, for example.
1031 *
1032 * These flags affect output of top target hits from
1033 * <p7_tophits_Targets()>.
1034 *
1035 * It only makes sense to call this function in context of
1036 * an iterative search.
1037 *
1038 * The <p7_IS_NEW> flag is comprehensive: all new hits
1039 * are flagged (and counted in <*opt_nnew>). The <p7_WAS_DROPPED>
1040 * flag is not comprehensive: only those hits that still
1041 * appear in the current top hits list are flagged. If a
1042 * hit dropped entirely off the list, it isn't counted
1043 * as "dropped". (This could be done, but we would want
1044 * to have two keyhashes, one old and one new, to do the
1045 * necessary comparisons efficiently.)
1046 *
1047 * If the target names in <th> are not unique, results may
1048 * be strange.
1049 *
1050 * Args: th - current top hits list
1051 * kh - hash of top hits' ranks (in: previous tophits; out: <th>'s tophits)
1052 * opt_nnew - optRETURN: number of new hits above inclusion threshold
1053 *
1054 * Returns: <eslOK> on success.
1055 *
1056 * Throws: <eslEMEM> if <kh> needed to be reallocated but this failed.
1057 */
1058 int
1059 p7_tophits_CompareRanking(P7_TOPHITS *th, ESL_KEYHASH *kh, int *opt_nnew)
1060 {
1061 int nnew = 0;
1062 int oldrank;
1063 int h;
1064 int status;
1065
1066 /* Flag the hits in the list with whether they're new in the included top hits,
1067 * and whether they've dropped off the included list.
1068 */
1069 for (h = 0; h < th->N; h++)
1070 {
1071 esl_keyhash_Lookup(kh, th->hit[h]->name, -1, &oldrank);
1072
1073 if (th->hit[h]->flags & p7_IS_INCLUDED)
1074 {
1075 if (oldrank == -1) { th->hit[h]->flags |= p7_IS_NEW; nnew++; }
1076 }
1077 else
1078 {
1079 if (oldrank >= 0) th->hit[h]->flags |= p7_IS_DROPPED;
1080 }
1081 }
1082
1083 /* Replace the old rank list with the new one */
1084 esl_keyhash_Reuse(kh);
1085 for (h = 0; h < th->N; h++)
1086 {
1087 if (th->hit[h]->flags & p7_IS_INCLUDED)
1088 {
1089 /* What happens when the same sequence name appears twice? It gets stored with higher rank */
1090 status = esl_keyhash_Store(kh, th->hit[h]->name, -1, NULL);
1091 if (status != eslOK && status != eslEDUP) goto ERROR;
1092 }
1093 }
1094
1095 if (opt_nnew != NULL) *opt_nnew = nnew;
1096 return eslOK;
1097
1098 ERROR:
1099 if (opt_nnew != NULL) *opt_nnew = 0;
1100 return status;
1101 }
1102
1103
1104 /* Function: p7_tophits_Targets()
1105 * Synopsis: Format and write a top target hits list to an output stream.
1106 *
1107 * Purpose: Output a list of the reportable top target hits in <th>
1108 * in human-readable ASCII text format to stream <ofp>, using
1109 * final pipeline accounting stored in <pli>.
1110 *
1111 * The tophits list <th> should already be sorted (see
1112 * <p7_tophits_Sort()> and thresholded (see
1113 * <p7_tophits_Threshold>).
1114 *
1115 * Returns: <eslOK> on success.
1116 *
1117 * Throws: <eslEWRITE> on write failure.
1118 */
1119 int
1120 p7_tophits_Targets(FILE *ofp, P7_TOPHITS *th, P7_PIPELINE *pli, int textw)
1121 {
1122 char newness;
1123 int h;
1124 int d;
1125 int namew;
1126 int posw;
1127 int descw;
1128 char *showname;
1129
1130 int have_printed_incthresh = FALSE;
1131
1132 /* when --acc is on, we'll show accession if available, and fall back to name */
1133 if (pli->show_accessions) namew = ESL_MAX(8, p7_tophits_GetMaxShownLength(th));
1134 else namew = ESL_MAX(8, p7_tophits_GetMaxNameLength(th));
1135
1136
1137 if (pli->long_targets)
1138 {
1139 posw = ESL_MAX(6, p7_tophits_GetMaxPositionLength(th));
1140
1141 if (textw > 0) descw = ESL_MAX(32, textw - namew - 2*posw - 32); /* 32 chars excluding desc and two posw's is from the format: 2 + 9+2 +6+2 +5+2 +<name>+1 +<startpos>+1 +<endpos>+1 +1 */
1142 else descw = 0; /* unlimited desc length is handled separately */
1143
1144 if (fprintf(ofp, "Scores for complete hit%s:\n", pli->mode == p7_SEARCH_SEQS ? "s" : "") < 0)
1145 ESL_EXCEPTION_SYS(eslEWRITE, "per-sequence hit list: write failed");
1146 if (fprintf(ofp, " %9s %6s %5s %-*s %*s %*s %s\n",
1147 "E-value", " score", " bias", namew, (pli->mode == p7_SEARCH_SEQS ? "Sequence":"Model"), posw, "start", posw, "end", "Description") < 0)
1148 ESL_EXCEPTION_SYS(eslEWRITE, "per-sequence hit list: write failed");
1149 if (fprintf(ofp, " %9s %6s %5s %-*s %*s %*s %s\n",
1150 "-------", "------", "-----", namew, "--------", posw, "-----", posw, "-----", "-----------") < 0)
1151 ESL_EXCEPTION_SYS(eslEWRITE, "per-sequence hit list: write failed");
1152 }
1153 else
1154 {
1155
1156 if (textw > 0) descw = ESL_MAX(32, textw - namew - 61); /* 61 chars excluding desc is from the format: 2 + 22+2 +22+2 +8+2 +<name>+1 */
1157 else descw = 0; /* unlimited desc length is handled separately */
1158
1159
1160 /* The minimum width of the target table is 111 char: 47 from fields, 8 from min name, 32 from min desc, 13 spaces */
1161 if (fprintf(ofp, "Scores for complete sequence%s (score includes all domains):\n", pli->mode == p7_SEARCH_SEQS ? "s" : "") < 0)
1162 ESL_EXCEPTION_SYS(eslEWRITE, "per-sequence hit list: write failed");
1163 if (fprintf(ofp, " %22s %22s %8s\n", " --- full sequence ---", " --- best 1 domain ---", "-#dom-") < 0)
1164 ESL_EXCEPTION_SYS(eslEWRITE, "per-sequence hit list: write failed");
1165 if (fprintf(ofp, " %9s %6s %5s %9s %6s %5s %5s %2s %-*s %s\n",
1166 "E-value", " score", " bias", "E-value", " score", " bias", " exp", "N", namew, (pli->mode == p7_SEARCH_SEQS ? "Sequence":"Model"), "Description") < 0)
1167 ESL_EXCEPTION_SYS(eslEWRITE, "per-sequence hit list: write failed");
1168 if (fprintf(ofp, " %9s %6s %5s %9s %6s %5s %5s %2s %-*s %s\n",
1169 "-------", "------", "-----", "-------", "------", "-----", " ----", "--", namew, "--------", "-----------") < 0)
1170 ESL_EXCEPTION_SYS(eslEWRITE, "per-sequence hit list: write failed");
1171 }
1172
1173 for (h = 0; h < th->N; h++)
1174 {
1175 if (th->hit[h]->flags & p7_IS_REPORTED)
1176 {
1177 d = th->hit[h]->best_domain;
1178
1179 if (! (th->hit[h]->flags & p7_IS_INCLUDED) && ! have_printed_incthresh)
1180 {
1181 if (fprintf(ofp, " ------ inclusion threshold ------\n") < 0)
1182 ESL_EXCEPTION_SYS(eslEWRITE, "per-sequence hit list: write failed");
1183 have_printed_incthresh = TRUE;
1184 }
1185
1186 if (pli->show_accessions)
1187 { /* the --acc option: report accessions rather than names if possible */
1188 if (th->hit[h]->acc != NULL && th->hit[h]->acc[0] != '\0') showname = th->hit[h]->acc;
1189 else showname = th->hit[h]->name;
1190 }
1191 else
1192 showname = th->hit[h]->name;
1193
1194 if (th->hit[h]->flags & p7_IS_NEW) newness = '+';
1195 else if (th->hit[h]->flags & p7_IS_DROPPED) newness = '-';
1196 else newness = ' ';
1197
1198 if (pli->long_targets)
1199 {
1200 if (fprintf(ofp, "%c %9.2g %6.1f %5.1f %-*s %*" PRId64 " %*" PRId64 "",
1201 newness,
1202 exp(th->hit[h]->lnP), // * pli->Z,
1203 th->hit[h]->score,
1204 eslCONST_LOG2R * th->hit[h]->dcl[d].dombias, // an nhmmer hit is really a domain, so this is the hit's bias correction
1205 namew, showname,
1206 posw, th->hit[h]->dcl[d].iali,
1207 posw, th->hit[h]->dcl[d].jali) < 0)
1208 ESL_EXCEPTION_SYS(eslEWRITE, "per-sequence hit list: write failed");
1209 }
1210 else
1211 {
1212 if (fprintf(ofp, "%c %9.2g %6.1f %5.1f %9.2g %6.1f %5.1f %5.1f %2d %-*s ",
1213 newness,
1214 exp(th->hit[h]->lnP) * pli->Z,
1215 th->hit[h]->score,
1216 th->hit[h]->pre_score - th->hit[h]->score, /* bias correction */
1217 exp(th->hit[h]->dcl[d].lnP) * pli->Z,
1218 th->hit[h]->dcl[d].bitscore,
1219 eslCONST_LOG2R * th->hit[h]->dcl[d].dombias, /* convert NATS to BITS at last moment */
1220 th->hit[h]->nexpected,
1221 th->hit[h]->nreported,
1222 namew, showname) < 0)
1223 ESL_EXCEPTION_SYS(eslEWRITE, "per-sequence hit list: write failed");
1224 }
1225
1226 if (textw > 0)
1227 {
1228 if (fprintf(ofp, " %-.*s\n", descw, th->hit[h]->desc == NULL ? "" : th->hit[h]->desc) < 0)
1229 ESL_EXCEPTION_SYS(eslEWRITE, "per-sequence hit list: write failed");
1230 }
1231 else
1232 {
1233 if (fprintf(ofp, " %s\n", th->hit[h]->desc == NULL ? "" : th->hit[h]->desc) < 0)
1234 ESL_EXCEPTION_SYS(eslEWRITE, "per-sequence hit list: write failed");
1235 }
1236 /* do NOT use *s with unlimited (INT_MAX) line length. Some systems
1237 * have an fprintf() bug here (we found one on an Opteron/SUSE Linux
1238 * system (#h66)
1239 */
1240 }
1241 }
1242
1243 if (th->nreported == 0)
1244 {
1245 if (fprintf(ofp, "\n [No hits detected that satisfy reporting thresholds]\n") < 0)
1246 ESL_EXCEPTION_SYS(eslEWRITE, "per-sequence hit list: write failed");
1247 }
1248 return eslOK;
1249 }
1250
1251
1252 /* Function: p7_tophits_Domains()
1253 * Synopsis: Standard output format for top domain hits and alignments.
1254 *
1255 * Purpose: For each reportable target sequence, output a tabular summary
1256 * of reportable domains found in it, followed by alignments of
1257 * each domain.
1258 *
1259 * Similar to <p7_tophits_Targets()>; see additional notes there.
1260 *
1261 * Returns: <eslOK> on success.
1262 *
1263 * Throws: <eslEWRITE> if a write to <ofp> fails; for example, if
1264 * the disk fills up.
1265 */
1266 int
1267 p7_tophits_Domains(FILE *ofp, P7_TOPHITS *th, P7_PIPELINE *pli, int textw)
1268 {
1269 int h, d;
1270 int nd;
1271 int namew, descw;
1272 char *showname;
1273 int status;
1274
1275 if (pli->long_targets)
1276 {
1277 if (fprintf(ofp, "Annotation for each hit %s:\n",
1278 pli->show_alignments ? " (and alignments)" : "") < 0)
1279 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1280 }
1281 else
1282 {
1283 if (fprintf(ofp, "Domain annotation for each %s%s:\n",
1284 pli->mode == p7_SEARCH_SEQS ? "sequence" : "model",
1285 pli->show_alignments ? " (and alignments)" : "") < 0)
1286 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1287 }
1288
1289 for (h = 0; h < th->N; h++)
1290 if (th->hit[h]->flags & p7_IS_REPORTED)
1291 {
1292 if (pli->show_accessions && th->hit[h]->acc != NULL && th->hit[h]->acc[0] != '\0')
1293 {
1294 showname = th->hit[h]->acc;
1295 namew = strlen(th->hit[h]->acc);
1296 }
1297 else
1298 {
1299 showname = th->hit[h]->name;
1300 namew = strlen(th->hit[h]->name);
1301 }
1302
1303 if (textw > 0)
1304 {
1305 descw = ESL_MAX(32, textw - namew - 5);
1306 if (fprintf(ofp, ">> %s %-.*s\n", showname, descw, (th->hit[h]->desc == NULL ? "" : th->hit[h]->desc)) < 0)
1307 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1308 }
1309 else
1310 {
1311 if (fprintf(ofp, ">> %s %s\n", showname, (th->hit[h]->desc == NULL ? "" : th->hit[h]->desc)) < 0)
1312 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1313 }
1314
1315 if (th->hit[h]->nreported == 0)
1316 {
1317 if (fprintf(ofp," [No individual domains that satisfy reporting thresholds (although complete target did)]\n\n") < 0)
1318 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1319 continue;
1320 }
1321
1322
1323 if (pli->long_targets)
1324 {
1325 /* The dna hit table is 119 char wide:
1326 score bias Evalue hmmfrom hmm to alifrom ali to envfrom env to hqfrom hq to sq len acc
1327 ------ ----- --------- ------- ------- --------- --------- --------- --------- --------- --------- --------- ----
1328 ! 82.7 104.4 4.9e-22 782 998 .. 241981174 241980968 .. 241981174 241980966 .. 241981174 241980968 234234233 0.78
1329 */
1330 if (fprintf(ofp, " %6s %5s %9s %9s %9s %2s %9s %9s %2s %9s %9s %9s %2s %4s\n", "score", "bias", " Evalue", "hmmfrom", "hmm to", " ", " alifrom ", " ali to ", " ", " envfrom ", " env to ", (pli->mode == p7_SEARCH_SEQS ? " sq len " : " mod len "), " ", "acc") < 0)
1331 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1332 if (fprintf(ofp, " %6s %5s %9s %9s %9s %2s %9s %9s %2s %9s %9s %9s %2s %4s\n", "------", "-----", "---------", "-------", "-------", " ", "---------", "---------", " ", "---------", "---------", "---------", " ", "----") < 0)
1333 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1334 }
1335 else
1336 {
1337 /* The domain table is 101 char wide:
1338 # score bias c-Evalue i-Evalue hmmfrom hmmto alifrom ali to envfrom env to acc
1339 --- ------ ----- --------- --------- ------- ------- ------- ------- ------- ------- ----
1340 1 ? 123.4 23.1 9.7e-11 6.8e-9 3 1230 .. 1 492 [] 2 490 .] 0.90
1341 123 ! 1234.5 123.4 123456789 123456789 1234567 1234567 .. 1234567 1234567 [] 1234567 1234568 .] 0.12
1342 */
1343 if (fprintf(ofp, " %3s %6s %5s %9s %9s %7s %7s %2s %7s %7s %2s %7s %7s %2s %4s\n", "#", "score", "bias", "c-Evalue", "i-Evalue", "hmmfrom", "hmm to", " ", "alifrom", "ali to", " ", "envfrom", "env to", " ", "acc") < 0)
1344 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1345 if (fprintf(ofp, " %3s %6s %5s %9s %9s %7s %7s %2s %7s %7s %2s %7s %7s %2s %4s\n", "---", "------", "-----", "---------", "---------", "-------", "-------", " ", "-------", "-------", " ", "-------", "-------", " ", "----") < 0)
1346 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1347 }
1348
1349
1350 /* Domain hit table for each reported domain in this reported sequence. */
1351 nd = 0;
1352 for (d = 0; d < th->hit[h]->ndom; d++)
1353 {
1354 if (th->hit[h]->dcl[d].is_reported)
1355 {
1356 nd++;
1357 if (pli->long_targets)
1358 {
1359 if (fprintf(ofp, " %c %6.1f %5.1f %9.2g %9d %9d %c%c %9" PRId64 " %9" PRId64 " %c%c %9" PRId64 " %9" PRId64 " %c%c %9" PRId64 " %4.2f\n",
1360 //nd,
1361 th->hit[h]->dcl[d].is_included ? '!' : '?',
1362 th->hit[h]->dcl[d].bitscore,
1363 th->hit[h]->dcl[d].dombias * eslCONST_LOG2R, /* convert NATS to BITS at last moment */
1364 exp(th->hit[h]->dcl[d].lnP),
1365 th->hit[h]->dcl[d].ad->hmmfrom,
1366 th->hit[h]->dcl[d].ad->hmmto,
1367 (th->hit[h]->dcl[d].ad->hmmfrom == 1) ? '[' : '.',
1368 (th->hit[h]->dcl[d].ad->hmmto == th->hit[h]->dcl[d].ad->M) ? ']' : '.',
1369 th->hit[h]->dcl[d].ad->sqfrom,
1370 th->hit[h]->dcl[d].ad->sqto,
1371 (th->hit[h]->dcl[d].ad->sqfrom == 1) ? '[' : '.',
1372 (th->hit[h]->dcl[d].ad->sqto == th->hit[h]->dcl[d].ad->L) ? ']' : '.',
1373 th->hit[h]->dcl[d].ienv,
1374 th->hit[h]->dcl[d].jenv,
1375 (th->hit[h]->dcl[d].ienv == 1) ? '[' : '.',
1376 (th->hit[h]->dcl[d].jenv == th->hit[h]->dcl[d].ad->L) ? ']' : '.',
1377 th->hit[h]->dcl[d].ad->L,
1378 (th->hit[h]->dcl[d].oasc / (1.0 + fabs((float) (th->hit[h]->dcl[d].jenv - th->hit[h]->dcl[d].ienv))))) < 0)
1379 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1380 }
1381 else
1382 {
1383 if (fprintf(ofp, " %3d %c %6.1f %5.1f %9.2g %9.2g %7d %7d %c%c",
1384 nd,
1385 th->hit[h]->dcl[d].is_included ? '!' : '?',
1386 th->hit[h]->dcl[d].bitscore,
1387 th->hit[h]->dcl[d].dombias * eslCONST_LOG2R, /* convert NATS to BITS at last moment */
1388 exp(th->hit[h]->dcl[d].lnP) * pli->domZ,
1389 exp(th->hit[h]->dcl[d].lnP) * pli->Z,
1390 th->hit[h]->dcl[d].ad->hmmfrom,
1391 th->hit[h]->dcl[d].ad->hmmto,
1392 (th->hit[h]->dcl[d].ad->hmmfrom == 1) ? '[' : '.',
1393 (th->hit[h]->dcl[d].ad->hmmto == th->hit[h]->dcl[d].ad->M ) ? ']' : '.') < 0)
1394 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1395
1396 if (fprintf(ofp, " %7" PRId64 " %7" PRId64 " %c%c",
1397 th->hit[h]->dcl[d].ad->sqfrom,
1398 th->hit[h]->dcl[d].ad->sqto,
1399 (th->hit[h]->dcl[d].ad->sqfrom == 1) ? '[' : '.',
1400 (th->hit[h]->dcl[d].ad->sqto == th->hit[h]->dcl[d].ad->L) ? ']' : '.') < 0)
1401 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1402
1403 if (fprintf(ofp, " %7" PRId64 " %7" PRId64 " %c%c",
1404 th->hit[h]->dcl[d].ienv,
1405 th->hit[h]->dcl[d].jenv,
1406 (th->hit[h]->dcl[d].ienv == 1) ? '[' : '.',
1407 (th->hit[h]->dcl[d].jenv == th->hit[h]->dcl[d].ad->L) ? ']' : '.') < 0)
1408 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1409
1410 if (fprintf(ofp, " %4.2f\n",
1411 (th->hit[h]->dcl[d].oasc / (1.0 + fabs((float) (th->hit[h]->dcl[d].jenv - th->hit[h]->dcl[d].ienv))))) < 0)
1412 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1413 }
1414
1415 }
1416 } // end of domain table in this reported sequence.
1417
1418 /* Alignment data for each reported domain in this reported sequence. */
1419 if (pli->show_alignments)
1420 {
1421 if (pli->long_targets)
1422 {
1423 if (fprintf(ofp, "\n Alignment:\n") < 0)
1424 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1425 }
1426 else
1427 {
1428 if (fprintf(ofp, "\n Alignments for each domain:\n") < 0)
1429 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1430 nd = 0;
1431 }
1432
1433 for (d = 0; d < th->hit[h]->ndom; d++)
1434 if (th->hit[h]->dcl[d].is_reported)
1435 {
1436 nd++;
1437 if (!pli->long_targets)
1438 {
1439 if (fprintf(ofp, " == domain %d", nd ) < 0)
1440 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1441 }
1442 if (fprintf(ofp, " score: %.1f bits", th->hit[h]->dcl[d].bitscore) < 0)
1443 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1444 if (!pli->long_targets)
1445 {
1446 if (fprintf(ofp, "; conditional E-value: %.2g\n", exp(th->hit[h]->dcl[d].lnP) * pli->domZ) < 0)
1447 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1448 }
1449 else
1450 {
1451 if (fprintf(ofp, "\n") < 0)
1452 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1453 }
1454
1455 if ((status = p7_alidisplay_Print(ofp, th->hit[h]->dcl[d].ad, 40, textw, pli)) != eslOK) return status;
1456
1457 if (fprintf(ofp, "\n") < 0)
1458 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1459 }
1460 }
1461 else // alignment reporting is off:
1462 {
1463 if (fprintf(ofp, "\n") < 0)
1464 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1465 }
1466
1467 } // end, loop over all reported hits
1468
1469 if (th->nreported == 0)
1470 {
1471 if (fprintf(ofp, "\n [No targets detected that satisfy reporting thresholds]\n") < 0)
1472 ESL_EXCEPTION_SYS(eslEWRITE, "domain hit list: write failed");
1473 }
1474 return eslOK;
1475 }
1476
1477
1478 /* Function: p7_tophits_Alignment()
1479 * Synopsis: Create multiple alignment of all included domains.
1480 *
1481 * Purpose: Create a multiple alignment of all domains marked
1482 * "includable" in the top hits list <th>. Return it in
1483 * <*ret_msa>.
1484 *
1485 * Use of <optflags> is identical to <optflags> in <p7_tracealign_Seqs()>.
1486 * Possible flags include <p7_DIGITIZE>, <p7_ALL_CONSENSUS_COLS>,
1487 * and <p7_TRIM>; they may be OR'ed together. Otherwise, pass
1488 * <p7_DEFAULT> to set no flags.
1489 *
1490 * Caller may optionally provide <inc_sqarr>, <inc_trarr>, and
1491 * <inc_n> to include additional sequences in the alignment
1492 * (the jackhmmer query, for example). Otherwise, pass <NULL, NULL, 0>.
1493 *
1494 * Returns: <eslOK> on success, and <*ret_msa> points to a new MSA that
1495 * the caller is responsible for freeing.
1496 *
1497 * Returns <eslFAIL> if there are no included domains,
1498 * in which case <*ret_msa> is <NULL>.
1499 *
1500 * Throws: <eslEMEM> on allocation failure;
1501 * <eslECORRUPT> on unexpected internal data corruption.
1502 *
1503 * Xref: J4/29: incept.
1504 * J4/76: added inc_sqarr, inc_trarr, inc_n, optflags
1505 * H4/72: iss131, segfault if top seq has no reported domains
1506 * H5/88: iss140, jackhmmer --fast segfaults
1507 */
1508 int
1509 p7_tophits_Alignment(const P7_TOPHITS *th, const ESL_ALPHABET *abc,
1510 ESL_SQ **inc_sqarr, P7_TRACE **inc_trarr, int inc_n,
1511 int optflags, ESL_MSA **ret_msa)
1512 {
1513 ESL_SQ **sqarr = NULL;
1514 P7_TRACE **trarr = NULL;
1515 ESL_MSA *msa = NULL;
1516 int ndom = 0;
1517 int M = 0;
1518 int h, d, y;
1519 int status;
1520
1521 /* How many domains will be included in the new alignment? We also
1522 * set M here; we don't have hmm, but every ali has a copy.
1523 *
1524 * Beware: although p7_Pipeline() must not register hits that have
1525 * no domains, it did happen {iss131}. So we're careful (here, at
1526 * least) about testing ndom before reaching into dcl[] to get M.
1527 */
1528 for (h = 0; h < th->N; h++)
1529 if (th->hit[h]->flags & p7_IS_INCLUDED)
1530 for (d = 0; d < th->hit[h]->ndom; d++)
1531 if (th->hit[h]->dcl[d].is_included)
1532 {
1533 if (M == 0) M = th->hit[h]->dcl[d].ad->M;
1534 ndom++;
1535 }
1536
1537 if (inc_n)
1538 {
1539 if (M == 0) M = inc_trarr[0]->M; // unusual case where there's an included trace, but no other aligned hits: get M from included trace
1540 else if (M != inc_trarr[0]->M) // spot check that included traces appear to have same # of consensus positions; iss#140
1541 ESL_XEXCEPTION(eslECORRUPT, "top hits and included trace(s) have different profile lengths");
1542 }
1543 if (inc_n+ndom == 0) { status = eslFAIL; goto ERROR; }
1544
1545 /* Allocation */
1546 ESL_ALLOC(sqarr, sizeof(ESL_SQ *) * (ndom + inc_n));
1547 ESL_ALLOC(trarr, sizeof(P7_TRACE *) * (ndom + inc_n));
1548 /* Inclusion of preexisting seqs, traces: make copy of pointers */
1549 for (y = 0; y < inc_n; y++) { sqarr[y] = inc_sqarr[y]; trarr[y] = inc_trarr[y]; }
1550 for (; y < (ndom+inc_n); y++) { sqarr[y] = NULL; trarr[y] = NULL; }
1551
1552 /* Make faux sequences, traces from hit list */
1553 y = inc_n;
1554 for (h = 0; h < th->N; h++)
1555 if (th->hit[h]->flags & p7_IS_INCLUDED)
1556 {
1557 for (d = 0; d < th->hit[h]->ndom; d++)
1558 if (th->hit[h]->dcl[d].is_included)
1559 {
1560 if ((status = p7_alidisplay_Backconvert(th->hit[h]->dcl[d].ad, abc, &(sqarr[y]), &(trarr[y]))) != eslOK) goto ERROR;
1561 y++;
1562 }
1563 }
1564
1565 /* Make the multiple alignment */
1566 if ((status = p7_tracealign_Seqs(sqarr, trarr, inc_n+ndom, M, optflags, NULL, &msa)) != eslOK) goto ERROR;
1567
1568 /* Clean up */
1569 for (y = inc_n; y < ndom+inc_n; y++) esl_sq_Destroy(sqarr[y]);
1570 for (y = inc_n; y < ndom+inc_n; y++) p7_trace_Destroy(trarr[y]);
1571 free(sqarr);
1572 free(trarr);
1573 *ret_msa = msa;
1574 return eslOK;
1575
1576 ERROR:
1577 if (sqarr != NULL) { for (y = inc_n; y < ndom+inc_n; y++) if (sqarr[y] != NULL) esl_sq_Destroy(sqarr[y]); free(sqarr); }
1578 if (trarr != NULL) { for (y = inc_n; y < ndom+inc_n; y++) if (trarr[y] != NULL) p7_trace_Destroy(trarr[y]); free(trarr); }
1579 if (msa != NULL) esl_msa_Destroy(msa);
1580 *ret_msa = NULL;
1581 return status;
1582 }
1583 /*---------------- end, standard output format ------------------*/
1584
1585
1586
1587
1588
1589 /*****************************************************************
1590 * 3. Tabular (parsable) output of pipeline results.
1591 *****************************************************************/
1592
1593 /* Function: p7_tophits_TabularTargets()
1594 * Synopsis: Output parsable table of per-sequence hits.
1595 *
1596 * Purpose: Output a parseable table of reportable per-sequence hits
1597 * in sorted tophits list <th> in an easily parsed ASCII
1598 * tabular form to stream <ofp>, using final pipeline
1599 * accounting stored in <pli>.
1600 *
1601 * Designed to be concatenated for multiple queries and
1602 * multiple top hits list.
1603 *
1604 * Returns: <eslOK> on success.
1605 *
1606 * Throws: <eslEWRITE> if a write to <ofp> fails; for example, if
1607 * the disk fills up.
1608 */
1609 int
1610 p7_tophits_TabularTargets(FILE *ofp, char *qname, char *qacc, P7_TOPHITS *th, P7_PIPELINE *pli, int show_header)
1611 {
1612 int qnamew = ESL_MAX(20, strlen(qname));
1613 int tnamew = ESL_MAX(20, p7_tophits_GetMaxNameLength(th));
1614 int qaccw = ((qacc != NULL) ? ESL_MAX(10, strlen(qacc)) : 10);
1615 int taccw = ESL_MAX(10, p7_tophits_GetMaxAccessionLength(th));
1616 int posw = (pli->long_targets ? ESL_MAX(7, p7_tophits_GetMaxPositionLength(th)) : 0);
1617 int h,d;
1618
1619 if (show_header)
1620 {
1621 if (pli->long_targets)
1622 {
1623 if (fprintf(ofp, "#%-*s %-*s %-*s %-*s %s %s %*s %*s %*s %*s %*s %6s %9s %6s %5s %s\n",
1624 tnamew-1, " target name", taccw, "accession", qnamew, "query name", qaccw, "accession", "hmmfrom", "hmm to", posw, "alifrom", posw, "ali to", posw, "envfrom", posw, "env to", posw, ( pli->mode == p7_SCAN_MODELS ? "modlen" : "sq len" ), "strand", " E-value", " score", " bias", "description of target") < 0)
1625 ESL_EXCEPTION_SYS(eslEWRITE, "tabular per-sequence hit list: write failed");
1626 if (fprintf(ofp, "#%*s %*s %*s %*s %s %s %*s %*s %*s %*s %*s %6s %9s %6s %5s %s\n",
1627 tnamew-1, "-------------------", taccw, "----------", qnamew, "--------------------", qaccw, "----------", "-------", "-------", posw, "-------", posw, "-------", posw, "-------", posw, "-------", posw, "-------", "------", "---------", "------", "-----", "---------------------") < 0)
1628 ESL_EXCEPTION_SYS(eslEWRITE, "tabular per-per-sequence hit list: write failed");
1629 }
1630 else
1631 {
1632 if (fprintf(ofp, "#%*s %22s %22s %33s\n", tnamew+qnamew+taccw+qaccw+2, "", "--- full sequence ----", "--- best 1 domain ----", "--- domain number estimation ----") < 0)
1633 ESL_EXCEPTION_SYS(eslEWRITE, "tabular per-sequence hit list: write failed");
1634 if (fprintf(ofp, "#%-*s %-*s %-*s %-*s %9s %6s %5s %9s %6s %5s %5s %3s %3s %3s %3s %3s %3s %3s %s\n",
1635 tnamew-1, " target name", taccw, "accession", qnamew, "query name", qaccw, "accession", " E-value", " score", " bias", " E-value", " score", " bias", "exp", "reg", "clu", " ov", "env", "dom", "rep", "inc", "description of target") < 0)
1636 ESL_EXCEPTION_SYS(eslEWRITE, "tabular per-sequence hit list: write failed");
1637 if (fprintf(ofp, "#%*s %*s %*s %*s %9s %6s %5s %9s %6s %5s %5s %3s %3s %3s %3s %3s %3s %3s %s\n",
1638 tnamew-1, "-------------------", taccw, "----------", qnamew, "--------------------", qaccw, "----------", "---------", "------", "-----", "---------", "------", "-----", "---", "---", "---", "---", "---", "---", "---", "---", "---------------------") < 0)
1639 ESL_EXCEPTION_SYS(eslEWRITE, "tabular per-sequence hit list: write failed");
1640 }
1641 }
1642
1643 for (h = 0; h < th->N; h++)
1644 if (th->hit[h]->flags & p7_IS_REPORTED)
1645 {
1646 d = th->hit[h]->best_domain;
1647 if (pli->long_targets)
1648 {
1649 if (fprintf(ofp, "%-*s %-*s %-*s %-*s %7d %7d %*" PRId64 " %*" PRId64 " %*" PRId64 " %*" PRId64 " %*" PRId64 " %6s %9.2g %6.1f %5.1f %s\n",
1650 tnamew, th->hit[h]->name,
1651 taccw, th->hit[h]->acc ? th->hit[h]->acc : "-",
1652 qnamew, qname,
1653 qaccw, ( (qacc != NULL && qacc[0] != '\0') ? qacc : "-"),
1654 th->hit[h]->dcl[d].ad->hmmfrom,
1655 th->hit[h]->dcl[d].ad->hmmto,
1656 posw, th->hit[h]->dcl[d].iali,
1657 posw, th->hit[h]->dcl[d].jali,
1658 posw, th->hit[h]->dcl[d].ienv,
1659 posw, th->hit[h]->dcl[d].jenv,
1660 posw, th->hit[h]->dcl[0].ad->L,
1661 (th->hit[h]->dcl[d].iali < th->hit[h]->dcl[d].jali ? " + " : " - "),
1662 exp(th->hit[h]->lnP),
1663 th->hit[h]->score,
1664 th->hit[h]->dcl[d].dombias * eslCONST_LOG2R, /* convert NATS to BITS at last moment */
1665 th->hit[h]->desc == NULL ? "-" : th->hit[h]->desc ) < 0)
1666 ESL_EXCEPTION_SYS(eslEWRITE, "tabular per-sequence hit list: write failed");
1667 }
1668 else
1669 {
1670 if (fprintf(ofp, "%-*s %-*s %-*s %-*s %9.2g %6.1f %5.1f %9.2g %6.1f %5.1f %5.1f %3d %3d %3d %3d %3d %3d %3d %s\n",
1671 tnamew, th->hit[h]->name,
1672 taccw, th->hit[h]->acc ? th->hit[h]->acc : "-",
1673 qnamew, qname,
1674 qaccw, ( (qacc != NULL && qacc[0] != '\0') ? qacc : "-"),
1675 exp(th->hit[h]->lnP) * pli->Z,
1676 th->hit[h]->score,
1677 th->hit[h]->pre_score - th->hit[h]->score, /* bias correction */
1678 exp(th->hit[h]->dcl[d].lnP) * pli->Z,
1679 th->hit[h]->dcl[d].bitscore,
1680 th->hit[h]->dcl[d].dombias * eslCONST_LOG2R, /* convert NATS to BITS at last moment */
1681 th->hit[h]->nexpected,
1682 th->hit[h]->nregions,
1683 th->hit[h]->nclustered,
1684 th->hit[h]->noverlaps,
1685 th->hit[h]->nenvelopes,
1686 th->hit[h]->ndom,
1687 th->hit[h]->nreported,
1688 th->hit[h]->nincluded,
1689 (th->hit[h]->desc == NULL ? "-" : th->hit[h]->desc)) < 0)
1690 ESL_EXCEPTION_SYS(eslEWRITE, "tabular per-sequence hit list: write failed");
1691 }
1692 }
1693 return eslOK;
1694 }
1695
1696
1697 /* Function: p7_tophits_TabularDomains()
1698 * Synopsis: Output parseable table of per-domain hits
1699 *
1700 * Purpose: Output a parseable table of reportable per-domain hits
1701 * in sorted tophits list <th> in an easily parsed ASCII
1702 * tabular form to stream <ofp>, using final pipeline
1703 * accounting stored in <pli>.
1704 *
1705 * Designed to be concatenated for multiple queries and
1706 * multiple top hits list.
1707 *
1708 * Returns: <eslOK> on success.
1709 *
1710 * Throws: <eslEWRITE> if a write to <ofp> fails; for example, if
1711 * the disk fills up.
1712 */
1713 int
1714 p7_tophits_TabularDomains(FILE *ofp, char *qname, char *qacc, P7_TOPHITS *th, P7_PIPELINE *pli, int show_header)
1715 {
1716
1717 int qnamew = ESL_MAX(20, strlen(qname));
1718 int tnamew = ESL_MAX(20, p7_tophits_GetMaxNameLength(th));
1719 int qaccw = (qacc ? ESL_MAX(10, strlen(qacc)) : 10);
1720 int taccw = ESL_MAX(10, p7_tophits_GetMaxAccessionLength(th));
1721 int tlen, qlen;
1722 int h,d,nd;
1723
1724 if (show_header)
1725 {
1726 if (fprintf(ofp, "#%*s %22s %40s %11s %11s %11s\n", tnamew+qnamew-1+15+taccw+qaccw, "", "--- full sequence ---", "-------------- this domain -------------", "hmm coord", "ali coord", "env coord") < 0)
1727 ESL_EXCEPTION_SYS(eslEWRITE, "tabular per-domain hit list: write failed");
1728 if (fprintf(ofp, "#%-*s %-*s %5s %-*s %-*s %5s %9s %6s %5s %3s %3s %9s %9s %6s %5s %5s %5s %5s %5s %5s %5s %4s %s\n",
1729 tnamew-1, " target name", taccw, "accession", "tlen", qnamew, "query name", qaccw, "accession", "qlen", "E-value", "score", "bias", "#", "of", "c-Evalue", "i-Evalue", "score", "bias", "from", "to", "from", "to", "from", "to", "acc", "description of target") < 0)
1730 ESL_EXCEPTION_SYS(eslEWRITE, "tabular per-domain hit list: write failed");
1731 if (fprintf(ofp, "#%*s %*s %5s %*s %*s %5s %9s %6s %5s %3s %3s %9s %9s %6s %5s %5s %5s %5s %5s %5s %5s %4s %s\n",
1732 tnamew-1, "-------------------", taccw, "----------", "-----", qnamew, "--------------------", qaccw, "----------", "-----", "---------", "------", "-----", "---", "---", "---------", "---------", "------", "-----", "-----", "-----", "-----", "-----", "-----", "-----", "----", "---------------------") < 0)
1733 ESL_EXCEPTION_SYS(eslEWRITE, "tabular per-domain hit list: write failed");
1734 }
1735
1736 for (h = 0; h < th->N; h++)
1737 if (th->hit[h]->flags & p7_IS_REPORTED)
1738 {
1739 nd = 0;
1740 for (d = 0; d < th->hit[h]->ndom; d++)
1741 if (th->hit[h]->dcl[d].is_reported)
1742 {
1743 nd++;
1744
1745 /* in hmmsearch, targets are seqs and queries are HMMs;
1746 * in hmmscan, the reverse. but in the ALIDISPLAY
1747 * structure, lengths L and M are for seq and HMMs, not
1748 * for query and target, so sort it out.
1749 */
1750 if (pli->mode == p7_SEARCH_SEQS) { qlen = th->hit[h]->dcl[d].ad->M; tlen = th->hit[h]->dcl[d].ad->L; }
1751 else { qlen = th->hit[h]->dcl[d].ad->L; tlen = th->hit[h]->dcl[d].ad->M; }
1752
1753
1754
1755 if (fprintf(ofp, "%-*s %-*s %5d %-*s %-*s %5d %9.2g %6.1f %5.1f %3d %3d %9.2g %9.2g %6.1f %5.1f %5d %5d %5" PRId64 " %5" PRId64 " %5" PRId64 " %5" PRId64 " %4.2f %s\n",
1756 tnamew, th->hit[h]->name,
1757 taccw, th->hit[h]->acc ? th->hit[h]->acc : "-",
1758 tlen,
1759 qnamew, qname,
1760 qaccw, ( (qacc != NULL && qacc[0] != '\0') ? qacc : "-"),
1761 qlen,
1762 exp(th->hit[h]->lnP) * pli->Z,
1763 th->hit[h]->score,
1764 th->hit[h]->pre_score - th->hit[h]->score, /* bias correction */
1765 nd,
1766 th->hit[h]->nreported,
1767 exp(th->hit[h]->dcl[d].lnP) * pli->domZ,
1768 exp(th->hit[h]->dcl[d].lnP) * pli->Z,
1769 th->hit[h]->dcl[d].bitscore,
1770 th->hit[h]->dcl[d].dombias * eslCONST_LOG2R, /* NATS to BITS at last moment */
1771 th->hit[h]->dcl[d].ad->hmmfrom,
1772 th->hit[h]->dcl[d].ad->hmmto,
1773 th->hit[h]->dcl[d].ad->sqfrom,
1774 th->hit[h]->dcl[d].ad->sqto,
1775 th->hit[h]->dcl[d].ienv,
1776 th->hit[h]->dcl[d].jenv,
1777 (th->hit[h]->dcl[d].oasc / (1.0 + fabs((float) (th->hit[h]->dcl[d].jenv - th->hit[h]->dcl[d].ienv)))),
1778 (th->hit[h]->desc ? th->hit[h]->desc : "-")) < 0)
1779 ESL_EXCEPTION_SYS(eslEWRITE, "tabular per-domain hit list: write failed");
1780
1781 }
1782 }
1783 return eslOK;
1784 }
1785
1786
1787 /* Function: p7_tophits_TabularXfam()
1788 * Synopsis: Output parsable table(s) of hits, in format desired by Xfam.
1789 *
1790 * Purpose: Output a parseable table of reportable hits in sorted
1791 * tophits list <th> in an easily parsed ASCII tabular
1792 * form to stream <ofp>, using final pipeline accounting
1793 * stored in <pli>.
1794 *
1795 * For long-target nucleotide queries, this will print the
1796 * same hits as p7_tophits_TabularTargets(), but with the
1797 * smaller number of (reordered) fields required by Dfam
1798 * scripts.
1799 *
1800 * For protein queries, this will print two tables:
1801 * (a) per-sequence hits as presented by
1802 * p7_tophits_TabularTargets(), but formatted for
1803 * Pfam scripts;
1804 * (b) per-domain hits, similar to those presented by
1805 * p7_tophits_TabularDomains(), but sorted by
1806 * score/e-value, and formated for Pfam scripts.
1807 *
1808 * Returns: <eslOK> on success.
1809 *
1810 * Throws: <eslEMEM> on allocation failure.
1811 * <eslEWRITE> if a write to <ofp> fails; for example, if
1812 * the disk fills up.
1813 */
1814 int
1815 p7_tophits_TabularXfam(FILE *ofp, char *qname, char *qacc, P7_TOPHITS *th, P7_PIPELINE *pli)
1816 {
1817 P7_TOPHITS *domHitlist = NULL;
1818 P7_HIT *domhit = NULL;
1819 int tnamew = ESL_MAX(20, p7_tophits_GetMaxNameLength(th));
1820 int taccw = ESL_MAX(20, p7_tophits_GetMaxAccessionLength(th));
1821 int qnamew = ESL_MAX(20, strlen(qname));
1822 int posw = (pli->long_targets ? ESL_MAX(7, p7_tophits_GetMaxPositionLength(th)) : 0);
1823 int h,d;
1824 int status;
1825
1826
1827 if (pli->long_targets)
1828 {
1829 if (fprintf(ofp, "# hit scores\n# ----------\n#\n") < 0)
1830 ESL_XEXCEPTION_SYS(eslEWRITE, "xfam tabular output: write failed");
1831 if (fprintf(ofp, "# %-*s %-*s %-*s %6s %9s %5s %s %s %6s %*s %*s %*s %*s %*s %s\n",
1832 tnamew-1, "target name", taccw, "acc", qnamew, "query name", "bits", " e-value", " bias", "hmm-st", "hmm-en", "strand", posw, "ali-st", posw, "ali-en", posw, "env-st", posw, "env-en", posw, ( pli->mode == p7_SCAN_MODELS ? "modlen" : "sq-len" ), "description of target") < 0)
1833 ESL_XEXCEPTION_SYS(eslEWRITE, "xfam tabular output: write failed");
1834 if (fprintf(ofp, "# %-*s %-*s %-*s %6s %9s %5s %s %s %6s %*s %*s %*s %*s %*s %s\n",
1835 tnamew-1, "-------------------", taccw, "-------------------", qnamew, "-------------------", "------", "---------", "-----", "-------", "-------", "------", posw, "-------", posw, "-------", posw, "-------", posw, "-------", posw, "-------", "---------------------") < 0)
1836 ESL_XEXCEPTION_SYS(eslEWRITE, "xfam tabular output: write failed");
1837
1838 for (h = 0; h < th->N; h++)
1839 if (th->hit[h]->flags & p7_IS_REPORTED)
1840 {
1841 //d = th->hit[h]->best_domain;
1842 if (fprintf(ofp, "%-*s %-*s %-*s %6.1f %9.2g %5.1f %7d %7d %s %*" PRId64 " %*" PRId64 " %*" PRId64 " %*" PRId64 " %*" PRId64 " %s\n",
1843 tnamew, th->hit[h]->name,
1844 taccw, ( pli->mode == p7_SCAN_MODELS ? (th->hit[h]->acc ? th->hit[h]->acc : "-") : ((qacc && qacc[0] != '\0') ? qacc : "-")),
1845 qnamew, qname,
1846 th->hit[h]->score,
1847 exp(th->hit[h]->lnP),
1848 th->hit[h]->dcl[0].dombias * eslCONST_LOG2R, /* convert nats to bits at last moment */
1849 th->hit[h]->dcl[0].ad->hmmfrom,
1850 th->hit[h]->dcl[0].ad->hmmto,
1851 (th->hit[h]->dcl[0].iali < th->hit[h]->dcl[0].jali ? " + " : " - "),
1852 posw, th->hit[h]->dcl[0].iali,
1853 posw, th->hit[h]->dcl[0].jali,
1854 posw, th->hit[h]->dcl[0].ienv,
1855 posw, th->hit[h]->dcl[0].jenv,
1856 posw, th->hit[h]->dcl[0].ad->L,
1857 th->hit[h]->desc == NULL ? "-" : th->hit[h]->desc) < 0)
1858 ESL_XEXCEPTION_SYS(eslEWRITE, "xfam tabular output: write failed");
1859 }
1860 }
1861 else
1862 {
1863 if (fprintf(ofp, "# Sequence scores\n# ---------------\n#\n") < 0)
1864 ESL_XEXCEPTION_SYS(eslEWRITE, "xfam tabular output: write failed");
1865 if (fprintf(ofp, "# %-*s %6s %9s %3s %5s %5s %s\n",
1866 tnamew-1, "name", " bits", " E-value", "n", "exp", " bias", "description") < 0)
1867 ESL_XEXCEPTION_SYS(eslEWRITE, "xfam tabular output: write failed");
1868 if (fprintf(ofp, "# %*s %6s %9s %3s %5s %5s %s\n",
1869 tnamew-1, "-------------------", "------", "---------","---", "-----", "-----", "---------------------") < 0)
1870 ESL_XEXCEPTION_SYS(eslEWRITE, "xfam tabular output: write failed");
1871
1872 for (h = 0; h < th->N; h++)
1873 {
1874 if (th->hit[h]->flags & p7_IS_REPORTED)
1875 {
1876 if (fprintf(ofp, "%-*s %6.1f %9.2g %3d %5.1f %5.1f %s\n",
1877 tnamew, th->hit[h]->name,
1878 th->hit[h]->score,
1879 exp(th->hit[h]->lnP) * pli->Z,
1880 th->hit[h]->ndom,
1881 th->hit[h]->nexpected,
1882 th->hit[h]->pre_score - th->hit[h]->score, /* bias correction */
1883 (th->hit[h]->desc == NULL ? "-" : th->hit[h]->desc)) < 0)
1884 ESL_XEXCEPTION_SYS(eslEWRITE, "xfam tabular output: write failed");
1885 }
1886 }
1887 if (fprintf(ofp, "\n") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "xfam tabular output: write failed");
1888
1889 /* Need to sort the domains. One way to do this is to re-use the hit sorting machinery,
1890 * so we create one "hit" for each domain, then hand it off to the sorter
1891 */
1892 if ((domHitlist = p7_tophits_Create()) == NULL) return eslEMEM;
1893 for (h = 0; h < th->N; h++)
1894 {
1895 if (th->hit[h]->flags & p7_IS_REPORTED)
1896 {
1897 int ndomReported = 0;
1898 for (d = 0; d < th->hit[h]->ndom; d++)
1899 {
1900 if (th->hit[h]->dcl[d].is_reported)
1901 {
1902 p7_tophits_CreateNextHit(domHitlist, &domhit);
1903 ndomReported++;
1904 ESL_ALLOC(domhit->dcl, sizeof(P7_DOMAIN) );
1905
1906 domhit->ndom = ndomReported; // re-using this variable to track the ordinal value of the domain in the original hit list that generated this pseudo-hit
1907 domhit->name = th->hit[h]->name;
1908 domhit->desc = th->hit[h]->desc;
1909 domhit->dcl[0] = th->hit[h]->dcl[d];
1910 domhit->sortkey = pli->inc_by_E ? -1.0 * th->hit[h]->dcl[d].lnP : th->hit[h]->dcl[d].bitscore;
1911 }
1912 }
1913 }
1914 }
1915 p7_tophits_SortBySortkey(domHitlist);
1916
1917 // Now with this list of sorted "hits" (really domains)
1918 if (fprintf(ofp, "# Domain scores\n# -------------\n#\n") < 0)
1919 ESL_XEXCEPTION_SYS(eslEWRITE, "xfam tabular output: write failed");
1920 if (fprintf(ofp, "# %-*s %6s %9s %5s %5s %6s %6s %6s %6s %6s %6s %s\n",
1921 tnamew-1, " name", "bits", "E-value", "hit", "bias", "env-st", "env-en", "ali-st", "ali-en", "hmm-st", "hmm-en", "description") < 0)
1922 ESL_XEXCEPTION_SYS(eslEWRITE, "xfam tabular output: write failed");
1923 if (fprintf(ofp, "# %*s %6s %9s %5s %5s %6s %6s %6s %6s %6s %6s %s\n",
1924 tnamew-1, "-------------------", "------", "---------", "-----", "-----", "------", "------", "------", "------", "------", "------", "---------------------") < 0)
1925 ESL_XEXCEPTION_SYS(eslEWRITE, "xfam tabular output: write failed");
1926
1927 for (h = 0; h < domHitlist->N; h++)
1928 {
1929 domhit = domHitlist->hit[h];
1930
1931 if (fprintf(ofp, "%-*s %6.1f %9.2g %5d %5.1f %6" PRId64 " %6" PRId64 " %6" PRId64 " %6" PRId64 " %6d %6d %s\n",
1932 tnamew, domHitlist->hit[h]->name,
1933 domhit->dcl[0].bitscore,
1934 exp(domhit->dcl[0].lnP) * pli->Z, //i-Evalue
1935 domhit->ndom,
1936 domhit->dcl[0].dombias * eslCONST_LOG2R, // NATS to BITS at last moment
1937 domhit->dcl[0].ienv,
1938 domhit->dcl[0].jenv,
1939 domhit->dcl[0].ad->sqfrom,
1940 domhit->dcl[0].ad->sqto,
1941 domhit->dcl[0].ad->hmmfrom,
1942 domhit->dcl[0].ad->hmmto,
1943 (domhit->desc ? domhit->desc : "-")) < 0)
1944 ESL_XEXCEPTION_SYS(eslEWRITE, "xfam tabular output: write failed");
1945 }
1946 free (domHitlist->unsrt);
1947 free (domHitlist->hit);
1948 free (domHitlist);
1949 }
1950 return eslOK;
1951
1952 ERROR:
1953 if (domHitlist)
1954 {
1955 free (domHitlist->unsrt);
1956 free (domHitlist->hit);
1957 free (domHitlist);
1958 }
1959 return status;
1960 }
1961
1962
1963 /* Function: p7_tophits_AliScores()
1964 * Synopsis: Output per-position scores for each position of each query/hit pair
1965 *
1966 * Purpose: This depends on per-alignment-position scores having been
1967 * previously computed, as in p7_pipeline_computeAliScores()
1968 *
1969 * Returns: <eslOK> on success.
1970 *
1971 * Throws: none
1972 */
1973 int
1974 p7_tophits_AliScores(FILE *ofp, char *qname, P7_TOPHITS *th )
1975 {
1976 P7_HIT *hit;
1977 int h, i;
1978 float *scores;
1979
1980 for (h = 0; h < th->N; h++) {
1981 hit = th->hit[h];
1982 if (hit->flags & p7_IS_REPORTED)
1983 {
1984 fprintf (ofp, "%s %s %" PRId64 " %" PRId64 " :", qname, hit->name, hit->dcl[0].iali, hit->dcl[0].jali);
1985
1986 scores = hit->dcl[0].scores_per_pos;
1987 for (i=0; i<hit->dcl[0].ad->N; i++) {
1988 if (scores[i] == -eslINFINITY)
1989 fprintf (ofp, " >");
1990 else
1991 fprintf (ofp, " %.3f", scores[i] * eslCONST_LOG2R);
1992
1993 }
1994 fprintf (ofp, "\n");
1995 }
1996
1997 }
1998 return eslOK;
1999
2000 }
2001
2002
2003
2004
•2005 /* Function: p7_tophits_TabularTail()
2006 * Synopsis: Print a trailer on a tabular output file.
2007 *
2008 * Purpose: Print some metadata as a trailer on a tabular output file:
2009 * date/time, the program, HMMER3 version info, the pipeline mode (SCAN or SEARCH),
2010 * the query and target filenames, a spoof commandline
2011 * recording the entire program configuration, and
2012 * a "fini!" that's useful for detecting successful
2013 * output completion.
2014 *
2015 * Args: ofp - open tabular output file (either --tblout or --domtblout)
2016 * progname - "hmmscan", for example
2017 * pipemode - p7_SEARCH_SEQS | p7_SCAN_MODELS
2018 * qfile - name of query file, or '-' for stdin, or '[none]' if NULL
2019 * tfile - name of target file, or '-' for stdin, or '[none]' if NULL
2020 * go - program configuration; used to generate spoofed command line
2021 *
2022 * Returns: <eslOK>.
2023 *
2024 * Throws: <eslEMEM> on allocation failure.
2025 * <eslESYS> if time() or ctime_r() system calls fail.
2026 * <eslEWRITE> on write failure.
2027 *
2028 * Xref: SRE:J7/54
2029 */
2030 int
2031 p7_tophits_TabularTail(FILE *ofp, const char *progname, enum p7_pipemodes_e pipemode, const char *qfile, const char *tfile, const ESL_GETOPTS *go)
2032 {
2033 time_t date = time(NULL);
2034 char *spoof_cmd = NULL;
2035 char *cwd = NULL;
2036 char timestamp[32];
2037 char modestamp[16];
2038 int status;
2039
2040
2041 if ((status = esl_opt_SpoofCmdline(go, &spoof_cmd)) != eslOK) goto ERROR;
2042 if (date == -1) ESL_XEXCEPTION(eslESYS, "time() failed");
2043 if ((ctime_r(&date, timestamp)) == NULL) ESL_XEXCEPTION(eslESYS, "ctime_r() failed");
2044 switch (pipemode) {
2045 case p7_SEARCH_SEQS: strcpy(modestamp, "SEARCH"); break;
2046 case p7_SCAN_MODELS: strcpy(modestamp, "SCAN"); break;
2047 default: ESL_EXCEPTION(eslEINCONCEIVABLE, "wait, what? no such pipemode");
2048 }
2049 esl_getcwd(&cwd);
2050
2051 if (fprintf(ofp, "#\n") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "tabular output tail, write failed");
2052 if (fprintf(ofp, "# Program: %s\n", (progname == NULL) ? "[none]" : progname) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "tabular output tail, write failed");
2053 if (fprintf(ofp, "# Version: %s (%s)\n", HMMER_VERSION, HMMER_DATE) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "tabular output tail, write failed");
2054 if (fprintf(ofp, "# Pipeline mode: %s\n", modestamp) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "tabular output tail, write failed");
2055 if (fprintf(ofp, "# Query file: %s\n", (qfile == NULL) ? "[none]" : qfile) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "tabular output tail, write failed");
2056 if (fprintf(ofp, "# Target file: %s\n", (tfile == NULL) ? "[none]" : tfile) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "tabular output tail, write failed");
2057 if (fprintf(ofp, "# Option settings: %s\n", spoof_cmd) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "tabular output tail, write failed");
2058 if (getenv("SOURCE_DATE_EPOCH") != NULL) {
2059 if (fprintf(ofp, "# Current dir: %s\n", (cwd == NULL) ? "[unknown]" : cwd) < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "tabular output tail, write failed");
2060 if (fprintf(ofp, "# Date: %s", timestamp) < 0) /* timestamp ends in \n */ ESL_XEXCEPTION_SYS(eslEWRITE, "tabular output tail, write failed");
2061 }
2062 if (fprintf(ofp, "# [ok]\n") < 0) ESL_XEXCEPTION_SYS(eslEWRITE, "tabular output tail, write failed");
2063
2064 free(spoof_cmd);
2065 if (cwd) free(cwd);
2066 return eslOK;
2067
2068 ERROR:
2069 if (spoof_cmd) free(spoof_cmd);
2070 if (cwd) free(cwd);
2071 return status;
2072 }
2073 /*------------------- end, tabular output -----------------------*/
2074
2075
2076
2077
2078 /*****************************************************************
2079 * 4. Benchmark driver
2080 *****************************************************************/
2081 #ifdef p7TOPHITS_BENCHMARK
2082 /*
2083 gcc -o benchmark-tophits -std=gnu99 -g -O2 -I. -L. -I../easel -L../easel -Dp7TOPHITS_BENCHMARK p7_tophits.c -lhmmer -leasel -lm
2084 ./benchmark-tophits
2085
2086 As of 28 Dec 07, shows 0.20u for 10 lists of 10,000 hits each (at least ~100x normal expectation),
2087 so we expect top hits list time to be negligible for typical hmmsearch/hmmscan runs.
2088
2089 If needed, we do have opportunity for optimization, however - especially in memory handling.
2090 */
2091 #include <p7_config.h>
2092
2093 #include "easel.h"
2094 #include "esl_getopts.h"
2095 #include "esl_stopwatch.h"
2096 #include "esl_random.h"
2097
2098 #include "hmmer.h"
2099
2100 static ESL_OPTIONS options[] = {
2101 /* name type default env range toggles reqs incomp help docgroup*/
2102 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
2103 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
2104 { "-M", eslARG_INT, "10", NULL, NULL, NULL, NULL, NULL, "number of top hits lists to simulate and merge", 0 },
2105 { "-N", eslARG_INT, "10000", NULL, NULL, NULL, NULL, NULL, "number of top hits to simulate", 0 },
2106 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
2107 };
2108 static char usage[] = "[-options]";
2109 static char banner[] = "benchmark driver for P7_TOPHITS";
2110
2111 int
2112 main(int argc, char **argv)
2113 {
2114 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage);
2115 ESL_STOPWATCH *w = esl_stopwatch_Create();
2116 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
2117 int N = esl_opt_GetInteger(go, "-N");
2118 int M = esl_opt_GetInteger(go, "-M");
2119 P7_TOPHITS **h = NULL;
2120 double *sortkeys = NULL;
2121 char name[] = "not_unique_name";
2122 char acc[] = "not_unique_acc";
2123 char desc[] = "Test description for the purposes of making the benchmark allocate space";
2124 int i,j;
2125 int status;
2126
2127 /* prep work: generate our sort keys before starting to time anything */
2128 ESL_ALLOC(h, sizeof(P7_TOPHITS *) * M); /* allocate pointers for M lists */
2129 ESL_ALLOC(sortkeys, sizeof(double) * N * M);
2130 for (i = 0; i < N*M; i++) sortkeys[i] = esl_random(r);
2131
2132 esl_stopwatch_Start(w);
2133
2134 /* generate M "random" lists and sort them */
2135 for (j = 0; j < M; j++)
2136 {
2137 h[j] = p7_tophits_Create();
2138 for (i = 0; i < N; i++)
2139 p7_tophits_Add(h[j], name, acc, desc, sortkeys[j*N + i],
2140 (float) sortkeys[j*N+i], sortkeys[j*N+i],
2141 (float) sortkeys[j*N+i], sortkeys[j*N+i],
2142 i, i, N,
2143 i, i, N,
2144 i, N, NULL);
2145 p7_tophits_SortBySortkey(h[j]);
2146 }
2147 /* then merge them into one big list in h[0] */
2148 for (j = 1; j < M; j++)
2149 {
2150 p7_tophits_Merge(h[0], h[j]);
2151 p7_tophits_Destroy(h[j]);
2152 }
2153
2154 esl_stopwatch_Stop(w);
2155
2156 p7_tophits_Destroy(h[0]);
2157 status = eslOK;
2158 ERROR:
2159 esl_getopts_Destroy(go);
2160 esl_stopwatch_Destroy(w);
2161 esl_randomness_Destroy(r);
2162 if (sortkeys != NULL) free(sortkeys);
2163 if (h != NULL) free(h);
2164 return status;
2165 }
2166 #endif /*p7TOPHITS_BENCHMARK*/
2167
2168
2169 /*****************************************************************
2170 * 5. Test driver
2171 *****************************************************************/
2172
2173 #ifdef p7TOPHITS_TESTDRIVE
2174 /*
2175 gcc -o tophits_utest -std=gnu99 -g -O2 -I. -L. -I../easel -L../easel -Dp7TOPHITS_TESTDRIVE p7_tophits.c -lhmmer -leasel -lm
2176 ./tophits_test
2177 */
2178 #include <p7_config.h>
2179
2180 #include "easel.h"
2181 #include "esl_getopts.h"
2182 #include "esl_stopwatch.h"
2183 #include "esl_random.h"
2184
2185 #include "hmmer.h"
2186
2187 static ESL_OPTIONS options[] = {
2188 /* name type default env range toggles reqs incomp help docgroup*/
2189 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
2190 { "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 },
2191 { "-N", eslARG_INT, "100", NULL, NULL, NULL, NULL, NULL, "number of top hits to simulate", 0 },
2192 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
2193 };
2194
2195 static char usage[] = "[-options]";
2196 static char banner[] = "test driver for P7_TOPHITS";
2197
2198 int
2199 main(int argc, char **argv)
2200 {
2201 ESL_GETOPTS *go = p7_CreateDefaultApp(options, 0, argc, argv, banner, usage);
2202 ESL_RANDOMNESS *r = esl_randomness_CreateFast(esl_opt_GetInteger(go, "-s"));
2203 int N = esl_opt_GetInteger(go, "-N");
2204 P7_TOPHITS *h1 = NULL;
2205 P7_TOPHITS *h2 = NULL;
2206 P7_TOPHITS *h3 = NULL;
2207 char name[] = "not_unique_name";
2208 char acc[] = "not_unique_acc";
2209 char desc[] = "Test description for the purposes of making the test driver allocate space";
2210 double key;
2211 int i;
2212
2213 h1 = p7_tophits_Create();
2214 h2 = p7_tophits_Create();
2215 h3 = p7_tophits_Create();
2216
2217 for (i = 0; i < N; i++)
2218 {
2219 key = esl_random(r);
2220 p7_tophits_Add(h1, name, acc, desc, key, (float) key, key, (float) key, key, i, i, N, i, i, N, 1, 1, NULL);
2221 key = 10.0 * esl_random(r);
2222 p7_tophits_Add(h2, name, acc, desc, key, (float) key, key, (float) key, key, i, i, N, i, i, N, 2, 2, NULL);
2223 key = 0.1 * esl_random(r);
2224 p7_tophits_Add(h3, name, acc, desc, key, (float) key, key, (float) key, key, i, i, N, i, i, N, 3, 3, NULL);
2225 }
2226 p7_tophits_Add(h1, "last", NULL, NULL, -1.0, (float) key, key, (float) key, key, i, i, N, i, i, N, 1, 1, NULL);
2227 p7_tophits_Add(h1, "first", NULL, NULL, 20.0, (float) key, key, (float) key, key, i, i, N, i, i, N, 1, 1, NULL);
2228
2229 p7_tophits_SortBySortkey(h1);
2230 if (strcmp(h1->hit[0]->name, "first") != 0) esl_fatal("sort failed (top is %s = %f)", h1->hit[0]->name, h1->hit[0]->sortkey);
2231 if (strcmp(h1->hit[N+1]->name, "last") != 0) esl_fatal("sort failed (last is %s = %f)", h1->hit[N+1]->name, h1->hit[N+1]->sortkey);
2232
2233 p7_tophits_Merge(h1, h2);
2234 if (strcmp(h1->hit[0]->name, "first") != 0) esl_fatal("after merge 1, sort failed (top is %s = %f)", h1->hit[0]->name, h1->hit[0]->sortkey);
2235 if (strcmp(h1->hit[2*N+1]->name, "last") != 0) esl_fatal("after merge 1, sort failed (last is %s = %f)", h1->hit[2*N+1]->name, h1->hit[2*N+1]->sortkey);
2236
2237 p7_tophits_Merge(h3, h1);
2238 if (strcmp(h3->hit[0]->name, "first") != 0) esl_fatal("after merge 2, sort failed (top is %s = %f)", h3->hit[0]->name, h3->hit[0]->sortkey);
2239 if (strcmp(h3->hit[3*N+1]->name, "last") != 0) esl_fatal("after merge 2, sort failed (last is %s = %f)", h3->hit[3*N+1]->name, h3->hit[3*N+1]->sortkey);
2240
2241 if (p7_tophits_GetMaxNameLength(h3) != strlen(name)) esl_fatal("GetMaxNameLength() failed");
2242
2243 p7_tophits_Destroy(h1);
2244 p7_tophits_Destroy(h2);
2245 p7_tophits_Destroy(h3);
2246 esl_randomness_Destroy(r);
2247 esl_getopts_Destroy(go);
2248 return eslOK;
2249 }
2250 #endif /*p7TOPHITS_TESTDRIVE*/
2251
2252
2253