| 1 |
|
| 2 |
|
| 3 |
#include <p7_config.h> |
| 4 |
|
| 5 |
#include <stdio.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include "easel.h" |
| 10 |
#include "esl_alphabet.h" |
| 11 |
#include "esl_getopts.h" |
| 12 |
#include "esl_msa.h" |
| 13 |
#include "esl_msafile.h" |
| 14 |
#include "esl_sq.h" |
| 15 |
#include "esl_sqio.h" |
| 16 |
#include "esl_vectorops.h" |
| 17 |
|
| 18 |
#include "hmmer.h" |
| 19 |
|
| 20 |
static int map_alignment(const char *msafile, const P7_HMM *hmm, ESL_SQ ***ret_sq, P7_TRACE ***ret_tr, int *ret_ntot); |
| 21 |
|
| 22 |
|
| 23 |
#define ALPHOPTS "--amino,--dna,--rna" |
| 24 |
|
| 25 |
static ESL_OPTIONS options[] = { |
| 26 |
|
| 27 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 1 }, |
| 28 |
{ "-o", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "output alignment to file <f>, not stdout", 1 }, |
| 29 |
|
| 30 |
{ "--mapali", eslARG_INFILE, NULL, NULL, NULL, NULL, NULL, NULL, "include alignment in file <f> (same ali that HMM came from)", 2 }, |
| 31 |
{ "--trim", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "trim terminal tails of nonaligned residues from alignment", 2 }, |
| 32 |
{ "--amino", eslARG_NONE, FALSE, NULL, NULL, ALPHOPTS, NULL, NULL, "assert <seqfile>, <hmmfile> both protein: no autodetection", 2 }, |
| 33 |
{ "--dna", eslARG_NONE, FALSE, NULL, NULL, ALPHOPTS, NULL, NULL, "assert <seqfile>, <hmmfile> both DNA: no autodetection", 2 }, |
| 34 |
{ "--rna", eslARG_NONE, FALSE, NULL, NULL, ALPHOPTS, NULL, NULL, "assert <seqfile>, <hmmfile> both RNA: no autodetection", 2 }, |
| 35 |
{ "--informat", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "assert <seqfile> is in format <s>: no autodetection", 2 }, |
| 36 |
{ "--outformat", eslARG_STRING, "Stockholm", NULL, NULL, NULL, NULL, NULL, "output alignment in format <s>", 2 }, |
| 37 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 38 |
}; |
| 39 |
|
| 40 |
static char usage[] = "[-options] <hmmfile> <seqfile>"; |
| 41 |
static char banner[] = "align sequences to a profile HMM"; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
static void |
| 47 |
cmdline_failure(char *argv0, char *format, ...) |
| 48 |
{ |
| 49 |
va_list argp; |
| 50 |
printf("\nERROR: "); |
| 51 |
va_start(argp, format); |
| 52 |
vfprintf(stdout, format, argp); |
| 53 |
va_end(argp); |
| 54 |
esl_usage(stdout, argv0, usage); |
| 55 |
printf("\nTo see more help on available options, do %s -h\n\n", argv0); |
| 56 |
exit(1); |
| 57 |
} |
| 58 |
|
| 59 |
static void |
| 60 |
cmdline_help(char *argv0, ESL_GETOPTS *go) |
| 61 |
{ |
| 62 |
p7_banner (stdout, argv0, banner); |
| 63 |
esl_usage (stdout, argv0, usage); |
| 64 |
puts("\nBasic options:"); |
| 65 |
esl_opt_DisplayHelp(stdout, go, 1, 2, 80); |
| 66 |
puts("\nLess common options:"); |
| 67 |
esl_opt_DisplayHelp(stdout, go, 2, 2, 80); |
| 68 |
puts("\nSequence input formats include: FASTA, EMBL, GenBank, UniProt"); |
| 69 |
puts("Alignment output formats include: Stockholm, Pfam, A2M, PSIBLAST\n"); |
| 70 |
exit(0); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
int |
| 76 |
main(int argc, char **argv) |
| 77 |
{ |
| 78 |
ESL_GETOPTS *go = NULL; |
| 79 |
char *hmmfile = NULL; |
| 80 |
char *seqfile = NULL; |
| 81 |
char *mapfile = NULL; |
| 82 |
int infmt = eslSQFILE_UNKNOWN; |
| 83 |
int outfmt = eslMSAFILE_STOCKHOLM; |
| 84 |
P7_HMMFILE *hfp = NULL; |
| 85 |
ESL_SQFILE *sqfp = NULL; |
| 86 |
char *outfile = NULL; |
| 87 |
FILE *ofp = stdout; |
| 88 |
ESL_SQ **sq = NULL; |
| 89 |
void *p = NULL; |
| 90 |
int nseq = 0; |
| 91 |
int mapseq = 0; |
| 92 |
int totseq = 0; |
| 93 |
ESL_ALPHABET *abc = NULL; |
| 94 |
P7_HMM *hmm = NULL; |
| 95 |
P7_TRACE **tr = NULL; |
| 96 |
ESL_MSA *msa = NULL; |
| 97 |
int msaopts = 0; |
| 98 |
int idx; |
| 99 |
int status; |
| 100 |
char errbuf[eslERRBUFSIZE]; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
go = esl_getopts_Create(options); |
| 105 |
if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) cmdline_failure(argv[0], "Failed to parse command line: %s\n", go->errbuf); |
| 106 |
if (esl_opt_VerifyConfig(go) != eslOK) cmdline_failure(argv[0], "Error in configuration: %s\n", go->errbuf); |
| 107 |
if (esl_opt_GetBoolean(go, "-h") ) cmdline_help (argv[0], go); |
| 108 |
if (esl_opt_ArgNumber(go) != 2) cmdline_failure(argv[0], "Incorrect number of command line arguments.\n"); |
| 109 |
|
| 110 |
hmmfile = esl_opt_GetArg(go, 1); |
| 111 |
seqfile = esl_opt_GetArg(go, 2); |
| 112 |
|
| 113 |
if (strcmp(hmmfile, "-") == 0 && strcmp(seqfile, "-") == 0) |
| 114 |
cmdline_failure(argv[0], "Either <hmmfile> or <seqfile> may be '-' (to read from stdin), but not both.\n"); |
| 115 |
|
| 116 |
msaopts |= p7_ALL_CONSENSUS_COLS; |
| 117 |
if (esl_opt_GetBoolean(go, "--trim")) msaopts |= p7_TRIM; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
if (esl_opt_IsOn(go, "--informat")) { |
| 122 |
infmt = esl_sqio_EncodeFormat(esl_opt_GetString(go, "--informat")); |
| 123 |
if (infmt == eslSQFILE_UNKNOWN) cmdline_failure(argv[0], "%s is not a recognized input sequence file format\n", esl_opt_GetString(go, "--informat")); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
outfmt = esl_msafile_EncodeFormat(esl_opt_GetString(go, "--outformat")); |
| 128 |
if (outfmt == eslMSAFILE_UNKNOWN) cmdline_failure(argv[0], "%s is not a recognized output MSA file format\n", esl_opt_GetString(go, "--outformat")); |
| 129 |
|
| 130 |
|
| 131 |
if ( (outfile = esl_opt_GetString(go, "-o")) != NULL) |
| 132 |
{ |
| 133 |
if ((ofp = fopen(outfile, "w")) == NULL) |
| 134 |
cmdline_failure(argv[0], "failed to open -o output file %s for writing\n", outfile); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
if (esl_opt_GetBoolean(go, "--amino")) abc = esl_alphabet_Create(eslAMINO); |
| 141 |
else if (esl_opt_GetBoolean(go, "--dna")) abc = esl_alphabet_Create(eslDNA); |
| 142 |
else if (esl_opt_GetBoolean(go, "--rna")) abc = esl_alphabet_Create(eslRNA); |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
status = p7_hmmfile_Open(hmmfile, NULL, &hfp, errbuf); |
| 147 |
if (status == eslENOTFOUND) p7_Fail("File existence/permissions problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf); |
| 148 |
else if (status == eslEFORMAT) p7_Fail("File format problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf); |
| 149 |
else if (status != eslOK) p7_Fail("Unexpected error %d in opening HMM file %s.\n%s\n", status, hmmfile, errbuf); |
| 150 |
|
| 151 |
status = p7_hmmfile_Read(hfp, &abc, &hmm); |
| 152 |
if (status == eslEFORMAT) p7_Fail("Bad file format in HMM file %s:\n%s\n", hfp->fname, hfp->errbuf); |
| 153 |
else if (status == eslEINCOMPAT) p7_Fail("HMM in %s is not in the expected %s alphabet\n", hfp->fname, esl_abc_DecodeType(abc->type)); |
| 154 |
else if (status == eslEOF) p7_Fail("Empty HMM file %s? No HMM data found.\n", hfp->fname); |
| 155 |
else if (status != eslOK) p7_Fail("Unexpected error in reading HMMs from %s\n", hfp->fname); |
| 156 |
|
| 157 |
status = p7_hmmfile_Read(hfp, &abc, NULL); |
| 158 |
if (status != eslEOF) p7_Fail("HMM file %s does not contain just one HMM\n", hfp->fname); |
| 159 |
p7_hmmfile_Close(hfp); |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
if ( (mapfile = esl_opt_GetString(go, "--mapali")) != NULL) |
| 166 |
{ |
| 167 |
map_alignment(mapfile, hmm, &sq, &tr, &mapseq); |
| 168 |
} |
| 169 |
totseq = mapseq; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
status = esl_sqfile_OpenDigital(abc, seqfile, infmt, NULL, &sqfp); |
| 174 |
if (status == eslENOTFOUND) p7_Fail("Failed to open sequence file %s for reading\n", seqfile); |
| 175 |
else if (status == eslEFORMAT) p7_Fail("Sequence file %s is empty or misformatted\n", seqfile); |
| 176 |
else if (status != eslOK) p7_Fail("Unexpected error %d opening sequence file %s\n", status, seqfile); |
| 177 |
|
| 178 |
ESL_RALLOC(sq, p, sizeof(ESL_SQ *) * (totseq + 1)); |
| 179 |
sq[totseq] = esl_sq_CreateDigital(abc); |
| 180 |
nseq = 0; |
| 181 |
while ((status = esl_sqio_Read(sqfp, sq[totseq+nseq])) == eslOK) |
| 182 |
{ |
| 183 |
nseq++; |
| 184 |
ESL_RALLOC(sq, p, sizeof(ESL_SQ *) * (totseq+nseq+1)); |
| 185 |
sq[totseq+nseq] = esl_sq_CreateDigital(abc); |
| 186 |
} |
| 187 |
if (status == eslEFORMAT) esl_fatal("Parse failed (sequence file %s):\n%s\n", |
| 188 |
sqfp->filename, esl_sqfile_GetErrorBuf(sqfp)); |
| 189 |
else if (status != eslEOF) esl_fatal("Unexpected error %d reading sequence file %s", status, sqfp->filename); |
| 190 |
esl_sqfile_Close(sqfp); |
| 191 |
totseq += nseq; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
ESL_RALLOC(tr, p, sizeof(P7_TRACE *) * totseq); |
| 197 |
for (idx = mapseq; idx < totseq; idx++) |
| 198 |
tr[idx] = p7_trace_CreateWithPP(); |
| 199 |
|
| 200 |
p7_tracealign_computeTraces(hmm, sq, mapseq, totseq - mapseq, tr); |
| 201 |
|
| 202 |
p7_tracealign_Seqs(sq, tr, totseq, hmm->M, msaopts, hmm, &msa); |
| 203 |
|
| 204 |
esl_msafile_Write(ofp, msa, outfmt); |
| 205 |
|
| 206 |
for (idx = 0; idx <= totseq; idx++) esl_sq_Destroy(sq[idx]); |
| 207 |
for (idx = 0; idx < totseq; idx++) p7_trace_Destroy(tr[idx]); |
| 208 |
free(sq); |
| 209 |
free(tr); |
| 210 |
esl_msa_Destroy(msa); |
| 211 |
p7_hmm_Destroy(hmm); |
| 212 |
if (ofp != stdout) fclose(ofp); |
| 213 |
esl_alphabet_Destroy(abc); |
| 214 |
esl_getopts_Destroy(go); |
| 215 |
return eslOK; |
| 216 |
|
| 217 |
ERROR: |
| 218 |
return status; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
static int |
| 228 |
map_alignment(const char *msafile, const P7_HMM *hmm, ESL_SQ ***ret_sq, P7_TRACE ***ret_tr, int *ret_ntot) |
| 229 |
{ |
| 230 |
ESL_SQ **sq = NULL; |
| 231 |
P7_TRACE **tr = NULL; |
| 232 |
ESL_MSAFILE *afp = NULL; |
| 233 |
ESL_MSA *msa = NULL; |
| 234 |
ESL_ALPHABET *abc = (ESL_ALPHABET *) hmm->abc; |
| 235 |
int *matassign = NULL; |
| 236 |
uint32_t chksum = 0; |
| 237 |
int i,k; |
| 238 |
int status; |
| 239 |
|
| 240 |
status = esl_msafile_Open(&abc, msafile, NULL, eslMSAFILE_UNKNOWN, NULL, &afp); |
| 241 |
if (status != eslOK) esl_msafile_OpenFailure(afp, status); |
| 242 |
|
| 243 |
status = esl_msafile_Read(afp, &msa); |
| 244 |
if (status != eslOK) esl_msafile_ReadFailure(afp, status); |
| 245 |
|
| 246 |
if (! (hmm->flags & p7H_CHKSUM) ) esl_fatal("HMM has no checksum. --mapali unreliable without it."); |
| 247 |
if (! (hmm->flags & p7H_MAP) ) esl_fatal("HMM has no map. --mapali can't work without it."); |
| 248 |
esl_msa_Checksum(msa, &chksum); |
| 249 |
if (hmm->checksum != chksum) esl_fatal("--mapali MSA %s isn't same as the one HMM came from (checksum mismatch)", msafile); |
| 250 |
|
| 251 |
ESL_ALLOC(sq, sizeof(ESL_SQ *) * msa->nseq); |
| 252 |
ESL_ALLOC(tr, sizeof(P7_TRACE *) * msa->nseq); |
| 253 |
ESL_ALLOC(matassign, sizeof(int) * (msa->alen + 1)); |
| 254 |
|
| 255 |
esl_vec_ISet(matassign, msa->alen+1, 0); |
| 256 |
for (k = 1; k <= hmm->M; k++) matassign[hmm->map[k]] = 1; |
| 257 |
|
| 258 |
p7_trace_FauxFromMSA(msa, matassign, p7_DEFAULT, tr); |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
for (i = 0; i < msa->nseq; i++) |
| 268 |
esl_sq_FetchFromMSA(msa, i, &(sq[i])); |
| 269 |
|
| 270 |
*ret_ntot = msa->nseq; |
| 271 |
*ret_tr = tr; |
| 272 |
*ret_sq = sq; |
| 273 |
|
| 274 |
esl_msafile_Close(afp); |
| 275 |
esl_msa_Destroy(msa); |
| 276 |
free(matassign); |
| 277 |
return eslOK; |
| 278 |
|
| 279 |
ERROR: |
| 280 |
*ret_ntot = 0; |
| 281 |
*ret_tr = NULL; |
| 282 |
*ret_sq = NULL; |
| 283 |
if (afp != NULL) esl_msafile_Close(afp); |
| 284 |
if (msa != NULL) esl_msa_Destroy(msa); |
| 285 |
if (matassign != NULL) free(matassign); |
| 286 |
return status; |
| 287 |
} |
| 288 |
|