]> git.donarmstrong.com Git - mothur.git/blob - sharedordervector.cpp
added multiple processors option for Windows users to align.seqs, dist.seqs, summary...
[mothur.git] / sharedordervector.cpp
1 /*
2  *  sharedSharedOrderVector.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 12/9/08.
6  *  Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "sharedordervector.h"
11 #include "sharedutilities.h"
12
13 /***********************************************************************/
14
15 SharedOrderVector::SharedOrderVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0)  {}
16
17 /***********************************************************************/
18
19 SharedOrderVector::SharedOrderVector(string id, vector<individual>  ov) : 
20                                                                                         DataVector(id), data(ov)
21 {
22                 updateStats();
23 }
24
25 /***********************************************************************/
26 //This function is used to read a .shared file for the collect.shared, rarefaction.shared and summary.shared commands
27 //if you don't use a list and groupfile.  
28
29 SharedOrderVector::SharedOrderVector(ifstream& f) : DataVector() {  //reads in a shared file
30         try {
31                 maxRank = 0; numBins = 0; numSeqs = 0;
32                                 
33                 groupmap = new GroupMap(); 
34                 
35                 int num, inputData, count;
36                 count = 0;  numSeqs = 0;
37                 string holdLabel, nextLabel, groupN;
38                 individual newguy;
39                 
40                 //read in first row since you know there is at least 1 group.
41                 f >> label >> groupN >> num;
42                 holdLabel = label;
43                 
44                 vector<string> allGroups;
45                 //save group in groupmap
46                 allGroups.push_back(groupN);
47                 groupmap->groupIndex[groupN] = 0;
48                 
49                 
50                 for(int i=0;i<num;i++){
51                         f >> inputData;
52                         
53                         for (int j = 0; j < inputData; j++) {
54                                 push_back(i, i, groupN);
55                                 numSeqs++;
56                         }
57                 }
58                 
59                 m->gobble(f); 
60                 
61                 if (f.eof() != true) { f >> nextLabel; }
62                 
63                 //read the rest of the groups info in
64                 while ((nextLabel == holdLabel) && (f.eof() != true)) {
65                         f >> groupN >> num;
66                         count++;
67                         
68                         
69                         //save group in groupmap
70                         allGroups.push_back(groupN);
71                         groupmap->groupIndex[groupN] = count;
72                         
73                         
74                         for(int i=0;i<num;i++){
75                                 f >> inputData;
76                                 
77                                 for (int j = 0; j < inputData; j++) {
78                                         push_back(i, i, groupN);
79                                         numSeqs++;
80                                 }
81                         }
82                         
83                         m->gobble(f);
84                                 
85                         if (f.eof() != true) { f >> nextLabel; }
86
87                 }
88                 
89                 //put file pointer back since you are now at a new distance label
90                 for (int i = 0; i < nextLabel.length(); i++) { f.unget();  }
91                 
92                 groupmap->setNamesOfGroups(allGroups);
93                 m->setAllGroups(allGroups);
94                 
95                 updateStats();
96                 
97         }
98         catch(exception& e) {
99                 m->errorOut(e, "SharedOrderVector", "SharedOrderVector");
100                 exit(1);
101         }
102 }
103 /***********************************************************************/
104
105 int SharedOrderVector::getNumBins(){
106         return numBins;
107 }
108
109 /***********************************************************************/
110
111 int SharedOrderVector::getNumSeqs(){
112         return numSeqs;
113 }
114
115 /***********************************************************************/
116
117 int SharedOrderVector::getMaxRank(){
118         return maxRank;
119 }
120
121
122 /***********************************************************************/
123
124
125
126 void SharedOrderVector::set(int index, int binNumber, int abund, string groupName){
127         
128         data[index].group = groupName;
129         data[index].bin = binNumber;
130         data[index].abundance = abund;
131         //if (abund > maxRank) { maxRank = abund; }
132         updateStats();
133 }
134
135 /***********************************************************************/
136
137 individual SharedOrderVector::get(int index){
138         return data[index];                     
139 }
140
141
142 /***********************************************************************/
143 //commented updateStats out to improve speed, but whoever calls this must remember to update when they are done with all the pushbacks they are doing 
144 void SharedOrderVector::push_back(int binNumber, int abund, string groupName){
145         individual newGuy;
146         newGuy.group = groupName;
147         newGuy.abundance = abund;
148         newGuy.bin = binNumber;
149         data.push_back(newGuy);
150         //numSeqs++;
151         //numBins++;
152         //if (abund > maxRank) { maxRank = abund; }
153         
154         //updateStats();
155 }
156
157 /***********************************************************************/
158
159 void SharedOrderVector::print(ostream& output){
160         try {
161                 output << label << '\t' << numSeqs << '\t';
162         
163                 for(int i=0;i<data.size();i++){
164                         output << data[i].bin << '\t';
165                 }
166                 output << endl;
167         }
168         catch(exception& e) {
169                 m->errorOut(e, "SharedOrderVector", "print");
170                 exit(1);
171         }
172 }
173
174 /***********************************************************************/
175
176 void SharedOrderVector::clear(){
177         numBins = 0;
178         maxRank = 0;
179         numSeqs = 0;
180         data.clear();
181 }
182 /***********************************************************************/
183
184 void SharedOrderVector::resize(int){
185         m->mothurOut("resize() did nothing in class SharedOrderVector");
186 }
187
188 /***********************************************************************/
189
190
191 vector<individual>::iterator SharedOrderVector::begin(){
192         return data.begin();    
193 }
194
195 /***********************************************************************/
196
197 vector<individual>::iterator SharedOrderVector::end(){
198         return data.end();              
199 }
200
201 /***********************************************************************/
202
203 int SharedOrderVector::size(){
204         return data.size();                                     
205 }
206
207 /***********************************************************************/
208
209 RAbundVector SharedOrderVector::getRAbundVector(){
210         try {
211                 RAbundVector rav(data.size());
212         
213                 for(int i=0;i<numSeqs;i++){
214                         rav.set(data[i].bin, rav.get(data[i].bin) + 1);
215                 }       
216                 sort(rav.rbegin(), rav.rend());
217                 for(int i=numSeqs-1;i>=0;i--){
218                         if(rav.get(i) == 0){    rav.pop_back(); }
219                         else{
220                                 break;
221                         }
222                 }
223                 rav.setLabel(label);
224
225                 return rav;
226         }
227         catch(exception& e) {
228                 m->errorOut(e, "SharedOrderVector", "getRAbundVector");
229                 exit(1);
230         }
231 }
232 /***********************************************************************/
233
234 OrderVector SharedOrderVector::getOrderVector(map<string,int>* nameMap = NULL) {
235         try {
236                 OrderVector ov;
237         
238                 for (int i = 0; i < data.size(); i++) {
239                         ov.push_back(data[i].bin);
240                 }
241                 
242                 random_shuffle(ov.begin(), ov.end());
243
244                 ov.setLabel(label);     
245                 return ov;
246         }
247         catch(exception& e) {
248                 m->errorOut(e, "SharedOrderVector", "getOrderVector");
249                 exit(1);
250         }
251 }
252
253
254 /***********************************************************************/
255
256 SAbundVector SharedOrderVector::getSAbundVector(){
257         
258         RAbundVector rav(this->getRAbundVector());
259         return rav.getSAbundVector();
260
261 }
262 /***********************************************************************/
263 SharedRAbundVector SharedOrderVector::getSharedRAbundVector(string group) {
264         try {
265                 SharedRAbundVector sharedRav(data.size());
266                 
267                 sharedRav.setLabel(label);
268                 sharedRav.setGroup(group);
269                 
270                 for (int i = 0; i < data.size(); i++) {
271                         if (data[i].group == group) {
272                                 sharedRav.set(data[i].abundance, sharedRav.getAbundance(data[i].abundance) + 1, data[i].group);
273                         }
274                 }
275                 return sharedRav;
276         }
277         catch(exception& e) {
278                 m->errorOut(e, "SharedOrderVector", "getSharedRAbundVector");
279                 exit(1);
280         }
281 }
282 /***********************************************************************/
283 vector<SharedRAbundVector*> SharedOrderVector::getSharedRAbundVector() {
284         try {
285                 SharedUtil* util;
286                 util = new SharedUtil();
287                 vector<SharedRAbundVector*> lookup;
288                 
289                 vector<string> Groups = m->getGroups();
290                 vector<string> allGroups = m->getAllGroups();
291                 util->setGroups(Groups, allGroups);
292                 util->getSharedVectors(Groups, lookup, this);
293                 m->setGroups(Groups);
294                 m->setAllGroups(allGroups);
295                 
296                 return lookup;
297         }
298         catch(exception& e) {
299                 m->errorOut(e, "SharedOrderVector", "getSharedRAbundVector");
300                 exit(1);
301         }
302 }
303 /***********************************************************************/
304 SharedSAbundVector SharedOrderVector::getSharedSAbundVector(string group) {
305         try {
306                 
307                 SharedRAbundVector sharedRav(this->getSharedRAbundVector(group));
308                 return sharedRav.getSharedSAbundVector();
309                                 
310         }
311         catch(exception& e) {
312                 m->errorOut(e, "SharedOrderVector", "getSharedSAbundVector");
313                 exit(1);
314         }
315 }
316
317 /***********************************************************************/
318
319 SharedOrderVector SharedOrderVector::getSharedOrderVector(){
320         random_shuffle(data.begin(), data.end());
321         return *this;                   
322 }
323
324 /***********************************************************************/
325
326 void SharedOrderVector::updateStats(){
327         try {
328                 needToUpdate = 0;
329                 numSeqs = 0;
330                 numBins = 0;
331                 maxRank = 0;
332         
333                 numSeqs = data.size();
334                                 
335                 vector<int> hold(numSeqs, 0);
336                 for(int i=0;i<numSeqs;i++){
337                         hold[data[i].bin] = hold[data[i].bin]+1;
338                 }       
339                 
340                 for(int i=0;i<numSeqs;i++){
341                         if(hold[i] > 0)                         {       numBins++;                              }
342                         if(hold[i] > maxRank)           {       maxRank = hold[i];              }
343                 }
344                 
345         }
346         catch(exception& e) {
347                 m->errorOut(e, "SharedOrderVector", "updateStats");
348                 exit(1);
349         }
350 }
351
352 /***********************************************************************/
353
354