initial commit
[jalview.git] / forester / archive / RIO / others / hmmer / squid / translate.c
1 /*****************************************************************
2  * HMMER - Biological sequence analysis with profile HMMs
3  * Copyright (C) 1992-1999 Washington University School of Medicine
4  * All Rights Reserved
5  * 
6  *     This source code is distributed under the terms of the
7  *     GNU General Public License. See the files COPYING and LICENSE
8  *     for details.
9  *****************************************************************/
10
11 /*
12  * translate.c - functions for translating nucleic acid sequence
13  * created Tue Jan 12 11:27:29 1993, SRE
14  * 
15  * RCS $Id: translate.c,v 1.1.1.1 2005/03/22 08:34:31 cmzmasek Exp $
16  */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include "squid.h"
21
22
23 #ifdef MEMDEBUG
24 #include "dbmalloc.h"
25 #endif
26
27
28
29 /* Function: Translate(char *seq, char **code)
30  * 
31  * Given a ptr to the start of a nucleic acid sequence,
32  * and a genetic code, translate the sequence into
33  * amino acid sequence.
34  * 
35  * code is an array of 65 strings, representing
36  * the translations of the 64 codons, arranged
37  * in order AAA, AAC, AAG, AAU, ..., UUA, UUC, UUG, UUU.
38  * '*' or '***' is used to represent termination
39  * codons, usually. The final string, code[64],
40  * is the code for an ambiguous amino acid.
41  *
42  * Because of the way space is allocated for the amino
43  * acid sequence, the amino acid strings cannot be
44  * longer than 3 letters each. (I don't foresee using
45  * anything but the single- and triple- letter codes.)
46  * 
47  * Returns a ptr to the translation string on success,
48  * or NULL on failure.
49  */
50 char *
51 Translate(char *seq, char **code)
52 {
53   int   codon;                  /* index for codon         */
54   char *aaseq;                  /* RETURN: the translation */
55   char *aaptr;                  /* ptr into aaseq */
56   int   i;
57   
58   if (seq == NULL) 
59     { squid_errno = SQERR_NODATA; return NULL; }
60   if ((aaseq = (char *) calloc (strlen(seq) + 1, sizeof(char))) == NULL)
61     Die("calloc failed");
62
63   aaptr = aaseq;
64   for (; *seq != '\0' && *(seq+1) != '\0' && *(seq+2) != '\0'; seq += 3)
65     {
66                                 /* calculate the lookup value for
67                                    this codon */
68       codon = 0;
69       for (i = 0; i < 3; i++)
70         {
71           codon *= 4;
72           switch (*(seq + i)) {
73           case 'A': case 'a':             break;
74           case 'C': case 'c': codon += 1; break;
75           case 'G': case 'g': codon += 2; break;
76           case 'T': case 't': codon += 3; break;
77           case 'U': case 'u': codon += 3; break;
78           default: codon = 64; break;
79           }
80           if (codon == 64) break;
81         }
82
83       strcpy(aaptr, code[codon]);
84       aaptr += strlen(code[codon]);
85     }
86   return aaseq;
87 }