| 1 |
#include <esl_config.h> |
| 2 |
|
| 3 |
#include <string.h> |
| 4 |
|
| 5 |
#include "easel.h" |
| 6 |
#include "esl_getopts.h" |
| 7 |
#include "esl_msa.h" |
| 8 |
#include "esl_msafile.h" |
| 9 |
#include "esl_msaweight.h" |
| 10 |
#include "esl_subcmd.h" |
| 11 |
|
| 12 |
#define PREFOPTS "--conscover,--randorder,--origorder" |
| 13 |
|
| 14 |
static ESL_OPTIONS cmd_options[] = { |
| 15 |
|
| 16 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 1 }, |
| 17 |
{ "-o", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "send filtered output MSAs to file <f>, not stdout", 1 }, |
| 18 |
{ "--informat", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "specify the input MSA file is in format <s>", 1 }, |
| 19 |
{ "--outformat", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "write the filtered output MSA in format <s>", 1 }, |
| 20 |
{ "--dna", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "specify that input MSA is DNA (don't autodetect)", 1 }, |
| 21 |
{ "--rna", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, " ... that input MSA is RNA", 1 }, |
| 22 |
{ "--amino", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, " ... that input MSA is protein", 1 }, |
| 23 |
|
| 24 |
{ "--ignore-rf", eslARG_NONE, eslMSAWEIGHT_IGNORE_RF, NULL, NULL, NULL, NULL, NULL, "ignore any RF line; always determine our own consensus", 2 }, |
| 25 |
{ "--fragthresh", eslARG_REAL, ESL_STR(eslMSAWEIGHT_FRAGTHRESH), NULL, "0<=x<=1", NULL, NULL, NULL, "seq is fragment if aspan/alen < fragthresh", 2 }, |
| 26 |
{ "--symfrac", eslARG_REAL, ESL_STR(eslMSAWEIGHT_SYMFRAC), NULL, "0<=x<=1", NULL, NULL, NULL, "col is consensus if nres/(nres+ngap) >= symfrac", 2 }, |
| 27 |
|
| 28 |
{ "--no-sampling", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "never use subsampling to determine consensus", 3 }, |
| 29 |
{ "--nsamp", eslARG_INT, ESL_STR(eslMSAWEIGHT_NSAMP), NULL, "n>=1", NULL, NULL, "--no-sampling", "number of seqs to sample (if using sampling)", 3 }, |
| 30 |
{ "--sampthresh", eslARG_INT, ESL_STR(eslMSAWEIGHT_SAMPTHRESH), NULL, "n>=0", NULL, NULL, "--no-sampling", "switch to using sampling when nseq > nsamp", 3 }, |
| 31 |
{ "--maxfrag", eslARG_INT, ESL_STR(eslMSAWEIGHT_MAXFRAG), NULL, "n>=0", NULL, NULL, "--no-sampling", "if sample has > maxfrag fragments, don't use sample", 3 }, |
| 32 |
{ "-s", eslARG_INT, ESL_STR(eslMSAWEIGHT_RNGSEED), NULL, "n>=0", NULL, NULL, NULL, "set random number seed to <n>", 3 }, |
| 33 |
|
| 34 |
{ "--conscover", eslARG_NONE,"default", NULL, NULL, PREFOPTS, NULL, NULL, "keep seq whose alispan has better consensus coverage", 4 }, |
| 35 |
{ "--randorder", eslARG_NONE, NULL, NULL, NULL, PREFOPTS, NULL, NULL, " ... or with random preference", 4 }, |
| 36 |
{ "--origorder", eslARG_NONE, NULL, NULL, NULL, PREFOPTS, NULL, NULL, " ... or prefer seq that comes first in order", 4 }, |
| 37 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 38 |
}; |
| 39 |
|
| 40 |
|
| 41 |
static ESL_GETOPTS *process_cmdline(const char *topcmd, const ESL_SUBCMD *sub, const ESL_OPTIONS *suboptions, int argc, char **argv); |
| 42 |
|
| 43 |
|
| 44 |
int |
| 45 |
esl_cmd_filter(const char *topcmd, const ESL_SUBCMD *sub, int argc, char **argv) |
| 46 |
{ |
| 47 |
ESL_GETOPTS *go = process_cmdline(topcmd, sub, cmd_options, argc, argv); |
| 48 |
ESL_ALPHABET *abc = NULL; |
| 49 |
double maxid = strtod( esl_opt_GetArg(go, 1), NULL); |
| 50 |
char *msafile = esl_opt_GetArg(go, 2); |
| 51 |
int infmt = eslMSAFILE_UNKNOWN; |
| 52 |
int outfmt = eslMSAFILE_UNKNOWN; |
| 53 |
FILE *ofp = NULL; |
| 54 |
ESL_MSAWEIGHT_CFG *cfg = esl_msaweight_cfg_Create(); |
| 55 |
ESL_MSAFILE *afp = NULL; |
| 56 |
ESL_MSA *msa = NULL; |
| 57 |
ESL_MSA *msa2 = NULL; |
| 58 |
int nali = 0; |
| 59 |
int status = eslOK; |
| 60 |
|
| 61 |
if (maxid < 0. || maxid > 1.0) |
| 62 |
esl_fatal("invalid <maxid> argument; should be a fractional identity in range [0,1]"); |
| 63 |
|
| 64 |
if (esl_opt_GetBoolean(go, "--rna")) abc = esl_alphabet_Create(eslRNA); |
| 65 |
else if (esl_opt_GetBoolean(go, "--dna")) abc = esl_alphabet_Create(eslDNA); |
| 66 |
else if (esl_opt_GetBoolean(go, "--amino")) abc = esl_alphabet_Create(eslAMINO); |
| 67 |
|
| 68 |
if (esl_opt_IsOn(go, "--informat") && |
| 69 |
(infmt = esl_msafile_EncodeFormat(esl_opt_GetString(go, "--informat"))) == eslMSAFILE_UNKNOWN) |
| 70 |
esl_fatal("%s is not a valid MSA file format for --informat", esl_opt_GetString(go, "--informat")); |
| 71 |
|
| 72 |
cfg->fragthresh = esl_opt_GetReal (go, "--fragthresh"); |
| 73 |
cfg->symfrac = esl_opt_GetReal (go, "--symfrac"); |
| 74 |
cfg->ignore_rf = esl_opt_GetBoolean(go, "--ignore-rf"); |
| 75 |
cfg->allow_samp = !esl_opt_GetBoolean(go, "--no-sampling"); |
| 76 |
cfg->sampthresh = esl_opt_GetInteger(go, "--sampthresh"); |
| 77 |
cfg->nsamp = esl_opt_GetInteger(go, "--nsamp"); |
| 78 |
cfg->maxfrag = esl_opt_GetInteger(go, "--maxfrag"); |
| 79 |
cfg->seed = esl_opt_GetInteger(go, "-s"); |
| 80 |
|
| 81 |
if (esl_opt_GetBoolean(go, "--conscover")) cfg->filterpref = eslMSAWEIGHT_FILT_CONSCOVER; |
| 82 |
else if (esl_opt_GetBoolean(go, "--randorder")) cfg->filterpref = eslMSAWEIGHT_FILT_RANDOM; |
| 83 |
else if (esl_opt_GetBoolean(go, "--origorder")) cfg->filterpref = eslMSAWEIGHT_FILT_ORIGORDER; |
| 84 |
|
| 85 |
if ((status = esl_msafile_Open(&abc, msafile, NULL, infmt, NULL, &afp)) != eslOK) |
| 86 |
esl_msafile_OpenFailure(afp, status); |
| 87 |
|
| 88 |
outfmt = afp->format; |
| 89 |
if ( esl_opt_IsOn(go, "--outformat") && |
| 90 |
(outfmt = esl_msafile_EncodeFormat(esl_opt_GetString(go, "--outformat"))) == eslMSAFILE_UNKNOWN) |
| 91 |
esl_fatal("%s is not a valid MSA file format for --outformat", esl_opt_GetString(go, "--outformat")); |
| 92 |
|
| 93 |
ofp = (esl_opt_GetString (go, "-o") == NULL ? stdout : fopen(esl_opt_GetString(go, "-o"), "w")); |
| 94 |
if (! ofp) esl_fatal("Failed to open output file %s\n", esl_opt_GetString(go, "-o")); |
| 95 |
|
| 96 |
while ((status = esl_msafile_Read(afp, &msa)) == eslOK) |
| 97 |
{ |
| 98 |
nali++; |
| 99 |
|
| 100 |
if (( status = esl_msaweight_IDFilter_adv(cfg, msa, maxid, &msa2)) != eslOK) |
| 101 |
esl_fatal("%id filtering function failed"); |
| 102 |
|
| 103 |
if (( status = esl_msafile_Write(ofp, msa2, outfmt)) != eslOK) |
| 104 |
esl_fatal("sequence alignment write failed"); |
| 105 |
|
| 106 |
esl_msa_Destroy(msa); |
| 107 |
esl_msa_Destroy(msa2); |
| 108 |
} |
| 109 |
if (nali == 0 || status != eslEOF) esl_msafile_ReadFailure(afp, status); |
| 110 |
|
| 111 |
if (ofp != stdout) fclose(ofp); |
| 112 |
esl_msaweight_cfg_Destroy(cfg); |
| 113 |
esl_alphabet_Destroy(abc); |
| 114 |
esl_msafile_Close(afp); |
| 115 |
esl_getopts_Destroy(go); |
| 116 |
return eslOK; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
static ESL_GETOPTS * |
| 125 |
process_cmdline(const char *topcmd, const ESL_SUBCMD *sub, const ESL_OPTIONS *suboptions, int argc, char **argv) |
| 126 |
{ |
| 127 |
ESL_GETOPTS *go = esl_getopts_Create(suboptions); |
| 128 |
char *lastslash = strrchr(topcmd, '/'); |
| 129 |
|
| 130 |
if (lastslash) topcmd = lastslash+1; |
| 131 |
|
| 132 |
if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK || |
| 133 |
esl_opt_VerifyConfig(go) != eslOK) |
| 134 |
{ |
| 135 |
if ( esl_printf("Failed to parse command line: %s\n", go->errbuf) != eslOK) goto ERROR; |
| 136 |
if ( esl_printf("Usage:\n %s %s %s\n", topcmd, sub->subcmd, sub->usage) != eslOK) goto ERROR; |
| 137 |
if ( esl_printf("\nTo see more help on available options, do `%s %s -h`\n\n", topcmd, sub->subcmd) != eslOK) goto ERROR; |
| 138 |
exit(1); |
| 139 |
} |
| 140 |
if (esl_opt_GetBoolean(go, "-h") == TRUE) |
| 141 |
{ |
| 142 |
if ( esl_printf("%s %s :: %s\n", topcmd, sub->subcmd, sub->description) != eslOK) goto ERROR; |
| 143 |
if ( esl_printf("\nUsage:\n %s %s %s\n", topcmd, sub->subcmd, sub->usage) != eslOK) goto ERROR; |
| 144 |
if ( esl_printf("\nOptions:\n") != eslOK) goto ERROR; |
| 145 |
if ( esl_opt_DisplayHelp(stdout, go, 1, 2, 80) != eslOK) goto ERROR; |
| 146 |
if ( esl_printf("\noptions for deriving consensus:\n") != eslOK) goto ERROR; |
| 147 |
if ( esl_opt_DisplayHelp(stdout, go, 2, 2, 80) != eslOK) goto ERROR; |
| 148 |
if ( esl_printf("\noptions for deriving consensus by sampling (on deep MSAs):\n") != eslOK) goto ERROR; |
| 149 |
if ( esl_opt_DisplayHelp(stdout, go, 3, 2, 80) != eslOK) goto ERROR; |
| 150 |
if ( esl_printf("\noptions for sequence preference:\n") != eslOK) goto ERROR; |
| 151 |
if ( esl_opt_DisplayHelp(stdout, go, 4, 2, 80) != eslOK) goto ERROR; |
| 152 |
exit(0); |
| 153 |
} |
| 154 |
if (esl_opt_ArgNumber(go) != sub->nargs) |
| 155 |
{ |
| 156 |
if ( esl_printf("Incorrect number of command line arguments.\n") != eslOK) goto ERROR; |
| 157 |
if ( esl_printf("Usage:\n %s %s %s\n", topcmd, sub->subcmd, sub->usage) != eslOK) goto ERROR; |
| 158 |
if ( esl_printf("\nTo see more help on available options, do `%s %s -h`\n\n", topcmd, sub->subcmd) != eslOK) goto ERROR; |
| 159 |
exit(1); |
| 160 |
} |
| 161 |
return go; |
| 162 |
|
| 163 |
ERROR: |
| 164 |
esl_getopts_Destroy(go); |
| 165 |
return NULL; |
| 166 |
} |