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