comparison srf2fastq/io_lib-1.12.2/progs/srf_extract_linear.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 * ======================================================================
3 * This software has been created by Genome Research Limited (GRL).
4 *
5 * GRL hereby grants permission to use, copy, modify and distribute
6 * this software and its documentation for non-commercial purposes
7 * without fee at the user's own risk on the basis set out below.
8 *
9 * GRL neither undertakes nor accepts any duty whether contractual or
10 * otherwise in connection with the software, its use or the use of
11 * any derivative, and makes no representations or warranties, express
12 * or implied, concerning the software, its suitability, fitness for
13 * a particular purpose or non-infringement.
14 *
15 * In no event shall the authors of the software or GRL be responsible
16 * or liable for any loss or damage whatsoever arising in any way
17 * directly or indirectly out of the use of this software or its
18 * derivatives, even if advised of the possibility of such damage.
19 *
20 * Our software can be freely distributed under the conditions set out
21 * above, and must contain this copyright notice.
22 * ======================================================================
23 */
24
25 /*
26 * This performs a linear (non-indexed) search for a trace in an SRF archive.
27 *
28 * It's not intended as a suitable production program or as a library of code
29 * to use, but as a test and benchmark statistic.
30 */
31
32 #ifdef HAVE_CONFIG_H
33 #include "io_lib_config.h"
34 #endif
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <fcntl.h>
39 #include <io_lib/Read.h>
40 #include <io_lib/misc.h>
41 #include <io_lib/ztr.h>
42 #include <io_lib/srf.h>
43
44 /* ------------------------------------------------------------------------ */
45 /*
46 * Looks for a trace name in an SRF archive and returns the binary contents
47 * if found, or NULL if not.
48 */
49 mFILE *find_reading(srf_t *srf, char *tr_name) {
50 do {
51 int type;
52
53 switch(type = srf_next_block_type(srf)) {
54 case -1:
55 /* EOF */
56 return NULL;
57
58 case SRFB_CONTAINER:
59 if (0 != srf_read_cont_hdr(srf, &srf->ch))
60 return NULL;
61 break;
62
63 case SRFB_XML:
64 if (0 != srf_read_xml(srf, &srf->xml))
65 return NULL;
66 break;
67
68
69 case SRFB_TRACE_HEADER: {
70 /* off_t pos = ftell(srf->fp); */
71
72 if (0 != srf_read_trace_hdr(srf, &srf->th))
73 return NULL;
74
75 #if 0
76 /*
77 * If the name prefix doesn't match tr_name then skip this entire
78 * block.
79 */
80 if (0 != strncmp(tr_name, srf->th.id_prefix,
81 strlen(srf->th.id_prefix)) &&
82 0 != srf->th.next_block_offset) {
83 fseek(srf->fp, pos + srf->th.next_block_offset, SEEK_SET);
84 }
85 #endif
86 break;
87 }
88
89 case SRFB_TRACE_BODY: {
90 mFILE *mf = mfcreate(NULL, 0);
91 srf_trace_body_t tb;
92 char name[512];
93
94 if (!mf || 0 != srf_read_trace_body(srf, &tb, 0))
95 return NULL;
96
97 sprintf(name, "%s%s", srf->th.id_prefix, tb.read_id);
98 if (strcmp(name, tr_name)) {
99 mfdestroy(mf);
100 if (tb.trace)
101 free(tb.trace);
102 continue;
103 }
104
105 if (srf->th.trace_hdr_size)
106 mfwrite(srf->th.trace_hdr, 1,
107 srf->th.trace_hdr_size, mf);
108 if (tb.trace_size)
109 mfwrite(tb.trace, 1, tb.trace_size, mf);
110 if (tb.trace)
111 free(tb.trace);
112 mrewind(mf);
113 return mf;
114 }
115
116 case SRFB_INDEX: {
117 off_t pos = ftello(srf->fp);
118 srf_read_index_hdr(srf, &srf->hdr, 1);
119
120 /* Skip the index body */
121 fseeko(srf->fp, pos + srf->hdr.size, SEEK_SET);
122 break;
123 }
124
125 case SRFB_NULL_INDEX:
126 break;
127
128 default:
129 fprintf(stderr, "Block of unknown type '%c'. Aborting\n", type);
130 return NULL;
131 }
132 } while (1);
133
134 return NULL;
135 }
136
137 /* ------------------------------------------------------------------------ */
138 int main(int argc, char **argv) {
139 char *ar_name, *tr_name;
140 mFILE *mf;
141 srf_t *srf;
142
143 if (argc != 3) {
144 fprintf(stderr, "Usage: extract_linear_srf archive_name trace_name\n");
145 return 1;
146 }
147 ar_name = argv[1];
148 tr_name = argv[2];
149
150 if (NULL == (srf = srf_open(ar_name, "r"))) {
151 perror(ar_name);
152 return 4;
153 }
154
155 if (NULL == (mf = find_reading(srf, tr_name))) {
156 fprintf(stderr, "%s not found in archive\n", tr_name);
157 return 3;
158 }
159
160 #ifdef _WIN32
161 _setmode(_fileno(stdout), _O_BINARY);
162 #endif
163
164 fwrite(mf->data, 1, mf->size, stdout);
165 return 0;
166 }