Debian Codegraph @

easel/miniapps/esl-compstruct.c

raw symbols SCIP
1 /* esl-compstruct - calculate accuracy of RNA secondary structure predictions
2 */
3 #include <esl_config.h>
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <ctype.h>
8
9 #include "easel.h"
10 #include "esl_getopts.h"
11 #include "esl_sq.h"
12 #include "esl_msa.h"
13 #include "esl_msafile.h"
14 #include "esl_wuss.h"
15
16 static char banner[] = "calculate accuracy of RNA secondary structure predictions";
17
18 static char usage[] = "\
19 [-options] <trusted file> <test file>\n\
20 Both files must be in Stockholm format with secondary structure markup.\n\
21 Sequences must occur in the same order in the two files.\n\
22 The markup must be in WUSS notation.\n\
23 \n";
24
25 static ESL_OPTIONS options[] = {
26 /* name type default env range togs reqs incomp help docgroup */
27 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "help; show brief info on version and usage", 0 },
28 { "-m", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "use Mathews'relaxed criterion for correctness; allow +/-1 slip", 0 },
29 { "-p", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "count pseudoknotted base pairs", 0 },
30 { "--quiet", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "suppress verbose header", 0 },
31 { 0,0,0,0,0,0,0,0,0,0 },
32 };
33
34 int
35 main(int argc, char **argv)
36 {
37 ESL_GETOPTS *go; /* application configuration */
38 int kstatus, tstatus;/* return code from Easel routine */
39 int fmt; /* expected format of kfile, tfile */
40 char *kfile, *tfile; /* known, test structure file */
41 ESL_MSAFILE *kfp, *tfp; /* open kfile, tfile */
42 ESL_MSA *ka, *ta; /* known, trusted alignment */
43 int64_t klen, tlen; /* lengths of dealigned seqs */
44 int *kct, *tct; /* known, test CT rep of structure */
45 int i; /* counter over sequences */
46 int pos; /* counter over residues */
47
48 int nseq; /* total number of sequences in the files */
49 int nseq_rejected; /* total number of sequences rejected */
50
51 int kpairs; /* count of base pairs in trusted structure */
52 int tpairs; /* count of base pairs in test structure */
53 int kcorrect; /* # bp in known structure correctly predicted */
54 int tcorrect; /* # bp in test structure that are true */
55
56 int tot_kpairs; /* total bp in all known structures */
57 int tot_tpairs; /* total bp in all predicted structures */
58 int tot_kcorrect; /* total correct bp in all known structures */
59 int tot_tcorrect; /* total true pairs in all test structures */
60 int tot_positions; /* total # of bases */
61
62 int status;
63
64 /***********************************************
65 * Parse command line
66 ***********************************************/
67
68 go = esl_getopts_Create(options);
69 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK ||
70 esl_opt_VerifyConfig(go) != eslOK)
71 {
72 printf("Failed to parse command line: %s\n", go->errbuf);
73 esl_usage(stdout, argv[0], usage);
74 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
75 exit(1);
76 }
77
78 if (esl_opt_GetBoolean(go, "-h") )
79 {
80 esl_banner(stdout, argv[0], banner);
81 esl_usage (stdout, argv[0], usage);
82 puts("\n where options are:");
83 esl_opt_DisplayHelp(stdout, go, 0, 2, 80);
84 exit(EXIT_SUCCESS);
85 }
86
87 if (esl_opt_ArgNumber(go) != 2)
88 {
89 printf("Incorrect number of command line arguments.\n");
90 esl_usage(stdout, argv[0], usage);
91 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
92 exit(1);
93 }
94
95 kfile = esl_opt_GetArg(go, 1);
96 tfile = esl_opt_GetArg(go, 2);
97
98 if (! esl_opt_GetBoolean(go, "--quiet"))
99 esl_banner(stdout, argv[0], banner);
100
101 fmt = eslMSAFILE_STOCKHOLM;
102
103 /***********************************************
104 * Open the two Stockholm files.
105 ***********************************************/
106
107 if ( (status = esl_msafile_Open(NULL, kfile, NULL, fmt, NULL, &kfp)) != eslOK) esl_msafile_OpenFailure(kfp, status);
108 if ( (status = esl_msafile_Open(NULL, tfile, NULL, fmt, NULL, &tfp)) != eslOK) esl_msafile_OpenFailure(tfp, status);
109
110 /***********************************************
111 * Do structure comparisons, one seq at a time;
112 * this means looping over all seqs in all alignments.
113 ***********************************************/
114
115 tot_kpairs = tot_kcorrect = 0;
116 tot_tpairs = tot_tcorrect = 0;
117 nseq = nseq_rejected = 0;
118 tot_positions = 0;
119
120 printf("%20s %17s %17s\n", "", "[sensitivity]", "[PPV]");
121
122 while ( (kstatus = esl_msafile_Read(kfp, &ka)) != eslEOF)
123 {
124 if ( kstatus != eslOK) esl_msafile_ReadFailure(kfp, kstatus);
125 if ( (tstatus = esl_msafile_Read(tfp, &ta)) != eslOK) esl_msafile_ReadFailure(tfp, tstatus);
126
127 /* Sanity check on alignment
128 */
129 if (ka->nseq != ta->nseq) esl_fatal("trusted, test alignments don't have same seq #\n");
130 if (ka->ss == NULL) esl_fatal("trusted alignment has no secondary structure annotation\n");
131 if (ta->ss == NULL) esl_fatal("test alignment has no secondary structure annotation\n");
132
133 for (i = 0; i < ka->nseq; i++)
134 {
135 nseq++;
136 printf("%-20s ", ka->sqname[i]);
137
138 /* Sanity checks on seqs to compare, plus conversion
139 * to dealigned ct arrays ready for comparison.
140 */
141 if (ta->ss[i] == NULL)
142 {
143 printf("[REJECTED: no predicted structure]\n");
144 nseq_rejected++;
145 continue;
146 }
147 if (ka->ss[i] == NULL)
148 {
149 printf("[REJECTED: no trusted structure]\n");
150 nseq_rejected++;
151 continue;
152 }
153 if (strcmp(ka->sqname[i], ta->sqname[i]) != 0)
154 {
155 printf("[REJECTED: test seq name is %s]\n", ta->sqname[i]);
156 nseq_rejected++;
157 continue;
158 }
159
160 esl_strdealign(ka->ss[i], ka->aseq[i], "-_.~", NULL);
161 esl_strdealign(ka->aseq[i], ka->aseq[i], "-_.~", &klen);
162
163 esl_strdealign(ta->ss[i], ta->aseq[i], "-_.~", NULL);
164 esl_strdealign(ta->aseq[i], ta->aseq[i], "-_.~", &tlen);
165
166 if (klen != tlen)
167 {
168 printf("[REJECTED: seq lengths not identical]\n");
169 nseq_rejected++;
170 continue;
171 }
172
173 /* not counting pseudoknots? suppress them in the ss strings*/
174 if (! esl_opt_GetBoolean(go, "-p"))
175 {
176 esl_wuss_nopseudo(ka->ss[i], ka->ss[i]);
177 esl_wuss_nopseudo(ta->ss[i], ta->ss[i]);
178 }
179
180 ESL_ALLOC(kct, sizeof(int) * (klen+1));
181 ESL_ALLOC(tct, sizeof(int) * (tlen+1));
182 if (esl_wuss2ct(ka->ss[i], klen, kct) != eslOK)
183 {
184 printf("[REJECTED: bad trusted structure]\n");
185 nseq_rejected++;
186 continue;
187 }
188 if (esl_wuss2ct(ta->ss[i], tlen, tct) != eslOK)
189 {
190 printf("[REJECTED: bad test structure]\n");
191 nseq_rejected++;
192 continue;
193 }
194
195 /* OK, we're all set up with kct (trusted) and tct (predicted)
196 * CT arrays, and we're about to count up our correctly predicted
197 * pairs. A brief digression/commentary first. We have to
198 * define what you mean by a "correctly predicted" base
199 * pair.
200 *
201 * Our default criterion is simple and strict: the known base pair
202 * must be exactly present in the prediction; kct[pos] == tct[pos]
203 * where kct[pos] > 0.
204 *
205 * Dave Mathews [MathewsTurner99] uses a more relaxed
206 * criterion that allows a little helix slippage in the prediction.
207 * For a known pair (i,j), he considers the prediction to be correct
208 * if the prediction contains a base pair (i,j), (i+1,j), (i-1,j),
209 * (i,j+1), or (i,j-1).
210 *
211 * A problem that arises here is that the mapping of known
212 * to predicted base pairs is not one-to-one under Mathews'
213 * rule: a single predicted pair can cause two known pairs
214 * to be considered to be "correctly predicted". You'd sort
215 * of like to run some sort of maximum matching algorithm to
216 * assign a one-to-one correspondence between known and
217 * predicted pairs. It does not appear that Mathews does this,
218 * though.
219 *
220 * And for us, the problem becomes a little worse. Mathews only
221 * tabulates "correct" base pairs (our "sensitivity"), and
222 * does not run a calculation of how many predicted pairs
223 * are true (our "specificity", or positive predictive
224 * value).
225 *
226 * So: when we implement the Mathews rule, we do it the most
227 * simple and obvious way. We apply his correctness rule in
228 * both directions. A known pair i,j is considered to be
229 * correctly predicted if the prediction contains any one of
230 * the pairs (i,j), (i+1,j), (i-1,j), (i,j+1), or (i,j-1), for
231 * the purposes of sensitivity. Conversely, a predicted pair
232 * i,j is considered to be correct if the known structure
233 * contains any one of the pairs (i,j), (i+1,j), (i-1,j),
234 * (i,j+1), or (i,j-1), for the purposes of PPV. That is, we
235 * do not worry at all about establishing an optimal
236 * one-to-one mapping between known and predicted pairs. I
237 * think that this is likelyto reflect Mathews' own
238 * implementation, but have not verified this.
239 */
240 tpairs = tcorrect = 0; /* predicted "test" structure */
241 kpairs = kcorrect = 0; /* trusted "known" structure */
242 for (pos = 1; pos <= klen; pos++)
243 {
244 /* sensitivity; looking from the known (trusted) structure's
245 * base pairs.
246 */
247 if (kct[pos] > pos) /* trusted bp between (pos, kct[pos]) */
248 {
249 kpairs++; /* don't doublecount */
250
251 if (esl_opt_GetBoolean(go, "-m")) { /* mathews' version */
252 if (tct[pos] == kct[pos] || /* i,j */
253 (pos > 1 && tct[pos-1] == kct[pos]) || /* i-1, j */
254 (pos < klen && tct[pos+1] == kct[pos]) || /* i+1, j */
255 (tct[pos]> 0 && tct[pos] == kct[pos]-1) || /* i, j-1 */
256 (tct[pos]> 0 && tct[pos] == kct[pos]+1)) /* i, j+1 */
257 kcorrect++;
258 } else {
259 if (tct[pos] == kct[pos]) kcorrect++;
260 }
261 }
262
263 /* PPV/specificity; looking from the test (predicted) structure's
264 * base pairs.
265 */
266 if (tct[pos] > pos) /* predicted base pair (pos, tct[pos]) */
267 {
268 tpairs++;
269
270 if (esl_opt_GetBoolean(go, "-m")) { /* mathews' version */
271 if (kct[pos] == tct[pos] || /* i,j */
272 (pos > 1 && kct[pos-1] == tct[pos]) || /* i-1, j */
273 (pos < tlen && kct[pos+1] == tct[pos]) || /* i+1, j */
274 (kct[pos]> 0 && kct[pos] == tct[pos]-1) || /* i, j-1 */
275 (kct[pos]> 0 && kct[pos] == tct[pos]+1)) /* i, j+1 */
276 tcorrect++;
277 } else {
278 if (kct[pos] == tct[pos]) tcorrect++;
279 }
280 }
281 }
282
283 /* side note: under the default rule, tcorrect==kcorrect,
284 * because there's a one-to-one mapping of known to predicted
285 * pairs; but this is not necessarily the case for the relaxed
286 * Mathews rule.
287 */
288 tot_tpairs += tpairs;
289 tot_tcorrect += tcorrect;
290 tot_kpairs += kpairs;
291 tot_kcorrect += kcorrect;
292 tot_positions += klen;
293
294 /* print out per sequence info */
295 printf(" == %5d %5d %5.2f%% %5d %5d %5.2f%%\n",
296 kcorrect, kpairs, 100. * (float) kcorrect/ (float) kpairs,
297 tcorrect, tpairs, 100. * (float) tcorrect/ (float) tpairs);
298
299 free(tct);
300 free(kct);
301 }
302 esl_msa_Destroy(ka);
303 esl_msa_Destroy(ta);
304 }
305
306 /* Print the final summary:
307 */
308 puts("\n");
309 if (nseq_rejected > 0) {
310 printf("%d total sequences; %d counted towards comparison; %d rejected\n",
311 nseq, nseq-nseq_rejected, nseq_rejected);
312 printf("(grep \"REJECTED\" in the output to identify the problems)\n\n");
313 }
314
315 printf("Overall prediction accuracy (%d sequences, %d positions)\n",
316 nseq - nseq_rejected, tot_positions);
317 printf(" %d/%d trusted pairs predicted (%.2f%% sensitivity)\n",
318 tot_kcorrect, tot_kpairs,
319 100. * (float) tot_kcorrect/ (float) tot_kpairs);
320 printf(" %d/%d predicted pairs correct (%.2f%% PPV)\n",
321 tot_tcorrect, tot_tpairs,
322 100. * (float) tot_tcorrect/ (float) tot_tpairs);
323 puts("");
324
325 esl_getopts_Destroy(go);
326 esl_msafile_Close(tfp);
327 esl_msafile_Close(kfp);
328 return 0;
329
330 ERROR:
331 return status;
332 }
333
334
335