| 1 |
|
| 2 |
|
| 3 |
#include <esl_config.h> |
| 4 |
|
| 5 |
#include <stdlib.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include <string.h> |
| 8 |
#include <time.h> |
| 9 |
|
| 10 |
#include "easel.h" |
| 11 |
#include "esl_getopts.h" |
| 12 |
#include "esl_mem.h" |
| 13 |
#include "esl_msa.h" |
| 14 |
#include "esl_msafile.h" |
| 15 |
#include "esl_sq.h" |
| 16 |
#include "esl_sqio.h" |
| 17 |
#include "esl_wuss.h" |
| 18 |
|
| 19 |
static char banner[] = "convert between sequence file formats"; |
| 20 |
|
| 21 |
static char usage[] = "[-options] <format> <seqfile>\n\ |
| 22 |
Output format choices: Unaligned Aligned \n\ |
| 23 |
----------- ------- \n\ |
| 24 |
fasta a2m \n\ |
| 25 |
hmmpgmd afa \n\ |
| 26 |
clustal \n\ |
| 27 |
clustallike\n\ |
| 28 |
pfam \n\ |
| 29 |
phylip \n\ |
| 30 |
phylips \n\ |
| 31 |
psiblast \n\ |
| 32 |
selex \n\ |
| 33 |
stockholm \n\ |
| 34 |
\n"; |
| 35 |
|
| 36 |
#define INCOMPATWITHSMALLOPT "--mingap,--nogap,--ignore,--acceptx" |
| 37 |
|
| 38 |
static ESL_OPTIONS options[] = { |
| 39 |
|
| 40 |
{ "-d", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-r", "convert to DNA alphabet (U->T)", 0 }, |
| 41 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "help; print brief info on version and usage", 0 }, |
| 42 |
{ "-l", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-u", "convert to lower case", 0 }, |
| 43 |
{ "-n", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-x", "remove DNA IUPAC codes; convert ambig chars to N", 0 }, |
| 44 |
{ "-o", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "send output to file <f>, not stdout", 0 }, |
| 45 |
{ "-r", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-d", "convert to RNA alphabet (T->U)", 0 }, |
| 46 |
{ "-u", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-l", "convert to upper case", 0 }, |
| 47 |
{ "-x", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-n", "convert non-IUPAC chars (e.g. X) in DNA to N", 0 }, |
| 48 |
{ "--gapsym", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, "--mingap,--nogap", "convert all gaps to character <s>", 0 }, |
| 49 |
{ "--informat", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "input sequence file is in format <s>", 0 }, |
| 50 |
{ "--mingap", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--nogap", "remove columns containing all gaps (seqfile=MSA)", 0 }, |
| 51 |
{ "--keeprf", eslARG_NONE, FALSE, NULL, NULL, NULL, "--mingap", NULL, "with --mingap, keep all nongap #=GC RF columns", 0 }, |
| 52 |
{ "--nogap", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--mingap,--gapsym", "remove columns containing any gaps (seqfile=MSA)", 0 }, |
| 53 |
{ "--wussify", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--dewuss,--fullwuss", "convert old RNA structure markup lines to WUSS", 0 }, |
| 54 |
{ "--dewuss", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--wussify,--fullwuss","convert WUSS RNA structure markup to old format", 0 }, |
| 55 |
{ "--fullwuss", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--wussify,--dewuss", "convert simple WUSS notation to full (output) WUSS", 0 }, |
| 56 |
{ "--ignore", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "ignore input seq characters listed in string <s>", 0 }, |
| 57 |
{ "--acceptx", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "accept input seq chars in string <s> as X", 0 }, |
| 58 |
{ "--rename", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "rename and number each sequence <s>.<n>", 0 }, |
| 59 |
{ "--replace", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "<s> = <s1>:<s2> replace characters in <s1> with those in <s2>", 0}, |
| 60 |
{ "--small", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, INCOMPATWITHSMALLOPT, "use minimal RAM, input must be pfam, output must be afa or pfam",0 }, |
| 61 |
{ "--id_map", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "if format is hmmpgmd, put the id map into file <s>", 0 }, |
| 62 |
{ "--namelen", eslARG_INT, NULL, NULL,"n>0", NULL, NULL, NULL, "for PHYLIP formats, set name len to <n> not 10", 0 }, |
| 63 |
{ 0,0,0,0,0,0,0,0 }, |
| 64 |
}; |
| 65 |
|
| 66 |
static void symconvert(char *s, char *oldsyms, char *newsyms); |
| 67 |
static void regurgitate_pfam_as_afa(ESL_MSAFILE *afp, FILE *ofp, char *alifile, char *gapsym, int force_lower, int force_upper, |
| 68 |
int force_rna, int force_dna, int iupac_to_n, int x_is_bad, char *rename, char *rfrom, |
| 69 |
char *rto, int *ret_reached_eof); |
| 70 |
static int regurgitate_pfam_as_pfam(ESL_MSAFILE *afp, FILE *ofp, char *gapsym, int force_lower, int force_upper, int force_rna, |
| 71 |
int force_dna, int iupac_to_n, int x_is_bad, int wussify, int dewuss, int fullwuss, |
| 72 |
char *rfrom, char *rto); |
| 73 |
static int parse_replace_string(const char *rstring, char **ret_from, char **ret_to); |
| 74 |
|
| 75 |
int |
| 76 |
main(int argc, char **argv) |
| 77 |
{ |
| 78 |
ESL_GETOPTS *go; |
| 79 |
char *infile; |
| 80 |
int infmt = eslSQFILE_UNKNOWN; |
| 81 |
int outfmt = eslSQFILE_UNKNOWN; |
| 82 |
int status; |
| 83 |
FILE *ofp; |
| 84 |
|
| 85 |
char *outfile; |
| 86 |
int force_rna; |
| 87 |
int force_dna; |
| 88 |
int force_lower; |
| 89 |
int force_upper; |
| 90 |
int iupac_to_n; |
| 91 |
int x_is_bad; |
| 92 |
int do_mingap; |
| 93 |
int do_nogap; |
| 94 |
char *gapsym; |
| 95 |
int wussify; |
| 96 |
int dewuss; |
| 97 |
int fullwuss; |
| 98 |
char *rename; |
| 99 |
int do_small; |
| 100 |
int do_fixbps; |
| 101 |
char *rstring; |
| 102 |
char *rfrom, *rto; |
| 103 |
int reached_eof; |
| 104 |
int idx; |
| 105 |
int nali; |
| 106 |
int phylip_namelen = -1; |
| 107 |
char errbuf[eslERRBUFSIZE]; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
go = esl_getopts_Create(options); |
| 116 |
if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK || |
| 117 |
esl_opt_VerifyConfig(go) != eslOK) |
| 118 |
{ |
| 119 |
printf("Failed to parse command line: %s\n", go->errbuf); |
| 120 |
esl_usage(stdout, argv[0], usage); |
| 121 |
printf("\nTo see more help on available options, do %s -h\n\n", argv[0]); |
| 122 |
exit(1); |
| 123 |
} |
| 124 |
|
| 125 |
if (esl_opt_GetBoolean(go, "-h")) |
| 126 |
{ |
| 127 |
esl_banner(stdout, argv[0], banner); |
| 128 |
esl_usage (stdout, argv[0], usage); |
| 129 |
puts(" where options are:\n"); |
| 130 |
esl_opt_DisplayHelp(stdout, go, 0, 2, 80); |
| 131 |
exit(0); |
| 132 |
} |
| 133 |
|
| 134 |
if (esl_opt_ArgNumber(go) != 2) |
| 135 |
{ |
| 136 |
printf("Incorrect number of command line arguments.\n"); |
| 137 |
esl_usage(stdout, argv[0], usage); |
| 138 |
printf("\nTo see more help on available options, do %s -h\n\n", argv[0]); |
| 139 |
exit(1); |
| 140 |
} |
| 141 |
|
| 142 |
outfmt = esl_sqio_EncodeFormat(esl_opt_GetArg(go, 1)); |
| 143 |
if (outfmt == eslSQFILE_UNKNOWN) |
| 144 |
esl_fatal("%s is not a recognized output seqfile format\n", esl_opt_GetArg(go, 1)); |
| 145 |
|
| 146 |
infile = esl_opt_GetArg(go, 2); |
| 147 |
|
| 148 |
if (esl_opt_IsOn(go, "--informat") && |
| 149 |
(infmt = esl_sqio_EncodeFormat(esl_opt_GetString( go, "--informat"))) == eslSQFILE_UNKNOWN) |
| 150 |
esl_fatal("%s is not a recognized input seqfile format\n", esl_opt_GetString(go, "--informat")); |
| 151 |
|
| 152 |
force_dna = esl_opt_GetBoolean(go, "-d"); |
| 153 |
force_lower = esl_opt_GetBoolean(go, "-l"); |
| 154 |
iupac_to_n = esl_opt_GetBoolean(go, "-n"); |
| 155 |
outfile = esl_opt_GetString (go, "-o"); |
| 156 |
force_rna = esl_opt_GetBoolean(go, "-r"); |
| 157 |
force_upper = esl_opt_GetBoolean(go, "-u"); |
| 158 |
x_is_bad = esl_opt_GetBoolean(go, "-x"); |
| 159 |
gapsym = esl_opt_GetString( go, "--gapsym"); |
| 160 |
do_mingap = esl_opt_GetBoolean(go, "--mingap"); |
| 161 |
do_nogap = esl_opt_GetBoolean(go, "--nogap"); |
| 162 |
wussify = esl_opt_GetBoolean(go, "--wussify"); |
| 163 |
dewuss = esl_opt_GetBoolean(go, "--dewuss"); |
| 164 |
fullwuss = esl_opt_GetBoolean(go, "--fullwuss"); |
| 165 |
rename = esl_opt_GetString (go, "--rename"); |
| 166 |
do_small = esl_opt_GetBoolean(go, "--small"); |
| 167 |
rstring = esl_opt_GetString( go, "--replace"); |
| 168 |
do_fixbps = (force_rna || force_dna || wussify || dewuss || fullwuss) ? TRUE : FALSE; |
| 169 |
|
| 170 |
if (esl_opt_IsOn(go, "--namelen")) |
| 171 |
phylip_namelen = esl_opt_GetInteger(go, "--namelen"); |
| 172 |
|
| 173 |
|
| 174 |
if(do_small && (infmt != eslMSAFILE_PFAM || (outfmt != eslMSAFILE_AFA && outfmt != eslMSAFILE_PFAM))) |
| 175 |
esl_fatal("--small requires '--informat pfam' and output format of either 'afa' or 'pfam'"); |
| 176 |
|
| 177 |
if (gapsym != NULL && strlen(gapsym) != 1) |
| 178 |
esl_fatal("Argument to --gapsym must be a single character."); |
| 179 |
|
| 180 |
if (outfile == NULL) ofp = stdout; |
| 181 |
else if ((ofp = fopen(outfile, "w")) == NULL) |
| 182 |
esl_fatal("Failed to open output file %s\n", outfile); |
| 183 |
|
| 184 |
if (rstring == NULL) { |
| 185 |
rfrom = NULL; |
| 186 |
rto = NULL; |
| 187 |
} |
| 188 |
else { |
| 189 |
if((status = parse_replace_string(rstring, &rfrom, &rto)) != eslOK) esl_fatal("Out of memory"); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
if (esl_sqio_IsAlignment(outfmt)) |
| 200 |
{ |
| 201 |
ESL_MSAFILE *afp; |
| 202 |
ESL_MSA *msa; |
| 203 |
|
| 204 |
status = esl_msafile_Open(NULL, infile, NULL, infmt, NULL, &afp); |
| 205 |
if (status != eslOK) esl_msafile_OpenFailure(afp, status); |
| 206 |
|
| 207 |
if ( esl_opt_IsOn(go, "--ignore")) esl_fatal("The --ignore option is unimplemented for alignment reformatting."); |
| 208 |
if ( esl_opt_IsOn(go, "--acceptx")) esl_fatal("The --acceptx option is unimplemented for alignment reformatting."); |
| 209 |
|
| 210 |
nali = 0; |
| 211 |
|
| 212 |
if (do_small) { |
| 213 |
if(infmt == eslMSAFILE_PFAM && outfmt == eslMSAFILE_AFA) { |
| 214 |
if (afp->bf->mode_is == eslBUFFER_STREAM) esl_fatal("--small with afa out format and stdin input is unimplemented."); |
| 215 |
regurgitate_pfam_as_afa(afp, ofp, infile, gapsym, force_lower, force_upper, force_rna, force_dna, iupac_to_n, x_is_bad, rename, rfrom, rto, &reached_eof); |
| 216 |
if(! reached_eof) esl_fatal("Input file contains >1 alignments, but afa formatted output file can only contain 1"); |
| 217 |
} |
| 218 |
else if (infmt == eslMSAFILE_PFAM && outfmt == eslMSAFILE_PFAM) { |
| 219 |
if(rename != NULL) esl_fatal("--rename is unimplemented for combination of --small and output format pfam"); |
| 220 |
while((status = regurgitate_pfam_as_pfam(afp, ofp, gapsym, force_lower, force_upper, force_rna, force_dna, iupac_to_n, x_is_bad, wussify, dewuss, fullwuss, rfrom, rto)) != eslEOF) { |
| 221 |
if (status == eslEFORMAT) esl_fatal("--small alignment file parse error:\n%s\n", afp->errmsg); |
| 222 |
else if (status == eslEINVAL) esl_fatal("--small alignment file parse error:\n%s\n", afp->errmsg); |
| 223 |
else if (status != eslOK) esl_fatal("--small alignment file read failed with error code %d\n", status); |
| 224 |
} |
| 225 |
esl_msafile_Close(afp); |
| 226 |
} |
| 227 |
else { |
| 228 |
esl_fatal("--small requires '--informat pfam' and output format of either 'afa' or 'pfam'"); |
| 229 |
} |
| 230 |
} |
| 231 |
else { |
| 232 |
while ((status = esl_msafile_Read(afp, &msa)) != eslEOF) |
| 233 |
{ |
| 234 |
if (status != eslOK) esl_msafile_ReadFailure(afp, status); |
| 235 |
nali++; |
| 236 |
|
| 237 |
if (nali > 1 && ! esl_msafile_IsMultiRecord(outfmt)) |
| 238 |
esl_fatal("Input file contains >1 alignments, but %s formatted output file can only contain 1", esl_msafile_DecodeFormat(outfmt)); |
| 239 |
|
| 240 |
if (do_mingap) if((status = esl_msa_MinimGapsText(msa, errbuf, "-_.~", esl_opt_GetBoolean(go, "--keeprf"), do_fixbps)) != eslOK) esl_fatal(errbuf); |
| 241 |
if (do_nogap) if((status = esl_msa_NoGapsText (msa, errbuf, "-_.~", do_fixbps)) != eslOK) esl_fatal(errbuf); |
| 242 |
if (rfrom) esl_msa_SymConvert(msa, rfrom, rto); |
| 243 |
if (gapsym) esl_msa_SymConvert(msa, "-_.", gapsym); |
| 244 |
if (force_lower) esl_msa_SymConvert(msa, |
| 245 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ", |
| 246 |
"abcdefghijklmnopqrstuvwxyz"); |
| 247 |
if (force_upper) esl_msa_SymConvert(msa, |
| 248 |
"abcdefghijklmnopqrstuvwxyz", |
| 249 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"); |
| 250 |
if (force_rna) esl_msa_SymConvert(msa, "Tt", "Uu"); |
| 251 |
if (force_dna) esl_msa_SymConvert(msa, "Uu", "Tt"); |
| 252 |
if (iupac_to_n) esl_msa_SymConvert(msa, |
| 253 |
"RYMKSWHBVDrymkswhbvd", |
| 254 |
"NNNNNNNNNNnnnnnnnnnn"); |
| 255 |
if (x_is_bad) esl_msa_SymConvert(msa, "Xx", "Nn"); |
| 256 |
|
| 257 |
if (rename) |
| 258 |
{ |
| 259 |
for (idx = 0; idx < msa->nseq; idx++) |
| 260 |
esl_msa_FormatSeqName(msa, idx, "%s.%d", rename, idx+1); |
| 261 |
} |
| 262 |
|
| 263 |
if (wussify) |
| 264 |
{ |
| 265 |
if (msa->ss_cons) esl_kh2wuss(msa->ss_cons, msa->ss_cons); |
| 266 |
if (msa->ss) |
| 267 |
for (idx = 0; idx < msa->nseq; idx++) |
| 268 |
if (msa->ss[idx]) esl_kh2wuss(msa->ss[idx], msa->ss[idx]); |
| 269 |
} |
| 270 |
|
| 271 |
if (dewuss) |
| 272 |
{ |
| 273 |
if (msa->ss_cons) esl_wuss2kh(msa->ss_cons, msa->ss_cons); |
| 274 |
if (msa->ss) |
| 275 |
for (idx = 0; idx < msa->nseq; idx++) |
| 276 |
if (msa->ss[idx]) esl_wuss2kh(msa->ss[idx], msa->ss[idx]); |
| 277 |
} |
| 278 |
|
| 279 |
if (fullwuss) |
| 280 |
{ |
| 281 |
if (msa->ss_cons != NULL) |
| 282 |
{ |
| 283 |
status = esl_wuss_full(msa->ss_cons, msa->ss_cons); |
| 284 |
if (status == eslESYNTAX) esl_fatal("Bad consensus SS: not in WUSS format\n"); |
| 285 |
else if (status != eslOK) esl_fatal("Conversion of SS_cons failed, code %d\n", status); |
| 286 |
} |
| 287 |
if (msa->ss != NULL) |
| 288 |
for (idx = 0; idx < msa->nseq; idx++) |
| 289 |
if (msa->ss[idx] != NULL) |
| 290 |
{ |
| 291 |
status = esl_wuss_full(msa->ss[idx], msa->ss[idx]); |
| 292 |
if (status == eslESYNTAX) esl_fatal("Bad SS for %s: not in WUSS format\n", msa->sqname[idx]); |
| 293 |
else if (status != eslOK) esl_fatal("Conversion of SS for %s failed, code %d\n", msa->sqname[idx], status); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
if (phylip_namelen > 0 && (outfmt == eslMSAFILE_PHYLIP || outfmt == eslMSAFILE_PHYLIPS)) |
| 298 |
{ |
| 299 |
ESL_MSAFILE_FMTDATA optfmt; |
| 300 |
esl_msafile_fmtdata_Init(&optfmt); |
| 301 |
optfmt.namewidth = phylip_namelen; |
| 302 |
esl_msafile_phylip_Write(ofp, msa, outfmt, &optfmt); |
| 303 |
} |
| 304 |
else |
| 305 |
esl_msafile_Write(ofp, msa, outfmt); |
| 306 |
|
| 307 |
|
| 308 |
esl_msa_Destroy(msa); |
| 309 |
} |
| 310 |
} |
| 311 |
esl_msafile_Close(afp); |
| 312 |
} |
| 313 |
else |
| 314 |
{ |
| 315 |
ESL_SQFILE *sqfp; |
| 316 |
ESL_SQ *sq; |
| 317 |
char *mapfile; |
| 318 |
FILE *mapfp; |
| 319 |
|
| 320 |
status = esl_sqfile_Open(infile, infmt, NULL, &sqfp); |
| 321 |
if (status == eslENOTFOUND) |
| 322 |
esl_fatal("Couldn't open seqfile %s\n", infile); |
| 323 |
else if (status == eslEFORMAT) |
| 324 |
esl_fatal("Couldn't determine format of seqfile %s\n", infile); |
| 325 |
else if (status == eslEINVAL) |
| 326 |
esl_fatal("Can't autodetect format of stdin or .gz; use --informat\n"); |
| 327 |
else if (status != eslOK) |
| 328 |
esl_fatal("Open of seqfile %s failed, code %d\n", infile, status); |
| 329 |
|
| 330 |
if ( esl_opt_IsOn(go, "--ignore")) esl_sqio_Ignore (sqfp, esl_opt_GetString(go, "--ignore")); |
| 331 |
if ( esl_opt_IsOn(go, "--acceptx")) esl_sqio_AcceptAs(sqfp, esl_opt_GetString(go, "--acceptx"), 'X'); |
| 332 |
|
| 333 |
sq = esl_sq_Create(); |
| 334 |
|
| 335 |
if ( outfmt == eslSQFILE_HMMPGMD ) { |
| 336 |
int64_t res_cnt = 0; |
| 337 |
char timestamp[32]; |
| 338 |
time_t date; |
| 339 |
|
| 340 |
|
| 341 |
if (! esl_sqfile_IsRewindable(sqfp)) |
| 342 |
esl_fatal("Target sequence file %s isn't rewindable; can't produce a file in hmmpgmd format", infile); |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
if (esl_opt_IsUsed(go, "--id_map")) { |
| 347 |
mapfile = esl_opt_GetString( go, "--id_map"); |
| 348 |
} else { |
| 349 |
esl_strdup(infile, -1, &mapfile); |
| 350 |
esl_strcat(&mapfile, -1, ".map", 4); |
| 351 |
} |
| 352 |
if ((mapfp = fopen(mapfile, "w")) == NULL) |
| 353 |
esl_fatal("Failed to open map output file %s\n", mapfile); |
| 354 |
|
| 355 |
|
| 356 |
idx = 0; |
| 357 |
while ((status = esl_sqio_Read(sqfp, sq)) == eslOK) { |
| 358 |
res_cnt += sq->n; |
| 359 |
esl_sq_Reuse(sq); |
| 360 |
idx++; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
if (status == eslEFORMAT) esl_fatal("Parse failed (sequence file %s):\n%s\n", |
| 365 |
sqfp->filename, esl_sqfile_GetErrorBuf(sqfp)); |
| 366 |
else if (status != eslEOF) esl_fatal("Unexpected error %d reading sequence file %s", |
| 367 |
status, sqfp->filename); |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
date = time(NULL); |
| 374 |
ctime_r(&date, timestamp); |
| 375 |
fprintf(mapfp, "%d\n", idx); |
| 376 |
fprintf(ofp, "#%" PRId64 " %d %d %d %d %s", res_cnt, idx, 1, idx, idx, timestamp); |
| 377 |
|
| 378 |
esl_sqfile_Position(sqfp, 0); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
idx = 0; |
| 383 |
while ((status = esl_sqio_Read(sqfp, sq)) == eslOK) |
| 384 |
{ |
| 385 |
if (rfrom!=NULL) symconvert(sq->seq, rfrom, rto); |
| 386 |
if (force_lower) symconvert(sq->seq, |
| 387 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ", |
| 388 |
"abcdefghijklmnopqrstuvwxyz"); |
| 389 |
if (force_upper) symconvert(sq->seq, |
| 390 |
"abcdefghijklmnopqrstuvwxyz", |
| 391 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"); |
| 392 |
if (force_rna) symconvert(sq->seq, "Tt", "Uu"); |
| 393 |
if (force_dna) symconvert(sq->seq, "Uu", "Tt"); |
| 394 |
if (iupac_to_n) symconvert(sq->seq, |
| 395 |
"RYMKSWHBVDrymkswhbvd", |
| 396 |
"NNNNNNNNNNnnnnnnnnnn"); |
| 397 |
if (x_is_bad) symconvert(sq->seq, "Xx", "Nn"); |
| 398 |
|
| 399 |
if (wussify && sq->ss != NULL) esl_kh2wuss(sq->ss, sq->ss); |
| 400 |
if (dewuss && sq->ss != NULL) esl_wuss2kh(sq->ss, sq->ss); |
| 401 |
|
| 402 |
if (fullwuss && sq->ss != NULL) |
| 403 |
{ |
| 404 |
status = esl_wuss_full(sq->ss, sq->ss); |
| 405 |
if (status == eslESYNTAX) |
| 406 |
esl_fatal("Bad SS for %s: not in WUSS format\n", sq->name); |
| 407 |
else if (status != eslOK) |
| 408 |
esl_fatal("Conversion of SS for %s failed, code %d\n", |
| 409 |
sq->name, status); |
| 410 |
} |
| 411 |
|
| 412 |
if ( outfmt == eslSQFILE_HMMPGMD ) { |
| 413 |
fprintf(mapfp, "%d %s %s\n", idx+1, sq->name?sq->name:"", sq->desc?sq->desc:""); |
| 414 |
esl_sq_FormatName(sq, "%d 1", idx+1); |
| 415 |
esl_sq_SetDesc (sq, ""); |
| 416 |
} else { |
| 417 |
if (rename) esl_sq_FormatName(sq, "%s.%d", rename, idx+1); |
| 418 |
} |
| 419 |
esl_sqio_Write(ofp, sq, outfmt, FALSE); |
| 420 |
esl_sq_Reuse(sq); |
| 421 |
idx++; |
| 422 |
} |
| 423 |
|
| 424 |
if (status == eslEFORMAT) esl_fatal("Parse failed (sequence file %s):\n%s\n", |
| 425 |
sqfp->filename, esl_sqfile_GetErrorBuf(sqfp)); |
| 426 |
else if (status != eslEOF) esl_fatal("Unexpected error %d reading sequence file %s", |
| 427 |
status, sqfp->filename); |
| 428 |
|
| 429 |
if ( outfmt == eslSQFILE_HMMPGMD ) |
| 430 |
fclose(mapfp); |
| 431 |
|
| 432 |
esl_sq_Destroy(sq); |
| 433 |
esl_sqfile_Close(sqfp); |
| 434 |
} |
| 435 |
|
| 436 |
if (ofp != stdout) fclose(ofp); |
| 437 |
esl_getopts_Destroy(go); |
| 438 |
|
| 439 |
if(rfrom != NULL) free(rfrom); |
| 440 |
if(rto != NULL) free(rto); |
| 441 |
|
| 442 |
exit(0); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
static void |
| 458 |
symconvert(char *s, char *oldsyms, char *newsyms) |
| 459 |
{ |
| 460 |
int pos; |
| 461 |
char *sptr; |
| 462 |
int special; |
| 463 |
|
| 464 |
special = (strlen(newsyms) == 1 ? TRUE : FALSE); |
| 465 |
|
| 466 |
for (pos = 0; s[pos] != '\0'; pos++) |
| 467 |
if ((sptr = strchr(oldsyms, s[pos])) != NULL) |
| 468 |
s[pos] = (special ? *newsyms : newsyms[sptr-oldsyms]); |
| 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 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
static void |
| 522 |
regurgitate_pfam_as_afa(ESL_MSAFILE *afp, FILE *ofp, char *alifile, char *gapsym, int force_lower, int force_upper, int force_rna, int force_dna, int iupac_to_n, int x_is_bad, char *rename, char *rfrom, char *rto, int *ret_reached_eof) |
| 523 |
{ |
| 524 |
char *p = NULL; |
| 525 |
esl_pos_t n = 0; |
| 526 |
esl_pos_t gslen, seqnamelen, taglen; |
| 527 |
char *seqname = NULL; |
| 528 |
char *first_seqname = NULL; |
| 529 |
char *tag = NULL; |
| 530 |
char *gs = NULL; |
| 531 |
int nseq_read = 0; |
| 532 |
int reached_eof; |
| 533 |
|
| 534 |
char ac_tmpfile[16] = "esltmpXXXXXX"; |
| 535 |
FILE *ac_fp = NULL; |
| 536 |
char *ac_buf = NULL; |
| 537 |
int ac_buflen = 0; |
| 538 |
char *ac_s = NULL; |
| 539 |
char *ac_seqname = NULL; |
| 540 |
char *ac = NULL; |
| 541 |
int have_ac = FALSE; |
| 542 |
|
| 543 |
char de_tmpfile[16] = "esltmpXXXXXX"; |
| 544 |
FILE *de_fp = NULL; |
| 545 |
char *de_buf = NULL; |
| 546 |
int de_buflen = 0; |
| 547 |
char *de_s = NULL; |
| 548 |
char *de_seqname = NULL; |
| 549 |
char *de = NULL; |
| 550 |
int have_de = FALSE; |
| 551 |
|
| 552 |
char *aseq = NULL; |
| 553 |
esl_pos_t aseqlen = 0; |
| 554 |
int64_t apos; |
| 555 |
char aseqbuf[61]; |
| 556 |
int cpl = 60; |
| 557 |
int acpl; |
| 558 |
int status; |
| 559 |
|
| 560 |
afp->errmsg[0] = '\0'; |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
do { |
| 568 |
status = esl_msafile_GetLine(afp, &p, &n); |
| 569 |
if (status == eslEOF) return; |
| 570 |
else if (status != eslOK) esl_fatal("small mem parse error. problem reading line %d of msafile", (int) afp->linenumber); |
| 571 |
} while (esl_memspn(afp->line, afp->n, " \t") == afp->n || |
| 572 |
(esl_memstrpfx(afp->line, afp->n, "#") |
| 573 |
&& ! esl_memstrpfx(afp->line, afp->n, "# STOCKHOLM"))); |
| 574 |
|
| 575 |
if (! esl_memstrpfx(afp->line, afp->n, "# STOCKHOLM 1.")) esl_fatal("small mem parse failed (line %d): missing \"# STOCKHOLM\" header", (int) afp->linenumber); |
| 576 |
|
| 577 |
while ((status = esl_msafile_GetLine(afp, &p, &n)) == eslOK) |
| 578 |
{ |
| 579 |
while (n && ( *p == ' ' || *p == '\t')) { p++; n--; } |
| 580 |
|
| 581 |
if (esl_memstrpfx(p, n, "#=GS")) |
| 582 |
{ |
| 583 |
if (esl_memtok(&p, &n, " \t", &gs, &gslen) != eslOK) esl_fatal("small mem parse failed (line %d) in a way that can't happen", (int) afp->linenumber); |
| 584 |
if (esl_memtok(&p, &n, " \t", &seqname, &seqnamelen) != eslOK) esl_fatal("small mem parse failed (line %d): #=GS line missing <seqname>, <tag>, annotation", (int) afp->linenumber); |
| 585 |
if (esl_memtok(&p, &n, " \t", &tag, &taglen) != eslOK) esl_fatal("small mem parse failed (line %d): #=GS line missing <tag>, annotation", (int) afp->linenumber); |
| 586 |
if (! esl_memstrcmp(gs, gslen, "#=GS")) esl_fatal("small mem parse failed (line %d): faux #=GS line?", (int) afp->linenumber); |
| 587 |
|
| 588 |
if (esl_memstrcmp(tag, taglen, "AC")) |
| 589 |
{ |
| 590 |
if (! ac_fp && esl_tmpfile(ac_tmpfile, &ac_fp) != eslOK) esl_fatal("small mem parse failed, unable to open accession tmpfile"); |
| 591 |
fprintf(ac_fp, "%.*s %.*s\n", (int) seqnamelen, seqname, (int) n, p); |
| 592 |
} |
| 593 |
if (esl_memstrcmp(tag, taglen, "DE")) |
| 594 |
{ |
| 595 |
if (! de_fp && esl_tmpfile(de_tmpfile, &de_fp) != eslOK) esl_fatal("small mem parse failed, unable to open description tmpfile"); |
| 596 |
fprintf(de_fp, "%.*s %.*s\n", (int) seqnamelen, seqname, (int) n, p); |
| 597 |
} |
| 598 |
} |
| 599 |
else if (esl_memstrpfx(p, n, "//")) break; |
| 600 |
} |
| 601 |
if (status == eslEOF) esl_fatal("small mem parse failed (line %d): missing // terminator", (int) afp->linenumber); |
| 602 |
else if (status != eslOK) esl_fatal("small mem parse failed (line %d) with code %d", (int) afp->linenumber, status); |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
while ((status = esl_msafile_GetLine(afp, &p, &n)) == eslOK) |
| 608 |
{ |
| 609 |
while (n && ( *p == ' ' || *p == '\t')) { p++; n--; } |
| 610 |
if (esl_memstrpfx(p, n, "# STOCKHOLM 1.")) break; |
| 611 |
if (n && ! esl_memstrpfx(p, n, "#")) esl_fatal("small mem parse failed (line %d): unexpected data", (int) afp->linenumber); |
| 612 |
} |
| 613 |
if (status == eslOK) reached_eof = FALSE; |
| 614 |
else if (status == eslEOF) reached_eof = TRUE; |
| 615 |
else esl_fatal("--small parse error. problem reading line %d of msafile", (int) afp->linenumber); |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
esl_msafile_Close(afp); |
| 622 |
if ((status = esl_msafile_Open(NULL, alifile, NULL, eslMSAFILE_PFAM, NULL, &afp)) != eslOK) |
| 623 |
esl_fatal("--small, second pass, unable to open file %s for reading", alifile); |
| 624 |
|
| 625 |
if (ac_fp) { |
| 626 |
rewind(ac_fp); |
| 627 |
if((status = esl_fgets(&(ac_buf), &(ac_buflen), ac_fp)) != eslOK) esl_fatal("--small accession tmpfile parse failed"); |
| 628 |
ac_s = ac_buf; |
| 629 |
if (esl_strtok_adv(&ac_s, " \t\n\r", &ac_seqname, NULL, NULL) != eslOK) esl_fatal("--small accession tmpfile parse failed"); |
| 630 |
if (esl_strtok_adv(&ac_s, "\n\r", &ac, NULL, NULL) != eslOK) esl_fatal("--small accession tmpfile parse failed"); |
| 631 |
} |
| 632 |
if (de_fp) { |
| 633 |
rewind(de_fp); |
| 634 |
if((status = esl_fgets(&(de_buf), &(de_buflen), de_fp)) != eslOK) esl_fatal("--small description tmpfile parse failed"); |
| 635 |
de_s = de_buf; |
| 636 |
if (esl_strtok_adv(&de_s, " \t\n\r", &de_seqname, NULL, NULL) != eslOK) esl_fatal("--small description tmpfile parse failed"); |
| 637 |
if (esl_strtok_adv(&de_s, "\n\r", &de, NULL, NULL) != eslOK) esl_fatal("--small description tmpfile parse failed"); |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
do { |
| 645 |
status = esl_msafile_GetLine(afp, &p, &n); |
| 646 |
if (status == eslEOF) return; |
| 647 |
else if (status != eslOK) esl_fatal("small mem parse pass 2 error. problem reading line %d of msafile", (int) afp->linenumber); |
| 648 |
} while (esl_memspn(afp->line, afp->n, " \t") == afp->n || |
| 649 |
(esl_memstrpfx(afp->line, afp->n, "#") |
| 650 |
&& ! esl_memstrpfx(afp->line, afp->n, "# STOCKHOLM"))); |
| 651 |
|
| 652 |
if (! esl_memstrpfx(afp->line, afp->n, "# STOCKHOLM 1.")) esl_fatal("small mem parse pass 2 failed (line %d): missing \"# STOCKHOLM\" header", (int) afp->linenumber); |
| 653 |
|
| 654 |
while ((status = esl_msafile_GetLine(afp, &p, &n)) == eslOK) |
| 655 |
{ |
| 656 |
while (n && ( *p == ' ' || *p == '\t')) { p++; n--; } |
| 657 |
|
| 658 |
if (!n || *p == '#') continue; |
| 659 |
else if (esl_memstrpfx(p, n, "//")) break; |
| 660 |
else |
| 661 |
{ |
| 662 |
if (esl_memtok(&p, &n, " \t", &seqname, &seqnamelen) != eslOK) esl_fatal("small mem parse pass 2 failed (line %d): no seq name", (int) afp->linenumber); |
| 663 |
if (esl_memtok(&p, &n, " \t", &aseq, &aseqlen) != eslOK) esl_fatal("small mem parse pass 2 failed (line %d): no aseq", (int) afp->linenumber); |
| 664 |
|
| 665 |
|
| 666 |
if (nseq_read == 0) { if ((status = esl_memstrdup(seqname, seqnamelen, &(first_seqname))) != eslOK) esl_fatal("small mem parse failed: unable to copy seqname"); } |
| 667 |
else if (esl_memstrcmp(seqname, seqnamelen, first_seqname)) esl_fatal("--small parse pass 2 failed (line %d): two seqs named %s. Alignment appears to be in interleaved Stockholm (not Pfam) format.", (int) afp->linenumber, seqname); |
| 668 |
nseq_read++; |
| 669 |
|
| 670 |
|
| 671 |
have_de = have_ac = FALSE; |
| 672 |
if (ac_seqname && (esl_memstrcmp(seqname, seqnamelen, ac_seqname))) have_ac = TRUE; |
| 673 |
if (de_seqname && (esl_memstrcmp(seqname, seqnamelen, de_seqname))) have_de = TRUE; |
| 674 |
|
| 675 |
if (rename) fprintf(ofp, ">%s.%d%s%s%s%s\n", rename, nseq_read, (have_ac ? " " : "") , (have_ac ? ac : ""), (have_de ? " " : "") , (have_de ? de : "")); |
| 676 |
else fprintf(ofp, ">%.*s%s%s%s%s\n", (int) seqnamelen, seqname, (have_ac ? " " : "") , (have_ac ? ac : ""), (have_de ? " " : "") , (have_de ? de : "")); |
| 677 |
|
| 678 |
|
| 679 |
if (have_ac) { |
| 680 |
status = esl_fgets(&(ac_buf), &(ac_buflen), ac_fp); |
| 681 |
if (status == eslEOF) ac_seqname = NULL; |
| 682 |
else if (status == eslOK) { |
| 683 |
ac_s = ac_buf; |
| 684 |
if (esl_strtok_adv(&ac_s, " \t\n\r", &ac_seqname, NULL, NULL) != eslOK) esl_fatal("--small accession tmpfile parse failed"); |
| 685 |
if (esl_strtok_adv(&ac_s, "\n\r", &ac, NULL, NULL) != eslOK) esl_fatal("--small accession tmpfile parse failed"); |
| 686 |
} |
| 687 |
} |
| 688 |
if (have_de) { |
| 689 |
status = esl_fgets(&(de_buf), &(de_buflen), de_fp); |
| 690 |
if(status == eslEOF) de_seqname = NULL; |
| 691 |
else if (status == eslOK) { |
| 692 |
de_s = de_buf; |
| 693 |
if (esl_strtok_adv(&de_s, " \t\n\r", &de_seqname, NULL, NULL) != eslOK) esl_fatal("--small description tmpfile parse failed"); |
| 694 |
if (esl_strtok_adv(&de_s, "\n\r", &de, NULL, NULL) != eslOK) esl_fatal("--small description tmpfile parse failed"); |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
for (apos = 0; apos < aseqlen; apos += cpl) |
| 703 |
{ |
| 704 |
acpl = (aseqlen - apos > cpl ? cpl : aseqlen - apos); |
| 705 |
strncpy(aseqbuf, aseq + apos, acpl); |
| 706 |
aseqbuf[acpl] = '\0'; |
| 707 |
|
| 708 |
if (rfrom) symconvert(aseqbuf, rfrom, rto); |
| 709 |
if (gapsym) symconvert(aseqbuf, "-_.", gapsym); |
| 710 |
if (force_lower) symconvert(aseqbuf, |
| 711 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ", |
| 712 |
"abcdefghijklmnopqrstuvwxyz"); |
| 713 |
if (force_upper) symconvert(aseqbuf, |
| 714 |
"abcdefghijklmnopqrstuvwxyz", |
| 715 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"); |
| 716 |
if (force_rna) symconvert(aseqbuf, "Tt", "Uu"); |
| 717 |
if (force_dna) symconvert(aseqbuf, "Uu", "Tt"); |
| 718 |
if (iupac_to_n) symconvert(aseqbuf, |
| 719 |
"RYMKSWHBVDrymkswhbvd", |
| 720 |
"NNNNNNNNNNnnnnnnnnnn"); |
| 721 |
if (x_is_bad) symconvert(aseqbuf, "Xx", "Nn"); |
| 722 |
|
| 723 |
fprintf(ofp, "%s\n", aseqbuf); |
| 724 |
} |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
if (status != eslOK) esl_fatal("--small parse pass 2 failed (line %d): didn't find // at end of alignment", (int) afp->linenumber); |
| 731 |
if (ac_seqname) esl_fatal("--small parse pass 2 failed, sequence %s with #=GS AC line does not exist in alignment or is in different order.", ac_seqname); |
| 732 |
if (de_seqname) esl_fatal("--small parse pass 2 failed, sequence %s with #=GS DE line does not exist in alignment or is in different order.", de_seqname); |
| 733 |
|
| 734 |
if (ac_fp) fclose(ac_fp); |
| 735 |
if (de_fp) fclose(de_fp); |
| 736 |
esl_msafile_Close(afp); |
| 737 |
|
| 738 |
if (first_seqname) free(first_seqname); |
| 739 |
if (ac_buf) free(ac_buf); |
| 740 |
if (de_buf) free(de_buf); |
| 741 |
|
| 742 |
*ret_reached_eof = reached_eof; |
| 743 |
return; |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
static int |
| 759 |
regurgitate_pfam_as_pfam(ESL_MSAFILE *afp, FILE *ofp, char *gapsym, int force_lower, int force_upper, int force_rna, int force_dna, int iupac_to_n, int x_is_bad, int wussify, int dewuss, int fullwuss, char *rfrom, char *rto) |
| 760 |
{ |
| 761 |
char *p; |
| 762 |
esl_pos_t n; |
| 763 |
char *first_seqname = NULL; |
| 764 |
char *gx = NULL; |
| 765 |
char *seqname = NULL; |
| 766 |
char *tag = NULL; |
| 767 |
char *text = NULL; |
| 768 |
esl_pos_t gxlen, namelen, taglen, textlen; |
| 769 |
int nseq_read = 0; |
| 770 |
int parse_gc_and_gr; |
| 771 |
int flushpoint = 10000; |
| 772 |
int exp_alen = -1; |
| 773 |
char *buf = NULL; |
| 774 |
esl_pos_t pos, pos2; |
| 775 |
int status; |
| 776 |
|
| 777 |
|
| 778 |
parse_gc_and_gr = (wussify || dewuss || fullwuss) ? TRUE : FALSE; |
| 779 |
afp->errmsg[0] = '\0'; |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
do { |
| 788 |
status = esl_msafile_GetLine(afp, &p, &n); |
| 789 |
if (status == eslEOF) return eslEOF; |
| 790 |
else if (status != eslOK) esl_fatal("small mem parse error. problem reading line %d of msafile", (int) afp->linenumber); |
| 791 |
fprintf(ofp, "%.*s\n", (int) afp->n, afp->line); |
| 792 |
} while (esl_memspn(afp->line, afp->n, " \t") == afp->n || |
| 793 |
(esl_memstrpfx(afp->line, afp->n, "#") |
| 794 |
&& ! esl_memstrpfx(afp->line, afp->n, "# STOCKHOLM"))); |
| 795 |
|
| 796 |
if (! esl_memstrpfx(afp->line, afp->n, "# STOCKHOLM 1.")) esl_fatal("small mem parse failed (line %d): missing \"# STOCKHOLM\" header", (int) afp->linenumber); |
| 797 |
|
| 798 |
|
| 799 |
while ((status = esl_msafile_GetLine(afp, &p, &n)) == eslOK) |
| 800 |
{ |
| 801 |
if ((int) afp->linenumber % flushpoint == 0) fflush(ofp); |
| 802 |
while (n && ( *p == ' ' || *p == '\t')) { p++; n--; } |
| 803 |
|
| 804 |
if (!n) fprintf(ofp, "\n"); |
| 805 |
else if (esl_memstrpfx(p, n, "//")) { fprintf(ofp, "//\n"); break; } |
| 806 |
else if (*p == '#') |
| 807 |
{ |
| 808 |
if (parse_gc_and_gr && esl_memstrpfx(p, n, "#=GC")) |
| 809 |
{ |
| 810 |
if (esl_memtok(&p, &n, " \t", &gx, &gxlen) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "small mem parse failed (line %d): bad #=GC line", (int) afp->linenumber); |
| 811 |
if (esl_memtok(&p, &n, " \t", &tag, &taglen) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "small mem parse failed (line %d): bad #=GC line", (int) afp->linenumber); |
| 812 |
if (esl_memtok(&p, &n, " \t", &text, &textlen) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "small mem parse failed (line %d): bad #=GC line", (int) afp->linenumber); |
| 813 |
pos = text - afp->line; |
| 814 |
|
| 815 |
|
| 816 |
if (exp_alen == -1) exp_alen = textlen; |
| 817 |
else if (exp_alen != textlen) ESL_XFAIL(eslEFORMAT, afp->errmsg, "small mem parse failed (line %d): bad #=GC line, len %d, expected %d", (int) afp->linenumber, (int) textlen, (int) exp_alen); |
| 818 |
|
| 819 |
|
| 820 |
ESL_REALLOC(buf, sizeof(char) * (textlen+1)); |
| 821 |
esl_memstrcpy(text, textlen, buf); |
| 822 |
|
| 823 |
if (esl_memstrcmp(tag, taglen, "SS_cons")) |
| 824 |
{ |
| 825 |
if (wussify) esl_kh2wuss(buf, buf); |
| 826 |
else if (dewuss) esl_wuss2kh(buf, buf); |
| 827 |
else if (fullwuss) |
| 828 |
{ |
| 829 |
status = esl_wuss_full(buf, buf); |
| 830 |
if (status == eslESYNTAX) esl_fatal("Bad SS_cons line: not in WUSS format, alifile line: %d", (int) afp->linenumber); |
| 831 |
else if (status != eslOK) esl_fatal("Conversion of SS_cons line failed, code %d, alifile line: %d", status, (int) afp->linenumber); |
| 832 |
} |
| 833 |
} |
| 834 |
fprintf(ofp, "#=GC %.*s%*s%s\n", (int) taglen, tag, (int) (pos-taglen-5), "", buf); |
| 835 |
} |
| 836 |
else if (parse_gc_and_gr && esl_memstrpfx(p, n, "#=GR") == 0) |
| 837 |
{ |
| 838 |
|
| 839 |
if (esl_memtok(&p, &n, " \t", &gx, &gxlen) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "--small parse failed (line %d): bad #=GR line", (int) afp->linenumber); |
| 840 |
if (esl_memtok(&p, &n, " \t", &seqname, &namelen) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "--small parse failed (line %d): bad #=GR line", (int) afp->linenumber); |
| 841 |
if (esl_memtok(&p, &n, " \t", &tag, &taglen) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "--small parse failed (line %d): bad #=GR line", (int) afp->linenumber); |
| 842 |
pos = tag - afp->line; |
| 843 |
if (esl_memtok(&p, &n, " \t", &text, &textlen) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "--small parse failed (line %d): bad #=GR line", (int) afp->linenumber); |
| 844 |
pos2 = text - afp->line; |
| 845 |
|
| 846 |
|
| 847 |
ESL_REALLOC(buf, sizeof(char) * (textlen+1)); |
| 848 |
esl_memstrcpy(text, textlen, buf); |
| 849 |
|
| 850 |
|
| 851 |
if (exp_alen == -1) exp_alen = textlen; |
| 852 |
else if (exp_alen != textlen) ESL_XFAIL(eslEFORMAT, afp->errmsg, "small mem parse failed (line %d): bad seq line, len %d, expected %d", (int) afp->linenumber, (int) textlen, (int) exp_alen); |
| 853 |
|
| 854 |
if (esl_memstrcmp(tag, taglen, "SS") == 0) |
| 855 |
{ |
| 856 |
if (wussify) esl_kh2wuss(buf, buf); |
| 857 |
else if (dewuss) esl_wuss2kh(buf, buf); |
| 858 |
else if (fullwuss) { |
| 859 |
status = esl_wuss_full(buf, buf); |
| 860 |
if (status == eslESYNTAX) esl_fatal("Bad SS line: not in WUSS format, alifile line: %d", (int) afp->linenumber); |
| 861 |
else if (status != eslOK) esl_fatal("Conversion of SS line failed, code %d, alifile line: %d", status, (int) afp->linenumber); |
| 862 |
} |
| 863 |
} |
| 864 |
|
| 865 |
fprintf(ofp, "#=GR %.*s%*s%.*s%*s%s\n", (int) namelen, seqname, (int) (pos-namelen-5), "", (int) taglen, tag, (int) (pos2-pos-taglen), "", buf); |
| 866 |
} |
| 867 |
else { |
| 868 |
fprintf(ofp, "%.*s\n", (int) afp->n, afp->line); |
| 869 |
} |
| 870 |
} |
| 871 |
else |
| 872 |
{ |
| 873 |
if (esl_memtok(&p, &n, " \t", &seqname, &namelen) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "--small parse failed (line %d): bad sequence line", (int) afp->linenumber); |
| 874 |
if (esl_memtok(&p, &n, " \t", &text, &textlen) != eslOK) ESL_XFAIL(eslEFORMAT, afp->errmsg, "--small parse failed (line %d): bad sequence line", (int) afp->linenumber); |
| 875 |
pos = text - afp->line; |
| 876 |
|
| 877 |
|
| 878 |
if (exp_alen == -1) exp_alen = textlen; |
| 879 |
else if(exp_alen != textlen) ESL_XFAIL(eslEFORMAT, afp->errmsg, "small mem parse failed (line %d): bad seq line, len %d, expected %d", (int) afp->linenumber, (int) textlen, (int) exp_alen); |
| 880 |
|
| 881 |
|
| 882 |
if (nseq_read == 0) { if ((status = esl_memstrdup(seqname, namelen, &(first_seqname))) != eslOK) goto ERROR; } |
| 883 |
else if (esl_memstrcmp(seqname, namelen, first_seqname)) { ESL_XFAIL(eslEFORMAT, afp->errmsg, "parse failed (line %d): two seqs named %s. Alignment appears to be in Stockholm format. Reformat to Pfam with esl-reformat.", (int) afp->linenumber, seqname); } |
| 884 |
nseq_read++; |
| 885 |
|
| 886 |
|
| 887 |
ESL_REALLOC(buf, sizeof(char) * (textlen+1)); |
| 888 |
esl_memstrcpy(text, textlen, buf); |
| 889 |
|
| 890 |
|
| 891 |
if (rfrom) symconvert(buf, rfrom, rto); |
| 892 |
if (gapsym) symconvert(buf, "-_.", gapsym); |
| 893 |
if (force_lower) symconvert(buf, |
| 894 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ", |
| 895 |
"abcdefghijklmnopqrstuvwxyz"); |
| 896 |
if (force_upper) symconvert(buf, |
| 897 |
"abcdefghijklmnopqrstuvwxyz", |
| 898 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"); |
| 899 |
if (force_rna) symconvert(buf, "Tt", "Uu"); |
| 900 |
if (force_dna) symconvert(buf, "Uu", "Tt"); |
| 901 |
if (iupac_to_n) symconvert(buf, |
| 902 |
"RYMKSWHBVDrymkswhbvd", |
| 903 |
"NNNNNNNNNNnnnnnnnnnn"); |
| 904 |
if (x_is_bad) symconvert(buf, "Xx", "Nn"); |
| 905 |
|
| 906 |
fprintf(ofp, "%.*s%*s%s\n", (int) namelen, seqname, (int) (pos-namelen), "", buf); |
| 907 |
} |
| 908 |
} |
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
if (status != eslOK) esl_fatal("--small parse failed (line %d): didn't find // at end of alignment", (int) afp->linenumber); |
| 914 |
if (first_seqname) free(first_seqname); |
| 915 |
if (buf) free(buf); |
| 916 |
return eslOK; |
| 917 |
|
| 918 |
ERROR: |
| 919 |
return status; |
| 920 |
} |
| 921 |
|
| 922 |
static int |
| 923 |
parse_replace_string(const char *rstring, char **ret_from, char **ret_to) |
| 924 |
{ |
| 925 |
int status; |
| 926 |
int rlen, mid, i; |
| 927 |
int is_valid = FALSE; |
| 928 |
char *from = NULL; |
| 929 |
char *to = NULL; |
| 930 |
|
| 931 |
|
| 932 |
rlen = strlen(rstring); |
| 933 |
|
| 934 |
if((rlen % 2) != 0) { |
| 935 |
mid = rlen / 2; |
| 936 |
if(rstring[mid] == ':') { |
| 937 |
ESL_ALLOC(from, sizeof(char) * (mid+1)); |
| 938 |
ESL_ALLOC(to, sizeof(char) * (mid+1)); |
| 939 |
for(i = 0; i < mid; i++) from[i] = rstring[i]; |
| 940 |
for(i = mid+1; i < rlen; i++) to[i-(mid+1)] = rstring[i]; |
| 941 |
from[mid] = '\0'; |
| 942 |
to[mid] = '\0'; |
| 943 |
is_valid = TRUE; |
| 944 |
} |
| 945 |
} |
| 946 |
if(! is_valid) esl_fatal("--replace takes arg of <s1>:<s2> with len(<s1>) == len(<s2>); %s not recognized", rstring); |
| 947 |
*ret_from = from; |
| 948 |
*ret_to = to; |
| 949 |
|
| 950 |
return eslOK; |
| 951 |
|
| 952 |
ERROR: |
| 953 |
if(from != NULL) free(from); |
| 954 |
if(to != NULL) free(to); |
| 955 |
return status; |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|