File: BlasrFASTAReader.cpp

package info (click to toggle)
pbseqlib 5.3.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,020 kB
  • sloc: cpp: 77,246; python: 331; sh: 103; makefile: 42
file content (446 lines) | stat: -rw-r--r-- 11,733 bytes parent folder | download | duplicates (3)
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
#include <pbdata/Enumerations.h>
#include <pbdata/FASTAReader.hpp>
#include <pbdata/FASTASequence.hpp>
#include <pbdata/NucConversion.hpp>

#include <climits>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

#include <sys/fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

void FASTAReader::SetFileSize()
{
    //
    // Seek operation returns the amount seeked forwared in bytes.
    // This is the size used to map (which is rounded up by the
    // system).
    //
    fileSize = lseek(fileDes, 0, SEEK_END);
    lseek(fileDes, 0, SEEK_SET);
}

void FASTAReader::Init()
{
    padding = 0;
    fileDes = -1;
    fileSize = 0;
    endOfReadDelim = '>';
    readStartDelim = '>';
    doToUpper = false;
    convMat = PreserveCase;
    computeMD5 = false;
    filePtr = NULL;
    curPos = 0;
}

FASTAReader::FASTAReader() { Init(); }

FASTAReader::FASTAReader(std::string &fileName)
{
    Init();          // initialze defaults.
    Init(fileName);  // open file.
    padding = 0;
    endOfReadDelim = '>';
    readStartDelim = '>';
}

void FASTAReader::SetSpacePadding(int _padding)
{
    assert(_padding >= 0);
    padding = _padding;
}

void FASTAReader::SetToUpper()
{
    doToUpper = true;
    convMat = AllToUpper;
}

//
// Synonym for Init() for consistency.
//
int FASTAReader::Initialize(std::string &seqInName) { return Init(seqInName); }

int FASTAReader::Init(std::string &seqInName, int passive)
{

    // Check if file is exist and not empty (length of the file is non-zero)
    struct stat st;
    if (stat(seqInName.c_str(), &st) != 0) {
        std::cerr << "FASTA file " << seqInName << " doesn't exist" << std::endl;
        std::exit(EXIT_FAILURE);
    }
    if (st.st_size == 0) {
        std::cerr << "FASTA file " << seqInName << " is empty" << std::endl;
        std::exit(EXIT_FAILURE);
    }

    fileDes = open(seqInName.c_str(), O_RDONLY);
    padding = 0;
    if (fileDes == -1) {
        if (passive) {
            return 0;
        } else {
            std::cout << "Could not open FASTA file " << seqInName << std::endl;
            std::exit(EXIT_FAILURE);
        }
    }
    SetFileSize();
    filePtr = (char *)mmap(0, fileSize, PROT_READ, MAP_PRIVATE, fileDes, 0);
    if (filePtr == MAP_FAILED) {
        std::cout << "ERROR, Fail to load FASTA file " << seqInName << " to virtual memory."
                  << std::endl;
        std::exit(EXIT_FAILURE);
    }
    curPos = 0;
    return 1;
}

void FASTAReader::AdvanceToTitleStart(GenomeLength &p, char delim)
{
    while (p < fileSize and filePtr[p] != delim) {
        p++;
    }
}

void FASTAReader::CheckValidTitleStart(GenomeLength &p, char delim)
{
    if (p >= fileSize or filePtr[p] != delim) {
        std::cout << "ERROR, FASTA entry must begin with \"" << delim << "\"" << std::endl;
        std::exit(EXIT_FAILURE);
    }
}

