Wrapper for Clustal Omega.
[jabaws.git] / binaries / src / clustalo / src / squid / seqencode.c
1 /*****************************************************************
2  * SQUID - a library of functions for biological sequence analysis
3  * Copyright (C) 1992-2002 Washington University School of Medicine
4  * 
5  *     This source code is freely distributed under the terms of the
6  *     GNU General Public License. See the files COPYRIGHT and LICENSE
7  *     for details.
8  *****************************************************************/
9
10 /* seqencode.c
11  * 
12  * Routines for creating and manipulating encoded sequence strings.
13  * RCS $Id: seqencode.c 217 2011-03-19 10:27:10Z andreas $ (Original squid RCS Id: seqencode.c,v 1.3 1999/05/02 21:55:27 eddy Exp)
14  */
15 #include <stdio.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include "squid.h"
19
20                         
21 #ifdef MEMDEBUG
22 #include "dbmalloc.h"
23 #endif
24                                 /* seqcmp()
25                                    returns 0 if s1 == s2
26                                    mismatch number otherwise */
27 int
28 seqcmp(char *s1, char *s2, int allow)
29 {
30   int mmat = 0;
31
32   while ((*s1 != NTEND) && (*s2 != NTEND) && (mmat <= allow)) 
33     {
34       if (!(ntmatch(*s1, *s2)))
35         mmat++;;
36       s1++;
37       s2++;
38     }
39   while ((*s1++ != NTEND) && (mmat <= allow))
40     mmat++;
41   return(mmat);
42 }
43                                 /* seqncmp()
44                                    same as seqcmp but it looks at,
45                                    at most, n positions */
46 int
47 seqncmp(char *s1, char *s2, int n, int allow)
48 {
49   int mmat = 0;
50
51   while ((*s2 != NTEND) &&
52          (n-- != 0))
53     {
54       if ((!(ntmatch(*s1, *s2))) &&
55           (++mmat > allow))
56         return(mmat);
57       s1++;
58       s2++;
59     }
60   while ((n-- != 0) && (*s1++ != NTEND) && (mmat <= allow))
61     mmat++;
62   return (mmat);
63 }
64       
65                                 /* seqencode()
66                                    given a character text string str (A,C,G,T),
67                                    convert to an encoded seq string;
68                                    return 1 for success, 0 if fail */
69 int
70 seqencode(char *codeseq, /* pre-allocated space for answer */
71           char *str)     /* character string to convert    */
72 {
73   char  *ptr;
74   int    idx;
75
76   ptr = codeseq;
77   while (*str != '\0')
78     {
79       if (islower((int) (*str))) *str = (char) toupper((int) (*str));
80       for (idx = 0; *str != iupac[idx].sym && idx <= IUPACSYMNUM; idx++)
81         ;
82       if (idx > IUPACSYMNUM)
83         {
84           *ptr = (char) NTEND;
85           return 0;
86         }
87       else
88         *ptr = iupac[idx].code;
89       ptr++;
90       str++;
91     }
92   *ptr = NTEND;
93   return 1;
94 }
95
96
97 int
98 coded_revcomp(char *comp, char *seq)
99 {
100   long  bases;
101   char *bckp, *fwdp;
102   int   idx;
103   long  pos;
104
105   bases = strlen(seq);
106
107   fwdp = comp;
108   bckp = seq + bases -1;
109   for (pos = 0; pos < bases; pos++)
110     {
111       for (idx = 0; *bckp != iupac[idx].code && idx < IUPACSYMNUM; idx++);
112       if (idx > IUPACSYMNUM)
113         {
114           *fwdp = NTEND;
115           return 0;
116         }
117       else
118         *fwdp = iupac[idx].comp;
119       fwdp++;
120       bckp--;
121     }
122   *fwdp = NTEND;
123   return(1);
124 }
125   
126 int
127 seqdecode(char *str, char *codeseq)
128 {
129   int idx;
130   int pos;
131
132   pos = 0;
133   while (*codeseq != NTEND)
134     {
135       for (idx = 0; *codeseq != iupac[idx].code && idx < IUPACSYMNUM; idx++)
136         ;
137       if (idx > IUPACSYMNUM)
138         {
139           str[pos] = 'X';
140           return 0;
141         }
142       else
143         str[pos] = iupac[idx].sym;
144       codeseq++;
145       pos++;
146     }
147   str[pos] = '\0';
148   return 1;
149 }
150
151 int
152 seqndecode(
153      char       *str,           /* pre-allocated string to write into */
154      char *codeseq,             /* sequence to decode */
155      int         n)             /* how many bases to decode */
156 {
157   int idx;
158   int pos = 0;
159
160   while (--n >= 0)
161     {
162       for (idx = 0; *codeseq != iupac[idx].code && idx < IUPACSYMNUM; idx++);
163       if (idx > IUPACSYMNUM)
164         {
165           str[pos]  = 'X';
166           return 0;
167         }
168       else
169         str[pos] = iupac[idx].sym;
170       codeseq++;
171       pos++;
172     }
173   str[pos] = '\0';
174   return 1;
175 }
176