 /* File alitos2_prot.c */

#if !defined(COMPILE_ENVIRONMENT)
#include <gp/stdcenvf.h> /* std compile environment for functions */
#endif

 void alignment_i_to_s_prot(ALIGNMENT_INT_PROT *pia1, ALIGNMENT_CHR_PROT *psa1, int *perr)
 /**********************************************************************/
 /*                                                                    */
 /*    Converts the protected integer alignment *pia1 into a protected */
 /* character string alignment *psa1.                                  */
 /*                                                                    */
 /*   *perr = 0 means a.o.k.                                           */
 /*           11  "   *pia1 is aligned, but len(nsa) != len(nsb)       */
 /*           13  "   naux > 0 (handles simple alignments only)        */
 /*                                                                    */
 /**********************************************************************/
{
/* local data: */
  char *proc = "alignment_i_to_s_prot";
  int na,nb,nsymbs,nspaces,i,k,ichr,jchr;
  int lsa, lsb;
  pcodeset *pc1;
/* code: */
 db_enter_msg(proc,3); /* debug only */
/* error check */
  if (pia1->naux > 0)
    {fprintf(stderr,"*ERR:%s: cannot handle naux=%d\n",proc,pia1->naux);
/*     exit(2); */ /* this line for testing only */
     *perr = 15;
     goto RETURN;
    }
/* initialize */
  *perr=0;
  pc1 = pia1->pcset;
/* go */
  psa1->aligned = pia1->aligned;
  strcpy_prot_s(psa1->sa," ");
  strcpy_prot_s(psa1->sb," ");
  lsa = 1;
  lsb = 1;
  na = pia1->nsa->str[0];
  nb = pia1->nsb->str[0];
  if (pia1->aligned)
    {if (nb != na)
       {fprintf(stderr,"%s: pia1 is supposed to be aligned, but\n",proc);
        fprintf(stderr," length(nsa->str) NOT = length(nsb->str).\n");
        fprintf(stderr," length(nsa->str)=%d, length(nsb->str)=%d\n",na,nb);
        fprintf(stderr," SYSTEM PROBLEM\n");
/*        exit(2); */ /* this line for testing only */
        *perr = 11;
        goto RETURN;
       } /* "if (pia1->nb ... " */
     nsymbs = na;
     for (i=1; i <= nsymbs; i++)
       {ichr = pia1->nsa->str[i];
       /* put in A string word */
        strcat_prot_s(psa1->sa,pc1->pc[ichr].ac);
        lsa += strlen(pc1->pc[ichr].ac);
        jchr = pia1->nsb->str[i];
       /* put in B string word */
        strcat_prot_s(psa1->sb,pc1->pc[jchr].ac);
        lsb += strlen(pc1->pc[jchr].ac);
       /* add spaces to even up A and B string lengths */
        nspaces = abs(lsa-lsb);
        for (k=1; k<=nspaces; k++)
          {if (lsb > lsa) {strcat_prot_s(psa1->sa," "); ++lsa;}
           else           {strcat_prot_s(psa1->sb," "); ++lsb;}
          }
       }
    } /* " if (pia1->aligned) ... else x" */
  else
    {decode3_prot(psa1->sa,pc1,pia1->nsa,perr);
     if (*perr > 0) fprintf(stderr,"Decode of string a returns perr=%d\n",*perr);
     decode3_prot(psa1->sb,pc1,pia1->nsb,perr);
     if (*perr > 0) fprintf(stderr,"Decode of string b returns perr=%d\n",*perr);
    }; /*end of "if (pia1->aligned) x; else ..." */
RETURN:
 db_leave_msg(proc,3); /* debug only */
  } /* end of function "alignment_i_to_s_prot" */
 /* end file alitos2_prot.c */
