Debian Codegraph @

hmmer/src/p7_gbands.c

raw symbols SCIP
1 #include <p7_config.h>
2
3 #include "easel.h"
4 #include "esl_vectorops.h"
5
6 #include "p7_gbands.h"
7
8 P7_GBANDS *
9 p7_gbands_Create(void)
10 {
11 P7_GBANDS *bnd = NULL;
12 int init_segalloc = 4;
13 int init_rowalloc = 64;
14 int status;
15
16 ESL_ALLOC(bnd, sizeof(P7_GBANDS));
17 bnd->nseg = 0;
18 bnd->nrow = 0;
19 bnd->L = 0;
20 bnd->M = 0;
21 bnd->ncell = 0;
22 bnd->imem = NULL;
23 bnd->kmem = NULL;
24
25
26 ESL_ALLOC(bnd->imem, sizeof(int) * init_segalloc * 2); /* *2: for ia, ib pairs */
27 ESL_ALLOC(bnd->kmem, sizeof(int) * init_rowalloc * p7_GBANDS_NK);
28 bnd->segalloc = init_segalloc;
29 bnd->rowalloc = init_rowalloc;
30
31 return bnd;
32
33 ERROR:
34 p7_gbands_Destroy(bnd);
35 return NULL;
36 }
37
38 int
39 p7_gbands_Reuse(P7_GBANDS *bnd)
40 {
41 bnd->nseg = 0;
42 bnd->nrow = 0;
43 bnd->L = 0;
44 bnd->M = 0;
45 bnd->ncell = 0;
46 return eslOK;
47 }
48
49 /* Function:
50 * Synopsis:
51 *
52 * Purpose:
53 * <p7_gbands_Append()> calls must be made in ascending <i> order,
54 * from <i == 1..L>.
55 * Args:
56 *
57 * Returns:
58 *
59 * Throws: (no abnormal error conditions)
60 *
61 * Xref:
62 */
63 int
64 p7_gbands_Append(P7_GBANDS *bnd, int i, int ka, int kb)
65 {
66 int status;
67
68 if (bnd->nseg == 0 ||
69 i > 1 + bnd->imem[(bnd->nseg-1)*2 +1]) /* i > ib[cur_g] + 1; need to start a new segment */
70 {
71 if (bnd->nseg == bnd->segalloc && (status = p7_gbands_GrowSegs(bnd)) != eslOK) goto ERROR;
72 bnd->imem[bnd->nseg*2] = i; /* ia */
73 bnd->imem[bnd->nseg*2+1] = i; /* ib */
74 bnd->nseg++;
75 }
76 else /* else, append i onto previous segment by incrementing ib */
77 bnd->imem[(bnd->nseg-1)*2+1] += 1; /* equiv to setting = i */
78
79 if (bnd->nrow == bnd->rowalloc && (status = p7_gbands_GrowRows(bnd)) != eslOK) goto ERROR;
80 bnd->kmem[bnd->nrow*p7_GBANDS_NK] = ka;
81 bnd->kmem[bnd->nrow*p7_GBANDS_NK+1] = kb;
82 bnd->nrow += 1;
83 bnd->ncell += kb-ka+1;
84 return eslOK;
85
86 ERROR:
87 return status;
88 }
89
90
91 /*
92 * Build the band structure backwards. Caller will need to
93 * call <p7_gbands_Reverse()> when done.
94 */
95 int
96 p7_gbands_Prepend(P7_GBANDS *bnd, int i, int ka, int kb)
97 {
98 int status;
99
100 if (! bnd->nseg ||
101 i < bnd->imem[(bnd->nseg-1)*2+1] - 1) /* i < ia[cur_g]-1; start a new segment */
102 {
103 if (bnd->nseg == bnd->segalloc && (status = p7_gbands_GrowSegs(bnd)) != eslOK) return status;
104 bnd->imem[bnd->nseg*2] = i; /* ib */
105 bnd->imem[bnd->nseg*2+1] = i; /* ia */
106 bnd->nseg++;
107 }
108 else /* else, prepend i to prev segment by decrementing its ia */
109 bnd->imem[(bnd->nseg-1)*2+1] -= 1; /* equiv to setting ia[g] = i */
110
111 if (bnd->nrow == bnd->rowalloc && (status = p7_gbands_GrowRows(bnd)) != eslOK) return status;
112 bnd->kmem[bnd->nrow*p7_GBANDS_NK] = kb;
113 bnd->kmem[bnd->nrow*p7_GBANDS_NK+1] = ka;
114 bnd->nrow += 1;
115 bnd->ncell += kb-ka+1;
116 return eslOK;
117 }
118
119 /* Function: p7_gbands_Reverse()
120 * Synopsis: Reverse the band structure arrays, after a backwards DP pass.
121 *
122 * Purpose: Our checkpointed DP posterior decoding algorithms that make a
123 * band structure work backwards in rows from L..1, and so they
124 * construct a <P7_GBANDS> structure that has its data elements
125 * in reversed order. Before we can use that structure, we have
126 * to reverse these arrays.
127 *
128 * Args: bnd - band list to reverse.
129 *
130 * Returns: <eslOK> on success.
131 */
132 int
133 p7_gbands_Reverse(P7_GBANDS *bnd)
134 {
135 esl_vec_IReverse(bnd->imem, bnd->imem, 2*bnd->nseg);
136 esl_vec_IReverse(bnd->kmem, bnd->kmem, 2*bnd->nrow);
137 return eslOK;
138 }
139
140
141 int
142 p7_gbands_GrowSegs(P7_GBANDS *bnd)
143 {
144 int new_segalloc = bnd->segalloc * 2; /* grow by doubling */
145 int status;
146
147 ESL_REALLOC(bnd->imem, sizeof(int) * new_segalloc * 2);
148 bnd->segalloc = new_segalloc;
149 return eslOK;
150
151 ERROR:
152 return status;
153 }
154
155 int
156 p7_gbands_GrowRows(P7_GBANDS *bnd)
157 {
158 int new_rowalloc = bnd->rowalloc * 2;
159 int status;
160
161 ESL_REALLOC(bnd->kmem, sizeof(int) * new_rowalloc * p7_GBANDS_NK);
162 bnd->rowalloc = new_rowalloc;
163 return eslOK;
164
165 ERROR:
166 return status;
167 }
168
169 void
170 p7_gbands_Destroy(P7_GBANDS *bnd)
171 {
172 if (bnd) {
173 if (bnd->imem) free(bnd->imem);
174 if (bnd->kmem) free(bnd->kmem);
175 free(bnd);
176 }
177 }
178
179
180
181 /* Function:
182 * Synopsis:
183 *
184 * Purpose:
185 * Also serves to demonstrate standard iteration method,
186 * over segments and rows.
187 * Args:
188 *
189 * Returns:
190 *
191 * Throws: (no abnormal error conditions)
192 *
193 * Xref:
194 */
195 int
196 p7_gbands_Dump(FILE *ofp, P7_GBANDS *bnd)
197 {
198 int g, i;
199 int *bnd_ip = bnd->imem;
200 int *bnd_kp = bnd->kmem;
201 int ia, ib;
202 int ka, kb;
203
204 i = 0;
205 for (g = 0; g < bnd->nseg; g++)
206 {
207 ia = *bnd_ip; bnd_ip++;
208 ib = *bnd_ip; bnd_ip++;
209 if (ia > i+1) fprintf(ofp, "...\n");
210
211 for (i = ia; i <= ib; i++)
212 {
213 ka = *bnd_kp; bnd_kp++;
214 kb = *bnd_kp; bnd_kp++;
215
216 fprintf(ofp, "%6d %6d %6d\n", i, ka, kb);
217 }
218 }
219 if (i <= bnd->L) fprintf(ofp, "...\n");
220
221 printf("%" PRId64 " cells banded, %" PRId64 " total; fraction = %f\n",
222 bnd->ncell,
223 (int64_t) bnd->L * (int64_t) bnd->M,
224 (double) bnd->ncell / ((double) bnd->L * (double) bnd->M));
225
226 return eslOK;
227 }
228
229
230