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