]> git.donarmstrong.com Git - mothur.git/blob - sharedlistvector.cpp
fixing minor 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){globaldata = GlobalData::getInstance();}
20
21 /***********************************************************************/
22
23 SharedListVector::SharedListVector(int n):      DataVector(), data(n, "") , maxRank(0), numBins(0), numSeqs(0){globaldata = GlobalData::getInstance();}
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
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                 
297                 delete util;
298
299                 for (int i = 0; i < globaldata->Groups.size(); i++) {
300                         SharedRAbundVector* temp = new SharedRAbundVector();
301                         *temp = getSharedRAbundVector(globaldata->Groups[i]);
302                         lookup.push_back(temp);
303                 }
304
305                 return lookup;
306         }
307         catch(exception& e) {
308                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
309                 exit(1);
310         }
311         catch(...) {
312                 cout << "An unknown error has occurred in the SharedListVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
313                 exit(1);
314         }
315         
316 }
317
318 /***********************************************************************/
319 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
320         try { 
321                 SharedSAbundVector sav;
322                 SharedRAbundVector rav;
323                 
324                 rav = this->getSharedRAbundVector(groupName);
325                 sav = rav.getSharedSAbundVector();
326                 
327                 return sav;
328         }
329         catch(exception& e) {
330                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
331                 exit(1);
332         }
333         catch(...) {
334                 cout << "An unknown error has occurred in the SharedListVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
335                 exit(1);
336         }
337 }
338 /***********************************************************************/
339
340 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
341         
342         try {
343                 if(orderMap == NULL){
344                         OrderVector ov;
345                 
346                         for(int i=0;i<data.size();i++){
347                                 int binSize = getNumNames(data[i]);             
348                                 for(int j=0;j<binSize;j++){
349                                         ov.push_back(i);
350                                 }
351                         }
352                         random_shuffle(ov.begin(), ov.end());
353                         ov.setLabel(label);
354                         ov.getNumBins();
355                 
356                         return ov;
357                 
358                 }
359                 else{
360                         OrderVector ov(numSeqs);
361                 
362                         for(int i=0;i<data.size();i++){
363                                 string listOTU = data[i];
364                                 int length = listOTU.size();
365                                 
366                                 string seqName="";
367                         
368                                 for(int j=0;j<length;j++){
369                                 
370                                         if(listOTU[j] != ','){
371                                                 seqName += listOTU[j];
372                                         }
373                                         else{
374                                                 if(orderMap->count(seqName) == 0){
375                                                         cerr << seqName << " not found, check *.names file\n";
376                                                         exit(1);
377                                                 }
378                                         
379                                                 ov.set((*orderMap)[seqName], i);
380                                                 seqName = "";
381                                         }                                               
382                                 }
383                         
384                                 if(orderMap->count(seqName) == 0){
385                                         cerr << seqName << " not found, check *.names file\n";
386                                         exit(1);
387                                 }
388                                 ov.set((*orderMap)[seqName], i);        
389                         }
390                 
391                         ov.setLabel(label);
392                         ov.getNumBins();
393                 
394                         return ov;              
395                 }
396         }
397         catch(exception& e) {
398                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
399                 exit(1);
400         }
401         catch(...) {
402                 cout << "An unknown error has occurred in the SharedListVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
403                 exit(1);
404         }
405 }
406
407 /***********************************************************************/
408