]> git.donarmstrong.com Git - mothur.git/blob - sharedlistvector.cpp
started shared utilities, updates to venn and heatmap added tree.groups command
[mothur.git] / sharedlistvector.cpp
1 /*
2  *  sharedSharedListVector.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 1/22/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10
11 using namespace std;
12
13
14 #include "sabundvector.hpp"
15 #include "rabundvector.hpp"
16 #include "ordervector.hpp"
17 #include "sharedlistvector.h"
18 #include "sharedordervector.h"
19
20 /***********************************************************************/
21
22 SharedListVector::SharedListVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0){};
23
24 /***********************************************************************/
25
26 SharedListVector::SharedListVector(int n):      DataVector(), data(n, "") , maxRank(0), numBins(0), numSeqs(0){};
27
28 /***********************************************************************/
29 SharedListVector::SharedListVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
30         try {
31                 globaldata = GlobalData::getInstance();
32
33                 //set up groupmap for later.
34                 groupmap = new GroupMap(globaldata->getGroupFile());
35                 groupmap->readMap();
36
37                 int hold;
38                 string inputData;
39                 f >> label >> hold;
40         
41                 data.assign(hold, "");
42         
43                 for(int i=0;i<hold;i++){
44                         f >> inputData;
45                         set(i, inputData);
46                 }
47         
48         }
49         catch(exception& e) {
50                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function SharedListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
51                 exit(1);
52         }
53         catch(...) {
54                 cout << "An unknown error has occurred in the SharedListVector class function SharedListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
55                 exit(1);
56         }
57 }
58
59 /***********************************************************************/
60 void SharedListVector::set(int binNumber, string seqNames){
61         try {
62                 int nNames_old = getNumNames(data[binNumber]);
63                 data[binNumber] = seqNames;
64                 int nNames_new = getNumNames(seqNames);
65         
66                 if(nNames_old == 0)                     {       numBins++;                              }
67                 if(nNames_new == 0)                     {       numBins--;                              }
68                 if(nNames_new > maxRank)        {       maxRank = nNames_new;   }
69         
70                 numSeqs += (nNames_new - nNames_old);
71         }
72         catch(exception& e) {
73                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
74                 exit(1);
75         }
76         catch(...) {
77                 cout << "An unknown error has occurred in the SharedListVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
78                 exit(1);
79         }
80 }
81
82 /***********************************************************************/
83
84 string SharedListVector::get(int index){
85         return data[index];
86 }
87
88 /***********************************************************************/
89
90 void SharedListVector::push_back(string seqNames){
91         try {
92                 data.push_back(seqNames);
93                 int nNames = getNumNames(seqNames);
94         
95                 numBins++;
96         
97                 if(nNames > maxRank)    {       maxRank = nNames;       }
98         
99                 numSeqs += nNames;
100         }
101         catch(exception& e) {
102                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
103                 exit(1);
104         }
105         catch(...) {
106                 cout << "An unknown error has occurred in the SharedListVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
107                 exit(1);
108         }
109 }
110
111 /***********************************************************************/
112
113 void SharedListVector::resize(int size){
114         data.resize(size);              
115 }
116
117 /***********************************************************************/
118
119 int SharedListVector::size(){
120         return data.size();
121 }
122 /***********************************************************************/
123
124 void SharedListVector::clear(){
125         numBins = 0;
126         maxRank = 0;
127         numSeqs = 0;
128         return data.clear();
129         
130 }
131
132 /***********************************************************************/
133
134 void SharedListVector::print(ostream& output){
135         try {
136                 output << label << '\t' << numBins << '\t';
137         
138                 for(int i=0;i<data.size();i++){
139                         if(data[i] != ""){
140                                 output << data[i] << '\t';
141                         }
142                 }
143                 output << endl;
144         }
145         catch(exception& e) {
146                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
147                 exit(1);
148         }
149         catch(...) {
150                 cout << "An unknown error has occurred in the SharedListVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
151                 exit(1);
152         }
153 }
154
155
156 /***********************************************************************/
157
158 RAbundVector SharedListVector::getRAbundVector(){
159         try {
160                 RAbundVector rav;
161         
162                 for(int i=0;i<data.size();i++){
163                         int binSize = getNumNames(data[i]);
164                         rav.push_back(binSize);
165                 }
166         
167         //  This was here before to output data in a nice format, but it screws up the name mapping steps
168         //      sort(rav.rbegin(), rav.rend());
169         //      
170         //      for(int i=data.size()-1;i>=0;i--){
171         //              if(rav.get(i) == 0){    rav.pop_back(); }
172         //              else{
173         //                      break;
174         //              }
175         //      }
176                 rav.setLabel(label);
177         
178                 return rav;
179         }
180         catch(exception& e) {
181                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
182                 exit(1);
183         }
184         catch(...) {
185                 cout << "An unknown error has occurred in the SharedListVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
186                 exit(1);
187         }
188 }
189
190 /***********************************************************************/
191
192 SAbundVector SharedListVector::getSAbundVector(){
193         try {
194                 SAbundVector sav(maxRank+1);
195         
196                 for(int i=0;i<data.size();i++){
197                         int binSize = getNumNames(data[i]);     
198                         sav.set(binSize, sav.get(binSize) + 1); 
199                 }
200                 sav.set(0, 0);
201                 sav.setLabel(label);
202         
203                 return sav;
204         }
205         catch(exception& e) {
206                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
207                 exit(1);
208         }
209         catch(...) {
210                 cout << "An unknown error has occurred in the SharedListVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
211                 exit(1);
212         }
213 }
214
215 /***********************************************************************/
216 SharedOrderVector* SharedListVector::getSharedOrderVector(){
217         try {
218                 string groupName, names, name;
219         
220                 SharedOrderVector* order = new SharedOrderVector();
221                 order->setLabel(label);
222         
223                 for(int i=0;i<numBins;i++){
224                         int binSize = getNumNames(get(i));      //find number of individual in given bin        
225                         names = get(i);
226                         while (names.find_first_of(',') != -1) { 
227                                 name = names.substr(0,names.find_first_of(','));
228                                 names = names.substr(names.find_first_of(',')+1, names.length());
229                                 groupName = groupmap->getGroup(name);
230                                 order->push_back(i, binSize, groupName);  //i represents what bin you are in
231                         }
232                         //get last name
233                         groupName = groupmap->getGroup(names);
234                         order->push_back(i, binSize, groupName);
235                 }
236                 random_shuffle(order->begin(), order->end());
237                 return order;
238         }
239         catch(exception& e) {
240                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
241                 exit(1);
242         }
243         catch(...) {
244                 cout << "An unknown error has occurred in the SharedListVector class function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
245                 exit(1);
246         }
247
248 }
249 /***********************************************************************/
250 SharedRAbundVector SharedListVector::getSharedRAbundVector(string groupName) {
251         try {
252                 SharedRAbundVector rav(data.size());
253                 string group, names, name;
254                 
255                 for(int i=0;i<numBins;i++){
256                         names = get(i);  
257                         while (names.find_first_of(',') != -1) { 
258                                 name = names.substr(0,names.find_first_of(','));
259                                 names = names.substr(names.find_first_of(',')+1, names.length());
260                                 group = groupmap->getGroup(name);
261                                 if (group == groupName) { //this name is in the group you want the vector for.
262                                         rav.set(i, rav.getAbundance(i) + 1, group);  //i represents what bin you are in
263                                 }
264                         }
265                         
266                         //get last name
267                         groupName = groupmap->getGroup(names);
268                         if (group == groupName) { //this name is in the group you want the vector for.
269                                         rav.set(i, rav.getAbundance(i) + 1, group);  //i represents what bin you are in
270                         }
271                 }
272                 
273                 rav.setLabel(label);
274                 rav.setGroup(groupName);
275                 return rav;
276                 
277         }
278         catch(exception& e) {
279                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
280                 exit(1);
281         }
282         catch(...) {
283                 cout << "An unknown error has occurred in the SharedListVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
284                 exit(1);
285         }
286 }
287
288 /***********************************************************************/
289 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
290         try { 
291                 SharedSAbundVector sav;
292                 SharedRAbundVector rav;
293                 
294                 rav = this->getSharedRAbundVector(groupName);
295                 sav = rav.getSharedSAbundVector();
296                 
297                 return sav;
298         }
299         catch(exception& e) {
300                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
301                 exit(1);
302         }
303         catch(...) {
304                 cout << "An unknown error has occurred in the SharedListVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
305                 exit(1);
306         }
307 }
308 /***********************************************************************/
309
310 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
311         
312         try {
313                 if(orderMap == NULL){
314                         OrderVector ov;
315                 
316                         for(int i=0;i<data.size();i++){
317                                 int binSize = getNumNames(data[i]);             
318                                 for(int j=0;j<binSize;j++){
319                                         ov.push_back(i);
320                                 }
321                         }
322                         random_shuffle(ov.begin(), ov.end());
323                         ov.setLabel(label);
324                         ov.getNumBins();
325                 
326                         return ov;
327                 
328                 }
329                 else{
330                         OrderVector ov(numSeqs);
331                 
332                         for(int i=0;i<data.size();i++){
333                                 string listOTU = data[i];
334                                 int length = listOTU.size();
335                                 
336                                 string seqName="";
337                         
338                                 for(int j=0;j<length;j++){
339                                 
340                                         if(listOTU[j] != ','){
341                                                 seqName += listOTU[j];
342                                         }
343                                         else{
344                                                 if(orderMap->count(seqName) == 0){
345                                                         cerr << seqName << " not found, check *.names file\n";
346                                                         exit(1);
347                                                 }
348                                         
349                                                 ov.set((*orderMap)[seqName], i);
350                                                 seqName = "";
351                                         }                                               
352                                 }
353                         
354                                 if(orderMap->count(seqName) == 0){
355                                         cerr << seqName << " not found, check *.names file\n";
356                                         exit(1);
357                                 }
358                                 ov.set((*orderMap)[seqName], i);        
359                         }
360                 
361                         ov.setLabel(label);
362                         ov.getNumBins();
363                 
364                         return ov;              
365                 }
366         }
367         catch(exception& e) {
368                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
369                 exit(1);
370         }
371         catch(...) {
372                 cout << "An unknown error has occurred in the SharedListVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
373                 exit(1);
374         }
375 }
376
377 /***********************************************************************/
378