File: ContigNode.h

package info (click to toggle)
abyss 2.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,284 kB
  • sloc: cpp: 78,182; ansic: 6,512; makefile: 2,252; perl: 672; sh: 509; haskell: 412; python: 4
file content (306 lines) | stat: -rw-r--r-- 6,750 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#ifndef CONTIGNODE_H
#define CONTIGNODE_H 1

#include "config.h" // for WORDS_BIGENDIAN
#include "ContigID.h"
#include "Graph/Properties.h"
#include "StringUtil.h"
#include <boost/property_map/property_map.hpp>
#include <cassert>
#include <cstdlib> // for strtoul
#include <iostream>
#include <stdint.h> // for intptr_t
#include <string>
#include <utility>

/** A vertex of a contig graph, which is a pair of a contig index and
 * an orientation.
 */
class ContigNode
{
public:

ContigNode() : m_index(0) { }

ContigNode(const ContigNode& o) : m_index(o.m_index) { }

/** Construct from a vertex index. */
explicit ContigNode(unsigned index) : m_index(index) { }

/** Construct from a contig index and an orientation. */
ContigNode(unsigned index, bool sense)
	: m_index(2 * index + sense) { }

/** Construct from a contig index and an orientation. */
ContigNode(unsigned index, int sense)
	: m_index(2 * index + sense)
{
	assert(sense == 0 || sense == 1);
}

/** Construct an ambiguous ContigNode. */
ContigNode(unsigned n, char c)
	: m_index(-(int)n)
{
	assert(n > 0);
	assert(c == 'N');
	(void)c;
}

bool operator==(const ContigNode& o) const
{
	return m_index == o.m_index;
}

bool operator!=(const ContigNode& o) const
{
	return m_index != o.m_index;
}

bool operator<(const ContigNode& o) const
{
	return m_index < o.m_index;
}

/** Return the complement of this vertex if sense is true. */
ContigNode operator^(bool sense) const
{
	assert(!ambiguous());
	return ContigNode(m_index ^ sense);
}

/** Copy constructors */
ContigNode(ContigNode&&) = default;
ContigNode& operator=(const ContigNode&) = default;
ContigNode& operator=(ContigNode&&) = default;

/** Return whether this ContigNode is ambiguous. */
bool ambiguous() const
{
	return m_index < 0;
}

/** Return the vertex index. */
unsigned index() const
{
	assert(!ambiguous());
	return m_index;
}

/** Return the contig index. */
unsigned id() const
{
	return ambiguous() ? m_index : m_index / 2;
}

/** Return the contig index as a ContigID. */
ContigID contigIndex() const
{
	assert(!ambiguous());
	return ContigID(id());
}

/** Return the orientation of this vertex. */
bool sense() const
{
	assert(!ambiguous());
	return m_index & 1;
}

/** Return the length in k-mer of this ambiguous contig. */
unsigned length() const
{
	assert(ambiguous());
	return -m_index;
}

/** Return the string of Ns. */
std::string ambiguousSequence() const
{
	assert(ambiguous());
	unsigned n = length();
	if (n > 100000) {
		std::cerr
			<< "warning: scaffold gap is longer than 100 kbp: "
			<< n << '\n';
	} else if (n > 1000000) {
		std::cerr << "error: scaffold gap is longer than 1 Mbp: "
			<< n << '\n';
		exit(EXIT_FAILURE);
	}
	return std::string(n, 'N');
}

/** Toggle the orientation of this vertex if sense is true. */
ContigNode& operator^=(bool sense)
{
	assert(!ambiguous());
	m_index ^= sense;
	return *this;
}

/** Increment this vertex index. */
ContigNode& operator++()
{
	assert(!ambiguous());
	++m_index;
	return *this;
}

private:
	int m_index;
};

/** Return the hash value of this ContigNode. */
static inline unsigned hash_value(const ContigNode& o)
{
	return o.index();
}

