File: VertexData.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 (172 lines) | stat: -rw-r--r-- 3,785 bytes parent folder | download | duplicates (6)
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
#ifndef ASSEMBLY_VERTEXDATA_H
#define ASSEMBLY_VERTEXDATA_H 1

#include "Common/Sense.h"
#include <cassert>
#include <stdint.h>
#include <ostream>

enum SeqFlag
{
	SF_MARK_SENSE = 0x1,
	SF_MARK_ANTISENSE = 0x2,
	SF_DELETE = 0x4,
};

static inline SeqFlag complement(SeqFlag flag)
{
	unsigned out = 0;
	if (flag & SF_MARK_SENSE)
		out |= SF_MARK_ANTISENSE;
	if (flag & SF_MARK_ANTISENSE)
		out |= SF_MARK_SENSE;
	if (flag & SF_DELETE)
		out |= SF_DELETE;
	return SeqFlag(out);
}

/** Vertex properties of a de Bruijn Graph vertex. */
template <typename S, typename Set>
class VertexData
{
/** Maximum value of k-mer coverage. */
#define COVERAGE_MAX 32767U

  public:
	/** A symbol, such as a nucleotide or amino acid. */
	typedef S Symbol;

	/** A set of symbols. */
	typedef Set SymbolSet;

	/** A pair of edge sets; one for out edges and one for in edges. */
	struct SymbolSetPair
	{
		SymbolSet dir[2];
		SymbolSetPair complement() const
		{
			SymbolSetPair o;
			o.dir[SENSE] = dir[ANTISENSE].complement();
			o.dir[ANTISENSE] = dir[SENSE].complement();
			return o;
		}
	};

	VertexData() : m_flags(0)
	{
		m_multiplicity[SENSE] = 1;
		m_multiplicity[ANTISENSE] = 0;
	}

	VertexData(extDirection dir, unsigned multiplicity) : m_flags(0)
	{
		assert(multiplicity <= COVERAGE_MAX);
		m_multiplicity[dir] = multiplicity;
		m_multiplicity[!dir] = 0;
	}

	VertexData(unsigned multiplicity, SymbolSetPair ext)
		: m_flags(0), m_ext(ext)
	{
		setMultiplicity(multiplicity);
	}

	unsigned getMultiplicity(extDirection dir) const
	{
		return m_multiplicity[dir];
	}

	unsigned getMultiplicity() const
	{
		return m_multiplicity[SENSE] + m_multiplicity[ANTISENSE];
	}

	void addMultiplicity(extDirection dir, unsigned n = 1)
	{
		m_multiplicity[dir]
			= std::min(m_multiplicity[dir] + n, COVERAGE_MAX);
		assert(m_multiplicity[dir] > 0);
	}

	/** Set the multiplicity (not strand specific). */
	void setMultiplicity(unsigned multiplicity)
	{
		assert(multiplicity <= 2*COVERAGE_MAX);
		// Split the multiplicity over both senses.
		m_multiplicity[SENSE] = (multiplicity + 1) / 2;
		m_multiplicity[ANTISENSE] = multiplicity / 2;
		assert(getMultiplicity() == multiplicity);
	}

	void setFlag(SeqFlag flag) { m_flags |= flag; }
	bool isFlagSet(SeqFlag flag) const { return m_flags & flag; }
	void clearFlag(SeqFlag flag) { m_flags &= ~flag; }

	/** Return true if the specified sequence is deleted. */
	bool deleted() const { return isFlagSet(SF_DELETE); }

	/** Return true if the specified sequence is marked. */
	bool marked(extDirection sense) const
	{
		return isFlagSet(sense == SENSE
				? SF_MARK_SENSE : SF_MARK_ANTISENSE);
	}

	/** Return true if the specified sequence is marked. */
	bool marked() const
	{
		return isFlagSet(SeqFlag(SF_MARK_SENSE | SF_MARK_ANTISENSE));
	}

	SymbolSetPair extension() const { return m_ext; }

	SymbolSet getExtension(extDirection dir) const
	{
		return m_ext.dir[dir];
	}

	void setBaseExtension(extDirection dir, Symbol x)
	{
		m_ext.dir[dir].setBase(x);
	}

	void removeExtension(extDirection dir, SymbolSet ext)
	{
		m_ext.dir[dir].clear(ext);
	}

	bool hasExtension(extDirection dir) const
	{
		return m_ext.dir[dir].hasExtension();
	}

	bool isAmbiguous(extDirection dir) const
	{
		return m_ext.dir[dir].isAmbiguous();
	}

	/** Return the complement of this data. */
	VertexData operator~() const
	{
		VertexData o;
		o.m_flags = complement(SeqFlag(m_flags));
		o.m_multiplicity[0] = m_multiplicity[1];
		o.m_multiplicity[1] = m_multiplicity[0];
		o.m_ext = m_ext.complement();
		return o;
	}

	friend std::ostream& operator<<(
			std::ostream& out, const VertexData& o)
	{
		return out << "C="
			<< o.m_multiplicity[0] + o.m_multiplicity[1];
	}

  private:
	uint8_t m_flags;
	uint16_t m_multiplicity[2];
	SymbolSetPair m_ext;
};

#endif