-
Notifications
You must be signed in to change notification settings - Fork 24
/
fileio.c
171 lines (154 loc) · 4.5 KB
/
fileio.c
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
/* PANDAseq -- Assemble paired FASTQ Illumina reads and strip the region between amplification primers.
Copyright (C) 2011-2012 Andre Masella
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <bzlib.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <zlib.h>
#if HAVE_PTHREAD
# include <pthread.h>
#endif
#include "pandaseq.h"
#include "misc.h"
#ifdef HAVE_PTHREAD
# include"pandaseq-mux.h"
#endif
static bool buff_read_gz(
char *buf,
size_t buf_len,
size_t *read,
void *data) {
gzFile file = (gzFile) data;
int code;
code = gzread(file, buf, buf_len);
if (code < 1) {
*read = 0;
return gzeof(file);
}
*read = code;
return true;
}
static bool buff_read_bz2(
char *buf,
size_t buf_len,
size_t *read,
void *data) {
BZFILE *file = (BZFILE *) data;
int bzerror;
*read = BZ2_bzRead(&bzerror, file, buf, buf_len);
return bzerror == BZ_OK || bzerror == BZ_STREAM_END;
}
PandaBufferRead panda_open_buffer(
const char *file_name,
PandaLogProxy logger,
void **user_data,
PandaDestroy *destroy) {
char buffer[2];
int fd;
*user_data = NULL;
*destroy = NULL;
fd = open(file_name, O_RDONLY);
if (fd < 0 || read(fd, &buffer, 2) != 2 || lseek(fd, 0, SEEK_SET) != 0) {
panda_log_proxy_write(logger, PANDA_CODE_NO_FILE, NULL, NULL, file_name);
return NULL;
}
if (buffer[0] == 'B' && buffer[1] == 'Z') {
BZFILE *bz_file;
bz_file = BZ2_bzdopen(fd, "r");
if (bz_file == NULL) {
panda_log_proxy_write(logger, PANDA_CODE_NO_FILE, NULL, NULL, file_name);
close(fd);
return NULL;
}
*user_data = bz_file;
*destroy = BZ2_bzclose;
return buff_read_bz2;
} else {
gzFile gz_file;
gz_file = gzdopen(fd, "r");
if (gz_file == NULL) {
panda_log_proxy_write(logger, PANDA_CODE_NO_FILE, NULL, NULL, file_name);
close(fd);
return NULL;
}
*user_data = gz_file;
*destroy = (PandaDestroy) gzclose;
return buff_read_gz;
}
}
PandaNextSeq panda_open_fastq(
const char *forward,
const char *reverse,
PandaLogProxy logger,
unsigned char qualmin,
PandaTagging policy,
const char *index,
void **user_data,
PandaDestroy *destroy) {
MANAGED_STACK(PandaBufferRead,
forward_file);
MANAGED_STACK(PandaBufferRead,
reverse_file);
MANAGED_STACK(PandaBufferRead,
index_file);
*user_data = NULL;
*destroy = NULL;
forward_file = panda_open_buffer(forward, logger, &forward_file_data, &forward_file_destroy);
if (forward_file == NULL) {
return NULL;
}
reverse_file = panda_open_buffer(reverse, logger, &reverse_file_data, &reverse_file_destroy);
if (reverse_file == NULL) {
DESTROY_STACK(forward_file);
return NULL;
}
index_file = index == NULL ? NULL : panda_open_buffer(index, logger, &index_file_data, &index_file_destroy);
if (index != NULL && index_file == NULL) {
DESTROY_STACK(forward_file);
DESTROY_STACK(reverse_file);
return NULL;
}
return panda_create_fastq_reader(forward_file, forward_file_data, forward_file_destroy, reverse_file, reverse_file_data, reverse_file_destroy, logger, qualmin, policy, index_file, index_file_data, index_file_destroy, user_data, destroy);
}
PandaAssembler panda_assembler_open_fastq(
const char *forward,
const char *reverse,
PandaLogProxy logger,
unsigned char qualmin,
PandaTagging policy) {
PandaNextSeq next;
void *next_data;
PandaDestroy next_destroy;
if ((next = panda_open_fastq(forward, reverse, logger, qualmin, policy, NULL, &next_data, &next_destroy)) == NULL) {
return NULL;
}
return panda_assembler_new(next, next_data, next_destroy, logger);
}
#ifdef HAVE_PTHREAD
PandaMux panda_mux_open_fastq(
const char *forward,
const char *reverse,
PandaLogProxy logger,
unsigned char qualmin,
PandaTagging policy) {
PandaNextSeq next;
void *next_data;
PandaDestroy next_destroy;
if ((next = panda_open_fastq(forward, reverse, logger, qualmin, policy, NULL, &next_data, &next_destroy)) == NULL) {
return NULL;
}
return panda_mux_new(next, next_data, next_destroy, logger);
}
#endif