1 | /* ------------------------------------------------------------ */ |
---|
2 | /* */ |
---|
3 | /* Format Conversion Program. */ |
---|
4 | /* */ |
---|
5 | /* Woese Lab., Dept. of Microbiology, UIUC */ |
---|
6 | /* */ |
---|
7 | /* ------------------------------------------------------------ */ |
---|
8 | /* if using Dec. Unix, <sys/time.h> doesn't not exist */ |
---|
9 | |
---|
10 | #include <stdio.h> |
---|
11 | #include <stdlib.h> |
---|
12 | #include <ctype.h> |
---|
13 | #include <string.h> |
---|
14 | #include "convert.h" |
---|
15 | #include "global.h" |
---|
16 | |
---|
17 | /* --------------------------------------------------------------- |
---|
18 | * Main program: |
---|
19 | * File conversion; convert from one file format to |
---|
20 | * the other. eg. Genbank<-->Macke format. |
---|
21 | */ |
---|
22 | int main(int argc, char *argv[]) { |
---|
23 | int intype, outtype; |
---|
24 | /* int Lenstr(), file_exist(); */ |
---|
25 | /* void error(), genbank_to_macke(), macke_to_genbank(); */ |
---|
26 | /* void Getstr(), change_file_suffix(); */ |
---|
27 | /* char *Dupstr(), *Reallocspace(); */ |
---|
28 | /* char error_msg[LINENUM]; */ |
---|
29 | char temp[LINENUM]; |
---|
30 | char choice[LINENUM]; |
---|
31 | |
---|
32 | intype = outtype = UNKNOWN; |
---|
33 | if(argc < 3 || argv[1][0] != '-') { |
---|
34 | fprintf(stderr, |
---|
35 | "---------------------------------------------------------------\n"); |
---|
36 | fprintf(stderr, |
---|
37 | "\n convert_aln - an alignment and file converter written by\n"); |
---|
38 | fprintf(stderr, |
---|
39 | " WenMin Kuan for the RDP database project. Please\n"); |
---|
40 | fprintf(stderr, |
---|
41 | " report errors or deficiencies to kuan@phylo.life.uiuc.edu\n"); |
---|
42 | fprintf(stderr, |
---|
43 | "\n Command line usage:\n\n"); |
---|
44 | fprintf(stderr, |
---|
45 | " $ convert_aln -infmt input_file -outfmt output_file, where \n"); |
---|
46 | fprintf(stderr, |
---|
47 | " infmt can be either GenBank, EMBL, AE2, SwissProt or ALMA\n"); |
---|
48 | fprintf(stderr, |
---|
49 | " outfmt GenBank, EMBL, AE2, PAUP, PHYLIP, GCG, Printable or ALMA\n\n"); |
---|
50 | fprintf(stderr, |
---|
51 | "---------------------------------------------------------------\n\n"); |
---|
52 | fprintf(stderr, "Select input format (<CR> means default)\n\n"); |
---|
53 | fprintf(stderr, " (1) GenBank [default]\n"); |
---|
54 | fprintf(stderr, " (2) EMBL\n"); |
---|
55 | fprintf(stderr, " (3) AE2\n"); |
---|
56 | fprintf(stderr, " (4) SwissProt\n"); |
---|
57 | fprintf(stderr, " (5) ALMA\n"); |
---|
58 | fprintf(stderr, " (6) Quit\n"); |
---|
59 | fprintf(stderr, " ? "); |
---|
60 | Getstr(choice, LINENUM); |
---|
61 | switch(choice[0]) { |
---|
62 | case '1': intype = GENBANK; |
---|
63 | break; |
---|
64 | case '2': intype = EMBL; |
---|
65 | break; |
---|
66 | case '3': intype = MACKE; |
---|
67 | break; |
---|
68 | case '4': intype = PROTEIN; |
---|
69 | break; |
---|
70 | case '5': intype = ALMA; |
---|
71 | break; |
---|
72 | case '6': exit(0); |
---|
73 | case '\0': intype = GENBANK; |
---|
74 | break; /* default selection */ |
---|
75 | default : error(16, "Unknown input file format, EXIT."); |
---|
76 | } |
---|
77 | argv=(char**)calloc(1,sizeof(char*)*5); |
---|
78 | |
---|
79 | fprintf(stderr, "\nInput file name? "); |
---|
80 | Getstr(temp, LINENUM); |
---|
81 | argv[2] = Dupstr(temp); |
---|
82 | if(!file_exist(temp)) |
---|
83 | error(77, "Input file not found, EXIT."); |
---|
84 | |
---|
85 | /* output file information */ |
---|
86 | fprintf(stderr, "\nSelect output format (<CR> means default)\n\n"); |
---|
87 | fprintf(stderr, " (1) GenBank\n"); |
---|
88 | fprintf(stderr, " (2) EMBL\n"); |
---|
89 | fprintf(stderr, " (3) AE2 [default]\n"); |
---|
90 | fprintf(stderr, " (4) PAUP\n"); |
---|
91 | fprintf(stderr, " (5) PHYLIP\n"); |
---|
92 | fprintf(stderr, " (A) PHYLIP2 (insert stdin in first line)\n"); |
---|
93 | fprintf(stderr, " (6) GCG\n"); |
---|
94 | fprintf(stderr, " (7) Printable\n"); |
---|
95 | fprintf(stderr, " (8) ALMA\n"); |
---|
96 | fprintf(stderr, " (9) Quit\n"); |
---|
97 | fprintf(stderr, " ? "); |
---|
98 | Getstr(choice, LINENUM); |
---|
99 | switch(choice[0]) { |
---|
100 | case '1': outtype = GENBANK; |
---|
101 | break; |
---|
102 | case '2': outtype = EMBL; |
---|
103 | break; |
---|
104 | case '3': outtype = MACKE; |
---|
105 | break; |
---|
106 | case '4': outtype = PAUP; |
---|
107 | break; |
---|
108 | case '5': outtype = PHYLIP; |
---|
109 | break; |
---|
110 | case 'A': outtype = PHYLIP2; |
---|
111 | break; |
---|
112 | case '6': outtype = GCG; |
---|
113 | break; |
---|
114 | case '7': outtype = PRINTABLE; |
---|
115 | break; |
---|
116 | case '8': outtype = ALMA; |
---|
117 | break; |
---|
118 | case '9': exit(0); |
---|
119 | case '\0': outtype = MACKE; |
---|
120 | break; |
---|
121 | default : error(66, "Unknown output file format, EXIT."); |
---|
122 | } |
---|
123 | change_file_suffix(argv[2], temp, outtype); |
---|
124 | if(outtype!=GCG) { |
---|
125 | fprintf(stderr, "\nOutput file name [%s]? ", temp); |
---|
126 | Getstr(temp, LINENUM); |
---|
127 | if(Lenstr(temp)==0) |
---|
128 | change_file_suffix(argv[2], temp, outtype); |
---|
129 | } |
---|
130 | argv[4] = Dupstr(temp); |
---|
131 | argc = 5; |
---|
132 | } else { /* processing command line */ |
---|
133 | /* input file */ |
---|
134 | if(argv[1][1]=='G'||argv[1][1]=='g') |
---|
135 | intype=GENBANK; |
---|
136 | else if(argv[1][1]=='E'||argv[1][1]=='e') |
---|
137 | intype=EMBL; |
---|
138 | else if((argv[1][1]=='A'||argv[1][1]=='a') |
---|
139 | &&(argv[1][2]=='E'|| argv[1][2]=='e')) |
---|
140 | intype=MACKE; |
---|
141 | else if(argv[1][1]=='S'||argv[1][1]=='s') |
---|
142 | intype = PROTEIN; |
---|
143 | else if((argv[1][1]=='A'||argv[1][1]=='a') |
---|
144 | &&(argv[1][2]=='L'||argv[1][2]=='l')) |
---|
145 | intype = ALMA; |
---|
146 | else error(67, "UNKNOWN input file type, EXIT."); |
---|
147 | |
---|
148 | /* output file */ |
---|
149 | if((argv[3][1]=='G'||argv[3][1]=='g') |
---|
150 | &&(argv[3][2]=='E'||argv[3][2]=='e')) |
---|
151 | outtype = GENBANK; |
---|
152 | else if((argv[3][1]=='E'||argv[3][1]=='e')) |
---|
153 | outtype = EMBL; |
---|
154 | else if((argv[3][1]=='A'||argv[3][1]=='a') |
---|
155 | &&(argv[3][2]=='E'||argv[3][2]=='e')) |
---|
156 | outtype = MACKE; |
---|
157 | else if((argv[3][1]=='P'||argv[3][1]=='p') |
---|
158 | &&(argv[3][2]=='a'||argv[3][2]=='A')) |
---|
159 | outtype = PAUP; |
---|
160 | else if(!strncasecmp(argv[3]+1,"ph",2)){ |
---|
161 | if (!strcasecmp(argv[3]+1,"phylip2")){ |
---|
162 | outtype = PHYLIP2; |
---|
163 | }else{ |
---|
164 | outtype = PHYLIP; |
---|
165 | } |
---|
166 | }else if((argv[3][1]=='G'||argv[3][1]=='g') |
---|
167 | &&(argv[3][2]=='C'||argv[3][2]=='c')) |
---|
168 | outtype = GCG; |
---|
169 | else if((argv[3][1]=='P'||argv[3][1]=='p') |
---|
170 | &&(argv[3][2]=='R'||argv[3][2]=='r')) |
---|
171 | outtype = PRINTABLE; |
---|
172 | else if((argv[3][1]=='A'||argv[3][1]=='a') |
---|
173 | &&(argv[3][2]=='L'||argv[3][2]=='l')) |
---|
174 | outtype = ALMA; |
---|
175 | else error(68, "UNKNOWN output file file, EXIT."); |
---|
176 | } /* command line */ |
---|
177 | |
---|
178 | if(argc==4) { /* default output file */ |
---|
179 | /* argv = (char**)Reallocspace(argv, sizeof(char*)*5); */ |
---|
180 | const char **argv_new = calloc(sizeof(char*), 5); |
---|
181 | memcpy(argv_new, argv, sizeof(char*)*4); |
---|
182 | argv_new[4] = ""; |
---|
183 | argv = (char**)argv_new; |
---|
184 | } |
---|
185 | |
---|
186 | #ifdef log |
---|
187 | if(outtype!=GCG) |
---|
188 | fprintf(stderr, "\n\nConvert file %s to file %s.\n", argv[2], |
---|
189 | argv[4]); |
---|
190 | else fprintf(stderr, "\n\nConvert file %s to GCG files\n", argv[2]); |
---|
191 | #endif |
---|
192 | |
---|
193 | /* check if output file exists and filename's validation */ |
---|
194 | if(file_exist(argv[4])) { |
---|
195 | sprintf(temp, "Output file %s exists, will be overwritten.", argv[4]); |
---|
196 | warning(151, temp); |
---|
197 | } |
---|
198 | |
---|
199 | /* file format transfer... */ |
---|
200 | convert(argv[2], argv[4], intype, outtype); |
---|
201 | return 0; |
---|
202 | |
---|
203 | } |
---|
204 | /* --------------------------------------------------------------- |
---|
205 | * Function file_type |
---|
206 | * According to the first line in the file to decide |
---|
207 | * the file type. File type could be Genbank, Macke,... |
---|
208 | */ |
---|
209 | int |
---|
210 | file_type(filename) |
---|
211 | char *filename; |
---|
212 | { |
---|
213 | /* char line[LINENUM]; */ |
---|
214 | char token[LINENUM], temp[LINENUM]; |
---|
215 | /* int Cmpstr(); */ |
---|
216 | FILE *fp; |
---|
217 | /* void error(); */ |
---|
218 | |
---|
219 | if((fp=fopen(filename, "r"))==NULL) { |
---|
220 | sprintf(temp, "Cannot open file: %s. Exit.\n", filename); |
---|
221 | error(5, temp); |
---|
222 | } |
---|
223 | /* rewind(fp); */ |
---|
224 | fscanf(fp, "%s", token); |
---|
225 | if(Cmpstr(token, "LOCUS")==EQ) |
---|
226 | return(GENBANK); |
---|
227 | else if(Cmpstr(token, "#-")==EQ) |
---|
228 | return(MACKE); |
---|
229 | else if(Cmpstr(token, "ID")==EQ) |
---|
230 | return(PROTEIN); |
---|
231 | else if(Cmpstr(token, "#NEXUS")==EQ) |
---|
232 | return(PAUP); |
---|
233 | else if(isnum(token)) |
---|
234 | return(PHYLIP); |
---|
235 | else return(UNKNOWN); |
---|
236 | } |
---|
237 | /* ----------------------------------------------------------------- |
---|
238 | * Function isnum(). |
---|
239 | * Return TRUE if the string is an integer number. |
---|
240 | */ |
---|
241 | int |
---|
242 | isnum(string) |
---|
243 | char *string; |
---|
244 | { |
---|
245 | int indi, length, flag; |
---|
246 | |
---|
247 | for(indi=0, length=Lenstr(string), flag=1; flag&&indi<length; indi++) |
---|
248 | if(!isdigit(string[indi])) flag = 0; |
---|
249 | |
---|
250 | return(flag); |
---|
251 | } |
---|
252 | /* ------------------------------------------------------------------ |
---|
253 | * Function file_exist(). |
---|
254 | * Check if file is already existed and also check file |
---|
255 | * name is valid or not. |
---|
256 | */ |
---|
257 | int |
---|
258 | file_exist(file_name) |
---|
259 | char *file_name; |
---|
260 | { |
---|
261 | /* int Lenstr(); */ |
---|
262 | /* void error(); */ |
---|
263 | char temp[TOKENNUM]; |
---|
264 | |
---|
265 | if(Lenstr(file_name)<=0) { |
---|
266 | sprintf(temp, "ilegal file name: %s, EXIT.", file_name); |
---|
267 | error(152, temp); |
---|
268 | } |
---|
269 | return((fopen(file_name, "r")!=NULL)); |
---|
270 | } |
---|
271 | /* ------------------------------------------------------------------- |
---|
272 | * Function change_file_suffix(). |
---|
273 | * Define the default file name by changing suffix. |
---|
274 | */ |
---|
275 | void |
---|
276 | change_file_suffix(old_file, file_name, type) |
---|
277 | char *old_file, *file_name; |
---|
278 | int type; |
---|
279 | { |
---|
280 | int indi, indj; |
---|
281 | /* char *Catstr(); */ |
---|
282 | /* void Cpystr(); */ |
---|
283 | |
---|
284 | for(indi=Lenstr(old_file)-1;indi>=0&&old_file[indi]!='.'; indi--) |
---|
285 | if(indi==0) |
---|
286 | Cpystr(file_name, old_file); |
---|
287 | else { |
---|
288 | for(indj=0; indj<(indi-1); indj++) |
---|
289 | file_name[indj]=old_file[indj]; |
---|
290 | file_name[indj]='\0'; |
---|
291 | } |
---|
292 | switch(type) { |
---|
293 | case GENBANK: Catstr(file_name, ".GB"); |
---|
294 | break; |
---|
295 | case MACKE: Catstr(file_name, ".aln"); |
---|
296 | break; |
---|
297 | case PAUP: Catstr(file_name, ".PAUP"); |
---|
298 | break; |
---|
299 | case PHYLIP: Catstr(file_name, ".PHY"); |
---|
300 | break; |
---|
301 | case EMBL: Catstr(file_name, ".EMBL"); |
---|
302 | break; |
---|
303 | case PRINTABLE: Catstr(file_name, ".prt"); |
---|
304 | break; |
---|
305 | case ALMA: Catstr(file_name, ".ALMA"); |
---|
306 | break; |
---|
307 | default: Catstr(file_name, ".???"); |
---|
308 | } |
---|
309 | } |
---|