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