| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <esl_config.h> |
| 7 |
|
| 8 |
#include <stdlib.h> |
| 9 |
#include <stdio.h> |
| 10 |
#include <string.h> |
| 11 |
#include <assert.h> |
| 12 |
#include <math.h> |
| 13 |
#include <limits.h> |
| 14 |
|
| 15 |
#include "easel.h" |
| 16 |
#include "esl_distance.h" |
| 17 |
#include "esl_fileparser.h" |
| 18 |
#include "esl_getopts.h" |
| 19 |
#include "esl_sq.h" |
| 20 |
#include "esl_sqio.h" |
| 21 |
#include "esl_msa.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 |
#define CONSOPTS "-x,--ffreq,--fmin,-r,-c,--indi" |
| 30 |
|
| 31 |
static char banner[] = "describe or create a consensus secondary structure"; |
| 32 |
static char usage[] = "[options] <msafile>\n\ |
| 33 |
<msafile> must contain RNA or DNA sequences and be in Stockholm format."; |
| 34 |
|
| 35 |
static int get_gaps_per_column(ESL_MSA *msa, int **ret_ngaps); |
| 36 |
|
| 37 |
static ESL_OPTIONS options[] = { |
| 38 |
|
| 39 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "help; show brief info on version and usage", 1}, |
| 40 |
{ "-a", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, CONSOPTS, "print info on all conflicting bps in individual structures", 1}, |
| 41 |
{ "-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL,NULL, "be verbose", 1 }, |
| 42 |
|
| 43 |
{ "-x", eslARG_NONE, NULL, NULL, NULL, NULL, "-o", CONSOPTS, "set SS_cons as max set of non-conflicting bps from indi SSs", 2 }, |
| 44 |
{ "-r", eslARG_NONE, NULL, NULL, NULL, NULL, "-o", CONSOPTS, "remove SS_cons basepairs that conflicts with > 0 indi SS", 2 }, |
| 45 |
{ "-c", eslARG_NONE, NULL, NULL, NULL, NULL, "-o", CONSOPTS, "set SS_cons as indi SS with max bps consistent with SS_cons", 2 }, |
| 46 |
{ "--rfc", eslARG_NONE, NULL, NULL, NULL, NULL, "-c", NULL, "with -c, set RF annotation as seq SS_cons structure comes from", 2}, |
| 47 |
{ "--indi", eslARG_STRING, NULL, NULL, NULL, NULL, "-o", CONSOPTS, "define SS_cons as individual SS for sequence <x>", 2 }, |
| 48 |
{ "--rfindi", eslARG_NONE, NULL, NULL, NULL, NULL, "--indi",NULL, "with --indi <x>, define RF annotation as <x>", 2 }, |
| 49 |
{ "--ffreq", eslARG_REAL, NULL, NULL,"0.<=x<=1", NULL,"-o", CONSOPTS, "aln cols i:j become SS_cons bps if paired in > <x> indi SS", 2}, |
| 50 |
{ "--fmin", eslARG_NONE, NULL, NULL, NULL, NULL, "-o", CONSOPTS, "same as --ffreq but find min <x> that gives consistent SS_cons", 2}, |
| 51 |
{ "-o", eslARG_OUTFILE,NULL, NULL, NULL, NULL, NULL, "-a", "output a new alignment to file <f>", 2 }, |
| 52 |
{ "--pfam", eslARG_NONE, FALSE, NULL, NULL, NULL, "-o", NULL, "output alignment in Pfam (non-interleaved, 1 line/seq) format", 2 }, |
| 53 |
|
| 54 |
{ "-l", eslARG_OUTFILE,NULL, NULL, NULL, NULL, NULL, NULL, "list seqs w/> 0 indi bp that conflicts w/a SS_cons bp to file <f>", 3}, |
| 55 |
{ "--lmax", eslARG_INT, "0", NULL, "n>=0", NULL, NULL, NULL, "with -l, change maximum allowed conflicts of 0 to <x>", 3}, |
| 56 |
|
| 57 |
{ "--dna", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL,"--rna", "<msafile> contain DNA alignments", 4 }, |
| 58 |
{ "--rna", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL,"--dna", "<msafile> contain RNA alignments", 4 }, |
| 59 |
{ 0,0,0,0,0,0,0,0,0,0 }, |
| 60 |
}; |
| 61 |
|
| 62 |
int |
| 63 |
main(int argc, char **argv) |
| 64 |
{ |
| 65 |
ESL_GETOPTS *go = NULL; |
| 66 |
ESL_ALPHABET *abc = NULL; |
| 67 |
char *alifile = NULL; |
| 68 |
int fmt; |
| 69 |
ESL_MSAFILE *afp = NULL; |
| 70 |
ESL_MSA *msa = NULL; |
| 71 |
int status; |
| 72 |
|
| 73 |
int do_info = TRUE; |
| 74 |
int do_max = FALSE; |
| 75 |
int do_ffreq = FALSE; |
| 76 |
int do_fmin = FALSE; |
| 77 |
float fthresh = 0.; |
| 78 |
int do_remove_bps = FALSE; |
| 79 |
int do_consistent = FALSE; |
| 80 |
int do_indi2cons = FALSE; |
| 81 |
int have_cons; |
| 82 |
int do_newcons = FALSE; |
| 83 |
|
| 84 |
|
| 85 |
int do_a = FALSE; |
| 86 |
char *indi; |
| 87 |
int nindi_read; |
| 88 |
|
| 89 |
int a; |
| 90 |
int i, i2; |
| 91 |
int j, j2; |
| 92 |
int nali; |
| 93 |
int **bp = NULL; |
| 94 |
int *cur_ct = NULL; |
| 95 |
int *cons_ct = NULL; |
| 96 |
int *xcons_ct = NULL; |
| 97 |
int *ngaps = NULL; |
| 98 |
FILE *ofp; |
| 99 |
int be_verbose = FALSE; |
| 100 |
int seqthresh; |
| 101 |
char *sscons = NULL; |
| 102 |
FILE *lfp = NULL; |
| 103 |
int nlist = 0; |
| 104 |
int *nconflictsA; |
| 105 |
int nconflicts_total = 0; |
| 106 |
int nconflicts_list = 0; |
| 107 |
int noverlaps_total = 0; |
| 108 |
int nconsistent_total = 0; |
| 109 |
int nbps_total = 0; |
| 110 |
int *nconsistentA; |
| 111 |
int *noverlapsA; |
| 112 |
int *nbpsA; |
| 113 |
int ncons_bps = 0; |
| 114 |
int max_noverlaps_aidx; |
| 115 |
int max_nconsistent_aidx; |
| 116 |
int max_nbps_aidx; |
| 117 |
int *removebp; |
| 118 |
int *has_conflict; |
| 119 |
int *nmates_l2r; |
| 120 |
int *nmates_r2l; |
| 121 |
|
| 122 |
int lmax; |
| 123 |
int namewidth = 18; |
| 124 |
char *namedashes = NULL; |
| 125 |
|
| 126 |
|
| 127 |
int nbps = 0; |
| 128 |
int prev_nbps = -1; |
| 129 |
float fmin; |
| 130 |
int inconsistent_flag; |
| 131 |
int pknot_flag; |
| 132 |
int k,l; |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
go = esl_getopts_Create(options); |
| 139 |
if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK || |
| 140 |
esl_opt_VerifyConfig(go) != eslOK) |
| 141 |
{ |
| 142 |
printf("Failed to parse command line: %s\n", go->errbuf); |
| 143 |
esl_usage(stdout, argv[0], usage); |
| 144 |
printf("\nTo see more help on available options, do %s -h\n\n", argv[0]); |
| 145 |
exit(1); |
| 146 |
} |
| 147 |
|
| 148 |
if (esl_opt_GetBoolean(go, "-h") ) |
| 149 |
{ |
| 150 |
esl_banner(stdout, argv[0], banner); |
| 151 |
esl_usage (stdout, argv[0], usage); |
| 152 |
puts("\nwhere basic options are:"); |
| 153 |
esl_opt_DisplayHelp(stdout, go, 1, 2, 80); |
| 154 |
puts("\noptions for defining a new consensus structure (all of these require -o):"); |
| 155 |
esl_opt_DisplayHelp(stdout, go, 2, 2, 80); |
| 156 |
puts("\noptions for listing sequences based on structure:"); |
| 157 |
esl_opt_DisplayHelp(stdout, go, 3, 2, 80); |
| 158 |
exit(0); |
| 159 |
} |
| 160 |
|
| 161 |
if (esl_opt_ArgNumber(go) != 1) |
| 162 |
{ |
| 163 |
printf("Incorrect number of command line arguments.\n"); |
| 164 |
esl_usage(stdout, argv[0], usage); |
| 165 |
printf("\nTo see more help on available options, do %s -h\n\n", argv[0]); |
| 166 |
exit(1); |
| 167 |
} |
| 168 |
|
| 169 |
alifile = esl_opt_GetArg(go, 1); |
| 170 |
|
| 171 |
fmt = eslMSAFILE_STOCKHOLM; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
if (esl_opt_GetBoolean(go, "--dna")) abc = esl_alphabet_Create(eslDNA); |
| 178 |
else if (esl_opt_GetBoolean(go, "--rna")) abc = esl_alphabet_Create(eslRNA); |
| 179 |
|
| 180 |
if ( (status = esl_msafile_Open(&abc, alifile, NULL, fmt, NULL, &afp)) != eslOK) |
| 181 |
esl_msafile_OpenFailure(afp, status); |
| 182 |
|
| 183 |
|
| 184 |
if (esl_opt_GetString(go, "-o") != NULL) { |
| 185 |
if ((ofp = fopen(esl_opt_GetString(go, "-o"), "w")) == NULL) |
| 186 |
esl_fatal("Failed to open -o output file %s\n", esl_opt_GetString(go, "-o")); |
| 187 |
} else ofp = NULL; |
| 188 |
if (esl_opt_GetString(go, "-l") != NULL) { |
| 189 |
if ((lfp = fopen(esl_opt_GetString(go, "-l"), "w")) == NULL) |
| 190 |
esl_fatal("Failed to open -l output file %s\n", esl_opt_GetString(go, "-l")); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
do_max = esl_opt_GetBoolean(go, "-x"); |
| 195 |
|
| 196 |
if(!(esl_opt_IsDefault(go, "--ffreq"))) { |
| 197 |
do_ffreq = TRUE; |
| 198 |
fthresh = esl_opt_GetReal(go, "--ffreq"); |
| 199 |
} |
| 200 |
if(!(esl_opt_IsDefault(go, "--fmin"))) { |
| 201 |
do_fmin = TRUE; |
| 202 |
} |
| 203 |
do_remove_bps = esl_opt_GetBoolean(go, "-r"); |
| 204 |
do_consistent = esl_opt_GetBoolean(go, "-c"); |
| 205 |
if(!(esl_opt_IsDefault(go, "--indi"))) { |
| 206 |
do_indi2cons = TRUE; |
| 207 |
} |
| 208 |
if(do_max || do_ffreq || do_fmin || do_remove_bps || do_consistent || do_indi2cons) { |
| 209 |
do_newcons = TRUE; |
| 210 |
} |
| 211 |
do_a = esl_opt_GetBoolean(go, "-a"); |
| 212 |
if(do_a || do_max || do_ffreq || do_fmin || do_remove_bps || do_consistent || do_indi2cons) { |
| 213 |
do_info = FALSE; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
nali = 0; |
| 220 |
have_cons = FALSE; |
| 221 |
lmax = esl_opt_GetInteger(go, "--lmax"); |
| 222 |
if(esl_opt_GetBoolean(go, "-v")) be_verbose = TRUE; |
| 223 |
|
| 224 |
while ((status = esl_msafile_Read(afp, &msa)) != eslEOF) |
| 225 |
{ |
| 226 |
if (status != eslOK) esl_msafile_ReadFailure(afp, status); |
| 227 |
nali++; |
| 228 |
|
| 229 |
|
| 230 |
namewidth = 18; |
| 231 |
for(i = 0; i < msa->nseq; i++) namewidth = ESL_MAX(namewidth, strlen(msa->sqname[i])); |
| 232 |
if(namedashes != NULL) { free(namedashes); } |
| 233 |
ESL_ALLOC(namedashes, sizeof(char) * namewidth+1); |
| 234 |
namedashes[namewidth] = '\0'; |
| 235 |
for(i = 0; i < namewidth; i++) namedashes[i] = '-'; |
| 236 |
|
| 237 |
ESL_ALLOC(sscons, sizeof(char) * (msa->alen+1)); |
| 238 |
ESL_ALLOC(cur_ct, sizeof(int) * (msa->alen+1)); |
| 239 |
ESL_ALLOC(cons_ct, sizeof(int) * (msa->alen+1)); |
| 240 |
ESL_ALLOC(xcons_ct, sizeof(int) * (msa->alen+1)); |
| 241 |
ESL_ALLOC(bp, sizeof(int *) * (msa->alen+1)); |
| 242 |
ESL_ALLOC(removebp, sizeof(int) * (msa->alen+1)); |
| 243 |
ESL_ALLOC(has_conflict, sizeof(int) * (msa->alen+1)); |
| 244 |
ESL_ALLOC(nmates_l2r, sizeof(int) * (msa->alen+1)); |
| 245 |
ESL_ALLOC(nmates_r2l, sizeof(int) * (msa->alen+1)); |
| 246 |
esl_vec_ISet(cur_ct, (msa->alen+1), 0); |
| 247 |
esl_vec_ISet(cons_ct, (msa->alen+1), 0); |
| 248 |
esl_vec_ISet(xcons_ct, (msa->alen+1), 0); |
| 249 |
esl_vec_ISet(removebp, (msa->alen+1), FALSE); |
| 250 |
esl_vec_ISet(has_conflict, (msa->alen+1), FALSE); |
| 251 |
esl_vec_ISet(nmates_l2r, (msa->alen+1), 0); |
| 252 |
esl_vec_ISet(nmates_r2l, (msa->alen+1), 0); |
| 253 |
|
| 254 |
ESL_ALLOC(nconflictsA, sizeof(int) * msa->nseq); |
| 255 |
ESL_ALLOC(noverlapsA, sizeof(int) * msa->nseq); |
| 256 |
ESL_ALLOC(nconsistentA, sizeof(int) * msa->nseq); |
| 257 |
ESL_ALLOC(nbpsA, sizeof(int) * msa->nseq); |
| 258 |
esl_vec_ISet(nconflictsA, msa->nseq, 0); |
| 259 |
esl_vec_ISet(noverlapsA, msa->nseq, 0); |
| 260 |
esl_vec_ISet(nconsistentA, msa->nseq, 0); |
| 261 |
esl_vec_ISet(nbpsA, msa->nseq, 0); |
| 262 |
|
| 263 |
max_noverlaps_aidx = max_nconsistent_aidx = max_nbps_aidx = 0; |
| 264 |
nconsistent_total = nbps_total = noverlaps_total = nconflicts_total = nconflicts_list = 0; |
| 265 |
for(i = 1; i <= msa->alen; i++) { |
| 266 |
ESL_ALLOC(bp[i], sizeof(int) * (msa->alen+1)); |
| 267 |
esl_vec_ISet(bp[i], (msa->alen+1), 0); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
if(msa->ss_cons == NULL && do_remove_bps) esl_fatal("-r requires all alignments have SS_cons annotation, alignment %d does not.", nali); |
| 272 |
if(msa->ss == NULL && do_max) esl_fatal("-x requires all alignments have individual SS annotation, alignment %d does not.", nali); |
| 273 |
if(msa->ss == NULL && do_consistent) esl_fatal("-c requires all alignments have individual SS annotation, alignment %d does not.", nali); |
| 274 |
if(msa->ss == NULL && do_indi2cons) esl_fatal("--indi requires all alignments have individual SS annotation, alignment %d does not.", nali); |
| 275 |
if(msa->ss == NULL && do_ffreq) esl_fatal("--ffreq requires all alignments have individual SS annotation, alignment %d does not.", nali); |
| 276 |
if(msa->ss == NULL && do_fmin) esl_fatal("--fmin requires all alignments have individual SS annotation, alignment %d does not.", nali); |
| 277 |
|
| 278 |
if(msa->ss_cons != NULL) { |
| 279 |
if((status = esl_wuss2ct(msa->ss_cons, msa->alen, xcons_ct)) != eslOK) { |
| 280 |
esl_fatal("Existing SS_cons for alignment %d is invalid.", nali); |
| 281 |
} |
| 282 |
ncons_bps = 0; |
| 283 |
for(i = 1; i <= msa->alen; i++) |
| 284 |
if(xcons_ct[i] != 0 && i < xcons_ct[i]) |
| 285 |
ncons_bps++; |
| 286 |
|
| 287 |
if(nali > 1 && !have_cons) |
| 288 |
esl_fatal("the first aln has SS_cons but aln %d lacks it, if one has it, they all must.", nali); |
| 289 |
if(nali == 1) have_cons = TRUE; |
| 290 |
} |
| 291 |
else if (lfp != NULL) { |
| 292 |
esl_fatal("the -l option requires existing SS_cons annotation, aln %d lacks it.", nali); |
| 293 |
} |
| 294 |
else if (do_remove_bps) { |
| 295 |
esl_fatal("the -r option requires existing SS_cons annotation, aln %d lacks it.", nali); |
| 296 |
} |
| 297 |
else if (do_consistent) { |
| 298 |
esl_fatal("the -c option requires existing SS_cons annotation, aln %d lacks it.", nali); |
| 299 |
} |
| 300 |
else { |
| 301 |
if(nali > 1 && have_cons) |
| 302 |
esl_fatal("the first aln does not have SS_cons but aln %d does, if one has it, they all must.", nali); |
| 303 |
} |
| 304 |
|
| 305 |
if(do_info) { |
| 306 |
printf("# Per-sequence basepair information:\n"); |
| 307 |
printf("# Alignment file: %s\n", alifile); |
| 308 |
printf("# Alignment idx: %d\n", nali); |
| 309 |
if(msa->name != NULL) { printf("# Alignment name: %s\n", msa->name); } |
| 310 |
if(have_cons) { |
| 311 |
printf("#\n"); |
| 312 |
printf("# indibp: number of basepairs in the individual sequence SS annotation\n"); |
| 313 |
printf("# ovrlap: number of indibp basepairs that also exist as consensus basepairs\n"); |
| 314 |
printf("# cnsist: number of indibp basepairs that do not conflict with any consensus basepairs\n"); |
| 315 |
printf("# cnflct: number of indibp basepairs that conflict with >= 1 consensus basepairs\n"); |
| 316 |
printf("#\n"); |
| 317 |
printf("# A conflict exists between two basepairs in different structures, one between columns i and j\n"); |
| 318 |
printf("# and the other between columns k and l, if (i == k and j != l) or (j == l and i != k).\n"); |
| 319 |
printf("#\n"); |
| 320 |
printf("# %-*s %6s %6s %6s %6s\n", namewidth, "seqname", "indibp", "ovrlap", "cnsist", "cnflct"); |
| 321 |
printf("# %-*s %6s %6s %6s %6s\n", namewidth, namedashes, "------", "------", "-----", "------"); |
| 322 |
} |
| 323 |
else { |
| 324 |
printf("# %-*s %6s\n", namewidth, "seqname", "nbp"); |
| 325 |
printf("# %-*s %6s\n", namewidth, namedashes, "------"); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
nindi_read = 0; |
| 330 |
for (a = 0; a < msa->nseq; a++) { |
| 331 |
if(msa->ss != NULL && msa->ss[a] != NULL) { |
| 332 |
if((status = esl_wuss2ct(msa->ss[a], msa->alen, cur_ct)) != eslOK) { |
| 333 |
esl_fatal("SS annotation for sequence %d, aln %d is invalid.\n", (a+1), nali); |
| 334 |
} |
| 335 |
nindi_read++; |
| 336 |
for(i = 1; i <= msa->alen; i++) { |
| 337 |
if(i < cur_ct[i]) { |
| 338 |
bp[i][cur_ct[i]]++; |
| 339 |
if(bp[i][cur_ct[i]] == 1) { |
| 340 |
nmates_l2r[i]++; |
| 341 |
nmates_r2l[cur_ct[i]]++; |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
for(i = 1; i <= msa->alen; i++) { |
| 347 |
if(cur_ct[i] != 0 && i < cur_ct[i]) { |
| 348 |
if(xcons_ct[i] == cur_ct[i]) noverlapsA[a]++; |
| 349 |
if((xcons_ct[i] != 0) && (xcons_ct[i] != cur_ct[i])) { |
| 350 |
if(be_verbose) { printf("ali: %2d seq %3d (%s) bp %4d:%4d conflicts with consensus bp %4d:%4d\n", nali, a, msa->sqname[a], i, cur_ct[i], i, xcons_ct[i]); } |
| 351 |
nconflictsA[a]++; |
| 352 |
|
| 353 |
removebp[i] = TRUE; |
| 354 |
removebp[xcons_ct[i]] = TRUE; |
| 355 |
} |
| 356 |
else if((xcons_ct[cur_ct[i]] != 0) && (xcons_ct[cur_ct[i]] != i) && (cur_ct[xcons_ct[cur_ct[i]]] == 0)) { |
| 357 |
if(be_verbose) { printf("ali: %2d seq %3d (%s) bp %4d:%4d conflicts with consensus bp %4d:%4d\n", nali, a, msa->sqname[a], xcons_ct[i], cur_ct[xcons_ct[i]], xcons_ct[cur_ct[i]], cur_ct[i]); } |
| 358 |
nconflictsA[a]++; |
| 359 |
|
| 360 |
removebp[cur_ct[i]] = TRUE; |
| 361 |
removebp[xcons_ct[cur_ct[i]]] = TRUE; |
| 362 |
} |
| 363 |
else nconsistentA[a]++; |
| 364 |
} |
| 365 |
} |
| 366 |
if(nconflictsA[a] > lmax) { |
| 367 |
if(lfp != NULL) fprintf(lfp, "%s\n", msa->sqname[a]); |
| 368 |
nconflicts_list += nconflictsA[a]; |
| 369 |
nlist++; |
| 370 |
} |
| 371 |
nbpsA[a] = nconflictsA[a] + nconsistentA[a]; |
| 372 |
nconflicts_total += nconflictsA[a]; |
| 373 |
nconsistent_total += nconsistentA[a]; |
| 374 |
noverlaps_total += noverlapsA[a]; |
| 375 |
nbps_total += nbpsA[a]; |
| 376 |
|
| 377 |
if(do_info && have_cons) printf(" %-*s %6d %6d %6d %6d\n", namewidth, msa->sqname[a], nbpsA[a], noverlapsA[a], nconsistentA[a], nconflictsA[a]); |
| 378 |
if(do_info && !have_cons) printf(" %-*s %6d\n", namewidth, msa->sqname[a], nbpsA[a]); |
| 379 |
if(nbpsA[a] > nbpsA[max_nbps_aidx]) max_nbps_aidx = a; |
| 380 |
if((noverlapsA[a] > noverlapsA[max_noverlaps_aidx]) || ((noverlapsA[a] == noverlapsA[max_noverlaps_aidx]) && (nbpsA[a] > nbpsA[max_noverlaps_aidx]))) max_noverlaps_aidx = a; |
| 381 |
if((nconsistentA[a] > nconsistentA[max_nconsistent_aidx]) || ((nconsistentA[a] == nconsistentA[max_nconsistent_aidx]) && (nbpsA[a] > nbpsA[max_nconsistent_aidx]))) max_nconsistent_aidx = a; |
| 382 |
} |
| 383 |
else if(do_newcons || esl_opt_GetBoolean(go, "-a")) { esl_fatal("No SS annotation for sequence %d, aln %d.\n", (a+1), nali); } |
| 384 |
} |
| 385 |
|
| 386 |
if(do_info && have_cons) { |
| 387 |
if(nindi_read > 0) printf("\n"); |
| 388 |
printf(" %-*s %6d %6d %6d %6d\n", namewidth, "SS_cons(consensus)", ncons_bps, ncons_bps, ncons_bps, 0); |
| 389 |
if(nindi_read > 0) { |
| 390 |
printf("\n# %6d/%6d (%.3f) overlap\n", noverlaps_total, nbps_total, nbps_total > 0 ? (float) noverlaps_total / (float) nbps_total : 0.); |
| 391 |
printf("# %6d/%6d (%.3f) consistent\n", nconsistent_total, nbps_total, nbps_total > 0 ? (float) nconsistent_total / (float) nbps_total: 0.); |
| 392 |
printf("# %6d/%6d (%.3f) conflict\n", nconflicts_total, nbps_total, nbps_total > 0 ? (float) nconflicts_total / (float) nbps_total: 0.); |
| 393 |
} |
| 394 |
else { |
| 395 |
printf("# No sequences in the alignment have GR SS annotation.\n"); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
if(lfp != NULL) { |
| 400 |
printf("# %d/%d sequences with %.3f individual bps on avg that conflict with SS_cons written to %s\n", nlist, msa->nseq, (float) nconflicts_list / (float) nlist, esl_opt_GetString(go, "-l")); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
if((status = get_gaps_per_column(msa, &ngaps)) != eslOK) goto ERROR; |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
if(do_max || do_a) { |
| 409 |
for(i = 1; i <= msa->alen; i++) { |
| 410 |
if(nmates_l2r[i] > 1) { |
| 411 |
has_conflict[i] = TRUE; |
| 412 |
for(j = 1; j <= msa->alen; j++) { |
| 413 |
if(bp[i][j] > 0) { |
| 414 |
if(do_a) printf("More than 1 right mates for left mate %4d %4d:%4d bp exists in %4d/%4d seqs (%.3f)\n", i, i, j, bp[i][j], msa->nseq - ngaps[i], (float) bp[i][j] / (float) (msa->nseq - ngaps[i])); |
| 415 |
has_conflict[j] = TRUE; |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
for(i = 1; i <= msa->alen; i++) { |
| 421 |
if(nmates_r2l[i] > 1) { |
| 422 |
has_conflict[i] = TRUE; |
| 423 |
for(j = 1; j <= msa->alen; j++) { |
| 424 |
if(bp[j][i] > 0) { |
| 425 |
if(do_a) printf("More than 1 left mates for right mate %4d %4d:%4d bp exists in %4d/%4d seqs (%.3f)\n", i, j, i, bp[j][i], msa->nseq - ngaps[i], (float) bp[j][i] / (float) (msa->nseq - ngaps[i])); |
| 426 |
has_conflict[j] = TRUE; |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
for(i = 1; i <= msa->alen; i++) { |
| 432 |
|
| 433 |
if(nmates_l2r[i] == 1 && (!(has_conflict[i]))) { |
| 434 |
j = i+1; |
| 435 |
while(bp[i][j] == 0) j++; |
| 436 |
cons_ct[i] = j; |
| 437 |
cons_ct[j] = i; |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
for(i = 1; i <= msa->alen; i++) { |
| 443 |
j = cons_ct[i]; |
| 444 |
if(j != 0 && i < j) { |
| 445 |
for(i2 = i+1; i2 <= msa->alen; i2++) { |
| 446 |
j2 = cons_ct[i2]; |
| 447 |
if(j2 != 0 && i2 < j2) { |
| 448 |
if((i2 < j) && (j < j2)) { |
| 449 |
|
| 450 |
|
| 451 |
if(bp[i][j] <= bp[i2][j2]) { |
| 452 |
|
| 453 |
cons_ct[cons_ct[i]] = 0; |
| 454 |
cons_ct[i] = 0; |
| 455 |
} |
| 456 |
if(bp[i][j] >= bp[i2][j2]) { |
| 457 |
|
| 458 |
cons_ct[cons_ct[i2]] = 0; |
| 459 |
cons_ct[i2] = 0; |
| 460 |
} |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
if((status = esl_msa_Textize(msa)) != eslOK) esl_fatal("ERROR textizing alignment %d\n", nali); |
| 508 |
|
| 509 |
|
| 510 |
if(do_fmin) { |
| 511 |
|
| 512 |
prev_nbps = -1; |
| 513 |
fthresh = 0.99; |
| 514 |
inconsistent_flag = pknot_flag = FALSE; |
| 515 |
printf("# Defining consensus structure:\n"); |
| 516 |
printf("# indi SS basepair aln columns i:j (from at least 1 indi SS) will become consensus basepair\n"); |
| 517 |
printf("# if > <x> individual SS contain i:j as a pair\n"); |
| 518 |
printf("# We'll search for minimal <x> that gives a consistent consensus structure.\n"); |
| 519 |
printf("# A consistent structure has each position involved in 0 or 1 basepairs.\n"); |
| 520 |
printf("#\n"); |
| 521 |
printf("# Alignment file: %s\n", alifile); |
| 522 |
printf("# Alignment idx: %d\n", nali); |
| 523 |
printf("# Number of seqs: %d\n", msa->nseq); |
| 524 |
printf("#\n"); |
| 525 |
printf("# %5s %23s %6s\n", "<x>", "nseq-required-with-bp", "numbps"); |
| 526 |
printf("# %5s %23s %6s\n", "-----", "-----------------------", "------"); |
| 527 |
while(fthresh >= 0.00 && (inconsistent_flag == FALSE) && (pknot_flag == FALSE)) { |
| 528 |
nbps = 0; |
| 529 |
seqthresh = (int) (fthresh * msa->nseq); |
| 530 |
|
| 531 |
esl_vec_ISet(cons_ct, msa->alen+1, 0); |
| 532 |
for(i = 1; i <= msa->alen; i++) { |
| 533 |
for(j = i+1; j <= msa->alen; j++) { |
| 534 |
if(bp[i][j] > seqthresh) { |
| 535 |
if(cons_ct[i] != 0 || cons_ct[j] != 0) { |
| 536 |
inconsistent_flag = TRUE; |
| 537 |
} |
| 538 |
|
| 539 |
for(k = i+1; k < j; k++) { |
| 540 |
l = cons_ct[k]; |
| 541 |
if((k < l) && (l > j)) { |
| 542 |
pknot_flag = TRUE; |
| 543 |
} |
| 544 |
if((k > l) && (l != 0) && (l < i)) { |
| 545 |
pknot_flag = TRUE; |
| 546 |
} |
| 547 |
} |
| 548 |
cons_ct[i] = j; |
| 549 |
cons_ct[j] = i; |
| 550 |
nbps++; |
| 551 |
} |
| 552 |
} |
| 553 |
} |
| 554 |
if(inconsistent_flag) |
| 555 |
printf(" %.3f %23d %s\n", fthresh, seqthresh+1, "inconsistent"); |
| 556 |
else if(pknot_flag) |
| 557 |
printf(" %.3f %23d %s\n", fthresh, seqthresh+1, "pseudoknotted"); |
| 558 |
else { |
| 559 |
if(nbps != prev_nbps) { |
| 560 |
printf(" %.3f %23d %6d\n", fthresh, seqthresh+1, nbps); |
| 561 |
} |
| 562 |
fmin = fthresh; |
| 563 |
} |
| 564 |
fthresh -= 0.01; |
| 565 |
prev_nbps = nbps; |
| 566 |
} |
| 567 |
fthresh = fmin; |
| 568 |
esl_vec_ISet(cons_ct, msa->alen+1, 0); |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
if(do_ffreq || do_fmin) { |
| 573 |
if(do_fmin) { printf("#\n# <x> determined to be %.3f\n", fthresh); } |
| 574 |
if(do_ffreq) { |
| 575 |
printf("# Defining consensus structure:\n"); |
| 576 |
printf("# indi SS basepair aln columns i:j (from at least 1 indi SS) will become consensus basepair\n"); |
| 577 |
printf("# if > %f individual SS contain i:j as a pair\n", fthresh); |
| 578 |
} |
| 579 |
esl_vec_ISet(cons_ct, msa->alen+1, 0); |
| 580 |
|
| 581 |
seqthresh = (int) (fthresh * msa->nseq); |
| 582 |
|
| 583 |
for(i = 1; i <= msa->alen; i++) { |
| 584 |
for(j = i+1; j <= msa->alen; j++) { |
| 585 |
if(bp[i][j] > seqthresh) { |
| 586 |
if(cons_ct[i] != 0) { |
| 587 |
esl_fatal("ERROR, two base pairs including position %d satisfy threshold (%d:%d and %d:%d)!\n", i, i, cons_ct[i], i, j); |
| 588 |
} |
| 589 |
if(cons_ct[j] != 0) { |
| 590 |
esl_fatal("ERROR, two base pairs including position %d satisfy threshold (%d:%d and %d:%d)!\n", j, j, cons_ct[j], i, j); |
| 591 |
} |
| 592 |
cons_ct[i] = j; |
| 593 |
cons_ct[j] = i; |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
if(do_remove_bps) { |
| 601 |
for(i = 1; i <= msa->alen; i++) { |
| 602 |
if(!(removebp[i])) { |
| 603 |
cons_ct[i] = xcons_ct[i]; |
| 604 |
cons_ct[cons_ct[i]] = i; |
| 605 |
} |
| 606 |
else { |
| 607 |
printf("# Removing consensus bp: %d:%d\n", i, xcons_ct[i]); |
| 608 |
cons_ct[xcons_ct[i]] = 0; |
| 609 |
cons_ct[i] = 0; |
| 610 |
} |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
if(do_consistent || do_indi2cons) { |
| 617 |
if(do_indi2cons) { |
| 618 |
indi = esl_opt_GetString(go, "--indi"); |
| 619 |
for(a = 0; a < msa->nseq; a++) { |
| 620 |
if(strcmp(indi, msa->sqname[a]) == 0) break; |
| 621 |
} |
| 622 |
if(a == msa->nseq) esl_fatal("ERROR, could not find a sequence named %s in the alignment.\n", indi); |
| 623 |
} |
| 624 |
else { |
| 625 |
a = max_nconsistent_aidx; |
| 626 |
} |
| 627 |
if(msa->ss == NULL || msa->ss[a] == NULL) esl_fatal("ERROR, no individual SS annotation for %s in the alignment.\n", msa->sqname[a]); |
| 628 |
if((status = esl_wuss2ct(msa->ss[a], msa->alen, cons_ct)) != eslOK) { |
| 629 |
esl_fatal("Second pass... SS annotation for sequence %d, aln %d is invalid.\n", (a), nali); |
| 630 |
} |
| 631 |
printf("# Defined new SS_cons as SS annotation for %s (%d basepairs)\n", msa->sqname[a], nbpsA[a]); |
| 632 |
if(esl_opt_GetBoolean(go, "--rfc") || esl_opt_GetBoolean(go, "--rfindi")) { |
| 633 |
if(msa->rf != NULL) { free(msa->rf); msa->rf = NULL; } |
| 634 |
if((status = esl_strcat(&(msa->rf), -1, msa->aseq[a], msa->alen)) != eslOK) goto ERROR; |
| 635 |
printf("# Defined new RF as %s sequence\n", msa->sqname[a]); |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
if(do_newcons) { |
| 641 |
if((status = esl_ct2wuss(cons_ct, msa->alen, sscons)) != eslOK) goto ERROR; |
| 642 |
if(msa->ss_cons != NULL) { free(msa->ss_cons); msa->ss_cons = NULL; } |
| 643 |
if((status = esl_strcat(&(msa->ss_cons), -1, sscons, msa->alen)) != eslOK) goto ERROR; |
| 644 |
status = esl_msafile_Write(ofp, msa, (esl_opt_GetBoolean(go, "--pfam") ? eslMSAFILE_PFAM : eslMSAFILE_STOCKHOLM)); |
| 645 |
if (status == eslEMEM) esl_fatal("Memory error when outputting alignment\n"); |
| 646 |
else if (status != eslOK) esl_fatal("Writing alignment file failed with error %d\n", status); |
| 647 |
} |
| 648 |
|
| 649 |
free(sscons); |
| 650 |
free(cur_ct); |
| 651 |
free(cons_ct); |
| 652 |
free(xcons_ct); |
| 653 |
for(i = 1; i <= msa->alen; i++) free(bp[i]); |
| 654 |
free(bp); |
| 655 |
esl_msa_Destroy(msa); |
| 656 |
} |
| 657 |
if (nali == 0) esl_fatal("No alignments found in file %s\n", alifile); |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
if(lfp != NULL) fclose(lfp); |
| 662 |
if(ofp != NULL) { |
| 663 |
printf("# Alignment(s) saved to file %s\n", esl_opt_GetString(go, "-o")); |
| 664 |
fclose(ofp); |
| 665 |
} |
| 666 |
esl_msafile_Close(afp); |
| 667 |
esl_getopts_Destroy(go); |
| 668 |
free(has_conflict); |
| 669 |
free(nmates_r2l); |
| 670 |
free(nmates_l2r); |
| 671 |
free(removebp); |
| 672 |
free(nconsistentA); |
| 673 |
free(nbpsA); |
| 674 |
free(noverlapsA); |
| 675 |
free(nconflictsA); |
| 676 |
free(namedashes); |
| 677 |
free(ngaps); |
| 678 |
return 0; |
| 679 |
|
| 680 |
ERROR: |
| 681 |
if(afp) esl_msafile_Close(afp); |
| 682 |
if(go) esl_getopts_Destroy(go); |
| 683 |
if(msa) esl_msa_Destroy(msa); |
| 684 |
if(lfp) fclose(lfp); |
| 685 |
if(ofp) fclose(ofp); |
| 686 |
esl_fatal("ERROR\n"); |
| 687 |
return 1; |
| 688 |
|
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
static int get_gaps_per_column(ESL_MSA *msa, int **ret_ngaps) |
| 699 |
{ |
| 700 |
int status; |
| 701 |
int i, apos; |
| 702 |
int *ngaps = NULL; |
| 703 |
|
| 704 |
if(! (msa->flags & eslMSA_DIGITAL)) { status = eslEINVAL; goto ERROR; } |
| 705 |
|
| 706 |
ESL_ALLOC(ngaps, sizeof(int) * (msa->alen+1)); |
| 707 |
esl_vec_ISet(ngaps, msa->alen+1, 0); |
| 708 |
for(i = 0; i < msa->nseq; i++) { |
| 709 |
for(apos = 1; apos <= msa->alen; apos++) |
| 710 |
ngaps[apos] += esl_abc_XIsGap(msa->abc, msa->ax[i][apos]); |
| 711 |
} |
| 712 |
*ret_ngaps = ngaps; |
| 713 |
return eslOK; |
| 714 |
|
| 715 |
ERROR: |
| 716 |
if(ngaps != NULL) free(ngaps); |
| 717 |
return status; |
| 718 |
} |