File: arb_2_bin.cxx

package info (click to toggle)
arb 6.0.2-1
  • links: PTS, VCS
  • area: non-free
  • in suites: jessie-kfreebsd
  • size: 65,852 kB
  • sloc: ansic: 394,903; cpp: 250,252; makefile: 19,635; sh: 15,878; perl: 10,461; fortran: 6,019; ruby: 683; xml: 503; python: 53; awk: 32
file content (98 lines) | stat: -rw-r--r-- 3,474 bytes parent folder | download | duplicates (6)
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
// =============================================================== //
//                                                                 //
//   File      : arb_2_bin.cxx                                     //
//   Purpose   :                                                   //
//                                                                 //
//   Institute of Microbiology (Technical University Munich)       //
//   http://www.arb-home.de/                                       //
//                                                                 //
// =============================================================== //


#include <arbdbt.h>

int ARB_main(int argc, char *argv[]) {
    GB_ERROR error = 0;

    fprintf(stderr, "arb_2_bin - ARB database ascii to binary converter\n");

    if (argc <= 1 || strcmp(argv[1], "--help") == 0) {
        fprintf(stderr,
                "\n"
                "Purpose: Converts a database to binary format\n"
                "Syntax:  arb_2_bin [Options] database [newdatabase]\n"
                "Options: -m            create map file too\n"
                "         -r            try to repair destroyed database\n"
                "         -c[tree_xxx]  optimize database using tree_xxx or largest tree\n"
                "\n"
                "database my be '-' in which case arb_2_bin reads from stdin.\n"
                "\n"
            );

        if (strcmp(argv[1], "--help") != 0) { error = "Missing arguments"; }
    }
    else {
        char rtype[256];
        char wtype[256];
        int  ci   = 1;
        int  nidx = 0;

        const char *opt_tree = 0;

        {
            char *rtypep = rtype;
            char *wtypep = wtype;

            memset(rtype, 0, 10);
            memset(wtype, 0, 10);
            *(wtypep++) = 'b';
            *(rtypep++) = 'r';
            *(rtypep++) = 'w';

            while (argv[ci][0] == '-' && argv[ci][1] != 0) {
                if (!strcmp(argv[ci], "-m")) { ci++; *(wtypep++) = 'm'; }
                if (!strcmp(argv[ci], "-r")) { ci++; *(rtypep++) = 'R'; }
                if (!strncmp(argv[ci], "-c", 2)) { opt_tree = argv[ci]+2; ci++; }
                if (!strncmp(argv[ci], "-i", 2)) { nidx = atoi(argv[ci]+2); ci++; }
            }
        }

        const char *in  = argv[ci++];
        const char *out = ci >= argc ? in : argv[ci++];

        printf("Reading database...\n");
        GB_shell  shell;
        GBDATA   *gb_main = GBT_open(in, rtype);
        if (!gb_main) {
            error = GB_await_error();
        }
        else {
            if (opt_tree) {
                char *ali_name;
                {
                    GB_transaction ta(gb_main);
                    ali_name = GBT_get_default_alignment(gb_main);
                }
                if (!strlen(opt_tree)) opt_tree = 0;

                printf("Optimizing database...\n");
                error = GBT_compress_sequence_tree2(gb_main, opt_tree, ali_name);
                if (error) error = GBS_global_string("Error during optimize: %s", error);
                free(ali_name);
            }

            if (!error) {
                GB_set_next_main_idx(nidx);
                printf("Saving database...\n");
                error = GB_save(gb_main, out, wtype);
            }
            GB_close(gb_main);
        }
    }

    if (error) {
        fprintf(stderr, "arb_2_bin: Error: %s\n", error);
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}