File: ColorFileXmlParser.cpp

package info (click to toggle)
clustalx 2.1%2Blgpl-8
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,320 kB
  • sloc: cpp: 40,050; sh: 163; xml: 102; makefile: 16
file content (205 lines) | stat: -rw-r--r-- 6,356 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <QtWidgets>

#include "ColorFileXmlParser.h"
#include "ClustalQtParams.h"

ColorFileXmlParser::ColorFileXmlParser(bool showMess)
{
    showMessages = showMess;
}

bool ColorFileXmlParser::read(QIODevice *device)
{
    colorParams.clear();
    
    QString errorStr;
    int errorLine;
    int errorColumn;

    if (!domDocument.setContent(device, true, &errorStr, &errorLine,
                                &errorColumn)) 
    {
        if(showMessages)
        {
            QMessageBox::information(window(), tr("Color File Parser"),
                                 tr("Parse error at line %1, column %2:\n%3")
                                 .arg(errorLine)
                                 .arg(errorColumn)
                                 .arg(errorStr));
        }
        return false;
    }

    QDomElement root = domDocument.documentElement();
    if (root.tagName() != "colorparam") 
    {
        if(showMessages)
        {        
            QMessageBox::information(window(), tr("Color File"),
                                 tr("The file is not a color parameter file."));
        }
        return false;
    }

    int numColors = 0;
    QDomElement rgbIndex = root.firstChildElement("rgbindex");
    if (!rgbIndex.isNull()) 
    {
        numColors = parseRGBIndex(rgbIndex);
    }
    if(numColors == 0)
    {
        // We use the hard coded color index!!!!!
        colorParams.useHardCodedColors();
    }

    QDomElement consensus = root.firstChildElement("consensus");
    if (!consensus.isNull()) // NOTE optional!
    {
        parseConsensus(consensus);
    }
    
    QDomElement colorRules = root.firstChildElement("colorrules");
    int numOfRules = 0;
    if (!colorRules.isNull()) 
    {
        numOfRules = parseColorRule(colorRules);
    }
    if(numOfRules == 0)
    {
        // So we need to use the default colors specified!!!!!!!!!!
        if(showMessages)
        {        
            //QMessageBox::warning(this, tr("Warning"),
            //                     tr("colorrules not found in the parameter file, using\
            //                      defaults!\n"));
        }
        colorParams.useDefaultColorRules();
    }     
    
    return true;
}


int ColorFileXmlParser::parseRGBIndex(const QDomElement &element,
                            QTreeWidgetItem *parentItem)
{
    int numColors = 0;
    QString colorName;
    QString redString, greenString, blueString;
    int red, green, blue;
    bool ok1, ok2, ok3;
    int numColorsSoFar = 0;
    
    // parse the colors!!!!!!!
    QDomElement color = element.firstChildElement("color");
    while (!color.isNull()) 
    {
        numColorsSoFar = colorParams.getNumColors();
                    
        if(numColorsSoFar == MaxColors)
        {
            if(showMessages)
            {
                QMessageBox::warning(this, tr("Warning"),
                                     tr("Max number of colors is %1. Not using the rest!\n")
                                     .arg(MaxColors));
            }
            return numColors;
        }        
        // get each colors information!!!!!!!
        if(color.hasAttribute("name") && color.hasAttribute("red") && 
           color.hasAttribute("green") && color.hasAttribute("blue"))
        {
            colorName = color.attribute("name");            
            redString = color.attribute("red");
            greenString = color.attribute("green");
            blueString = color.attribute("blue");
            
            if(colorName.length() > 0 && redString.length() > 0 && greenString.length() > 0
               && blueString.length() > 0)
            {
                red = redString.toInt(&ok1);
                green = greenString.toInt(&ok2);
                blue = blueString.toInt(&ok3);
                
                if(ok1 && ok2 && ok3)
                {
                    colorParams.addColor(colorName, red, green, blue);
                    numColors++;
                }
            }
        }
        color = color.nextSiblingElement("color");
    }
    return numColors;
}                            

void ColorFileXmlParser::parseConsensus(const QDomElement &element, QTreeWidgetItem *parentItem)
{
    QString conditionNameString;
    QChar name;
    int cutOffPercent;
    QString residues;
    QString temp;
    bool ok;
    
    QDomElement condition = element.firstChildElement("condition");
    while (!condition.isNull()) 
    {
        // get each colors information!!!!!!!
        if(condition.hasAttribute("name") && condition.hasAttribute("cutoffpercent") && 
           condition.hasAttribute("residues"))
        {
            conditionNameString = condition.attribute("name");           
            temp = condition.attribute("cutoffpercent");
            residues = condition.attribute("residues");
            
            if(conditionNameString.length() > 0 && temp.length() > 0 && 
               residues.length() > 0)
            {
                
                name = conditionNameString[0];
                cutOffPercent = temp.toInt(&ok);
                
                if(ok)
                { 
                    colorParams.addConsensusCondition(name, cutOffPercent, residues);
                }
            }
        }
        condition = condition.nextSiblingElement("condition");
    }    
}                            

int ColorFileXmlParser::parseColorRule(const QDomElement &element, QTreeWidgetItem *parentItem)
{
    int numOfRules = 0;
    QString colorName;
    QChar res;
    QString conditions;
    
    QString temp;
    
    QDomElement rule = element.firstChildElement("resrule");
    while (!rule.isNull()) 
    {
        // get each colors information!!!!!!!
        if(rule.hasAttribute("residue") && rule.hasAttribute("colorname") && 
           rule.hasAttribute("conditions"))
        {
            colorName = rule.attribute("colorname");           
            temp = rule.attribute("residue");
            conditions = rule.attribute("conditions");
            
            if(colorName.length() > 0 && temp.length() > 0)
            {
                res = temp[0];
                colorParams.addColorRule(res, colorName, conditions);
                numOfRules++;
            }
        }
        rule = rule.nextSiblingElement("resrule");
    }  
    return numOfRules;  
}