source: tags/ms_r16q2/PROBE/probe.h

Last change on this file was 13970, checked in by westram, 9 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.8 KB
Line 
1#ifndef PROBE_H
2#define PROBE_H
3
4#ifndef __LIST__
5#include <list>
6#endif
7#ifndef __SET__
8#include <set>
9#endif
10
11#ifndef ARBDB_H
12#include <arbdb.h>
13#endif
14#ifndef PT_COM_H
15#include <PT_com.h>
16#endif
17#ifndef AISC_GEN_SERVER_INCLUDED
18#include <PT_server.h>
19#endif
20#ifndef PT_TOOLS_H
21#include "PT_tools.h"
22#endif
23#ifndef CACHE_H
24#include <cache.h>
25#endif
26
27#define PT_SERVER_MAGIC   0x32108765                // pt server identifier
28#define PT_SERVER_VERSION 3                         // version of pt server database (no versioning till 2009/05/13)
29
30#if defined(DEBUG)
31// # define PTM_DEBUG_NODES
32# define PTM_DEBUG_STAGE_ASSERTIONS
33// # define PTM_DEBUG_VALIDATE_CHAINS
34#endif // DEBUG
35
36#define CALCULATE_STATS_ON_QUERY
37
38#if defined(PTM_DEBUG_STAGE_ASSERTIONS)
39#define pt_assert_stage(s) pt_assert(psg.get_stage() == (s))
40#else // !defined(PTM_DEBUG_STAGE_ASSERTIONS)
41#define pt_assert_stage(s)
42#endif
43
44#if defined(PTM_DEBUG_VALIDATE_CHAINS)
45#define pt_assert_valid_chain_stage1(node) pt_assert(PT_chain_has_valid_entries<ChainIteratorStage1>(node))
46#else // !defined(PTM_DEBUG_VALIDATE_CHAINS)
47#define pt_assert_valid_chain_stage1(node)
48#endif
49
50typedef unsigned long ULONG;
51typedef unsigned int  UINT;
52typedef unsigned char uchar;
53
54#define PT_MAX_PARTITION_DEPTH 4
55
56#define PT_POS_TREE_HEIGHT 20
57#define PT_MIN_TREE_HEIGHT PT_MAX_PARTITION_DEPTH
58
59#define MIN_PROBE_LENGTH 2
60
61enum PT_MATCH_TYPE {
62    PT_MATCH_TYPE_INTEGER           = 0,
63    PT_MATCH_TYPE_WEIGHTED_PLUS_POS = 1,
64    PT_MATCH_TYPE_WEIGHTED          = -1
65};
66
67
68
69#define MATCHANSWER  50                             // private msg type: match result answer
70#define CREATEANSWER 51                             // private msg type: create result answer
71#define FINDANSWER   52                             // private msg type: find result answer
72
73extern int gene_flag;           // if 'gene_flag' == 1 -> we are a gene pt server
74struct Hs_struct;
75
76enum type_types {
77    t_int    = 1,
78    t_string = 0,
79    t_float  = 2
80};
81
82enum PT_base {
83    PT_QU = 0,
84    PT_N  = 1,
85    PT_A,
86    PT_C,
87    PT_G,
88    PT_T,
89    PT_BASES,
90    PT_B_UNDEF,
91};
92
93inline bool is_std_base     (char b) { return b >= PT_A && b <= PT_T; }
94inline bool is_std_base_or_N(char b) { return b >= PT_N && b <= PT_T; }
95inline bool is_ambig_base   (char b) { return b == PT_QU || b == PT_N; }
96inline bool is_valid_base   (char b) { return b >= PT_QU && b < PT_BASES; }
97
98inline char base_2_readable(char base) {
99    static char table[] = ".NACGU";
100    return base<PT_BASES ? table[safeCharIndex(base)] : base;
101}
102
103inline char *probe_2_readable(char *id_string, int len) {
104    //! translate a string containing PT_base into readable characters.
105    // caution if 'id_string' contains PT_QU ( == zero == EOS).
106    // (see also: probe_compress_sequence)
107    for (int i = 0; i<len; ++i) {
108        id_string[i] = base_2_readable(id_string[i]);
109    }
110    return id_string;
111}
112
113inline void reverse_probe(char *seq, int len) {
114    int i = 0;
115    int j = len-1;
116
117    while (i<j) std::swap(seq[i++], seq[j--]);
118}
119
120struct POS_TREE1;
121struct POS_TREE2;
122
123enum Stage { STAGE1, STAGE2 };
124
125// ---------------------
126//      Probe search
127
128class probe_input_data : virtual Noncopyable { // every taxa's own data
129    int     size;       // @@@ misleading (contains 1 if no bases in sequence)
130    GBDATA *gb_species;
131    bool    group;      // probe_design: whether species is in group
132
133    typedef SmartArrayPtr(int) SmartIntPtr;
134
135    mutable cache::CacheHandle<SmartCharPtr> seq;
136    mutable cache::CacheHandle<SmartIntPtr>  rel2abs;
137
138    static cache::Cache<SmartCharPtr> seq_cache;
139    static cache::Cache<SmartIntPtr>  rel2abs_cache;
140
141    SmartCharPtr loadSeq() const {
142        GB_transaction ta(gb_species);
143        GBDATA *gb_compr = GB_entry(gb_species, "compr");
144        return SmartCharPtr(GB_read_bytes(gb_compr));
145    }
146
147public:
148
149    probe_input_data()
150        : size(0),
151          gb_species(0),
152          group(false)
153    {}
154    ~probe_input_data() {
155        seq.release(seq_cache);
156        rel2abs.release(rel2abs_cache);
157    }
158
159    static void set_cache_sizes(size_t seq_cache_size, size_t abs_cache_size) {
160        seq_cache.resize(seq_cache_size);
161        rel2abs_cache.resize(abs_cache_size);
162    }
163
164    GB_ERROR init(GBDATA *gb_species_);
165
166    SmartCharPtr get_dataPtr() const {
167        if (!seq.is_cached()) seq.assign(loadSeq(), seq_cache);
168        return seq.access(seq_cache);
169    }
170
171    const char *get_shortname() const {
172        GB_transaction ta(gb_species);
173
174        GBDATA *gb_name = GB_entry(gb_species, "name");
175        pt_assert(gb_name);
176        return GB_read_char_pntr(gb_name);
177    }
178    const char *get_fullname() const {
179        GB_transaction ta(gb_species);
180
181        GBDATA *gb_full = GB_entry(gb_species, "full_name");
182        return gb_full ? GB_read_char_pntr(gb_full) : "";
183    }
184
185    const char *get_acc() const {
186        GB_transaction ta(gb_species);
187
188        GBDATA *gb_acc = GB_entry(gb_species, "acc");
189        pt_assert(gb_acc);
190        return gb_acc ? GB_read_char_pntr(gb_acc) : NULL;
191    }
192    int get_start() const {
193        GB_transaction ta(gb_species);
194
195        GBDATA *gb_start = GB_entry(gb_species, "start");
196        return gb_start ? GB_read_int(gb_start) : 0;
197    }
198    int get_stop() const {
199        GB_transaction ta(gb_species);
200
201        GBDATA *gb_stop = GB_entry(gb_species, "stop");
202        return gb_stop ? GB_read_int(gb_stop) : 0;
203    }
204
205    long get_checksum() const { // @@@ change return-type -> uint32_t
206        GB_transaction ta(gb_species);
207        GBDATA *gb_cs = GB_entry(gb_species, "cs");
208        pt_assert(gb_cs);
209        uint32_t csum = uint32_t(GB_read_int(gb_cs));
210        return csum;
211    }
212    int get_size() const { return size; }
213
214    bool valid_rel_pos(int rel_pos) const { // returns true if rel_pos is inside sequence
215        return rel_pos >= 0 && rel_pos<get_size();
216    }
217
218    bool inside_group() const { return group; }
219    bool outside_group() const { return !group; }
220
221    void set_group_state(bool isGroupMember) { group = isGroupMember; }
222
223    long get_geneabspos() const {
224        pt_assert(gene_flag); // only legal in gene-ptserver
225        GBDATA *gb_pos = GB_entry(gb_species, "abspos");
226        if (gb_pos) return GB_read_int(gb_pos);
227        return -1;
228    }
229
230    void preload_rel2abs() const {
231        if (!rel2abs.is_cached()) {
232            GB_transaction ta(gb_species);
233
234            GBDATA *gb_baseoff = GB_entry(gb_species, "baseoff");
235            pt_assert(gb_baseoff);
236
237            const GB_CUINT4 *baseoff = GB_read_ints_pntr(gb_baseoff);
238
239            int *r2a = new int[size];
240
241            int abs = 0;
242            for (int rel = 0; rel<size; ++rel) {
243                abs      += baseoff[rel];
244                r2a[rel]  = abs;
245            }
246
247            rel2abs.assign(r2a, rel2abs_cache);
248        }
249    }
250    size_t get_abspos(size_t rel_pos) const {
251        pt_assert(rel2abs.is_cached()); // you need to call preload_rel2abs
252        return (&*rel2abs.access(rel2abs_cache))[rel_pos]; // @@@ brute-forced
253    }
254
255    size_t calc_relpos(int abs_pos) const { // expensive
256        preload_rel2abs();
257        SmartIntPtr  rel2abs_ptr = rel2abs.access(rel2abs_cache);
258        const int   *r2a     = &*rel2abs_ptr;
259
260        int l = 0;
261        int h = get_size()-1;
262
263        if (r2a[l] == abs_pos) return l;
264        if (r2a[h] == abs_pos) return h;
265
266        while (l<h) {
267            int m = (l+h)/2;
268            if (r2a[m]<abs_pos) {
269                l = m;
270            }
271            else if (r2a[m]>abs_pos) {
272                h = m;
273            }
274            else {
275                return m;
276            }
277        }
278        return l;
279    }
280};
281
282struct probe_statistic_struct {
283    // common
284    int cut_offs;                                   // statistic of chains
285    int single_node;
286    int short_node;
287    int long_node;
288    int longs;
289    int shorts;
290    int shorts2;
291    int chars;
292
293#ifdef ARB_64
294    // 64bit specials
295    int  int_node;
296    int  ints;
297    int  ints2;
298    long maxdiff;
299#endif
300
301    void setup();
302};
303
304class BI_ecoli_ref;
305
306class MostUsedPos : virtual Noncopyable {
307    int pos;
308    int used;
309
310    MostUsedPos *next;
311
312    void swapWith(MostUsedPos *other) {
313        std::swap(pos, other->pos);
314        std::swap(used, other->used);
315    }
316
317public:
318    MostUsedPos() : pos(0), used(0), next(NULL) { }
319    MostUsedPos(int p) : pos(p), used(1), next(NULL) { }
320    ~MostUsedPos() { delete next; }
321
322    void clear() {
323        pos  = 0;
324        used = 0;
325        delete next;
326        next = NULL;
327    }
328
329    void announce(int p) {
330        if (p == pos) used++;
331        else {
332            if (next) next->announce(p);
333            else next = new MostUsedPos(p);
334            if (next->used>used) swapWith(next);
335        }
336    }
337
338
339    int get_most_used() const { return pos; }
340};
341
342class probe_struct_global {
343    Stage stage;
344
345    union {
346        POS_TREE1 *p1;
347        POS_TREE2 *p2;
348    } pt;
349
350public:
351    GB_shell *gb_shell;
352    GBDATA   *gb_main;                              // ARBDB interface
353    char     *alignment_name;
354    GB_HASH  *namehash;                             // name to int
355
356    int                      data_count;
357    struct probe_input_data *data;                  // the internal database
358
359    char         *ecoli;                            // the ecoli sequenz
360    BI_ecoli_ref *bi_ecoli;
361
362    int  max_size;                                  // maximum sequence len
363    long char_count;                                // number of all 'acgtuACGTU'
364
365    int reversed;                                   // tell the matcher whether probe is reversed
366
367    double *pos_to_weight;                          // position to weight
368
369    MostUsedPos abs_pos;
370
371    int sort_by;
372
373    char *main_probe;
374
375    char      *server_name;                         // name of this server
376    aisc_com  *link;
377    T_PT_MAIN  main;
378    Hs_struct *com_so;                              // the communication socket
379
380    probe_statistic_struct stat;
381
382    bool big_db; // STAGE2 only (true -> uses 8 bit pointers)
383
384    void setup();
385    void cleanup();
386
387    void enter_stage(Stage stage_);
388    Stage get_stage() const { return stage; }
389
390    POS_TREE1*& TREE_ROOT1() { pt_assert(stage == STAGE1); return pt.p1; }
391    POS_TREE2*& TREE_ROOT2() { pt_assert(stage == STAGE2); return pt.p2; }
392};
393
394extern probe_struct_global psg;
395
396class gene_struct {
397    char       *gene_name;
398    const char *arb_species_name; // pointers into 'gene_name'
399    const char *arb_gene_name;
400
401    void init(const char *gene_name_, const char *arb_species_name_, const char *arb_gene_name_) {
402        int gene_name_len        = strlen(gene_name_);
403        int arb_species_name_len = strlen(arb_species_name_);
404        int arb_gene_name_len    = strlen(arb_gene_name_);
405
406        int fulllen      = gene_name_len+1+arb_species_name_len+1+arb_gene_name_len+1;
407        gene_name        = new char[fulllen];
408        strcpy(gene_name, gene_name_);
409        arb_species_name = gene_name+(gene_name_len+1);
410        strcpy((char*)arb_species_name, arb_species_name_);
411        arb_gene_name    = arb_species_name+(arb_species_name_len+1);
412        strcpy((char*)arb_gene_name, arb_gene_name_);
413    }
414
415public:
416    gene_struct(const char *gene_name_, const char *arb_species_name_, const char *arb_gene_name_) {
417        init(gene_name_, arb_species_name_, arb_gene_name_);
418    }
419    gene_struct(const gene_struct& other) {
420        if (&other != this) {
421            init(other.get_internal_gene_name(), other.get_arb_species_name(), other.get_arb_gene_name());
422        }
423    }
424    gene_struct& operator = (const gene_struct& other) {
425        if (&other != this) {
426            delete [] gene_name;
427            init(other.get_internal_gene_name(), other.get_arb_species_name(), other.get_arb_gene_name());
428        }
429        return *this;
430    }
431
432    ~gene_struct() {
433        delete [] gene_name;
434    }
435
436    const char *get_internal_gene_name() const { return gene_name; }
437    const char *get_arb_species_name() const { return arb_species_name; }
438    const char *get_arb_gene_name() const { return arb_gene_name; }
439};
440
441struct ltByArbName {
442    bool operator()(const gene_struct *gs1, const gene_struct *gs2) const {
443        int cmp           = strcmp(gs1->get_arb_species_name(), gs2->get_arb_species_name());
444        if (cmp == 0) { cmp = strcmp(gs1->get_arb_gene_name(), gs2->get_arb_gene_name()); }
445        return cmp<0;
446    }
447};
448struct ltByInternalName {
449    bool operator()(const gene_struct *gs1, const gene_struct *gs2) const {
450        int cmp = strcmp(gs1->get_internal_gene_name(), gs2->get_internal_gene_name());
451        return cmp<0;
452    }
453};
454
455typedef std::list<gene_struct>                          gene_struct_list;
456typedef std::set<const gene_struct *, ltByInternalName> gene_struct_index_internal;
457typedef std::set<const gene_struct *, ltByArbName>      gene_struct_index_arb;
458
459extern gene_struct_index_arb      gene_struct_arb2internal; // sorted by arb species+gene name
460extern gene_struct_index_internal gene_struct_internal2arb; // sorted by internal name
461
462#else
463#error probe.h included twice
464#endif
465
Note: See TracBrowser for help on using the repository browser.