| 1 | /* Splitting to training/test sets: Cobalt and Blue algorithms. |
| 2 | * [Petti & Eddy, 2022] |
| 3 | * |
| 4 | * Constructing quasi-independent training and test sets when input |
| 5 | * data aren't truly independent; for example, for homologous |
| 6 | * biological sequences. Instead of true independence, we aim to |
| 7 | * construct _dissimilar_ training/test sets, such that no sequence in |
| 8 | * the training set has >= some pairwise identity threshold to any |
| 9 | * sequence in the test set. |
| 10 | * |
| 11 | * More formally: split an input set V into disjoint sets S and T such |
| 12 | * that no element in S is linked (has an edge) to any element in T, |
| 13 | * with edges defined as pairs with >= some pairwise similarity |
| 14 | * measure; and we define S as the larger of the two sets, |S| >= |
| 15 | * |T|. Some elements of V may not be included in either S or T to |
| 16 | * achieve the split; |S \cup T| <= |V|. We call S,T a _bipartite |
| 17 | * independent pair_ (BIP). |
| 18 | * |
| 19 | * There are two algorithms, Blue and Cobalt. Cobalt is the simpler |
| 20 | * and faster algorithm. Blue is more likely to achieve a split in |
| 21 | * difficult cases. Both algorithms are randomized and will generally |
| 22 | * give different results in repeated runs. |
| 23 | * |
| 24 | * Simpler variants of both algorithms filter redundancy from V, |
| 25 | * creating an "independent set" (IS) U such that there is no edge |
| 26 | * between any pair of elements in U; |U| <= |V|. These "independent |
| 27 | * set" algorithms from graph theory were the starting point for |
| 28 | * developing Blue and Cobalt for the BIP problem. The IS functions |
| 29 | * are called "mono", whereas the BIP functions are called "bi", thus: |
| 30 | * |
| 31 | * monoCobalt = greedy sequential maximum IS algorithm [Blelloch et al, 2012] |
| 32 | * biCobalt = adapted by us to the BIP problem |
| 33 | * monoBlue = IS random priority algorithm [Metivier et al, 2011] |
| 34 | * biBlue = adapted by us to the BIP problem |
| 35 | * |
| 36 | * Solely as a baseline for comparison, we also provide |
| 37 | * esl_iset_biRandom(), which simply puts some random fraction of |
| 38 | * elements in set S, then puts all other eligible elements (those |
| 39 | * with no linkage to any element in S) into T. (This is a bad way to |
| 40 | * do it.) [Petti & Eddy, 2022] calls this "independent selection". |
| 41 | * For what [Petty & Eddy, 2022] call the Cluster algorithm, see |
| 42 | * `esl_msacluster_SingleLinkage()`. |
| 43 | * |
| 44 | * When we construct training and test sets, we typically use two |
| 45 | * steps: first splitting an input alignment V into training set S and |
| 46 | * interim test set T' using (say) biCobalt, then filtering redundancy |
| 47 | * out of T' using (say) monoCobalt to create the final test set T. |
| 48 | * |
| 49 | * The implementations here are generalized such that elements |
| 50 | * (vertices) can be of any data type or structure; there is no |
| 51 | * dependency on Easel biosequence modules, only `random` and |
| 52 | * `vectorops`. The user provides a helper function that determines if |
| 53 | * two elements are linked by an edge. See the example routine at the |
| 54 | * end for an example of how this generalized interface works with a |
| 55 | * digital MSA. |
| 56 | * |
| 57 | * Contents: |
| 58 | * 1. Cobalt splitting algorithms: mono and bipartite |
| 59 | * 2. Blue splitting algorithms: mono and bipartite |
| 60 | * 3. Random splitting algorithm: bipartite |
| 61 | * 4. Counting, evaluation of splits |
| 62 | * 5. Debugging and development routines |
| 63 | * 6. Unit tests |
| 64 | * 7. Test driver |
| 65 | * 8. Example |
| 66 | */ |
| 67 | #include <esl_config.h> |
| 68 | |
| 69 | #include <stdlib.h> |
| 70 | |
| 71 | #include "easel.h" |
| 72 | #include "esl_random.h" |
| 73 | #include "esl_vectorops.h" |
| 74 | |
| 75 | #include "esl_iset.h" |
| 76 | |
| 77 | /***************************************************************** |
| 78 | * 1. Cobalt splitting algorithms |
| 79 | *****************************************************************/ |
| 80 | |
| 81 | /* Function: esl_iset_monoCobalt() |
| 82 | * Synopsis: Greedy algorithm for independent set, with a random order |
| 83 | * Incept: SNP, 16 Oct 2020 |
| 84 | * |
| 85 | * Purpose: Produces an independent set. |
| 86 | * |
| 87 | * U= empty set |
| 88 | * For each vertex v: |
| 89 | * If v is not adjacent to any vertex in U, add v to U |
| 90 | * return U |
| 91 | * |
| 92 | * Two vertices are adjacent if their corresponding sequences are >t% identical |
| 93 | * |
| 94 | * Args: r - source of randomness |
| 95 | * base - pointer to array of n fixed-size vertices to be clustered. |
| 96 | * n - number of vertices |
| 97 | * size - size of each vertex element |
| 98 | * linkfunc - pointer to caller's function for defining linked pairs |
| 99 | * param - pointer to any data that needs to be provided to <(*linkfunc)> |
| 100 | * workspace - buffer for intermediate calculations. Must be at least 2 * sizeof(int) * n bytes. |
| 101 | * assignments - RETURN: assignments to sets (caller provides n*sizeof(int) space) |
| 102 | * |
| 103 | * Returns: <eslOK> on success; the <assignments[0..nseq-1]> array contains |
| 104 | * set indices: |
| 105 | * 0 - vertex not in independent set |
| 106 | * 1 - vertex is in independent set |
| 107 | * |
| 108 | * Throws: status codes from the caller's <(*linkfunc)> on failure; in this case, |
| 109 | * the contents of <*assignments> is undefined, and <*ret_C> is 0. |
| 110 | */ |
| 111 | int |
| 112 | esl_iset_monoCobalt(ESL_RANDOMNESS *rng, const void *base, size_t n, size_t size, |
| 113 | int (*linkfunc)(const void *, const void *, const void *, int *), const void *param, |
| 114 | int *workspace, int *assignments) |
| 115 | { |
| 116 | int *a = NULL; // randomly shuffled array of input vertex indices |
| 117 | int *b = NULL; // array of vertex indices added to iset so far |
| 118 | int nb = 0; // number of vertices added to b so far |
| 119 | int i,j,v; // indices and vertices |
| 120 | int do_link; |
| 121 | int adj; |
| 122 | int status; |
| 123 | |
| 124 | a = workspace; |
| 125 | b = workspace + n; |
| 126 | |
| 127 | for (v = 0; v < n; v++) a[v] = n-v-1; // initialize by putting all vertices into array a |
| 128 | esl_vec_IShuffle(rng, a, n); // ... and shuffling them randomly |
| 129 | esl_vec_ISet(assignments, n, 0); |
| 130 | |
| 131 | for (j = 0; j < n; j++) |
| 132 | { |
| 133 | v = a[j]; // decide whether this v goes in iset, having no link to anything already in it |
| 134 | adj=FALSE; |
| 135 | for (i = n; i < n+nb; i++) |
| 136 | { |
| 137 | if ((status = (*linkfunc)( (char *) base + v*size, (char *) base + a[i]*size, param, &do_link)) != eslOK) return status; |
| 138 | if (do_link) { adj = TRUE; break; } |
| 139 | } |
| 140 | |
| 141 | if (adj) assignments[v]=0; // if exited loop early, v is adjacent to a vertex in b, v will not go in iset |
| 142 | else { // else, if ran through loop without exiting early, v is not adjacent to any vertex in b, v will go in iset |
| 143 | assignments[v] = 1; |
| 144 | b[nb] = v; |
| 145 | nb++; |
| 146 | } |
| 147 | } |
| 148 | return eslOK; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | |
| 153 | /* Function: esl_iset_biCobalt() |
| 154 | * Synopsis: Greedy algorithm for bipartite independent set, with a random order |
| 155 | * Incept: SNP, 16 Oct 2020 |
| 156 | * |
| 157 | * Purpose: Produces an bipartite independent pair. |
| 158 | * |
| 159 | * S= empty set |
| 160 | * T= empty set |
| 161 | * for each vertex v: |
| 162 | * With probability 1/2: |
| 163 | * if v is not adjacent to any vertex in T, add v to S |
| 164 | * else, if v is not adjacent to any vertex in S, add v to T |
| 165 | * Alternately (with probability 1/2): |
| 166 | * if v is not adjacent to any vertex in S, add v to T |
| 167 | * else, if v is not adjacent to any vertex in T, add v to S |
| 168 | * return S,T |
| 169 | * |
| 170 | * Two vertices are adjacent if their corresponding sequences are >t% identical |
| 171 | * |
| 172 | * Args: base - pointer to array of n fixed-size vertices to be clustered. |
| 173 | * n - number of vertices |
| 174 | * size - size of each vertex element |
| 175 | * linkfunc - pointer to caller's function for defining linked pairs |
| 176 | * param - pointer to any data that needs to be provided to <(*linkfunc)> |
| 177 | * workspace - buffer for intermediate calculations. Must be at least 3 * sizeof(int) * n bytes. |
| 178 | * assignments - RETURN: assignments to sets (caller provides n*sizeof(int) space) |
| 179 | * r - source of randomness |
| 180 | * |
| 181 | * Returns: <eslOK> on success; the <assignments[0..nseq-1]> array contains |
| 182 | * set indices: |
| 183 | * 0 - vertex not in bipartite independent pair |
| 184 | * 1 - vertex is in set S of bipartite independent pair |
| 185 | * 2 - vertex is in set T of bipartite independent pair |
| 186 | * |
| 187 | * Throws: status codes from the caller's <(*linkfunc)> on failure; in this case, |
| 188 | * the contents of <*assignments> is undefined, and <*ret_C> is 0. |
| 189 | */ |
| 190 | int |
| 191 | esl_iset_biCobalt(ESL_RANDOMNESS *rng, const void *base, size_t n, size_t size, |
| 192 | int (*linkfunc)(const void *, const void *, const void *, int *), const void *param, |
| 193 | int *workspace, int *assignments) |
| 194 | { |
| 195 | int *a = NULL; // randomly shuffled array of indices of the input vertices |
| 196 | int *b1 = NULL; // array of vertex indices added to side 1 so far |
| 197 | int *b2 = NULL; // array of vertex indices added to side 2 so far |
| 198 | int nb1 = 0; // number added to b1 so far |
| 199 | int nb2 = 0; // number added to b2 so far |
| 200 | int v,i,j; |
| 201 | int do_link; |
| 202 | int adj1, adj2; |
| 203 | int status; |
| 204 | |
| 205 | a = workspace; |
| 206 | b1 = workspace + n; |
| 207 | b2 = workspace + 2*n; |
| 208 | |
| 209 | for (v = 0; v < n; v++) a[v] = n-v-1; // initialize by putting all vertices into an array |
| 210 | esl_vec_IShuffle(rng, a, n); // ... and shuffling them into random order |
| 211 | esl_vec_ISet(assignments, n, 0); |
| 212 | |
| 213 | for (j = 0; j <n; j++) |
| 214 | { |
| 215 | v = a[j]; // decide whether v goes in side1 or side2 or neither |
| 216 | if (esl_random(rng) <= 0.5) // with prob 0.5: |
| 217 | { |
| 218 | // first try to put v in b2. check if v is adjacent to any vertex in b1. |
| 219 | adj1 = FALSE; |
| 220 | for (i = n; i < n+nb1; i++) |
| 221 | { |
| 222 | if ((status = (*linkfunc)( (char *) base + v*size, (char *) base + a[i]*size, param, &do_link)) != eslOK) return status; |
| 223 | if (do_link) { adj1=TRUE; break; } |
| 224 | } |
| 225 | |
| 226 | if (adj1) // if exited loop early, v is adjacent to a vertex in b1, v will not go in b2; ok, so try putting v in b1 |
| 227 | { |
| 228 | adj2=FALSE; |
| 229 | for (i = 2*n; i < 2*n+nb2; i++) |
| 230 | { |
| 231 | if ((status = (*linkfunc)( (char *) base + v*size, (char *) base + a[i]*size, param, &do_link)) != eslOK) return status; |
| 232 | if (do_link) { adj2 = TRUE; break; } |
| 233 | } |
| 234 | |
| 235 | if (adj2) assignments[v]=0; // v is adjacent to something in b2, v can't go in b1 either |
| 236 | else { // else, ok to add v to b1 |
| 237 | assignments[v]=1; |
| 238 | b1[nb1]= v; |
| 239 | nb1++; |
| 240 | } |
| 241 | } |
| 242 | else // if ran through loop without exiting early, v is not adjacent to any vertex in b1, v can go in b2 |
| 243 | { |
| 244 | assignments[v] = 2; |
| 245 | b2[nb2]= v; |
| 246 | nb2++; |
| 247 | } |
| 248 | } |
| 249 | else // roll is > 0.5; do the same as above, but the other way |
| 250 | { |
| 251 | /* first try to put v in b1. check if v is adjacent to any vertex in b2*/ |
| 252 | adj2 = FALSE; |
| 253 | for (i = 2*n; i < 2*n+nb2; i++) |
| 254 | { |
| 255 | if ((status = (*linkfunc)( (char *) base + v*size, (char *) base + a[i]*size, param, &do_link)) != eslOK) return status; |
| 256 | if (do_link) { adj2 = TRUE; break; } |
| 257 | } |
| 258 | |
| 259 | if (adj2) // if exited loop early, v is adjacent to a vertex in b2, v will not go in b1; try putting v in b2 |
| 260 | { |
| 261 | adj1=FALSE; |
| 262 | for (i = n; i < n+nb1; i++) |
| 263 | { |
| 264 | if ((status = (*linkfunc)( (char *) base + v*size, (char *) base + a[i]*size, param, &do_link)) != eslOK) return status; |
| 265 | if (do_link) { adj1 = TRUE; break; } |
| 266 | } |
| 267 | |
| 268 | if (adj1) assignments[v] = 0; // v is adjacent to something in b1, v will not go in b2 |
| 269 | else { // else v is not adjacent to something in b1, v will go in b2 |
| 270 | assignments[v] = 2; |
| 271 | b2[nb2] = v; |
| 272 | nb2++; |
| 273 | } |
| 274 | } |
| 275 | else // if ran through loop without exiting early, v is not adjacent to any vertex in b2, v can go in b1 |
| 276 | { |
| 277 | assignments[v] = 1; |
| 278 | b1[nb1] = v; |
| 279 | nb1++; |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /* Define set 1 (S) as larger than set 2 (T); swap labels if needed */ |
| 285 | if (nb2 > nb1) |
| 286 | { |
| 287 | for (i = 0; i < n; i++) |
| 288 | if (assignments[i] == 1) assignments[i] = 2; |
| 289 | else if (assignments[i] == 2) assignments[i] = 1; |
| 290 | } |
| 291 | return eslOK; |
| 292 | } |
| 293 | |
| 294 | |
| 295 | /***************************************************************** |
| 296 | * 2. Blue splitting algorithms |
| 297 | *****************************************************************/ |
| 298 | |
| 299 | |
| 300 | /* used by <esl_iset_monoBlue()> to fill <to_add> */ |
| 301 | static int |
| 302 | i_select(const void *base, size_t n, size_t size, int k, |
| 303 | int (*linkfunc)(const void *, const void *, const void *, int *), const void *param, |
| 304 | int *dec_o, int *label_o, int *status_d, int *to_add, int *ret_lta) |
| 305 | { |
| 306 | int lta = 0; // length of to_add |
| 307 | int v, w; // vertices |
| 308 | int do_link; |
| 309 | int adj; // keeps track if adjacency is found |
| 310 | int found_self = FALSE; // keeps track of whether have found self in label_o |
| 311 | int w_there = FALSE; // keeps track of whether a vertex is still in graph |
| 312 | int i,j,l; // indices for for loops |
| 313 | int status; |
| 314 | |
| 315 | for (i = 0; i<k; i++) |
| 316 | { |
| 317 | v=dec_o[i]; // decide fate of this vertex v |
| 318 | |
| 319 | if (status_d[v] < 0) continue; // if vertex v has already been removed, nothing to decide so skip this iteration |
| 320 | |
| 321 | /* check if adjacent to any vertex in to_add*/ |
| 322 | adj=FALSE; |
| 323 | for (j = status_d[v]; j < lta; j++) |
| 324 | { |
| 325 | /* if v is adjacent to to_add[j], remove v from graph and break out of loop */ |
| 326 | if ((status = (*linkfunc)( (char *) base + v*size, (char *) base + to_add[j]*size, param, &do_link)) != eslOK) return status; |
| 327 | if (do_link) |
| 328 | { |
| 329 | status_d[v]= -3; |
| 330 | adj=TRUE; |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /* if not adjacent to any vertex in to_add, check if v is not adjacent to any vertex with a lower label*/ |
| 336 | if (!adj) |
| 337 | { // <adj> becomes true when v is determined to be adjacent to a vertex with a lower label that is still in the graph |
| 338 | status_d[v] = lta; // next time check adjacencies between v and to_add, start at index lta |
| 339 | found_self = FALSE; // becomes true when v is reached in label_o |
| 340 | |
| 341 | /* iterate through label_o until find v or find that v is adjacent to a vertex with a lower label that is still in graph*/ |
| 342 | j=0; |
| 343 | while (!found_self && !adj) |
| 344 | { |
| 345 | w=label_o[j]; // w is a vertex with a lower label than v's label |
| 346 | |
| 347 | /* check if w is v, if so break*/ |
| 348 | if (w == v) { found_self = TRUE; break; } |
| 349 | |
| 350 | if (status_d[w] >= 0) |
| 351 | { |
| 352 | /* check whether w and v are adjacent */ |
| 353 | if ((status = (*linkfunc)( (char *) base + v*size, (char *) base + w*size, param, &do_link)) != eslOK) return status; |
| 354 | if (do_link) // is adjacent; check whether w should really be in the graph |
| 355 | { |
| 356 | w_there=TRUE; |
| 357 | for (l = status_d[w]; l < lta; l++) |
| 358 | { |
| 359 | if ((status = (*linkfunc)( (char *) base + w*size, (char *) base + to_add[l]*size, param, &do_link)) != eslOK) return status; |
| 360 | if (do_link) |
| 361 | { // remove w from graph |
| 362 | status_d[w] = -3; |
| 363 | w_there = FALSE; |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | /* w is in the graph and so v does not get added to iset (since it is adjacent to w, which is a vertex in the graph with a lower label)*/ |
| 369 | if (w_there) |
| 370 | { |
| 371 | status_d[w] = lta; // next time check adjacencies between w and to_add, start at index lta |
| 372 | adj = TRUE; // v is adjacent to w, which has a lower label |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | j++; |
| 378 | } |
| 379 | |
| 380 | /* if v is not adjacent to any vertex with a lower label, v should be added to iset */ |
| 381 | if (found_self) |
| 382 | { |
| 383 | to_add[lta] = v; |
| 384 | lta++; |
| 385 | status_d[v] = -1; |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /* check if vertices are adjacent to vertices in to_add that were added after them, if so remove vertex*/ |
| 391 | for (i = 0; i < k; i++) |
| 392 | { |
| 393 | v=dec_o[i]; // vertex to check |
| 394 | |
| 395 | if (status_d[v] >= 0) |
| 396 | { |
| 397 | for (j = status_d[v]; j < lta; j++) |
| 398 | { |
| 399 | if ((status = (*linkfunc)( (char *) base + v*size, (char *) base + to_add[j]*size, param, &do_link)) != eslOK) return status; |
| 400 | if (do_link) { status_d[v]=-3; break; } /* remove v from graph*/ |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | *ret_lta = lta; |
| 406 | return eslOK; |
| 407 | } |
| 408 | |
| 409 | |
| 410 | /* used by <esl_iset_monoBlue()> to reset dec_o, label_o, and status_d */ |
| 411 | static void |
| 412 | i_update_workspace(int *dec_o, int *label_o, int *status_d, int *to_add, int *assignments, size_t n, int *k, int *lta, ESL_RANDOMNESS *r) |
| 413 | { |
| 414 | int d = 0; |
| 415 | int i; |
| 416 | |
| 417 | /* add all vertices in to_add to iset and clear to_add*/ |
| 418 | for (i = 0; i < *lta; i++) |
| 419 | { |
| 420 | assignments[to_add[i]] = 1; |
| 421 | to_add[i] = 0; |
| 422 | } |
| 423 | |
| 424 | /* clear decison order */ |
| 425 | for (i = 0; i < *k; i++) dec_o[i]=0; |
| 426 | |
| 427 | /* put all vertices left in graph (i.e. status order is >=0 and is in label order) into decision order */ |
| 428 | for (i=0; i<*k; i++) |
| 429 | { |
| 430 | if (status_d[label_o[i]] >= 0) |
| 431 | { |
| 432 | dec_o[d] = label_o[i]; |
| 433 | d++; |
| 434 | status_d[label_o[i]] = 0; |
| 435 | } |
| 436 | if (status_d[label_o[i]] == -3) assignments[label_o[i]] = 0; |
| 437 | label_o[i] = 0; |
| 438 | } |
| 439 | |
| 440 | /* copy decision order to label order */ |
| 441 | for (i = 0; i < d; i++) label_o[i] = dec_o[i]; |
| 442 | |
| 443 | /* shuffle label_o and dec_o */ |
| 444 | esl_vec_IShuffle(r, dec_o, d); |
| 445 | esl_vec_IShuffle(r, label_o, d); |
| 446 | |
| 447 | *k = d; |
| 448 | *lta = 0; |
| 449 | } |
| 450 | |
| 451 | /* used by esl_iset_biBlue() */ |
| 452 | static void |
| 453 | bi_update_workspace_blue(int *dec_o, int *label_o, int *status_d, int *to_add, int *elig, int *assignments, int n, int *d, int *l, int *lta1, int *lta2, int *nb1, int *nb2, ESL_RANDOMNESS *r) |
| 454 | { |
| 455 | int i; |
| 456 | |
| 457 | *d = 0; |
| 458 | *l = 0; |
| 459 | |
| 460 | /* add all vertices on left side of to_add to side 1 and clear to_add*/ |
| 461 | for (i = 0; i < *lta1; i++) |
| 462 | { |
| 463 | assignments[to_add[i]] = 1; |
| 464 | (*nb1)++; |
| 465 | to_add[i] = -1; |
| 466 | } |
| 467 | |
| 468 | /* add all vertices on right side of to_add to side 2 and clear to_add*/ |
| 469 | for (i = n-1; i >= n-*lta2; i--) |
| 470 | { |
| 471 | assignments[to_add[i]] = 2; |
| 472 | (*nb2)++; |
| 473 | to_add[i] = -1; |
| 474 | } |
| 475 | |
| 476 | for (i=0; i<n; i++) |
| 477 | { |
| 478 | if (elig[i] == 3) // randomly assign to a side |
| 479 | { |
| 480 | if (esl_random(r) < .5) // 50%: vertex is a 1-candidate, put into left side of order |
| 481 | { |
| 482 | dec_o[*d] = i; |
| 483 | (*d)++; |
| 484 | } |
| 485 | else // else 50%: vertex is a 2-candidate, put into right side of order |
| 486 | { |
| 487 | dec_o[n-1-*l] = i; |
| 488 | (*l)++; |
| 489 | status_d[i] = 0; |
| 490 | } |
| 491 | } |
| 492 | else if (elig[i] == 1) // vertex is a 1-candidate, put into left side of order |
| 493 | { |
| 494 | dec_o[*d]=i; |
| 495 | (*d)++; |
| 496 | } |
| 497 | else if (elig[i] == 2) // vertex is a 2-candidate, put into right side of order |
| 498 | { |
| 499 | dec_o[n-1-*l] = i; |
| 500 | (*l)++; |
| 501 | status_d[i] = 0; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | /* right side of dec_o is the label order*/ |
| 506 | label_o = dec_o+n-*l; |
| 507 | |
| 508 | /* place 1-side candidates in label order*/ |
| 509 | for (i = 0; i < *d; i++) |
| 510 | { |
| 511 | /* vertex v is before this position in label order */ |
| 512 | /* choose random value between 0 and l inclusive */ |
| 513 | status_d[dec_o[i]] = esl_rnd_Roll(r, (*l)+1); |
| 514 | } |
| 515 | |
| 516 | esl_vec_IShuffle(r, dec_o, *d); |
| 517 | esl_vec_IShuffle(r, label_o, *l); |
| 518 | |
| 519 | *lta1=0; |
| 520 | *lta2=0; |
| 521 | } |
| 522 | |
| 523 | |
| 524 | |
| 525 | /* used in esl_iset_biBlue() */ |
| 526 | static int |
| 527 | update_2_elig(int j, const void *base, int n, size_t size, |
| 528 | int (*linkfunc)(const void *, const void *, const void *, int *), const void *param,int *label_o, int *status_d, int *to_add, int *elig, int lta1) |
| 529 | { |
| 530 | int w,v,i; |
| 531 | int do_link; |
| 532 | int status; |
| 533 | |
| 534 | w = label_o[j]; |
| 535 | |
| 536 | /* if not 2-eligible, nothing to do */ |
| 537 | if (elig[w] == 2 || elig[w] == 3) |
| 538 | { |
| 539 | /* check 1- side of to_add for adjacencies with w */ |
| 540 | for (i = status_d[w]; i < lta1; i++) |
| 541 | { |
| 542 | v = to_add[i]; |
| 543 | /* if v has a higher label than j, v and u were already compared and determined to be non-adjacent before v was added to_add */ |
| 544 | if (status_d[v] <= j) |
| 545 | { |
| 546 | if ((status = (*linkfunc)( (char *) base + v*size, (char *) base + w*size, param, &do_link)) != eslOK) return status; |
| 547 | if (do_link) |
| 548 | { |
| 549 | elig[w] = elig[w]-2; |
| 550 | status_d[w] = lta1; |
| 551 | break; |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | return eslOK; |
| 557 | } |
| 558 | |
| 559 | static int |
| 560 | bi_select_blue(const void *base, int n, size_t size, |
| 561 | int (*linkfunc)(const void *, const void *, const void *, int *), const void *param, |
| 562 | int *dec_o, int *label_o, int *status_d, int *to_add, int *elig, int d, const int l, int *ret_lta1, int *ret_lta2) |
| 563 | { |
| 564 | int v, w, u; // vertices |
| 565 | int lta1=0, lta2=0; // length of to_add |
| 566 | int do_link; |
| 567 | int i,j,k; |
| 568 | int should_add; |
| 569 | int status; |
| 570 | |
| 571 | /* select 1-candidates for 1-side |
| 572 | * iterate over 1-candidates, all of which are in dec_o |
| 573 | */ |
| 574 | for (i = 0; i < d; i++) |
| 575 | { |
| 576 | v = dec_o[i]; // decide fate of this vertex v |
| 577 | |
| 578 | /* iterate over 2-candidate vertices that have a smaller label than v */ |
| 579 | should_add = TRUE; |
| 580 | for (j = 0; j < status_d[v]; j++) |
| 581 | { |
| 582 | update_2_elig(j, base, n, size, linkfunc, param, label_o, status_d, to_add, elig, lta1); /*update eligibility of w*/ |
| 583 | w = label_o[j]; |
| 584 | |
| 585 | /* if w is still 2-eligible and is adjacent to v, v should not be added*/ |
| 586 | if (elig[w] == 2 || elig[w] == 3) |
| 587 | { |
| 588 | if ((status = (*linkfunc)( (char *) base + v*size, (char *) base + w*size, param, &do_link)) != eslOK) return status; |
| 589 | if (do_link) |
| 590 | { |
| 591 | status_d[v] = j; // keep track that v got up to j in label order*/ |
| 592 | should_add = FALSE; |
| 593 | break; |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | if (should_add) |
| 599 | { |
| 600 | to_add[lta1] = v; |
| 601 | lta1++; |
| 602 | elig[v] = 0; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | /* select 2-candidates for 2-side |
| 607 | * iterate over 2-candidates, all of which are in label_o |
| 608 | */ |
| 609 | for (j = 0; j < l; j++) |
| 610 | { |
| 611 | update_2_elig(j, base, n, size, linkfunc, param, label_o, status_d, to_add, elig, lta1); |
| 612 | w = label_o[j]; // decide whether w goes into 2-side |
| 613 | if (elig[w] == 2 || elig[w] == 3) |
| 614 | { |
| 615 | /* add to 2-side */ |
| 616 | to_add[n-1-lta2] = w; |
| 617 | lta2++; |
| 618 | elig[w] = 0; |
| 619 | |
| 620 | /* remove 1-side candidates that are adjacent to w */ |
| 621 | for (i = 0; i < d; i++) |
| 622 | { |
| 623 | v = dec_o[i]; |
| 624 | if (elig[v] == 0) continue; |
| 625 | if (elig[v] == 1 || elig[v] == 3) |
| 626 | { |
| 627 | /* since v was not added, status_d[v] represents last position in label order that v was compared to and v is adjacent to vertex at that position */ |
| 628 | if (status_d[v] == j) elig[v] = elig[v]-1; /* v is adjacent to w, which was just added to 2-side, so remove 1-elig of v */ |
| 629 | /* v only checked adjacencies up to label_o[status_d[v]] and so v and w=label_o[j] have never been compared */ |
| 630 | else if (status_d[v] < j) |
| 631 | { |
| 632 | if ((status = (*linkfunc)( (char *) base + v*size, (char *) base + w*size, param, &do_link)) != eslOK) return status; |
| 633 | if (do_link) elig[v] = elig[v]-1; /* v is adjacent to w, which was just added to 2-side, so remove 1-elig of v */ |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | /* remove 1-elig of 2-candidates that are adjacent to a vertex in 2-side of to_add */ |
| 641 | for (j = 0; j < l; j++) |
| 642 | { |
| 643 | w = label_o[j]; |
| 644 | if (elig[w] == 1 || elig[w] == 3) |
| 645 | { |
| 646 | for (k = n-1; k > n-1-lta2; k--) |
| 647 | { |
| 648 | u = to_add[k]; |
| 649 | if ((status = (*linkfunc)( (char *) base + u*size, (char *) base + w*size, param, &do_link)) != eslOK) return status; |
| 650 | if (do_link) { elig[w]=elig[w]-1; break; } /* w is adjacent to u, which is in 2-side, so remove 1-elig of w */ |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | /* remove 2-elig of 1-candidates that are adjacent to a vertex in 1-side of to_add */ |
| 656 | for (i = 0; i < d; i++) |
| 657 | { |
| 658 | v = dec_o[i]; |
| 659 | if (elig[v] == 2 || elig[v] == 3) |
| 660 | { |
| 661 | for (k = 0; k < lta1; k++) |
| 662 | { |
| 663 | u =to_add[k]; |
| 664 | if ((status = (*linkfunc)( (char *) base + v*size, (char *) base + u*size, param, &do_link)) != eslOK) return status; |
| 665 | if (do_link) { elig[v] = elig[v]-2; break; } /* v is adjacent to u, which is in 1-side, so remove 2-elig of v */ |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | *ret_lta1=lta1; |
| 670 | *ret_lta2=lta2; |
| 671 | return eslOK; |
| 672 | } |
| 673 | |
| 674 | |
| 675 | |
| 676 | /* Function: esl_iset_monoBlue() |
| 677 | * Synopsis: Algorithm for independent set via a multi-round election process |
| 678 | * Incept: SNP, 16 Oct 2020 |
| 679 | * |
| 680 | * Purpose: Produces an independent set. |
| 681 | * |
| 682 | * U= empty set |
| 683 | * L= all vertices |
| 684 | * while L is non-empty: |
| 685 | * Place vertices of L in a random order v_1,...v_k |
| 686 | * Assign each vertex in L a value ~ unif[0,1] |
| 687 | * for i=1 to k: |
| 688 | * if label of v_i < label of w for all neighbors w of v_i in L: |
| 689 | * Add v_i to U |
| 690 | * Remove all neighbors of v_i from L |
| 691 | * return U |
| 692 | * |
| 693 | * Two vertices are adjacent if their corresponding sequences are >t% identical |
| 694 | * |
| 695 | * Notes: Pseudocode above is given for intuition. Here we implement the following pseudocode |
| 696 | * which produces the same result as the above pseudocode with fewer edge queries. |
| 697 | * |
| 698 | * U= empty set |
| 699 | * status_d = dictionary with keys=vertices; all values initially 0 |
| 700 | * // keeps track of current status: status_d[i] = -1 if i in iset, -3 if i removed from graph, |
| 701 | * // >=0 if i still eligible, value is next position of to_add that needs to be checked |
| 702 | * k= number of vertices still in graph // will represent number of eligible vertices (i.e. the number |
| 703 | * // of vertices for which status_d[v] is non-negative) |
| 704 | * |
| 705 | * while k>0: |
| 706 | * dec_o= array with eligible vertices placed in a random order // order in which we will make |
| 707 | * // decisions about vertices |
| 708 | * label_o= array with eligible vertices placed in a random order // instead of labeling the vertices with |
| 709 | * // random values, place them in a random |
| 710 | * // order representing lowest to highest label |
| 711 | * to_add = empty array // array of vertices to be added to iset in this round |
| 712 | * lta= 0 //length of to_add array |
| 713 | * |
| 714 | * Iterate through the vertices v according to dec_o: |
| 715 | * if status_d[v] <0, continue // vertex already removed, nothing to do here |
| 716 | * |
| 717 | * //first check if v is adjacent to a vertex in to_add |
| 718 | * for i=status_d[v] to lta: |
| 719 | * if to_add[i] is adjacent to v: |
| 720 | * status_d[v]=-3 // remove v's eligibility since v is adjacent to vertex in iset |
| 721 | * break |
| 722 | * |
| 723 | * status_d[v]=lta // next time checking for adjacencies between v and vertices in to_add, start at lta |
| 724 | * |
| 725 | * //now check if v is adjacent to a vertex with a lower label |
| 726 | * found_self=False //will become true after v is reached during iteration through label_o |
| 727 | * adj=False //will become true if v is adjcent to a vertex in label_o that is eligible |
| 728 | * j=0// current vertex in label order being evaluated |
| 729 | * |
| 730 | * while !found_self && !adj: |
| 731 | * w=label_o[j] |
| 732 | * if w==v, found_self=TRUE break |
| 733 | * if status_d[w]>=0: //nothing to do in other cases; if status_d[w], w no longer eligible |
| 734 | * //we already know that v is not adjacent to any vertex in to_add |
| 735 | * if w and v are adjacent: |
| 736 | * // need to check if w is actually eligible (or whether w is ineligible because an adjacency in to_add) |
| 737 | * w_there=True |
| 738 | * for l=status_d[w] to lta: |
| 739 | * if w and to_add[l]: |
| 740 | * status_d[w]=-3 // w is not eligible |
| 741 | * w_there=False |
| 742 | * break |
| 743 | * if w_there==True: |
| 744 | * status_d[w]=lta // next time checking for adjacencies between w and vertices |
| 745 | * // in to_add, start at lta |
| 746 | * adj=True // v is adjacent to a vertex (w) with a lower label |
| 747 | * break |
| 748 | * j++ |
| 749 | * |
| 750 | * if found_self==True: // v is not adjacent to a vertex with a lower label, can add v to iset! |
| 751 | * to_add[lta]=v |
| 752 | * lta++ |
| 753 | * status_d[v]=-1 |
| 754 | * |
| 755 | * // Remove eligibility of vertices adjacent to vertices in to_add (This is necesary when a vertex y |
| 756 | * // in to_add is adjacent to an eligible vertex x whose fate was decided before y) |
| 757 | * for i=0 to k: |
| 758 | * v=dec_o[i] //check whether v is adjacent to any vertex in to_add |
| 759 | * for j=status_d[v] to lta: |
| 760 | * if to_add[j] and v are adjacent: |
| 761 | * status_d[v]=-3 //remove eligibility of v |
| 762 | * |
| 763 | * Add all vertices in to_add to U |
| 764 | * Reset status_d[v]=0 for all eligible vertices (i.e. vertices with status_d[v]>=0) |
| 765 | * k=number of eligible vertices remaining |
| 766 | * Clear dec_o, label_o |
| 767 | * |
| 768 | * return U |
| 769 | * |
| 770 | * |
| 771 | * Args: rng - source of randomness |
| 772 | * base - pointer to array of n fixed-size vertices to be clustered. |
| 773 | * n - number of vertices |
| 774 | * size - size of each vertex element |
| 775 | * linkfunc - pointer to caller's function for defining linked pairs |
| 776 | * param - pointer to any data that needs to be provided to <(*linkfunc)> |
| 777 | * workspace - buffer for intermediate calculations. Must be at least 4 * sizeof(int) * n bytes. |
| 778 | * assignments - RETURN: assignments to sets (caller provides n*sizeof(int) space) |
| 779 | |
| 780 | * |
| 781 | * Returns: <eslOK> on success; the <assignments[0..nseq-1]> array contains |
| 782 | * set indices: |
| 783 | * 0 - vertex not in independent set |
| 784 | * 1 - vertex is in independent set |
| 785 | * |
| 786 | * Throws: status codes from the caller's <(*linkfunc)> on failure; in this case, |
| 787 | * the contents of <*assignments> is undefined, and <*ret_C> is 0. |
| 788 | */ |
| 789 | int |
| 790 | esl_iset_monoBlue(ESL_RANDOMNESS *rng, const void *base, size_t n, size_t size, |
| 791 | int (*linkfunc)(const void *, const void *, const void *, int *), const void *param, |
| 792 | int *workspace, int *assignments) |
| 793 | { |
| 794 | int *dec_o = NULL; // vertices in order in which decisions about them will be made |
| 795 | int *label_o = NULL; // vertices in order of smallest to largest label |
| 796 | int *status_d = NULL; // keeps track of current status of each vertex like a dictionary; status_d[i] is the status of vertex i |
| 797 | int k = n; |
| 798 | int lta = 0; // length of to_add |
| 799 | int i; |
| 800 | /* -1 in iset, -3 removed from graph, >=0 still in graph- value is (most recent index of to_add checked against) + 1 */ |
| 801 | int *to_add = NULL; // vertices to add to independent set |
| 802 | int status; |
| 803 | |
| 804 | dec_o = workspace; |
| 805 | label_o = workspace + n; |
| 806 | status_d = workspace +2*n; |
| 807 | to_add = workspace +3*n; |
| 808 | |
| 809 | for (i = 0; i < n; i++) |
| 810 | { |
| 811 | dec_o[i] = i; |
| 812 | label_o[i] = i; |
| 813 | status_d[i] = 0; |
| 814 | assignments[i] = 0; |
| 815 | } |
| 816 | |
| 817 | while (k > 0) |
| 818 | { |
| 819 | if ((status = i_select(base, n, size, k, linkfunc, param, dec_o, label_o ,status_d, to_add, <a))!= eslOK) return status; |
| 820 | i_update_workspace(dec_o, label_o ,status_d, to_add, assignments, n, &k, <a, rng); |
| 821 | } |
| 822 | return eslOK; |
| 823 | } |
| 824 | |
| 825 | |
| 826 | /* Function: esl_iset_biBlue() |
| 827 | * Synopsis: Algorithm for bipartite independent pair via a multi-round election process |
| 828 | * Incept: SNP, 16 Oct 2020 |
| 829 | * |
| 830 | * Purpose: Produces an bipartite independent pair. |
| 831 | * |
| 832 | * S,T= empty set |
| 833 | * L_S, L_T= all vertices // represent eligibility for S and T sets |
| 834 | * while L_T or L_S is non-empty: |
| 835 | * |
| 836 | * C_S, C_T = empty set // represents S-candidates and T-candidates for the round |
| 837 | * For each vertex that is in L_S, but not in L_T, add the vertex to C_S |
| 838 | * For each vertex that is in L_T, but not in L_S, add the vertex to C_T |
| 839 | * For each vertex in both L_S and L_T, randomly assign to C_S (exclusive) or C_T |
| 840 | * |
| 841 | * |
| 842 | * Place vertices of C_S in a random order v_1,...v_k |
| 843 | * Assign each vertex in L a value ~ unif[0,1] |
| 844 | * For i=1 to k: |
| 845 | * If label of v_i < label of w for all neighbors w of v_i in both L_T and C_T: |
| 846 | * Add v_i to S |
| 847 | * Remove all neighbors of v_i from L_T |
| 848 | * Remove v_i from L_T and L_S |
| 849 | * |
| 850 | * For all vertices w in both L_T and C_T: |
| 851 | * Add w to T, remove w from L_T, remove w and all of its neighbors from L_S |
| 852 | * |
| 853 | * Return S, T |
| 854 | * |
| 855 | * Two vertices are adjacent if their corresponding sequences are >t% identical |
| 856 | * |
| 857 | * Notes: Pseudocode above is given for intuition. Here we implement the following pseudocode |
| 858 | * which produces the same result as the above pseudocode with fewer edge queries. |
| 859 | * |
| 860 | * S,T=empty set |
| 861 | * |
| 862 | * elig= dictionary with key=vertices; all values initially 3 // keeps track of eligibility of the vertices |
| 863 | * // 0 removed from graph (in one side of iset or disqualified because no longer eligibile for either side) |
| 864 | * // 1 eligibile for 1 only, 2 eligible for 2 only, 3 eligible for both 1 and 2 |
| 865 | * |
| 866 | * while some vertices are still eligible: |
| 867 | d=0 // number of 1-side candidates |
| 868 | * l=0 // number of 2-side candidate |
| 869 | * dec_o= empty array of length number of total vertices // will store 1-candidates on left side, and 2-candidates on right side |
| 870 | * // assign candidacy to vertices |
| 871 | * for each vertex: |
| 872 | * if v is 1-eligible and not 2-eligible: |
| 873 | * // make v 1-candidate by placing v in next open spot of dec_o on left side |
| 874 | * dec_o[d]=v |
| 875 | * d++ |
| 876 | * if v is 2-eligible and not 1-eligible: |
| 877 | * // make v a 2-candidate by placing v in next open spot of dec_o on right side |
| 878 | * dec_o=[n-1-l]=v |
| 879 | * l++ |
| 880 | * if v is 1-eligible and 2-eligible: |
| 881 | * with prob 1/2 make v 1-candidate by placing v in the next open spot of dec_o on left side (dec_o[d]=v, d++) |
| 882 | * alternately make v a 2-candidate by placing v in the next open spot of dec_o on right side (dec_o=[n-1-l]=v, l++) |
| 883 | * |
| 884 | * label_o= dec_o+n-l // to avoid indexing from the right, we break off the dec_o array into an array called label_o |
| 885 | * status_d=dictionary with keys=vertices; |
| 886 | * //if v is a 1-candidate, v is before position status_d[i] in label order (there is never a need to compare |
| 887 | * //the labels of two 1-candidates, so it is fine if multiple 1-candidates are in the same position of the label order) |
| 888 | * //if v is a 2-candidate, value of status_d[v] is next position of to_add that needs to be checked |
| 889 | * for all 2-candidates, initialize status_d[v]=0 |
| 890 | * for all 1-candidates, status_d[v]= random integer in [0, l+1] |
| 891 | * |
| 892 | * shuffle 1-candidates within dec_0 (i.e. apply random permutation to first d elements of dec_o) |
| 893 | * shuffle 2-candidates within label_0 (i.e. apply random permutation to the l elements of label_o) |
| 894 | * |
| 895 | * // elect vertices to 1-side |
| 896 | * lta1=0, lta2=0 // length of to_add for each of the sides |
| 897 | * to_add=empty array // left side will store vertices added to 1-side, right side will store vertices added to 2-side |
| 898 | * Iterate through the vertices v according to dec_o (only the 1-candidates): |
| 899 | * should_add=True |
| 900 | * for j=0 to status_d[v]: //check if v is adjacent to a vertex that is a 2-candidate with a lower label |
| 901 | * w=label_o[j] |
| 902 | * update_2_elig(j) // see helper function below, updates 2-elig of label_o[j] |
| 903 | * if elig[w]==2 or 3 // then w is 2-eligible |
| 904 | * if w and v are adjacent: |
| 905 | * status_d[v]=j // keep track that v has been compared to first j vertices in label order (new meaning) |
| 906 | * should_add=False |
| 907 | * break |
| 908 | * if should_add: |
| 909 | * to_add[lta1]=v; lta++; elig[v]=0 |
| 910 | * |
| 911 | * // add vertices to 2-side |
| 912 | * for j=0 to l: |
| 913 | * w=label_o[j] // 2-candidate vertex to decide |
| 914 | * update_2_elig(j) // see helper function below, updates 2-elig of label_o[j] |
| 915 | * |
| 916 | * if elig[w]==2 or 3 // then w is 2-eligible so add w to 2-side |
| 917 | * to_add[n-1-lta2]=w; lta2++; elig[w]=0 |
| 918 | * |
| 919 | * //remove eligibility of 1-side candidates that are adjacent to w |
| 920 | * for i=0 to d: |
| 921 | * v=dec_o[d] // decide 2-eligibility of v |
| 922 | * if elig[v]==1 or 3: |
| 923 | * // since v was not added to 1-side, status_d[v] has new meaning, v is not adjacent to first j-1 vertices |
| 924 | * // of label_o and is adjacent to label_o[j] |
| 925 | * if status_d[v]==j, elig[v]=elig[v]-1 // w is adj to v, w is on 2-side, so must remove 1-eligibility of v |
| 926 | * else if status_d[v]< j: |
| 927 | * if v and w are adjacent, elig[v]=elig[v]-1 // w is adj to v, w is on 2-side, so must remove 1-eligibility of v |
| 928 | * |
| 929 | * // remove 1-elig of 2-candidates that are adjacent ot a vertex in 2-side of to-add |
| 930 | * for j=0 to l: |
| 931 | * w=label_o[j] |
| 932 | * if elig[w]==1 or 3: |
| 933 | * for k=n-1 to n-1-lta2: //iterate through vertices added to 2-side |
| 934 | * u=to_add[k] |
| 935 | * if u and w are adjacent, elig[w]=elig[w]-1, break // w is adajcent to u (which is in 2-side), so w is no longer 1-eligible |
| 936 | * |
| 937 | * // remove 2-elig of 1-candidates that are adjacent to a vertex in 1-side of to_add |
| 938 | * for i=0 to d: |
| 939 | * v=dec_o[i] |
| 940 | * if elig[v]==2 or 3: //iterate through vertices added to 1-side |
| 941 | * for k=0 to lta1: |
| 942 | * u=to_add[k] |
| 943 | * if u and v are adjacent, elig[v]=elig[v]-2 // v is adjacent to u (which is in 1-side) so must remove 2-elig of v |
| 944 | * |
| 945 | * add vertices on left side of to_add (positions 0 to lta1) to S |
| 946 | * add vertices on right side of to add (position n-lta2 to n-1) to T |
| 947 | * |
| 948 | * return S,T |
| 949 | * |
| 950 | * PSEUDOCODE for update_2_elig |
| 951 | * |
| 952 | * update_2_elig(j): |
| 953 | * w=label_o[j] |
| 954 | * if elig[w]==2 or 3: |
| 955 | * //check 1-side of to_add for adjacencies with w |
| 956 | * for i=status_d[w] to lta: |
| 957 | * v=to_add[i] |
| 958 | * // if v has a higher label that j, v and u were already compared and determined to be non-adjacent before v was added |
| 959 | * if status_d[v]<=j: |
| 960 | * if v and w are adjacent, elig[w]=elig[w]-2, status_d[w]=lta1, break // w is not 2 eligible because it is adjacent |
| 961 | * // to v, which is in 1-side of to_add |
| 962 | * |
| 963 | * |
| 964 | * Args: rng - source of randomness |
| 965 | * base - pointer to array of n fixed-size vertices to be clustered. |
| 966 | * n - number of vertices |
| 967 | * size - size of each vertex element |
| 968 | * linkfunc - pointer to caller's function for defining linked pairs |
| 969 | * param - pointer to any data that needs to be provided to <(*linkfunc)> |
| 970 | * workspace - buffer for intermediate calculations. Must be at least 4 * sizeof(int) * n bytes. |
| 971 | * assignments - RETURN: assignments to sets (caller provides n*sizeof(int) space) |
| 972 | * |
| 973 | * Returns: <eslOK> on success; the <assignments[0..nseq-1]> array contains |
| 974 | * set indices: |
| 975 | * 0 - vertex not in bipartite independent pair |
| 976 | * 1 - vertex is in set S of bipartite independent pair |
| 977 | * 2 - vertex is in set T of bipartite independent pair |
| 978 | * |
| 979 | * Throws: status codes from the caller's <(*linkfunc)> on failure; in this case, |
| 980 | * the contents of <*assignments> is undefined, and <*ret_C> is 0. |
| 981 | */ |
| 982 | int |
| 983 | esl_iset_biBlue(ESL_RANDOMNESS *rng, const void *base, size_t n, size_t size, |
| 984 | int (*linkfunc)(const void *, const void *, const void *, int *), const void *param, |
| 985 | int *workspace, int *assignments) |
| 986 | { |
| 987 | int nb1 = 0; // number of vertices selected for 1-side |
| 988 | int nb2 = 0; // number of vertices selected for 2-side |
| 989 | int d = 0; // number of 1-side candidates |
| 990 | int l = 0; // number of 2-side candidate |
| 991 | int lta1 = 0; // length of to_add |
| 992 | int lta2 = 0; |
| 993 | int *dec_o = NULL; // vertices in order in which decisions about them will be made |
| 994 | int *label_o = NULL; // vertices in order of smallest to largest label |
| 995 | int *status_d = NULL; // keeps track of current status of each vertex like a dictionary; status_d[i] is the status of vertex i |
| 996 | /* if v is a 1-candidate v is before status_d[i] in label order; if v is a 2-candidate status_d keeps track of most recent member of to_add compared to + 1 */ |
| 997 | int *to_add = NULL; // vertices to add to independent set |
| 998 | int *elig = NULL; // dictionary to keep track of eligibility of the vertices |
| 999 | /* 0 removed from graph (in one side of iset or disqualified because no longer eligibile for either side), 1 eligibile for 1 only, 2 eligible for 2 only, 3 eligible for both 1 and 2 */ |
| 1000 | int i; |
| 1001 | int status; |
| 1002 | |
| 1003 | label_o = workspace; // label_o and dec_o deliberately point to the same place |
| 1004 | dec_o = workspace; // (SRE checked this with SNP; May 2022) |
| 1005 | status_d = workspace +n; |
| 1006 | to_add = workspace +2*n; |
| 1007 | elig = workspace +3*n; |
| 1008 | |
| 1009 | /* initialize assignments to avoid funny business; assignments should not have 0 or 1 */ |
| 1010 | /* initialize to_add */ |
| 1011 | for (i = 0; i < n; i++) |
| 1012 | { |
| 1013 | assignments[i] = 0; |
| 1014 | to_add[i] = -1; // should never try to add vertex -1 |
| 1015 | } |
| 1016 | |
| 1017 | /* all vertices initially eligible for both sides */ |
| 1018 | for (i = 0; i < n; i++) elig[i]=3; |
| 1019 | |
| 1020 | bi_update_workspace_blue(dec_o, label_o, status_d, to_add, elig, assignments, (int) n, &d, &l, <a1, <a2, &nb1, &nb2, rng); |
| 1021 | label_o = dec_o+n-l; |
| 1022 | |
| 1023 | while (l+d>0) |
| 1024 | { |
| 1025 | if ((status=bi_select_blue(base, (int) n, size, linkfunc, param, dec_o, label_o ,status_d, to_add, elig, d, l, <a1, <a2)) != eslOK) return status; |
| 1026 | bi_update_workspace_blue(dec_o, label_o, status_d, to_add, elig, assignments, (int) n, &d, &l, <a1,<a2, &nb1, &nb2, rng); |
| 1027 | label_o = dec_o+n-l; |
| 1028 | } |
| 1029 | |
| 1030 | /* Define set 1 (S) as larger than set 2 (T); swap labels if needed */ |
| 1031 | if (nb2 > nb1) |
| 1032 | { |
| 1033 | for (i = 0; i < n; i++) |
| 1034 | if (assignments[i] == 1) assignments[i] = 2; |
| 1035 | else if (assignments[i] == 2) assignments[i] = 1; |
| 1036 | } |
| 1037 | return eslOK; |
| 1038 | } |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | /***************************************************************** |
| 1044 | * 3. Random splitting algorithm |
| 1045 | *****************************************************************/ |
| 1046 | |
| 1047 | /* Function: esl_iset_biRandom() |
| 1048 | * Synopsis: Random bipartite independent pair algorithm |
| 1049 | * Incept: SNP, 16 Oct 2020 |
| 1050 | * |
| 1051 | * Purpose: Produces a bipartite independent pair by randomly selecting |
| 1052 | * vertices for one group and placing all eligible vertices |
| 1053 | * in the other group. |
| 1054 | * |
| 1055 | * For each vertex v: |
| 1056 | * With probability t_prob, place vertex v in set 1 |
| 1057 | * For each vertex w not in set 1: |
| 1058 | * If w is not adjacent to any vertex in set 1, place w in set 2 |
| 1059 | * |
| 1060 | * Two vertices are adjacent if their corresponding sequences are >t% identical |
| 1061 | * |
| 1062 | * Args: rng - source of randomness |
| 1063 | * t_prob - probability of set 1 |
| 1064 | * base - pointer to array of n fixed-size vertices to be clustered. |
| 1065 | * n - number of vertices |
| 1066 | * size - size of each vertex element |
| 1067 | * linkfunc - pointer to caller's function for defining linked pairs |
| 1068 | * param - pointer to any data that needs to be provided to <(*linkfunc)> |
| 1069 | * assignments - RETURN: assignments to sets (caller provides n*sizeof(int) space) |
| 1070 | * |
| 1071 | * Returns: <eslOK> on success; the <assignments[0..nseq-1]> array contains |
| 1072 | * set indices: |
| 1073 | * 0 - vertex not in bipartite independent pair |
| 1074 | * 1 - vertex in one set of bipartite independent pair |
| 1075 | * 2 - vertex in other set of bipartite independent pair |
| 1076 | * |
| 1077 | * Throws: status codes from the caller's <(*linkfunc)> on failure; in this case, |
| 1078 | * the contents of <*assignments> is undefined, and <*ret_C> is 0. |
| 1079 | */ |
| 1080 | int |
| 1081 | esl_iset_biRandom(ESL_RANDOMNESS *rng, double t_prob, const void *base, size_t n, size_t size, |
| 1082 | int (*linkfunc)(const void *, const void *, const void *, int *), const void *param, |
| 1083 | int *assignments) |
| 1084 | { |
| 1085 | int i,j; |
| 1086 | int do_link; |
| 1087 | int status; |
| 1088 | |
| 1089 | for (i = 0; i < n; i++) |
| 1090 | { |
| 1091 | if (esl_random(rng) < t_prob) assignments[i] = 1; |
| 1092 | else assignments[i] = 2; |
| 1093 | } |
| 1094 | |
| 1095 | for (i = 0; i < n; i++) |
| 1096 | { |
| 1097 | if (assignments[i] == 2) |
| 1098 | { |
| 1099 | /* check if adjacent to anyone on side 1*/ |
| 1100 | for (j = 0; j < n; j++) |
| 1101 | { |
| 1102 | if (assignments[j] == 1) |
| 1103 | { |
| 1104 | if ((status = (*linkfunc)( (char *) base + j*size, (char *) base + i*size, param, &do_link)) != eslOK) return status; |
| 1105 | if (do_link) { assignments[i]=0; break; } |
| 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | return eslOK; |
| 1111 | } |
| 1112 | |
| 1113 | |
| 1114 | /***************************************************************** |
| 1115 | * 4. Counting, evaluation of split assignments |
| 1116 | *****************************************************************/ |
| 1117 | |
| 1118 | int |
| 1119 | esl_iset_biCount(int *assignment, int n, int *ret_nS, int *ret_nT, int *ret_nX) |
| 1120 | { |
| 1121 | int nS,nT,nX = 0; |
| 1122 | int i; |
| 1123 | |
| 1124 | nS = nT = nX = 0; |
| 1125 | for (i = 0; i < n; i++) |
| 1126 | { |
| 1127 | if (assignment[i] == 0) nX++; |
| 1128 | else if (assignment[i] == 1) nS++; |
| 1129 | else if (assignment[i] == 2) nT++; |
| 1130 | } |
| 1131 | ESL_DASSERT1(( nS >= nT )); |
| 1132 | *ret_nS = nS; |
| 1133 | *ret_nT = nT; |
| 1134 | *ret_nX = nX; |
| 1135 | return eslOK; |
| 1136 | } |
| 1137 | |
| 1138 | |
| 1139 | |
| 1140 | /***************************************************************** |
| 1141 | * 4. Debugging and development routines |
| 1142 | *****************************************************************/ |
| 1143 | |
| 1144 | /* Function: esl_iset_monoValidate() |
| 1145 | * Synopsis: Verify that a subset of vertices is an independent set |
| 1146 | * Incept: SNP, Oct 16 2020 |
| 1147 | * |
| 1148 | * Purpose: Given a subset of vertices, verify whether they form an |
| 1149 | * independent set |
| 1150 | * |
| 1151 | * Args: base - pointer to array of n fixed-size vertices in graph |
| 1152 | * n - number of vertices |
| 1153 | * size - size of each vertex element |
| 1154 | * linkfunc - pointer to caller's function for defining linked pairs (edges) |
| 1155 | * param - pointer to any data that needs to be provided to <(*linkfunc)> |
| 1156 | * assignments - array of 0/1s; 1 indicates a vertex is in the subset, 0 indicates |
| 1157 | * vertex not in the subset |
| 1158 | * |
| 1159 | * Returns: <eslOK> if the subset is an independent set; |
| 1160 | * <eslFAIL> if not |
| 1161 | |
| 1162 | * Throws: other errors if <linkfunc()> fails |
| 1163 | */ |
| 1164 | int |
| 1165 | esl_iset_monoValidate(const void *base, size_t n, size_t size, |
| 1166 | int (*linkfunc)(const void *, const void *, const void *, int *), const void *param, |
| 1167 | int *assignments) |
| 1168 | { |
| 1169 | int i,j; |
| 1170 | int do_link; |
| 1171 | int status; |
| 1172 | |
| 1173 | for (i = 0; i < n; i++) |
| 1174 | { |
| 1175 | for (j = i+1; j < n; j++) |
| 1176 | { |
| 1177 | if (assignments[i] == 1 && assignments[j] == 1) |
| 1178 | { |
| 1179 | if ((status = (*linkfunc)( (char *) base + j*size, (char *) base + i*size, param, &do_link)) != eslOK) return status; |
| 1180 | if (do_link) return eslFAIL; |
| 1181 | } |
| 1182 | } |
| 1183 | } |
| 1184 | return eslOK; |
| 1185 | } |
| 1186 | |
| 1187 | /* Function: esl_iset_biValidate() |
| 1188 | * Synopsis: Verify that a pair of subsets of vertices form a bipartite independent pair |
| 1189 | * Incept: SNP, Oct 16 2020 |
| 1190 | * |
| 1191 | * Purpose: Given a pair of disjoint subsets of vertices, verify that the pair is a |
| 1192 | * bipartite independent pair, and that |S| >= |T|. |
| 1193 | * |
| 1194 | * Args: base - pointer to array of n fixed-size vertices in graph |
| 1195 | * n - number of vertices |
| 1196 | * size - size of each vertex element |
| 1197 | * linkfunc - pointer to caller's function for defining linked pairs (edges) |
| 1198 | * param - pointer to any data that needs to be provided to <(*linkfunc)> |
| 1199 | * assignments - array of 0/1/2s; 1 indicates the vertex is in one subset, 2 indicates a |
| 1200 | * the vertex in in the other subset, 0 indicates the vertex is in neither |
| 1201 | * |
| 1202 | * Returns: <eslOK> if the pair forms a bipartite independent pair; |
| 1203 | * <eslFAIL> if not |
| 1204 | * |
| 1205 | * Throws: other errors if the <linkfunc> fails |
| 1206 | */ |
| 1207 | int |
| 1208 | esl_iset_biValidate(const void *base, size_t n, size_t size, |
| 1209 | int (*linkfunc)(const void *, const void *, const void *, int *), const void *param, |
| 1210 | int *assignments) |
| 1211 | { |
| 1212 | int i,j; |
| 1213 | int do_link; |
| 1214 | int nS = 0; |
| 1215 | int nT = 0; |
| 1216 | int status; |
| 1217 | |
| 1218 | for (i = 0; i < n; i++) |
| 1219 | { |
| 1220 | for (j = i+1; j < n; j++) |
| 1221 | { |
| 1222 | if ( (assignments[i] == 1 && assignments[j] == 2) || (assignments[i] == 2 && assignments[j] == 1) ) |
| 1223 | { |
| 1224 | if ((status = (*linkfunc)( (char *) base + j*size, (char *) base + i*size, param, &do_link)) != eslOK) return status; |
| 1225 | if (do_link) return eslFAIL; |
| 1226 | } |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | for (i = 0; i < n; i++) |
| 1231 | if (assignments[i] == 1) nS++; |
| 1232 | else if (assignments[i] == 2) nT++; |
| 1233 | else if (assignments[i] != 0) return eslFAIL; // assignments[i] can also be 0, meaning "unassigned" |
| 1234 | if (nT > nS) return eslFAIL; |
| 1235 | |
| 1236 | return eslOK; |
| 1237 | } |
| 1238 | |
| 1239 | |
| 1240 | /***************************************************************** |
| 1241 | * 5. Unit tests |
| 1242 | *****************************************************************/ |
| 1243 | #ifdef eslISET_TESTDRIVE |
| 1244 | |
| 1245 | #include "esl_alphabet.h" |
| 1246 | #include "esl_distance.h" |
| 1247 | #include "esl_msa.h" |
| 1248 | #include "esl_msafile.h" |
| 1249 | |
| 1250 | /* In digital mode, we'll need to pass the clustering routine two parameters - |
| 1251 | * %id threshold and alphabet ptr - so make a structure that bundles them. |
| 1252 | */ |
| 1253 | struct msa_param_s { |
| 1254 | double maxid; |
| 1255 | ESL_ALPHABET *abc; |
| 1256 | }; |
| 1257 | |
| 1258 | static int |
| 1259 | test_linkage(const void *v1, const void *v2, const void *p, int *ret_link) |
| 1260 | { |
| 1261 | ESL_DSQ *ax1 = *(ESL_DSQ **) v1; |
| 1262 | ESL_DSQ *ax2 = *(ESL_DSQ **) v2; |
| 1263 | struct msa_param_s *param = (struct msa_param_s *) p; |
| 1264 | double pid; |
| 1265 | int status = eslOK; |
| 1266 | |
| 1267 | if ( (status = esl_dst_XPairId(param->abc, ax1, ax2, &pid, NULL, NULL)) != eslOK) return status; |
| 1268 | |
| 1269 | *ret_link = (pid >= param->maxid ? TRUE : FALSE); |
| 1270 | return status; |
| 1271 | } |
| 1272 | |
| 1273 | static void |
| 1274 | utest_basic(ESL_RANDOMNESS *rng) |
| 1275 | { |
| 1276 | char msg[] = "iset utest_basic failed"; |
| 1277 | ESL_ALPHABET *abc = esl_alphabet_Create(eslAMINO); |
| 1278 | int *assignments = NULL; |
| 1279 | int *workspace = NULL; |
| 1280 | float maxid = 0.5; |
| 1281 | int status; |
| 1282 | struct msa_param_s param; |
| 1283 | ESL_MSA *msa = esl_msa_CreateFromString("\ |
| 1284 | # STOCKHOLM 1.0\n\ |
| 1285 | \n\ |
| 1286 | seq0 AAAAAAAAAA\n\ |
| 1287 | seq1 AAAAAAAAAA\n\ |
| 1288 | seq2 AAAAAAAAAC\n\ |
| 1289 | seq3 AAAAAAAADD\n\ |
| 1290 | seq4 AAAAAAAEEE\n\ |
| 1291 | seq5 AAAAAAFFFF\n\ |
| 1292 | seq6 AAAAAGGGGG\n\ |
| 1293 | seq7 AAAAHHHHHH\n\ |
| 1294 | seq8 AAAIIIIIII\n\ |
| 1295 | seq9 AAKKKKKKKK\n\ |
| 1296 | seq10 ALLLLLLLLL\n\ |
| 1297 | seq11 MMMMMMMMMM\n\ |
| 1298 | //", eslMSAFILE_STOCKHOLM); |
| 1299 | |
| 1300 | esl_msa_Digitize(abc, msa, NULL); |
| 1301 | ESL_ALLOC(workspace, 4 * msa->nseq * sizeof(int)); // allocate to the largest workspace required by any test |
| 1302 | ESL_ALLOC(assignments, msa->nseq * sizeof(int)); // must be = # of sequences in alignment |
| 1303 | |
| 1304 | param.maxid = maxid; |
| 1305 | param.abc = abc; |
| 1306 | |
| 1307 | if (esl_iset_monoCobalt (rng, msa->ax, msa->nseq, sizeof(ESL_DSQ *), test_linkage, ¶m, workspace, assignments) != eslOK) esl_fatal(msg); |
| 1308 | if (esl_iset_monoValidate( msa->ax, msa->nseq, sizeof(ESL_DSQ *), test_linkage, ¶m, assignments) != eslOK) esl_fatal(msg); |
| 1309 | |
| 1310 | if (esl_iset_monoBlue (rng, msa->ax, msa->nseq, sizeof(ESL_DSQ *), test_linkage, ¶m, workspace, assignments) != eslOK) esl_fatal(msg); |
| 1311 | if (esl_iset_monoValidate( msa->ax, msa->nseq, sizeof(ESL_DSQ *), test_linkage, ¶m, assignments) != eslOK) esl_fatal(msg); |
| 1312 | |
| 1313 | if (esl_iset_biCobalt (rng, msa->ax, msa->nseq, sizeof(ESL_DSQ *), test_linkage, ¶m, workspace, assignments) != eslOK) esl_fatal(msg); |
| 1314 | if (esl_iset_biValidate ( msa->ax, msa->nseq, sizeof(ESL_DSQ *), test_linkage, ¶m, assignments) != eslOK) esl_fatal(msg); |
| 1315 | |
| 1316 | if (esl_iset_biBlue (rng, msa->ax, msa->nseq, sizeof(ESL_DSQ *), test_linkage, ¶m, workspace, assignments) != eslOK) esl_fatal(msg); |
| 1317 | if (esl_iset_biValidate ( msa->ax, msa->nseq, sizeof(ESL_DSQ *), test_linkage, ¶m, assignments) != eslOK) esl_fatal(msg); |
| 1318 | |
| 1319 | if (esl_iset_biRandom (rng, 0.7, msa->ax, msa->nseq, sizeof(ESL_DSQ *), test_linkage, ¶m, assignments) != eslOK) esl_fatal(msg); |
| 1320 | if (esl_iset_biValidate ( msa->ax, msa->nseq, sizeof(ESL_DSQ *), test_linkage, ¶m, assignments) != eslOK) esl_fatal(msg); |
| 1321 | |
| 1322 | free(workspace); |
| 1323 | free(assignments); |
| 1324 | esl_msa_Destroy(msa); |
| 1325 | esl_alphabet_Destroy(abc); |
| 1326 | return; |
| 1327 | |
| 1328 | ERROR: |
| 1329 | esl_fatal(msg); |
| 1330 | } |
| 1331 | #endif //eslISET_TESTDRIVE |
| 1332 | |
| 1333 | |
| 1334 | /***************************************************************** |
| 1335 | * 6. Test driver |
| 1336 | *****************************************************************/ |
| 1337 | #ifdef eslISET_TESTDRIVE |
| 1338 | |
| 1339 | #include <esl_config.h> |
| 1340 | |
| 1341 | #include <stdio.h> |
| 1342 | #include <math.h> |
| 1343 | |
| 1344 | #include "easel.h" |
| 1345 | #include "esl_getopts.h" |
| 1346 | #include "esl_random.h" |
| 1347 | |
| 1348 | static ESL_OPTIONS options[] = { |
| 1349 | /* name type default env range toggles reqs incomp help docgroup*/ |
| 1350 | { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 1351 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 1352 | }; |
| 1353 | static char usage[] = "[-options]"; |
| 1354 | static char banner[] = "test driver for iset module"; |
| 1355 | |
| 1356 | int |
| 1357 | main(int argc, char **argv) |
| 1358 | { |
| 1359 | ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 1360 | ESL_RANDOMNESS *rng = esl_randomness_Create(0); |
| 1361 | |
| 1362 | fprintf(stderr, "## %s\n", argv[0]); |
| 1363 | fprintf(stderr, "# rng seed = %" PRIu32 "\n", esl_randomness_GetSeed(rng)); |
| 1364 | |
| 1365 | utest_basic(rng); |
| 1366 | |
| 1367 | fprintf(stderr, "# status = ok\n"); |
| 1368 | |
| 1369 | esl_randomness_Destroy(rng); |
| 1370 | esl_getopts_Destroy(go); |
| 1371 | return eslOK; |
| 1372 | } |
| 1373 | #endif /* eslISET_TESTDRIVE*/ |
| 1374 | |
| 1375 | |
| 1376 | |
| 1377 | /***************************************************************** |
| 1378 | * 7. Example |
| 1379 | *****************************************************************/ |
| 1380 | |
| 1381 | #ifdef eslISET_EXAMPLE |
| 1382 | |
| 1383 | #include "easel.h" |
| 1384 | #include "esl_alphabet.h" |
| 1385 | #include "esl_distance.h" |
| 1386 | #include "esl_msa.h" |
| 1387 | #include "esl_msafile.h" |
| 1388 | #include "esl_random.h" |
| 1389 | |
| 1390 | #include "esl_iset.h" |
| 1391 | |
| 1392 | /* The example_linkfunc() helper function will need two things in its parameter package: */ |
| 1393 | struct example_param_s { |
| 1394 | double maxid; // 0 <= maxid <= 1. No seq in S has >= this fractional identity to any seq in T. Smaller maxid = more challenging split. |
| 1395 | ESL_ALPHABET *abc; |
| 1396 | }; |
| 1397 | |
| 1398 | /* The helper linkage function returns TRUE when object v1 is linked to object v2: here, when aseq s1 has >= maxid identity to s2. |
| 1399 | */ |
| 1400 | static int |
| 1401 | example_linkfunc(const void *v1, const void *v2, const void *p, int *ret_link) |
| 1402 | { |
| 1403 | ESL_DSQ *ax1 = *(ESL_DSQ **) v1; |
| 1404 | ESL_DSQ *ax2 = *(ESL_DSQ **) v2; |
| 1405 | struct example_param_s *prm = (struct example_param_s *) p; |
| 1406 | double id; |
| 1407 | int status = eslOK; |
| 1408 | |
| 1409 | if ( (status = esl_dst_XPairId(prm->abc, ax1, ax2, &id, /*opt_nid=*/ NULL, /*opt_n=*/ NULL)) != eslOK) return status; // eEINVAL: aseqs aren't same len |
| 1410 | *ret_link = (id >= prm->maxid ? TRUE : FALSE); |
| 1411 | return status; |
| 1412 | } |
| 1413 | |
| 1414 | int |
| 1415 | main(int argc, char **argv) |
| 1416 | { |
| 1417 | char *filename = argv[1]; |
| 1418 | int fmt = eslMSAFILE_UNKNOWN; |
| 1419 | ESL_RANDOMNESS *rng = esl_randomness_Create(0); |
| 1420 | ESL_ALPHABET *abc = NULL; |
| 1421 | ESL_MSAFILE *afp = NULL; |
| 1422 | ESL_MSA *msa = NULL; |
| 1423 | int *wrk = NULL; // workspace provided to Cobalt/Blue. monoCobalt needs 2n ints; biCobalt 3n; monoBlue 4n; biBlue 4n. |
| 1424 | double maxid = 0.25; |
| 1425 | struct example_param_s prm; |
| 1426 | int *assignments = NULL; |
| 1427 | int nS = 0; // number of sequences in larger (training) set S |
| 1428 | int nT = 0; // number of sequences in smaller (test) set T |
| 1429 | int nX = 0; // number of sequences removed to make the split work |
| 1430 | int i; |
| 1431 | int status; |
| 1432 | |
| 1433 | /* Open MSA file; guess alphabet; set to digital mode */ |
| 1434 | if ((status = esl_msafile_Open(&abc, filename, NULL, fmt, NULL, &afp)) != eslOK) |
| 1435 | esl_msafile_OpenFailure(afp, status); |
| 1436 | |
| 1437 | /* Read one alignment */ |
| 1438 | if ((status = esl_msafile_Read(afp, &msa)) != eslOK) |
| 1439 | esl_msafile_ReadFailure(afp, status); |
| 1440 | |
| 1441 | /* Allocate workspace needed by Cobalt/Blue. Use max, which is biBlue's requirement for 4n integers */ |
| 1442 | ESL_ALLOC(wrk, 4 * msa->nseq * sizeof(int)); |
| 1443 | |
| 1444 | /* ... and space for assignment results, n integers */ |
| 1445 | ESL_ALLOC(assignments, 2 * msa->nseq * sizeof(int)); |
| 1446 | |
| 1447 | /* ... and configure data package we need to pass on to the linkage helper function, example_linkfunc() */ |
| 1448 | prm.maxid = maxid; |
| 1449 | prm.abc = abc; |
| 1450 | |
| 1451 | /* Use Cobalt to split into independent training and test set S, T. |
| 1452 | * assignments[i] = 0,1,2 for seq i assigned to neither, S, T; |S| >= |T|; no seq in S has >= maxid to any seq in T. |
| 1453 | * |
| 1454 | * Substitute Blue for Cobalt to get even better splits, with higher compute cost. |
| 1455 | * Substitute mono for bi to filter instead of split; assignments[i] = 0|1 for inclusion in redundancy-filtered set |
| 1456 | */ |
| 1457 | status = esl_iset_biCobalt(rng, msa->ax, msa->nseq, sizeof(ESL_DSQ *), example_linkfunc, &prm, wrk, assignments); |
| 1458 | |
| 1459 | for (i = 0; i < msa->nseq; i++) |
| 1460 | if (assignments[i] == 0) nX++; |
| 1461 | else if (assignments[i] == 1) nS++; |
| 1462 | else if (assignments[i] == 2) nT++; |
| 1463 | else esl_fatal("unexpected assignment"); |
| 1464 | |
| 1465 | printf("Training: %d\n", nS); |
| 1466 | printf("Test: %d\n", nT); |
| 1467 | printf("Removed: %d\n", nX); |
| 1468 | |
| 1469 | free(assignments); |
| 1470 | free(wrk); |
| 1471 | esl_msafile_Close(afp); |
| 1472 | esl_msa_Destroy(msa); |
| 1473 | esl_alphabet_Destroy(abc); |
| 1474 | esl_randomness_Destroy(rng); |
| 1475 | return 0; |
| 1476 | |
| 1477 | ERROR: |
| 1478 | esl_fatal("allocation failed"); |
| 1479 | } |
| 1480 | #endif /*eslISET_EXAMPLE*/ |