source: tags/ms_r16q3/TOOLS/arb_probe_match.cxx

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