Debian Codegraph @

easel/esl_msafile2.c

raw symbols SCIP
1 /* Memory-efficient multiple sequence alignment i/o from Pfam format
2 *
3 * Legacy interface, now that ESL_MSAFILE is rewritten. Just enough
4 * of the old interface is retained to support --small option in
5 * various tools, for reading Pfam format in memory-efficient ways.
6 *
7 * Table of contents:
8 * 1. The ESL_MSAFILE2 object
9 * 2. Memory efficient read/write in Pfam format
10 * 3. Legacy Stockholm parsing tools
11 * 4. Unit tests
12 * 5. Test driver
13 *
14 * to-do:
15 * :: add memory-efficient interface in ESL_MSAFILE
16 * :: add memory-efficient ESL_MSA w/ API
17 * :: add space-efficient MSA file format
18 */
19 #include <esl_config.h>
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
25
26 #include "easel.h"
27 #include "esl_alphabet.h" /* digital alphabet */
28 #include "esl_arr2.h"
29 #include "esl_arr3.h"
30 #include "esl_keyhash.h" /* string hashes, for mapping unique seq names */
31 #include "esl_msa.h" /* ESL_MSA structure */
32 #include "esl_msafile.h" /* preferred msafile interface, inc. fmt codes shared w/ ESL_MSAFILE2 */
33 #include "esl_ssi.h" /* indexing large flatfiles on disk */
34 #include "esl_vectorops.h"
35 #include "esl_wuss.h"
36
37 #include "esl_msafile2.h"
38
39
40 static int msafile2_getline(ESL_MSAFILE2 *afp);
41 static int is_blankline(char *s);
42 static int parse_gf(ESL_MSA *msa, char *buf);
43 static int parse_gc(ESL_MSA *msa, char *buf);
44 static int parse_comment(ESL_MSA *msa, char *buf);
45
46 /*****************************************************************
47 *# 1. The ESL_MSAFILE2 object
48 *****************************************************************/
49
50 static int msafile2_open(const char *filename, const char *env, ESL_MSAFILE2 **ret_afp);
51
52 /* Function: esl_msafile2_Open()
53 * Synopsis: Open an MSA file for small-memory input.
54 *
55 * Purpose: Open an alignment database file <filename>, which must be
56 * in Pfam format (<eslMSAFILE_PFAM>), and prepare for
57 * reading information through the legacy small-memory
58 * interface. Return the opened file pointer in <ret_msafp>.
59 *
60 * There are one or two special cases for <filename>. If
61 * <filename> is "-", then the alignment is read from
62 * <stdin>. If <filename> ends in ".gz", then the file is
63 * assumed to be compressed by gzip, and it is opened as a
64 * pipe from <gzip -dc>. (Auto-decompression of gzip'ed files
65 * is only available on POSIX-compliant systems w/ popen(), when
66 * <HAVE_POPEN> is defined at compile-time.)
67 *
68 * If <env> is non-NULL, then we look for <filename> in
69 * one or more directories in a colon-delimited list
70 * that is the value of the environment variable <env>.
71 * For example, if we had
72 * <setenv HMMERDB /nfs/db/Pfam:/nfs/db/Rfam>
73 * in the environment, a profile HMM application
74 * might pass "HMMERDB" as <env>.
75 *
76 * Returns: <eslOK> on success, and <ret_msafp> is set to point at
77 * an open <ESL_MSAFILE2>. Caller frees this file pointer with
78 * <esl_msafile2_Close()>.
79 *
80 * Returns <eslENOTFOUND> if <filename> cannot be opened.
81 *
82 * Throws: <eslEMEM> on allocation failure.
83 *
84 * Note: Implemented as a wrapper around msafile2_open(), because
85 * esl_msafile2_OpenDigital() shares almost all the same code.
86 */
87 int
88 esl_msafile2_Open(const char *filename, const char *env, ESL_MSAFILE2 **ret_afp)
89 {
90 return msafile2_open(filename, env, ret_afp);
91 }
92
93 /* Function: esl_msafile2_Close()
94 * Synopsis: Closes an open MSA file.
95 *
96 * Purpose: Close an open <ESL_MSAFILE2>.
97 */
98 void
99 esl_msafile2_Close(ESL_MSAFILE2 *afp)
100 {
101 if (afp == NULL) return;
102
103 #ifdef HAVE_POPEN /* gzip functionality */
104 if (afp->do_gzip && afp->f != NULL) pclose(afp->f);
105 #endif
106 if (!afp->do_gzip && ! afp->do_stdin && afp->f != NULL) fclose(afp->f);
107 if (afp->fname) free(afp->fname);
108 if (afp->buf) free(afp->buf);
109 if (afp->ssi) esl_ssi_Close(afp->ssi);
110 if (afp->msa_cache) esl_msa_Destroy(afp->msa_cache);
111 free(afp);
112 }
113
114
115 /* Function: esl_msafile2_OpenDigital()
116 * Synopsis: Open an msa file for digital input.
117 *
118 * Purpose: Same as <esl_msafile2_Open()>, except the alignment file
119 * will be read into a digitized internal representation,
120 * using internal alphabet <abc>, rather than the default
121 * internal ASCII text representation.
122 *
123 * The file must be in Pfam format (<eslMSAFILE_PFAM>).
124 *
125 * Args: abc - pointer to internal alphabet
126 * filename - name of alignment data file to open;
127 * if "*.gz", attempt to read through <gzip -dc> using <popen()>;
128 * or "-" for stdin
129 * env - NULL, or the name of an environment variable from which
130 * to retrieve a colon-delimited directory list to search
131 * for <filename> in. (e.g. "HMMERDB")
132 * ret_msafp - RETURN: open <ESL_MSAFILE2>.
133 *
134 * Returns: <eslOK> on success, and <ret_msafp> is set to point at
135 * an open <ESL_MSAFILE2>. Caller frees this file pointer with
136 * <esl_msafile2_Close()>.
137 *
138 * <eslENOTFOUND> if <filename> cannot be opened;
139 * <eslEFORMAT> if <filename> doesn't seem to be in Pfam format.
140 *
141 * Throws: <eslEMEM> on allocation failure.
142 * <eslEINVAL> if format autodetection is attempted on
143 * stdin or a gzip -dc pipe.
144 */
145 int
146 esl_msafile2_OpenDigital(const ESL_ALPHABET *abc, const char *filename,
147 const char *env, ESL_MSAFILE2 **ret_msafp)
148 {
149 ESL_MSAFILE2 *msafp;
150 int status;
151
152 if ((status = msafile2_open(filename, env, &msafp)) != eslOK) return status;
153
154 msafp->abc = abc;
155 msafp->do_digital = TRUE;
156
157 *ret_msafp = msafp;
158 return eslOK;
159 }
160
161 /* msafile2_open():
162 * this is the routine that actually opens an ESL_MSAFILE2;
163 * esl_msafile2_Open() and esl_msafile2_OpenDigital() are wrappers around it.
164 */
165 static int
166 msafile2_open(const char *filename, const char *env, ESL_MSAFILE2 **ret_afp)
167 {
168 ESL_MSAFILE2 *afp = NULL;
169 char *ssifile = NULL;
170 char *envfile = NULL;
171 char *cmd = NULL;
172 int n = strlen(filename);
173 int nc;
174 int status;
175
176 ESL_ALLOC(afp, sizeof(ESL_MSAFILE2));
177 afp->f = NULL;
178 afp->fname = NULL;
179 afp->linenumber = 0;
180 afp->errbuf[0] = '\0';
181 afp->buf = NULL;
182 afp->buflen = 0;
183 afp->do_gzip = FALSE;
184 afp->do_stdin = FALSE;
185 afp->format = eslMSAFILE_PFAM; /* legacy interface is stripped down to support ONLY --small, Pfam */
186 afp->do_digital = FALSE;
187 afp->abc = NULL;
188 afp->ssi = NULL;
189 afp->msa_cache = NULL;
190
191 if (strcmp(filename, "-") == 0)
192 {
193 afp->f = stdin;
194 afp->do_stdin = TRUE;
195 if ((status = esl_strdup("[STDIN]", -1, &(afp->fname))) != eslOK) goto ERROR;
196 }
197 #ifdef HAVE_POPEN
198 /* popen(), pclose() aren't portable to non-POSIX systems;
199 * disable this section in strict ANSI C mode.
200 */
201 /* tricky: if n= length of a string s, then
202 * s+n-i repositions pointer s at the last i chars
203 * of the string.
204 */
205 else if (n > 3 && strcmp(filename+n-3, ".gz") == 0)
206 {
207 /* Note that popen() will return "successfully"
208 * if file doesn't exist, because gzip works fine
209 * and prints an error! So we have to check for
210 * existence of file ourself.
211 */
212 if (! esl_FileExists(filename)) { status = eslENOTFOUND; goto ERROR; }
213 nc = strlen("gzip -dc ") + n + 1;
214 ESL_ALLOC(cmd, nc);
215 snprintf(cmd, nc, "gzip -dc %s", filename);
216 if ((afp->f = popen(cmd, "r")) == NULL) { status = eslENOTFOUND; goto ERROR; }
217 if ((status = esl_strdup(filename, n, &(afp->fname))) != eslOK) goto ERROR;
218 afp->do_gzip = TRUE;
219 }
220 #endif /*HAVE_POPEN*/
221 else /* Normal file open or env file open: set ssifile */
222 {
223 /* When we open a file, it may be either in the current
224 * directory, or in the directory indicated by the env
225 * argument - and we construct an SSI filename accordingly.
226 */
227 if ((afp->f = fopen(filename, "r")) != NULL)
228 {
229 if ((status = esl_strdup(filename, n, &ssifile)) != eslOK) goto ERROR;
230 if ((status = esl_strcat(&ssifile, n, ".ssi", 4)) != eslOK) goto ERROR;
231 if ((status = esl_strdup(filename, n, &(afp->fname))) != eslOK) goto ERROR;
232 }
233 else if (esl_FileEnvOpen(filename, env, &(afp->f), &envfile) == eslOK)
234 {
235 if ((status = esl_strdup(envfile, n, &ssifile)) != eslOK) goto ERROR;
236 if ((status = esl_strcat(&ssifile, n, ".ssi", 4)) != eslOK) goto ERROR;
237 if ((status = esl_strdup(envfile, n, &(afp->fname))) != eslOK) goto ERROR;
238 }
239 else
240 { status = eslENOTFOUND; goto ERROR;}
241 }
242
243 /* Open the SSI index file. If it doesn't exist, or
244 * it's corrupt, or some error happens, afp->ssi stays NULL.
245 * We should warn, probably, or provide some way for caller to
246 * to know that we've opened the index successfully or not.
247 */
248 esl_ssi_Open(ssifile, &(afp->ssi));
249
250 if (envfile != NULL) free(envfile);
251 if (ssifile != NULL) free(ssifile);
252 if (cmd != NULL) free(cmd);
253 *ret_afp = afp;
254 return eslOK;
255
256 ERROR:
257 if (envfile != NULL) free(envfile);
258 if (ssifile != NULL) free(ssifile);
259 if (cmd != NULL) free(cmd);
260 if (afp != NULL) esl_msafile2_Close(afp);
261 *ret_afp = NULL;
262 return status;
263 }
264 /*--------------- end, ESL_MSAFILE2 object ----------------------*/
265
266 /*------------------ end, digital mode -------------------------*/
267
268
269 /******************************************************************************
270 * 2. Memory efficient routines for PFAM format
271 *****************************************************************************/
272
273 static int get_pp_idx(ESL_ALPHABET *abc, char ppchar);
274 static int gapize_string(char *src_str, int64_t src_len, int64_t dst_len, int *ngapA, char gapchar, char **ret_dst_str);
275 static void shrink_string(char *str, const int *useme, int len);
276 static int determine_spacelen(char *s);
277
278 /* Function: esl_msafile2_ReadInfoPfam()
279 * Synopsis: Read Pfam formatted MSA information but not sequence data.
280 *
281 * Purpose: Read the next alignment from an open Stockholm Pfam
282 * (non-interleaved, one line per seq) format alignment file
283 * <afp> and store all non-sequence information (comments,
284 * GF annotation and GC annotation) in a new msa object.
285 *
286 * This function is not as rigorous about validating the
287 * input msa as the other read functions that store the full
288 * alignment. Here, we only verify that there is only one
289 * line for the first sequence read. Verifying that all
290 * sequences are only one line would require storing and
291 * looking up all sequence names.
292 *
293 * Many optional return values (<opt_*>) allow this function
294 * to serve the diverse needs of the miniapps that can run
295 * in a memory-efficient mode (esl-alimerge, esl-alimask,
296 * esl-alistat, esl-ssdraw). For any that are unwanted, pass
297 * <NULL>.
298 *
299 * Args: afp - open alignment file pointer
300 * listfp - if non-NULL, dump each sequence name we read
301 * to listfp, separated by newlines
302 * abc - alphabet to use, only nec and used if one
303 * of the opt_*_ct arrays is non-NULL
304 * known_alen - known length of the alignment, -1 if unknown
305 * must not be -1, if known_rf != NULL or
306 * known_ss_cons != NULL.
307 * known_rf - known RF annot. (msa->rf) for this alignment,
308 * might be known from prev call of this func,
309 * for example. NULL if unknown.
310 * known_ss_cons - the known SS_cons annotation (msa->ss_cons)
311 * for this alignment, NULL if unknown.
312 * ret_msa - RETURN: msa with comments, GC, GF
313 * annotation but no sequence info (nor GS, GR),
314 * pass NULL if not wanted.
315 * opt_nseq - optRETURN: number of sequences in msa
316 * opt_alen - optRETURN: length of first aligned sequence
317 * opt_ngs - optRETURN: number of GS lines in alignment
318 * opt_maxname - optRETURN: maximum seqname length
319 * opt_maxgf - optRETURN: maximum GF tag length
320 * opt_maxgc - optRETURN: maximum GC tag length
321 * opt_maxgr - optRETURN: maximum GR tag length
322 * opt_abc_ct - optRETURN: [0..apos..alen-1][0..abc->K]
323 * per position count of each symbol in abc over all seqs
324 * opt_pp_ct - optRETURN: [0..apos..alen-1][0..11],
325 * per position count of PPs over all seqs,
326 * [11] is gaps, [10] is '*', [0-9] are '0'-'9'
327 * opt_bp_ct - optRETURN: [0..apos..alen-1][0..abc->K-1][0..abc->K-1]
328 * per position count of each possible basepair
329 * in alignment, for pair apos:apos2, where
330 * apos < apos2 and apos:apos2 form a basepair
331 * in <known_ss_cons>. If non-NULL, <known_ss_cons>
332 * must be non-NULL.
333 * opt_spos_ct - optRETURN: [0..apos..alen-1] per position count
334 * of first nongap residue in each sequence,
335 * ex: opt_spos_ct[100] = x means x seqs have their
336 * first nongap residue at position 100
337 * opt_epos_ct - optRETURN: [0..apos..alen-1] same as opt_spos_ct,
338 * except for final position instead of first
339 *
340 * Returns: <eslOK> on success. Returns <eslEOF> if there are no more
341 * alignments in <afp>, and <ret_msa> is set to NULL and
342 * <opt_*> are set to 0.
343 * <eslEFORMAT> if parse fails because of a file format
344 * problem, in which case <afp->errbuf> is set to contain a
345 * formatted message that indicates the cause of the
346 * problem, <ret_msa> is set to <NULL> and <opt_*> are set
347 * to 0.
348 *
349 * Throws: <eslEMEM> on allocation error.
350 *
351 * Xref: ~nawrockie/notebook/9_1206_esl_msa_mem_efficient/
352 */
353 int
354 esl_msafile2_ReadInfoPfam(ESL_MSAFILE2 *afp, FILE *listfp, ESL_ALPHABET *abc, int64_t known_alen, char *known_rf, char *known_ss_cons, ESL_MSA **ret_msa,
355 int *opt_nseq, int64_t *opt_alen, int *opt_ngs, int *opt_maxname, int *opt_maxgf, int *opt_maxgc, int *opt_maxgr,
356 double ***opt_abc_ct, double ***opt_pp_ct, double ****opt_bp_ct, int **opt_spos_ct, int **opt_epos_ct)
357 {
358 char *s; /* pointer to current character in afp */
359 int status; /* easel status code */
360 int status2; /* another easel status code */
361 ESL_MSA *msa = NULL; /* the msa we're creating */
362 int nseq = 0; /* number of sequences read */
363 int64_t alen = -1; /* length of the alignment */
364 int ngs = 0; /* number of GS lines read */
365 int maxname = 0; /* max length seq name */
366 int maxgf = 0; /* max length GF tag */
367 int maxgc = 0; /* max length GC tag */
368 int maxgr = 0; /* max length GR tag */
369 char *seqname; /* ptr to a sequence name */
370 int namelen; /* length of a sequence name */
371 char *first_seqname = NULL; /* name of first sequence read */
372 char *gf, *gc, *gr; /* for storing '#=GF', '#=GC', '#=GR', temporarily */
373 char *tag; /* a GC or GR tag */
374 int taglen; /* length of a tag */
375 char *text; /* text string */
376 int textlen; /* length of text string */
377 int i, x; /* counters */
378 int j; /* position for a right half of a bp */
379 int apos; /* counter over alignment positions */
380 double **abc_ct = NULL; /* [0..alen-1][0..abc->K] per position count of each residue in abc and gaps over all seqs */
381 double ***bp_ct = NULL; /* [0..alen-1][0..abc->Kp][0..abc->Kp], count of each possible base pair at each position, over all sequences, missing and nonresidues are *not counted*
382 base pairs are indexed by 'i' for a base pair between positions i and j, where i < j. */
383 int nppvals = 12; /* '0'-'9' = 0-9, '*' = 10, gap = '11' */
384 double **pp_ct = NULL; /* [0..alen-1][0..nppvals-1] per position count of each possible PP char over all seqs */
385 int ppidx; /* index for 2nd dim of pp_ct array */
386 int *spos_ct = NULL; /* [0..alen-1] number of seqs that start (have first nongap residue) at each position */
387 int *epos_ct = NULL; /* [0..alen-1] number of seqs that end (have final nongap residue) at each position */
388 ESL_DSQ *tmp_dsq = NULL; /* temporary digitized sequence, only used if opt_abc_ct != NULL */
389 int *a2rf_map = NULL; /* [0..apos..known_alen-1] = rfpos, nongap RF position apos maps to,
390 * -1 if apos is not a nongap RF position */
391 int *ct = NULL; /* 0..known_alen-1 base pair partners array for known_ss_cons */
392 char *ss_nopseudo = NULL; /* no-pseudoknot version of known_ss_cons */
393
394 if(afp->format != eslMSAFILE_PFAM) ESL_EXCEPTION(eslEINCONCEIVABLE, "only non-interleaved (1 line /seq, Pfam) Stockholm formatted files can be read in small memory mode");
395 if(opt_abc_ct != NULL && abc == NULL) ESL_FAIL(eslEINVAL, afp->errbuf, "contract violation, abc == NULL, opt_abc_ct != NULL");
396 if(opt_pp_ct != NULL && abc == NULL) ESL_FAIL(eslEINVAL, afp->errbuf, "contract violation, abc == NULL, opt_pp_ct != NULL");
397 if(opt_bp_ct != NULL && abc == NULL) ESL_FAIL(eslEINVAL, afp->errbuf, "contract violation, abc == NULL, opt_bp_ct != NULL");
398 if(opt_spos_ct != NULL && abc == NULL) ESL_FAIL(eslEINVAL, afp->errbuf, "contract violation, abc == NULL, opt_spos_ct != NULL");
399 if(opt_epos_ct != NULL && abc == NULL) ESL_FAIL(eslEINVAL, afp->errbuf, "contract violation, abc == NULL, opt_epos_ct != NULL");
400 if(opt_spos_ct != NULL && known_alen == -1) ESL_FAIL(eslEINVAL, afp->errbuf, "contract violation, opt_spos_ct != NULL, known_alen == -1");
401 if(opt_epos_ct != NULL && known_alen == -1) ESL_FAIL(eslEINVAL, afp->errbuf, "contract violation, opt_epos_ct != NULL, known_alen == -1");
402 if(opt_bp_ct != NULL && known_ss_cons == NULL) ESL_FAIL(eslEINVAL, afp->errbuf, "contract violation, known_ss_cons == NULL, opt_bp_ct != NULL");
403 if(known_rf != NULL && known_alen == -1) ESL_FAIL(eslEINVAL, afp->errbuf, "contract violation, known_rf != NULL, known_alen == -1");
404 if(known_ss_cons != NULL && known_alen == -1) ESL_FAIL(eslEINVAL, afp->errbuf, "contract violation, known_ss_cons != NULL, known_alen == -1");
405
406 if (feof(afp->f)) { status = eslEOF; goto ERROR; }
407 afp->errbuf[0] = '\0';
408
409 /* Preliminaries */
410 /* allocate and initialize spos_ct and epos_ct, if we'll return them */
411 if(opt_spos_ct != NULL || opt_epos_ct != NULL) {
412 ESL_ALLOC(spos_ct, sizeof(int) * known_alen);
413 ESL_ALLOC(epos_ct, sizeof(int) * known_alen);
414 esl_vec_ISet(spos_ct, known_alen, 0);
415 esl_vec_ISet(epos_ct, known_alen, 0);
416 }
417
418 /* if bp_ct != NULL, determine the ct array from the known_ss_cons, and allocate the bp_ct */
419 if(opt_bp_ct != NULL) { /* contract enforces that if this is true, known_ss_cons != NULL and known_alen != -1 */
420 /* get ct array which defines the consensus base pairs */
421 ESL_ALLOC(ct, sizeof(int) * (known_alen+1));
422 ESL_ALLOC(ss_nopseudo, sizeof(char) * (known_alen+1));
423 esl_wuss_nopseudo(known_ss_cons, ss_nopseudo);
424 if ((status = esl_wuss2ct(ss_nopseudo, known_alen, ct)) != eslOK) ESL_FAIL(status, afp->errbuf, "consensus structure string is inconsistent.");
425 ESL_ALLOC(bp_ct, sizeof(double **) * known_alen);
426 for(apos = 0; apos < known_alen; apos++) {
427 /* careful ct is indexed 1..alen, not 0..alen-1 */
428 if(ct[(apos+1)] > (apos+1)) { /* apos+1 is an 'i' in an i:j pair, where i < j */
429 ESL_ALLOC(bp_ct[apos], sizeof(double *) * (abc->Kp));
430 for(x = 0; x < abc->Kp; x++) {
431 ESL_ALLOC(bp_ct[apos][x], sizeof(double) * (abc->Kp));
432 esl_vec_DSet(bp_ct[apos][x], abc->Kp, 0.);
433 }
434 }
435 else { /* apos+1 is not an 'i' in an i:j pair, where i < j, set to NULL */
436 bp_ct[apos] = NULL;
437 }
438 }
439 }
440 /* end of preliminaries */
441
442 /* Initialize allocation of the MSA:
443 * We won't store any sequence information, so initial blocksize is
444 * 0 seqs of 0 length.
445 */
446 if (afp->do_digital == TRUE && (msa = esl_msa_CreateDigital(afp->abc, 16, -1)) == NULL)
447 { status = eslEMEM; goto ERROR; }
448 if (afp->do_digital == FALSE && (msa = esl_msa_Create(16, -1)) == NULL)
449 { status = eslEMEM; goto ERROR; }
450 if (msa == NULL)
451 { status = eslEMEM; goto ERROR; }
452
453 /* Check the magic Stockholm header line.
454 * We have to skip blank lines here, else we perceive
455 * trailing blank lines in a file as a format error when
456 * reading in multi-record mode.
457 */
458 do {
459 if ((status = msafile2_getline(afp)) != eslOK) goto ERROR; /* includes EOF */
460 } while (is_blankline(afp->buf));
461
462 if (strncmp(afp->buf, "# STOCKHOLM 1.", 14) != 0)
463 ESL_XFAIL(eslEFORMAT, afp->errbuf, "parse failed (line %d): missing \"# STOCKHOLM\" header", afp->linenumber);
464
465 /* Read the alignment file one line at a time.
466 */
467 while ((status2 = msafile2_getline(afp)) == eslOK)
468 {
469 s = afp->buf;
470 while (*s == ' ' || *s == '\t') s++; /* skip leading whitespace */
471
472 if (*s == '#') {
473 if(strncmp(s, "#=GF", 4) == 0)
474 {
475 if (ret_msa != NULL) {
476 if ((status = parse_gf(msa, s)) != eslOK)
477 ESL_XFAIL(status, afp->errbuf, "parse failed (line %d): bad #=GF line", afp->linenumber);
478 }
479 else if (opt_maxgf != NULL) { /* we need to parse out GF tag len to see if it is > maxgf */
480 s = afp->buf;
481 if (esl_strtok (&s, " \t\n\r", &gf) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GF line", afp->linenumber);
482 if (esl_strtok_adv(&s, " \t\n\r", &tag, &taglen, NULL) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GF line", afp->linenumber);
483 maxgf = ESL_MAX(maxgf, taglen);
484 }
485 }
486
487 else if (strncmp(s, "#=GC", 4) == 0)
488 {
489 if (ret_msa != NULL) {
490 if ((status = parse_gc(msa, s)) != eslOK)
491 ESL_XFAIL(status, afp->errbuf, "parse failed (line %d): bad #=GC line", afp->linenumber);
492 }
493 else if (opt_maxgc != NULL) { /* we need to parse out GC tag len to see if it is > maxgc */
494 s = afp->buf;
495 if (esl_strtok (&s, " \t\n\r", &gc) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GC line", afp->linenumber);
496 if (esl_strtok_adv(&s, " \t\n\r", &tag, &taglen, NULL) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GC line", afp->linenumber);
497 maxgc = ESL_MAX(maxgc, taglen);
498 }
499 }
500 else if (strncmp(s, "#=GS", 4) == 0)
501 {
502 ngs++;
503 }
504 else if (strncmp(s, "#=GR", 4) == 0)
505 {
506 if(opt_maxgr != NULL || opt_pp_ct != NULL) {
507 s = afp->buf;
508 if (esl_strtok (&s, " \t\n\r", &gr) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GR line", afp->linenumber);
509 if (esl_strtok_adv(&s, " \t\n\r", &seqname, &namelen, NULL) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GR line", afp->linenumber);
510 if (esl_strtok_adv(&s, " \t\n\r", &tag, &taglen, NULL) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GR line", afp->linenumber);
511 maxgr = ESL_MAX(maxgr, taglen);
512 if(opt_pp_ct != NULL) {
513 if (strncmp(tag, "PP", 2) == 0) {
514 if (esl_strtok_adv(&s, " \t\n\r", &text, &textlen, NULL) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GR PP line", afp->linenumber);
515 /* verify, or set alignment length */
516 if(alen == -1) { /* first aligned text line, need to allocate pp_ct, and possibly abc_ct, spos_ct, epos_ct */
517 alen = textlen;
518 if(known_alen != -1 && known_alen != textlen) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): known alen (%" PRId64 " passed in) != actual alen (%d)", afp->linenumber, known_alen, textlen);
519 ESL_ALLOC(pp_ct, sizeof(double *) * alen);
520 for(apos = 0; apos < alen; apos++) {
521 ESL_ALLOC(pp_ct[apos], sizeof(double) * nppvals);
522 esl_vec_DSet(pp_ct[apos], nppvals, 0.);
523 }
524 if(opt_abc_ct != NULL || opt_bp_ct != NULL) {
525 ESL_ALLOC(tmp_dsq, (alen+2) * sizeof(ESL_DSQ));
526 }
527 if(opt_abc_ct != NULL) {
528 ESL_ALLOC(abc_ct, sizeof(double *) * alen);
529 for(apos = 0; apos < alen; apos++) {
530 ESL_ALLOC(abc_ct[apos], sizeof(double) * (abc->K+1));
531 esl_vec_DSet(abc_ct[apos], (abc->K+1), 0.);
532 }
533 }
534 }
535 else if(alen != textlen) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GR PP line, len %d, expected %" PRId64, afp->linenumber, textlen, alen);
536 for(apos = 0; apos < alen; apos++) { /* update appropriate PP count */
537 if((ppidx = get_pp_idx(abc, text[apos])) == -1) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GR PP char: %c", afp->linenumber, text[apos]);
538 pp_ct[apos][ppidx] += 1.;
539 }
540 }
541 }
542 }
543 }
544 else if (ret_msa != NULL && ((status = parse_comment(msa, s)) != eslOK)) {
545 ESL_XFAIL(status, afp->errbuf, "parse failed (line %d): bad comment line", afp->linenumber);
546 }
547 } /* end of 'if (*s == '#')' */
548 else if (strncmp(s, "//", 2) == 0) break; /* normal way out */
549 else if (*s == '\n' || *s == '\r') continue;
550 else { /* sequence line */
551 if(listfp != NULL || opt_maxname != NULL || opt_alen != NULL || opt_abc_ct != NULL || opt_spos_ct != NULL || opt_epos_ct != NULL) { /* we need to parse out the seqname */
552 s = afp->buf;
553 if (esl_strtok_adv(&s, " \t\n\r", &seqname, &namelen, NULL) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad sequence line", afp->linenumber);
554 if (listfp != NULL) fprintf(listfp, "%s\n", seqname);
555 maxname = ESL_MAX(maxname, namelen);
556 if (opt_alen != NULL || opt_abc_ct != NULL || opt_spos_ct != NULL || opt_epos_ct != NULL) { /* we need to parse out the seq */
557 if (esl_strtok_adv(&s, " \t\n\r", &text, &textlen, NULL) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad sequence line", afp->linenumber);
558 /* if first aligned seq read, store it's name, else see if it is an additional line of first aseq */
559 if(nseq == 0) {
560 if ((status = esl_strdup(seqname, -1, &(first_seqname))) != eslOK) goto ERROR;
561 }
562 else if(strcmp(first_seqname, seqname) == 0) { ESL_XFAIL(eslEFORMAT, afp->errbuf, "parse failed (line %d): two seqs with same name. Alignment may be in interleaved Stockholm. Reformat to Pfam with esl-reformat.", afp->linenumber); }
563 if(alen == -1) { /* first aligned text line, need to allocate pp_ct, and possibly abc_ct, spos_ct, epos_ct */
564 alen = textlen;
565 if(known_alen != -1 && known_alen != textlen) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): known alen (%" PRId64 " passed in) != actual alen (%d)", afp->linenumber, known_alen, textlen);
566 if(opt_abc_ct != NULL || opt_bp_ct != NULL) {
567 ESL_ALLOC(tmp_dsq, (alen+2) * sizeof(ESL_DSQ));
568 }
569 if(opt_abc_ct != NULL) {
570 ESL_ALLOC(abc_ct, sizeof(double *) * alen);
571 for(apos = 0; apos < alen; apos++) {
572 ESL_ALLOC(abc_ct[apos], sizeof(double) * (abc->K+1));
573 esl_vec_DSet(abc_ct[apos], (abc->K+1), 0.);
574 }
575 }
576 if(opt_pp_ct != NULL) {
577 ESL_ALLOC(pp_ct, sizeof(double *) * alen);
578 for(apos = 0; apos < alen; apos++) {
579 ESL_ALLOC(pp_ct[apos], sizeof(double) * nppvals);
580 esl_vec_DSet(pp_ct[apos], nppvals, 0.);
581 }
582 }
583 }
584 else if(alen != textlen) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad aligned seq line, len %d, expected %" PRId64, afp->linenumber, textlen, alen);
585 if(opt_abc_ct != NULL || opt_bp_ct != NULL) {
586 /* update appropriate abc and/or bp count. first, digitize the text */
587 if((status = esl_abc_Digitize(abc, text, tmp_dsq)) != eslOK) ESL_XFAIL(status, afp->errbuf, "small mem parse failed (line %d): problem digitizing sequence", afp->linenumber);
588 }
589 if(opt_abc_ct != NULL) {
590 for(apos = 0; apos < alen; apos++) { /* update appropriate abc count, careful, tmp_dsq ranges from 1..alen (not 0..alen-1) */
591 if((status = esl_abc_DCount(abc, abc_ct[apos], tmp_dsq[apos+1], 1.0)) != eslOK) ESL_XFAIL(status, afp->errbuf, "small mem parse failed (line %d): problem counting residue %d", afp->linenumber, apos+1);
592 }
593 }
594 if(opt_bp_ct != NULL) {
595 for(apos = 0; apos < alen; apos++) { /* update appropriate abc count, careful, tmp_dsq ranges from 1..alen (not 0..alen-1) */
596 if(bp_ct[apos] != NULL) { /* our flag for whether position (apos+1) is an 'i' in an i:j pair where i < j */
597 j = ct[apos+1] - 1; /* ct is indexed 1..alen */
598 bp_ct[apos][tmp_dsq[(apos+1)]][tmp_dsq[(j+1)]]++;
599 }
600 }
601 }
602 if(opt_spos_ct != NULL) {
603 for(apos = 0; apos < alen; apos++) { /* find first non-gap position */
604 if(! esl_abc_XIsGap(abc, tmp_dsq[apos+1])) {
605 spos_ct[apos]++;
606 break;
607 }
608 }
609 }
610 if(opt_epos_ct != NULL) { /* find final non-gap position */
611 for(apos = alen-1; apos >= 0; apos--) {
612 if(! esl_abc_XIsGap(abc, tmp_dsq[apos+1])) {
613 epos_ct[apos]++;
614 break;
615 }
616 }
617 }
618 }
619 }
620 nseq++;
621 }
622 }
623
624 /* If we saw a normal // end, we would've successfully read a line,
625 * so when we get here, status (from the line read) should be eslOK.
626 */
627 if (status2 != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "parse failed (line %d): didn't find // at end of alignment", afp->linenumber);
628
629 /* if we're returning maxgc and an msa, determine maxgc, which we didn't do above b/c we parsed GC lines with parse_gc()
630 * If msa != NULL, we already know maxgc */
631 if(ret_msa != NULL && opt_maxgc != NULL) {
632 for(i = 0; i < msa->ngc; i++) maxgc = ESL_MAX(maxgc, strlen(msa->gc_tag[i]));
633 if (msa->rf != NULL) maxgc = ESL_MAX(maxgc, 2); /* 2 == strlen("RF") */
634 if (msa->mm != NULL) maxgc = ESL_MAX(maxgc, 2); /* 2 == strlen("MM") */
635 if (msa->ss_cons != NULL) maxgc = ESL_MAX(maxgc, 7); /* 7 == strlen("SS_cons") */
636 if (msa->sa_cons != NULL) maxgc = ESL_MAX(maxgc, 7); /* 7 == strlen("SA_cons") */
637 if (msa->pp_cons != NULL) maxgc = ESL_MAX(maxgc, 7); /* 7 == strlen("PP_cons") */
638 }
639 /* same as for maxgc, but now for maxgf */
640 if(ret_msa != NULL && opt_maxgf != NULL) {
641 for(i = 0; i < msa->ngf; i++) maxgf = ESL_MAX(maxgf, strlen(msa->gf_tag[i]));
642 if (msa->name != NULL) maxgf = ESL_MAX(maxgf, 2); /* 2 == strlen("ID") */
643 if (msa->desc != NULL) maxgf = ESL_MAX(maxgf, 2); /* 2 == strlen("DE") */
644 if (msa->acc != NULL) maxgf = ESL_MAX(maxgf, 2); /* 2 == strlen("AC") */
645 if (msa->au != NULL) maxgf = ESL_MAX(maxgf, 2); /* 2 == strlen("AU") */
646 }
647
648 if (first_seqname) free(first_seqname);
649 if (tmp_dsq) free(tmp_dsq);
650 if (ct) free(ct);
651 if (ss_nopseudo) free(ss_nopseudo);
652 if (a2rf_map) free(a2rf_map);
653
654 if (ret_msa) *ret_msa = msa; else esl_msa_Destroy(msa);
655 if (opt_nseq) *opt_nseq = nseq;
656 if (opt_alen) *opt_alen = alen;
657 if (opt_ngs) *opt_ngs = ngs;
658 if (opt_maxname) *opt_maxname = maxname;
659 if (opt_maxgf) *opt_maxgf = maxgf;
660 if (opt_maxgc) *opt_maxgc = maxgc;
661 if (opt_maxgr) *opt_maxgr = maxgr;
662 if (opt_abc_ct) *opt_abc_ct = abc_ct; else esl_arr2_Destroy((void **) abc_ct, alen);
663 if (opt_pp_ct) *opt_pp_ct = pp_ct; else esl_arr2_Destroy((void **) pp_ct, alen);
664 if (opt_bp_ct) *opt_bp_ct = bp_ct; else esl_arr3_Destroy((void ***) bp_ct, known_alen, abc ? abc->Kp : 0);
665 if (opt_spos_ct) *opt_spos_ct = spos_ct; else esl_free(spos_ct);
666 if (opt_epos_ct) *opt_epos_ct = epos_ct; else esl_free(epos_ct);
667 return eslOK;
668
669 ERROR:
670 esl_free(first_seqname);
671 esl_free(tmp_dsq);
672 esl_free(ct);
673 esl_free(ss_nopseudo);
674 esl_free(a2rf_map);
675
676 esl_msa_Destroy(msa);
677 esl_arr2_Destroy((void **) pp_ct, alen);
678 esl_arr2_Destroy((void **) abc_ct, alen);
679 esl_arr3_Destroy((void ***) bp_ct, known_alen, abc ? abc->Kp : 0);
680 esl_free(spos_ct);
681 esl_free(epos_ct);
682
683 if (ret_msa) *ret_msa = NULL;
684 if (opt_nseq) *opt_nseq = 0;
685 if (opt_alen) *opt_alen = 0;
686 if (opt_ngs) *opt_ngs = 0;
687 if (opt_maxname) *opt_maxname = 0;
688 if (opt_maxgf) *opt_maxgf = 0;
689 if (opt_maxgc) *opt_maxgc = 0;
690 if (opt_maxgr) *opt_maxgr = 0;
691 if (opt_pp_ct) *opt_pp_ct = NULL;
692 if (opt_abc_ct) *opt_abc_ct = NULL;
693 if (opt_bp_ct) *opt_bp_ct = NULL;
694 if (opt_spos_ct) *opt_spos_ct = NULL;
695 if (opt_epos_ct) *opt_epos_ct = NULL;
696 return status;
697 }
698
699 /* Function: esl_msafile2_RegurgitatePfam()
700 * Synopsis: Read and write next Pfam formatted MSA without storing it.
701 *
702 * Purpose: Read and immediately write each line of a MSA after
703 * optionally modifying aligned data by either adding all
704 * gap columns or removing some columns. Do this without
705 * creating an msa object, so memory usage is minimized.
706 *
707 * The alignment file <afp> must be in Pfam format (1
708 * line/seq non-interleaved Stockholm). The <do_*> arguments
709 * specify which parts of the alignment to write. <useme>
710 * specifies which positions to keep in aligned strings, if
711 * NULL then all are kept. <add2me> specifies how many gap
712 * characters to add after each aligned position, if NULL
713 * then none are added. Only one of <useme> and <add2me>
714 * can be non-NULL.
715 *
716 * If one of the <seqs2regurg> or <seqs2skip> keyhashes are
717 * non-NULL, they specify names of sequences (and affiliated
718 * annotation) to output (<seqs2regurg>) or not output
719 * (<seqs2skip>). Only one of these may be non-NULL.
720 *
721 * <maxname>, <maxgf>, <maxgc> and <maxgr> specify the max
722 * length sequence name, GF tag, GC tag, and GR tag, and can
723 * be provided by a caller that knows their values, e.g. as
724 * revealed by a previous call to <esl_msafile2_ReadInfoPfam()>.
725 * If any are -1, the caller didn't know the value, and the
726 * spacing in the alignment file we read in will be
727 * preserved. An example of useful non -1 values is if we're
728 * merging multiple alignments into a single alignment, and
729 * the spacing of any given alignment should change when all
730 * alignments are considered (like what the esl-alimerge
731 * miniapp does).
732 *
733 * This function is not as rigorous about validating the
734 * input as the other read functions that store the full
735 * alignment. Here, we verify that there is only one line
736 * for the first sequence read (verifying that all sequences
737 * are only one line would require storing and looking up
738 * all sequence names), that each aligned data line (<aseq>,
739 * GC, GR) are all the same length. The aligned length must
740 * equal <exp_alen> if it is not passed in as -1 (indicating
741 * caller doesn't know alignment length). If <useme> or
742 * <add2me> is non-NULL, <exp_alen> must not be -1.
743 *
744 * Aligned sequence residues are not checked to make sure
745 * they are consistent with an alphabet, they are simply
746 * rewritten as they are read from the input file.
747 *
748 * Args: afp - open alignment file pointer
749 * ofp - output file pointer
750 * maxname - maximum length of a sequence name (-1 if unknown)
751 * maxgf - maximum length of a GF tag (-1 if unknown)
752 * maxgc - maximum length of a GC tag (-1 if unknown)
753 * maxgr - maximum length of a GR tag (-1 if unknown)
754 * do_header - TRUE to write magic Stockholm header at top to ofp
755 * do_trailer - TRUE to write '//' at end to ofp
756 * do_blanks - TRUE to regurgitate blank lines, FALSE not to
757 * do_comments - TRUE to write comments to ofp
758 * do_gf - TRUE to write GF annotation to ofp
759 * do_gs - TRUE to write GS annotation to ofp
760 * do_gc - TRUE to write GC annotation to ofp
761 * do_gr - TRUE to write GR annotation to ofp
762 * do_aseq - TRUE to write aligned sequences to ofp
763 * seqs2regurg - keyhash of names of the sequences to write, all others
764 * will not be written. Associated annotation (GS, GR)
765 * will be written for these sequences only. Must be NULL
766 * if seqs2skip is non-NULL (enforced by contract).
767 * If both are NULL all seqs are written.
768 * seqs2skip - keyhash of names of the sequences to skip (not write),
769 * all others will be written. Associated annotation (GS, GR)
770 * will not be written for these sequences. Must be NULL
771 * if seqs2regurg is NULL (enforced by contract).
772 * If both are NULL all seqs are written.
773 * useme - [0..apos..exp_alen-1] TRUE to include position apos in output of
774 * aligned data (GC,GR,aseq), FALSE to remove it, can be NULL
775 * add2me - [0..apos..exp_alen-1] number of all gaps to add after each
776 * position of aligned data (GC,GR,aseq), can be NULL
777 * exp_alen - expected alignment length, -1 if unknown, which
778 * is okay as long as useme == add2me == NULL
779 * gapchar2add - gap character, only relevant if add2me != NULL
780 * opt_nseq_read - RETURN: optional, number of aligned sequences read
781 * opt_nseq_regurged - RETURN: optional, number of aligned sequences regurgitated,
782 * same as opt_nseq_read unless seqs2regurg != NULL or
783 * seqs2skip != NULL.
784 *
785 * Returns: <eslOK> on success.
786 * Returns <eslEOF> if there are no more alignments in <afp>.
787 * <eslEFORMAT> if parse fails because of a file format problem,
788 * in which case <afp->errbuf> is set to contain a formatted message
789 * that indicates the cause of the problem.
790 *
791 * Throws: <eslEMEM> on allocation error.
792 *
793 * Xref: ~nawrockie/notebook/9_1206_esl_msa_mem_efficient/
794 */
795 int
796 esl_msafile2_RegurgitatePfam(ESL_MSAFILE2 *afp, FILE *ofp, int maxname, int maxgf, int maxgc, int maxgr,
797 int do_header, int do_trailer, int do_blanks, int do_comments, int do_gf,
798 int do_gs, int do_gc, int do_gr, int do_aseq, ESL_KEYHASH *seqs2regurg,
799 ESL_KEYHASH *seqs2skip, int *useme, int *add2me, int exp_alen, char gapchar2add,
800 int *opt_nseq_read, int *opt_nseq_regurged)
801 {
802 char *s = NULL;
803 int status;
804 int status2;
805 char *seqname = NULL;
806 char *first_seqname = NULL;
807 char *text = NULL;
808 char *gapped_text = NULL;
809 char *tag = NULL;
810 char *gf = NULL;
811 char *gc = NULL;
812 char *gs = NULL;
813 char *gr = NULL;
814 int curmargin, curmargin2, namelen, spacelen, spacelen2, textlen, taglen;
815 int gaps2addlen;
816 int margin; /* width of left hand side margin */
817 int flushpoint = 10000; /* number of lines read at which to flush ofp */
818 int nseq_read = 0;
819 int nseq_regurged = 0;
820
821 /* contract check */
822 if ( ofp == NULL) ESL_EXCEPTION(eslEINCONCEIVABLE, "ofp is NULL");
823 if ( afp->format != eslMSAFILE_PFAM) ESL_EXCEPTION(eslEINCONCEIVABLE, "only Pfam (1 line /seq) Stockholm formatted files allowed in small mem mode");
824 if(add2me != NULL && useme != NULL) ESL_EXCEPTION(eslEINCONCEIVABLE, "add2me and useme both non-NULL");
825 if((add2me != NULL || useme != NULL) && exp_alen == -1) ESL_EXCEPTION(eslEINCONCEIVABLE, "exp_alen == -1, but add2me or useme non-NULL");
826 if(seqs2regurg != NULL && seqs2skip != NULL) ESL_EXCEPTION(eslEINVAL, "seqs2regurg and seqs2skip both non-NULL, only one may be");
827
828 gaps2addlen = (add2me == NULL) ? 0 : esl_vec_ISum(add2me, (exp_alen+1));
829
830 margin = -1;
831 if (maxgf != -1 && maxgf < 2) maxgf = 2;
832 if (maxname != -1) margin = maxname+1;
833 if (maxgc > 0 && maxgc+6 > margin) margin = maxgc+6;
834 if (maxgr > 0 && maxname+maxgr+7 > margin) margin = maxname+maxgr+7;
835 /* if margin is still -1, we'll use the same spacing we read in from the file */
836
837 if (feof(afp->f)) { status = eslEOF; goto ERROR; }
838 afp->errbuf[0] = '\0';
839
840 /* Check the magic Stockholm header line.
841 * We have to skip blank lines here, else we perceive
842 * trailing blank lines in a file as a format error when
843 * reading in multi-record mode.
844 */
845 do {
846 if ((status = msafile2_getline(afp)) != eslOK) goto ERROR; /* includes EOF */
847 } while (is_blankline(afp->buf));
848
849 if (strncmp(afp->buf, "# STOCKHOLM 1.", 14) != 0)
850 ESL_XFAIL(eslEFORMAT, afp->errbuf, "parse failed (line %d): missing \"# STOCKHOLM\" header", afp->linenumber);
851 if(do_header) fprintf(ofp, "%s", afp->buf);
852
853 /* Read the alignment file one line at a time.
854 */
855 while ((status2 = msafile2_getline(afp)) == eslOK)
856 {
857 if(afp->linenumber % flushpoint == 0) fflush(ofp);
858 s = afp->buf;
859 while (*s == ' ' || *s == '\t') s++; /* skip leading whitespace */
860
861 if (*s == '#') {
862
863 if (strncmp(s, "#=GF", 4) == 0)
864 {
865 if (do_gf) {
866 if(maxgf == -1) { /* just print line as is */
867 fprintf(ofp, "%s", afp->buf);
868 }
869 else { /* parse line into temporary strings, then print it out with correct formatting */
870 s = afp->buf;
871 if (esl_strtok(&s, " \t\n\r", &gf) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GF line", afp->linenumber);
872 if (esl_strtok(&s, " \t\n\r", &tag) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GF line", afp->linenumber);
873 if (esl_strtok(&s, "\n\r", &text) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GF line", afp->linenumber);
874 fprintf(ofp, "#=GF %-*s %s\n", maxgf, tag, text);
875 }
876 }
877 }
878 else if (strncmp(s, "#=GC", 4) == 0)
879 {
880 if(do_gc) {
881 /* parse line into temporary strings */
882 s = afp->buf;
883 if (esl_strtok (&s, " \t\n\r", &gc) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GC line", afp->linenumber);
884 if (esl_strtok_adv(&s, " \t\n\r", &tag, &taglen, NULL) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GC line", afp->linenumber);
885 spacelen = determine_spacelen(s);
886 if (esl_strtok_adv(&s, " \t\n\r", &text, &textlen, NULL) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GC line", afp->linenumber);
887
888 /* verify alignment length */
889 if(exp_alen == -1) exp_alen = textlen;
890 else if(exp_alen != textlen) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GC line, len %d, expected %d", afp->linenumber, textlen, exp_alen);
891
892 /* determine margin length of sequence name field for output formatting */
893 curmargin = (margin == -1) ? taglen + spacelen : margin - 6;
894
895 /* output, after optionally removing some characters (if useme != NULL) or adding gaps (if add2me != NULL) (contract enforces only one can be non-null) */
896 if(useme != NULL) {
897 /* if this is a GC SS_cons line, remove broken basepairs first - only if it's in WUSS RNA format (NOT for a protein SS!) */
898 if (strncmp(tag, "SS_cons", 7) == 0 && afp->abc && (afp->abc->type == eslRNA || afp->abc->type == eslDNA)) {
899 if((status = esl_msa_RemoveBrokenBasepairsFromSS(text, afp->errbuf, textlen, useme)) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GC SS_cons line", afp->linenumber);
900 }
901 shrink_string(text, useme, exp_alen); /* this is done in place on text */
902 }
903 if(add2me != NULL) {
904 if((status = gapize_string(text, textlen, textlen + gaps2addlen, add2me, gapchar2add, &gapped_text)) != eslOK) goto ERROR;
905 fprintf(ofp, "#=GC %-*s %s\n", curmargin, tag, gapped_text);
906 free(gapped_text);
907 }
908 else {
909 fprintf(ofp, "#=GC %-*s %s\n", curmargin, tag, text);
910 }
911 }
912 }
913 else if (strncmp(s, "#=GS", 4) == 0)
914 {
915 /* we don't validate the sequence exists, this would require storing all seqnames */
916 if (do_gs) {
917 if(maxname == -1 && seqs2regurg == NULL) { /* just print line as is */
918 fprintf(ofp, "%s", afp->buf);
919 }
920 else { /* parse line into temporary strings, then print it out with correct formatting */
921 if((seqs2regurg == NULL && seqs2skip == NULL) ||
922 (seqs2regurg != NULL && (status = esl_keyhash_Lookup(seqs2regurg, seqname, -1, NULL)) == eslOK) ||
923 (seqs2skip != NULL && (status = esl_keyhash_Lookup(seqs2skip, seqname, -1, NULL)) == eslENOTFOUND))
924 { /* this if() will evaluate as TRUE if seqs2regurg and seqs2skip are both NULL, or the seqname exists in seqs2regurg or does not exist in seqs2skip, else it will return FALSE */
925 s = afp->buf;
926 if (esl_strtok(&s, " \t\n\r", &gs) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GS line", afp->linenumber);
927 if (esl_strtok(&s, " \t\n\r", &seqname) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GS line", afp->linenumber);
928 if (esl_strtok(&s, " \t\n\r", &tag) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GS line", afp->linenumber);
929 if (esl_strtok(&s, "\n\r", &text) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GS line", afp->linenumber);
930 fprintf(ofp, "#=GS %-*s %s %s\n", maxname, seqname, tag, text);
931 }
932 }
933 }
934 }
935 else if (strncmp(s, "#=GR", 4) == 0)
936 {
937 if(do_gr) {
938 /* parse line into temporary strings */
939 s = afp->buf;
940 if (esl_strtok (&s, " \t\n\r", &gr) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GR line", afp->linenumber);
941 if (esl_strtok_adv(&s, " \t\n\r", &seqname, &namelen, NULL) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GR line", afp->linenumber);
942 spacelen = determine_spacelen(s);
943 if (esl_strtok_adv(&s, " \t\n\r", &tag, &taglen, NULL) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GR line", afp->linenumber);
944 spacelen2 = determine_spacelen(s);
945 if (esl_strtok_adv(&s, " \t\n\r", &text, &textlen, NULL) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GR line", afp->linenumber);
946
947 /* verify alignment length */
948 if(exp_alen == -1) exp_alen = textlen;
949 else if(exp_alen != textlen) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GR line, len %d, expected %d", afp->linenumber, textlen, exp_alen);
950
951 /* determine length of fields for output formatting */
952 curmargin = (maxname == -1) ? namelen + spacelen : maxname;
953 curmargin2 = (maxname == -1) ? taglen + spacelen2 : margin - maxname - 7;
954
955 /* determine if we should regurgitate GR for this sequence or not */
956 if((seqs2regurg == NULL && seqs2skip == NULL) ||
957 (seqs2regurg != NULL && (status = esl_keyhash_Lookup(seqs2regurg, seqname, -1, NULL)) == eslOK) ||
958 (seqs2skip != NULL && (status = esl_keyhash_Lookup(seqs2skip, seqname, -1, NULL)) == eslENOTFOUND))
959 { /* this if() will evaluate as TRUE if seqs2regurg and seqs2skip are both NULL, or the seqname exists in seqs2regurg or does not exist in seqs2skip, else it will return FALSE */
960 /* output GR, after optionally removing some characters (if useme != NULL) or adding gaps (if add2me != NULL) (contract enforces only one can be non-null) */
961 if(useme != NULL) {
962 /* if this is a GR SS line, remove broken basepairs first */
963 if( strncmp(tag, "SS", 2) == 0 && afp->abc && (afp->abc->type == eslRNA || afp->abc->type == eslDNA)) {
964 if((status = esl_msa_RemoveBrokenBasepairsFromSS(text, afp->errbuf, textlen, useme)) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad #=GR SS line", afp->linenumber);
965 }
966 shrink_string(text, useme, exp_alen); /* this is done in place on text */
967 }
968 if(add2me != NULL) {
969 if((status = gapize_string(text, textlen, textlen + gaps2addlen, add2me, gapchar2add, &gapped_text)) != eslOK) goto ERROR;
970 fprintf(ofp, "#=GR %-*s %-*s %s\n", curmargin, seqname, curmargin2, tag, gapped_text);
971 free(gapped_text);
972 }
973 else {
974 fprintf(ofp, "#=GR %-*s %-*s %s\n", curmargin, seqname, curmargin2, tag, text);
975 }
976 }
977 }
978 }
979 else if (do_comments) fprintf(ofp, "%s", afp->buf); /* print comment line, if desired */
980 } /* end of 'if (*s == '#')' */
981 else if (strncmp(s, "//", 2) == 0) { if(do_trailer) fprintf(ofp, "%s", afp->buf); break; /* normal way out */ }
982 else if (*s == '\n' || *s == '\r') { if(do_blanks) { fprintf(ofp, "%s", afp->buf); } continue; }
983 else { /* sequence line */
984 if(do_aseq) {
985 /* parse line into temporary strings */
986 s = afp->buf;
987 if (esl_strtok_adv(&s, " \t\n\r", &seqname, &namelen, NULL) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad sequence line", afp->linenumber);
988 spacelen = determine_spacelen(s);
989 if (esl_strtok_adv(&s, " \t\n\r", &text, &textlen, NULL) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad sequence line", afp->linenumber);
990
991 /* verify alignment length */
992 if((exp_alen != -1) && (exp_alen != textlen)) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): bad seq line, len %d, expected %d", afp->linenumber, textlen, exp_alen);
993
994 /* determine length of sequence name field for output formatting */
995 curmargin = (margin == -1) ? namelen + spacelen : margin-1;
996
997 /* 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) */
998 if(nseq_read == 0) { if ((status = esl_strdup(seqname, -1, &(first_seqname))) != eslOK) goto ERROR; }
999 else if(strcmp(first_seqname, seqname) == 0) { ESL_XFAIL(eslEFORMAT, afp->errbuf, "parse failed (line %d): two seqs named %s. Alignment appears to be in Stockholm format. Reformat to Pfam with esl-reformat.", afp->linenumber, seqname); }
1000 nseq_read++;
1001
1002 /* determine if we should regurgitate this sequence or not */
1003 if((seqs2regurg == NULL && seqs2skip == NULL) ||
1004 (seqs2regurg != NULL && (status = esl_keyhash_Lookup(seqs2regurg, seqname, -1, NULL)) == eslOK) ||
1005 (seqs2skip != NULL && (status = esl_keyhash_Lookup(seqs2skip, seqname, -1, NULL)) == eslENOTFOUND))
1006 { /* this if() will evaluate as TRUE if seqs2regurg and seqs2skip are both NULL, or the seqname exists in seqs2regurg or does not exist in seqs2skip, else it will return FALSE */
1007 /* output sequence, after optionally removing some characters (if useme != NULL) or adding gaps (if add2me != NULL) (contract enforces only one can be non-null) */
1008 nseq_regurged++;
1009 if(useme != NULL) shrink_string(text, useme, exp_alen); /* this is done in place on text */
1010 if(add2me != NULL) {
1011 if((status = gapize_string(text, textlen, textlen + gaps2addlen, add2me, gapchar2add, &gapped_text)) != eslOK) goto ERROR;
1012 fprintf(ofp, "%-*s %s\n", curmargin, seqname, gapped_text);
1013 free(gapped_text);
1014 }
1015 else {
1016 fprintf(ofp, "%-*s %s\n", curmargin, seqname, text);
1017 }
1018 }
1019 }
1020 }
1021 }
1022
1023 /* If we saw a normal // end, we would've successfully read a line,
1024 * so when we get here, status (from the line read) should be eslOK.
1025 */
1026 if (status2 != eslOK) ESL_XFAIL(eslEFORMAT, afp->errbuf, "small mem parse failed (line %d): didn't find // at end of alignment", afp->linenumber);
1027 if (first_seqname != NULL) free(first_seqname);
1028 if (opt_nseq_read != NULL) *opt_nseq_read = nseq_read;
1029 if (opt_nseq_regurged != NULL) *opt_nseq_regurged = nseq_regurged;
1030 return eslOK;
1031
1032 ERROR:
1033 return status;
1034 }
1035
1036 /* get_pp_idx
1037 *
1038 * Given a GR PP or GC PP_cons character, return the appropriate index
1039 * in a pp_ct[] vector.
1040 * '0' return 0;
1041 * '1' return 1;
1042 * '2' return 2;
1043 * '3' return 3;
1044 * '4' return 4;
1045 * '5' return 5;
1046 * '6' return 6;
1047 * '7' return 7;
1048 * '8' return 8;
1049 * '9' return 9;
1050 * '*' return 10;
1051 * gap return 11;
1052 *
1053 * Anything else (including missing or nonresidue) return -1;
1054 *
1055 * This mapping of PP chars to return values should probably be
1056 * stored in some internal map structure somewhere, instead of
1057 * only existing in this function as used by esl_msafile2_ReadInfoPfam().
1058 */
1059 static int
1060 get_pp_idx(ESL_ALPHABET *abc, char ppchar)
1061 {
1062 if(esl_abc_CIsGap(abc, ppchar)) return 11;
1063 if(ppchar == '*') return 10;
1064 if(ppchar == '9') return 9;
1065 if(ppchar == '8') return 8;
1066 if(ppchar == '7') return 7;
1067 if(ppchar == '6') return 6;
1068 if(ppchar == '5') return 5;
1069 if(ppchar == '4') return 4;
1070 if(ppchar == '3') return 3;
1071 if(ppchar == '2') return 2;
1072 if(ppchar == '1') return 1;
1073 if(ppchar == '0') return 0;
1074 return -1;
1075 }
1076
1077 /* gapize_string
1078 *
1079 * Given a string, create a new one that is a copy of it,
1080 * but with gaps added before each position (apos) as specified
1081 * by ngapA[0..apos..len]. <gapchar> specifies the gap character
1082 * to add.
1083 *
1084 * ngapA[0] - number of gaps to add before first posn
1085 * ngapA[apos] - number of gaps to add before posn apos
1086 * ngapA[len] - number of gaps to add after final posn
1087 *
1088 * ret_str is allocated here.
1089 *
1090 * Returns eslOK on success.
1091 * eslEMEM on memory error.
1092 */
1093 static int
1094 gapize_string(char *src_str, int64_t src_len, int64_t dst_len, int *ngapA, char gapchar, char **ret_dst_str)
1095 {
1096 int status;
1097 int src_apos = 0;
1098 int dst_apos = 0;
1099 int i;
1100 char *dst_str;
1101
1102 ESL_ALLOC(dst_str, sizeof(char) * (dst_len+1));
1103 dst_str[dst_len] = '\0';
1104
1105 /* add gaps before first position */
1106 for(i = 0; i < ngapA[0]; i++) dst_str[dst_apos++] = gapchar;
1107
1108 /* add gaps after every position */
1109 for(src_apos = 0; src_apos < src_len; src_apos++) {
1110 dst_str[dst_apos++] = src_str[src_apos];
1111 for(i = 0; i < ngapA[(src_apos+1)]; i++) dst_str[dst_apos++] = gapchar;
1112 }
1113
1114 *ret_dst_str = dst_str;
1115 return eslOK;
1116
1117 ERROR:
1118 return eslEMEM;
1119 }
1120
1121 /* shrink_string
1122 *
1123 * Remove some characters of a string in place.
1124 * If useme[0..pos..len-1] == FALSE, remove position pos.
1125 *
1126 */
1127 static void
1128 shrink_string(char *str, const int *useme, int len)
1129 {
1130 int opos, npos;
1131
1132 for (opos = 0, npos = 0; opos < len; opos++) {
1133 if (useme[opos] == FALSE) continue;
1134 str[npos++] = str[opos];
1135 }
1136 str[npos] = '\0';
1137 return;
1138 }
1139
1140
1141 /* determine_spacelen
1142 *
1143 * Determine number of consecutive ' ' characters
1144 * in the string pointed to by s.
1145 */
1146 static int
1147 determine_spacelen(char *s)
1148 {
1149 int spacelen = 0;
1150 while (*s == ' ') { spacelen++; s++; }
1151 return spacelen;
1152 }
1153 /*------------- end, memory efficient Pfam i/o -----------------*/
1154
1155
1156
1157 /*****************************************************************
1158 * 3. Legacy Stockholm parsing tools
1159 *****************************************************************/
1160
1161 /* msafile_getline():
1162 * load the next line of <afp> into <afp->buf>.
1163 * Returns eslOK on success, eslEOF on normal eof.
1164 * Throws eslEMEM on alloc failure.
1165 */
1166 static int
1167 msafile2_getline(ESL_MSAFILE2 *afp)
1168 {
1169 int status;
1170 status = esl_fgets(&(afp->buf), &(afp->buflen), afp->f);
1171 afp->linenumber++;
1172 return status;
1173 }
1174
1175
1176 static int
1177 is_blankline(char *s)
1178 {
1179 for (; *s != '\0'; s++)
1180 if (! isspace((int) *s)) return FALSE;
1181 return TRUE;
1182 }
1183
1184 /* Format of a GF line:
1185 * #=GF <tag> <text>
1186 * Returns eslOK on success; eslEFORMAT on parse failure.
1187 * Throws eslEMEM on allocation failure.
1188 */
1189 static int
1190 parse_gf(ESL_MSA *msa, char *buf)
1191 {
1192 char *gf;
1193 char *tag;
1194 char *text;
1195 char *tok;
1196 char *s;
1197 int n;
1198 int status;
1199
1200 s = buf;
1201 if (esl_strtok(&s, " \t\n\r", &gf) != eslOK) return eslEFORMAT;
1202 if (esl_strtok(&s, " \t\n\r", &tag) != eslOK) return eslEFORMAT;
1203
1204 /* special case: accept a blank #=GF CC line. Otherwise, <s> is required. */
1205 while (isspace(*s)) s++;
1206 status = esl_strtok_adv(&s, "\n\r", &text, &n, NULL);
1207 if (status == eslEOL) { if (strcmp(tag, "CC") != 0) return eslEFORMAT; }
1208 else if (status != eslOK) { return eslEFORMAT; }
1209
1210 if (strcmp(tag, "ID") == 0) status = esl_strdup(text, n, &(msa->name));
1211 else if (strcmp(tag, "AC") == 0) status = esl_strdup(text, n, &(msa->acc));
1212 else if (strcmp(tag, "DE") == 0) status = esl_strdup(text, n, &(msa->desc));
1213 else if (strcmp(tag, "AU") == 0) status = esl_strdup(text, n, &(msa->au));
1214 else if (strcmp(tag, "GA") == 0)
1215 { /* Pfam has GA1, GA2. Rfam just has GA1. */
1216 s = text;
1217 if ((esl_strtok(&s, " \t\n\r", &tok)) != eslOK)
1218 return eslEFORMAT;
1219 msa->cutoff[eslMSA_GA1] = atof(tok);
1220 msa->cutset[eslMSA_GA1] = TRUE;
1221 if ((esl_strtok(&s, " \t\n\r", &tok)) == eslOK)
1222 {
1223 msa->cutoff[eslMSA_GA2] = atof(tok);
1224 msa->cutset[eslMSA_GA2] = TRUE;
1225 }
1226 status = eslOK;
1227 }
1228 else if (strcmp(tag, "NC") == 0)
1229 {
1230 s = text;
1231 if ((esl_strtok(&s, " \t\n\r", &tok)) != eslOK)
1232 return eslEFORMAT;
1233 msa->cutoff[eslMSA_NC1] = atof(tok);
1234 msa->cutset[eslMSA_NC1] = TRUE;
1235 if ((esl_strtok(&s, " \t\n\r", &tok)) == eslOK)
1236 {
1237 msa->cutoff[eslMSA_NC2] = atof(tok);
1238 msa->cutset[eslMSA_NC2] = TRUE;
1239 }
1240 status = eslOK;
1241 }
1242 else if (strcmp(tag, "TC") == 0)
1243 {
1244 s = text;
1245 if ((esl_strtok(&s, " \t\n\r", &tok)) != eslOK)
1246 return eslEFORMAT;
1247 msa->cutoff[eslMSA_TC1] = atof(tok);
1248 msa->cutset[eslMSA_TC1] = TRUE;
1249 if ((esl_strtok(&s, "\t\n\r", &tok)) == eslOK)
1250 {
1251 msa->cutoff[eslMSA_TC2] = atof(tok);
1252 msa->cutset[eslMSA_TC2] = TRUE;
1253 }
1254 status = eslOK;
1255 }
1256 else if (strcmp(tag, "CC") == 0 && text == NULL)
1257 status = esl_msa_AddGF (msa, tag, -1, "", -1);
1258 else
1259 status = esl_msa_AddGF(msa, tag, -1, text, -1);
1260
1261 return status;
1262 }
1263
1264 /* parse_gc():
1265 * Format of a GC line:
1266 * #=GC <tag> <aligned text>
1267 */
1268 static int
1269 parse_gc(ESL_MSA *msa, char *buf)
1270 {
1271 char *gc;
1272 char *tag;
1273 char *text;
1274 char *s;
1275 int len;
1276 int status;
1277
1278 s = buf;
1279 if (esl_strtok (&s, " \t\n\r", &gc) != eslOK) return eslEFORMAT;
1280 if (esl_strtok (&s, " \t\n\r", &tag) != eslOK) return eslEFORMAT;
1281 if (esl_strtok_adv(&s, " \t\n\r", &text, &len, NULL) != eslOK) return eslEFORMAT;
1282
1283 if (strcmp(tag, "SS_cons") == 0) status = esl_strcat(&(msa->ss_cons), -1, text, len);
1284 else if (strcmp(tag, "SA_cons") == 0) status = esl_strcat(&(msa->sa_cons), -1, text, len);
1285 else if (strcmp(tag, "PP_cons") == 0) status = esl_strcat(&(msa->pp_cons), -1, text, len);
1286 else if (strcmp(tag, "RF") == 0) status = esl_strcat(&(msa->rf), -1, text, len);
1287 else if (strcmp(tag, "MM") == 0) status = esl_strcat(&(msa->mm), -1, text, len);
1288 else status = esl_msa_AppendGC(msa, tag, text);
1289
1290 return status;
1291 }
1292
1293
1294 /* parse_comment():
1295 * comments are simply stored verbatim, not parsed
1296 */
1297 static int
1298 parse_comment(ESL_MSA *msa, char *buf)
1299 {
1300 char *s;
1301 char *comment;
1302
1303 s = buf + 1; /* skip leading '#' */
1304 if (*s == '\n' || *s == '\r') { *s = '\0'; comment = s; } /* deal with blank comment */
1305 else if (esl_strtok(&s, "\n\r", &comment)!= eslOK) return eslEFORMAT;
1306 return (esl_msa_AddComment(msa, comment, -1));
1307 }
1308 /*-------------- end, legacy stockholm parser -------------------*/
1309
1310 /*****************************************************************
1311 * 4. Unit tests
1312 *****************************************************************/
1313 #ifdef eslMSAFILE2_TESTDRIVE
1314
1315 /* write_known_pfam_msa()
1316 * Write a known MSA to a tmpfile in Pfam Stockholm format.
1317 */
1318 static void
1319 write_known_pfam_msa(FILE *ofp)
1320 {
1321 fprintf(ofp, "# STOCKHOLM 1.0\n");
1322 fprintf(ofp, "#=GF ID pfam-test\n");
1323 fprintf(ofp, "#=GS seq2 DE seq2 is interesting\n");
1324 fprintf(ofp, "seq1 --ACDEFGHIK~LMNPQRS-TVWYACDEFGHIKLMNPQRSTVWY~~~\n");
1325 fprintf(ofp, "seq2 aaACDEFGHIK~LMNPQRS-TVWYACDEFGHIKLMNPQRSTVWYyyy\n");
1326 fprintf(ofp, "seq3 aaACDEFGHIK~LMNPQRS-TVWYACDEFGHIKLMNPQRSTVWYyyy\n");
1327 fprintf(ofp, "#=GC RF ..xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...\n");
1328 fprintf(ofp, "//\n");
1329 return;
1330 }
1331
1332
1333 static void
1334 utest_ReadInfoPfam(char *filename)
1335 {
1336 char *msg = "ReadInfo() unit test failure";
1337 ESL_MSAFILE2 *mfp = NULL;
1338 ESL_MSA *msa = NULL;
1339 int nseq = 0;
1340 int64_t alen = 0;
1341 int ngs = 0;
1342 int maxname = 0;
1343 int maxgf = 0;
1344 int maxgc = 0;
1345 int maxgr = 0;
1346
1347 if (esl_msafile2_Open(filename, NULL, &mfp) != eslOK) esl_fatal(msg);
1348 if (esl_msafile2_ReadInfoPfam(mfp, NULL, NULL, -1, NULL, NULL, &msa, &nseq, &alen, &ngs, &maxname, &maxgf, &maxgc, &maxgr, NULL, NULL, NULL, NULL, NULL) != eslOK) esl_fatal(msg);
1349
1350 if (msa->nseq != 0) esl_fatal("bad msa->nseq");
1351 if (msa->alen != -1) esl_fatal("bad msa->alen");
1352 if (nseq != 3) esl_fatal("bad nseq");
1353 if (alen != 47) esl_fatal("bad alen");
1354 if (ngs != 1) esl_fatal("bad ngs");
1355 if (maxname != 4) esl_fatal("bad maxname");
1356 if (maxgf != 2) esl_fatal("bad maxgf");
1357 if (maxgc != 2) esl_fatal("bad maxgc");
1358 if (maxgr != 0) esl_fatal("bad maxgr");
1359 esl_msa_Destroy(msa);
1360
1361 if (esl_msafile2_ReadInfoPfam(mfp, NULL, NULL, -1, NULL, NULL, &msa, &nseq, &alen, &ngs, &maxname, &maxgf, &maxgc, &maxgr, NULL, NULL, NULL, NULL, NULL) != eslEOF) esl_fatal(msg);
1362 if (msa != NULL) esl_fatal(msg);
1363 if (nseq != 0 || alen != 0 || ngs != 0 || maxname != 0 || maxgf != 0 || maxgc != 0 || maxgr != 0) esl_fatal("bad nseq");
1364
1365 esl_msafile2_Close(mfp);
1366 }
1367
1368 static void
1369 utest_RegurgitatePfam(char *filename)
1370 {
1371 char *msg = "RegurgitatePfam() unit test failure";
1372 ESL_MSAFILE2 *mfp = NULL;
1373 ESL_MSAFILE *afp = NULL;
1374 char tmpfile[16] = "esltmpXXXXXX";
1375 FILE *fp = NULL;
1376 ESL_MSA *msa1 = NULL;
1377 ESL_MSA *msa2 = NULL;
1378
1379 /* regurgitate msa in filename to tmpfile (an msa structure will not be created) */
1380 if (esl_msafile2_Open(filename, NULL, &mfp) != eslOK) esl_fatal(msg);
1381 if (esl_tmpfile_named(tmpfile, &fp) != eslOK) esl_fatal(msg);
1382 if (esl_msafile2_RegurgitatePfam(mfp, fp,
1383 -1, -1, -1, -1, /* maxname, maxgf, maxgc, maxgr unknown: output msa formatting will match input msa formatting */
1384 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, /* do_header, do_trailer, do_blanks, do_comments, do_gf, do_gs, do_gc, do_gr, do_aseq: print all components */
1385 NULL, /* seqs2regurg: if non-NULL specifies which sequences to keep in output */
1386 NULL, /* seqs2skip: if non-NULL specifies which sequences to skip in output */
1387 NULL, /* useme: if non-NULL specifies which columns to keep in output */
1388 NULL, /* add2me: if non-NULL specifies how many gap columns to add in output */
1389 -1, /* expected alignment length, unknown (must not be if useme != NULL or add2me != NULL */
1390 '.', /* gapchar2add, irrelevant since add2me is NULL */
1391 NULL, /* don't return num seqs read */
1392 NULL) /* don't return num seqs read */
1393 != eslOK) esl_fatal(msg);
1394 fclose(fp);
1395 esl_msafile2_Close(mfp);
1396
1397 /* Using normal interface, read in msa from filename as msa1 */
1398 if (esl_msafile_Open(NULL, filename, NULL, eslMSAFILE_PFAM, NULL, &afp) != eslOK) esl_fatal(msg);
1399 if (esl_msafile_Read(afp, &msa1) != eslOK) esl_fatal(msg);
1400 esl_msafile_Close(afp);
1401
1402 /* Using normal interface, read in msa from tmpfile as msa2 */
1403 if (esl_msafile_Open(NULL, tmpfile, NULL, eslMSAFILE_PFAM, NULL, &afp) != eslOK) esl_fatal(msg);
1404 if (esl_msafile_Read(afp, &msa2) != eslOK) esl_fatal(msg);
1405 esl_msafile_Close(afp);
1406
1407 if (esl_msa_Compare(msa1, msa2) != eslOK) esl_fatal(msg);
1408
1409 esl_msa_Destroy(msa1);
1410 esl_msa_Destroy(msa2);
1411 remove(tmpfile);
1412 }
1413 #endif /* eslMSAFILE2_TESTDRIVE */
1414 /*------------------ end, unit tests ----------------------------*/
1415
1416
1417 /*****************************************************************
1418 * 5. Test driver
1419 *****************************************************************/
1420 #ifdef eslMSAFILE2_TESTDRIVE
1421
1422 /* compile: gcc -g -Wall -I. -L. -o esl_msafile2_utest -DeslMSAFILE2_TESTDRIVE esl_msafile2.c -leasel -lm
1423 * (gcov): gcc -g -Wall -fprofile-arcs -ftest-coverage -I. -L. -o esl_msafile2_utest -DeslMSAFILE2_TESTDRIVE esl_msafile2.c -leasel -lm
1424 * run: ./esl_msafile2_utest
1425 */
1426 #include <esl_config.h>
1427
1428 #include <stdio.h>
1429
1430 #include "easel.h"
1431 #include "esl_getopts.h"
1432 #include "esl_msafile2.h"
1433
1434 static ESL_OPTIONS options[] = {
1435 /* name type default env range togs reqs incomp help docgrp */
1436 {"-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0},
1437 { 0,0,0,0,0,0,0,0,0,0},
1438 };
1439 static char usage[] = "[-options]";
1440 static char banner[] = "test driver for legacy memory-efficient Pfam format input/output";
1441
1442 int
1443 main(int argc, char **argv)
1444 {
1445 ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage);
1446 FILE *fp = NULL;
1447 char tmpfile[16] = "esltmpXXXXXX";
1448
1449 if (esl_tmpfile_named(tmpfile, &fp) != eslOK) esl_fatal("failed to create tmpfile");
1450 write_known_pfam_msa(fp);
1451 fclose(fp);
1452
1453 utest_ReadInfoPfam(tmpfile);
1454 utest_RegurgitatePfam(tmpfile);
1455
1456 remove(tmpfile);
1457 esl_getopts_Destroy(go);
1458 exit(0);
1459 }
1460 #endif /*eslMSAFILE2_TESTDRIVE*/
1461 /*----------------- end, test driver ----------------------------*/