]> git.donarmstrong.com Git - mothur.git/blob - sequence.cpp
added alignment code
[mothur.git] / sequence.cpp
1 /*
2  *  sequence.cpp
3  *  
4  *
5  *  Created by Pat Schloss on 12/15/08.
6  *  Copyright 2008 Patrick D. Schloss. All rights reserved.
7  *
8  */
9
10 using namespace std;
11
12 #include "sequence.hpp"
13
14 /***********************************************************************/
15
16 Sequence::Sequence()  {}
17
18 /***********************************************************************/
19
20 Sequence::Sequence(string newName, string sequence) {
21         name = newName;
22         if(sequence.find_first_of('-') != string::npos) {
23                 setAligned(sequence);
24         }
25         setUnaligned(sequence);
26 }
27 //********************************************************************************************************************
28
29 Sequence::Sequence(ifstream& fastaFile){
30         
31         string accession;                               //      provided a file handle to a fasta-formatted sequence file, read in the next
32         fastaFile >> accession;                 //      accession number and sequence we find...
33         setName(accession);
34
35         char letter;
36         string sequence;
37         
38         while(fastaFile){
39                 letter= fastaFile.get();
40                 if(letter == '>'){
41                         fastaFile.putback(letter);
42                         break;
43                 }
44                 else if(isprint(letter)){
45                         letter = toupper(letter);
46                         if(letter == 'U'){letter = 'T';}
47                         sequence += letter;
48                 }
49                 
50         }
51
52         if(sequence.find_first_of('-') != string::npos){        //      if there are any gaps in the sequence, assume that it is
53                 setAligned(sequence);                                                   //      an alignment file
54         }
55         setUnaligned(sequence);                                                         //      also set the unaligned sequence file
56 }
57
58 //********************************************************************************************************************
59
60 string Sequence::convert2ints() {
61         
62         if(unaligned == "")     {       /* need to throw an error */    }
63         
64         string processed;
65         
66         for(int i=0;i<unaligned.length();i++) {
67                 if(toupper(unaligned[i]) == 'A')                        {       processed += '0';       }
68                 else if(toupper(unaligned[i]) == 'C')   {       processed += '1';       }
69                 else if(toupper(unaligned[i]) == 'G')   {       processed += '2';       }
70                 else if(toupper(unaligned[i]) == 'T')   {       processed += '3';       }
71                 else if(toupper(unaligned[i]) == 'U')   {       processed += '3';       }
72                 else                                                                    {       processed += '4';       }
73         }
74         return processed;
75 }
76
77 //********************************************************************************************************************
78
79 void Sequence::setName(string seqName) {
80         if(seqName[0] == '>')   {       name = seqName.substr(1);       }
81         else                                    {       name = seqName;                         }
82 }
83
84 //********************************************************************************************************************
85
86 void Sequence::setUnaligned(string sequence){
87         
88         if(sequence.find_first_of('-') != string::npos) {
89                 string temp = "";
90                 for(int j=0;j<sequence.length();j++) {
91                         if(isalpha(sequence[j]))        {       temp += sequence[j];    }
92                 }
93                 unaligned = temp;
94         }
95         else {
96                 unaligned = sequence;
97         }
98         
99 }
100
101 //********************************************************************************************************************
102
103 void Sequence::setAligned(string sequence){
104         aligned = sequence;
105 }
106
107 //********************************************************************************************************************
108
109 void Sequence::setPairwise(string sequence){
110         pairwise = sequence;
111 }
112
113 //********************************************************************************************************************
114
115 string Sequence::getName(){
116         return name;
117 }
118
119 //********************************************************************************************************************
120
121 string Sequence::getAligned(){
122         return aligned;
123 }
124
125 //********************************************************************************************************************
126
127 string Sequence::getPairwise(){
128         return pairwise;
129 }
130
131 //********************************************************************************************************************
132
133 string Sequence::getUnaligned(){
134         return unaligned;
135 }
136
137 //********************************************************************************************************************
138
139 int Sequence::getLength(){
140         if(unaligned.length() > aligned.length())
141                 return unaligned.length();
142         return aligned.length();
143 }
144
145 //********************************************************************************************************************
146
147 void Sequence::printSequence(ostream& out){
148         string toPrint = unaligned;
149         if(aligned.length() > unaligned.length())
150                 toPrint = aligned;
151         out << ">" << name << "\n" << toPrint << "\n";
152 }
153
154 //********************************************************************************************************************
155
156 int Sequence::getUnalignLength(){
157         return unaligned.length();
158 }
159
160 //********************************************************************************************************************
161
162 int Sequence::getAlignLength(){
163         return aligned.length();
164 }
165
166 //********************************************************************************************************************
167
168
169