]> git.donarmstrong.com Git - mothur.git/blob - sharedlistvector.cpp
speed up rarefaction.shared
[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
238                 random_shuffle(order->begin(), order->end());
239                 order->updateStats();
240                 
241                 return order;
242         }
243         catch(exception& e) {
244                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
245                 exit(1);
246         }
247         catch(...) {
248                 cout << "An unknown error has occurred in the SharedListVector class function getSharedOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
249                 exit(1);
250         }
251
252 }
253 /***********************************************************************/
254 SharedRAbundVector SharedListVector::getSharedRAbundVector(string groupName) {
255         try {
256                 SharedRAbundVector rav(data.size());
257                 string group, names, name;
258                 
259                 for(int i=0;i<numBins;i++){
260                         names = get(i);  
261                         while (names.find_first_of(',') != -1) { 
262                                 name = names.substr(0,names.find_first_of(','));
263                                 names = names.substr(names.find_first_of(',')+1, names.length());
264                                 group = groupmap->getGroup(name);
265                                 if (group == groupName) { //this name is in the group you want the vector for.
266                                         rav.set(i, rav.getAbundance(i) + 1, group);  //i represents what bin you are in
267                                 }
268                         }
269                         
270                         //get last name
271                         groupName = groupmap->getGroup(names);
272                         if (group == groupName) { //this name is in the group you want the vector for.
273                                         rav.set(i, rav.getAbundance(i) + 1, group);  //i represents what bin you are in
274                         }
275                 }
276                 
277                 rav.setLabel(label);
278                 rav.setGroup(groupName);
279                 return rav;
280                 
281         }
282         catch(exception& e) {
283                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
284                 exit(1);
285         }
286         catch(...) {
287                 cout << "An unknown error has occurred in the SharedListVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
288                 exit(1);
289         }
290 }
291 /***********************************************************************/
292 vector<SharedRAbundVector*> SharedListVector::getSharedRAbundVector() {
293         try {
294                 SharedUtil* util;
295                 util = new SharedUtil();
296                 vector<SharedRAbundVector*> lookup;
297                 
298                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
299
300                 for (int i = 0; i < globaldata->Groups.size(); i++) {
301                         SharedRAbundVector* temp = new SharedRAbundVector();
302                         *temp = getSharedRAbundVector(globaldata->Groups[i]);
303                         lookup.push_back(temp);
304                 }
305
306                 return lookup;
307         }
308         catch(exception& e) {
309                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
310                 exit(1);
311         }
312         catch(...) {
313                 cout << "An unknown error has occurred in the SharedListVector class function getSharedRAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
314                 exit(1);
315         }
316         
317 }
318
319 /***********************************************************************/
320 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
321         try { 
322                 SharedSAbundVector sav;
323                 SharedRAbundVector rav;
324                 
325                 rav = this->getSharedRAbundVector(groupName);
326                 sav = rav.getSharedSAbundVector();
327                 
328                 return sav;
329         }
330         catch(exception& e) {
331                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
332                 exit(1);
333         }
334         catch(...) {
335                 cout << "An unknown error has occurred in the SharedListVector class function getSharedSAbundVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
336                 exit(1);
337         }
338 }
339 /***********************************************************************/
340
341 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
342         
343         try {
344                 if(orderMap == NULL){
345                         OrderVector ov;
346                 
347                         for(int i=0;i<data.size();i++){
348                                 int binSize = getNumNames(data[i]);             
349                                 for(int j=0;j<binSize;j++){
350                                         ov.push_back(i);
351                                 }
352                         }
353                         random_shuffle(ov.begin(), ov.end());
354                         ov.setLabel(label);
355                         ov.getNumBins();
356                 
357                         return ov;
358                 
359                 }
360                 else{
361                         OrderVector ov(numSeqs);
362                 
363                         for(int i=0;i<data.size();i++){
364                                 string listOTU = data[i];
365                                 int length = listOTU.size();
366                                 
367                                 string seqName="";
368                         
369                                 for(int j=0;j<length;j++){
370                                 
371                                         if(listOTU[j] != ','){
372                                                 seqName += listOTU[j];
373                                         }
374                                         else{
375                                                 if(orderMap->count(seqName) == 0){
376                                                         cerr << seqName << " not found, check *.names file\n";
377                                                         exit(1);
378                                                 }
379                                         
380                                                 ov.set((*orderMap)[seqName], i);
381                                                 seqName = "";
382                                         }                                               
383                                 }
384                         
385                                 if(orderMap->count(seqName) == 0){
386                                         cerr << seqName << " not found, check *.names file\n";
387                                         exit(1);
388                                 }
389                                 ov.set((*orderMap)[seqName], i);        
390                         }
391                 
392                         ov.setLabel(label);
393                         ov.getNumBins();
394                 
395                         return ov;              
396                 }
397         }
398         catch(exception& e) {
399                 cout << "Standard Error: " << e.what() << " has occurred in the SharedListVector class Function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
400                 exit(1);
401         }
402         catch(...) {
403                 cout << "An unknown error has occurred in the SharedListVector class function getOrderVector. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
404                 exit(1);
405         }
406 }
407
408 /***********************************************************************/
409