File: Fasta_reader.cpp

package info (click to toggle)
trinityrnaseq 2.11.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 417,528 kB
  • sloc: perl: 48,420; cpp: 17,749; java: 12,695; python: 3,124; sh: 1,030; ansic: 983; makefile: 688; xml: 62
file content (144 lines) | stat: -rw-r--r-- 3,820 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
#include "Fasta_reader.hpp"
#include "sequenceUtil.hpp"
#include "stacktrace.hpp"
#include <algorithm>
#include <omp.h>

//constructor
Fasta_reader::Fasta_reader (string filename) {
  
    //this->_hasNext = false;
    
    this->end_reading = -1; // turn off
    this->file_byte_pos = 0; // init

    if (filename == "-") {
        filename = "/dev/fd/0"; // read from stdin
    }
    this->_filereader.open(filename.c_str());
    if (! _filereader.is_open()) {
        throw(stacktrace() + "\n\nError, cannot open file " + filename );
    }
        
    this->_init_reader();
   
}

Fasta_reader::Fasta_reader(string filename, long start_reading, long end_reading) {

    this->file_byte_pos = start_reading;
    this->end_reading = end_reading;
    
    this->_filereader.open(filename.c_str());
    if (! _filereader.is_open()) {
        throw(stacktrace() + "\n\nError, cannot open file " + filename );
    }

    if (start_reading > 0) {
      this->_filereader.seekg(start_reading, _filereader.beg);
      this->file_byte_pos = start_reading;
    }
    
    this->_init_reader();
    


}

void Fasta_reader::_init_reader() {

    // primer reader to first fasta header
    getline(this->_filereader, this->_lastline);
    this->file_byte_pos += this->_lastline.length() + 1;

    while ((! this->_filereader.eof()) && this->_lastline[0] != '>') {
        getline(this->_filereader, this->_lastline);
        this->file_byte_pos += this->_lastline.length() + 1;
    }

}


bool Fasta_reader::hasNext() {
    bool ret;
    
    #pragma omp critical (FileReader)
    {
        ret = !(this->_filereader.eof());
        
        // see if we're reading only part of the file.
        if (ret && this->end_reading > 0) {
            if (this->file_byte_pos >= end_reading) { // bad:  this->_filereader.tellg() >= end_reading) {
                // force it to go to the end of the file
                this->_filereader.seekg(0, this->_filereader.end);
                ret = false;
            }
        }
    }
    return ret;
}


Fasta_entry Fasta_reader::getNext() {
    
    string sequence;
    string header;
    bool ret;

    #pragma omp critical (FileReader)
    {
        header = this->_lastline;
        
        ret = !(this->_filereader.eof());
        if (ret == true)
        {
            this->_lastline = "";
            while ((! this->_filereader.eof()) && this->_lastline[0] != '>') {
                getline(this->_filereader, this->_lastline);
                
                this->file_byte_pos += this->_lastline.length() + 1;

                // cerr << "Comparing mine: " << this->file_byte_pos << " to " << this->_filereader.tellg() << endl;
                
                if (this->_lastline[0] != '>') {
                    sequence += this->_lastline;
                }
            }
        }

        // check if only reading section of a file
        if (this->end_reading > 0) {
            if (this->file_byte_pos >= end_reading) { // bad: this->_filereader.tellg() >= end_reading) {
                // force it to go to the end of the file
                this->_filereader.seekg(0, this->_filereader.end);
            }
        }

    }
    
    if (ret == true)
    {
        sequence = remove_whitespace(sequence);
        transform(sequence.begin(), sequence.end(), sequence.begin(), ::toupper);
        Fasta_entry fe(header, sequence);
        return(fe);
    } else {
        Fasta_entry fe("", "");
        return(fe);
    }
}

map<string,string> Fasta_reader::retrieve_all_seqs_hash() {
    
    map<string,string> all_seqs_hash;
    
    while (this->hasNext()) {
        Fasta_entry f = this->getNext();
        string acc = f.get_accession();
        string seq = f.get_sequence();
        
        all_seqs_hash[acc] = seq;
    }
    
    return(all_seqs_hash);
}