root/branches/stable_5.0/AWTC/AWTC_next_neighbours.cxx

Revision 6101, 6.8 KB (checked in by westram, 3 years ago)
  • fix warning "format not a string literal and no format arguments"
    • GB_warning -> GB_warning/GB_warningf
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <arbdb.h>
2#include <arbdbt.h>
3
4#include <servercntrl.h>
5#include <PT_com.h>
6#include <client.h>
7#include <aw_root.hxx>
8#include <aw_window.hxx>
9
10#include <awtc_next_neighbours.hxx>
11
12#include <cstdio>
13#include <cstdlib>
14#include <cstring>
15#include <climits>
16
17
18AWTC_FIND_FAMILY_MEMBER::AWTC_FIND_FAMILY_MEMBER(){
19    matches = 0;
20    name = 0;
21    next = 0;
22}
23
24AWTC_FIND_FAMILY_MEMBER::~AWTC_FIND_FAMILY_MEMBER(){
25    free(name);
26}
27
28void awtc_ff_message(const char *msg){
29    GB_warning(msg);
30}
31
32void AWTC_FIND_FAMILY::delete_family_list(){
33    AWTC_FIND_FAMILY_MEMBER *fl,*fln;
34    for (fl = family_list; fl ; fl = fln){
35         fln = fl->next;
36         delete fl;
37    }
38    family_list = 0;
39}
40
41GB_ERROR AWTC_FIND_FAMILY::init_communication(void)
42{
43    const char *user = "Find Family";
44
45/*** create and init local com structure ***/
46    if( aisc_create(link, PT_MAIN, com,
47                    MAIN_LOCS, PT_LOCS, &locs,
48                    LOCS_USER, user,
49                    NULL)){
50        return GB_export_error("Cannot initialize communication");
51    }
52    return 0;
53}
54
55
56GB_ERROR AWTC_FIND_FAMILY::open(char *servername) {
57    GB_ERROR error = 0;
58    if (arb_look_and_start_server(AISC_MAGIC_NUMBER,servername,gb_main)){
59        error = "Cannot contact PT  server";
60    }
61    else {
62        const char *socketid = GBS_read_arb_tcp(servername);
63        if (!socketid) error = GB_await_error();
64        else {
65            link = (aisc_com *)aisc_open(socketid, &com, AISC_MAGIC_NUMBER);
66            if (!link) error = "Cannot contact PT server [1]";
67            else if (init_communication()) error = "Cannot contact PT server [2]";
68        }
69    }
70    return error;
71}
72
73void AWTC_FIND_FAMILY::close(void)
74{
75   if (link) aisc_close(link);
76   link = 0;
77}
78
79AWTC_FIND_FAMILY::AWTC_FIND_FAMILY(GBDATA *gb_maini)
80{
81    memset((char *)this,0,sizeof(*this));
82    this->gb_main = gb_maini;
83}
84
85AWTC_FIND_FAMILY::~AWTC_FIND_FAMILY(void)
86{
87    delete_family_list();
88    close();
89}
90
91GB_ERROR AWTC_FIND_FAMILY::retrieve_family(char *sequence, int oligo_len, int mismatches, bool fast_flag, bool rel_matches, FF_complement compl_mode, int max_results) {
92    T_PT_FAMILYLIST f_list;
93    char *compressed_sequence = GB_command_interpreter(gb_main, sequence, "|keep(acgtunACGTUN)", 0, 0);
94
95    bytestring bs;
96    bs.data = compressed_sequence;
97    bs.size = strlen(bs.data)+1;
98
99    delete_family_list();
100    /*
101     * Start find_family() at the PT_SERVER
102     *
103     * Here we have to make a loop, until the match count of the
104     * first member is big enough
105     *
106     */
107
108    if (aisc_put(link, PT_LOCS, locs,
109                 LOCS_FF_PROBE_LEN,             oligo_len,        // oligo length (12 hardcoded till July 2008)
110                 LOCS_FF_MISMATCH_NUMBER,       mismatches,       // number of mismatches (0 hardcoded till July 2008)
111                 LOCS_FF_FIND_TYPE,             int(fast_flag),   // 0: complete search, 1: quick search (only search oligos starting with 'A')
112                 LOCS_FF_SORT_TYPE,             int(rel_matches), // 0: matches, 1: relative matches (0 hardcoded till July 2008)
113                 LOCS_FF_SORT_MAX,              max_results,      // speed up family sorting (only sort retrieved results)
114                 LOCS_FF_COMPLEMENT,            compl_mode,       // any combination of: 1 = forward, 2 = reverse, 4 = reverse-complement, 8 = complement (1 hardcoded in PT-Server till July 2008)
115                 LOCS_FF_FIND_FAMILY,           &bs,
116                 NULL))
117    {
118        return GB_export_error  ("Communication Error (2)");
119    }
120
121    /*
122     * Read family list
123     */
124    aisc_get(link, PT_LOCS, locs,
125             LOCS_FF_FAMILY_LIST, &f_list,
126             LOCS_FF_FAMILY_LIST_SIZE, &real_hits,
127             NULL);
128
129    hits_truncated = false;
130    if (max_results<1) max_results = INT_MAX; 
131
132    AWTC_FIND_FAMILY_MEMBER *tail = NULL;
133    while (f_list){
134        if (max_results == 0) {
135            hits_truncated = true;
136            break;
137        }
138        max_results--;
139
140        AWTC_FIND_FAMILY_MEMBER *fl = new AWTC_FIND_FAMILY_MEMBER();
141
142        (tail ? tail->next : family_list) = fl;
143        tail                              = fl;
144        fl->next                          = NULL;
145
146        aisc_get(link, PT_FAMILYLIST, f_list,
147                 FAMILYLIST_NAME,        &fl->name,
148                 FAMILYLIST_MATCHES,     &fl->matches,
149                 FAMILYLIST_REL_MATCHES, &fl->rel_matches,
150                 FAMILYLIST_NEXT,        &f_list,
151                 NULL);
152    }
153
154    free(compressed_sequence);
155    return 0;
156}
157
158void AWTC_FIND_FAMILY::print(){
159    AWTC_FIND_FAMILY_MEMBER *fl;
160    for (fl = family_list; fl ; fl = fl->next){
161        printf("%s %li\n",fl->name,fl->matches);
162    }
163}
164
165GB_ERROR AWTC_FIND_FAMILY::findFamily(int server_id, char *sequence, int oligo_len, int mismatches, bool fast_flag, bool rel_matches, FF_complement compl_mode, int max_results) {
166    // searches the PT-server for related species.
167    //
168    // relation-score is calculated by fragmenting the sequence into oligos of length 'oligo_len' and
169    // then summarizing the number of hits.
170    //
171    // 'mismatches'  = the number of allowed mismatches
172    // 'fast_flag'   = 0 -> do complete search, 1 -> search only oligos starting with 'A'
173    // 'rel_matches' = 0 -> score is number of oligo-hits, 1 -> score is relative to longer sequence (target or source) * 10
174    //
175    // 'max_results' limits the length of the generated result list (low scores deleted first)
176    //               if < 1 -> don't limit
177
178    char     *buffer = GBS_global_string_copy("ARB_PT_SERVER%i", server_id);
179    GB_ERROR  error  = open(buffer);
180
181    if (!error) {
182        error = init_communication();
183        if (!error) {
184            error = retrieve_family(sequence, oligo_len, mismatches, fast_flag, rel_matches, compl_mode, max_results);
185        }
186    }
187    close();
188    return error;
189}
190
191
192void AWTC_create_common_next_neighbour_vars(AW_root *aw_root) {
193    static bool created = false;
194    if (!created) {
195        aw_root->awar_int(AWAR_NN_OLIGO_LEN,   12);
196        aw_root->awar_int(AWAR_NN_MISMATCHES,  0);
197        aw_root->awar_int(AWAR_NN_FAST_MODE,   0);
198        aw_root->awar_int(AWAR_NN_REL_MATCHES, 1);
199
200        created = true;
201    }
202}
203
204void AWTC_create_common_next_neighbour_fields(AW_window *aws) {
205    // used in several figs: ad_spec_nn.fig ad_spec_nnm.fig awtc/family_settings.fig
206   
207    aws->at("oligo_len");
208    aws->create_input_field(AWAR_NN_OLIGO_LEN, 3);
209   
210    aws->at("mismatches");
211    aws->create_input_field(AWAR_NN_MISMATCHES, 3);
212
213    aws->at("mode");
214    aws->create_option_menu(AWAR_NN_FAST_MODE, 0, 0);
215    aws->insert_default_option("Complete", "", 0);
216    aws->insert_option("Quick", "", 1);
217    aws->update_option_menu();
218
219    aws->at("score");
220    aws->create_option_menu(AWAR_NN_REL_MATCHES, 0, 0);
221    aws->insert_option("absolute", "", 0);
222    aws->insert_default_option("relative", "", 1);
223    aws->update_option_menu();
224
225}
Note: See TracBrowser for help on using the browser.