Debian Codegraph @

easel/miniapps/esl-alimerge.c

raw symbols SCIP
1 /* Merge alignments into a single alignment based on their reference (RF) annotation.
2 *
3 * Special care is taken to be consistent with missing data '~'
4 * annotation in two different contexts.
5 *
6 * First, from Infernal 1.1rc2 and later cmalign and cmsearch -A
7 * output alignments: '~' can occur in GC RF and SS_cons
8 * annotation. The '~' after a given nongap/nonmissing RF position x
9 * (and before nongap/nonmissing RF position x+1) must all occur 5' of
10 * all inserted positions (gap: '.') that occur between x and x+1.
11 *
12 * Second, from HMMER3, hmmbuild reads and writes '~' in sequences to
13 * mark the ends of fragments. Any fragment must begin and end with a
14 * contiguous string of 1 or more '~'. '~' characters are not allowed
15 * within sequences anywhere except within these leading and trailing
16 * contiguous stretches.
17 *
18 * esl-alimerge will accept as input and produce as output alignments
19 * that are consistent with both of these conventions.
20 *
21 * EPN, Fri Nov 20 16:28:59 2009
22 */
23 #include <esl_config.h>
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "easel.h"
30 #include "esl_alphabet.h"
31 #include "esl_distance.h"
32 #include "esl_fileparser.h"
33 #include "esl_getopts.h"
34 #include "esl_msa.h"
35 #include "esl_msafile.h"
36 #include "esl_msafile2.h"
37 #include "esl_sqio.h"
38 #include "esl_stopwatch.h"
39 #include "esl_vectorops.h"
40
41 static char banner[] = "merge alignments based on their reference (RF) annotation";
42 static char usage1[] = "[options] <alignment file 1> <alignment file 2>";
43 static char usage2[] = "[options] --list <file listing n > 1 ali files to merge>\n\
44 \n\
45 Input alignments must be in Stockholm or Pfam format.\n\
46 Ouput format choices\n\
47 --------------------\n\
48 stockholm [default]\n\
49 pfam\n\
50 a2m\n\
51 psiblast\n\
52 afa";
53
54 static void read_list_file(char *listfile, char ***ret_alifile_list, int *ret_nalifile);
55 static int update_maxgap_and_maxmis(ESL_MSA *msa, char *errbuf, int clen, int64_t alen, int *maxgap, int *maxmis);
56 static int validate_and_copy_msa_annotation(const ESL_GETOPTS *go, int outfmt, ESL_MSA *mmsa, ESL_MSA **msaA, int clen, int nmsa, int alen_merged, int *maxgap, int *maxmis, char *errbuf);
57 static int add_msa(ESL_MSA *mmsa, char *errbuf, ESL_MSA *msa_to_add, int *maxgap, int *maxmis, int clen, int alen_merged);
58 static int inflate_string_with_gaps_and_missing(char *src_str, int64_t src_len, int64_t dst_len, int *ngapA, char gapchar, int *nmisA, char mischar, char **ret_dst_str);
59 static int inflate_seq_with_gaps(const ESL_ALPHABET *abc, char *src_str, int64_t src_len, int64_t dst_len, int *ngapA, char gapchar, char **ret_dst_str);
60 static int validate_no_nongaps_in_rf_gaps(const ESL_ALPHABET *abc, char *rf_str, char *other_str, int64_t len);
61 static int determine_gap_columns_to_add(ESL_MSA *msa, int *maxgap, int *maxmis, int clen, int **ret_ngapA, int **ret_nmisA, int **ret_neitherA, char *errbuf);
62 static void write_pfam_msa_top(FILE *fp, ESL_MSA *msa);
63 static void write_pfam_msa_gc(FILE *fp, ESL_MSA *msa, int maxwidth);
64 static int64_t maxwidth(char **s, int n);
65 static int rfchar_is_nongap_nonmissing(const ESL_ALPHABET *abc, char rfchar);
66
67 static ESL_OPTIONS options[] = {
68 /* name type default env range togs reqs incomp help docgroup */
69 { "--list", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "command-line argument is a file that lists ali files to merge", 99 },
70 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "help; show brief info on version and usage", 1 },
71 { "-o", eslARG_OUTFILE, NULL, NULL, NULL, NULL,NULL, NULL, "output the final alignment to file <f>, not stdout", 1 },
72 { "-v", eslARG_NONE, FALSE, NULL, NULL, NULL,"-o", NULL, "print info on merge to stdout; requires -o", 1 },
73 { "--small", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "use minimal RAM (RAM usage will be independent of aln sizes)", 1 },
74 { "--rfonly", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "remove all columns that are gaps in GC RF annotation", 1 },
75 { "--informat", eslARG_STRING, FALSE, NULL, NULL, NULL,NULL, NULL, "NOT YET DISPLAYED", 99 },
76 { "--outformat",eslARG_STRING, FALSE, NULL, NULL, NULL,NULL, NULL, "specify that output aln be format <s> (see choices above)", 1 },
77 { "--rna", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL,"--amino,--rna", "alignments to merge are RNA alignments", 1 },
78 { "--dna", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL,"--amino,--rna", "alignments to merge are DNA alignments", 1 },
79 { "--amino", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL,"--dna,--rna", "alignments to merge are protein alignments", 1 },
80 { "--stall", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "arrest after start: for debugging under gdb", 99 },
81 { 0,0,0,0,0,0,0,0,0,0 },
82 };
83
84 int
85 main(int argc, char **argv)
86 {
87 int status; /* easel return code */
88 ESL_GETOPTS *go = NULL; /* application configuration */
89 ESL_ALPHABET *abc = NULL; /* biological alphabet */
90 char *msafile1 = NULL; /* msa file 1 (stays NULL if --list) */
91 char *msafile2 = NULL; /* msa file 2 (stays NULL if --list) */
92 char *listfile = NULL; /* list file name (stays NULL unless --list) */
93 int infmt = eslMSAFILE_UNKNOWN; /* format code for input alifiles */
94 int outfmt = eslMSAFILE_UNKNOWN; /* format code for output ali */
95 ESL_MSAFILE *afp = NULL; /* open alignment file, normal interface */
96 ESL_MSAFILE2 *afp2 = NULL; /* open alignment file, small-mem interface */
97 FILE *ofp; /* output file (default is stdout) */
98 char **alifile_list = NULL; /* list of alignment files to merge */
99 int nalifile; /* size of alifile_list */
100 int do_stall; /* used to stall when debugging */
101 int fi; /* counter over alignment files */
102 int ai, ai2; /* counters over alignments */
103 int nali_tot; /* number of alignments in all files */
104 int *nali_per_file = NULL; /* [0..nalifile-1] number of alignments per file */
105 int nseq_tot; /* number of sequences in all alignments */
106 int nseq_cur; /* number of sequences in current alignment */
107 int64_t alen_cur; /* length of current alignment */
108 int64_t *alenA = NULL; /* [0..nali_tot-1] alignment length of input msas (even after
109 * potentially removingeinserts (--rfonly)) */
110 ESL_MSA **msaA = NULL; /* [0..nali_tot-1] all msas read from all files */
111 int *maxgap = NULL; /* [0..cpos..cm->clen+1] max number of gap columns
112 * before each consensus position in all alignments */
113 int *maxmis = NULL; /* [0..cpos..cm->clen+1] max number of missing data columns
114 * before each consensus position in all alignments */
115 int nalloc = 0; /* current size of msaA */
116 int chunksize = 10; /* size to increase nalloc by when realloc'ing */
117 void *tmp; /* for ESL_RALLOC() */
118 int clen; /* consensus length (non-gap #=GC RF length) of all alignments */
119 int cur_clen; /* consensus length (non-gap #=GC RF length) of current alignments */
120 int apos; /* alignment position */
121 ESL_MSA *mmsa = NULL; /* the merged alignment created by merging all alignments in msaA */
122 int alen_mmsa; /* number of columns in merged MSA */
123 char errbuf[eslERRBUFSIZE]; /* buffer for error messages */
124 char *tmpstr; /* used if -v, for printing file names */
125 int **usemeA = NULL; /* [0..nali_tot-1][0..alen] used only if --rfonly enabled, for removing gap RF columns */
126 ESL_STOPWATCH *w = NULL; /* for timing the merge, only used if -o enabled */
127 int do_small; /* TRUE if --small, operate in special small memory mode, aln seq data is not stored */
128 int do_rfonly; /* TRUE if --rfonly, output only non-gap RF columns (remove all insert columns) */
129 int *ngapA = NULL; /* [0..alen] number of insert gap columns to add after each alignment column when merging */
130 int *nmisA = NULL; /* [0..alen] number of missing data ('~') gap columns to add after each alignment column when merging */
131 int *neitherA = NULL; /* [0..apos..alen] = ngapA[apos] + nmisA[apos] */
132
133 /* output formatting, only relevant if -v */
134 char *namedashes = NULL; /* string of dashes, an underline */
135 int ni; /* counter */
136 int namewidth; /* max width of file name */
137 int tmp_len; /* current width of merged aln */
138
139 /* variables only used in small mode (--small) */
140 int ngs_cur; /* number of GS lines in current alignment (only used if do_small) */
141 int gs_exists = FALSE; /* set to TRUE if do_small and any input aln has >= 1 GS line */
142 int maxname, maxgf, maxgc, maxgr; /* max length of seqname, GF tag, GC tag, GR tag in all input alignments */
143 int maxname_cur, maxgf_cur, maxgc_cur, maxgr_cur; /* max length of seqname, GF tag, GC tag, GR tag in current input alignment */
144 int margin; /* total margin length for output msa */
145
146 /***********************************************
147 * Parse command line
148 ***********************************************/
149
150 go = esl_getopts_Create(options);
151 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK ||
152 esl_opt_VerifyConfig(go) != eslOK)
153 {
154 printf("Failed to parse command line: %s\n", go->errbuf);
155 esl_usage(stdout, argv[0], usage1);
156 esl_usage(stdout, argv[0], usage2);
157 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
158 exit(1);
159 }
160
161 if (esl_opt_GetBoolean(go, "-h") )
162 {
163 esl_banner(stdout, argv[0], banner);
164 esl_usage (stdout, argv[0], usage1);
165 esl_usage (stdout, argv[0], usage2);
166 puts("\n where options are:");
167 esl_opt_DisplayHelp(stdout, go, 1, 2, 80);
168 exit(0);
169 }
170
171 if(((! esl_opt_GetBoolean(go, "--list")) && (esl_opt_ArgNumber(go) != 2)) ||
172 (( esl_opt_GetBoolean(go, "--list")) && (esl_opt_ArgNumber(go) != 1)))
173 {
174 printf("Incorrect number of command line arguments.\n");
175 esl_usage(stdout, argv[0], usage1);
176 esl_usage(stdout, argv[0], usage2);
177 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
178 exit(1);
179 }
180
181 if(esl_opt_GetBoolean(go, "--list")) {
182 listfile = esl_opt_GetArg(go, 1);
183 }
184 else {
185 msafile1 = esl_opt_GetArg(go, 1);
186 msafile2 = esl_opt_GetArg(go, 2);
187 }
188
189 do_small = (esl_opt_IsOn(go, "--small")) ? TRUE : FALSE;
190 do_rfonly = (esl_opt_IsOn(go, "--rfonly")) ? TRUE : FALSE;
191
192 /* open output file */
193 if (esl_opt_GetString(go, "-o") != NULL) {
194 if ((ofp = fopen(esl_opt_GetString(go, "-o"), "w")) == NULL)
195 esl_fatal("Failed to open -o output file %s\n", esl_opt_GetString(go, "-o"));
196 } else ofp = stdout;
197
198 if (esl_opt_IsOn(go, "--informat")) {
199 infmt = esl_msafile_EncodeFormat(esl_opt_GetString(go, "--informat"));
200 if (infmt == eslMSAFILE_UNKNOWN) esl_fatal("%s is not a valid input sequence file format for --informat", esl_opt_GetString(go, "--informat"));
201 if (do_small && infmt != eslMSAFILE_PFAM) esl_fatal("small memory mode requires Pfam formatted alignments");
202 }
203 if (esl_opt_IsOn(go, "--outformat")) {
204 outfmt = esl_msafile_EncodeFormat(esl_opt_GetString(go, "--outformat"));
205 if (outfmt == eslMSAFILE_UNKNOWN) esl_fatal("%s is not a valid input sequence file format for --outformat", esl_opt_GetString(go, "--outformat"));
206 if (do_small && outfmt != eslMSAFILE_PFAM) esl_fatal("we can only output Pfam formatted alignments in small memory mode");
207 }
208 else outfmt = eslMSAFILE_STOCKHOLM;
209
210 if (do_small) {
211 infmt = eslMSAFILE_PFAM; /* this must be true, else we can't do small memory mode */
212 outfmt = eslMSAFILE_PFAM;
213 }
214
215 do_stall = esl_opt_GetBoolean(go, "--stall"); /* a stall point for attaching gdb */
216 while (do_stall);
217
218 /* determine file names to merge */
219 if(listfile != NULL) { /* read list file */
220 read_list_file(listfile, &alifile_list, &nalifile);
221 if(nalifile == 0) esl_fatal("Failed to read a single alignment file name from %s\n", listfile);
222 }
223 else { /* we're merging two alignment files from command-line */
224 nalifile = 2;
225 ESL_ALLOC(alifile_list, sizeof(char *) * nalifile);
226 if((status = esl_strdup(msafile1, -1, &(alifile_list[0]))) != eslOK) esl_fatal("Error storing alignment file name %s, error status: %d\n", msafile1, status);
227 if((status = esl_strdup(msafile2, -1, &(alifile_list[1]))) != eslOK) esl_fatal("Error storing alignment file name %s, error status: %d\n", msafile2, status);
228 }
229
230 /* create and start stopwatch */
231 if(ofp != stdout) {
232 w = esl_stopwatch_Create();
233 esl_stopwatch_Start(w);
234 }
235
236 if(esl_opt_GetBoolean(go, "-v")) {
237 /* determine the longest file name in alifile_list */
238 namewidth = 9; /* length of 'file name' */
239 for(fi = 0; fi < nalifile; fi++) {
240 if((status = esl_FileTail(alifile_list[fi], FALSE, &tmpstr)) != eslOK) esl_fatal("Memory allocation error.");
241 namewidth = ESL_MAX(namewidth, strlen(tmpstr));
242 free(tmpstr);
243 }
244
245 ESL_ALLOC(namedashes, sizeof(char) * (namewidth+1));
246 namedashes[namewidth] = '\0';
247 for(ni = 0; ni < namewidth; ni++) namedashes[ni] = '-';
248 fprintf(stdout, "# Reading %d alignment files...\n", nalifile);
249 fprintf(stdout, "#\n");
250 fprintf(stdout, "# %7s %-*s %7s %9s %9s %13s %8s\n", "", namewidth,"", "", "", "", "", "ncols");
251 fprintf(stdout, "# %7s %-*s %7s %9s %9s %13s %8s\n", "file #", namewidth,"file name", "ali #", "#seq/ali", "ncols/ali", "# seq total", "required");
252 fprintf(stdout, "# %7s %*s %7s %9s %9s %13s %8s\n", "-------", namewidth, namedashes, "-------", "---------", "---------", "-------------", "--------");
253 }
254
255 /* Allocate and initialize */
256 nalloc = chunksize;
257 ESL_ALLOC(msaA, sizeof(ESL_MSA *) * nalloc);
258 ESL_ALLOC(alenA, sizeof(int64_t) * nalloc);
259 if(do_rfonly) ESL_ALLOC(usemeA, sizeof(int *) * nalloc);
260
261 /* abc handling is weird. We only use alphabet to define gap characters in this miniapp.
262 * msa's are actually read in text mode. Thus eslRNA suffices for anything.
263 */
264 if (esl_opt_GetBoolean(go, "--amino")) abc = esl_alphabet_Create(eslAMINO);
265 else if (esl_opt_GetBoolean(go, "--dna")) abc = esl_alphabet_Create(eslDNA);
266 else if (esl_opt_GetBoolean(go, "--rna")) abc = esl_alphabet_Create(eslRNA);
267 else if (do_small) abc = esl_alphabet_Create(eslRNA); /* alphabet is only used to define gap characters, so (in this miniapp) we're okay specifying RNA for any alignment (even non-RNA ones) */
268 else abc = esl_alphabet_Create(eslRNA); /* ditto */
269
270 /****************************************************************
271 * Read alignments one at a time, storing them all, separately *
272 ****************************************************************/
273
274 ai = 0;
275 nseq_tot = 0;
276 maxname = maxgf = maxgc = maxgr = 0;
277 ESL_ALLOC(nali_per_file, sizeof(int) * nalifile);
278 esl_vec_ISet(nali_per_file, nalifile, 0);
279
280 for (fi = 0; fi < nalifile; fi++) {
281
282 if (do_small)
283 {
284 status = esl_msafile2_Open(alifile_list[fi], NULL, &afp2);
285 if (status == eslENOTFOUND) esl_fatal("Alignment file %s doesn't exist or is not readable\n", alifile_list[fi]);
286 else if (status != eslOK) esl_fatal("Alignment file %s open failed with error %d\n", alifile_list[fi], status);
287 }
288 else
289 {
290 status = esl_msafile_Open(NULL, alifile_list[fi], NULL, infmt, NULL, &afp);
291 if (status != eslOK) esl_msafile_OpenFailure(afp, status);
292 }
293
294
295 /* while loop: while we have an alignment in current alignment file, (statement looks weird b/c we use a different function if --small) */
296 while((status = (do_small) ?
297 esl_msafile2_ReadInfoPfam(afp2, NULL, NULL, -1, NULL,NULL, &(msaA[ai]), &nseq_cur, &alen_cur, &ngs_cur, &maxname_cur, &maxgf_cur, &maxgc_cur, &maxgr_cur, NULL, NULL, NULL, NULL, NULL) :
298 esl_msafile_Read (afp, &(msaA[ai]))) == eslOK) {
299
300 if(msaA[ai]->rf == NULL) esl_fatal("Error, all alignments must have #=GC RF annotation; alignment %d of file %d does not (%s)\n", nali_per_file[fi], (fi+1), alifile_list[fi]);
301 msaA[ai]->abc = abc; /* msa's are read in text mode, so this is (currently) only used to define gap characters, it doesn't even have to be the correct alphabet. if --small, this is set as RNA regardless of input */
302
303 if (do_small) {
304 maxname = ESL_MAX(maxname, maxname_cur);
305 maxgf = ESL_MAX(maxgf, maxgf_cur);
306 maxgc = ESL_MAX(maxgc, maxgc_cur);
307 maxgr = ESL_MAX(maxgr, maxgr_cur);
308 msaA[ai]->alen = alen_cur;
309 if(ngs_cur > 0) gs_exists = TRUE;
310 }
311 else {
312 nseq_cur = msaA[ai]->nseq;
313 }
314 alenA[ai] = msaA[ai]->alen; /* impt if --small and --rfonly, to remember total width of aln to expect in second pass */
315 nali_per_file[fi]++;
316 nseq_tot += nseq_cur;
317
318 /* reallocate per-alignment data, if nec */
319 if((ai+1) == nalloc) {
320 nalloc += chunksize;
321 ESL_RALLOC(msaA, tmp, sizeof(ESL_MSA *) * nalloc);
322 ESL_RALLOC(alenA, tmp, sizeof(int64_t) * nalloc);
323 for(ai2 = ai+1; ai2 < nalloc; ai2++) { msaA[ai2] = NULL; }
324 if(do_rfonly) {
325 ESL_RALLOC(usemeA, tmp, sizeof(int *) * nalloc);
326 for(ai2 = ai+1; ai2 < nalloc; ai2++) { usemeA[ai2] = NULL; }
327 }
328 }
329
330 /* either store consensus (non-gap RF) length (if first aln), or verify it is what we expect */
331 cur_clen = 0;
332 for(apos = 0; apos < (int) msaA[ai]->alen; apos++) {
333 if(rfchar_is_nongap_nonmissing(msaA[ai]->abc, msaA[ai]->rf[apos])) cur_clen++;
334 }
335 if(ai == 0) { /* first alignment, store clen, allocate maxgap, maxmis */
336 clen = cur_clen;
337 ESL_ALLOC(maxgap, sizeof(int) * (clen+1));
338 esl_vec_ISet(maxgap, (clen+1), 0);
339 ESL_ALLOC(maxmis, sizeof(int) * (clen+1));
340 esl_vec_ISet(maxmis, (clen+1), 0); /* these will all stay 0 unless we see '~' in the alignments */
341 }
342 else if(cur_clen != clen) {
343 esl_fatal("Error, all alignments must have identical non-gap #=GC RF lengths; expected (RF length of first ali read): %d,\nalignment %d of file %d length is %d (%s))\n", clen, nali_per_file[fi], (fi+1), cur_clen, alifile_list[fi]);
344 }
345
346
347 if(do_rfonly) {
348 /* Remove all columns that are gaps/missing data in the RF annotation, we keep an array of usemes,
349 * one per aln, in case of --small, so we know useme upon second pass of alignment files */
350 ESL_ALLOC(usemeA[ai], sizeof(int) * (msaA[ai]->alen));
351 for(apos = 0; apos < msaA[ai]->alen; apos++) { usemeA[ai][apos] = (rfchar_is_nongap_nonmissing(abc, msaA[ai]->rf[apos])) ? TRUE : FALSE; }
352 if((status = esl_msa_ColumnSubset(msaA[ai], errbuf, usemeA[ai])) != eslOK) {
353 esl_fatal("status code: %d removing gap RF columns for msa %d from file %s:\n%s", status, (ai+1), alifile_list[fi], errbuf);
354 }
355 }
356 else { /* --rfonly not enabled, determine max number inserts between each position */
357 /* update_maxgap_and_maxmis checks to make sure the msa follows our rule for missing data and gaps:
358 * for all positions x..y b/t any two adjacent nongap RF positions (x-1 and y+1 are nongap RF positions)
359 * all missing data columns '~' must come before all gap positions ('.', '-', or '_')
360 */
361 if((status = update_maxgap_and_maxmis(msaA[ai], errbuf, clen, msaA[ai]->alen, maxgap, maxmis)) != eslOK) esl_fatal(errbuf);
362 }
363 if(esl_opt_GetBoolean(go, "-v")) {
364 if((status = esl_FileTail(alifile_list[fi], FALSE, &tmpstr)) != eslOK) esl_fatal("Memory allocation error.");
365 tmp_len = clen + esl_vec_ISum(maxgap, clen+1) + esl_vec_ISum(maxmis, clen+1);
366 fprintf(stdout, " %7d %-*s %7d %9d %9" PRId64 " %13d %8d\n", (fi+1), namewidth, tmpstr, (ai+1), nseq_cur, msaA[ai]->alen, nseq_tot, tmp_len);
367 free(tmpstr);
368 }
369 ai++;
370 } /* end of while esl_msafile_Read() loop */
371
372 if (do_small)
373 {
374 if (status == eslEFORMAT) esl_fatal("Alignment file %s, parse error:\n%s\n", alifile_list[fi], afp2->errbuf);
375 else if (status == eslEINVAL) esl_fatal("Alignment file %s, parse error:\n%s\n", alifile_list[fi], afp2->errbuf);
376 else if (status != eslEOF) esl_fatal("Alignment file %s, read failed with error code %d\n", alifile_list[fi], status);
377 }
378 else if (status != eslEOF) esl_msafile_ReadFailure(afp, status);
379
380 if(nali_per_file[fi] == 0) esl_fatal("Failed to read any alignments from file %s\n", alifile_list[fi]);
381
382 if (do_small) esl_msafile2_Close(afp2);
383 else esl_msafile_Close(afp);
384 } /* end of for (fi=0; fi < nalifile; fi++) */
385 nali_tot = ai;
386
387 /*********************************************
388 * Merge all alignments into the merged MSA *
389 *********************************************/
390
391 /* We allocate space for all sequences, but leave sequences as NULL (nseq = -1).
392 * If (do_small) we didn't store the sequences on the first pass through the
393 * alignment files, and we'll never allocate space for the sequences in mmsa,
394 * we'll just output them as we reread them on another pass through the
395 * individual alignments. If we read >= 1 GS line in any of the input alignments,
396 * we need to do an additional pass through the files, outputting only GS
397 * data. Then, in a final (3rd) pass we'll output aligned data.
398 *
399 * if (!do_small), we have the sequences in memory, we'll copy these
400 * to the merged alignment, freeing them in the orignal msaA alignments
401 * as we go so we never need to allocate the full mmsa while we still have
402 * the individual msas (in msaA[]) in memory.
403 */
404 mmsa = esl_msa_Create(nseq_tot, -1);
405 alen_mmsa = clen + esl_vec_ISum(maxgap, clen+1) + esl_vec_ISum(maxmis, clen+1);
406
407 /* Determine what annotation from the input alignments
408 * we will include in the merged MSA.
409 * See comments in header of validate_and_copy_msa_annotation()
410 * for rules on what we include.
411 */
412 if((status = validate_and_copy_msa_annotation(go, outfmt, mmsa, msaA, nali_tot, clen, alen_mmsa, maxgap, maxmis, errbuf)) != eslOK)
413 esl_fatal("Error while checking and copying individual MSA annotation to merged MSA:%s\n", errbuf);
414
415 if (do_small) {
416 /* Small memory mode, do up to 2 more passes through the input alignments:
417 * pass 2 will output only GS data at top of output alignment file (only performed if >= 1 GS line read in input files
418 * pass 3 will output all aligned data at to output alignment file (always performed)
419 */
420
421 /* output header, comments, and #=GF data */
422 write_pfam_msa_top(ofp, mmsa);
423
424 if(ofp != stdout) {
425 if(esl_opt_GetBoolean(go, "-v")) { fprintf(stdout, "#\n"); }
426 fprintf(stdout, "# Outputting merged alignment to file %s ... ", esl_opt_GetString(go, "-o"));
427 fflush(stdout);
428 }
429
430 /* if there was any GS annotation in any of the individual alignments,
431 * do second pass through alignment files, outputting GS annotation as we go. */
432 if(gs_exists) {
433 ai = 0;
434 for(fi = 0; fi < nalifile; fi++) {
435 /* we're in small memory mode... */
436 status = esl_msafile2_Open(alifile_list[fi], NULL, &afp2); /* this should work b/c it did on the first pass */
437 if (status == eslENOTFOUND) esl_fatal("Second pass, alignment file %s doesn't exist or is not readable\n", alifile_list[fi]);
438 else if (status == eslEFORMAT) esl_fatal("Second pass, couldn't determine format of alignment %s\n", alifile_list[fi]);
439 else if (status != eslOK) esl_fatal("Second pass, alignment file %s open failed with error %d\n", alifile_list[fi], status);
440
441 for(ai2 = 0; ai2 < nali_per_file[fi]; ai2++) {
442 status = esl_msafile2_RegurgitatePfam(afp2, ofp,
443 maxname, maxgf, maxgc, maxgr, /* max width of a seq name, gf tag, gc tag, gr tag (irrelevant here) */
444 FALSE, /* regurgitate stockholm header ? */
445 FALSE, /* regurgitate // trailer ? */
446 FALSE, /* regurgitate blank lines */
447 FALSE, /* regurgitate comments */
448 FALSE, /* regurgitate GF ? */
449 TRUE, /* regurgitate GS ? */
450 FALSE, /* regurgitate GC ? */
451 FALSE, /* regurgitate GR ? */
452 FALSE, /* regurgitate aseq ? */
453 NULL, /* regurgitate all seqs, not a subset */
454 NULL, /* regurgitate all seqs, not a subset */
455 NULL, /* we're not keeping a subset of columns */
456 NULL, /* we're not adding all gap columns */
457 alenA[ai], /* alignment length, as we read it in first pass (inserts may have been removed since then) */
458 '.', /* gap char, irrelevant */
459 NULL, /* don't return num seqs read */
460 NULL); /* don't return num seqs regurgitated */
461 if(status == eslEOF) esl_fatal("Second pass, error out of alignments too soon, when trying to read aln %d of file %s", ai2, alifile_list[fi]);
462 if(status != eslOK) esl_fatal("Second pass, error reading alignment %d of file %s: %s", ai2, alifile_list[fi], afp2->errbuf);
463 ai++;
464 fflush(ofp);
465 }
466 esl_msafile2_Close(afp2);
467 }
468 fprintf(ofp, "\n"); /* a single blank line to separate GS annotation from aligned data */
469 }
470
471 /* do another (either second or third) pass through alignment files, outputting aligned sequence data (and GR) as we go */
472 ai = 0;
473 for(fi = 0; fi < nalifile; fi++) {
474 status = esl_msafile2_Open(alifile_list[fi], NULL, &afp2); /* this should work b/c it did on the first pass */
475 if (status == eslENOTFOUND) esl_fatal("Second pass, alignment file %s doesn't exist or is not readable\n", alifile_list[fi]);
476 else if (status == eslEFORMAT) esl_fatal("Second pass, couldn't determine format of alignment %s\n", alifile_list[fi]);
477 else if (status != eslOK) esl_fatal("Second pass, alignment file %s open failed with error %d\n", alifile_list[fi], status);
478
479 for(ai2 = 0; ai2 < nali_per_file[fi]; ai2++) {
480 /* determine how many all gap columns to insert after each alignment position
481 * of the child msa when copying it to the merged msa */
482 if(! do_rfonly) {
483 if((status = determine_gap_columns_to_add(msaA[ai], maxgap, maxmis, clen, &(ngapA), &(nmisA), &(neitherA), errbuf)) != eslOK)
484 esl_fatal("error determining number of all gap columns to add to alignment %d of file %s", ai2, alifile_list[fi]);
485 }
486 status = esl_msafile2_RegurgitatePfam(afp2, ofp,
487 maxname, maxgf, maxgc, maxgr, /* max width of a seq name, gf tag, gc tag, gr tag */
488 FALSE, /* regurgitate stockholm header ? */
489 FALSE, /* regurgitate // trailer ? */
490 FALSE, /* regurgitate blank lines */
491 FALSE, /* regurgitate comments */
492 FALSE, /* regurgitate GF ? */
493 FALSE, /* regurgitate GS ? */
494 FALSE, /* regurgitate GC ? */
495 TRUE, /* regurgitate GR ? */
496 TRUE, /* regurgitate aseq ? */
497 NULL, /* regurgitate all seqs, not a subset */
498 NULL, /* regurgitate all seqs, not a subset */
499 (do_rfonly) ? usemeA[ai] : NULL,
500 (do_rfonly) ? NULL : neitherA,
501 alenA[ai], /* alignment length, as we read it in first pass (inserts may have been removed since then) */
502 '.',
503 NULL, /* don't return num seqs read */
504 NULL); /* don't return num seqs regurgitated */
505
506 if(status == eslEOF) esl_fatal("Second pass, error out of alignments too soon, when trying to read aln %d of file %s", ai2, alifile_list[fi]);
507 if(status != eslOK) esl_fatal("Second pass, error reading alignment %d of file %s: %s", ai2, alifile_list[fi], afp2->errbuf);
508 free(ngapA);
509 free(nmisA);
510 free(neitherA);
511 esl_msa_Destroy(msaA[ai]);
512 msaA[ai] = NULL;
513 ai++;
514 fflush(ofp);
515 }
516 esl_msafile2_Close(afp2);
517 }
518 /* finally, write GC annotation and '//' line */
519 margin = maxname + 1;
520 if (maxgc > 0 && maxgc+6 > margin) margin = maxgc+6;
521 if (maxgr > 0 && maxname+maxgr+7 > margin) margin = maxname+maxgr+7;
522 write_pfam_msa_gc(ofp, mmsa, margin);
523 } /* end of if(do_small) */
524
525 else { /* ! do_small: for each input alignment in msaA[], add all aligned data to mmsa, then free it */
526 if(esl_opt_GetBoolean(go, "-v")) { fprintf(stdout, "#\n# Merging alignments ... "); fflush(stdout); }
527 for(ai = 0; ai < nali_tot; ai++) {
528 if((status = add_msa(mmsa, errbuf, msaA[ai], maxgap, maxmis, clen, alen_mmsa)) != eslOK)
529 esl_fatal("Error, merging alignment %d of %d:\n%s.", (ai+1), nali_tot, errbuf);
530 esl_msa_Destroy(msaA[ai]); /* note: the aligned sequences will have already been freed in add_msa() */
531 msaA[ai] = NULL;
532 }
533 if(esl_opt_GetBoolean(go, "-v")) { fprintf(stdout, "done.\n#\n"); fflush(stdout); }
534 mmsa->alen = alen_mmsa; /* it was -1, b/c we filled in each seq as we marched through each msaA[] alignment */
535 if(ofp != stdout) { fprintf(stdout, "# Saving alignment to file %s ... ", esl_opt_GetString(go, "-o")); }
536 status = esl_msafile_Write(ofp, mmsa, outfmt);
537 if(status != eslOK) esl_fatal("Error, during alignment output; status code: %d\n", status);
538 }
539 if(ofp != stdout) {
540 fflush(stdout);
541 fclose(ofp);
542 fprintf(stdout, "done\n#\n");
543 fflush(stdout);
544 esl_stopwatch_Stop(w);
545 esl_stopwatch_Display(stdout, w, "# CPU time: ");
546 }
547
548 /* clean up and exit */
549 if(alifile_list != NULL) {
550 for(fi = 0; fi < nalifile; fi++) {
551 if(alifile_list[fi] != NULL) free(alifile_list[fi]);
552 }
553 free(alifile_list);
554 }
555 if(usemeA != NULL) {
556 for(ai = 0; ai < nali_tot; ai++) {
557 free(usemeA[ai]);
558 }
559 free(usemeA);
560 }
561
562 if(nali_per_file != NULL) free(nali_per_file);
563 if(alenA != NULL) free(alenA);
564 if(namedashes != NULL) free(namedashes);
565 if(msaA != NULL) free(msaA);
566 if(maxgap != NULL) free(maxgap);
567 if(maxmis != NULL) free(maxmis);
568 if(mmsa != NULL) esl_msa_Destroy(mmsa);
569 if(abc != NULL) esl_alphabet_Destroy(abc);
570 if(w != NULL) esl_stopwatch_Destroy(w);
571 esl_getopts_Destroy(go);
572 return 0;
573
574 ERROR:
575 esl_fatal("Out of memory. Reformat to Pfam with esl-reformat and try esl-alimerge --small.");
576 return eslEMEM; /*NEVERREACHED*/
577 }
578
579 /* Function: read_list_file
580 * Date: EPN, Fri Nov 20 16:41:32 2009
581 *
582 * Read a file listing alignment files to merge.
583 * Store file names in *ret_alifile_list and return it,
584 * return number of files in ret_nalifile and return it.
585 * Each white-space delimited token is considered a
586 * different alignment name.
587 *
588 * Dies if we encounter an error.
589 *
590 * Returns: void.
591 */
592 void
593 read_list_file(char *listfile, char ***ret_alifile_list, int *ret_nalifile)
594 {
595 int status;
596 ESL_FILEPARSER *efp;
597 char *tok;
598 int nalloc = 10;
599 int chunksize = 10;
600 char **alifile_list = NULL;
601 int n = 0;
602 void *tmp;
603
604 ESL_ALLOC(alifile_list, sizeof(char *) * nalloc);
605 status = esl_fileparser_Open(listfile, NULL, &efp);
606 if (status == eslENOTFOUND) esl_fatal("List file %s does not exist or is not readable\n", listfile);
607 else if(status == eslEMEM) esl_fatal("Ran out of memory when opening list file %s\n", listfile);
608 else if(status != eslOK) esl_fatal("Error opening list file %s\n", listfile);
609
610 esl_fileparser_SetCommentChar(efp, '#');
611 while((status = esl_fileparser_GetToken(efp, &tok, NULL)) != eslEOF) {
612 if(n == nalloc) { nalloc += chunksize; ESL_RALLOC(alifile_list, tmp, sizeof(char *) * nalloc); }
613 if((status = esl_strdup(tok, -1, &(alifile_list[n++]))) != eslOK) {
614 esl_fatal("Error storing alignment file name while reading list file %s, error status: %d\n", listfile, status);
615 }
616 }
617 esl_fileparser_Close(efp);
618 *ret_alifile_list = alifile_list;
619 *ret_nalifile = n;
620
621 return;
622
623 ERROR:
624 esl_fatal("Out of memory.");
625 return; /*NOTREACHED*/
626 }
627
628
629 /* Function: update_maxgap_and_maxmis
630 * Date: EPN, Sun Nov 22 09:40:48 2009
631 * (stolen from Infernal's cmalign.c)
632 *
633 * Update maxgap[] and maxmis[], arrays that keeps track of the max
634 * number of gap columns and missing data columns ('~' gap #=GC RF)
635 * columns before each cpos (consensus (non-gap #=GC RF) column)
636 *
637 * We require that all missing columns between cpos x and x+1 occur before
638 * all gap columns between cpos x and x+1.
639 *
640 * Consensus columns are index [0..cpos..clen].
641 *
642 * max{gap,mis}[0] is number of gap/missing columns before 1st cpos.
643 * max{gap,mis}[clen-1] is number of gap/missing columns before final cpos.
644 * max{gap,mis}[clen] is number of gap/missing columns after final cpos.
645 *
646 * Returns: eslOK on success.
647 * eslEINVAL if msa->rf is NULL, nongap RF length is not clen of
648 * for any two cpos x and x+1, a gap column precedes a
649 * missing data column.
650 *
651 */
652 int
653 update_maxgap_and_maxmis(ESL_MSA *msa, char *errbuf, int clen, int64_t alen, int *maxgap, int *maxmis)
654 {
655 int apos;
656 int cpos = 0;
657 int ngap = 0;
658 int nmis = 0;
659
660 for(apos = 0; apos < alen; apos++) {
661 if(esl_abc_CIsGap(msa->abc, msa->rf[apos])) {
662 ngap++;
663 }
664 else if (esl_abc_CIsMissing(msa->abc, msa->rf[apos])) {
665 nmis++;
666 if(ngap > 0) ESL_FAIL(eslEINVAL, errbuf, "after nongap RF pos %d, %d gap columns precede a missing data column (none should)", cpos, ngap);
667 }
668 else {
669 maxgap[cpos] = ESL_MAX(maxgap[cpos], ngap);
670 maxmis[cpos] = ESL_MAX(maxmis[cpos], nmis);
671 cpos++;
672 ngap = 0;
673 nmis = 0;
674 }
675 }
676
677 /* update final value, max{ins,el}[clen+1], the number of inserts
678 * after the final consensus position */
679 maxgap[cpos] = ESL_MAX(maxgap[cpos], ngap);
680 maxmis[cpos] = ESL_MAX(maxmis[cpos], nmis);
681 if(cpos != clen) ESL_FAIL(eslEINVAL, errbuf, "second pass expected clen (%d) not equal to actual clen (%d).\n", clen, cpos);
682
683 return eslOK;
684 }
685
686 /* Function: validate_and_copy_msa_annotation
687 * Date: EPN, Tue Nov 24 05:35:50 2009
688 *
689 * Decide what individual MSA annotation from
690 * the input alignments in msaA[], if any, will be
691 * included in the merged alignment (mmsa) and
692 * add that info to it.
693 *
694 * Rules for what to include in mmsa:
695 *
696 * We include name,desc,acc,author annotation in merged alignment
697 * if it is identical in all msaA[] input alignments.
698 *
699 * We include comments and per-file (GF) annotation if they
700 * are present and identical in all input msaA[] alignments.
701 *
702 * We include per-column (GC) annotation if it is present and
703 * identical *with-respect-to* #=GC RF annotation AND all
704 * the annotation in gap #=GC RF columns in all msaA[] are gaps
705 * ('.'). This also pertains to the following parsed per-column
706 * annotation: ss_cons, sa_cons, pp_cons, and rf. With rf,
707 * de-gapped rf annotation must be identical in all input
708 * alignments period, if it is not we'll die with an error message.
709 *
710 * Per-sequence information and per-residue information is always
711 * included in merged alignment. This is done by add_msa() function.
712 *
713 * Returns: eslOK on success.
714 * eslEINCONCEIVABLE if input/msa is corrupt in some way (example: ngf>0 but gf_tag[0] == NULL)
715 * eslEMEM on memory error
716 * if !eslOK, errbuf is filled before return
717 */
718 int
719 validate_and_copy_msa_annotation(const ESL_GETOPTS *go, int outfmt, ESL_MSA *mmsa, ESL_MSA **msaA, int nmsa, int clen, int alen_merged, int *maxgap, int *maxmis, char *errbuf)
720 {
721 int status;
722 int j; /* counter over alignment annotations */
723 int j2; /* counter over alignment annotations */
724 int ai; /* counter over alignments */
725 char *dealigned = NULL; /* a temporary, dealigned string */
726 char *dealigned2 = NULL; /* another temporary, dealigned string */
727 char *gapped_out = NULL; /* a temporary string with gaps added to fit into merged aln */
728 int *ngapA = NULL; /* [0..alen] number of insert gap columns to add after each alignment column when merging */
729 int *nmisA = NULL; /* [0..alen] number of missing data ('~') gap columns to add after each alignment column when merging */
730 int *neitherA = NULL; /* [0..apos..alen] = ngapA[apos] + nmisA[apos] */
731 int do_add;
732 int found_tag;
733 int be_verbose = FALSE;
734
735 /* we only print info about annotation if -v AND we'll actually
736 * output it (as stockholm or pfam)
737 * (we actually don't even need to be in this function if we're
738 * not output in stockholm or pfam...)
739 */
740 if((esl_opt_GetBoolean(go, "-v")) &&
741 (outfmt == eslMSAFILE_STOCKHOLM || outfmt == eslMSAFILE_PFAM))
742 { be_verbose = TRUE; }
743
744 if(be_verbose) fprintf(stdout, "#\n");
745
746 if(nmsa == 0) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "in validate_and_copy_msa_annotation(): zero child alignments.");
747
748 /* First, determine how many all gap columns to insert after each alignment position
749 * of the first child msa, so we can (possibly) gap out GC,SS_cons,SA_cons,PP_cons annotation
750 * to appropriate length when adding it to the merged MSA. */
751 if((status = determine_gap_columns_to_add(msaA[0], maxgap, maxmis, clen, &(ngapA), &(nmisA), &(neitherA), errbuf)) != eslOK)
752 return status;
753
754 /* Note: esl_strcmp() can handle NULL strings (they are not identical to non-NULL strings) */
755
756 /*********************************************************************/
757 /* Check if name annotation is identical in all alignments */
758 do_add = TRUE; /* until proven otherwise */
759 if(msaA[0]->name != NULL) {
760 for(ai = 1; ai < nmsa; ai++) {
761 if(esl_strcmp(msaA[0]->name, msaA[ai]->name) != 0) { do_add = FALSE; break; }
762 }
763 if(do_add) {
764 if(be_verbose) fprintf(stdout, "# Identical name annotation from all alignments transferred to merged alignment.\n");
765 if((status = esl_strdup(msaA[0]->name, -1, &(mmsa->name))) != eslOK) goto ERROR;
766 }
767 else if(be_verbose) fprintf(stdout, "# Name annotation is not identical in all alignments; not included in merged alignment.\n");
768 }
769 else if(be_verbose) fprintf(stdout, "# Name annotation absent from (at least) first alignment; not included in merged alignment.\n");
770
771 /*********************************************************************/
772 /* Check if description annotation is identical in all alignments */
773 do_add = TRUE; /* until proven otherwise */
774 if(msaA[0]->desc != NULL) {
775 for(ai = 1; ai < nmsa; ai++) {
776 if(esl_strcmp(msaA[0]->desc, msaA[ai]->desc) != 0) { do_add = FALSE; break; }
777 }
778 if(do_add) {
779 if(be_verbose) fprintf(stdout, "# Identical description annotation from all alignments transferred to merged alignment.\n");
780 if((status = esl_strdup(msaA[0]->desc, -1, &(mmsa->desc))) != eslOK) goto ERROR;
781 }
782 else if(be_verbose) fprintf(stdout, "# Description annotation is not identical in all alignments; not included in merged alignment.\n");
783 }
784 else if(be_verbose) fprintf(stdout, "# Description annotation absent from (at least) first alignment; not included in merged alignment.\n");
785
786 /*********************************************************************/
787 /* Check if accession annotation is identical in all alignments */
788 do_add = TRUE; /* until proven otherwise */
789 if(msaA[0]->acc != NULL) {
790 for(ai = 1; ai < nmsa; ai++) {
791 if(esl_strcmp(msaA[0]->acc, msaA[ai]->acc) != 0) { do_add = FALSE; break; }
792 }
793 if(do_add) {
794 if(be_verbose) fprintf(stdout, "# Identical accession annotation from all alignments transferred to merged alignment.\n");
795 if((status = esl_strdup(msaA[0]->acc, -1, &(mmsa->acc))) != eslOK) goto ERROR;
796 }
797 else if(be_verbose) fprintf(stdout, "# Accession annotation is not identical in all alignments; not included in merged alignment.\n");
798 }
799 else if(be_verbose) fprintf(stdout, "# Accession annotation absent from (at least) first alignment; not included in merged alignment.\n");
800
801 /*********************************************************************/
802 /* Check if author annotation is identical in all alignments */
803 do_add = TRUE; /* until proven otherwise */
804 if(msaA[0]->au != NULL) {
805 for(ai = 1; ai < nmsa; ai++) {
806 if(esl_strcmp(msaA[0]->au, msaA[ai]->au) != 0) { do_add = FALSE; break; }
807 }
808 if(do_add) {
809 if(be_verbose) fprintf(stdout, "# Identical author annotation from all alignments transferred to merged alignment.\n");
810 if((status = esl_strdup(msaA[0]->au, -1, &(mmsa->au))) != eslOK) goto ERROR;
811 }
812 else if(be_verbose) fprintf(stdout, "# Author annotation is not identical in all alignments; not included in merged alignment.\n");
813 }
814 else if(be_verbose) fprintf(stdout, "# Author annotation absent from (at least) first alignment; not included in merged alignment.\n");
815
816 /*********************************************************************/
817 /* Check per-file (GF) annotation, must be present and identical in all msaA[] alignments to be included */
818 if(msaA[0]->ngf > 0) {
819 for(j = 0; j < msaA[0]->ngf; j++) {
820 do_add = TRUE; /* until proven otherwise */
821 /* verify that what we think is true is true */
822 if(msaA[0]->gf_tag[j] == NULL) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "unexpectedly, GF tag %d of msaA[0] is NULL, but msaA[0]->ngf is %d.\n", j, msaA[0]->ngf);
823 if(msaA[0]->gf[j] == NULL) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "unexpectedly, GF annotation %d of msaA[0] is NULL, but msaA[0]->ngf is %d.\n", j, msaA[0]->ngf);
824 for(ai = 1; ai < nmsa; ai++) {
825 found_tag = FALSE;
826 for(j2 = 0; j2 < msaA[ai]->ngf; j2++) {
827 if(esl_strcmp(msaA[0]->gf_tag[j], msaA[ai]->gf_tag[j2]) == 0) {
828 found_tag = TRUE;
829 if(esl_strcmp(msaA[0]->gf[j], msaA[ai]->gf_tag[j2]) != 0) {
830 do_add = FALSE;
831 }
832 break; /* if we found a match, do_add remains TRUE */
833 }
834 }
835 if(found_tag && do_add) {
836 if(be_verbose) fprintf(stdout, "# Identical GF tag %s annotation from all alignments transferred to merged alignment.\n", msaA[0]->gf_tag[j]);
837 if((status = esl_msa_AddGF(mmsa, msaA[0]->gf_tag[j], -1, msaA[0]->gf[j], -1)) != eslOK) goto ERROR;
838 }
839 else {
840 if(be_verbose) fprintf(stdout, "# GF tag %s annotation from first alignment absent from >= 1 other alignments; not included in merged alignment.\n", msaA[0]->gf_tag[j]);
841 }
842 }
843 }
844 }
845 else if(be_verbose) fprintf(stdout, "# Unparsed GF annotation absent from (at least) first alignment; not included in merged alignment.\n");
846
847 /*********************************************************************/
848 /* Check comments, all must be identically ordered and identical in all msaA[] aligments to include them */
849 if(msaA[0]->ncomment > 0) {
850 do_add = TRUE; /* until proven otherwise */
851 /* make sure all alignments have same number of comments */
852 for(ai = 1; ai < nmsa; ai++) {
853 if(msaA[ai]->ncomment != msaA[0]->ncomment) {
854 do_add = FALSE;
855 break;
856 }
857 }
858 if(do_add) {
859 /* make sure all alignments have identical comments */
860 for(j = 0; j < msaA[0]->ncomment; j++) {
861 /* verify that what we think is true is true */
862 if(msaA[0]->comment[j] == NULL) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "unexpectedly, comment %d of msaA[0] is NULL, but msaA[0]->ncomment is %d.\n", j, msaA[0]->ncomment);
863 for(ai = 1; ai < nmsa; ai++) {
864 if(esl_strcmp(msaA[0]->comment[j], msaA[ai]->comment[j]) != 0) { /* comment doesn't match */
865 do_add = FALSE;
866 break;
867 }
868 }
869 }
870 }
871 if(do_add) {
872 for(j = 0; j < msaA[0]->ncomment; j++) {
873 if((status = esl_msa_AddComment(mmsa, msaA[0]->comment[j], -1))!= eslOK) goto ERROR;
874 }
875 if(be_verbose) fprintf(stdout, "# All alignments have identical comments in the same order. These were transferred to merged alignment.\n");
876 }
877 else {
878 if(be_verbose) fprintf(stdout, "# Comments are not identical in all alignments; not included in merged alignment.\n");
879 }
880 }
881 else if(be_verbose) fprintf(stdout, "# No comments in (at least) first alignment; not included in merged alignment.\n");
882
883 /*********************************************************************/
884 /* Check unparsed per-column (GC) annotation, it must include all gaps in gap RF columns and
885 * be identical once gap RF columns are removed in all msaA[] alignments to be included. */
886
887 if(msaA[0]->ngc > 0) {
888 for(j = 0; j < msaA[0]->ngc; j++) {
889 do_add = TRUE; /* until proven otherwise */
890 /* verify that what we think is true is true */
891 if(msaA[0]->gc_tag[j] == NULL) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "unexpectedly, GC tag %d of msaA[0] is NULL, but msaA[0]->ngf is %d.\n", j, msaA[0]->ngc);
892 if(msaA[0]->gc[j] == NULL) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "unexpectedly, GC annotation %d of msaA[0] is NULL, but msaA[0]->ngf is %d.\n", j, msaA[0]->ngc);
893
894 /* ensure it does not have non-gaps in gap RF columns */
895 if(validate_no_nongaps_in_rf_gaps(msaA[0]->abc, msaA[0]->rf, msaA[0]->gc[j], msaA[0]->alen)) { /* returns TRUE if gc[j] has 0 non-gap characters in gap columns of RF annotation */
896 /* dealign gc line */
897 if((status = esl_strdup(msaA[0]->gc[j], msaA[0]->alen, &(dealigned))) != eslOK) goto ERROR;
898 if((status = esl_strdealign(dealigned, msaA[0]->rf, "-_.~", NULL)) != eslOK) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "unexpected error dealigning GC tag %s of msaA[0]", msaA[0]->gc_tag[j]);
899
900 for(ai = 1; ai < nmsa; ai++) {
901 found_tag = FALSE;
902 for(j2 = 0; j2 < msaA[ai]->ngc; j2++) {
903 if(esl_strcmp(msaA[0]->gc_tag[j], msaA[ai]->gc_tag[j2]) == 0) {
904 found_tag = TRUE;
905 /* ensure it does not have non-gaps in gap RF columns */
906 if(validate_no_nongaps_in_rf_gaps(msaA[ai]->abc, msaA[ai]->rf, msaA[ai]->gc[j2], msaA[ai]->alen)) { /* returns TRUE if gc[j2] has 0 non-gap characters in gap columns of RF annotation */
907 /* dealign */
908 if((status = esl_strdup(msaA[ai]->gc[j2], msaA[ai]->alen, &(dealigned2))) != eslOK) goto ERROR;
909 if((status = esl_strdealign(dealigned2, msaA[ai]->rf, "-_.~", NULL)) != eslOK) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "unexpected error dealigning GC tag %s of msaA[%d]", msaA[ai]->gc_tag[j2], ai);
910 /* check identity */
911 if(esl_strcmp(dealigned, dealigned2) != 0) { do_add = FALSE; }
912 free(dealigned2);
913 dealigned2 = NULL;
914 break; /* if we matched, do_add remains TRUE */
915 } /* end of 'if(validate_no_nongaps_in_rf_gaps(msaA[ai]->abc...)' */
916 else {
917 do_add = FALSE;
918 }
919 }
920 }
921 } /* end of (for(ai = 1...)) */
922 if(dealigned != NULL) { free(dealigned); dealigned = NULL; }
923 if(found_tag && do_add) {
924 /* gap out the the GC annotation to fit in merged alignment */
925 if((status = inflate_string_with_gaps_and_missing(msaA[0]->gc[j], msaA[0]->alen, alen_merged, neitherA, '.', NULL, '~', &(gapped_out))) != eslOK) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "Error adding gaps to create GC tag %s annotation for merged alignment.", msaA[0]->gc_tag[j]);
926 if((status = esl_msa_AppendGC(mmsa, msaA[0]->gc_tag[j], gapped_out)) != eslOK) goto ERROR;
927 free(gapped_out);
928 gapped_out = NULL;
929 if(be_verbose) fprintf(stdout, "# Identical GC tag %s annotation from all alignments transferred to merged alignment.\n", msaA[0]->gc_tag[j]);
930 }
931 else {
932 if(be_verbose) fprintf(stdout, "# GC tag %s annotation from first alignment absent from or different in >= 1 other alignments; not included in merged alignment.\n", msaA[0]->gc_tag[j]);
933 }
934 } /* end of 'if(validate_no_nongaps_in_rf_gaps(msaA[0]->abc' */
935 } /* end of for(j = 0 j < msaA[0]->ngc... */
936 } /* end of if(msaA[0]->ngc > 0) */
937 else if(be_verbose) fprintf(stdout, "# Unparsed GC annotation absent from (at least) first alignment; not included in merged alignment.\n");
938
939 /*********************************************************************/
940 /* Check ss_cons: it must include all gaps in gap RF columns and be identical once gap RF columns are removed in all
941 * msaA[] alignments to be included. (Same requirements as unparsed GC annotation, so code block below is analogous to one above). */
942 if(msaA[0]->ss_cons != NULL) {
943 do_add = TRUE; /* until proven otherwise */
944 /* ensure it does not have non-gaps in gap RF columns */
945 if(validate_no_nongaps_in_rf_gaps(msaA[0]->abc, msaA[0]->rf, msaA[0]->ss_cons, msaA[0]->alen)) { /* returns TRUE if ss_cons has 0 non-gap characters in gap columns of RF annotation */
946 /* dealign ss_cons */
947 if((status = esl_strdup(msaA[0]->ss_cons, msaA[0]->alen, &(dealigned))) != eslOK) goto ERROR;
948 if((status = esl_strdealign(dealigned, msaA[0]->rf, "-_.~", NULL)) != eslOK) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "unexpected error dealigning ss_cons of msaA[0]");
949 for(ai = 1; ai < nmsa; ai++) {
950 if(msaA[ai]->ss_cons == NULL) {
951 do_add = FALSE;
952 break;
953 }
954 /* ss_cons != NULL, ensure it does not have non-gaps in gap RF columns */
955 if(validate_no_nongaps_in_rf_gaps(msaA[ai]->abc, msaA[ai]->rf, msaA[ai]->ss_cons, msaA[ai]->alen)) { /* returns TRUE if ss_cons has 0 non-gap characters in gap columns of RF annotation */
956 /* dealign */
957 if((status = esl_strdup(msaA[ai]->ss_cons, msaA[ai]->alen, &(dealigned2))) != eslOK) goto ERROR;
958 if((status = esl_strdealign(dealigned2, msaA[ai]->rf, "-_.~", NULL)) != eslOK) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "unexpected error dealigning ss_cons of msaA[%d]", ai);
959 /* check identity */
960 if(esl_strcmp(dealigned, dealigned2) != 0) { do_add = FALSE; }
961 free(dealigned2);
962 dealigned2 = NULL;
963 break; /* if we matched, do_add remains TRUE */
964 } /* end of 'if(validate_no_nongaps_in_rf_gaps(msaA[ai]->abc...)' */
965 else {
966 do_add = FALSE;
967 }
968 } /* end of (for(ai = 1...)) */
969 if(dealigned != NULL) { free(dealigned); dealigned = NULL; }
970 if(do_add) {
971 /* gap out the the ss_cons to fit in merged alignment,
972 * as a special case, we use '~' in SS_cons as we add missing data and gaps
973 * like we do in RF (for all other annotation we only add gaps) */
974 if((status = inflate_string_with_gaps_and_missing(msaA[0]->ss_cons, msaA[0]->alen, alen_merged, ngapA, '.', nmisA, '~', &(gapped_out))) != eslOK) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "Error adding gaps to create SS_cons annotation for merged alignment.");
975 if(mmsa->ss_cons != NULL) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "Error adding SS_cons to merged alignment, it is already non-NULL.");
976 if((status = esl_strdup(gapped_out, alen_merged, &(mmsa->ss_cons))) != eslOK) goto ERROR;
977 free(gapped_out);
978 gapped_out = NULL;
979 if(be_verbose) fprintf(stdout, "# Identical SS_cons annotation from all alignments transferred to merged alignment.\n");
980 }
981 else {
982 if(be_verbose) fprintf(stdout, "# SS_cons annotation from first alignment absent from or different in >= 1 other alignments; not included in merged alignment.\n");
983 }
984 } /* end of 'if(validate_no_nongaps_in_rf_gaps(msaA[0]->abc' */
985 } /* end of if(msaA[0]->ss_cons != NULL) */
986 else if(be_verbose) fprintf(stdout, "# SS_cons annotation absent from (at least) first alignment; not included in merged alignment.\n");
987
988 /*********************************************************************/
989 /* Check sa_cons: it must include all gaps in gap RF columns and be identical once gap RF columns are removed in all
990 * msaA[] alignments to be included. (Same requirements as unparsed GC annotation, so code block below is analogous to one above). */
991 if(msaA[0]->sa_cons != NULL) {
992 do_add = TRUE; /* until proven otherwise */
993 /* ensure it does not have non-gaps in gap RF columns */
994 if(validate_no_nongaps_in_rf_gaps(msaA[0]->abc, msaA[0]->rf, msaA[0]->sa_cons, msaA[0]->alen)) { /* returns TRUE if sa_cons has 0 non-gap characters in gap columns of RF annotation */
995 /* dealign sa_cons */
996 if((status = esl_strdup(msaA[0]->sa_cons, msaA[0]->alen, &(dealigned))) != eslOK) goto ERROR;
997 if((status = esl_strdealign(dealigned, msaA[0]->rf, "-_.~", NULL)) != eslOK) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "unexpected error dealigning sa_cons of msaA[0]");
998 for(ai = 1; ai < nmsa; ai++) {
999 if(msaA[ai]->sa_cons == NULL) {
1000 do_add = FALSE;
1001 break;
1002 }
1003 /* sa_cons != NULL, ensure it does not have non-gaps in gap RF columns */
1004 if(validate_no_nongaps_in_rf_gaps(msaA[ai]->abc, msaA[ai]->rf, msaA[ai]->sa_cons, msaA[ai]->alen)) { /* returns TRUE if sa_cons has 0 non-gap characters in gap columns of RF annotation */
1005 /* dealign */
1006 if((status = esl_strdup(msaA[ai]->sa_cons, msaA[ai]->alen, &(dealigned2))) != eslOK) goto ERROR;
1007 if((status = esl_strdealign(dealigned2, msaA[ai]->rf, "-_.~", NULL)) != eslOK) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "unexpected error dealigning sa_cons of msaA[%d]", ai);
1008 /* check identity */
1009 if(esl_strcmp(dealigned, dealigned2) != 0) { do_add = FALSE; }
1010 free(dealigned2);
1011 dealigned2 = NULL;
1012 break; /* if we matched, do_add remains TRUE */
1013 } /* end of 'if(validate_no_nongaps_in_rf_gaps(msaA[ai]->abc...)' */
1014 else {
1015 do_add = FALSE;
1016 }
1017 } /* end of (for(ai = 1...)) */
1018 if(dealigned != NULL) { free(dealigned); dealigned = NULL; }
1019 if(do_add) {
1020 /* gap out the the sa_cons to fit in merged alignment */
1021 if((status = inflate_string_with_gaps_and_missing(msaA[0]->sa_cons, msaA[0]->alen, alen_merged, neitherA, '.', NULL, '~', &(gapped_out))) != eslOK) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "Error adding gaps to create SA_cons annotation for merged alignment.");
1022 if(mmsa->sa_cons != NULL) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "Error adding SA_cons to merged alignment, it is already non-NULL.");
1023 if((status = esl_strdup(gapped_out, alen_merged, &(mmsa->sa_cons))) != eslOK) goto ERROR;
1024 free(gapped_out);
1025 gapped_out = NULL;
1026 if(be_verbose) fprintf(stdout, "# Identical SA_cons annotation from all alignments transferred to merged alignment.\n");
1027 }
1028 else {
1029 if(be_verbose) fprintf(stdout, "# SA_cons annotation from first alignment absent from or different in >= 1 other alignments; not included in merged alignment.\n");
1030 }
1031 } /* end of 'if(validate_no_nongaps_in_rf_gaps(msaA[0]->abc' */
1032 } /* end of if(msaA[0]->sa_cons != NULL) */
1033 else if(be_verbose) fprintf(stdout, "# SA_cons annotation absent from (at least) first alignment; not included in merged alignment.\n");
1034
1035 /*********************************************************************/
1036 /* Check pp_cons: it must include all gaps in gap RF columns and be identical once gap RF columns are removed in all
1037 * msaA[] alignments to be included. (Same requirements as unparsed GC annotation, so code block below is analogous to one above). */
1038 if(msaA[0]->pp_cons != NULL) {
1039 do_add = TRUE; /* until proven otherwise */
1040 /* ensure it does not have non-gaps in gap RF columns */
1041 if(validate_no_nongaps_in_rf_gaps(msaA[0]->abc, msaA[0]->rf, msaA[0]->pp_cons, msaA[0]->alen)) { /* returns TRUE if pp_cons has 0 non-gap characters in gap columns of RF annotation */
1042 /* dealign pp_cons */
1043 if((status = esl_strdup(msaA[0]->pp_cons, msaA[0]->alen, &(dealigned))) != eslOK) goto ERROR;
1044 if((status = esl_strdealign(dealigned, msaA[0]->rf, "-_.~", NULL)) != eslOK) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "unexpected error dealigning pp_cons of msaA[0]");
1045 for(ai = 1; ai < nmsa; ai++) {
1046 if(msaA[ai]->pp_cons == NULL) {
1047 do_add = FALSE;
1048 break;
1049 }
1050 /* pp_cons != NULL, ensure it does not have non-gaps in gap RF columns */
1051 if(validate_no_nongaps_in_rf_gaps(msaA[ai]->abc, msaA[ai]->rf, msaA[ai]->pp_cons, msaA[ai]->alen)) { /* returns TRUE if pp_cons has 0 non-gap characters in gap columns of RF annotation */
1052 /* dealign */
1053 if((status = esl_strdup(msaA[ai]->pp_cons, msaA[ai]->alen, &(dealigned2))) != eslOK) goto ERROR;
1054 if((status = esl_strdealign(dealigned2, msaA[ai]->rf, "-_.~", NULL)) != eslOK) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "unexpected error dealigning pp_cons of msaA[%d]", ai);
1055 /* check identity */
1056 if(esl_strcmp(dealigned, dealigned2) != 0) { do_add = FALSE; }
1057 free(dealigned2);
1058 dealigned2 = NULL;
1059 break; /* if we matched, do_add remains TRUE */
1060 } /* end of 'if(validate_no_nongaps_in_rf_gaps(msaA[ai]->abc...)' */
1061 else {
1062 do_add = FALSE;
1063 }
1064 } /* end of (for(ai = 1...)) */
1065 if(dealigned != NULL) { free(dealigned); dealigned = NULL; }
1066 if(do_add) {
1067 /* gap out the the pp_cons to fit in merged alignment */
1068 if((status = inflate_string_with_gaps_and_missing(msaA[0]->pp_cons, msaA[0]->alen, alen_merged, neitherA, '.', NULL, '~', &(gapped_out))) != eslOK) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "Error adding gaps to create PP_cons annotation for merged alignment.");
1069 if(mmsa->pp_cons != NULL) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "Error adding PP_cons to merged alignment, it is already non-NULL.");
1070 if((status = esl_strdup(gapped_out, alen_merged, &(mmsa->pp_cons))) != eslOK) goto ERROR;
1071 free(gapped_out);
1072 gapped_out = NULL;
1073 if(be_verbose) fprintf(stdout, "# Identical PP_cons annotation from all alignments transferred to merged alignment.\n");
1074 }
1075 else {
1076 if(be_verbose) fprintf(stdout, "# PP_cons annotation from first alignment absent from or different in >= 1 other alignments; not included in merged alignment.\n");
1077 }
1078 } /* end of 'if(validate_no_nongaps_in_rf_gaps(msaA[0]->abc' */
1079 } /* end of if(msaA[0]->pp_cons != NULL) */
1080 else if(be_verbose) fprintf(stdout, "# PP_cons annotation absent from (at least) first alignment; not included in merged alignment.\n");
1081
1082 /*********************************************************************/
1083 /* Finally, validate that RF annotation is identical in all alignments after removing gaps. */
1084
1085 if(msaA[0]->rf == NULL) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "All alignments must have #= GC RF annotation.");
1086 /* dealign rf */
1087 if((status = esl_strdup(msaA[0]->rf, msaA[0]->alen, &(dealigned))) != eslOK) goto ERROR;
1088 if((status = esl_strdealign(dealigned, dealigned, "-_.~", NULL)) != eslOK) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "unexpected error dealigning RF of msaA[0]");
1089 for(ai = 1; ai < nmsa; ai++) {
1090 if(msaA[ai]->rf == NULL) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "All alignments must have #= GC RF annotation.");
1091 /* dealign */
1092 if((status = esl_strdup(msaA[ai]->rf, msaA[ai]->alen, &(dealigned2))) != eslOK) goto ERROR;
1093 if((status = esl_strdealign(dealigned2, dealigned2, "-_.~", NULL)) != eslOK) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "unexpected error dealigning RF of msaA[%d]", ai);
1094 /* check identity */
1095 if(esl_strcmp(dealigned, dealigned2) != 0) {
1096 printf("%s\n%s\n", dealigned, dealigned2);
1097 ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "All alignments must have identical #=GC RF annotation, once gaps (\".\",\"-\",\"_\") are removed.\nAlignment %d de-gapped RF annotation differs from that of alignment 1.\n%s\n%s", ai+1, dealigned, dealigned2);
1098 }
1099 if(dealigned2 != NULL) { free(dealigned2); dealigned2 = NULL; }
1100 }
1101 if(dealigned != NULL) { free(dealigned); dealigned = NULL; }
1102 /* gap out the the RF to fit in merged alignment */
1103 if((status = inflate_string_with_gaps_and_missing(msaA[0]->rf, msaA[0]->alen, alen_merged, ngapA, '.', nmisA, '~', &(gapped_out))) != eslOK) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "Error adding gaps to create RF annotation for merged alignment.");
1104 if(mmsa->rf != NULL) ESL_XFAIL(eslEINCONCEIVABLE, errbuf, "Error adding RF to merged alignment, it is already non-NULL.");
1105 if((status = esl_strdup(gapped_out, alen_merged, &(mmsa->rf))) != eslOK) goto ERROR;
1106 free(gapped_out);
1107 gapped_out = NULL;
1108 if(be_verbose) fprintf(stdout, "# Identical RF annotation from all alignments transferred to merged alignment.\n");
1109
1110 if(dealigned != NULL) free(dealigned);
1111 if(dealigned2 != NULL) free(dealigned2);
1112 if(gapped_out != NULL) free(gapped_out);
1113 if(ngapA != NULL) free(ngapA);
1114 if(nmisA != NULL) free(nmisA);
1115 if(neitherA != NULL) free(neitherA);
1116 return eslOK;
1117
1118 ERROR:
1119 if(dealigned != NULL) free(dealigned);
1120 if(dealigned2 != NULL) free(dealigned2);
1121 if(gapped_out != NULL) free(gapped_out);
1122 if(ngapA != NULL) free(ngapA);
1123 if(nmisA != NULL) free(nmisA);
1124 if(neitherA != NULL) free(neitherA);
1125 return status;
1126 }
1127
1128 /* Function: add_msa
1129 * Date: EPN, Mon Nov 23 05:54:37 2009
1130 *
1131 * Add a "child" MSA we read from a file to the merged
1132 * MSA - the merged alignment that we'll eventually
1133 * output. We free each string in the child as soon
1134 * as we've added it to the merged, to save memory.
1135 *
1136 * We add all sequence data (aseq), and per sequence
1137 * annotation, including sqname, sqdesc, sqacc, pp, ss,
1138 * sa, as well as non-parsed GS and GR annotation.
1139 *
1140 * <maxgap>[0..clen] is an array specifying the
1141 * number of inserted columns necessary between
1142 * each consensus position.
1143 *
1144 * max{gap,mis}[0] is number of gaps/missing columns before 1st cpos.
1145 * max{gap,mis}[clen-1] is number of gaps/missing columns before final cpos.
1146 * max{gap,mis}[clen] is number of gaps/missing columns after final cpos.
1147 *
1148 * <alen_merged> is the number of columns in the merged
1149 * alignment. This is the non-gap RF length plus the
1150 * sum of the maxgap vector.
1151 *
1152 * Returns: eslOK on success.
1153 * eslEMEM on memory allocation failure.
1154 */
1155 int
1156 add_msa(ESL_MSA *mmsa, char *errbuf, ESL_MSA *msa_to_add, int *maxgap, int *maxmis, int clen, int alen_merged)
1157 {
1158 int status;
1159 int i; /* counter over sequences in msa_to_add */
1160 int j; /* counter over alignment annotations */
1161 int mi; /* counter over sequences in mmsa */
1162 void *tmp; /* for reallocations */
1163 char *tmpstr; /* used for copying GR annotation */
1164 int nseq_existing; /* number of sequences already added to mmsa, by previous calls of this function */
1165 int *ngapA = NULL; /* [0..alen] number of insert gap columns to add after each alignment column when merging */
1166 int *nmisA = NULL; /* [0..alen] number of missing data ('~') gap columns to add after each alignment column when merging */
1167 int *neitherA = NULL; /* [0..apos..alen] = ngapA[apos] + nmisA[apos] */
1168
1169 nseq_existing = mmsa->nseq;
1170
1171 /* determine how many all gap columns to insert after each alignment position
1172 * of the child msa when copying it to the merged msa */
1173 if((status = determine_gap_columns_to_add(msa_to_add, maxgap, maxmis, clen, &(ngapA), &(nmisA), &(neitherA), errbuf)) != eslOK)
1174 return status;
1175
1176 /* Append msa_to_add's sequence data and per-sequence annotation to mmsa after adding necessary gaps */
1177 /* sequence names and aligned sequence data (digitized) */
1178 for(i = 0, mi = nseq_existing; i < msa_to_add->nseq; i++, mi++) {
1179 esl_strdup(msa_to_add->sqname[i], -1, &(mmsa->sqname[mi]));
1180
1181 status = inflate_seq_with_gaps(msa_to_add->abc, msa_to_add->aseq[i], msa_to_add->alen, alen_merged, neitherA, '.', &(mmsa->aseq[mi]));
1182 if (status == eslEMEM) ESL_XFAIL(status, errbuf, "Out of memory adding sequence %s", mmsa->sqname[mi]);
1183 else if(status != eslOK) ESL_XFAIL(status, errbuf, "Found internal missing data symbols in seq: %s", mmsa->sqname[mi]);
1184 free(msa_to_add->aseq[i]); /* free immediately */
1185 msa_to_add->aseq[i] = NULL;
1186 }
1187
1188 /* parsed annotation that is optional */
1189 /* sqacc */
1190 if(msa_to_add->sqacc != NULL) {
1191 if(mmsa->sqacc == NULL) { /* allocate for all sequences, even ones added in previous calls to add_msa() */
1192 ESL_ALLOC(mmsa->sqacc, sizeof(char *) * (nseq_existing + msa_to_add->nseq));
1193 for(mi = 0; mi < nseq_existing; mi++) { mmsa->sqacc[mi] = NULL; }
1194 }
1195 else { /* reallocate; to add space for new seqs */
1196 ESL_RALLOC(mmsa->sqacc, tmp, sizeof(char *) * (nseq_existing + msa_to_add->nseq));
1197 }
1198 for(i = 0, mi = nseq_existing; i < msa_to_add->nseq; i++, mi++) {
1199 if(msa_to_add->sqacc[i] != NULL) {
1200 if((status = esl_strdup(msa_to_add->sqacc[i], -1, &(mmsa->sqacc[mi]))) != eslOK)
1201 ESL_XFAIL(status, errbuf, "Memory allocation error when copying sequence accession number %d.\n", i+1);
1202 free(msa_to_add->sqacc[i]); /* free immediately */
1203 msa_to_add->sqacc[i] = NULL;
1204 }
1205 else {
1206 mmsa->sqacc[mi] = NULL;
1207 }
1208 }
1209 } /* end of if(msa_to_add->sqacc != NULL */
1210 else if(mmsa->sqacc != NULL) {
1211 /* msa_to_add->sqacc == NULL, but mmsa->sqacc != NULL, reallocate mmsa->sqacc, and set new ones to NULL */
1212 ESL_RALLOC(mmsa->sqacc, tmp, sizeof(char *) * (nseq_existing + msa_to_add->nseq));
1213 for(mi = nseq_existing; mi < nseq_existing + msa_to_add->nseq; mi++) {
1214 mmsa->sqacc[mi] = NULL;
1215 }
1216 }
1217
1218 /* sqdesc */
1219 if(msa_to_add->sqdesc != NULL) {
1220 if(mmsa->sqdesc == NULL) { /* allocate for all sequences, even ones added in previous calls to add_msa() */
1221 ESL_ALLOC(mmsa->sqdesc, sizeof(char *) * (nseq_existing + msa_to_add->nseq));
1222 for(mi = 0; mi < nseq_existing; mi++) { mmsa->sqdesc[mi] = NULL; }
1223 }
1224 else { /* reallocate; to add space for new seqs */
1225 ESL_RALLOC(mmsa->sqdesc, tmp, sizeof(char *) * (nseq_existing + msa_to_add->nseq));
1226 }
1227 for(i = 0, mi = nseq_existing; i < msa_to_add->nseq; i++, mi++) {
1228 if(msa_to_add->sqdesc[i] != NULL) {
1229 if((status = esl_strdup(msa_to_add->sqdesc[i], -1, &(mmsa->sqdesc[mi]))) != eslOK)
1230 ESL_XFAIL(status, errbuf, "Memory allocation error when copying sequence description number %d.\n", i+1);
1231 free(msa_to_add->sqdesc[i]); /* free immediately */
1232 msa_to_add->sqdesc[i] = NULL;
1233 }
1234 else {
1235 mmsa->sqdesc[mi] = NULL;
1236 }
1237 }
1238 } /* end of if(msa_to_add->sqdesc != NULL */
1239 else if(mmsa->sqdesc != NULL) {
1240 /* msa_to_add->sqdesc == NULL, but mmsa->sqdesc != NULL, reallocate mmsa->sqdesc, and set new ones to NULL */
1241 ESL_RALLOC(mmsa->sqdesc, tmp, sizeof(char *) * (nseq_existing + msa_to_add->nseq));
1242 for(mi = nseq_existing; mi < nseq_existing + msa_to_add->nseq; mi++) {
1243 mmsa->sqdesc[mi] = NULL;
1244 }
1245 }
1246
1247 /* per-seq posterior probabilities */
1248 if(msa_to_add->pp != NULL) {
1249 if(mmsa->pp == NULL) { /* allocate for all sequences, even ones added in previous calls to add_msa() */
1250 ESL_ALLOC(mmsa->pp, sizeof(char *) * (nseq_existing + msa_to_add->nseq));
1251 for(mi = 0; mi < nseq_existing; mi++) { mmsa->pp[mi] = NULL; }
1252 }
1253 else { /* reallocate; to add space for new seqs */
1254 ESL_RALLOC(mmsa->pp, tmp, sizeof(char *) * (nseq_existing + msa_to_add->nseq));
1255 }
1256 for(i = 0, mi = nseq_existing; i < msa_to_add->nseq; i++, mi++) {
1257 if(msa_to_add->pp[i] != NULL) {
1258 if((status = inflate_string_with_gaps_and_missing(msa_to_add->pp[i], msa_to_add->alen, alen_merged, neitherA, '.', NULL, '~', &(mmsa->pp[mi]))) != eslOK)
1259 ESL_XFAIL(status, errbuf, "Memory allocation error when copying sequence number %d posterior probabilities.\n", i+1);
1260 free(msa_to_add->pp[i]); /* free immediately */
1261 msa_to_add->pp[i] = NULL;
1262 }
1263 else {
1264 mmsa->pp[mi] = NULL;
1265 }
1266 }
1267 } /* end of if(msa_to_add->pp != NULL */
1268 else if(mmsa->pp != NULL) {
1269 /* msa_to_add->pp == NULL, but mmsa->pp != NULL, reallocate mmsa->pp, and set new ones to NULL */
1270 ESL_RALLOC(mmsa->pp, tmp, sizeof(char *) * (nseq_existing + msa_to_add->nseq));
1271 for(mi = nseq_existing; mi < nseq_existing + msa_to_add->nseq; mi++) {
1272 mmsa->pp[mi] = NULL;
1273 }
1274 }
1275
1276 /* per-seq secondary structure */
1277 if(msa_to_add->ss != NULL) {
1278 if(mmsa->ss == NULL) { /* allocate for all sequences, even ones added in previous calls to add_msa() */
1279 ESL_ALLOC(mmsa->ss, sizeof(char *) * (nseq_existing + msa_to_add->nseq));
1280 for(mi = 0; mi < nseq_existing; mi++) { mmsa->ss[mi] = NULL; }
1281 }
1282 else { /* reallocate; to add space for new seqs */
1283 ESL_RALLOC(mmsa->ss, tmp, sizeof(char *) * (nseq_existing + msa_to_add->nseq));
1284 }
1285 for(i = 0, mi = nseq_existing; i < msa_to_add->nseq; i++, mi++) {
1286 if(msa_to_add->ss[i] != NULL) {
1287 if((status = inflate_string_with_gaps_and_missing(msa_to_add->ss[i], msa_to_add->alen, alen_merged, neitherA, '.', NULL, '~', &(mmsa->ss[mi]))) != eslOK)
1288 ESL_XFAIL(status, errbuf, "Memory allocation error when copying sequence number %d secondary structure.\n", i+1);
1289 free(msa_to_add->ss[i]); /* free immediately */
1290 msa_to_add->ss[i] = NULL;
1291 }
1292 else {
1293 mmsa->ss[mi] = NULL;
1294 }
1295 }
1296 } /* end of if(msa_to_add->ss != NULL */
1297 else if(mmsa->ss != NULL) {
1298 /* msa_to_add->ss == NULL, but mmsa->ss != NULL, reallocate mmsa->ss, and set new ones to NULL */
1299 ESL_RALLOC(mmsa->ss, tmp, sizeof(char *) * (nseq_existing + msa_to_add->nseq));
1300 for(mi = nseq_existing; mi < nseq_existing + msa_to_add->nseq; mi++) {
1301 mmsa->ss[mi] = NULL;
1302 }
1303 }
1304
1305 /* per-seq surface accessibility */
1306 if(msa_to_add->sa != NULL) {
1307 if(mmsa->sa == NULL) { /* allocate for all sequences, even ones added in previous calls to add_msa() */
1308 ESL_ALLOC(mmsa->sa, sizeof(char *) * (nseq_existing + msa_to_add->nseq));
1309 for(mi = 0; mi < nseq_existing; mi++) { mmsa->sa[mi] = NULL; }
1310 }
1311 else { /* reallocate; to add space for new seqs */
1312 ESL_RALLOC(mmsa->sa, tmp, sizeof(char *) * (nseq_existing + msa_to_add->nseq));
1313 }
1314 for(i = 0, mi = nseq_existing; i < msa_to_add->nseq; i++, mi++) {
1315 if(msa_to_add->sa[i] != NULL) {
1316 if((status = inflate_string_with_gaps_and_missing(msa_to_add->sa[i], msa_to_add->alen, alen_merged, neitherA, '.', NULL, '~', &(mmsa->sa[mi]))) != eslOK)
1317 ESL_XFAIL(status, errbuf, "Memory allocation error when copying sequence number %d surface accessibility.\n", i+1);
1318 free(msa_to_add->sa[i]); /* free immediately */
1319 msa_to_add->sa[i] = NULL;
1320 }
1321 else {
1322 mmsa->sa[mi] = NULL;
1323 }
1324 }
1325 } /* end of if(msa_to_add->sa != NULL */
1326 else if(mmsa->sa != NULL) {
1327 /* msa_to_add->sa == NULL, but mmsa->sa != NULL, reallocate mmsa->sa, and set new ones to NULL */
1328 ESL_RALLOC(mmsa->sa, tmp, sizeof(char *) * (nseq_existing + msa_to_add->nseq));
1329 for(mi = nseq_existing; mi < nseq_existing + msa_to_add->nseq; mi++) {
1330 mmsa->sa[mi] = NULL;
1331 }
1332 }
1333
1334 /* Unparsed per-sequence (GS) annotation */
1335 if(msa_to_add->ngs > 0) {
1336 for(j = 0; j < msa_to_add->ngs; j++) {
1337 for(i = 0, mi = nseq_existing; i < msa_to_add->nseq; i++, mi++) {
1338 if(msa_to_add->gs[j][i] != NULL)
1339 if((status =esl_msa_AddGS(mmsa, msa_to_add->gs_tag[j], -1, mi, msa_to_add->gs[j][i], -1)) != eslOK)
1340 ESL_XFAIL(status, errbuf, "Memory allocation error when copying sequence number %d GS annotation.\n", i+1);
1341 }
1342 free(msa_to_add->gs[j][i]); /* free immediately */
1343 msa_to_add->gs[j][i] = NULL;
1344 }
1345 }
1346 /* caller will free the rest of gs via esl_msa_Destroy() */
1347
1348 /* unparsed per-residue (GR) annotation */
1349 if(msa_to_add->gr != NULL) {
1350 for(j = 0; j < msa_to_add->ngr; j++) {
1351 for(i = 0, mi = nseq_existing; i < msa_to_add->nseq; i++, mi++) {
1352 if(msa_to_add->gr[j][i] != NULL) {
1353 if((status = inflate_string_with_gaps_and_missing(msa_to_add->gr[j][i], msa_to_add->alen, alen_merged, neitherA, '.', NULL, '~', &(tmpstr))) != eslOK)
1354 ESL_XFAIL(status, errbuf, "Memory allocation error when copying sequence number %d GR annotation.\n", i+1);
1355 if((status = esl_msa_AppendGR(mmsa, msa_to_add->gr_tag[j], mi, tmpstr)) != eslOK)
1356 ESL_XFAIL(status, errbuf, "Memory allocation error when copying sequence number %d GR annotation.\n", i+1);
1357 free(tmpstr);
1358 free(msa_to_add->gr[j][i]); /* free immediately */
1359 msa_to_add->gr[j][i] = NULL;
1360 }
1361 }
1362 }
1363 }
1364 /* caller will free the rest of gr in esl_msa_Destroy() */
1365
1366 /* msa_to_add should be destroyed by caller */
1367
1368 /* update nseq in mmsa */
1369 mmsa->nseq += msa_to_add->nseq;
1370
1371 if(ngapA != NULL) free(ngapA);
1372 if(nmisA != NULL) free(nmisA);
1373 if(neitherA != NULL) free(neitherA);
1374 return eslOK;
1375
1376 ERROR:
1377 if(ngapA != NULL) free(ngapA);
1378 if(nmisA != NULL) free(nmisA);
1379 if(neitherA != NULL) free(neitherA);
1380 return status;
1381 }
1382
1383 /* inflate_string_with_gaps_and_missing
1384 *
1385 * Given a string, create a new one that is a copy of it,
1386 * but with missing data and gaps added before each position (apos)
1387 * as specified by n{gap,mis}A[0..apos..len]. <gapchar> and <mischar>
1388 * specify the gap character and missing data character. Either
1389 * ngapA or nmisA can be either be NULL, if so they're treated as if
1390 * they're all zeroes (no gaps/missing data will be added anywhere).
1391 *
1392 * n{gap,mis}A[0] - number of gaps/missing to add before first posn
1393 * n{gap,mis}A[apos] - number of gaps/missing to add before posn apos
1394 * n{gap,mis}A[src_len] - number of gaps/missing to add after final posn
1395 *
1396 * By convention, missing data symbols are always put before gap symbols
1397 * when more than 1 of each are to be placed after the some nongap RF position.
1398 *
1399 * ret_str is allocated here.
1400 *
1401 * Returns eslOK on success.
1402 * eslEMEM on memory error.
1403 */
1404 int
1405 inflate_string_with_gaps_and_missing(char *src_str, int64_t src_len, int64_t dst_len, int *ngapA, char gapchar, int *nmisA, char mischar, char **ret_dst_str)
1406 {
1407 int status;
1408 int src_apos = 0;
1409 int dst_apos = 0;
1410 int i;
1411 char *dst_str;
1412
1413 ESL_ALLOC(dst_str, sizeof(char) * (dst_len+1));
1414 dst_str[dst_len] = '\0';
1415
1416 /* add gaps before first position */
1417 if(nmisA) for(i = 0; i < nmisA[0]; i++) dst_str[dst_apos++] = mischar;
1418 if(ngapA) for(i = 0; i < ngapA[0]; i++) dst_str[dst_apos++] = gapchar;
1419
1420 /* add gaps after every position */
1421 for(src_apos = 0; src_apos < src_len; src_apos++) {
1422 dst_str[dst_apos++] = src_str[src_apos];
1423 if(nmisA) for(i = 0; i < nmisA[(src_apos+1)]; i++) dst_str[dst_apos++] = mischar;
1424 if(ngapA) for(i = 0; i < ngapA[(src_apos+1)]; i++) dst_str[dst_apos++] = gapchar;
1425 }
1426
1427 *ret_dst_str = dst_str;
1428 return eslOK;
1429
1430 ERROR:
1431 return eslEMEM;
1432 }
1433
1434
1435 /* inflate_seq_with_gaps()
1436 *
1437 * Given an aligned sequence, create a new one that is a copy of it,
1438 * but with gaps added before each position (apos) as specified by
1439 * ngapA[0..apos..len]. <gapchar> specifies the gap character.
1440 *
1441 * This function follows the HMMER3 convention of using '~' to mark
1442 * fragment sequences. If a sequence is a fragment, the first and final
1443 * string of gaps (contiguous gaps) will be marked as '~'. That is,
1444 * the 1st column will necessarily be a '~' and the last will be a '~'.
1445 * This function takes care to add '~' as gap characters in fragments,
1446 * where appropriate, to follow HMMER3 convention.
1447 *
1448 * This function is importantly different from inflate_string_with_gaps_and_missing()
1449 * in that it does not allow internal '~' missing data characters to
1450 * be added (which are dictated by nmisA[] in inflate_string_with_gaps_and_missing())
1451 * Those '~' are possible in Infernal output alignments only in
1452 * #=GC RF and #=GC SS_cons alignment.
1453 *
1454 * It only adds '~' as necessary to the beginning and ends of fragments.
1455 *
1456 * ngapA[0] - number of gaps/missing to add before first posn
1457 * ngapA[apos] - number of gaps/missing to add before posn apos
1458 * ngapA[src_len] - number of gaps/missing to add after final posn
1459 *
1460 * ret_str is allocated here.
1461 *
1462 * Returns eslOK on success.
1463 * eslEMEM on memory error.
1464 * eslEINVAL if sequence (src_str) has an internal missing
1465 * data symbol that violates HMMER3 convention.
1466 */
1467 int
1468 inflate_seq_with_gaps(const ESL_ALPHABET *abc, char *src_str, int64_t src_len, int64_t dst_len, int *ngapA, char gapchar, char **ret_dst_str)
1469 {
1470 int status;
1471 int src_apos = 0;
1472 int dst_apos = 0;
1473 int src_flpos = 0; /* position of rightmost '~' in contiguous string that begins at position 0 */
1474 int src_frpos = 0; /* position of leftmost '~' in contiguous string that ends at position src_len-1 */
1475 int i;
1476 char *dst_str;
1477 char char2add;
1478 int i_am_fragment = FALSE;
1479
1480 ESL_ALLOC(dst_str, sizeof(char) * (dst_len+1));
1481 dst_str[dst_len] = '\0';
1482
1483 /* determine if sequence is a fragment, and determine src_flpos and src_frpos */
1484 src_flpos = 0;
1485 while(src_flpos < src_len && esl_abc_CIsMissing(abc, src_str[src_flpos])) { src_flpos++; }
1486 src_flpos--; /* we overshot by 1 */
1487 /* src_flpos is now rightmost '~' in stretch of '~' that begin at position 0, so
1488 * it's -1 if sequence is not a fragment (there are no leading '~' in src_str)
1489 */
1490
1491 src_frpos = src_len-1;
1492 while(src_frpos > -1 && esl_abc_CIsMissing(abc, src_str[src_frpos])) { src_frpos--; }
1493 src_frpos++; /* we overshot by 1 */
1494 /* src_frpos is now leftmost '~' in stretch of '~' that end at position src_len-1, so
1495 * it's src_len if sequence is not a fragment (there are no trailing '~' in src_str)
1496 */
1497 i_am_fragment = (src_flpos != -1 || src_frpos != src_len) ? TRUE : FALSE;
1498
1499 /* Now verify that we don't have any internal '~' characters */
1500 for(src_apos = src_flpos+1; src_apos <= src_frpos-1; src_apos++) {
1501 if(esl_abc_CIsMissing(abc, src_str[src_apos])) return eslEINVAL;
1502 }
1503
1504 /* add gaps before first position */
1505 src_apos = 0;
1506 if(i_am_fragment) {
1507 char2add = ((src_flpos >= src_apos) || (src_frpos <= src_apos)) ? esl_abc_CGetMissing(abc) : gapchar;
1508 }
1509 else {
1510 char2add = gapchar;
1511 }
1512 if(ngapA) for(i = 0; i < ngapA[0]; i++) dst_str[dst_apos++] = char2add;
1513
1514 /* add gaps or missing character after every position */
1515 for(src_apos = 0; src_apos < src_len; src_apos++) {
1516 dst_str[dst_apos++] = src_str[src_apos];
1517 if(i_am_fragment) {
1518 char2add = ((src_flpos >= src_apos) || (src_frpos <= src_apos)) ? esl_abc_CGetMissing(abc) : gapchar;
1519 }
1520 else {
1521 char2add = gapchar;
1522 }
1523 for(i = 0; i < ngapA[(src_apos+1)]; i++) dst_str[dst_apos++] = char2add;
1524 }
1525
1526 *ret_dst_str = dst_str;
1527 return eslOK;
1528
1529 ERROR:
1530 return eslEMEM;
1531 }
1532
1533
1534 /* validate_no_nongaps_in_rf_gaps
1535 *
1536 * Given an RF string with gaps defined as by alphabet <abc>
1537 * and another string of same length. Make sure none of the
1538 * positions that are gaps in the RF string are non-gaps in the
1539 * other string. Return TRUE if none are. Return FALSE if at
1540 * least one is.
1541 *
1542 * Returns TRUE if 0 characters in <other_str> in same position
1543 * as a gap in <rf_str> are non-gaps. FALSE otherwise.
1544 */
1545 int
1546 validate_no_nongaps_in_rf_gaps(const ESL_ALPHABET *abc, char *rf_str, char *other_str, int64_t len)
1547 {
1548 int64_t i;
1549 for(i = 0; i < len; i++) {
1550 if((! rfchar_is_nongap_nonmissing(abc, rf_str[i])) && (rfchar_is_nongap_nonmissing(abc, other_str[i]))) return FALSE;
1551 }
1552 return TRUE;
1553 }
1554
1555 /* determine_gap_columns_to_add
1556 * (stolen and slightly modified from Infernal 1.1rc1's cmalign.c)
1557 *
1558 * Given <maxgap> and <maxmis>, two arrays of the number of gap RF
1559 * positions and '~' RF (missing data) after each non-gap RF
1560 * (consensus) position in the eventual final merged alignment,
1561 * calculate how many inserts and missing data inserts
1562 * we need to add at each position of <msa> to expand it out to the
1563 * appropriate size of the eventual merged alignment.
1564 *
1565 * max{gap,mis}[0] is number of gaps/missing before 1st cpos in merged aln
1566 * max{gap,mis}[cpos] is number of gaps/missing after cpos in merged aln
1567 * for cpos = 1..clen
1568 * clen is the number of non-gap RF positions in msa (and in eventual merged msa).
1569 *
1570 * We allocate fill and return ret_ngapA[0..msa->alen], ret_nmisA[0..msa->alen],
1571 * and ret_neitherA[0..msa->alen] here.
1572 *
1573 * ret_n{gap,mis}gapA[0] is number of gaps/missing to add before 1st position of msa
1574 * ret_n{gap,mis}gapA[apos] is number of gaps/missing to add after alignment position apos
1575 * for apos = 1..msa->alen
1576 *
1577 * ret_neitherA[apos] = ngapA[apos] + nmisA[apos]
1578 *
1579 * This is similar to the esl_msa.c helper function of the same name,
1580 * but that function does not bother with missing data '~'.
1581 *
1582 * Returns eslOK on success.
1583 * eslEMEM on memory alloaction error
1584 * eslEINVAL if gaps occur before missing data b/t any two nongap RF posns
1585 * eslERANGE if a value exceeds what we expected (based on earlier
1586 * checks before this function was entered).
1587 * if !eslOK, errbuf if filled.
1588 */
1589 int
1590 determine_gap_columns_to_add(ESL_MSA *msa, int *maxgap, int *maxmis, int clen, int **ret_ngapA, int **ret_nmisA, int **ret_neitherA, char *errbuf)
1591 {
1592 int status;
1593 int apos;
1594 int prv_cpos = 0; /* alignment position corresponding to consensus position cpos-1 */
1595 int cpos = 0;
1596 int ngap = 0;
1597 int nmis = 0;
1598 int *ngapA = NULL;
1599 int *nmisA = NULL;
1600 int *neitherA = NULL;
1601
1602 /* contract check */
1603 if(maxmis[0] != 0) ESL_FAIL(eslEINVAL, errbuf, "missing characters exist prior to first cpos, this shouldn't happen.\n");
1604
1605 ESL_ALLOC(ngapA, sizeof(int) * (msa->alen+1));
1606 ESL_ALLOC(nmisA, sizeof(int) * (msa->alen+1));
1607 ESL_ALLOC(neitherA, sizeof(int) * (msa->alen+1));
1608 esl_vec_ISet(ngapA, (msa->alen+1), 0);
1609 esl_vec_ISet(nmisA, (msa->alen+1), 0);
1610 esl_vec_ISet(neitherA, (msa->alen+1), 0);
1611
1612 for(apos = 0; apos < msa->alen; apos++) {
1613 if(esl_abc_CIsMissing(msa->abc, msa->rf[apos])) {
1614 nmis++;
1615 if(ngap > 0) ESL_FAIL(eslEINVAL, errbuf, "after nongap RF pos %d, %d gap columns precede a missing data column (none should)", cpos, ngap);
1616 }
1617 else if(esl_abc_CIsGap(msa->abc, msa->rf[apos])) {
1618 ngap++;
1619 }
1620 else { /* a consensus position */
1621 /* a few sanity checks */
1622 if(ngap > maxgap[cpos]) ESL_FAIL(eslEINCONCEIVABLE, errbuf, "%d inserts before cpos %d greater than max expected (%d).\n", ngap, cpos, maxgap[cpos]);
1623 if(nmis > maxmis[cpos]) ESL_FAIL(eslEINCONCEIVABLE, errbuf, "%d EL inserts before cpos %d greater than max expected (%d).\n", nmis, cpos, maxmis[cpos]);
1624
1625 if (cpos == 0) {
1626 /* gaps and/or missing data before first consensus position: flush right */
1627 if(maxgap[cpos] > 0 && maxmis[cpos] == 0) { /* most common case */
1628 ngapA[prv_cpos] = maxgap[cpos] - ngap; /* flush right gaps (no missing) */
1629 }
1630 else if(maxgap[cpos] == 0 && maxmis[cpos] > 0) {
1631 nmisA[prv_cpos] = maxmis[cpos] - nmis; /* flush right missing (no gaps) */
1632 }
1633 else if(maxgap[cpos] > 0 && maxmis[cpos] > 0) {
1634 /* missing data (ELs) is always 5' of gaps */
1635 nmisA[prv_cpos] = maxmis[cpos] - nmis; /* flush right */
1636 ngapA[prv_cpos+nmis] = maxgap[cpos] - ngap; /* flush right, after missing */
1637 }
1638 }
1639 else {
1640 /* gaps and/missing data after interior consensus position (i.e. not before 1st cpos or after last)
1641 * Determine where to place inserts and/or missing data.
1642 * Handle each of 4 possibilities separately, note that if
1643 * maxgap[cpos] == 0 then ngap == 0, and if maxmis[cpos] == 0 then nmis == 0 (see sanity check above).
1644 */
1645 if(maxgap[cpos] > 0 && maxmis[cpos] == 0) { /* most common case */
1646 ngapA[prv_cpos + 1 + (ngap/2)] = maxgap[cpos] - ngap; /* internal cpos: split */
1647 }
1648 else if(maxgap[cpos] == 0 && maxmis[cpos] > 0) {
1649 nmisA[prv_cpos + 1 + (nmis/2)] = maxmis[cpos] - nmis; /* internal cpos: split */
1650 }
1651 else if(maxgap[cpos] > 0 && maxmis[cpos] > 0) {
1652 /* missing data is always 5' of gaps */
1653 nmisA[prv_cpos + 1 + (nmis/2)] = maxmis[cpos] - nmis; /* internal cpos: split */
1654 ngapA[prv_cpos + 1 + nmis + (ngap/2)] = maxgap[cpos] - ngap; /* internal cpos: split */
1655 }
1656 /* final case is if (maxgap[cpos] == 0 && maxmis[cpos] == 0)
1657 * in this case we do nothing.
1658 */
1659 }
1660 cpos++;
1661 prv_cpos = apos;
1662 ngap = 0;
1663 nmis = 0;
1664 }
1665 }
1666 /* deal with gaps and missing data after final consensus position */
1667
1668 /* first, validate that clen is what it should be */
1669 if(cpos != clen) {
1670 if(ngapA != NULL) free(ngapA);
1671 if(nmisA != NULL) free(nmisA);
1672 if(neitherA != NULL) free(neitherA);
1673 ESL_FAIL(eslEINCONCEIVABLE, errbuf, "consensus length (%d) is not the expected length (%d).", cpos, clen);
1674 }
1675
1676 if(maxgap[cpos] > 0 && maxmis[cpos] == 0) { /* most common case */
1677 ngapA[prv_cpos + 1 + ngap] = maxgap[cpos] - ngap; /* flush left gaps (no missing) */
1678 }
1679 else if(maxgap[cpos] == 0 && maxmis[cpos] > 0) {
1680 nmisA[prv_cpos + 1 + nmis] = maxmis[cpos] - nmis; /* flush left missing (no gaps) */
1681 }
1682 else if(maxgap[cpos] > 0 && maxmis[cpos] > 0) {
1683 /* missing data (ELs) is always 5' of gaps */
1684 nmisA[prv_cpos + 1 + nmis] = maxmis[cpos] - nmis; /* flush left */
1685 ngapA[prv_cpos + 1 + nmis + ngap] = maxgap[cpos] - ngap; /* flush left, after missing */
1686 }
1687
1688 /* determine neitherA[], the number of gaps due to either inserts or missing data after each apos */
1689 for(apos = 0; apos <= msa->alen; apos++) {
1690 neitherA[apos] = ngapA[apos] + nmisA[apos];
1691 }
1692
1693 *ret_ngapA = ngapA;
1694 *ret_nmisA = nmisA;
1695 *ret_neitherA = neitherA;
1696
1697 return eslOK;
1698
1699 ERROR:
1700 if(ngapA != NULL) free(ngapA);
1701 if(nmisA != NULL) free(nmisA);
1702 if(neitherA != NULL) free(neitherA);
1703 ESL_FAIL(status, errbuf, "Memory allocation error.");
1704 return status; /*NEVERREACHED*/
1705 }
1706
1707 /* write_pfam_msa_top
1708 *
1709 * Highly specialized function for printing out the 'top' of a
1710 * Stockholm alignment file, i.e. the Stockholm header line, comments
1711 * and GF annotation to an msa file. This function is necessary when
1712 * printing the merged alignment file in small memory mode (when
1713 * --small enabled). <msa> is an alignment with no sequence
1714 * information (no aseq, ax, GS, or GR data).
1715 */
1716 void
1717 write_pfam_msa_top(FILE *fp, ESL_MSA *msa)
1718 {
1719 int i, maxgf;
1720
1721 /* rest of code in this function was stolen verbatim from actually_write_stockholm in esl_msa.c */
1722
1723 /* Magic Stockholm header
1724 */
1725 fprintf(fp, "# STOCKHOLM 1.0\n");
1726
1727 /* Free text comments
1728 */
1729 for (i = 0; i < msa->ncomment; i++)
1730 fprintf(fp, "# %s\n", msa->comment[i]);
1731 if (msa->ncomment > 0) fprintf(fp, "\n");
1732
1733 maxgf = maxwidth(msa->gf_tag, msa->ngf);
1734 if (maxgf < 2) maxgf = 2;
1735
1736 /* GF section: per-file annotation
1737 */
1738 if (msa->name != NULL) fprintf(fp, "#=GF %-*s %s\n", maxgf, "ID", msa->name);
1739 if (msa->acc != NULL) fprintf(fp, "#=GF %-*s %s\n", maxgf, "AC", msa->acc);
1740 if (msa->desc != NULL) fprintf(fp, "#=GF %-*s %s\n", maxgf, "DE", msa->desc);
1741 if (msa->au != NULL) fprintf(fp, "#=GF %-*s %s\n", maxgf, "AU", msa->au);
1742
1743 /* Thresholds are hacky. Pfam has two. Rfam has one.
1744 */
1745 if (msa->cutset[eslMSA_GA1] && msa->cutset[eslMSA_GA2])
1746 fprintf(fp, "#=GF %-*s %.1f %.1f\n",
1747 maxgf, "GA", msa->cutoff[eslMSA_GA1], msa->cutoff[eslMSA_GA2]);
1748 else if (msa->cutset[eslMSA_GA1])
1749 fprintf(fp, "#=GF %-*s %.1f\n",
1750 maxgf, "GA", msa->cutoff[eslMSA_GA1]);
1751
1752 if (msa->cutset[eslMSA_NC1] && msa->cutset[eslMSA_NC2])
1753 fprintf(fp, "#=GF %-*s %.1f %.1f\n",
1754 maxgf, "NC", msa->cutoff[eslMSA_NC1], msa->cutoff[eslMSA_NC2]);
1755 else if (msa->cutset[eslMSA_NC1])
1756 fprintf(fp, "#=GF %-*s %.1f\n",
1757 maxgf, "NC", msa->cutoff[eslMSA_NC1]);
1758
1759 if (msa->cutset[eslMSA_TC1] && msa->cutset[eslMSA_TC2])
1760 fprintf(fp, "#=GF %-*s %.1f %.1f\n",
1761 maxgf, "TC", msa->cutoff[eslMSA_TC1], msa->cutoff[eslMSA_TC2]);
1762 else if (msa->cutset[eslMSA_TC1])
1763 fprintf(fp, "#=GF %-*s %.1f\n",
1764 maxgf, "TC", msa->cutoff[eslMSA_TC1]);
1765
1766 for (i = 0; i < msa->ngf; i++)
1767 fprintf(fp, "#=GF %-*s %s\n", maxgf, msa->gf_tag[i], msa->gf[i]);
1768 fprintf(fp, "\n");
1769
1770 return;
1771 }
1772
1773 /* write_pfam_msa_gc
1774 *
1775 * Highly specialized function for printing out the GC annotation to a
1776 * Pfam Stockholm alignment file (1 line/seq). This function is
1777 * necessary when printing the merged alignment file in small memory
1778 * mode (when --small enabled). <msa> is an alignment with no sequence
1779 * information (no aseq, ax, GS, nor GR data).
1780 */
1781 void
1782 write_pfam_msa_gc(FILE *fp, ESL_MSA *msa, int margin)
1783 {
1784 int i;
1785 if (msa->ss_cons != NULL) { fprintf(fp, "#=GC %-*s %s\n", margin-6, "SS_cons", msa->ss_cons); }
1786 if (msa->sa_cons != NULL) { fprintf(fp, "#=GC %-*s %s\n", margin-6, "SA_cons", msa->sa_cons); }
1787 if (msa->pp_cons != NULL) { fprintf(fp, "#=GC %-*s %s\n", margin-6, "PP_cons", msa->pp_cons); }
1788 if (msa->rf != NULL) { fprintf(fp, "#=GC %-*s %s\n", margin-6, "RF", msa->rf); }
1789 for (i = 0; i < msa->ngc; i++) { fprintf(fp, "#=GC %-*s %s\n", margin-6, msa->gc_tag[i], msa->gc[i]); }
1790 fprintf(fp, "//\n");
1791 }
1792
1793 /* maxwidth()
1794 * Return the length of the longest string in
1795 * an array of strings.
1796 */
1797 static int64_t
1798 maxwidth(char **s, int n)
1799 {
1800 int64_t max,len;
1801 int i;
1802
1803 max = 0;
1804 for (i = 0; i < n; i++)
1805 if (s[i] != NULL)
1806 {
1807 len = strlen(s[i]);
1808 if (len > max) max = len;
1809 }
1810 return max;
1811 }
1812
1813 /* rfchar_is_nongap_nonmissing()
1814 * Return FALSE if a character from RF annotation is
1815 * a gap or a missing character, else return TRUE.
1816 */
1817 static int
1818 rfchar_is_nongap_nonmissing(const ESL_ALPHABET *abc, char rfchar) {
1819 if(esl_abc_CIsGap (abc, rfchar)) return FALSE;
1820 if(esl_abc_CIsMissing(abc, rfchar)) return FALSE;
1821 return TRUE;
1822 }