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