comparison srf2fastq/io_lib-1.12.2/progs/extract_fastq.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-1999. 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 #include <stdio.h>
16 #include <errno.h>
17 #include <string.h>
18 #include <strings.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <io_lib/Read.h>
22 #include <io_lib/traceType.h>
23 #include <io_lib/expFileIO.h>
24 #include <io_lib/open_trace_file.h>
25
26 static int do_trans(mFILE *infp, char *in_file, FILE *outfp, int format) {
27 Read *r;
28 char *p = strrchr(in_file, '/');
29 int i;
30
31 read_sections(READ_BASES);
32 if (NULL == (r = mfread_reading(infp, in_file, format))) {
33 fprintf(stderr, "Failed to read file '%s'\n", in_file);
34 return 1;
35 }
36
37 if (NULL == p)
38 p = in_file;
39 else
40 p++;
41
42 fprintf(outfp, "@%s\n", p);
43 fprintf(outfp, "%.*s\n", r->NBases, r->base);
44 fprintf(outfp, "+%s\n", p);
45 for (i = 0; i < r->NBases; i++) {
46 int qual;
47 switch (r->base[i]) {
48 case 'A':
49 case 'a':
50 qual = r->prob_A[i];
51 break;
52 case 'C':
53 case 'c':
54 qual = r->prob_C[i];
55 break;
56 case 'G':
57 case 'g':
58 qual = r->prob_G[i];
59 break;
60 case 'T':
61 case 't':
62 qual = r->prob_T[i];
63 break;
64 default:
65 qual = 0;
66 }
67 fputc(qual + 33, outfp);
68 }
69 fputc('\n', outfp);
70
71 read_deallocate(r);
72 fflush(outfp);
73
74 return 0;
75 }
76
77 static void usage(void) {
78 fprintf(stderr, "Usage: extract_fastq [-(abi|alf|scf|exp|pln)]\n"
79 " [-output output_name] [-fofn fofn] [input_name] ...\n");
80 exit(1);
81 }
82
83 int main(int argc, char **argv) {
84 int from_stdin = 1;
85 mFILE *infp = mstdin();
86 FILE *outfp = stdout;
87 int format = TT_ANY;
88 int ret = 0;
89 char *fofn = NULL;
90
91 for (argc--, argv++; argc > 0; argc--, argv++) {
92 if (strcasecmp(*argv, "-abi") == 0) {
93 format = TT_ABI;
94 } else if (strcasecmp(*argv, "-alf") == 0) {
95 format = TT_ALF;
96 } else if (strcasecmp(*argv, "-scf") == 0) {
97 format = TT_SCF;
98 } else if (strcasecmp(*argv, "-exp") == 0) {
99 format = TT_EXP;
100 } else if (strcasecmp(*argv, "-pln") == 0) {
101 format = TT_PLN;
102 } else if (strcasecmp(*argv, "-ztr") == 0) {
103 format = TT_ZTR;
104 } else if (strcasecmp(*argv, "-ctf") == 0) {
105 format = TT_CTF;
106 } else if (strcmp(*argv, "-fofn") == 0) {
107 fofn = *++argv;
108 argc--;
109 from_stdin = 0;
110 } else if (strcasecmp(*argv, "-output") == 0) {
111 if (NULL == (outfp = fopen(*++argv, "wb"))) {
112 perror(*argv);
113 return 1;
114 }
115 argc--;
116 } else if (**argv != '-') {
117 from_stdin = 0;
118 break;
119 } else {
120 usage();
121 }
122 }
123
124 if (!from_stdin) {
125 if (fofn) {
126 FILE *fofn_fp;
127 char line[8192];
128
129 if (strcmp(fofn, "stdin") == 0)
130 fofn_fp = stdin;
131 else
132 fofn_fp = fopen(fofn, "r");
133
134 if (fofn_fp) {
135 while (fgets(line, 8192, fofn_fp) != NULL) {
136 char *cp;
137 if (cp = strchr(line, '\n'))
138 *cp = 0;
139 if (format == TT_EXP) {
140 infp = open_exp_mfile(line, NULL);
141 } else {
142 infp = open_trace_mfile(line, NULL);
143 }
144 if (NULL == infp) {
145 perror(line);
146 ret = 1;
147 } else {
148 ret |= do_trans(infp, line, outfp, format);
149 mfclose(infp);
150 }
151 }
152 fclose(fofn_fp);
153 }
154 }
155 for (;argc > 0; argc--, argv++) {
156 if (format == TT_EXP) {
157 infp = open_exp_mfile(*argv, NULL);
158 } else {
159 infp = open_trace_mfile(*argv, NULL);
160 }
161 if (NULL == infp) {
162 perror(*argv);
163 ret = 1;
164 } else {
165 ret |= do_trans(infp, *argv, outfp, format);
166 mfclose(infp);
167 }
168 }
169 } else {
170 ret = do_trans(infp, "<stdin>", outfp, format);
171 }
172
173 return ret;
174 }
175