source: branches/tree/AWTC/AWTC_next_neighbours.cxx

Last change on this file was 18732, checked in by westram, 3 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.3 KB
Line 
1// ================================================================ //
2//                                                                  //
3//   File      : AWTC_next_neighbours.cxx                           //
4//   Purpose   :                                                    //
5//                                                                  //
6//   Institute of Microbiology (Technical University Munich)        //
7//   http://www.arb-home.de/                                        //
8//                                                                  //
9// ================================================================ //
10
11#include "awtc_next_neighbours.hxx"
12
13#include <servercntrl.h>
14#include <PT_com.h>
15#include <client.h>
16#include <aw_window.hxx>
17#include <aw_root.hxx>
18#include <aw_awar.hxx>
19#include <arbdbt.h>
20#include <arb_strbuf.h>
21
22#include <climits>
23
24struct PT_FF_comImpl {
25    aisc_com  *link;
26    T_PT_MAIN  com;
27    T_PT_LOCS  locs;
28
29    PT_FF_comImpl() :
30        link(NULp)
31    {
32        ff_assert(!com.exists());
33        ff_assert(!locs.exists());
34    }
35};
36
37// -------------------
38//      FamilyList
39
40FamilyList::FamilyList() :
41    next(NULp),
42    name(NULp),
43    matches(0),
44    rel_matches(0.0)
45{}
46
47FamilyList::~FamilyList() {
48    free(name);
49    delete next;
50}
51
52FamilyList *FamilyList::insertSortedBy_matches(FamilyList *other) {
53    ff_assert(!next); // only insert unlinked instances!
54
55    if (!other) {
56        return this;
57    }
58
59    if (matches >= other->matches) {
60        next = other;
61        return this;
62    }
63
64    // insert into other
65    FamilyList *rest = other->next;
66    other->next      = rest ? insertSortedBy_matches(rest) : this;
67
68    return other;
69}
70
71FamilyList *FamilyList::insertSortedBy_rel_matches(FamilyList *other) {
72    ff_assert(!next); // only insert unlinked instances!
73
74    if (rel_matches >= other->rel_matches) {
75        next = other;
76        return this;
77    }
78
79    // insert into other
80    FamilyList *rest = other->next;
81    other->next      = rest ? insertSortedBy_rel_matches(rest) : this;
82
83    return other;
84
85}
86
87// ---------------------
88//      FamilyFinder
89
90FamilyFinder::FamilyFinder(bool rel_matches_, RelativeScoreScaling scaling_) :
91    rel_matches(rel_matches_),
92    scaling(scaling_),
93    family_list(NULp),
94    hits_truncated(false),
95    real_hits(-1),
96    range(-1, -1)
97{}
98
99FamilyFinder::~FamilyFinder() {
100    delete_family_list();
101}
102
103void FamilyFinder::delete_family_list() {
104    delete family_list;
105    family_list = NULp;
106}
107
108// ------------------------
109//      PT_FamilyFinder
110
111PT_FamilyFinder::PT_FamilyFinder(GBDATA *gb_main_, int server_id_, int oligo_len_, int mismatches_, bool fast_flag_, bool rel_matches_, RelativeScoreScaling scaling_)
112    : FamilyFinder(rel_matches_, scaling_),
113      gb_main(gb_main_),
114      server_id(server_id_),
115      oligo_len(oligo_len_),
116      mismatches(mismatches_),
117      fast_flag(fast_flag_)
118{
119    // 'mismatches'  = the number of allowed mismatches
120    // 'fast_flag'   = 0 -> do complete search, 1 -> search only oligos starting with 'A'
121    // 'rel_matches' = 0 -> score is number of oligo-hits, 1 -> score is relative to longer sequence (target or source) * 10
122
123    ci = new PT_FF_comImpl;
124}
125
126PT_FamilyFinder::~PT_FamilyFinder() {
127    delete_family_list();
128    close();
129    delete ci;
130}
131
132GB_ERROR PT_FamilyFinder::init_communication() {
133    const char *user = "PT_FamilyFinder";
134
135    ff_assert(!ci->locs.exists());
136
137    // connect PT server
138    if (aisc_create(ci->link, PT_MAIN, ci->com,
139                    MAIN_LOCS, PT_LOCS, ci->locs,
140                    LOCS_USER, user,
141                    NULp)) {
142        return GB_export_error("Cannot initialize communication");
143    }
144    return NULp;
145}
146
147
148GB_ERROR PT_FamilyFinder::open(const char *servername) {
149    GB_ERROR error = NULp;
150
151    ff_assert(!ci->com.exists() && !ci->link);
152
153    if (arb_look_and_start_server(AISC_MAGIC_NUMBER, servername)) {
154        error = "Cannot contact PT  server";
155    }
156    else {
157        const char *socketid = GBS_read_arb_tcp(servername);
158        if (!socketid) error = GB_await_error();
159        else {
160            ci->link = aisc_open(socketid, ci->com, AISC_MAGIC_NUMBER, &error);
161            if (!error) {
162                if (!ci->link) error                 = "Cannot contact PT server [1]";
163                else if (init_communication()) error = "Cannot contact PT server [2]";
164            }
165        }
166    }
167
168    ff_assert(error || (ci->com.exists() && ci->link));
169
170    return error;
171}
172
173void PT_FamilyFinder::close() {
174    if (ci->link) aisc_close(ci->link, ci->com);
175    ci->link = NULp;
176    ff_assert(!ci->com.exists());
177    ci->locs.clear();
178}
179
180GB_ERROR PT_FamilyFinder::retrieve_family(const char *sequence, FF_complement compl_mode, int max_results, double min_score) {
181    delete_family_list();
182
183    char     *compressed_sequence = GB_command_interpreter(sequence, "|keep(acgtunACGTUN)", gb_main);
184    GB_ERROR  error               = NULp;
185
186    if      (!compressed_sequence)    error = GB_await_error();
187    else if (!compressed_sequence[0]) error = "No data in sequence(-region)";
188    else {
189        bytestring bs;
190        bs.data = compressed_sequence;
191        bs.size = strlen(bs.data)+1;
192
193        /* Start find_family() at the PT_SERVER
194         *
195         * Here we have to make a loop, until the match count of the
196         * first member is big enough
197         */
198
199        ff_assert(!range.is_empty());
200
201        // create and init family finder object
202        T_PT_FAMILYFINDER ffinder;
203        if (aisc_create(ci->link, PT_LOCS, ci->locs,
204                        LOCS_FFINDER, PT_FAMILYFINDER, ffinder,
205                        FAMILYFINDER_PROBE_LEN,       long(oligo_len),          // oligo length (12 hardcoded till July 2008)
206                        FAMILYFINDER_MISMATCH_NUMBER, long(mismatches),         // number of mismatches (0 hardcoded till July 2008)
207                        FAMILYFINDER_FIND_TYPE,       long(fast_flag),          // 0: complete search, 1: quick search (only search oligos starting with 'A')
208                        FAMILYFINDER_SORT_TYPE,       long(uses_rel_matches()), // 0: matches, 1: relative matches (0 hardcoded till July 2008)
209                        FAMILYFINDER_REL_SCORING,     long(get_scaling()),      // scaling of relative scores
210                        FAMILYFINDER_SORT_MAX,        long(max_results),        // speed up family sorting (only sort retrieved results)
211                        FAMILYFINDER_MIN_SCORE,       double(min_score),        // limit hits by score
212                        FAMILYFINDER_COMPLEMENT,      long(compl_mode),         // any combination of: 1 = forward, 2 = reverse, 4 = reverse-complement, 8 = complement (1 hardcoded in PT-Server till July 2008)
213                        FAMILYFINDER_RANGE_STARTPOS,  long(range.start()),
214                        FAMILYFINDER_RANGE_ENDPOS,    long(range.is_limited() ? range.end() : -1),
215                        FAMILYFINDER_FIND_FAMILY,     &bs,                      // RPC (has to be last parameter!)
216                        NULp))
217        {
218            error = "Communication error with PT server ('retrieve_family')";
219        }
220        else {
221            char *ff_error = NULp;
222
223            // Read family list
224            T_PT_FAMILYLIST f_list;
225            aisc_get(ci->link, PT_FAMILYFINDER, ffinder,
226                     FAMILYFINDER_FAMILY_LIST,      f_list.as_result_param(),
227                     FAMILYFINDER_FAMILY_LIST_SIZE, &real_hits,
228                     FAMILYFINDER_ERROR,            &ff_error,
229                     NULp);
230
231            if (ff_error[0]) {
232                error = GBS_global_string("PTSERVER: %s", ff_error);
233            }
234            else {
235                hits_truncated = false;
236                if (max_results<1) max_results = INT_MAX;
237
238                FamilyList *tail = NULp;
239                while (f_list.exists()) {
240                    if (max_results == 0) {
241                        hits_truncated = true;
242                        break;
243                    }
244                    max_results--;
245
246                    FamilyList *fl = new FamilyList;
247
248                    (tail ? tail->next : family_list) = fl;
249                    tail                              = fl;
250                    fl->next                          = NULp;
251
252                    aisc_get(ci->link, PT_FAMILYLIST, f_list,
253                             FAMILYLIST_NAME,        &fl->name,
254                             FAMILYLIST_MATCHES,     &fl->matches,
255                             FAMILYLIST_REL_MATCHES, &fl->rel_matches,
256                             FAMILYLIST_NEXT,        f_list.as_result_param(),
257                             NULp);
258                }
259            }
260            free(ff_error);
261        }
262
263        free(compressed_sequence);
264    }
265    return error;
266}
267
268GB_ERROR PT_FamilyFinder::searchFamily(const char *sequence, FF_complement compl_mode, int max_results, double min_score) {
269    // searches the PT-server for species related to 'sequence'.
270    //
271    // relation-score is calculated by fragmenting the sequence into oligos of length 'oligo_len' and
272    // then summarizing the number of hits.
273    //
274    // 'max_results' limits the length of the generated result list (low scores deleted first)
275    //               if < 1 -> don't limit
276    //
277    // 'min_score' limits the results by score (use 0 for unlimited results)
278    //
279    // When using restrict_2_region(), only pass the corresponding part via 'sequence' (not the full alignment)
280
281    GB_ERROR error = range.is_empty() ? "Specified range is empty" : NULp;
282    if (!error) {
283        error             = open(GBS_ptserver_tag(server_id));
284        if (!error) error = retrieve_family(sequence, compl_mode, max_results, min_score);
285        close();
286    }
287    return error;
288}
289
290const char *PT_FamilyFinder::results2string() {
291    GBS_strstruct *out = GBS_stropen(1000);
292    for (FamilyList *fl = family_list; fl; fl = fl->next) {
293        GBS_strnprintf(out, 100, "%s/%li/%3.5f,", fl->name, fl->matches, fl->rel_matches*100);
294    }
295    GBS_str_cut_tail(out, 1);
296    RETURN_LOCAL_ALLOC(GBS_strclose(out));
297}
298
299static void adjustOligolenAndMismatches(AW_root *aw_root, bool oligolen_changed) {
300    AW_awar *awar_oligolen   = aw_root->awar(AWAR_NN_OLIGO_LEN);
301    AW_awar *awar_mismatches = aw_root->awar(AWAR_NN_MISMATCHES);
302
303    int oligolen   = awar_oligolen->read_int();
304    int mismatches = awar_mismatches->read_int();
305
306    if (oligolen<=mismatches) { // =unwanted state
307        if (oligolen_changed) {
308            awar_mismatches->write_int(oligolen-1);
309        }
310        else {
311            awar_oligolen->write_int(mismatches+1);
312        }
313    }
314}
315
316void AWTC_create_common_next_neighbour_vars(AW_root *aw_root, const RootCallback& awar_changed_cb) {
317    static bool created = false;
318    if (!created) {
319        aw_root->awar_int(AWAR_NN_OLIGO_LEN,  12)->set_minmax(1, 200)->add_callback(makeRootCallback(adjustOligolenAndMismatches, true))->add_callback(awar_changed_cb);
320        aw_root->awar_int(AWAR_NN_MISMATCHES,  0)->set_minmax(0,  50)->add_callback(makeRootCallback(adjustOligolenAndMismatches, false))->add_callback(awar_changed_cb);
321        aw_root->awar_int(AWAR_NN_FAST_MODE,   0)->add_callback(awar_changed_cb);
322        aw_root->awar_int(AWAR_NN_REL_MATCHES, 1)->add_callback(awar_changed_cb);
323        aw_root->awar_int(AWAR_NN_REL_SCALING, RSS_BOTH_MIN)->add_callback(awar_changed_cb);
324
325        created = true;
326    }
327}
328
329void AWTC_create_common_next_neighbour_fields(AW_window *aws, int scaler_length) {
330    // used in several figs:
331    // - ad_spec_nn.fig
332    // - ad_spec_nnm.fig
333    // - faligner/family_settings.fig
334
335    aws->at("oligo_len");
336    aws->create_input_field_with_scaler(AWAR_NN_OLIGO_LEN, 3, scaler_length, AW_SCALER_EXP_LOWER);
337
338    aws->at("mismatches");
339    aws->create_input_field_with_scaler(AWAR_NN_MISMATCHES, 3, scaler_length, AW_SCALER_EXP_LOWER);
340
341    aws->at("mode");
342    aws->create_option_menu(AWAR_NN_FAST_MODE, true);
343    aws->insert_default_option("Complete", "", 0);
344    aws->insert_option("Quick", "", 1);
345    aws->update_option_menu();
346
347    aws->at("score");
348    aws->create_option_menu(AWAR_NN_REL_MATCHES, true);
349    aws->insert_option("absolute", "", 0);
350    aws->insert_default_option("relative", "", 1);
351    aws->update_option_menu();
352
353    aws->at("scaling");
354    aws->create_option_menu(AWAR_NN_REL_SCALING, true);
355    aws->insert_option        ("to source POC",  "", RSS_SOURCE);
356    aws->insert_option        ("to target POC",  "", RSS_TARGET);
357    aws->insert_default_option("to maximum POC", "", RSS_BOTH_MAX);
358    aws->insert_option        ("to minimum POC", "", RSS_BOTH_MIN);
359    aws->update_option_menu();
360}
361
362// --------------------------------------------------------------------------------
363
364#ifdef UNIT_TESTS
365
366#include <test_unit.h>
367
368class ff_tester {
369    GBDATA *gb_main;
370public:
371    bool    relativeMatches;
372    bool    fastMode;
373    bool    partial;
374    bool    shortOligo;
375    double  min_score;
376
377    RelativeScoreScaling scaling;
378
379    ff_tester(GBDATA *gb_main_)
380        : gb_main(gb_main_),
381          relativeMatches(false),
382          fastMode(false),
383          partial(false),
384          shortOligo(false),
385          min_score(0.0),
386          scaling(RSS_BOTH_MAX)
387    {}
388
389    const char *get_result(GB_ERROR& error) {
390        int             oligoLen = shortOligo ? (partial ? 3 : 6) : (partial ? 10 : 18);
391        PT_FamilyFinder ff(gb_main, TEST_SERVER_ID, oligoLen, 1, fastMode, relativeMatches, scaling);
392
393        const char *sequence;
394        if (partial) {
395            ff.restrict_2_region(PosRange(39, 91)); // alignment range of bases 30-69 of sequence of 'LgtLytic'
396            sequence = "UCUAGCUUGCUAGACGGGUGGCGAG" "GGUAACCGUAGGGGA"; // bases 30-54 of sequence of 'LgtLytic' + 15 bases from 'DcdNodos' (outside region)
397        }
398        else {
399            // sequence of 'LgtLytic' in TEST_pt.arb:
400            sequence = "AGAGUUUGAUCAAGUCGAACGGCAGCACAGUCUAGCUUGCUAGACGGGUGGCGAGUGGCGAACGGACUUGGGGAAACUCAAGCUAAUACCGCAUAAUCAUGACUGGGGUGAAGUCGUAACAAGGUAGCCGUAGGGGAACCUGCGGCUGGAUCACCUCCUN";
401        }
402
403        error = ff.searchFamily(sequence, FF_FORWARD, 4, min_score);
404        return ff.results2string();
405    }
406};
407
408
409#define TEST_RELATIVES_COMMON(tester,expctd) \
410        GB_ERROR    error;                                      \
411        const char *result   = tester.get_result(error);        \
412        const char *expected = expctd;                          \
413        TEST_EXPECT_NO_ERROR(error);                            \
414
415#define TEST_EXPECT_RELATIVES(tester,expctd) do {               \
416        TEST_RELATIVES_COMMON(tester,expctd);                   \
417        TEST_EXPECT_EQUAL(result, expected);                    \
418    } while(0)
419
420#define TEST_EXPECT_REL__BROK(tester,expctd) do {               \
421        TEST_RELATIVES_COMMON(tester,expctd);                   \
422        TEST_EXPECT_EQUAL__BROKEN(result, expected);            \
423    } while(0)
424
425void TEST_SLOW_PT_FamilyFinder() {
426    GB_shell shell;
427    TEST_SETUP_GLOBAL_ENVIRONMENT("ptserver");
428
429    GBDATA *gb_main = GB_open("no.arb", "c");
430
431    // check some error cases
432    {
433        PT_FamilyFinder ffe(gb_main, TEST_SERVER_ID, 0, 1, 0, 0, RSS_BOTH_MAX);
434        TEST_EXPECT_CONTAINS(ffe.searchFamily("whatever", FF_FORWARD, 4, 0.0), "minimum oligo length is 1");
435    }
436
437    ff_tester test(gb_main);
438
439    ff_tester ______RESET = test; TEST_EXPECT_RELATIVES(test, "LgtLytic/142/97.93103,HllHalod/62/43.05556,AclPleur/59/38.06452,PtVVVulg/51/34.00000");
440    test.partial          = true; TEST_EXPECT_RELATIVES(test, "LgtLytic/18/11.76471,VblVulni/5/3.24675,VbhChole/4/2.59740,DcdNodos/4/2.59740");
441    test.shortOligo       = true; TEST_EXPECT_RELATIVES(test, "PtVVVulg/38/23.03030,AclPleur/38/22.35294,VbhChole/38/23.60248,VblVulni/38/23.60248");
442    test.relativeMatches  = true; TEST_EXPECT_RELATIVES(test, "DsssDesu/38/38.77551,CltBotul/38/34.23423,PsAAAA00/38/32.75862,Bl0LLL00/38/25.67568");
443    test.min_score        = 32.6; TEST_EXPECT_RELATIVES(test, "DsssDesu/38/38.77551,CltBotul/38/34.23423,PsAAAA00/38/32.75862");
444    test                  = ______RESET;
445    test.shortOligo       = true; TEST_EXPECT_RELATIVES(test, "LgtLytic/154/98.08917,VbhChole/133/84.17722,VblVulni/133/84.17722,HllHalod/133/85.25641");
446    test.relativeMatches  = true; TEST_EXPECT_RELATIVES(test, "LgtLytic/154/98.08917,HllHalod/133/85.25641,VbhChole/133/84.17722,VblVulni/133/84.17722");
447    test.fastMode         = true; TEST_EXPECT_RELATIVES(test, "LgtLytic/42/26.75159,VblVulni/37/23.41772,HllHalod/36/23.07692,Stsssola/36/23.07692");
448    test.min_score        = 26.7; TEST_EXPECT_RELATIVES(test, "LgtLytic/42/26.75159");
449    test.min_score        = 26.8; TEST_EXPECT_RELATIVES(test, "");
450    test                  = ______RESET;
451    test.fastMode         = true; TEST_EXPECT_RELATIVES(test, "LgtLytic/40/27.58621,HllHalod/18/12.50000,AclPleur/17/10.96774,PtVVVulg/15/10.00000");
452    test.min_score        = 17.0; TEST_EXPECT_RELATIVES(test, "LgtLytic/40/27.58621,HllHalod/18/12.50000,AclPleur/17/10.96774");
453    test.min_score        = 17.5; TEST_EXPECT_RELATIVES(test, "LgtLytic/40/27.58621,HllHalod/18/12.50000");
454    test                  = ______RESET;
455
456    test.shortOligo      = true;
457    test.relativeMatches = true;
458    test.scaling         = RSS_BOTH_MAX; TEST_EXPECT_RELATIVES(test, "LgtLytic/154/98.08917,HllHalod/133/85.25641,VbhChole/133/84.17722,VblVulni/133/84.17722");
459    test.scaling         = RSS_BOTH_MIN; TEST_EXPECT_RELATIVES(test, "LgtLytic/154/98.71795,DsssDesu/84/88.42105,CltBotul/95/87.96296,PsAAAA00/97/85.84071");
460    test.scaling         = RSS_TARGET;   TEST_EXPECT_RELATIVES(test, "LgtLytic/154/98.08917,DsssDesu/84/88.42105,CltBotul/95/87.96296,PsAAAA00/97/85.84071");
461    test.scaling         = RSS_SOURCE;   TEST_EXPECT_RELATIVES(test, "LgtLytic/154/98.71795,VbhChole/133/85.25641,VblVulni/133/85.25641,HllHalod/133/85.25641");
462    test.partial         = true;
463    test.shortOligo      = false;
464    test.scaling         = RSS_BOTH_MAX; TEST_EXPECT_RELATIVES(test, "LgtLytic/18/11.76471,VblVulni/5/3.24675,VbhChole/4/2.59740,DcdNodos/4/2.59740");
465    test.scaling         = RSS_BOTH_MIN; TEST_EXPECT_RELATIVES(test, "LgtLytic/18/56.25000,VblVulni/5/15.62500,VbhChole/4/12.50000,DcdNodos/4/12.50000");
466    test.scaling         = RSS_TARGET;   TEST_EXPECT_RELATIVES(test, "LgtLytic/18/11.76471,VblVulni/5/3.24675,VbhChole/4/2.59740,DcdNodos/4/2.59740");
467    test.scaling         = RSS_SOURCE;   TEST_EXPECT_RELATIVES(test, "LgtLytic/18/56.25000,VblVulni/5/15.62500,VbhChole/4/12.50000,DcdNodos/4/12.50000");
468    test                 = ______RESET;
469
470    GB_close(gb_main);
471}
472
473#endif // UNIT_TESTS
474
Note: See TracBrowser for help on using the repository browser.