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
|
#include "kindex.h"
//////////////////////////////
////////// KixBuild //////////
//////////////////////////////
int KixBuild::create_index( const std::string &fname, const std::string &iname, bool convert_spaces, bool trim_ids ) {
// Add all sequences of a (multi-)fasta file to the set of sequences.
try {
if ( add_fasta(fname, convert_spaces, trim_ids) == 0 )
return 1;
} catch ( std::ifstream::failure &readErr ) {
std::cerr << readErr.what() << std::endl;
return 2;
}
// save the metadata
if ( ! save_metadata(iname) ) {
std::cerr << "Writing metadata failed." << std::endl;
return 3;
}
// modify sequences for index building (reverse and add reverse complement)
seqan2::StringSet<seqan2::DnaString> all_seqs;
for(uint16_t i = 0; i < length(seqs); i++) {
seqan2::ModifiedString<seqan2::DnaString, seqan2::ModReverse> rev(seqs[i]);
seqan2::appendValue(all_seqs, rev);
seqan2::complement(seqs[i]);
seqan2::appendValue(all_seqs, seqs[i]);
}
// Define the index
FMIndex idx(all_seqs);
// Force the index to be created
if( ! seqan2::indexCreate(idx) )
return 4;
// Save the index to file
if ( save_fmindex(idx, iname) != 1 )
return 5;
return 0;
}
int KixBuild::add_fasta(const std::string &fname, bool convert_spaces, bool trim_ids) {
std::ios::sync_with_stdio(false);
std::ifstream::sync_with_stdio(false);
// open input fasta file
std::ifstream infile (fname.c_str());
if ( !infile.is_open() ) {
throw std::ifstream::failure("Could not open fasta file " + fname + ".");
}
std::string line; // current line of fasta file
std::string seq_name; // name of the current sequence
bool startNewSequence = false; // true if last line was a header / new sequence begins
seqan2::DnaString newSeq = ""; // current DNA sequence
while(getline(infile, line)) {
// ignore empty lines
if (line.length() == 0) {continue;};
// check for Windows newline characters (just in case the fasta comes from Windows --> thanks Simon)
if (line[line.length()-1] == '\r'){
line.erase(line.length()-1);
}
// handle header line
if (line[0] == '>') {
// add last sequence to sequence vector (if exist)
if ( newSeq != "" ) {
seqan2::appendValue(seqs, newSeq);
}
// trim the header (remove leading and trailing whitespaces)
trim(line);
// handle sequence name options
if (convert_spaces)
std::replace( line.begin(), line.end(), ' ', '_');
if (trim_ids)
seq_name = line.substr(1,line.find(' ')-1);
else
seq_name = line.substr(1,line.length()-1);
// add the sequence name to the sequence name vector
seq_names.push_back(seq_name);
// init new sequence fields
seq_lengths.push_back(0);
startNewSequence = true;
newSeq = "";
}
// handle sequence lines
else {
// TODO: remove leading and trailling Ns
// start new sequence
if (startNewSequence) {
newSeq = line;
startNewSequence = false;
}
// extend existing sequence
else {
newSeq += line;
}
*(--seq_lengths.end())+=line.length();
}
}
infile.close();
// append the last sequence to the StringSet
if ( newSeq != "" ) {
seqan2::appendValue(seqs, newSeq);
}
return seq_lengths.size();
}
int KixBuild::save_fmindex( FMIndex & idx, const std::string &iname ) {
const char * filename = iname.c_str();
return seqan2::save(idx, filename);
}
int KixBuild::save_metadata( const std::string &iname ) {
bool success = save_seqnames(iname);
success = success && save_seqlengths(iname);
return success;
}
int KixBuild::save_seqnames( const std::string &iname ) {
std::string file_name = iname;
file_name += ".seqnames";
// get total amount of bytes needed
uint32_t data_size = sizeof(uint32_t); // number of sequences
for ( CountType i = 0; i < seq_names.size(); i++ ) {
data_size += sizeof(uint32_t); // length of the name
data_size += seq_names[i].size();
}
// init data vector
std::vector<char> data(data_size);
char* d = data.data();
// write number of sequences
uint32_t size = seq_names.size();
memcpy(d,&size,sizeof(uint32_t));
d += sizeof(uint32_t);
// write all sequence names
for ( uint32_t i = 0; i < seq_names.size(); i++ ) {
// write sequence name length
size = seq_names[i].size();
memcpy(d,&size,sizeof(uint32_t));
d += sizeof(uint32_t);
const char* seqn = seq_names[i].c_str();
// write sequence name
memcpy(d,seqn,seq_names[i].size());
d += seq_names[i].size();
}
// write data to file
uint32_t written = write_binary_file(file_name, data);
return ( written == data_size );
}
int KixBuild::save_seqlengths( const std::string &iname ) {
std::string file_name = iname;
file_name += ".seqlengths";
// get total amount of bytes needed
uint32_t data_size = sizeof(uint32_t); // number of sequences
for ( CountType i = 0; i < seq_lengths.size(); i++ ) {
data_size += sizeof(uint32_t); // sequence length
}
// init data vector
std::vector<char> data(data_size);
char* d = data.data();
// write number of sequences
uint32_t seqlen = seq_lengths.size();
memcpy(d,&seqlen,sizeof(uint32_t));
d += sizeof(uint32_t);
// write all sequence names
for ( uint32_t i = 0; i < seq_lengths.size(); i++ ) {
// write sequence length
seqlen = seq_lengths[i];
memcpy(d,&seqlen,sizeof(uint32_t));
d += sizeof(uint32_t);
}
// write data to file
uint32_t written = write_binary_file(file_name, data);
return ( written == data_size );
}
////////////////////////////
////////// KixRun //////////
////////////////////////////
int KixRun::load_fmindex( std::string index_name ) {
const char * filename = index_name.c_str();
if ( seqan2::open(idx, filename) != 1 ) // function returns 1 on success
return 1;
return 0;
}
int KixRun::load_metadata( const std::string &iname ) {
bool success = load_seqnames(iname);
success = success && load_seqlengths(iname);
return success;
}
int KixRun::load_seqnames( const std::string &iname ) {
std::string file_name = iname;
file_name += ".seqnames";
std::vector<char> data = read_binary_file(file_name);
char* d = data.data();
uint32_t num_seqs;
memcpy(&num_seqs, d, sizeof(uint32_t));
d += sizeof(uint32_t);
uint32_t curr_seqLength;
for ( uint32_t i=0; i<num_seqs; i++) {
memcpy(&curr_seqLength, d, sizeof(uint32_t));
d += sizeof(uint32_t);
char* seq_name = new char[curr_seqLength];
memcpy(seq_name, d, curr_seqLength);
d += curr_seqLength;
// Don't know why the substring is necessary here, but without it there are strange artifacts in some names.
seq_names.push_back(std::string(seq_name).substr(0,curr_seqLength));
}
return true;
}
int KixRun::load_seqlengths( const std::string &iname ) {
std::string file_name = iname;
file_name += ".seqlengths";
std::vector<char> data = read_binary_file(file_name);
char* d = data.data();
uint32_t num_seqs;
memcpy(&num_seqs, d, sizeof(uint32_t));
d += sizeof(uint32_t);
uint32_t seqlength;
for ( uint32_t i=0; i<num_seqs; i++) {
memcpy(&seqlength, d, sizeof(uint32_t));
d += sizeof(uint32_t);
seq_lengths.push_back(seqlength);
}
return true;
}
|