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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
|
#include "sfasta.h"
#include "orf.h"
#include "alpha.h"
#include "timing.h"
static inline bool isgap(byte c)
{
return c == '-' || c == '.';
}
const unsigned BufferSize = 16*1024*1024;
static unsigned GetMaxPoly(const byte *Seq, unsigned L)
{
byte CurrChar = Seq[0];
unsigned Start = 0;
unsigned MaxLen = 1;
for (unsigned i = 1; i < L; ++i)
{
char c = Seq[i];
if (c != CurrChar || i+1 == L)
{
unsigned Len = i - Start;
if (Len > MaxLen)
MaxLen = Len;
CurrChar = c;
Start = i;
}
}
return MaxLen;
}
SFasta::SFasta()
{
m_FileName = "";
m_File = 0;
m_Buffer = 0;
m_BufferSize = 0;
m_BufferOffset = 0;
m_BufferBytes = 0;
m_FilePos = 0;
m_FileSize = 0;
m_Label = 0;
m_SeqLength = 0;
m_TooShortCount = 0;
m_TooLongCount = 0;
m_ShortestLength = 0;
m_LongestLength = 0;
m_IsNucleo = false;
m_IsNucleoSet = false;
}
SFasta::~SFasta()
{
Clear();
}
void SFasta::Clear()
{
MYFREE(m_Buffer, m_BufferSize, SFasta);
if (m_File != 0)
CloseStdioFile(m_File);
m_FileName = "";
m_File = 0;
m_Buffer = 0;
m_BufferSize = 0;
m_BufferOffset = 0;
m_BufferBytes = 0;
m_FilePos = 0;
m_FileSize = 0;
m_Label = 0;
m_SeqLength = 0;
m_SeqIndex = UINT_MAX;
m_AllowGaps = false;
m_IsNucleo = false;
m_IsNucleoSet = false;
m_TooShortCount = 0;
m_TooLongCount = 0;
m_ShortestLength = 0;
m_LongestLength = 0;
m_TooPolyCount = 0;
}
void SFasta::LogMe() const
{
Log("\n");
Log("SFasta::LogMe()\n");
Log("FileName=%s\n", m_FileName.c_str());
Log("FileSize=%u\n", (unsigned) m_FileSize);
Log("FilePos=%u\n", (unsigned) m_FilePos);
Log("BufferSize=%u\n", m_BufferSize);
Log("BufferPos=%u\n", m_BufferOffset);
Log("BufferBytes=%u\n", m_BufferBytes);
if (m_Label == 0)
Log("Label=NULL\n");
else
Log("Label=%s\n", m_Label);
Log("SeqLength=%u\n", m_SeqLength);
}
const byte *SFasta::GetNextSeq()
{
for (;;)
{
const byte *Seq = GetNextSeqLo();
if (Seq == 0)
{
if (m_TooShortCount > 0)
Warning("%u short sequences (--minlen %u, shortest %u) discarded from %s",
m_TooShortCount, opt_minlen, m_ShortestLength, m_FileName.c_str());
if (m_TooLongCount > 0)
Warning("%u long sequences (--maxlen %u, longest %u) discarded from %s",
m_TooLongCount, opt_maxlen, m_LongestLength, m_FileName.c_str());
if (m_TooPolyCount > 0)
Warning("%u sequences with long homopolymers discarded (--maxpoly %u)",
m_TooPolyCount, opt_maxpoly);
return 0;
}
if (m_SeqLength < opt_minlen)
{
++m_TooShortCount;
if (m_ShortestLength == 0 || m_SeqLength < m_ShortestLength)
m_ShortestLength = m_SeqLength;
continue;
}
if (m_SeqLength > opt_maxlen && opt_maxlen != 0)
{
if (m_LongestLength == 0 || m_SeqLength > m_LongestLength)
m_LongestLength = m_SeqLength;
++m_TooLongCount;
continue;
}
return Seq;
}
}
const byte *SFasta::GetNextSeqLo()
{
// End of cache?
if (m_BufferOffset == m_BufferBytes)
{
// End of file?
if (m_FilePos == m_FileSize)
return 0;
FillCache();
}
StartTimer(SF_GetNextSeq);
asserta(m_Buffer[m_BufferOffset] == '>');
m_Label = (char *) (m_Buffer + m_BufferOffset + 1);
//// Scan to end-of-line.
//// Use dubious library function strchr() in the hope
//// that it uses fast machine code.
// byte *ptr = (byte *) strchr(m_Label, '\n');
// asserta(ptr != 0);
// *ptr = 0;
byte *ptr = 0;
for (unsigned i = m_BufferOffset; i < m_BufferSize; ++i)
{
char c = m_Buffer[i];
if (c == '\n' || c == '\r')
{
ptr = m_Buffer + i;
break;
}
}
asserta(ptr != 0);
if (opt_trunclabels)
{
for (char *p = m_Label; *p; ++p)
if (isspace(*p))
{
*p = 0;
break;
}
}
else
{
for (char *p = m_Label; *p; ++p)
{
if (*p == '\t')
*p = ' ';
else if (*p == '\r' || *p == '\n')
{
*p = 0;
char NextChar = *(p+1);
if (NextChar == '\r' || NextChar == '\n')
++p;
break;
}
}
}
// ptr points to end-of-line.
// Move to start of sequence data.
byte *Seq = ++ptr;
// Delete white space in-place
byte *To = ptr;
m_BufferOffset = (unsigned) (ptr - m_Buffer);
while (m_BufferOffset < m_BufferBytes)
{
byte c = m_Buffer[m_BufferOffset];
if (c == '>')
{
char prevc = '\n';
if (m_BufferOffset > 0)
prevc = m_Buffer[m_BufferOffset-1];
if (prevc == '\n' || prevc == '\r')
break;
}
++m_BufferOffset;
if (isalpha(c) || (isgap(c) && m_AllowGaps))
*To++ = c;
else if (c == '\n' || c == '\r')
continue;
else
{
const char *Label = (m_Label == 0 ? "" : m_Label);
static bool WarningDone = false;
if (!WarningDone)
{
if (isgap(c))
Warning("Ignoring gaps in FASTA file '%s'",
m_FileName.c_str());
else if (isprint(c))
Warning("Invalid FASTA file '%s', non-letter '%c' in sequence >%s",
m_FileName.c_str(), c, Label);
else
Warning("Invalid FASTA file '%s', non-printing byte (hex %02x) in sequence >%s",
m_FileName.c_str(), c, Label);
WarningDone = true;
}
continue;
}
}
m_SeqLength = unsigned(To - Seq);
if (m_SeqIndex == UINT_MAX)
m_SeqIndex = 0;
else
++m_SeqIndex;
EndTimer(SF_GetNextSeq);
return Seq;
}
void SFasta::Open(const string &FileName)
{
Clear();
m_FileName = FileName;
m_File = OpenStdioFile(FileName);
m_BufferSize = BufferSize;
//m_Buffer = myalloc<byte>(m_BufferSize);
m_Buffer = MYALLOC(byte, m_BufferSize, SFasta);
m_FileSize = GetStdioFileSize(m_File);
}
void SFasta::Rewind()
{
m_BufferOffset = 0;
m_BufferBytes = 0;
m_FilePos = 0;
}
bool SFasta::SetIsNucleo()
{
if (m_FilePos != 0)
Die("SFasta::IsNucleo, not at BOF");
unsigned LetterCount = 0;
unsigned NucleoLetterCount = 0;
for (;;)
{
const byte *Seq = GetNextSeq();
if (Seq == 0)
break;
unsigned L = GetSeqLength();
for (unsigned i = 0; i < L; ++i)
if (g_IsNucleoChar[Seq[i]])
++NucleoLetterCount;
LetterCount += L;
if (LetterCount > 256)
break;
}
Rewind();
if (LetterCount == 0)
{
m_IsNucleoSet = true;
m_IsNucleo = true;
return true;
}
// Nucleo if more than 90% nucleo letters AGCTUN
m_IsNucleo = double(NucleoLetterCount)/LetterCount > 0.9;
m_IsNucleoSet = true;
return m_IsNucleo;
}
void SFasta::FillCache()
{
StartTimer(SF_FillCache);
asserta(m_FilePos < m_FileSize);
// off_t may be larger type than unsigned, e.g. 64- vs. 32-bit.
off_t otBytesToRead = m_FileSize - m_FilePos;
bool FinalBuffer = true;
if (otBytesToRead > (off_t) m_BufferSize)
{
FinalBuffer = false;
otBytesToRead = m_BufferSize;
}
unsigned BytesToRead = unsigned(otBytesToRead);
asserta(BytesToRead > 0);
asserta(BytesToRead <= m_BufferSize);
SetStdioFilePos(m_File, m_FilePos);
ReadStdioFile(m_File, m_Buffer, BytesToRead);
if (m_Buffer[0] != '>')
{
if (m_FilePos == 0)
Die("Input is not FASTA file");
else
Die("SFasta::FillCache() failed, expected '>'");
}
m_BufferOffset = 0;
// If last buffer in file, done
if (FinalBuffer)
{
m_BufferBytes = BytesToRead;
m_FilePos += BytesToRead;
EndTimer(SF_FillCache);
return;
}
// If not last buffer, truncate any partial sequence
// at end of buffer. Search backwards to find last '>'.
byte *ptr = m_Buffer + BytesToRead - 1;
while (ptr > m_Buffer)
{
if (ptr[0] == '>' && (ptr[-1] == '\n' || ptr[-1] == '\r'))
break;
--ptr;
}
if (ptr == m_Buffer)
{
LogMe();
if (*ptr != '>')
{
// No '>' found.
// This might techincally be legal FASTA if the entire
// buffer is white space, but strange if not the last buffer
// in the file, so quit anyway.
Die("Failed to find '>' (pos=%u, bytes=%u)",
(unsigned) m_FilePos, BytesToRead);
}
else
{
// Entire buffer is one sequence which may be truncated.
Die("Sequence too long (pos=%u, bytes=%u)",
(unsigned) m_FilePos, BytesToRead);
}
}
asserta(*ptr == '>');
m_BufferBytes = unsigned(ptr - m_Buffer);
m_FilePos += m_BufferBytes;
EndTimer(SF_FillCache);
}
unsigned SFasta::GetPctDoneX10() const
{
if (m_FilePos == 0 || m_FileSize == 0)
return 0;
assert(m_FilePos >= (off_t) m_BufferBytes);
off_t BufferStart = m_FilePos - m_BufferBytes;
off_t BufferPos = BufferStart + m_BufferOffset;
unsigned iPctX10 = unsigned(10.0*double(BufferPos)*100.0/double(m_FileSize));
if (iPctX10 == 0)
return 1;
if (iPctX10 >= 999)
return 998;
return iPctX10;
}
double SFasta::GetPctDone() const
{
if (m_FilePos == 0 || m_FileSize == 0)
return 0;
assert(m_FilePos >= (off_t) m_BufferBytes);
off_t BufferStart = m_FilePos - m_BufferBytes;
off_t BufferPos = BufferStart + m_BufferOffset;
return double(BufferPos)*100.0/double(m_FileSize);
}
bool SFasta::GetNextSD(SeqData &SD)
{
SD.Seq = GetNextSeq();
if (SD.Seq == 0)
return false;
SD.Label = GetLabel();
SD.L = GetSeqLength();
SD.Index = GetSeqIndex();
SD.ORFParent = 0;
SD.Nucleo = GetIsNucleo();
SD.RevComp = false;
return true;
}
#if TEST
void TestSFasta()
{
SFasta SF;
SF.Open(opt_input);
if (opt_verbose)
{
Log(" Index Length Label\n");
Log("------- ------- -----\n");
}
unsigned Index = 0;
unsigned SeqCount = 0;
double LetterCount = 0.0;
ProgressStep(0, 1000, "Reading");
for (;;)
{
const byte *Seq = SF.GetNextSeq();
if (Seq == 0)
break;
ProgressStep(SF.GetPctDoneX10(), 1000, "Reading");
const char *Label = SF.GetLabel();
unsigned L = SF.GetSeqLength();
++SeqCount;
LetterCount += L;
if (opt_verbose)
{
Log(">%7u %7u '%s'\n", Index, L, Label);
Log("+%7.7s %7.7s \"%*.*s\"\n", "", "", L, L, Seq);
}
++Index;
}
ProgressStep(999, 1000, "Reading");
Progress("%u seqs, %s letters\n", SeqCount, FloatToStr(LetterCount));
Log("%u seqs, %s letters\n", SeqCount, FloatToStr(LetterCount));
}
#endif // TEST
|