]> git.donarmstrong.com Git - mothur.git/blob - listvector.cpp
added mothur.h and fixed includes in many files
[mothur.git] / listvector.cpp
1 /*
2  *  list.cpp
3  *  
4  *
5  *  Created by Pat Schloss on 8/8/08.
6  *  Copyright 2008 Patrick D. Schloss. All rights reserved.
7  *
8  */
9
10 using namespace std;
11
12
13 #include "sabundvector.hpp"
14 #include "rabundvector.hpp"
15 #include "ordervector.hpp"
16 #include "utilities.hpp"
17 #include "listvector.hpp"
18
19
20 /***********************************************************************/
21
22 ListVector::ListVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0){};
23
24 /***********************************************************************/
25
26 ListVector::ListVector(int n):  DataVector(), data(n, "") , maxRank(0), numBins(0), numSeqs(0){};
27
28 /***********************************************************************/
29
30 ListVector::ListVector(string id, vector<string> lv) : DataVector(id), data(lv){
31         try {
32                 for(int i=0;i<data.size();i++){
33                         if(data[i] != ""){
34                                 int binSize = getNumNames(data[i]);
35                                 numBins = i+1;
36                                 if(binSize > maxRank)   {       maxRank = binSize;      }
37                                 numSeqs += binSize;
38                         }
39                 }
40         }
41         catch(exception& e) {
42                 cout << "Standard Error: " << e.what() << " has occurred in the ListVector class Function ListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
43                 exit(1);
44         }
45         catch(...) {
46                 cout << "An unknown error has occurred in the ListVector class function ListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
47                 exit(1);
48         }
49 }
50
51 /**********************************************************************/
52
53 ListVector::ListVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
54         try {
55                 int hold;
56                 f >> label >> hold;
57         
58                 data.assign(hold, "");
59                 string inputData = "";
60         
61                 for(int i=0;i<hold;i++){
62                         f >> inputData;
63                         set(i, inputData);
64                 }
65         }
66         catch(exception& e) {
67                 cout << "Standard Error: " << e.what() << " has occurred in the ListVector class Function ListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
68                 exit(1);
69         }
70         catch(...) {
71                 cout << "An unknown error has occurred in the ListVector class function ListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
72                 exit(1);
73         }
74 }
75
76 /***********************************************************************/
77
78 void ListVector::set(int binNumber, string seqNames){
79         try {
80                 int nNames_old = getNumNames(data[binNumber]);
81                 data[binNumber] = seqNames;
82                 int nNames_new = getNumNames(seqNames);
83         
84                 if(nNames_old == 0)                     {       numBins++;                              }
85                 if(nNames_new == 0)                     {       numBins--;                              }
86                 if(nNames_new > maxRank)        {       maxRank = nNames_new;   }
87         
88                 numSeqs += (nNames_new - nNames_old);
89         }
90         catch(exception& e) {
91                 cout << "Standard Error: " << e.what() << " has occurred in the ListVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
92                 exit(1);
93         }
94         catch(...) {
95                 cout << "An unknown error has occurred in the ListVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
96                 exit(1);
97         }
98 }
99
100 /***********************************************************************/
101
102 string ListVector::get(int index){
103         return data[index];
104 }
105
106 /***********************************************************************/
107
108 void ListVector::push_back(string seqNames){
109         try {
110                 data.push_back(seqNames);
111                 int nNames = getNumNames(seqNames);
112         
113                 numBins++;
114         
115                 if(nNames > maxRank)    {       maxRank = nNames;       }
116         
117                 numSeqs += nNames;
118         }
119         catch(exception& e) {
120                 cout << "Standard Error: " << e.what() << " has occurred in the ListVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
121                 exit(1);
122         }
123         catch(...) {
124                 cout << "An unknown error has occurred in the ListVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
125                 exit(1);
126         }
127 }
128
129 /***********************************************************************/
130
131 void ListVector::resize(int size){
132         data.resize(size);              
133 }
134
135 /***********************************************************************/
136
137 int ListVector::size(){
138         return data.size();
139 }
140 /***********************************************************************/
141
142 void ListVector::clear(){
143         numBins = 0;
144         maxRank = 0;
145         numSeqs = 0;
146         return data.clear();
147         
148 }
149
150 /***********************************************************************/
151
152 void ListVector::print(ostream& output){
153         try {
154                 output << label << '\t' << numBins << '\t';
155         
156                 for(int i=0;i<data.size();i++){
157                         if(data[i] != ""){
158                                 output << data[i] << '\t';
159                         }
160                 }
161                 output << endl;
162         }
163         catch(exception& e) {
164                 cout << "Standard Error: " << e.what() << " has occurred in the ListVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
165                 exit(1);
166         }
167         catch(...) {
168                 cout << "An unknown error has occurred in the ListVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
169                 exit(1);
170         }
171 }
172
173
174 /***********************************************************************/
175
176 RAbundVector ListVector::getRAbundVector(){
177         try {
178                 RAbundVector rav;
179         
180                 for(int i=0;i<data.size();i++){
181                         int binSize = getNumNames(data[i]);
182                         rav.push_back(binSize);
183                 }
184         
185         //  This was here before to output data in a nice format, but it screws up the name mapping steps
186         //      sort(rav.rbegin(), rav.rend());
187         //      
188         //      for(int i=data.size()-1;i>=0;i--){
189         //              if(rav.get(i) == 0){    rav.pop_back(); }
190         //              else{
191         //                      break;
192         //              }
193         //      }
194                 rav.setLabel(label);
195         
196                 return rav;
197         }
198         catch(exception& e) {
199                 cout << "Standard Error: " << e.what() << " has occurred in the ListVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
200                 exit(1);
201         }
202         catch(...) {
203                 cout << "An unknown error has occurred in the ListVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
204                 exit(1);
205         }
206 }
207
208 /***********************************************************************/
209
210 SAbundVector ListVector::getSAbundVector(){
211         try {
212                 SAbundVector sav(maxRank+1);
213         
214                 for(int i=0;i<data.size();i++){
215                         int binSize = getNumNames(data[i]);     
216                         sav.set(binSize, sav.get(binSize) + 1); 
217                 }
218                 sav.set(0, 0);
219                 sav.setLabel(label);
220         
221                 return sav;
222         }
223         catch(exception& e) {
224                 cout << "Standard Error: " << e.what() << " has occurred in the ListVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
225                 exit(1);
226         }
227         catch(...) {
228                 cout << "An unknown error has occurred in the ListVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
229                 exit(1);
230         }
231 }
232
233 /***********************************************************************/
234
235 OrderVector ListVector::getOrderVector(map<string,int>* orderMap = NULL){
236         
237         try {
238                 if(orderMap == NULL){
239                         OrderVector ov;
240                 
241                         for(int i=0;i<data.size();i++){
242                                 int binSize = getNumNames(data[i]);             
243                                 for(int j=0;j<binSize;j++){
244                                         ov.push_back(i);
245                                 }
246                         }
247                         random_shuffle(ov.begin(), ov.end());
248                         ov.setLabel(label);
249                         ov.getNumBins();
250                 
251                         return ov;
252                 
253                 }
254                 else{
255                         OrderVector ov(numSeqs);
256                 
257                         for(int i=0;i<data.size();i++){
258                                 string listOTU = data[i];
259                                 int length = listOTU.size();
260                                 
261                                 string seqName="";
262                         
263                                 for(int j=0;j<length;j++){
264                                 
265                                         if(listOTU[j] != ','){
266                                                 seqName += listOTU[j];
267                                         }
268                                         else{
269                                                 if(orderMap->count(seqName) == 0){
270                                                         cerr << seqName << " not found, check *.names file\n";
271                                                         exit(1);
272                                                 }
273                                         
274                                                 ov.set((*orderMap)[seqName], i);
275                                                 seqName = "";
276                                         }                                               
277                                 }
278                         
279                                 if(orderMap->count(seqName) == 0){
280                                         cerr << seqName << " not found, check *.names file\n";
281                                         exit(1);
282                                 }
283                                 ov.set((*orderMap)[seqName], i);        
284                         }
285                 
286                         ov.setLabel(label);
287                         ov.getNumBins();
288                 
289                         return ov;              
290                 }
291         }
292         catch(exception& e) {
293                 cout << "Standard Error: " << e.what() << " has occurred in the ListVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
294                 exit(1);
295         }
296         catch(...) {
297                 cout << "An unknown error has occurred in the ListVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
298                 exit(1);
299         }
300 }
301
302 /***********************************************************************/