]> git.donarmstrong.com Git - mothur.git/blob - subsample.cpp
Merge remote-tracking branch 'mothur/master'
[mothur.git] / subsample.cpp
1 //
2 //  subsample.cpp
3 //  Mothur
4 //
5 //  Created by Sarah Westcott on 4/2/12.
6 //  Copyright (c) 2012 Schloss Lab. All rights reserved.
7 //
8
9 #include "subsample.h"
10
11 //**********************************************************************************************************************
12 vector<string> SubSample::getSample(vector<SharedRAbundVector*>& thislookup, int size) {
13         try {
14                 
15                 //save mothurOut's binLabels to restore for next label
16                 vector<string> saveBinLabels = m->currentBinLabels;
17                 
18                 int numBins = thislookup[0]->getNumBins();
19                 for (int i = 0; i < thislookup.size(); i++) {           
20                         int thisSize = thislookup[i]->getNumSeqs();
21                         
22                         if (thisSize != size) {
23                                 
24                                 string thisgroup = thislookup[i]->getGroup();
25                                 
26                                 OrderVector order;
27                                 for(int p=0;p<numBins;p++){
28                                         for(int j=0;j<thislookup[i]->getAbundance(p);j++){
29                                                 order.push_back(p);
30                                         }
31                                 }
32                                 random_shuffle(order.begin(), order.end());
33                                 
34                                 SharedRAbundVector* temp = new SharedRAbundVector(numBins);
35                                 temp->setLabel(thislookup[i]->getLabel());
36                                 temp->setGroup(thislookup[i]->getGroup());
37                                 
38                                 delete thislookup[i];
39                                 thislookup[i] = temp;
40                                 
41                                 
42                                 for (int j = 0; j < size; j++) {
43                                         
44                                         if (m->control_pressed) {  return m->currentBinLabels; }
45                                         
46                                         int bin = order.get(j);
47                                         
48                                         int abund = thislookup[i]->getAbundance(bin);
49                                         thislookup[i]->set(bin, (abund+1), thisgroup);
50                                 }       
51                         }
52                 }
53                 
54                 //subsampling may have created some otus with no sequences in them
55                 eliminateZeroOTUS(thislookup);
56                 
57                 if (m->control_pressed) { return m->currentBinLabels; }
58                 
59                 //save mothurOut's binLabels to restore for next label
60         vector<string> subsampleBinLabels = m->currentBinLabels;
61                 m->currentBinLabels = saveBinLabels;
62                 
63                 return subsampleBinLabels;
64                 
65         }
66         catch(exception& e) {
67                 m->errorOut(e, "SubSample", "getSample");
68                 exit(1);
69         }
70 }       
71 //**********************************************************************************************************************
72 int SubSample::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
73         try {
74                 
75                 vector<SharedRAbundVector*> newLookup;
76                 for (int i = 0; i < thislookup.size(); i++) {
77                         SharedRAbundVector* temp = new SharedRAbundVector();
78                         temp->setLabel(thislookup[i]->getLabel());
79                         temp->setGroup(thislookup[i]->getGroup());
80                         newLookup.push_back(temp);
81                 }
82                 
83                 //for each bin
84                 vector<string> newBinLabels;
85                 string snumBins = toString(thislookup[0]->getNumBins());
86                 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
87                         if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) {  delete newLookup[j];  } return 0; }
88                         
89                         //look at each sharedRabund and make sure they are not all zero
90                         bool allZero = true;
91                         for (int j = 0; j < thislookup.size(); j++) {
92                                 if (thislookup[j]->getAbundance(i) != 0) { allZero = false;  break;  }
93                         }
94                         
95                         //if they are not all zero add this bin
96                         if (!allZero) {
97                                 for (int j = 0; j < thislookup.size(); j++) {
98                                         newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
99                                 }
100                                 //if there is a bin label use it otherwise make one
101                                 string binLabel = "Otu";
102                                 string sbinNumber = toString(i+1);
103                                 if (sbinNumber.length() < snumBins.length()) { 
104                                         int diff = snumBins.length() - sbinNumber.length();
105                                         for (int h = 0; h < diff; h++) { binLabel += "0"; }
106                                 }
107                                 binLabel += sbinNumber; 
108                                 if (i < m->currentBinLabels.size()) {  binLabel = m->currentBinLabels[i]; }
109                                 
110                                 newBinLabels.push_back(binLabel);
111                         }
112                 }
113                 
114                 for (int j = 0; j < thislookup.size(); j++) {  delete thislookup[j];  }
115                 thislookup.clear();
116                 
117                 thislookup = newLookup;
118                 m->currentBinLabels = newBinLabels;
119                 
120                 return 0;
121                 
122         }
123         catch(exception& e) {
124                 m->errorOut(e, "SubSample", "eliminateZeroOTUS");
125                 exit(1);
126         }
127 }
128
129
130 //**********************************************************************************************************************
131
132