File: blast-mgr.c

package info (click to toggle)
ncbi-vdb 3.0.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 41,756 kB
  • sloc: ansic: 310,503; cpp: 57,986; python: 5,353; perl: 3,392; yacc: 2,158; sh: 706; lex: 570; makefile: 421; xml: 4
file content (670 lines) | stat: -rw-r--r-- 17,082 bytes parent folder | download
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
36
37
38
39
40
41
42
43
44
45
46
47
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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*===========================================================================
 *
 *                            PUBLIC DOMAIN NOTICE
 *               National Center for Biotechnology Information
 *
 *  This software/database is a "United States Government Work" under the
 *  terms of the United States Copyright Act.  It was written as part of
 *  the author's official duties as a United States Government employee and
 *  thus cannot be copyrighted.  This software/database is freely available
 *  to the public for use. The National Library of Medicine and the U.S.
 *  Government have not placed any restriction on its use or reproduction.
 *
 *  Although all reasonable efforts have been taken to ensure the accuracy
 *  and reliability of the software and data, the NLM and the U.S.
 *  Government do not and cannot warrant the performance or results that
 *  may be obtained by using this software or data. The NLM and the U.S.
 *  Government disclaim all warranties, express or implied, including
 *  warranties of performance, merchantability or fitness for any particular
 *  purpose.
 *
 *  Please cite the author in any work or product based on this material.
 *
 * ===========================================================================
 *
 */

#include <vdb/extern.h>
#include "blast-mgr.h" /* BTableType */
#include <kdb/manager.h> /* KDBManagerRelease */
#include <kdb/meta.h> /* KMetadataRelease */
#include <kfg/config.h> /* KConfig */
#include <kfs/directory.h> /* KDirectoryRelease */
#include <klib/debug.h> /* KDbgSetString */
#include <klib/log.h> /* KLogLevel */
#include <klib/out.h>  /* KOutHandlerSetStdOut */
#include <klib/refcount.h> /* KRefcount */
#include <klib/status.h> /* STSMSG */
#include <sra/sraschema.h> /* VDBManagerMakeSRASchema */
#include <vdb/database.h> /* VDBManagerOpenDBRead */
#include <vdb/manager.h> /* VDBManager */
#include <vdb/schema.h> /* VSchema */
#include <vdb/table.h> /* VDBManagerOpenTableRead */
#include <vdb/vdb-priv.h> /* VDBManagerOpenKDBManagerRead */

#include <vfs/manager.h> /* VFSManager */
#include <vfs/path.h> /* VPath */
#include <vfs/resolver.h> /* VResolver */

#include <sysalloc.h>
#include <stdio.h> /* fprintf */
#include <string.h> /* memset */

#define TOOLKIT "sratoolkit3_0_2"

/******************************************************************************/

static const char VDB_BLAST_MGR[] = "VdbBlastMgr";

struct VdbBlastMgr {
    KRefcount refcount;
    VSchema *schema;
    const VDBManager *mgr;
    const KDBManager *kmgr;
    VFSManager * vfs;
    KDirectory *dir;
    VResolver *resolver;
};

rc_t _VdbBlastMgrNativeToPosix(const VdbBlastMgr *self,
    const char *native, char *posix, size_t size)
{
    rc_t rc = 0;
    VPath *temp_v_path = NULL;
    assert(self);
    rc = VFSManagerMakeSysPath(self->vfs, &temp_v_path, native);
    if (rc == 0) {
        size_t written = 0;
        rc = VPathReadPath(temp_v_path, posix, size, &written);
    }
    RELEASE(VPath, temp_v_path);
    return rc;
}

static
void _VdbBlastMgrWhack(VdbBlastMgr *self)
{
    assert(self);

    VSchemaRelease(self->schema);
    VDBManagerRelease(self->mgr);
    KDBManagerRelease(self->kmgr);
    VFSManagerRelease ( self -> vfs );
    KDirectoryRelease(self->dir);
    VResolverRelease(self->resolver);

    memset(self, 0, sizeof *self);

    free(self);

    STSMSG(1, ("Deleted VdbBlastMgr"));
}

