ARB
ps_node.hxx
Go to the documentation of this file.
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 
14 using namespace std;
15 
16 struct PS_Probe {
17  short int quality; // negative quality <=> matches inverse path
18  unsigned short int length;
19  unsigned short int GC_content;
20 };
21 
23 
24 inline void PS_printProbe(const PS_Probe *p) {
25  printf("%+i_%u_%u", p->quality, p->length, p->GC_content);
26 }
27 
28 inline void PS_printProbe(const PS_ProbePtr& p) {
29  printf("%+i_%u_%u", p->quality, p->length, p->GC_content);
30 }
31 
32 struct lt_probe {
33  bool operator()(const PS_ProbePtr& p1, const PS_ProbePtr& p2) const {
34  if (abs(p1->quality) == abs(p2->quality)) {
35  if (p1->length == p2->length) {
36  return p1->GC_content < p2->GC_content; // equal quality & length => sort by GC-content
37  }
38  else {
39  return p1->length < p2->length; // equal quality => sort by length
40  }
41  }
42  else {
43  return p1->quality < p2->quality; // sort by quality
44  }
45  }
46 };
47 
48 
49 typedef set<PS_ProbePtr, lt_probe> PS_ProbeSet;
51 typedef PS_ProbeSet::iterator PS_ProbeSetIter;
52 typedef PS_ProbeSet::const_iterator PS_ProbeSetCIter;
53 class PS_Node;
55 typedef map<SpeciesID, PS_NodePtr> PS_NodeMap;
56 typedef PS_NodeMap::iterator PS_NodeMapIterator;
57 typedef PS_NodeMap::reverse_iterator PS_NodeMapRIterator;
58 typedef PS_NodeMap::const_iterator PS_NodeMapConstIterator;
59 typedef PS_NodeMap::const_reverse_iterator PS_NodeMapConstRIterator;
60 
61 class PS_Node : virtual Noncopyable {
62  SpeciesID num;
63  PS_NodeMap children;
64  PS_ProbeSetPtr probes;
65 
66 public:
67 
68  //
69  // *** num ***
70  //
71  void setNum(SpeciesID id) { num = id; }
72  SpeciesID getNum() const { return num; }
73 
74  //
75  // *** children ***
76  //
77  bool addChild(PS_NodePtr& _child) {
78  PS_NodeMapIterator it = children.find(_child->getNum());
79  if (it == children.end()) {
80  children[_child->getNum()] = _child;
81  return true;
82  }
83  else {
84  printf("child[#%u] already exists\n", _child->getNum());
85  return false;
86  }
87  }
88 
90  PS_NodeMapIterator it = children.find(_id);
91  if (it == children.end()) {
92  PS_NodePtr new_child(new PS_Node(_id));
93  children[_id] = new_child;
94  return new_child;
95  }
96  else {
97  return it->second;
98  }
99  }
100 
101  pair<bool, PS_NodePtr> getChild(SpeciesID id) {
102  PS_NodeMapIterator it = children.find(id);
103  return pair<bool, PS_NodePtr>(it!=children.end(), it->second);
104  }
105 
106  pair<bool, const PS_NodePtr> getChild(SpeciesID id) const {
107  PS_NodeMapConstIterator it = children.find(id);
108  return pair<bool, const PS_NodePtr>(it!=children.end(), it->second);
109  }
110 
111  size_t countChildren() const { return children.size(); }
112  bool hasChildren() const { return !children.empty(); }
113 
114  PS_NodeMapIterator getChildrenBegin() { return children.begin(); }
115  PS_NodeMapRIterator getChildrenRBegin() { return children.rbegin(); }
116  PS_NodeMapConstIterator getChildrenBegin() const { return children.begin(); }
117  PS_NodeMapConstRIterator getChildrenRBegin() const { return children.rbegin(); }
118  PS_NodeMapIterator getChildrenEnd() { return children.end(); }
119  PS_NodeMapRIterator getChildrenREnd() { return children.rend(); }
120  PS_NodeMapConstIterator getChildrenEnd() const { return children.end(); }
121  PS_NodeMapConstRIterator getChildrenREnd() const { return children.rend(); }
122 
123  //
124  // *** probes ***
125  //
126  bool addProbe(const PS_ProbePtr& probe) {
127  if (!probes) {
128  probes = new PS_ProbeSet;
129  probes->insert(probe);
130  return true;
131  }
132  else {
133  pair<PS_ProbeSetCIter, bool> p = probes->insert(probe);
134  return p.second;
135  }
136  }
137 
139  if (_begin == _end) return;
140  if (!probes) probes = new PS_ProbeSet;
141  for (PS_ProbeSetCIter probe = _begin; probe != _end; ++probe) {
142  probes->insert(*probe);
143  }
144  }
145 
147  if (_begin == _end) return;
148  if (!probes) probes = new PS_ProbeSet;
149  for (PS_ProbeSetCIter probe = _begin; probe != _end; ++probe) {
150  PS_ProbePtr new_probe(new PS_Probe);
151  new_probe->length = (*probe)->length;
152  new_probe->quality = -((*probe)->quality);
153  new_probe->GC_content = (*probe)->GC_content;
154  probes->insert(new_probe);
155  }
156  }
157 
158  size_t countProbes() const { return probes ? probes->size() : 0; }
159  bool hasProbes() const { return probes; }
160  bool hasPositiveProbes() const {
161  if (!probes) return false;
162  for (PS_ProbeSetCIter i=probes->begin(); i!=probes->end(); ++i) {
163  if ((*i)->quality >= 0) return true;
164  }
165  return false;
166  }
167 
168  bool hasInverseProbes() const {
169  if (!probes) return false;
170  for (PS_ProbeSetCIter i=probes->begin(); i!=probes->end(); ++i) {
171  if ((*i)->quality < 0) return true;
172  }
173  return false;
174  }
175 
177  ps_assert(probes);
178  return probes->begin();
179  }
181  ps_assert(probes);
182  return probes->end();
183  }
184 
186  ps_assert(probes);
187  probes->erase(it);
188  }
189  void removeProbes() {
190  if (probes) {
191  delete probes;
192  probes = NULp;
193  }
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 = NULp; }
238 
239  //
240  // *** destructor ***
241  //
243  if (probes) delete probes;
244  children.clear();
245  }
246 };
247 
248 #else
249 #error ps_node.hxx included twice
250 #endif
const char * id
Definition: AliAdmin.cxx:17
PS_NodeMapIterator getChildrenBegin()
Definition: ps_node.hxx:114
PS_NodeMapConstIterator getChildrenEnd() const
Definition: ps_node.hxx:120
SpeciesID getNum() const
Definition: ps_node.hxx:72
void setNum(SpeciesID id)
Definition: ps_node.hxx:71
PS_Node(SpeciesID id)
Definition: ps_node.hxx:237
SmartPtr< PS_Node > PS_NodePtr
Definition: ps_node.hxx:53
void removeProbe(PS_ProbeSetCIter it)
Definition: ps_node.hxx:185
PS_NodePtr assertChild(SpeciesID _id)
Definition: ps_node.hxx:89
size_t countChildren() const
Definition: ps_node.hxx:111
void addProbes(PS_ProbeSetCIter _begin, PS_ProbeSetCIter _end)
Definition: ps_node.hxx:138
PS_ProbeSet::const_iterator PS_ProbeSetCIter
Definition: ps_node.hxx:52
size_t countProbes() const
Definition: ps_node.hxx:158
STL namespace.
PS_ProbeSetCIter getProbesBegin() const
Definition: ps_node.hxx:176
PS_NodeMap::const_iterator PS_NodeMapConstIterator
Definition: ps_node.hxx:58
void print()
Definition: ps_node.hxx:199
short int quality
Definition: ps_node.hxx:17
char buffer[MESSAGE_BUFFERSIZE]
Definition: seq_search.cxx:34
void addProbesInverted(PS_ProbeSetCIter _begin, PS_ProbeSetCIter _end)
Definition: ps_node.hxx:146
PS_ProbeSetCIter getProbesEnd() const
Definition: ps_node.hxx:180
PS_NodeMap::reverse_iterator PS_NodeMapRIterator
Definition: ps_node.hxx:57
PS_NodeMapIterator getChildrenEnd()
Definition: ps_node.hxx:118
PS_ProbeSet * PS_ProbeSetPtr
Definition: ps_node.hxx:50
Generic smart pointer.
Definition: smartptr.h:149
void printOnlyMe() const
Definition: ps_node.hxx:214
PS_NodeMapConstRIterator getChildrenREnd() const
Definition: ps_node.hxx:121
map< SpeciesID, PS_NodePtr > PS_NodeMap
Definition: ps_node.hxx:55
fputc('\n', stderr)
bool hasInverseProbes() const
Definition: ps_node.hxx:168
void removeProbes()
Definition: ps_node.hxx:189
unsigned short int GC_content
Definition: ps_node.hxx:19
SmartPtr< PS_Probe > PS_ProbePtr
Definition: ps_node.hxx:22
PS_NodeMapConstRIterator getChildrenRBegin() const
Definition: ps_node.hxx:117
bool addProbe(const PS_ProbePtr &probe)
Definition: ps_node.hxx:126
PS_NodeMap::const_reverse_iterator PS_NodeMapConstRIterator
Definition: ps_node.hxx:59
pair< bool, const PS_NodePtr > getChild(SpeciesID id) const
Definition: ps_node.hxx:106
bool hasChildren() const
Definition: ps_node.hxx:112
bool addChild(PS_NodePtr &_child)
Definition: ps_node.hxx:77
PS_ProbeSet::iterator PS_ProbeSetIter
Definition: ps_node.hxx:51
bool operator()(const PS_ProbePtr &p1, const PS_ProbePtr &p2) const
Definition: ps_node.hxx:33
void PS_printProbe(const PS_Probe *p)
Definition: ps_node.hxx:24
PS_NodeMapRIterator getChildrenREnd()
Definition: ps_node.hxx:119
~PS_Node()
Definition: ps_node.hxx:242
unsigned short int length
Definition: ps_node.hxx:18
#define abs(x)
Definition: f2c.h:151
#define NULp
Definition: cxxforward.h:116
pair< bool, PS_NodePtr > getChild(SpeciesID id)
Definition: ps_node.hxx:101
PS_NodeMapConstIterator getChildrenBegin() const
Definition: ps_node.hxx:116
bool hasProbes() const
Definition: ps_node.hxx:159
#define ps_assert(cond)
Definition: ps_assert.hxx:18
bool hasPositiveProbes() const
Definition: ps_node.hxx:160
set< PS_ProbePtr, lt_probe > PS_ProbeSet
Definition: ps_node.hxx:49
PS_NodeMap::iterator PS_NodeMapIterator
Definition: ps_node.hxx:56
PS_NodeMapRIterator getChildrenRBegin()
Definition: ps_node.hxx:115