source: tags/arb_5.3/ALIV3/a3_helix.cxx

Last change on this file was 4992, checked in by boehnel, 16 years ago

"int → long" to get 64bit work

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 KB
Line 
1// -----------------------------------------------------------------------------
2//  Include-Dateien
3// -----------------------------------------------------------------------------
4
5#include <iostream>
6#include <fstream>
7#include <cstring>
8
9#include "a3_helix.hxx"
10#include "a3_ali.hxx"
11#include "a3_matrix.hxx"
12
13using std::cout;
14
15// -----------------------------------------------------------------------------
16static str CleanHelix ( str hel )
17// -----------------------------------------------------------------------------
18{
19    str heli = strdup(hel),
20        ptr  = heli;
21
22    if (ptr)
23    {
24        int ch;
25       
26        while ((ch = *ptr) != 0)
27        {
28            if (ch == '<') *ptr = '[';
29            if (ch == '>') *ptr = ']';
30
31            ptr++;
32        }
33    }
34
35    return heli;
36}
37
38// -----------------------------------------------------------------------------
39    static int FindHelices ( str       helix,
40                             int       left,
41                             int       right,
42                             A3Matrix &matrix )
43// -----------------------------------------------------------------------------
44{
45    int    error     =  0,
46           increment =  0,
47           count     =  0,
48           last      = -1,
49           pos       =  0;
50    DArray mark(10,5,DARRAY_NOFREE);
51   
52    while (!error && (pos <= right))
53    {
54        if       (helix[pos] == '[') count++, increment = 1;
55        else if ((helix[pos] == ']') && count)
56        {
57            if (increment > 0)
58            {
59                int back = pos - 1;
60               
61                while ((back >= left) && (helix[back] != '[')) back--;
62
63                if      (back < left) error = 2;
64                else if (matrix.Set(pos - left,back - left,(vp)1) ||
65                         matrix.Set(back - left,pos - left,(vp)1)) error = 3;
66                {
67                    if      (last < 0)                     last = mark.Add((vp)pos);
68                    else if ((back - (long)mark[last]) > 1) last = mark.Add((vp)pos);
69                }
70            }
71            else if (increment < 0)
72            {
73                long back    = (long)mark[last],
74                    backend = left,
75                    tmp     = back;
76               
77                while (tmp--) if (matrix.Get(back - left,tmp - left)) break;
78               
79                back = tmp - 1;
80               
81                if (last > 0) backend = (long)mark[last - 1] + 1;
82
83                while (back >= backend)
84                {
85                    if (helix[back] == '[')
86                    {
87                        tmp = back + 1;
88                       
89                        while (tmp <= right)
90                        {
91                            if (matrix.Get(tmp - left,back - left)) break;
92                            else                                    tmp++;
93                        }
94
95                        if (tmp > right)
96                        {
97                            if (matrix.Set(pos - left,back - left,(vp)1) ||
98                                matrix.Set(back - left,pos - left,(vp)1)) error = 3;
99                            else
100                            {
101                                if (last >= 0) mark.Del(last--);
102
103                                if      (last < 0)                     last = mark.Add((vp)pos);
104                                else if ((back - (long)mark[last]) > 1) last = mark.Add((vp)pos);
105
106                                break;
107                            }
108                        }
109                    }
110
111                    if (back == backend)
112                    {
113                        if (last >= 0) mark.Del(last--);
114                       
115                        if (last > 0) backend = (long)mark[last - 1] + 1;
116                        else          backend = left;
117                    }
118                   
119                    back--;
120                }
121            }
122            else error = 1;
123
124            increment = -1;
125            count--;
126        }
127       
128        pos++;
129    }
130
131    return error;
132}
133
134// -----------------------------------------------------------------------------
135    int intcmp ( const void *a,
136                 const void *b )
137// -----------------------------------------------------------------------------
138{
139    int res  = 0,
140        diff = *(int*)a - *(int*)b;
141
142    if      (diff < 0) res = -1;
143    else if (diff > 0) res =  1;
144
145    return res;
146}
147
148// -----------------------------------------------------------------------------
149    int hmatchcmp ( const void *a,
150                    const void *b )
151// -----------------------------------------------------------------------------
152{
153    int res  = 0,
154        diff = (*(HMatch**)a)->first - (*(HMatch**)b)->first;
155
156    if      (diff < 0) res = -1;
157    else if (diff > 0) res =  1;
158
159    return res;
160}
161
162// -----------------------------------------------------------------------------
163    void hmatchdump ( vp val )
164// -----------------------------------------------------------------------------
165{
166    HMatch *m = (HMatch*)val;
167   
168    if (m) cout << "   " << m->first << ", " << m->last;
169}
170
171// -----------------------------------------------------------------------------
172    HMatch &A3Helix::GetPart ( int part )
173// -----------------------------------------------------------------------------
174{
175    HMatch *area  = NULL;
176    int     parts = Parts();
177   
178    if ((part >= 0) && (part < parts))
179    {
180        int anz = match.Elements(),
181            beg = !!(((HMatch*)match[0])->first),
182            end = (((HMatch*)match[anz - 1])->last == (length - 1)),
183            pos = 0;
184       
185        area  = new HMatch;
186        parts = 0;
187
188        while (pos <= anz)
189        {
190            if (!pos)
191            {
192                area->first = 0;
193
194                if (beg) area->last = ((HMatch*)match[pos])->last;
195                else     area->last = ((HMatch*)match[pos + 1])->last;
196            }
197            else if (pos == anz)
198            {
199                if (!beg) break;
200
201                area->first = ((HMatch*)match[pos - 1])->first;
202                area->last  = length - 1;
203            }
204            else if (pos == (anz - 1))
205            {
206                if (beg)
207                {
208                    area->first = ((HMatch*)match[pos - 1])->first;
209                    area->last  = ((HMatch*)match[pos])->last;
210                }
211
212                if (end) break;
213
214                if (!beg)
215                {
216                    area->first = ((HMatch*)match[pos])->first;
217                    area->last  = length - 1;
218                }
219            }
220            else
221            {
222                if (beg)
223                {
224                    area->first = ((HMatch*)match[pos - 1])->first;
225                    area->last  = ((HMatch*)match[pos])->last;
226                }
227                else
228                {
229                    area->first = ((HMatch*)match[pos])->first;
230                    area->last  = ((HMatch*)match[pos + 1])->last;
231                }
232            }
233           
234            if (parts == part) break;
235
236            parts++;
237            pos++;
238        }
239    }
240   
241    return *area;
242}
243
244// -----------------------------------------------------------------------------
245    A3Helix::A3Helix ( void ) : match()
246// -----------------------------------------------------------------------------
247{
248    helix     =
249    consensus = NULL;
250    length    = 0;
251
252    match.Free(DARRAY_NOFREE);
253    match.Null(DARRAY_NONULL);
254    match.Sort(intcmp);
255}
256
257// -----------------------------------------------------------------------------
258    A3Helix::A3Helix ( str     hel,
259                       str     kon,
260                       DArray &mat ) : match(mat)
261// -----------------------------------------------------------------------------
262{
263    helix     = CleanHelix(hel);
264    consensus = strdup(kon);
265    length    = strlen(helix);
266}
267
268// -----------------------------------------------------------------------------
269    A3Helix::~A3Helix ( void )
270// -----------------------------------------------------------------------------
271{
272    if (helix)  delete helix;
273    if (consensus)  delete consensus;
274}
275
276// -----------------------------------------------------------------------------
277    void A3Helix::Set ( DArray &mat )
278// -----------------------------------------------------------------------------
279{
280    match = mat;
281}
282
283// -----------------------------------------------------------------------------
284    int A3Helix::Parts ( void )
285// -----------------------------------------------------------------------------
286{
287    int anz   = match.Elements(),
288        parts = anz;
289
290    if (parts)
291    {
292        parts++;
293
294        if (!((HMatch*)match[0])->first) parts--;
295        if (((HMatch*)match[anz - 1])->last == (length - 1)) parts--;
296    }
297   
298    return parts;
299}
300
301// -----------------------------------------------------------------------------
302    DArray &A3Helix::Helices ( int part,
303                               int minlen )
304// -----------------------------------------------------------------------------
305{
306    DArray   *helices = new DArray;
307    HMatch   &area    = GetPart(part);
308    int       len     = area.last - area.first + 1;
309    A3Matrix  mat(len,DARRAY_NOFREE);
310
311    if (!FindHelices(helix,area.first,area.last,mat)) mat.Dump(NULL);
312   
313    delete &area;
314   
315    return *helices;
316}
317
318// -----------------------------------------------------------------------------
319    void A3Helix::Dump ( int all )
320// -----------------------------------------------------------------------------
321{
322    if (all)
323    {
324        cout << "\nhelix     = "   << helix;
325        // cout << "\n\nkonsensus = " << konsensus;
326        cout << "\n\nlength    = " << length;
327
328        cout << "\n";
329    }
330
331    match.Dump(hmatchdump);
332}
Note: See TracBrowser for help on using the repository browser.