| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 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; |
| 62 |
ESL_ALPHABET *abc = NULL; |
| 63 |
char *alifile1= NULL; |
| 64 |
char *alifile2= NULL; |
| 65 |
int fmt; |
| 66 |
ESL_MSAFILE *afp1 = NULL; |
| 67 |
ESL_MSAFILE *afp2 = NULL; |
| 68 |
ESL_MSA *msa1 = NULL; |
| 69 |
ESL_MSA *msa2 = NULL; |
| 70 |
int status; |
| 71 |
char errbuf[eslERRBUFSIZE*4]; |
| 72 |
|
| 73 |
int *msa1_to_msa2_map; |
| 74 |
char *sub_msa1_to_msa2_mask; |
| 75 |
FILE *subfp = NULL; |
| 76 |
|
| 77 |
|
| 78 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 138 |
else { |
| 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 |
|
| 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; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 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; |
| 187 |
|
| 188 |
int *rf2a_map1 = NULL; |
| 189 |
int *rf2a_map2 = NULL; |
| 190 |
int *a2rf_map1 = NULL; |
| 191 |
int *a2rf_map2 = NULL; |
| 192 |
int apos1, apos2; |
| 193 |
int alen1, alen2; |
| 194 |
int rfpos1, rfpos2; |
| 195 |
int rflen1, rflen2; |
| 196 |
int **mx; |
| 197 |
|
| 198 |
int **tb; |
| 199 |
char *seq1, *seq2; |
| 200 |
int64_t len1, len2; |
| 201 |
int isgap1, isgap2; |
| 202 |
int i; |
| 203 |
int *res1_per_apos; |
| 204 |
int sc; |
| 205 |
int tb_sc; |
| 206 |
int *one2two_map; |
| 207 |
int total_res = 0; |
| 208 |
float coverage; |
| 209 |
|
| 210 |
int total_cres1=0; |
| 211 |
int covered_cres1 = 0; |
| 212 |
|
| 213 |
|
| 214 |
int be_quiet = esl_opt_GetBoolean(go, "-q"); |
| 215 |
int *choices; |
| 216 |
int i_choice; |
| 217 |
|
| 218 |
|
| 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 |
|
| 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 |
|
| 233 |
|
| 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 |
|
| 246 |
esl_abc_Textize(msa1->abc, msa1->ax[i], alen1, seq1); |
| 247 |
esl_abc_Textize(msa1->abc, msa2->ax[i], alen2, seq2); |
| 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 |
|
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 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); |
| 291 |
tb[apos1][0] = HORZ; |
| 292 |
} |
| 293 |
esl_vec_ISet(tb[0], (alen2+1), VERT); |
| 294 |
tb[0][0] = -2; |
| 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; |
| 300 |
|
| 301 |
|
| 302 |
|
| 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 |
|
| 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 |
|
| 326 |
|
| 327 |
|
| 328 |
sc = mx[alen1][alen2]; |
| 329 |
if(!be_quiet) { |
| 330 |
|
| 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 |
|
| 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; |
| 365 |
|
| 366 |
while(tb[apos1][apos2] != -1) { |
| 367 |
if(tb[apos1][apos2] == DIAG) { |
| 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]; |
| 408 |
apos1--; apos2--; |
| 409 |
} |
| 410 |
else if(tb[apos1][apos2] == VERT) { |
| 411 |
apos2--; |
| 412 |
} |
| 413 |
else if(tb[apos1][apos2] == HORZ) { |
| 414 |
apos1--; |
| 415 |
} |
| 416 |
else if(tb[apos1][apos2] != -1) |
| 417 |
ESL_FAIL(eslEINVAL, errbuf, "in dp traceback, tb[apos1: %d][apos2: %d] %d\n", apos1, apos2, tb[apos1][apos2]); |
| 418 |
} |
| 419 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 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; |
| 467 |
int i; |
| 468 |
int *msa1_to_msa2_map; |
| 469 |
char *mask; |
| 470 |
|
| 471 |
|
| 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 |
|
| 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)) { |
| 492 |
for(i = 0; i < msa1->nseq; i++) { |
| 493 |
if(msa1->ax[i][apos1] != msa2->ax[i][apos2]) { |
| 494 |
apos1++; |
| 495 |
break; |
| 496 |
} |
| 497 |
} |
| 498 |
if(i == msa1->nseq) { |
| 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 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 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 |
|
| 529 |
if(msa->rf == NULL) { status = eslEINVAL; goto ERROR; } |
| 530 |
|
| 531 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 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; |
| 579 |
int rfpos1, rfpos2; |
| 580 |
int num_ones; |
| 581 |
int num_zeroes; |
| 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 |
|
| 589 |
|
| 590 |
|
| 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 |
|
| 610 |
|
| 611 |
|
| 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++; } |
| 618 |
else { |
| 619 |
rfpos2 = a2rf_map2[apos2]; |
| 620 |
if(rfpos2 <= 0) { mask[(apos1-1)] = '0'; num_zeroes++; } |
| 621 |
else { mask[(apos1-1)] = '1'; num_ones++; } |
| 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 |
|
| 636 |
|
| 637 |
|
| 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 |
|
| 660 |
|
| 661 |
|
| 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++; } |
| 669 |
else { |
| 670 |
rfpos2 = a2rf_map2[apos2]; |
| 671 |
if(rfpos2 <= 0) { mask[(rfpos1-1)] = '0'; num_zeroes++; } |
| 672 |
else { mask[(rfpos1-1)] = '1'; num_ones++; } |
| 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; |
| 686 |
} |