Next version of JABA
[jabaws.git] / binaries / src / fasta34 / last_tat.c
1
2 /* copyright (c) 1996, 1997, 1998, 1999 William R. Pearson and the
3    U. of Virginia */
4
5 /* $Name: fa_34_26_5 $ - $Id: last_tat.c,v 1.8 2006/04/12 18:00:02 wrp Exp $ */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include "defs.h"
12 #include "mm_file.h"
13
14 #include "structs.h"
15 #include "param.h"
16
17 #ifndef PCOMPLIB
18 #include "mw.h"
19 #else
20 #include "msg.h"
21 #include "p_mw.h"
22
23 void do_stage2(struct beststr **bptr, int nbest, struct mngmsg m_msg0,
24                int s_func, struct qmng_str *qm_msp);
25 #endif
26
27
28 extern int (*ranlib) (char *str, int cnt,
29                fseek_t libpos, char *libstr,
30                struct lmf_str *lm_fd);
31
32 #define RANLIB (m_fptr->ranlib)
33
34 #define MAX_BLINE 200
35
36 int
37 re_getlib(unsigned char *, int, int, int, int, int, long *, long *, 
38           struct lmf_str *m_fptr);
39
40 void
41 do_work(unsigned char *aa0, int n0, unsigned char *aa1, int n1, int frame, 
42         struct pstruct *ppst, void *f_str, int qr_flg, struct rstruct *rst);
43
44 extern void
45 do_opt (unsigned char *aa0, int n0, unsigned char *aa1, int n1,
46         int frame, struct pstruct *pst, void *f_str,
47         struct rstruct *rst);
48
49 struct lmf_str *re_openlib(struct lmf_str *, int outtty);
50
51 void sortbestz (struct beststr **bptr, int nbest);
52
53 double zs_to_E(double zs,int n1, int isdna, long entries, struct db_str db);
54
55 double scale_one_score(int ipos, double escore, struct db_str db, void *rs_str);
56
57 void sortbests (struct beststr **bptr, int nbest)
58 {
59     int gap, i, j;
60     struct beststr *tmp;
61
62     for (gap = nbest/2; gap > 0; gap /= 2)
63         for (i = gap; i < nbest; i++)
64             for (j = i - gap; j >= 0; j-= gap) {
65               if (bptr[j]->score[0] >= bptr[j + gap]->score[0]) break;
66               tmp = bptr[j];
67               bptr[j] = bptr[j + gap];
68               bptr[j + gap] = tmp;
69             }
70 }
71
72 int
73 last_calc(
74 #ifndef PCOMPLIB
75           unsigned char **aa0, unsigned char *aa1, int maxn,
76 #endif    
77           struct beststr **bptr, int nbest, 
78           struct mngmsg m_msg, struct pstruct *ppst
79 #ifdef PCOMPLIB
80           , struct qmng_str *qm_msp
81 #else
82           , void **f_str
83 #endif
84           , void *rstat_str)
85 {
86   int nopt, ib;
87   struct beststr *bbp;
88   long loffset, l_off;
89   int n0, n1;
90   struct rstruct rst;
91   struct lmf_str *m_fptr;
92   char bline[60];
93   int tat_samp, tat_inc, loop_cnt, i;
94   double min_escore, ess;
95
96   n0 = m_msg.n0;
97
98   sortbestz(bptr,nbest);
99
100   tat_inc = 500;
101 /*
102   if (zs_to_E(bptr[0]->zscore,bptr[0]->n1,0,ppst->zdb_size,m_msg.db)/ 
103       zs_to_E(bptr[nbest-1]->zscore,bptr[nbest-1]->n1,0,ppst->zdb_size,m_msg.db)
104         < 1e-20) { tat_inc /= 4 ;}
105 */
106
107 /* || (zs_to_E(bptr[0]->zscore,bptr[0]->n1,0,ppst->zdb_size,m_msg.db)< 1e-5); */
108
109   ib = tat_samp = 0;
110   for (loop_cnt = 0; loop_cnt < 5; loop_cnt++) {
111     tat_samp += tat_inc;
112     nopt = min(nbest,tat_samp);
113     min_escore = 1000000.0;
114 #ifndef PCOMPLIB
115     for ( ; ib<nopt; ib++) {
116       bbp = bptr[ib];
117
118       if (bbp->score[0] < 0) break;
119
120       if ((m_fptr=re_openlib(bbp->m_file_p,!m_msg.quiet))==NULL) {
121         fprintf(stderr,"*** cannot re-open %s\n",bbp->m_file_p->lb_name);
122         exit(1);
123       }
124       RANLIB(bline,sizeof(bline),bbp->lseek,bbp->libstr,m_fptr);
125
126       n1 = re_getlib(aa1,maxn,m_msg.maxt3,m_msg.loff,bbp->cont,m_msg.term_code,
127                      &loffset,&l_off,bbp->m_file_p);
128
129       do_opt(aa0[bbp->frame],m_msg.n0,aa1,n1,bbp->frame,ppst,
130              f_str[bbp->frame],&rst);
131       bbp->score[0]=rst.score[0];
132       bbp->score[1]=rst.score[1];
133       bbp->score[2]=rst.score[2];
134       bbp->escore=rst.escore;
135       bbp->segnum = rst.segnum;
136       bbp->seglen = rst.seglen;
137
138       if ((ess=scale_one_score(ib, bbp->escore, m_msg.db, rstat_str)) < 
139         min_escore) { min_escore = ess;}
140       /*
141       fprintf(stderr,"%d: %4d %2d %3d %.4g %.4g\n",
142               ib, bbp->score[0], bbp->segnum,bbp->seglen,bbp->escore, ess);
143       */
144     }
145 #else
146     do_stage2(&bptr[ib], nopt-ib, m_msg, DO_CALC_FLG, qm_msp);
147
148     for ( ; ib < nopt; ib++) {
149       if ((ess=scale_one_score(ib, bptr[ib]->escore, m_msg.db, rstat_str)) < 
150         min_escore) { min_escore = ess;}
151       /*
152       fprintf(stderr, "%d: %4d %2d %3d %.4g %.4g\n",
153               ib,bptr[ib]->score[0],bptr[ib]->segnum,bptr[ib]->seglen,bptr[ib]->escore,ess);
154       */
155     }
156 #endif
157
158
159     if (min_escore > m_msg.e_cut) return ib;
160   }
161   return ib;
162 }