1 #include <objres.h>
2 #include <objseq.h>
3 #include <seqport.h>
4 #include <seqfltr.h>
5 #include <dust.h>
6 #include <aacomp.h>
7 
8 /* defines */
9 #define FILTS            32
10 
11 /* functions - internal */
12 
ReadFilter(CharPtr res,FloatHiPtr scr,CharPtr buf,CharPtr filename)13 static Boolean ReadFilter (CharPtr res, FloatHiPtr scr,
14                            CharPtr buf, CharPtr filename)
15 {
16   FILE    *fin;
17   Int2    i, val;
18   Int4    type;
19   long    ltype;
20   Char    buff[256];
21 
22   static Char c[] =
23           {'A','B','C','D','G','H','K','M','N','R','S','T','V','W','Y'};
24 
25   for (i = 0; i < FILTS; i++)
26     res[i] = '\0';
27   for (i = 0; i < FILTS; i++)
28     scr[i] = 0.0;
29 
30   if ((fin = FileOpen (buf, "r")) == NULL)
31   {
32     if ((fin = FileOpen (filename, "r")) == NULL)
33     {
34       return FALSE;
35     }
36   }
37 
38   val = 0;
39   type = -1;
40   while ((FileGets (buff, sizeof (buff), fin)) != NULL)
41   {
42     if (buff[0] == ';')
43       continue;
44     if (type == -1)
45     {
46       sscanf (buff, "%ld", &ltype);
47       type = (Int4) ltype;
48       if (type != 1)
49       {
50         FileClose (fin);
51         return FALSE;
52       }
53       FileGets (buff, sizeof (buff), fin);
54       continue;
55     }
56     res[val] = c[val];
57     sscanf (buff, "%ld", &ltype);
58     scr[val] = (FloatHi) ltype;
59     scr[val] /= 100;
60     val++;
61   }
62   FileClose (fin);
63   return TRUE;
64 }
65 
NTDust(SeqPortPtr spp,Int4 length,Int4 level,Int4 window,Int4 minwin,Int4 linker)66 static FloatHiPtr NTDust (SeqPortPtr spp, Int4 length,
67                           Int4 level, Int4 window,
68                           Int4 minwin, Int4 linker)
69 {
70   FloatHiPtr fptr;
71 
72   fptr = DustGraph (spp, length, (Int2) level, (Int2) window,
73                     (Int2) minwin, (Int2) linker);
74   return fptr;
75 }
76 
NTComposition(SeqPortPtr spp,Int4 length,Int4 window,Uint1 filtertype)77 static FloatHiPtr NTComposition (SeqPortPtr spp, Int4 length,
78                                  Int4 window, Uint1 filtertype)
79 {
80   Int4         i, n, iring;
81   Boolean      flagMatch;
82   FloatHi      NTcount;
83   FloatHiPtr   ringptr;
84   Char         res[FILTS], chres;
85   FloatHi      scr[FILTS];
86   Boolean      flagFilter;
87   FloatHiPtr   fhead, fptr;
88   Char         buf[PATH_MAX], filename[32];
89 
90   if (length < window)
91     return NULL;
92 
93   if (!FindPath ("ncbi", "ncbi", "data", buf, sizeof (buf)))
94     return NULL;
95 
96   fptr = (FloatHiPtr) MemNew ((sizeof (FloatHi)) * length);
97   fhead = fptr;
98   for (i = 0; i < length; i++)
99     *fptr++ = 0.0;
100 
101   fptr = fhead;
102   switch (filtertype)
103   {
104    case NA_FILTER_GC:
105     StringCpy (filename, "KSgc.flt");
106     FileBuildPath (buf, NULL, "KSgc.flt");
107     break;
108    case NA_FILTER_AT:
109     StringCpy (filename, "KSat.flt");
110     FileBuildPath (buf, NULL, "KSat.flt");
111     break;
112    case NA_FILTER_PUR:
113     StringCpy (filename, "KSpur.flt");
114     FileBuildPath (buf, NULL, "KSpur.flt");
115     break;
116    case NA_FILTER_PYR:
117     StringCpy (filename, "KSpyr.flt");
118     FileBuildPath (buf, NULL, "KSpyr.flt");
119     break;
120    default:
121     MemFree (fptr);
122     return NULL;
123   }
124   flagFilter = ReadFilter (res, scr, buf, filename);
125   if (!flagFilter)
126   {
127     MemFree (fptr);
128     return NULL;
129   }
130 
131 /* set up ring buffer */
132   ringptr = (FloatHiPtr) MemNew (sizeof (FloatHi) * window);
133   iring = 0;
134   NTcount = 0;
135   for (i = 0; i < window; i++)
136   {
137     chres = SeqPortGetResidue (spp);
138     flagMatch = 0;
139     n = 0;
140     while (res[n] != '\0')
141     {
142       if (chres == res[n])
143       {
144         flagMatch = 1;
145         break;
146       }
147       n++;
148     }
149     switch (flagMatch)
150     {
151       case 1:
152         NTcount += scr[n];
153         ringptr[iring] = scr[n];
154         break;
155       default:
156         ringptr[iring] = 0;
157         break;
158     }
159     iring++;
160     if (iring == window)
161       iring = 0;
162   }
163 
164   for (i = 0; i < window/2; i++)
165     fptr++;
166   *fptr = NTcount/window*100;
167   fptr++;
168 
169 /* calculate average */
170   for (i = window; i < length; i++)
171   {
172     NTcount = NTcount - ringptr[iring];
173     chres = SeqPortGetResidue (spp);
174     flagMatch = FALSE;
175     n = 0;
176     while (res[n] != '\0')
177     {
178       if (chres == res[n])
179       {
180         flagMatch = TRUE;
181         break;
182       }
183       n++;
184     }
185     switch (flagMatch)
186     {
187       case 1:
188         NTcount += scr[n];
189         ringptr[iring] = scr[n];
190         break;
191       default:
192         ringptr[iring] = 0;
193         break;
194     }
195     iring++;
196     if (iring == window)
197       iring = 0;
198     *fptr = NTcount/window*100;
199     fptr++;
200   }
201   MemFree (ringptr);
202   return fhead;
203 }
204 
205 /* functions - external */
206 
FilterSeq(SeqPortPtr spp,Int4 start,Int4 end,FloatHiPtr scr,CharPtr res,Int4Ptr exval,Uint1 filtertype)207 extern SeqGraphPtr FilterSeq (SeqPortPtr spp, Int4 start, Int4 end,
208                               FloatHiPtr scr, CharPtr res, Int4Ptr exval,
209                               Uint1 filtertype)
210 {
211   SeqGraphPtr   sgp = NULL;
212   Int4          gwidth = 500;
213   FloatHiPtr    fptr;
214 
215   if (spp != NULL)
216   {
217     if ((sgp = SeqGraphNew ()) != NULL)
218     {
219 /* type and number of values and compression */
220       sgp->numval = end - start + 1;
221       sgp->compr = (Int4) (sgp->numval / gwidth);
222       if ((sgp->numval%gwidth) != 0)
223         sgp->compr += 1;
224 /* graph type */
225       sgp->flags[2] = 1;
226 /* scaling */
227       sgp->flags[1] = 1;
228       sgp->a = 2;
229       sgp->b = 0;
230 /* do it */
231       switch (filtertype)
232       {
233        case NA_FILTER_GC:
234        case NA_FILTER_AT:
235        case NA_FILTER_PUR:
236        case NA_FILTER_PYR:
237         fptr = NTComposition (spp, sgp->numval, exval[0], filtertype);
238         break;
239        case NA_FILTER_DUST:
240         fptr = NTDust (spp, sgp->numval,
241                        exval[0], exval[1], exval[2], exval[3]);
242         break;
243        case AA_FILTER_COMP:
244        case AA_FILTER_COMP_HOPP:
245        case AA_FILTER_COMP_KYTE:
246         fptr = AAComposition (spp, start, end, sgp->numval,
247                               scr, res, exval[0]);
248         break;
249        default:
250         sgp = SeqGraphFree (sgp);
251         break;
252       }
253       if (fptr == NULL)
254       {
255         sgp = SeqGraphFree (sgp);
256       }
257       else
258       {
259         sgp->values = (Pointer) fptr;
260 /* min/max */
261         sgp->max.realvalue = 100.0;
262         sgp->min.realvalue = 0.0;
263         sgp->axis.realvalue = 0.0;
264       }
265     }
266   }
267   return sgp;
268 }
269