initial commit
[jalview.git] / forester / archive / RIO / others / hmmer / squid / seqencode.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 /* seqencode.c
12  * 
13  * Routines for creating and manipulating encoded sequence strings.
14  * RCS $Id: seqencode.c,v 1.1.1.1 2005/03/22 08:34:29 cmzmasek Exp $
15  */
16 #include <stdio.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include "squid.h"
20
21                         
22 #ifdef MEMDEBUG
23 #include "dbmalloc.h"
24 #endif
25                                 /* seqcmp()
26                                    returns 0 if s1 == s2
27                                    mismatch number otherwise */
28 int
29 seqcmp(char *s1, char *s2, int allow)
30 {
31   int mmat = 0;
32
33   while ((*s1 != NTEND) && (*s2 != NTEND) && (mmat <= allow)) 
34     {
35       if (!(ntmatch(*s1, *s2)))
36         mmat++;;
37       s1++;
38       s2++;
39     }
40   while ((*s1++ != NTEND) && (mmat <= allow))
41     mmat++;
42   return(mmat);
43 }
44                                 /* seqncmp()
45                                    same as seqcmp but it looks at,
46                                    at most, n positions */
47 int
48 seqncmp(char *s1, char *s2, int n, int allow)
49 {
50   int mmat = 0;
51
52   while ((*s2 != NTEND) &&
53          (n-- != 0))
54     {
55       if ((!(ntmatch(*s1, *s2))) &&
56           (++mmat > allow))
57         return(mmat);
58       s1++;
59       s2++;
60     }
61   while ((n-- != 0) && (*s1++ != NTEND) && (mmat <= allow))
62     mmat++;
63   return (mmat);
64 }
65       
66                                 /* seqencode()
67                                    given a character text string str (A,C,G,T),
68                                    convert to an encoded seq string;
69                                    return 1 for success, 0 if fail */
70 int
71 seqencode(char *codeseq, /* pre-allocated space for answer */
72           char *str)     /* character string to convert    */
73 {
74   char  *ptr;
75   int    idx;
76
77   ptr = codeseq;
78   while (*str != '\0')
79     {
80       if (islower((int) (*str))) *str = (char) toupper((int) (*str));
81       for (idx = 0; *str != iupac[idx].sym && idx <= IUPACSYMNUM; idx++)
82         ;
83       if (idx > IUPACSYMNUM)
84         {
85           *ptr = (char) NTEND;
86           return 0;
87         }
88       else
89         *ptr = iupac[idx].code;
90       ptr++;
91       str++;
92     }
93   *ptr = NTEND;
94   return 1;
95 }
96
97
98 int
99 coded_revcomp(char *comp, char *seq)
100 {
101   long  bases;
102   char *bckp, *fwdp;
103   int   idx;
104   long  pos;
105
106   bases = strlen(seq);
107
108   fwdp = comp;
109   bckp = seq + bases -1;
110   for (pos = 0; pos < bases; pos++)
111     {
112       for (idx = 0; *bckp != iupac[idx].code && idx < IUPACSYMNUM; idx++);
113       if (idx > IUPACSYMNUM)
114         {
115           *fwdp = NTEND;
116           return 0;
117         }
118       else
119         *fwdp = iupac[idx].comp;
120       fwdp++;
121       bckp--;
122     }
123   *fwdp = NTEND;
124   return(1);
125 }
126   
127 int
128 seqdecode(char *str, char *codeseq)
129 {
130   int idx;
131   int pos;
132
133   pos = 0;
134   while (*codeseq != NTEND)
135     {
136       for (idx = 0; *codeseq != iupac[idx].code && idx < IUPACSYMNUM; idx++)
137         ;
138       if (idx > IUPACSYMNUM)
139         {
140           str[pos] = 'X';
141           return 0;
142         }
143       else
144         str[pos] = iupac[idx].sym;
145       codeseq++;
146       pos++;
147     }
148   str[pos] = '\0';
149   return 1;
150 }
151
152 int
153 seqndecode(
154      char       *str,           /* pre-allocated string to write into */
155      char *codeseq,             /* sequence to decode */
156      int         n)             /* how many bases to decode */
157 {
158   int idx;
159   int pos = 0;
160
161   while (--n >= 0)
162     {
163       for (idx = 0; *codeseq != iupac[idx].code && idx < IUPACSYMNUM; idx++);
164       if (idx > IUPACSYMNUM)
165         {
166           str[pos]  = 'X';
167           return 0;
168         }
169       else
170         str[pos] = iupac[idx].sym;
171       codeseq++;
172       pos++;
173     }
174   str[pos] = '\0';
175   return 1;
176 }
177