File: FastaIndex.h

package info (click to toggle)
abyss 2.3.5%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,476 kB
  • sloc: cpp: 169,166; ansic: 6,828; java: 2,239; makefile: 2,229; sh: 1,159; perl: 672; haskell: 412; python: 72
file content (181 lines) | stat: -rw-r--r-- 4,164 bytes parent folder | download | duplicates (8)
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
#ifndef FASTA_INDEX_H
#define FASTA_INDEX_H 1

#include "IOUtil.h"
#include <boost/tuple/tuple.hpp>
#include <algorithm>
#include <cassert>
#include <fstream>
#include <iterator> // for ostream_iterator
#include <string>
#include <vector>

/** A record of an indexed FASTA file. */
struct FAIRecord
{
	size_t offset;
	size_t size;
	std::string id;

	FAIRecord() : offset(0), size(0) { }
	FAIRecord(size_t offset, size_t size, const std::string& id)
		: offset(offset), size(size), id(id) { }

	friend std::ostream& operator<<(std::ostream& out,
			const FAIRecord& o)
	{
		return out << o.id << '\t' << o.size << '\t' << o.offset
			<< '\t' << o.size << '\t' << o.size + 1;
	}

	friend std::istream& operator>>(std::istream& in,
			FAIRecord& o)
	{
		size_t lineLen, lineBinLen;
		in >> o.id >> o.size >> o.offset >> lineLen >> lineBinLen;
		if (!in)
			return in;
		assert(o.size == lineLen || lineLen == lineBinLen);
		return in >> Ignore('\n');
	}
};

/** An indexed FASTA (fai) file. */
class FastaIndex
{
	typedef std::vector<FAIRecord> Data;

	struct CompareOffset
	{
		/** Used with upper_bound. */
		bool operator()(size_t a, const FAIRecord& b) const
		{
			return a < b.offset;
		}
	};

  public:
	typedef boost::tuple<const FAIRecord&, size_t> SeqPos;

	/** Return the number of contigs. */
	size_t size() { return m_data.size(); }

	/** Return the size of the FASTA file. */
	size_t fileSize() const
	{
		assert(!m_data.empty());
		return m_data.back().offset + m_data.back().size + 1;
	}

	typedef Data::const_iterator const_iterator;
	const_iterator begin() const { return m_data.begin(); }
	const_iterator end() const { return m_data.end(); }

	/** Index the specified FASTA file. */
	void index(const std::string& path)
	{
		m_data.clear();
		std::ifstream in(path.c_str());
		assert_good(in, path);
		char c;
		for (std::string id;
				in >> c && in >> id && in >> Ignore('\n');) {
			assert(c == '>');
			assert(!id.empty());
			std::streampos offset = in.tellg();
			assert(offset > 0);
			in >> Ignore('\n');
			size_t n = in.gcount();
			assert(n > 0);
			m_data.push_back(FAIRecord(offset, n - 1, id));
		}
		assert(in.eof());
	}

	/** Translate a file offset to a sequence:position coordinate. */
	SeqPos operator[](size_t offset) const
	{
		Data::const_iterator it = std::upper_bound(
				m_data.begin(), m_data.end(),
				offset, CompareOffset());
		assert(it != m_data.begin());
		--it;
		assert(it != m_data.end());
		assert(it->offset <= offset);
		assert(offset < it->offset + it->size);
		return SeqPos(*it, offset - it->offset);
	}

	/** Write FASTA headers to the specified seekable stream. */
	void writeFASTAHeaders(std::ostream& out) const
	{
		assert(out);
		if (!out.seekp(0))
			return;
		for (Data::const_iterator it = m_data.begin();
				it != m_data.end(); ++it) {
			out << '>' << it->id << ' ';
			assert(it->offset > 0);
			if (!out.seekp(it->offset - 1))
				break;
			out << '\n';
			if (!out.seekp(it->offset + it->size))
				break;
			out << '\n';
		}
	}

	/** Write this index to a stream in SAM format. */
	void writeSAMHeader(std::ostream& out) const
	{
		for (Data::const_iterator it = m_data.begin();
				it != m_data.end(); ++it)
			out << "@SQ\tSN:" << it->id
				<< "\tLN:" << it->size << '\n';
	}

	/** Write this index to a stream. */
	friend std::ostream& operator<<(std::ostream& out,
			const FastaIndex& o)
	{
		std::copy(o.m_data.begin(), o.m_data.end(),
				std::ostream_iterator<FAIRecord>(out, "\n"));
		return out;
	}

	/** Read a FASTA index from a stream. */
	friend std::istream& operator>>(std::istream& in,
			FastaIndex& o)
	{
		assert(in.good());
		o.m_data.clear();

		// Count the number of records.
		if (in.seekg(0, std::ios::beg)) {
			size_t n = 0;
			while (in)
				if (in.get() == '\n')
					n++;
			o.m_data.reserve(n);
			in.clear();
			in.seekg(0, std::ios::beg);
			assert(in.good());
		} else
			in.clear();

		// Read the records.
		for (FAIRecord rec; in >> rec;) {
			if (!o.m_data.empty())
				assert(rec.offset > o.m_data.back().offset);
			o.m_data.push_back(rec);
		}
		assert(in.eof());
		assert(!o.m_data.empty());
		return in;
	}

  private:
	Data m_data;
};

#endif