 /* File alitos5_prot.c */

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

 void aln_i_to_s_prot5(ALIGNMENT_INT_PROT *pia1, 
  ALIGNMENT_CHR_PROT *psa1, char *delimiter, int *perr)
 /**********************************************************************/
 /*                                                                    */
 /*    Converts the protected integer alignment *pia1 into a protected */
 /* character string alignment *psa1, fancily.  If *pia1 is aligned,   */
 /* bounds each constituent with the delimiter symbol, puts strings of */
 /* asterisks in for nulls, and changes the type case of symbols that  */
 /* are in error.  If not aligned, just decodes the int strings.       */
 /*                                                                    */
 /*    On return, *perr = 0 means a.o.k.                               */
 /*    Fatal error if *pia1 is aligned but length(A) != length(B).     */
 /*                                                                    */
 /**********************************************************************/
 {char *proc = " aln_i_to_s_prot5";
  PROTECTED_CHR_STR sxx, *sx = &sxx;
  PROTECTED_CHR_STR istrx, *istr = &istrx;
  PROTECTED_CHR_STR jstrx, *jstr = &jstrx;
  int na,nb,nspaces,i,k,lsa,lsb,ichr,jchr;
  pcodeset *pc1;
/* code: */
 db_enter_msg(proc,1); /* debug only */
/* initialize */
  *perr=0;
  alloc_prot_chr_str_guts(sx,133);
  alloc_prot_chr_str_guts(istr,133);
  alloc_prot_chr_str_guts(jstr,133);
  pc1 = pia1->pcset;
/* go */
  psa1->aligned = pia1->aligned;
  if (!pia1->aligned)
    {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);
    }
  else
    {/* put initial field-delimiter characters in */
     strcpy_prot_s(psa1->sa,delimiter); lsa = strlen(psa1->sa->str);
     strcpy_prot_s(psa1->sb,delimiter); lsb = strlen(psa1->sb->str);
     na = pia1->nsa->str[0];
     nb = pia1->nsb->str[0];
     if (nb != na) fatal_error(proc,"aln is aligned, but length(A) != length(B)",2);
     /* loop putting corresponding items in */
     for (i=1; i <= na; i++)
       {ichr = pia1->nsa->str[i];
        jchr = pia1->nsb->str[i];
     /* make symbols in istr and jstr */
        istr = ascii_str_prot(istr,ichr,pc1,pia1);
        jstr = ascii_str_prot(jstr,jchr,pc1,pia1);
     /* put ascii for symbol #ichr into psa1->sa ("A" string) */
        if (ichr == 0) /* put null symbol into sx */
          {strcpy_prot_s(sx,"");
           for (k=1; k<=strlen(jstr->str); k++) strcat_prot_s(sx,"*");
          }
        else /* put ascii rep into sx, case-switched if in error */
          {strcpy_prot_p(sx,istr);
           if (ichr != jchr) switch_case(sx->str);
          }
       /* concatenate sx onto A string */
        strcat_prot_p(psa1->sa,sx); lsa += strlen(sx->str);
       /* put ascii for symbol #jchr into psa1->sb ("B" string) */
        if (jchr == 0) /* put null symbol into sx */
          {strcpy_prot_s(sx,"");
           for (k=1; k<=strlen(istr->str); k++) strcat_prot_s(sx,"*");
          }
        else /* put ascii rep into sx, case-switched if in error */
          {strcpy_prot_p(sx,jstr);
           if (ichr != jchr) switch_case(sx->str);
          }
       /* concatenate sx onto B string */
        strcat_prot_p(psa1->sb,sx); lsb += strlen(sx->str);
      /* 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;}
          }
      /* add final field delimiter to both A and B strings */
        strcat_prot_s(psa1->sa,delimiter); lsa += strlen(delimiter);
        strcat_prot_s(psa1->sb,delimiter); lsb += strlen(delimiter);
    }  }
  dealloc_prot_chr_str_guts(sx);
  dealloc_prot_chr_str_guts(istr);
  dealloc_prot_chr_str_guts(jstr);
 db_leave_msg(proc,1); /* debug only */
  } /* end of function "aln_i_to_s_prot5" */
 /* end file alitos5_prot.c */
