File: gzFastq.h

package info (click to toggle)
stacks 2.55%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,420 kB
  • sloc: cpp: 38,596; perl: 1,337; sh: 539; python: 497; makefile: 144
file content (241 lines) | stat: -rw-r--r-- 6,479 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
// -*-mode:c++; c-style:k&r; c-basic-offset:4;-*-
//
// Copyright 2013, Julian Catchen <jcatchen@uoregon.edu>
//
// This file is part of Stacks.
//
// Stacks is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Stacks is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Stacks.  If not, see <http://www.gnu.org/licenses/>.
//

#ifndef __GZFASTQ_H__
#define __GZFASTQ_H__

#ifdef HAVE_LIBZ

#include <cerrno>
#include <zlib.h>
#include "input.h"

class GzFastq: public Input {
    gzFile gz_fh;

public:
    GzFastq(string path) : Input() {
        this->gz_fh = gzopen(path.c_str(), "rb");
        if (!this->gz_fh) {
            cerr << "Failed to open gzipped file '" << path << "': " << strerror(errno) << ".\n";
            exit(EXIT_FAILURE);
        }
        #if ZLIB_VERNUM >= 0x1240
        gzbuffer(this->gz_fh, libz_buffer_size);
        #endif
    };
    GzFastq(const char *path) : Input() {
        this->gz_fh = gzopen(path, "rb");
        if (!this->gz_fh) {
            cerr << "Failed to open gzipped file '" << path << "': " << strerror(errno) << ".\n";
            exit(EXIT_FAILURE);
        }
        #if ZLIB_VERNUM >= 0x1240
        gzbuffer(this->gz_fh, libz_buffer_size);
        #endif
    };
    ~GzFastq() {
        gzclose(this->gz_fh);
    };
    Seq *next_seq();
    int  next_seq(Seq &s);
};

Seq *GzFastq::next_seq() {
    char *res = NULL;

    //
    // Check the contents of the line buffer. When we finish reading a FASTQ record
    // the buffer will either contain whitespace or the header of the next FASTQ
    // record.
    //
    this->line[0] = '\0';
    do {
        res = gzgets(this->gz_fh, this->line, max_len);
    } while (this->line[0] != '@' && res != NULL);

    if (res == NULL) {
        return NULL;
    }

    //
    // Check if there is a carraige return in the buffer
    //
    uint len = strlen(this->line);
    if (len > 0 && this->line[len - 1] == '\n') this->line[len - 1] = '\0';
    if (len > 1 && this->line[len - 2] == '\r') this->line[len - 2] = '\0';

    //
    // Initialize the Seq structure and store the FASTQ ID
    //
    Seq *s = new Seq;
    s->id = new char[strlen(this->line) + 1];
    strcpy(s->id, this->line + 1);

    //
    // Read the sequence from the file
    //
    gzgets(this->gz_fh, this->line, max_len);

    if (gzeof(this->gz_fh)) {
        return NULL;
    }

    len = strlen(this->line);
    if (len > 0 && this->line[len - 1] == '\n') this->line[len - 1] = '\0';
    if (len > 1 && this->line[len - 2] == '\r') this->line[len - 2] = '\0';

    s->seq = new char[len + 1];
    strcpy(s->seq, this->line);

    //
    // Read the repeat of the ID
    //
    this->line[0] = '\0';
    res = gzgets(this->gz_fh, this->line, max_len);

    if (this->line[0] != '+' || res == NULL) {
        return NULL;
    }

    //
    // Read the quality score from the file
    //
    this->line[0] = '\0';
    res = gzgets(this->gz_fh, this->line, max_len);

    if (res == NULL && strlen(this->line) == 0) {
        return NULL;
    }

    len = strlen(this->line);
    if (len > 0 && this->line[len - 1] == '\n') this->line[len - 1] = '\0';
    if (len > 1 && this->line[len - 2] == '\r') this->line[len - 2] = '\0';

    s->qual = new char[len + 1];
    strcpy(s->qual, this->line);

    //
    // Clear the line buffer so it is set up for the next record. If a '@'
    // appears in the quality scores read, it will break parsing next time
    // it is called.
    //
    this->line[0] = '\0';

    return s;
}

int GzFastq::next_seq(Seq &s) {
    char *res = NULL;

    //
    // Check the contents of the line buffer. When we finish reading a FASTQ record
    // the buffer will either contain whitespace or the header of the next FASTQ
    // record.
    //
    this->line[0] = '\0';
    do {
        res = gzgets(this->gz_fh, this->line, max_len);
    } while (this->line[0] != '@' && res != NULL);

    if (res == NULL) {
        return 0;
    }

    //
    // Check if there is a carraige return in the buffer
    //
    uint len = strlen(this->line);
    if (len > 0 && this->line[len - 1] == '\n') this->line[len - 1] = '\0';
    if (len > 1 && this->line[len - 2] == '\r') this->line[len - 2] = '\0';

    //
    // Store the FASTQ ID
    //
    strcpy(s.id, this->line + 1);

    //
    // Read the sequence from the file
    //
    this->line[0] = '\0';
    res = gzgets(this->gz_fh, this->line, max_len);

    if (res == NULL) {
        return 0;
    }

    len = strlen(this->line);
    if (len > 0 && this->line[len - 1] == '\n') this->line[len - 1] = '\0';
    if (len > 1 && this->line[len - 2] == '\r') this->line[len - 2] = '\0';

    strcpy(s.seq, this->line);

    //
    // Read the repeat of the ID
    //
    this->line[0] = '\0';
    res = gzgets(this->gz_fh, this->line, max_len);

    if (this->line[0] != '+' || res == NULL) {
        return 0;
    }

    //
    // Read the quality score from the file
    //
    this->line[0] = '\0';
    res = gzgets(this->gz_fh, this->line, max_len);

    if (res == NULL && strlen(this->line) == 0) {
        return 0;
    }

    len = strlen(this->line);
    if (len > 0 && this->line[len - 1] == '\n') this->line[len - 1] = '\0';
    if (len > 1 && this->line[len - 2] == '\r') this->line[len - 2] = '\0';

    strcpy(s.qual, this->line);

    //
    // Clear the line buffer so it is set up for the next record. If a '@'
    // appears in the quality scores read, it will break parsing next time
    // it is called.
    //
    this->line[0] = '\0';

    return 1;
}

#else  // If HAVE_LIBZ is undefined and zlib library is not present.

#include "input.h"

class GzFastq: public Input {
 public:
    GzFastq(const char *path) : Input() { cerr << "Gzip support was not enabled when Stacks was compiled.\n"; };
    GzFastq(string path) : Input() { cerr << "Gzip support was not enabled when Stacks was compiled.\n"; };
    ~GzFastq() {};
    Seq *next_seq()      { return NULL; };
    int  next_seq(Seq &) { return 0; };
};

#endif // HAVE_LIBZ

#endif // __GZFASTQ_H__