LIB_EXPORT
VdbBlastMgr* CC VdbBlastInit(uint32_t *status)
{
    VdbBlastMgr *item = NULL;
    rc_t rc = 0;

    uint32_t dummy = eVdbBlastNoErr;
    if (status == NULL)
    {   status = &dummy; }

    *status = eVdbBlastErr;

    if (getenv(TOOLKIT) != NULL) {
#if _DEBUGGING
fprintf(stderr, "DEBUGNG with " TOOLKIT " release\n");
#else
fprintf(stderr, "RELEASE with " TOOLKIT " release\n");
#endif
    }
    item = calloc(1, sizeof *item);
    if (item == NULL) {
        *status = eVdbBlastMemErr;
        return item;
    }

    if ( rc == 0 )
    {
        rc = VFSManagerMake ( & item -> vfs );
        if ( rc != 0 )
            LOGERR ( klogInt, rc, "Error in VFSManagerMake" );
    }

    if (rc == 0) {
        rc = VFSManagerGetCWD( item -> vfs, &item->dir);
        if (rc != 0)
        {   LOGERR(klogInt, rc, "Error in VFSManagerGetCWD"); }
    }

    if (rc == 0) {
        rc = VDBManagerMakeRead(&item->mgr, NULL);
        if (rc != 0)
        {   LOGERR(klogInt, rc, "Error in VDBManagerMakeRead"); }
    }

    if (rc == 0) {
        rc = VDBManagerOpenKDBManagerRead(item->mgr, &item->kmgr);
        if (rc != 0)
        {   LOGERR(klogInt, rc, "Error in VDBManagerOpenKDBManagerRead"); }
    }

    if (rc == 0) {
        rc = VDBManagerMakeSRASchema(item->mgr, &item->schema);
        if (rc != 0)
        {   LOGERR(klogInt, rc, "Error in VDBManagerMakeSRASchema"); }
    }

    if (rc == 0) {
        VFSManager* mgr = NULL;
        KConfig* cfg = NULL;
        if (rc == 0) {
            rc = VFSManagerMake(&mgr);
        }
        if (rc == 0) {
            rc = KConfigMake(&cfg, NULL);
        }
        if (rc == 0) {
            rc = VFSManagerMakeResolver(mgr, &item->resolver, cfg);
        }
        RELEASE(KConfig, cfg);
        RELEASE(VFSManager, mgr);
    }

    if (rc != 0) {
        _VdbBlastMgrWhack(item);
        item = NULL;
        STSMSG(1, ("Error: failed to create VdbBlastMgr"));
    }
    else {
        KRefcountInit(&item->refcount, 1, VDB_BLAST_MGR, __func__, "mgr");
        *status = eVdbBlastNoErr;
        STSMSG(1, ("Created VdbBlastMgr"));
    }

    return item;
}

LIB_EXPORT
VdbBlastMgr* CC VdbBlastMgrAddRef(VdbBlastMgr *self)
{
    if (self == NULL) {
        STSMSG(2, ("VdbBlastMgrAddRef(NULL)"));
        return self;
    }

    if (KRefcountAdd(&self->refcount, VDB_BLAST_MGR) == krefOkay) {
        STSMSG(2, ("VdbBlastMgrAddRef"));
        return self;
    }

    STSMSG(1, ("Error: failed to VdbBlastMgrAddRef"));
    return NULL;
}

LIB_EXPORT
void CC VdbBlastMgrRelease(VdbBlastMgr *self)
{
    if (self == NULL)
    {   return; }

    STSMSG(2, ("VdbBlastMgrRelease"));
    if (KRefcountDrop(&self->refcount, VDB_BLAST_MGR) != krefWhack)
    {   return; }

    _VdbBlastMgrWhack(self);
}


LIB_EXPORT bool CC VdbBlastMgrIsCSraRun(
    const VdbBlastMgr *self, const char *rundesc)
{
    bool csra = false;
    rc_t rc = 0;
    const VDBManager *aMgr = NULL;
    const VDBManager *mgr = NULL;
    const VDatabase *db = NULL;
    if (self != NULL) {
        mgr = self->mgr;
    }
    if (mgr == NULL) {
        rc = VDBManagerMakeRead(&mgr, NULL);
        if (rc != 0) {
            return false;
        }
        if (self != NULL && self->mgr == NULL) {
            ((VdbBlastMgr*)self)->mgr = mgr;
        }
        else {
            aMgr = mgr;
        }
    }
    rc = VDBManagerOpenDBRead(mgr, &db,
        self == NULL ? NULL : self->schema, "%s", rundesc);
    if (rc == 0) {
        csra = VDatabaseIsCSRA(db);
    }
    RELEASE(VDatabase, db);
    RELEASE(VDBManager, aMgr);
    return csra;
}


