| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include <esl_config.h> |
| 18 |
|
| 19 |
#include "easel.h" |
| 20 |
#include "esl_alphabet.h" |
| 21 |
#include "esl_cluster.h" |
| 22 |
#include "esl_distance.h" |
| 23 |
#include "esl_msa.h" |
| 24 |
|
| 25 |
#include "esl_msacluster.h" |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#if defined(eslMSACLUSTER_REGRESSION) || defined(eslMSAWEIGHT_REGRESSION) |
| 31 |
#include <ctype.h> |
| 32 |
static double squid_distance(char *s1, char *s2); |
| 33 |
static double squid_xdistance(ESL_ALPHABET *a, ESL_DSQ *x1, ESL_DSQ *x2); |
| 34 |
#endif |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
static int msacluster_clinkage(const void *v1, const void *v2, const void *p, int *ret_link); |
| 40 |
static int msacluster_xlinkage(const void *v1, const void *v2, const void *p, int *ret_link); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
struct msa_param_s { |
| 46 |
double maxid; |
| 47 |
ESL_ALPHABET *abc; |
| 48 |
}; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
int |
| 109 |
esl_msacluster_SingleLinkage(const ESL_MSA *msa, double maxid, |
| 110 |
int **opt_c, int **opt_nin, int *opt_nc) |
| 111 |
|
| 112 |
{ |
| 113 |
int status; |
| 114 |
int *workspace = NULL; |
| 115 |
int *assignment = NULL; |
| 116 |
int *nin = NULL; |
| 117 |
int nc; |
| 118 |
int i; |
| 119 |
struct msa_param_s param; |
| 120 |
int free_assignment = 0; |
| 121 |
int allocated_assignment = 0; |
| 122 |
|
| 123 |
ESL_ALLOC(workspace, sizeof(int) * msa->nseq * 2); |
| 124 |
if(opt_c != NULL && *opt_c !=NULL){ |
| 125 |
assignment = *opt_c; |
| 126 |
} |
| 127 |
else{ |
| 128 |
ESL_ALLOC(assignment, sizeof(int) * msa->nseq); |
| 129 |
allocated_assignment = 1; |
| 130 |
if(opt_c != NULL){ |
| 131 |
*opt_c = assignment; |
| 132 |
} |
| 133 |
else{ |
| 134 |
free_assignment = 1; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
if (! (msa->flags & eslMSA_DIGITAL)) |
| 141 |
status = esl_cluster_SingleLinkage((void *) msa->aseq, (size_t) msa->nseq, sizeof(char *), |
| 142 |
msacluster_clinkage, (void *) &maxid, |
| 143 |
workspace, assignment, &nc); |
| 144 |
else { |
| 145 |
param.maxid = maxid; |
| 146 |
param.abc = msa->abc; |
| 147 |
status = esl_cluster_SingleLinkage((void *) msa->ax, (size_t) msa->nseq, sizeof(ESL_DSQ *), |
| 148 |
msacluster_xlinkage, (void *) ¶m, |
| 149 |
workspace, assignment, &nc); |
| 150 |
} |
| 151 |
if (status != eslOK) goto ERROR; |
| 152 |
|
| 153 |
|
| 154 |
if (opt_nin != NULL) |
| 155 |
{ |
| 156 |
if(*opt_nin == NULL){ |
| 157 |
ESL_ALLOC(nin, sizeof(int) * nc); |
| 158 |
*opt_nin = nin; |
| 159 |
} |
| 160 |
else{ |
| 161 |
nin = *opt_nin; |
| 162 |
} |
| 163 |
for (i = 0; i < nc; i++) nin[i] = 0; |
| 164 |
for (i = 0; i < msa->nseq; i++) |
| 165 |
nin[assignment[i]]++; |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
free(workspace); |
| 171 |
if(free_assignment){ |
| 172 |
free(assignment); |
| 173 |
} |
| 174 |
if (opt_nc != NULL) *opt_nc = nc; |
| 175 |
return eslOK; |
| 176 |
|
| 177 |
ERROR: |
| 178 |
if (workspace != NULL) free(workspace); |
| 179 |
if (allocated_assignment) free(assignment); |
| 180 |
if (nin != NULL) free(nin); |
| 181 |
if (opt_c != NULL) *opt_c = NULL; |
| 182 |
if (opt_nc != NULL) *opt_nc = 0; |
| 183 |
return status; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
static int |
| 196 |
msacluster_clinkage(const void *v1, const void *v2, const void *p, int *ret_link) |
| 197 |
{ |
| 198 |
char *as1 = *(char **) v1; |
| 199 |
char *as2 = *(char **) v2; |
| 200 |
double maxid = *(double *) p; |
| 201 |
double pid; |
| 202 |
int status = eslOK; |
| 203 |
|
| 204 |
#if defined(eslMSACLUSTER_REGRESSION) || defined(eslMSAWEIGHT_REGRESSION) |
| 205 |
pid = 1. - squid_distance(as1, as2); |
| 206 |
#else |
| 207 |
if ((status = esl_dst_CPairId(as1, as2, &pid, NULL, NULL)) != eslOK) return status; |
| 208 |
#endif |
| 209 |
|
| 210 |
*ret_link = (pid >= maxid ? TRUE : FALSE); |
| 211 |
return status; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
static int |
| 216 |
msacluster_xlinkage(const void *v1, const void *v2, const void *p, int *ret_link) |
| 217 |
{ |
| 218 |
ESL_DSQ *ax1 = *(ESL_DSQ **) v1; |
| 219 |
ESL_DSQ *ax2 = *(ESL_DSQ **) v2; |
| 220 |
struct msa_param_s *param = (struct msa_param_s *) p; |
| 221 |
double pid; |
| 222 |
int status = eslOK; |
| 223 |
|
| 224 |
#if defined(eslMSACLUSTER_REGRESSION) || defined(eslMSAWEIGHT_REGRESSION) |
| 225 |
pid = 1. - squid_xdistance(param->abc, ax1, ax2); |
| 226 |
#else |
| 227 |
if ( (status = esl_dst_XPairId(param->abc, ax1, ax2, &pid, NULL, NULL)) != eslOK) return status; |
| 228 |
#endif |
| 229 |
|
| 230 |
*ret_link = (pid >= param->maxid ? TRUE : FALSE); |
| 231 |
return status; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
#if defined(eslMSACLUSTER_REGRESSION) || defined(eslMSAWEIGHT_REGRESSION) |
| 244 |
static double |
| 245 |
squid_distance(char *s1, char *s2) |
| 246 |
{ |
| 247 |
int diff = 0; |
| 248 |
int valid = 0; |
| 249 |
|
| 250 |
for (; *s1 != '\0'; s1++, s2++) |
| 251 |
{ |
| 252 |
if (!isalpha(*s1) || !isalpha(*s2)) continue; |
| 253 |
if (*s1 != *s2) diff++; |
| 254 |
valid++; |
| 255 |
} |
| 256 |
return (valid > 0 ? ((double) diff / (double) valid) : 0.0); |
| 257 |
} |
| 258 |
static double |
| 259 |
squid_xdistance(ESL_ALPHABET *a, ESL_DSQ *x1, ESL_DSQ *x2) |
| 260 |
{ |
| 261 |
int diff = 0; |
| 262 |
int valid = 0; |
| 263 |
|
| 264 |
for (; *x1 != eslDSQ_SENTINEL; x1++, x2++) |
| 265 |
{ |
| 266 |
if (esl_abc_XIsGap(a, *x1) || esl_abc_XIsGap(a, *x2)) continue; |
| 267 |
if (*x1 != *x2) diff++; |
| 268 |
valid++; |
| 269 |
} |
| 270 |
return (valid > 0 ? ((double) diff / (double) valid) : 0.0); |
| 271 |
} |
| 272 |
#endif |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
#ifdef eslMSACLUSTER_TESTDRIVE |
| 279 |
#include "esl_getopts.h" |
| 280 |
|
| 281 |
static void |
| 282 |
utest_SingleLinkage(ESL_GETOPTS *go, const ESL_MSA *msa, double maxid, int expected_nc, int last_assignment) |
| 283 |
{ |
| 284 |
char *msg = "utest_SingleLinkage() failed"; |
| 285 |
int *assignment = NULL; |
| 286 |
int *nin = NULL; |
| 287 |
int nc; |
| 288 |
|
| 289 |
if (esl_msacluster_SingleLinkage(msa, maxid, &assignment, &nin, &nc) != eslOK) esl_fatal(msg); |
| 290 |
if (nc != expected_nc) esl_fatal(msg); |
| 291 |
if (assignment[msa->nseq-1] != last_assignment) esl_fatal(msg); |
| 292 |
free(assignment); |
| 293 |
free(nin); |
| 294 |
} |
| 295 |
#endif |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
#ifdef eslMSACLUSTER_TESTDRIVE |
| 301 |
|
| 302 |
|
| 303 |
#include <esl_config.h> |
| 304 |
|
| 305 |
#include <stdio.h> |
| 306 |
#include <math.h> |
| 307 |
|
| 308 |
#include "easel.h" |
| 309 |
#include "esl_alphabet.h" |
| 310 |
#include "esl_getopts.h" |
| 311 |
#include "esl_msa.h" |
| 312 |
#include "esl_msacluster.h" |
| 313 |
#include "esl_msafile.h" |
| 314 |
|
| 315 |
static ESL_OPTIONS options[] = { |
| 316 |
|
| 317 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 318 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 319 |
}; |
| 320 |
static char usage[] = "[-options]"; |
| 321 |
static char banner[] = "test driver for msacluster module"; |
| 322 |
|
| 323 |
int |
| 324 |
main(int argc, char **argv) |
| 325 |
{ |
| 326 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 327 |
ESL_ALPHABET *abc = esl_alphabet_Create(eslAMINO); |
| 328 |
char *msg = "esl_msacluster_utest failed"; |
| 329 |
int status; |
| 330 |
ESL_MSA *msa = esl_msa_CreateFromString("\ |
| 331 |
# STOCKHOLM 1.0\n\ |
| 332 |
\n\ |
| 333 |
seq0 AAAAAAAAAA\n\ |
| 334 |
seq1 AAAAAAAAAA\n\ |
| 335 |
seq2 AAAAAAAAAC\n\ |
| 336 |
seq3 AAAAAAAADD\n\ |
| 337 |
seq4 AAAAAAAEEE\n\ |
| 338 |
seq5 AAAAAAFFFF\n\ |
| 339 |
seq6 AAAAAGGGGG\n\ |
| 340 |
seq7 AAAAHHHHHH\n\ |
| 341 |
seq8 AAAIIIIIII\n\ |
| 342 |
seq9 AAKKKKKKKK\n\ |
| 343 |
seq10 ALLLLLLLLL\n\ |
| 344 |
seq11 MMMMMMMMMM\n\ |
| 345 |
//", eslMSAFILE_STOCKHOLM); |
| 346 |
|
| 347 |
|
| 348 |
utest_SingleLinkage(go, msa, 1.0, 11, 10); |
| 349 |
utest_SingleLinkage(go, msa, 0.5, 6, 5); |
| 350 |
utest_SingleLinkage(go, msa, 0.0, 1, 0); |
| 351 |
|
| 352 |
|
| 353 |
esl_msa_Digitize(abc, msa, NULL); |
| 354 |
utest_SingleLinkage(go, msa, 1.0, 11, 10); |
| 355 |
utest_SingleLinkage(go, msa, 0.5, 6, 5); |
| 356 |
utest_SingleLinkage(go, msa, 0.0, 1, 0); |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
int *nin = NULL; |
| 361 |
int nc; |
| 362 |
|
| 363 |
|
| 364 |
if (esl_msacluster_SingleLinkage(msa, 0.5, NULL, &nin, &nc) != eslOK) esl_fatal(msg); |
| 365 |
|
| 366 |
int *assignment = NULL; |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
if (esl_msacluster_SingleLinkage(msa, 0.5, &assignment, &nin, &nc) != eslOK) esl_fatal(msg); |
| 371 |
|
| 372 |
if(assignment == NULL) esl_fatal(msg); |
| 373 |
free(assignment); |
| 374 |
|
| 375 |
ESL_ALLOC(assignment, 12*sizeof(int)); |
| 376 |
int *assignment2 = assignment; |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
if (esl_msacluster_SingleLinkage(msa, 0.5, &assignment, &nin, &nc) != eslOK) esl_fatal(msg); |
| 381 |
|
| 382 |
if(assignment != assignment2) esl_fatal(msg); |
| 383 |
free(assignment); |
| 384 |
|
| 385 |
|
| 386 |
free(nin); |
| 387 |
|
| 388 |
esl_msa_Destroy(msa); |
| 389 |
esl_alphabet_Destroy(abc); |
| 390 |
esl_getopts_Destroy(go); |
| 391 |
return 0; |
| 392 |
|
| 393 |
ERROR: |
| 394 |
return eslFAIL; |
| 395 |
} |
| 396 |
#endif |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
#ifdef eslMSACLUSTER_EXAMPLE |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
#include <stdio.h> |
| 412 |
#include "easel.h" |
| 413 |
#include "esl_msa.h" |
| 414 |
#include "esl_msacluster.h" |
| 415 |
#include "esl_msafile.h" |
| 416 |
|
| 417 |
int |
| 418 |
main(int argc, char **argv) |
| 419 |
{ |
| 420 |
char *filename = argv[1]; |
| 421 |
int fmt = eslMSAFILE_UNKNOWN; |
| 422 |
ESL_ALPHABET *abc = NULL; |
| 423 |
ESL_MSAFILE *afp = NULL; |
| 424 |
ESL_MSA *msa = NULL; |
| 425 |
double maxid = 0.62; |
| 426 |
int *assignment = NULL; |
| 427 |
int *nin = NULL; |
| 428 |
int nclusters; |
| 429 |
int c, i; |
| 430 |
int status; |
| 431 |
|
| 432 |
|
| 433 |
if ((status = esl_msafile_Open(&abc, filename, NULL, fmt, NULL, &afp)) != eslOK) |
| 434 |
esl_msafile_OpenFailure(afp, status); |
| 435 |
|
| 436 |
|
| 437 |
if ((status = esl_msafile_Read(afp, &msa)) != eslOK) |
| 438 |
esl_msafile_ReadFailure(afp, status); |
| 439 |
|
| 440 |
|
| 441 |
esl_msacluster_SingleLinkage(msa, maxid, &assignment, &nin, &nclusters); |
| 442 |
|
| 443 |
printf("%d clusters at threshold of %f fractional identity\n", nclusters, maxid); |
| 444 |
for (c = 0; c < nclusters; c++) { |
| 445 |
printf("cluster %d:\n", c); |
| 446 |
for (i = 0; i < msa->nseq; i++) if (assignment[i] == c) printf(" %s\n", msa->sqname[i]); |
| 447 |
printf("(%d sequences)\n\n", nin[c]); |
| 448 |
} |
| 449 |
|
| 450 |
esl_msa_Destroy(msa); |
| 451 |
esl_msafile_Close(afp); |
| 452 |
free(assignment); |
| 453 |
free(nin); |
| 454 |
return 0; |
| 455 |
} |
| 456 |
|
| 457 |
#endif |
| 458 |
|