Mercurial > repos > dawe > srf2fastq
comparison srf2fastq/io_lib-1.12.2/progs/srf_list.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 #ifdef HAVE_CONFIG_H | |
2 #include "io_lib_config.h" | |
3 #endif | |
4 | |
5 #include <stdio.h> | |
6 #include <string.h> | |
7 #include <stdlib.h> | |
8 #include <errno.h> | |
9 #include <unistd.h> | |
10 #include <inttypes.h> | |
11 | |
12 #include <io_lib/os.h> | |
13 #include <io_lib/hash_table.h> | |
14 #include <io_lib/srf.h> | |
15 | |
16 /* Command line options */ | |
17 typedef struct { | |
18 int long_format; | |
19 int count_only; | |
20 int verbose; | |
21 } opts; | |
22 | |
23 /* | |
24 * Lists the contents of an SRF file. | |
25 * | |
26 * Returns num_reads for success | |
27 * -1 for failure | |
28 */ | |
29 int64_t list_file(char *fname, opts *opts) { | |
30 srf_t *srf; | |
31 char name[512]; | |
32 int64_t count = 0; | |
33 int type; | |
34 uint64_t pos; | |
35 | |
36 if (NULL == (srf = srf_open(fname, "r"))) { | |
37 perror(fname); | |
38 return -1; | |
39 } | |
40 | |
41 /* Scan through file gathering the details to index in memory */ | |
42 while ((type = srf_next_block_details(srf, &pos, name)) >= 0) { | |
43 if (type == SRFB_TRACE_BODY) { | |
44 count++; | |
45 if (!opts->count_only) { | |
46 if (opts->long_format) | |
47 printf("%-30s %10"PRId64" + %4d + %5d\n", | |
48 name, pos, | |
49 srf->tb.trace_size, | |
50 srf->th.trace_hdr_size); | |
51 else | |
52 puts(name); | |
53 } | |
54 } | |
55 } | |
56 | |
57 srf_destroy(srf, 1); | |
58 | |
59 return count; | |
60 } | |
61 | |
62 /* | |
63 * Counts the contents of an SRF file. | |
64 * If the hash index exists it uses this instead. | |
65 * | |
66 * Returns num_reads for success | |
67 * -1 for failure | |
68 */ | |
69 int64_t count_file(char *fname, opts *opts) { | |
70 srf_t *srf; | |
71 srf_index_hdr_t hdr; | |
72 int64_t count = 0; | |
73 off_t ipos, skip; | |
74 int item_sz = 9, i; | |
75 unsigned char data[16]; | |
76 | |
77 if (NULL == (srf = srf_open(fname, "r"))) { | |
78 perror(fname); | |
79 return -1; | |
80 } | |
81 | |
82 /* Read the index header */ | |
83 if (0 != srf_read_index_hdr(srf, &hdr, 0)) { | |
84 srf_destroy(srf, 1); | |
85 return list_file(fname, opts); | |
86 } | |
87 ipos = ftello(srf->fp); | |
88 | |
89 /* Compute the remaining size of the index and divide by item_sz */ | |
90 if (hdr.dbh_pos_stored_sep) | |
91 item_sz += 4; | |
92 skip = hdr.index_hdr_sz | |
93 + hdr.n_container * 8 | |
94 + hdr.n_data_block_hdr * 8 | |
95 + hdr.n_buckets * 8; | |
96 | |
97 srf_destroy(srf, 1); | |
98 | |
99 return (hdr.size - skip - 16/* footer*/) / item_sz; | |
100 } | |
101 | |
102 void usage(int error) { | |
103 printf("Usage: srf_list [options] srf_file ...\n"); | |
104 printf("Options: -c\tCount only - do not list filenames\n"); | |
105 printf(" -v\tVerbose - gives summary data per file too\n"); | |
106 printf(" -l\tList in long format. Lines contain:\n"); | |
107 printf(" \t name position body-size header-size\n"); | |
108 | |
109 exit(error); | |
110 } | |
111 | |
112 /* | |
113 * Lists the contents of a .hash file | |
114 */ | |
115 int main(int argc, char **argv) { | |
116 opts opts; | |
117 int i, c; | |
118 int64_t count = 0; | |
119 | |
120 opts.long_format = 0; | |
121 opts.count_only = 0; | |
122 opts.verbose = 0; | |
123 | |
124 while ((c = getopt(argc, argv, "lcvh")) != -1) { | |
125 switch (c) { | |
126 case 'l': | |
127 opts.long_format = 1; | |
128 break; | |
129 | |
130 case 'c': | |
131 opts.count_only = 1; | |
132 break; | |
133 | |
134 case 'v': | |
135 opts.verbose = 1; | |
136 break; | |
137 | |
138 case 'h': | |
139 usage(0); | |
140 | |
141 default: | |
142 usage(1); | |
143 } | |
144 } | |
145 | |
146 for (i = optind; i < argc; i++) { | |
147 int64_t c; | |
148 | |
149 if (opts.count_only) | |
150 c = count_file(argv[i], &opts); | |
151 else | |
152 c = list_file(argv[i], &opts); | |
153 | |
154 if (c < 0) | |
155 return 1; | |
156 | |
157 if (opts.verbose) | |
158 printf("%s: %d sequences\n", argv[i], c); | |
159 count += c; | |
160 } | |
161 | |
162 if (opts.count_only) | |
163 printf("%"PRId64"\n", count); | |
164 | |
165 return 0; | |
166 } |