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