From 2f66b3836d645ff2907886238bf19526c55e9bfa Mon Sep 17 00:00:00 2001 From: Simon Potter Date: Wed, 19 Jun 2019 11:03:01 +0100 Subject: [PATCH] Prepend sequence index if names non-unique in aligned fasta format --- esl_msafile_afa.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/esl_msafile_afa.c b/esl_msafile_afa.c index 8f5681bd..e393c926 100644 --- a/esl_msafile_afa.c +++ b/esl_msafile_afa.c @@ -279,10 +279,24 @@ esl_msafile_afa_Write(FILE *fp, const ESL_MSA *msa) int64_t pos; char buf[61]; int acpl; /* actual number of characters per line */ + int make_uniquenames = FALSE; /* TRUE if we force names to be unique */ + int status; + int tmpnseq; + int uniqwidth; + status = esl_msa_CheckUniqueNames(msa); + if (status == eslFAIL) { + make_uniquenames = TRUE; + for (tmpnseq = msa->nseq; tmpnseq; tmpnseq /= 10) uniqwidth++; /* how wide the uniqizing numbers need to be */ + } + for (i = 0; i < msa->nseq; i++) { - if (fprintf(fp, ">%s", msa->sqname[i]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "afa msa file write failed"); + if (make_uniquenames) { + if (fprintf(fp, ">%0*d/%s", uniqwidth, i, msa->sqname[i]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "afa msa file write failed"); + } else { + if (fprintf(fp, ">%s", msa->sqname[i]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "afa msa file write failed"); + } if (msa->sqacc != NULL && msa->sqacc[i] != NULL) { if (fprintf(fp, " %s", msa->sqacc[i]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "afa msa file write failed"); } if (msa->sqdesc != NULL && msa->sqdesc[i] != NULL) { if (fprintf(fp, " %s", msa->sqdesc[i]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "afa msa file write failed"); } if (fputc('\n', fp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "afa msa file write failed");