1 // -*-mode:c++; c-style:k&r; c-basic-offset:4;-*-
2 //
3 // Copyright 2013-2015, Julian Catchen <jcatchen@illinois.edu>
4 //
5 // This file is part of Stacks.
6 //
7 // Stacks is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Stacks is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with Stacks.  If not, see <http://www.gnu.org/licenses/>.
19 //
20 
21 //
22 // write.cc -- common routines for writing FASTA/FASTQ records to a file..
23 //
24 // Julian Catchen
25 // jcatchen@uoregon.edu
26 // University of Oregon
27 //
28 
29 #include "write.h"
30 
31 int
write_fasta(ofstream * fh,RawRead * href,bool overhang)32 write_fasta(ofstream *fh, RawRead *href, bool overhang) {
33     char tile[id_len];
34     sprintf(tile, "%04d", href->tile);
35 
36     int offset = href->inline_bc_len;
37     offset += overhang ? 1 : 0;
38 
39     if (href->fastq_type != generic_fastq)
40         *fh <<
41             ">" << href->run <<
42             "_" << href->lane <<
43             "_" << tile <<
44             "_" << href->x <<
45             "_" << href->y <<
46             "/" << href->read << "\n" <<
47             href->seq + offset << "\n";
48     else
49         *fh <<
50             ">" << href->machine <<
51             "/" << href->read << "\n" <<
52             href->seq + offset << "\n";
53 
54     if (fh->fail()) return -1;
55 
56     return 1;
57 }
58 
59 int
write_fasta(gzFile * fh,RawRead * href,bool overhang)60 write_fasta(gzFile *fh, RawRead *href, bool overhang) {
61     stringstream sstr;
62     char tile[id_len];
63     sprintf(tile, "%04d", href->tile);
64 
65     int offset = href->inline_bc_len;
66     offset += overhang ? 1 : 0;
67 
68     if (href->fastq_type != generic_fastq)
69         sstr <<
70             ">" << href->run <<
71             "_" << href->lane <<
72             "_" << tile <<
73             "_" << href->x <<
74             "_" << href->y <<
75             "/" << href->read << "\n" <<
76             href->seq + offset << "\n";
77     else
78         sstr <<
79             ">" << href->machine <<
80             "/" << href->read << "\n" <<
81             href->seq + offset << "\n";
82 
83     int res = gzputs(*fh, sstr.str().c_str());
84 
85     return res;
86 }
87 
88 int
write_fasta(ofstream * fh,Seq * href)89 write_fasta(ofstream *fh, Seq *href) {
90     *fh <<
91         ">" <<
92         href->id  << "\n" <<
93         href->seq << "\n";
94 
95     if (fh->fail()) return -1;
96 
97     return 1;
98 }
99 
100 int
write_fasta(gzFile * fh,Seq * href)101 write_fasta(gzFile *fh, Seq *href) {
102     stringstream sstr;
103 
104     sstr <<
105         ">" <<
106         href->id  << "\n" <<
107         href->seq << "\n";
108 
109     int res = gzputs(*fh, sstr.str().c_str());
110 
111     return res;
112 }
113 
114 int
write_fastq(ofstream * fh,RawRead * href,bool overhang)115 write_fastq(ofstream *fh, RawRead *href, bool overhang) {
116     //
117     // Write the sequence and quality scores in FASTQ format.
118     //
119     char tile[id_len];
120     sprintf(tile, "%04d", href->tile);
121 
122     int offset = href->inline_bc_len;
123     offset += overhang ? 1 : 0;
124 
125     if (href->fastq_type != generic_fastq)
126         *fh <<
127             "@" << href->run <<
128             "_" << href->lane <<
129             "_" << tile <<
130             "_" << href->x <<
131             "_" << href->y <<
132             "/" << href->read << "\n" <<
133             href->seq + offset << "\n" <<
134             "+\n" <<
135             href->phred + offset << "\n";
136     else
137         *fh <<
138             "@" << href->machine <<
139             "/" << href->read << "\n" <<
140             href->seq + offset << "\n" <<
141             "+\n" <<
142             href->phred + offset << "\n";
143 
144     if (fh->fail()) return -1;
145 
146     return 1;
147 }
148 
149 int
write_fastq(gzFile * fh,RawRead * href,bool overhang)150 write_fastq(gzFile *fh, RawRead *href, bool overhang) {
151     //
152     // Write the sequence and quality scores in FASTQ format.
153     //
154     stringstream sstr;
155     char tile[id_len];
156     sprintf(tile, "%04d", href->tile);
157 
158     int offset = href->inline_bc_len;
159     offset += overhang ? 1 : 0;
160 
161     if (href->fastq_type != generic_fastq)
162         sstr <<
163             "@" << href->run <<
164             "_" << href->lane <<
165             "_" << tile <<
166             "_" << href->x <<
167             "_" << href->y <<
168             "/" << href->read << "\n" <<
169             href->seq + offset << "\n" <<
170             "+\n" <<
171             href->phred + offset << "\n";
172     else
173         sstr <<
174             "@" << href->machine <<
175             "/" << href->read << "\n" <<
176             href->seq + offset << "\n" <<
177             "+\n" <<
178             href->phred + offset << "\n";
179 
180     int res = gzputs(*fh, sstr.str().c_str());
181 
182     return res;
183 }
184 
185 int
write_fastq(ofstream * fh,Seq * href,int offset)186 write_fastq(ofstream *fh, Seq *href, int offset) {
187     *fh <<
188         "@" << href->id     << "\n" <<
189         href->seq + offset  << "\n" <<
190         "+\n" <<
191         href->qual + offset << "\n";
192 
193     if (fh->fail()) return -1;
194 
195     return 1;
196 }
197 
198 int
write_fastq(gzFile * fh,Seq * href,int offset)199 write_fastq(gzFile *fh, Seq *href, int offset) {
200     stringstream sstr;
201     sstr <<
202         "@" << href->id     << "\n" <<
203         href->seq + offset  << "\n" <<
204         "+\n" <<
205         href->qual + offset << "\n";
206 
207     int res = gzputs(*fh, sstr.str().c_str());
208 
209     return res;
210 }
211 
212 int
write_fasta(ofstream * fh,Seq * href,int offset)213 write_fasta(ofstream *fh, Seq *href, int offset) {
214     *fh <<
215         ">" <<
216         href->id << "\n" <<
217         href->seq + offset << "\n";
218 
219     if (fh->fail()) return -1;
220 
221     return 1;
222 }
223 
224 int
write_fasta(gzFile * fh,Seq * href,int offset)225 write_fasta(gzFile *fh, Seq *href, int offset) {
226     stringstream sstr;
227     sstr <<
228         ">" <<
229         href->id << "\n" <<
230         href->seq + offset << "\n";
231 
232     int res = gzputs(*fh, sstr.str().c_str());
233 
234     return res;
235 }
236 
237 int
write_fastq(ofstream * fh,Seq * href)238 write_fastq(ofstream *fh, Seq *href) {
239     *fh <<
240         "@" << href->id << "\n" <<
241         href->seq << "\n" <<
242         "+\n" <<
243         href->qual << "\n";
244 
245     if (fh->fail()) return -1;
246 
247     return 1;
248 }
249 
250 int
write_fastq(gzFile * fh,Seq * href)251 write_fastq(gzFile *fh, Seq *href) {
252     stringstream sstr;
253     sstr <<
254         "@" << href->id << "\n" <<
255         href->seq << "\n" <<
256         "+\n" <<
257         href->qual << "\n";
258 
259     int res = gzputs(*fh, sstr.str().c_str());
260 
261     return res;
262 }
263 
264 int
write_fastq(ofstream * fh,Seq * href,string msg)265 write_fastq(ofstream *fh, Seq *href, string msg) {
266     *fh <<
267         "@" << href->id << "|" << msg << "\n" <<
268         href->seq << "\n" <<
269         "+\n" <<
270         href->qual << "\n";
271 
272     if (fh->fail()) return -1;
273 
274     return 1;
275 }
276 
277 int
write_fastq(gzFile * fh,Seq * href,string msg)278 write_fastq(gzFile *fh, Seq *href, string msg) {
279     stringstream sstr;
280     sstr <<
281         "@" << href->id << "|" << msg << "\n" <<
282         href->seq << "\n" <<
283         "+\n" <<
284         href->qual << "\n";
285 
286     int res = gzputs(*fh, sstr.str().c_str());
287 
288     return res;
289 }
290 
291 int
write_fasta(ofstream * fh,Seq * href,string msg)292 write_fasta(ofstream *fh, Seq *href, string msg) {
293     *fh <<
294         ">" <<
295         href->id  << "|" << msg << "\n" <<
296         href->seq << "\n";
297 
298     if (fh->fail()) return -1;
299 
300     return 1;
301 }
302 
303 int
write_fasta(gzFile * fh,Seq * href,string msg)304 write_fasta(gzFile *fh, Seq *href, string msg) {
305     stringstream sstr;
306     sstr <<
307         ">" <<
308         href->id  << "|" << msg << "\n" <<
309         href->seq << "\n";
310 
311     int res = gzputs(*fh, sstr.str().c_str());
312 
313     return res;
314 }
315 
316 int
write_fasta(ofstream * fh,Seq * href,RawRead * r)317 write_fasta(ofstream *fh, Seq *href, RawRead *r) {
318     *fh        << ">"
319         << href->id << "\n"
320         << r->seq + r->inline_bc_len << "\n";
321 
322     if (fh->fail()) return -1;
323 
324     return 1;
325 }
326 
327 int
write_fasta(gzFile * fh,Seq * href,RawRead * r)328 write_fasta(gzFile *fh, Seq *href, RawRead *r) {
329     stringstream sstr;
330     sstr << ">"
331          << href->id << "\n"
332          << r->seq + r->inline_bc_len << "\n";
333 
334     int res = gzputs(*fh, sstr.str().c_str());
335 
336     return res;
337 }
338 
339 int
write_fastq(ofstream * fh,Seq * href,RawRead * r)340 write_fastq(ofstream *fh, Seq *href, RawRead *r) {
341     *fh << "@" << href->id << "\n"
342         << r->seq   + r->inline_bc_len << "\n"
343         << "+\n"
344         << r->phred + r->inline_bc_len << "\n";
345 
346     if (fh->fail()) return -1;
347 
348     return 1;
349 }
350 
351 int
write_fastq(gzFile * fh,Seq * href,RawRead * r)352 write_fastq(gzFile *fh, Seq *href, RawRead *r) {
353     stringstream sstr;
354     sstr << "@" << href->id << "\n"
355          << r->seq   + r->inline_bc_len << "\n"
356          << "+\n"
357          << r->phred + r->inline_bc_len << "\n";
358 
359     int res = gzputs(*fh, sstr.str().c_str());
360 
361     return res;
362 }
363