source: branches/stable/GDE/MUSCLE/src/seqvect.cpp

Last change on this file was 10390, checked in by aboeckma, 11 years ago

added muscle sourcles amd makefile

File size: 6.2 KB
Line 
1#include "muscle.h"
2#include "seqvect.h"
3#include "textfile.h"
4#include "msa.h"
5
6const size_t MAX_FASTA_LINE = 16000;
7
8SeqVect::~SeqVect()
9        {
10        Clear();
11        }
12
13void SeqVect::Clear()
14        {
15        for (size_t n = 0; n < size(); ++n)
16                delete (*this)[n];
17        }
18
19void SeqVect::ToFASTAFile(TextFile &File) const
20        {
21        unsigned uSeqCount = Length();
22        for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
23                {
24                Seq *ptrSeq = at(uSeqIndex);
25                ptrSeq->ToFASTAFile(File);
26                }
27        }
28
29void SeqVect::FromFASTAFile(TextFile &File)
30        {
31        Clear();
32
33        FILE *f = File.GetStdioFile();
34        for (;;)
35                {
36                char *Label;
37                unsigned uLength;
38                char *SeqData = GetFastaSeq(f, &uLength, &Label);
39                if (0 == SeqData)
40                        return;
41                Seq *ptrSeq = new Seq;
42
43                for (unsigned i = 0; i < uLength; ++i)
44                        {
45                        char c = SeqData[i];
46                        ptrSeq->push_back(c);
47                        }
48
49                ptrSeq->SetName(Label);
50                push_back(ptrSeq);
51
52                delete[] SeqData;
53                delete[] Label;
54                }
55        }
56
57void SeqVect::PadToMSA(MSA &msa)
58        {
59        unsigned uSeqCount = Length();
60        if (0 == uSeqCount)
61                {
62                msa.Clear();
63                return;
64                }
65
66        unsigned uLongestSeqLength = 0;
67        for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
68                {
69                Seq *ptrSeq = at(uSeqIndex);
70                unsigned uColCount = ptrSeq->Length();
71                if (uColCount > uLongestSeqLength)
72                        uLongestSeqLength = uColCount;
73                }
74        msa.SetSize(uSeqCount, uLongestSeqLength);
75        for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
76                {
77                Seq *ptrSeq = at(uSeqIndex);
78                msa.SetSeqName(uSeqIndex, ptrSeq->GetName());
79                unsigned uColCount = ptrSeq->Length();
80                unsigned uColIndex;
81                for (uColIndex = 0; uColIndex < uColCount; ++uColIndex)
82                        {
83                        char c = ptrSeq->at(uColIndex);
84                        msa.SetChar(uSeqIndex, uColIndex, c);
85                        }
86                while (uColIndex < uLongestSeqLength)
87                        msa.SetChar(uSeqIndex, uColIndex++, '.');
88                }
89        }
90
91void SeqVect::Copy(const SeqVect &rhs)
92        {
93        clear();
94        unsigned uSeqCount = rhs.Length();
95        for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
96                {
97                Seq *ptrSeq = rhs.at(uSeqIndex);
98                Seq *ptrSeqCopy = new Seq;
99                ptrSeqCopy->Copy(*ptrSeq);
100                push_back(ptrSeqCopy);
101                }
102        }
103
104void SeqVect::StripGaps()
105        {
106        unsigned uSeqCount = Length();
107        for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
108                {
109                Seq *ptrSeq = at(uSeqIndex);
110                ptrSeq->StripGaps();
111                }
112        }
113
114void SeqVect::StripGapsAndWhitespace()
115        {
116        unsigned uSeqCount = Length();
117        for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
118                {
119                Seq *ptrSeq = at(uSeqIndex);
120                ptrSeq->StripGapsAndWhitespace();
121                }
122        }
123
124void SeqVect::ToUpper()
125        {
126        unsigned uSeqCount = Length();
127        for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
128                {
129                Seq *ptrSeq = at(uSeqIndex);
130                ptrSeq->ToUpper();
131                }
132        }
133
134bool SeqVect::FindName(const char *ptrName, unsigned *ptruIndex) const
135        {
136        unsigned uSeqCount = Length();
137        for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
138                {
139                const Seq *ptrSeq = at(uSeqIndex);
140                if (0 == stricmp(ptrSeq->GetName(), ptrName))
141                        {
142                        *ptruIndex = uSeqIndex;
143                        return true;
144                        }
145                }
146        return false;
147        }
148
149void SeqVect::AppendSeq(const Seq &s)
150        {
151        Seq *ptrSeqCopy = new Seq;
152        ptrSeqCopy->Copy(s);
153        push_back(ptrSeqCopy);
154        }
155
156void SeqVect::LogMe() const
157        {
158        unsigned uSeqCount = Length();
159        for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
160                {
161                const Seq *ptrSeq = at(uSeqIndex);
162                ptrSeq->LogMe();
163                }
164        }
165
166const char *SeqVect::GetSeqName(unsigned uSeqIndex) const
167        {
168        assert(uSeqIndex < size());
169        const Seq *ptrSeq = at(uSeqIndex);
170        return ptrSeq->GetName();
171        }
172
173unsigned SeqVect::GetSeqId(unsigned uSeqIndex) const
174        {
175        assert(uSeqIndex < size());
176        const Seq *ptrSeq = at(uSeqIndex);
177        return ptrSeq->GetId();
178        }
179
180unsigned SeqVect::GetSeqIdFromName(const char *Name) const
181        {
182        const unsigned uSeqCount = GetSeqCount();
183        for (unsigned i = 0; i < uSeqCount; ++i)
184                {
185                if (!strcmp(Name, GetSeqName(i)))
186                        return GetSeqId(i);
187                }
188        Quit("SeqVect::GetSeqIdFromName(%s): not found", Name);
189        return 0;
190        }
191
192Seq &SeqVect::GetSeqById(unsigned uId)
193        {
194        const unsigned uSeqCount = GetSeqCount();
195        for (unsigned i = 0; i < uSeqCount; ++i)
196                {
197                if (GetSeqId(i) == uId)
198                        return GetSeq(i);
199                }
200        Quit("SeqVect::GetSeqIdByUd(%d): not found", uId);
201        return (Seq &) *((Seq *) 0);
202        }
203
204unsigned SeqVect::GetSeqLength(unsigned uSeqIndex) const
205        {
206        assert(uSeqIndex < size());
207        const Seq *ptrSeq = at(uSeqIndex);
208        return ptrSeq->Length();
209        }
210
211Seq &SeqVect::GetSeq(unsigned uSeqIndex)
212        {
213        assert(uSeqIndex < size());
214        return *at(uSeqIndex);
215        }
216
217const Seq &SeqVect::GetSeq(unsigned uSeqIndex) const
218        {
219        assert(uSeqIndex < size());
220        return *at(uSeqIndex);
221        }
222
223void SeqVect::SetSeqId(unsigned uSeqIndex, unsigned uId)
224        {
225        assert(uSeqIndex < size());
226        Seq *ptrSeq = at(uSeqIndex);
227        return ptrSeq->SetId(uId);
228        }
229
230ALPHA SeqVect::GuessAlpha() const
231        {
232// If at least MIN_NUCLEO_PCT of the first CHAR_COUNT non-gap
233// letters belong to the nucleotide alphabet, guess nucleo.
234// Otherwise amino.
235        const unsigned CHAR_COUNT = 100;
236        const unsigned MIN_NUCLEO_PCT = 95;
237
238        const unsigned uSeqCount = GetSeqCount();
239        if (0 == uSeqCount)
240                return ALPHA_Amino;
241
242        unsigned uSeqIndex = 0;
243        unsigned uPos = 0;
244        unsigned uSeqLength = GetSeqLength(0);
245        unsigned uDNACount = 0;
246        unsigned uRNACount = 0;
247        unsigned uTotal = 0;
248        const Seq *ptrSeq = &GetSeq(0);
249        for (;;)
250                {
251                while (uPos >= uSeqLength)
252                        {
253                        ++uSeqIndex;
254                        if (uSeqIndex >= uSeqCount)
255                                break;
256                        ptrSeq = &GetSeq(uSeqIndex);
257                        uSeqLength = ptrSeq->Length();
258                        uPos = 0;
259                        }
260                if (uSeqIndex >= uSeqCount)
261                        break;
262                char c = ptrSeq->at(uPos++);
263                if (IsGapChar(c))
264                        continue;
265                if (IsDNA(c))
266                        ++uDNACount;
267                if (IsRNA(c))
268                        ++uRNACount;
269                ++uTotal;
270                if (uTotal >= CHAR_COUNT)
271                        break;
272                }
273        if (uTotal != 0 && ((uDNACount*100)/uTotal) >= MIN_NUCLEO_PCT)
274                return ALPHA_DNA;
275        if (uTotal != 0 && ((uRNACount*100)/uTotal) >= MIN_NUCLEO_PCT)
276                return ALPHA_RNA;
277        return ALPHA_Amino;
278        }
279
280void SeqVect::FixAlpha()
281        {
282        ClearInvalidLetterWarning();
283        unsigned uSeqCount = Length();
284        for (unsigned uSeqIndex = 0; uSeqIndex < uSeqCount; ++uSeqIndex)
285                {
286                Seq *ptrSeq = at(uSeqIndex);
287                ptrSeq->FixAlpha();
288                }
289        ReportInvalidLetters();
290        }
Note: See TracBrowser for help on using the repository browser.