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