source: tags/ms_r16q3/PROBE_SET/ps_node.hxx

Last change on this file was 15082, checked in by westram, 8 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1#ifndef PS_NODE_HXX
2#define PS_NODE_HXX
3
4#ifndef SMARTPTR_H
5#include <smartptr.h>
6#endif
7#ifndef PS_DEFS_HXX
8#include "ps_defs.hxx"
9#endif
10#ifndef PS_FILEBUFFER_HXX
11#include "ps_filebuffer.hxx"
12#endif
13
14using namespace std;
15
16struct PS_Probe {
17    short int          quality;             // negative quality <=> matches inverse path
18    unsigned short int length;
19    unsigned short int GC_content;
20};
21
22typedef SmartPtr<PS_Probe> PS_ProbePtr;
23
24inline void PS_printProbe(const PS_Probe *p) {
25    printf("%+i_%u_%u", p->quality, p->length, p->GC_content);
26}
27
28inline void PS_printProbe(const PS_ProbePtr& p) {
29    printf("%+i_%u_%u", p->quality, p->length, p->GC_content);
30}
31
32struct lt_probe
33{
34    bool operator()(const PS_ProbePtr& p1, const PS_ProbePtr& p2) const
35    {
36        if (abs(p1->quality) == abs(p2->quality)) {
37            if (p1->length == p2->length) {
38                return (p1->GC_content < p2->GC_content); // equal quality & length => sort by GC-content
39            }
40            else {
41                return (p1->length < p2->length);         // equal quality => sort by length
42            }
43        }
44        else {
45            return ((p1->quality < p2->quality));       // sort by quality
46        }
47    }
48};
49
50
51typedef set<PS_ProbePtr, lt_probe>   PS_ProbeSet;
52typedef PS_ProbeSet*                PS_ProbeSetPtr;
53typedef PS_ProbeSet::iterator       PS_ProbeSetIter;
54typedef PS_ProbeSet::const_iterator PS_ProbeSetCIter;
55class PS_Node;
56typedef SmartPtr<PS_Node>         PS_NodePtr;
57typedef map<SpeciesID, PS_NodePtr> PS_NodeMap;
58typedef PS_NodeMap::iterator         PS_NodeMapIterator;
59typedef PS_NodeMap::reverse_iterator PS_NodeMapRIterator;
60typedef PS_NodeMap::const_iterator         PS_NodeMapConstIterator;
61typedef PS_NodeMap::const_reverse_iterator PS_NodeMapConstRIterator;
62
63class PS_Node : virtual Noncopyable {
64    SpeciesID      num;
65    PS_NodeMap     children;
66    PS_ProbeSetPtr probes;
67
68public:
69
70    //
71    // *** num ***
72    //
73    void      setNum(SpeciesID id) { num = id; }
74    SpeciesID getNum() const         { return num; }
75
76    //
77    // *** children ***
78    //
79    bool addChild(PS_NodePtr& _child) {
80        PS_NodeMapIterator it = children.find(_child->getNum());
81        if (it == children.end()) {
82            children[_child->getNum()] = _child;
83            return true;
84        }
85        else {
86            printf("child[#%u] already exists\n", _child->getNum());
87            return false;
88        }
89    }
90
91    PS_NodePtr assertChild(SpeciesID _id) {
92        PS_NodeMapIterator it = children.find(_id);
93        if (it == children.end()) {
94            PS_NodePtr new_child(new PS_Node(_id));
95            children[_id] = new_child;
96            return new_child;
97        }
98        else {
99            return it->second;
100        }
101    }
102
103    pair<bool, PS_NodePtr> getChild(SpeciesID id) {
104        PS_NodeMapIterator it = children.find(id);
105        return pair<bool, PS_NodePtr>(it!=children.end(), it->second);
106    }
107
108    pair<bool, const PS_NodePtr> getChild(SpeciesID id) const {
109        PS_NodeMapConstIterator it = children.find(id);
110        return pair<bool, const PS_NodePtr>(it!=children.end(), it->second);
111    }
112
113    size_t countChildren() const { return children.size(); }
114    bool   hasChildren()   const { return (!children.empty()); }
115
116    PS_NodeMapIterator       getChildrenBegin()        { return children.begin(); }
117    PS_NodeMapRIterator      getChildrenRBegin()       { return children.rbegin(); }
118    PS_NodeMapConstIterator  getChildrenBegin()  const { return children.begin(); }
119    PS_NodeMapConstRIterator getChildrenRBegin() const { return children.rbegin(); }
120    PS_NodeMapIterator       getChildrenEnd()          { return children.end(); }
121    PS_NodeMapRIterator      getChildrenREnd()         { return children.rend(); }
122    PS_NodeMapConstIterator  getChildrenEnd()    const { return children.end(); }
123    PS_NodeMapConstRIterator getChildrenREnd()   const { return children.rend(); }
124
125    //
126    // *** probes ***
127    //
128    bool addProbe(const PS_ProbePtr& probe) {
129        if (!probes) {
130            probes = new PS_ProbeSet;
131            probes->insert(probe);
132            return true;
133        }
134        else {
135            pair<PS_ProbeSetCIter, bool> p = probes->insert(probe);
136            return p.second;
137        }
138    }
139
140    void addProbes(PS_ProbeSetCIter _begin, PS_ProbeSetCIter _end) {
141        if (_begin == _end) return;
142        if (!probes) probes = new PS_ProbeSet;
143        for (PS_ProbeSetCIter probe = _begin; probe != _end; ++probe) {
144            probes->insert(*probe);
145        }
146    }
147
148    void addProbesInverted(PS_ProbeSetCIter _begin, PS_ProbeSetCIter _end) {
149        if (_begin == _end) return;
150        if (!probes) probes = new PS_ProbeSet;
151        for (PS_ProbeSetCIter probe = _begin; probe != _end; ++probe) {
152            PS_ProbePtr new_probe(new PS_Probe);
153            new_probe->length     = (*probe)->length;
154            new_probe->quality    = -((*probe)->quality);
155            new_probe->GC_content = (*probe)->GC_content;
156            probes->insert(new_probe);
157        }
158    }
159
160    size_t countProbes() const { return (probes == 0) ? 0 : probes->size(); }
161    bool   hasProbes()   const { return (probes != 0); }
162    bool   hasPositiveProbes() const {
163        if (!probes) return false;
164        for (PS_ProbeSetCIter i=probes->begin(); i!=probes->end(); ++i) {
165            if ((*i)->quality >= 0) return true;
166        }
167        return false;
168    }
169
170    bool   hasInverseProbes() const {
171        if (!probes) return false;
172        for (PS_ProbeSetCIter i=probes->begin(); i!=probes->end(); ++i) {
173            if ((*i)->quality < 0) return true;
174        }
175        return false;
176    }
177
178    PS_ProbeSetCIter getProbesBegin() const {
179        ps_assert(probes);
180        return probes->begin();
181    }
182    PS_ProbeSetCIter getProbesEnd() const {
183        ps_assert(probes);
184        return probes->end();
185    }
186
187    void   removeProbe(PS_ProbeSetCIter it) {
188        ps_assert(probes);
189        probes->erase(it);
190    }
191    void   removeProbes() {
192        if (probes) delete probes;
193        probes = 0;
194    }
195
196    //
197    // *** output **
198    //
199    void print() {
200        printf("\nN[%d] P[ ", num);
201        if (probes) {
202            for (PS_ProbeSetCIter i=probes->begin(); i!=probes->end(); ++i) {
203                PS_printProbe(*i);
204                fputc(' ', stdout);
205            }
206        }
207        printf("] C[");
208        for (PS_NodeMapIterator i=children.begin(); i!=children.end(); ++i) {
209            i->second->print();
210        }
211        fputc(']', stdout);
212    }
213
214    void printOnlyMe() const {
215        printf("N[%d] P[ ", num);
216        if (probes) {
217            for (PS_ProbeSetCIter i=probes->begin(); i!=probes->end(); ++i) {
218                PS_printProbe(*i);
219                fputc(' ', stdout);
220            }
221        }
222        printf("] C[ %zu ]", children.size());
223    }
224
225    //
226    // *** disk i/o ***
227    //
228    bool save(PS_FileBuffer *_fb);
229    bool saveASCII(PS_FileBuffer *_fb, char *buffer);
230    bool load(PS_FileBuffer *_fb);
231    bool append(PS_FileBuffer *_fb);      // load from file and append to node
232    bool read(PS_FileBuffer *_fb, PS_Callback *_call_destination);        // parse file and callback after probes read
233
234    //
235    // *** constructors ***
236    //
237    PS_Node(SpeciesID id) { num = id; probes = 0; }
238
239    //
240    // *** destructor ***
241    //
242    ~PS_Node() {
243        if (probes) delete probes;
244        children.clear();
245    }
246};
247
248#else
249#error ps_node.hxx included twice
250#endif
Note: See TracBrowser for help on using the repository browser.