source: tags/arb_5.0/SECEDIT/SEC_helix.cxx

Last change on this file was 6017, checked in by westram, 15 years ago
  • do not insert \n into error (\n will not display in secedit main window)
  • rename (SEC_root) get_helix → get_helixDef (get_helix is duplicated in SEC_helix_strand)
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : SEC_helix.cxx                                     //
4//   Purpose   : helix related things                              //
5//                                                                 //
6//   Coded by Ralf Westram (coder@reallysoft.de) in August 2007    //
7//   Institute of Microbiology (Technical University Munich)       //
8//   http://www.arb-home.de/                                       //
9//                                                                 //
10// =============================================================== //
11
12#include <cstring>
13
14#include "SEC_root.hxx"
15#include "SEC_helix.hxx"
16
17using namespace std;
18
19const size_t *SEC_root::getHelixPositions(const char *helixNr) const {
20    sec_assert(helixNr);
21    const BI_helix *helix  = get_helixDef();
22    long            start1 = helix->first_position(helixNr);
23
24    if (start1 == -1) return 0;
25
26    size_t        start2 = helix->last_position(helixNr);
27    static size_t pos[4];
28
29    if (size_t(start1)<start2) {
30        pos[0] = size_t(start1);
31        pos[1] = start2;
32    }
33    else {
34        pos[0] = start2;
35        pos[1] = size_t(start1);
36    }
37
38    pos[2] = helix->opposite_position(pos[1]);
39    pos[3] = helix->opposite_position(pos[0]);
40
41    return pos;
42}
43
44// -------------------------------------------
45// current way to save folded helices:
46//
47// save a list of helix-numbers (with sign) seperated by ';'
48// strand start-positions are saved as 'num'
49// segment start-positions are saved as '!num'
50
51char *SEC_xstring_to_foldedHelixList(const char *x_string, size_t xlength, const BI_helix *helix) {
52    GBS_strstruct *out           = GBS_stropen(1000);
53    bool           next_is_start = true;
54
55    for (size_t pos = 0; pos<xlength; ++pos) {
56        if (x_string[pos] == 'x') {
57            BI_PAIR_TYPE  pairType = helix->pairtype(pos);
58            const char   *helixNr  = helix->helixNr(pos);
59
60            if (next_is_start) {
61                sec_assert(pairType != HELIX_NONE);
62                GBS_strcat(out, helixNr);
63            }
64            else {
65                bool is_end_and_start = pairType != HELIX_NONE;
66               
67                sec_assert(helix->pairtype(pos-1) != HELIX_NONE);
68                GBS_strnprintf(out, 20, "!%s", helix->helixNr(pos-1));
69
70                if (is_end_and_start) {
71                    GBS_chrcat(out, ';');
72                    GBS_strcat(out, helixNr);
73                    next_is_start = !next_is_start;
74                }
75            }
76            next_is_start = !next_is_start;
77            GBS_chrcat(out, ';');
78        }
79    }
80
81    return GBS_strclose(out);
82}
83
84char *SEC_foldedHelixList_to_xstring(const char *foldedHelices, size_t xlength, const BI_helix *helix, GB_ERROR& error) {
85    // example of foldedHelices: '-5;!-5;-8;!-8;-9;!-9;9;!9;8;!8;5;!5;-18;!-18;18;!18;-19a;!-19a;19a;!19a;-21;!-21;-24;!-24;24;!24;21;!21;-27;!-27;27;!27;-31;!-31;-43;!-43;43;!43;-46;!-46;46;!46;31;!31;-50;!-50;50;!50;'
86    char *xstring    = (char*)malloc(xlength+1);
87    memset(xstring, '.', xlength);
88    xstring[xlength] = 0;
89
90    sec_assert(error == 0); 
91    error = 0;
92
93    while (!error) {
94        const char *semi = strchr(foldedHelices, ';');
95        if (!semi) break;
96
97        char       *taggedHelixNr = GB_strpartdup(foldedHelices, semi-1);
98        const char *helixNr       = 0;
99        long        xpos          = -1;
100
101        if (taggedHelixNr[0] == '!') { // position behind end of helix
102            helixNr = taggedHelixNr+1;
103            xpos    = helix->last_position(helixNr);
104            if (xpos >= 0) ++xpos;
105        }
106        else { // position at start of helix
107            helixNr = taggedHelixNr;
108            xpos    = helix->first_position(helixNr);
109        }
110
111        if (xpos == -1) {
112            error = GBS_global_string("Can't find helix '%s'", helixNr);
113        }
114        else {
115            sec_assert(xpos >= 0 && size_t(xpos) < xlength);
116            xstring[xpos] = 'x';
117        }
118
119        free(taggedHelixNr);
120
121        foldedHelices = semi+1;
122    }
123
124    return xstring;
125}
126
127// ------------------------------------------------------------------------
128//      xstring was made helix-relative, when saved to old version file
129// ------------------------------------------------------------------------
130
131
132char *old_decode_xstring_rel_helix(GB_CSTR rel_helix, size_t xlength, const BI_helix *helix, int *no_of_helices_ptr)
133{
134    const char *start_helix_nr = 0;
135    int         no_of_helices  = 0;
136    int         start_helix    = 0;
137    int         end_helix      = -1;
138    int         rel_pos        = 0;
139    size_t      lastpos        = helix->size()-1;
140
141    char *x_buffer    = (char*)malloc(xlength+1);
142    memset(x_buffer, '.', xlength);
143    x_buffer[xlength] = 0;
144
145    for (size_t pos=0; ; pos++) {
146        const char   *helix_nr = 0;
147        BI_PAIR_TYPE  pairType = helix->pairtype(pos);
148
149        if (pairType!=HELIX_NONE) {
150            helix_nr = helix->helixNr(pos);
151
152            if (helix_nr==start_helix_nr) { // same helix as last
153                end_helix = pos;
154            }
155            else { // new helix nr
156                if (start_helix_nr) { // not first helix -> write last to xstring
157                insert_helix:
158                    helix_nr = helix->helixNr(pos); // re-init (needed in case of goto insert_helix)
159                    char flag = rel_helix[rel_pos++];
160
161                    no_of_helices++;
162                    if (flag=='1') {
163                        sec_assert(end_helix!=-1);
164                        sec_assert(size_t(start_helix)<xlength);
165                        sec_assert(size_t(end_helix+1)<xlength);
166
167                        x_buffer[start_helix] = 'x';
168                        x_buffer[end_helix+1] = 'x';
169                    }
170                    else if (flag!='0') {
171                        if (flag==0) break; // eos
172
173                        sec_assert(0); // illegal character
174                       
175                        break;
176                    }
177                    if (pos==lastpos) {
178                        break;
179                    }
180                }
181                start_helix = pos;
182                end_helix = pos;
183                start_helix_nr = helix_nr;
184            }
185        }
186        if (pos==lastpos) {
187            if (start_helix_nr) {
188                goto insert_helix;
189            }
190        }
191    }
192
193    *no_of_helices_ptr = no_of_helices;
194    return x_buffer;
195}
Note: See TracBrowser for help on using the repository browser.