]> git.donarmstrong.com Git - mothur.git/blob - sharedlistvector.cpp
fixed some bugs
[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 #include "sabundvector.hpp"
11 #include "rabundvector.hpp"
12 #include "ordervector.hpp"
13 #include "sharedlistvector.h"
14 #include "sharedordervector.h"
15 #include "sharedutilities.h"
16
17 /***********************************************************************/
18
19 SharedListVector::SharedListVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0){}
20
21 /***********************************************************************/
22
23 SharedListVector::SharedListVector(int n):      DataVector(), data(n, "") , maxRank(0), numBins(0), numSeqs(0){}
24
25 /***********************************************************************/
26 SharedListVector::SharedListVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
27         try {
28                 globaldata = GlobalData::getInstance();
29
30                 //set up groupmap for later.
31                 groupmap = new GroupMap(globaldata->getGroupFile());
32                 groupmap->readMap();
33
34                 int hold;
35                 string inputData;
36                 f >> label >> hold;
37         
38                 data.assign(hold, "");
39         
40                 for(int i=0;i<hold;i++){
41                         f >> inputData;
42                         set(i, inputData);
43                 }
44         
45         }
46         catch(exception& e) {
47                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function SharedListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
48                 exit(1);
49         }
50         catch(...) {
51                 cout << "An unknown error has occurred in the SharedListVector class function SharedListVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
52                 exit(1);
53         }
54 }
55
56 /***********************************************************************/
57 void SharedListVector::set(int binNumber, string seqNames){
58         try {
59                 int nNames_old = getNumNames(data[binNumber]);
60                 data[binNumber] = seqNames;
61                 int nNames_new = getNumNames(seqNames);
62         
63                 if(nNames_old == 0)                     {       numBins++;                              }
64                 if(nNames_new == 0)                     {       numBins--;                              }
65                 if(nNames_new > maxRank)        {       maxRank = nNames_new;   }
66         
67                 numSeqs += (nNames_new - nNames_old);
68         }
69         catch(exception& e) {
70                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
71                 exit(1);
72         }
73         catch(...) {
74                 cout << "An unknown error has occurred in the SharedListVector class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
75                 exit(1);
76         }
77 }
78
79 /***********************************************************************/
80
81 string SharedListVector::get(int index){
82         return data[index];
83 }
84
85 /***********************************************************************/
86
87 void SharedListVector::push_back(string seqNames){
88         try {
89                 data.push_back(seqNames);
90                 int nNames = getNumNames(seqNames);
91         
92                 numBins++;
93         
94                 if(nNames > maxRank)    {       maxRank = nNames;       }
95         
96                 numSeqs += nNames;
97         }
98         catch(exception& e) {
99                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
100                 exit(1);
101         }
102         catch(...) {
103                 cout << "An unknown error has occurred in the SharedListVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
104                 exit(1);
105         }
106 }
107
108 /***********************************************************************/
109
110 void SharedListVector::resize(int size){
111         data.resize(size);              
112 }
113
114 /***********************************************************************/
115
116 int SharedListVector::size(){
117         return data.size();
118 }
119 /***********************************************************************/
120
121 void SharedListVector::clear(){
122         numBins = 0;
123         maxRank = 0;
124         numSeqs = 0;
125         return data.clear();
126         
127 }
128
129 /***********************************************************************/
130
131 void SharedListVector::print(ostream& output){
132         try {
133                 output << label << '\t' << numBins << '\t';
134         
135                 for(int i=0;i<data.size();i++){
136                         if(data[i] != ""){
137                                 output << data[i] << '\t';
138                         }
139                 }
140                 output << endl;
141         }
142         catch(exception& e) {
143                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
144                 exit(1);
145         }
146         catch(...) {
147                 cout << "An unknown error has occurred in the SharedListVector class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
148                 exit(1);
149         }
150 }
151
152
153 /***********************************************************************/
154
155 RAbundVector SharedListVector::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                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
179                 exit(1);
180         }
181         catch(...) {
182                 cout << "An unknown error has occurred in the SharedListVector class function getRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
183                 exit(1);
184         }
185 }
186
187 /***********************************************************************/
188
189 SAbundVector SharedListVector::getSAbundVector(){
190         try {
191                 SAbundVector sav(maxRank+1);
192         
193                 for(int i=0;i<data.size();i++){
194                         int binSize = getNumNames(data[i]);     
195                         sav.set(binSize, sav.get(binSize) + 1); 
196                 }
197                 sav.set(0, 0);
198                 sav.setLabel(label);
199         
200                 return sav;
201         }
202         catch(exception& e) {
203                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
204                 exit(1);
205         }
206         catch(...) {
207                 cout << "An unknown error has occurred in the SharedListVector class function getSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
208                 exit(1);
209         }
210 }
211
212 /***********************************************************************/
213 SharedOrderVector* SharedListVector::getSharedOrderVector(){
214         try {
215                 string groupName, names, name;
216         
217                 SharedOrderVector* order = new SharedOrderVector();
218                 order->setLabel(label);
219         
220                 for(int i=0;i<numBins;i++){
221                         int binSize = getNumNames(get(i));      //find number of individual in given bin        
222                         names = get(i);
223                         while (names.find_first_of(',') != -1) { 
224                                 name = names.substr(0,names.find_first_of(','));
225                                 names = names.substr(names.find_first_of(',')+1, names.length());
226                                 groupName = groupmap->getGroup(name);
227                                 order->push_back(i, binSize, groupName);  //i represents what bin you are in
228                         }
229                         //get last name
230                         groupName = groupmap->getGroup(names);
231                         order->push_back(i, binSize, groupName);
232                 }
233
234                 random_shuffle(order->begin(), order->end());
235                 order->updateStats();
236                 
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 vector<SharedRAbundVector*> SharedListVector::getSharedRAbundVector() {
289         try {
290                 SharedUtil* util;
291                 util = new SharedUtil();
292                 vector<SharedRAbundVector*> lookup;
293                 
294                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
295
296                 for (int i = 0; i < globaldata->Groups.size(); i++) {
297                         SharedRAbundVector* temp = new SharedRAbundVector();
298                         *temp = getSharedRAbundVector(globaldata->Groups[i]);
299                         lookup.push_back(temp);
300                 }
301
302                 return lookup;
303         }
304         catch(exception& e) {
305                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
306                 exit(1);
307         }
308         catch(...) {
309                 cout << "An unknown error has occurred in the SharedListVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
310                 exit(1);
311         }
312         
313 }
314
315 /***********************************************************************/
316 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
317         try { 
318                 SharedSAbundVector sav;
319                 SharedRAbundVector rav;
320                 
321                 rav = this->getSharedRAbundVector(groupName);
322                 sav = rav.getSharedSAbundVector();
323                 
324                 return sav;
325         }
326         catch(exception& e) {
327                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
328                 exit(1);
329         }
330         catch(...) {
331                 cout << "An unknown error has occurred in the SharedListVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
332                 exit(1);
333         }
334 }
335 /***********************************************************************/
336
337 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
338         
339         try {
340                 if(orderMap == NULL){
341                         OrderVector ov;
342                 
343                         for(int i=0;i<data.size();i++){
344                                 int binSize = getNumNames(data[i]);             
345                                 for(int j=0;j<binSize;j++){
346                                         ov.push_back(i);
347                                 }
348                         }
349                         random_shuffle(ov.begin(), ov.end());
350                         ov.setLabel(label);
351                         ov.getNumBins();
352                 
353                         return ov;
354                 
355                 }
356                 else{
357                         OrderVector ov(numSeqs);
358                 
359                         for(int i=0;i<data.size();i++){
360                                 string listOTU = data[i];
361                                 int length = listOTU.size();
362                                 
363                                 string seqName="";
364                         
365                                 for(int j=0;j<length;j++){
366                                 
367                                         if(listOTU[j] != ','){
368                                                 seqName += listOTU[j];
369                                         }
370                                         else{
371                                                 if(orderMap->count(seqName) == 0){
372                                                         cerr << seqName << " not found, check *.names file\n";
373                                                         exit(1);
374                                                 }
375                                         
376                                                 ov.set((*orderMap)[seqName], i);
377                                                 seqName = "";
378                                         }                                               
379                                 }
380                         
381                                 if(orderMap->count(seqName) == 0){
382                                         cerr << seqName << " not found, check *.names file\n";
383                                         exit(1);
384                                 }
385                                 ov.set((*orderMap)[seqName], i);        
386                         }
387                 
388                         ov.setLabel(label);
389                         ov.getNumBins();
390                 
391                         return ov;              
392                 }
393         }
394         catch(exception& e) {
395                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
396                 exit(1);
397         }
398         catch(...) {
399                 cout << "An unknown error has occurred in the SharedListVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
400                 exit(1);
401         }
402 }
403
404 /***********************************************************************/
405