GenomeLength FASTAReader::ReadAllSequencesIntoOne(FASTASequence &seq,
                                                  SequenceIndexDatabase<FASTASequence> *seqDBPtr)
{
    seq.Free();
    GenomeLength p = curPos;
    AdvanceToTitleStart(p);
    CheckValidTitleStart(p);
    ReadTitle(p, seq);

    if (seq.title == NULL) {
        std::cout << "ERROR, sequence must have a nonempty title." << std::endl;
        std::exit(EXIT_FAILURE);
    }
    if (seqDBPtr != NULL) {
        seqDBPtr->growableName.push_back(seq.title);
    }
    GenomeLength seqLength;
    seqLength = fileSize - p;
    GenomeLength memorySize = seqLength + padding + 1;

    if (memorySize > UINT_MAX) {
        std::cout << "ERROR! Reading fasta files greater than 4Gbytes is not supported."
                  << std::endl;
        std::exit(EXIT_FAILURE);
    }
    seq.Resize(memorySize);
    GenomeLength i;
    i = 0L;
    for (; p < fileSize; p++, i++) {
        seq.seq[i] = filePtr[p];
    }
    i = p = 0;
    while (p < seqLength) {
        //
        // If this is the beginning of another read, add an 'N'
        // to delineate spaces between reads.
        //

        while (p < seqLength && (seq.seq[p] == ' ' || seq.seq[p] == '\n' || seq.seq[p] == '\t' ||
                                 seq.seq[p] == '\r')) {
            p++;
        }
        if (p < seqLength && seq.seq[p] == '>') {
            seq.seq[i] = 'N';

            GenomeLength titleStartPos = p + 1;
            i++;
            while (p < seqLength and seq.seq[p] != '\n')
                p++;
            if (seqDBPtr != NULL and p < seqLength) {
                std::string title;
                GenomeLength tp;
                for (tp = titleStartPos; tp < p; tp++) {
                    title.push_back(seq.seq[tp]);
                }

                seqDBPtr->growableName.push_back(title);
                seqDBPtr->growableSeqStartPos.push_back(i);
                int nSeq = seqDBPtr->growableSeqStartPos.size();
                if (nSeq > 1 and computeMD5) {
                    std::string md5Str;
                    MakeMD5((const char *)&seq.seq[seqDBPtr->growableSeqStartPos[nSeq - 2]],
                            seqDBPtr->growableSeqStartPos[nSeq - 1] -
                                seqDBPtr->growableSeqStartPos[nSeq - 2] - 1,
                            md5Str);
                    seqDBPtr->md5.push_back(md5Str);
                }
            }
        } else if (p < seqLength) {
            // Otherwise, p may be at whitespace
            // advance past that as well.
            seq.seq[i] = convMat[seq.seq[p]];
            i++;
            p++;
        }
    }
    if (i > UINT_MAX) {
        std::cout << "ERROR! Sequences greater than 4Gbase are not supported." << std::endl;
        std::exit(EXIT_FAILURE);
    }
    //
    // Append an 'N' at the end of the last sequence for consistency
    // between different orderings of reference input.
    //
    seq.seq[i] = 'N';
    i++;
    seq.length = i;
    // fill padding.
    for (; i < memorySize; i++) {
        seq.seq[i] = 0;
    }
    seq.deleteOnExit = true;
    if (seqDBPtr != NULL) {
        seqDBPtr->growableSeqStartPos.push_back(seq.length);
        int nSeq = seqDBPtr->growableSeqStartPos.size();
        if (nSeq > 1 and computeMD5) {
            std::string md5Str;
            MakeMD5((const char *)&seq.seq[seqDBPtr->growableSeqStartPos[nSeq - 2]],
                    seqDBPtr->growableSeqStartPos[nSeq - 1] -
                        seqDBPtr->growableSeqStartPos[nSeq - 2] - 1,
                    md5Str);
            seqDBPtr->md5.push_back(md5Str);
        }
        seqDBPtr->Finalize();
    }
    return seq.length;
}

void FASTAReader::ReadTitle(GenomeLength &p, FASTASequence &seq)
{
    char *seqTitle = NULL;
    int seqTitleLen;
    ReadTitle(p, seqTitle, seqTitleLen);
    seq.CopyTitle(seqTitle, seqTitleLen);
    if (seqTitle) {
        delete[] seqTitle;
    }
}

void FASTAReader::ReadTitle(GenomeLength &p, char *&title, int &titleLength)
{
    //
    // Extract the title.  The length of the title does not include the newline.
    //
    p++;  // Move past '>'
    curPos = p;
    while (p < fileSize and filePtr[p] != '\n') {
        p++;
    }
    titleLength = p - curPos;
    if (titleLength > 0) {
        if (title) {
            delete[] title;
            title = NULL;
        }
        title = ProtectedNew<char>(titleLength + 1);
        if (title == nullptr) {
            std::cout << "ERROR, unable to read FASTA file to memory. " << std::endl;
            std::exit(EXIT_FAILURE);
        }
        int t = 0;
        for (p = curPos; p < curPos + titleLength; p++, t++) {
            title[t] = filePtr[p];
        }
        title[titleLength] = '\0';
    } else {
        title = NULL;
        titleLength = 0;
    }
}

