Debian Codegraph @

easel/miniapps/esl-compalign.c

raw symbols SCIP
1 /* esl-compalign - compare two multiple sequence alignments
2 *
3 */
4 #include <esl_config.h>
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <ctype.h>
9
10 #include "easel.h"
11 #include "esl_fileparser.h"
12 #include "esl_getopts.h"
13 #include "esl_sq.h"
14 #include "esl_msa.h"
15 #include "esl_msafile.h"
16 #include "esl_vectorops.h"
17 #include "esl_wuss.h"
18
19 static char banner[] = "compare two multiple sequence alignments";
20
21 static char usage[] = "\
22 [-options] <trusted file> <test file>\n\
23 Both files must be in Stockholm format with #=GC RF markup.\n\
24 Sequences must occur in the same order in the two files.\n\
25 Number of non-gap characters in #=GC RF markup must be identical.\n\
26 Note: accuracy is computed differently than in Squid\'s compalign.\n\
27 See the manual page for details on how accuracy is computed.";
28
29 static int get_pp_idx(ESL_ALPHABET *abc, char ppchar);
30 static int read_mask_file(char *filename, char *errbuf, char **ret_mask, int *ret_masklen);
31
32 static ESL_OPTIONS options[] = {
33 /* name type default env range togs reqs incomp help docgroup */
34 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "help; show brief info on version and usage", 1 },
35 { "-c", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "print per column statistics instead of per sequence stats", 1 },
36 { "-p", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "print stats on accuracy versus posterior probability (PP)", 1 },
37 { "--p-mask", eslARG_OUTFILE,NULL, NULL, NULL, NULL,"-p", NULL, "with -p, only consider columns within mask ('1' columns) in <f>",1 },
38 { "--c2dfile", eslARG_OUTFILE,NULL, NULL, NULL, NULL,"-c", NULL, "print per column stats to esl-ssdraw --dfile file <f>", 1 },
39 { "--amino", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--dna,--rna", "<msafile> contains protein alignments", 2 },
40 { "--dna", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--amino,--rna", "<msafile> contains DNA alignments", 2 },
41 { "--rna", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--amino,--dna", "<msafile> contains RNA alignments", 2 },
42 { 0,0,0,0,0,0,0,0,0,0 },
43 };
44
45 int
46 main(int argc, char **argv)
47 {
48 ESL_GETOPTS *go; /* application configuration */
49 int kstatus, tstatus;/* return code from Easel routine */
50 int fmt; /* expected format of kfile, tfile */
51 char *kfile, *tfile; /* known, test structure file */
52 ESL_MSAFILE *kfp, *tfp; /* open kfile, tfile */
53 ESL_MSA *ka, *ta; /* known, trusted alignment */
54 int64_t klen, tlen; /* lengths of dealigned seqs */
55 int i; /* counter over sequences */
56 int apos; /* counter over alignment columns */
57 int rfpos; /* counter over consensus (non-gap RF) columns */
58 int is_rfpos; /* TRUE if current apos is a consensus pos, FALSE if not */
59 int uapos; /* counter over unaligned residue positions */
60 int nali; /* number of alignment we're on in each file */
61
62 int **kp; /* [0..i..nseq-1][1..r..sq->n] = x known non-gap RF position of residue r in sequence i */
63 int **tp; /* [0..i..nseq-1][1..r..sq->n] = x predicted non-gap RF position of residue r in sequence i */
64 /* for both kp and pp, if x <= 0, residue r for seq i is not aligned to a non-gap RF position, but rather as an 'insert'
65 * after non-gap RF position (x * -1)
66 */
67 int *km_pos; /* [0..rflen] = x, in known aln, number of residues aligned to non-gap RF column x; special case: mct[0] = 0 */
68 int *ki_pos; /* [0..rflen] = x, in known aln, number of residues inserted after non-gap RF column x */
69 int *tm_pos; /* [0..rflen] = x, in predicted aln, number of residues aligned to non-gap RF column x; special case: mct[0] = 0 */
70 int *ti_pos; /* [0..rflen] = x, in predicted aln, number of residues inserted after non-gap RF column x */
71 int *cor_tm_pos; /* [0..rflen] = x, in predicted aln, number of correctly predicted residues aligned to non-gap RF column x; special case: mct[0] = 0 */
72 int *cor_ti_pos; /* [0..rflen] = x, in predicted aln, number of correctly predicted residues inserted after non-gap RF column x */
73
74 int *km_seq; /* [0..i..nseq-1] = x, in known aln, number of residues aligned to non-gap RF columns in seq i; */
75 int *ki_seq; /* [0..i..nseq-1] = x, in known aln, number of residues inserted in seq i */
76 int *tm_seq; /* [0..i..nseq-1] = x, in predicted aln, number of residues aligned to non-gap RF columns in seq i; */
77 int *ti_seq; /* [0..i..nseq-1] = x, in predicted aln, number of residues inserted in seq i */
78 int *cor_tm_seq; /* [0..i..nseq-1] = x, in predicted aln, number of correctly predicted residues aligned to non-gap RF columns in seq i */
79 int *cor_ti_seq; /* [0..i..nseq-1] = x, in predicted aln, number of correctly predicted residues inserted in seq i */
80
81 int *seqlen; /* [0..i..nseq-1] = x, unaligned seq i has length x */
82 ESL_ALPHABET *abc = NULL; /* alphabet for all alignments */
83 int rflen, t_rflen; /* non-gap RF length (consensus lengths) */
84 int status;
85 char *namedashes;
86 int ni;
87 int namewidth = 8; /* length of 'seq name' */
88 int cor_tm, cor_ti, km, ki; /* correct predicted match, correct predicted insert, total match, total insert */
89 char *mask = NULL;
90 int masklen;
91 ESL_DSQ *ks;
92 ESL_DSQ *ts;
93 FILE *dfp = NULL; /* for --c2dfile */
94
95 /* variables needed for -p and related options */
96 int do_post = FALSE; /* TRUE if -p enabled */
97 int do_post_for_this_rfpos = FALSE; /* set for each consensus position, always TRUE unless --mask-p2xm */
98 int p; /* counter over integerized posteriors */
99 int *ptm = NULL; /* [0..p..10] number of total matches with posterior value p (10="*")*/
100 int *pti = NULL; /* [0..p..10] number of total inserts with posterior value p */
101 int *cor_ptm = NULL; /* [0..p..10] number of correct matches with posterior value p */
102 int *cor_pti = NULL; /* [0..p..10] number of correct inserts with posterior value p */
103 int npostvals = 11; /* number of posterior values 0-9, * */
104 int ppidx; /* index of PP */
105 char ppchars[11] = "0123456789*";
106 // int tot_cor_ptm, tot_cor_pti, tot_ptm, tot_pti; /* total counts of posteriors */
107 // int tot_incor_ptm,tot_incor_pti; // SRE: commented out; don't seem to be used; need to silence compiler warning
108 char errbuf[eslERRBUFSIZE];
109
110 /***********************************************
111 * Parse command line
112 ***********************************************/
113
114 go = esl_getopts_Create(options);
115 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK ||
116 esl_opt_VerifyConfig(go) != eslOK)
117 {
118 printf("Failed to parse command line: %s\n", go->errbuf);
119 esl_usage(stdout, argv[0], usage);
120 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
121 exit(1);
122 }
123
124 if (esl_opt_GetBoolean(go, "-h") )
125 {
126 esl_banner(stdout, argv[0], banner);
127 esl_usage (stdout, argv[0], usage);
128 puts("\n where options are:");
129 esl_opt_DisplayHelp(stdout, go, 1, 2, 80);
130 esl_opt_DisplayHelp(stdout, go, 2, 2, 80);
131 exit(EXIT_SUCCESS);
132 }
133
134 if (esl_opt_ArgNumber(go) != 2)
135 {
136 printf("Incorrect number of command line arguments.\n");
137 esl_usage(stdout, argv[0], usage);
138 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
139 exit(1);
140 }
141
142 kfile = esl_opt_GetArg(go, 1);
143 tfile = esl_opt_GetArg(go, 2);
144
145 fmt = eslMSAFILE_STOCKHOLM;
146
147 /***********************************************
148 * Open the two Stockholm files.
149 ***********************************************/
150
151 if (esl_opt_GetBoolean(go, "--amino")) abc = esl_alphabet_Create(eslAMINO);
152 else if (esl_opt_GetBoolean(go, "--dna")) abc = esl_alphabet_Create(eslDNA);
153 else if (esl_opt_GetBoolean(go, "--rna")) abc = esl_alphabet_Create(eslRNA);
154
155 if ( (kstatus = esl_msafile_Open(&abc, kfile, NULL, fmt, NULL, &kfp)) != eslOK) esl_msafile_OpenFailure(kfp, kstatus);
156 if ( (tstatus = esl_msafile_Open(&abc, tfile, NULL, fmt, NULL, &tfp)) != eslOK) esl_msafile_OpenFailure(tfp, tstatus);
157
158 do_post = esl_opt_GetBoolean(go, "-p");
159
160 /* read the mask file if --p-mask is enabled */
161 if(! esl_opt_IsDefault(go, "--p-mask")) {
162 if((status = read_mask_file(esl_opt_GetString(go, "--p-mask"), errbuf, &mask, &masklen)) != eslOK) esl_fatal(errbuf);
163 }
164 /* open the c2dfile for output, if nec */
165 if (esl_opt_IsOn(go, "--c2dfile")) {
166 if ((dfp = fopen(esl_opt_GetString(go, "--c2dfile"), "w")) == NULL) esl_fatal("Failed to open --c2dfile output file %s\n", esl_opt_GetString(go, "--c2dfile"));
167 }
168
169 /***********************************************
170 * Do alignment comparisons, one seq at a time;
171 * this means looping over all seqs in all alignments.
172 ***********************************************/
173 nali = 0;
174 while ( (kstatus = esl_msafile_Read(kfp, &ka)) != eslEOF){
175 if ( kstatus != eslOK) esl_msafile_ReadFailure(kfp, kstatus);
176 if ( (tstatus = esl_msafile_Read(tfp, &ta)) != eslOK) esl_msafile_ReadFailure(tfp, tstatus);
177
178 nali++;
179 if((nali > 1) && (esl_opt_IsOn(go, "--c2dfile"))) esl_fatal("--c2dfile is only meant for msafiles with single alignments");
180
181 /* Sanity check on alignment
182 */
183 if (ka->nseq != ta->nseq)
184 esl_fatal("trusted, test alignments don't have same seq #\n");
185 if (ka->rf == NULL)
186 esl_fatal("trusted alignment has no reference annotation\n");
187 if (ta->rf == NULL)
188 esl_fatal("test alignment has no reference annotation\n");
189
190 /* make sure the sequences are all identical */
191 ESL_ALLOC(seqlen, sizeof(int) * ka->nseq);
192 for(i = 0; i < ka->nseq; i++) {
193 if(strcmp(ka->sqname[i], ta->sqname[i]) != 0) esl_fatal("sequence %d of trusted alignment %s has different name than seq %d of predicted alignment %s\n", (i+1), ka->sqname[i], (i+1), ta->sqname[i]);
194 ESL_ALLOC(ks, sizeof(ESL_DSQ) * (ka->alen+2));
195 memcpy(ks, ka->ax[i], (ka->alen+2) * sizeof(ESL_DSQ));
196 esl_abc_XDealign(ka->abc, ks, ka->ax[i], &klen);
197
198 ESL_ALLOC(ts, sizeof(ESL_DSQ) * (ta->alen+2));
199 memcpy(ts, ta->ax[i], (ta->alen+2) * sizeof(ESL_DSQ));
200 esl_abc_XDealign(ta->abc, ts, ta->ax[i], &tlen);
201
202 if (tlen != klen)
203 esl_fatal("dealigned sequence mismatch, seq %d, when dealigned, is %d residues in the known alignment, but %d residues in the trusted alignment.", (i+1), klen, tlen);
204
205 if (memcmp(ks, ts, sizeof(ESL_DSQ) * klen) != 0)
206 esl_fatal("dealigned sequence mismatch, seq %d %s, when dealigned, are not identical.", (i+1), ka->sqname[i]);
207
208 seqlen[i] = tlen;
209 free(ks);
210 free(ts);
211 }
212
213 /* determine non-gap RF length */
214 rflen = 0;
215 for(apos = 1; apos <= ka->alen; apos++) {
216 if((! esl_abc_CIsGap (ka->abc, ka->rf[apos-1])) &&
217 (! esl_abc_CIsMissing(ka->abc, ka->rf[apos-1]))) rflen++;
218 }
219 t_rflen = 0;
220 for(apos = 1; apos <= ta->alen; apos++) {
221 if((! esl_abc_CIsGap (ta->abc, ta->rf[apos-1])) &&
222 (! esl_abc_CIsMissing (ta->abc, ta->rf[apos-1]))) t_rflen++;
223 }
224 if(t_rflen != rflen) esl_fatal("Trusted alignment non-gap RF length (%d) != predicted alignment non-gap RF length (%d).\n", rflen, t_rflen);
225
226 /* if -p, make sure the test alignment has posterior probabilities, and allocate our counters for correct/incorrect per post value */
227 if(do_post) {
228 if(! esl_opt_IsDefault(go, "--p-mask")) {
229 if(masklen != rflen) {
230 esl_fatal("Length of mask in %s (%d) not equal to non-gap RF len of alignments (%d)\n", esl_opt_GetString(go, "--p-mask"), masklen, rflen);
231 }
232 }
233 if(ta->pp == NULL) esl_fatal("-p requires \"#=GR PP\" annotation in the test alignment, but none exists");
234 ESL_ALLOC(ptm, sizeof(int) * npostvals);
235 ESL_ALLOC(pti, sizeof(int) * npostvals);
236 ESL_ALLOC(cor_ptm, sizeof(int) * npostvals);
237 ESL_ALLOC(cor_pti, sizeof(int) * npostvals);
238 esl_vec_ISet(ptm, npostvals, 0);
239 esl_vec_ISet(pti, npostvals, 0);
240 esl_vec_ISet(cor_ptm, npostvals, 0);
241 esl_vec_ISet(cor_pti, npostvals, 0);
242 }
243
244 /* allocate and initialize our counters */
245 ESL_ALLOC(kp, sizeof(int *) * ka->nseq);
246 ESL_ALLOC(tp, sizeof(int *) * ta->nseq);
247 for(i = 0; i < ka->nseq; i++) {
248 ESL_ALLOC(kp[i], sizeof(int) * (seqlen[i]+1));
249 ESL_ALLOC(tp[i], sizeof(int) * (seqlen[i]+1));
250 esl_vec_ISet(kp[i], seqlen[i]+1, -987654321);
251 esl_vec_ISet(tp[i], seqlen[i]+1, -987654321);
252 }
253
254 ESL_ALLOC(km_pos, sizeof(int) * (rflen+1));
255 ESL_ALLOC(ki_pos, sizeof(int) * (rflen+1));
256 ESL_ALLOC(tm_pos, sizeof(int) * (rflen+1));
257 ESL_ALLOC(ti_pos, sizeof(int) * (rflen+1));
258 ESL_ALLOC(cor_tm_pos, sizeof(int) * (rflen+1));
259 ESL_ALLOC(cor_ti_pos, sizeof(int) * (rflen+1));
260 esl_vec_ISet(km_pos, rflen+1, 0);
261 esl_vec_ISet(ki_pos, rflen+1, 0);
262 esl_vec_ISet(tm_pos, rflen+1, 0);
263 esl_vec_ISet(ti_pos, rflen+1, 0);
264 esl_vec_ISet(cor_tm_pos, rflen+1, 0);
265 esl_vec_ISet(cor_ti_pos, rflen+1, 0);
266
267 ESL_ALLOC(km_seq, sizeof(int) * ka->nseq);
268 ESL_ALLOC(ki_seq, sizeof(int) * ka->nseq);
269 ESL_ALLOC(tm_seq, sizeof(int) * ka->nseq);
270 ESL_ALLOC(ti_seq, sizeof(int) * ka->nseq);
271 ESL_ALLOC(cor_tm_seq, sizeof(int) * ka->nseq);
272 ESL_ALLOC(cor_ti_seq, sizeof(int) * ka->nseq);
273 esl_vec_ISet(km_seq, ka->nseq, 0);
274 esl_vec_ISet(ki_seq, ka->nseq, 0);
275 esl_vec_ISet(tm_seq, ka->nseq, 0);
276 esl_vec_ISet(ti_seq, ka->nseq, 0);
277 esl_vec_ISet(cor_tm_seq, ka->nseq, 0);
278 esl_vec_ISet(cor_ti_seq, ka->nseq, 0);
279
280 /* determine non-gap RF location of each residue in known alignment */
281 for(i = 0; i < ka->nseq; i++) {
282 uapos = rfpos = 0;
283 for(apos = 1; apos <= ka->alen; apos++) {
284 is_rfpos = FALSE;
285 if((! esl_abc_CIsGap (ka->abc, ka->rf[apos-1])) &&
286 (! esl_abc_CIsMissing (ka->abc, ka->rf[apos-1]))) {
287 rfpos++; is_rfpos = TRUE;
288 }
289 if(esl_abc_XIsResidue(ka->abc, ka->ax[i][apos])) {
290 uapos++;
291 kp[i][uapos] = (is_rfpos) ? rfpos : (-1 * rfpos);
292 if(is_rfpos) { km_pos[rfpos]++; km_seq[i]++; }
293 else { ki_pos[rfpos]++; ki_seq[i]++; }
294 }
295 }
296 }
297
298 /* determine non-gap RF location of each residue in predicted alignment */
299 for(i = 0; i < ta->nseq; i++) {
300 uapos = rfpos = 0;
301 for(apos = 1; apos <= ta->alen; apos++) {
302 is_rfpos = FALSE;
303 if((! esl_abc_CIsGap (abc, ta->rf[apos-1])) &&
304 (! esl_abc_CIsMissing (abc, ta->rf[apos-1]))) {
305 rfpos++; is_rfpos = TRUE;
306 if(do_post) {
307 do_post_for_this_rfpos = (mask != NULL && mask[rfpos-1] == '0') ? FALSE : TRUE;
308 }
309 }
310 if(esl_abc_XIsResidue(ta->abc, ta->ax[i][apos])) {
311 uapos++;
312 tp[i][uapos] = (is_rfpos) ? rfpos : (-1 * rfpos);
313 if(do_post) {
314 if(esl_abc_CIsGap(abc, ta->pp[i][(apos-1)])) esl_fatal("gap PP value for nongap residue: ali: %d seq: %d apos: %d\n", nali, i, apos);
315 ppidx = get_pp_idx(abc, ta->pp[i][(apos-1)]);
316 if(ppidx == -1) esl_fatal("unrecognized PP value (%c) for nongap residue: ali: %d seq: %d apos: %d\n", ta->pp[i][(apos-1)], nali, i, apos);
317 }
318 if(is_rfpos) {
319 tm_pos[rfpos]++; tm_seq[i]++;
320 if(do_post_for_this_rfpos) ptm[ppidx]++;
321 }
322 else {
323 ti_pos[rfpos]++; ti_seq[i]++;
324 if(do_post) pti[ppidx]++;
325 }
326 if(kp[i][uapos] == tp[i][uapos]) { /* correctly predicted this residue */
327 if(is_rfpos) {
328 cor_tm_seq[i]++; cor_tm_pos[rfpos]++;
329 if(do_post_for_this_rfpos) cor_ptm[ppidx]++;
330 }
331 else {
332 cor_ti_seq[i]++; cor_ti_pos[rfpos]++;
333 if(do_post) cor_pti[ppidx]++;
334 }
335 }
336 }
337 }
338 }
339 if((! (esl_opt_GetBoolean(go, "-c"))) && (! esl_opt_GetBoolean(go, "-p"))) {
340 /* print per sequence statistics */
341 /* determine the longest name in msa */
342 for(ni = 0; ni < ka->nseq; ni++) namewidth = ESL_MAX(namewidth, strlen(ka->sqname[ni]));
343 ESL_ALLOC(namedashes, sizeof(char) * namewidth+1);
344 namedashes[namewidth] = '\0';
345 for(ni = 0; ni < namewidth; ni++) namedashes[ni] = '-';
346
347 printf("# %-*s %6s %28s %28s %28s\n", namewidth, "seq name", "len", "match columns", "insert columns", "all columns");
348 printf("# %-*s %6s %28s %28s %28s\n", namewidth, namedashes, "------", "----------------------------", "----------------------------", "----------------------------");
349 for(i = 0; i < ta->nseq; i++) {
350 printf(" %-*s %6d %8d / %8d (%.3f) %8d / %8d (%.3f) %8d / %8d (%.3f)\n", namewidth, ka->sqname[i], seqlen[i],
351 cor_tm_seq[i], km_seq[i], (km_seq[i] == 0) ? 0. : ((float) cor_tm_seq[i] / (float) km_seq[i]),
352 cor_ti_seq[i], ki_seq[i], (ki_seq[i] == 0) ? 0. : ((float) cor_ti_seq[i] / (float) ki_seq[i]),
353 (cor_tm_seq[i] + cor_ti_seq[i]), (km_seq[i] + ki_seq[i]), ((float) (cor_tm_seq[i] + cor_ti_seq[i]) / ((float) km_seq[i] + ki_seq[i])));
354 }
355 cor_tm = esl_vec_ISum(cor_tm_seq, ka->nseq);
356 cor_ti = esl_vec_ISum(cor_ti_seq, ka->nseq);
357 km = esl_vec_ISum(km_seq, ka->nseq);
358 ki = esl_vec_ISum(ki_seq, ka->nseq);
359
360 printf("# %-*s %6s %28s %28s %28s\n", namewidth, namedashes, "-----", "----------------------------", "----------------------------", "----------------------------");
361 printf("# %-*s %6s %8d / %8d (%.3f) %8d / %8d (%.3f) %8d / %8d (%.3f)\n",
362 namewidth, "*all*", "-",
363 cor_tm, km, ((float) cor_tm / (float) km),
364 cor_ti, ki, ((float) cor_ti / (float) ki),
365 (cor_tm+cor_ti), (km+ki), (((float) (cor_tm + cor_ti))/ ((float) (km + ki))));
366 free(namedashes);
367
368 }
369 else if(esl_opt_GetBoolean(go, "-c")) { /* print per column statistics */
370 printf("# %5s %20s %20s %20s\n", "rfpos", "match", "insert", "both");
371 printf("# %5s %20s %20s %20s\n", "-----", "--------------------", "--------------------", "--------------------");
372 for(rfpos = 0; rfpos <= rflen; rfpos++) {
373 printf(" %5d %4d / %4d (%.3f) %4d / %4d (%.3f) %4d / %4d (%.3f)\n", rfpos,
374
375 cor_tm_pos[rfpos], km_pos[rfpos], (km_pos[rfpos] == 0) ? 0. : ((float) cor_tm_pos[rfpos] / (float) km_pos[rfpos]),
376 cor_ti_pos[rfpos], ki_pos[rfpos], (ki_pos[rfpos] == 0) ? 0. : ((float) cor_ti_pos[rfpos] / (float) ki_pos[rfpos]),
377 (cor_tm_pos[rfpos] + cor_ti_pos[rfpos]), (km_pos[rfpos] + ki_pos[rfpos]), ((float) (cor_tm_pos[rfpos] + cor_ti_pos[rfpos]) / ((float) km_pos[rfpos] + ki_pos[rfpos])));
378 }
379 }
380 else if(do_post) { /* do posterior output */
381 if(mask == NULL) {
382 printf("# %2s %29s %29s\n", "", " match columns ", " insert columns ");
383 printf("# %2s %29s %29s\n", "", "-----------------------------", "-----------------------------") ;
384 printf("# %2s %8s %8s %9s %8s %8s %9s\n", "PP", "ncorrect", "ntotal", "fractcor", "ncorrect", "ntotal", "fractcor");
385 printf("# %2s %8s %8s %9s %8s %8s %9s\n", "--", "--------", "--------", "---------", "--------", "--------", "---------");
386 }
387 else {
388 printf("# %2s %29s %29s\n", "", " match columns within mask ", " insert columns ");
389 printf("# %2s %29s %29s\n", "", "-----------------------------", "-----------------------------") ;
390 printf("# %2s %8s %8s %9s %8s %8s %9s\n", "PP", "ncorrect", "ntotal", "fractcor", "ncorrect", "ntotal", "fractcor");
391 printf("# %2s %8s %8s %9s %8s %8s %9s\n", "--", "--------", "--------", "---------", "--------", "--------", "---------");
392 }
393 //tot_ptm = esl_vec_ISum(ptm, npostvals);
394 //tot_pti = esl_vec_ISum(pti, npostvals);
395 //tot_cor_ptm = esl_vec_ISum(cor_ptm, npostvals);
396 //tot_cor_pti = esl_vec_ISum(cor_pti, npostvals);
397 //tot_incor_ptm = tot_ptm - tot_cor_ptm;
398 //tot_incor_pti = tot_pti - tot_cor_pti;
399 for(p = (npostvals-1); p >= 0; p--) {
400 printf(" %2c %8d / %8d (%.5f) %8d / %8d (%.5f)\n",
401 ppchars[p], cor_ptm[p], ptm[p],
402 (ptm[p] == 0) ? 0. : (float) cor_ptm[p] / (float) ptm[p],
403 cor_pti[p], pti[p],
404 (pti[p] == 0) ? 0. : (float) cor_pti[p] / (float) pti[p]);
405 }
406 }
407
408 /* handle --c2dfile */
409 if (dfp != NULL) {
410 /* match stats, 4 fields, CMYK color values */
411 for(rfpos = 1; rfpos <= rflen; rfpos++) {
412 if(km_pos[rfpos] == 0) { /* special case, no known alignment residues, a blank position */
413 fprintf(dfp, "%.3f %.3f %.3f %.3f\n", 0., 0., 0., 0.);
414 }
415 else {
416 fprintf(dfp, "%.3f %.3f %.3f %.3f\n",
417 0., /* cyan */
418 1. - ((float) cor_tm_pos[rfpos] / (float) km_pos[rfpos]), /* magenta, fraction incorrect */
419 1. - ((float) km_pos[rfpos] / ta->nseq), /* yellow, 1 - fraction of seqs with residue in column */
420 0.);
421 }
422 }
423 fprintf(dfp, "//\n");
424 /* insert stats, 4 fields, CMYK color values */
425 rfpos = 0; /* special case, combine insert posn 0 and 1 together */
426 if(ki_pos[rfpos] == 0) { /* special case, no known alignment residues, a blank position */
427 fprintf(dfp, "%.3f %.3f %.3f %.3f\n", 0., 0., 0., 0.);
428 }
429 else {
430 fprintf(dfp, "%.3f %.3f %.3f %.3f\n",
431 0., /* cyan */
432 1. - ((float) (cor_ti_pos[0] + cor_ti_pos[1]) / ((float) (ki_pos[0] + ki_pos[1]))), /* magenta, fraction correct */
433 0.,
434 0.);
435 }
436 /* insert stats posn 2..rflen */
437 for(rfpos = 2; rfpos <= rflen; rfpos++) {
438 if(ki_pos[rfpos] == 0) { /* special case, no known alignment residues, a blank position */
439 fprintf(dfp, "%.3f %.3f %.3f %.3f\n", 0., 0., 0., 0.);
440 }
441 else {
442 fprintf(dfp, "%.3f %.3f %.3f %.3f\n",
443 0., /* cyan */
444 1. - ((float) cor_ti_pos[rfpos] / (float) ki_pos[rfpos]), /* magenta, fraction correct */
445 0.,
446 0.);
447 }
448 }
449 fprintf(dfp, "//\n");
450 }
451
452 if(ptm != NULL) free(ptm);
453 if(pti != NULL) free(pti);
454 if(cor_ptm != NULL) free(cor_ptm);
455 if(cor_ptm != NULL) free(cor_pti);
456 for(i = 0; i < ka->nseq; i++) {
457 free(kp[i]);
458 free(tp[i]);
459 }
460 free(kp);
461 free(tp);
462 free(km_seq);
463 free(ki_seq);
464 free(tm_seq);
465 free(ti_seq);
466 free(cor_tm_seq);
467 free(cor_ti_seq);
468 free(km_pos);
469 free(ki_pos);
470 free(tm_pos);
471 free(ti_pos);
472 free(cor_tm_pos);
473 free(cor_ti_pos);
474 free(seqlen);
475 esl_msa_Destroy(ka);
476 esl_msa_Destroy(ta);
477 }
478
479 if(mask != NULL) free(mask);
480 if(dfp != NULL) {
481 fclose(dfp);
482 printf("# Draw file of per-column stats saved to file: %s\n", esl_opt_GetString(go, "--c2dfile"));
483 }
484
485 if(abc) esl_alphabet_Destroy(abc);
486 esl_getopts_Destroy(go);
487 esl_msafile_Close(tfp);
488 esl_msafile_Close(kfp);
489 return 0;
490
491 ERROR:
492 return status;
493 }
494
495 /* get_pp_idx
496 *
497 * Given a #=GR PP or #=GC PP_cons character, return the appropriate index
498 * in a pp_ct[] vector.
499 * '0' return 0;
500 * '1' return 1;
501 * '2' return 2;
502 * '3' return 3;
503 * '4' return 4;
504 * '5' return 5;
505 * '6' return 6;
506 * '7' return 7;
507 * '8' return 8;
508 * '9' return 9;
509 * '*' return 10;
510 * gap return 11;
511 *
512 * Anything else (including missing or nonresidue) return -1;
513 */
514 static int get_pp_idx(ESL_ALPHABET *abc, char ppchar)
515 {
516 if(ppchar == '*') return 10;
517 if(ppchar == '9') return 9;
518 if(ppchar == '8') return 8;
519 if(ppchar == '7') return 7;
520 if(ppchar == '6') return 6;
521 if(ppchar == '5') return 5;
522 if(ppchar == '4') return 4;
523 if(ppchar == '3') return 3;
524 if(ppchar == '2') return 2;
525 if(ppchar == '1') return 1;
526 if(ppchar == '0') return 0;
527 return -1;
528 }
529
530
531 /* read_mask_file
532 *
533 * Given an open file pointer, read the first token of the
534 * file and return it as *ret_mask. It must contain only
535 * '0' or '1' characters.
536 *
537 * Returns: eslOK on success.
538 */
539 int
540 read_mask_file(char *filename, char *errbuf, char **ret_mask, int *ret_mask_len)
541 {
542 ESL_FILEPARSER *efp = NULL;
543 char *mask = NULL;
544 char *tok;
545 int toklen;
546 int n;
547 int status;
548
549
550 if (esl_fileparser_Open(filename, NULL, &efp) != eslOK) ESL_XFAIL(eslFAIL, errbuf, "failed to open %s in read_mask_file\n", filename);
551 esl_fileparser_SetCommentChar(efp, '#');
552
553 if((status = esl_fileparser_GetToken(efp, &tok, &toklen)) != eslOK) ESL_XFAIL(eslFAIL, errbuf, "failed to read a single token from %s\n", filename);
554
555 ESL_ALLOC(mask, sizeof(char) * (toklen+1));
556 for(n = 0; n < toklen; n++) {
557 if((tok[n] == '0') || (tok[n] == '1')) {
558 mask[n] = tok[n];
559 }
560 else { ESL_XFAIL(eslFAIL, errbuf, "read a non-0 and non-1 character (%c) in the mask file %s\n", tok[n], filename); }
561 }
562 mask[n] = '\0';
563
564 *ret_mask = mask;
565 *ret_mask_len = n;
566 esl_fileparser_Close(efp);
567 return eslOK;
568
569 ERROR:
570 if (efp) esl_fileparser_Close(efp);
571 if (mask) free(mask);
572 return status;
573 }