static bool _VdbBlastMgrSchemaEquals(const VdbBlastMgr *self,
    const VDatabase *db, const char *rundesc,
    const char* name, uint32_t max_chars)
{
    bool equals = false;

    const KMetadata *meta = NULL;
    const KMDataNode *node = NULL;

    char buffer[512] = "";
    size_t size = 0;

    rc_t rc = 0;

    assert(self);

    if (db != NULL) {
        rc = VDatabaseOpenMetadataRead(db, &meta);
    }
    else {
        const VTable *tbl = NULL;
        rc = VDBManagerOpenTableRead(self->mgr, &tbl, self->schema, rundesc);

        if (rc == 0) {
            rc = VTableOpenMetadataRead(tbl, &meta);
        }

        RELEASE(VTable, tbl);
    }

    if (rc == 0) {
        rc = KMetadataOpenNodeRead(meta, &node, "schema");
    }

    if (rc == 0) {
        rc = KMDataNodeReadAttr(node, "name", buffer, sizeof buffer, &size);
    }

    if (rc == 0) {
        STSMSG(1, ("%s.schema@name='%.*s'", rundesc, (uint32_t)size, buffer));

        if (size >= sizeof name && 
            string_cmp(buffer, max_chars, name, max_chars, max_chars)
                == 0)
        {
            equals = true;
        }
    }

    RELEASE(KMDataNode, node);
    RELEASE(KMetadata, meta);

    return equals;
}

BTableType _VdbBlastMgrBTableType(
    const VdbBlastMgr *self, const char *rundesc)
{
    BTableType type = btpSRA;

    rc_t rc = 0;

    const VDatabase *db = NULL;

    assert(self);

    rc = VDBManagerOpenDBRead(self->mgr, &db, NULL, rundesc);

    if (rc == 0) {
        const char name[] = "NCBI:WGS:db:contig";

        if (_VdbBlastMgrSchemaEquals(self, db, rundesc, name, sizeof name - 1))
        {
            STSMSG(1, ("%s is WGS", rundesc));
            type = btpWGS;
        }

        RELEASE(VDatabase, db);
    }
    else {
        const char name[] = "NCBI:refseq:tbl:reference";

        if (_VdbBlastMgrSchemaEquals(self, db, rundesc, name, sizeof name - 1))
        {
            STSMSG(1, ("%s is REFSEQ", rundesc));
            type = btpREFSEQ;
        }
    }

    if (type == btpSRA) {
        STSMSG(1, ("%s is SRA", rundesc));
    }

    return type;
}

static
VdbBlastStatus _VdbBlastMgrOpenSequenceTable(const VdbBlastMgr *self,
    const char *path,
    const VTable **tbl,
    const VDatabase **db)
{
    KPathType type = kptNotFound;
    VSchema *schema = NULL;

    assert(self && tbl && db);
    *tbl = NULL;

    /* Always use VDBManagerMakeSRASchema to VDBManagerOpenTableRead
       Otherwise CMP_BASE_COUNT column sometimes cannot be found */
    schema = self->schema;

    type = KDBManagerPathType(self->kmgr, "%s", path);
    if (type == kptNotFound) {
        STSMSG(1, ("Error: cannot find '%s'", path));
        return eVdbBlastErr;
    }

    if ((type & ~kptAlias) == kptDatabase) {
        const char *table = "SEQUENCE";
        rc_t rc = VDBManagerOpenDBRead(self->mgr, db, NULL, "%s", path);
        if (rc != 0) {
            PLOGERR(klogInt, (klogInt, rc,
                "Error in VDBManagerOpenDBRead($(name))", "name=%s", path));
            STSMSG(1, ("Error: failed to open DB '%s'", path));
            return eVdbBlastErr;
        }
        assert(*db);
        rc = VDatabaseOpenTableRead(*db, tbl, table);
        if (rc != 0) {
            PLOGERR(klogInt, (klogInt, rc,
                "Error in VDatabaseOpenTableRead($(name), $(tbl))",
                "name=%s,tbl=%s", path, table));
            STSMSG(1, ("Error: failed to open DB table '%s/%s'", path, table));
        }
        else {
            STSMSG(1, ("Opened DB table '%s/%s'", path, table));
        }

        return rc != 0 ? eVdbBlastErr : eVdbBlastNoErr;
    }

    while (true) {
        rc_t rc = VDBManagerOpenTableRead(self->mgr, tbl, schema, "%s", path);
        if (rc == 0) {
            if (schema == NULL) {
                STSMSG(1, ("Opened table '%s'(schema=NULL)", path));
            }
            else {
                STSMSG(1, ("Opened table '%s'(SRASchema)", path));
            }
            return eVdbBlastNoErr;
        }

        assert(self->schema);

        if (schema == NULL)
        {   schema = self->schema; }
        else {
            PLOGERR(klogInt, (klogInt, rc,
                "Error in VDBManagerOpenTableRead($(name))", "name=%s", path));
            STSMSG(1, ("Error: failed to open table '%s'", path));
            return eVdbBlastRunErr;
        }
    }
}

