| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#include <esl_config.h> |
| 36 |
|
| 37 |
#include <stdio.h> |
| 38 |
#include <string.h> |
| 39 |
#include <stdint.h> |
| 40 |
#include <pthread.h> |
| 41 |
|
| 42 |
#include "easel.h" |
| 43 |
#include "esl_alphabet.h" |
| 44 |
#include "esl_random.h" |
| 45 |
#include "esl_sq.h" |
| 46 |
#include "esl_sqio.h" |
| 47 |
|
| 48 |
#include "esl_dsqdata.h" |
| 49 |
|
| 50 |
static ESL_DSQDATA_CHUNK *dsqdata_chunk_Create (ESL_DSQDATA *dd); |
| 51 |
static void dsqdata_chunk_Destroy(ESL_DSQDATA_CHUNK *chu); |
| 52 |
|
| 53 |
static void *dsqdata_loader_thread (void *p); |
| 54 |
static void *dsqdata_unpacker_thread(void *p); |
| 55 |
|
| 56 |
static int dsqdata_unpack_chunk(ESL_DSQDATA_CHUNK *chu, int do_pack5); |
| 57 |
static int dsqdata_unpack5(uint32_t *psq, ESL_DSQ *dsq, int *ret_L, int *ret_P); |
| 58 |
static int dsqdata_unpack2(uint32_t *psq, ESL_DSQ *dsq, int *ret_L, int *ret_P); |
| 59 |
static int dsqdata_pack5 (ESL_DSQ *dsq, int L, uint32_t *psq, int *ret_P); |
| 60 |
static int dsqdata_pack2 (ESL_DSQ *dsq, int L, uint32_t *psq, int *ret_P); |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
static uint32_t eslDSQDATA_MAGIC_V1 = 0xc4d3d1b1; |
| 68 |
static uint32_t eslDSQDATA_MAGIC_V1SWAP = 0xb1d1d3c4; |
| 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 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
int |
| 156 |
esl_dsqdata_Open(ESL_ALPHABET **byp_abc, char *basename, int nconsumers, ESL_DSQDATA **ret_dd) |
| 157 |
{ |
| 158 |
ESL_DSQDATA *dd = NULL; |
| 159 |
int bufsize = 4096; |
| 160 |
uint32_t magic = 0; |
| 161 |
uint32_t tag = 0; |
| 162 |
uint32_t alphatype = eslUNKNOWN; |
| 163 |
int ndd; |
| 164 |
char *p; |
| 165 |
char buf[4096]; |
| 166 |
int u; |
| 167 |
int status; |
| 168 |
|
| 169 |
ESL_DASSERT1(( nconsumers > 0 )); |
| 170 |
ESL_DASSERT1(( byp_abc != NULL )); |
| 171 |
|
| 172 |
ESL_ALLOC(dd, sizeof(ESL_DSQDATA)); |
| 173 |
dd->stubfp = NULL; |
| 174 |
dd->ifp = NULL; |
| 175 |
dd->sfp = NULL; |
| 176 |
dd->mfp = NULL; |
| 177 |
dd->abc_r = *byp_abc; |
| 178 |
|
| 179 |
dd->magic = 0; |
| 180 |
dd->uniquetag = 0; |
| 181 |
dd->flags = 0; |
| 182 |
dd->max_namelen = 0; |
| 183 |
dd->max_acclen = 0; |
| 184 |
dd->max_desclen = 0; |
| 185 |
dd->max_seqlen = 0; |
| 186 |
dd->nseq = 0; |
| 187 |
dd->nres = 0; |
| 188 |
|
| 189 |
dd->chunk_maxseq = eslDSQDATA_CHUNK_MAXSEQ; |
| 190 |
dd->chunk_maxpacket = eslDSQDATA_CHUNK_MAXPACKET; |
| 191 |
dd->do_byteswap = FALSE; |
| 192 |
dd->pack5 = FALSE; |
| 193 |
|
| 194 |
dd->nconsumers = nconsumers; |
| 195 |
dd->n_unpackers = eslDSQDATA_UNPACKERS; |
| 196 |
dd->errbuf[0] = '\0'; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
ndd = strlen(basename) + 6; |
| 201 |
ESL_ALLOC( dd->basename, ndd); |
| 202 |
if ( snprintf(dd->basename, ndd, "%s.dsqi", basename) <= 0) ESL_XEXCEPTION_SYS(eslESYS, "sprintf() failure"); |
| 203 |
if (( dd->ifp = fopen(dd->basename, "rb")) == NULL) ESL_XFAIL(eslENOTFOUND, dd->errbuf, "Failed to find or open index file %s\n", dd->basename); |
| 204 |
|
| 205 |
if ( snprintf(dd->basename, ndd, "%s.dsqm", basename) <= 0) ESL_XEXCEPTION_SYS(eslESYS, "sprintf() failure"); |
| 206 |
if (( dd->mfp = fopen(dd->basename, "rb")) == NULL) ESL_XFAIL(eslENOTFOUND, dd->errbuf, "Failed to find or open metadata file %s\n", dd->basename); |
| 207 |
|
| 208 |
if ( snprintf(dd->basename, ndd, "%s.dsqs", basename) <= 0) ESL_XEXCEPTION_SYS(eslESYS, "sprintf() failure"); |
| 209 |
if (( dd->sfp = fopen(dd->basename, "rb")) == NULL) ESL_XFAIL(eslENOTFOUND, dd->errbuf, "Failed to find or open sequence file %s\n", dd->basename); |
| 210 |
|
| 211 |
strcpy(dd->basename, basename); |
| 212 |
if (( dd->stubfp = fopen(dd->basename, "r")) == NULL) ESL_XFAIL(eslENOTFOUND, dd->errbuf, "Failed to find or open stub file %s\n", dd->basename); |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
if ( fgets(buf, bufsize, dd->stubfp) == NULL) ESL_XFAIL(eslEFORMAT, dd->errbuf, "stub file is empty - no tag line found"); |
| 220 |
if (( p = strtok(buf, " \t\n\r")) == NULL) ESL_XFAIL(eslEFORMAT, dd->errbuf, "stub file has bad format: tag line has no data"); |
| 221 |
if ( strcmp(p, "Easel") != 0) ESL_XFAIL(eslEFORMAT, dd->errbuf, "stub file has bad format in tag line"); |
| 222 |
if (( p = strtok(NULL, " \t\n\r")) == NULL) ESL_XFAIL(eslEFORMAT, dd->errbuf, "stub file has bad format in tag line"); |
| 223 |
if ( strcmp(p, "dsqdata") != 0) ESL_XFAIL(eslEFORMAT, dd->errbuf, "stub file has bad format in tag line"); |
| 224 |
if (( p = strtok(NULL, " \t\n\r")) == NULL) ESL_XFAIL(eslEFORMAT, dd->errbuf, "stub file has bad format in tag line"); |
| 225 |
if ( *p != 'v') ESL_XFAIL(eslEFORMAT, dd->errbuf, "stub file has bad format: no v on version"); |
| 226 |
if ( ! esl_str_IsInteger(p+1)) ESL_XFAIL(eslEFORMAT, dd->errbuf, "stub file had bad format: no version number"); |
| 227 |
|
| 228 |
if (( p = strtok(NULL, " \t\n\r")) == NULL) ESL_XFAIL(eslEFORMAT, dd->errbuf, "stub file has bad format in tag line"); |
| 229 |
if ( *p != 'x') ESL_XFAIL(eslEFORMAT, dd->errbuf, "stub file has bad format: no x on tag"); |
| 230 |
if ( ! esl_str_IsInteger(p+1)) ESL_XFAIL(eslEFORMAT, dd->errbuf, "stub file had bad format: no integer tag"); |
| 231 |
dd->uniquetag = strtoul(p+1, NULL, 10); |
| 232 |
|
| 233 |
|
| 234 |
if ( fread(&(dd->magic), sizeof(uint32_t), 1, dd->ifp) != 1) ESL_XFAIL(eslEFORMAT, dd->errbuf, "index file has no header - is empty?"); |
| 235 |
if ( fread(&tag, sizeof(uint32_t), 1, dd->ifp) != 1) ESL_XFAIL(eslEFORMAT, dd->errbuf, "index file header truncated, no tag"); |
| 236 |
if ( fread(&alphatype, sizeof(uint32_t), 1, dd->ifp) != 1) ESL_XFAIL(eslEFORMAT, dd->errbuf, "index file header truncated, no alphatype"); |
| 237 |
if ( fread(&(dd->flags), sizeof(uint32_t), 1, dd->ifp) != 1) ESL_XFAIL(eslEFORMAT, dd->errbuf, "index file header truncated, no flags"); |
| 238 |
if ( fread(&(dd->max_namelen), sizeof(uint32_t), 1, dd->ifp) != 1) ESL_XFAIL(eslEFORMAT, dd->errbuf, "index file header truncated, no max name len"); |
| 239 |
if ( fread(&(dd->max_acclen), sizeof(uint32_t), 1, dd->ifp) != 1) ESL_XFAIL(eslEFORMAT, dd->errbuf, "index file header truncated, no max accession len"); |
| 240 |
if ( fread(&(dd->max_desclen), sizeof(uint32_t), 1, dd->ifp) != 1) ESL_XFAIL(eslEFORMAT, dd->errbuf, "index file header truncated, no max description len"); |
| 241 |
|
| 242 |
if ( fread(&(dd->max_seqlen), sizeof(uint64_t), 1, dd->ifp) != 1) ESL_XFAIL(eslEFORMAT, dd->errbuf, "index file header truncated, no max seq len"); |
| 243 |
if ( fread(&(dd->nseq), sizeof(uint64_t), 1, dd->ifp) != 1) ESL_XFAIL(eslEFORMAT, dd->errbuf, "index file header truncated, no nseq"); |
| 244 |
if ( fread(&(dd->nres), sizeof(uint64_t), 1, dd->ifp) != 1) ESL_XFAIL(eslEFORMAT, dd->errbuf, "index file header truncated, no nres"); |
| 245 |
|
| 246 |
|
| 247 |
if (tag != dd->uniquetag) ESL_XFAIL(eslEFORMAT, dd->errbuf, "index file has bad tag, doesn't go with stub file"); |
| 248 |
|
| 249 |
if (dd->magic == eslDSQDATA_MAGIC_V1SWAP) ESL_XEXCEPTION(eslEUNIMPLEMENTED, "dsqdata cannot yet read data in different byte orders"); |
| 250 |
else if (dd->magic != eslDSQDATA_MAGIC_V1) ESL_XFAIL(eslEFORMAT, dd->errbuf, "index file has bad magic"); |
| 251 |
|
| 252 |
|
| 253 |
if (dd->abc_r) |
| 254 |
{ |
| 255 |
if (alphatype != dd->abc_r->type) |
| 256 |
ESL_XFAIL(eslEFORMAT, dd->errbuf, "data files use %s alphabet; expected %s alphabet", |
| 257 |
esl_abc_DecodeType(alphatype), |
| 258 |
esl_abc_DecodeType(dd->abc_r->type)); |
| 259 |
} |
| 260 |
else |
| 261 |
{ |
| 262 |
if ( esl_abc_ValidateType(alphatype) != eslOK) ESL_XFAIL(eslEFORMAT, dd->errbuf, "index file has invalid alphabet type %d", alphatype); |
| 263 |
if (( dd->abc_r = esl_alphabet_Create(alphatype)) == NULL) ESL_XEXCEPTION(eslEMEM, "alphabet creation failed"); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
if (dd->abc_r->type == eslAMINO) dd->pack5 = TRUE; |
| 268 |
|
| 269 |
|
| 270 |
if (( fread(&magic, sizeof(uint32_t), 1, dd->mfp)) != 1) ESL_XFAIL(eslEFORMAT, dd->errbuf, "metadata file has no header - is empty?"); |
| 271 |
if (( fread(&tag, sizeof(uint32_t), 1, dd->mfp)) != 1) ESL_XFAIL(eslEFORMAT, dd->errbuf, "metadata file header truncated - no tag?"); |
| 272 |
if ( magic != dd->magic) ESL_XFAIL(eslEFORMAT, dd->errbuf, "metadata file has bad magic"); |
| 273 |
if ( tag != dd->uniquetag) ESL_XFAIL(eslEFORMAT, dd->errbuf, "metadata file has bad tag, doesn't match stub"); |
| 274 |
|
| 275 |
|
| 276 |
if (( fread(&magic, sizeof(uint32_t), 1, dd->sfp)) != 1) ESL_XFAIL(eslEFORMAT, dd->errbuf, "sequence file has no header - is empty?"); |
| 277 |
if (( fread(&tag, sizeof(uint32_t), 1, dd->sfp)) != 1) ESL_XFAIL(eslEFORMAT, dd->errbuf, "sequence file header truncated - no tag?"); |
| 278 |
if ( magic != dd->magic) ESL_XFAIL(eslEFORMAT, dd->errbuf, "sequence file has bad magic"); |
| 279 |
if ( tag != dd->uniquetag) ESL_XFAIL(eslEFORMAT, dd->errbuf, "sequence file has bad tag, doesn't match stub"); |
| 280 |
|
| 281 |
|
| 282 |
for (u = 0; u < dd->n_unpackers; u++) |
| 283 |
{ |
| 284 |
dd->inbox[u] = NULL; |
| 285 |
if ( pthread_mutex_init(&(dd->inbox_mutex[u]), NULL) != 0) ESL_XEXCEPTION(eslESYS, "pthread_mutex_init() failed"); |
| 286 |
if ( pthread_cond_init (&(dd->inbox_cv[u]), NULL) != 0) ESL_XEXCEPTION(eslESYS, "pthread_cond_init() failed"); |
| 287 |
dd->inbox_eod[u] = FALSE; |
| 288 |
|
| 289 |
dd->outbox[u] = NULL; |
| 290 |
if ( pthread_mutex_init(&(dd->outbox_mutex[u]), NULL) != 0) ESL_XEXCEPTION(eslESYS, "pthread_mutex_init() failed"); |
| 291 |
if ( pthread_cond_init (&(dd->outbox_cv[u]), NULL) != 0) ESL_XEXCEPTION(eslESYS, "pthread_cond_init() failed"); |
| 292 |
dd->outbox_eod[u] = FALSE; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
dd->nchunk = 0; |
| 297 |
if ( pthread_mutex_init(&dd->nchunk_mutex, NULL) != 0) ESL_XEXCEPTION(eslESYS, "pthread_mutex_init() failed"); |
| 298 |
|
| 299 |
|
| 300 |
dd->recycling = NULL; |
| 301 |
if ( pthread_mutex_init(&dd->recycling_mutex, NULL) != 0) ESL_XEXCEPTION(eslESYS, "pthread_mutex_init() failed"); |
| 302 |
if ( pthread_cond_init(&dd->recycling_cv, NULL) != 0) ESL_XEXCEPTION(eslESYS, "pthread_cond_init() failed"); |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
dd->go = FALSE; |
| 312 |
if ( pthread_mutex_init(&dd->go_mutex, NULL) != 0) ESL_XEXCEPTION(eslESYS, "pthread_mutex_init() failed on go_mutex"); |
| 313 |
if ( pthread_cond_init( &dd->go_cv, NULL) != 0) ESL_XEXCEPTION(eslESYS, "pthread_cond_init() failed on go_cv"); |
| 314 |
if ( pthread_mutex_lock(&dd->go_mutex) != 0) ESL_XEXCEPTION(eslESYS, "pthread_mutex_lock() failed on go_mutex"); |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
if ( pthread_create(&dd->loader_t, NULL, dsqdata_loader_thread, dd) != 0) ESL_XEXCEPTION(eslESYS, "pthread_create() failed"); |
| 320 |
for (u = 0; u < dd->n_unpackers; u++) |
| 321 |
if ( pthread_create(&(dd->unpacker_t[u]), NULL, dsqdata_unpacker_thread, dd) != 0) ESL_XEXCEPTION(eslESYS, "pthread_create() failed"); |
| 322 |
|
| 323 |
|
| 324 |
dd->go = TRUE; |
| 325 |
if ( pthread_mutex_unlock(&dd->go_mutex) != 0) ESL_XEXCEPTION(eslESYS, "pthread_mutex_unlock() failed on go_mutex"); |
| 326 |
if ( pthread_cond_broadcast(&dd->go_cv) != 0) ESL_XEXCEPTION(eslESYS, "pthread_cond_broadcast() failed on go_cv"); |
| 327 |
|
| 328 |
*ret_dd = dd; |
| 329 |
*byp_abc = dd->abc_r; |
| 330 |
return eslOK; |
| 331 |
|
| 332 |
ERROR: |
| 333 |
if (status == eslENOTFOUND || status == eslEFORMAT || status == eslEINCOMPAT) |
| 334 |
{ |
| 335 |
*ret_dd = dd; |
| 336 |
if (*byp_abc == NULL && dd->abc_r) esl_alphabet_Destroy(dd->abc_r); |
| 337 |
return status; |
| 338 |
} |
| 339 |
else if (status != eslESYS) |
| 340 |
{ |
| 341 |
esl_dsqdata_Close(dd); |
| 342 |
*ret_dd = NULL; |
| 343 |
if (*byp_abc == NULL && dd->abc_r) esl_alphabet_Destroy(dd->abc_r); |
| 344 |
return status; |
| 345 |
} |
| 346 |
else |
| 347 |
{ |
| 348 |
*ret_dd = NULL; |
| 349 |
if (*byp_abc == NULL && dd->abc_r) esl_alphabet_Destroy(dd->abc_r); |
| 350 |
return status; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
int |
| 391 |
esl_dsqdata_Read(ESL_DSQDATA *dd, ESL_DSQDATA_CHUNK **ret_chu) |
| 392 |
{ |
| 393 |
ESL_DSQDATA_CHUNK *chu = NULL; |
| 394 |
int u; |
| 395 |
|
| 396 |
|
| 397 |
if ( pthread_mutex_lock(&dd->nchunk_mutex) != 0) ESL_EXCEPTION(eslESYS, "failed to lock reader mutex"); |
| 398 |
u = (int) (dd->nchunk % dd->n_unpackers); |
| 399 |
|
| 400 |
|
| 401 |
if ( pthread_mutex_lock(&(dd->outbox_mutex[u])) != 0) ESL_EXCEPTION(eslESYS, "failed to lock outbox[u] mutex"); |
| 402 |
while (! dd->outbox_eod[u] && dd->outbox[u] == NULL) { |
| 403 |
if ( pthread_cond_wait(&(dd->outbox_cv[u]), &(dd->outbox_mutex[u])) != 0) ESL_EXCEPTION(eslESYS, "failed to wait on outbox[u] signal"); |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
chu = dd->outbox[u]; |
| 408 |
dd->outbox[u] = NULL; |
| 409 |
if (chu) dd->nchunk++; |
| 410 |
|
| 411 |
|
| 412 |
if ( pthread_mutex_unlock(&(dd->outbox_mutex[u])) != 0) ESL_EXCEPTION(eslESYS, "failed to unlock outbox[u] mutex"); |
| 413 |
if ( chu && pthread_cond_signal (&(dd->outbox_cv[u])) != 0) ESL_EXCEPTION(eslESYS, "failed to signal outbox[u] is empty"); |
| 414 |
|
| 415 |
|
| 416 |
if ( pthread_mutex_unlock(&dd->nchunk_mutex) != 0) ESL_EXCEPTION(eslESYS, "failed to unlock reader mutex"); |
| 417 |
|
| 418 |
*ret_chu = chu; |
| 419 |
return (chu ? eslOK : eslEOF); |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
int |
| 444 |
esl_dsqdata_Recycle(ESL_DSQDATA *dd, ESL_DSQDATA_CHUNK *chu) |
| 445 |
{ |
| 446 |
if (chu) |
| 447 |
{ |
| 448 |
if ( pthread_mutex_lock(&dd->recycling_mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread mutex lock failed"); |
| 449 |
chu->nxt = dd->recycling; |
| 450 |
dd->recycling = chu; |
| 451 |
if ( pthread_mutex_unlock(&dd->recycling_mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread mutex unlock failed"); |
| 452 |
if ( pthread_cond_signal(&dd->recycling_cv) != 0) ESL_EXCEPTION(eslESYS, "pthread cond signal failed"); |
| 453 |
} |
| 454 |
return eslOK; |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
int |
| 492 |
esl_dsqdata_Close(ESL_DSQDATA *dd) |
| 493 |
{ |
| 494 |
int u; |
| 495 |
|
| 496 |
if (dd) |
| 497 |
{ |
| 498 |
|
| 499 |
if ( pthread_join(dd->loader_t, NULL) != 0) ESL_EXCEPTION(eslESYS, "pthread join failed"); |
| 500 |
for (u = 0; u < dd->n_unpackers; u++) |
| 501 |
if ( pthread_join(dd->unpacker_t[u], NULL) != 0) ESL_EXCEPTION(eslESYS, "pthread join failed"); |
| 502 |
|
| 503 |
if (dd->basename) free(dd->basename); |
| 504 |
if (dd->stubfp) { if ( fclose(dd->stubfp) != 0) ESL_EXCEPTION(eslESYS, "fclose failed"); } |
| 505 |
if (dd->ifp) { if ( fclose(dd->ifp) != 0) ESL_EXCEPTION(eslESYS, "fclose failed"); } |
| 506 |
if (dd->sfp) { if ( fclose(dd->sfp) != 0) ESL_EXCEPTION(eslESYS, "fclose failed"); } |
| 507 |
if (dd->mfp) { if ( fclose(dd->mfp) != 0) ESL_EXCEPTION(eslESYS, "fclose failed"); } |
| 508 |
|
| 509 |
for (u = 0; u < dd->n_unpackers; u++) |
| 510 |
{ |
| 511 |
if ( pthread_mutex_destroy(&(dd->inbox_mutex[u])) != 0) ESL_EXCEPTION(eslESYS, "pthread mutex destroy failed"); |
| 512 |
if ( pthread_cond_destroy(&(dd->inbox_cv[u])) != 0) ESL_EXCEPTION(eslESYS, "pthread cond destroy failed"); |
| 513 |
if ( pthread_mutex_destroy(&(dd->outbox_mutex[u])) != 0) ESL_EXCEPTION(eslESYS, "pthread mutex destroy failed"); |
| 514 |
if ( pthread_cond_destroy(&(dd->outbox_cv[u])) != 0) ESL_EXCEPTION(eslESYS, "pthread cond destroy failed"); |
| 515 |
} |
| 516 |
if ( pthread_mutex_destroy(&dd->nchunk_mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread mutex destroy failed"); |
| 517 |
if ( pthread_mutex_destroy(&dd->recycling_mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread mutex destroy failed"); |
| 518 |
if ( pthread_cond_destroy(&dd->recycling_cv) != 0) ESL_EXCEPTION(eslESYS, "pthread cond destroy failed"); |
| 519 |
if ( pthread_mutex_destroy(&dd->go_mutex) != 0) ESL_EXCEPTION(eslESYS, "pthread mutex destroy failed"); |
| 520 |
if ( pthread_cond_destroy(&dd->go_cv) != 0) ESL_EXCEPTION(eslESYS, "pthread cond destroy failed"); |
| 521 |
|
| 522 |
|
| 523 |
#if (eslDEBUGLEVEL >= 1) |
| 524 |
for (u = 0; u < dd->n_unpackers; u++) { |
| 525 |
assert( dd->inbox[u] == NULL ); |
| 526 |
assert( dd->outbox[u] == NULL ); |
| 527 |
} |
| 528 |
assert(dd->recycling == NULL ); |
| 529 |
#endif |
| 530 |
free(dd); |
| 531 |
} |
| 532 |
return eslOK; |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
int |
| 573 |
esl_dsqdata_Write(ESL_SQFILE *sqfp, char *basename, char *errbuf) |
| 574 |
{ |
| 575 |
ESL_RANDOMNESS *rng = NULL; |
| 576 |
ESL_SQ *sq = NULL; |
| 577 |
FILE *stubfp = NULL; |
| 578 |
FILE *ifp = NULL; |
| 579 |
FILE *mfp = NULL; |
| 580 |
FILE *sfp = NULL; |
| 581 |
char *outfile = NULL; |
| 582 |
uint32_t magic = eslDSQDATA_MAGIC_V1; |
| 583 |
uint32_t uniquetag; |
| 584 |
uint32_t alphatype; |
| 585 |
uint32_t flags = 0; |
| 586 |
uint32_t max_namelen = 0; |
| 587 |
uint32_t max_acclen = 0; |
| 588 |
uint32_t max_desclen = 0; |
| 589 |
uint64_t max_seqlen = 0; |
| 590 |
uint64_t nseq = 0; |
| 591 |
uint64_t nres = 0; |
| 592 |
int do_pack5 = FALSE; |
| 593 |
uint32_t *psq; |
| 594 |
ESL_DSQDATA_RECORD idx; |
| 595 |
int plen; |
| 596 |
int64_t spos = 0; |
| 597 |
int64_t mpos = 0; |
| 598 |
int n; |
| 599 |
int na; |
| 600 |
int status; |
| 601 |
|
| 602 |
if (! esl_sqfile_IsRewindable(sqfp)) ESL_EXCEPTION(eslEINVAL, "sqfp must be rewindable (e.g. an open file)"); |
| 603 |
if (! sqfp->abc) ESL_EXCEPTION(eslEINVAL, "sqfp must be digital"); |
| 604 |
|
| 605 |
if ( (sq = esl_sq_CreateDigital(sqfp->abc)) == NULL) { status = eslEMEM; goto ERROR; } |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
while ((status = esl_sqio_Read(sqfp, sq)) == eslOK) |
| 611 |
{ |
| 612 |
if (sq->n >= 6 * eslDSQDATA_CHUNK_MAXPACKET) |
| 613 |
ESL_EXCEPTION(eslEUNIMPLEMENTED, "dsqdata cannot currently deal with large sequences"); |
| 614 |
|
| 615 |
nseq++; |
| 616 |
nres += sq->n; |
| 617 |
if (sq->n > max_seqlen) max_seqlen = sq->n; |
| 618 |
n = strlen(sq->name); if (n > max_namelen) max_namelen = n; |
| 619 |
n = strlen(sq->acc); if (n > max_acclen) max_acclen = n; |
| 620 |
n = strlen(sq->desc); if (n > max_desclen) max_desclen = n; |
| 621 |
esl_sq_Reuse(sq); |
| 622 |
} |
| 623 |
if (status == eslEFORMAT) ESL_XFAIL(eslEFORMAT, errbuf, sqfp->get_error(sqfp)); |
| 624 |
else if (status != eslEOF) return status; |
| 625 |
|
| 626 |
if ((status = esl_sqfile_Position(sqfp, 0)) != eslOK) return status; |
| 627 |
|
| 628 |
|
| 629 |
if (( rng = esl_randomness_Create(0) ) == NULL) { status = eslEMEM; goto ERROR; } |
| 630 |
uniquetag = esl_random_uint32(rng); |
| 631 |
alphatype = sqfp->abc->type; |
| 632 |
|
| 633 |
if (alphatype == eslAMINO) do_pack5 = TRUE; |
| 634 |
else if (alphatype != eslDNA && alphatype != eslRNA) ESL_EXCEPTION(eslEINVAL, "alphabet must be protein or nucleic"); |
| 635 |
|
| 636 |
na = strlen(basename) + 6; |
| 637 |
ESL_ALLOC(outfile, na); |
| 638 |
if ( snprintf(outfile, na, "%s.dsqi", basename) < 0) ESL_EXCEPTION(eslESYS, "sprintf failed"); |
| 639 |
if (( ifp = fopen(outfile, "wb")) == NULL) ESL_XFAIL(eslEWRITE, errbuf, "failed to open dsqdata index file %s for writing", outfile); |
| 640 |
if ( snprintf(outfile, na, "%s.dsqm", basename) < 0) ESL_EXCEPTION(eslESYS, "sprintf failed"); |
| 641 |
if (( mfp = fopen(outfile, "wb")) == NULL) ESL_XFAIL(eslEWRITE, errbuf, "failed to open dsqdata metadata file %s for writing", outfile); |
| 642 |
if ( snprintf(outfile, na, "%s.dsqs", basename) < 0) ESL_EXCEPTION(eslESYS, "sprintf failed"); |
| 643 |
if (( sfp = fopen(outfile, "wb")) == NULL) ESL_XFAIL(eslEWRITE, errbuf, "failed to open dsqdata sequence file %s for writing", outfile); |
| 644 |
if (( stubfp = fopen(basename, "w")) == NULL) ESL_XFAIL(eslEWRITE, errbuf, "failed to open dsqdata stub file %s for writing", basename); |
| 645 |
|
| 646 |
|
| 647 |
if (fwrite(&magic, sizeof(uint32_t), 1, ifp) != 1 || |
| 648 |
fwrite(&uniquetag, sizeof(uint32_t), 1, ifp) != 1 || |
| 649 |
fwrite(&alphatype, sizeof(uint32_t), 1, ifp) != 1 || |
| 650 |
fwrite(&flags, sizeof(uint32_t), 1, ifp) != 1 || |
| 651 |
fwrite(&max_namelen, sizeof(uint32_t), 1, ifp) != 1 || |
| 652 |
fwrite(&max_acclen, sizeof(uint32_t), 1, ifp) != 1 || |
| 653 |
fwrite(&max_desclen, sizeof(uint32_t), 1, ifp) != 1 || |
| 654 |
fwrite(&max_seqlen, sizeof(uint64_t), 1, ifp) != 1 || |
| 655 |
fwrite(&nseq, sizeof(uint64_t), 1, ifp) != 1 || |
| 656 |
fwrite(&nres, sizeof(uint64_t), 1, ifp) != 1) |
| 657 |
ESL_XEXCEPTION_SYS(eslESYS, "fwrite() failed, index file header"); |
| 658 |
|
| 659 |
|
| 660 |
if (fwrite(&magic, sizeof(uint32_t), 1, mfp) != 1 || |
| 661 |
fwrite(&uniquetag, sizeof(uint32_t), 1, mfp) != 1) |
| 662 |
ESL_XEXCEPTION_SYS(eslESYS, "fwrite() failed, metadata file header"); |
| 663 |
|
| 664 |
|
| 665 |
if (fwrite(&magic, sizeof(uint32_t), 1, sfp) != 1 || |
| 666 |
fwrite(&uniquetag, sizeof(uint32_t), 1, sfp) != 1) |
| 667 |
ESL_XEXCEPTION_SYS(eslESYS, "fwrite() failed, metadata file header"); |
| 668 |
|
| 669 |
|
| 670 |
while ((status = esl_sqio_Read(sqfp, sq)) == eslOK) |
| 671 |
{ |
| 672 |
|
| 673 |
psq = (uint32_t *) sq->dsq; |
| 674 |
ESL_DASSERT1(( sq->salloc >= 4 )); |
| 675 |
if (do_pack5) dsqdata_pack5(sq->dsq, sq->n, psq, &plen); |
| 676 |
else dsqdata_pack2(sq->dsq, sq->n, psq, &plen); |
| 677 |
if ( fwrite(psq, sizeof(uint32_t), plen, sfp) != plen) |
| 678 |
ESL_XEXCEPTION(eslESYS, "fwrite() failed, packed seq"); |
| 679 |
spos += plen; |
| 680 |
|
| 681 |
|
| 682 |
n = strlen(sq->name); |
| 683 |
if ( fwrite(sq->name, sizeof(char), n+1, mfp) != n+1) |
| 684 |
ESL_XEXCEPTION(eslESYS, "fwrite () failed, metadata, name"); |
| 685 |
mpos += n+1; |
| 686 |
|
| 687 |
n = strlen(sq->acc); |
| 688 |
if ( fwrite(sq->acc, sizeof(char), n+1, mfp) != n+1) |
| 689 |
ESL_XEXCEPTION(eslESYS, "fwrite () failed, metadata, accession"); |
| 690 |
mpos += n+1; |
| 691 |
|
| 692 |
n = strlen(sq->desc); |
| 693 |
if ( fwrite(sq->desc, sizeof(char), n+1, mfp) != n+1) |
| 694 |
ESL_XEXCEPTION(eslESYS, "fwrite () failed, metadata, description"); |
| 695 |
mpos += n+1; |
| 696 |
|
| 697 |
if ( fwrite( &(sq->tax_id), sizeof(int32_t), 1, mfp) != 1) |
| 698 |
ESL_XEXCEPTION(eslESYS, "fwrite () failed, metadata, taxonomy id"); |
| 699 |
mpos += sizeof(int32_t); |
| 700 |
|
| 701 |
|
| 702 |
idx.psq_end = spos-1; |
| 703 |
idx.metadata_end = mpos-1; |
| 704 |
if ( fwrite(&idx, sizeof(ESL_DSQDATA_RECORD), 1, ifp) != 1) |
| 705 |
ESL_XEXCEPTION(eslESYS, "fwrite () failed, index file"); |
| 706 |
|
| 707 |
esl_sq_Reuse(sq); |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
fprintf(stubfp, "Easel dsqdata v1 x%" PRIu32 "\n", uniquetag); |
| 712 |
fprintf(stubfp, "\n"); |
| 713 |
fprintf(stubfp, "Original file: %s\n", sqfp->filename); |
| 714 |
fprintf(stubfp, "Original format: %s\n", esl_sqio_DecodeFormat(sqfp->format)); |
| 715 |
fprintf(stubfp, "Type: %s\n", esl_abc_DecodeType(sqfp->abc->type)); |
| 716 |
fprintf(stubfp, "Sequences: %" PRIu64 "\n", nseq); |
| 717 |
fprintf(stubfp, "Residues: %" PRIu64 "\n", nres); |
| 718 |
|
| 719 |
esl_sq_Destroy(sq); |
| 720 |
esl_randomness_Destroy(rng); |
| 721 |
free(outfile); |
| 722 |
fclose(stubfp); |
| 723 |
fclose(ifp); |
| 724 |
fclose(mfp); |
| 725 |
fclose(sfp); |
| 726 |
return eslOK; |
| 727 |
|
| 728 |
ERROR: |
| 729 |
if (sq) esl_sq_Destroy(sq); |
| 730 |
if (rng) esl_randomness_Destroy(rng); |
| 731 |
if (outfile) free(outfile); |
| 732 |
if (stubfp) fclose(stubfp); |
| 733 |
if (ifp) fclose(ifp); |
| 734 |
if (mfp) fclose(mfp); |
| 735 |
if (sfp) fclose(sfp); |
| 736 |
return status; |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
static ESL_DSQDATA_CHUNK * |
| 746 |
dsqdata_chunk_Create(ESL_DSQDATA *dd) |
| 747 |
{ |
| 748 |
ESL_DSQDATA_CHUNK *chu = NULL; |
| 749 |
int U; |
| 750 |
int status; |
| 751 |
|
| 752 |
ESL_ALLOC(chu, sizeof(ESL_DSQDATA_CHUNK)); |
| 753 |
chu->i0 = 0; |
| 754 |
chu->N = 0; |
| 755 |
chu->pn = 0; |
| 756 |
chu->dsq = NULL; |
| 757 |
chu->name = NULL; |
| 758 |
chu->acc = NULL; |
| 759 |
chu->desc = NULL; |
| 760 |
chu->taxid = NULL; |
| 761 |
chu->L = NULL; |
| 762 |
chu->metadata = NULL; |
| 763 |
chu->smem = NULL; |
| 764 |
chu->nxt = NULL; |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
ESL_ALLOC(chu->dsq, dd->chunk_maxseq * sizeof(ESL_DSQ *)); |
| 772 |
ESL_ALLOC(chu->name, dd->chunk_maxseq * sizeof(char *)); |
| 773 |
ESL_ALLOC(chu->acc, dd->chunk_maxseq * sizeof(char *)); |
| 774 |
ESL_ALLOC(chu->desc, dd->chunk_maxseq * sizeof(char *)); |
| 775 |
ESL_ALLOC(chu->taxid, dd->chunk_maxseq * sizeof(int)); |
| 776 |
ESL_ALLOC(chu->L, dd->chunk_maxseq * sizeof(int64_t)); |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
U = (dd->pack5 ? 6 * dd->chunk_maxpacket : 15 * dd->chunk_maxpacket); |
| 804 |
U += dd->chunk_maxseq + 1; |
| 805 |
ESL_ALLOC(chu->smem, sizeof(ESL_DSQ) * U); |
| 806 |
chu->psq = (uint32_t *) (chu->smem + U - 4*dd->chunk_maxpacket); |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
chu->mdalloc = 20 * dd->chunk_maxseq; |
| 816 |
ESL_ALLOC(chu->metadata, sizeof(char) * chu->mdalloc); |
| 817 |
|
| 818 |
return chu; |
| 819 |
|
| 820 |
ERROR: |
| 821 |
dsqdata_chunk_Destroy(chu); |
| 822 |
return NULL; |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
static void |
| 827 |
dsqdata_chunk_Destroy(ESL_DSQDATA_CHUNK *chu) |
| 828 |
{ |
| 829 |
if (chu) |
| 830 |
{ |
| 831 |
if (chu->metadata) free(chu->metadata); |
| 832 |
if (chu->smem) free(chu->smem); |
| 833 |
if (chu->L) free(chu->L); |
| 834 |
if (chu->taxid) free(chu->taxid); |
| 835 |
if (chu->desc) free(chu->desc); |
| 836 |
if (chu->acc) free(chu->acc); |
| 837 |
if (chu->name) free(chu->name); |
| 838 |
if (chu->dsq) free(chu->dsq); |
| 839 |
free(chu); |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
static void * |
| 849 |
dsqdata_loader_thread(void *p) |
| 850 |
{ |
| 851 |
ESL_DSQDATA *dd = (ESL_DSQDATA *) p; |
| 852 |
ESL_DSQDATA_RECORD *idx = NULL; |
| 853 |
ESL_DSQDATA_CHUNK *chu = NULL; |
| 854 |
int64_t nchunk = 0; |
| 855 |
int nalloc = 0; |
| 856 |
int nidx = 0; |
| 857 |
int nload = 0; |
| 858 |
int ncarried = 0; |
| 859 |
int nread = 0; |
| 860 |
int nmeta = 0; |
| 861 |
int i0 = 0; |
| 862 |
int64_t psq_last = -1; |
| 863 |
int64_t meta_last = -1; |
| 864 |
int u; |
| 865 |
int status; |
| 866 |
|
| 867 |
|
| 868 |
if ( pthread_mutex_lock(&dd->go_mutex) != 0) ESL_XEXCEPTION(eslESYS, "pthread_mutex_lock failed on go_mutex"); |
| 869 |
while (! dd->go) { |
| 870 |
if ( pthread_cond_wait(&dd->go_cv, &dd->go_mutex) != 0) ESL_XEXCEPTION(eslESYS, "pthread_cond_wait failed on go_cv"); |
| 871 |
} |
| 872 |
if ( pthread_mutex_unlock(&dd->go_mutex) != 0) ESL_XEXCEPTION(eslESYS, "pthread_mutex_lock failed on go_mutex"); |
| 873 |
|
| 874 |
|
| 875 |
ESL_ALLOC(idx, sizeof(ESL_DSQDATA_RECORD) * dd->chunk_maxseq); |
| 876 |
while (1) |
| 877 |
{ |
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
if (nalloc < dd->nconsumers + 3 * dd->n_unpackers + 2) |
| 890 |
{ |
| 891 |
|
| 892 |
|
| 893 |
if ( (chu = dsqdata_chunk_Create(dd)) == NULL) { status = eslEMEM; goto ERROR; } |
| 894 |
nalloc++; |
| 895 |
} |
| 896 |
else |
| 897 |
{ |
| 898 |
|
| 899 |
|
| 900 |
if ( pthread_mutex_lock(&dd->recycling_mutex) != 0) ESL_XEXCEPTION(eslESYS, "pthread mutex lock failed"); |
| 901 |
while (dd->recycling == NULL) { |
| 902 |
if ( pthread_cond_wait(&dd->recycling_cv, &dd->recycling_mutex) != 0) ESL_XEXCEPTION(eslESYS, "pthread cond wait failed"); |
| 903 |
} |
| 904 |
chu = dd->recycling; |
| 905 |
dd->recycling = chu->nxt; |
| 906 |
if ( pthread_mutex_unlock(&dd->recycling_mutex) != 0) ESL_XEXCEPTION(eslESYS, "pthread mutex unlock failed"); |
| 907 |
if ( pthread_cond_signal(&dd->recycling_cv) != 0) ESL_XEXCEPTION(eslESYS, "pthread cond signal failed"); |
| 908 |
|
| 909 |
|
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
i0 += nload; |
| 927 |
ncarried = (nidx - nload); |
| 928 |
memmove(idx, idx + nload, sizeof(ESL_DSQDATA_RECORD) * ncarried); |
| 929 |
nidx = fread(idx + ncarried, sizeof(ESL_DSQDATA_RECORD), dd->chunk_maxseq - ncarried, dd->ifp); |
| 930 |
nidx += ncarried; |
| 931 |
|
| 932 |
if (nidx == 0) |
| 933 |
{ |
| 934 |
|
| 935 |
dsqdata_chunk_Destroy(chu); |
| 936 |
nalloc--; |
| 937 |
break; |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
ESL_DASSERT1(( idx[0].psq_end - psq_last <= dd->chunk_maxpacket )); |
| 945 |
if (idx[nidx-1].psq_end - psq_last <= dd->chunk_maxpacket) |
| 946 |
nload = nidx; |
| 947 |
else |
| 948 |
{ |
| 949 |
int righti = nidx; |
| 950 |
int mid; |
| 951 |
nload = 1; |
| 952 |
while (righti - nload > 1) |
| 953 |
{ |
| 954 |
mid = nload + (righti - nload) / 2; |
| 955 |
if (idx[mid-1].psq_end - psq_last <= dd->chunk_maxpacket) nload = mid; |
| 956 |
else righti = mid; |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
chu->pn = idx[nload-1].psq_end - psq_last; |
| 964 |
nread = fread(chu->psq, sizeof(uint32_t), chu->pn, dd->sfp); |
| 965 |
|
| 966 |
if ( nread != chu->pn ) ESL_XEXCEPTION(eslEOD, "dsqdata packet loader: expected %d, got %d", chu->pn, nread); |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
nmeta = idx[nload-1].metadata_end - meta_last; |
| 971 |
if (nmeta > chu->mdalloc) { |
| 972 |
ESL_REALLOC(chu->metadata, sizeof(char) * nmeta); |
| 973 |
chu->mdalloc = nmeta; |
| 974 |
} |
| 975 |
nread = fread(chu->metadata, sizeof(char), nmeta, dd->mfp); |
| 976 |
if ( nread != nmeta ) ESL_XEXCEPTION(eslEOD, "dsqdata metadata loader: expected %d, got %d", nmeta, nread); |
| 977 |
|
| 978 |
chu->i0 = i0; |
| 979 |
chu->N = nload; |
| 980 |
psq_last = idx[nload-1].psq_end; |
| 981 |
meta_last = idx[nload-1].metadata_end; |
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
u = nchunk % dd->n_unpackers; |
| 986 |
|
| 987 |
if ( pthread_mutex_lock(&(dd->inbox_mutex[u])) != 0) ESL_XEXCEPTION(eslESYS, "pthread mutex lock failed"); |
| 988 |
while (dd->inbox[u] != NULL) { |
| 989 |
if (pthread_cond_wait(&(dd->inbox_cv[u]), &(dd->inbox_mutex[u])) != 0) ESL_XEXCEPTION(eslESYS, "pthread cond wait failed"); |
| 990 |
} |
| 991 |
dd->inbox[u] = chu; |
| 992 |
if ( pthread_mutex_unlock(&(dd->inbox_mutex[u])) != 0) ESL_XEXCEPTION(eslESYS, "pthread mutex unlock failed"); |
| 993 |
if ( pthread_cond_signal(&(dd->inbox_cv[u])) != 0) ESL_XEXCEPTION(eslESYS, "pthread cond signal failed"); |
| 994 |
|
| 995 |
|
| 996 |
nchunk++; |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
for (u = 0; u < dd->n_unpackers; u++) |
| 1002 |
{ |
| 1003 |
|
| 1004 |
if ( pthread_mutex_lock(&(dd->inbox_mutex[u])) != 0) ESL_XEXCEPTION(eslESYS, "pthread mutex lock failed"); |
| 1005 |
while (dd->inbox[u] != NULL) { |
| 1006 |
if (pthread_cond_wait(&(dd->inbox_cv[u]), &(dd->inbox_mutex[u])) != 0) ESL_XEXCEPTION(eslESYS, "pthread cond wait failed"); |
| 1007 |
} |
| 1008 |
dd->inbox_eod[u] = TRUE; |
| 1009 |
if ( pthread_mutex_unlock(&(dd->inbox_mutex[u])) != 0) ESL_XEXCEPTION(eslESYS, "pthread mutex unlock failed"); |
| 1010 |
if ( pthread_cond_signal(&(dd->inbox_cv[u])) != 0) ESL_XEXCEPTION(eslESYS, "pthread cond signal failed"); |
| 1011 |
} |
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
while (nalloc) |
| 1019 |
{ |
| 1020 |
|
| 1021 |
if ( pthread_mutex_lock(&dd->recycling_mutex) != 0) ESL_XEXCEPTION(eslESYS, "pthread mutex lock failed"); |
| 1022 |
while (dd->recycling == NULL) { |
| 1023 |
if ( pthread_cond_wait(&dd->recycling_cv, &dd->recycling_mutex) != 0) ESL_XEXCEPTION(eslESYS, "pthread cond wait failed"); |
| 1024 |
} |
| 1025 |
while (dd->recycling != NULL) { |
| 1026 |
chu = dd->recycling; |
| 1027 |
dd->recycling = chu->nxt; |
| 1028 |
dsqdata_chunk_Destroy(chu); |
| 1029 |
nalloc--; |
| 1030 |
} |
| 1031 |
if ( pthread_mutex_unlock(&dd->recycling_mutex) != 0) ESL_XEXCEPTION(eslESYS, "pthread mutex unlock failed"); |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
} |
| 1036 |
|
| 1037 |
free(idx); |
| 1038 |
pthread_exit(NULL); |
| 1039 |
|
| 1040 |
ERROR: |
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
if (idx) free(idx); |
| 1052 |
esl_fatal(" ... dsqdata loader thread failed: unrecoverable"); |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
static void * |
| 1058 |
dsqdata_unpacker_thread(void *p) |
| 1059 |
{ |
| 1060 |
ESL_DSQDATA *dd = (ESL_DSQDATA *) p; |
| 1061 |
ESL_DSQDATA_CHUNK *chu = NULL; |
| 1062 |
pthread_t my_id = pthread_self(); |
| 1063 |
int u; |
| 1064 |
int status; |
| 1065 |
|
| 1066 |
|
| 1067 |
if ( pthread_mutex_lock(&dd->go_mutex) != 0) ESL_XEXCEPTION(eslESYS, "pthread_mutex_lock failed on go_mutex"); |
| 1068 |
while (! dd->go) { |
| 1069 |
if ( pthread_cond_wait(&dd->go_cv, &dd->go_mutex) != 0) ESL_XEXCEPTION(eslESYS, "pthread_cond_wait failed on go_cv"); |
| 1070 |
} |
| 1071 |
if ( pthread_mutex_unlock(&dd->go_mutex) != 0) ESL_XEXCEPTION(eslESYS, "pthread_mutex_lock failed on go_mutex"); |
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
for (u = 0; u < dd->n_unpackers; u++) |
| 1079 |
if ( pthread_equal(my_id, dd->unpacker_t[u] )) break; |
| 1080 |
if (u >= dd->n_unpackers) esl_fatal("unpacker failed to figure out its identity"); |
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
do { |
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
if ( pthread_mutex_lock(&(dd->inbox_mutex[u])) != 0) ESL_XEXCEPTION(eslESYS, "pthread mutex lock failed"); |
| 1090 |
while (! dd->inbox_eod[u] && dd->inbox[u] == NULL) { |
| 1091 |
if ( pthread_cond_wait(&(dd->inbox_cv[u]), &(dd->inbox_mutex[u])) != 0) ESL_XEXCEPTION(eslESYS, "pthread cond wait failed"); |
| 1092 |
} |
| 1093 |
chu = dd->inbox[u]; |
| 1094 |
dd->inbox[u] = NULL; |
| 1095 |
if ( pthread_mutex_unlock(&(dd->inbox_mutex[u])) != 0) ESL_XEXCEPTION(eslESYS, "pthread mutex unlock failed"); |
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
if (chu) { |
| 1101 |
|
| 1102 |
if ( pthread_cond_signal(&(dd->inbox_cv[u])) != 0) ESL_XEXCEPTION(eslESYS, "pthread cond signal failed"); |
| 1103 |
|
| 1104 |
if (( status = dsqdata_unpack_chunk(chu, dd->pack5)) != eslOK) goto ERROR; |
| 1105 |
} |
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
if ( pthread_mutex_lock(&(dd->outbox_mutex[u])) != 0) ESL_XEXCEPTION(eslESYS, "pthread mutex lock failed"); |
| 1111 |
while (dd->outbox[u] != NULL) { |
| 1112 |
if ( pthread_cond_wait(&(dd->outbox_cv[u]), &(dd->outbox_mutex[u])) != 0) ESL_XEXCEPTION(eslESYS, "pthread cond wait failed"); |
| 1113 |
} |
| 1114 |
dd->outbox[u] = chu; |
| 1115 |
if (! chu) dd->outbox_eod[u] = TRUE; |
| 1116 |
if ( pthread_mutex_unlock(&(dd->outbox_mutex[u])) != 0) ESL_XEXCEPTION(eslESYS, "pthread mutex unlock failed"); |
| 1117 |
if ( pthread_cond_signal(&(dd->outbox_cv[u])) != 0) ESL_XEXCEPTION(eslESYS, "pthread cond signal failed"); |
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
} while (chu); |
| 1122 |
pthread_exit(NULL); |
| 1123 |
|
| 1124 |
ERROR: |
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
esl_fatal(" ... dsqdata unpacker thread failed: unrecoverable"); |
| 1130 |
} |
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
static int |
| 1149 |
dsqdata_unpack_chunk(ESL_DSQDATA_CHUNK *chu, int do_pack5) |
| 1150 |
{ |
| 1151 |
char *ptr = chu->metadata; |
| 1152 |
int r; |
| 1153 |
int i; |
| 1154 |
int pos; |
| 1155 |
int L; |
| 1156 |
int P; |
| 1157 |
|
| 1158 |
|
| 1159 |
for (i = 0; i < chu->N; i++) |
| 1160 |
{ |
| 1161 |
|
| 1162 |
if ( ptr >= chu->metadata + chu->mdalloc) ESL_EXCEPTION(eslEFORMAT, "metadata format error"); |
| 1163 |
chu->name[i] = ptr; ptr = 1 + strchr(ptr, '\0'); if ( ptr >= chu->metadata + chu->mdalloc) ESL_EXCEPTION(eslEFORMAT, "metadata format error"); |
| 1164 |
chu->acc[i] = ptr; ptr = 1 + strchr(ptr, '\0'); if ( ptr >= chu->metadata + chu->mdalloc) ESL_EXCEPTION(eslEFORMAT, "metadata format error"); |
| 1165 |
chu->desc[i] = ptr; ptr = 1 + strchr(ptr, '\0'); if ( ptr >= chu->metadata + chu->mdalloc) ESL_EXCEPTION(eslEFORMAT, "metadata format error"); |
| 1166 |
chu->taxid[i] = (int32_t) *((int32_t *) ptr); ptr += sizeof(int32_t); |
| 1167 |
} |
| 1168 |
|
| 1169 |
|
| 1170 |
i = 0; |
| 1171 |
r = 0; |
| 1172 |
pos = 0; |
| 1173 |
chu->smem[0] = eslDSQ_SENTINEL; |
| 1174 |
while (pos < chu->pn) |
| 1175 |
{ |
| 1176 |
chu->dsq[i] = (ESL_DSQ *) chu->smem + r; |
| 1177 |
if (do_pack5) dsqdata_unpack5(chu->psq + pos, chu->dsq[i], &L, &P); |
| 1178 |
else dsqdata_unpack2(chu->psq + pos, chu->dsq[i], &L, &P); |
| 1179 |
|
| 1180 |
r += L+1; |
| 1181 |
pos += P; |
| 1182 |
chu->L[i] = L; |
| 1183 |
i++; |
| 1184 |
} |
| 1185 |
|
| 1186 |
ESL_DASSERT1(( pos == chu->pn )); |
| 1187 |
ESL_DASSERT1(( i == chu->N )); |
| 1188 |
return eslOK; |
| 1189 |
} |
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
static int |
| 1198 |
dsqdata_unpack5(uint32_t *psq, ESL_DSQ *dsq, int *ret_L, int *ret_P) |
| 1199 |
{ |
| 1200 |
int pos = 0; |
| 1201 |
int r = 1; |
| 1202 |
uint32_t v = psq[pos++]; |
| 1203 |
int b; |
| 1204 |
|
| 1205 |
while (! ESL_DSQDATA_EOD(v)) |
| 1206 |
{ |
| 1207 |
ESL_DASSERT1(( ESL_DSQDATA_5BIT(v) )); |
| 1208 |
dsq[r++] = (v >> 25) & 31; dsq[r++] = (v >> 20) & 31; dsq[r++] = (v >> 15) & 31; |
| 1209 |
dsq[r++] = (v >> 10) & 31; dsq[r++] = (v >> 5) & 31; dsq[r++] = (v >> 0) & 31; |
| 1210 |
v = psq[pos++]; |
| 1211 |
} |
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
ESL_DASSERT1(( ESL_DSQDATA_5BIT(v) )); |
| 1217 |
for (b = 25; b >= 0 && ((v >> b) & 31) != 31; b -= 5) |
| 1218 |
dsq[r++] = (v >> b) & 31; |
| 1219 |
dsq[r++] = eslDSQ_SENTINEL; |
| 1220 |
|
| 1221 |
|
| 1222 |
*ret_L = r-2; |
| 1223 |
*ret_P = pos; |
| 1224 |
return eslOK; |
| 1225 |
} |
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
static int |
| 1235 |
dsqdata_unpack2(uint32_t *psq, ESL_DSQ *dsq, int *ret_L, int *ret_P) |
| 1236 |
{ |
| 1237 |
int pos = 0; |
| 1238 |
int r = 1; |
| 1239 |
uint32_t v = psq[pos++]; |
| 1240 |
int b; |
| 1241 |
|
| 1242 |
while (! ESL_DSQDATA_EOD(v)) |
| 1243 |
{ |
| 1244 |
if ( ESL_DSQDATA_5BIT(v)) |
| 1245 |
{ |
| 1246 |
dsq[r++] = (v >> 25) & 31; dsq[r++] = (v >> 20) & 31; dsq[r++] = (v >> 15) & 31; |
| 1247 |
dsq[r++] = (v >> 10) & 31; dsq[r++] = (v >> 5) & 31; dsq[r++] = (v >> 0) & 31; |
| 1248 |
} |
| 1249 |
else |
| 1250 |
{ |
| 1251 |
dsq[r++] = (v >> 28) & 3; dsq[r++] = (v >> 26) & 3; dsq[r++] = (v >> 24) & 3; |
| 1252 |
dsq[r++] = (v >> 22) & 3; dsq[r++] = (v >> 20) & 3; dsq[r++] = (v >> 18) & 3; |
| 1253 |
dsq[r++] = (v >> 16) & 3; dsq[r++] = (v >> 14) & 3; dsq[r++] = (v >> 12) & 3; |
| 1254 |
dsq[r++] = (v >> 10) & 3; dsq[r++] = (v >> 8) & 3; dsq[r++] = (v >> 6) & 3; |
| 1255 |
dsq[r++] = (v >> 4) & 3; dsq[r++] = (v >> 2) & 3; dsq[r++] = (v >> 0) & 3; |
| 1256 |
} |
| 1257 |
v = psq[pos++]; |
| 1258 |
} |
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
if ( ESL_DSQDATA_5BIT(v)) |
| 1264 |
{ |
| 1265 |
for (b = 25; b >= 0 && ((v >> b) & 31) != 31; b -= 5) |
| 1266 |
dsq[r++] = (v >> b) & 31; |
| 1267 |
} |
| 1268 |
else |
| 1269 |
{ |
| 1270 |
dsq[r++] = (v >> 28) & 3; dsq[r++] = (v >> 26) & 3; dsq[r++] = (v >> 24) & 3; |
| 1271 |
dsq[r++] = (v >> 22) & 3; dsq[r++] = (v >> 20) & 3; dsq[r++] = (v >> 18) & 3; |
| 1272 |
dsq[r++] = (v >> 16) & 3; dsq[r++] = (v >> 14) & 3; dsq[r++] = (v >> 12) & 3; |
| 1273 |
dsq[r++] = (v >> 10) & 3; dsq[r++] = (v >> 8) & 3; dsq[r++] = (v >> 6) & 3; |
| 1274 |
dsq[r++] = (v >> 4) & 3; dsq[r++] = (v >> 2) & 3; dsq[r++] = (v >> 0) & 3; |
| 1275 |
} |
| 1276 |
dsq[r++] = eslDSQ_SENTINEL; |
| 1277 |
|
| 1278 |
*ret_L = r-2; |
| 1279 |
*ret_P = pos; |
| 1280 |
return eslOK; |
| 1281 |
} |
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
static int |
| 1297 |
dsqdata_pack5(ESL_DSQ *dsq, int n, uint32_t *psq, int *ret_P) |
| 1298 |
{ |
| 1299 |
int r = 1; |
| 1300 |
int pos = 0; |
| 1301 |
int b; |
| 1302 |
uint32_t v; |
| 1303 |
|
| 1304 |
while (r <= n) |
| 1305 |
{ |
| 1306 |
v = eslDSQDATA_5BIT; |
| 1307 |
for (b = 25; b >= 0 && r <= n; b -= 5) v |= (uint32_t) dsq[r++] << b; |
| 1308 |
for ( ; b >= 0; b -= 5) v |= (uint32_t) 31 << b; |
| 1309 |
|
| 1310 |
if (r > n) v |= eslDSQDATA_EOD; |
| 1311 |
psq[pos++] = v; |
| 1312 |
} |
| 1313 |
|
| 1314 |
|
| 1315 |
if (pos == 0) { v = 0; psq[pos++] = ~v; } |
| 1316 |
|
| 1317 |
*ret_P = pos; |
| 1318 |
return eslOK; |
| 1319 |
} |
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
static int |
| 1337 |
dsqdata_pack2(ESL_DSQ *dsq, int n, uint32_t *psq, int *ret_P) |
| 1338 |
{ |
| 1339 |
int pos = 0; |
| 1340 |
int d = 0; |
| 1341 |
int r = 1; |
| 1342 |
int b; |
| 1343 |
uint32_t v; |
| 1344 |
|
| 1345 |
while (r <= n) |
| 1346 |
{ |
| 1347 |
|
| 1348 |
if (d < r) |
| 1349 |
for (d = r; d <= n; d++) |
| 1350 |
if (dsq[d] > 3) break; |
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
if (n-r+1 >= 15 && d > r+14) |
| 1355 |
{ |
| 1356 |
v = 0; |
| 1357 |
for (b = 28; b >= 0; b -=2) v |= (uint32_t) dsq[r++] << b; |
| 1358 |
} |
| 1359 |
else |
| 1360 |
{ |
| 1361 |
v = eslDSQDATA_5BIT; |
| 1362 |
for (b = 25; b >= 0 && r <= n; b -= 5) v |= (uint32_t) dsq[r++] << b; |
| 1363 |
for ( ; b >= 0; b -= 5) v |= (uint32_t) 31 << b; |
| 1364 |
} |
| 1365 |
|
| 1366 |
if (r > n) v |= eslDSQDATA_EOD; |
| 1367 |
psq[pos++] = v; |
| 1368 |
} |
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
if (pos == 0) { v = 0; psq[pos++] = ~v; } |
| 1374 |
|
| 1375 |
*ret_P = pos; |
| 1376 |
return eslOK; |
| 1377 |
} |
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
#ifdef eslDSQDATA_TESTDRIVE |
| 1490 |
|
| 1491 |
#include "esl_randomseq.h" |
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
static void |
| 1497 |
utest_packing(ESL_RANDOMNESS *rng, ESL_ALPHABET *abc, int nsamples) |
| 1498 |
{ |
| 1499 |
char msg[] = "esl_dsqdata :: packing unit test failed"; |
| 1500 |
ESL_DSQ *dsq = NULL; |
| 1501 |
uint32_t *psq = NULL; |
| 1502 |
ESL_DSQ *dsq2 = NULL; |
| 1503 |
int L_max = 46; |
| 1504 |
int P_max = ESL_MAX(1, (L_max + 5) / 6); |
| 1505 |
int L, P, L2, P2; |
| 1506 |
int i; |
| 1507 |
|
| 1508 |
if ((dsq = malloc(sizeof(ESL_DSQ) * (L_max + 2))) == NULL) esl_fatal(msg); |
| 1509 |
if ((psq = malloc(sizeof(uint32_t) * P_max)) == NULL) esl_fatal(msg); |
| 1510 |
if ((dsq2 = malloc(sizeof(ESL_DSQ) * (L_max + 2))) == NULL) esl_fatal(msg); |
| 1511 |
|
| 1512 |
for (i = 0; i < nsamples; i++) |
| 1513 |
{ |
| 1514 |
L = esl_rnd_Roll(rng, L_max+1); |
| 1515 |
|
| 1516 |
esl_rsq_SampleDirty(rng, abc, NULL, L, dsq); |
| 1517 |
|
| 1518 |
if (abc->type == eslAMINO) { if ( dsqdata_pack5(dsq, L, psq, &P) != eslOK) esl_fatal(msg); } |
| 1519 |
else { if ( dsqdata_pack2(dsq, L, psq, &P) != eslOK) esl_fatal(msg); } |
| 1520 |
|
| 1521 |
dsq2[0] = eslDSQ_SENTINEL; |
| 1522 |
if (abc->type == eslAMINO) { if ( dsqdata_unpack5(psq, dsq2, &L2, &P2) != eslOK) esl_fatal(msg); } |
| 1523 |
else { if ( dsqdata_unpack2(psq, dsq2, &L2, &P2) != eslOK) esl_fatal(msg); } |
| 1524 |
|
| 1525 |
if (L2 != L) esl_fatal(msg); |
| 1526 |
if (P2 != P) esl_fatal(msg); |
| 1527 |
if (memcmp((void *) dsq, (void *) dsq2, L+2) != 0) esl_fatal(msg); |
| 1528 |
|
| 1529 |
|
| 1530 |
esl_rnd_mem(rng, (void *) dsq, L_max+2); |
| 1531 |
esl_rnd_mem(rng, (void *) dsq2, L_max+2); |
| 1532 |
esl_rnd_mem(rng, (void *) psq, (sizeof(uint32_t) * P_max)); |
| 1533 |
} |
| 1534 |
|
| 1535 |
free(dsq); |
| 1536 |
free(psq); |
| 1537 |
free(dsq2); |
| 1538 |
} |
| 1539 |
|
| 1540 |
|
| 1541 |
static void |
| 1542 |
utest_readwrite(ESL_RANDOMNESS *rng, ESL_ALPHABET *abc) |
| 1543 |
{ |
| 1544 |
char msg[] = "esl_dsqdata :: readwrite unit test failed"; |
| 1545 |
char tmpfile[16] = "esltmpXXXXXX"; |
| 1546 |
char basename[32]; |
| 1547 |
ESL_SQ **sqarr = NULL; |
| 1548 |
FILE *tmpfp = NULL; |
| 1549 |
ESL_SQFILE *sqfp = NULL; |
| 1550 |
ESL_DSQDATA *dd = NULL; |
| 1551 |
ESL_DSQDATA_CHUNK *chu = NULL; |
| 1552 |
int nseq = 1 + esl_rnd_Roll(rng, 20000); |
| 1553 |
int maxL = 100; |
| 1554 |
int i; |
| 1555 |
int status; |
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
if (( status = esl_tmpfile_named(tmpfile, &tmpfp)) != eslOK) esl_fatal(msg); |
| 1563 |
if (( sqarr = malloc(sizeof(ESL_SQ *) * nseq)) == NULL) esl_fatal(msg); |
| 1564 |
for (i = 0; i < nseq; i++) |
| 1565 |
{ |
| 1566 |
sqarr[i] = NULL; |
| 1567 |
if (( status = esl_sq_Sample(rng, abc, maxL, &(sqarr[i]))) != eslOK) esl_fatal(msg); |
| 1568 |
if (( status = esl_sq_SetAccession(sqarr[i], "")) != eslOK) esl_fatal(msg); |
| 1569 |
if (( status = esl_sqio_Write(tmpfp, sqarr[i], eslSQFILE_FASTA, FALSE)) != eslOK) esl_fatal(msg); |
| 1570 |
} |
| 1571 |
fclose(tmpfp); |
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
if (( status = esl_sqfile_OpenDigital(abc, tmpfile, eslSQFILE_FASTA, NULL, &sqfp)) != eslOK) esl_fatal(msg); |
| 1576 |
if (( snprintf(basename, 32, "%s-db", tmpfile)) <= 0) esl_fatal(msg); |
| 1577 |
if (( status = esl_dsqdata_Write(sqfp, basename, NULL)) != eslOK) esl_fatal(msg); |
| 1578 |
esl_sqfile_Close(sqfp); |
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
if (( status = esl_dsqdata_Open(&abc, basename, 1, &dd)) != eslOK) esl_fatal(msg); |
| 1583 |
while (( status = esl_dsqdata_Read(dd, &chu)) == eslOK) |
| 1584 |
{ |
| 1585 |
for (i = 0; i < chu->N; i++) |
| 1586 |
{ |
| 1587 |
if ( chu->L[i] != sqarr[i+chu->i0]->n ) esl_fatal(msg); |
| 1588 |
if ( memcmp( chu->dsq[i], sqarr[i+chu->i0]->dsq, chu->L[i]) != 0) esl_fatal(msg); |
| 1589 |
if ( strcmp( chu->name[i], sqarr[i+chu->i0]->name) != 0) esl_fatal(msg); |
| 1590 |
|
| 1591 |
if ( strcmp( chu->desc[i], sqarr[i+chu->i0]->desc) != 0) esl_fatal(msg); |
| 1592 |
|
| 1593 |
} |
| 1594 |
esl_dsqdata_Recycle(dd, chu); |
| 1595 |
} |
| 1596 |
if (status != eslEOF) esl_fatal(msg); |
| 1597 |
esl_dsqdata_Close(dd); |
| 1598 |
|
| 1599 |
remove(tmpfile); |
| 1600 |
remove(basename); |
| 1601 |
snprintf(basename, 32, "%s-db.dsqi", tmpfile); remove(basename); |
| 1602 |
snprintf(basename, 32, "%s-db.dsqm", tmpfile); remove(basename); |
| 1603 |
snprintf(basename, 32, "%s-db.dsqs", tmpfile); remove(basename); |
| 1604 |
for (i = 0; i < nseq; i++) esl_sq_Destroy(sqarr[i]); |
| 1605 |
free(sqarr); |
| 1606 |
} |
| 1607 |
#endif |
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
#ifdef eslDSQDATA_TESTDRIVE |
| 1615 |
|
| 1616 |
#include "easel.h" |
| 1617 |
#include "esl_alphabet.h" |
| 1618 |
#include "esl_dsqdata.h" |
| 1619 |
#include "esl_getopts.h" |
| 1620 |
#include "esl_random.h" |
| 1621 |
|
| 1622 |
static ESL_OPTIONS options[] = { |
| 1623 |
|
| 1624 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 1625 |
{ "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 1626 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 1627 |
}; |
| 1628 |
static char usage[] = "[-options]"; |
| 1629 |
static char banner[] = "unit test driver for Easel dsqdata module"; |
| 1630 |
|
| 1631 |
int |
| 1632 |
main(int argc, char **argv) |
| 1633 |
{ |
| 1634 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 1635 |
ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s")); |
| 1636 |
ESL_ALPHABET *amino = esl_alphabet_Create(eslAMINO); |
| 1637 |
ESL_ALPHABET *nucleic = esl_alphabet_Create(eslRNA); |
| 1638 |
int nsamples = 100; |
| 1639 |
|
| 1640 |
fprintf(stderr, "## %s\n", argv[0]); |
| 1641 |
fprintf(stderr, "# rng seed = %" PRIu32 "\n", esl_randomness_GetSeed(rng)); |
| 1642 |
|
| 1643 |
utest_packing(rng, nucleic, nsamples); |
| 1644 |
utest_packing(rng, amino, nsamples); |
| 1645 |
|
| 1646 |
utest_readwrite(rng, nucleic); |
| 1647 |
utest_readwrite(rng, amino); |
| 1648 |
|
| 1649 |
fprintf(stderr, "# status = ok\n"); |
| 1650 |
|
| 1651 |
esl_alphabet_Destroy(amino); |
| 1652 |
esl_alphabet_Destroy(nucleic); |
| 1653 |
esl_randomness_Destroy(rng); |
| 1654 |
esl_getopts_Destroy(go); |
| 1655 |
exit(0); |
| 1656 |
} |
| 1657 |
#endif |
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
#ifdef eslDSQDATA_EXAMPLE2 |
| 1667 |
#include "easel.h" |
| 1668 |
#include "esl_alphabet.h" |
| 1669 |
#include "esl_dsqdata.h" |
| 1670 |
#include "esl_getopts.h" |
| 1671 |
|
| 1672 |
static ESL_OPTIONS options[] = { |
| 1673 |
|
| 1674 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 1675 |
{ "--dna", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "use DNA alphabet", 0 }, |
| 1676 |
{ "--rna", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "use RNA alphabet", 0 }, |
| 1677 |
{ "--amino", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "use protein alphabet", 0 }, |
| 1678 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 1679 |
}; |
| 1680 |
static char usage[] = "[-options] <seqfile_in> <binary seqfile_out>"; |
| 1681 |
static char banner[] = "experimental: create binary database for esl_dsqdata"; |
| 1682 |
|
| 1683 |
int |
| 1684 |
main(int argc, char **argv) |
| 1685 |
{ |
| 1686 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 2, argc, argv, banner, usage); |
| 1687 |
ESL_ALPHABET *abc = NULL; |
| 1688 |
char *infile = esl_opt_GetArg(go, 1); |
| 1689 |
char *basename = esl_opt_GetArg(go, 2); |
| 1690 |
int format = eslSQFILE_UNKNOWN; |
| 1691 |
int alphatype = eslUNKNOWN; |
| 1692 |
ESL_SQFILE *sqfp = NULL; |
| 1693 |
char errbuf[eslERRBUFSIZE]; |
| 1694 |
int status; |
| 1695 |
|
| 1696 |
status = esl_sqfile_Open(infile, format, NULL, &sqfp); |
| 1697 |
if (status == eslENOTFOUND) esl_fatal("No such file."); |
| 1698 |
else if (status == eslEFORMAT) esl_fatal("Format unrecognized."); |
| 1699 |
else if (status != eslOK) esl_fatal("Open failed, code %d.", status); |
| 1700 |
|
| 1701 |
if (esl_opt_GetBoolean(go, "--rna")) alphatype = eslRNA; |
| 1702 |
else if (esl_opt_GetBoolean(go, "--dna")) alphatype = eslDNA; |
| 1703 |
else if (esl_opt_GetBoolean(go, "--amino")) alphatype = eslAMINO; |
| 1704 |
else { |
| 1705 |
status = esl_sqfile_GuessAlphabet(sqfp, &alphatype); |
| 1706 |
if (status == eslENOALPHABET) esl_fatal("Couldn't guess alphabet from first sequence in %s", infile); |
| 1707 |
else if (status == eslEFORMAT) esl_fatal("Parse failed (sequence file %s)\n%s\n", infile, sqfp->get_error(sqfp)); |
| 1708 |
else if (status == eslENODATA) esl_fatal("Sequence file %s contains no data?", infile); |
| 1709 |
else if (status != eslOK) esl_fatal("Failed to guess alphabet (error code %d)\n", status); |
| 1710 |
} |
| 1711 |
abc = esl_alphabet_Create(alphatype); |
| 1712 |
esl_sqfile_SetDigital(sqfp, abc); |
| 1713 |
|
| 1714 |
status = esl_dsqdata_Write(sqfp, basename, errbuf); |
| 1715 |
if (status == eslEWRITE) esl_fatal("Failed to open dsqdata output files:\n %s", errbuf); |
| 1716 |
else if (status == eslEFORMAT) esl_fatal("Parse failed (sequence file %s)\n %s", infile, sqfp->get_error(sqfp)); |
| 1717 |
else if (status != eslOK) esl_fatal("Unexpected error while creating dsqdata file (code %d)\n", status); |
| 1718 |
|
| 1719 |
esl_sqfile_Close(sqfp); |
| 1720 |
esl_alphabet_Destroy(abc); |
| 1721 |
esl_getopts_Destroy(go); |
| 1722 |
return eslOK; |
| 1723 |
} |
| 1724 |
#endif |
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
#ifdef eslDSQDATA_EXAMPLE |
| 1731 |
#include "easel.h" |
| 1732 |
#include "esl_alphabet.h" |
| 1733 |
#include "esl_dsqdata.h" |
| 1734 |
#include "esl_getopts.h" |
| 1735 |
#include "esl_vectorops.h" |
| 1736 |
|
| 1737 |
static ESL_OPTIONS options[] = { |
| 1738 |
|
| 1739 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 1740 |
{ "-c", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "report summary of chunk contents", 0 }, |
| 1741 |
{ "-r", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "report summary of residue counts", 0 }, |
| 1742 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 1743 |
}; |
| 1744 |
|
| 1745 |
static char usage[] = "[-options] <basename>"; |
| 1746 |
static char banner[] = "example of using ESL_DSQDATA to read sequence db"; |
| 1747 |
|
| 1748 |
int |
| 1749 |
main(int argc, char **argv) |
| 1750 |
{ |
| 1751 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 1, argc, argv, banner, usage); |
| 1752 |
ESL_ALPHABET *abc = NULL; |
| 1753 |
char *basename = esl_opt_GetArg(go, 1); |
| 1754 |
int do_summary = esl_opt_GetBoolean(go, "-c"); |
| 1755 |
int do_resct = esl_opt_GetBoolean(go, "-r"); |
| 1756 |
int ncpu = 1; |
| 1757 |
ESL_DSQDATA *dd = NULL; |
| 1758 |
ESL_DSQDATA_CHUNK *chu = NULL; |
| 1759 |
int nchunk = 0; |
| 1760 |
int i; |
| 1761 |
int64_t pos; |
| 1762 |
int64_t ct[128], total; |
| 1763 |
int x; |
| 1764 |
int status; |
| 1765 |
|
| 1766 |
status = esl_dsqdata_Open(&abc, basename, ncpu, &dd); |
| 1767 |
if (status == eslENOTFOUND) esl_fatal("Failed to open dsqdata files:\n %s", dd->errbuf); |
| 1768 |
else if (status == eslEFORMAT) esl_fatal("Format problem in dsqdata files:\n %s", dd->errbuf); |
| 1769 |
else if (status != eslOK) esl_fatal("Unexpected error in opening dsqdata (code %d)", status); |
| 1770 |
|
| 1771 |
for (x = 0; x < 127; x++) ct[x] = 0; |
| 1772 |
|
| 1773 |
if (do_summary) esl_dataheader(stdout, 8, "idx", 4, "nseq", 8, "nres", 7, "npacket", 0); |
| 1774 |
|
| 1775 |
while ((status = esl_dsqdata_Read(dd, &chu)) == eslOK) |
| 1776 |
{ |
| 1777 |
nchunk++; |
| 1778 |
|
| 1779 |
if (do_resct) |
| 1780 |
for (i = 0; i < chu->N; i++) |
| 1781 |
for (pos = 1; pos <= chu->L[i]; pos++) |
| 1782 |
ct[ chu->dsq[i][pos] ]++; |
| 1783 |
|
| 1784 |
if (do_summary) |
| 1785 |
printf("%-8d %4d %8" PRId64 " %7d\n", nchunk, chu->N, esl_vec_LSum(chu->L, chu->N), chu->pn); |
| 1786 |
|
| 1787 |
esl_dsqdata_Recycle(dd, chu); |
| 1788 |
} |
| 1789 |
if (status != eslEOF) esl_fatal("unexpected error %d in reading dsqdata", status); |
| 1790 |
|
| 1791 |
if (do_resct) |
| 1792 |
{ |
| 1793 |
total = 0; |
| 1794 |
for (x = 0; x < abc->Kp; x++) |
| 1795 |
{ |
| 1796 |
printf("%c %" PRId64 "\n", abc->sym[x], ct[x]); |
| 1797 |
total += ct[x]; |
| 1798 |
} |
| 1799 |
printf("Total = %" PRId64 "\n", total); |
| 1800 |
} |
| 1801 |
|
| 1802 |
esl_alphabet_Destroy(abc); |
| 1803 |
esl_dsqdata_Close(dd); |
| 1804 |
esl_getopts_Destroy(go); |
| 1805 |
return 0; |
| 1806 |
} |
| 1807 |
#endif |
| 1808 |
|
| 1809 |
|
| 1810 |
|