NCBI C++ ToolKit
thrdttbi.c
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: thrdttbi.c 32839 2007-03-05 20:41:55Z kazimird $
2 *===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * File Name: thrdttbi.c
27 *
28 * Author: Stephen Bryant
29 *
30 * Initial Version Creation Date: 08/16/2000
31 *
32 * $Revision: 32839 $
33 *
34 * File Description: threader
35 */
36 
37 
38 
41 #include <math.h>
42 #define SIGDIF 1
43 
44 /* Merge a sampled thread into a linked-list stack containing the lowest */
45 /* energy threads found in sampling up until now. */
46 
47 /* Modified 7/96 to optionally consider as new only those threads which */
48 /* differ in alignment variables. If new location variables give a lower */
49 /* energy, replace previous values for that alignment. This change is */
50 /* intended to reduce the storage required for alternative, suboptimal */
51 /* alignments. It is refered to below as alignment-only mode. */
52 
53 int ttbi(Cur_Aln* sai, Cur_Loc* sli, Thd_Gsm* tdg, Thd_Tbl* ttb, int nrs, int o) {
54 /*---------------------------------------------------------*/
55 /* sai: Current alignment of query sequence with core */
56 /* sli: Current locations of core segments in the motif */
57 /* tdg: Energy of the current alignment, current core */
58 /* ttb: Tables to hold Results of Gibbs sampled threading */
59 /* nrs: Random start index for this thread */
60 /* o: If nonzero, only different alignments are stored */
61 /*---------------------------------------------------------*/
62 
63 int i,j; /* Counters */
64 int ct; /* Index of current thread in list traversal */
65 int in; /* Index where a new thread will be inserted */
66 int pr; /* Index of previous thread in linked list */
67 int nx; /* Index of next thread in linked list */
68 int sm=0; /* Test if two threads are the same */
69 int le; /* Test if new thread has lower energy */
70 float cg; /* Energy of current thread */
71 
72 
73 /* Determine the sort point in the linked list, the index of the first thread */
74 /* whose score is equal to or lower than that of the new thread. Also flag */
75 /* whether the new thread is identical to this one. */
76 
77 if(tdg->dg<ttb->tg[ttb->mn]) {
78  ct=-1; sm=0;
79 }
80 
81 else {
82  /* Decend linked list from the top-scoring thread */
83  ct=ttb->mx; for(i=0; i<ttb->n; i++) {
84 
85  /* Test if new thread has higher score than the current */
86  cg=ttb->tg[ct];
87  le=(tdg->dg>=cg) ? 1:0;
88 
89  /* Test if the new thread is the same as the current. If it's */
90  /* energy is within roundoff of the current thread, test that */
91  /* all location and aligment variables are the same. */
92  sm=0; if(fabs((double)((tdg->dg)-cg))<SIGDIF) {
93  sm=1; for(j=0; j<sli->nsc; j++) {
94  if(sai->al[j]!=ttb->al[j][ct]) {sm=0; break;}
95  if(sli->no[j]!=ttb->no[j][ct]) {sm=0; break;}
96  if(sli->co[j]!=ttb->co[j][ct]) {sm=0; break;} } }
97 
98 
99  /* Continue to next thread if not higher score and not the same */
100  if(le==0&&sm==0) {ct=ttb->nx[ct]; continue; }
101  else break; } }
102 
103 
104 /* If the new thread is identical to that identified in the list search, */
105 /* increment its frequency counter and return. No other action needed. */
106 
107 if(sm==1) { ttb->tf[ct]++;
108  if(ttb->ls[ct]!=nrs) { ttb->ts[ct]++; ttb->ls[ct]=nrs; }
109  return(0); }
110 
111 
112 /* If in alignment-only mode, check whether a thread with the same alignment */
113 /* variables exists within the list, regardless of its score. This will be */
114 /* the replacement point, the index where new thread data will be recorded. */
115 
116 in=-1; if (o!=0) {
117 
118  /* Loop over thread table */
119  for(i=0; i<=ttb->n; i++) {
120 
121  /* Test whether alignment variables are the same */
122  in=i; for(j=0; j<sli->nsc; j++) {
123  if(sai->al[j]!=ttb->al[j][i]) {in=-1; break;} }
124 
125  /* If so, record this thread index */
126  if(in>=0) break; } }
127 
128 
129 /* If not in alignment-only mode, or if the new thread does not match any in */
130 /* the list, check whether the new thread's score is higher than the current */
131 /* minimum. If not, ignore the new thread. If so, set the replacement point */
132 /* as the index of the current minimum score thread. Initialize the frequency */
133 /* counter of the new thread. */
134 
135 if(o==0 || in<0) {
136 
137  /* If the new thread has score lower than any, no action needed. */
138  if(ct<0) return(0);
139 
140  /* Otherwise set the replace point as the minimum score thread */
141  in=ttb->mn;
142 
143  /* And set the new thread's frequency counter to one */
144  ttb->tf[in]=1; ttb->ts[in]=1; ttb->ls[in]=nrs; }
145 
146 
147 /* If in alignment-only mode, and a matching alignment has been found, */
148 /* increment it's frequency counter. If the new thread has lower score take */
149 /* take no further action. */
150 
151 else {
152 
153  /* Increment frequency counter for the matching thread */
154  ttb->tf[in]++;
155  if(ttb->ls[in]!=nrs) { ttb->ts[in]++; ttb->ls[in]=nrs; }
156 
157  /* If new thread has lower score, no action needed */
158  if(tdg->dg<=ttb->tg[in]) return(0); }
159 
160 
161 /* Copy score and alignment data of the new thread into the thread table. */
162 
163 
164 ttb->tg[in]=tdg->dg;
165 ttb->ps[in]=tdg->ps;
166 ttb->ms[in]=tdg->ms;
167 ttb->m0[in]=tdg->m0;
168 ttb->cs[in]=tdg->cs;
169 ttb->lps[in]=tdg->ls;
170 /*printf("%d,%f,%f,%f\n",in,ttb->ps[in],ttb->ms[in],ttb->tg[in]);*/
171 
172 for(j=0;j<sli->nsc;j++) {
173  ttb->al[j][in]=sai->al[j];
174  ttb->no[j][in]=sli->no[j];
175  ttb->co[j][in]=sli->co[j]; }
176 
177 
178 /* If the sort position of the new thread differs from that of the thread */
179 /* it replaces, update the linked list pointers. */
180 
181 
182 if(ct!=in) {
183 
184  /* Pull thread to be replaced from the list */
185  pr=ttb->pr[in];
186  nx=ttb->nx[in];
187  if(in==ttb->mn) ttb->mn=pr; else
188  ttb->pr[nx]=pr;
189  if(in!=ttb->mx) ttb->nx[pr]=nx;
190 
191  /* Insert the new thread at its sort position */
192  if(ct==ttb->mx) ttb->mx=in; else {
193  pr=ttb->pr[ct];
194  ttb->nx[pr]=in;
195  ttb->pr[in]=pr; }
196  ttb->nx[in]=ct;
197  ttb->pr[ct]=in; }
198 
199 return(1);
200 }
int i
#define fabs(v)
Definition: ncbi_dispd.c:46
bool le(T x_, T y_, T round_)
Definition: njn_approx.hpp:84
std::istream & in(std::istream &in_, double &x_)
int * al
Definition: thrdatd.h:190
int nsc
Definition: thrdatd.h:181
int * no
Definition: thrdatd.h:179
int * co
Definition: thrdatd.h:180
float ps
Definition: thrdatd.h:250
float m0
Definition: thrdatd.h:252
float ls
Definition: thrdatd.h:254
float cs
Definition: thrdatd.h:253
float ms
Definition: thrdatd.h:251
float dg
Definition: thrdatd.h:249
int ** no
Definition: thrdatd.h:153
float * cs
Definition: thrdatd.h:142
int mn
Definition: thrdatd.h:158
float * ms
Definition: thrdatd.h:141
int * pr
Definition: thrdatd.h:155
int * tf
Definition: thrdatd.h:149
int n
Definition: thrdatd.h:159
int * ts
Definition: thrdatd.h:150
float * ps
Definition: thrdatd.h:140
int * nx
Definition: thrdatd.h:156
float * lps
Definition: thrdatd.h:143
int ** al
Definition: thrdatd.h:152
int * ls
Definition: thrdatd.h:151
float * m0
Definition: thrdatd.h:146
int ** co
Definition: thrdatd.h:154
float * tg
Definition: thrdatd.h:139
int mx
Definition: thrdatd.h:157
int ttbi(Cur_Aln *sai, Cur_Loc *sli, Thd_Gsm *tdg, Thd_Tbl *ttb, int nrs, int o)
Definition: thrdttbi.c:53
#define SIGDIF
Definition: thrdttbi.c:42
Modified on Mon Apr 22 04:02:47 2024 by modify_doxy.py rev. 669887