VdbBlastStatus _VdbBlastMgrFindNOpenSeqTable(const VdbBlastMgr *self,
    const char *rundesc,
    const VTable **tbl,
    BTableType *type,
    char **fullpath,
    const VDatabase **db)
{
    VdbBlastStatus status = eVdbBlastNoErr;
    char *path = (char*) rundesc;

    assert(self && type);

    status = _VdbBlastMgrOpenSequenceTable(self, path, tbl, db);
    if (status == eVdbBlastNoErr) {
        STSMSG(1, ("Added run %s(%s)", rundesc, path));
    }
    else {
        STSMSG(1, ("Error: failed to add run %s(%s)", rundesc, path));
    }

    *type = _VdbBlastMgrBTableType(self, path);

    if (path != rundesc) {
        free(path);
        path = NULL;
    }

    return status;
}

/******************************************************************************/

/* KConfigPrint
 * print current configuration to the output handler
 */
LIB_EXPORT uint32_t VdbBlastMgrKConfigPrint(const VdbBlastMgr *self) {
    KConfig *kfg = NULL;

    rc_t rc = KConfigMake(&kfg, NULL);

    if (rc == 0) {
        rc = KConfigPrint(kfg, 0);
    }

    {
        rc_t rc2 = KConfigRelease(kfg);
        if (rc == 0 && rc2 != 0) {
            rc = rc2;
        }
    }

    if (rc != 0) {
        LOGERR(klogInt, rc, "An error occurred when printing Configiration");
        return eVdbBlastErr;
    }

    return eVdbBlastNoErr;
}

LIB_EXPORT uint32_t CC VdbBlastMgrKDbgSetString(const VdbBlastMgr *self,
    const char *string)
{
    rc_t rc = KDbgSetString(string);

    if (rc != 0) {
        LOGERR(klogInt, rc, "An error occurred when setting KDbgString");
        return eVdbBlastErr;
    }

    return eVdbBlastNoErr;
}

LIB_EXPORT uint32_t CC VdbBlastMgrKLogHandlerSet(const VdbBlastMgr *self,
    KWrtWriter writer, void *data)
{
    rc_t rc = KLogHandlerSet(writer, data);

    if (rc != 0) {
        LOGERR(klogInt, rc, "An error occurred when setting KLogHandler");
        return eVdbBlastErr;
    }

    return eVdbBlastNoErr;
}

LIB_EXPORT uint32_t CC VdbBlastMgrKLogHandlerSetStdErr(
    const VdbBlastMgr *self)
{
    rc_t rc = KLogHandlerSetStdErr();

    if (rc != 0) {
        LOGERR(
            klogInt, rc, "An error occurred when setting KLogHandler to StdErr");
        return eVdbBlastErr;
    }

    return eVdbBlastNoErr;
}

LIB_EXPORT uint32_t CC VdbBlastMgrKLogHandlerSetStdOut(
    const VdbBlastMgr *self)
{
    rc_t rc = KLogHandlerSetStdOut();

    if (rc != 0) {
        LOGERR(
            klogInt, rc, "An error occurred when setting KLogHandler to StdOut");
        return eVdbBlastErr;
    }

    return eVdbBlastNoErr;
}

