comparison srf2fastq/io_lib-1.12.2/progs/srf2fasta.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 #include <stdio.h>
33 #include <math.h>
34 #include <string.h>
35 #include <fcntl.h>
36
37 #include <io_lib/Read.h>
38 #include <io_lib/misc.h>
39 #include <io_lib/ztr.h>
40 #include <io_lib/srf.h>
41
42 /* ------------------------------------------------------------------------ */
43
44 #define MAX_READ_LEN 10000
45 void ztr2fasta(ztr_t *z, char *name) {
46 int i, nc;
47 char buf[MAX_READ_LEN*2 + 512 + 6];
48 char *seq = buf;
49 ztr_chunk_t **chunks;
50
51 /* Extract the sequence only */
52 chunks = ztr_find_chunks(z, ZTR_TYPE_BASE, &nc);
53 if (nc != 1) {
54 fprintf(stderr, "Zero or greater than one BASE chunks found.\n");
55 if (chunks)
56 free(chunks);
57 return;
58 }
59
60 uncompress_chunk(z, chunks[0]);
61
62 /* Construct fasta entry */
63 *seq++ = '>';
64 while (*name)
65 *seq++ = *name++;
66 *seq++ = '\n';
67
68 for (i = 1; i < chunks[0]->dlength; i++) {
69 char base = chunks[0]->data[i];
70 switch (base) {
71 case 'A': case 'a':
72 case 'C': case 'c':
73 case 'G': case 'g':
74 case 'T': case 't':
75 *seq++ = base;
76 break;
77 default:
78 *seq++ = 'N';
79 }
80 }
81 *seq++ = '\n';
82
83 fwrite(buf, 1, seq - buf, stdout);
84 free(chunks);
85
86 return;
87 }
88
89 /* ------------------------------------------------------------------------ */
90 void usage(void) {
91 fprintf(stderr, "Usage: srf2fasta [-C] archive_name\n");
92 exit(1);
93 }
94
95 int main(int argc, char **argv) {
96 char *ar_name;
97 srf_t *srf;
98 char name[512];
99 ztr_t *ztr;
100 int mask = 0, i;
101
102 /* Parse args */
103 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
104 if (!strcmp(argv[i], "-")) {
105 break;
106 } else if (!strcmp(argv[i], "-C")) {
107 mask = SRF_READ_FLAG_BAD_MASK;
108 } else {
109 usage();
110 }
111 }
112
113 if (i == argc) {
114 usage();
115 }
116 ar_name = argv[i];
117
118 if (NULL == (srf = srf_open(ar_name, "r"))) {
119 perror(ar_name);
120 return 4;
121 }
122
123 read_sections(READ_BASES);
124
125 #ifdef _WIN32
126 _setmode(_fileno(stdout), _O_BINARY);
127 #endif
128
129 while (NULL != (ztr = srf_next_ztr(srf, name, mask))) {
130 ztr2fasta(ztr, name);
131 delete_ztr(ztr);
132 }
133
134 srf_destroy(srf, 1);
135
136 return 0;
137 }