source: branches/nameserver/TOOLS/arb_probe_match.cxx

Last change on this file was 18125, checked in by westram, 5 years ago
File size: 7.3 KB
Line 
1#include <arbdb.h>
2#include <PT_com.h>
3#include <client.h>
4
5#include <iostream>
6
7using std::cerr;
8using std::endl;
9
10static void help() {
11    cerr <<
12        "Executes a probe match on a running PT server\n"
13        "\n"
14        "Parameters:\n"
15        "\n"
16        " --db <FILE>             ARB file\n"
17        " --port <PORT>           port of PT server\n"
18        " --sequence <SEQUENCE>   probe\n"
19        "\n"
20        " --complementFirst       before matching first complement probe (applied before '--alsoRevCompl')\n"
21        " --alsoRevCompl          also report matches of reverse-complement probe\n"
22        "  (Note: specifying both does report complement plus reverse matches)\n"
23        "\n"
24        " --max-hits <N>          maximum number of hits on db (1,000,000)\n"
25        " --weighted              use weighted matching\n"
26        " --weighted-pos          use weighted matching with pos&strength\n"
27        " --n-matches <N>         consider N occurrences of 'N' as match (2)\n"
28        " --n-match-bound <N>     abover N occurancse of 'N', consider all as mismatch (5)\n"
29        " --mismatches <N>        allowed mismatches (0)\n"
30
31         << endl;
32}
33
34enum SORT_BY_TYPE {
35    SORT_BY_MISMATCHES = 0,
36    SORT_BY_WEIGHTED_MISMATCHES = 1,
37    SORT_BY_WEIGHTED_MISMATCHES_W_POS_AND_STRENGTH = 2
38};
39
40int ARB_main(int argc, char ** argv) {
41    const char*  port            = NULp;
42    const char*  sequence        = NULp;
43    int          alsoRevCompl    = 0;
44    int          complementFirst = 0;
45    int          max_hits        = 1000000;
46    SORT_BY_TYPE sort_by         = SORT_BY_MISMATCHES;
47    int          n_accept        = 2;
48    int          n_limit         = 5;
49    int          max_mismatches  = 0;
50
51    GB_shell shell;
52
53    if (argc<2) {
54        help();
55        return 0;
56    }
57
58    for (int i = 1; i < argc; i++) {
59        int err = 0;
60        if (!strcmp(argv[i], "--help")) {
61            help();
62            return 0;
63        } else if (!strcmp(argv[i], "--alsoRevCompl")) {
64            alsoRevCompl = 1;
65        } else if (!strcmp(argv[i], "--complementFirst")) {
66            complementFirst = 1;
67        } else if (!strcmp(argv[i], "--weighted")) {
68            sort_by = SORT_BY_WEIGHTED_MISMATCHES;
69        } else if (!strcmp(argv[i], "--weighted-pos")) {
70            sort_by = SORT_BY_WEIGHTED_MISMATCHES_W_POS_AND_STRENGTH;
71        } else if (argc > i+1) {
72            if (!strcmp(argv[i], "--port")) {
73                port = argv[++i];
74            } else if (!strcmp(argv[i], "--sequence")) {
75                sequence = argv[++i];
76            } else if (!strcmp(argv[i], "--max-hits")) {
77                max_hits = atoi(argv[++i]);
78            } else if (!strcmp(argv[i], "--n-matches")) {
79                n_accept = atoi(argv[++i]);
80            } else if (!strcmp(argv[i], "--n-match-bound")) {
81                n_limit = atoi(argv[++i]);
82            } else if (!strcmp(argv[i], "--mismatches")) {
83                max_mismatches = atoi(argv[++i]);
84            } else {
85                err = 1;
86            }
87        } else {
88            err = 1;
89        }
90        if (err) {
91            cerr << "Error: Did not understand argument '" << argv[i]
92                 << "'." << endl << endl;
93            help();
94            return 1;
95        }
96    }
97
98    bool err = false;
99
100    if (!port) {
101        cerr << "need '--port' parameter" << endl;
102        err = true;
103    }
104    if (!sequence) {
105        cerr << "need '--sequence' parameter" << endl;
106        err = true;
107    }
108    if (max_mismatches < 0 || max_mismatches > 10) {
109        cerr << "mismatches must be between 0 and 10" << endl;
110        err = true;
111    }
112    if (err) {
113        help();
114        return 1;
115    }
116
117    T_PT_LOCS  locs;
118    T_PT_MAIN  com;
119    GB_ERROR   error = NULp;
120    aisc_com  *link  = aisc_open(port, com, PT::AISC_MAGIC_NUMBER, &error);
121    if (!link) {
122        cerr << "Cannot contact PT server (" << error << ')' << endl;
123        return 1;
124    }
125
126    if (aisc_create(link, PT_MAIN, com,
127                    MAIN_LOCS, PT_LOCS, locs,
128                    NULp)) {
129        cerr << "Cannot create PT LOCS structure" << endl;
130        return 1;
131    }
132
133    if (aisc_nput(link, PT_LOCS, locs,
134                  LOCS_MATCH_ALSO_REVCOMP,   (long)alsoRevCompl,
135                  LOCS_COMPLEMENT_FIRST,     (long)complementFirst,
136                  LOCS_MATCH_MAX_HITS,       (long)max_hits,
137                  LOCS_MATCH_SORT_BY,        (long)sort_by,
138                  LOCS_MATCH_N_ACCEPT,       (long)n_accept,
139                  LOCS_MATCH_N_LIMIT,        (long)n_limit,
140                  LOCS_MATCH_MAX_MISMATCHES, (long)max_mismatches,
141                  LOCS_SEARCHMATCH,          sequence,
142                  NULp)) {
143        cerr << "Connection to PT server lost" << endl;
144        return 1;
145    }
146
147    T_PT_MATCHLIST match_list;
148
149    long match_list_cnt    = 0;
150    long matches_truncated = 0;
151
152    bytestring bs;
153    bs.data = NULp;
154
155    char *locs_error = NULp;
156
157    aisc_get(link, PT_LOCS, locs,
158             LOCS_MATCH_LIST,        match_list.as_result_param(),
159             LOCS_MATCH_LIST_CNT,    &match_list_cnt,
160             LOCS_MATCH_STRING,      &bs,
161             LOCS_MATCHES_TRUNCATED, &matches_truncated,
162             LOCS_ERROR,             &locs_error,
163             NULp);
164    if (locs_error) {
165        cerr << "received error: " << locs_error << endl;
166        free(locs_error);
167    }
168
169    if (matches_truncated) {
170        std::cout << "match list was truncated!" << endl;
171    }
172
173    std::cout << "acc     \t"
174              << "start\t"
175              << "stop\t"
176              << "pos\t"
177              << "mis\t"
178              << "wmis\t"
179              << "nmis\t"
180              << "dt\t"
181              << "rev\t"
182              << "seq"
183              << std::endl;
184
185    while (match_list.exists()) {
186        char *m_acc, *m_sequence;
187        long m_start, m_stop, m_pos, m_mismatches, m_n_mismatches;
188        long m_reversed;
189        double m_wmismatches, m_dt;
190
191        aisc_get(link, PT_MATCHLIST, match_list,
192                 MATCHLIST_FIELD_ACC,     &m_acc,
193                 MATCHLIST_FIELD_START,   &m_start,
194                 MATCHLIST_FIELD_STOP,    &m_stop,
195                 MATCHLIST_POS,           &m_pos,
196                 MATCHLIST_MISMATCHES,    &m_mismatches,
197                 MATCHLIST_WMISMATCHES,   &m_wmismatches,
198                 MATCHLIST_N_MISMATCHES,  &m_n_mismatches,
199                 MATCHLIST_DT,            &m_dt,
200                 MATCHLIST_OVERLAY,       &m_sequence,
201                 MATCHLIST_REVERSED,      &m_reversed,
202                 MATCHLIST_NEXT,          match_list.as_result_param(),
203                 NULp);
204
205        std::cout << m_acc << "\t"
206                  << m_start << "\t"
207                  << m_stop << "\t"
208                  << m_pos << "\t"
209                  << m_mismatches << "\t"
210                  << m_wmismatches << "\t"
211                  << m_n_mismatches << "\t"
212                  << m_dt << "\t"
213                  << m_reversed << "\t"
214                  << m_sequence << "\t"
215                  << std::endl;
216        fflush(stdout);
217
218        free(m_acc);
219        free(m_sequence);
220    }
221
222    free(bs.data);
223    aisc_close(link, com);
224    return 0;
225}
226
227
228/*
229  Local Variables:
230  mode:c++
231  c-file-style:"stroustrup"
232  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . 0))
233  indent-tabs-mode:nil
234  fill-column:99
235  End:
236*/
237// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
Note: See TracBrowser for help on using the repository browser.