| 1 |
|
| 2 |
|
| 3 |
#include <esl_config.h> |
| 4 |
|
| 5 |
#include <stdlib.h> |
| 6 |
#include <stdio.h> |
| 7 |
|
| 8 |
#include "easel.h" |
| 9 |
#include "esl_alphabet.h" |
| 10 |
#include "esl_arr2.h" |
| 11 |
#include "esl_fileparser.h" |
| 12 |
#include "esl_getopts.h" |
| 13 |
#include "esl_msa.h" |
| 14 |
#include "esl_msafile.h" |
| 15 |
#include "esl_msafile2.h" |
| 16 |
#include "esl_regexp.h" |
| 17 |
#include "esl_stopwatch.h" |
| 18 |
#include "esl_vectorops.h" |
| 19 |
|
| 20 |
static char banner[] = "remove columns from a multiple sequence alignment"; |
| 21 |
static char usage1[] = "[options] <msafile> <maskfile> (use mask from maskfile)"; |
| 22 |
static char usage2[] = "[options] -t <msafile> <coords> (truncate alignment to coords)"; |
| 23 |
static char usage3[] = "[options] -g <msafile> (use gap frequencies in aln)"; |
| 24 |
static char usage4[] = "[options] -p <msafile> (use post probs (PP) in aln)"; |
| 25 |
static char usage5[] = "[options] --rf-is-mask <msafile> (use #=GC RF in aln as mask)"; |
| 26 |
|
| 27 |
static int read_mask_file(char *filename, char *errbuf, int **ret_useme, int *ret_mlen); |
| 28 |
static int map_rfpos_to_apos(ESL_MSA *msa, ESL_ALPHABET *abc, char *errbuf, int **i_am_rf, int **ret_rf2a_map, int *ret_rflen); |
| 29 |
static int expand_rf_useme_to_alen(int *useme_rf, int *rf2a_map, int rflen, int alen, char *errbuf, int *useme_a); |
| 30 |
static int count_gaps_in_msa(ESL_MSA *msa, ESL_ALPHABET *abc, int *countme, char *errbuf, double **ret_gap_ct); |
| 31 |
static int count_postprobs_in_msa(ESL_MSA *msa, ESL_ALPHABET *abc, int *countme, char *errbuf, double ***ret_pp_ct); |
| 32 |
static int mask_based_on_gapfreq(double *gap_ct, int64_t alen, int nseq, float gapthresh, int *i_am_eligible, char *errbuf, int **ret_useme); |
| 33 |
static int get_pp_idx(ESL_ALPHABET *abc, char ppchar); |
| 34 |
static int mask_based_on_postprobs(double **pp_ct, int64_t alen, int nseq, float pthresh, float pfract, int do_pavg, float pavg_min, int do_ppcons, float ppcons_min, char *pp_cons, ESL_ALPHABET *abc, int *i_am_eligible, int allgapok, char *errbuf, int **ret_useme); |
| 35 |
static int output_mask(char *filename, int *useme, int *i_am_eligible, int64_t alen, char *errbuf); |
| 36 |
static int determine_nkept_rf(int *useme, int *i_am_rf, int64_t len); |
| 37 |
static int parse_coord_string(const char *cstring, uint32_t *ret_start, uint32_t *ret_end); |
| 38 |
|
| 39 |
static ESL_OPTIONS options[] = { |
| 40 |
|
| 41 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "help; show brief info on version and usage", 1 }, |
| 42 |
{ "-o", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "output the final alignment to file <f>, not stdout", 1 }, |
| 43 |
{ "-q", eslARG_NONE, FALSE, NULL, NULL, NULL, "-o", NULL, "be quiet; w/-o, don't print mask info to stdout", 1 }, |
| 44 |
{ "--small", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "use minimal RAM (RAM usage will be independent of aln size)", 1 }, |
| 45 |
{ "--informat", eslARG_STRING, FALSE, NULL, NULL, NULL, NULL, NULL, "specify that input file is in format <s>", 1 }, |
| 46 |
{ "--outformat", eslARG_STRING, FALSE, NULL, NULL, NULL, NULL, NULL, "specify that output aln be format <s>", 1 }, |
| 47 |
{ "--fmask-rf", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "output final mask of non-gap RF len to file <f>", 1 }, |
| 48 |
{ "--fmask-all", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "output final mask of full aln len to file <f>", 1 }, |
| 49 |
|
| 50 |
{ "--amino", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--dna,--rna", "<msafile> contains protein alignments", 2 }, |
| 51 |
{ "--dna", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--amino,--rna","<msafile> contains DNA alignments", 2 }, |
| 52 |
{ "--rna", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--amino,--dna","<msafile> contains RNA alignments", 2 }, |
| 53 |
|
| 54 |
{ "--t-rf", eslARG_NONE, NULL, NULL, NULL, NULL, "-t", NULL, "<coords> string corresponds to non-gap RF positions", 3 }, |
| 55 |
{ "--t-rmins", eslARG_NONE, NULL, NULL, NULL, NULL, "-t", NULL, "remove all gap RF positions within <coords>", 3 }, |
| 56 |
|
| 57 |
{ "--gapthresh", eslARG_REAL, "0.5", NULL, "0<=x<=1", NULL, "-g", NULL, "only keep columns with <= <x> fraction of gaps in them", 4 }, |
| 58 |
{ "--gmask-rf", eslARG_OUTFILE, NULL, NULL, NULL, NULL, "-g", NULL, "output gap-based 0/1 mask of non-gap RF len to file <f>", 4 }, |
| 59 |
{ "--gmask-all", eslARG_OUTFILE, NULL, NULL, NULL, NULL, "-g", NULL, "output gap-based 0/1 mask of full aln len to file <f>", 4 }, |
| 60 |
|
| 61 |
{ "--pfract", eslARG_REAL, "0.95", NULL, "0<=x<=1", NULL, "-p", NULL, "keep cols w/<x> fraction of seqs w/PP >= --pthresh", 5 }, |
| 62 |
{ "--pthresh", eslARG_REAL, "0.95", NULL, "0<=x<=1", NULL, "-p", NULL, "set post prob threshold for --pfract as <x>", 5 }, |
| 63 |
{ "--pavg", eslARG_REAL, NULL, NULL, "0<=x<=1", NULL, "-p", "--pfract,--pthresh", "keep cols with average post prob >= <x>", 5 }, |
| 64 |
{ "--ppcons", eslARG_REAL, NULL, NULL, "0<=x<=1", NULL, "-p", "--keepins,--pavg,--pfract,--pthresh", "keep cols with PP_cons value >= <x>", 5 }, |
| 65 |
{ "--pallgapok", eslARG_NONE, NULL, NULL, FALSE, NULL, "-p", NULL, "keep 100% gap columns (by default, they're removed w/-p)", 5 }, |
| 66 |
{ "--pmask-rf", eslARG_OUTFILE, NULL, NULL, NULL, NULL, "-p", NULL, "output PP-based 0/1 mask of non-gap RF len to file <f>", 5 }, |
| 67 |
{ "--pmask-all", eslARG_OUTFILE, NULL, NULL, NULL, NULL, "-p", NULL, "output PP-based 0/1 mask of full aln len to file <f>", 5 }, |
| 68 |
|
| 69 |
{ "--keepins", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "if msa has RF annotation, allow gap-RF columns to possibly survive",6 }, |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
{ "-t", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-g,-p,--rf-is-mask", "", 99 }, |
| 74 |
{ "-g", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "mask based on gap frequency in the alignment", 99 }, |
| 75 |
{ "-p", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "mask based on posterior probability annotation in the alignment", 99 }, |
| 76 |
{ "--rf-is-mask", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-t,-g,-p,--keepins","remove a column if and only if it is a gap in the RF annotation", 99 }, |
| 77 |
{ 0,0,0,0,0,0,0,0,0,0 }, |
| 78 |
}; |
| 79 |
|
| 80 |
int |
| 81 |
main(int argc, char **argv) |
| 82 |
{ |
| 83 |
ESL_GETOPTS *go = NULL; |
| 84 |
ESL_ALPHABET *abc = NULL; |
| 85 |
char *alifile = NULL; |
| 86 |
int infmt = eslMSAFILE_UNKNOWN; |
| 87 |
int outfmt = eslMSAFILE_UNKNOWN; |
| 88 |
ESL_MSAFILE *afp = NULL; |
| 89 |
ESL_MSAFILE2 *afp2 = NULL; |
| 90 |
FILE *ofp; |
| 91 |
ESL_MSA *msa = NULL; |
| 92 |
int status; |
| 93 |
int do_rfonly = FALSE; |
| 94 |
int nseq = 0; |
| 95 |
char errbuf[eslERRBUFSIZE]; |
| 96 |
int rflen = 0; |
| 97 |
int nkept = 0; |
| 98 |
int nkept_rf = 0; |
| 99 |
int be_verbose = FALSE; |
| 100 |
int apos = 0; |
| 101 |
int *i_am_rf = NULL; |
| 102 |
int *i_am_eligible = NULL; |
| 103 |
|
| 104 |
int *rf2a_map = NULL; |
| 105 |
|
| 106 |
|
| 107 |
int *useme_final = NULL; |
| 108 |
ESL_STOPWATCH *w = NULL; |
| 109 |
int64_t orig_alen = 0; |
| 110 |
|
| 111 |
|
| 112 |
char *maskfile = NULL; |
| 113 |
int do_maskfile; |
| 114 |
int file_mlen = 0; |
| 115 |
double **pp_ct = NULL; |
| 116 |
int *useme_mfile = NULL; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
int do_truncate; |
| 121 |
uint32_t tstart, tend; |
| 122 |
|
| 123 |
|
| 124 |
int do_gapthresh; |
| 125 |
double **abc_ct = NULL; |
| 126 |
double *gap_ct = NULL; |
| 127 |
int *useme_g = NULL; |
| 128 |
|
| 129 |
|
| 130 |
int do_postprob; |
| 131 |
int do_pavg; |
| 132 |
int do_ppcons; |
| 133 |
float pavg_min; |
| 134 |
float ppcons_min; |
| 135 |
int *useme_pp = NULL; |
| 136 |
|
| 137 |
|
| 138 |
int do_rf_is_mask = FALSE; |
| 139 |
|
| 140 |
|
| 141 |
int do_small; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
go = esl_getopts_Create(options); |
| 148 |
if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK || |
| 149 |
esl_opt_VerifyConfig(go) != eslOK) |
| 150 |
{ |
| 151 |
printf("Failed to parse command line: %s\n", go->errbuf); |
| 152 |
esl_usage(stdout, argv[0], usage1); |
| 153 |
esl_usage(stdout, argv[0], usage2); |
| 154 |
esl_usage(stdout, argv[0], usage3); |
| 155 |
esl_usage(stdout, argv[0], usage4); |
| 156 |
esl_usage(stdout, argv[0], usage5); |
| 157 |
printf("\nTo see more help on available options, do %s -h\n\n", argv[0]); |
| 158 |
exit(1); |
| 159 |
} |
| 160 |
|
| 161 |
if (esl_opt_GetBoolean(go, "-h") ) |
| 162 |
{ |
| 163 |
esl_banner(stdout, argv[0], banner); |
| 164 |
esl_usage (stdout, argv[0], usage1); |
| 165 |
esl_usage (stdout, argv[0], usage2); |
| 166 |
esl_usage (stdout, argv[0], usage3); |
| 167 |
esl_usage (stdout, argv[0], usage4); |
| 168 |
esl_usage (stdout, argv[0], usage5); |
| 169 |
puts("\n Only one usage listed above can be used per execution with the exception of"); |
| 170 |
puts(" -p and -g, which can be used in combination with each other.\n"); |
| 171 |
puts(" With -t, <coords> is a string that specifies positive integer start and end"); |
| 172 |
puts(" coordinates seperated by any nonnumeric, nonspace character(s)."); |
| 173 |
puts(" For example, \"23..100\" or \"23/100\" or \"23-100\" all specify that columns"); |
| 174 |
puts(" 23 to 100 be kept and all other columns be removed. Additionally, to retrieve"); |
| 175 |
puts(" all columns from a position to the end, omit the end coord; \"23:\" will work."); |
| 176 |
puts(" With -t and --t-rf, coordinates are interpreted as non-gap positions in the"); |
| 177 |
puts(" reference (\"#=GC RF\") annotation.\n"); |
| 178 |
puts(" With --rf-is-mask, all columns that are not gaps in the reference annotation"); |
| 179 |
puts(" will be kept and all other columns will be removed."); |
| 180 |
puts("\n other options are:"); |
| 181 |
esl_opt_DisplayHelp(stdout, go, 1, 2, 80); |
| 182 |
puts("\n options for specifying alphabet in <msafile>, one is required w/--small:"); |
| 183 |
esl_opt_DisplayHelp(stdout, go, 2, 2, 80); |
| 184 |
puts("\n options related to truncating the alignment (require -t):"); |
| 185 |
esl_opt_DisplayHelp(stdout, go, 3, 2, 80); |
| 186 |
puts("\n options for masking based on gap frequencies (require -g):"); |
| 187 |
esl_opt_DisplayHelp(stdout, go, 4, 2, 80); |
| 188 |
puts("\n options for masking based on posterior probabilities (require -p):"); |
| 189 |
esl_opt_DisplayHelp(stdout, go, 5, 2, 80); |
| 190 |
puts("\n options that modify masking behavior when -g and/or -p are used:"); |
| 191 |
esl_opt_DisplayHelp(stdout, go, 6, 2, 80); |
| 192 |
exit(0); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
if ((! (((! esl_opt_GetBoolean(go, "--rf-is-mask")) && (! esl_opt_GetBoolean(go, "-p")) && (! esl_opt_GetBoolean(go, "-g"))) && esl_opt_ArgNumber(go) == 2)) && |
| 203 |
(! ((esl_opt_GetBoolean(go, "-g")) && (esl_opt_ArgNumber(go) == 1))) && |
| 204 |
(! ((esl_opt_GetBoolean(go, "-p")) && (esl_opt_ArgNumber(go) == 1))) && |
| 205 |
(! ((esl_opt_GetBoolean(go, "--rf-is-mask")) && (esl_opt_ArgNumber(go) == 1)))) |
| 206 |
{ |
| 207 |
printf("Incorrect number of command line arguments.\n"); |
| 208 |
esl_usage(stdout, argv[0], usage1); |
| 209 |
esl_usage(stdout, argv[0], usage2); |
| 210 |
esl_usage(stdout, argv[0], usage3); |
| 211 |
esl_usage(stdout, argv[0], usage4); |
| 212 |
esl_usage(stdout, argv[0], usage5); |
| 213 |
printf("\nTo see more help on available options, do %s -h\n\n", argv[0]); |
| 214 |
exit(1); |
| 215 |
} |
| 216 |
else |
| 217 |
|
| 218 |
alifile = esl_opt_GetArg(go, 1); |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
do_maskfile = do_truncate = do_postprob = do_gapthresh = do_rf_is_mask = FALSE; |
| 234 |
if(esl_opt_ArgNumber(go) == 2) { |
| 235 |
if(esl_opt_GetBoolean(go, "-t")) { |
| 236 |
do_truncate = TRUE; |
| 237 |
|
| 238 |
} |
| 239 |
else { |
| 240 |
do_maskfile = TRUE; |
| 241 |
maskfile = esl_opt_GetArg(go, 2); |
| 242 |
} |
| 243 |
} |
| 244 |
else { |
| 245 |
do_rf_is_mask = esl_opt_GetBoolean(go, "--rf-is-mask"); |
| 246 |
do_postprob = esl_opt_GetBoolean(go, "-p"); |
| 247 |
do_gapthresh = esl_opt_GetBoolean(go, "-g"); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
if(esl_opt_GetBoolean(go, "--keepins") && (! do_postprob) && (! do_gapthresh)) |
| 253 |
esl_fatal("--keepins only makes sense in combination with -p and/or -g"); |
| 254 |
|
| 255 |
|
| 256 |
be_verbose = (esl_opt_IsOn(go, "-o") && (! esl_opt_GetBoolean(go, "-q"))) ? TRUE : FALSE; |
| 257 |
|
| 258 |
|
| 259 |
do_small = (esl_opt_GetBoolean(go, "--small")) ? TRUE : FALSE; |
| 260 |
|
| 261 |
|
| 262 |
if (esl_opt_IsOn(go, "--informat")) { |
| 263 |
infmt = esl_msafile_EncodeFormat(esl_opt_GetString(go, "--informat")); |
| 264 |
if (infmt == eslMSAFILE_UNKNOWN) esl_fatal("%s is not a valid input sequence file format for --informat", esl_opt_GetString(go, "--informat")); |
| 265 |
if (do_small && infmt != eslMSAFILE_PFAM) esl_fatal("small memory mode requires Pfam formatted alignments"); |
| 266 |
} |
| 267 |
if (esl_opt_IsOn(go, "--outformat")) { |
| 268 |
outfmt = esl_msafile_EncodeFormat(esl_opt_GetString(go, "--outformat")); |
| 269 |
if (outfmt == eslMSAFILE_UNKNOWN) esl_fatal("%s is not a valid input sequence file format for --outformat", esl_opt_GetString(go, "--outformat")); |
| 270 |
if (do_small && outfmt != eslMSAFILE_PFAM) esl_fatal("we can only output Pfam formatted alignments in small memory mode"); |
| 271 |
} |
| 272 |
else outfmt = eslMSAFILE_STOCKHOLM; |
| 273 |
|
| 274 |
if (do_small) { |
| 275 |
|
| 276 |
infmt = eslMSAFILE_PFAM; |
| 277 |
outfmt = eslMSAFILE_PFAM; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
if (esl_opt_GetString(go, "-o") != NULL) { |
| 282 |
if ((ofp = fopen(esl_opt_GetString(go, "-o"), "w")) == NULL) |
| 283 |
esl_fatal("Failed to open -o output file %s\n", esl_opt_GetString(go, "-o")); |
| 284 |
|
| 285 |
w = esl_stopwatch_Create(); |
| 286 |
esl_stopwatch_Start(w); |
| 287 |
} else ofp = stdout; |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
if (esl_opt_GetBoolean(go, "--amino")) abc = esl_alphabet_Create(eslAMINO); |
| 299 |
else if (esl_opt_GetBoolean(go, "--dna")) abc = esl_alphabet_Create(eslDNA); |
| 300 |
else if (esl_opt_GetBoolean(go, "--rna")) abc = esl_alphabet_Create(eslRNA); |
| 301 |
else if (do_small) esl_fatal("With --small, the alphabet must be specified with --amino, --rna, or --dna."); |
| 302 |
else abc = esl_alphabet_Create(eslRNA); |
| 303 |
|
| 304 |
if (do_small) |
| 305 |
{ |
| 306 |
status = esl_msafile2_Open(alifile, NULL, &afp2); |
| 307 |
if (status == eslENOTFOUND) esl_fatal("Alignment file %s doesn't exist or is not readable\n", alifile); |
| 308 |
else if (status != eslOK) esl_fatal("Alignment file open failed with error %d\n", status); |
| 309 |
} |
| 310 |
else |
| 311 |
{ |
| 312 |
status = esl_msafile_Open(NULL, alifile, NULL, infmt, NULL, &afp); |
| 313 |
if (status != eslOK) esl_msafile_OpenFailure(afp, status); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
if (do_maskfile) if((status = read_mask_file(maskfile, errbuf, &useme_mfile, &file_mlen)) != eslOK) esl_fatal(errbuf); |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
if (do_small) |
| 324 |
{ |
| 325 |
status = esl_msafile2_ReadInfoPfam(afp2, NULL, abc, -1, NULL, NULL, &msa, &nseq, &orig_alen, NULL, NULL, NULL, NULL, NULL, |
| 326 |
(do_gapthresh) ? &abc_ct : NULL, |
| 327 |
(do_postprob) ? &pp_ct : NULL, |
| 328 |
NULL, NULL, NULL); |
| 329 |
if (status == eslEFORMAT) esl_fatal("Alignment file parse error:\n%s\n", afp2->errbuf); |
| 330 |
else if (status == eslEINVAL) esl_fatal("Alignment file parse error:\n%s\n", afp2->errbuf); |
| 331 |
else if (status != eslOK) esl_fatal("Alignment file read failed with error code %d\n%s", status, afp); |
| 332 |
|
| 333 |
msa->alen = orig_alen; |
| 334 |
} |
| 335 |
else |
| 336 |
{ |
| 337 |
status = esl_msafile_Read(afp, &msa); |
| 338 |
if (status != eslOK) esl_msafile_ReadFailure(afp, status); |
| 339 |
|
| 340 |
orig_alen = msa->alen; |
| 341 |
if(do_postprob && msa->pp == NULL && (! esl_opt_IsOn(go, "--ppcons"))) esl_fatal("-p was enabled, but the MSA has no posterior probability (#=GR PP) annotation."); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
ESL_ALLOC(i_am_eligible, sizeof(int) * (int) msa->alen); |
| 350 |
esl_vec_ISet(i_am_eligible, (int) msa->alen, TRUE); |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
if(msa->rf != NULL) { |
| 355 |
if((status = map_rfpos_to_apos(msa, abc, errbuf, &i_am_rf, &rf2a_map, &rflen)) != eslOK) esl_fatal(errbuf); |
| 356 |
if(! esl_opt_GetBoolean(go, "--keepins")) { |
| 357 |
esl_vec_ICopy(i_am_rf, (int) msa->alen, i_am_eligible); |
| 358 |
} |
| 359 |
} |
| 360 |
else { |
| 361 |
if(esl_opt_IsOn(go, "--pmask-rf")) esl_fatal("Error, --pmask-rf enabled, but alignment does not have RF annotation."); |
| 362 |
if(esl_opt_IsOn(go, "--gmask-rf")) esl_fatal("Error, --gmask-rf enabled, but alignment does not have RF annotation."); |
| 363 |
if(esl_opt_IsOn(go, "--fmask-rf")) esl_fatal("Error, --fmask-rf enabled, but alignment does not have RF annotation."); |
| 364 |
if(esl_opt_GetBoolean(go, "--rf-is-mask")) esl_fatal("Error, --rf-is-mask enabled but msa does not have RF annotation."); |
| 365 |
if(esl_opt_GetBoolean(go, "--keepins")) esl_fatal("Error, --keepins enabled but msa does not have RF annotation."); |
| 366 |
if(esl_opt_GetBoolean(go, "--t-rmins")) esl_fatal("Error, --t-rmins enabled but msa does not have RF annotation."); |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
if(esl_opt_IsOn(go, "--ppcons") && msa->pp_cons == NULL) esl_fatal("--ppcons was enabled, but the MSA has no consensus posterior probability (#=GC PP_cons) annotation."); |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
do_rfonly = FALSE; |
| 379 |
if(do_maskfile) { |
| 380 |
if(msa->rf == NULL) { |
| 381 |
if((int) msa->alen != file_mlen) esl_fatal("Error, mask length (%d) not equal to alignment length (%d), and alignment has no #=GC RF annotation.", file_mlen, msa->alen); |
| 382 |
} |
| 383 |
else { |
| 384 |
if(((int) msa->alen != file_mlen) && (rflen != file_mlen)) esl_fatal("Error, mask length (%d) not equal to alignment length (%d), and alignment has no #=GC RF annotation.", file_mlen, msa->alen); |
| 385 |
if(rflen == file_mlen) do_rfonly = TRUE; |
| 386 |
} |
| 387 |
} |
| 388 |
else if(do_truncate) { |
| 389 |
if(msa->rf != NULL && esl_opt_GetBoolean(go, "--t-rmins")) do_rfonly = TRUE; |
| 390 |
|
| 391 |
} |
| 392 |
else if(do_rf_is_mask) { |
| 393 |
do_rfonly = TRUE; |
| 394 |
} |
| 395 |
else { |
| 396 |
if(msa->rf != NULL && (! esl_opt_GetBoolean(go, "--keepins"))) do_rfonly = TRUE; |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
ESL_ALLOC(useme_final, sizeof(int) * msa->alen); |
| 403 |
if(be_verbose) { |
| 404 |
fprintf(stdout, "# %19s %7s %7s %16s %16s %13s\n", "", "", "", "all columns", "non-gap RF colns",""); |
| 405 |
fprintf(stdout, "# %19s %7s %7s %16s %16s %13s\n", "", "", "", "----------------", "----------------",""); |
| 406 |
fprintf(stdout, "# %19s %7s %7s %7s %7s %7s %7s %13s\n", "", "", "non-gap", "num", "num", "num", "num", "gap RF colns"); |
| 407 |
fprintf(stdout, "# %-19s %7s %7s %7s %7s %7s %7s %13s\n","mask mode", "aln len", "RF len", "kept", "removed", "kept", "removed", "auto removed?"); |
| 408 |
fprintf(stdout, "# %19s %7s %7s %7s %7s %7s %7s %13s\n", "-------------------", "-------", "-------", "-------", "-------", "-------", "-------", "-------------"); |
| 409 |
} |
| 410 |
|
| 411 |
if(do_maskfile) { |
| 412 |
if(do_rfonly) { |
| 413 |
if((status = expand_rf_useme_to_alen(useme_mfile, rf2a_map, rflen, msa->alen, errbuf, useme_final)) != eslOK) esl_fatal(errbuf); |
| 414 |
} |
| 415 |
else { |
| 416 |
esl_vec_ICopy(useme_mfile, msa->alen, useme_final); |
| 417 |
} |
| 418 |
if(be_verbose) { |
| 419 |
nkept = esl_vec_ISum(useme_final, (int) msa->alen); |
| 420 |
if(msa->rf == NULL) fprintf(stdout, " %-19s %7" PRId64 " %7s %7d %7d %7s %7s %13s\n", "maskfile", msa->alen, "-", nkept, (int) msa->alen - nkept, "-", "-", "-"); |
| 421 |
else { |
| 422 |
nkept_rf = (do_rfonly) ? nkept: determine_nkept_rf(useme_final, i_am_rf, msa->alen); |
| 423 |
fprintf(stdout, " %-19s %7" PRId64 " %7d %7d %7d %7d %7d %13s\n", "maskfile", msa->alen, rflen, nkept, (int) msa->alen - nkept, nkept_rf, rflen - nkept_rf, do_rfonly ? "yes" : "no"); |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
if(do_truncate) { |
| 428 |
parse_coord_string(esl_opt_GetArg(go, 2), &tstart, &tend); |
| 429 |
|
| 430 |
if(esl_opt_GetBoolean(go, "--t-rf")) { |
| 431 |
|
| 432 |
if(tstart > rflen) esl_fatal("with -t and --t-rf, start coordinate value %d exceeds non-gap RF length of %d", tstart, rflen); |
| 433 |
if(tend > rflen) esl_fatal("with -t and --t-rf, end coordinate value %d exceeds non-gap RF length of %d", tend, rflen); |
| 434 |
tstart = rf2a_map[(tstart-1)] + 1; |
| 435 |
if(tend == 0) tend = (int) msa->alen; |
| 436 |
else tend = rf2a_map[(tend-1)] + 1; |
| 437 |
} |
| 438 |
else { |
| 439 |
if(tstart > msa->alen) esl_fatal("with -t, start coordinate value %d exceeds alignment length of %" PRId64, tstart, msa->alen); |
| 440 |
if(tend > msa->alen) esl_fatal("with -t, end coordinate value %d exceeds alignment length of %" PRId64, tend, msa->alen); |
| 441 |
if(tend == 0) tend = (int) msa->alen; |
| 442 |
} |
| 443 |
|
| 444 |
for(apos = 0; apos < (tstart-1); apos++) useme_final[apos] = FALSE; |
| 445 |
for(apos = (tstart-1); apos < tend; apos++) useme_final[apos] = do_rfonly ? i_am_rf[apos] : TRUE; |
| 446 |
for(apos = tend; apos < msa->alen; apos++) useme_final[apos] = FALSE; |
| 447 |
if(be_verbose) { |
| 448 |
nkept = esl_vec_ISum(useme_final, (int) msa->alen); |
| 449 |
if(msa->rf == NULL) fprintf(stdout, " %-19s %7" PRId64 " %7s %7d %7d %7s %7s %13s\n", "truncation", msa->alen, "-", nkept, (int) msa->alen - nkept, "-", "-", "-"); |
| 450 |
else { |
| 451 |
nkept_rf = (do_rfonly) ? nkept: determine_nkept_rf(useme_final, i_am_rf, msa->alen); |
| 452 |
fprintf(stdout, " %-19s %7" PRId64 " %7d %7d %7d %7d %7d %13s\n", "truncation", msa->alen, rflen, nkept, (int) msa->alen - nkept, nkept_rf, rflen - nkept_rf, do_rfonly ? "yes" : "no"); |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
if(do_gapthresh) { |
| 457 |
if(! do_small) { |
| 458 |
if((status = count_gaps_in_msa(msa, abc, i_am_eligible, errbuf, &gap_ct)) != eslOK) esl_fatal(errbuf); |
| 459 |
} |
| 460 |
else { |
| 461 |
ESL_ALLOC(gap_ct, sizeof(double) * msa->alen); |
| 462 |
for(apos = 0; apos < msa->alen; apos++) gap_ct[apos] = abc_ct[apos][abc->K]; |
| 463 |
} |
| 464 |
if((status = mask_based_on_gapfreq(gap_ct, msa->alen, (do_small) ? nseq : msa->nseq, esl_opt_GetReal(go, "--gapthresh"), i_am_eligible, errbuf, &useme_g)) != eslOK) esl_fatal(errbuf); |
| 465 |
if(be_verbose) { |
| 466 |
nkept = esl_vec_ISum(useme_g, (int) msa->alen); |
| 467 |
if(msa->rf == NULL) fprintf(stdout, " %-19s %7" PRId64 " %7s %7d %7d %7s %7s %13s\n", "gapfreq", msa->alen, "-", nkept, (int) msa->alen - nkept, "-", "-", "-"); |
| 468 |
else { |
| 469 |
nkept_rf = (do_rfonly) ? nkept: determine_nkept_rf(useme_g, i_am_rf, msa->alen); |
| 470 |
fprintf(stdout, " %-19s %7" PRId64 " %7d %7d %7d %7d %7d %13s\n", "gapfreq", msa->alen, rflen, nkept, (int) msa->alen - nkept, nkept_rf, rflen - nkept_rf, do_rfonly ? "yes" : "no"); |
| 471 |
} |
| 472 |
} |
| 473 |
} |
| 474 |
if(do_postprob) { |
| 475 |
if(! do_small) { |
| 476 |
if((status = count_postprobs_in_msa(msa, abc, i_am_eligible, errbuf, &pp_ct)) != eslOK) esl_fatal(errbuf); |
| 477 |
} |
| 478 |
do_pavg = esl_opt_IsOn(go, "--pavg"); |
| 479 |
pavg_min = do_pavg ? esl_opt_GetReal(go, "--pavg") : 0.; |
| 480 |
do_ppcons = esl_opt_IsOn(go, "--ppcons"); |
| 481 |
ppcons_min = do_ppcons ? esl_opt_GetReal(go, "--ppcons") : 0.; |
| 482 |
if((status = mask_based_on_postprobs(pp_ct, msa->alen, (do_small) ? nseq : msa->nseq, esl_opt_GetReal(go, "--pthresh"), esl_opt_GetReal(go, "--pfract"), do_pavg, pavg_min, do_ppcons, ppcons_min, msa->pp_cons, abc, i_am_eligible, esl_opt_GetBoolean(go, "--pallgapok"), errbuf, &useme_pp)) != eslOK) esl_fatal(errbuf); |
| 483 |
if(be_verbose) { |
| 484 |
nkept = esl_vec_ISum(useme_pp, (int) msa->alen); |
| 485 |
if(msa->rf == NULL) fprintf(stdout, " %-19s %7" PRId64 " %7s %7d %7d %7s %7s %13s\n", "postprobs", msa->alen, "-", nkept, (int) msa->alen - nkept, "-", "-", "-"); |
| 486 |
else { |
| 487 |
nkept_rf = (do_rfonly) ? nkept: determine_nkept_rf(useme_pp, i_am_rf, msa->alen); |
| 488 |
fprintf(stdout, " %-19s %7" PRId64 " %7d %7d %7d %7d %7d %13s\n", "postprobs", msa->alen, rflen, nkept, (int) msa->alen - nkept, nkept_rf, rflen - nkept_rf, do_rfonly ? "yes" : "no"); |
| 489 |
} |
| 490 |
} |
| 491 |
} |
| 492 |
if(do_rf_is_mask) { |
| 493 |
for(apos = 0; apos < msa->alen; apos++) useme_final[apos] = i_am_rf[apos]; |
| 494 |
if(be_verbose) fprintf(stdout, " %-19s %7" PRId64 " %7d %7d %7d %7d %7d %13s\n", "RF", msa->alen, rflen, rflen, (int) msa->alen - rflen, rflen, 0, "yes"); |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
if(do_gapthresh && do_postprob) { |
| 502 |
for(apos = 0; apos < msa->alen; apos++) |
| 503 |
useme_final[apos] = (useme_g[apos] && useme_pp[apos]) ? TRUE : FALSE; |
| 504 |
if(be_verbose) { |
| 505 |
nkept = esl_vec_ISum(useme_final, (int) msa->alen); |
| 506 |
if(msa->rf == NULL) fprintf(stdout, " %-19s %7" PRId64 " %7s %7d %7d %7s %7s %13s\n", "gapfreq&postprobs", msa->alen, "-", nkept, (int) msa->alen - nkept, "-", "-", "-"); |
| 507 |
else { |
| 508 |
nkept_rf = (do_rfonly) ? nkept: determine_nkept_rf(useme_final, i_am_rf, msa->alen); |
| 509 |
fprintf(stdout, " %-19s %7" PRId64 " %7d %7d %7d %7d %7d %13s\n", "gapfreq&postprobs", msa->alen, rflen, nkept, (int) msa->alen - nkept, nkept_rf, rflen - nkept_rf, do_rfonly ? "yes" : "no"); |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
else if(do_gapthresh) { |
| 514 |
esl_vec_ICopy(useme_g, msa->alen, useme_final); |
| 515 |
} |
| 516 |
else if(do_postprob) { |
| 517 |
esl_vec_ICopy(useme_pp, msa->alen, useme_final); |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
if(! do_small) { |
| 525 |
if (abc && (abc->type == eslRNA || abc->type == eslDNA) && |
| 526 |
(status = esl_msa_RemoveBrokenBasepairs(msa, errbuf, useme_final)) != eslOK) esl_fatal(errbuf); |
| 527 |
if ((status = esl_msa_ColumnSubset (msa, errbuf, useme_final)) != eslOK) esl_fatal(errbuf); |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
if (! do_small) |
| 534 |
{ |
| 535 |
esl_msafile_Write(ofp, msa, outfmt); |
| 536 |
} |
| 537 |
else { |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
esl_msafile2_Close(afp2); |
| 543 |
status = esl_msafile2_OpenDigital(abc, alifile, NULL, &afp2); |
| 544 |
if (status == eslENOTFOUND) esl_fatal("Second pass: alignment file %s doesn't exist or is not readable\n", alifile); |
| 545 |
else if (status == eslEFORMAT) esl_fatal("Second pass: couldn't determine format of alignment %s\n", alifile); |
| 546 |
else if (status != eslOK) esl_fatal("Second pass: alignment file open failed with error %d\n"); |
| 547 |
status = esl_msafile2_RegurgitatePfam(afp2, ofp, |
| 548 |
-1, -1, -1, -1, |
| 549 |
TRUE, |
| 550 |
TRUE, |
| 551 |
TRUE, |
| 552 |
TRUE, |
| 553 |
TRUE, |
| 554 |
TRUE, |
| 555 |
TRUE, |
| 556 |
TRUE, |
| 557 |
TRUE, |
| 558 |
NULL, |
| 559 |
NULL, |
| 560 |
useme_final, |
| 561 |
NULL, |
| 562 |
msa->alen, |
| 563 |
'.', |
| 564 |
NULL, |
| 565 |
NULL); |
| 566 |
if(status == eslEOF) esl_fatal("Second pass, unable to reread alignment"); |
| 567 |
if(status != eslOK) esl_fatal("Second pass, error rereading alignment"); |
| 568 |
} |
| 569 |
if(be_verbose) fprintf(stdout, "#\n"); |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
if(esl_opt_IsOn(go, "--pmask-rf")) { |
| 575 |
if((status = output_mask(esl_opt_GetString(go, "--pmask-rf"), useme_pp, i_am_rf, orig_alen, errbuf)) != eslOK) esl_fatal(errbuf); |
| 576 |
if(be_verbose) fprintf(stdout, "# Posterior probability mask of non-gap RF length (%d) saved to file %s.\n", rflen, esl_opt_GetString(go, "--pmask-rf")); |
| 577 |
} |
| 578 |
if(esl_opt_IsOn(go, "--pmask-all")) { |
| 579 |
if((status = output_mask(esl_opt_GetString(go, "--pmask-all"), useme_pp, NULL, orig_alen, errbuf)) != eslOK) esl_fatal(errbuf); |
| 580 |
if(be_verbose) fprintf(stdout, "# Posterior probability mask of full alignment length (%d) saved to file %s.\n", (int) orig_alen, esl_opt_GetString(go, "--pmask-all")); |
| 581 |
} |
| 582 |
if(esl_opt_IsOn(go, "--gmask-rf")) { |
| 583 |
if((status = output_mask(esl_opt_GetString(go, "--gmask-rf"), useme_g, i_am_rf, orig_alen, errbuf)) != eslOK) esl_fatal(errbuf); |
| 584 |
if(be_verbose) fprintf(stdout, "# Gap frequency mask of non-gap RF length (%d) saved to file %s.\n", rflen, esl_opt_GetString(go, "--gmask-rf")); |
| 585 |
} |
| 586 |
if(esl_opt_IsOn(go, "--gmask-all")) { |
| 587 |
if((status = output_mask(esl_opt_GetString(go, "--gmask-all"), useme_g, NULL, orig_alen, errbuf)) != eslOK) esl_fatal(errbuf); |
| 588 |
if(be_verbose) fprintf(stdout, "# Gap frequency mask of full alignment length (%d) saved to file %s.\n", (int) orig_alen, esl_opt_GetString(go, "--gmask-all")); |
| 589 |
} |
| 590 |
if(esl_opt_IsOn(go, "--fmask-rf")) { |
| 591 |
if((status = output_mask(esl_opt_GetString(go, "--fmask-rf"), useme_final, i_am_rf, orig_alen, errbuf)) != eslOK) esl_fatal(errbuf); |
| 592 |
if(be_verbose) fprintf(stdout, "# Final mask of non-gap RF length (%d) saved to file %s.\n", rflen, esl_opt_GetString(go, "--fmask-rf")); |
| 593 |
} |
| 594 |
if(esl_opt_IsOn(go, "--fmask-all")) { |
| 595 |
if((status = output_mask(esl_opt_GetString(go, "--fmask-all"), useme_final, NULL, orig_alen, errbuf)) != eslOK) esl_fatal(errbuf); |
| 596 |
if(be_verbose) fprintf(stdout, "# Final mask of full alignment length (%d) saved to file %s.\n", (int) orig_alen, esl_opt_GetString(go, "--fmask-all")); |
| 597 |
} |
| 598 |
if(esl_opt_GetString(go, "-o") != NULL) { |
| 599 |
fclose(ofp); |
| 600 |
if(be_verbose) fprintf(stdout, "# Masked alignment saved to file %s.\n", esl_opt_GetString(go, "-o")); |
| 601 |
esl_stopwatch_Stop(w); |
| 602 |
esl_stopwatch_Display(stdout, w, "# CPU time: "); |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
esl_free(rf2a_map); |
| 607 |
esl_free(useme_mfile); |
| 608 |
esl_free(useme_g); |
| 609 |
esl_free(useme_pp); |
| 610 |
esl_free(useme_final); |
| 611 |
esl_free(i_am_rf); |
| 612 |
esl_free(i_am_eligible); |
| 613 |
esl_free(gap_ct); |
| 614 |
|
| 615 |
esl_arr2_Destroy((void **) pp_ct, orig_alen); |
| 616 |
esl_arr2_Destroy((void **) abc_ct, orig_alen); |
| 617 |
esl_msa_Destroy(msa); |
| 618 |
esl_alphabet_Destroy(abc); |
| 619 |
esl_stopwatch_Destroy(w); |
| 620 |
esl_msafile_Close(afp); |
| 621 |
esl_msafile2_Close(afp2); |
| 622 |
esl_getopts_Destroy(go); |
| 623 |
return 0; |
| 624 |
|
| 625 |
ERROR: |
| 626 |
esl_fatal("Memory allocation error."); |
| 627 |
return status; |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
int |
| 649 |
read_mask_file(char *filename, char *errbuf, int **ret_useme, int *ret_mlen) |
| 650 |
{ |
| 651 |
int status; |
| 652 |
ESL_FILEPARSER *efp; |
| 653 |
char *tok; |
| 654 |
int *useme = NULL; |
| 655 |
int toklen; |
| 656 |
int mlen = 0; |
| 657 |
int i; |
| 658 |
void *tmp; |
| 659 |
|
| 660 |
if (esl_fileparser_Open(filename, NULL, &efp) != eslOK) ESL_FAIL(eslFAIL, errbuf, "failed to open %s in read_mask_file\n", filename); |
| 661 |
esl_fileparser_SetCommentChar(efp, '#'); |
| 662 |
|
| 663 |
while ((status = esl_fileparser_GetToken(efp, &tok, &toklen)) == eslOK) { |
| 664 |
if(mlen == 0) ESL_ALLOC (useme, sizeof(int) * toklen); |
| 665 |
else ESL_RALLOC(useme, tmp, sizeof(int) * (mlen+toklen)); |
| 666 |
for(i = 0; i < toklen; i++) { |
| 667 |
if(tok[i] == '0') useme[mlen+i] = FALSE; |
| 668 |
else if(tok[i] == '1') useme[mlen+i] = TRUE; |
| 669 |
else ESL_FAIL(eslEINVAL, errbuf, "Mask character number %d is neither a '0', nor a '1' but a (%c). The mask must be only 1s and 0s.", mlen+i+1, tok[i]); |
| 670 |
} |
| 671 |
mlen += toklen; |
| 672 |
} |
| 673 |
|
| 674 |
*ret_useme = useme; |
| 675 |
*ret_mlen = mlen; |
| 676 |
esl_fileparser_Close(efp); |
| 677 |
return eslOK; |
| 678 |
|
| 679 |
ERROR: |
| 680 |
if(useme != NULL) free(useme); |
| 681 |
ESL_FAIL(status, errbuf, "Ran out of memory while reading the mask file."); |
| 682 |
return status; |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
static int map_rfpos_to_apos(ESL_MSA *msa, ESL_ALPHABET *abc, char *errbuf, int **ret_i_am_rf, int **ret_rf2a_map, int *ret_rflen) |
| 695 |
{ |
| 696 |
int status; |
| 697 |
int rflen = 0; |
| 698 |
int *rf2a_map = NULL; |
| 699 |
int *i_am_rf = NULL; |
| 700 |
int rfpos = 0; |
| 701 |
int apos = 0; |
| 702 |
|
| 703 |
|
| 704 |
if(msa->rf == NULL) ESL_FAIL(eslEINVAL, errbuf, "Error, trying to map RF positions to alignment positions, but msa->rf is NULL."); |
| 705 |
|
| 706 |
|
| 707 |
for(apos = 0; apos < msa->alen; apos++) { |
| 708 |
if((! esl_abc_CIsGap(abc, msa->rf[apos])) && |
| 709 |
(! esl_abc_CIsMissing(abc, msa->rf[apos])) && |
| 710 |
(! esl_abc_CIsNonresidue(abc, msa->rf[apos]))) |
| 711 |
{ |
| 712 |
rflen++; |
| 713 |
|
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
ESL_ALLOC(i_am_rf, sizeof(int) * msa->alen); |
| 718 |
ESL_ALLOC(rf2a_map, sizeof(int) * rflen); |
| 719 |
for(apos = 0; apos < msa->alen; apos++) { |
| 720 |
if((! esl_abc_CIsGap(abc, msa->rf[apos])) && |
| 721 |
(! esl_abc_CIsMissing(abc, msa->rf[apos])) && |
| 722 |
(! esl_abc_CIsNonresidue(abc, msa->rf[apos]))) { |
| 723 |
i_am_rf[apos] = TRUE; |
| 724 |
rf2a_map[rfpos++] = apos; |
| 725 |
} |
| 726 |
else { |
| 727 |
i_am_rf[apos] = FALSE; |
| 728 |
} |
| 729 |
} |
| 730 |
*ret_i_am_rf = i_am_rf; |
| 731 |
*ret_rf2a_map = rf2a_map; |
| 732 |
*ret_rflen = rflen; |
| 733 |
return eslOK; |
| 734 |
|
| 735 |
ERROR: |
| 736 |
if(i_am_rf != NULL) free(i_am_rf); |
| 737 |
if(rf2a_map != NULL) free(rf2a_map); |
| 738 |
ESL_FAIL(status, errbuf, "Error, out of memory while mapping RF positions to alignment positions."); |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
static int expand_rf_useme_to_alen(int *useme_rf, int *rf2a_map, int rflen, int alen, char *errbuf, int *useme_a) |
| 752 |
{ |
| 753 |
int rfpos = 0; |
| 754 |
|
| 755 |
|
| 756 |
esl_vec_ISet(useme_a, alen, FALSE); |
| 757 |
|
| 758 |
for(rfpos = 0; rfpos < rflen; rfpos++) { |
| 759 |
if(rf2a_map[rfpos] > (alen-1)) ESL_FAIL(eslEINVAL, errbuf, "Error in expand_rf_useme_to_alen(), rf2a_map[rfpos:%d] = %d, but alen is %d.", rfpos, rf2a_map[rfpos], alen); |
| 760 |
useme_a[rf2a_map[rfpos]] = useme_rf[rfpos]; |
| 761 |
} |
| 762 |
|
| 763 |
return eslOK; |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
static int count_gaps_in_msa(ESL_MSA *msa, ESL_ALPHABET *abc, int *countme, char *errbuf, double **ret_gap_ct) |
| 783 |
{ |
| 784 |
int status; |
| 785 |
double *gap_ct = NULL; |
| 786 |
int apos, i; |
| 787 |
|
| 788 |
|
| 789 |
if(msa->flags & eslMSA_DIGITAL) ESL_FAIL(eslEINVAL, errbuf, "count_gaps_in_msa() contract violation, MSA is digitized"); |
| 790 |
|
| 791 |
ESL_ALLOC(gap_ct, sizeof(double) * msa->alen); |
| 792 |
esl_vec_DSet(gap_ct, msa->alen, 0.); |
| 793 |
|
| 794 |
for(apos = 0; apos < (int) msa->alen; apos++) { |
| 795 |
if(countme[apos]) { |
| 796 |
for(i = 0; i < msa->nseq; i++) { |
| 797 |
if(esl_abc_CIsGap(abc, msa->aseq[i][apos])) gap_ct[apos] += 1.0; |
| 798 |
} |
| 799 |
} |
| 800 |
} |
| 801 |
*ret_gap_ct = gap_ct; |
| 802 |
return eslOK; |
| 803 |
|
| 804 |
ERROR: |
| 805 |
if(gap_ct != NULL) free(gap_ct); |
| 806 |
ESL_FAIL(status, errbuf, "Error, out of memory while counting gaps in the msa."); |
| 807 |
return status; |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
static int mask_based_on_gapfreq(double *gap_ct, int64_t alen, int nseq, float gapthresh, int *i_am_eligible, char *errbuf, int **ret_useme) |
| 827 |
{ |
| 828 |
int status; |
| 829 |
int *useme = NULL; |
| 830 |
int apos; |
| 831 |
float gapfreq; |
| 832 |
|
| 833 |
|
| 834 |
if(i_am_eligible == NULL) ESL_FAIL(eslEINVAL, errbuf, "mask_based_on_gapthresh() contract violation, i_am_eligible is NULL"); |
| 835 |
|
| 836 |
|
| 837 |
ESL_ALLOC(useme, sizeof(int) * alen); |
| 838 |
|
| 839 |
for(apos = 0; apos < alen; apos++) { |
| 840 |
if(i_am_eligible[apos]) { |
| 841 |
gapfreq = gap_ct[apos] / (float) nseq; |
| 842 |
useme[apos] = gapthresh < gapfreq ? FALSE : TRUE; |
| 843 |
|
| 844 |
} |
| 845 |
else useme[apos] = FALSE; |
| 846 |
} |
| 847 |
|
| 848 |
*ret_useme = useme; |
| 849 |
return eslOK; |
| 850 |
|
| 851 |
ERROR: |
| 852 |
if(useme != NULL) free(useme); |
| 853 |
ESL_FAIL(status, errbuf, "Error, out of memory while masking based on gaps."); |
| 854 |
return status; |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
static int get_pp_idx(ESL_ALPHABET *abc, char ppchar) |
| 882 |
{ |
| 883 |
if(esl_abc_CIsGap(abc, ppchar)) return 11; |
| 884 |
if(ppchar == '*') return 10; |
| 885 |
if(ppchar == '9') return 9; |
| 886 |
if(ppchar == '8') return 8; |
| 887 |
if(ppchar == '7') return 7; |
| 888 |
if(ppchar == '6') return 6; |
| 889 |
if(ppchar == '5') return 5; |
| 890 |
if(ppchar == '4') return 4; |
| 891 |
if(ppchar == '3') return 3; |
| 892 |
if(ppchar == '2') return 2; |
| 893 |
if(ppchar == '1') return 1; |
| 894 |
if(ppchar == '0') return 0; |
| 895 |
return -1; |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
static int count_postprobs_in_msa(ESL_MSA *msa, ESL_ALPHABET *abc, int *countme, char *errbuf, double ***ret_pp_ct) |
| 919 |
{ |
| 920 |
int status; |
| 921 |
double **pp_ct = NULL; |
| 922 |
int apos, i; |
| 923 |
int nppvals = 12; |
| 924 |
int ppidx; |
| 925 |
|
| 926 |
|
| 927 |
if(msa->flags & eslMSA_DIGITAL) ESL_FAIL(eslEINVAL, errbuf, "count_postprobs_in_msa() contract violation, MSA is digitized"); |
| 928 |
if(msa->pp == NULL) ESL_FAIL(eslEINVAL, errbuf, "count_postprobs_in_msa() contract violation, msa->pp is NULL"); |
| 929 |
|
| 930 |
ESL_ALLOC(pp_ct, sizeof(double *) * msa->alen); |
| 931 |
for(apos = 0; apos < msa->alen; apos++) { |
| 932 |
ESL_ALLOC(pp_ct[apos], sizeof(double) * nppvals); |
| 933 |
esl_vec_DSet(pp_ct[apos], nppvals, 0.); |
| 934 |
} |
| 935 |
|
| 936 |
for(apos = 0; apos < (int) msa->alen; apos++) { |
| 937 |
if(countme[apos]) { |
| 938 |
for(i = 0; i < msa->nseq; i++) { |
| 939 |
if(msa->pp[i] == NULL) { |
| 940 |
ESL_FAIL(eslEINVAL, errbuf, "some but not all sequences have PP annotation in msa, seq %d does not.\n", (i+1)); |
| 941 |
} |
| 942 |
ppidx = get_pp_idx(abc, msa->pp[i][apos]); |
| 943 |
if(ppidx == 11) { |
| 944 |
|
| 945 |
if(! esl_abc_CIsGap(abc, msa->aseq[i][apos])) ESL_FAIL(eslEINVAL, errbuf, "post prob annotation for seq: %d aln column: %d is a gap (%c), but seq res is not: (%c)", i, apos, msa->pp[i][apos], msa->aseq[i][apos]); |
| 946 |
} |
| 947 |
pp_ct[apos][ppidx] += 1.; |
| 948 |
} |
| 949 |
} |
| 950 |
} |
| 951 |
*ret_pp_ct = pp_ct; |
| 952 |
return eslOK; |
| 953 |
|
| 954 |
ERROR: |
| 955 |
if(pp_ct != NULL) { |
| 956 |
for(apos = 0; apos < msa->alen; apos++) { |
| 957 |
if(pp_ct[apos] != NULL) free(pp_ct[apos]); |
| 958 |
} |
| 959 |
free(pp_ct); |
| 960 |
} |
| 961 |
ESL_FAIL(status, errbuf, "Error, out of memory while counting post probs in the msa."); |
| 962 |
return status; |
| 963 |
} |
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
static int mask_based_on_postprobs(double **pp_ct, int64_t alen, int nseq, float pthresh, float pfract, int do_pavg, float pavg_min, int do_ppcons, float ppcons_min, char *pp_cons, ESL_ALPHABET *abc, int *i_am_eligible, int allgapok, char *errbuf, int **ret_useme) |
| 1000 |
{ |
| 1001 |
int status; |
| 1002 |
int *useme = NULL; |
| 1003 |
int apos; |
| 1004 |
float ppfreq; |
| 1005 |
double nnongap; |
| 1006 |
int nppvals = 12; |
| 1007 |
int ppidx_thresh; |
| 1008 |
int ppidx = 0; |
| 1009 |
double ppcount = 0.; |
| 1010 |
double ppsum = 0.; |
| 1011 |
double pavg; |
| 1012 |
double ppminA[11]; |
| 1013 |
double ppavgA[11]; |
| 1014 |
|
| 1015 |
ppminA[0] = 0.00; |
| 1016 |
ppminA[1] = 0.05; |
| 1017 |
ppminA[2] = 0.15; |
| 1018 |
ppminA[3] = 0.25; |
| 1019 |
ppminA[4] = 0.35; |
| 1020 |
ppminA[5] = 0.45; |
| 1021 |
ppminA[6] = 0.55; |
| 1022 |
ppminA[7] = 0.65; |
| 1023 |
ppminA[8] = 0.75; |
| 1024 |
ppminA[9] = 0.85; |
| 1025 |
ppminA[10] = 0.95; |
| 1026 |
|
| 1027 |
ppavgA[0] = 0.025; |
| 1028 |
ppavgA[1] = 0.10; |
| 1029 |
ppavgA[2] = 0.20; |
| 1030 |
ppavgA[3] = 0.30; |
| 1031 |
ppavgA[4] = 0.40; |
| 1032 |
ppavgA[5] = 0.50; |
| 1033 |
ppavgA[6] = 0.60; |
| 1034 |
ppavgA[7] = 0.70; |
| 1035 |
ppavgA[8] = 0.80; |
| 1036 |
ppavgA[9] = 0.90; |
| 1037 |
ppavgA[10] = 0.975; |
| 1038 |
|
| 1039 |
|
| 1040 |
if(i_am_eligible == NULL) ESL_FAIL(eslEINVAL, errbuf, "mask_based_on_postprobs() contract violation, i_am_eligible is NULL"); |
| 1041 |
if(do_pavg == TRUE && do_ppcons == TRUE) ESL_FAIL(eslEINVAL, errbuf, "mask_based_on_postprobs() contract violation, do_pavg and do_ppcons are both TRUE"); |
| 1042 |
if(do_ppcons == TRUE && pp_cons == NULL) ESL_FAIL(eslEINVAL, errbuf, "mask_based_on_postprobs() contract violation, do_ppcons is TRUE, but ppcons string is NULL"); |
| 1043 |
|
| 1044 |
|
| 1045 |
ESL_ALLOC(useme, sizeof(int) * alen); |
| 1046 |
|
| 1047 |
if((! do_pavg) && (! do_ppcons)) { |
| 1048 |
|
| 1049 |
ppidx = 0; |
| 1050 |
while(ppidx < 10) { |
| 1051 |
if((esl_FCompare_old(pthresh, ppminA[ppidx], eslSMALLX1) == eslOK) || pthresh < ppminA[ppidx]) break; |
| 1052 |
ppidx++; |
| 1053 |
} |
| 1054 |
ppidx_thresh = ppidx; |
| 1055 |
} |
| 1056 |
|
| 1057 |
for(apos = 0; apos < alen; apos++) { |
| 1058 |
ppcount = 0.; |
| 1059 |
ppsum = 0.; |
| 1060 |
if(i_am_eligible[apos]) { |
| 1061 |
nnongap = esl_vec_DSum(pp_ct[apos], nppvals) - pp_ct[apos][11]; |
| 1062 |
if(esl_FCompare_old(nnongap, 0., eslSMALLX1) == eslOK) { |
| 1063 |
useme[apos] = allgapok ? TRUE : FALSE; |
| 1064 |
} |
| 1065 |
else { |
| 1066 |
if(do_pavg) { |
| 1067 |
for(ppidx = 0; ppidx < 11; ppidx++) { |
| 1068 |
ppsum += pp_ct[apos][ppidx] * ppavgA[ppidx]; |
| 1069 |
|
| 1070 |
} |
| 1071 |
pavg = ppsum / nnongap; |
| 1072 |
useme[apos] = pavg < pavg_min? FALSE : TRUE; |
| 1073 |
|
| 1074 |
} |
| 1075 |
else if(do_ppcons) { |
| 1076 |
ppidx = get_pp_idx(abc, pp_cons[apos]); |
| 1077 |
if(ppidx == -1) ESL_FAIL(eslEFORMAT, errbuf, "bad #=GC PP_cons char: %c at position %d", pp_cons[apos], apos+1); |
| 1078 |
if(ppidx != 11) { |
| 1079 |
useme[apos] = ((esl_FCompare_old(ppcons_min, ppminA[ppidx], eslSMALLX1) == eslOK) || ppminA[ppidx] > ppcons_min) ? TRUE : FALSE; |
| 1080 |
|
| 1081 |
} |
| 1082 |
else useme[apos] = FALSE; |
| 1083 |
} |
| 1084 |
else { |
| 1085 |
for(ppidx = 10; ppidx >= ppidx_thresh; ppidx--) { |
| 1086 |
ppcount += pp_ct[apos][ppidx]; |
| 1087 |
} |
| 1088 |
ppfreq = ppcount / nnongap; |
| 1089 |
useme[apos] = (ppfreq < pfract) ? FALSE : TRUE; |
| 1090 |
|
| 1091 |
} |
| 1092 |
} |
| 1093 |
} |
| 1094 |
else useme[apos] = FALSE; |
| 1095 |
} |
| 1096 |
|
| 1097 |
*ret_useme = useme; |
| 1098 |
return eslOK; |
| 1099 |
|
| 1100 |
ERROR: |
| 1101 |
if(useme != NULL) free(useme); |
| 1102 |
ESL_FAIL(status, errbuf, "Error, out of memory while masking based on postprobs."); |
| 1103 |
return status; |
| 1104 |
} |
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
static int |
| 1124 |
output_mask(char *filename, int *useme, int *i_am_eligible, int64_t alen, char *errbuf) |
| 1125 |
{ |
| 1126 |
int status; |
| 1127 |
FILE *ofp; |
| 1128 |
int apos; |
| 1129 |
char *mask = NULL; |
| 1130 |
int64_t mlen; |
| 1131 |
int mpos; |
| 1132 |
|
| 1133 |
if(useme == NULL) ESL_FAIL(eslEINVAL, errbuf, "Error, trying to output a mask but useme is NULL."); |
| 1134 |
|
| 1135 |
|
| 1136 |
if(i_am_eligible == NULL) mlen = alen; |
| 1137 |
else mlen = esl_vec_ISum(i_am_eligible, (int) alen); |
| 1138 |
|
| 1139 |
ESL_ALLOC(mask, sizeof(char) * (mlen+1)); |
| 1140 |
|
| 1141 |
mpos = 0; |
| 1142 |
for (apos = 0; apos < alen; apos++) { |
| 1143 |
if(i_am_eligible == NULL) { |
| 1144 |
mask[mpos++] = useme[apos] ? '1' : '0'; |
| 1145 |
} |
| 1146 |
else if(i_am_eligible[apos]) { |
| 1147 |
mask[mpos++] = useme[apos] ? '1' : '0'; |
| 1148 |
} |
| 1149 |
} |
| 1150 |
mask[mlen] = '\0'; |
| 1151 |
|
| 1152 |
if ((ofp = fopen(filename, "w")) == NULL) ESL_FAIL(eslFAIL, errbuf, "Failed to open output file %s\n", filename); |
| 1153 |
fprintf(ofp, "%s\n", mask); |
| 1154 |
|
| 1155 |
free(mask); |
| 1156 |
fclose(ofp); |
| 1157 |
return eslOK; |
| 1158 |
|
| 1159 |
ERROR: |
| 1160 |
ESL_FAIL(status, errbuf, "Memory allocation error while outputting mask."); |
| 1161 |
return status; |
| 1162 |
} |
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
static int |
| 1171 |
determine_nkept_rf(int *useme, int *i_am_rf, int64_t len) |
| 1172 |
{ |
| 1173 |
int nkept_rf = 0; |
| 1174 |
int apos; |
| 1175 |
for(apos = 0; apos < len; apos++) { |
| 1176 |
if(useme[apos] && i_am_rf[apos]) nkept_rf++; |
| 1177 |
} |
| 1178 |
return nkept_rf; |
| 1179 |
} |
| 1180 |
|
| 1181 |
static int |
| 1182 |
parse_coord_string(const char *cstring, uint32_t *ret_start, uint32_t *ret_end) |
| 1183 |
{ |
| 1184 |
ESL_REGEXP *re = esl_regexp_Create(); |
| 1185 |
char tok1[32]; |
| 1186 |
char tok2[32]; |
| 1187 |
uint32_t start, end; |
| 1188 |
|
| 1189 |
if (esl_regexp_Match(re, "^(\\d+)\\D+(\\d*)$", cstring) != eslOK) esl_fatal("with -t, 2nd cmdline arg must be coords <from>..<to>; %s not recognized", cstring); |
| 1190 |
if (esl_regexp_SubmatchCopy(re, 1, tok1, 32) != eslOK) esl_fatal("Failed to find <from> coord in %s", cstring); |
| 1191 |
if (esl_regexp_SubmatchCopy(re, 2, tok2, 32) != eslOK) esl_fatal("Failed to find <to> coord in %s", cstring); |
| 1192 |
|
| 1193 |
|
| 1194 |
start = atol(tok1); |
| 1195 |
if(start == 0) esl_fatal("with -t, coordinates must be positive integers <from>..<to>, <from> coordinate read as '0'"); |
| 1196 |
if(tok2[0] == '\0') { |
| 1197 |
end = 0; |
| 1198 |
} |
| 1199 |
else { |
| 1200 |
end = atol(tok2); |
| 1201 |
if(end <= 0) esl_fatal("with -t, coordinates must be positive integers <from>..<to>, <to> coordinate read as '0'"); |
| 1202 |
if(start > end) esl_fatal("with -t, 2nd cmdline arg <coords> must equal <from>..<to>, with <from> <= <to>"); |
| 1203 |
} |
| 1204 |
|
| 1205 |
*ret_start = start; |
| 1206 |
*ret_end = end; |
| 1207 |
|
| 1208 |
esl_regexp_Destroy(re); |
| 1209 |
return eslOK; |
| 1210 |
} |
| 1211 |
|
| 1212 |
|