File: input_reader.cpp

package info (click to toggle)
fsm-lite 1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 192 kB
  • sloc: cpp: 563; makefile: 37
file content (236 lines) | stat: -rw-r--r-- 5,669 bytes parent folder | download | duplicates (4)
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
#include "default.h"
#include "input_reader.h"
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib> // abort()
#include <cassert>
using namespace std;

input_reader * input_reader::build(configuration const &config)
{
    switch (config.inputformat)
    {
    case input_fasta:
        return new fasta_input_reader(config);
        break;
    default:
        cerr << "Error: invalid file type at InputReader::build()" << endl; 
        abort();
    }
}

    std::istream *fp;
    std::vector<std::string> id_;
    std::vector<std::size_t> size_;
    std::size_t total_size_;
    bool good_;

input_reader::input_reader(string file)
    : fp(0), id_(), size_(), total_size_(0), total_seqs_(0)
{
    // Open file handle, "-" uses standard input
    if (file == "-")
        fp = &std::cin;
    else
        fp = new ifstream(file.c_str());
}

void error_reading_input(configuration const &config, char const *message)
{
    cerr << message << endl;
    config.print_short_usage();
    exit(1);
}

/**
 * Simple FASTA input
 */
void normalize(std::string &t)
{
    for (std::string::iterator it = t.begin(); it != t.end(); ++it)
    {
        switch (*it)
        {
        case('a'):
            *it = 'A';
            break;
        case('c'):
            *it = 'C';
            break;
        case('g'):
            *it = 'G';
            break;
        case('t'):
            *it = 'T';
            break;
        case('n'):
            *it = 'N';
            break;
        case('A'):
        case('C'):
        case('G'):
        case('T'):
        case('N'):
            break;
        case('0'):
        case('1'):
        case('2'):
        case('3'):
        case('.'):
            break;
        default:
            *it = 'N';
            break;
        }
    }
}

void revcmpl(std::string &t)
{
    char c;
    std::size_t n = t.size();
    for (std::size_t i = 0; i < n / 2; ++i) {
        c = t[i];
        t[i] = t[n - i - 1];
        t[n - i - 1] = c;
    }
    for (std::string::iterator it = t.begin(); it != t.end(); ++it)
    {
        switch (*it)
        {
        case('T'):
            *it = 'A'; break;
        case('G'):
            *it = 'C'; break;
        case('C'):
            *it = 'G'; break;
        case('A'):
            *it = 'T'; break;
        }
    }
}

bool input_reader::smaller_than_rev_cmpl(string const& path)
{
    // Check if given path is smaller than its rev.compl.
    string rc(path);
    revcmpl(rc);
    if (path < rc)
        return true;
    return false;
}

size_t append(ofstream &ofs, string const &fname, unsigned &nseqs)
{
    string row;
    string buffer;
    buffer.reserve(1024*1024);
    ifstream ifs(fname.c_str());
    bool first = true;
    size_t n = 0;
    while (std::getline(ifs, row).good())
    {
        if (row.empty())
            continue;
        if (row[0] == '>')
        {
            if (!first)
            {
                ofs << '#';                
                nseqs++;
                n ++;
                revcmpl(buffer);
                ofs << buffer << '#';
                nseqs++;
                n += buffer.size() + 1;
                buffer.clear();
            }
            first = false;
            continue;
        }
        if (row[row.size()-1] == '\n')
            row.pop_back();
        if (row[row.size()-1] == '\r')
            row.pop_back();
        normalize(row);
        ofs << row;
        buffer += row;
        n += row.size();
    }
    if (n == 0)
    {
        cerr << "error: Unable to read the input file " << fname << endl;
        exit(1);
    }
    ofs << '#';
    nseqs ++;
    n ++;
    revcmpl(buffer);
    ofs << buffer << '$';
    nseqs++;
    n += buffer.size() + 1;
    buffer.clear();
    return n;
}

bool file_exists (string const &name)
{
    ifstream f(name.c_str());
    if (f.good())
    {
        f.close();
        return true;
    }
    else
    {
        f.close();
        return false;
    }   
}

fasta_input_reader::fasta_input_reader(configuration const &config)
    : input_reader(config.listfile)
{
    string tmpfile = config.tmpfile + ".tmp";
    string metafile = config.tmpfile + ".meta";
//    if (file_exists(tmpfile) && file_exists(metafile))
// TODO        cerr << "warning: tmp files under " << config.tmpfile << " will be rebuilt" << endl;
        
    ofstream ofs(tmpfile.c_str());
    ofstream ofsmeta(metafile.c_str());
    // Parse the list file
    string id, fname;
    while (fp->good())
    {
        *fp >> id;
        if (id.empty() || !fp->good())
            break;
        *fp >> fname;
        if (fname.empty())
            error_reading_input(config, "error: Unable to read the -l,--list input file: please check the list file format.");

        id_.push_back(id);
        size_t s = append(ofs, fname, total_seqs_);
        size_.push_back(s);
        total_size_ += s;
        ofsmeta << id << "\t" << s << "\t" << endl;
    }
    if (id_.empty() || total_size_ == 0) 
        error_reading_input(config, "error: Unable to read the -l,--list input file: please check the list file format.");
    if (!ofs.good()) 
        error_reading_input(config, "error: Unable to write the temporary files under the location -i,--index.");
    if (!ofsmeta.good()) 
        error_reading_input(config, "error: Unable to write the temporary files under the location -i,--index.");
    
    if ((1 << DBITS) <= size_.size())
    {
        cerr << "error: increase DBITS value to support more than " << (1<<DBITS)
             << " input files; see the file default.h for more details and recompile." << endl;
        exit(1);
    }

    ofsmeta.close();
    ofs.close();
}