File: ColorParameters.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 (273 lines) | stat: -rw-r--r-- 7,015 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "ColorParameters.h"
#include "ClustalQtParams.h"
#include <QColor>
#include <QString>
#include <iostream>
#include "clustalW/general/userparams.h"

ColorParameters::ColorParameters()
{   
    defaultColor = QColor(Qt::white);
}

void ColorParameters::useDefaultColorRules()
{
    bool isDNA = clustalw::userParameters->getDNAFlag();
    
    // Clear the colorPar map
    colorPar.clear();
    QString redRes;
    QString orangeRes;
    QString blueRes;
    QString greenRes;
    
    if(!isDNA)
    {
        // Default simple protein colors!!!!!
        redRes = "krh";
        orangeRes = "gpst";
        blueRes = "fwy";
        greenRes = "ilmv";
    }
    else
    {
        // Default simple DNA colors!!!
        redRes = "a";
        orangeRes = "c";
        blueRes = "tu";
        greenRes = "g";
    }

    // Now add the rules!!!!!!!!!!!!    
    for(int i = 0; i < redRes.length(); i++)
    {
        addColorRule(redRes[i], "RED", "");
    }
    for(int i = 0; i < orangeRes.length(); i++)
    {
        addColorRule(orangeRes[i], "ORANGE", "");
    }
    for(int i = 0; i < blueRes.length(); i++)
    {
        addColorRule(blueRes[i], "BLUE", "");
    }  
    for(int i = 0; i < greenRes.length(); i++)
    {
        addColorRule(greenRes[i], "GREEN", "");
    }          
}

void ColorParameters::addColor(QString colorName, int red, int green, int blue)
{      
    QColor colorToAdd(red, green, blue);
    colors.push_back(std::pair<QString, QColor>(colorName, colorToAdd));
}

void ColorParameters::addConsensusCondition(QChar name, int cutOff, QString res)
{
    consensusParams paramToAdd;
    paramToAdd.consensusName = name;
    paramToAdd.cutOffPercent = cutOff;
    paramToAdd.cutOffList = res;
    paramToAdd.length = res.length();
    conPar.push_back(paramToAdd);
}

void ColorParameters::addColorRule(QChar res, QString colorName, QString conditions)
{
    residueColors paramToAdd;
    paramToAdd.residue = res;
    paramToAdd.consensusConditionList = conditions;
    paramToAdd.length = conditions.length();
    paramToAdd.colorName = colorName;
    
    if(paramToAdd.length > 0)
    {
        paramToAdd.type = Compound;
    }
    else
    {
        paramToAdd.type = Simple;
    }
    
    //std::map<QString, QColor>::iterator colorIterator;
    std::vector<std::pair<QString, QColor> >::iterator colorIterator;
    colorIterator = colors.begin();//colors.find(colorName);
    
    while(colorIterator != colors.end())
    {
        if(colorIterator->first == colorName)
        {
            break;
        }
        colorIterator++;
    }
    
    if(colorIterator == colors.end())
    {
        // If it didnt find the color name in the map!!!!
        paramToAdd.color = QColor(defaultColor);
    }
    else
    {
        paramToAdd.color = QColor(colorIterator->second);
    }
    colorPar.push_back(paramToAdd);
}

void ColorParameters::addAllRulesTogether(std::vector<residueColors> colPar)
{
    // Clear old rules
    colorPar.clear();
    
    // add each color
    std::vector<residueColors>::iterator begin = colPar.begin();
    std::vector<residueColors>::iterator end = colPar.end();
    
    for(; begin != end; begin++)
    {
        addColorRule(begin->residue, begin->colorName, begin->consensusConditionList);
    }
}

/**
 * If we cannot find the @rgbindex tag in the file, we use the default colors.
 * These colors are specified in the HardCodedColorScheme class.
 */
void ColorParameters::useHardCodedColors()
{
    HardCodedColorScheme hardCodedColors;
    colors.clear();
    
    const std::vector<ColorNamePair>* colorScheme = hardCodedColors.getHardCodedScheme();
    
    int numOfColors = colorScheme->size();
    
    for(int i = 0; i< numOfColors; i++)
    {
        colors.push_back(std::pair<QString, QColor>(QString((*colorScheme)[i].name), 
                                                  QColor((*colorScheme)[i].color)));
    }
}

QColor ColorParameters::getResidueColor(QChar res, QChar consensus)
{
    int i, j;
    QChar colorParRes;
    char showRes = res.toLatin1();
    char showCon = consensus.toLatin1();
    
    for(i = 0; i < colorPar.size(); i++)
    {
        colorParRes = colorPar[i].residue;
        if(res.toLower() == colorParRes.toLower())
        {
            if(colorPar[i].type == Simple)
            {
                return colorPar[i].color;
            }
            else if(colorPar[i].type == Compound)
            {
                
                for(j = 0; j < colorPar[i].consensusConditionList.size(); j++)
                {
                    char temp = colorPar[i].consensusConditionList[j].toLatin1();
                    if(consensus == colorPar[i].consensusConditionList[j])
                    {
                        return colorPar[i].color;
                    }
                }
            }
            else
            {
                return defaultColor;
            }
        }
    }
    return defaultColor;
}

int ColorParameters::getResidueColorNum(QChar res, QChar consensus)
{
    int i, j;
    QChar colorParRes;
    char showRes = res.toLatin1();
    char showCon = consensus.toLatin1();
    
    for(i = 0; i < colorPar.size(); i++)
    {
        colorParRes = colorPar[i].residue;
        if(res.toLower() == colorParRes.toLower())
        {
            if(colorPar[i].type == Simple)
            {
                return getIndexOfColor(colorPar[i].color);
            }
            else if(colorPar[i].type == Compound)
            {
                
                for(j = 0; j < colorPar[i].consensusConditionList.size(); j++)
                {
                    char temp = colorPar[i].consensusConditionList[j].toLatin1();
                    if(consensus == colorPar[i].consensusConditionList[j])
                    {
                        return getIndexOfColor(colorPar[i].color);
                    }
                }
            }
            else
            {
                return 0;
            }
        }
    }
    return 0;    
}

void ColorParameters::printColorInfo()
{
    for(int i = 0; i < colors.size(); i++)
    {
        cout << "Color index = " << i + 1 << " Color name is " 
             << colors[i].first.toStdString() << "\n";
    }
}

int ColorParameters::getIndexOfColor(QColor col)
{
    for(int i = 0; i < colors.size(); i++)
    {
        if(colors[i].second == col)
        {
            //cout << "the color is " << colors[i].first.toStdString() << "\n";
            return i + 1;
        }
    }
    return 0; // Not found
}

QColor ColorParameters::getColorFromNum(int colorNum)
{
    if(colorNum > 0 && colorNum - 1 < colorPar.size())
    {
        //cout << "color " << colorNum << " is " 
        //     << colors[colorNum - 1].second
        return colors[colorNum - 1].second;
    }
    else
    {
        return defaultColor;
    }
}

int ColorParameters::getNumConsensusParams()
{
    return conPar.size();
}

void ColorParameters::clear()
{
    colors.clear();
    colorPar.clear();
    conPar.clear();
}