/** Vertex index property map of a ContigNode. */
struct ContigNodeIndexMap
	: boost::put_get_helper<unsigned, ContigNodeIndexMap>
{
	typedef ContigNode key_type;
	typedef unsigned value_type;
	typedef value_type reference;
	typedef boost::readable_property_map_tag category;

	reference operator[](const key_type& u) const
	{
		return u.index();
	}
};

/** Contig index property map of a ContigNode. */
struct ContigIndexMap
	: boost::put_get_helper<ContigID, ContigIndexMap>
{
	typedef ContigNode key_type;
	typedef ContigID value_type;
	typedef value_type reference;
	typedef boost::readable_property_map_tag category;

	reference operator[](const key_type& u) const
	{
		return u.contigIndex();
	}
};

/** Return the contig name of the specified vertex. */
template <typename Graph>
Dictionary::name_reference
get(vertex_contig_name_t, const Graph&, ContigNode u)
{
	assert(!u.ambiguous());
	return get(g_contigNames, u.id());
}

/** The string representation of a vertex name. */
struct VertexName : std::pair<intptr_t, char>
{
	typedef std::pair<intptr_t, char> Base;

	VertexName(const char* s, char c)
		: Base(reinterpret_cast<intptr_t>(s), c)
	{
		assert(c == '+' || c == '-');
	}

	VertexName(unsigned n, char c) : Base(n, c)
	{
		assert(c == 'N');
	}

	friend std::ostream& operator<<(
			std::ostream& out, const VertexName& o)
	{
		if (o.second == 'N')
			out << o.first;
		else
			out << reinterpret_cast<const char*>(o.first);
		return out << o.second;
	}
};

/** Return the name of the specified vertex. */
static inline VertexName get(const Dictionary& pmap, ContigNode u)
{
	return u.ambiguous()
		? VertexName(u.length(), 'N')
		: VertexName(get(pmap, u.id()), u.sense() ? '-' : '+');
}

/** Return the name of the specified vertex. */
template <typename Graph>
VertexName get(vertex_name_t, const Graph&, ContigNode u)
{
	return get(g_contigNames, u);
}

/** Set the name of the specified vertex. */
template <typename Graph>
void put(vertex_name_t, const Graph&, ContigNode u,
		const std::string& name)
{
	assert(!name.empty());
	char c = name[name.size() - 1];
	if (c == '+' || c == '-')
		put(g_contigNames, u.id(), name.substr(0, name.size() - 1));
	else
		put(g_contigNames, u.id(), name);
}

/** The string representation of an edge name. */
struct EdgeName : std::pair<VertexName, VertexName>
{
	typedef std::pair<VertexName, VertexName> Base;

	EdgeName(Base::first_type a, Base::second_type b)
		: Base(a, b) { }

	friend std::ostream& operator<<(
			std::ostream& out, const EdgeName& o)
	{
		return out << '"' << o.first << "\" -> \"" << o.second << '"';
	}
};

/** Return the name of the specified edge. */
static inline EdgeName get(const Dictionary& pmap,
		std::pair<ContigNode, ContigNode> e)
{
	return EdgeName(get(pmap, e.first), get(pmap, e.second));
}

/** Return the name of the specified edge. */
template <typename Graph>
EdgeName get(edge_name_t, const Graph& g,
		std::pair<ContigNode, ContigNode> e)
{
	return EdgeName(
			get(vertex_name, g, source(e, g)),
			get(vertex_name, g, target(e, g)));
}

/** Find the vertex with the specified name. */
static inline ContigNode find_vertex(
		std::string name, bool sense, const Dictionary& pmap)
{
	assert(!name.empty());
	return ContigNode(get(pmap, name), sense);
}

/** Find the vertex with the specified name. */
static inline ContigNode find_vertex(
		std::string name, const Dictionary& pmap)
{
	assert(!name.empty());
	char c = chop(name);
	assert(c == '+' || c == '-' || c == 'N');
	return c == 'N'
		? ContigNode(strtoul(name.c_str(), NULL, 0), 'N')
		: find_vertex(name, c == '-', pmap);
}

#endif