File: sequence_io.h

package info (click to toggle)
bowtie 1.2.3+dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 16,888 kB
  • sloc: cpp: 35,784; perl: 5,903; ansic: 1,247; sh: 1,180; python: 487; makefile: 426
file content (174 lines) | stat: -rw-r--r-- 5,397 bytes parent folder | download | duplicates (5)
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
#ifndef SEQUENCE_IO_H_
#define SEQUENCE_IO_H_

#include <string>
#include <vector>
#include <stdexcept>
#include <fstream>
#include <stdio.h>
#include <seqan/sequence.h>
#include <seqan/file.h>
#include "assert_helpers.h"
#include "pat.h"

/**
 * Read a sequence file of the given format and alphabet type.  Store
 * all of the extracted sequences in vector ss.  Note that SeqAn's
 * policy for when it encounters characters not from the specified
 * alphabet is to convert them to the lexicographically smallest
 * character in the alphabet.
 */
template <typename TStr, typename TFile>
static void readSequenceFile(const std::string& infile,
                             std::vector<TStr>& ss,
                             int64_t& baseCutoff, // limit for total bases
                             int seqCutoff = -1,  // limit for sequences
                             bool reverse = false)
{
	typedef typename Value<TStr>::Type TVal;
	static char buf[256 * 1024]; // fairly large input buffer
	if(baseCutoff <= 0) return;
	FILE *in = fopen(infile.c_str(), "r");
	if(in == NULL) {
		cerr << "Could not open sequence file" << endl;
		throw 1;
	}
	// Associate large input buffer with FILE *in
	if(setvbuf(in, buf, _IOFBF, 256 * 1024) != 0) {
		cerr << "Could not create input buffer for sequence file" << endl;
		throw 1;
	}
	// Read entries using SeqAn
	int cnt = 0;
	while(!feof(in)) {
		while(true) {
			ss.push_back(TStr()); // add a new empty string to the end
			// Fill the new empty string with the next sequence from
			// the file.  SeqAn allocates just enough mem for it (at
			// the expense of lots of file seeks, which can add up)
			seqan::read(in, ss.back(), TFile()); 
			if(seqan::empty(ss.back())) {
				ss.pop_back();
				break;
			}
			// Enforce the base cutoff
			if((int64_t)length(ss.back()) > baseCutoff) {
				resize(ss.back(), baseCutoff);
				baseCutoff = 0;
			} else {
				baseCutoff -= length(ss.back());
			}
			// Reverse the newly-read sequence in-place if desired
			if(reverse) {
				size_t len = length(ss.back());
				for(size_t i = 0; i < len/2; i++) {
					TVal t = ss.back()[i];
					ss.back()[i] = ss.back()[len-i-1];
					ss.back()[len-i-1] = t;
				}
			}
			#ifndef NDEBUG
			// Sanity check that all (int) values are in range
			for(size_t i = 0; i < length(ss.back()); i++) {
				assert_lt(ss.back()[i], (int)(ValueSize<TVal>::VALUE));
				assert_geq(ss.back()[i], 0);
			}
			#endif
			cnt++;
			// Enforce the sequence cutoff
			if(seqCutoff != -1 && cnt >= seqCutoff) {
				fclose(in);
				return;
			}
		}
	}
	fclose(in);
}

/**
 * Read a set of sequence files of the given format and alphabet type.
 * Store all of the extracted sequences in vector ss.
 */
template <typename TStr, typename TFile>
static void readSequenceFiles(const std::vector<std::string>& infiles,
                              std::vector<TStr>& ss,
                              int64_t& baseCutoff,
                              int seqCutoff = -1,
                              bool reverse = false)
{
	for(size_t i = 0; i < infiles.size() && baseCutoff > 0; i++) {
		readSequenceFile<TStr,TFile>(infiles[i], ss, baseCutoff, seqCutoff, reverse);
		if(baseCutoff <= 0) break;
	}
}

/**
 * Read a set of sequence files of the given format and alphabet type.
 * Store all of the extracted sequences in vector ss.
 */
template <typename TStr, typename TFile>
static void readSequenceFiles(const std::vector<std::string>& infiles,
                              std::vector<TStr>& ss,
                              int seqCutoff = -1,
                              bool reverse = false)
{
	int64_t i = 0xffffffffll;
	readSequenceFiles<TStr,TFile>(infiles, ss, i, seqCutoff, reverse);
}

/**
 * Parse a comma-delimited list of strings of type T into a vector. 
 */
template <typename T>
void readSequenceString(const std::string& s,
                        std::vector<T>& ss,
                        int64_t& baseCutoff,
                        int seqCutoff = -1,
                        bool reverse = false)
{
	// Split string s using comma as a delimiter.  Borrowed from C++
	// Programming HOWTO 7.3
	std::string::size_type lastPos = s.find_first_not_of(",", 0);
	std::string::size_type pos = s.find_first_of(",", lastPos);
	while (baseCutoff > 0 && (std::string::npos != pos || std::string::npos != lastPos)) {
		string stmp = s.substr(lastPos, pos - lastPos);
		if((int64_t)stmp.length() < baseCutoff) {
			baseCutoff -= stmp.length();
		} else {
			stmp = stmp.substr(0, baseCutoff);
			baseCutoff = 0;
		}
		if(reverse) {
			size_t len = stmp.length();
			for(size_t i = 0; i < len/2; i++) {
				char tmp = stmp[i];
				stmp[i] = stmp[len-i-1];
				stmp[len-i-1] = tmp;
			}
			ss.push_back(T(stmp.c_str()));
		} else {
			ss.push_back(T(stmp.c_str()));
		}
		if(seqCutoff != -1 && ss.size() >= (size_t)seqCutoff) {
			return;
		}
	    lastPos = s.find_first_not_of(",", pos);
	    pos = s.find_first_of(",", lastPos);
	}
}

/**
 * Parse a comma-delimited list of strings of type T into a vector. 
 * Doesn't require callee to supply a baseCutoff.
 */
template <typename T>
void readSequenceString(const std::string& s,
                        std::vector<T>& ss,
                        int seqCutoff = -1,
                        bool reverse = false)
{
	int64_t i = 0xffffffffll;
	readSequenceString(s, ss, i, seqCutoff, reverse);
}

#endif /*SEQUENCE_IO_H_*/