File: Sequence.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 (239 lines) | stat: -rw-r--r-- 4,645 bytes parent folder | download | duplicates (6)
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
/**
 * Author: Mark Larkin
 * 
 * Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.  
 */
#ifdef HAVE_CONFIG_H
    #include "config.h"
#endif
#include <algorithm>
#include "Sequence.h"

using namespace std;

namespace clustalw
{

/**
 * 
 * @param seq 
 * @param name 
 * @param title 
 * @return 
 */
Sequence::Sequence(string& seq, string& name, string& title)
{
    copyStringIntoVector(&_sequence, &seq);
    encodeSequence();
    _name = name;
    _title = title;
    identifier = utilityObject->getUniqueSequenceIdentifier();
}

Sequence::Sequence(std::string& seq, std::string& name, std::string& title, unsigned long id)
{
    copyStringIntoVector(&_sequence, &seq);
    encodeSequence();
    _name = name;
    _title = title;
    identifier = id;
}

/**
 * This is an overloaded constructor that is used to construct a seq object from an
 * encoded sequenced instead of a string.
 * @param encodedSequence 
 * @param name 
 * @param title 
 * @param id The unique identifier from the previous sequence!!!
 * @return 
 */
Sequence::Sequence(std::vector<int>* encodedSequence, std::string& name, std::string& title,
                   unsigned long id)
{
    _encodedSequence = *encodedSequence;
    _name = name;
    _title = title;
    identifier = id;        
}

/**
 * 
 */
void Sequence::encodeSequence()
{
     /* code seq as ints .. use gapPos2 for gap */
    std::vector<char>::iterator it;

    _encodedSequence.push_back(0);
    
    for(it = _sequence.begin(); it != _sequence.end(); ++it) 
    {
        if (*it == '-')
        {
            _encodedSequence.push_back(userParameters->getGapPos2());
        }
        else
        {
            _encodedSequence.push_back(userParameters->resIndex(
                                   userParameters->getAminoAcidCodes(), *it));
        }
    }
}

/**
 * 
 * @param _vectorTo 
 * @param _stringFrom 
 */
void Sequence::copyStringIntoVector(vector<char>* _vectorTo, string* _stringFrom)
{
    _vectorTo->clear();

    for(int i = 0; i < (int)_stringFrom->size(); i++)
    {
        _vectorTo->push_back(_stringFrom->at(i));
    }
    
    if(_vectorTo->size() != _stringFrom->size())
    {
        std::cerr << "Error: In function copyStringIntoVector. Strings different length!\n";
        exit(1);
    }
}

/**
 * 
 */
void Sequence::printSequence()
{
    std::cout << "This is the sequence and the encoded sequence " << _name << std::endl;
    
    std::vector<char>::iterator itChar;    
    for(itChar = _sequence.begin(); itChar != _sequence.end(); ++itChar)
    {
        cout << *itChar;
    } 
    cout << std::endl;
        
    std::vector<int>::iterator itInt;
    for(itInt = _encodedSequence.begin(); itInt != _encodedSequence.end(); ++itInt)
    {
        cout << "  " << *itInt;
    } 
    cout << std::endl;
}

/**
 * 
 */
void Sequence::checkIntegrity()
{
    // The sequences should be the same length.
    if(_sequence.size() != _encodedSequence.size())
    {
        std::cerr << "Error: _sequence is not same size as _encodedSequence\n";
        exit(1);
    }
}

/**
 * 
 * @return the encoded sequence, this is what is used in the pairwise!
 */
std::vector<int>* Sequence::getSequence()
{
    return &_encodedSequence;
}

/**
 * 
 * @return 
 */
std::string Sequence::getName()
{
    return _name;
}

/**
 * 
 * @return 
 */
std::string Sequence::getTitle()
{
    return _title;
}

/**
 * 
 * @return 
 */
bool Sequence::isEmpty()
{
    if(_sequence.size() == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
   
}

/**
 * 
 * @return 
 */
bool Sequence::checkDNAFlag()
// check if DNA or Protein
// The decision is based on counting all A,C,G,T,U or N.
// If >= 85% of all characters (except -) are as above => DNA 
{
    int c, numResidues, numBases;
    float ratio;
    string dna_codes = "ACGTUN";

    numResidues = numBases = 0;
    
    vector<char>::iterator seqIterator = _sequence.begin();
    
    while (seqIterator != _sequence.end())
    {
        if (*seqIterator != '-')
        {
            numResidues++;
            if (*seqIterator == 'N')
            {
                numBases++;
            }
            else
            {
                c = userParameters->resIndex(dna_codes, *seqIterator);
                if (c >= 0)
                {
                    numBases++;
                }
            }
        }
        seqIterator++;
    }
    
    if ((numBases == 0) || (numResidues == 0))
    {
        return false;
    }
    ratio = (float)numBases / (float)numResidues;

    if (ratio >= 0.85)
    {
        return true;
    }
    else
    {
        return false;
    }
}

}