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