LIB_EXPORT uint32_t CC VdbBlastMgrKLogLibHandlerSet(const VdbBlastMgr *self,
    KWrtWriter writer, void *data)
{
    rc_t rc = KLogLibHandlerSet(writer, data);

    if (rc != 0) {
        LOGERR(klogInt, rc, "An error occurred when setting KLogLibHandler");
        return eVdbBlastErr;
    }

    return eVdbBlastNoErr;
}

LIB_EXPORT
uint32_t CC VdbBlastMgrKLogLibHandlerSetStdErr(const VdbBlastMgr *self)
{
    rc_t rc = KLogLibHandlerSetStdErr();

    if (rc != 0) {
        LOGERR(klogInt, rc,
            "An error occurred when setting KLogLibHandler to StdErr");
        return eVdbBlastErr;
    }

    return eVdbBlastNoErr;
}

LIB_EXPORT
uint32_t CC VdbBlastMgrKLogLibHandlerSetStdOut(const VdbBlastMgr *self)
{
    rc_t rc = KLogLibHandlerSetStdOut();

    if (rc != 0) {
        LOGERR(klogInt, rc,
            "An error occurred when setting KLogLibHandler to StdOut");
        return eVdbBlastErr;
    }

    return eVdbBlastNoErr;
}

LIB_EXPORT uint32_t CC VdbBlastMgrKOutHandlerSet(const VdbBlastMgr *self,
    VdbBlastKWrtWriter writer, void *data)
{
    rc_t rc = KOutHandlerSet(writer, data);

    if (rc != 0) {
        LOGERR(klogInt, rc, "An error occurred when setting KOutHandler");
        return eVdbBlastErr;
    }

    return eVdbBlastNoErr;
}

LIB_EXPORT
uint32_t CC VdbBlastMgrKOutHandlerSetStdErr(const VdbBlastMgr *self)
{
    rc_t rc = KOutHandlerSetStdErr();

    if (rc != 0) {
        LOGERR(
            klogInt, rc, "An error occurred when setting KOutHandler to StdErr");
        return eVdbBlastErr;
    }

    return eVdbBlastNoErr;
}

LIB_EXPORT
uint32_t CC VdbBlastMgrKOutHandlerSetStdOut(const VdbBlastMgr *self)
{
    rc_t rc = KOutHandlerSetStdOut();

    if (rc != 0) {
        LOGERR(
            klogInt, rc, "An error occurred when setting KOutHandler to StdOut");
        return eVdbBlastErr;
    }

    return eVdbBlastNoErr;
}

LIB_EXPORT
uint32_t CC VdbBlastMgrKStsHandlerSetStdErr(const VdbBlastMgr *self)
{
    rc_t rc = KStsHandlerSetStdErr();

    if (rc != 0) {
        LOGERR(
            klogInt, rc, "An error occurred when setting KStsHandler to StdErr");
        return eVdbBlastErr;
    }

    return eVdbBlastNoErr;
}

LIB_EXPORT
uint32_t CC VdbBlastMgrKStsHandlerSetStdOut(const VdbBlastMgr *self)
{
    rc_t rc = KStsHandlerSetStdOut();

    if (rc != 0) {
        LOGERR(
            klogInt, rc, "An error occurred when setting KStsHandler to StdOut");
        return eVdbBlastErr;
    }

    return eVdbBlastNoErr;
}

LIB_EXPORT uint32_t CC VdbBlastMgrKLogLevelSet(const VdbBlastMgr *self,
    KLogLevel lvl)
{
    rc_t rc = KLogLevelSet(lvl);

    if (rc != 0) {
        LOGERR(klogInt, rc, "An error occurred when setting KLogLevel");
        return eVdbBlastErr;
    }

    return eVdbBlastNoErr;
}

LIB_EXPORT uint32_t CC VdbBlastMgrKLogLevelSetInfo(
    const VdbBlastMgr *self)
{   return VdbBlastMgrKLogLevelSet(self, klogInfo); }

LIB_EXPORT uint32_t CC VdbBlastMgrKLogLevelSetWarn(
    const VdbBlastMgr *self)
{   return VdbBlastMgrKLogLevelSet(self, klogWarn); }

LIB_EXPORT VdbBlastStatus CC VdbBlastMgrKStsLevelSet(const VdbBlastMgr *self,
    uint32_t level)
{
    KStsLevelSet(level);
    return eVdbBlastNoErr; 
}