Mercurial > repos > dawe > srf2fastq
comparison srf2fastq/io_lib-1.12.2/io_lib/Read.c @ 0:d901c9f41a6a default tip
Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
author | dawe |
---|---|
date | Tue, 07 Jun 2011 17:48:05 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d901c9f41a6a |
---|---|
1 /* | |
2 * Copyright (c) Medical Research Council 1994. All rights reserved. | |
3 * | |
4 * Permission to use, copy, modify and distribute this software and its | |
5 * documentation for any purpose is hereby granted without fee, provided that | |
6 * this copyright and notice appears in all copies. | |
7 * | |
8 * This file was written by James Bonfield, Simon Dear, Rodger Staden, | |
9 * as part of the Staden Package at the MRC Laboratory of Molecular | |
10 * Biology, Hills Road, Cambridge, CB2 2QH, United Kingdom. | |
11 * | |
12 * MRC disclaims all warranties with regard to this software. | |
13 */ | |
14 | |
15 /* | |
16 * File: Read.c | |
17 * Purpose: Performs read/write IO on the Read data stucture. | |
18 * Last update: 01/09/94 | |
19 */ | |
20 | |
21 | |
22 /* | |
23 The Read data type is designed so that it can hold a varying degree | |
24 of information about sequences, yet have a single set of calls | |
25 to access the data. | |
26 | |
27 There are plenty of assumptions around that both the number of | |
28 bases and the number of points will fit into an int_2, a short. | |
29 | |
30 */ | |
31 | |
32 /* ---- Includes ---- */ | |
33 | |
34 #include <stdlib.h> | |
35 #include <stdio.h> | |
36 #include <string.h> | |
37 #include <fcntl.h> /* Only need on windows for _O_BINARY */ | |
38 #include <unistd.h> | |
39 | |
40 #include "io_lib/Read.h" | |
41 #include "io_lib/mFILE.h" | |
42 | |
43 #ifdef IOLIB_ABI | |
44 # include "io_lib/abi.h" | |
45 #endif | |
46 #ifdef IOLIB_SCF | |
47 # include "io_lib/scf.h" | |
48 #endif | |
49 #ifdef IOLIB_ALF | |
50 # include "io_lib/alf.h" | |
51 #endif | |
52 #ifdef IOLIB_PLN | |
53 # include "io_lib/plain.h" | |
54 #endif | |
55 #ifdef IOLIB_ZTR | |
56 # include "io_lib/ztr.h" | |
57 #endif | |
58 #ifdef IOLIB_CTF | |
59 # include "io_lib/seqIOCTF.h" | |
60 #endif | |
61 #ifdef IOLIB_SFF | |
62 # include "io_lib/sff.h" | |
63 #endif | |
64 #ifdef IOLIB_EXP | |
65 # include "io_lib/expFileIO.h" | |
66 #endif | |
67 #ifdef USE_BIOLIMS | |
68 # include "spBiolims.h" | |
69 #endif | |
70 | |
71 #include "io_lib/xalloc.h" | |
72 #include "io_lib/translate.h" | |
73 #include "io_lib/traceType.h" | |
74 #include "io_lib/misc.h" | |
75 #include "io_lib/open_trace_file.h" | |
76 | |
77 /* | |
78 * Read a sequence from a file "fnin" of format "format". If "format" is 0 | |
79 * (ANY_FORMAT), we automatically determine the correct format. | |
80 * Returns: | |
81 * Read * for success | |
82 * NULLRead for failure | |
83 */ | |
84 Read *read_reading(char *fn, int format) { | |
85 Read *read; | |
86 mFILE *fp; | |
87 | |
88 #ifdef USE_BIOLIMS | |
89 if( !strncmp(fn,BIOLIMS_TAG,strlen(BIOLIMS_TAG))){ | |
90 return spReadBiolimsReading(fn); | |
91 } | |
92 #endif | |
93 | |
94 /* | |
95 * If we're asking for an Experiment file, read it. | |
96 * If the format is ANY then attempt EXP first following by trace. | |
97 * Otherwise use the trace search mechanism. | |
98 * | |
99 * Note this is purely for locating files and not for forcing the file | |
100 * format. It's here so that experiment files and trace files may be | |
101 * given identical names but accessed through different search paths | |
102 * (as is the case with the trace server). | |
103 */ | |
104 if (format == TT_EXP) { | |
105 if (NULL == (fp = open_exp_mfile(fn, NULL))) { | |
106 errout("'%s': couldn't open\n", fn); | |
107 return NULL; | |
108 } | |
109 } else { | |
110 fp = NULL; | |
111 if (format == TT_ANY) | |
112 fp = open_exp_mfile(fn, NULL); | |
113 | |
114 if (!fp && NULL == (fp = open_trace_mfile(fn, NULL))) { | |
115 errout("'%s': couldn't open\n", fn); | |
116 return NULL; | |
117 } | |
118 } | |
119 | |
120 read = mfread_reading(fp, fn, format); | |
121 mfclose(fp); | |
122 | |
123 return read; | |
124 } | |
125 | |
126 /* | |
127 * Read a sequence from a FILE *fp of format "format". If "format" is 0 | |
128 * (ANY_FORMAT), we automatically determine the correct format. | |
129 * We still pass a filename 'fn' although this isn't used other than for | |
130 * filling in the read->trace_name field. | |
131 * | |
132 * NB this function should NOT be used when Biolims support is required | |
133 * (as biolims readings are not stored in a file) | |
134 * | |
135 * Returns: | |
136 * Read * for success | |
137 * NULLRead for failure | |
138 */ | |
139 Read *mfread_reading(mFILE *fp, char *fn, int format) { | |
140 Read *read; | |
141 mFILE *newfp; | |
142 | |
143 if (!fn) | |
144 fn = "(unknown)"; | |
145 | |
146 newfp = freopen_compressed(fp, NULL); | |
147 if (newfp != fp) { | |
148 fp = newfp; | |
149 } else { | |
150 newfp = NULL; | |
151 } | |
152 | |
153 #ifdef _WIN32 | |
154 /* | |
155 * jkb 16/05/00 comment below | |
156 * | |
157 * On windows "prog < file.abi" will work wrongly (compared to | |
158 * "prog file.abi") because windows is rather stupid. It treats ascii | |
159 * and binary streams differently, it considers stdin to be ascii unless | |
160 * told otherwise, and it can only be told otherwise by using non-ansi | |
161 * windows-specific function calls. | |
162 */ | |
163 if (format != TT_EXP && format != TT_PLN && fp->fp) | |
164 _setmode(_fileno(fp->fp), _O_BINARY); | |
165 #endif | |
166 | |
167 if (format == TT_ANY || format == TT_ANYTR) { | |
168 format = fdetermine_trace_type(fp); | |
169 mrewind(fp); | |
170 } | |
171 | |
172 switch (format) { | |
173 case TT_UNK: | |
174 case TT_ERR: | |
175 errout("File '%s' has unknown trace type\n", fn); | |
176 read = NULLRead; | |
177 break; | |
178 | |
179 #ifdef IOLIB_SCF | |
180 case TT_SCF: { | |
181 Scf *scf; | |
182 scf = mfread_scf(fp); | |
183 | |
184 if (scf) { | |
185 read = scf2read(scf); | |
186 scf_deallocate(scf); | |
187 } else | |
188 read = NULLRead; | |
189 | |
190 break; | |
191 } | |
192 #endif | |
193 | |
194 #ifdef IOLIB_CTF | |
195 case TT_CTF: | |
196 read = mfread_ctf(fp); | |
197 break; | |
198 #endif | |
199 | |
200 #ifdef IOLIB_SFF | |
201 case TT_SFF: | |
202 read = mfread_sff(fp); | |
203 break; | |
204 #endif | |
205 | |
206 #ifdef IOLIB_ZTR | |
207 case TT_ZTR: | |
208 case TT_ZTR1: | |
209 case TT_ZTR2: | |
210 case TT_ZTR3: { | |
211 ztr_t *ztr; | |
212 | |
213 if ((ztr = mfread_ztr(fp))) { | |
214 uncompress_ztr(ztr); | |
215 read = ztr2read(ztr); | |
216 delete_ztr(ztr); | |
217 } else { | |
218 read = NULLRead; | |
219 } | |
220 break; | |
221 } | |
222 #endif | |
223 | |
224 #ifdef IOLIB_ABI | |
225 case TT_ABI: | |
226 read = mfread_abi(fp); | |
227 break; | |
228 #endif | |
229 | |
230 #ifdef IOLIB_ALF | |
231 case TT_ALF: | |
232 read = mfread_alf(fp); | |
233 break; | |
234 #endif | |
235 | |
236 #ifdef IOLIB_EXP | |
237 case TT_EXP: { | |
238 /* FIXME: we shouldn't redirect like this */ | |
239 Exp_info *e = exp_mfread_info(fp); | |
240 | |
241 read = e ? exp2read(e,fn) : NULLRead; | |
242 break; | |
243 } | |
244 #endif | |
245 | |
246 #ifdef IOLIB_PLN | |
247 case TT_PLN: | |
248 read = mfread_pln(fp); | |
249 break; | |
250 #endif | |
251 | |
252 default: | |
253 errout("Unknown format %d specified to read_reading()\n", format); | |
254 read = NULLRead; | |
255 } | |
256 | |
257 if (read != NULLRead && (read->trace_name = (char *)xmalloc(strlen(fn)+1))) | |
258 strcpy(read->trace_name, fn); | |
259 | |
260 if (newfp) mfclose(newfp); | |
261 | |
262 return read; | |
263 } | |
264 | |
265 Read *fread_reading(FILE *fp, char *fn, int format) { | |
266 return mfread_reading(mfreopen(fn, "rb", fp), fn, format); | |
267 } | |
268 | |
269 /* | |
270 * Write a sequence to a FILE *fp of format "format". If "format" is 0, | |
271 * we choose our favourite - SCF. | |
272 * | |
273 * Returns: | |
274 * 0 for success | |
275 * -1 for failure | |
276 */ | |
277 int mfwrite_reading(mFILE *fp, Read *read, int format) { | |
278 int r = -1; | |
279 int no_compress = 0; | |
280 | |
281 #ifdef _WIN32 | |
282 /* | |
283 * jkb 09/06/00 comment below | |
284 * | |
285 * On windows "prog > file.scf" will work wrongly (compared to | |
286 * "prog file.scf") because windows is rather stupid. It treats ascii | |
287 * and binary streams differently, it considers stdout to be ascii unless | |
288 * told otherwise, and it can only be told otherwise by using non-ansi | |
289 * windows-specific function calls. | |
290 */ | |
291 if (format != TT_EXP && format != TT_PLN && fp->fp) | |
292 _setmode(_fileno(fp->fp), _O_BINARY); | |
293 #endif | |
294 | |
295 switch (format) { | |
296 default: | |
297 /* Defaults to ZTR type */ | |
298 | |
299 #ifdef IOLIB_ZTR | |
300 case TT_ZTR: | |
301 case TT_ZTR2: { | |
302 ztr_t *ztr; | |
303 ztr = read2ztr(read); | |
304 compress_ztr(ztr, 2); | |
305 r = mfwrite_ztr(fp, ztr); | |
306 delete_ztr(ztr); | |
307 no_compress = 1; | |
308 break; | |
309 } | |
310 case TT_ZTR1: { | |
311 ztr_t *ztr; | |
312 ztr = read2ztr(read); | |
313 compress_ztr(ztr, 1); | |
314 r = mfwrite_ztr(fp, ztr); | |
315 delete_ztr(ztr); | |
316 break; | |
317 } | |
318 case TT_ZTR3: { | |
319 ztr_t *ztr; | |
320 ztr = read2ztr(read); | |
321 compress_ztr(ztr, 3); | |
322 r = mfwrite_ztr(fp, ztr); | |
323 delete_ztr(ztr); | |
324 no_compress = 1; | |
325 break; | |
326 } | |
327 #endif | |
328 | |
329 #ifdef IOLIB_SCF | |
330 case TT_SCF: { | |
331 Scf *scf; | |
332 scf = read2scf(read); | |
333 r = mfwrite_scf(scf, fp); | |
334 scf_deallocate(scf); | |
335 break; | |
336 } | |
337 #endif | |
338 | |
339 #ifdef IOLIB_CTF | |
340 case TT_CTF: | |
341 r = mfwrite_ctf(fp, read); | |
342 break; | |
343 #endif | |
344 | |
345 #ifdef IOLIB_ABI | |
346 case TT_ABI: | |
347 /*return mfwrite_abi(fp, read); */ | |
348 break; | |
349 #endif | |
350 | |
351 #ifdef IOLIB_SFF | |
352 case TT_SFF: | |
353 /*return mfwrite_sff(fp, read); */ | |
354 break; | |
355 #endif | |
356 | |
357 #ifdef IOLIB_ALF | |
358 case TT_ALF: | |
359 /* return mfwrite_alf(fp, read); */ | |
360 break; | |
361 #endif | |
362 | |
363 #ifdef IOLIB_EXP | |
364 case TT_EXP: { | |
365 Exp_info *e = read2exp(read, read->ident ? read->ident : "unknown"); | |
366 | |
367 if (NULL == e) { | |
368 fprintf(stderr, "Failed to create experiment file.\n"); | |
369 r = -1; | |
370 } else { | |
371 exp_print_mfile(fp, e); | |
372 exp_destroy_info(e); | |
373 r = 0; | |
374 } | |
375 break; | |
376 } | |
377 #endif | |
378 | |
379 #ifdef IOLIB_PLN | |
380 case TT_PLN: | |
381 r = mfwrite_pln(fp, read); | |
382 break; | |
383 #endif | |
384 } | |
385 | |
386 mftruncate(fp, -1); | |
387 if (r == 0 && !no_compress) { | |
388 fcompress_file(fp); | |
389 } | |
390 mfflush(fp); | |
391 | |
392 return r; | |
393 } | |
394 | |
395 int fwrite_reading(FILE *fp, Read *read, int format) { | |
396 int ret; | |
397 mFILE *mf = mfreopen(NULL, "wbx", fp); | |
398 if (mf) { | |
399 ret = mfwrite_reading(mf, read, format); | |
400 mfflush(mf); | |
401 mf->fp = NULL; /* Don't want this closed here */ | |
402 mfclose(mf); | |
403 } else { | |
404 return -1; | |
405 } | |
406 | |
407 return ret; | |
408 } | |
409 | |
410 /* | |
411 * Write a sequence to a file "fn" of format "format". If "format" is 0, | |
412 * we choose our favourite - SCF. | |
413 * | |
414 * Returns: | |
415 * 0 for success | |
416 * -1 for failure | |
417 */ | |
418 int write_reading(char *fn, Read *read, int format) { | |
419 int ret; | |
420 mFILE *fp = mfopen(fn, "wb"); | |
421 if (!fp) | |
422 return -1; | |
423 | |
424 ret = mfwrite_reading(fp, read, format); | |
425 mfclose(fp); | |
426 return ret; | |
427 } | |
428 | |
429 /* | |
430 * Old style stub interfaces implemented simply as redirection through | |
431 * fread_reading and frwrite_reading. | |
432 */ | |
433 #ifdef IOLIB_ABI | |
434 Read *fread_abi(FILE *fp) { | |
435 return fread_reading(fp, NULL, TT_ABI); | |
436 } | |
437 | |
438 int fwrite_abi(FILE *fp, Read *read) { | |
439 return fwrite_reading(fp, read, TT_ABI); | |
440 } | |
441 #endif | |
442 | |
443 #ifdef IOLIB_ALF | |
444 Read *fread_alf(FILE *fp) { | |
445 return fread_reading(fp, NULL, TT_ALF); | |
446 } | |
447 | |
448 int fwrite_alf(FILE *fp, Read *read) { | |
449 return fwrite_reading(fp, read, TT_ALF); | |
450 } | |
451 #endif | |
452 | |
453 #ifdef IOLIB_CTF | |
454 Read *fread_ctf(FILE *fp) { | |
455 return fread_reading(fp, NULL, TT_CTF); | |
456 } | |
457 | |
458 int fwrite_ctf(FILE *fp, Read *read) { | |
459 return fwrite_reading(fp, read, TT_CTF); | |
460 } | |
461 #endif | |
462 | |
463 #ifdef IOLIB_PLN | |
464 Read *fread_pln(FILE *fp) { | |
465 return fread_reading(fp, NULL, TT_PLN); | |
466 } | |
467 | |
468 int fwrite_pln(FILE *fp, Read *read) { | |
469 return fwrite_reading(fp, read, TT_PLN); | |
470 } | |
471 #endif | |
472 | |
473 #ifdef IOLIB_ZTR | |
474 ztr_t *fread_ztr(FILE *fp) { | |
475 ztr_t *z; | |
476 mFILE *mf; | |
477 | |
478 if (NULL == (mf = mfreopen(NULL, "rb", fp))) | |
479 return NULL; | |
480 | |
481 z = mfread_ztr(mf); | |
482 mfclose(mf); | |
483 return z; | |
484 } | |
485 | |
486 int fwrite_ztr(FILE *fp, ztr_t *z) { | |
487 mFILE *mf; | |
488 int r; | |
489 | |
490 if (NULL == (mf = mfreopen(NULL, "wbx", fp))) | |
491 return -1; | |
492 | |
493 r = mfwrite_ztr(mf, z); | |
494 mfflush(mf); | |
495 mf->fp = NULL; /* Don't want this closed here */ | |
496 mfclose(mf); | |
497 return r; | |
498 } | |
499 #endif | |
500 | |
501 #ifdef IOLIB_SCF | |
502 Scf *fread_scf(FILE *fp) { | |
503 Scf *s; | |
504 mFILE *mf; | |
505 | |
506 if (NULL == (mf = mfreopen(NULL, "rb", fp))) | |
507 return NULL; | |
508 | |
509 s = mfread_scf(mf); | |
510 mf->fp = NULL; /* Don't want this closed here */ | |
511 mfclose(mf); | |
512 return s; | |
513 } | |
514 | |
515 int fwrite_scf(Scf *s, FILE *fp) { | |
516 mFILE *mf; | |
517 int r; | |
518 | |
519 if (NULL == (mf = mfreopen(NULL, "wbx", fp))) | |
520 return -1; | |
521 | |
522 r = mfwrite_scf(s, mf); | |
523 mfflush(mf); | |
524 mf->fp = NULL; /* Don't want this closed here */ | |
525 mfclose(mf); | |
526 return r; | |
527 } | |
528 #endif | |
529 | |
530 #ifdef IOLIB_EXP | |
531 Exp_info *exp_fread_info(FILE *fp) { | |
532 Exp_info *e; | |
533 mFILE *mf; | |
534 | |
535 if (NULL == (mf = mfreopen(NULL, "rb", fp))) | |
536 return NULL; | |
537 | |
538 e = exp_mfread_info(mf); | |
539 mf->fp = NULL; /* Don't want this closed here */ | |
540 mfclose(mf); | |
541 return e; | |
542 } | |
543 | |
544 void exp_print_file(FILE *fp, Exp_info *e) { | |
545 mFILE *mf; | |
546 | |
547 if (NULL == (mf = mfreopen(NULL, "wbx", fp))) | |
548 return; | |
549 | |
550 exp_print_mfile(mf, e); | |
551 mfflush(mf); | |
552 mf->fp = NULL; /* Don't want this closed here */ | |
553 mfclose(mf); | |
554 } | |
555 #endif |