int FASTAReader::GetNext(FASTASequence &seq)
{
    if (curPos == fileSize) {
        return 0;
    }

    seq.Free();  //Free seq before read

    //
    // Extract the title of the current record.
    //
    GenomeLength p = curPos;

    AdvanceToTitleStart(p);

    //
    // Make sure there is a '>'
    //
    CheckValidTitleStart(p);

    ReadTitle(p, seq);

    //
    // Read in the next sequence.
    //

    // Count the length of the sequence.

    GenomeLength seqLength = 0;
    curPos = p;
    char c;
    while (p < fileSize and (c = filePtr[p]) != endOfReadDelim) {
        if (c != ' ' and c != '\t' and c != '\n' and c != '\r') {
            seqLength++;
        }
        p++;
    }
    if (seqLength > UINT_MAX) {
        std::cout
            << "ERROR! Reading sequences stored in more than 4Gbytes of space is not supported."
            << std::endl;
        std::exit(EXIT_FAILURE);
    }

    seq.length = 0;
    if (seqLength > 0) {
        seq.length = seqLength;
        seq.seq = ProtectedNew<Nucleotide>(seqLength + padding + 1);
        p = curPos;
        seq.deleteOnExit = true;
        GenomeLength s = 0;
        while (p < fileSize and (c = filePtr[p]) != endOfReadDelim) {
            if (c != ' ' and c != '\t' and c != '\n' and c != '\r') {
                seq.seq[s] = convMat[static_cast<unsigned char>(filePtr[p])];
                s++;
            }
            p++;
        }
        seq.seq[seqLength] = 0;
    }
    curPos = p;

    if (computeMD5) {
        MakeMD5((const char *)&seq.seq, seq.length, curReadMD5);
    }
    return 1;
}
/*
   Advance to the read nSeq forward.

input: nSeq, the number of sequences to skip.
output:
returns 1 if after advancing nSeq sequences, the file pointer is pointing to
a new sequence.
0 otherwise.
A return value of 0 will signal that the file is done being processed if it is
iterting over reads.
*/
int FASTAReader::Advance(int nSeq)
{

    int nAdvanced = 0;
    GenomeLength p = curPos;
    // base case -- it's always ok to advance 0
    if (nSeq == 0) {
        return 1;
    }

    // Make sure the advance starts at
    // the beginning of a sequence.
    while (p < fileSize and filePtr[p] != endOfReadDelim)
        p++;
    // No sequence was found, coudln't advance.
    if (p >= fileSize) {
        return 0;
    }

    p++;
    nAdvanced = 1;
    while (nAdvanced <= nSeq and p < fileSize) {
        if (filePtr[p] == endOfReadDelim) {
            if (nAdvanced == nSeq) {
                //
                // Want to end with the reader on a '>', so return before
                // hitting the rest of the loop.
                //
                curPos = p;
                return 1;
            } else {
                nAdvanced++;
            }
        }
        p++;
    }
    curPos = p;
    return 0;
}

int FASTAReader::CriticalGetNext(FASTASequence &seq)
{
    if (!GetNext(seq)) {
        std::cout << "Could not read a sequence." << std::endl;
        std::exit(EXIT_FAILURE);
    }
    return 1;
}

int FASTAReader::ConcatenateNext(FASTASequence &cur)
{
    FASTASequence next;
    int retVal;
    if ((retVal = GetNext(next))) {
        next.CleanupASCII();
        cur.Concatenate((Nucleotide *)"N");
        cur.Concatenate(next);
    }
    next.Free();
    return retVal;
}

void FASTAReader::Close()
{
    if (fileDes == -1) {
        std::cout << "ERROR, calling close on an uninitialized fasta reader" << std::endl;
        std::exit(EXIT_FAILURE);
    } else {
        munmap(filePtr, fileSize);
        close(fileDes);
        fileDes = -1;
    }
}

void FASTAReader::ReadAllSequences(std::vector<FASTASequence> &sequences)
{
    //
    // Step 1, compute the number of reads in the file.
    //

    GenomeLength p;
    p = 0;
    int nSeq = 0;
    while (p < fileSize) {
        if (filePtr[p] == '>') {
            ++nSeq;
        }
        p++;
    }

    p = 0;
    sequences.resize(nSeq);
    int curSeq = 0;
    while (GetNext(sequences[curSeq])) {
        ++curSeq;
    }
}