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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
/*
This file is a part of KMC software distributed under GNU GPL 3 licence.
The homepage of the KMC project is http://sun.aei.polsl.pl/kmc
Authors: Marek Kokot
Version: 3.2.4
Date : 2024-02-09
*/
#include <algorithm>
#include <cstring>
#include "defs.h"
#include "fastq_reader.h"
//************************************************************************************************************
// CFastqReader - reader class
//************************************************************************************************************
uint64 CFastqReader::OVERHEAD_SIZE = 1 << 16;
//----------------------------------------------------------------------------------
// Constructor of FASTA/FASTQ reader
// Parameters:
// * _mm - pointer to memory monitor (to check the memory limits)
CFastqReader::CFastqReader(CMemoryPool *_pmm_fastq, CFilteringParams::file_type _file_type, uint32 _gzip_buffer_size, int _kmer_len)
{
pmm_fastq = _pmm_fastq;
file_type = _file_type;
kmer_len = _kmer_len;
// Input file mode (default: uncompressed)
mode = m_plain;
// Pointers to input files in various formats (uncompressed, gzip-compressed)
in = NULL;
in_gzip = NULL;
// Size and pointer for the buffer
part_size = 1 << 23;
part = NULL;
gzip_buffer_size = _gzip_buffer_size;
}
//----------------------------------------------------------------------------------
// Destructor - close the files
CFastqReader::~CFastqReader()
{
if(mode == m_plain)
{
if(in)
fclose(in);
}
else if(mode == m_gzip)
{
if(in_gzip)
gzclose(in_gzip);
}
if(part)
pmm_fastq->free(part);
}
//----------------------------------------------------------------------------------
// Set the name of the file to process
bool CFastqReader::SetNames(string _input_file_name)
{
input_file_name = _input_file_name;
// Set mode according to the extension of the file name
if(input_file_name.size() > 3 && string(input_file_name.end()-3, input_file_name.end()) == ".gz")
mode = m_gzip;
else
mode = m_plain;
return true;
}
//----------------------------------------------------------------------------------
// Set part size of the buffer
bool CFastqReader::SetPartSize(uint64 _part_size)
{
if(in || in_gzip)
return false;
if(_part_size < (1 << 20) || _part_size > (1 << 30))
return false;
part_size = _part_size;
return true;
}
//----------------------------------------------------------------------------------
// Open the file
bool CFastqReader::OpenFiles()
{
if(in || in_gzip)
return false;
// Uncompressed file
if(mode == m_plain)
{
if((in = fopen(input_file_name.c_str(), "rb")) == NULL)
return false;
}
// Gzip-compressed file
else if(mode == m_gzip)
{
if((in_gzip = gzopen(input_file_name.c_str(), "rb")) == NULL)
return false;
gzbuffer(in_gzip, gzip_buffer_size);
}
// Reserve via PMM
pmm_fastq->reserve(part);
part_filled = 0;
return true;
}
//----------------------------------------------------------------------------------
// Read a part of the file
bool CFastqReader::GetPart(uchar *&_part, uint64 &_size)
{
if(!in && !in_gzip)
return false;
if(IsEof())
return false;
uint64 readed;
// Read data
if(mode == m_plain)
readed = fread(part+part_filled, 1, part_size - part_filled, in);
else if(mode == m_gzip)
readed = gzread(in_gzip, part+part_filled, (int) (part_size - part_filled));
else
readed = 0; // Never should be here
int64 total_filled = part_filled + readed;
int64 i;
//Commented to finish in the next iteration to check if file is valid FASTA or FASTQ, related to #114
//if(IsEof())
//{
// _part = part;
// _size = total_filled;
//
// part = nullptr;
// return true;
//}
// Look for the end of the last complete record in a buffer
if(file_type == CFilteringParams::file_type::fasta) // FASTA files
{
// Looking for a FASTA record at the end of the area
i = total_filled - 1;
int64 start, end;
int64 line_start[4], line_end[4];
int readed_lines = 0;
bool success = false;
int k;
while (i >= 0 && readed_lines < 4)
{
GetFullLineFromEnd(start, end, part, i);
line_start[4 - readed_lines - 1] = start;
line_end[4 - readed_lines - 1] = end;
++readed_lines;
if (readed_lines >= 2)
{
k = 4 - readed_lines;
if (part[line_start[k]] == '>')
{
success = true;
break;
}
}
}
// Looking for a FASTQ record at the end of the area
if (!success)
{
cerr << "Error: Wrong input file!\n";
exit(1);
}
_part = part;
_size = line_end[k + 1];
}
else
{
i = total_filled - 1;
int64 start, end;
int64 line_start[8], line_end[8];
int readed_lines = 0;
bool success = false;
int k;
while (i >= 0 && readed_lines < 8)
{
GetFullLineFromEnd(start, end, part, i);
line_start[8 - readed_lines - 1] = start;
line_end[8 - readed_lines - 1] = end;
++readed_lines;
if (readed_lines >= 4)
{
k = 8 - readed_lines;
if (part[line_start[k]] == '@' && part[line_start[k + 2]] == '+')
{
if (part[line_start[k + 2] + 1] == '\n' || part[line_start[k + 2] + 1] == '\r')
{
success = true;
break;
}
if (line_start[k + 1] - line_start[k] == line_start[k + 3] - line_start[k + 2] &&
memcmp(part + line_start[k] + 1, part + line_start[k + 2] + 1, line_start[k + 3] - line_start[k + 2] - 1) == 0)
{
success = true;
break;
}
}
}
}
if (!success)
{
cerr << "Error: Wrong input file!\n";
exit(1);
}
_part = part;
_size = line_end[k + 3];
}
// Allocate new memory for the buffer
pmm_fastq->reserve(part);
copy(_part+_size, _part+total_filled, part);
part_filled = total_filled - _size;
return true;
}
//----------------------------------------------------------------------------------
// Skip to next EOL from the current position in a buffer
bool CFastqReader::SkipNextEOL(uchar *part, int64 &pos, int64 max_pos)
{
int64 i;
for(i = pos; i < max_pos-2; ++i)
if((part[i] == '\n' || part[i] == '\r') && !(part[i+1] == '\n' || part[i+1] == '\r'))
break;
if(i >= max_pos-2)
return false;
pos = i+1;
return true;
}
void CFastqReader::GetFullLineFromEnd(int64& line_sart, int64& line_end, uchar* buff, int64& pos)
{
while (pos >= 0 && buff[pos] != '\n' && buff[pos] != '\r')
--pos;
line_end = pos + 1;
while (pos >= 0 && (buff[pos] == '\n' || buff[pos] == '\r'))
--pos;
while (pos >= 0 && buff[pos] != '\n' && buff[pos] != '\r')
--pos;
line_sart = pos + 1;
};
//----------------------------------------------------------------------------------
// Check whether there is an EOF
bool CFastqReader::IsEof()
{
if(mode == m_plain)
return feof(in) != 0;
else if(mode == m_gzip)
return gzeof(in_gzip) != 0;
return true;
}
//************************************************************************************************************
// CWFastqReader - wrapper for multithreading purposes
//************************************************************************************************************
CWFastqReader::CWFastqReader(CFilteringParams &Params, CFilteringQueues &Queues)
{
pmm_fastq = Queues.pmm_fastq_reader;
input_files_queue = Queues.input_files_queue;
part_size = Params.fastq_buffer_size;
part_queue = Queues.input_part_queue;
file_type = Params.input_file_type;
kmer_len = Params.kmer_len;
gzip_buffer_size = Params.gzip_buffer_size;
fqr = nullptr;
}
//----------------------------------------------------------------------------------
CWFastqReader::~CWFastqReader()
{
}
//----------------------------------------------------------------------------------
void CWFastqReader::operator()()
{
uchar *part;
uint64 part_filled;
while(input_files_queue->pop(file_name))
{
fqr = new CFastqReader(pmm_fastq, file_type, gzip_buffer_size, kmer_len);
fqr->SetNames(file_name);
fqr->SetPartSize(part_size);
if(fqr->OpenFiles())
{
// Reading Fastq parts
while(fqr->GetPart(part, part_filled))
part_queue->push(part, part_filled);
}
else
cerr << "Error: Cannot open file " << file_name << "\n";
delete fqr;
}
part_queue->mark_completed();
}
// ***** EOF
|