Debian Codegraph @

easel/miniapps/esl-alistat.c

raw symbols SCIP
1 /* Show statistics about a multiple sequence alignment file or MSA database.
2 */
3 #include <esl_config.h>
4
5 #include <stdlib.h>
6 #include <stdio.h>
7
8 #include "easel.h"
9 #include "esl_alphabet.h"
10 #include "esl_arr2.h"
11 #include "esl_arr3.h"
12 #include "esl_getopts.h"
13 #include "esl_msa.h"
14 #include "esl_msafile.h"
15 #include "esl_msafile2.h"
16 #include "esl_distance.h"
17 #include "esl_vectorops.h"
18 #include "esl_wuss.h"
19
20 static char banner[] = "show summary statistics for a multiple sequence alignment file";
21 static char usage[] = "[options] <msafile>";
22
23 static int dump_infocontent_info(FILE *fp, ESL_ALPHABET *abc, double **abc_ct, int use_weights, int nali, int64_t alen, int nseq, int *i_am_rf, char *msa_name, char *alifile, char *errbuf);
24 static int dump_residue_info(FILE *fp, ESL_ALPHABET *abc, double **abc_ct, int use_weights, int nali, int64_t alen, int nseq, int *i_am_rf, char *msa_name, char *alifile, char *errbuf);
25 static int dump_posterior_column_info(FILE *fp, double **pp_ct, int use_weights, int nali, int64_t alen, int nseq, int *i_am_rf, char *msa_name, char *alifile, char *errbuf);
26 static int dump_posterior_sequence_info(FILE *fp, ESL_MSA *msa, int nali, char *alifile, char *errbuf);
27 static int dump_insert_info(FILE *fp, ESL_MSA *msa, int use_weights, int nali, int *i_am_rf, char *alifile, char *errbuf);
28 static int dump_column_residue_counts(FILE *fp, ESL_ALPHABET *abc, double **abc_ct, int do_ambig, int use_weights, int nali, int64_t alen, int nseq, char *msa_name, char *alifile, char *errbuf);
29 static int dump_basepair_counts(FILE *fp, ESL_MSA *msa, ESL_ALPHABET *abc, double ***bp_ct, int use_weights, int nali, int nseq, char *msa_name, char *alifile, char *errbuf);
30 static int map_rfpos_to_apos(ESL_MSA *msa, ESL_ALPHABET *abc, char *errbuf, int64_t alen, int **ret_i_am_rf, int **ret_rf2a_map, int *ret_rflen);
31 static int get_pp_idx(ESL_ALPHABET *abc, char ppchar);
32 static int count_msa(ESL_MSA *msa, char *errbuf, int nali, int no_ambig, int use_weights, double ***ret_abc_ct, double ****ret_bp_ct, double ***ret_pp_ct);
33 static int check_msa_weights(ESL_MSA *msa);
34
35 static ESL_OPTIONS options[] = {
36 /* name type default env range togs reqs incomp help docgroup */
37 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "help; show brief info on version and usage", 1 },
38 { "-1", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "use tabular output, one line per alignment", 1 },
39 { "--informat", eslARG_STRING, FALSE, NULL, NULL, NULL,NULL, NULL, "specify that input file is in format <s>", 1 },
40 { "--amino", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL,"--dna,--rna", "<msafile> contains protein alignments", 1 },
41 { "--dna", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL,"--amino,--rna", "<msafile> contains DNA alignments", 1 },
42 { "--rna", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL,"--amino,--dna", "<msafile> contains RNA alignments", 1 },
43 { "--small", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "use minimal RAM (RAM usage will be independent of aln size)", 2 },
44 /* options for optional output files */
45 { "--list", eslARG_OUTFILE,NULL, NULL, NULL, NULL,NULL, NULL, "output list of sequence names in alignment(s) to file <f>", 3 },
46 { "--icinfo", eslARG_OUTFILE,NULL, NULL, NULL, NULL,NULL, NULL, "print info on information content alignment column", 3 },
47 { "--rinfo", eslARG_OUTFILE,NULL, NULL, NULL, NULL,NULL, NULL, "print info on # of non-gap residues in each column to <f>", 3 },
48 { "--pcinfo", eslARG_OUTFILE,NULL, NULL, NULL, NULL,NULL, NULL, "print per-column posterior probability info to <f>", 3 },
49 { "--psinfo", eslARG_OUTFILE,NULL, NULL, NULL, NULL,NULL, "--small", "print per-sequence posterior probability info to <f>", 3 },
50 { "--iinfo", eslARG_OUTFILE,NULL, NULL, NULL, NULL,NULL, "--small", "print info on # of insertions b/t all non-gap RF cols to <f>", 3 },
51 { "--cinfo", eslARG_OUTFILE,NULL, NULL, NULL, NULL,NULL, NULL, "print per-column residue counts to <f>", 3 },
52 { "--noambig", eslARG_NONE, NULL, NULL, NULL, NULL,NULL, "--small", "with --cinfo, do not count ambiguous residues", 3 },
53 { "--bpinfo", eslARG_OUTFILE,NULL, NULL, NULL, NULL,NULL, "--small", "print per-column base-pair counts to <f>", 3 },
54 { "--weight", eslARG_NONE, NULL, NULL, NULL, NULL,NULL, "--small", "with --*info files, weight counts using WT annotation from msa", 3 },
55 { "--stall", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "arrest after start: for debugging under gdb", 99 },
56 { 0,0,0,0,0,0,0,0,0,0 },
57 };
58
59 int
60 main(int argc, char **argv)
61 {
62 ESL_GETOPTS *go = NULL; /* application configuration */
63 ESL_ALPHABET *abc = NULL; /* biological alphabet */
64 char *alifile = NULL; /* alignment file name */
65 int fmt = eslMSAFILE_UNKNOWN; /* format code for alifile */
66 ESL_MSAFILE *afp = NULL; /* open msa file */
67 ESL_MSAFILE2 *old_afp = NULL; /* open msa file, legacy (--small) */
68 ESL_MSA *msa = NULL; /* one multiple sequence alignment */
69 int nali; /* number of alignments read */
70 int i; /* counter over seqs */
71 int64_t alen; /* alignment length */
72 int nseq; /* number of sequences in the msa */
73 int64_t rlen; /* a raw (unaligned) seq length */
74 int64_t small, large; /* smallest, largest sequence */
75 int64_t nres; /* total # of residues in msa */
76 double avgid; /* average fractional pair id */
77 int max_comparisons; /* maximum # comparisons for avg id */
78 int do_stall; /* used to stall when debugging */
79 double **abc_ct = NULL; /* [0..msa->alen-1][0..abc->K] number of each residue at each position (abc->K is gap) */
80 double ***bp_ct = NULL; /* [0..msa->alen-1][0..abc->Kp-1][0..abc->Kp-1] per (non-pknotted) consensus basepair *
81 * count of each possible basepair over all seqs basepairs are indexed by 'i' the minimum *
82 * of 'i:j' for a pair between i and j, where i < j. */
83 double **pp_ct = NULL; /* [0..msa->alen-1][0..11], count of each posterior probability (PP) code, over all sequences, gap is 11 */
84 int *i_am_rf = NULL; /* [0..i..msa->alen-1]: TRUE if pos i is non-gap RF posn, if msa->rf == NULL remains NULL */
85 int *rf2a_map = NULL; /* [0..rfpos..rflen-1] = apos,
86 * apos is the alignment position (0..msa->alen-1) that
87 * is non-gap RF position rfpos+1 (for rfpos in 0..rflen-1) */
88 int rflen = -1; /* nongap RF length */
89 char errbuf[eslERRBUFSIZE];
90 int status;
91
92 /* optional output files */
93 FILE *iinfofp = NULL; /* output file for --iinfo */
94 FILE *pcinfofp = NULL; /* output file for --pcinfo */
95 FILE *psinfofp = NULL; /* output file for --psinfo */
96 FILE *rinfofp = NULL; /* output file for --rinfo */
97 FILE *icinfofp = NULL; /* output file for --icinfo */
98 FILE *listfp = NULL; /* output file for --list */
99 FILE *cinfofp = NULL; /* output file for --cinfo */
100 FILE *bpinfofp = NULL; /* output file for --bpinfo */
101 int use_weights; /* TRUE if --weight, reported weighted counts (using msa->wgt) to all output files */
102 int weights_exist; /* TRUE if at least one msa->wgt value differs from 1.0, FALSE if not (or if msa->wgt==NULL) */
103
104 /***********************************************
105 * Parse command line
106 ***********************************************/
107
108 go = esl_getopts_Create(options);
109 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK ||
110 esl_opt_VerifyConfig(go) != eslOK)
111 {
112 printf("Failed to parse command line: %s\n", go->errbuf);
113 esl_usage(stdout, argv[0], usage);
114 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
115 exit(1);
116 }
117
118 if (esl_opt_GetBoolean(go, "-h") )
119 {
120 esl_banner(stdout, argv[0], banner);
121 esl_usage (stdout, argv[0], usage);
122 puts("\n where options are:");
123 esl_opt_DisplayHelp(stdout, go, 1, 2, 80);
124 puts("\n small memory mode, requires --amino,--dna, or --rna and --informat pfam:");
125 esl_opt_DisplayHelp(stdout, go, 2, 2, 80);
126 puts("\n optional output files:");
127 esl_opt_DisplayHelp(stdout, go, 3, 2, 80);
128 exit(0);
129 }
130
131 if (esl_opt_ArgNumber(go) != 1)
132 {
133 printf("Incorrect number of command line arguments.\n");
134 esl_usage(stdout, argv[0], usage);
135 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
136 exit(1);
137 }
138
139 alifile = esl_opt_GetArg(go, 1);
140
141 if (esl_opt_IsOn(go, "--informat") &&
142 (fmt = esl_msafile_EncodeFormat(esl_opt_GetString(go, "--informat"))) == eslMSAFILE_UNKNOWN)
143 esl_fatal("%s is not a valid input sequence file format for --informat", esl_opt_GetString(go, "--informat"));
144
145 if (esl_opt_GetBoolean(go, "--small") && fmt != eslMSAFILE_PFAM) esl_fatal("--small requires --informat pfam\n");
146
147 max_comparisons = 1000;
148
149 do_stall = esl_opt_GetBoolean(go, "--stall"); /* a stall point for attaching gdb */
150 while (do_stall);
151
152 /***********************************************
153 * Open the MSA file; determine alphabet; set for digital input
154 ***********************************************/
155
156 if (esl_opt_GetBoolean(go, "--amino")) abc = esl_alphabet_Create(eslAMINO);
157 else if (esl_opt_GetBoolean(go, "--dna")) abc = esl_alphabet_Create(eslDNA);
158 else if (esl_opt_GetBoolean(go, "--rna")) abc = esl_alphabet_Create(eslRNA);
159
160 /* We'd like to get rid of the legacy msafile interface, but it
161 * includes small memory functionality for Pfam format which we have
162 * to replace first. For now, use both interfaces, new and legacy
163 */
164 if ( esl_opt_GetBoolean(go, "--small") )
165 {
166 if (! abc) esl_fatal("--small requires one of --amino, --dna, --rna be specified.");
167
168 status = esl_msafile2_OpenDigital(abc, alifile, NULL, &old_afp);
169 if (status == eslENOTFOUND) esl_fatal("Alignment file %s doesn't exist or is not readable\n", alifile);
170 else if (status == eslEFORMAT) esl_fatal("Couldn't determine format of alignment %s\n", alifile);
171 else if (status != eslOK) esl_fatal("Alignment file open failed with error %d\n", status);
172 }
173 else
174 {
175 if ( (status = esl_msafile_Open(&abc, alifile, NULL, fmt, NULL, &afp)) != eslOK)
176 esl_msafile_OpenFailure(afp, status);
177 }
178
179
180
181 /**************************************
182 * Open optional output files, as nec *
183 **************************************/
184 /* determine name for first list file, if nec */
185 if( esl_opt_IsOn(go, "--list")) {
186 if ((listfp = fopen(esl_opt_GetString(go, "--list"), "w")) == NULL)
187 esl_fatal("Failed to open --list output file %s\n", esl_opt_GetString(go, "--list"));
188 }
189 if( esl_opt_IsOn(go, "--icinfo")) {
190 if ((icinfofp = fopen(esl_opt_GetString(go, "--icinfo"), "w")) == NULL)
191 esl_fatal("Failed to open --icinfo output file %s\n", esl_opt_GetString(go, "--icinfo"));
192 }
193 if( esl_opt_IsOn(go, "--rinfo")) {
194 if ((rinfofp = fopen(esl_opt_GetString(go, "--rinfo"), "w")) == NULL)
195 esl_fatal("Failed to open --rinfo output file %s\n", esl_opt_GetString(go, "--rinfo"));
196 }
197 if( esl_opt_IsOn(go, "--pcinfo")) {
198 if ((pcinfofp = fopen(esl_opt_GetString(go, "--pcinfo"), "w")) == NULL)
199 esl_fatal("Failed to open --pcinfo output file %s\n", esl_opt_GetString(go, "--pcinfo"));
200 }
201 if( esl_opt_IsOn(go, "--psinfo")) {
202 if ((psinfofp = fopen(esl_opt_GetString(go, "--psinfo"), "w")) == NULL)
203 esl_fatal("Failed to open --psinfo output file %s\n", esl_opt_GetString(go, "--psinfo"));
204 }
205 if( esl_opt_IsOn(go, "--iinfo")) {
206 if ((iinfofp = fopen(esl_opt_GetString(go, "--iinfo"), "w")) == NULL)
207 esl_fatal("Failed to open --iinfo output file %s\n", esl_opt_GetString(go, "--iinfo"));
208 }
209 if( esl_opt_IsOn(go, "--cinfo")) {
210 if ((cinfofp = fopen(esl_opt_GetString(go, "--cinfo"), "w")) == NULL)
211 esl_fatal("Failed to open --cinfo output file %s\n", esl_opt_GetString(go, "--cinfo"));
212 }
213 if( esl_opt_IsOn(go, "--bpinfo")) {
214 if ((bpinfofp = fopen(esl_opt_GetString(go, "--bpinfo"), "w")) == NULL)
215 esl_fatal("Failed to open --bpinfo output file %s\n", esl_opt_GetString(go, "--bpinfo"));
216 }
217
218 /***********************************************
219 * Read MSAs one at a time.
220 ***********************************************/
221
222 if (esl_opt_GetBoolean(go, "-1")) {
223 puts("#");
224 if(! esl_opt_GetBoolean(go, "--small")) {
225 printf("# %-4s %-20s %10s %7s %7s %12s %6s %6s %10s %3s\n", "idx", "name", "format", "nseq", "alen", "nres", "small", "large", "avlen", "%id");
226 printf("# %-4s %-20s %10s %7s %7s %12s %6s %6s %10s %3s\n", "----", "--------------------", "----------", "-------", "-------", "------------", "------", "------", "----------", "---");
227 }
228 else {
229 printf("# %-4s %-20s %10s %7s %7s %12s %10s\n", "idx", "name", "format", "nseq", "alen", "nres", "avlen");
230 printf("# %-4s %-20s %10s %7s %7s %12s %10s\n", "----", "--------------------", "----------", "-------", "-------", "------------", "----------");
231 }
232 }
233
234 nali = 0;
235
236 fmt = (esl_opt_GetBoolean(go, "--small") ? old_afp->format : afp->format);
237
238 while ( (status = ( esl_opt_GetBoolean(go, "--small") ?
239 esl_msafile2_ReadInfoPfam(old_afp, listfp, abc, -1, NULL, NULL, &msa, &nseq, &alen, NULL, NULL, NULL, NULL, NULL, &abc_ct, &pp_ct, NULL, NULL, NULL) :
240 esl_msafile_Read (afp, &msa))) == eslOK)
241 {
242 nali++;
243 nres = 0;
244
245 if (! esl_opt_GetBoolean(go, "--small")) {
246 nseq = msa->nseq;
247 alen = msa->alen;
248 small = large = -1;
249 for (i = 0; i < msa->nseq; i++)
250 {
251 rlen = esl_abc_dsqrlen(msa->abc, msa->ax[i]);
252 nres += rlen;
253 if (small == -1 || rlen < small) small = rlen;
254 if (large == -1 || rlen > large) large = rlen;
255 }
256
257 esl_dst_XAverageId(abc, msa->ax, msa->nseq, max_comparisons, &avgid);
258 }
259 else { /* --small invoked */
260 for(i = 0; i < alen; i++) nres += (int) esl_vec_DSum(abc_ct[i], abc->K);
261 }
262
263 if (esl_opt_GetBoolean(go, "-1"))
264 {
265 printf("%-6d %-20s %10s %7d %7" PRId64 " %12" PRId64,
266 nali,
267 msa->name != NULL ? msa->name : "(null)",
268 esl_msafile_DecodeFormat(fmt),
269 nseq,
270 alen,
271 nres);
272 if (! esl_opt_GetBoolean(go, "--small")) {
273 printf(" %6" PRId64 " %6" PRId64 " %10.1f %3.0f\n",
274 small,
275 large,
276 (double) nres / (double) msa->nseq,
277 100.*avgid);
278 }
279 else {
280 printf(" %10.1f\n", (double) nres / (double) nseq);
281 }
282 }
283 else
284 {
285 printf("Alignment number: %d\n", nali);
286 if (msa->name != NULL)
287 printf("Alignment name: %s\n", msa->name);
288 printf("Format: %s\n", esl_msafile_DecodeFormat(fmt));
289 printf("Number of sequences: %d\n", nseq);
290 printf("Alignment length: %" PRId64 "\n", alen);
291 printf("Total # residues: %" PRId64 "\n", nres);
292 if(! esl_opt_GetBoolean(go, "--small")) {
293 printf("Smallest: %" PRId64 "\n", small);
294 printf("Largest: %" PRId64 "\n", large);
295 }
296 printf("Average length: %.1f\n", (double) nres / (double) nseq);
297 if(! esl_opt_GetBoolean(go, "--small")) {
298 printf("Average identity: %.0f%%\n", 100.*avgid);
299 }
300 printf("//\n");
301 }
302
303 /* Dump data to optional output files, if nec */
304 if(esl_opt_IsOn(go, "--list")) {
305 if(! esl_opt_GetBoolean(go, "--small")) {
306 /* only print sequence name to list file if ! --small, else we already have in esl_msafile2_ReadInfoPfam() */
307 for(i = 0; i < msa->nseq; i++) fprintf(listfp, "%s\n", msa->sqname[i]);
308 }
309 }
310
311 /* if RF exists, get i_am_rf array[0..alen] which tells us which positions are non-gap RF positions
312 * and rf2a_map, a map of non-gap RF positions to overall alignment positions */
313 if(msa->rf != NULL) {
314 if((status = map_rfpos_to_apos(msa, abc, errbuf, alen, &i_am_rf, &rf2a_map, &rflen)) != eslOK) esl_fatal(errbuf);
315 }
316 else i_am_rf = NULL;
317
318 weights_exist = check_msa_weights(msa);
319 use_weights = (weights_exist && esl_opt_GetBoolean(go, "--weight")) ? TRUE : FALSE;
320
321 if( (! esl_opt_GetBoolean(go, "--small")) &&
322 (esl_opt_IsOn(go, "--icinfo") || esl_opt_IsOn(go, "--rinfo") || esl_opt_IsOn(go, "--pcinfo") ||
323 esl_opt_IsOn(go, "--cinfo") || esl_opt_IsOn(go, "--bpinfo"))) {
324 /* collect counts of each residue and PPs (if they exist) from the msa */
325 if(esl_opt_GetBoolean(go, "--weight") && msa->wgt == NULL) esl_fatal("--weight requires all alignments have #=GS WT annotation, but aln %d does not", nali);
326 if((status = count_msa(msa, errbuf, nali,
327 esl_opt_GetBoolean(go, "--noambig"), /* ignore ambiguous residues? */
328 esl_opt_GetBoolean(go, "--weight"), /* use msa->wgt sequence weights? */
329 &abc_ct,
330 ((bpinfofp != NULL && msa->ss_cons != NULL) ? &bp_ct : NULL), /* get basepair counts? */
331 (msa->pp != NULL ? &pp_ct : NULL))) /* get PP counts? */
332 != eslOK) esl_fatal(errbuf);
333 }
334
335 if( esl_opt_IsOn(go, "--icinfo")) {
336 if((status = dump_infocontent_info(icinfofp, abc, abc_ct, use_weights, nali, alen, nseq, i_am_rf, msa->name, alifile, errbuf) != eslOK)) esl_fatal(errbuf);
337 }
338 if( esl_opt_IsOn(go, "--rinfo")) {
339 if((status = dump_residue_info(rinfofp, abc, abc_ct, use_weights, nali, alen, nseq, i_am_rf, msa->name, alifile, errbuf) != eslOK)) esl_fatal(errbuf);
340 }
341 if(esl_opt_IsOn(go, "--pcinfo")) {
342 if(pp_ct == NULL) esl_fatal("Error: --pcinfo requires all alignments have #=GR PP annotation, but alignment %d does not", nali);
343 if((status = dump_posterior_column_info(pcinfofp, pp_ct, use_weights, nali, alen, nseq, i_am_rf, msa->name, alifile, errbuf) != eslOK)) esl_fatal(errbuf);
344 }
345 if(esl_opt_IsOn(go, "--psinfo")) {
346 if(msa->pp == NULL) esl_fatal("Error: --psinfo requires all alignments have #=GR PP annotation, but alignment %d does not", nali);
347 if((status = dump_posterior_sequence_info(psinfofp, msa, nali, alifile, errbuf) != eslOK)) esl_fatal(errbuf);
348 }
349 if( esl_opt_IsOn(go, "--iinfo")) {
350 if(msa->rf == NULL) esl_fatal("--iinfo requires all alignments have #=GC RF annotation, but alignment %d does not", nali);
351 if(esl_opt_GetBoolean(go, "--weight") && msa->wgt == NULL) esl_fatal("--weight requires all alignments have #=GS WT annotation, but aln %d does not", nali);
352 if((status = dump_insert_info(iinfofp, msa, use_weights, nali, i_am_rf, alifile, errbuf) != eslOK)) esl_fatal(errbuf);
353 }
354 if( esl_opt_IsOn(go, "--cinfo")) {
355 if((status = dump_column_residue_counts(cinfofp, abc, abc_ct, esl_opt_GetBoolean(go, "--noambig"), use_weights, nali, alen, nseq, msa->name, alifile, errbuf) != eslOK)) esl_fatal(errbuf);
356 }
357 if( esl_opt_IsOn(go, "--bpinfo")) {
358 if(msa->ss_cons == NULL) esl_fatal("--bpinfo requires all alignments have #=GC SS_cons annotation, but alignment %d does not", nali);
359 if((status = dump_basepair_counts(bpinfofp, msa, abc, bp_ct, use_weights, nali, nseq, msa->name, alifile, errbuf) != eslOK)) esl_fatal(errbuf);
360 }
361
362 esl_msa_Destroy(msa);
363 esl_arr2_Destroy((void **) abc_ct, alen); abc_ct = NULL;
364 esl_arr2_Destroy((void **) pp_ct, alen); pp_ct = NULL;
365 esl_arr3_Destroy((void ***) bp_ct, alen, abc ? abc->Kp : 0); bp_ct = NULL;
366 esl_free(i_am_rf); i_am_rf = NULL;
367 esl_free(rf2a_map); rf2a_map = NULL;
368 }
369
370 /* If an msa read failed, we've dropped out to here with an informative status code.
371 * we have to handle failures from new vs. legacy msa parsing differently
372 */
373 if (esl_opt_GetBoolean(go, "--small"))
374 {
375 if (status == eslEFORMAT) esl_fatal("Alignment file parse error, line %d of file %s:\n%s\nOffending line is:\n%s\n", old_afp->linenumber, old_afp->fname, old_afp->errbuf, old_afp->buf);
376 else if (status != eslEOF) esl_fatal("Alignment file read failed with error code %d\n", status);
377 else if (nali == 0) esl_fatal("No alignments found in file %s\n", alifile);
378 }
379 else
380 {
381 if (nali == 0 || status != eslEOF) esl_msafile_ReadFailure(afp, status);
382 }
383
384 /* Cleanup, normal return
385 */
386 if(listfp != NULL) {
387 fclose(listfp);
388 printf("# List of sequences in %d alignment(s) saved to file %s\n", nali, esl_opt_GetString(go, "--list"));
389 }
390 if(icinfofp != NULL) {
391 fclose(icinfofp);
392 printf("# Information content data saved to file %s.\n", esl_opt_GetString(go, "--icinfo"));
393 }
394 if(rinfofp != NULL) {
395 fclose(rinfofp);
396 printf("# Residue data saved to file %s.\n", esl_opt_GetString(go, "--rinfo"));
397 }
398 if(pcinfofp != NULL) {
399 fclose(pcinfofp);
400 printf("# Per-column posterior probability data saved to file %s.\n", esl_opt_GetString(go, "--pcinfo"));
401 }
402 if(psinfofp != NULL) {
403 fclose(psinfofp);
404 printf("# Per-sequence posterior probability data saved to file %s.\n", esl_opt_GetString(go, "--psinfo"));
405 }
406 if(iinfofp != NULL) {
407 printf("# Insert data saved to file %s.\n", esl_opt_GetString(go, "--iinfo"));
408 fclose(iinfofp);
409 }
410 if(cinfofp != NULL) {
411 printf("# Per-column counts data saved to file %s.\n", esl_opt_GetString(go, "--cinfo"));
412 fclose(cinfofp);
413 }
414 if(bpinfofp != NULL) {
415 printf("# Per-column basepair counts data saved to file %s.\n", esl_opt_GetString(go, "--bpinfo"));
416 fclose(bpinfofp);
417 }
418
419
420 if (afp) esl_msafile_Close(afp);
421 if (old_afp) esl_msafile2_Close(old_afp);
422 esl_alphabet_Destroy(abc);
423 esl_getopts_Destroy(go);
424 return 0;
425 }
426
427 /* count_msa()
428 *
429 * Given an msa, count residues, and optionally base pairs and
430 * posterior probabilities per column and store them in <ret_abc_ct>
431 * and <ret_pp_ct>.
432 *
433 * <ret_abc_ct> [0..apos..alen-1][0..abc->K]:
434 * - per position count of each symbol in alphabet over all seqs.
435 *
436 * <ret_bp_ct> [0..apos..alen-1][0..abc->Kp-1][0..abc->Kp-1]
437 * - per (non-pknotted) consensus basepair count of each possible basepair
438 * over all seqs basepairs are indexed by 'i' the minimum of 'i:j' for a
439 * pair between i and j, where i < j. Note that non-canonicals and
440 * gaps and the like are all stored independently.
441 *
442 * <ret_pp_ct> [0..apos..alen-1][0..11]
443 * - per position count of each posterior probability code over all seqs.
444 *
445 * A 'gap' has a looser definition than in esl_abc here, esl_abc's gap,
446 * missing residues and nonresidues are all considered 'gaps' here.
447 *
448 * If we encounter an error, we return non-eslOK status and fill
449 * errbuf with error message.
450 *
451 * Returns eslOK upon success.
452 */
453 static int count_msa(ESL_MSA *msa, char *errbuf, int nali, int no_ambig, int use_weights, double ***ret_abc_ct, double ****ret_bp_ct, double ***ret_pp_ct)
454 {
455 int status;
456 double **abc_ct = NULL;
457 double ***bp_ct = NULL;
458 int apos, rpos, i, x;
459 int nppvals = 12; /* '0'-'9' = 0-9, '*' = 10, gap = '11' */
460 double **pp_ct = NULL; /* [0..alen-1][0..nppvals-1] per position count of each possible PP char over all seqs */
461 int ppidx;
462 /* variables related to getting bp counts */
463 int *ct = NULL; /* 0..alen-1 base pair partners array for current sequence */
464 char *ss_nopseudo = NULL; /* no-pseudoknot version of structure */
465 double seqwt; /* weight of current sequence, always 1.0 if !use_weights */
466
467 if(! (msa->flags & eslMSA_DIGITAL)) ESL_FAIL(eslEINVAL, errbuf, "count_msa() contract violation, MSA is not digitized");
468 if(use_weights && msa->wgt == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "count_msa(): use_weights==TRUE but msa->wgt == NULL");
469
470 /* allocate pp_ct array, if nec */
471 if(ret_pp_ct != NULL) {
472 if(msa->pp == NULL) ESL_FAIL(eslEINVAL, errbuf, "count_msa() ret_pp_ct != NULL, but msa->pp is NULL");
473 ESL_ALLOC(pp_ct, sizeof(double *) * msa->alen);
474 for(apos = 0; apos < msa->alen; apos++) {
475 ESL_ALLOC(pp_ct[apos], sizeof(double) * nppvals);
476 esl_vec_DSet(pp_ct[apos], nppvals, 0.);
477 }
478 }
479
480 /* allocate and initialize bp_ct, if nec */
481 if(ret_bp_ct != NULL) {
482 ESL_ALLOC(bp_ct, sizeof(double **) * msa->alen);
483 /* get ct array which defines the consensus base pairs */
484 ESL_ALLOC(ct, sizeof(int) * (msa->alen+1));
485 ESL_ALLOC(ss_nopseudo, sizeof(char) * (msa->alen+1));
486 esl_wuss_nopseudo(msa->ss_cons, ss_nopseudo);
487 if ((status = esl_wuss2ct(ss_nopseudo, msa->alen, ct)) != eslOK) ESL_XFAIL(status, errbuf, "Consensus structure string is inconsistent.");
488 for(apos = 0; apos < msa->alen; apos++) {
489 /* careful ct is indexed 1..alen, not 0..alen-1 */
490 if(ct[(apos+1)] > (apos+1)) { /* apos+1 is an 'i' in an i:j pair, where i < j */
491 ESL_ALLOC(bp_ct[apos], sizeof(double *) * (msa->abc->Kp));
492 for(x = 0; x < msa->abc->Kp; x++) {
493 ESL_ALLOC(bp_ct[apos][x], sizeof(double) * (msa->abc->Kp));
494 esl_vec_DSet(bp_ct[apos][x], msa->abc->Kp, 0.);
495 }
496 }
497 else { /* apos+1 is not an 'i' in an i:j pair, where i < j, set to NULL */
498 bp_ct[apos] = NULL;
499 }
500 }
501 }
502
503 ESL_ALLOC(abc_ct, sizeof(double *) * msa->alen);
504 for(apos = 0; apos < msa->alen; apos++) {
505 ESL_ALLOC(abc_ct[apos], sizeof(double) * (msa->abc->K+1));
506 esl_vec_DSet(abc_ct[apos], (msa->abc->K+1), 0.);
507 }
508
509 for(i = 0; i < msa->nseq; i++) {
510 seqwt = use_weights ? msa->wgt[i] : 1.0;
511
512 for(apos = 0; apos < msa->alen; apos++) { /* update appropriate abc count, careful, ax ranges from 1..msa->alen (but abc_ct is 0..msa->alen-1) */
513 if((! no_ambig) || (! esl_abc_XIsDegenerate(msa->abc, msa->ax[i][apos+1]))) { /* skip ambiguities (degenerate residues) if no_ambig is TRUE */
514 if((status = esl_abc_DCount(msa->abc, abc_ct[apos], msa->ax[i][apos+1], seqwt)) != eslOK) ESL_XFAIL(status, errbuf, "problem counting residue %d of seq %d", apos, i);
515 }
516 }
517
518 /* get bp counts, if nec */
519 if(bp_ct != NULL) {
520 for(apos = 0; apos < msa->alen; apos++) { /* update appropriate abc count, careful, ax ranges from 1..msa->alen (but abc_ct is 0..msa->alen-1) */
521 if(bp_ct[apos] != NULL) { /* our flag for whether position (apos+1) is an 'i' in an i:j pair where i < j */
522 rpos = ct[apos+1] - 1; /* ct is indexed 1..alen */
523 bp_ct[apos][msa->ax[i][apos+1]][msa->ax[i][rpos+1]] += seqwt;
524 }
525 }
526 }
527
528 /* get PP counts, if nec */
529 if(pp_ct != NULL) {
530 if(msa->pp[i] != NULL) {
531 for(apos = 0; apos < msa->alen; apos++) {
532 if((! no_ambig) || (! esl_abc_XIsDegenerate(msa->abc, msa->ax[i][apos+1]))) { /* skip ambiguities (degenerate residues) if no_ambig is TRUE */
533 if((ppidx = get_pp_idx(msa->abc, msa->pp[i][apos])) == -1) ESL_XFAIL(eslEFORMAT, errbuf, "bad #=GR PP char: %c", msa->pp[i][apos]);
534 pp_ct[apos][ppidx] += seqwt;
535 }
536 }
537 }
538 }
539 }
540
541 *ret_abc_ct = abc_ct;
542 if (ret_bp_ct) *ret_bp_ct = bp_ct; /* we only allocated bp_ct if ret_bp_ct != NULL */
543 if (ret_pp_ct) *ret_pp_ct = pp_ct; /* we only allocated pp_ct if ret_pp_ct != NULL */
544
545 esl_free(ss_nopseudo);
546 esl_free(ct);
547 return eslOK;
548
549 ERROR:
550 esl_arr2_Destroy((void **) abc_ct, msa? msa->alen:0);
551 esl_arr3_Destroy((void ***) bp_ct, msa? msa->alen:0, msa && msa->abc ? msa->abc->Kp: 0);
552 esl_arr2_Destroy((void **) pp_ct, msa? msa->alen:0);
553 esl_free(ss_nopseudo);
554 esl_free(ct);
555
556 *ret_abc_ct = NULL;
557 if (ret_bp_ct) *ret_bp_ct = NULL;
558 if (ret_pp_ct) *ret_pp_ct = NULL;
559 return status;
560 }
561
562
563 /* get_pp_idx
564 *
565 * Given a #=GR PP or #=GC PP_cons character, return the appropriate index
566 * in a pp_ct[] vector.
567 * '0' return 0;
568 * '1' return 1;
569 * '2' return 2;
570 * '3' return 3;
571 * '4' return 4;
572 * '5' return 5;
573 * '6' return 6;
574 * '7' return 7;
575 * '8' return 8;
576 * '9' return 9;
577 * '*' return 10;
578 * gap return 11;
579 *
580 * Anything else (including missing or nonresidue) return -1;
581 */
582 static int get_pp_idx(ESL_ALPHABET *abc, char ppchar)
583 {
584 if(esl_abc_CIsGap(abc, ppchar)) return 11;
585 if(ppchar == '*') return 10;
586 if(ppchar == '9') return 9;
587 if(ppchar == '8') return 8;
588 if(ppchar == '7') return 7;
589 if(ppchar == '6') return 6;
590 if(ppchar == '5') return 5;
591 if(ppchar == '4') return 4;
592 if(ppchar == '3') return 3;
593 if(ppchar == '2') return 2;
594 if(ppchar == '1') return 1;
595 if(ppchar == '0') return 0;
596 return -1;
597 }
598
599
600 /* dump_infocontent_info
601 *
602 * Given an MSA with RF annotation, dump information content per column data to
603 * an open output file.
604 */
605 static int dump_infocontent_info(FILE *fp, ESL_ALPHABET *abc, double **abc_ct, int use_weights, int nali, int64_t alen, int nseq, int *i_am_rf, char *msa_name, char *alifile, char *errbuf)
606 {
607 int status;
608 int apos, rfpos;
609 double bg_ent;
610 double *bg = NULL;
611 double *abc_freq = NULL;
612 double nnongap;
613
614 ESL_ALLOC(bg, sizeof(double) * abc->K);
615 esl_vec_DSet(bg, abc->K, 1./(abc->K));
616 bg_ent = esl_vec_DEntropy(bg, abc->K);
617 free(bg);
618
619 ESL_ALLOC(abc_freq, sizeof(double) * abc->K);
620
621
622 fprintf(fp, "# Information content per column (bits):\n");
623 fprintf(fp, "# Alignment file: %s\n", alifile);
624 fprintf(fp, "# Alignment idx: %d\n", nali);
625 if(msa_name != NULL) { fprintf(fp, "# Alignment name: %s\n", msa_name); }
626 fprintf(fp, "# Number of sequences: %d\n", nseq);
627 if(use_weights) { fprintf(fp, "# IMPORTANT: Counts are weighted based on sequence weights in alignment file.\n"); }
628 else { fprintf(fp, "# Sequence weights from alignment were ignored (if they existed).\n"); }
629 fprintf(fp, "#\n");
630
631 if(i_am_rf != NULL) {
632 fprintf(fp, "# %7s %7s %10s %10s\n", "rfpos", "alnpos", "freqnongap", "info(bits)");
633 fprintf(fp, "# %7s %7s %10s %10s\n", "-------", "-------", "----------", "----------");
634 }
635 else {
636 fprintf(fp, "# %7s %10s %10s\n", "alnpos", "freqnongap", "info(bits)");
637 fprintf(fp, "# %7s %10s %10s\n", "-------", "----------", "----------");
638 }
639
640 rfpos = 0;
641 for(apos = 0; apos < alen; apos++) {
642 if(i_am_rf != NULL) {
643 if(i_am_rf[apos]) {
644 fprintf(fp, " %7d", rfpos+1);
645 rfpos++;
646 }
647 else {
648 fprintf(fp, " %7s", "-");
649 }
650 }
651 nnongap = esl_vec_DSum(abc_ct[apos], abc->K);
652 esl_vec_DCopy(abc_ct[apos], abc->K, abc_freq);
653 esl_vec_DNorm(abc_freq, abc->K);
654 fprintf(fp, " %7d %10.8f %10.8f\n", apos+1,
655 nnongap / (nnongap + abc_ct[apos][abc->K]),
656 (bg_ent - esl_vec_DEntropy(abc_freq, abc->K)));
657 }
658 fprintf(fp, "//\n");
659
660 if(abc_freq != NULL) free(abc_freq);
661
662 return eslOK;
663
664 ERROR:
665 ESL_FAIL(eslEINVAL, errbuf, "out of memory");
666 return status; /* NEVERREACHED */
667 }
668
669
670 /* dump_residue_info
671 *
672 * Given an MSA, print out the number of sequences with
673 * a non-gap residue in each column of the alignment.
674 */
675 static int dump_residue_info(FILE *fp, ESL_ALPHABET *abc, double **abc_ct, int use_weights, int nali, int64_t alen, int nseq, int *i_am_rf, char *msa_name, char *alifile, char *errbuf)
676 {
677 int apos, rfpos;
678 double rct, gct;
679
680 fprintf(fp, "# Insert information:\n");
681 fprintf(fp, "# Alignment file: %s\n", alifile);
682 fprintf(fp, "# Alignment idx: %d\n", nali);
683 if(msa_name != NULL) { fprintf(fp, "# Alignment name: %s\n", msa_name); }
684 fprintf(fp, "# Number of sequences: %d\n", nseq);
685 if(use_weights) { fprintf(fp, "# IMPORTANT: Counts are weighted based on sequence weights in alignment file.\n"); }
686 else { fprintf(fp, "# Sequence weights from alignment were ignored (if they existed).\n"); }
687 fprintf(fp, "#\n");
688 if(i_am_rf != NULL) {
689 fprintf(fp, "# %7s %7s %10s %8s %10s %8s\n", "rfpos", "alnpos", "numres", "freqres", "numgap", "freqgap");
690 fprintf(fp, "# %7s %7s %10s %8s %10s %8s\n", "-------", "-------", "----------", "--------", "----------", "--------");
691 }
692 else {
693 fprintf(fp, "# %7s %10s %8s %10s %8s\n", "alnpos", "numres", "freqres", "numgap", "freqgap");
694 fprintf(fp, "# %7s %10s %8s %10s %8s\n", "-------", "----------", "--------", "----------", "--------");
695 }
696
697 rfpos = 0;
698 for(apos = 0; apos < alen; apos++) {
699 rct = esl_vec_DSum(abc_ct[apos], abc->K);
700 gct = abc_ct[apos][abc->K];
701 if(i_am_rf != NULL) {
702 if(i_am_rf[apos]) {
703 fprintf(fp, " %7d", rfpos+1);
704 rfpos++;
705 }
706 else {
707 fprintf(fp, " %7s", "-");
708 }
709 }
710 fprintf(fp, " %7d %10.1f %8.6f %10.1f %8.6f\n", apos+1, rct, rct / (float) nseq, gct, gct / (float) nseq);
711 }
712 fprintf(fp, "//\n");
713
714 return eslOK;
715 }
716
717 /* dump_posterior_column_info
718 *
719 * Dump per-column posterior probability data to a file.
720 *
721 */
722 static int dump_posterior_column_info(FILE *fp, double **pp_ct, int use_weights, int nali, int64_t alen, int nseq, int *i_am_rf, char *msa_name, char *alifile, char *errbuf)
723 {
724 int p,apos; /* counters over sequences, columns of MSA */
725 int nppvals = 12;
726 int rfpos;
727 double nnongap;
728 double sum;
729 float ppavgA[11];
730 char ppstring[12] = "0123456789*.";
731
732 ppavgA[0] = 0.025;
733 ppavgA[1] = 0.10;
734 ppavgA[2] = 0.20;
735 ppavgA[3] = 0.30;
736 ppavgA[4] = 0.40;
737 ppavgA[5] = 0.50;
738 ppavgA[6] = 0.60;
739 ppavgA[7] = 0.70;
740 ppavgA[8] = 0.80;
741 ppavgA[9] = 0.90;
742 ppavgA[10] = 0.975;
743
744 fprintf(fp, "# Posterior probability stats per column:\n");
745 fprintf(fp, "# Alignment file: %s\n", alifile);
746 fprintf(fp, "# Alignment idx: %d\n", nali);
747 if(msa_name != NULL) { fprintf(fp, "# Alignment name: %s\n", msa_name); }
748 fprintf(fp, "# Number of sequences: %d\n", nseq);
749 if(use_weights) { fprintf(fp, "# IMPORTANT: Counts are weighted based on sequence weights in alignment file.\n"); }
750 else { fprintf(fp, "# Sequence weights from alignment were ignored (if they existed).\n"); }
751 fprintf(fp, "#\n");
752
753 fprintf(fp, "# %6s", "alnpos");
754 if(i_am_rf != NULL) fprintf(fp, " %6s", "rfpos");
755 fprintf(fp, " %9s", "nnongap");
756 for(p = 0; p < nppvals; p++) {
757 fprintf(fp, " %9c", ppstring[p]);
758 }
759 fprintf(fp, " %9s\n", "avgPP");
760
761 fprintf(fp, "# %6s %6s %9s", "------", "------", "---------");
762 for(p = 0; p < nppvals; p++) {
763 fprintf(fp, " %9s", "---------");
764 }
765 fprintf(fp, " %9s\n", "---------");
766
767 rfpos = 1;
768 for(apos = 0; apos < alen; apos++) {
769 sum = 0;
770 fprintf(fp, " %6d", apos+1);
771 if(i_am_rf != NULL) {
772 if(i_am_rf[apos]) fprintf(fp, " %6d", rfpos++);
773 else fprintf(fp, " %6s", "-");
774 }
775 nnongap = esl_vec_DSum(pp_ct[apos], 11);
776 fprintf(fp, " %9.1f", nnongap);
777 for(p = 0; p < nppvals; p++) {
778 fprintf(fp, " %9.1f", pp_ct[apos][p]);
779 if(p <= 10) sum += pp_ct[apos][p] * ppavgA[p];
780 }
781 fprintf(fp, " %.5f\n", sum / nnongap);
782 }
783 fprintf(fp, "//\n");
784
785 return eslOK;
786 }
787
788
789 /* dump_posterior_sequence_info
790 *
791 * Dump per-sequence posterior probability data to a file.
792 *
793 */
794 static int dump_posterior_sequence_info(FILE *fp, ESL_MSA *msa, int nali, char *alifile, char *errbuf)
795 {
796 int i,p,apos; /* counters over sequences, columns of MSA */
797 int ppidx;
798 int nppvals = 12;
799 int nnongap;
800 double sum;
801 float ppavgA[11];
802 char ppstring[12] = "0123456789*.";
803 int seq_pp_ct[12];
804
805 ppavgA[0] = 0.025;
806 ppavgA[1] = 0.10;
807 ppavgA[2] = 0.20;
808 ppavgA[3] = 0.30;
809 ppavgA[4] = 0.40;
810 ppavgA[5] = 0.50;
811 ppavgA[6] = 0.60;
812 ppavgA[7] = 0.70;
813 ppavgA[8] = 0.80;
814 ppavgA[9] = 0.90;
815 ppavgA[10] = 0.975;
816
817 fprintf(fp, "# Posterior probability stats per sequence:\n");
818 fprintf(fp, "# Alignment file: %s\n", alifile);
819 fprintf(fp, "# Alignment idx: %d\n", nali);
820 if(msa->name != NULL) { fprintf(fp, "# Alignment name: %s\n", msa->name); }
821 fprintf(fp, "# Number of sequences: %d\n", msa->nseq);
822 fprintf(fp, "# %7s %-40s %7s", "seqidx", "seqname", "nnongap");
823 for(p = 0; p < nppvals-1; p++) { /* don't include gaps in per-sequence output */
824 fprintf(fp, " %7c", ppstring[p]);
825 }
826 fprintf(fp, " %7s\n", "avgPP");
827
828 fprintf(fp, "# %7s %40s %7s", "-------", "----------------------------------------", "-------");
829 for(p = 0; p < nppvals-1; p++) { /* don't include gaps in per-sequence output */
830 fprintf(fp, " %7s", "-------");
831 }
832 fprintf(fp, " %7s\n", "-------");
833
834 for(i = 0; i < msa->nseq; i++) {
835 if(msa->pp[i] != NULL) {
836 fprintf(fp, " %7d %-40s", i+1, msa->sqname[i]);
837 sum = 0.;
838 esl_vec_ISet(seq_pp_ct, nppvals, 0);
839 for(apos = 0; apos < msa->alen; apos++) {
840 if((ppidx = get_pp_idx(msa->abc, msa->pp[i][apos])) == -1) ESL_FAIL(eslEFORMAT, errbuf, "bad #=GR PP char: %c", msa->pp[i][apos]);
841 seq_pp_ct[ppidx]++;
842 }
843 nnongap = esl_vec_ISum(seq_pp_ct, 11);
844 fprintf(fp, " %7d", nnongap);
845 for(p = 0; p < nppvals-1; p++) { /* don't include gaps in per-sequence output */
846 fprintf(fp, " %7d", seq_pp_ct[p]);
847 if(p <= 10) sum += (float) seq_pp_ct[p] * ppavgA[p];
848 }
849 fprintf(fp, " %.5f\n", sum / (float) nnongap);
850 }
851 }
852 fprintf(fp, "//\n");
853
854 return eslOK;
855 }
856
857
858 /* dump_insert_info
859 *
860 * Given an MSA with RF annotation, print out information about how many 'insertions' come
861 * after each non-gap RF column (consensus column).
862 */
863 static int dump_insert_info(FILE *fp, ESL_MSA *msa, int use_weights, int nali, int *i_am_rf, char *alifile, char *errbuf)
864 {
865 double **ict = NULL;
866 double *total_ict = NULL;
867 int apos, rfpos;
868 int i;
869 int rflen;
870 double seqwt; /* weight of current sequence */
871 double nseq;
872 int status;
873
874 /* contract checks */
875 if (! (msa->flags & eslMSA_DIGITAL)) ESL_FAIL(eslEINVAL, errbuf, "in dump_insert_info(), msa must be digitized.");
876 if (msa->rf == NULL) ESL_FAIL(eslEINVAL, errbuf, "No #=GC RF markup in alignment, it is needed for --iinfo.");
877 if (i_am_rf == NULL) ESL_FAIL(eslEINVAL, errbuf, "internal error, dump_insert_info() i_am_rf is NULL.");
878 if (use_weights && msa->wgt == NULL) ESL_FAIL(eslEINCOMPAT, errbuf, "dump_insert_info(): use_weights==TRUE but msa->wgt == NULL");
879
880 ESL_ALLOC(total_ict, sizeof(double) * (msa->alen+2));
881 esl_vec_DSet(total_ict, (msa->alen+2), 0.);
882
883 ESL_ALLOC(ict, sizeof(double *) * (msa->alen+2));
884 for (i = 0; i <= msa->alen; i++) ict[i] = NULL;
885
886 for (i = 0; i <= msa->alen; i++)
887 {
888 ESL_ALLOC(ict[i], sizeof(double) * (msa->nseq));
889 esl_vec_DSet(ict[i], (msa->nseq), 0.);
890 }
891
892 fprintf(fp, "# Insert information:\n");
893 fprintf(fp, "# Alignment file: %s\n", alifile);
894 fprintf(fp, "# Alignment idx: %d\n", nali);
895 if (msa->name) { fprintf(fp, "# Alignment name: %s\n", msa->name); }
896 fprintf(fp, "# rfpos is the nongap RF position after which insertions occur\n");
897 fprintf(fp, "# An rfpos of '0' indicates insertions before the first nongap RF position\n");
898 fprintf(fp, "# Number of sequences: %d\n", msa->nseq);
899 if (use_weights) { fprintf(fp, "# IMPORTANT: Counts are weighted based on sequence weights in alignment file.\n"); }
900 else { fprintf(fp, "# Sequence weights from alignment were ignored (if they existed).\n"); }
901 fprintf(fp, "#\n");
902
903 fprintf(fp, "# %8s %10s %8s %8s\n", "rfpos", "nseq w/ins", "freq ins", "avg len");
904 fprintf(fp, "# %8s %10s %8s %8s\n", "--------", "----------", "--------", "--------");
905
906 rflen = 0;
907 for(apos = 1; apos <= msa->alen; apos++)
908 if (i_am_rf[apos-1]) rflen++;
909
910 rfpos = 0;
911 for (apos = 1; apos <= msa->alen; apos++)
912 {
913 if (i_am_rf[apos-1]) rfpos++;
914 else {
915 for(i = 0; i < msa->nseq; i++) {
916 seqwt = use_weights ? msa->wgt[i] : 1.0;
917 if(esl_abc_XIsResidue(msa->abc, msa->ax[i][apos])) {
918 ict[rfpos][i]++;
919 total_ict[rfpos] += seqwt;
920 }
921 }
922 }
923 }
924 rflen = rfpos;
925
926 for(rfpos = 0; rfpos <= rflen; rfpos++)
927 {
928 nseq = 0.;
929 for(i = 0; i < msa->nseq; i++) {
930 if(ict[rfpos][i] >= 1) {
931 seqwt = use_weights ? msa->wgt[i] : 1.0;
932 nseq += seqwt;
933 }
934 }
935 if(nseq > 0.)
936 fprintf(fp, " %8d %10.1f %8.6f %8.3f\n", rfpos, nseq, nseq / (float) msa->nseq, ((float) total_ict[rfpos] / (float) nseq));
937 }
938 fprintf(fp, "//\n");
939
940 for(i = 0; i <= msa->alen; i++) free(ict[i]);
941 free(ict);
942 free(total_ict);
943 return eslOK;
944
945 ERROR:
946 if (ict) {
947 for (i = 0; i <= msa->alen; i++)
948 if (ict[i]) free(ict[i]);
949 free(ict);
950 }
951 if (total_ict) free(total_ict);
952 return status;
953 }
954
955 /* dump_column_residue_counts
956 *
957 * Dump per-column residue counts from abc_ct[][] to
958 * an open output file.
959 *
960 * abc_ct: [0..msa->alen-1][0..abc->K] number of each residue at each position (abc->K is gap)
961 */
962 static int dump_column_residue_counts(FILE *fp, ESL_ALPHABET *abc, double **abc_ct, int no_ambig, int use_weights, int nali, int64_t alen, int nseq, char *msa_name, char *alifile, char *errbuf)
963 {
964 int apos;
965 int i;
966
967 fprintf(fp, "# Per column residue counts:\n");
968 fprintf(fp, "# Alignment file: %s\n", alifile);
969 fprintf(fp, "# Alignment idx: %d\n", nali);
970 if(msa_name != NULL) { fprintf(fp, "# Alignment name: %s\n", msa_name); }
971 fprintf(fp, "# Number of sequences: %d\n", nseq);
972 if(no_ambig) {
973 fprintf(fp, "# Ambiguous residues were not counted.\n");
974 }
975 else {
976 if(abc->type == eslRNA) fprintf(fp, "# Ambiguities were averaged (e.g. 1 'N' = 0.25 'A', 0.25 'C', 0.25 'G' and 0.25 'U')\n");
977 if(abc->type == eslDNA) fprintf(fp, "# Ambiguities were averaged (e.g. 1 'N' = 0.25 'A', 0.25 'C', 0.25 'G' and 0.25 'T')\n");
978 if(abc->type == eslAMINO) fprintf(fp, "# Ambiguities were averaged (e.g. 1 'X' = 0.05 each for all 20 amino acids\n");
979 }
980 if(use_weights) { fprintf(fp, "# IMPORTANT: Counts are weighted based on sequence weights in alignment file.\n"); }
981 else { fprintf(fp, "# Sequence weights from alignment were ignored (if they existed).\n"); }
982 fprintf(fp, "#\n");
983
984 fprintf(fp, "# %7s", "alnpos");
985 for(i = 0; i < abc->K; i++) fprintf(fp, " %c ", abc->sym[i]);
986 fprintf(fp, "\n");
987
988 fprintf(fp, "# %7s", "-------");
989 for(i = 0; i < abc->K; i++) fprintf(fp, " %7s", "-------");
990 fprintf(fp, "\n");
991
992 for(apos = 0; apos < alen; apos++) {
993 fprintf(fp, " %7d", apos+1);
994 for(i = 0; i < abc->K; i++) fprintf(fp, " %7.1f", abc_ct[apos][i]);
995 fprintf(fp, "\n");
996 }
997 fprintf(fp, "//\n");
998
999 return eslOK;
1000
1001 }
1002
1003 /* dump_basepair_counts
1004 *
1005 * Dump per-basepaired-column basepair counts from bp_ct[][][] to
1006 * an open output file. Only pairs involving canonical residues
1007 * are printed. (i.e. for RNA: AA,AC,AG,AU, CA,CC,CG,CU, GA,GC,GG,GU,
1008 * UA,UC,UG,UU).
1009 *
1010 * <bp_ct> [0..apos..alen-1][0..abc->Kp-1][0..abc->Kp-1]
1011 * - per (non-pknotted) consensus basepair count of each possible basepair
1012 * over all seqs basepairs are indexed by 'i' the minimum of 'i:j' for a
1013 * pair between i and j, where i < j. Note that non-canonicals and
1014 * gaps and the like are all stored independently.
1015 */
1016 static int dump_basepair_counts(FILE *fp, ESL_MSA *msa, ESL_ALPHABET *abc, double ***bp_ct, int use_weights, int nali, int nseq, char *msa_name, char *alifile, char *errbuf)
1017 {
1018 int status;
1019 int apos, rpos;
1020 int i, j;
1021
1022 int *ct = NULL; /* 0..msa->alen-1 base pair partners array for current sequence */
1023 char *ss_nopseudo = NULL; /* no-pseudoknot version of structure */
1024
1025 /* get ct array which defines the consensus base pairs */
1026 ESL_ALLOC(ct, sizeof(int) * (msa->alen+1));
1027 ESL_ALLOC(ss_nopseudo, sizeof(char) * (msa->alen+1));
1028 esl_wuss_nopseudo(msa->ss_cons, ss_nopseudo);
1029 if ((status = esl_wuss2ct(ss_nopseudo, msa->alen, ct)) != eslOK) ESL_FAIL(status, errbuf, "Consensus structure string is inconsistent.");
1030
1031 fprintf(fp, "# Per-column basepair counts:\n");
1032 fprintf(fp, "# Alignment file: %s\n", alifile);
1033 fprintf(fp, "# Alignment idx: %d\n", nali);
1034 if(msa_name != NULL) { fprintf(fp, "# Alignment name: %s\n", msa_name); }
1035 fprintf(fp, "# Number of sequences: %d\n", nseq);
1036 fprintf(fp, "# Only basepairs involving two canonical (non-degenerate) residues were counted.\n");
1037 if(use_weights) { fprintf(fp, "# IMPORTANT: Counts are weighted based on sequence weights in alignment file.\n"); }
1038 else { fprintf(fp, "# Sequence weights from alignment were ignored (if they existed).\n"); }
1039 fprintf(fp, "#\n");
1040
1041 fprintf(fp, "# %7s %7s", "lpos", "rpos");
1042 for(i = 0; i < abc->K; i++) {
1043 for(j = 0; j < abc->K; j++) {
1044 fprintf(fp, " %c%c ", abc->sym[i], abc->sym[j]);
1045 }
1046 }
1047 fprintf(fp, "\n");
1048
1049 fprintf(fp, "# %7s %7s", "-------", "-------");
1050 for(i = 0; i < abc->K; i++) {
1051 for(j = 0; j < abc->K; j++) {
1052 fprintf(fp, " %6s", "------");
1053 }
1054 }
1055 fprintf(fp, "\n");
1056
1057 for(apos = 0; apos < msa->alen; apos++) {
1058 if(bp_ct[apos] != NULL) {
1059 rpos = ct[(apos+1)];
1060 fprintf(fp, " %7d %7d", apos+1, rpos);
1061 for(i = 0; i < abc->K; i++) {
1062 for(j = 0; j < abc->K; j++) {
1063 fprintf(fp, " %6d", (int) bp_ct[apos][i][j]);
1064 }
1065 }
1066 fprintf(fp, "\n");
1067 }
1068 }
1069 fprintf(fp, "//\n");
1070
1071 if(ss_nopseudo != NULL) free(ss_nopseudo);
1072 if(ct != NULL) free(ct);
1073 return eslOK;
1074
1075 ERROR:
1076 if(ss_nopseudo != NULL) free(ss_nopseudo);
1077 if(ct != NULL) free(ct);
1078 ESL_FAIL(status, errbuf, "Error, out of memory while dumping basepair info");
1079 }
1080
1081 /* map_rfpos_to_apos
1082 *
1083 * Given an MSA, determine the alignment position of each
1084 * non-gap RF (reference) position. The abc is only necessary
1085 * for defining gap characters.
1086 *
1087 * rf2a_map[0..rfpos..rflen-1] = apos, apos is the alignment position (0..msa->alen-1) that
1088 * is non-gap RF position rfpos+1 (for rfpos in 0..rflen-1)
1089 */
1090 static int map_rfpos_to_apos(ESL_MSA *msa, ESL_ALPHABET *abc, char *errbuf, int64_t alen, int **ret_i_am_rf, int **ret_rf2a_map, int *ret_rflen)
1091 {
1092 int status;
1093 int rflen = 0;
1094 int *rf2a_map = NULL;
1095 int *i_am_rf = NULL;
1096 int rfpos = 0;
1097 int apos = 0;
1098
1099 /* contract check */
1100 if(msa->rf == NULL) ESL_FAIL(eslEINVAL, errbuf, "Error, trying to map RF positions to alignment positions, but msa->rf is NULL.");
1101
1102 /* count non-gap RF columns */
1103 for(apos = 0; apos < alen; apos++) {
1104 if((! esl_abc_CIsGap(abc, msa->rf[apos])) &&
1105 (! esl_abc_CIsMissing(abc, msa->rf[apos])) &&
1106 (! esl_abc_CIsNonresidue(abc, msa->rf[apos])))
1107 {
1108 rflen++;
1109 /* I don't use esl_abc_CIsResidue() b/c that would return FALSE for 'x' with RNA and DNA */
1110 }
1111 }
1112 /* build map */
1113 ESL_ALLOC(i_am_rf, sizeof(int) * alen);
1114 ESL_ALLOC(rf2a_map, sizeof(int) * rflen);
1115 for(apos = 0; apos < alen; apos++) {
1116 if((! esl_abc_CIsGap(abc, msa->rf[apos])) &&
1117 (! esl_abc_CIsMissing(abc, msa->rf[apos])) &&
1118 (! esl_abc_CIsNonresidue(abc, msa->rf[apos]))) {
1119 i_am_rf[apos] = TRUE;
1120 rf2a_map[rfpos++] = apos;
1121 }
1122 else {
1123 i_am_rf[apos] = FALSE;
1124 }
1125 }
1126 *ret_i_am_rf = i_am_rf;
1127 *ret_rf2a_map = rf2a_map;
1128 *ret_rflen = rflen;
1129 return eslOK;
1130
1131 ERROR:
1132 if(i_am_rf != NULL) free(i_am_rf);
1133 if(rf2a_map != NULL) free(rf2a_map);
1134 ESL_FAIL(status, errbuf, "Error, out of memory while mapping RF positions to alignment positions.");
1135 }
1136
1137 /* check_msa_weights
1138 *
1139 * Given an MSA, check if it has any weight that is
1140 * different from 1.0. If yes return TRUE, if not
1141 * return FALSE. If it does not have weights return
1142 * FALSE.
1143 */
1144 static int check_msa_weights(ESL_MSA *msa)
1145 {
1146 int i;
1147
1148 if(msa->wgt == NULL) return FALSE;
1149 for(i = 0; i < msa->nseq; i++) {
1150 if (esl_FCompare_old(msa->wgt[i], 1.0, eslSMALLX1) != eslOK) return TRUE;
1151 }
1152 return FALSE;
1153 }
1154
1155