source: tags/svn.1.5.4/RNA3D/RNA3D_StructureData.cxx

Last change on this file was 8036, checked in by westram, 14 years ago

merge from dev [7950] [7951] [7952] [7953] [7954] [7955] [7957]

  • replaced weird GBS_find_lib_file (GB_lib_file, GB_property_file, GB_arbtcpdat_path, GB_path_in_ARBLIB, ..)
  • changed handling of property-files (removed path prefix '.arb_prop')
  • replaced several hardcoded paths
  • fixed arb_notify
  • fixed crash when showing "lib/message" on startup (outside GB_shell)
  • cleaned up error handling in start_macro_recording
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 50.3 KB
Line 
1#include "RNA3D_GlobalHeader.hxx"
2#include "RNA3D_Global.hxx"
3#include "RNA3D_OpenGLGraphics.hxx"
4#include "RNA3D_Graphics.hxx"
5#include "RNA3D_StructureData.hxx"
6
7#include <cerrno>
8#include <string>
9#include <iostream>
10#include <fstream>
11
12#include <arbdbt.h>
13#include <aw_msg.hxx>
14#include <aw_awars.hxx>
15#include <aw_root.hxx>
16#include <ed4_extern.hxx>
17#include <ed4_plugins.hxx>
18#include <BI_basepos.hxx>
19
20
21#define COLORLINK (ED4_G_SBACK_0 - RNA3D_GC_SBACK_0)  // to link to the colors defined in primary editor ed4_defs.hxx
22#define SAICOLORS (ED4_G_CBACK_0 - RNA3D_GC_CBACK_0)
23
24using namespace std;
25
26static Struct3Dinfo   *start3D                 = NULL;
27static Struct2Dinfo   *start2D                 = NULL;
28static Struct2Dplus3D *start2D3D               = NULL;
29static HelixNrInfo    *start                   = NULL;
30static CurrSpecies    *startSp                 = NULL;
31static bool            bOldSpeciesDataExists   = false;
32static Insertions     *startIns                = NULL;
33static bool            bOldInsertionDataExists = false;
34static bool            bStartPosStored         = false;
35static bool            bEndPosStored           = false;
36
37static char *find_data_file(const char *name) {
38    char *fname = GB_lib_file(false, "rna3d/", name);
39    if (!fname) throw string("file not found: ")+name;
40    return fname;
41}
42
43static void throw_IO_error(const char *filename) __ATTR__NORETURN;
44static void throw_IO_error(const char *filename) {
45    string error = string("IO-Error: ")+strerror(errno)+" ('"+filename+"')";
46    throw error;
47}
48
49GBDATA *Structure3D::gb_main = 0;
50
51Structure3D::Structure3D(ED4_plugin_host& host_)
52    : strCen(new Vector3(0.0, 0.0, 0.0)),
53      iInterval(25),
54      iMapSAI(0),
55      iMapSearch(0),
56      iMapEnable(0),
57      iStartPos(0),
58      iEndPos(0),
59      iEColiStartPos(0),
60      iEColiEndPos(0),
61      iTotalSubs(0),
62      iTotalDels(0),
63      iTotalIns(0),
64      LSU_molID(3), // default molecule to load in case of 23S rRNA : 1C2W
65      HelixBase(500),
66      EColiRef(NULL),
67      Host(host_),
68      GRAPHICS(new OpenGLGraphics())
69{
70}
71
72Structure3D::~Structure3D() {
73    delete GRAPHICS;
74    delete strCen;
75}
76
77
78void Structure3D::StoreCoordinates(float x, float y, float z, char base, unsigned int pos) {
79    Struct3Dinfo *data, *temp;
80    data = new Struct3Dinfo;
81    data->x    = x;
82    data->y    = y;
83    data->z    = z;
84    data->base = base;
85    data->pos  = pos;
86    data->next = NULL;
87
88    if (start3D == NULL)
89        start3D = data;
90    else {
91        temp = start3D;
92        // We know this is not NULL - list not empty!
93        while (temp->next != NULL) {
94            temp = temp->next;  // Move to next link in chain
95        }
96        temp->next = data;
97    }
98}
99
100// ------------------Selecting rRNA type--------------------------//
101
102int Structure3D::FindTypeOfRNA() {
103    int rnaType = 0;
104    GB_push_transaction(gb_main);  // opening a transaction
105
106    GBDATA *gbTemplate = GBT_find_SAI(gb_main, "ECOLI");
107    if (!gbTemplate) {
108        aw_message("SAI:ECOLI not found");
109    }
110    else {
111        char   *ali_name          = GBT_get_default_alignment(gb_main);
112        GBDATA *gbAlignment       = GB_entry(gbTemplate, ali_name);
113        GBDATA *gbTemplateSeqData = gbAlignment ? GB_entry(gbAlignment, "data") : 0;
114
115        if (!gbTemplateSeqData) {
116          aw_message("Could not find species in the database!");
117        }
118        else {
119            const char *pTemplateSeqData  = GB_read_char_pntr(gbTemplateSeqData);
120            int iSeqLen = strlen(pTemplateSeqData);
121            int iBaseCount = 0;
122            for (int i = 0; i<iSeqLen; i++) {
123                if ((pTemplateSeqData[i] != '.') && (pTemplateSeqData[i] != '-')) {
124                    iBaseCount++;
125                }
126            }
127
128            if (iBaseCount > 2000)      rnaType = LSU_23S;
129            else if (iBaseCount > 300)  rnaType = SSU_16S;
130            else                        rnaType = LSU_5S;
131        }
132    }
133    GB_pop_transaction(gb_main);
134
135    return rnaType;
136}
137
138// ----------Delete old molecule data-------------------------------//
139// Struct2Dinfo, Struct2Dplus3D, Struct3Dinfo, HelixNrInfo, Insertions
140
141void Structure3D::DeleteOldMoleculeData() {
142    // Struct2Dinfo -> start2D
143    {
144        Struct2Dinfo *tmp, *data;
145        for (data = start2D; data != NULL; data = tmp) {
146            tmp = data->next;
147            delete data;
148        }
149        start2D = NULL;
150    }
151
152    // Struct2Dplus3D ->start2D3D
153    {
154        Struct2Dplus3D *tmp, *data;
155        for (data = start2D3D; data != NULL; data = tmp) {
156            tmp = data->next;
157            delete data;
158        }
159        start2D3D = NULL;
160    }
161
162    // Struct3Dinfo ->start3D
163    {
164        Struct3Dinfo *tmp, *data;
165        for (data = start3D; data != NULL; data = tmp) {
166            tmp = data->next;
167            delete data;
168        }
169        start3D = NULL;
170    }
171
172    // HelixNrInfo -> start
173    {
174        HelixNrInfo *tmp, *data;
175        for (data = start; data != NULL; data = tmp) {
176            tmp = data->next;
177            delete data;
178        }
179        start = NULL;
180    }
181
182    // Delete insertions data
183    DeleteOldInsertionData();
184
185    // Delete mapped species data
186    DeleteOldSpeciesData ();
187}
188
189// =========== Reading 3D Coordinates from PDB file ====================//
190
191void Structure3D::ReadCoOrdinateFile() {
192    static char *DataFile = 0;
193    int rnaType    = FindTypeOfRNA();
194
195    RNA3D->bDisplayComments = true; // displaying comments in main window
196
197    switch (rnaType) {
198    case LSU_23S:
199        switch (LSU_molID) {
200        case _1PNU:
201            DataFile = find_data_file("Ecoli_1PNU_23S_rRNA.pdb");
202            sprintf(globalComment, "The 3D molecule rendered from PDB entry : 1PNU at 8.7 Angstrom.");
203            break;
204        case _1VOR:
205            DataFile = find_data_file("Ecoli_1VOR_23S_rRNA.pdb");
206            sprintf(globalComment, "The 3D molecule is rendered from PDB entry [1VOR] with 11.5 Angstrom resolution.");
207            break;
208        case _1C2W:
209            DataFile = find_data_file("Ecoli_1C2W_23S_rRNA.pdb");
210            sprintf(globalComment, "The 3D molecule is rendered from PDB entry [1C2W] with 7.5 Angstrom resolution.");
211            break;
212        }
213        break;
214    case LSU_5S:
215        DataFile = find_data_file("Ecoli_1C2X_5S_rRNA.pdb");
216        sprintf(globalComment, "The 3D molecule is rendered from PDB entry [1C2X] with 7.5 Angstrom resolution.");
217        break;
218    case SSU_16S:
219        DataFile = find_data_file("Ecoli_1M5G_16S_rRNA.pdb");
220        sprintf(globalComment, "The 3D molecule is rendered from PDB entry [1M5G] with 11.5 Angstrom resolution.");
221        break;
222    }
223
224    char      buf[256];
225
226    float X, Y, Z;
227    unsigned int pos;
228    char Base;
229
230    ifstream readData;
231    readData.open(DataFile, ios::in);
232    if (!readData.is_open()) {
233        throw_IO_error(DataFile);
234    }
235
236    int cntr = 0;
237    unsigned int last3Dpos = 0;
238
239    static bool bEColiStartPosStored = false;
240
241    while (!readData.eof()) {
242        readData.getline(buf, 100);
243        string tmp, atom, line = string(buf);
244
245        if ((line.find("ATOM") != string::npos) || (line.find("HETATM") != string::npos)) {
246            atom = (line.substr(77, 2)).c_str();
247            if (atom.find("P") != string::npos) {
248                tmp = (line.substr(18, 3)).c_str();
249                Base = tmp[1];
250                pos  = atoi((line.substr(22, 4)).c_str());
251                X    = atof((line.substr(31, 8)).c_str());
252                Y    = atof((line.substr(39, 8)).c_str());
253                Z    = atof((line.substr(47, 8)).c_str());
254
255                // special filter for 23S rRNA structure (IVOR/IPNU)
256                // IVOR/IPNU contains artifacts and are not mentioned in any of the
257                // remarks of PDB file
258                if (last3Dpos != pos && !(pos >= 3093)) {
259                    StoreCoordinates(X, Y, Z, Base, pos);
260                    last3Dpos = pos;
261                    strCen->x += X; strCen->y += Y; strCen->z += Z;
262                    cntr++;
263                }
264                if (!bEColiStartPosStored) {
265                    iEColiStartPos = pos;
266                    bEColiStartPosStored = true;
267                }
268            }
269        }
270    }
271    iEColiEndPos = pos;
272
273    cout<<"--------------------------------------------------"<<endl
274        <<globalComment<<endl
275        <<"Structure contains "<<iEColiEndPos-iEColiStartPos<<"( "<<iEColiStartPos<<" - "
276        <<iEColiEndPos<<" ) positions."<<endl
277        <<"---------------------------------------------------"<<endl;
278
279    strCen->x = strCen->x/cntr;
280    strCen->y = strCen->y/cntr;
281    strCen->z = strCen->z/cntr;
282
283    readData.close();
284    free(DataFile);
285}
286
287
288void Structure3D::Store2Dinfo(char *info, int pos, int helixNr) {
289    Struct2Dinfo *data, *temp;
290    data = new Struct2Dinfo;
291    data->base    = info[0];
292    data->mask    = info[1];
293    data->code    = info[2];
294    data->pos     = pos;
295    data->helixNr = helixNr;
296
297    data->next = NULL;
298
299    if (start2D == NULL)
300        start2D = data;
301    else {
302        temp = start2D;
303        // We know this is not NULL - list not empty!
304        while (temp->next != NULL) {
305            temp = temp->next;  // Move to next link in chain
306        }
307        temp->next = data;
308    }
309}
310
311// =========== Reading Secondary Structure Data from Ecoli Secondary Structure Mask file ====================//
312
313void Structure3D::GetSecondaryStructureInfo() {
314    static char *DataFile = 0;
315    int rnaType    = FindTypeOfRNA();
316
317     switch (rnaType) {
318     case LSU_23S:
319         DataFile = find_data_file("SecondaryStructureModel_23SrRNA.data");
320         break;
321     case LSU_5S:
322         DataFile = find_data_file("SecondaryStructureModel_5SrRNA.data");
323         break;
324     case SSU_16S:
325         DataFile = find_data_file("SecondaryStructureModel_16SrRNA.data");
326         break;
327     }
328
329    char  buf[256];
330
331    int pos, helixNr, lastHelixNr; lastHelixNr = 0;
332    char info[4];
333    info[3] = '\0';
334    bool insideHelix = false;
335    bool skip = false;
336
337    ifstream readData;
338    readData.open(DataFile, ios::in);
339    if (!readData.is_open()) {
340        throw_IO_error(DataFile);
341    }
342
343    while (!readData.eof()) {
344        readData.getline(buf, 100);
345        char *tmp;
346        tmp = strtok(buf, " ");
347        for (int i = 0; tmp != NULL; tmp = strtok(NULL, " "), i++)
348            {
349                switch (i) {
350                case 0: pos = atoi(tmp);      break;
351                case 1: info[0] = tmp[0];     break;
352                case 2: info[1] = tmp[0];     break;
353                case 3: info[2] = tmp[0];     break;
354                case 4: helixNr = atoi(tmp); break;
355                }
356            }
357        if (((info[2] == 'S') || (info[2] == 'E')) && (helixNr > 0))  lastHelixNr = helixNr;
358
359        if ((info[2] == 'S') || (info[2] == 'E')) {
360            if (!insideHelix) insideHelix = true;
361            else {
362                Store2Dinfo(info, pos, lastHelixNr);
363                skip        = true;
364                insideHelix = false;
365            }
366        }
367        if (insideHelix) Store2Dinfo(info, pos, lastHelixNr);
368        else {
369            if (skip) skip = false;
370            else Store2Dinfo(info, pos, 0);
371        }
372    }
373    readData.close();
374    free(DataFile);
375}
376
377void Structure3D::Store2D3Dinfo(Struct2Dinfo *s2D, Struct3Dinfo *s3D) {
378    Struct2Dplus3D *data, *temp;
379    data = new Struct2Dplus3D;
380    data->base    = s2D->base;
381    data->mask    = s2D->mask;
382    data->code    = s2D->code;
383    data->pos     = s2D->pos;
384    data->helixNr = s2D->helixNr;
385    data->x       = s3D->x;
386    data->y       = s3D->y;
387    data->z       = s3D->z;
388    data->next    = NULL;
389
390    if (start2D3D == NULL)
391        start2D3D = data;
392    else {
393        temp = start2D3D;
394        while (temp->next != NULL) {
395            temp = temp->next;
396        }
397        temp->next = data;
398    }
399}
400
401// =========== Combining Secondary Structure Data with 3D Coordinates =======================//
402
403void Structure3D::Combine2Dand3DstructureInfo() {
404    Struct3Dinfo *temp3D;
405    Struct2Dinfo *temp2D;
406    int cntr = 0;
407
408    cout<<"Missing Base Positions : "<<endl;
409
410    temp3D = start3D;
411    temp2D = start2D;
412    while ((temp3D != NULL) &&  (temp2D != NULL)) {
413        if (temp2D->pos == temp3D->pos) {
414            Store2D3Dinfo(temp2D, temp3D);
415        }
416        else {
417            while (temp2D->pos != temp3D->pos) {
418                cout<<temp2D->pos<<", "; // missing base positions
419                cntr++;
420                temp2D = temp2D->next;
421            }
422            Store2D3Dinfo(temp2D, temp3D);
423        }
424
425        temp3D = temp3D->next;
426        temp2D = temp2D->next;
427    }
428    cout<<endl<<"Total No. of bases missing = "<<cntr<<endl;
429
430    // printing comments in the main window
431    {
432        RNA3D->bDisplayComments = true;
433        char buf[256];
434        sprintf(buf, "Total No. of bases missing = %d. See the console messages for the actual missing base positions.", cntr);
435        strcat(globalComment, buf);
436    }
437}
438
439void Structure3D::PointsToQuads(float x, float y, float z) {
440
441    if (RNA3D->bPointSpritesSupported) {
442        glVertex3f(x, y, z);
443    }
444    else {
445        glBegin(GL_QUADS);
446        // Front Face
447        glTexCoord2f(0, 0); glVertex3f(x - 1, y + 1, z + 1);
448        glTexCoord2f(1, 0); glVertex3f(x + 1, y + 1, z + 1);
449        glTexCoord2f(1, 1); glVertex3f(x + 1, y - 1, z + 1);
450        glTexCoord2f(0, 1); glVertex3f(x - 1, y - 1, z + 1);
451
452        // Back Face
453        glTexCoord2f(0, 0); glVertex3f(x + 1, y + 1, z - 1);
454        glTexCoord2f(1, 0); glVertex3f(x - 1, y + 1, z - 1);
455        glTexCoord2f(1, 1); glVertex3f(x - 1, y - 1, z - 1);
456        glTexCoord2f(0, 1); glVertex3f(x + 1, y - 1, z - 1);
457
458        // Top Face
459        glTexCoord2f(0, 0); glVertex3f(x + 1, y + 1, z + 1);
460        glTexCoord2f(1, 0); glVertex3f(x - 1, y + 1, z + 1);
461        glTexCoord2f(1, 1); glVertex3f(x - 1, y + 1, z - 1);
462        glTexCoord2f(0, 1); glVertex3f(x + 1, y + 1, z - 1);
463
464        // Bottom Face
465        glTexCoord2f(0, 0); glVertex3f(x + 1, y - 1, z - 1);
466        glTexCoord2f(1, 0); glVertex3f(x - 1, y - 1, z - 1);
467        glTexCoord2f(1, 1); glVertex3f(x - 1, y - 1, z + 1);
468        glTexCoord2f(0, 1); glVertex3f(x + 1, y - 1, z + 1);
469
470        // Left Face
471        glTexCoord2f(0, 0); glVertex3f(x + 1, y + 1, z + 1);
472        glTexCoord2f(1, 0); glVertex3f(x + 1, y + 1, z - 1);
473        glTexCoord2f(1, 1); glVertex3f(x + 1, y - 1, z - 1);
474        glTexCoord2f(0, 1); glVertex3f(x + 1, y - 1, z + 1);
475
476        // Right Face
477        glTexCoord2f(0, 0); glVertex3f(x - 1, y + 1, z - 1);
478        glTexCoord2f(1, 0); glVertex3f(x - 1, y + 1, z + 1);
479        glTexCoord2f(1, 1); glVertex3f(x - 1, y - 1, z + 1);
480        glTexCoord2f(0, 1); glVertex3f(x - 1, y - 1, z - 1);
481
482        glEnd();
483    }
484}
485
486void Structure3D::PositionsToCoordinatesDispList(int listID, int *pos, int len) {
487    Struct2Dplus3D *t;
488    int tmpPos = 0;
489
490    glNewList(listID, GL_COMPILE);
491    {
492        if (RNA3D->bPointSpritesSupported) {
493            glBegin(GL_POINTS);
494        }
495        for (int i = 0; i < len; i++)
496            {
497                tmpPos = pos[i];
498                t = start2D3D;
499                while (t != NULL) {
500                    if (t->pos == tmpPos) {
501                        PointsToQuads(t->x, t->y, t->z);
502                        break;
503                    }
504                    t = t->next;
505                }
506            }
507        if (RNA3D->bPointSpritesSupported) {
508            glEnd();
509        }
510    }
511    glEndList();
512}
513
514void Structure3D::GenerateSecStructureNonHelixRegions() {
515    Struct2Dplus3D *t;
516    const int MAX_BASE = 1000;
517    int baseA[MAX_BASE], baseG[MAX_BASE], baseC[MAX_BASE], baseU[MAX_BASE];
518    int a, g, c, u; a=g=c=u=0;
519
520    {
521        t = start2D3D;
522        while (t != NULL) {
523            if (t->helixNr == 0) {
524                switch (t->base)
525                    {
526                    case 'A': baseA[a++] = t->pos; break;
527                    case 'G': baseG[g++] = t->pos; break;
528                    case 'C': baseC[c++] = t->pos; break;
529                    case 'U': baseU[u++] = t->pos; break;
530                    }
531            }
532            t = t->next;
533        }
534    }
535
536    PositionsToCoordinatesDispList(NON_HELIX_A, baseA, a);
537    PositionsToCoordinatesDispList(NON_HELIX_G, baseG, g);
538    PositionsToCoordinatesDispList(NON_HELIX_C, baseC, c);
539    PositionsToCoordinatesDispList(NON_HELIX_U, baseU, u);
540}
541
542void Structure3D::GenerateSecStructureHelixRegions() {
543    Struct2Dplus3D *t;
544    const int MAX_BASE = 1000;
545    int baseA[MAX_BASE], baseG[MAX_BASE], baseC[MAX_BASE], baseU[MAX_BASE];
546    int a, g, c, u; a=g=c=u=0;
547
548    {
549        t = start2D3D;
550        while (t != NULL) {
551            if (t->helixNr > 0) {
552                if ((t->mask == '[') || (t->mask == ']') ||
553                    (t->mask == '<') || (t->mask == '>'))
554                    {
555                        switch (t->base)
556                            {
557                            case 'A': baseA[a++] = t->pos; break;
558                            case 'G': baseG[g++] = t->pos; break;
559                            case 'C': baseC[c++] = t->pos; break;
560                            case 'U': baseU[u++] = t->pos; break;
561                            }
562                    }
563            }
564            t = t->next;
565        }
566    }
567
568    PositionsToCoordinatesDispList(HELIX_A, baseA, a);
569    PositionsToCoordinatesDispList(HELIX_G, baseG, g);
570    PositionsToCoordinatesDispList(HELIX_C, baseC, c);
571    PositionsToCoordinatesDispList(HELIX_U, baseU, u);
572}
573
574void Structure3D::GenerateSecStructureUnpairedHelixRegions() {
575    Struct2Dplus3D *t;
576    const int MAX_BASE = 500;
577    int baseA[MAX_BASE], baseG[MAX_BASE], baseC[MAX_BASE], baseU[MAX_BASE];
578    int a, g, c, u; a=g=c=u=0;
579
580    {
581        t = start2D3D;
582        while (t != NULL) {
583            if (t->helixNr > 0) {
584                if (t->mask == '.')
585                    {
586                        switch (t->base)
587                            {
588                            case 'A': baseA[a++] = t->pos; break;
589                            case 'G': baseG[g++] = t->pos; break;
590                            case 'C': baseC[c++] = t->pos; break;
591                            case 'U': baseU[u++] = t->pos; break;
592                            }
593                    }
594            }
595            t = t->next;
596        }
597    }
598
599    PositionsToCoordinatesDispList(UNPAIRED_HELIX_A, baseA, a);
600    PositionsToCoordinatesDispList(UNPAIRED_HELIX_G, baseG, g);
601    PositionsToCoordinatesDispList(UNPAIRED_HELIX_C, baseC, c);
602    PositionsToCoordinatesDispList(UNPAIRED_HELIX_U, baseU, u);
603}
604
605// ==============================================================================
606// Tertiary Interactions of 16S ribosomal RNA model of E.coli.
607// Reference : http://www.rna.icmb.utexas.edu/
608// Year of Last Update : 2001.
609// Pseudoknots and Triple Base pairs are extracted and displayed in
610// the 3D model.
611// ==============================================================================
612
613void Structure3D::GenerateTertiaryInteractionsDispLists() {
614    Struct2Dplus3D *t;
615    static char *DataFile = 0;
616    int rnaType    = FindTypeOfRNA();
617
618    switch (rnaType) {
619    case LSU_23S:
620        DataFile = find_data_file("TertiaryInteractions_23SrRNA.data");
621        break;
622    case LSU_5S:
623        cout<<"There are no tertiary interactions observed in 5S rRNA model!"<<endl;
624        return;
625    case SSU_16S:
626        DataFile = find_data_file("TertiaryInteractions_16SrRNA.data");
627        break;
628    }
629
630    cout<<"Tertiary Interactions are fetched from comparative RNA website [http://www.rna.icmb.utexas.edu]."<<endl
631        <<"The same are located in the file \""<<DataFile<<"\"."<<endl;
632
633    char  buf[256];
634
635    ifstream readData;
636    readData.open(DataFile, ios::in);
637    if (!readData.is_open()) {
638        throw_IO_error(DataFile);
639    }
640
641    int K[100];
642    int R[100];
643    int k, r; k = r = 0;
644
645    while (!readData.eof()) {
646        readData.getline(buf, 100);
647        char *tmp;
648        tmp = strtok(buf, " ");
649        if (tmp != NULL)
650            {
651                if (strcmp(tmp, "KNOT") == 0) {
652                    tmp = strtok(NULL, ":");
653                    while (tmp != NULL) {
654                        K[k++] = atoi(tmp);
655                        tmp = strtok(NULL, ":");
656                    }
657                }
658                else if (strcmp(tmp, "TRIPLE") == 0) {
659                    tmp = strtok(NULL, ":");
660                    while (tmp != NULL) {
661                        R[r++] = atoi(tmp);
662                        tmp = strtok(NULL, ":");
663                    }
664                }
665            }
666    }
667    readData.close();
668
669    glNewList(ECOLI_TERTIARY_INTRACTION_PSEUDOKNOTS, GL_COMPILE);
670    {
671        for (int i = 0; i < k;) {
672            glBegin(GL_LINES);
673            for (int j = 0; j < 2; j++)
674                {
675                    t = start2D3D;
676                    while (t != NULL) {
677                        if (t->pos == K[i]) {
678                            glVertex3f(t->x, t->y, t->z);
679                            i++;
680                            break;
681                        }
682                        t = t->next;
683                    }
684                }
685            glEnd();
686        }
687    }
688    glEndList();
689
690    glNewList(ECOLI_TERTIARY_INTRACTION_TRIPLE_BASES, GL_COMPILE);
691    {
692        for (int i = 0; i < r;) {
693            glBegin(GL_LINE_STRIP);
694            for (int j = 0; j < 3; j++)
695                {
696                    t = start2D3D;
697                    while (t != NULL) {
698                        if (t->pos == R[i]) {
699                            glVertex3f(t->x, t->y, t->z);
700                            i++;
701                            break;
702                        }
703                        t = t->next;
704                    }
705                }
706            glEnd();
707        }
708    }
709    glEndList();
710    free(DataFile);
711}
712
713// ==============================================================================
714
715void Structure3D::StoreHelixNrInfo(float x, float y, float z, int helixNr) {
716    HelixNrInfo *data, *temp;
717    data = new HelixNrInfo;
718    data->helixNr = helixNr;
719    data->x       = x;
720    data->y       = y;
721    data->z       = z;
722    data->next    = NULL;
723
724    if (start == NULL)
725        start = data;
726    else {
727        temp = start;
728        while (temp->next != NULL) {
729            temp = temp->next;
730        }
731        temp->next = data;
732    }
733}
734
735void Structure3D::GenerateHelixDispLists(int HELIX_NR_ID, int HELIX_NR) {
736    Struct2Dinfo *temp2D;
737    Struct2Dplus3D *temp2D3D;
738
739    const int MAX = 500;
740
741    int thisStrandPos[MAX], otherStrandPos[MAX];
742    int i, j; i = j = 0;
743    {
744        temp2D = start2D;
745        while (temp2D != NULL) {
746            if (temp2D->helixNr == HELIX_NR) {
747                if ((temp2D->mask == '[') || (temp2D->mask == '<'))
748                    thisStrandPos[i++]  = temp2D->pos;
749                if ((temp2D->mask == ']') || (temp2D->mask == '>'))
750                    otherStrandPos[j++] = temp2D->pos;
751            }
752            temp2D = temp2D->next;
753        }
754    }
755
756    int tempPos = 0;
757    float x1, x2, y1, y2, z1, z2; x1=x2=y1=y2=z1=z2=0.0;
758
759    bool bThisStrand, bOtherStrand;
760    bThisStrand = bOtherStrand = false;
761
762    bool bMissingBasePair = false;
763
764    glNewList(HELIX_NR_ID, GL_COMPILE);
765    {
766        for (int k = 0, l = j-1; k < i && l >= 0; k++, l--)
767            {
768                tempPos = thisStrandPos[k];
769                {
770                    temp2D3D = start2D3D;
771                    while (temp2D3D != NULL) {
772                        if (temp2D3D->pos == tempPos) {
773                            bThisStrand = true;
774                            x1=temp2D3D->x; y1=temp2D3D->y; z1=temp2D3D->z;
775                            break;
776                        }
777                        temp2D3D = temp2D3D->next;
778                    }
779                }
780                tempPos = otherStrandPos[l];
781                {
782                    temp2D3D = start2D3D;
783                    while (temp2D3D != NULL) {
784                        if (temp2D3D->pos == tempPos) {
785                            bOtherStrand = true;
786                            x2=temp2D3D->x; y2=temp2D3D->y; z2=temp2D3D->z;
787                            break;
788                        }
789                        temp2D3D = temp2D3D->next;
790                    }
791                }
792                // if bases present in both the strands then draw a bond
793                // and store the helix number information
794                if (bThisStrand && bOtherStrand) {
795                    glVertex3f(x1, y1, z1);
796                    glVertex3f(x2, y2, z2);
797
798                    x1 = (x1+x2)/2; y1 = (y1+y2)/2; z1 = (z1+z2)/2;
799                    StoreHelixNrInfo(x1, y1, z1, HELIX_NR);
800
801                    bThisStrand = bOtherStrand = false;
802                }
803                else {
804                    bMissingBasePair = true;
805                    cout<<thisStrandPos[k]<<"-"<<tempPos<<"("<<HELIX_NR_ID<<"), ";
806                }
807            }
808        if (bMissingBasePair) cout<<endl;
809    }
810    glEndList();
811}
812
813void Structure3D::GenerateHelixNrDispList(int startHx, int endHx) {
814    HelixNrInfo *t;
815
816    glNewList(HELIX_NUMBERS, GL_COMPILE);
817    {
818        char POS[50];
819        for (int i = startHx; i <= endHx; i++) {
820            t = start;
821            while (t != NULL) {
822                if (t->helixNr == i) {
823                    sprintf(POS, "%d", t->helixNr);
824                    GRAPHICS->PrintString(t->x, t->y, t->z, POS, GLUT_BITMAP_8_BY_13);
825                }
826                t = t->next;
827            }
828        }
829    }
830    glEndList();
831
832    glNewList(HELIX_NUMBERS_POINTS, GL_COMPILE);
833    {
834        if (RNA3D->bPointSpritesSupported) {
835            glBegin(GL_POINTS);
836        }
837        for (int i = startHx; i <= endHx; i++) {
838            t = start;
839            while (t != NULL) {
840                if (t->helixNr == i) {
841                    PointsToQuads(t->x, t->y, t->z);
842                }
843                t = t->next;
844            }
845        }
846        if (RNA3D->bPointSpritesSupported) {
847            glEnd();
848        }
849    }
850    glEndList();
851}
852
853void Structure3D::GenerateDisplayLists() {
854
855    GenerateMoleculeSkeleton();
856    ComputeBasePositions();
857
858    int rnaType = FindTypeOfRNA();
859    switch (rnaType) {
860    case SSU_16S:
861        if (glIsList(HelixBase) != GL_TRUE)
862            {
863                for (int i = 1; i <= 50; i++) {
864                    GenerateHelixDispLists(HelixBase+i, i);
865                }
866            }
867        break;
868    case LSU_23S:
869        cout<<"=========================================================="<<endl
870            <<"Missing base pairs (bracket number indicates helix number) "<<endl;
871        if (glIsList(HelixBase) != GL_TRUE)
872            {
873                for (int i = 1; i <= 101; i++) {
874                    GenerateHelixDispLists(HelixBase+i, i);
875                }
876            }
877        cout<<"=========================================================="<<endl;
878        break;
879    case LSU_5S:
880        if (glIsList(HelixBase) != GL_TRUE)
881            {
882                for (int i = 1; i <= 5; i++) {
883                    GenerateHelixDispLists(HelixBase+i, i);
884                }
885            }
886        break;
887    }
888
889    GenerateSecStructureHelixRegions();
890    GenerateSecStructureNonHelixRegions();
891    GenerateSecStructureUnpairedHelixRegions();
892
893    GenerateTertiaryInteractionsDispLists();
894}
895
896void Structure3D::GenerateMoleculeSkeleton() {
897    Struct2Dplus3D *t;
898
899    glNewList(STRUCTURE_BACKBONE, GL_COMPILE);
900    {
901        glBegin(GL_LINE_STRIP);
902        t = start2D3D;
903        while (t != NULL) {
904            glVertex3f(t->x, t->y, t->z);
905            t = t->next;
906        }
907        glEnd();
908    }
909    glEndList();
910
911    glNewList(STRUCTURE_BACKBONE_CLR, GL_COMPILE);
912    {
913        glBegin(GL_LINE_STRIP);
914        t = start2D3D;
915        while (t != NULL)
916            {
917                if (t->helixNr > 0) {
918                    if ((t->mask == '[') || (t->mask == ']') ||
919                        (t->mask == '<') || (t->mask == '>'))
920                        {
921                            GRAPHICS->SetColor(RNA3D_GC_BASES_HELIX);
922                            glVertex3f(t->x, t->y, t->z);
923                        }
924                    if (t->mask == '.') {
925                        GRAPHICS->SetColor(RNA3D_GC_BASES_UNPAIRED_HELIX);
926                        glVertex3f(t->x, t->y, t->z);
927                    }
928                }
929                 if (t->helixNr == 0) {
930                     GRAPHICS->SetColor(RNA3D_GC_BASES_NON_HELIX);
931                     glVertex3f(t->x, t->y, t->z);
932                 }
933                 t = t->next;
934            }
935        glEnd();
936    }
937    glEndList();
938}
939
940void Structure3D::GenerateCursorPositionDispList(long pos) {
941    Struct3Dinfo *temp;
942
943    glNewList(ECOLI_CURSOR_POSITION, GL_COMPILE);
944    {
945        glBegin(GL_POINTS);
946        temp = start3D;
947        while (temp != NULL) {
948            if (temp->pos == pos) {
949#ifdef DEBUG
950                cout<<"Cursor Position : "<<pos<<endl;
951#endif
952                glVertex3f(temp->x, temp->y, temp->z);
953                break;
954            }
955            temp = temp->next;
956        }
957        glEnd();
958    }
959    glEndList();
960}
961
962void Structure3D::ComputeBasePositions() {
963    Struct3Dinfo *temp;
964
965    char POS[50];
966    float spacer = 1.5;
967    int posSkip = iInterval;
968    if (posSkip <= 0) posSkip = 25; // default interval value
969
970    glNewList(STRUCTURE_POS, GL_COMPILE);
971    {
972        temp = start3D;
973        while (temp != NULL) {
974            if (temp->pos%posSkip == 0) {
975                sprintf(POS, "%d", temp->pos);
976                GRAPHICS->PrintString(temp->x-spacer, temp->y, temp->z-spacer, POS, GLUT_BITMAP_HELVETICA_10);
977            }
978            temp = temp->next;
979        }
980    }
981    glEndList();
982
983    glNewList(STRUCTURE_POS_ANCHOR, GL_COMPILE);
984    {
985        glLineWidth(0.5);
986        glBegin(GL_LINES);
987        temp = start3D;
988        while (temp != NULL) {
989            if (temp->pos%posSkip == 0) {
990                glVertex3f(temp->x, temp->y, temp->z);
991                glVertex3f(temp->x-spacer, temp->y, temp->z-spacer);
992            }
993            temp = temp->next;
994        }
995        glEnd();
996    }
997    glEndList();
998}
999
1000void Structure3D::PrepareSecondaryStructureData() {
1001//     const char
1002//         outFile[]      = "/nfshome/yadhu/DataBase/Struct3D/model16SrRNA/test.data",
1003//         EcoliFile[]    = "/nfshome/yadhu/DataBase/Struct3D/model16SrRNA/ECOLI_GAPS",
1004//         HelixNrFile[]  = "/nfshome/yadhu/DataBase/Struct3D/model16SrRNA/HELIX_NR",
1005//         HelixGapFile[] = "/nfshome/yadhu/DataBase/Struct3D/model16SrRNA/HELIX_GAP",
1006//         ErrorMsg[] = "\n *** Error Opening File : ";
1007
1008    const char
1009        outFile[]      = "/home/Yadhu/DataBase/Struct3D/model23SrRNA/test.data",
1010        EcoliFile[]    = "/home/Yadhu/DataBase/Struct3D/model23SrRNA/Ecoli_23SrRNA_with_gaps.seq",
1011        HelixNrFile[]  = "/home/Yadhu/DataBase/Struct3D/model23SrRNA/HelixNumbers.msk",
1012        HelixGapFile[] = "/home/Yadhu/DataBase/Struct3D/model23SrRNA/SecStructureMask_with_gaps.msk",
1013        ErrorMsg[] = "\n *** Error Opening File : ";
1014
1015//     const char
1016//         outFile[]      = "/home/Yadhu/DataBase/Struct3D/model5SrRNA/test.data",
1017//         EcoliFile[]    = "/home/Yadhu/DataBase/Struct3D/model5SrRNA/Ecoli_5SrRNA_with_gaps.seq",
1018//         HelixNrFile[]  = "/home/Yadhu/DataBase/Struct3D/model5SrRNA/HelixNumbers.msk",
1019//         HelixGapFile[] = "/home/Yadhu/DataBase/Struct3D/model5SrRNA/SecStructureMask_with_gaps.msk",
1020//         ErrorMsg[] = "\n *** Error Opening File : ";
1021
1022    int fileLen = 0;
1023    char *helixNrBuf, *helixGapBuf, *ecoliBuf;
1024
1025    ofstream out;
1026    out.open(outFile, ios::out);
1027    if (!out.is_open())  cerr<<ErrorMsg<<outFile<<endl;
1028
1029    ifstream inFile;
1030    {
1031        {
1032            inFile.open(EcoliFile, ios::binary);
1033            if (!inFile.is_open())  cerr<<ErrorMsg<<HelixGapFile<<endl;
1034
1035            inFile.seekg (0, ios::end);  // get length of file
1036            fileLen = inFile.tellg();
1037            inFile.seekg (0, ios::beg);
1038
1039            ecoliBuf = new char[fileLen];    // allocate memory:
1040
1041            inFile.read (ecoliBuf, fileLen);    // read data as a block:
1042            inFile.close();
1043        }
1044
1045        {
1046            inFile.open(HelixNrFile, ios::binary);
1047            if (!inFile.is_open())  cerr<<ErrorMsg<<HelixGapFile<<endl;
1048
1049            inFile.seekg (0, ios::end);  // get length of file
1050            fileLen = inFile.tellg();
1051            inFile.seekg (0, ios::beg);
1052
1053            helixNrBuf = new char[fileLen];    // allocate memory:
1054
1055            inFile.read (helixNrBuf, fileLen);    // read data as a block:
1056            inFile.close();
1057        }
1058
1059        {
1060            inFile.open(HelixGapFile, ios::binary);
1061            if (!inFile.is_open())  cerr<<ErrorMsg<<HelixNrFile<<endl;
1062
1063            inFile.seekg (0, ios::end);  // get length of file
1064            fileLen = inFile.tellg();
1065            inFile.seekg (0, ios::beg);
1066
1067            helixGapBuf = new char[fileLen];    // allocate memory:
1068
1069            inFile.read (helixGapBuf, fileLen);    // read data as a block:
1070            inFile.close();
1071        }
1072
1073        char helixNr[4]; helixNr[3] = '\0';
1074        int pos, skip, gaps, k;
1075        pos = skip = gaps = k = 0;
1076
1077        for (unsigned int i = 0; i < strlen(ecoliBuf); i++)
1078            {
1079                if (ecoliBuf[i] == '\n')    skip++;
1080                else {
1081                    if ((ecoliBuf[i] == '.') || (ecoliBuf[i] == '-'))  gaps++;
1082                    else
1083                        {
1084                            pos = (i - (skip + gaps)) + 1;
1085                            out<<pos<<"  "<<ecoliBuf[i]<<"  "<<helixGapBuf[i]<<"  ";
1086                            switch (helixGapBuf[i])
1087                                {
1088                                case '.': out<<"N  "; break;
1089                                case '[': out<<"S  "; break;
1090                                case ']': out<<"E  "; break;
1091                                case '<': out<<"H  "; break;
1092                                case '>': out<<"H  "; break;
1093                                }
1094                            for (unsigned int j = i; helixNrBuf[j] != '.'; j++) {
1095                                helixNr[k++] = helixNrBuf[j];
1096                            }
1097                            for (int l = k; l < 4; l++)  helixNr[l] = '\0';
1098                            k = 0;
1099                            if ((helixGapBuf[i] == '[') || (helixGapBuf[i] == ']')) {
1100                                out<<helixNr;
1101                            }
1102                            out<<endl;
1103                        }
1104                }
1105            }
1106        delete [] helixNrBuf;
1107        delete [] ecoliBuf;
1108        delete [] helixGapBuf;
1109        out.close();
1110    }
1111}
1112
1113void Structure3D::StoreCurrSpeciesDifference(char base, int pos) {
1114    Struct2Dplus3D *st;
1115    st = start2D3D;
1116    while (st != NULL) {
1117        if (st->pos == pos) {
1118            CurrSpecies *data, *temp;
1119            data = new CurrSpecies;
1120            data->base = base;
1121            data->pos  = pos;
1122            data->x = st->x;
1123            data->y = st->y;
1124            data->z = st->z;
1125            data->next = NULL;
1126
1127            if (startSp == NULL) {
1128                startSp = data;
1129                bOldSpeciesDataExists = true;
1130            }
1131            else {
1132                temp = startSp;
1133                while (temp->next != NULL) {
1134                    temp = temp->next;
1135                }
1136                temp->next = data;
1137            }
1138            break;
1139        }
1140        st = st->next;
1141    }
1142}
1143
1144void Structure3D::DeleteOldSpeciesData() {
1145    CurrSpecies *tmp, *data;
1146
1147    for (data = startSp; data != NULL; data = tmp) {
1148        tmp = data->next;
1149        delete data;
1150    }
1151    startSp = NULL;
1152
1153    bOldSpeciesDataExists = false;
1154}
1155
1156void Structure3D::GenerateBaseDifferencePositionDisplayList() {
1157    CurrSpecies *t;
1158    Struct3Dinfo *temp;
1159
1160    char POS[50];
1161    float spacer = 1.5;
1162
1163    glNewList(MAP_SPECIES_BASE_DIFFERENCE_POS, GL_COMPILE);
1164    {
1165        for (t = startSp; t != NULL; t = t->next)
1166            {
1167                for (temp = start3D; temp != NULL; temp = temp->next)
1168                    {
1169                        if (temp->pos == t->pos) {
1170                            sprintf(POS, "%d", temp->pos);
1171                            GRAPHICS->PrintString(temp->x-spacer, temp->y, temp->z-spacer, POS, GLUT_BITMAP_8_BY_13);
1172                        }
1173                    }
1174            }
1175    }
1176    glEndList();
1177
1178    glNewList(MAP_SPECIES_BASE_DIFFERENCE_POS_ANCHOR, GL_COMPILE);
1179    {
1180        glLineWidth(1.0);
1181        glBegin(GL_LINES);
1182
1183        for (t = startSp; t != NULL; t = t->next)
1184            {
1185                for (temp = start3D; temp != NULL; temp = temp->next)
1186                    {
1187                        if (temp->pos == t->pos) {
1188                            glVertex3f(temp->x, temp->y, temp->z);
1189                            glVertex3f(temp->x-spacer, temp->y, temp->z-spacer);
1190                        }
1191                    }
1192            }
1193        glEnd();
1194    }
1195    glEndList();
1196}
1197
1198void Structure3D::BuildDisplayList(int listID, int *pos, int len) {
1199    CurrSpecies *t;
1200    int tmpPos = 0;
1201
1202    glNewList(listID, GL_COMPILE);
1203    {
1204        if (RNA3D->bPointSpritesSupported) {
1205            glBegin(GL_POINTS);
1206        }
1207        for (int i = 0; i < len; i++)
1208            {
1209                tmpPos = pos[i];
1210                t = startSp;
1211                while (t != NULL) {
1212                    if (t->pos == tmpPos) {
1213                        PointsToQuads(t->x, t->y, t->z);
1214                        break;
1215                    }
1216                    t = t->next;
1217                }
1218            }
1219        if (RNA3D->bPointSpritesSupported) {
1220            glEnd();
1221        }
1222    }
1223    glEndList();
1224}
1225
1226void Structure3D::GenerateBaseDifferenceDisplayList() {
1227    CurrSpecies *t;
1228
1229    glNewList(MAP_SPECIES_BASE_DIFFERENCE, GL_COMPILE);
1230    {
1231        if (RNA3D->bPointSpritesSupported) {
1232            glBegin(GL_POINTS);
1233        }
1234        t = startSp;
1235        while (t != NULL) {
1236            PointsToQuads(t->x, t->y, t->z);
1237            t = t->next;
1238        }
1239        if (RNA3D->bPointSpritesSupported) {
1240            glEnd();
1241        }
1242    }
1243    glEndList();
1244
1245    const int MAX_BASE = 400;
1246    int baseA[MAX_BASE], baseG[MAX_BASE], baseC[MAX_BASE],
1247        baseU[MAX_BASE], deletion[MAX_BASE], miss[MAX_BASE];
1248    int a, g, c, u, d, m; a=g=c=u=d=m=0;
1249
1250    {
1251        t = startSp;
1252        while (t != NULL) {
1253            switch (t->base)
1254                {
1255                case 'A': baseA[a++]     = t->pos; break;
1256                case 'G': baseG[g++]     = t->pos; break;
1257                case 'C': baseC[c++]     = t->pos; break;
1258                case 'U': baseU[u++]     = t->pos; break;
1259                case '-': deletion[d++] = t->pos; break;
1260                case '.': miss[m++] = t->pos; break;
1261                }
1262            t = t->next;
1263        }
1264        BuildDisplayList(MAP_SPECIES_BASE_A, baseA, a);
1265        BuildDisplayList(MAP_SPECIES_BASE_U, baseU, u);
1266        BuildDisplayList(MAP_SPECIES_BASE_G, baseG, g);
1267        BuildDisplayList(MAP_SPECIES_BASE_C, baseC, c);
1268        BuildDisplayList(MAP_SPECIES_DELETION, deletion, d);
1269        BuildDisplayList(MAP_SPECIES_MISSING, miss, m);
1270        GenerateBaseDifferencePositionDisplayList();
1271
1272        iTotalSubs = a+g+c+u;  // Summing up the substitutions/mutations
1273        iTotalDels = d;        // Storing total number of deletions
1274#ifdef DEBUG
1275        cout<<"Substitutions = "<<iTotalSubs<<endl;
1276        cout<<"Deletions     = "<<iTotalDels<<endl;
1277#endif
1278    }
1279}
1280
1281void Structure3D::StoreInsertions(char base, int pos) {
1282    Insertions *data, *temp;
1283    data = new Insertions;
1284    data->base = base;
1285    data->pos  = pos;
1286    data->next = NULL;
1287
1288    if (startIns == NULL) {
1289        startIns = data;
1290        bOldInsertionDataExists = true;
1291    }
1292    else {
1293        temp = startIns;
1294        while (temp->next != NULL) {
1295            temp = temp->next;
1296        }
1297        temp->next = data;
1298    }
1299}
1300
1301void Structure3D::DeleteOldInsertionData() {
1302    Insertions *tmp, *data;
1303
1304    for (data = startIns; data != NULL; data = tmp) {
1305        tmp = data->next;
1306        delete data;
1307    }
1308    startIns = NULL;
1309
1310    bOldInsertionDataExists = false;
1311}
1312
1313void Structure3D::GenerateInsertionDisplayList() {
1314    Insertions   *ins;
1315    Struct3Dinfo *str;
1316    char inserts[500];
1317    int i, cntr;
1318    float spacer = 2.0;
1319    iTotalIns = 0;
1320
1321    glNewList(MAP_SPECIES_INSERTION_BASES, GL_COMPILE);
1322    {
1323        for (str = start3D; str != NULL; str = str->next)
1324        {
1325            i = cntr = 0;
1326            for (ins = startIns; ins != NULL; ins = ins->next)
1327                {
1328                    if (str->pos == ins->pos) {
1329                        inserts[i++] = ins->base;
1330                        cntr++;
1331                    }
1332                }
1333            if (cntr>0) {
1334                inserts[i] = '\0';
1335                char buffer[strlen(inserts) + 10];
1336                sprintf(buffer, "%d:%s", cntr, inserts);
1337                GRAPHICS->PrintString(str->x, str->y+spacer, str->z,
1338                                      buffer, GLUT_BITMAP_8_BY_13);
1339
1340                iTotalIns += cntr; // Summing up the insertions
1341            }
1342        }
1343    }
1344    glEndList();
1345
1346#ifdef DEBUG
1347    cout<<"Insertions = "<<iTotalIns<<endl;
1348#endif
1349
1350    glNewList(MAP_SPECIES_INSERTION_BASES_ANCHOR, GL_COMPILE);
1351    {
1352        glLineWidth(1.0);
1353        glBegin(GL_LINES);
1354
1355        for (str = start3D; str != NULL; str = str->next)
1356        {
1357            for (ins = startIns; ins != NULL; ins = ins->next)
1358                {
1359                    if (str->pos == ins->pos) {
1360                        glVertex3f(str->x, str->y, str->z);
1361                        glVertex3f(str->x, str->y+spacer, str->z);
1362                    }
1363                }
1364        }
1365        glEnd();
1366    }
1367    glEndList();
1368
1369    glNewList(MAP_SPECIES_INSERTION_POINTS, GL_COMPILE);
1370    {
1371        if (RNA3D->bPointSpritesSupported) {
1372            glBegin(GL_POINTS);
1373        }
1374        for (str = start3D; str != NULL; str = str->next)
1375        {
1376            for (ins = startIns; ins != NULL; ins = ins->next)
1377                {
1378                    if (str->pos == ins->pos) {
1379                        PointsToQuads(str->x, str->y, str->z);
1380                        break;
1381                    }
1382                }
1383        }
1384        if (RNA3D->bPointSpritesSupported) {
1385            glEnd();
1386        }
1387    }
1388    glEndList();
1389}
1390
1391void Structure3D::MapCurrentSpeciesToEcoliTemplate(AW_root *awr) {
1392
1393    GB_push_transaction(gb_main);
1394
1395    GBDATA *gbTemplate = GBT_find_SAI(gb_main, "ECOLI");
1396
1397    if (!gbTemplate) {
1398        aw_message("SAI:ECOLI not found");
1399    }
1400    else {
1401        char *pSpeciesName = awr->awar(AWAR_SPECIES_NAME)->read_string();
1402        if (pSpeciesName) {
1403            Host.announce_current_species(pSpeciesName);
1404            GBDATA *gbSpecies = GBT_find_species(gb_main, pSpeciesName);
1405            if (gbSpecies) {
1406                char   *ali_name          = GBT_get_default_alignment(gb_main);
1407                GBDATA *gbAlignment       = GB_entry(gbTemplate, ali_name);
1408                GBDATA *gbTemplateSeqData = gbAlignment ? GB_entry(gbAlignment, "data") : 0;
1409
1410                if (!gbTemplateSeqData) {
1411                    aw_message(GBS_global_string("Mapping impossible, since SAI:ECOLI has no data in alignment '%s'", ali_name));
1412                }
1413                else {
1414                    const char *pTemplateSeqData  = GB_read_char_pntr(gbTemplateSeqData);
1415
1416                    if (!RNA3D->bEColiRefInitialized) {
1417                        EColiRef = new BI_ecoli_ref();
1418                        EColiRef->init(gb_main);
1419                        RNA3D->bEColiRefInitialized = true;
1420                    }
1421
1422                    char buf[100];
1423                    char *pSpFullName = GB_read_string(GB_entry(gbSpecies, "full_name"));
1424                    sprintf(buf, "%s : %s", pSpeciesName, pSpFullName);
1425                    awr->awar(AWAR_3D_SELECTED_SPECIES)->write_string(buf);
1426
1427                    GBDATA *gbSeqData    = GBT_read_sequence(gbSpecies, ali_name);
1428                    const char *pSeqData = GB_read_char_pntr(gbSeqData);
1429
1430                    if (pSeqData && pTemplateSeqData) {
1431                        int iSeqLen = strlen(pTemplateSeqData);
1432
1433                        if (bOldSpeciesDataExists) {
1434                            DeleteOldSpeciesData();
1435                        }
1436
1437                        for (int i = 0, iSeqCount = 0; i<iSeqLen; i++) {
1438                            if ((pTemplateSeqData[i] != '.') && (pTemplateSeqData[i] != '-'))
1439                            {
1440                                if (!bStartPosStored) {
1441                                    iStartPos = i;
1442                                    bStartPosStored = true;
1443                                }
1444                                if (pTemplateSeqData[i] != pSeqData[i]) {
1445                                    StoreCurrSpeciesDifference(pSeqData[i], iSeqCount);
1446                                }
1447                                iSeqCount++;
1448                            }
1449                        }
1450
1451                        for (int i = iSeqLen; i>0; i--) {
1452                            if ((pTemplateSeqData[i] != '.') && (pTemplateSeqData[i] != '-')) {
1453                                if (!bEndPosStored) {
1454                                    iEndPos = i;
1455                                    bEndPosStored = true;
1456                                    break;
1457                                }
1458                            }
1459                        }
1460
1461                        if (bOldInsertionDataExists) {
1462                            DeleteOldInsertionData();
1463                        }
1464
1465                        for (int i = iStartPos, iSeqCount = 0; i < iEndPos; i++) {
1466                            if ((pTemplateSeqData[i] != '.') &&
1467                               (pTemplateSeqData[i] != '-'))
1468                                { // Store EColi base positions : Insertion point !
1469                                    iSeqCount++;
1470                                }
1471
1472                            if ((pTemplateSeqData[i] == '-') &&
1473                               (pSeqData[i]         != '-') &&
1474                               (pSeqData[i]         != '.'))
1475                                {
1476                                    StoreInsertions(pSeqData[i], iSeqCount);
1477                                }
1478                        }
1479                    }
1480                    free(pSpFullName);
1481                }
1482                free(ali_name);
1483            }
1484            GenerateBaseDifferenceDisplayList();
1485            GenerateInsertionDisplayList();
1486        }
1487        free(pSpeciesName);
1488    }
1489    GB_pop_transaction(gb_main);
1490}
1491
1492static bool ValidSearchColor(int iColor, int mode) {
1493    bool isValid = false;
1494
1495    switch (mode) {
1496        case SAI:    isValid = (iColor >= RNA3D_GC_CBACK_0) && (iColor < RNA3D_GC_MAX); break;
1497        case SEARCH: isValid = (iColor >= RNA3D_GC_SBACK_0) && (iColor < RNA3D_GC_MAX); break;
1498    }
1499    return isValid;
1500}
1501
1502void Structure3D::MapSearchStringsToEcoliTemplate(AW_root * /* awr */) {
1503    const char *pSearchColResults = 0;
1504
1505    if (iMapSearch) pSearchColResults = Host.get_search_background(iStartPos, iEndPos);
1506
1507    if (pSearchColResults) {
1508        int iColor = 0;
1509
1510        glNewList(MAP_SEARCH_STRINGS_TO_STRUCTURE, GL_COMPILE);
1511        {
1512            if (RNA3D->bPointSpritesSupported) glBegin(GL_POINTS);
1513            for (int i = iStartPos; i < iEndPos; i++) {
1514                if (RNA3D->bEColiRefInitialized) {
1515                    long absPos   = (long) i;
1516                    long EColiPos = EColiRef->abs_2_rel(absPos);
1517
1518                    for (Struct3Dinfo *t = start3D; t != NULL; t = t->next) {
1519                        if ((t->pos == EColiPos) && (pSearchColResults[i] >= 0)) {
1520                            iColor = pSearchColResults[i] - COLORLINK;
1521
1522                            if (ValidSearchColor(iColor, SEARCH)) {
1523                                RNA3D->cGraphics->SetColor(iColor);
1524                                PointsToQuads(t->x, t->y, t->z);
1525                            }
1526                            break;
1527                        }
1528                    }
1529                }
1530            }
1531            if (RNA3D->bPointSpritesSupported) glEnd();
1532        }
1533        glEndList();
1534
1535        glNewList(MAP_SEARCH_STRINGS_BACKBONE, GL_COMPILE);
1536        {
1537            int iLastClr = 0; int iLastPos = 0; Vector3 vLastPt;
1538            glBegin(GL_LINES);
1539            for (int i = iStartPos; i < iEndPos; i++) {
1540                if (RNA3D->bEColiRefInitialized) {
1541                    long absPos   = (long) i;
1542                    long EColiPos = EColiRef->abs_2_rel(absPos);
1543
1544                    for (Struct3Dinfo *t = start3D; t != NULL; t = t->next) {
1545                        if ((t->pos == EColiPos) && (pSearchColResults[i] >= 0)) {
1546                            iColor = pSearchColResults[i] - COLORLINK;
1547
1548                            if (ValidSearchColor(iColor, SEARCH)) {
1549                                if ((iLastClr == iColor) && (iLastPos == EColiPos-1)) {
1550                                    RNA3D->cGraphics->SetColor(iColor);
1551                                    glVertex3f(vLastPt.x, vLastPt.y, vLastPt.z);
1552                                    glVertex3f(t->x, t->y, t->z);
1553                                }
1554                                iLastPos = EColiPos;
1555                                iLastClr = iColor;
1556                                vLastPt.x = t->x; vLastPt.y = t->y; vLastPt.z = t->z;
1557                            }
1558                            break;
1559                        }
1560                    }
1561                }
1562            }
1563            glEnd();
1564        }
1565        glEndList();
1566
1567        RNA3D->bMapSearchStringsDispListCreated = true;
1568    }
1569    else cout<<"BuildColorString did not get the colors : SAI cannot be Visualized!"<<endl;
1570}
1571
1572void Structure3D::MapSaiToEcoliTemplate() {
1573    const char *pSearchColResults = 0;
1574
1575    if (iMapSAI && Host.SAIs_visualized()) {
1576        pSearchColResults = Host.get_SAI_background(iStartPos, iEndPos); // returns 0 if sth went wrong
1577    }
1578
1579    if (pSearchColResults) {
1580        int iColor = 0;
1581
1582        glNewList(MAP_SAI_TO_STRUCTURE, GL_COMPILE);
1583        {
1584            if (RNA3D->bPointSpritesSupported) glBegin(GL_POINTS);
1585
1586            for (int i = iStartPos; i < iEndPos; i++) {
1587                if (RNA3D->bEColiRefInitialized) {
1588                    long absPos   = (long) i;
1589                    long EColiPos = EColiRef->abs_2_rel(absPos);
1590
1591                    for (Struct3Dinfo *t = start3D; t != NULL; t = t->next) {
1592                        if ((t->pos == EColiPos) && (pSearchColResults[i] >= 0)) {
1593                            iColor = pSearchColResults[i-1] - SAICOLORS;
1594
1595                            if (ValidSearchColor(iColor, SAI)) {
1596                                RNA3D->cGraphics->SetColor(iColor);
1597                                PointsToQuads(t->x, t->y, t->z);
1598                            }
1599                            break;
1600                        }
1601                    }
1602                }
1603            }
1604
1605            if (RNA3D->bPointSpritesSupported) glEnd();
1606        }
1607        glEndList();
1608
1609        RNA3D->bMapSaiDispListCreated = true;
1610    }
1611    else cout<<"ED4_getSaiColorString did not get the colors : SAI cannot be Visualized!"<<endl;
1612}
Note: See TracBrowser for help on using the repository browser.