Debian Codegraph @

hmmer/easel/miniapps/esl-reformat.c

raw symbols SCIP
1 /* Convert between sequence file formats
2 */
3 #include <esl_config.h>
4
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <time.h>
9
10 #include "easel.h"
11 #include "esl_getopts.h"
12 #include "esl_mem.h"
13 #include "esl_msa.h"
14 #include "esl_msafile.h"
15 #include "esl_sq.h"
16 #include "esl_sqio.h"
17 #include "esl_wuss.h"
18
19 static char banner[] = "convert between sequence file formats";
20
21 static char usage[] = "[-options] <format> <seqfile>\n\
22 Output format choices: Unaligned Aligned \n\
23 ----------- ------- \n\
24 fasta a2m \n\
25 hmmpgmd afa \n\
26 clustal \n\
27 clustallike\n\
28 pfam \n\
29 phylip \n\
30 phylips \n\
31 psiblast \n\
32 selex \n\
33 stockholm \n\
34 \n";
35
36 #define INCOMPATWITHSMALLOPT "--mingap,--nogap,--ignore,--acceptx"
37
38 static ESL_OPTIONS options[] = {
39 /* name type default env range togs reqs incompat help docgroup */
40 { "-d", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-r", "convert to DNA alphabet (U->T)", 0 },
41 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "help; print brief info on version and usage", 0 },
42 { "-l", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-u", "convert to lower case", 0 },
43 { "-n", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-x", "remove DNA IUPAC codes; convert ambig chars to N", 0 },
44 { "-o", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "send output to file <f>, not stdout", 0 },
45 { "-r", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-d", "convert to RNA alphabet (T->U)", 0 },
46 { "-u", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-l", "convert to upper case", 0 },
47 { "-x", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-n", "convert non-IUPAC chars (e.g. X) in DNA to N", 0 },
48 { "--gapsym", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, "--mingap,--nogap", "convert all gaps to character <s>", 0 },
49 { "--informat", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "input sequence file is in format <s>", 0 },
50 { "--mingap", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--nogap", "remove columns containing all gaps (seqfile=MSA)", 0 },
51 { "--keeprf", eslARG_NONE, FALSE, NULL, NULL, NULL, "--mingap", NULL, "with --mingap, keep all nongap #=GC RF columns", 0 },
52 { "--nogap", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--mingap,--gapsym", "remove columns containing any gaps (seqfile=MSA)", 0 },
53 { "--wussify", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--dewuss,--fullwuss", "convert old RNA structure markup lines to WUSS", 0 },
54 { "--dewuss", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--wussify,--fullwuss","convert WUSS RNA structure markup to old format", 0 },
55 { "--fullwuss", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--wussify,--dewuss", "convert simple WUSS notation to full (output) WUSS", 0 },
56 { "--ignore", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "ignore input seq characters listed in string <s>", 0 },
57 { "--acceptx", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "accept input seq chars in string <s> as X", 0 },
58 { "--rename", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "rename and number each sequence <s>.<n>", 0 },
59 { "--replace", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "<s> = <s1>:<s2> replace characters in <s1> with those in <s2>", 0},
60 { "--small", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, INCOMPATWITHSMALLOPT, "use minimal RAM, input must be pfam, output must be afa or pfam",0 },
61 { "--id_map", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "if format is hmmpgmd, put the id map into file <s>", 0 },
62 { "--namelen", eslARG_INT, NULL, NULL,"n>0", NULL, NULL, NULL, "for PHYLIP formats, set name len to <n> not 10", 0 },
63 { 0,0,0,0,0,0,0,0 },
64 };
65
66 static void symconvert(char *s, char *oldsyms, char *newsyms);
67 static void regurgitate_pfam_as_afa(ESL_MSAFILE *afp, FILE *ofp, char *alifile, char *gapsym, int force_lower, int force_upper,
68 int force_rna, int force_dna, int iupac_to_n, int x_is_bad, char *rename, char *rfrom,
69 char *rto, int *ret_reached_eof);
70 static int regurgitate_pfam_as_pfam(ESL_MSAFILE *afp, FILE *ofp, char *gapsym, int force_lower, int force_upper, int force_rna,
71 int force_dna, int iupac_to_n, int x_is_bad, int wussify, int dewuss, int fullwuss,
72 char *rfrom, char *rto);
73 static int parse_replace_string(const char *rstring, char **ret_from, char **ret_to);
74
75 int
76 main(int argc, char **argv)
77 {
78 ESL_GETOPTS *go; /* application configuration */
79 char *infile; /* name of input sequence file */
80 int infmt = eslSQFILE_UNKNOWN; /* input format as a code; eslSQFILE_FASTA */
81 int outfmt = eslSQFILE_UNKNOWN; /* output format as a code */
82 int status; /* return code from an Easel call */
83 FILE *ofp; /* output stream */
84
85 char *outfile; /* output file, or NULL */
86 int force_rna; /* TRUE to force RNA alphabet */
87 int force_dna; /* TRUE to force DNA alphabet */
88 int force_lower; /* TRUE to force lower case */
89 int force_upper; /* TRUE to force upper case */
90 int iupac_to_n; /* TRUE to convert ambiguities all to N's */
91 int x_is_bad; /* TRUE to convert X to N */
92 int do_mingap; /* TRUE to remove cols containing all gaps */
93 int do_nogap; /* TRUE to remove cols containing any gaps */
94 char *gapsym; /* NULL if unset; else, char for gaps */
95 int wussify; /* TRUE to convert old KH SS markup to WUSS */
96 int dewuss; /* TRUE to convert WUSS back to old KH */
97 int fullwuss; /* TRUE to convert simple WUSS to full WUSS */
98 char *rename; /* if non-NULL rename seqs to <s>.<n> */
99 int do_small; /* TRUE to operate in small memory mode */
100 int do_fixbps; /* TRUE to assume SS/SS_cons are for WUSS RNA*/
101 char *rstring; /* <s> from --replace <s> */
102 char *rfrom, *rto; /* <s1> and <s2> from --replace <s>=<s1>:<s2>*/
103 int reached_eof; /* reached EOF? used only in small mem mode */
104 int idx; /* counter over sequences */
105 int nali; /* number of alignments read */
106 int phylip_namelen = -1; /* -1 = use default of 10 */
107 char errbuf[eslERRBUFSIZE]; /* for error messages */
108
109
110
111 /*****************************************************************
112 * Parse the command line
113 *****************************************************************/
114
115 go = esl_getopts_Create(options);
116 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK ||
117 esl_opt_VerifyConfig(go) != eslOK)
118 {
119 printf("Failed to parse command line: %s\n", go->errbuf);
120 esl_usage(stdout, argv[0], usage);
121 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
122 exit(1);
123 }
124
125 if (esl_opt_GetBoolean(go, "-h"))
126 {
127 esl_banner(stdout, argv[0], banner);
128 esl_usage (stdout, argv[0], usage);
129 puts(" where options are:\n");
130 esl_opt_DisplayHelp(stdout, go, 0, 2, 80); /* 0= group; 2 = indentation; 80=textwidth*/
131 exit(0);
132 }
133
134 if (esl_opt_ArgNumber(go) != 2)
135 {
136 printf("Incorrect number of command line arguments.\n");
137 esl_usage(stdout, argv[0], usage);
138 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
139 exit(1);
140 }
141
142 outfmt = esl_sqio_EncodeFormat(esl_opt_GetArg(go, 1));
143 if (outfmt == eslSQFILE_UNKNOWN)
144 esl_fatal("%s is not a recognized output seqfile format\n", esl_opt_GetArg(go, 1));
145
146 infile = esl_opt_GetArg(go, 2);
147
148 if (esl_opt_IsOn(go, "--informat") &&
149 (infmt = esl_sqio_EncodeFormat(esl_opt_GetString( go, "--informat"))) == eslSQFILE_UNKNOWN)
150 esl_fatal("%s is not a recognized input seqfile format\n", esl_opt_GetString(go, "--informat"));
151
152 force_dna = esl_opt_GetBoolean(go, "-d");
153 force_lower = esl_opt_GetBoolean(go, "-l");
154 iupac_to_n = esl_opt_GetBoolean(go, "-n");
155 outfile = esl_opt_GetString (go, "-o");
156 force_rna = esl_opt_GetBoolean(go, "-r");
157 force_upper = esl_opt_GetBoolean(go, "-u");
158 x_is_bad = esl_opt_GetBoolean(go, "-x");
159 gapsym = esl_opt_GetString( go, "--gapsym");
160 do_mingap = esl_opt_GetBoolean(go, "--mingap");
161 do_nogap = esl_opt_GetBoolean(go, "--nogap");
162 wussify = esl_opt_GetBoolean(go, "--wussify");
163 dewuss = esl_opt_GetBoolean(go, "--dewuss");
164 fullwuss = esl_opt_GetBoolean(go, "--fullwuss");
165 rename = esl_opt_GetString (go, "--rename");
166 do_small = esl_opt_GetBoolean(go, "--small");
167 rstring = esl_opt_GetString( go, "--replace");
168 do_fixbps = (force_rna || force_dna || wussify || dewuss || fullwuss) ? TRUE : FALSE;
169
170 if (esl_opt_IsOn(go, "--namelen"))
171 phylip_namelen = esl_opt_GetInteger(go, "--namelen");
172
173 /* if --small, make sure infmt == pfam and (outfmt == afa || outfmt == pfam) */
174 if(do_small && (infmt != eslMSAFILE_PFAM || (outfmt != eslMSAFILE_AFA && outfmt != eslMSAFILE_PFAM)))
175 esl_fatal("--small requires '--informat pfam' and output format of either 'afa' or 'pfam'");
176
177 if (gapsym != NULL && strlen(gapsym) != 1)
178 esl_fatal("Argument to --gapsym must be a single character.");
179
180 if (outfile == NULL) ofp = stdout;
181 else if ((ofp = fopen(outfile, "w")) == NULL)
182 esl_fatal("Failed to open output file %s\n", outfile);
183
184 if (rstring == NULL) {
185 rfrom = NULL;
186 rto = NULL;
187 }
188 else {
189 if((status = parse_replace_string(rstring, &rfrom, &rto)) != eslOK) esl_fatal("Out of memory");
190 }
191
192 /***********************************************
193 * Reformat the file, printing to stdout.
194 ***********************************************/
195
196 /* If the output format is an alignment, then the input format
197 * has to be an alignment.
198 */
199 if (esl_sqio_IsAlignment(outfmt))
200 {
201 ESL_MSAFILE *afp;
202 ESL_MSA *msa;
203
204 status = esl_msafile_Open(NULL, infile, NULL, infmt, NULL, &afp);
205 if (status != eslOK) esl_msafile_OpenFailure(afp, status);
206
207 if ( esl_opt_IsOn(go, "--ignore")) esl_fatal("The --ignore option is unimplemented for alignment reformatting.");
208 if ( esl_opt_IsOn(go, "--acceptx")) esl_fatal("The --acceptx option is unimplemented for alignment reformatting.");
209
210 nali = 0;
211
212 if (do_small) {
213 if(infmt == eslMSAFILE_PFAM && outfmt == eslMSAFILE_AFA) {
214 if (afp->bf->mode_is == eslBUFFER_STREAM) esl_fatal("--small with afa out format and stdin input is unimplemented.");
215 regurgitate_pfam_as_afa(afp, ofp, infile, gapsym, force_lower, force_upper, force_rna, force_dna, iupac_to_n, x_is_bad, rename, rfrom, rto, &reached_eof);
216 if(! reached_eof) esl_fatal("Input file contains >1 alignments, but afa formatted output file can only contain 1");
217 }
218 else if (infmt == eslMSAFILE_PFAM && outfmt == eslMSAFILE_PFAM) {
219 if(rename != NULL) esl_fatal("--rename is unimplemented for combination of --small and output format pfam");
220 while((status = regurgitate_pfam_as_pfam(afp, ofp, gapsym, force_lower, force_upper, force_rna, force_dna, iupac_to_n, x_is_bad, wussify, dewuss, fullwuss, rfrom, rto)) != eslEOF) {
221 if (status == eslEFORMAT) esl_fatal("--small alignment file parse error:\n%s\n", afp->errmsg);
222 else if (status == eslEINVAL) esl_fatal("--small alignment file parse error:\n%s\n", afp->errmsg);
223 else if (status != eslOK) esl_fatal("--small alignment file read failed with error code %d\n", status);
224 }
225 esl_msafile_Close(afp);
226 }
227 else { /* do_small enabled, but neither (infmt==pfam && outfmt=afa) nor (infmt==pfam && outfmt==pfam) */
228 esl_fatal("--small requires '--informat pfam' and output format of either 'afa' or 'pfam'");
229 }
230 }
231 else { /* normal mode, --small not enabled */
232 while ((status = esl_msafile_Read(afp, &msa)) != eslEOF)
233 {
234 if (status != eslOK) esl_msafile_ReadFailure(afp, status);
235 nali++;
236
237 if (nali > 1 && ! esl_msafile_IsMultiRecord(outfmt))
238 esl_fatal("Input file contains >1 alignments, but %s formatted output file can only contain 1", esl_msafile_DecodeFormat(outfmt));
239
240 if (do_mingap) if((status = esl_msa_MinimGapsText(msa, errbuf, "-_.~", esl_opt_GetBoolean(go, "--keeprf"), do_fixbps)) != eslOK) esl_fatal(errbuf);
241 if (do_nogap) if((status = esl_msa_NoGapsText (msa, errbuf, "-_.~", do_fixbps)) != eslOK) esl_fatal(errbuf);
242 if (rfrom) esl_msa_SymConvert(msa, rfrom, rto);
243 if (gapsym) esl_msa_SymConvert(msa, "-_.", gapsym);
244 if (force_lower) esl_msa_SymConvert(msa,
245 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
246 "abcdefghijklmnopqrstuvwxyz");
247 if (force_upper) esl_msa_SymConvert(msa,
248 "abcdefghijklmnopqrstuvwxyz",
249 "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
250 if (force_rna) esl_msa_SymConvert(msa, "Tt", "Uu");
251 if (force_dna) esl_msa_SymConvert(msa, "Uu", "Tt");
252 if (iupac_to_n) esl_msa_SymConvert(msa,
253 "RYMKSWHBVDrymkswhbvd",
254 "NNNNNNNNNNnnnnnnnnnn");
255 if (x_is_bad) esl_msa_SymConvert(msa, "Xx", "Nn");
256
257 if (rename)
258 {
259 for (idx = 0; idx < msa->nseq; idx++)
260 esl_msa_FormatSeqName(msa, idx, "%s.%d", rename, idx+1);
261 }
262
263 if (wussify)
264 {
265 if (msa->ss_cons) esl_kh2wuss(msa->ss_cons, msa->ss_cons);
266 if (msa->ss)
267 for (idx = 0; idx < msa->nseq; idx++)
268 if (msa->ss[idx]) esl_kh2wuss(msa->ss[idx], msa->ss[idx]);
269 }
270
271 if (dewuss)
272 {
273 if (msa->ss_cons) esl_wuss2kh(msa->ss_cons, msa->ss_cons);
274 if (msa->ss)
275 for (idx = 0; idx < msa->nseq; idx++)
276 if (msa->ss[idx]) esl_wuss2kh(msa->ss[idx], msa->ss[idx]);
277 }
278
279 if (fullwuss)
280 {
281 if (msa->ss_cons != NULL)
282 {
283 status = esl_wuss_full(msa->ss_cons, msa->ss_cons);
284 if (status == eslESYNTAX) esl_fatal("Bad consensus SS: not in WUSS format\n");
285 else if (status != eslOK) esl_fatal("Conversion of SS_cons failed, code %d\n", status);
286 }
287 if (msa->ss != NULL)
288 for (idx = 0; idx < msa->nseq; idx++)
289 if (msa->ss[idx] != NULL)
290 {
291 status = esl_wuss_full(msa->ss[idx], msa->ss[idx]);
292 if (status == eslESYNTAX) esl_fatal("Bad SS for %s: not in WUSS format\n", msa->sqname[idx]);
293 else if (status != eslOK) esl_fatal("Conversion of SS for %s failed, code %d\n", msa->sqname[idx], status);
294 }
295 }
296
297 if (phylip_namelen > 0 && (outfmt == eslMSAFILE_PHYLIP || outfmt == eslMSAFILE_PHYLIPS))
298 {
299 ESL_MSAFILE_FMTDATA optfmt;
300 esl_msafile_fmtdata_Init(&optfmt);
301 optfmt.namewidth = phylip_namelen;
302 esl_msafile_phylip_Write(ofp, msa, outfmt, &optfmt);
303 }
304 else
305 esl_msafile_Write(ofp, msa, outfmt);
306
307
308 esl_msa_Destroy(msa);
309 }
310 }
311 esl_msafile_Close(afp);
312 } /* end of alignment->alignment conversion */
313 else
314 { /* else: conversion to unaligned file formats */
315 ESL_SQFILE *sqfp; /* open input sequence file */
316 ESL_SQ *sq; /* an input sequence */
317 char *mapfile; /* file name into which an hmmpgmd map file should be written */
318 FILE *mapfp; /* output stream for the map file */
319
320 status = esl_sqfile_Open(infile, infmt, NULL, &sqfp);
321 if (status == eslENOTFOUND)
322 esl_fatal("Couldn't open seqfile %s\n", infile);
323 else if (status == eslEFORMAT)
324 esl_fatal("Couldn't determine format of seqfile %s\n", infile);
325 else if (status == eslEINVAL)
326 esl_fatal("Can't autodetect format of stdin or .gz; use --informat\n");
327 else if (status != eslOK)
328 esl_fatal("Open of seqfile %s failed, code %d\n", infile, status);
329
330 if ( esl_opt_IsOn(go, "--ignore")) esl_sqio_Ignore (sqfp, esl_opt_GetString(go, "--ignore"));
331 if ( esl_opt_IsOn(go, "--acceptx")) esl_sqio_AcceptAs(sqfp, esl_opt_GetString(go, "--acceptx"), 'X');
332
333 sq = esl_sq_Create();
334
335 if ( outfmt == eslSQFILE_HMMPGMD ) {
336 int64_t res_cnt = 0;
337 char timestamp[32];
338 time_t date;
339
340 //will need to make two passes through the file, one to get sequence count, one to convert sequences
341 if (! esl_sqfile_IsRewindable(sqfp))
342 esl_fatal("Target sequence file %s isn't rewindable; can't produce a file in hmmpgmd format", infile);
343
344
345 //pick map file name, and open the file
346 if (esl_opt_IsUsed(go, "--id_map")) {
347 mapfile = esl_opt_GetString( go, "--id_map");
348 } else {
349 esl_strdup(infile, -1, &mapfile); // there will be an infile, because stdin was restricted above
350 esl_strcat(&mapfile, -1, ".map", 4);
351 }
352 if ((mapfp = fopen(mapfile, "w")) == NULL)
353 esl_fatal("Failed to open map output file %s\n", mapfile);
354
355 //get counts, and write out the map file
356 idx = 0;
357 while ((status = esl_sqio_Read(sqfp, sq)) == eslOK) {
358 res_cnt += sq->n;
359 esl_sq_Reuse(sq);
360 idx++;
361 }
362
363 /* status should be eslEOF on normal end; if it isn't, deal w/ error */
364 if (status == eslEFORMAT) esl_fatal("Parse failed (sequence file %s):\n%s\n",
365 sqfp->filename, esl_sqfile_GetErrorBuf(sqfp));
366 else if (status != eslEOF) esl_fatal("Unexpected error %d reading sequence file %s",
367 status, sqfp->filename);
368
369 /* Print the first line of the hmmpgmd format, which contains database information.
370 * #<res_count> <seq_count> <db_count> <db_sequences_1> <db_sequences_before_removing_duplicates_1> <db_sequences_2> <db_sequences_before_removing_duplicates_2> ... <date_stamp>
371 * It shoves all sequences into a single database, #1.
372 */
373 date = time(NULL);
374 ctime_r(&date, timestamp);
375 fprintf(mapfp, "%d\n", idx);
376 fprintf(ofp, "#%" PRId64 " %d %d %d %d %s", res_cnt, idx, 1, idx, idx, timestamp);
377
378 esl_sqfile_Position(sqfp, 0);
379 }
380
381
382 idx = 0;
383 while ((status = esl_sqio_Read(sqfp, sq)) == eslOK)
384 {
385 if (rfrom!=NULL) symconvert(sq->seq, rfrom, rto);
386 if (force_lower) symconvert(sq->seq,
387 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
388 "abcdefghijklmnopqrstuvwxyz");
389 if (force_upper) symconvert(sq->seq,
390 "abcdefghijklmnopqrstuvwxyz",
391 "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
392 if (force_rna) symconvert(sq->seq, "Tt", "Uu");
393 if (force_dna) symconvert(sq->seq, "Uu", "Tt");
394 if (iupac_to_n) symconvert(sq->seq,
395 "RYMKSWHBVDrymkswhbvd",
396 "NNNNNNNNNNnnnnnnnnnn");
397 if (x_is_bad) symconvert(sq->seq, "Xx", "Nn");
398
399 if (wussify && sq->ss != NULL) esl_kh2wuss(sq->ss, sq->ss);
400 if (dewuss && sq->ss != NULL) esl_wuss2kh(sq->ss, sq->ss);
401
402 if (fullwuss && sq->ss != NULL)
403 {
404 status = esl_wuss_full(sq->ss, sq->ss);
405 if (status == eslESYNTAX)
406 esl_fatal("Bad SS for %s: not in WUSS format\n", sq->name);
407 else if (status != eslOK)
408 esl_fatal("Conversion of SS for %s failed, code %d\n",
409 sq->name, status);
410 }
411
412 if ( outfmt == eslSQFILE_HMMPGMD ) {
413 fprintf(mapfp, "%d %s %s\n", idx+1, sq->name?sq->name:"", sq->desc?sq->desc:"");
414 esl_sq_FormatName(sq, "%d 1", idx+1);
415 esl_sq_SetDesc (sq, "");
416 } else {
417 if (rename) esl_sq_FormatName(sq, "%s.%d", rename, idx+1);
418 }
419 esl_sqio_Write(ofp, sq, outfmt, FALSE);
420 esl_sq_Reuse(sq);
421 idx++;
422 }
423 /* status should be eslEOF on normal end; if it isn't, deal w/ error */
424 if (status == eslEFORMAT) esl_fatal("Parse failed (sequence file %s):\n%s\n",
425 sqfp->filename, esl_sqfile_GetErrorBuf(sqfp));
426 else if (status != eslEOF) esl_fatal("Unexpected error %d reading sequence file %s",
427 status, sqfp->filename);
428
429 if ( outfmt == eslSQFILE_HMMPGMD )
430 fclose(mapfp);
431
432 esl_sq_Destroy(sq);
433 esl_sqfile_Close(sqfp);
434 } /* end of unaligned seq conversion */
435
436 if (ofp != stdout) fclose(ofp);
437 esl_getopts_Destroy(go);
438
439 if(rfrom != NULL) free(rfrom);
440 if(rto != NULL) free(rto);
441
442 exit(0);
443 }
444
445 /* symconvert()
446 *
447 * single seq version of esl_msa_SymConvert(); see
448 * documentation there.
449 *
450 * no reason yet to include in sqio API, but that may change.
451 *
452 * inefficient to use this for upper/lower case conversion,
453 * prob by an order of magnitude (because of the strchr() call,
454 * which could be replaced by a range test), but I bet it's
455 * unnoticeable.
456 */
457 static void
458 symconvert(char *s, char *oldsyms, char *newsyms)
459 {
460 int pos;
461 char *sptr;
462 int special;
463
464 special = (strlen(newsyms) == 1 ? TRUE : FALSE);
465
466 for (pos = 0; s[pos] != '\0'; pos++)
467 if ((sptr = strchr(oldsyms, s[pos])) != NULL)
468 s[pos] = (special ? *newsyms : newsyms[sptr-oldsyms]);
469 }
470
471
472
473 /* regurgitate_pfam_as_afa()
474 *
475 * Given an open Pfam formatted msafile, read the next alignment and
476 * regurgitate it in aligned FASTA (AFA) format without storing
477 * it in a esl_msa data structure.
478 *
479 * We need to do two passes through the file because in Pfam
480 * sequence accessions (#=GS <seqname> AC) and sequence descriptions
481 * (#=GS <seqname> DE) appear altogether before any aligned sequence
482 * data, while in AFA they appear on the same line as the sequence
483 * name (accession, then description).
484 *
485 * Example:
486 * # STOCKHOLM 1.0
487 * #=GS tRNA1 AC RF00005-1
488 * #=GS tRNA2 AC RF00005-2
489 * #=GS tRNA1 DE first tRNA
490 * #=GS tRNA2 DE second tRNA
491 *
492 * tRNA1 GCGGAUUUAGCUCAGUUGGG.AGAGCGCCAGACUGAAGAUCUGGAGGUCCUGUGUUCGAUCCACAGAAUUCGCA
493 * tRNA2 UCCGAUAUAGUGUAAC.GGCUAUCACAUCACGCUUUCACCGUGGAGA.CCGGGGUUCGACUCCCCGUAUCGGAG
494 *
495 * converts to AFA:
496 * >tRNA1 RF00005-1 first tRNA
497 * GCGGAUUUAGCUCAGUUGGG.AGAGCGCCAGACUGAAGAUCUGGAGGUCCUGUGUUCGAU
498 * CCACAGAAUUCGCA
499 * >tRNA2 RF00005-2 second tRNA
500 * UCCGAUAUAGUGUAAC.GGCUAUCACAUCACGCUUUCACCGUGGAGA.CCGGGGUUCGAC
501 * UCCCCGUAUCGGAG
502 *
503 * In the first pass, output the sequence names and accessions we find
504 * as '#=GS <seqname> AC' lines in the Pfam alignment to an accession
505 * tmpfile, and output sequence names and descriptions we find as
506 * as '#=GS <seqname> DE' lines in the Pfam alignment to a description
507 * tmpfile.
508 *
509 * In the second pass, rewind all (up to 3) files: <ac_tmpfile>,
510 * <de_tmpfile> and the Pfam alignment file and start reading them
511 * again. As we're reading them, output the accessions, descriptions
512 * and aligned sequence data in the proper order to an aligned FASTA
513 * file.
514 *
515 * Set <ret_reached_eof> as TRUE if the alignment read and reformatted
516 * appears to be the only one remaining in afp. Set <ret_reached_eof>
517 * as FALSE if afp appears to include at least one more alignment.
518 *
519 * Returns void. Dies upon any input error.
520 */
521 static void
522 regurgitate_pfam_as_afa(ESL_MSAFILE *afp, FILE *ofp, char *alifile, char *gapsym, int force_lower, int force_upper, int force_rna, int force_dna, int iupac_to_n, int x_is_bad, char *rename, char *rfrom, char *rto, int *ret_reached_eof)
523 {
524 char *p = NULL;
525 esl_pos_t n = 0;
526 esl_pos_t gslen, seqnamelen, taglen;
527 char *seqname = NULL;
528 char *first_seqname = NULL;
529 char *tag = NULL;
530 char *gs = NULL;
531 int nseq_read = 0;
532 int reached_eof;
533 /* variables related to reading accessions */
534 char ac_tmpfile[16] = "esltmpXXXXXX";
535 FILE *ac_fp = NULL; /* file ptr for accession tmpfile */
536 char *ac_buf = NULL; /* buffer for line input w/ sre_fgets() */
537 int ac_buflen = 0; /* current allocated length for buf */
538 char *ac_s = NULL;
539 char *ac_seqname = NULL;
540 char *ac = NULL;
541 int have_ac = FALSE;
542 /* variables related to reading descriptions */
543 char de_tmpfile[16] = "esltmpXXXXXX";
544 FILE *de_fp = NULL; /* file ptr for description tmpfile */
545 char *de_buf = NULL; /* buffer for line input w/ sre_fgets() */
546 int de_buflen = 0; /* current allocated length for buf */
547 char *de_s = NULL;
548 char *de_seqname = NULL;
549 char *de = NULL;
550 int have_de = FALSE;
551 /* variables related to printing out sequences */
552 char *aseq = NULL;
553 esl_pos_t aseqlen = 0;
554 int64_t apos;
555 char aseqbuf[61];
556 int cpl = 60; /* number of residues per afa seq line */
557 int acpl; /* actual number of character per line */
558 int status;
559
560 afp->errmsg[0] = '\0';
561
562 /**************************************************************************************************
563 * First pass, go through each line of the Pfam file and output all GS DE and AC annotation to tmpfiles
564 **************************************************************************************************/
565
566 /* Check the magic Stockholm header line, allowing blank lines */
567 do {
568 status = esl_msafile_GetLine(afp, &p, &n);
569 if (status == eslEOF) return;
570 else if (status != eslOK) esl_fatal("small mem parse error. problem reading line %d of msafile", (int) afp->linenumber);
571 } while (esl_memspn(afp->line, afp->n, " \t") == afp->n || /* skip blank lines */
572 (esl_memstrpfx(afp->line, afp->n, "#") /* and skip comment lines */
573 && ! esl_memstrpfx(afp->line, afp->n, "# STOCKHOLM"))); /* but stop on Stockholm header */
574
575 if (! esl_memstrpfx(afp->line, afp->n, "# STOCKHOLM 1.")) esl_fatal("small mem parse failed (line %d): missing \"# STOCKHOLM\" header", (int) afp->linenumber);
576
577 while ((status = esl_msafile_GetLine(afp, &p, &n)) == eslOK)
578 {
579 while (n && ( *p == ' ' || *p == '\t')) { p++; n--; } /* skip leading whitespace */
580
581 if (esl_memstrpfx(p, n, "#=GS"))
582 { /* only lines we need to check are AC and DE lines, we don't even check other lines for validity */
583 if (esl_memtok(&p, &n, " \t", &gs, &gslen) != eslOK) esl_fatal("small mem parse failed (line %d) in a way that can't happen", (int) afp->linenumber);
584 if (esl_memtok(&p, &n, " \t", &seqname, &seqnamelen) != eslOK) esl_fatal("small mem parse failed (line %d): #=GS line missing <seqname>, <tag>, annotation", (int) afp->linenumber);
585 if (esl_memtok(&p, &n, " \t", &tag, &taglen) != eslOK) esl_fatal("small mem parse failed (line %d): #=GS line missing <tag>, annotation", (int) afp->linenumber);
586 if (! esl_memstrcmp(gs, gslen, "#=GS")) esl_fatal("small mem parse failed (line %d): faux #=GS line?", (int) afp->linenumber);
587
588 if (esl_memstrcmp(tag, taglen, "AC"))
589 {
590 if (! ac_fp && esl_tmpfile(ac_tmpfile, &ac_fp) != eslOK) esl_fatal("small mem parse failed, unable to open accession tmpfile");
591 fprintf(ac_fp, "%.*s %.*s\n", (int) seqnamelen, seqname, (int) n, p); // cppcheck thinks ac_fp is a "possible null pointer deference"; I think it's wrong.
592 }
593 if (esl_memstrcmp(tag, taglen, "DE"))
594 {
595 if (! de_fp && esl_tmpfile(de_tmpfile, &de_fp) != eslOK) esl_fatal("small mem parse failed, unable to open description tmpfile");
596 fprintf(de_fp, "%.*s %.*s\n", (int) seqnamelen, seqname, (int) n, p); // cppcheck thinks de_fp is a "possible null pointer deference"; I think it's wrong.
597 }
598 }
599 else if (esl_memstrpfx(p, n, "//")) break;
600 }
601 if (status == eslEOF) esl_fatal("small mem parse failed (line %d): missing // terminator", (int) afp->linenumber);
602 else if (status != eslOK) esl_fatal("small mem parse failed (line %d) with code %d", (int) afp->linenumber, status);
603
604 /* The regurgitate_*() functions are limited, and only deal with single-record Pfam files.
605 * If there appears to be more data in the file, drop the reached_eof flag.
606 */
607 while ((status = esl_msafile_GetLine(afp, &p, &n)) == eslOK)
608 {
609 while (n && ( *p == ' ' || *p == '\t')) { p++; n--; } /* skip leading whitespace */
610 if (esl_memstrpfx(p, n, "# STOCKHOLM 1.")) break;
611 if (n && ! esl_memstrpfx(p, n, "#")) esl_fatal("small mem parse failed (line %d): unexpected data", (int) afp->linenumber);
612 }
613 if (status == eslOK) reached_eof = FALSE;
614 else if (status == eslEOF) reached_eof = TRUE;
615 else esl_fatal("--small parse error. problem reading line %d of msafile", (int) afp->linenumber);
616
617 /*****************************************************************
618 * Pass 1 complete; rewind (close/reopen) all files
619 *****************************************************************/
620
621 esl_msafile_Close(afp);
622 if ((status = esl_msafile_Open(NULL, alifile, NULL, eslMSAFILE_PFAM, NULL, &afp)) != eslOK)
623 esl_fatal("--small, second pass, unable to open file %s for reading", alifile);
624
625 if (ac_fp) { /* open the tmpfile with the seq accessions */
626 rewind(ac_fp);
627 if((status = esl_fgets(&(ac_buf), &(ac_buflen), ac_fp)) != eslOK) esl_fatal("--small accession tmpfile parse failed");
628 ac_s = ac_buf;
629 if (esl_strtok_adv(&ac_s, " \t\n\r", &ac_seqname, NULL, NULL) != eslOK) esl_fatal("--small accession tmpfile parse failed");
630 if (esl_strtok_adv(&ac_s, "\n\r", &ac, NULL, NULL) != eslOK) esl_fatal("--small accession tmpfile parse failed");
631 }
632 if (de_fp) { /* open the tmpfile with the seq descriptions */
633 rewind(de_fp);
634 if((status = esl_fgets(&(de_buf), &(de_buflen), de_fp)) != eslOK) esl_fatal("--small description tmpfile parse failed");
635 de_s = de_buf;
636 if (esl_strtok_adv(&de_s, " \t\n\r", &de_seqname, NULL, NULL) != eslOK) esl_fatal("--small description tmpfile parse failed");
637 if (esl_strtok_adv(&de_s, "\n\r", &de, NULL, NULL) != eslOK) esl_fatal("--small description tmpfile parse failed");
638 }
639
640 /******************************************************************************************
641 * Pass 2, step through files, outputting appropriately
642 ******************************************************************************************/
643
644 do {
645 status = esl_msafile_GetLine(afp, &p, &n);
646 if (status == eslEOF) return;
647 else if (status != eslOK) esl_fatal("small mem parse pass 2 error. problem reading line %d of msafile", (int) afp->linenumber);
648 } while (esl_memspn(afp->line, afp->n, " \t") == afp->n || /* skip blank lines */
649 (esl_memstrpfx(afp->line, afp->n, "#") /* and skip comment lines */
650 && ! esl_memstrpfx(afp->line, afp->n, "# STOCKHOLM"))); /* but stop on Stockholm header */
651
652 if (! esl_memstrpfx(afp->line, afp->n, "# STOCKHOLM 1.")) esl_fatal("small mem parse pass 2 failed (line %d): missing \"# STOCKHOLM\" header", (int) afp->linenumber);
653
654 while ((status = esl_msafile_GetLine(afp, &p, &n)) == eslOK)
655 {
656 while (n && ( *p == ' ' || *p == '\t')) { p++; n--; } /* skip leading whitespace */
657
658 if (!n || *p == '#') continue; /* skip blank lines, comments */
659 else if (esl_memstrpfx(p, n, "//")) break; /* end of alignment: end of record */
660 else
661 { /* sequence line. parse line into temporary strings */
662 if (esl_memtok(&p, &n, " \t", &seqname, &seqnamelen) != eslOK) esl_fatal("small mem parse pass 2 failed (line %d): no seq name", (int) afp->linenumber);
663 if (esl_memtok(&p, &n, " \t", &aseq, &aseqlen) != eslOK) esl_fatal("small mem parse pass 2 failed (line %d): no aseq", (int) afp->linenumber);
664
665 /* make sure we haven't just read a second line of the first sequence in file (we must be in Pfam 1 line/seq file) */
666 if (nseq_read == 0) { if ((status = esl_memstrdup(seqname, seqnamelen, &(first_seqname))) != eslOK) esl_fatal("small mem parse failed: unable to copy seqname"); }
667 else if (esl_memstrcmp(seqname, seqnamelen, first_seqname)) esl_fatal("--small parse pass 2 failed (line %d): two seqs named %s. Alignment appears to be in interleaved Stockholm (not Pfam) format.", (int) afp->linenumber, seqname);
668 nseq_read++;
669
670 /* determine if we have an accession and/or description for this sequence */
671 have_de = have_ac = FALSE;
672 if (ac_seqname && (esl_memstrcmp(seqname, seqnamelen, ac_seqname))) have_ac = TRUE;
673 if (de_seqname && (esl_memstrcmp(seqname, seqnamelen, de_seqname))) have_de = TRUE;
674
675 if (rename) fprintf(ofp, ">%s.%d%s%s%s%s\n", rename, nseq_read, (have_ac ? " " : "") , (have_ac ? ac : ""), (have_de ? " " : "") , (have_de ? de : ""));
676 else fprintf(ofp, ">%.*s%s%s%s%s\n", (int) seqnamelen, seqname, (have_ac ? " " : "") , (have_ac ? ac : ""), (have_de ? " " : "") , (have_de ? de : ""));
677
678 /* load next ac, de */
679 if (have_ac) {
680 status = esl_fgets(&(ac_buf), &(ac_buflen), ac_fp);
681 if (status == eslEOF) ac_seqname = NULL;
682 else if (status == eslOK) {
683 ac_s = ac_buf;
684 if (esl_strtok_adv(&ac_s, " \t\n\r", &ac_seqname, NULL, NULL) != eslOK) esl_fatal("--small accession tmpfile parse failed");
685 if (esl_strtok_adv(&ac_s, "\n\r", &ac, NULL, NULL) != eslOK) esl_fatal("--small accession tmpfile parse failed");
686 }
687 }
688 if (have_de) {
689 status = esl_fgets(&(de_buf), &(de_buflen), de_fp);
690 if(status == eslEOF) de_seqname = NULL;
691 else if (status == eslOK) {
692 de_s = de_buf;
693 if (esl_strtok_adv(&de_s, " \t\n\r", &de_seqname, NULL, NULL) != eslOK) esl_fatal("--small description tmpfile parse failed");
694 if (esl_strtok_adv(&de_s, "\n\r", &de, NULL, NULL) != eslOK) esl_fatal("--small description tmpfile parse failed");
695 }
696 }
697
698 /* now print sequence, after converting symbols as nec */
699 /* remember, aseq itself is part of an ESL_BUFFER and you
700 can't write to it, so symconverts have to be on the
701 copy */
702 for (apos = 0; apos < aseqlen; apos += cpl)
703 {
704 acpl = (aseqlen - apos > cpl ? cpl : aseqlen - apos);
705 strncpy(aseqbuf, aseq + apos, acpl);
706 aseqbuf[acpl] = '\0';
707
708 if (rfrom) symconvert(aseqbuf, rfrom, rto);
709 if (gapsym) symconvert(aseqbuf, "-_.", gapsym);
710 if (force_lower) symconvert(aseqbuf,
711 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
712 "abcdefghijklmnopqrstuvwxyz");
713 if (force_upper) symconvert(aseqbuf,
714 "abcdefghijklmnopqrstuvwxyz",
715 "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
716 if (force_rna) symconvert(aseqbuf, "Tt", "Uu");
717 if (force_dna) symconvert(aseqbuf, "Uu", "Tt");
718 if (iupac_to_n) symconvert(aseqbuf,
719 "RYMKSWHBVDrymkswhbvd",
720 "NNNNNNNNNNnnnnnnnnnn");
721 if (x_is_bad) symconvert(aseqbuf, "Xx", "Nn");
722
723 fprintf(ofp, "%s\n", aseqbuf);
724 }
725 }
726 }
727 /* If we saw a normal // end, we would've successfully read a line,
728 * so when we get here, status (from the line read) should be eslOK.
729 */
730 if (status != eslOK) esl_fatal("--small parse pass 2 failed (line %d): didn't find // at end of alignment", (int) afp->linenumber);
731 if (ac_seqname) esl_fatal("--small parse pass 2 failed, sequence %s with #=GS AC line does not exist in alignment or is in different order.", ac_seqname);
732 if (de_seqname) esl_fatal("--small parse pass 2 failed, sequence %s with #=GS DE line does not exist in alignment or is in different order.", de_seqname);
733
734 if (ac_fp) fclose(ac_fp);
735 if (de_fp) fclose(de_fp);
736 esl_msafile_Close(afp);
737
738 if (first_seqname) free(first_seqname);
739 if (ac_buf) free(ac_buf);
740 if (de_buf) free(de_buf);
741
742 *ret_reached_eof = reached_eof;
743 return;
744 }
745
746 /* regurgitate_pfam_as_pfam()
747 *
748 * Given an open Pfam formatted msafile, read the next alignment and
749 * regurgitate it, after modifying it as necessary (change dna to rna,
750 * wussify SS, etc) in Pfam format.
751 *
752 * Returns <eslOK> on success.
753 * Returns <eslEOF> if there are no more alignments in <afp>.
754 * Returns <eslEFORMAT> if parse fails because of a file format
755 * problem, in which case afp->errmsg is set to contain a formatted
756 * message that indicates the cause of the problem.
757 */
758 static int
759 regurgitate_pfam_as_pfam(ESL_MSAFILE *afp, FILE *ofp, char *gapsym, int force_lower, int force_upper, int force_rna, int force_dna, int iupac_to_n, int x_is_bad, int wussify, int dewuss, int fullwuss, char *rfrom, char *rto)
760 {
761 char *p;
762 esl_pos_t n;
763 char *first_seqname = NULL;
764 char *gx = NULL;
765 char *seqname = NULL;
766 char *tag = NULL;
767 char *text = NULL;
768 esl_pos_t gxlen, namelen, taglen, textlen;
769 int nseq_read = 0;
770 int parse_gc_and_gr;
771 int flushpoint = 10000;
772 int exp_alen = -1;
773 char *buf = NULL;
774 esl_pos_t pos, pos2;
775 int status;
776
777
778 parse_gc_and_gr = (wussify || dewuss || fullwuss) ? TRUE : FALSE; /* should we parse out GR/GC lines and check if they're SS lines? */
779 afp->errmsg[0] = '\0';
780
781 /* Check the magic Stockholm header line.
782 * We have to skip blank lines here, else we perceive
783 * trailing blank lines in a file as a format error when
784 * reading in multi-record mode.
785 */
786 /* Check the magic Stockholm header line, allowing blank lines */
787 do {
788 status = esl_msafile_GetLine(afp, &p, &n);
789 if (status == eslEOF) return eslEOF;
790 else if (status != eslOK) esl_fatal("small mem parse error. problem reading line %d of msafile", (int) afp->linenumber);
791 fprintf(ofp, "%.*s\n", (int) afp->n, afp->line);
792 } while (esl_memspn(afp->line, afp->n, " \t") == afp->n || /* skip blank lines */
793 (esl_memstrpfx(afp->line, afp->n, "#") /* and skip comment lines */
794 && ! esl_memstrpfx(afp->line, afp->n, "# STOCKHOLM"))); /* but stop on Stockholm header */
795
796 if (! esl_memstrpfx(afp->line, afp->n, "# STOCKHOLM 1.")) esl_fatal("small mem parse failed (line %d): missing \"# STOCKHOLM\" header", (int) afp->linenumber);
797
798 /* Read the alignment file one line at a time. */
799 while ((status = esl_msafile_GetLine(afp, &p, &n)) == eslOK)
800 {
801 if ((int) afp->linenumber % flushpoint == 0) fflush(ofp);
802 while (n && ( *p == ' ' || *p == '\t')) { p++; n--; } /* skip leading whitespace */
803
804 if (!n) fprintf(ofp, "\n");
805 else if (esl_memstrpfx(p, n, "//")) { fprintf(ofp, "//\n"); break; } /* normal way out */
806 else if (*p == '#')
807 {
808 if (parse_gc_and_gr && esl_memstrpfx(p, n, "#=GC"))
809 { /* parse line into temporary strings */
810 if (esl_memtok(&p, &n, " \t", &gx, &gxlen) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "small mem parse failed (line %d): bad #=GC line", (int) afp->linenumber);
811 if (esl_memtok(&p, &n, " \t", &tag, &taglen) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "small mem parse failed (line %d): bad #=GC line", (int) afp->linenumber);
812 if (esl_memtok(&p, &n, " \t", &text, &textlen) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "small mem parse failed (line %d): bad #=GC line", (int) afp->linenumber);
813 pos = text - afp->line; /* pos: position of first aligned char on line; total width of annotation tag w/spaces */
814
815 /* verify alignment length */
816 if (exp_alen == -1) exp_alen = textlen;
817 else if (exp_alen != textlen) ESL_XFAIL(eslEFORMAT, afp->errmsg, "small mem parse failed (line %d): bad #=GC line, len %d, expected %d", (int) afp->linenumber, (int) textlen, (int) exp_alen);
818
819 /* we need to make a writable string copy of the annotation, to edit it */
820 ESL_REALLOC(buf, sizeof(char) * (textlen+1));
821 esl_memstrcpy(text, textlen, buf);
822
823 if (esl_memstrcmp(tag, taglen, "SS_cons"))
824 {
825 if (wussify) esl_kh2wuss(buf, buf);
826 else if (dewuss) esl_wuss2kh(buf, buf);
827 else if (fullwuss)
828 {
829 status = esl_wuss_full(buf, buf);
830 if (status == eslESYNTAX) esl_fatal("Bad SS_cons line: not in WUSS format, alifile line: %d", (int) afp->linenumber);
831 else if (status != eslOK) esl_fatal("Conversion of SS_cons line failed, code %d, alifile line: %d", status, (int) afp->linenumber);
832 }
833 }
834 fprintf(ofp, "#=GC %.*s%*s%s\n", (int) taglen, tag, (int) (pos-taglen-5), "", buf);
835 }
836 else if (parse_gc_and_gr && esl_memstrpfx(p, n, "#=GR") == 0)
837 {
838 /* parse line into temporary strings */
839 if (esl_memtok(&p, &n, " \t", &gx, &gxlen) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "--small parse failed (line %d): bad #=GR line", (int) afp->linenumber);
840 if (esl_memtok(&p, &n, " \t", &seqname, &namelen) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "--small parse failed (line %d): bad #=GR line", (int) afp->linenumber);
841 if (esl_memtok(&p, &n, " \t", &tag, &taglen) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "--small parse failed (line %d): bad #=GR line", (int) afp->linenumber);
842 pos = tag - afp->line;
843 if (esl_memtok(&p, &n, " \t", &text, &textlen) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "--small parse failed (line %d): bad #=GR line", (int) afp->linenumber);
844 pos2 = text - afp->line;
845
846 /* we need to make a writable string copy of the annotation, to edit it */
847 ESL_REALLOC(buf, sizeof(char) * (textlen+1));
848 esl_memstrcpy(text, textlen, buf);
849
850 /* verify alignment length */
851 if (exp_alen == -1) exp_alen = textlen;
852 else if (exp_alen != textlen) ESL_XFAIL(eslEFORMAT, afp->errmsg, "small mem parse failed (line %d): bad seq line, len %d, expected %d", (int) afp->linenumber, (int) textlen, (int) exp_alen);
853
854 if (esl_memstrcmp(tag, taglen, "SS") == 0)
855 {
856 if (wussify) esl_kh2wuss(buf, buf);
857 else if (dewuss) esl_wuss2kh(buf, buf);
858 else if (fullwuss) {
859 status = esl_wuss_full(buf, buf);
860 if (status == eslESYNTAX) esl_fatal("Bad SS line: not in WUSS format, alifile line: %d", (int) afp->linenumber);
861 else if (status != eslOK) esl_fatal("Conversion of SS line failed, code %d, alifile line: %d", status, (int) afp->linenumber);
862 }
863 }
864
865 fprintf(ofp, "#=GR %.*s%*s%.*s%*s%s\n", (int) namelen, seqname, (int) (pos-namelen-5), "", (int) taglen, tag, (int) (pos2-pos-taglen), "", buf);
866 }
867 else { /* '#' prefixed line that is not #=GR (or it is #=GR and wussify,dewuss,fullwuss are all FALSE) */
868 fprintf(ofp, "%.*s\n", (int) afp->n, afp->line); /* print the line */
869 }
870 } /* end of 'if (*s == '#')' */
871 else
872 { /* sequence line */
873 if (esl_memtok(&p, &n, " \t", &seqname, &namelen) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "--small parse failed (line %d): bad sequence line", (int) afp->linenumber);
874 if (esl_memtok(&p, &n, " \t", &text, &textlen) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "--small parse failed (line %d): bad sequence line", (int) afp->linenumber);
875 pos = text - afp->line;
876
877 /* verify alignment length */
878 if (exp_alen == -1) exp_alen = textlen;
879 else if(exp_alen != textlen) ESL_XFAIL(eslEFORMAT, afp->errmsg, "small mem parse failed (line %d): bad seq line, len %d, expected %d", (int) afp->linenumber, (int) textlen, (int) exp_alen);
880
881 /* make sure we haven't just read a second line of the first sequence in file (we must be in Pfam 1 line/seq file) */
882 if (nseq_read == 0) { if ((status = esl_memstrdup(seqname, namelen, &(first_seqname))) != eslOK) goto ERROR; }
883 else if (esl_memstrcmp(seqname, namelen, first_seqname)) { ESL_XFAIL(eslEFORMAT, afp->errmsg, "parse failed (line %d): two seqs named %s. Alignment appears to be in Stockholm format. Reformat to Pfam with esl-reformat.", (int) afp->linenumber, seqname); }
884 nseq_read++;
885
886 /* we need to make a writable string copy of the annotation, to edit it */
887 ESL_REALLOC(buf, sizeof(char) * (textlen+1));
888 esl_memstrcpy(text, textlen, buf);
889
890 /* make adjustments as necessary */
891 if (rfrom) symconvert(buf, rfrom, rto);
892 if (gapsym) symconvert(buf, "-_.", gapsym);
893 if (force_lower) symconvert(buf,
894 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
895 "abcdefghijklmnopqrstuvwxyz");
896 if (force_upper) symconvert(buf,
897 "abcdefghijklmnopqrstuvwxyz",
898 "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
899 if (force_rna) symconvert(buf, "Tt", "Uu");
900 if (force_dna) symconvert(buf, "Uu", "Tt");
901 if (iupac_to_n) symconvert(buf,
902 "RYMKSWHBVDrymkswhbvd",
903 "NNNNNNNNNNnnnnnnnnnn");
904 if (x_is_bad) symconvert(buf, "Xx", "Nn");
905 /* print it out */
906 fprintf(ofp, "%.*s%*s%s\n", (int) namelen, seqname, (int) (pos-namelen), "", buf);
907 }
908 }
909
910 /* If we saw a normal // end, we would've successfully read a line,
911 * so when we get here, status (from the line read) should be eslOK.
912 */
913 if (status != eslOK) esl_fatal("--small parse failed (line %d): didn't find // at end of alignment", (int) afp->linenumber);
914 if (first_seqname) free(first_seqname);
915 if (buf) free(buf);
916 return eslOK;
917
918 ERROR:
919 return status;
920 }
921
922 static int
923 parse_replace_string(const char *rstring, char **ret_from, char **ret_to)
924 {
925 int status;
926 int rlen, mid, i;
927 int is_valid = FALSE;
928 char *from = NULL;
929 char *to = NULL;
930
931 /* Note: we could use ESL_REGEXP but then multiple ':'s in rstring could cause problems */
932 rlen = strlen(rstring);
933 /* check validity of rstring: must be "<s1>:<s2>" with len(<s1>)==len(<s2>) */
934 if((rlen % 2) != 0) { /* odd num chars, good */
935 mid = rlen / 2;
936 if(rstring[mid] == ':') { /* middle character is ':', good */
937 ESL_ALLOC(from, sizeof(char) * (mid+1));
938 ESL_ALLOC(to, sizeof(char) * (mid+1));
939 for(i = 0; i < mid; i++) from[i] = rstring[i];
940 for(i = mid+1; i < rlen; i++) to[i-(mid+1)] = rstring[i];
941 from[mid] = '\0';
942 to[mid] = '\0';
943 is_valid = TRUE;
944 }
945 }
946 if(! is_valid) esl_fatal("--replace takes arg of <s1>:<s2> with len(<s1>) == len(<s2>); %s not recognized", rstring);
947 *ret_from = from;
948 *ret_to = to;
949
950 return eslOK;
951
952 ERROR:
953 if(from != NULL) free(from);
954 if(to != NULL) free(to);
955 return status;
956 }
957
958
959
960