Debian Codegraph @

easel/miniapps/esl-alimap.c

raw symbols SCIP
1 /* Map two multiple sequence alignments to each other.
2 *
3 * EPN, Tue Sep 23 13:39:03 2008
4 */
5 #include <esl_config.h>
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <assert.h>
11 #include <math.h>
12 #include <limits.h>
13
14 #include "easel.h"
15 #include "esl_distance.h"
16 #include "esl_fileparser.h"
17 #include "esl_getopts.h"
18 #include "esl_sq.h"
19 #include "esl_sqio.h"
20 #include "esl_msa.h"
21 #include "esl_msafile.h"
22 #include "esl_distance.h"
23 #include "esl_dmatrix.h"
24 #include "esl_vectorops.h"
25 #include "esl_stack.h"
26 #include "esl_tree.h"
27 #include "esl_wuss.h"
28
29 static char banner[] = "map two alignments to each other";
30 static char usage[] = "[options] <msafile1> <msafile2>\n\
31 <msafile1> and <msafile2> must be in Stockholm format.";
32
33 #define NCHOICES 3
34 #define DIAG 0
35 #define VERT 1
36 #define HORZ 2
37
38 static int map_msas(const ESL_GETOPTS *go, char *errbuf, ESL_MSA *msa1, ESL_MSA *msa2, int **ret_msa1_to_msa2_map);
39 static int map_sub_msas(const ESL_GETOPTS *go, char *errbuf, ESL_MSA *msa1, ESL_MSA *msa2, char **ret_msa1_to_msa2_mask);
40 static int map_rfpos_to_apos(ESL_MSA *msa, int **ret_rf2a_map, int **ret_a2rf_map, int *ret_rflen);
41 static int map2masks(const ESL_GETOPTS *go, char *errbuf, int alen1, int alen2, int *a2rf_map1, int *a2rf_map2, int *rf2a_map1, int *rf2a_map2, int rflen1, int rflen2, int *msa1_to_msa2_map);
42
43 static ESL_OPTIONS options[] = {
44 /* name type default env range togs reqs incomp help docgroup */
45 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "help; show brief info on version and usage", 1 },
46 { "-q", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "be quiet, don't print mapping of each column", 1 },
47 { "--mask-a2a", eslARG_OUTFILE,NULL, NULL, NULL, NULL,NULL, NULL, "mask to <f>:'1'=msa1 aln col x maps msa2 aln col", 1 },
48 { "--mask-a2rf", eslARG_OUTFILE,NULL, NULL, NULL, NULL,NULL, NULL, "mask to <f>:'1'=msa1 aln col x maps msa2 nongap RF col", 1 },
49 { "--mask-rf2a", eslARG_OUTFILE,NULL, NULL, NULL, NULL,NULL, NULL, "mask to <f>:'1'=msa1 nongap RF col x maps msa2 aln col", 1 },
50 { "--mask-rf2rf",eslARG_OUTFILE,NULL, NULL, NULL, NULL,NULL, NULL, "mask to <f>:'1'=msa1 nongap RF col x maps msa2 nongap RF col", 1 },
51 { "--submap", eslARG_OUTFILE,NULL, NULL, NULL, NULL,NULL, NULL, "<msafile2> is subaln of <msafile1>, output mask to <f>", 1 },
52 { "--amino", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL,"--dna,--rna", "<msafile{1,2}> contain protein alignments", 1 },
53 { "--dna", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL,"--amino,--rna", "<msafile{1,2}> contain DNA alignments", 1 },
54 { "--rna", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL,"--amino,--dna", "<msafile{1,2}> contain RNA alignments", 1 },
55 { 0,0,0,0,0,0,0,0,0,0 },
56 };
57
58 int
59 main(int argc, char **argv)
60 {
61 ESL_GETOPTS *go = NULL; /* application configuration */
62 ESL_ALPHABET *abc = NULL; /* biological alphabet */
63 char *alifile1= NULL; /* alignment 1 file name */
64 char *alifile2= NULL; /* alignment 2 file name */
65 int fmt; /* format code for alifiles */
66 ESL_MSAFILE *afp1 = NULL; /* open alignment file 1 */
67 ESL_MSAFILE *afp2 = NULL; /* open alignment file 2 */
68 ESL_MSA *msa1 = NULL; /* multiple sequence alignment 1 */
69 ESL_MSA *msa2 = NULL; /* multiple sequence alignment 2 */
70 int status; /* easel return code */
71 char errbuf[eslERRBUFSIZE*4];
72
73 int *msa1_to_msa2_map; /* map from <msafile1> to <msafile2> */
74 char *sub_msa1_to_msa2_mask; /* with --sub the map from <msafile1> to <msafile2> in mask form */
75 FILE *subfp = NULL;
76
77 /***********************************************
78 * Parse command line
79 ***********************************************/
80
81 go = esl_getopts_Create(options);
82 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK ||
83 esl_opt_VerifyConfig(go) != eslOK)
84 {
85 printf("Failed to parse command line: %s\n", go->errbuf);
86 esl_usage(stdout, argv[0], usage);
87 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
88 exit(1);
89 }
90
91 if (esl_opt_GetBoolean(go, "-h") )
92 {
93 esl_banner(stdout, argv[0], banner);
94 esl_usage (stdout, argv[0], usage);
95 puts("\nwhere basic options are:");
96 esl_opt_DisplayHelp(stdout, go, 1, 2, 80);
97 exit(0);
98 }
99
100 if (esl_opt_ArgNumber(go) != 2)
101 {
102 printf("Incorrect number of command line arguments.\n");
103 esl_usage(stdout, argv[0], usage);
104 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
105 exit(1);
106 }
107
108 alifile1 = esl_opt_GetArg(go, 1);
109 alifile2 = esl_opt_GetArg(go, 2);
110
111 fmt = eslMSAFILE_STOCKHOLM;
112
113 /***********************************************
114 * Open the MSA files
115 ***********************************************/
116
117 if (esl_opt_GetBoolean(go, "--amino")) abc = esl_alphabet_Create(eslAMINO);
118 else if (esl_opt_GetBoolean(go, "--dna")) abc = esl_alphabet_Create(eslDNA);
119 else if (esl_opt_GetBoolean(go, "--rna")) abc = esl_alphabet_Create(eslRNA);
120
121 if ( (status = esl_msafile_Open(&abc, alifile1, NULL, fmt, NULL, &afp1)) != eslOK) esl_msafile_OpenFailure(afp1, status);
122 if ( (status = esl_msafile_Open(&abc, alifile2, NULL, fmt, NULL, &afp2)) != eslOK) esl_msafile_OpenFailure(afp2, status);
123
124 /******************************************************************
125 * Read first alignment from each file, we only use the first one
126 ******************************************************************/
127
128 if ((status = esl_msafile_Read(afp1, &msa1)) != eslOK) esl_msafile_ReadFailure(afp1, status);
129 if ((status = esl_msafile_Read(afp2, &msa2)) != eslOK) esl_msafile_ReadFailure(afp2, status);
130
131 /* map the alignments in msa1 and msa2 */
132 if(! esl_opt_IsOn(go, "--submap")) {
133 if((status = map_msas(go, errbuf, msa1, msa2, &msa1_to_msa2_map)) != eslOK) goto ERROR;
134 free(msa1_to_msa2_map);
135 }
136
137 /* --submap: if nec, map <msafile1> to a subset of it's own columns in <msafile2> */
138 else { /* --submap was enabled */
139 if ((subfp = fopen(esl_opt_GetString(go, "--submap"), "w")) == NULL)
140 ESL_FAIL(eslFAIL, errbuf, "Failed to open --submap output file %s\n", esl_opt_GetString(go, "--submap"));
141 if((status = map_sub_msas(go, errbuf, msa1, msa2, &sub_msa1_to_msa2_mask)) != eslOK) goto ERROR;
142 fprintf(subfp, "%s\n", sub_msa1_to_msa2_mask);
143 fclose(subfp);
144 subfp = NULL;
145 printf("# Mask of 1/0s with 1 indicating aln column in %s maps to a column in %s saved to file %s.\n", alifile1, alifile2, esl_opt_GetString(go, "--submap"));
146 free(sub_msa1_to_msa2_mask);
147 }
148
149 /* Cleanup, normal return
150 */
151 esl_msafile_Close(afp1);
152 esl_msafile_Close(afp2);
153 esl_alphabet_Destroy(abc);
154 esl_getopts_Destroy(go);
155 esl_msa_Destroy(msa1);
156 esl_msa_Destroy(msa2);
157 return 0;
158
159 ERROR:
160 if (afp1) esl_msafile_Close(afp1);
161 if (afp2) esl_msafile_Close(afp2);
162 if (go) esl_getopts_Destroy(go);
163 if (msa1) esl_msa_Destroy(msa1);
164 if (msa2) esl_msa_Destroy(msa2);
165 if (subfp) fclose(subfp);
166 esl_fatal(errbuf);
167 return 1; /* never reached */
168 }
169
170
171 /* map_msas
172 *
173 * Align msa1 and msa2.
174 * For each column in msa1, determine the corresponding column
175 * in msa2. This implementation requires:
176 * - msa1 and msa2 contain exactly the same sequences in the same order
177 * Note: the seqs in msa1 and msa2 do not have to have the same names.
178 *
179 * Uses a DP algorithm similar to Needleman-Wunsch, but that's aligning
180 * two alignment columns at a time instead of two residues.
181 */
182 static int
183 map_msas(const ESL_GETOPTS *go, char *errbuf, ESL_MSA *msa1, ESL_MSA *msa2, int **ret_msa1_to_msa2_map)
184 {
185 int status;
186 int **one2two; /* [0..c..rflen1][0..a..alen2] number of residues from non-gap RF column c of msa1
187 * aligned in column a of msa 2 */
188 int *rf2a_map1 = NULL; /* msa1 map of reference columns (non-gap RF residues) to alignment columns, NULL if msa1->rf == NULL */
189 int *rf2a_map2 = NULL; /* msa2 map of reference columns (non-gap RF residues) to alignment columns, NULL if msa2->rf == NULL */
190 int *a2rf_map1 = NULL; /* msa1 map of alignment columns to reference columns, NULL if msa1->rf == NULL */
191 int *a2rf_map2 = NULL; /* msa2 map of alignment columns to reference columns, NULL if msa2->rf == NULL */
192 int apos1, apos2; /* counters over alignment position in msa1, msa2 respectively */
193 int alen1, alen2; /* alignment lengths */
194 int rfpos1, rfpos2; /* counters over reference positions */
195 int rflen1, rflen2; /* reference (non-gap RF) lengths */
196 int **mx; /* [0..c..rflen1][0..a..alen2] dp matrix, score of max scoring aln
197 * from 1..c in msa1 and 1..a in msa 2 */
198 int **tb; /* [0..c..rflen1][0..a..alen2] traceback ptrs, 0 for diagonal, 1 for vertical */
199 char *seq1, *seq2; /* temporary strings for ensuring dealigned sequences in msa1 and msa2 are identical */
200 int64_t len1, len2; /* length of seq1, seq2 */
201 int isgap1, isgap2; /* is this residue a gap in msa1, msa2? */
202 int i; /* counter over sequences */
203 int *res1_per_apos; /* [0..apos..alen1] number of residues in column apos of msa1 */
204 int sc; /* max score of full path (alignment) through dp mx */
205 int tb_sc; /* score of traceback, should equal sc */
206 int *one2two_map; /* [0..a..alen1] the alignment, msa2 column that column apos1 in msa1 maps to */
207 int total_res = 0; /* total number of residues in msa1 */
208 float coverage; /* fraction of total_res that are within mapped msa2 columns from one2two_map,
209 * this is tb_sc / total_res */
210 int total_cres1=0; /* total number of residues in reference positions in msa1 */
211 int covered_cres1 = 0; /* number of residues in reference positions in msa1 that also appear in the corresponding
212 * mapped column of msa2
213 */
214 int be_quiet = esl_opt_GetBoolean(go, "-q");
215 int *choices;
216 int i_choice;
217
218 /* contract check */
219 if(! (msa1->flags & eslMSA_DIGITAL)) ESL_FAIL(eslEINVAL, errbuf, "in map_msas() msa1 (%s) not digitized.\n", esl_opt_GetArg(go, 1));
220 if(! (msa2->flags & eslMSA_DIGITAL)) ESL_FAIL(eslEINVAL, errbuf, "in map_msas() msa2 (%s) not digitized.\n", esl_opt_GetArg(go, 2));
221 alen1 = msa1->alen;
222 alen2 = msa2->alen;
223
224 /* Map msa1 (reference) columns to alignment positions */
225 rflen1 = rflen2 = 0;
226 if(msa1->rf != NULL) if((status = map_rfpos_to_apos(msa1, &rf2a_map1, &a2rf_map1, &rflen1)) != eslOK) goto ERROR;
227 if(msa2->rf != NULL) if((status = map_rfpos_to_apos(msa2, &rf2a_map2, &a2rf_map2, &rflen2)) != eslOK) goto ERROR;
228 if(! be_quiet) {
229 printf("# %-25s alignment length: %d\n", esl_opt_GetArg(go, 1), alen1);
230 printf("# %-25s alignment length: %d\n", esl_opt_GetArg(go, 2), alen2);
231 }
232 /* collect counts in one2two[i][j]: number of sequences for which residue aligned in msa1 non-gap column i
233 * is aligned in msa2 alignment column j.
234 */
235 ESL_ALLOC(seq1, sizeof(char) * (alen1+1));
236 ESL_ALLOC(seq2, sizeof(char) * (alen2+1));
237 ESL_ALLOC(one2two, sizeof(int *) * (alen1+1));
238 for(apos1 = 0; apos1 <= alen1; apos1++) {
239 ESL_ALLOC(one2two[apos1], sizeof(int) * (alen2+1));
240 esl_vec_ISet(one2two[apos1], (alen2+1), 0);
241 }
242
243 total_res = 0;
244 for(i = 0; i < msa1->nseq; i++) {
245 /* ensure raw (unaligned) seq i in the 2 msas is the same */
246 esl_abc_Textize(msa1->abc, msa1->ax[i], alen1, seq1);
247 esl_abc_Textize(msa1->abc, msa2->ax[i], alen2, seq2); /* note: msa*1*->abc used on purpose, allows DNA/RNA to peacefully coexist in this func */
248 esl_strdealign(seq1, seq1, "-_.~", &len1);
249 esl_strdealign(seq2, seq2, "-_.~", &len2);
250
251 if(len1 != len2) {
252 ESL_FAIL(eslEINVAL, errbuf, "unaligned seq number %d (msa1: %s, msa2: %s) differs in length %s (%" PRId64 ") and %s (%" PRId64 "), those files must contain identical raw seqs\n",
253 i, msa1->sqname[i], msa2->sqname[i], esl_opt_GetArg(go, 1), len1, esl_opt_GetArg(go, 2), len2);
254 }
255 if(strncmp(seq1, seq2, len1) != 0) ESL_FAIL(eslEINVAL, errbuf, "unaligned seq number %d differs between %s and %s, those files must contain identical raw seqs\n", i, esl_opt_GetArg(go, 1), esl_opt_GetArg(go, 2));
256 total_res += len1;
257
258 apos1 = apos2 = 1;
259 while((apos1 <= alen1) || (apos2 <= alen2)) {
260 isgap1 = esl_abc_XIsGap(msa1->abc, msa1->ax[i][apos1]);
261 isgap2 = esl_abc_XIsGap(msa2->abc, msa2->ax[i][apos2]);
262 if ( isgap1 && isgap2) { apos1++; apos2++; }
263 else if ( isgap1 && !isgap2) { apos1++; }
264 else if (!isgap1 && isgap2) { apos2++; }
265 else if ( msa1->ax[i][apos1] == msa2->ax[i][apos2]) {
266 one2two[apos1++][apos2++]++;
267 /* two2one[apos2][apos1]++; */
268 }
269 }
270 }
271
272 /******************************************************************
273 * DP alignment of msa1 to msa2
274 * dp matrix: mx[apos1][apos2] apos1=1..msa->alen1, apos2=1..alen2 (apos1=0 || apos2=0 is invalid)
275 * mx[apos1][apos2] = score of maximal alignment for apos1=1..apos1, apos2'=1..apos2 INCLUDING
276 * apos1 and apos2. Score is number of residues from msa1 columns
277 * 1..apos1 that exist in their respective aligned columns in msa2 (the growing
278 * maximally scoring alignment).
279 */
280
281 /******************************************************************
282 * initialization
283 */
284 ESL_ALLOC(mx, sizeof(int *) * (alen1+1));
285 ESL_ALLOC(tb, sizeof(int *) * (alen1+1));
286 for(apos1 = 0; apos1 <= alen1; apos1++) {
287 ESL_ALLOC(mx[apos1], sizeof(int) * (alen2+1));
288 ESL_ALLOC(tb[apos1], sizeof(int) * (alen2+1));
289 esl_vec_ISet(mx[apos1], (alen2+1), 0);
290 esl_vec_ISet(tb[apos1], (alen2+1), -2); /* -2 is a bogus value, if we see it during traceback, there's a problem */
291 tb[apos1][0] = HORZ; /* special case, if we hit apos2==0 and apos1 > 0, we have to do HORZ moves until apos1==1 */
292 }
293 esl_vec_ISet(tb[0], (alen2+1), VERT); /* special case, if we hit apos1==0 and apos2 > 0, we have to do VERT moves until apos2==1 */
294 tb[0][0] = -2; /* all alignments must end here */
295
296 ESL_ALLOC(res1_per_apos, sizeof(int) * (alen1+1));
297 esl_vec_ISet(res1_per_apos, (alen1+1), 0);
298 mx[0][0] = 0;
299 tb[0][0] = -1; /* last cell, special value */
300
301 /*****************************************************************
302 * recursion
303 */
304 ESL_ALLOC(choices, sizeof(int) * NCHOICES);
305 for(apos1 = 1; apos1 <= alen1; apos1++) {
306 for(apos2 = 1; apos2 <= alen2; apos2++) {
307 choices[DIAG] = mx[(apos1-1)][(apos2-1)] + one2two[apos1][apos2];
308 choices[VERT] = mx[ apos1 ][(apos2-1)];
309 choices[HORZ] = mx[(apos1-1)][ apos2 ];
310 i_choice = esl_vec_IArgMax(choices, NCHOICES);
311 mx[apos1][apos2] = choices[i_choice];
312 tb[apos1][apos2] = i_choice;
313 res1_per_apos[apos1] += one2two[apos1][apos2];
314 /*printf("mx[%3d][%3d]: %5d (%d)\n", apos1, apos2, mx[apos1][apos2], tb[apos1][apos2]);*/
315 }
316 }
317 free(choices);
318
319 total_cres1 = 0;
320 if(rf2a_map1 != NULL) {
321 for(rfpos1 = 1; rfpos1 <= rflen1; rfpos1++) total_cres1 += res1_per_apos[rf2a_map1[rfpos1]];
322 }
323
324 /*****************************************************************
325 * traceback
326 */
327
328 sc = mx[alen1][alen2];
329 if(!be_quiet) {
330 /* printf("score %d\n", sc);*/
331 if(a2rf_map1 != NULL && a2rf_map2 != NULL) {
332 printf("# %12s %12s %22s\n", " msa 1 ", " msa 2 ", "");
333 printf("# %12s %12s %22s\n", "------------", "------------", "");
334 printf("# %5s %5s %5s %5s %22s\n", "rfpos", "apos", "rfpos", "apos", " num common residues");
335 printf("# %5s %5s %5s %5s %22s\n", "-----", "-----", "-----", "-----", "---------------------");
336 }
337 else if(a2rf_map1 != NULL) {
338 printf("# %12s %5s %22s\n", " msa 1 ", "msa 2", "");
339 printf("# %12s %5s %22s\n", "------------", "-----", "");
340 printf("# %5s %5s %5s %22s\n", "rfpos", "apos", "apos", " num common residues");
341 printf("# %5s %5s %5s %22s\n", "-----", "-----", "-----", "---------------------");
342 }
343 else if (a2rf_map2 != NULL) {
344 printf("# %5s %12s %22s\n", "msa 1", " msa 2 ", "");
345 printf("# %5s %12s %22s\n", "-----", "------------", "");
346 printf("# %5s %5s %5s %22s\n", "apos", "rfpos", "apos", " num common residues");
347 printf("# %5s %5s %5s %22s\n", "-----", "-----", "-----", "---------------------");
348 }
349 else {
350 printf("# %5s %5s %22s\n", "msa 1", "msa 2", "");
351 printf("# %5s %5s %22s\n", "-----", "-----", "");
352 printf("# %5s %5s %22s\n", "apos", "apos", " num common residues");
353 printf("# %5s %5s %22s\n", "-----", "-----", "---------------------");
354 }
355 }
356
357 /* traceback, and build one2two_map[] */
358 apos1 = alen1;
359 apos2 = alen2;
360 tb_sc = 0;
361 covered_cres1 = 0;
362 ESL_ALLOC(one2two_map, sizeof(int) * (alen1+1));
363 esl_vec_ISet(one2two_map, (alen1+1), 0);
364 one2two_map[0] = -1; /* invalid */
365
366 while(tb[apos1][apos2] != -1) {
367 if(tb[apos1][apos2] == DIAG) { /* diagonal move */
368 rfpos1 = (a2rf_map1 == NULL) ? -1 : a2rf_map1[apos1];
369 rfpos2 = (a2rf_map2 == NULL) ? -1 : a2rf_map2[apos2];
370 if(!be_quiet) {
371 if(a2rf_map1 != NULL && a2rf_map2 != NULL) {
372 if(rfpos1 == -1 && rfpos2 == -1) {
373 printf(" %5s %5d --> %5s %5d %5d / %5d (%.4f)\n", "-", apos1, "-", apos2, one2two[apos1][apos2], res1_per_apos[apos1], (res1_per_apos[apos1] == 0) ? 0.0000 : ((float) one2two[apos1][apos2] / (float) res1_per_apos[apos1]));
374 }
375 else if (rfpos1 == -1) {
376 printf(" %5s %5d --> %5d %5d %5d / %5d (%.4f)\n", "-", apos1, rfpos2, apos2, one2two[apos1][apos2], res1_per_apos[apos1], (res1_per_apos[apos1] == 0) ? 0.0000 : ((float) one2two[apos1][apos2] / (float) res1_per_apos[apos1]));
377 }
378 else if (rfpos2 == -1) {
379 printf(" %5d %5d --> %5s %5d %5d / %5d (%.4f)\n", rfpos1, apos1, "-", apos2, one2two[apos1][apos2], res1_per_apos[apos1], (res1_per_apos[apos1] == 0) ? 0.0000 : ((float) one2two[apos1][apos2] / (float) res1_per_apos[apos1]));
380 }
381 else {
382 printf(" %5d %5d --> %5d %5d %5d / %5d (%.4f)\n", rfpos1, apos1, rfpos2, apos2, one2two[apos1][apos2], res1_per_apos[apos1], (res1_per_apos[apos1] == 0) ? 0.0000 : ((float) one2two[apos1][apos2] / (float) res1_per_apos[apos1]));
383 }
384 }
385 else if(a2rf_map1 != NULL) {
386 if (rfpos1 == -1) {
387 printf(" %5s %5d --> %5d %5d / %5d (%.4f)\n", "-", apos1, apos2, one2two[apos1][apos2], res1_per_apos[apos1], (res1_per_apos[apos1] == 0) ? 0.0000 : ((float) one2two[apos1][apos2] / (float) res1_per_apos[apos1]));
388 }
389 else {
390 printf(" %5d %5d --> %5d %5d / %5d (%.4f)\n", rfpos1, apos1, apos2, one2two[apos1][apos2], res1_per_apos[apos1], (res1_per_apos[apos1] == 0) ? 0.0000 : ((float) one2two[apos1][apos2] / (float) res1_per_apos[apos1]));
391 }
392 }
393 else if (a2rf_map2 != NULL) {
394 if (rfpos2 == -1) {
395 printf(" %5d --> %5s %5d %5d / %5d (%.4f)\n", apos1, "-", apos2, one2two[apos1][apos2], res1_per_apos[apos1], (res1_per_apos[apos1] == 0) ? 0.0000 : ((float) one2two[apos1][apos2] / (float) res1_per_apos[apos1]));
396 }
397 else {
398 printf(" %5d --> %5d %5d %5d / %5d (%.4f)\n", apos1, rfpos2, apos2, one2two[apos1][apos2], res1_per_apos[apos1], (res1_per_apos[apos1] == 0) ? 0.0000 : ((float) one2two[apos1][apos2] / (float) res1_per_apos[apos1]));
399 }
400 }
401 else {
402 printf(" %5d --> %5d %5d / %5d (%.4f)\n", apos1, apos2, one2two[apos1][apos2], res1_per_apos[apos1], (res1_per_apos[apos1] == 0) ? 0.0000 : ((float) one2two[apos1][apos2] / (float) res1_per_apos[apos1]));
403 }
404 }
405 tb_sc += one2two[apos1][apos2];
406 one2two_map[apos1] = apos2;
407 if(rfpos1 > 0) covered_cres1 += one2two[apos1][apos2]; /* apos1 is a rfpos */
408 apos1--; apos2--;
409 }
410 else if(tb[apos1][apos2] == VERT) {
411 apos2--; /* vertical move */
412 }
413 else if(tb[apos1][apos2] == HORZ) {
414 apos1--; /* horizontal move */
415 }
416 else if(tb[apos1][apos2] != -1) /* shouldn't happen */
417 ESL_FAIL(eslEINVAL, errbuf, "in dp traceback, tb[apos1: %d][apos2: %d] %d\n", apos1, apos2, tb[apos1][apos2]);
418 }
419 /* done DP code
420 **********************************/
421
422 if(!be_quiet) printf("# Total trace back sc: %d\n", tb_sc);
423 if(tb_sc != sc) ESL_FAIL(eslEINVAL, errbuf, "in dp traceback, tb_sc (%d) != sc (%d)\n", tb_sc, sc);
424 coverage = (float) tb_sc / (float) total_res;
425 printf("# Coverage: %6d / %6d (%.4f)\n# Coverage is fraction of residues from %s in optimally mapped columns in %s\n", tb_sc, total_res, coverage, esl_opt_GetArg(go, 1), esl_opt_GetArg(go, 2));
426 if(total_cres1 > 0) printf("# RF coverage: %6d / %6d (%.4f)\n# RF coverage is fraction of non-gap RF residues from %s in optimally mapped columns in %s\n", covered_cres1, total_cres1, (float) covered_cres1 / (float) total_cres1, esl_opt_GetArg(go, 1), esl_opt_GetArg(go, 2));
427 /* print masks if nec */
428 if((status = map2masks(go, errbuf, alen1, alen2, a2rf_map1, a2rf_map2, rf2a_map1, rf2a_map2, rflen1, rflen2, one2two_map)) != eslOK) return status;
429
430 /* clean up and return */
431 for(apos1 = 0; apos1 <= alen1; apos1++) {
432 free(mx[apos1]);
433 free(tb[apos1]);
434 }
435 free(mx);
436 free(tb);
437
438 for(apos1 = 0; apos1 <= alen1; apos1++) free(one2two[apos1]);
439 free(one2two);
440 free(res1_per_apos);
441 if(rf2a_map1 != NULL) free(rf2a_map1);
442 if(rf2a_map2 != NULL) free(rf2a_map2);
443 if(a2rf_map1 != NULL) free(a2rf_map1);
444 if(a2rf_map2 != NULL) free(a2rf_map2);
445
446 free(seq1);
447 free(seq2);
448 *ret_msa1_to_msa2_map = one2two_map;
449 return eslOK;
450
451 ERROR:
452 return status;
453 }
454
455
456 /* map_sub_msas
457 *
458 * msa1 and msa2 contain the same named sequences, msa1 contains a superset
459 * of the columns in msa2. Determine which of the msa1 columns the msa2
460 * columns correspond to.
461 */
462 static int
463 map_sub_msas(const ESL_GETOPTS *go, char *errbuf, ESL_MSA *msa1, ESL_MSA *msa2, char **ret_msa1_to_msa2_mask)
464 {
465 int status;
466 int apos1, apos2; /* counters over alignment position in msa1, msa2 respectively */
467 int i;
468 int *msa1_to_msa2_map; /* [0..apos1..msa1->alen] msa2 alignment position that apos1 corresponds to */
469 char *mask;
470
471 /* contract check */
472 if(! (msa1->flags & eslMSA_DIGITAL)) ESL_FAIL(eslEINVAL, errbuf, "in map_sub_msas() msa1 (%s) not digitized.\n", esl_opt_GetArg(go, 1));
473 if(! (msa2->flags & eslMSA_DIGITAL)) ESL_FAIL(eslEINVAL, errbuf, "in map_sub_msas() msa2 (%s) not digitized.\n", esl_opt_GetString(go, "--submap"));
474 if(msa1->alen <= msa2->alen) ESL_FAIL(eslEINVAL, errbuf, "in map_sub_msas() alignment length for msa1 (%" PRId64 "d) <= length for msa2 (%" PRId64 ")\n", msa1->alen, msa2->alen);
475
476 ESL_ALLOC(mask, sizeof(char) * (msa1->alen+1));
477 for(apos1 = 0; apos1 < msa1->alen; apos1++) mask[apos1] = '0';
478 mask[msa1->alen] = '\0';
479
480 ESL_ALLOC(msa1_to_msa2_map, sizeof(int) * (msa1->alen+1));
481 esl_vec_ISet(msa1_to_msa2_map, (msa1->alen+1), -1);
482
483 /* both alignments must have same 'named' sequences in same order */
484 if(msa1->nseq != msa2->nseq) ESL_FAIL(eslEINVAL, errbuf, "in map_sub_msas() msa1 has %d sequences, msa2 has %d sequences\n", msa1->nseq, msa2->nseq);
485 for(i = 0; i < msa1->nseq; i++) {
486 if(strcmp(msa1->sqname[i], msa2->sqname[i]) != 0) ESL_FAIL(eslEINVAL, errbuf, "in map_sub_msas() msa1 seq %d is named %s, msa2 seq %d is named %s\n", i, msa1->sqname[i], i, msa2->sqname[i]);
487 }
488
489 apos1 = 1;
490 apos2 = 1;
491 while((apos2 <= msa2->alen) || (apos1 <= msa1->alen)) { /* determine which apos1 (alignment column in msa1), apos2 (alignment column in msa2) corresponds to */
492 for(i = 0; i < msa1->nseq; i++) {
493 if(msa1->ax[i][apos1] != msa2->ax[i][apos2]) {
494 apos1++;
495 break; /* try next apos1 */
496 }
497 }
498 if(i == msa1->nseq) { /* found a match */
499 msa1_to_msa2_map[apos1] = apos2;
500 mask[(apos1-1)] = '1';
501 apos1++;
502 apos2++;
503 }
504 }
505 if((apos1 != (msa1->alen+1)) || (apos2 != (msa2->alen+1))) ESL_FAIL(eslEINVAL, errbuf, "in map_sub_msas(), failure mapping alignments, end of loop apos1-1 = %d (msa1->alen: %" PRId64 ") and apos2-1 = %d (msa2->alen: %" PRId64 ")\n", apos1-1, msa1->alen, apos2-1, msa2->alen);
506
507 free(msa1_to_msa2_map);
508 *ret_msa1_to_msa2_mask = mask;
509 return eslOK;
510
511 ERROR:
512 return status;
513 }
514
515 /* map_rfpos_to_apos
516 *
517 * Given an MSA, determine the alignment position each
518 * reference position refers to.
519 */
520 static int map_rfpos_to_apos(ESL_MSA *msa, int **ret_rf2a_map, int **ret_a2rf_map, int *ret_rflen)
521 {
522 int status;
523 int rflen = 0;
524 int *rf2a_map = NULL;
525 int *a2rf_map = NULL;
526 int rfpos = 0;
527 int apos = 0;
528 /* contract check */
529 if(msa->rf == NULL) { status = eslEINVAL; goto ERROR; }
530
531 /* count reference columns */
532 for(apos = 1; apos <= msa->alen; apos++)
533 if((! esl_abc_CIsGap(msa->abc, msa->rf[(apos-1)])) &&
534 (! esl_abc_CIsMissing(msa->abc, msa->rf[(apos-1)])) &&
535 (! esl_abc_CIsNonresidue(msa->abc, msa->rf[(apos-1)]))) rflen++;
536
537 /* build maps */
538 ESL_ALLOC(rf2a_map, sizeof(int) * (rflen+1));
539 ESL_ALLOC(a2rf_map, sizeof(int) * (msa->alen+1));
540 esl_vec_ISet(a2rf_map, msa->alen+1, -1);
541 rf2a_map[0] = -1;
542 for(apos = 1; apos <= msa->alen; apos++) {
543 if((! esl_abc_CIsGap(msa->abc, msa->rf[(apos-1)])) &&
544 (! esl_abc_CIsMissing(msa->abc, msa->rf[(apos-1)])) &&
545 (! esl_abc_CIsNonresidue(msa->abc, msa->rf[(apos-1)]))) {
546 rf2a_map[++rfpos] = apos;
547 a2rf_map[apos] = rfpos;
548 }
549 /* else a2rf_map[apos] remains -1 as it was initialized */
550 }
551
552
553 if(ret_rf2a_map != NULL) *ret_rf2a_map = rf2a_map;
554 else free(rf2a_map);
555 if(ret_a2rf_map != NULL) *ret_a2rf_map = a2rf_map;
556 else free(a2rf_map);
557 if(ret_rflen != NULL) *ret_rflen = rflen;
558 return eslOK;
559
560 ERROR:
561 if(rf2a_map != NULL) free(rf2a_map);
562 if(a2rf_map != NULL) free(a2rf_map);
563 return status;
564 }
565
566 /* map2masks
567 *
568 * Given a map of alignment columns in msa1 to alignment columns
569 * to msa2, construct and output masks as per command-line options.
570 *
571 * Args: msa1_to_msa2_map: [1..apos..msa1->alen]: '0': msa1 apos maps to a gap in msa2 (doesn't map to any column in msa2)
572 * 'x': msa1 apos maps to posn x in msa2 (x>0)
573 */
574 static int
575 map2masks(const ESL_GETOPTS *go, char *errbuf, int alen1, int alen2, int *a2rf_map1, int *a2rf_map2, int *rf2a_map1, int *rf2a_map2, int rflen1, int rflen2, int *msa1_to_msa2_map)
576 {
577 int status;
578 int apos1, apos2; /* counters over alignment position in msa1, msa2 respectively */
579 int rfpos1, rfpos2; /* counters over reference positions */
580 int num_ones; /* number of 1s in current mask */
581 int num_zeroes; /* number of 0s in current mask */
582 FILE *fp;
583 char *mask = NULL;
584
585 if(esl_opt_GetString(go, "--mask-a2a")) {
586 if ((fp = fopen(esl_opt_GetString(go, "--mask-a2a"), "w")) == NULL)
587 ESL_FAIL(eslFAIL, errbuf, "Failed to open --mask-a2a mask output file %s", esl_opt_GetString(go, "--mask-a2a"));
588 /* construct mask as follows:
589 * mask[0..apos1..alen1-1] = '1' if column apos1+1 maps to an alignment column of msa2
590 * = '0' if column apos1+1 maps to a gap in msa2 (doesn't map to any column in msa2)
591 */
592 ESL_ALLOC(mask, sizeof(char) * (alen1+1));
593 num_ones = num_zeroes = 0;
594 for(apos1 = 1; apos1 <= alen1; apos1++) {
595 if(msa1_to_msa2_map[apos1] == 0) { mask[(apos1-1)] = '0'; num_zeroes++; }
596 else { mask[(apos1-1)] = '1'; num_ones++; }
597 }
598 mask[alen1] = '\0';
599 fprintf(fp, "%s\n", mask);
600 free(mask);
601 fclose(fp);
602 printf("# Mask of 1/0s with 1 indicating aln column in %s maps to aln column in %s saved to file %s.\n# (Length: %d; '1's: %d; '0's: %d)\n", esl_opt_GetArg(go, 1), esl_opt_GetArg(go, 2), esl_opt_GetString(go, "--mask-a2a"), (num_ones+num_zeroes), num_ones, num_zeroes);
603 }
604
605 if(esl_opt_GetString(go, "--mask-a2rf")) {
606 if (a2rf_map2 == NULL) ESL_FAIL(eslFAIL, errbuf, "with --mask-a2rf, <msafile2> %s must have #=GC RF annotation, but it doesn't.", esl_opt_GetArg(go, 2));
607 if ((fp = fopen(esl_opt_GetString(go, "--mask-a2rf"), "w")) == NULL)
608 ESL_FAIL(eslFAIL, errbuf, "Failed to open --mask-a2rf mask output file %s\n", esl_opt_GetString(go, "--mask-a2rf"));
609 /* construct mask as follows:
610 * mask[0..apos1..alen1-1] = '1' if column apos1+1 maps to a reference column (non-gap in RF) of msa2
611 * = '0' if column apos1+1 maps to a gap (doesn't map to any column in msa2) or an insert (gap in RF) in msa2
612 */
613 ESL_ALLOC(mask, sizeof(char) * (alen1+1));
614 num_ones = num_zeroes = 0;
615 for(apos1 = 1; apos1 <= alen1; apos1++) {
616 apos2 = msa1_to_msa2_map[apos1];
617 if(apos2 == 0) { mask[(apos1-1)] = '0'; num_zeroes++; } /* apos1 doesn't map to any column in msa2 */
618 else {
619 rfpos2 = a2rf_map2[apos2];
620 if(rfpos2 <= 0) { mask[(apos1-1)] = '0'; num_zeroes++; } /* apos1 maps to a gap RF (insert) in msa2 */
621 else { mask[(apos1-1)] = '1'; num_ones++; } /* apos1 maps to a non-gap RF (reference) column in msa2 */
622 }
623 }
624 mask[alen1] = '\0';
625 fprintf(fp, "%s\n", mask);
626 free(mask);
627 fclose(fp);
628 printf("# Mask of 1/0s with 1 indicating aln column in %s maps to reference (non-gap RF) column in %s saved to file %s.\n# (Length: %d; '1's: %d; '0's: %d)\n", esl_opt_GetArg(go, 1), esl_opt_GetArg(go, 2), esl_opt_GetString(go, "--mask-a2rf"), (num_ones+num_zeroes), num_ones, num_zeroes);
629 }
630
631 if(esl_opt_GetString(go, "--mask-rf2a")) {
632 if (a2rf_map1 == NULL) ESL_FAIL(eslFAIL, errbuf, "with --mask-rf2a, <msafile1> %s must have #=GC RF annotation, but it doesn't.", esl_opt_GetArg(go, 1));
633 if ((fp = fopen(esl_opt_GetString(go, "--mask-rf2a"), "w")) == NULL)
634 ESL_FAIL(eslFAIL, errbuf, "Failed to open --mask-rf2a mask output file %s\n", esl_opt_GetString(go, "--mask-rf2a"));
635 /* construct mask as follows:
636 * mask[0..rfpos1..rflen-1] = '1' if non-gap RF msa1 column rfpos1+1 maps to an alignment column of msa2
637 * = '0' if non-gap RF msa1 column rfpos1+1 maps to a gap in msa2 (doesn't map to any column in msa2)
638 */
639 ESL_ALLOC(mask, sizeof(char) * (rflen1+1));
640 num_ones = num_zeroes = 0;
641 for(rfpos1 = 1; rfpos1 <= rflen1; rfpos1++) {
642 apos1 = rf2a_map1[rfpos1];
643 apos2 = msa1_to_msa2_map[apos1];
644 if(apos2 == 0) { mask[(rfpos1-1)] = '0'; num_zeroes++; }
645 else { mask[(rfpos1-1)] = '1'; num_ones++; }
646 }
647 mask[rflen1] = '\0';
648 fprintf(fp, "%s\n", mask);
649 free(mask);
650 fclose(fp);
651 printf("# Mask of 1/0s with 1 indicating reference (non-gap RF) column in %s maps to aln column in %s saved to file %s.\n# (Length: %d; '1's: %d; '0's: %d)\n", esl_opt_GetArg(go, 1), esl_opt_GetArg(go, 2), esl_opt_GetString(go, "--mask-rf2a"), (num_ones+num_zeroes), num_ones, num_zeroes);
652 }
653
654 if(esl_opt_GetString(go, "--mask-rf2rf")) {
655 if (a2rf_map1 == NULL) ESL_FAIL(eslFAIL, errbuf, "with --mask-rf2rf, <msafile1> %s must have #=GC RF annotation, but it doesn't.", esl_opt_GetArg(go, 1));
656 if (a2rf_map2 == NULL) ESL_FAIL(eslFAIL, errbuf, "with --mask-rf2rf, <msafile2> %s must have #=GC RF annotation, but it doesn't.", esl_opt_GetArg(go, 2));
657 if ((fp = fopen(esl_opt_GetString(go, "--mask-rf2rf"), "w")) == NULL)
658 ESL_FAIL(eslFAIL, errbuf, "Failed to open --mask-rf2rf mask output file %s\n", esl_opt_GetString(go, "--mask-rf2rf"));
659 /* construct mask as follows:
660 * mask[0..apos1..alen-1] = '1' if column apos1+1 maps to a reference column (non-gap in RF) of msa2
661 * = '0' if column apos1+1 maps to a gap (doesn't map to any column in msa2) or an insert (gap in RF) in msa2
662 */
663 ESL_ALLOC(mask, sizeof(char) * (alen1+1));
664 num_ones = num_zeroes = 0;
665 for(rfpos1 = 1; rfpos1 <= rflen1; rfpos1++) {
666 apos1 = rf2a_map1[rfpos1];
667 apos2 = msa1_to_msa2_map[apos1];
668 if(apos2 == 0) { mask[(rfpos1-1)] = '0'; num_zeroes++; } /* rfpos1 doesn't map to any column in msa2 */
669 else {
670 rfpos2 = a2rf_map2[apos2];
671 if(rfpos2 <= 0) { mask[(rfpos1-1)] = '0'; num_zeroes++; } /* rfpos1 maps to a gap RF (insert) in msa2 */
672 else { mask[(rfpos1-1)] = '1'; num_ones++; } /* rfpos1 maps to a non-gap RF (reference) column in msa2 */
673 }
674 }
675 mask[rflen1] = '\0';
676 fprintf(fp, "%s\n", mask);
677 free(mask);
678 fclose(fp);
679 printf("# Mask of 1/0s with 1 indicating reference (non-gap RF) column in %s maps to reference (non-gap RF) column in %s saved to file %s.\n# (Length: %d; '1's: %d; '0's: %d)\n", esl_opt_GetArg(go, 1), esl_opt_GetArg(go, 2), esl_opt_GetString(go, "--mask-rf2rf"), (num_ones+num_zeroes), num_ones, num_zeroes);
680 }
681 return eslOK;
682
683 ERROR:
684 ESL_FAIL(eslEMEM, errbuf, "map2masks(): memory allocation error.");
685 return status; /* NEVERREACHED */
686 }